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 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
463 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
464 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP, size, 2 + endian);
465 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, BPF_REG_D,
468 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
469 *insn++ = BPF_JMP_A(8);
472 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
473 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
474 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
476 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
478 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
480 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
483 switch (BPF_SIZE(fp->code)) {
485 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
488 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
491 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
497 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
498 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
499 *insn = BPF_EXIT_INSN();
506 * bpf_convert_filter - convert filter program
507 * @prog: the user passed filter program
508 * @len: the length of the user passed filter program
509 * @new_prog: allocated 'struct bpf_prog' or NULL
510 * @new_len: pointer to store length of converted program
511 * @seen_ld_abs: bool whether we've seen ld_abs/ind
513 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
514 * style extended BPF (eBPF).
515 * Conversion workflow:
517 * 1) First pass for calculating the new program length:
518 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
520 * 2) 2nd pass to remap in two passes: 1st pass finds new
521 * jump offsets, 2nd pass remapping:
522 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
524 static int bpf_convert_filter(struct sock_filter *prog, int len,
525 struct bpf_prog *new_prog, int *new_len,
528 int new_flen = 0, pass = 0, target, i, stack_off;
529 struct bpf_insn *new_insn, *first_insn = NULL;
530 struct sock_filter *fp;
534 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
535 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
537 if (len <= 0 || len > BPF_MAXINSNS)
541 first_insn = new_prog->insnsi;
542 addrs = kcalloc(len, sizeof(*addrs),
543 GFP_KERNEL | __GFP_NOWARN);
549 new_insn = first_insn;
552 /* Classic BPF related prologue emission. */
554 /* Classic BPF expects A and X to be reset first. These need
555 * to be guaranteed to be the first two instructions.
557 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
558 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
560 /* All programs must keep CTX in callee saved BPF_REG_CTX.
561 * In eBPF case it's done by the compiler, here we need to
562 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
564 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
566 /* For packet access in classic BPF, cache skb->data
567 * in callee-saved BPF R8 and skb->len - skb->data_len
568 * (headlen) in BPF R9. Since classic BPF is read-only
569 * on CTX, we only need to cache it once.
571 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
572 BPF_REG_D, BPF_REG_CTX,
573 offsetof(struct sk_buff, data));
574 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
575 offsetof(struct sk_buff, len));
576 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
577 offsetof(struct sk_buff, data_len));
578 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
584 for (i = 0; i < len; fp++, i++) {
585 struct bpf_insn tmp_insns[32] = { };
586 struct bpf_insn *insn = tmp_insns;
589 addrs[i] = new_insn - first_insn;
592 /* All arithmetic insns and skb loads map as-is. */
593 case BPF_ALU | BPF_ADD | BPF_X:
594 case BPF_ALU | BPF_ADD | BPF_K:
595 case BPF_ALU | BPF_SUB | BPF_X:
596 case BPF_ALU | BPF_SUB | BPF_K:
597 case BPF_ALU | BPF_AND | BPF_X:
598 case BPF_ALU | BPF_AND | BPF_K:
599 case BPF_ALU | BPF_OR | BPF_X:
600 case BPF_ALU | BPF_OR | BPF_K:
601 case BPF_ALU | BPF_LSH | BPF_X:
602 case BPF_ALU | BPF_LSH | BPF_K:
603 case BPF_ALU | BPF_RSH | BPF_X:
604 case BPF_ALU | BPF_RSH | BPF_K:
605 case BPF_ALU | BPF_XOR | BPF_X:
606 case BPF_ALU | BPF_XOR | BPF_K:
607 case BPF_ALU | BPF_MUL | BPF_X:
608 case BPF_ALU | BPF_MUL | BPF_K:
609 case BPF_ALU | BPF_DIV | BPF_X:
610 case BPF_ALU | BPF_DIV | BPF_K:
611 case BPF_ALU | BPF_MOD | BPF_X:
612 case BPF_ALU | BPF_MOD | BPF_K:
613 case BPF_ALU | BPF_NEG:
614 case BPF_LD | BPF_ABS | BPF_W:
615 case BPF_LD | BPF_ABS | BPF_H:
616 case BPF_LD | BPF_ABS | BPF_B:
617 case BPF_LD | BPF_IND | BPF_W:
618 case BPF_LD | BPF_IND | BPF_H:
619 case BPF_LD | BPF_IND | BPF_B:
620 /* Check for overloaded BPF extension and
621 * directly convert it if found, otherwise
622 * just move on with mapping.
624 if (BPF_CLASS(fp->code) == BPF_LD &&
625 BPF_MODE(fp->code) == BPF_ABS &&
626 convert_bpf_extensions(fp, &insn))
628 if (BPF_CLASS(fp->code) == BPF_LD &&
629 convert_bpf_ld_abs(fp, &insn)) {
634 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
635 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
636 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
637 /* Error with exception code on div/mod by 0.
638 * For cBPF programs, this was always return 0.
640 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
641 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
642 *insn++ = BPF_EXIT_INSN();
645 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
648 /* Jump transformation cannot use BPF block macros
649 * everywhere as offset calculation and target updates
650 * require a bit more work than the rest, i.e. jump
651 * opcodes map as-is, but offsets need adjustment.
654 #define BPF_EMIT_JMP \
656 if (target >= len || target < 0) \
658 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
659 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
660 insn->off -= insn - tmp_insns; \
663 case BPF_JMP | BPF_JA:
664 target = i + fp->k + 1;
665 insn->code = fp->code;
669 case BPF_JMP | BPF_JEQ | BPF_K:
670 case BPF_JMP | BPF_JEQ | BPF_X:
671 case BPF_JMP | BPF_JSET | BPF_K:
672 case BPF_JMP | BPF_JSET | BPF_X:
673 case BPF_JMP | BPF_JGT | BPF_K:
674 case BPF_JMP | BPF_JGT | BPF_X:
675 case BPF_JMP | BPF_JGE | BPF_K:
676 case BPF_JMP | BPF_JGE | BPF_X:
677 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
678 /* BPF immediates are signed, zero extend
679 * immediate into tmp register and use it
682 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
684 insn->dst_reg = BPF_REG_A;
685 insn->src_reg = BPF_REG_TMP;
688 insn->dst_reg = BPF_REG_A;
690 bpf_src = BPF_SRC(fp->code);
691 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
694 /* Common case where 'jump_false' is next insn. */
696 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
697 target = i + fp->jt + 1;
702 /* Convert some jumps when 'jump_true' is next insn. */
704 switch (BPF_OP(fp->code)) {
706 insn->code = BPF_JMP | BPF_JNE | bpf_src;
709 insn->code = BPF_JMP | BPF_JLE | bpf_src;
712 insn->code = BPF_JMP | BPF_JLT | bpf_src;
718 target = i + fp->jf + 1;
723 /* Other jumps are mapped into two insns: Jxx and JA. */
724 target = i + fp->jt + 1;
725 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
729 insn->code = BPF_JMP | BPF_JA;
730 target = i + fp->jf + 1;
734 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
735 case BPF_LDX | BPF_MSH | BPF_B: {
736 struct sock_filter tmp = {
737 .code = BPF_LD | BPF_ABS | BPF_B,
744 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
745 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
746 convert_bpf_ld_abs(&tmp, &insn);
749 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
751 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
753 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
755 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
757 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
760 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
761 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
763 case BPF_RET | BPF_A:
764 case BPF_RET | BPF_K:
765 if (BPF_RVAL(fp->code) == BPF_K)
766 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
768 *insn = BPF_EXIT_INSN();
771 /* Store to stack. */
774 stack_off = fp->k * 4 + 4;
775 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
776 BPF_ST ? BPF_REG_A : BPF_REG_X,
778 /* check_load_and_stores() verifies that classic BPF can
779 * load from stack only after write, so tracking
780 * stack_depth for ST|STX insns is enough
782 if (new_prog && new_prog->aux->stack_depth < stack_off)
783 new_prog->aux->stack_depth = stack_off;
786 /* Load from stack. */
787 case BPF_LD | BPF_MEM:
788 case BPF_LDX | BPF_MEM:
789 stack_off = fp->k * 4 + 4;
790 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
791 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
796 case BPF_LD | BPF_IMM:
797 case BPF_LDX | BPF_IMM:
798 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
799 BPF_REG_A : BPF_REG_X, fp->k);
803 case BPF_MISC | BPF_TAX:
804 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
808 case BPF_MISC | BPF_TXA:
809 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
812 /* A = skb->len or X = skb->len */
813 case BPF_LD | BPF_W | BPF_LEN:
814 case BPF_LDX | BPF_W | BPF_LEN:
815 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
816 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
817 offsetof(struct sk_buff, len));
820 /* Access seccomp_data fields. */
821 case BPF_LDX | BPF_ABS | BPF_W:
822 /* A = *(u32 *) (ctx + K) */
823 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
826 /* Unknown instruction. */
833 memcpy(new_insn, tmp_insns,
834 sizeof(*insn) * (insn - tmp_insns));
835 new_insn += insn - tmp_insns;
839 /* Only calculating new length. */
840 *new_len = new_insn - first_insn;
842 *new_len += 4; /* Prologue bits. */
847 if (new_flen != new_insn - first_insn) {
848 new_flen = new_insn - first_insn;
855 BUG_ON(*new_len != new_flen);
864 * As we dont want to clear mem[] array for each packet going through
865 * __bpf_prog_run(), we check that filter loaded by user never try to read
866 * a cell if not previously written, and we check all branches to be sure
867 * a malicious user doesn't try to abuse us.
869 static int check_load_and_stores(const struct sock_filter *filter, int flen)
871 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
874 BUILD_BUG_ON(BPF_MEMWORDS > 16);
876 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
880 memset(masks, 0xff, flen * sizeof(*masks));
882 for (pc = 0; pc < flen; pc++) {
883 memvalid &= masks[pc];
885 switch (filter[pc].code) {
888 memvalid |= (1 << filter[pc].k);
890 case BPF_LD | BPF_MEM:
891 case BPF_LDX | BPF_MEM:
892 if (!(memvalid & (1 << filter[pc].k))) {
897 case BPF_JMP | BPF_JA:
898 /* A jump must set masks on target */
899 masks[pc + 1 + filter[pc].k] &= memvalid;
902 case BPF_JMP | BPF_JEQ | BPF_K:
903 case BPF_JMP | BPF_JEQ | BPF_X:
904 case BPF_JMP | BPF_JGE | BPF_K:
905 case BPF_JMP | BPF_JGE | BPF_X:
906 case BPF_JMP | BPF_JGT | BPF_K:
907 case BPF_JMP | BPF_JGT | BPF_X:
908 case BPF_JMP | BPF_JSET | BPF_K:
909 case BPF_JMP | BPF_JSET | BPF_X:
910 /* A jump must set masks on targets */
911 masks[pc + 1 + filter[pc].jt] &= memvalid;
912 masks[pc + 1 + filter[pc].jf] &= memvalid;
922 static bool chk_code_allowed(u16 code_to_probe)
924 static const bool codes[] = {
925 /* 32 bit ALU operations */
926 [BPF_ALU | BPF_ADD | BPF_K] = true,
927 [BPF_ALU | BPF_ADD | BPF_X] = true,
928 [BPF_ALU | BPF_SUB | BPF_K] = true,
929 [BPF_ALU | BPF_SUB | BPF_X] = true,
930 [BPF_ALU | BPF_MUL | BPF_K] = true,
931 [BPF_ALU | BPF_MUL | BPF_X] = true,
932 [BPF_ALU | BPF_DIV | BPF_K] = true,
933 [BPF_ALU | BPF_DIV | BPF_X] = true,
934 [BPF_ALU | BPF_MOD | BPF_K] = true,
935 [BPF_ALU | BPF_MOD | BPF_X] = true,
936 [BPF_ALU | BPF_AND | BPF_K] = true,
937 [BPF_ALU | BPF_AND | BPF_X] = true,
938 [BPF_ALU | BPF_OR | BPF_K] = true,
939 [BPF_ALU | BPF_OR | BPF_X] = true,
940 [BPF_ALU | BPF_XOR | BPF_K] = true,
941 [BPF_ALU | BPF_XOR | BPF_X] = true,
942 [BPF_ALU | BPF_LSH | BPF_K] = true,
943 [BPF_ALU | BPF_LSH | BPF_X] = true,
944 [BPF_ALU | BPF_RSH | BPF_K] = true,
945 [BPF_ALU | BPF_RSH | BPF_X] = true,
946 [BPF_ALU | BPF_NEG] = true,
947 /* Load instructions */
948 [BPF_LD | BPF_W | BPF_ABS] = true,
949 [BPF_LD | BPF_H | BPF_ABS] = true,
950 [BPF_LD | BPF_B | BPF_ABS] = true,
951 [BPF_LD | BPF_W | BPF_LEN] = true,
952 [BPF_LD | BPF_W | BPF_IND] = true,
953 [BPF_LD | BPF_H | BPF_IND] = true,
954 [BPF_LD | BPF_B | BPF_IND] = true,
955 [BPF_LD | BPF_IMM] = true,
956 [BPF_LD | BPF_MEM] = true,
957 [BPF_LDX | BPF_W | BPF_LEN] = true,
958 [BPF_LDX | BPF_B | BPF_MSH] = true,
959 [BPF_LDX | BPF_IMM] = true,
960 [BPF_LDX | BPF_MEM] = true,
961 /* Store instructions */
964 /* Misc instructions */
965 [BPF_MISC | BPF_TAX] = true,
966 [BPF_MISC | BPF_TXA] = true,
967 /* Return instructions */
968 [BPF_RET | BPF_K] = true,
969 [BPF_RET | BPF_A] = true,
970 /* Jump instructions */
971 [BPF_JMP | BPF_JA] = true,
972 [BPF_JMP | BPF_JEQ | BPF_K] = true,
973 [BPF_JMP | BPF_JEQ | BPF_X] = true,
974 [BPF_JMP | BPF_JGE | BPF_K] = true,
975 [BPF_JMP | BPF_JGE | BPF_X] = true,
976 [BPF_JMP | BPF_JGT | BPF_K] = true,
977 [BPF_JMP | BPF_JGT | BPF_X] = true,
978 [BPF_JMP | BPF_JSET | BPF_K] = true,
979 [BPF_JMP | BPF_JSET | BPF_X] = true,
982 if (code_to_probe >= ARRAY_SIZE(codes))
985 return codes[code_to_probe];
988 static bool bpf_check_basics_ok(const struct sock_filter *filter,
993 if (flen == 0 || flen > BPF_MAXINSNS)
1000 * bpf_check_classic - verify socket filter code
1001 * @filter: filter to verify
1002 * @flen: length of filter
1004 * Check the user's filter code. If we let some ugly
1005 * filter code slip through kaboom! The filter must contain
1006 * no references or jumps that are out of range, no illegal
1007 * instructions, and must end with a RET instruction.
1009 * All jumps are forward as they are not signed.
1011 * Returns 0 if the rule set is legal or -EINVAL if not.
1013 static int bpf_check_classic(const struct sock_filter *filter,
1019 /* Check the filter code now */
1020 for (pc = 0; pc < flen; pc++) {
1021 const struct sock_filter *ftest = &filter[pc];
1023 /* May we actually operate on this code? */
1024 if (!chk_code_allowed(ftest->code))
1027 /* Some instructions need special checks */
1028 switch (ftest->code) {
1029 case BPF_ALU | BPF_DIV | BPF_K:
1030 case BPF_ALU | BPF_MOD | BPF_K:
1031 /* Check for division by zero */
1035 case BPF_ALU | BPF_LSH | BPF_K:
1036 case BPF_ALU | BPF_RSH | BPF_K:
1040 case BPF_LD | BPF_MEM:
1041 case BPF_LDX | BPF_MEM:
1044 /* Check for invalid memory addresses */
1045 if (ftest->k >= BPF_MEMWORDS)
1048 case BPF_JMP | BPF_JA:
1049 /* Note, the large ftest->k might cause loops.
1050 * Compare this with conditional jumps below,
1051 * where offsets are limited. --ANK (981016)
1053 if (ftest->k >= (unsigned int)(flen - pc - 1))
1056 case BPF_JMP | BPF_JEQ | BPF_K:
1057 case BPF_JMP | BPF_JEQ | BPF_X:
1058 case BPF_JMP | BPF_JGE | BPF_K:
1059 case BPF_JMP | BPF_JGE | BPF_X:
1060 case BPF_JMP | BPF_JGT | BPF_K:
1061 case BPF_JMP | BPF_JGT | BPF_X:
1062 case BPF_JMP | BPF_JSET | BPF_K:
1063 case BPF_JMP | BPF_JSET | BPF_X:
1064 /* Both conditionals must be safe */
1065 if (pc + ftest->jt + 1 >= flen ||
1066 pc + ftest->jf + 1 >= flen)
1069 case BPF_LD | BPF_W | BPF_ABS:
1070 case BPF_LD | BPF_H | BPF_ABS:
1071 case BPF_LD | BPF_B | BPF_ABS:
1073 if (bpf_anc_helper(ftest) & BPF_ANC)
1075 /* Ancillary operation unknown or unsupported */
1076 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1081 /* Last instruction must be a RET code */
1082 switch (filter[flen - 1].code) {
1083 case BPF_RET | BPF_K:
1084 case BPF_RET | BPF_A:
1085 return check_load_and_stores(filter, flen);
1091 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1092 const struct sock_fprog *fprog)
1094 unsigned int fsize = bpf_classic_proglen(fprog);
1095 struct sock_fprog_kern *fkprog;
1097 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1101 fkprog = fp->orig_prog;
1102 fkprog->len = fprog->len;
1104 fkprog->filter = kmemdup(fp->insns, fsize,
1105 GFP_KERNEL | __GFP_NOWARN);
1106 if (!fkprog->filter) {
1107 kfree(fp->orig_prog);
1114 static void bpf_release_orig_filter(struct bpf_prog *fp)
1116 struct sock_fprog_kern *fprog = fp->orig_prog;
1119 kfree(fprog->filter);
1124 static void __bpf_prog_release(struct bpf_prog *prog)
1126 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1129 bpf_release_orig_filter(prog);
1130 bpf_prog_free(prog);
1134 static void __sk_filter_release(struct sk_filter *fp)
1136 __bpf_prog_release(fp->prog);
1141 * sk_filter_release_rcu - Release a socket filter by rcu_head
1142 * @rcu: rcu_head that contains the sk_filter to free
1144 static void sk_filter_release_rcu(struct rcu_head *rcu)
1146 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1148 __sk_filter_release(fp);
1152 * sk_filter_release - release a socket filter
1153 * @fp: filter to remove
1155 * Remove a filter from a socket and release its resources.
1157 static void sk_filter_release(struct sk_filter *fp)
1159 if (refcount_dec_and_test(&fp->refcnt))
1160 call_rcu(&fp->rcu, sk_filter_release_rcu);
1163 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1165 u32 filter_size = bpf_prog_size(fp->prog->len);
1167 atomic_sub(filter_size, &sk->sk_omem_alloc);
1168 sk_filter_release(fp);
1171 /* try to charge the socket memory if there is space available
1172 * return true on success
1174 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1176 u32 filter_size = bpf_prog_size(fp->prog->len);
1178 /* same check as in sock_kmalloc() */
1179 if (filter_size <= sysctl_optmem_max &&
1180 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1181 atomic_add(filter_size, &sk->sk_omem_alloc);
1187 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1189 if (!refcount_inc_not_zero(&fp->refcnt))
1192 if (!__sk_filter_charge(sk, fp)) {
1193 sk_filter_release(fp);
1199 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1201 struct sock_filter *old_prog;
1202 struct bpf_prog *old_fp;
1203 int err, new_len, old_len = fp->len;
1204 bool seen_ld_abs = false;
1206 /* We are free to overwrite insns et al right here as it
1207 * won't be used at this point in time anymore internally
1208 * after the migration to the internal BPF instruction
1211 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1212 sizeof(struct bpf_insn));
1214 /* Conversion cannot happen on overlapping memory areas,
1215 * so we need to keep the user BPF around until the 2nd
1216 * pass. At this time, the user BPF is stored in fp->insns.
1218 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1219 GFP_KERNEL | __GFP_NOWARN);
1225 /* 1st pass: calculate the new program length. */
1226 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1231 /* Expand fp for appending the new filter representation. */
1233 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1235 /* The old_fp is still around in case we couldn't
1236 * allocate new memory, so uncharge on that one.
1245 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1246 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1249 /* 2nd bpf_convert_filter() can fail only if it fails
1250 * to allocate memory, remapping must succeed. Note,
1251 * that at this time old_fp has already been released
1256 fp = bpf_prog_select_runtime(fp, &err);
1266 __bpf_prog_release(fp);
1267 return ERR_PTR(err);
1270 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1271 bpf_aux_classic_check_t trans)
1275 fp->bpf_func = NULL;
1278 err = bpf_check_classic(fp->insns, fp->len);
1280 __bpf_prog_release(fp);
1281 return ERR_PTR(err);
1284 /* There might be additional checks and transformations
1285 * needed on classic filters, f.e. in case of seccomp.
1288 err = trans(fp->insns, fp->len);
1290 __bpf_prog_release(fp);
1291 return ERR_PTR(err);
1295 /* Probe if we can JIT compile the filter and if so, do
1296 * the compilation of the filter.
1298 bpf_jit_compile(fp);
1300 /* JIT compiler couldn't process this filter, so do the
1301 * internal BPF translation for the optimized interpreter.
1304 fp = bpf_migrate_filter(fp);
1310 * bpf_prog_create - create an unattached filter
1311 * @pfp: the unattached filter that is created
1312 * @fprog: the filter program
1314 * Create a filter independent of any socket. We first run some
1315 * sanity checks on it to make sure it does not explode on us later.
1316 * If an error occurs or there is insufficient memory for the filter
1317 * a negative errno code is returned. On success the return is zero.
1319 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1321 unsigned int fsize = bpf_classic_proglen(fprog);
1322 struct bpf_prog *fp;
1324 /* Make sure new filter is there and in the right amounts. */
1325 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1328 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1332 memcpy(fp->insns, fprog->filter, fsize);
1334 fp->len = fprog->len;
1335 /* Since unattached filters are not copied back to user
1336 * space through sk_get_filter(), we do not need to hold
1337 * a copy here, and can spare us the work.
1339 fp->orig_prog = NULL;
1341 /* bpf_prepare_filter() already takes care of freeing
1342 * memory in case something goes wrong.
1344 fp = bpf_prepare_filter(fp, NULL);
1351 EXPORT_SYMBOL_GPL(bpf_prog_create);
1354 * bpf_prog_create_from_user - create an unattached filter from user buffer
1355 * @pfp: the unattached filter that is created
1356 * @fprog: the filter program
1357 * @trans: post-classic verifier transformation handler
1358 * @save_orig: save classic BPF program
1360 * This function effectively does the same as bpf_prog_create(), only
1361 * that it builds up its insns buffer from user space provided buffer.
1362 * It also allows for passing a bpf_aux_classic_check_t handler.
1364 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1365 bpf_aux_classic_check_t trans, bool save_orig)
1367 unsigned int fsize = bpf_classic_proglen(fprog);
1368 struct bpf_prog *fp;
1371 /* Make sure new filter is there and in the right amounts. */
1372 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1375 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1379 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1380 __bpf_prog_free(fp);
1384 fp->len = fprog->len;
1385 fp->orig_prog = NULL;
1388 err = bpf_prog_store_orig_filter(fp, fprog);
1390 __bpf_prog_free(fp);
1395 /* bpf_prepare_filter() already takes care of freeing
1396 * memory in case something goes wrong.
1398 fp = bpf_prepare_filter(fp, trans);
1405 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1407 void bpf_prog_destroy(struct bpf_prog *fp)
1409 __bpf_prog_release(fp);
1411 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1413 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1415 struct sk_filter *fp, *old_fp;
1417 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1423 if (!__sk_filter_charge(sk, fp)) {
1427 refcount_set(&fp->refcnt, 1);
1429 old_fp = rcu_dereference_protected(sk->sk_filter,
1430 lockdep_sock_is_held(sk));
1431 rcu_assign_pointer(sk->sk_filter, fp);
1434 sk_filter_uncharge(sk, old_fp);
1439 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1441 struct bpf_prog *old_prog;
1444 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1447 if (sk_unhashed(sk) && sk->sk_reuseport) {
1448 err = reuseport_alloc(sk);
1451 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1452 /* The socket wasn't bound with SO_REUSEPORT */
1456 old_prog = reuseport_attach_prog(sk, prog);
1458 bpf_prog_destroy(old_prog);
1464 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1466 unsigned int fsize = bpf_classic_proglen(fprog);
1467 struct bpf_prog *prog;
1470 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1471 return ERR_PTR(-EPERM);
1473 /* Make sure new filter is there and in the right amounts. */
1474 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1475 return ERR_PTR(-EINVAL);
1477 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1479 return ERR_PTR(-ENOMEM);
1481 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1482 __bpf_prog_free(prog);
1483 return ERR_PTR(-EFAULT);
1486 prog->len = fprog->len;
1488 err = bpf_prog_store_orig_filter(prog, fprog);
1490 __bpf_prog_free(prog);
1491 return ERR_PTR(-ENOMEM);
1494 /* bpf_prepare_filter() already takes care of freeing
1495 * memory in case something goes wrong.
1497 return bpf_prepare_filter(prog, NULL);
1501 * sk_attach_filter - attach a socket filter
1502 * @fprog: the filter program
1503 * @sk: the socket to use
1505 * Attach the user's filter code. We first run some sanity checks on
1506 * it to make sure it does not explode on us later. If an error
1507 * occurs or there is insufficient memory for the filter a negative
1508 * errno code is returned. On success the return is zero.
1510 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1512 struct bpf_prog *prog = __get_filter(fprog, sk);
1516 return PTR_ERR(prog);
1518 err = __sk_attach_prog(prog, sk);
1520 __bpf_prog_release(prog);
1526 EXPORT_SYMBOL_GPL(sk_attach_filter);
1528 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1530 struct bpf_prog *prog = __get_filter(fprog, sk);
1534 return PTR_ERR(prog);
1536 err = __reuseport_attach_prog(prog, sk);
1538 __bpf_prog_release(prog);
1545 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1547 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1548 return ERR_PTR(-EPERM);
1550 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1553 int sk_attach_bpf(u32 ufd, struct sock *sk)
1555 struct bpf_prog *prog = __get_bpf(ufd, sk);
1559 return PTR_ERR(prog);
1561 err = __sk_attach_prog(prog, sk);
1570 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1572 struct bpf_prog *prog = __get_bpf(ufd, sk);
1576 return PTR_ERR(prog);
1578 err = __reuseport_attach_prog(prog, sk);
1587 struct bpf_scratchpad {
1589 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1590 u8 buff[MAX_BPF_STACK];
1594 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1596 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1597 unsigned int write_len)
1599 return skb_ensure_writable(skb, write_len);
1602 static inline int bpf_try_make_writable(struct sk_buff *skb,
1603 unsigned int write_len)
1605 int err = __bpf_try_make_writable(skb, write_len);
1607 bpf_compute_data_pointers(skb);
1611 static int bpf_try_make_head_writable(struct sk_buff *skb)
1613 return bpf_try_make_writable(skb, skb_headlen(skb));
1616 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1618 if (skb_at_tc_ingress(skb))
1619 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1622 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1624 if (skb_at_tc_ingress(skb))
1625 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1628 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1629 const void *, from, u32, len, u64, flags)
1633 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1635 if (unlikely(offset > 0xffff))
1637 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1640 ptr = skb->data + offset;
1641 if (flags & BPF_F_RECOMPUTE_CSUM)
1642 __skb_postpull_rcsum(skb, ptr, len, offset);
1644 memcpy(ptr, from, len);
1646 if (flags & BPF_F_RECOMPUTE_CSUM)
1647 __skb_postpush_rcsum(skb, ptr, len, offset);
1648 if (flags & BPF_F_INVALIDATE_HASH)
1649 skb_clear_hash(skb);
1654 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1655 .func = bpf_skb_store_bytes,
1657 .ret_type = RET_INTEGER,
1658 .arg1_type = ARG_PTR_TO_CTX,
1659 .arg2_type = ARG_ANYTHING,
1660 .arg3_type = ARG_PTR_TO_MEM,
1661 .arg4_type = ARG_CONST_SIZE,
1662 .arg5_type = ARG_ANYTHING,
1665 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1666 void *, to, u32, len)
1670 if (unlikely(offset > 0xffff))
1673 ptr = skb_header_pointer(skb, offset, len, to);
1677 memcpy(to, ptr, len);
1685 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1686 .func = bpf_skb_load_bytes,
1688 .ret_type = RET_INTEGER,
1689 .arg1_type = ARG_PTR_TO_CTX,
1690 .arg2_type = ARG_ANYTHING,
1691 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1692 .arg4_type = ARG_CONST_SIZE,
1695 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1696 u32, offset, void *, to, u32, len, u32, start_header)
1700 if (unlikely(offset > 0xffff || len > skb_headlen(skb)))
1703 switch (start_header) {
1704 case BPF_HDR_START_MAC:
1705 ptr = skb_mac_header(skb) + offset;
1707 case BPF_HDR_START_NET:
1708 ptr = skb_network_header(skb) + offset;
1714 if (likely(ptr >= skb_mac_header(skb) &&
1715 ptr + len <= skb_tail_pointer(skb))) {
1716 memcpy(to, ptr, len);
1725 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1726 .func = bpf_skb_load_bytes_relative,
1728 .ret_type = RET_INTEGER,
1729 .arg1_type = ARG_PTR_TO_CTX,
1730 .arg2_type = ARG_ANYTHING,
1731 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1732 .arg4_type = ARG_CONST_SIZE,
1733 .arg5_type = ARG_ANYTHING,
1736 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1738 /* Idea is the following: should the needed direct read/write
1739 * test fail during runtime, we can pull in more data and redo
1740 * again, since implicitly, we invalidate previous checks here.
1742 * Or, since we know how much we need to make read/writeable,
1743 * this can be done once at the program beginning for direct
1744 * access case. By this we overcome limitations of only current
1745 * headroom being accessible.
1747 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1750 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1751 .func = bpf_skb_pull_data,
1753 .ret_type = RET_INTEGER,
1754 .arg1_type = ARG_PTR_TO_CTX,
1755 .arg2_type = ARG_ANYTHING,
1758 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1759 u64, from, u64, to, u64, flags)
1763 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1765 if (unlikely(offset > 0xffff || offset & 1))
1767 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1770 ptr = (__sum16 *)(skb->data + offset);
1771 switch (flags & BPF_F_HDR_FIELD_MASK) {
1773 if (unlikely(from != 0))
1776 csum_replace_by_diff(ptr, to);
1779 csum_replace2(ptr, from, to);
1782 csum_replace4(ptr, from, to);
1791 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1792 .func = bpf_l3_csum_replace,
1794 .ret_type = RET_INTEGER,
1795 .arg1_type = ARG_PTR_TO_CTX,
1796 .arg2_type = ARG_ANYTHING,
1797 .arg3_type = ARG_ANYTHING,
1798 .arg4_type = ARG_ANYTHING,
1799 .arg5_type = ARG_ANYTHING,
1802 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1803 u64, from, u64, to, u64, flags)
1805 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1806 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1807 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1810 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1811 BPF_F_PSEUDO_HDR | 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 if (is_mmzero && !do_mforce && !*ptr)
1822 switch (flags & BPF_F_HDR_FIELD_MASK) {
1824 if (unlikely(from != 0))
1827 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1830 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1833 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1839 if (is_mmzero && !*ptr)
1840 *ptr = CSUM_MANGLED_0;
1844 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1845 .func = bpf_l4_csum_replace,
1847 .ret_type = RET_INTEGER,
1848 .arg1_type = ARG_PTR_TO_CTX,
1849 .arg2_type = ARG_ANYTHING,
1850 .arg3_type = ARG_ANYTHING,
1851 .arg4_type = ARG_ANYTHING,
1852 .arg5_type = ARG_ANYTHING,
1855 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1856 __be32 *, to, u32, to_size, __wsum, seed)
1858 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1859 u32 diff_size = from_size + to_size;
1862 /* This is quite flexible, some examples:
1864 * from_size == 0, to_size > 0, seed := csum --> pushing data
1865 * from_size > 0, to_size == 0, seed := csum --> pulling data
1866 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1868 * Even for diffing, from_size and to_size don't need to be equal.
1870 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1871 diff_size > sizeof(sp->diff)))
1874 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1875 sp->diff[j] = ~from[i];
1876 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1877 sp->diff[j] = to[i];
1879 return csum_partial(sp->diff, diff_size, seed);
1882 static const struct bpf_func_proto bpf_csum_diff_proto = {
1883 .func = bpf_csum_diff,
1886 .ret_type = RET_INTEGER,
1887 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1888 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1889 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1890 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1891 .arg5_type = ARG_ANYTHING,
1894 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1896 /* The interface is to be used in combination with bpf_csum_diff()
1897 * for direct packet writes. csum rotation for alignment as well
1898 * as emulating csum_sub() can be done from the eBPF program.
1900 if (skb->ip_summed == CHECKSUM_COMPLETE)
1901 return (skb->csum = csum_add(skb->csum, csum));
1906 static const struct bpf_func_proto bpf_csum_update_proto = {
1907 .func = bpf_csum_update,
1909 .ret_type = RET_INTEGER,
1910 .arg1_type = ARG_PTR_TO_CTX,
1911 .arg2_type = ARG_ANYTHING,
1914 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1916 return dev_forward_skb(dev, skb);
1919 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1920 struct sk_buff *skb)
1922 int ret = ____dev_forward_skb(dev, skb);
1926 ret = netif_rx(skb);
1932 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1936 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1937 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1944 __this_cpu_inc(xmit_recursion);
1945 ret = dev_queue_xmit(skb);
1946 __this_cpu_dec(xmit_recursion);
1951 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1954 /* skb->mac_len is not set on normal egress */
1955 unsigned int mlen = skb->network_header - skb->mac_header;
1957 __skb_pull(skb, mlen);
1959 /* At ingress, the mac header has already been pulled once.
1960 * At egress, skb_pospull_rcsum has to be done in case that
1961 * the skb is originated from ingress (i.e. a forwarded skb)
1962 * to ensure that rcsum starts at net header.
1964 if (!skb_at_tc_ingress(skb))
1965 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1966 skb_pop_mac_header(skb);
1967 skb_reset_mac_len(skb);
1968 return flags & BPF_F_INGRESS ?
1969 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1972 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1975 /* Verify that a link layer header is carried */
1976 if (unlikely(skb->mac_header >= skb->network_header)) {
1981 bpf_push_mac_rcsum(skb);
1982 return flags & BPF_F_INGRESS ?
1983 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1986 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1989 if (dev_is_mac_header_xmit(dev))
1990 return __bpf_redirect_common(skb, dev, flags);
1992 return __bpf_redirect_no_mac(skb, dev, flags);
1995 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1997 struct net_device *dev;
1998 struct sk_buff *clone;
2001 if (unlikely(flags & ~(BPF_F_INGRESS)))
2004 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2008 clone = skb_clone(skb, GFP_ATOMIC);
2009 if (unlikely(!clone))
2012 /* For direct write, we need to keep the invariant that the skbs
2013 * we're dealing with need to be uncloned. Should uncloning fail
2014 * here, we need to free the just generated clone to unclone once
2017 ret = bpf_try_make_head_writable(skb);
2018 if (unlikely(ret)) {
2023 return __bpf_redirect(clone, dev, flags);
2026 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2027 .func = bpf_clone_redirect,
2029 .ret_type = RET_INTEGER,
2030 .arg1_type = ARG_PTR_TO_CTX,
2031 .arg2_type = ARG_ANYTHING,
2032 .arg3_type = ARG_ANYTHING,
2035 struct redirect_info {
2038 struct bpf_map *map;
2039 struct bpf_map *map_to_flush;
2040 unsigned long map_owner;
2043 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
2045 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2047 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2049 if (unlikely(flags & ~(BPF_F_INGRESS)))
2052 ri->ifindex = ifindex;
2055 return TC_ACT_REDIRECT;
2058 int skb_do_redirect(struct sk_buff *skb)
2060 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2061 struct net_device *dev;
2063 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2065 if (unlikely(!dev)) {
2070 return __bpf_redirect(skb, dev, ri->flags);
2073 static const struct bpf_func_proto bpf_redirect_proto = {
2074 .func = bpf_redirect,
2076 .ret_type = RET_INTEGER,
2077 .arg1_type = ARG_ANYTHING,
2078 .arg2_type = ARG_ANYTHING,
2081 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
2082 struct bpf_map *, map, void *, key, u64, flags)
2084 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2086 /* If user passes invalid input drop the packet. */
2087 if (unlikely(flags & ~(BPF_F_INGRESS)))
2090 tcb->bpf.flags = flags;
2091 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
2092 if (!tcb->bpf.sk_redir)
2098 static const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
2099 .func = bpf_sk_redirect_hash,
2101 .ret_type = RET_INTEGER,
2102 .arg1_type = ARG_PTR_TO_CTX,
2103 .arg2_type = ARG_CONST_MAP_PTR,
2104 .arg3_type = ARG_PTR_TO_MAP_KEY,
2105 .arg4_type = ARG_ANYTHING,
2108 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
2109 struct bpf_map *, map, u32, key, u64, flags)
2111 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2113 /* If user passes invalid input drop the packet. */
2114 if (unlikely(flags & ~(BPF_F_INGRESS)))
2117 tcb->bpf.flags = flags;
2118 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
2119 if (!tcb->bpf.sk_redir)
2125 struct sock *do_sk_redirect_map(struct sk_buff *skb)
2127 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2129 return tcb->bpf.sk_redir;
2132 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
2133 .func = bpf_sk_redirect_map,
2135 .ret_type = RET_INTEGER,
2136 .arg1_type = ARG_PTR_TO_CTX,
2137 .arg2_type = ARG_CONST_MAP_PTR,
2138 .arg3_type = ARG_ANYTHING,
2139 .arg4_type = ARG_ANYTHING,
2142 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg_buff *, msg,
2143 struct bpf_map *, map, void *, key, u64, flags)
2145 /* If user passes invalid input drop the packet. */
2146 if (unlikely(flags & ~(BPF_F_INGRESS)))
2150 msg->sk_redir = __sock_hash_lookup_elem(map, key);
2157 static const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
2158 .func = bpf_msg_redirect_hash,
2160 .ret_type = RET_INTEGER,
2161 .arg1_type = ARG_PTR_TO_CTX,
2162 .arg2_type = ARG_CONST_MAP_PTR,
2163 .arg3_type = ARG_PTR_TO_MAP_KEY,
2164 .arg4_type = ARG_ANYTHING,
2167 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg_buff *, msg,
2168 struct bpf_map *, map, u32, key, u64, flags)
2170 /* If user passes invalid input drop the packet. */
2171 if (unlikely(flags & ~(BPF_F_INGRESS)))
2175 msg->sk_redir = __sock_map_lookup_elem(map, key);
2182 struct sock *do_msg_redirect_map(struct sk_msg_buff *msg)
2184 return msg->sk_redir;
2187 static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
2188 .func = bpf_msg_redirect_map,
2190 .ret_type = RET_INTEGER,
2191 .arg1_type = ARG_PTR_TO_CTX,
2192 .arg2_type = ARG_CONST_MAP_PTR,
2193 .arg3_type = ARG_ANYTHING,
2194 .arg4_type = ARG_ANYTHING,
2197 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg_buff *, msg, u32, bytes)
2199 msg->apply_bytes = bytes;
2203 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2204 .func = bpf_msg_apply_bytes,
2206 .ret_type = RET_INTEGER,
2207 .arg1_type = ARG_PTR_TO_CTX,
2208 .arg2_type = ARG_ANYTHING,
2211 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg_buff *, msg, u32, bytes)
2213 msg->cork_bytes = bytes;
2217 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2218 .func = bpf_msg_cork_bytes,
2220 .ret_type = RET_INTEGER,
2221 .arg1_type = ARG_PTR_TO_CTX,
2222 .arg2_type = ARG_ANYTHING,
2225 BPF_CALL_4(bpf_msg_pull_data,
2226 struct sk_msg_buff *, msg, u32, start, u32, end, u64, flags)
2228 unsigned int len = 0, offset = 0, copy = 0;
2229 struct scatterlist *sg = msg->sg_data;
2230 int first_sg, last_sg, i, shift;
2231 unsigned char *p, *to, *from;
2232 int bytes = end - start;
2235 if (unlikely(flags || end <= start))
2238 /* First find the starting scatterlist element */
2243 if (start < offset + len)
2246 if (i == MAX_SKB_FRAGS)
2248 } while (i != msg->sg_end);
2250 if (unlikely(start >= offset + len))
2253 if (!msg->sg_copy[i] && bytes <= len)
2258 /* At this point we need to linearize multiple scatterlist
2259 * elements or a single shared page. Either way we need to
2260 * copy into a linear buffer exclusively owned by BPF. Then
2261 * place the buffer in the scatterlist and fixup the original
2262 * entries by removing the entries now in the linear buffer
2263 * and shifting the remaining entries. For now we do not try
2264 * to copy partial entries to avoid complexity of running out
2265 * of sg_entry slots. The downside is reading a single byte
2266 * will copy the entire sg entry.
2269 copy += sg[i].length;
2271 if (i == MAX_SKB_FRAGS)
2275 } while (i != msg->sg_end);
2278 if (unlikely(copy < end - start))
2281 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC, get_order(copy));
2282 if (unlikely(!page))
2284 p = page_address(page);
2289 from = sg_virt(&sg[i]);
2293 memcpy(to, from, len);
2296 put_page(sg_page(&sg[i]));
2299 if (i == MAX_SKB_FRAGS)
2301 } while (i != last_sg);
2303 sg[first_sg].length = copy;
2304 sg_set_page(&sg[first_sg], page, copy, 0);
2306 /* To repair sg ring we need to shift entries. If we only
2307 * had a single entry though we can just replace it and
2308 * be done. Otherwise walk the ring and shift the entries.
2310 shift = last_sg - first_sg - 1;
2318 if (i + shift >= MAX_SKB_FRAGS)
2319 move_from = i + shift - MAX_SKB_FRAGS;
2321 move_from = i + shift;
2323 if (move_from == msg->sg_end)
2326 sg[i] = sg[move_from];
2327 sg[move_from].length = 0;
2328 sg[move_from].page_link = 0;
2329 sg[move_from].offset = 0;
2332 if (i == MAX_SKB_FRAGS)
2335 msg->sg_end -= shift;
2336 if (msg->sg_end < 0)
2337 msg->sg_end += MAX_SKB_FRAGS;
2339 msg->data = sg_virt(&sg[i]) + start - offset;
2340 msg->data_end = msg->data + bytes;
2345 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2346 .func = bpf_msg_pull_data,
2348 .ret_type = RET_INTEGER,
2349 .arg1_type = ARG_PTR_TO_CTX,
2350 .arg2_type = ARG_ANYTHING,
2351 .arg3_type = ARG_ANYTHING,
2352 .arg4_type = ARG_ANYTHING,
2355 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2357 return task_get_classid(skb);
2360 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2361 .func = bpf_get_cgroup_classid,
2363 .ret_type = RET_INTEGER,
2364 .arg1_type = ARG_PTR_TO_CTX,
2367 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2369 return dst_tclassid(skb);
2372 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2373 .func = bpf_get_route_realm,
2375 .ret_type = RET_INTEGER,
2376 .arg1_type = ARG_PTR_TO_CTX,
2379 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2381 /* If skb_clear_hash() was called due to mangling, we can
2382 * trigger SW recalculation here. Later access to hash
2383 * can then use the inline skb->hash via context directly
2384 * instead of calling this helper again.
2386 return skb_get_hash(skb);
2389 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2390 .func = bpf_get_hash_recalc,
2392 .ret_type = RET_INTEGER,
2393 .arg1_type = ARG_PTR_TO_CTX,
2396 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2398 /* After all direct packet write, this can be used once for
2399 * triggering a lazy recalc on next skb_get_hash() invocation.
2401 skb_clear_hash(skb);
2405 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2406 .func = bpf_set_hash_invalid,
2408 .ret_type = RET_INTEGER,
2409 .arg1_type = ARG_PTR_TO_CTX,
2412 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2414 /* Set user specified hash as L4(+), so that it gets returned
2415 * on skb_get_hash() call unless BPF prog later on triggers a
2418 __skb_set_sw_hash(skb, hash, true);
2422 static const struct bpf_func_proto bpf_set_hash_proto = {
2423 .func = bpf_set_hash,
2425 .ret_type = RET_INTEGER,
2426 .arg1_type = ARG_PTR_TO_CTX,
2427 .arg2_type = ARG_ANYTHING,
2430 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2435 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2436 vlan_proto != htons(ETH_P_8021AD)))
2437 vlan_proto = htons(ETH_P_8021Q);
2439 bpf_push_mac_rcsum(skb);
2440 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2441 bpf_pull_mac_rcsum(skb);
2443 bpf_compute_data_pointers(skb);
2447 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2448 .func = bpf_skb_vlan_push,
2450 .ret_type = RET_INTEGER,
2451 .arg1_type = ARG_PTR_TO_CTX,
2452 .arg2_type = ARG_ANYTHING,
2453 .arg3_type = ARG_ANYTHING,
2456 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2460 bpf_push_mac_rcsum(skb);
2461 ret = skb_vlan_pop(skb);
2462 bpf_pull_mac_rcsum(skb);
2464 bpf_compute_data_pointers(skb);
2468 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2469 .func = bpf_skb_vlan_pop,
2471 .ret_type = RET_INTEGER,
2472 .arg1_type = ARG_PTR_TO_CTX,
2475 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2477 /* Caller already did skb_cow() with len as headroom,
2478 * so no need to do it here.
2481 memmove(skb->data, skb->data + len, off);
2482 memset(skb->data + off, 0, len);
2484 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2485 * needed here as it does not change the skb->csum
2486 * result for checksum complete when summing over
2492 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2494 /* skb_ensure_writable() is not needed here, as we're
2495 * already working on an uncloned skb.
2497 if (unlikely(!pskb_may_pull(skb, off + len)))
2500 skb_postpull_rcsum(skb, skb->data + off, len);
2501 memmove(skb->data + len, skb->data, off);
2502 __skb_pull(skb, len);
2507 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2509 bool trans_same = skb->transport_header == skb->network_header;
2512 /* There's no need for __skb_push()/__skb_pull() pair to
2513 * get to the start of the mac header as we're guaranteed
2514 * to always start from here under eBPF.
2516 ret = bpf_skb_generic_push(skb, off, len);
2518 skb->mac_header -= len;
2519 skb->network_header -= len;
2521 skb->transport_header = skb->network_header;
2527 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2529 bool trans_same = skb->transport_header == skb->network_header;
2532 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2533 ret = bpf_skb_generic_pop(skb, off, len);
2535 skb->mac_header += len;
2536 skb->network_header += len;
2538 skb->transport_header = skb->network_header;
2544 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2546 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2547 u32 off = skb_mac_header_len(skb);
2550 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2551 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2554 ret = skb_cow(skb, len_diff);
2555 if (unlikely(ret < 0))
2558 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2559 if (unlikely(ret < 0))
2562 if (skb_is_gso(skb)) {
2563 struct skb_shared_info *shinfo = skb_shinfo(skb);
2565 /* SKB_GSO_TCPV4 needs to be changed into
2568 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2569 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2570 shinfo->gso_type |= SKB_GSO_TCPV6;
2573 /* Due to IPv6 header, MSS needs to be downgraded. */
2574 skb_decrease_gso_size(shinfo, len_diff);
2575 /* Header must be checked, and gso_segs recomputed. */
2576 shinfo->gso_type |= SKB_GSO_DODGY;
2577 shinfo->gso_segs = 0;
2580 skb->protocol = htons(ETH_P_IPV6);
2581 skb_clear_hash(skb);
2586 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2588 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2589 u32 off = skb_mac_header_len(skb);
2592 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2593 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2596 ret = skb_unclone(skb, GFP_ATOMIC);
2597 if (unlikely(ret < 0))
2600 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2601 if (unlikely(ret < 0))
2604 if (skb_is_gso(skb)) {
2605 struct skb_shared_info *shinfo = skb_shinfo(skb);
2607 /* SKB_GSO_TCPV6 needs to be changed into
2610 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2611 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2612 shinfo->gso_type |= SKB_GSO_TCPV4;
2615 /* Due to IPv4 header, MSS can be upgraded. */
2616 skb_increase_gso_size(shinfo, len_diff);
2617 /* Header must be checked, and gso_segs recomputed. */
2618 shinfo->gso_type |= SKB_GSO_DODGY;
2619 shinfo->gso_segs = 0;
2622 skb->protocol = htons(ETH_P_IP);
2623 skb_clear_hash(skb);
2628 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2630 __be16 from_proto = skb->protocol;
2632 if (from_proto == htons(ETH_P_IP) &&
2633 to_proto == htons(ETH_P_IPV6))
2634 return bpf_skb_proto_4_to_6(skb);
2636 if (from_proto == htons(ETH_P_IPV6) &&
2637 to_proto == htons(ETH_P_IP))
2638 return bpf_skb_proto_6_to_4(skb);
2643 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2648 if (unlikely(flags))
2651 /* General idea is that this helper does the basic groundwork
2652 * needed for changing the protocol, and eBPF program fills the
2653 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2654 * and other helpers, rather than passing a raw buffer here.
2656 * The rationale is to keep this minimal and without a need to
2657 * deal with raw packet data. F.e. even if we would pass buffers
2658 * here, the program still needs to call the bpf_lX_csum_replace()
2659 * helpers anyway. Plus, this way we keep also separation of
2660 * concerns, since f.e. bpf_skb_store_bytes() should only take
2663 * Currently, additional options and extension header space are
2664 * not supported, but flags register is reserved so we can adapt
2665 * that. For offloads, we mark packet as dodgy, so that headers
2666 * need to be verified first.
2668 ret = bpf_skb_proto_xlat(skb, proto);
2669 bpf_compute_data_pointers(skb);
2673 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2674 .func = bpf_skb_change_proto,
2676 .ret_type = RET_INTEGER,
2677 .arg1_type = ARG_PTR_TO_CTX,
2678 .arg2_type = ARG_ANYTHING,
2679 .arg3_type = ARG_ANYTHING,
2682 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2684 /* We only allow a restricted subset to be changed for now. */
2685 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2686 !skb_pkt_type_ok(pkt_type)))
2689 skb->pkt_type = pkt_type;
2693 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2694 .func = bpf_skb_change_type,
2696 .ret_type = RET_INTEGER,
2697 .arg1_type = ARG_PTR_TO_CTX,
2698 .arg2_type = ARG_ANYTHING,
2701 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2703 switch (skb->protocol) {
2704 case htons(ETH_P_IP):
2705 return sizeof(struct iphdr);
2706 case htons(ETH_P_IPV6):
2707 return sizeof(struct ipv6hdr);
2713 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2715 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2718 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2719 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2722 ret = skb_cow(skb, len_diff);
2723 if (unlikely(ret < 0))
2726 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2727 if (unlikely(ret < 0))
2730 if (skb_is_gso(skb)) {
2731 struct skb_shared_info *shinfo = skb_shinfo(skb);
2733 /* Due to header grow, MSS needs to be downgraded. */
2734 skb_decrease_gso_size(shinfo, len_diff);
2735 /* Header must be checked, and gso_segs recomputed. */
2736 shinfo->gso_type |= SKB_GSO_DODGY;
2737 shinfo->gso_segs = 0;
2743 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2745 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2748 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2749 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2752 ret = skb_unclone(skb, GFP_ATOMIC);
2753 if (unlikely(ret < 0))
2756 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2757 if (unlikely(ret < 0))
2760 if (skb_is_gso(skb)) {
2761 struct skb_shared_info *shinfo = skb_shinfo(skb);
2763 /* Due to header shrink, MSS can be upgraded. */
2764 skb_increase_gso_size(shinfo, len_diff);
2765 /* Header must be checked, and gso_segs recomputed. */
2766 shinfo->gso_type |= SKB_GSO_DODGY;
2767 shinfo->gso_segs = 0;
2773 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2775 return skb->dev->mtu + skb->dev->hard_header_len;
2778 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2780 bool trans_same = skb->transport_header == skb->network_header;
2781 u32 len_cur, len_diff_abs = abs(len_diff);
2782 u32 len_min = bpf_skb_net_base_len(skb);
2783 u32 len_max = __bpf_skb_max_len(skb);
2784 __be16 proto = skb->protocol;
2785 bool shrink = len_diff < 0;
2788 if (unlikely(len_diff_abs > 0xfffU))
2790 if (unlikely(proto != htons(ETH_P_IP) &&
2791 proto != htons(ETH_P_IPV6)))
2794 len_cur = skb->len - skb_network_offset(skb);
2795 if (skb_transport_header_was_set(skb) && !trans_same)
2796 len_cur = skb_network_header_len(skb);
2797 if ((shrink && (len_diff_abs >= len_cur ||
2798 len_cur - len_diff_abs < len_min)) ||
2799 (!shrink && (skb->len + len_diff_abs > len_max &&
2803 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2804 bpf_skb_net_grow(skb, len_diff_abs);
2806 bpf_compute_data_pointers(skb);
2810 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2811 u32, mode, u64, flags)
2813 if (unlikely(flags))
2815 if (likely(mode == BPF_ADJ_ROOM_NET))
2816 return bpf_skb_adjust_net(skb, len_diff);
2821 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2822 .func = bpf_skb_adjust_room,
2824 .ret_type = RET_INTEGER,
2825 .arg1_type = ARG_PTR_TO_CTX,
2826 .arg2_type = ARG_ANYTHING,
2827 .arg3_type = ARG_ANYTHING,
2828 .arg4_type = ARG_ANYTHING,
2831 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2833 u32 min_len = skb_network_offset(skb);
2835 if (skb_transport_header_was_set(skb))
2836 min_len = skb_transport_offset(skb);
2837 if (skb->ip_summed == CHECKSUM_PARTIAL)
2838 min_len = skb_checksum_start_offset(skb) +
2839 skb->csum_offset + sizeof(__sum16);
2843 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2845 unsigned int old_len = skb->len;
2848 ret = __skb_grow_rcsum(skb, new_len);
2850 memset(skb->data + old_len, 0, new_len - old_len);
2854 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2856 return __skb_trim_rcsum(skb, new_len);
2859 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2862 u32 max_len = __bpf_skb_max_len(skb);
2863 u32 min_len = __bpf_skb_min_len(skb);
2866 if (unlikely(flags || new_len > max_len || new_len < min_len))
2868 if (skb->encapsulation)
2871 /* The basic idea of this helper is that it's performing the
2872 * needed work to either grow or trim an skb, and eBPF program
2873 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2874 * bpf_lX_csum_replace() and others rather than passing a raw
2875 * buffer here. This one is a slow path helper and intended
2876 * for replies with control messages.
2878 * Like in bpf_skb_change_proto(), we want to keep this rather
2879 * minimal and without protocol specifics so that we are able
2880 * to separate concerns as in bpf_skb_store_bytes() should only
2881 * be the one responsible for writing buffers.
2883 * It's really expected to be a slow path operation here for
2884 * control message replies, so we're implicitly linearizing,
2885 * uncloning and drop offloads from the skb by this.
2887 ret = __bpf_try_make_writable(skb, skb->len);
2889 if (new_len > skb->len)
2890 ret = bpf_skb_grow_rcsum(skb, new_len);
2891 else if (new_len < skb->len)
2892 ret = bpf_skb_trim_rcsum(skb, new_len);
2893 if (!ret && skb_is_gso(skb))
2897 bpf_compute_data_pointers(skb);
2901 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2902 .func = bpf_skb_change_tail,
2904 .ret_type = RET_INTEGER,
2905 .arg1_type = ARG_PTR_TO_CTX,
2906 .arg2_type = ARG_ANYTHING,
2907 .arg3_type = ARG_ANYTHING,
2910 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2913 u32 max_len = __bpf_skb_max_len(skb);
2914 u32 new_len = skb->len + head_room;
2917 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2918 new_len < skb->len))
2921 ret = skb_cow(skb, head_room);
2923 /* Idea for this helper is that we currently only
2924 * allow to expand on mac header. This means that
2925 * skb->protocol network header, etc, stay as is.
2926 * Compared to bpf_skb_change_tail(), we're more
2927 * flexible due to not needing to linearize or
2928 * reset GSO. Intention for this helper is to be
2929 * used by an L3 skb that needs to push mac header
2930 * for redirection into L2 device.
2932 __skb_push(skb, head_room);
2933 memset(skb->data, 0, head_room);
2934 skb_reset_mac_header(skb);
2937 bpf_compute_data_pointers(skb);
2941 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2942 .func = bpf_skb_change_head,
2944 .ret_type = RET_INTEGER,
2945 .arg1_type = ARG_PTR_TO_CTX,
2946 .arg2_type = ARG_ANYTHING,
2947 .arg3_type = ARG_ANYTHING,
2950 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
2952 return xdp_data_meta_unsupported(xdp) ? 0 :
2953 xdp->data - xdp->data_meta;
2956 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2958 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
2959 unsigned long metalen = xdp_get_metalen(xdp);
2960 void *data_start = xdp_frame_end + metalen;
2961 void *data = xdp->data + offset;
2963 if (unlikely(data < data_start ||
2964 data > xdp->data_end - ETH_HLEN))
2968 memmove(xdp->data_meta + offset,
2969 xdp->data_meta, metalen);
2970 xdp->data_meta += offset;
2976 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2977 .func = bpf_xdp_adjust_head,
2979 .ret_type = RET_INTEGER,
2980 .arg1_type = ARG_PTR_TO_CTX,
2981 .arg2_type = ARG_ANYTHING,
2984 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
2986 void *data_end = xdp->data_end + offset;
2988 /* only shrinking is allowed for now. */
2989 if (unlikely(offset >= 0))
2992 if (unlikely(data_end < xdp->data + ETH_HLEN))
2995 xdp->data_end = data_end;
3000 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3001 .func = bpf_xdp_adjust_tail,
3003 .ret_type = RET_INTEGER,
3004 .arg1_type = ARG_PTR_TO_CTX,
3005 .arg2_type = ARG_ANYTHING,
3008 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3010 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3011 void *meta = xdp->data_meta + offset;
3012 unsigned long metalen = xdp->data - meta;
3014 if (xdp_data_meta_unsupported(xdp))
3016 if (unlikely(meta < xdp_frame_end ||
3019 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3023 xdp->data_meta = meta;
3028 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3029 .func = bpf_xdp_adjust_meta,
3031 .ret_type = RET_INTEGER,
3032 .arg1_type = ARG_PTR_TO_CTX,
3033 .arg2_type = ARG_ANYTHING,
3036 static int __bpf_tx_xdp(struct net_device *dev,
3037 struct bpf_map *map,
3038 struct xdp_buff *xdp,
3041 struct xdp_frame *xdpf;
3044 if (!dev->netdev_ops->ndo_xdp_xmit) {
3048 xdpf = convert_to_xdp_frame(xdp);
3049 if (unlikely(!xdpf))
3052 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdpf);
3055 dev->netdev_ops->ndo_xdp_flush(dev);
3059 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3060 struct bpf_map *map,
3061 struct xdp_buff *xdp,
3066 switch (map->map_type) {
3067 case BPF_MAP_TYPE_DEVMAP: {
3068 struct net_device *dev = fwd;
3069 struct xdp_frame *xdpf;
3071 if (!dev->netdev_ops->ndo_xdp_xmit)
3074 xdpf = convert_to_xdp_frame(xdp);
3075 if (unlikely(!xdpf))
3078 /* TODO: move to inside map code instead, for bulk support
3079 * err = dev_map_enqueue(dev, xdp);
3081 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdpf);
3084 __dev_map_insert_ctx(map, index);
3087 case BPF_MAP_TYPE_CPUMAP: {
3088 struct bpf_cpu_map_entry *rcpu = fwd;
3090 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3093 __cpu_map_insert_ctx(map, index);
3096 case BPF_MAP_TYPE_XSKMAP: {
3097 struct xdp_sock *xs = fwd;
3099 err = __xsk_map_redirect(map, xdp, xs);
3108 void xdp_do_flush_map(void)
3110 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3111 struct bpf_map *map = ri->map_to_flush;
3113 ri->map_to_flush = NULL;
3115 switch (map->map_type) {
3116 case BPF_MAP_TYPE_DEVMAP:
3117 __dev_map_flush(map);
3119 case BPF_MAP_TYPE_CPUMAP:
3120 __cpu_map_flush(map);
3122 case BPF_MAP_TYPE_XSKMAP:
3123 __xsk_map_flush(map);
3130 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3132 static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3134 switch (map->map_type) {
3135 case BPF_MAP_TYPE_DEVMAP:
3136 return __dev_map_lookup_elem(map, index);
3137 case BPF_MAP_TYPE_CPUMAP:
3138 return __cpu_map_lookup_elem(map, index);
3139 case BPF_MAP_TYPE_XSKMAP:
3140 return __xsk_map_lookup_elem(map, index);
3146 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
3149 return (unsigned long)xdp_prog->aux != aux;
3152 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3153 struct bpf_prog *xdp_prog)
3155 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3156 unsigned long map_owner = ri->map_owner;
3157 struct bpf_map *map = ri->map;
3158 u32 index = ri->ifindex;
3166 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3172 fwd = __xdp_map_lookup_elem(map, index);
3177 if (ri->map_to_flush && ri->map_to_flush != map)
3180 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3184 ri->map_to_flush = map;
3185 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3188 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3192 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3193 struct bpf_prog *xdp_prog)
3195 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3196 struct net_device *fwd;
3197 u32 index = ri->ifindex;
3201 return xdp_do_redirect_map(dev, xdp, xdp_prog);
3203 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3205 if (unlikely(!fwd)) {
3210 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3214 _trace_xdp_redirect(dev, xdp_prog, index);
3217 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3220 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3222 static int __xdp_generic_ok_fwd_dev(struct sk_buff *skb, struct net_device *fwd)
3226 if (unlikely(!(fwd->flags & IFF_UP)))
3229 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
3236 static int xdp_do_generic_redirect_map(struct net_device *dev,
3237 struct sk_buff *skb,
3238 struct xdp_buff *xdp,
3239 struct bpf_prog *xdp_prog)
3241 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3242 unsigned long map_owner = ri->map_owner;
3243 struct bpf_map *map = ri->map;
3244 u32 index = ri->ifindex;
3252 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3257 fwd = __xdp_map_lookup_elem(map, index);
3258 if (unlikely(!fwd)) {
3263 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3264 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
3267 generic_xdp_tx(skb, xdp_prog);
3268 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3269 struct xdp_sock *xs = fwd;
3271 err = xsk_generic_rcv(xs, xdp);
3276 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3281 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3284 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3288 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3289 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3291 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3292 u32 index = ri->ifindex;
3293 struct net_device *fwd;
3297 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog);
3300 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3301 if (unlikely(!fwd)) {
3306 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
3310 _trace_xdp_redirect(dev, xdp_prog, index);
3311 generic_xdp_tx(skb, xdp_prog);
3314 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3317 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3319 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3321 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3323 if (unlikely(flags))
3326 ri->ifindex = ifindex;
3331 return XDP_REDIRECT;
3334 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3335 .func = bpf_xdp_redirect,
3337 .ret_type = RET_INTEGER,
3338 .arg1_type = ARG_ANYTHING,
3339 .arg2_type = ARG_ANYTHING,
3342 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
3343 unsigned long, map_owner)
3345 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3347 if (unlikely(flags))
3350 ri->ifindex = ifindex;
3353 ri->map_owner = map_owner;
3355 return XDP_REDIRECT;
3358 /* Note, arg4 is hidden from users and populated by the verifier
3359 * with the right pointer.
3361 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3362 .func = bpf_xdp_redirect_map,
3364 .ret_type = RET_INTEGER,
3365 .arg1_type = ARG_CONST_MAP_PTR,
3366 .arg2_type = ARG_ANYTHING,
3367 .arg3_type = ARG_ANYTHING,
3370 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3371 unsigned long off, unsigned long len)
3373 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3377 if (ptr != dst_buff)
3378 memcpy(dst_buff, ptr, len);
3383 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3384 u64, flags, void *, meta, u64, meta_size)
3386 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3388 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3390 if (unlikely(skb_size > skb->len))
3393 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3397 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3398 .func = bpf_skb_event_output,
3400 .ret_type = RET_INTEGER,
3401 .arg1_type = ARG_PTR_TO_CTX,
3402 .arg2_type = ARG_CONST_MAP_PTR,
3403 .arg3_type = ARG_ANYTHING,
3404 .arg4_type = ARG_PTR_TO_MEM,
3405 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3408 static unsigned short bpf_tunnel_key_af(u64 flags)
3410 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3413 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3414 u32, size, u64, flags)
3416 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3417 u8 compat[sizeof(struct bpf_tunnel_key)];
3421 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3425 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3429 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3432 case offsetof(struct bpf_tunnel_key, tunnel_label):
3433 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3435 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3436 /* Fixup deprecated structure layouts here, so we have
3437 * a common path later on.
3439 if (ip_tunnel_info_af(info) != AF_INET)
3442 to = (struct bpf_tunnel_key *)compat;
3449 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3450 to->tunnel_tos = info->key.tos;
3451 to->tunnel_ttl = info->key.ttl;
3453 if (flags & BPF_F_TUNINFO_IPV6) {
3454 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3455 sizeof(to->remote_ipv6));
3456 to->tunnel_label = be32_to_cpu(info->key.label);
3458 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3461 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3462 memcpy(to_orig, to, size);
3466 memset(to_orig, 0, size);
3470 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3471 .func = bpf_skb_get_tunnel_key,
3473 .ret_type = RET_INTEGER,
3474 .arg1_type = ARG_PTR_TO_CTX,
3475 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3476 .arg3_type = ARG_CONST_SIZE,
3477 .arg4_type = ARG_ANYTHING,
3480 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3482 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3485 if (unlikely(!info ||
3486 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3490 if (unlikely(size < info->options_len)) {
3495 ip_tunnel_info_opts_get(to, info);
3496 if (size > info->options_len)
3497 memset(to + info->options_len, 0, size - info->options_len);
3499 return info->options_len;
3501 memset(to, 0, size);
3505 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3506 .func = bpf_skb_get_tunnel_opt,
3508 .ret_type = RET_INTEGER,
3509 .arg1_type = ARG_PTR_TO_CTX,
3510 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3511 .arg3_type = ARG_CONST_SIZE,
3514 static struct metadata_dst __percpu *md_dst;
3516 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3517 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3519 struct metadata_dst *md = this_cpu_ptr(md_dst);
3520 u8 compat[sizeof(struct bpf_tunnel_key)];
3521 struct ip_tunnel_info *info;
3523 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3524 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3526 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3528 case offsetof(struct bpf_tunnel_key, tunnel_label):
3529 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3530 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3531 /* Fixup deprecated structure layouts here, so we have
3532 * a common path later on.
3534 memcpy(compat, from, size);
3535 memset(compat + size, 0, sizeof(compat) - size);
3536 from = (const struct bpf_tunnel_key *) compat;
3542 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3547 dst_hold((struct dst_entry *) md);
3548 skb_dst_set(skb, (struct dst_entry *) md);
3550 info = &md->u.tun_info;
3551 memset(info, 0, sizeof(*info));
3552 info->mode = IP_TUNNEL_INFO_TX;
3554 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3555 if (flags & BPF_F_DONT_FRAGMENT)
3556 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3557 if (flags & BPF_F_ZERO_CSUM_TX)
3558 info->key.tun_flags &= ~TUNNEL_CSUM;
3559 if (flags & BPF_F_SEQ_NUMBER)
3560 info->key.tun_flags |= TUNNEL_SEQ;
3562 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3563 info->key.tos = from->tunnel_tos;
3564 info->key.ttl = from->tunnel_ttl;
3566 if (flags & BPF_F_TUNINFO_IPV6) {
3567 info->mode |= IP_TUNNEL_INFO_IPV6;
3568 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3569 sizeof(from->remote_ipv6));
3570 info->key.label = cpu_to_be32(from->tunnel_label) &
3571 IPV6_FLOWLABEL_MASK;
3573 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3579 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3580 .func = bpf_skb_set_tunnel_key,
3582 .ret_type = RET_INTEGER,
3583 .arg1_type = ARG_PTR_TO_CTX,
3584 .arg2_type = ARG_PTR_TO_MEM,
3585 .arg3_type = ARG_CONST_SIZE,
3586 .arg4_type = ARG_ANYTHING,
3589 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3590 const u8 *, from, u32, size)
3592 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3593 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3595 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3597 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3600 ip_tunnel_info_opts_set(info, from, size);
3605 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3606 .func = bpf_skb_set_tunnel_opt,
3608 .ret_type = RET_INTEGER,
3609 .arg1_type = ARG_PTR_TO_CTX,
3610 .arg2_type = ARG_PTR_TO_MEM,
3611 .arg3_type = ARG_CONST_SIZE,
3614 static const struct bpf_func_proto *
3615 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3618 struct metadata_dst __percpu *tmp;
3620 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3625 if (cmpxchg(&md_dst, NULL, tmp))
3626 metadata_dst_free_percpu(tmp);
3630 case BPF_FUNC_skb_set_tunnel_key:
3631 return &bpf_skb_set_tunnel_key_proto;
3632 case BPF_FUNC_skb_set_tunnel_opt:
3633 return &bpf_skb_set_tunnel_opt_proto;
3639 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3642 struct bpf_array *array = container_of(map, struct bpf_array, map);
3643 struct cgroup *cgrp;
3646 sk = skb_to_full_sk(skb);
3647 if (!sk || !sk_fullsock(sk))
3649 if (unlikely(idx >= array->map.max_entries))
3652 cgrp = READ_ONCE(array->ptrs[idx]);
3653 if (unlikely(!cgrp))
3656 return sk_under_cgroup_hierarchy(sk, cgrp);
3659 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3660 .func = bpf_skb_under_cgroup,
3662 .ret_type = RET_INTEGER,
3663 .arg1_type = ARG_PTR_TO_CTX,
3664 .arg2_type = ARG_CONST_MAP_PTR,
3665 .arg3_type = ARG_ANYTHING,
3668 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3669 unsigned long off, unsigned long len)
3671 memcpy(dst_buff, src_buff + off, len);
3675 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3676 u64, flags, void *, meta, u64, meta_size)
3678 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3680 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3682 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3685 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3686 xdp_size, bpf_xdp_copy);
3689 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3690 .func = bpf_xdp_event_output,
3692 .ret_type = RET_INTEGER,
3693 .arg1_type = ARG_PTR_TO_CTX,
3694 .arg2_type = ARG_CONST_MAP_PTR,
3695 .arg3_type = ARG_ANYTHING,
3696 .arg4_type = ARG_PTR_TO_MEM,
3697 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3700 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3702 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3705 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3706 .func = bpf_get_socket_cookie,
3708 .ret_type = RET_INTEGER,
3709 .arg1_type = ARG_PTR_TO_CTX,
3712 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3714 struct sock *sk = sk_to_full_sk(skb->sk);
3717 if (!sk || !sk_fullsock(sk))
3719 kuid = sock_net_uid(sock_net(sk), sk);
3720 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3723 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3724 .func = bpf_get_socket_uid,
3726 .ret_type = RET_INTEGER,
3727 .arg1_type = ARG_PTR_TO_CTX,
3730 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3731 int, level, int, optname, char *, optval, int, optlen)
3733 struct sock *sk = bpf_sock->sk;
3737 if (!sk_fullsock(sk))
3740 if (level == SOL_SOCKET) {
3741 if (optlen != sizeof(int))
3743 val = *((int *)optval);
3745 /* Only some socketops are supported */
3748 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3749 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3752 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3753 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3755 case SO_MAX_PACING_RATE:
3756 sk->sk_max_pacing_rate = val;
3757 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3758 sk->sk_max_pacing_rate);
3761 sk->sk_priority = val;
3766 sk->sk_rcvlowat = val ? : 1;
3775 } else if (level == SOL_IP) {
3776 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3779 val = *((int *)optval);
3780 /* Only some options are supported */
3783 if (val < -1 || val > 0xff) {
3786 struct inet_sock *inet = inet_sk(sk);
3796 #if IS_ENABLED(CONFIG_IPV6)
3797 } else if (level == SOL_IPV6) {
3798 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3801 val = *((int *)optval);
3802 /* Only some options are supported */
3805 if (val < -1 || val > 0xff) {
3808 struct ipv6_pinfo *np = inet6_sk(sk);
3819 } else if (level == SOL_TCP &&
3820 sk->sk_prot->setsockopt == tcp_setsockopt) {
3821 if (optname == TCP_CONGESTION) {
3822 char name[TCP_CA_NAME_MAX];
3823 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3825 strncpy(name, optval, min_t(long, optlen,
3826 TCP_CA_NAME_MAX-1));
3827 name[TCP_CA_NAME_MAX-1] = 0;
3828 ret = tcp_set_congestion_control(sk, name, false,
3831 struct tcp_sock *tp = tcp_sk(sk);
3833 if (optlen != sizeof(int))
3836 val = *((int *)optval);
3837 /* Only some options are supported */
3840 if (val <= 0 || tp->data_segs_out > 0)
3845 case TCP_BPF_SNDCWND_CLAMP:
3849 tp->snd_cwnd_clamp = val;
3850 tp->snd_ssthresh = val;
3864 static const struct bpf_func_proto bpf_setsockopt_proto = {
3865 .func = bpf_setsockopt,
3867 .ret_type = RET_INTEGER,
3868 .arg1_type = ARG_PTR_TO_CTX,
3869 .arg2_type = ARG_ANYTHING,
3870 .arg3_type = ARG_ANYTHING,
3871 .arg4_type = ARG_PTR_TO_MEM,
3872 .arg5_type = ARG_CONST_SIZE,
3875 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3876 int, level, int, optname, char *, optval, int, optlen)
3878 struct sock *sk = bpf_sock->sk;
3880 if (!sk_fullsock(sk))
3884 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3885 if (optname == TCP_CONGESTION) {
3886 struct inet_connection_sock *icsk = inet_csk(sk);
3888 if (!icsk->icsk_ca_ops || optlen <= 1)
3890 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3891 optval[optlen - 1] = 0;
3895 } else if (level == SOL_IP) {
3896 struct inet_sock *inet = inet_sk(sk);
3898 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3901 /* Only some options are supported */
3904 *((int *)optval) = (int)inet->tos;
3909 #if IS_ENABLED(CONFIG_IPV6)
3910 } else if (level == SOL_IPV6) {
3911 struct ipv6_pinfo *np = inet6_sk(sk);
3913 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3916 /* Only some options are supported */
3919 *((int *)optval) = (int)np->tclass;
3931 memset(optval, 0, optlen);
3935 static const struct bpf_func_proto bpf_getsockopt_proto = {
3936 .func = bpf_getsockopt,
3938 .ret_type = RET_INTEGER,
3939 .arg1_type = ARG_PTR_TO_CTX,
3940 .arg2_type = ARG_ANYTHING,
3941 .arg3_type = ARG_ANYTHING,
3942 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
3943 .arg5_type = ARG_CONST_SIZE,
3946 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
3949 struct sock *sk = bpf_sock->sk;
3950 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
3952 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
3956 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
3958 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
3961 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
3962 .func = bpf_sock_ops_cb_flags_set,
3964 .ret_type = RET_INTEGER,
3965 .arg1_type = ARG_PTR_TO_CTX,
3966 .arg2_type = ARG_ANYTHING,
3969 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
3970 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
3972 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
3976 struct sock *sk = ctx->sk;
3979 /* Binding to port can be expensive so it's prohibited in the helper.
3980 * Only binding to IP is supported.
3983 if (addr->sa_family == AF_INET) {
3984 if (addr_len < sizeof(struct sockaddr_in))
3986 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3988 return __inet_bind(sk, addr, addr_len, true, false);
3989 #if IS_ENABLED(CONFIG_IPV6)
3990 } else if (addr->sa_family == AF_INET6) {
3991 if (addr_len < SIN6_LEN_RFC2133)
3993 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3995 /* ipv6_bpf_stub cannot be NULL, since it's called from
3996 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
3998 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
3999 #endif /* CONFIG_IPV6 */
4001 #endif /* CONFIG_INET */
4003 return -EAFNOSUPPORT;
4006 static const struct bpf_func_proto bpf_bind_proto = {
4009 .ret_type = RET_INTEGER,
4010 .arg1_type = ARG_PTR_TO_CTX,
4011 .arg2_type = ARG_PTR_TO_MEM,
4012 .arg3_type = ARG_CONST_SIZE,
4016 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4017 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4019 const struct sec_path *sp = skb_sec_path(skb);
4020 const struct xfrm_state *x;
4022 if (!sp || unlikely(index >= sp->len || flags))
4025 x = sp->xvec[index];
4027 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4030 to->reqid = x->props.reqid;
4031 to->spi = x->id.spi;
4032 to->family = x->props.family;
4033 if (to->family == AF_INET6) {
4034 memcpy(to->remote_ipv6, x->props.saddr.a6,
4035 sizeof(to->remote_ipv6));
4037 to->remote_ipv4 = x->props.saddr.a4;
4042 memset(to, 0, size);
4046 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4047 .func = bpf_skb_get_xfrm_state,
4049 .ret_type = RET_INTEGER,
4050 .arg1_type = ARG_PTR_TO_CTX,
4051 .arg2_type = ARG_ANYTHING,
4052 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4053 .arg4_type = ARG_CONST_SIZE,
4054 .arg5_type = ARG_ANYTHING,
4058 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4059 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4060 const struct neighbour *neigh,
4061 const struct net_device *dev)
4063 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4064 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4065 params->h_vlan_TCI = 0;
4066 params->h_vlan_proto = 0;
4068 return dev->ifindex;
4072 #if IS_ENABLED(CONFIG_INET)
4073 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4074 u32 flags, bool check_mtu)
4076 struct in_device *in_dev;
4077 struct neighbour *neigh;
4078 struct net_device *dev;
4079 struct fib_result res;
4085 dev = dev_get_by_index_rcu(net, params->ifindex);
4089 /* verify forwarding is enabled on this interface */
4090 in_dev = __in_dev_get_rcu(dev);
4091 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4094 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4096 fl4.flowi4_oif = params->ifindex;
4098 fl4.flowi4_iif = params->ifindex;
4101 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4102 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4103 fl4.flowi4_flags = 0;
4105 fl4.flowi4_proto = params->l4_protocol;
4106 fl4.daddr = params->ipv4_dst;
4107 fl4.saddr = params->ipv4_src;
4108 fl4.fl4_sport = params->sport;
4109 fl4.fl4_dport = params->dport;
4111 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4112 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4113 struct fib_table *tb;
4115 tb = fib_get_table(net, tbid);
4119 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4121 fl4.flowi4_mark = 0;
4122 fl4.flowi4_secid = 0;
4123 fl4.flowi4_tun_key.tun_id = 0;
4124 fl4.flowi4_uid = sock_net_uid(net, NULL);
4126 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4129 if (err || res.type != RTN_UNICAST)
4132 if (res.fi->fib_nhs > 1)
4133 fib_select_path(net, &res, &fl4, NULL);
4136 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4137 if (params->tot_len > mtu)
4141 nh = &res.fi->fib_nh[res.nh_sel];
4143 /* do not handle lwt encaps right now */
4144 if (nh->nh_lwtstate)
4152 params->ipv4_dst = nh->nh_gw;
4154 params->rt_metric = res.fi->fib_priority;
4156 /* xdp and cls_bpf programs are run in RCU-bh so
4157 * rcu_read_lock_bh is not needed here
4159 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4161 return bpf_fib_set_fwd_params(params, neigh, dev);
4167 #if IS_ENABLED(CONFIG_IPV6)
4168 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4169 u32 flags, bool check_mtu)
4171 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4172 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4173 struct neighbour *neigh;
4174 struct net_device *dev;
4175 struct inet6_dev *idev;
4176 struct fib6_info *f6i;
4182 /* link local addresses are never forwarded */
4183 if (rt6_need_strict(dst) || rt6_need_strict(src))
4186 dev = dev_get_by_index_rcu(net, params->ifindex);
4190 idev = __in6_dev_get_safely(dev);
4191 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4194 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4196 oif = fl6.flowi6_oif = params->ifindex;
4198 oif = fl6.flowi6_iif = params->ifindex;
4200 strict = RT6_LOOKUP_F_HAS_SADDR;
4202 fl6.flowlabel = params->flowlabel;
4203 fl6.flowi6_scope = 0;
4204 fl6.flowi6_flags = 0;
4207 fl6.flowi6_proto = params->l4_protocol;
4210 fl6.fl6_sport = params->sport;
4211 fl6.fl6_dport = params->dport;
4213 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4214 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4215 struct fib6_table *tb;
4217 tb = ipv6_stub->fib6_get_table(net, tbid);
4221 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4223 fl6.flowi6_mark = 0;
4224 fl6.flowi6_secid = 0;
4225 fl6.flowi6_tun_key.tun_id = 0;
4226 fl6.flowi6_uid = sock_net_uid(net, NULL);
4228 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4231 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4234 if (unlikely(f6i->fib6_flags & RTF_REJECT ||
4235 f6i->fib6_type != RTN_UNICAST))
4238 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4239 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4240 fl6.flowi6_oif, NULL,
4244 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4245 if (params->tot_len > mtu)
4249 if (f6i->fib6_nh.nh_lwtstate)
4252 if (f6i->fib6_flags & RTF_GATEWAY)
4253 *dst = f6i->fib6_nh.nh_gw;
4255 dev = f6i->fib6_nh.nh_dev;
4256 params->rt_metric = f6i->fib6_metric;
4258 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4259 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4260 * because we need to get nd_tbl via the stub
4262 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4263 ndisc_hashfn, dst, dev);
4265 return bpf_fib_set_fwd_params(params, neigh, dev);
4271 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4272 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4274 if (plen < sizeof(*params))
4277 switch (params->family) {
4278 #if IS_ENABLED(CONFIG_INET)
4280 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4283 #if IS_ENABLED(CONFIG_IPV6)
4285 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4292 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4293 .func = bpf_xdp_fib_lookup,
4295 .ret_type = RET_INTEGER,
4296 .arg1_type = ARG_PTR_TO_CTX,
4297 .arg2_type = ARG_PTR_TO_MEM,
4298 .arg3_type = ARG_CONST_SIZE,
4299 .arg4_type = ARG_ANYTHING,
4302 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4303 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4305 struct net *net = dev_net(skb->dev);
4308 if (plen < sizeof(*params))
4311 switch (params->family) {
4312 #if IS_ENABLED(CONFIG_INET)
4314 index = bpf_ipv4_fib_lookup(net, params, flags, false);
4317 #if IS_ENABLED(CONFIG_IPV6)
4319 index = bpf_ipv6_fib_lookup(net, params, flags, false);
4325 struct net_device *dev;
4327 dev = dev_get_by_index_rcu(net, index);
4328 if (!is_skb_forwardable(dev, skb))
4335 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4336 .func = bpf_skb_fib_lookup,
4338 .ret_type = RET_INTEGER,
4339 .arg1_type = ARG_PTR_TO_CTX,
4340 .arg2_type = ARG_PTR_TO_MEM,
4341 .arg3_type = ARG_CONST_SIZE,
4342 .arg4_type = ARG_ANYTHING,
4345 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4346 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4349 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4351 if (!seg6_validate_srh(srh, len))
4355 case BPF_LWT_ENCAP_SEG6_INLINE:
4356 if (skb->protocol != htons(ETH_P_IPV6))
4359 err = seg6_do_srh_inline(skb, srh);
4361 case BPF_LWT_ENCAP_SEG6:
4362 skb_reset_inner_headers(skb);
4363 skb->encapsulation = 1;
4364 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4370 bpf_compute_data_pointers(skb);
4374 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4375 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4377 return seg6_lookup_nexthop(skb, NULL, 0);
4379 #endif /* CONFIG_IPV6_SEG6_BPF */
4381 BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4385 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4386 case BPF_LWT_ENCAP_SEG6:
4387 case BPF_LWT_ENCAP_SEG6_INLINE:
4388 return bpf_push_seg6_encap(skb, type, hdr, len);
4395 static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4396 .func = bpf_lwt_push_encap,
4398 .ret_type = RET_INTEGER,
4399 .arg1_type = ARG_PTR_TO_CTX,
4400 .arg2_type = ARG_ANYTHING,
4401 .arg3_type = ARG_PTR_TO_MEM,
4402 .arg4_type = ARG_CONST_SIZE
4405 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4406 const void *, from, u32, len)
4408 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4409 struct seg6_bpf_srh_state *srh_state =
4410 this_cpu_ptr(&seg6_bpf_srh_states);
4411 void *srh_tlvs, *srh_end, *ptr;
4412 struct ipv6_sr_hdr *srh;
4415 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4418 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4419 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4420 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4422 ptr = skb->data + offset;
4423 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4424 srh_state->valid = 0;
4425 else if (ptr < (void *)&srh->flags ||
4426 ptr + len > (void *)&srh->segments)
4429 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4432 memcpy(skb->data + offset, from, len);
4434 #else /* CONFIG_IPV6_SEG6_BPF */
4439 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4440 .func = bpf_lwt_seg6_store_bytes,
4442 .ret_type = RET_INTEGER,
4443 .arg1_type = ARG_PTR_TO_CTX,
4444 .arg2_type = ARG_ANYTHING,
4445 .arg3_type = ARG_PTR_TO_MEM,
4446 .arg4_type = ARG_CONST_SIZE
4449 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4450 u32, action, void *, param, u32, param_len)
4452 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4453 struct seg6_bpf_srh_state *srh_state =
4454 this_cpu_ptr(&seg6_bpf_srh_states);
4455 struct ipv6_sr_hdr *srh;
4459 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4461 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4463 if (!srh_state->valid) {
4464 if (unlikely((srh_state->hdrlen & 7) != 0))
4467 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
4468 if (unlikely(!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3)))
4471 srh_state->valid = 1;
4475 case SEG6_LOCAL_ACTION_END_X:
4476 if (param_len != sizeof(struct in6_addr))
4478 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4479 case SEG6_LOCAL_ACTION_END_T:
4480 if (param_len != sizeof(int))
4482 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4483 case SEG6_LOCAL_ACTION_END_B6:
4484 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4488 ((struct ipv6_sr_hdr *)param)->hdrlen << 3;
4490 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
4491 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4495 ((struct ipv6_sr_hdr *)param)->hdrlen << 3;
4500 #else /* CONFIG_IPV6_SEG6_BPF */
4505 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4506 .func = bpf_lwt_seg6_action,
4508 .ret_type = RET_INTEGER,
4509 .arg1_type = ARG_PTR_TO_CTX,
4510 .arg2_type = ARG_ANYTHING,
4511 .arg3_type = ARG_PTR_TO_MEM,
4512 .arg4_type = ARG_CONST_SIZE
4515 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4518 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4519 struct seg6_bpf_srh_state *srh_state =
4520 this_cpu_ptr(&seg6_bpf_srh_states);
4521 void *srh_end, *srh_tlvs, *ptr;
4522 struct ipv6_sr_hdr *srh;
4523 struct ipv6hdr *hdr;
4527 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4529 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4531 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4532 ((srh->first_segment + 1) << 4));
4533 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4535 ptr = skb->data + offset;
4537 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4539 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4543 ret = skb_cow_head(skb, len);
4544 if (unlikely(ret < 0))
4547 ret = bpf_skb_net_hdr_push(skb, offset, len);
4549 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4552 bpf_compute_data_pointers(skb);
4553 if (unlikely(ret < 0))
4556 hdr = (struct ipv6hdr *)skb->data;
4557 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4559 srh_state->hdrlen += len;
4560 srh_state->valid = 0;
4562 #else /* CONFIG_IPV6_SEG6_BPF */
4567 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
4568 .func = bpf_lwt_seg6_adjust_srh,
4570 .ret_type = RET_INTEGER,
4571 .arg1_type = ARG_PTR_TO_CTX,
4572 .arg2_type = ARG_ANYTHING,
4573 .arg3_type = ARG_ANYTHING,
4576 bool bpf_helper_changes_pkt_data(void *func)
4578 if (func == bpf_skb_vlan_push ||
4579 func == bpf_skb_vlan_pop ||
4580 func == bpf_skb_store_bytes ||
4581 func == bpf_skb_change_proto ||
4582 func == bpf_skb_change_head ||
4583 func == bpf_skb_change_tail ||
4584 func == bpf_skb_adjust_room ||
4585 func == bpf_skb_pull_data ||
4586 func == bpf_clone_redirect ||
4587 func == bpf_l3_csum_replace ||
4588 func == bpf_l4_csum_replace ||
4589 func == bpf_xdp_adjust_head ||
4590 func == bpf_xdp_adjust_meta ||
4591 func == bpf_msg_pull_data ||
4592 func == bpf_xdp_adjust_tail ||
4593 func == bpf_lwt_push_encap ||
4594 func == bpf_lwt_seg6_store_bytes ||
4595 func == bpf_lwt_seg6_adjust_srh ||
4596 func == bpf_lwt_seg6_action
4603 static const struct bpf_func_proto *
4604 bpf_base_func_proto(enum bpf_func_id func_id)
4607 case BPF_FUNC_map_lookup_elem:
4608 return &bpf_map_lookup_elem_proto;
4609 case BPF_FUNC_map_update_elem:
4610 return &bpf_map_update_elem_proto;
4611 case BPF_FUNC_map_delete_elem:
4612 return &bpf_map_delete_elem_proto;
4613 case BPF_FUNC_get_prandom_u32:
4614 return &bpf_get_prandom_u32_proto;
4615 case BPF_FUNC_get_smp_processor_id:
4616 return &bpf_get_raw_smp_processor_id_proto;
4617 case BPF_FUNC_get_numa_node_id:
4618 return &bpf_get_numa_node_id_proto;
4619 case BPF_FUNC_tail_call:
4620 return &bpf_tail_call_proto;
4621 case BPF_FUNC_ktime_get_ns:
4622 return &bpf_ktime_get_ns_proto;
4623 case BPF_FUNC_trace_printk:
4624 if (capable(CAP_SYS_ADMIN))
4625 return bpf_get_trace_printk_proto();
4631 static const struct bpf_func_proto *
4632 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4635 /* inet and inet6 sockets are created in a process
4636 * context so there is always a valid uid/gid
4638 case BPF_FUNC_get_current_uid_gid:
4639 return &bpf_get_current_uid_gid_proto;
4641 return bpf_base_func_proto(func_id);
4645 static const struct bpf_func_proto *
4646 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4649 /* inet and inet6 sockets are created in a process
4650 * context so there is always a valid uid/gid
4652 case BPF_FUNC_get_current_uid_gid:
4653 return &bpf_get_current_uid_gid_proto;
4655 switch (prog->expected_attach_type) {
4656 case BPF_CGROUP_INET4_CONNECT:
4657 case BPF_CGROUP_INET6_CONNECT:
4658 return &bpf_bind_proto;
4663 return bpf_base_func_proto(func_id);
4667 static const struct bpf_func_proto *
4668 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4671 case BPF_FUNC_skb_load_bytes:
4672 return &bpf_skb_load_bytes_proto;
4673 case BPF_FUNC_skb_load_bytes_relative:
4674 return &bpf_skb_load_bytes_relative_proto;
4675 case BPF_FUNC_get_socket_cookie:
4676 return &bpf_get_socket_cookie_proto;
4677 case BPF_FUNC_get_socket_uid:
4678 return &bpf_get_socket_uid_proto;
4680 return bpf_base_func_proto(func_id);
4684 static const struct bpf_func_proto *
4685 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4688 case BPF_FUNC_skb_store_bytes:
4689 return &bpf_skb_store_bytes_proto;
4690 case BPF_FUNC_skb_load_bytes:
4691 return &bpf_skb_load_bytes_proto;
4692 case BPF_FUNC_skb_load_bytes_relative:
4693 return &bpf_skb_load_bytes_relative_proto;
4694 case BPF_FUNC_skb_pull_data:
4695 return &bpf_skb_pull_data_proto;
4696 case BPF_FUNC_csum_diff:
4697 return &bpf_csum_diff_proto;
4698 case BPF_FUNC_csum_update:
4699 return &bpf_csum_update_proto;
4700 case BPF_FUNC_l3_csum_replace:
4701 return &bpf_l3_csum_replace_proto;
4702 case BPF_FUNC_l4_csum_replace:
4703 return &bpf_l4_csum_replace_proto;
4704 case BPF_FUNC_clone_redirect:
4705 return &bpf_clone_redirect_proto;
4706 case BPF_FUNC_get_cgroup_classid:
4707 return &bpf_get_cgroup_classid_proto;
4708 case BPF_FUNC_skb_vlan_push:
4709 return &bpf_skb_vlan_push_proto;
4710 case BPF_FUNC_skb_vlan_pop:
4711 return &bpf_skb_vlan_pop_proto;
4712 case BPF_FUNC_skb_change_proto:
4713 return &bpf_skb_change_proto_proto;
4714 case BPF_FUNC_skb_change_type:
4715 return &bpf_skb_change_type_proto;
4716 case BPF_FUNC_skb_adjust_room:
4717 return &bpf_skb_adjust_room_proto;
4718 case BPF_FUNC_skb_change_tail:
4719 return &bpf_skb_change_tail_proto;
4720 case BPF_FUNC_skb_get_tunnel_key:
4721 return &bpf_skb_get_tunnel_key_proto;
4722 case BPF_FUNC_skb_set_tunnel_key:
4723 return bpf_get_skb_set_tunnel_proto(func_id);
4724 case BPF_FUNC_skb_get_tunnel_opt:
4725 return &bpf_skb_get_tunnel_opt_proto;
4726 case BPF_FUNC_skb_set_tunnel_opt:
4727 return bpf_get_skb_set_tunnel_proto(func_id);
4728 case BPF_FUNC_redirect:
4729 return &bpf_redirect_proto;
4730 case BPF_FUNC_get_route_realm:
4731 return &bpf_get_route_realm_proto;
4732 case BPF_FUNC_get_hash_recalc:
4733 return &bpf_get_hash_recalc_proto;
4734 case BPF_FUNC_set_hash_invalid:
4735 return &bpf_set_hash_invalid_proto;
4736 case BPF_FUNC_set_hash:
4737 return &bpf_set_hash_proto;
4738 case BPF_FUNC_perf_event_output:
4739 return &bpf_skb_event_output_proto;
4740 case BPF_FUNC_get_smp_processor_id:
4741 return &bpf_get_smp_processor_id_proto;
4742 case BPF_FUNC_skb_under_cgroup:
4743 return &bpf_skb_under_cgroup_proto;
4744 case BPF_FUNC_get_socket_cookie:
4745 return &bpf_get_socket_cookie_proto;
4746 case BPF_FUNC_get_socket_uid:
4747 return &bpf_get_socket_uid_proto;
4749 case BPF_FUNC_skb_get_xfrm_state:
4750 return &bpf_skb_get_xfrm_state_proto;
4752 case BPF_FUNC_fib_lookup:
4753 return &bpf_skb_fib_lookup_proto;
4755 return bpf_base_func_proto(func_id);
4759 static const struct bpf_func_proto *
4760 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4763 case BPF_FUNC_perf_event_output:
4764 return &bpf_xdp_event_output_proto;
4765 case BPF_FUNC_get_smp_processor_id:
4766 return &bpf_get_smp_processor_id_proto;
4767 case BPF_FUNC_csum_diff:
4768 return &bpf_csum_diff_proto;
4769 case BPF_FUNC_xdp_adjust_head:
4770 return &bpf_xdp_adjust_head_proto;
4771 case BPF_FUNC_xdp_adjust_meta:
4772 return &bpf_xdp_adjust_meta_proto;
4773 case BPF_FUNC_redirect:
4774 return &bpf_xdp_redirect_proto;
4775 case BPF_FUNC_redirect_map:
4776 return &bpf_xdp_redirect_map_proto;
4777 case BPF_FUNC_xdp_adjust_tail:
4778 return &bpf_xdp_adjust_tail_proto;
4779 case BPF_FUNC_fib_lookup:
4780 return &bpf_xdp_fib_lookup_proto;
4782 return bpf_base_func_proto(func_id);
4786 static const struct bpf_func_proto *
4787 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4790 case BPF_FUNC_setsockopt:
4791 return &bpf_setsockopt_proto;
4792 case BPF_FUNC_getsockopt:
4793 return &bpf_getsockopt_proto;
4794 case BPF_FUNC_sock_ops_cb_flags_set:
4795 return &bpf_sock_ops_cb_flags_set_proto;
4796 case BPF_FUNC_sock_map_update:
4797 return &bpf_sock_map_update_proto;
4798 case BPF_FUNC_sock_hash_update:
4799 return &bpf_sock_hash_update_proto;
4801 return bpf_base_func_proto(func_id);
4805 static const struct bpf_func_proto *
4806 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4809 case BPF_FUNC_msg_redirect_map:
4810 return &bpf_msg_redirect_map_proto;
4811 case BPF_FUNC_msg_redirect_hash:
4812 return &bpf_msg_redirect_hash_proto;
4813 case BPF_FUNC_msg_apply_bytes:
4814 return &bpf_msg_apply_bytes_proto;
4815 case BPF_FUNC_msg_cork_bytes:
4816 return &bpf_msg_cork_bytes_proto;
4817 case BPF_FUNC_msg_pull_data:
4818 return &bpf_msg_pull_data_proto;
4820 return bpf_base_func_proto(func_id);
4824 static const struct bpf_func_proto *
4825 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4828 case BPF_FUNC_skb_store_bytes:
4829 return &bpf_skb_store_bytes_proto;
4830 case BPF_FUNC_skb_load_bytes:
4831 return &bpf_skb_load_bytes_proto;
4832 case BPF_FUNC_skb_pull_data:
4833 return &bpf_skb_pull_data_proto;
4834 case BPF_FUNC_skb_change_tail:
4835 return &bpf_skb_change_tail_proto;
4836 case BPF_FUNC_skb_change_head:
4837 return &bpf_skb_change_head_proto;
4838 case BPF_FUNC_get_socket_cookie:
4839 return &bpf_get_socket_cookie_proto;
4840 case BPF_FUNC_get_socket_uid:
4841 return &bpf_get_socket_uid_proto;
4842 case BPF_FUNC_sk_redirect_map:
4843 return &bpf_sk_redirect_map_proto;
4844 case BPF_FUNC_sk_redirect_hash:
4845 return &bpf_sk_redirect_hash_proto;
4847 return bpf_base_func_proto(func_id);
4851 static const struct bpf_func_proto *
4852 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4855 case BPF_FUNC_skb_load_bytes:
4856 return &bpf_skb_load_bytes_proto;
4857 case BPF_FUNC_skb_pull_data:
4858 return &bpf_skb_pull_data_proto;
4859 case BPF_FUNC_csum_diff:
4860 return &bpf_csum_diff_proto;
4861 case BPF_FUNC_get_cgroup_classid:
4862 return &bpf_get_cgroup_classid_proto;
4863 case BPF_FUNC_get_route_realm:
4864 return &bpf_get_route_realm_proto;
4865 case BPF_FUNC_get_hash_recalc:
4866 return &bpf_get_hash_recalc_proto;
4867 case BPF_FUNC_perf_event_output:
4868 return &bpf_skb_event_output_proto;
4869 case BPF_FUNC_get_smp_processor_id:
4870 return &bpf_get_smp_processor_id_proto;
4871 case BPF_FUNC_skb_under_cgroup:
4872 return &bpf_skb_under_cgroup_proto;
4874 return bpf_base_func_proto(func_id);
4878 static const struct bpf_func_proto *
4879 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4882 case BPF_FUNC_lwt_push_encap:
4883 return &bpf_lwt_push_encap_proto;
4885 return lwt_out_func_proto(func_id, prog);
4889 static const struct bpf_func_proto *
4890 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4893 case BPF_FUNC_skb_get_tunnel_key:
4894 return &bpf_skb_get_tunnel_key_proto;
4895 case BPF_FUNC_skb_set_tunnel_key:
4896 return bpf_get_skb_set_tunnel_proto(func_id);
4897 case BPF_FUNC_skb_get_tunnel_opt:
4898 return &bpf_skb_get_tunnel_opt_proto;
4899 case BPF_FUNC_skb_set_tunnel_opt:
4900 return bpf_get_skb_set_tunnel_proto(func_id);
4901 case BPF_FUNC_redirect:
4902 return &bpf_redirect_proto;
4903 case BPF_FUNC_clone_redirect:
4904 return &bpf_clone_redirect_proto;
4905 case BPF_FUNC_skb_change_tail:
4906 return &bpf_skb_change_tail_proto;
4907 case BPF_FUNC_skb_change_head:
4908 return &bpf_skb_change_head_proto;
4909 case BPF_FUNC_skb_store_bytes:
4910 return &bpf_skb_store_bytes_proto;
4911 case BPF_FUNC_csum_update:
4912 return &bpf_csum_update_proto;
4913 case BPF_FUNC_l3_csum_replace:
4914 return &bpf_l3_csum_replace_proto;
4915 case BPF_FUNC_l4_csum_replace:
4916 return &bpf_l4_csum_replace_proto;
4917 case BPF_FUNC_set_hash_invalid:
4918 return &bpf_set_hash_invalid_proto;
4920 return lwt_out_func_proto(func_id, prog);
4924 static const struct bpf_func_proto *
4925 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4928 case BPF_FUNC_lwt_seg6_store_bytes:
4929 return &bpf_lwt_seg6_store_bytes_proto;
4930 case BPF_FUNC_lwt_seg6_action:
4931 return &bpf_lwt_seg6_action_proto;
4932 case BPF_FUNC_lwt_seg6_adjust_srh:
4933 return &bpf_lwt_seg6_adjust_srh_proto;
4935 return lwt_out_func_proto(func_id, prog);
4939 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
4940 const struct bpf_prog *prog,
4941 struct bpf_insn_access_aux *info)
4943 const int size_default = sizeof(__u32);
4945 if (off < 0 || off >= sizeof(struct __sk_buff))
4948 /* The verifier guarantees that size > 0. */
4949 if (off % size != 0)
4953 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
4954 if (off + size > offsetofend(struct __sk_buff, cb[4]))
4957 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
4958 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
4959 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
4960 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
4961 case bpf_ctx_range(struct __sk_buff, data):
4962 case bpf_ctx_range(struct __sk_buff, data_meta):
4963 case bpf_ctx_range(struct __sk_buff, data_end):
4964 if (size != size_default)
4968 /* Only narrow read access allowed for now. */
4969 if (type == BPF_WRITE) {
4970 if (size != size_default)
4973 bpf_ctx_record_field_size(info, size_default);
4974 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
4982 static bool sk_filter_is_valid_access(int off, int size,
4983 enum bpf_access_type type,
4984 const struct bpf_prog *prog,
4985 struct bpf_insn_access_aux *info)
4988 case bpf_ctx_range(struct __sk_buff, tc_classid):
4989 case bpf_ctx_range(struct __sk_buff, data):
4990 case bpf_ctx_range(struct __sk_buff, data_meta):
4991 case bpf_ctx_range(struct __sk_buff, data_end):
4992 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
4996 if (type == BPF_WRITE) {
4998 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5005 return bpf_skb_is_valid_access(off, size, type, prog, info);
5008 static bool lwt_is_valid_access(int off, int size,
5009 enum bpf_access_type type,
5010 const struct bpf_prog *prog,
5011 struct bpf_insn_access_aux *info)
5014 case bpf_ctx_range(struct __sk_buff, tc_classid):
5015 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5016 case bpf_ctx_range(struct __sk_buff, data_meta):
5020 if (type == BPF_WRITE) {
5022 case bpf_ctx_range(struct __sk_buff, mark):
5023 case bpf_ctx_range(struct __sk_buff, priority):
5024 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5032 case bpf_ctx_range(struct __sk_buff, data):
5033 info->reg_type = PTR_TO_PACKET;
5035 case bpf_ctx_range(struct __sk_buff, data_end):
5036 info->reg_type = PTR_TO_PACKET_END;
5040 return bpf_skb_is_valid_access(off, size, type, prog, info);
5043 /* Attach type specific accesses */
5044 static bool __sock_filter_check_attach_type(int off,
5045 enum bpf_access_type access_type,
5046 enum bpf_attach_type attach_type)
5049 case offsetof(struct bpf_sock, bound_dev_if):
5050 case offsetof(struct bpf_sock, mark):
5051 case offsetof(struct bpf_sock, priority):
5052 switch (attach_type) {
5053 case BPF_CGROUP_INET_SOCK_CREATE:
5058 case bpf_ctx_range(struct bpf_sock, src_ip4):
5059 switch (attach_type) {
5060 case BPF_CGROUP_INET4_POST_BIND:
5065 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5066 switch (attach_type) {
5067 case BPF_CGROUP_INET6_POST_BIND:
5072 case bpf_ctx_range(struct bpf_sock, src_port):
5073 switch (attach_type) {
5074 case BPF_CGROUP_INET4_POST_BIND:
5075 case BPF_CGROUP_INET6_POST_BIND:
5082 return access_type == BPF_READ;
5087 static bool __sock_filter_check_size(int off, int size,
5088 struct bpf_insn_access_aux *info)
5090 const int size_default = sizeof(__u32);
5093 case bpf_ctx_range(struct bpf_sock, src_ip4):
5094 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5095 bpf_ctx_record_field_size(info, size_default);
5096 return bpf_ctx_narrow_access_ok(off, size, size_default);
5099 return size == size_default;
5102 static bool sock_filter_is_valid_access(int off, int size,
5103 enum bpf_access_type type,
5104 const struct bpf_prog *prog,
5105 struct bpf_insn_access_aux *info)
5107 if (off < 0 || off >= sizeof(struct bpf_sock))
5109 if (off % size != 0)
5111 if (!__sock_filter_check_attach_type(off, type,
5112 prog->expected_attach_type))
5114 if (!__sock_filter_check_size(off, size, info))
5119 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5120 const struct bpf_prog *prog, int drop_verdict)
5122 struct bpf_insn *insn = insn_buf;
5127 /* if (!skb->cloned)
5130 * (Fast-path, otherwise approximation that we might be
5131 * a clone, do the rest in helper.)
5133 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5134 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5135 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5137 /* ret = bpf_skb_pull_data(skb, 0); */
5138 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5139 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5140 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5141 BPF_FUNC_skb_pull_data);
5144 * return TC_ACT_SHOT;
5146 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
5147 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
5148 *insn++ = BPF_EXIT_INSN();
5151 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
5153 *insn++ = prog->insnsi[0];
5155 return insn - insn_buf;
5158 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
5159 struct bpf_insn *insn_buf)
5161 bool indirect = BPF_MODE(orig->code) == BPF_IND;
5162 struct bpf_insn *insn = insn_buf;
5164 /* We're guaranteed here that CTX is in R6. */
5165 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
5167 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
5169 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
5171 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
5174 switch (BPF_SIZE(orig->code)) {
5176 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
5179 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
5182 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
5186 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
5187 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
5188 *insn++ = BPF_EXIT_INSN();
5190 return insn - insn_buf;
5193 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
5194 const struct bpf_prog *prog)
5196 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
5199 static bool tc_cls_act_is_valid_access(int off, int size,
5200 enum bpf_access_type type,
5201 const struct bpf_prog *prog,
5202 struct bpf_insn_access_aux *info)
5204 if (type == BPF_WRITE) {
5206 case bpf_ctx_range(struct __sk_buff, mark):
5207 case bpf_ctx_range(struct __sk_buff, tc_index):
5208 case bpf_ctx_range(struct __sk_buff, priority):
5209 case bpf_ctx_range(struct __sk_buff, tc_classid):
5210 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5218 case bpf_ctx_range(struct __sk_buff, data):
5219 info->reg_type = PTR_TO_PACKET;
5221 case bpf_ctx_range(struct __sk_buff, data_meta):
5222 info->reg_type = PTR_TO_PACKET_META;
5224 case bpf_ctx_range(struct __sk_buff, data_end):
5225 info->reg_type = PTR_TO_PACKET_END;
5227 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5231 return bpf_skb_is_valid_access(off, size, type, prog, info);
5234 static bool __is_valid_xdp_access(int off, int size)
5236 if (off < 0 || off >= sizeof(struct xdp_md))
5238 if (off % size != 0)
5240 if (size != sizeof(__u32))
5246 static bool xdp_is_valid_access(int off, int size,
5247 enum bpf_access_type type,
5248 const struct bpf_prog *prog,
5249 struct bpf_insn_access_aux *info)
5251 if (type == BPF_WRITE) {
5252 if (bpf_prog_is_dev_bound(prog->aux)) {
5254 case offsetof(struct xdp_md, rx_queue_index):
5255 return __is_valid_xdp_access(off, size);
5262 case offsetof(struct xdp_md, data):
5263 info->reg_type = PTR_TO_PACKET;
5265 case offsetof(struct xdp_md, data_meta):
5266 info->reg_type = PTR_TO_PACKET_META;
5268 case offsetof(struct xdp_md, data_end):
5269 info->reg_type = PTR_TO_PACKET_END;
5273 return __is_valid_xdp_access(off, size);
5276 void bpf_warn_invalid_xdp_action(u32 act)
5278 const u32 act_max = XDP_REDIRECT;
5280 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
5281 act > act_max ? "Illegal" : "Driver unsupported",
5284 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
5286 static bool sock_addr_is_valid_access(int off, int size,
5287 enum bpf_access_type type,
5288 const struct bpf_prog *prog,
5289 struct bpf_insn_access_aux *info)
5291 const int size_default = sizeof(__u32);
5293 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
5295 if (off % size != 0)
5298 /* Disallow access to IPv6 fields from IPv4 contex and vise
5302 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5303 switch (prog->expected_attach_type) {
5304 case BPF_CGROUP_INET4_BIND:
5305 case BPF_CGROUP_INET4_CONNECT:
5311 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5312 switch (prog->expected_attach_type) {
5313 case BPF_CGROUP_INET6_BIND:
5314 case BPF_CGROUP_INET6_CONNECT:
5323 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5324 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5325 /* Only narrow read access allowed for now. */
5326 if (type == BPF_READ) {
5327 bpf_ctx_record_field_size(info, size_default);
5328 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5331 if (size != size_default)
5335 case bpf_ctx_range(struct bpf_sock_addr, user_port):
5336 if (size != size_default)
5340 if (type == BPF_READ) {
5341 if (size != size_default)
5351 static bool sock_ops_is_valid_access(int off, int size,
5352 enum bpf_access_type type,
5353 const struct bpf_prog *prog,
5354 struct bpf_insn_access_aux *info)
5356 const int size_default = sizeof(__u32);
5358 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5361 /* The verifier guarantees that size > 0. */
5362 if (off % size != 0)
5365 if (type == BPF_WRITE) {
5367 case offsetof(struct bpf_sock_ops, reply):
5368 case offsetof(struct bpf_sock_ops, sk_txhash):
5369 if (size != size_default)
5377 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5379 if (size != sizeof(__u64))
5383 if (size != size_default)
5392 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5393 const struct bpf_prog *prog)
5395 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
5398 static bool sk_skb_is_valid_access(int off, int size,
5399 enum bpf_access_type type,
5400 const struct bpf_prog *prog,
5401 struct bpf_insn_access_aux *info)
5404 case bpf_ctx_range(struct __sk_buff, tc_classid):
5405 case bpf_ctx_range(struct __sk_buff, data_meta):
5409 if (type == BPF_WRITE) {
5411 case bpf_ctx_range(struct __sk_buff, tc_index):
5412 case bpf_ctx_range(struct __sk_buff, priority):
5420 case bpf_ctx_range(struct __sk_buff, mark):
5422 case bpf_ctx_range(struct __sk_buff, data):
5423 info->reg_type = PTR_TO_PACKET;
5425 case bpf_ctx_range(struct __sk_buff, data_end):
5426 info->reg_type = PTR_TO_PACKET_END;
5430 return bpf_skb_is_valid_access(off, size, type, prog, info);
5433 static bool sk_msg_is_valid_access(int off, int size,
5434 enum bpf_access_type type,
5435 const struct bpf_prog *prog,
5436 struct bpf_insn_access_aux *info)
5438 if (type == BPF_WRITE)
5442 case offsetof(struct sk_msg_md, data):
5443 info->reg_type = PTR_TO_PACKET;
5444 if (size != sizeof(__u64))
5447 case offsetof(struct sk_msg_md, data_end):
5448 info->reg_type = PTR_TO_PACKET_END;
5449 if (size != sizeof(__u64))
5453 if (size != sizeof(__u32))
5457 if (off < 0 || off >= sizeof(struct sk_msg_md))
5459 if (off % size != 0)
5465 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
5466 const struct bpf_insn *si,
5467 struct bpf_insn *insn_buf,
5468 struct bpf_prog *prog, u32 *target_size)
5470 struct bpf_insn *insn = insn_buf;
5474 case offsetof(struct __sk_buff, len):
5475 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5476 bpf_target_off(struct sk_buff, len, 4,
5480 case offsetof(struct __sk_buff, protocol):
5481 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5482 bpf_target_off(struct sk_buff, protocol, 2,
5486 case offsetof(struct __sk_buff, vlan_proto):
5487 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5488 bpf_target_off(struct sk_buff, vlan_proto, 2,
5492 case offsetof(struct __sk_buff, priority):
5493 if (type == BPF_WRITE)
5494 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5495 bpf_target_off(struct sk_buff, priority, 4,
5498 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5499 bpf_target_off(struct sk_buff, priority, 4,
5503 case offsetof(struct __sk_buff, ingress_ifindex):
5504 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5505 bpf_target_off(struct sk_buff, skb_iif, 4,
5509 case offsetof(struct __sk_buff, ifindex):
5510 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
5511 si->dst_reg, si->src_reg,
5512 offsetof(struct sk_buff, dev));
5513 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
5514 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5515 bpf_target_off(struct net_device, ifindex, 4,
5519 case offsetof(struct __sk_buff, hash):
5520 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5521 bpf_target_off(struct sk_buff, hash, 4,
5525 case offsetof(struct __sk_buff, mark):
5526 if (type == BPF_WRITE)
5527 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5528 bpf_target_off(struct sk_buff, mark, 4,
5531 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5532 bpf_target_off(struct sk_buff, mark, 4,
5536 case offsetof(struct __sk_buff, pkt_type):
5538 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
5540 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
5541 #ifdef __BIG_ENDIAN_BITFIELD
5542 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
5546 case offsetof(struct __sk_buff, queue_mapping):
5547 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5548 bpf_target_off(struct sk_buff, queue_mapping, 2,
5552 case offsetof(struct __sk_buff, vlan_present):
5553 case offsetof(struct __sk_buff, vlan_tci):
5554 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
5556 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5557 bpf_target_off(struct sk_buff, vlan_tci, 2,
5559 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
5560 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
5563 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
5564 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
5568 case offsetof(struct __sk_buff, cb[0]) ...
5569 offsetofend(struct __sk_buff, cb[4]) - 1:
5570 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
5571 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
5572 offsetof(struct qdisc_skb_cb, data)) %
5575 prog->cb_access = 1;
5577 off -= offsetof(struct __sk_buff, cb[0]);
5578 off += offsetof(struct sk_buff, cb);
5579 off += offsetof(struct qdisc_skb_cb, data);
5580 if (type == BPF_WRITE)
5581 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
5584 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
5588 case offsetof(struct __sk_buff, tc_classid):
5589 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
5592 off -= offsetof(struct __sk_buff, tc_classid);
5593 off += offsetof(struct sk_buff, cb);
5594 off += offsetof(struct qdisc_skb_cb, tc_classid);
5596 if (type == BPF_WRITE)
5597 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
5600 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
5604 case offsetof(struct __sk_buff, data):
5605 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
5606 si->dst_reg, si->src_reg,
5607 offsetof(struct sk_buff, data));
5610 case offsetof(struct __sk_buff, data_meta):
5612 off -= offsetof(struct __sk_buff, data_meta);
5613 off += offsetof(struct sk_buff, cb);
5614 off += offsetof(struct bpf_skb_data_end, data_meta);
5615 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5619 case offsetof(struct __sk_buff, data_end):
5621 off -= offsetof(struct __sk_buff, data_end);
5622 off += offsetof(struct sk_buff, cb);
5623 off += offsetof(struct bpf_skb_data_end, data_end);
5624 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5628 case offsetof(struct __sk_buff, tc_index):
5629 #ifdef CONFIG_NET_SCHED
5630 if (type == BPF_WRITE)
5631 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
5632 bpf_target_off(struct sk_buff, tc_index, 2,
5635 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5636 bpf_target_off(struct sk_buff, tc_index, 2,
5640 if (type == BPF_WRITE)
5641 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
5643 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5647 case offsetof(struct __sk_buff, napi_id):
5648 #if defined(CONFIG_NET_RX_BUSY_POLL)
5649 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5650 bpf_target_off(struct sk_buff, napi_id, 4,
5652 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
5653 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5656 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5659 case offsetof(struct __sk_buff, family):
5660 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
5662 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5663 si->dst_reg, si->src_reg,
5664 offsetof(struct sk_buff, sk));
5665 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5666 bpf_target_off(struct sock_common,
5670 case offsetof(struct __sk_buff, remote_ip4):
5671 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
5673 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5674 si->dst_reg, si->src_reg,
5675 offsetof(struct sk_buff, sk));
5676 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5677 bpf_target_off(struct sock_common,
5681 case offsetof(struct __sk_buff, local_ip4):
5682 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5683 skc_rcv_saddr) != 4);
5685 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5686 si->dst_reg, si->src_reg,
5687 offsetof(struct sk_buff, sk));
5688 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5689 bpf_target_off(struct sock_common,
5693 case offsetof(struct __sk_buff, remote_ip6[0]) ...
5694 offsetof(struct __sk_buff, remote_ip6[3]):
5695 #if IS_ENABLED(CONFIG_IPV6)
5696 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5697 skc_v6_daddr.s6_addr32[0]) != 4);
5700 off -= offsetof(struct __sk_buff, remote_ip6[0]);
5702 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5703 si->dst_reg, si->src_reg,
5704 offsetof(struct sk_buff, sk));
5705 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5706 offsetof(struct sock_common,
5707 skc_v6_daddr.s6_addr32[0]) +
5710 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5713 case offsetof(struct __sk_buff, local_ip6[0]) ...
5714 offsetof(struct __sk_buff, local_ip6[3]):
5715 #if IS_ENABLED(CONFIG_IPV6)
5716 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5717 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
5720 off -= offsetof(struct __sk_buff, local_ip6[0]);
5722 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5723 si->dst_reg, si->src_reg,
5724 offsetof(struct sk_buff, sk));
5725 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5726 offsetof(struct sock_common,
5727 skc_v6_rcv_saddr.s6_addr32[0]) +
5730 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5734 case offsetof(struct __sk_buff, remote_port):
5735 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
5737 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5738 si->dst_reg, si->src_reg,
5739 offsetof(struct sk_buff, sk));
5740 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5741 bpf_target_off(struct sock_common,
5744 #ifndef __BIG_ENDIAN_BITFIELD
5745 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
5749 case offsetof(struct __sk_buff, local_port):
5750 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
5752 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5753 si->dst_reg, si->src_reg,
5754 offsetof(struct sk_buff, sk));
5755 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5756 bpf_target_off(struct sock_common,
5757 skc_num, 2, target_size));
5761 return insn - insn_buf;
5764 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
5765 const struct bpf_insn *si,
5766 struct bpf_insn *insn_buf,
5767 struct bpf_prog *prog, u32 *target_size)
5769 struct bpf_insn *insn = insn_buf;
5773 case offsetof(struct bpf_sock, bound_dev_if):
5774 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
5776 if (type == BPF_WRITE)
5777 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5778 offsetof(struct sock, sk_bound_dev_if));
5780 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5781 offsetof(struct sock, sk_bound_dev_if));
5784 case offsetof(struct bpf_sock, mark):
5785 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
5787 if (type == BPF_WRITE)
5788 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5789 offsetof(struct sock, sk_mark));
5791 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5792 offsetof(struct sock, sk_mark));
5795 case offsetof(struct bpf_sock, priority):
5796 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
5798 if (type == BPF_WRITE)
5799 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5800 offsetof(struct sock, sk_priority));
5802 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5803 offsetof(struct sock, sk_priority));
5806 case offsetof(struct bpf_sock, family):
5807 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
5809 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5810 offsetof(struct sock, sk_family));
5813 case offsetof(struct bpf_sock, type):
5814 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5815 offsetof(struct sock, __sk_flags_offset));
5816 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
5817 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
5820 case offsetof(struct bpf_sock, protocol):
5821 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5822 offsetof(struct sock, __sk_flags_offset));
5823 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
5824 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
5827 case offsetof(struct bpf_sock, src_ip4):
5828 *insn++ = BPF_LDX_MEM(
5829 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
5830 bpf_target_off(struct sock_common, skc_rcv_saddr,
5831 FIELD_SIZEOF(struct sock_common,
5836 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5837 #if IS_ENABLED(CONFIG_IPV6)
5839 off -= offsetof(struct bpf_sock, src_ip6[0]);
5840 *insn++ = BPF_LDX_MEM(
5841 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
5844 skc_v6_rcv_saddr.s6_addr32[0],
5845 FIELD_SIZEOF(struct sock_common,
5846 skc_v6_rcv_saddr.s6_addr32[0]),
5847 target_size) + off);
5850 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5854 case offsetof(struct bpf_sock, src_port):
5855 *insn++ = BPF_LDX_MEM(
5856 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
5857 si->dst_reg, si->src_reg,
5858 bpf_target_off(struct sock_common, skc_num,
5859 FIELD_SIZEOF(struct sock_common,
5865 return insn - insn_buf;
5868 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
5869 const struct bpf_insn *si,
5870 struct bpf_insn *insn_buf,
5871 struct bpf_prog *prog, u32 *target_size)
5873 struct bpf_insn *insn = insn_buf;
5876 case offsetof(struct __sk_buff, ifindex):
5877 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
5878 si->dst_reg, si->src_reg,
5879 offsetof(struct sk_buff, dev));
5880 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5881 bpf_target_off(struct net_device, ifindex, 4,
5885 return bpf_convert_ctx_access(type, si, insn_buf, prog,
5889 return insn - insn_buf;
5892 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
5893 const struct bpf_insn *si,
5894 struct bpf_insn *insn_buf,
5895 struct bpf_prog *prog, u32 *target_size)
5897 struct bpf_insn *insn = insn_buf;
5900 case offsetof(struct xdp_md, data):
5901 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
5902 si->dst_reg, si->src_reg,
5903 offsetof(struct xdp_buff, data));
5905 case offsetof(struct xdp_md, data_meta):
5906 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
5907 si->dst_reg, si->src_reg,
5908 offsetof(struct xdp_buff, data_meta));
5910 case offsetof(struct xdp_md, data_end):
5911 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
5912 si->dst_reg, si->src_reg,
5913 offsetof(struct xdp_buff, data_end));
5915 case offsetof(struct xdp_md, ingress_ifindex):
5916 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
5917 si->dst_reg, si->src_reg,
5918 offsetof(struct xdp_buff, rxq));
5919 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
5920 si->dst_reg, si->dst_reg,
5921 offsetof(struct xdp_rxq_info, dev));
5922 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5923 offsetof(struct net_device, ifindex));
5925 case offsetof(struct xdp_md, rx_queue_index):
5926 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
5927 si->dst_reg, si->src_reg,
5928 offsetof(struct xdp_buff, rxq));
5929 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5930 offsetof(struct xdp_rxq_info,
5935 return insn - insn_buf;
5938 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
5939 * context Structure, F is Field in context structure that contains a pointer
5940 * to Nested Structure of type NS that has the field NF.
5942 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
5943 * sure that SIZE is not greater than actual size of S.F.NF.
5945 * If offset OFF is provided, the load happens from that offset relative to
5948 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
5950 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
5951 si->src_reg, offsetof(S, F)); \
5952 *insn++ = BPF_LDX_MEM( \
5953 SIZE, si->dst_reg, si->dst_reg, \
5954 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
5959 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
5960 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
5961 BPF_FIELD_SIZEOF(NS, NF), 0)
5963 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
5964 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
5966 * It doesn't support SIZE argument though since narrow stores are not
5967 * supported for now.
5969 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
5970 * "register" since two registers available in convert_ctx_access are not
5971 * enough: we can't override neither SRC, since it contains value to store, nor
5972 * DST since it contains pointer to context that may be used by later
5973 * instructions. But we need a temporary place to save pointer to nested
5974 * structure whose field we want to store to.
5976 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
5978 int tmp_reg = BPF_REG_9; \
5979 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
5981 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
5983 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
5985 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
5986 si->dst_reg, offsetof(S, F)); \
5987 *insn++ = BPF_STX_MEM( \
5988 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
5989 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
5992 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
5996 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
5999 if (type == BPF_WRITE) { \
6000 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6003 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6004 S, NS, F, NF, SIZE, OFF); \
6008 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6009 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6010 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6012 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6013 const struct bpf_insn *si,
6014 struct bpf_insn *insn_buf,
6015 struct bpf_prog *prog, u32 *target_size)
6017 struct bpf_insn *insn = insn_buf;
6021 case offsetof(struct bpf_sock_addr, user_family):
6022 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6023 struct sockaddr, uaddr, sa_family);
6026 case offsetof(struct bpf_sock_addr, user_ip4):
6027 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6028 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6029 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6032 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6034 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6035 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6036 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6037 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6041 case offsetof(struct bpf_sock_addr, user_port):
6042 /* To get port we need to know sa_family first and then treat
6043 * sockaddr as either sockaddr_in or sockaddr_in6.
6044 * Though we can simplify since port field has same offset and
6045 * size in both structures.
6046 * Here we check this invariant and use just one of the
6047 * structures if it's true.
6049 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
6050 offsetof(struct sockaddr_in6, sin6_port));
6051 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
6052 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
6053 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
6054 struct sockaddr_in6, uaddr,
6055 sin6_port, tmp_reg);
6058 case offsetof(struct bpf_sock_addr, family):
6059 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6060 struct sock, sk, sk_family);
6063 case offsetof(struct bpf_sock_addr, type):
6064 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6065 struct bpf_sock_addr_kern, struct sock, sk,
6066 __sk_flags_offset, BPF_W, 0);
6067 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6068 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6071 case offsetof(struct bpf_sock_addr, protocol):
6072 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6073 struct bpf_sock_addr_kern, struct sock, sk,
6074 __sk_flags_offset, BPF_W, 0);
6075 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6076 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
6081 return insn - insn_buf;
6084 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
6085 const struct bpf_insn *si,
6086 struct bpf_insn *insn_buf,
6087 struct bpf_prog *prog,
6090 struct bpf_insn *insn = insn_buf;
6094 case offsetof(struct bpf_sock_ops, op) ...
6095 offsetof(struct bpf_sock_ops, replylong[3]):
6096 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
6097 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
6098 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
6099 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
6100 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
6101 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
6103 off -= offsetof(struct bpf_sock_ops, op);
6104 off += offsetof(struct bpf_sock_ops_kern, op);
6105 if (type == BPF_WRITE)
6106 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6109 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6113 case offsetof(struct bpf_sock_ops, family):
6114 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6116 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6117 struct bpf_sock_ops_kern, sk),
6118 si->dst_reg, si->src_reg,
6119 offsetof(struct bpf_sock_ops_kern, sk));
6120 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6121 offsetof(struct sock_common, skc_family));
6124 case offsetof(struct bpf_sock_ops, remote_ip4):
6125 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6127 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6128 struct bpf_sock_ops_kern, sk),
6129 si->dst_reg, si->src_reg,
6130 offsetof(struct bpf_sock_ops_kern, sk));
6131 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6132 offsetof(struct sock_common, skc_daddr));
6135 case offsetof(struct bpf_sock_ops, local_ip4):
6136 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6137 skc_rcv_saddr) != 4);
6139 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6140 struct bpf_sock_ops_kern, sk),
6141 si->dst_reg, si->src_reg,
6142 offsetof(struct bpf_sock_ops_kern, sk));
6143 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6144 offsetof(struct sock_common,
6148 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
6149 offsetof(struct bpf_sock_ops, remote_ip6[3]):
6150 #if IS_ENABLED(CONFIG_IPV6)
6151 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6152 skc_v6_daddr.s6_addr32[0]) != 4);
6155 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
6156 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6157 struct bpf_sock_ops_kern, sk),
6158 si->dst_reg, si->src_reg,
6159 offsetof(struct bpf_sock_ops_kern, sk));
6160 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6161 offsetof(struct sock_common,
6162 skc_v6_daddr.s6_addr32[0]) +
6165 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6169 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
6170 offsetof(struct bpf_sock_ops, local_ip6[3]):
6171 #if IS_ENABLED(CONFIG_IPV6)
6172 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6173 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6176 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
6177 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6178 struct bpf_sock_ops_kern, sk),
6179 si->dst_reg, si->src_reg,
6180 offsetof(struct bpf_sock_ops_kern, sk));
6181 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6182 offsetof(struct sock_common,
6183 skc_v6_rcv_saddr.s6_addr32[0]) +
6186 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6190 case offsetof(struct bpf_sock_ops, remote_port):
6191 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6193 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6194 struct bpf_sock_ops_kern, sk),
6195 si->dst_reg, si->src_reg,
6196 offsetof(struct bpf_sock_ops_kern, sk));
6197 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6198 offsetof(struct sock_common, skc_dport));
6199 #ifndef __BIG_ENDIAN_BITFIELD
6200 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6204 case offsetof(struct bpf_sock_ops, local_port):
6205 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6207 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6208 struct bpf_sock_ops_kern, sk),
6209 si->dst_reg, si->src_reg,
6210 offsetof(struct bpf_sock_ops_kern, sk));
6211 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6212 offsetof(struct sock_common, skc_num));
6215 case offsetof(struct bpf_sock_ops, is_fullsock):
6216 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6217 struct bpf_sock_ops_kern,
6219 si->dst_reg, si->src_reg,
6220 offsetof(struct bpf_sock_ops_kern,
6224 case offsetof(struct bpf_sock_ops, state):
6225 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
6227 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6228 struct bpf_sock_ops_kern, sk),
6229 si->dst_reg, si->src_reg,
6230 offsetof(struct bpf_sock_ops_kern, sk));
6231 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
6232 offsetof(struct sock_common, skc_state));
6235 case offsetof(struct bpf_sock_ops, rtt_min):
6236 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
6237 sizeof(struct minmax));
6238 BUILD_BUG_ON(sizeof(struct minmax) <
6239 sizeof(struct minmax_sample));
6241 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6242 struct bpf_sock_ops_kern, sk),
6243 si->dst_reg, si->src_reg,
6244 offsetof(struct bpf_sock_ops_kern, sk));
6245 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6246 offsetof(struct tcp_sock, rtt_min) +
6247 FIELD_SIZEOF(struct minmax_sample, t));
6250 /* Helper macro for adding read access to tcp_sock or sock fields. */
6251 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6253 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6254 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6255 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6256 struct bpf_sock_ops_kern, \
6258 si->dst_reg, si->src_reg, \
6259 offsetof(struct bpf_sock_ops_kern, \
6261 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
6262 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6263 struct bpf_sock_ops_kern, sk),\
6264 si->dst_reg, si->src_reg, \
6265 offsetof(struct bpf_sock_ops_kern, sk));\
6266 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
6268 si->dst_reg, si->dst_reg, \
6269 offsetof(OBJ, OBJ_FIELD)); \
6272 /* Helper macro for adding write access to tcp_sock or sock fields.
6273 * The macro is called with two registers, dst_reg which contains a pointer
6274 * to ctx (context) and src_reg which contains the value that should be
6275 * stored. However, we need an additional register since we cannot overwrite
6276 * dst_reg because it may be used later in the program.
6277 * Instead we "borrow" one of the other register. We first save its value
6278 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
6279 * it at the end of the macro.
6281 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6283 int reg = BPF_REG_9; \
6284 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6285 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6286 if (si->dst_reg == reg || si->src_reg == reg) \
6288 if (si->dst_reg == reg || si->src_reg == reg) \
6290 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
6291 offsetof(struct bpf_sock_ops_kern, \
6293 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6294 struct bpf_sock_ops_kern, \
6297 offsetof(struct bpf_sock_ops_kern, \
6299 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
6300 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6301 struct bpf_sock_ops_kern, sk),\
6303 offsetof(struct bpf_sock_ops_kern, sk));\
6304 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
6306 offsetof(OBJ, OBJ_FIELD)); \
6307 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
6308 offsetof(struct bpf_sock_ops_kern, \
6312 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
6314 if (TYPE == BPF_WRITE) \
6315 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6317 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6320 case offsetof(struct bpf_sock_ops, snd_cwnd):
6321 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
6324 case offsetof(struct bpf_sock_ops, srtt_us):
6325 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
6328 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
6329 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
6333 case offsetof(struct bpf_sock_ops, snd_ssthresh):
6334 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
6337 case offsetof(struct bpf_sock_ops, rcv_nxt):
6338 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
6341 case offsetof(struct bpf_sock_ops, snd_nxt):
6342 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
6345 case offsetof(struct bpf_sock_ops, snd_una):
6346 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
6349 case offsetof(struct bpf_sock_ops, mss_cache):
6350 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
6353 case offsetof(struct bpf_sock_ops, ecn_flags):
6354 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6357 case offsetof(struct bpf_sock_ops, rate_delivered):
6358 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6362 case offsetof(struct bpf_sock_ops, rate_interval_us):
6363 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6367 case offsetof(struct bpf_sock_ops, packets_out):
6368 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6371 case offsetof(struct bpf_sock_ops, retrans_out):
6372 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6375 case offsetof(struct bpf_sock_ops, total_retrans):
6376 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6380 case offsetof(struct bpf_sock_ops, segs_in):
6381 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
6384 case offsetof(struct bpf_sock_ops, data_segs_in):
6385 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
6388 case offsetof(struct bpf_sock_ops, segs_out):
6389 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
6392 case offsetof(struct bpf_sock_ops, data_segs_out):
6393 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
6397 case offsetof(struct bpf_sock_ops, lost_out):
6398 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
6401 case offsetof(struct bpf_sock_ops, sacked_out):
6402 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
6405 case offsetof(struct bpf_sock_ops, sk_txhash):
6406 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
6410 case offsetof(struct bpf_sock_ops, bytes_received):
6411 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
6415 case offsetof(struct bpf_sock_ops, bytes_acked):
6416 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
6420 return insn - insn_buf;
6423 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
6424 const struct bpf_insn *si,
6425 struct bpf_insn *insn_buf,
6426 struct bpf_prog *prog, u32 *target_size)
6428 struct bpf_insn *insn = insn_buf;
6432 case offsetof(struct __sk_buff, data_end):
6434 off -= offsetof(struct __sk_buff, data_end);
6435 off += offsetof(struct sk_buff, cb);
6436 off += offsetof(struct tcp_skb_cb, bpf.data_end);
6437 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6441 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6445 return insn - insn_buf;
6448 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
6449 const struct bpf_insn *si,
6450 struct bpf_insn *insn_buf,
6451 struct bpf_prog *prog, u32 *target_size)
6453 struct bpf_insn *insn = insn_buf;
6457 case offsetof(struct sk_msg_md, data):
6458 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data),
6459 si->dst_reg, si->src_reg,
6460 offsetof(struct sk_msg_buff, data));
6462 case offsetof(struct sk_msg_md, data_end):
6463 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data_end),
6464 si->dst_reg, si->src_reg,
6465 offsetof(struct sk_msg_buff, data_end));
6467 case offsetof(struct sk_msg_md, family):
6468 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6470 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6471 struct sk_msg_buff, sk),
6472 si->dst_reg, si->src_reg,
6473 offsetof(struct sk_msg_buff, sk));
6474 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6475 offsetof(struct sock_common, skc_family));
6478 case offsetof(struct sk_msg_md, remote_ip4):
6479 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6481 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6482 struct sk_msg_buff, sk),
6483 si->dst_reg, si->src_reg,
6484 offsetof(struct sk_msg_buff, sk));
6485 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6486 offsetof(struct sock_common, skc_daddr));
6489 case offsetof(struct sk_msg_md, local_ip4):
6490 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6491 skc_rcv_saddr) != 4);
6493 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6494 struct sk_msg_buff, sk),
6495 si->dst_reg, si->src_reg,
6496 offsetof(struct sk_msg_buff, sk));
6497 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6498 offsetof(struct sock_common,
6502 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
6503 offsetof(struct sk_msg_md, remote_ip6[3]):
6504 #if IS_ENABLED(CONFIG_IPV6)
6505 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6506 skc_v6_daddr.s6_addr32[0]) != 4);
6509 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
6510 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6511 struct sk_msg_buff, sk),
6512 si->dst_reg, si->src_reg,
6513 offsetof(struct sk_msg_buff, sk));
6514 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6515 offsetof(struct sock_common,
6516 skc_v6_daddr.s6_addr32[0]) +
6519 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6523 case offsetof(struct sk_msg_md, local_ip6[0]) ...
6524 offsetof(struct sk_msg_md, local_ip6[3]):
6525 #if IS_ENABLED(CONFIG_IPV6)
6526 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6527 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6530 off -= offsetof(struct sk_msg_md, local_ip6[0]);
6531 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6532 struct sk_msg_buff, sk),
6533 si->dst_reg, si->src_reg,
6534 offsetof(struct sk_msg_buff, sk));
6535 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6536 offsetof(struct sock_common,
6537 skc_v6_rcv_saddr.s6_addr32[0]) +
6540 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6544 case offsetof(struct sk_msg_md, remote_port):
6545 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6547 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6548 struct sk_msg_buff, sk),
6549 si->dst_reg, si->src_reg,
6550 offsetof(struct sk_msg_buff, sk));
6551 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6552 offsetof(struct sock_common, skc_dport));
6553 #ifndef __BIG_ENDIAN_BITFIELD
6554 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6558 case offsetof(struct sk_msg_md, local_port):
6559 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6561 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6562 struct sk_msg_buff, sk),
6563 si->dst_reg, si->src_reg,
6564 offsetof(struct sk_msg_buff, sk));
6565 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6566 offsetof(struct sock_common, skc_num));
6570 return insn - insn_buf;
6573 const struct bpf_verifier_ops sk_filter_verifier_ops = {
6574 .get_func_proto = sk_filter_func_proto,
6575 .is_valid_access = sk_filter_is_valid_access,
6576 .convert_ctx_access = bpf_convert_ctx_access,
6577 .gen_ld_abs = bpf_gen_ld_abs,
6580 const struct bpf_prog_ops sk_filter_prog_ops = {
6581 .test_run = bpf_prog_test_run_skb,
6584 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
6585 .get_func_proto = tc_cls_act_func_proto,
6586 .is_valid_access = tc_cls_act_is_valid_access,
6587 .convert_ctx_access = tc_cls_act_convert_ctx_access,
6588 .gen_prologue = tc_cls_act_prologue,
6589 .gen_ld_abs = bpf_gen_ld_abs,
6592 const struct bpf_prog_ops tc_cls_act_prog_ops = {
6593 .test_run = bpf_prog_test_run_skb,
6596 const struct bpf_verifier_ops xdp_verifier_ops = {
6597 .get_func_proto = xdp_func_proto,
6598 .is_valid_access = xdp_is_valid_access,
6599 .convert_ctx_access = xdp_convert_ctx_access,
6602 const struct bpf_prog_ops xdp_prog_ops = {
6603 .test_run = bpf_prog_test_run_xdp,
6606 const struct bpf_verifier_ops cg_skb_verifier_ops = {
6607 .get_func_proto = sk_filter_func_proto,
6608 .is_valid_access = sk_filter_is_valid_access,
6609 .convert_ctx_access = bpf_convert_ctx_access,
6612 const struct bpf_prog_ops cg_skb_prog_ops = {
6613 .test_run = bpf_prog_test_run_skb,
6616 const struct bpf_verifier_ops lwt_in_verifier_ops = {
6617 .get_func_proto = lwt_in_func_proto,
6618 .is_valid_access = lwt_is_valid_access,
6619 .convert_ctx_access = bpf_convert_ctx_access,
6622 const struct bpf_prog_ops lwt_in_prog_ops = {
6623 .test_run = bpf_prog_test_run_skb,
6626 const struct bpf_verifier_ops lwt_out_verifier_ops = {
6627 .get_func_proto = lwt_out_func_proto,
6628 .is_valid_access = lwt_is_valid_access,
6629 .convert_ctx_access = bpf_convert_ctx_access,
6632 const struct bpf_prog_ops lwt_out_prog_ops = {
6633 .test_run = bpf_prog_test_run_skb,
6636 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
6637 .get_func_proto = lwt_xmit_func_proto,
6638 .is_valid_access = lwt_is_valid_access,
6639 .convert_ctx_access = bpf_convert_ctx_access,
6640 .gen_prologue = tc_cls_act_prologue,
6643 const struct bpf_prog_ops lwt_xmit_prog_ops = {
6644 .test_run = bpf_prog_test_run_skb,
6647 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
6648 .get_func_proto = lwt_seg6local_func_proto,
6649 .is_valid_access = lwt_is_valid_access,
6650 .convert_ctx_access = bpf_convert_ctx_access,
6653 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
6654 .test_run = bpf_prog_test_run_skb,
6657 const struct bpf_verifier_ops cg_sock_verifier_ops = {
6658 .get_func_proto = sock_filter_func_proto,
6659 .is_valid_access = sock_filter_is_valid_access,
6660 .convert_ctx_access = sock_filter_convert_ctx_access,
6663 const struct bpf_prog_ops cg_sock_prog_ops = {
6666 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
6667 .get_func_proto = sock_addr_func_proto,
6668 .is_valid_access = sock_addr_is_valid_access,
6669 .convert_ctx_access = sock_addr_convert_ctx_access,
6672 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
6675 const struct bpf_verifier_ops sock_ops_verifier_ops = {
6676 .get_func_proto = sock_ops_func_proto,
6677 .is_valid_access = sock_ops_is_valid_access,
6678 .convert_ctx_access = sock_ops_convert_ctx_access,
6681 const struct bpf_prog_ops sock_ops_prog_ops = {
6684 const struct bpf_verifier_ops sk_skb_verifier_ops = {
6685 .get_func_proto = sk_skb_func_proto,
6686 .is_valid_access = sk_skb_is_valid_access,
6687 .convert_ctx_access = sk_skb_convert_ctx_access,
6688 .gen_prologue = sk_skb_prologue,
6691 const struct bpf_prog_ops sk_skb_prog_ops = {
6694 const struct bpf_verifier_ops sk_msg_verifier_ops = {
6695 .get_func_proto = sk_msg_func_proto,
6696 .is_valid_access = sk_msg_is_valid_access,
6697 .convert_ctx_access = sk_msg_convert_ctx_access,
6700 const struct bpf_prog_ops sk_msg_prog_ops = {
6703 int sk_detach_filter(struct sock *sk)
6706 struct sk_filter *filter;
6708 if (sock_flag(sk, SOCK_FILTER_LOCKED))
6711 filter = rcu_dereference_protected(sk->sk_filter,
6712 lockdep_sock_is_held(sk));
6714 RCU_INIT_POINTER(sk->sk_filter, NULL);
6715 sk_filter_uncharge(sk, filter);
6721 EXPORT_SYMBOL_GPL(sk_detach_filter);
6723 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
6726 struct sock_fprog_kern *fprog;
6727 struct sk_filter *filter;
6731 filter = rcu_dereference_protected(sk->sk_filter,
6732 lockdep_sock_is_held(sk));
6736 /* We're copying the filter that has been originally attached,
6737 * so no conversion/decode needed anymore. eBPF programs that
6738 * have no original program cannot be dumped through this.
6741 fprog = filter->prog->orig_prog;
6747 /* User space only enquires number of filter blocks. */
6751 if (len < fprog->len)
6755 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
6758 /* Instead of bytes, the API requests to return the number