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>
69 * sk_filter_trim_cap - run a packet through a socket filter
70 * @sk: sock associated with &sk_buff
71 * @skb: buffer to filter
72 * @cap: limit on how short the eBPF program may trim the packet
74 * Run the eBPF program and then cut skb->data to correct size returned by
75 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
76 * than pkt_len we keep whole skb->data. This is the socket level
77 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
78 * be accepted or -EPERM if the packet should be tossed.
81 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
84 struct sk_filter *filter;
87 * If the skb was allocated from pfmemalloc reserves, only
88 * allow SOCK_MEMALLOC sockets to use it as this socket is
91 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
92 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
95 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
99 err = security_sock_rcv_skb(sk, skb);
104 filter = rcu_dereference(sk->sk_filter);
106 struct sock *save_sk = skb->sk;
107 unsigned int pkt_len;
110 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
112 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
118 EXPORT_SYMBOL(sk_filter_trim_cap);
120 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
122 return skb_get_poff(skb);
125 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
129 if (skb_is_nonlinear(skb))
132 if (skb->len < sizeof(struct nlattr))
135 if (a > skb->len - sizeof(struct nlattr))
138 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
140 return (void *) nla - (void *) skb->data;
145 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
149 if (skb_is_nonlinear(skb))
152 if (skb->len < sizeof(struct nlattr))
155 if (a > skb->len - sizeof(struct nlattr))
158 nla = (struct nlattr *) &skb->data[a];
159 if (nla->nla_len > skb->len - a)
162 nla = nla_find_nested(nla, x);
164 return (void *) nla - (void *) skb->data;
169 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
170 data, int, headlen, int, offset)
173 const int len = sizeof(tmp);
176 if (headlen - offset >= len)
177 return *(u8 *)(data + offset);
178 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
181 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
189 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
192 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
196 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
197 data, int, headlen, int, offset)
200 const int len = sizeof(tmp);
203 if (headlen - offset >= len)
204 return get_unaligned_be16(data + offset);
205 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
206 return be16_to_cpu(tmp);
208 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
210 return get_unaligned_be16(ptr);
216 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
219 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
223 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
224 data, int, headlen, int, offset)
227 const int len = sizeof(tmp);
229 if (likely(offset >= 0)) {
230 if (headlen - offset >= len)
231 return get_unaligned_be32(data + offset);
232 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
233 return be32_to_cpu(tmp);
235 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
237 return get_unaligned_be32(ptr);
243 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
246 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
250 BPF_CALL_0(bpf_get_raw_cpu_id)
252 return raw_smp_processor_id();
255 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
256 .func = bpf_get_raw_cpu_id,
258 .ret_type = RET_INTEGER,
261 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
262 struct bpf_insn *insn_buf)
264 struct bpf_insn *insn = insn_buf;
268 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
270 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
271 offsetof(struct sk_buff, mark));
275 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
276 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
277 #ifdef __BIG_ENDIAN_BITFIELD
278 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
283 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
285 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
286 offsetof(struct sk_buff, queue_mapping));
289 case SKF_AD_VLAN_TAG:
290 case SKF_AD_VLAN_TAG_PRESENT:
291 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
292 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
294 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
295 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
296 offsetof(struct sk_buff, vlan_tci));
297 if (skb_field == SKF_AD_VLAN_TAG) {
298 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
302 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
304 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
309 return insn - insn_buf;
312 static bool convert_bpf_extensions(struct sock_filter *fp,
313 struct bpf_insn **insnp)
315 struct bpf_insn *insn = *insnp;
319 case SKF_AD_OFF + SKF_AD_PROTOCOL:
320 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
322 /* A = *(u16 *) (CTX + offsetof(protocol)) */
323 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
324 offsetof(struct sk_buff, protocol));
325 /* A = ntohs(A) [emitting a nop or swap16] */
326 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
329 case SKF_AD_OFF + SKF_AD_PKTTYPE:
330 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
334 case SKF_AD_OFF + SKF_AD_IFINDEX:
335 case SKF_AD_OFF + SKF_AD_HATYPE:
336 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
337 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
339 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
340 BPF_REG_TMP, BPF_REG_CTX,
341 offsetof(struct sk_buff, dev));
342 /* if (tmp != 0) goto pc + 1 */
343 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
344 *insn++ = BPF_EXIT_INSN();
345 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
346 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
347 offsetof(struct net_device, ifindex));
349 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
350 offsetof(struct net_device, type));
353 case SKF_AD_OFF + SKF_AD_MARK:
354 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
358 case SKF_AD_OFF + SKF_AD_RXHASH:
359 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
361 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
362 offsetof(struct sk_buff, hash));
365 case SKF_AD_OFF + SKF_AD_QUEUE:
366 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
370 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
371 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
372 BPF_REG_A, BPF_REG_CTX, insn);
376 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
377 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
378 BPF_REG_A, BPF_REG_CTX, insn);
382 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
383 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
385 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
386 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
387 offsetof(struct sk_buff, vlan_proto));
388 /* A = ntohs(A) [emitting a nop or swap16] */
389 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
392 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
393 case SKF_AD_OFF + SKF_AD_NLATTR:
394 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
395 case SKF_AD_OFF + SKF_AD_CPU:
396 case SKF_AD_OFF + SKF_AD_RANDOM:
398 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
400 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
402 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
403 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
405 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
406 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
408 case SKF_AD_OFF + SKF_AD_NLATTR:
409 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
411 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
412 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
414 case SKF_AD_OFF + SKF_AD_CPU:
415 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
417 case SKF_AD_OFF + SKF_AD_RANDOM:
418 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
419 bpf_user_rnd_init_once();
424 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
426 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
430 /* This is just a dummy call to avoid letting the compiler
431 * evict __bpf_call_base() as an optimization. Placed here
432 * where no-one bothers.
434 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
442 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
444 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
445 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
446 bool endian = BPF_SIZE(fp->code) == BPF_H ||
447 BPF_SIZE(fp->code) == BPF_W;
448 bool indirect = BPF_MODE(fp->code) == BPF_IND;
449 const int ip_align = NET_IP_ALIGN;
450 struct bpf_insn *insn = *insnp;
454 ((unaligned_ok && offset >= 0) ||
455 (!unaligned_ok && offset >= 0 &&
456 offset + ip_align >= 0 &&
457 offset + ip_align % size == 0))) {
458 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
459 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
460 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP, size, 2 + endian);
461 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, BPF_REG_D,
464 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
465 *insn++ = BPF_JMP_A(8);
468 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
469 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
470 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
472 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
474 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
476 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
479 switch (BPF_SIZE(fp->code)) {
481 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
484 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
487 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
493 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
494 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
495 *insn = BPF_EXIT_INSN();
502 * bpf_convert_filter - convert filter program
503 * @prog: the user passed filter program
504 * @len: the length of the user passed filter program
505 * @new_prog: allocated 'struct bpf_prog' or NULL
506 * @new_len: pointer to store length of converted program
507 * @seen_ld_abs: bool whether we've seen ld_abs/ind
509 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
510 * style extended BPF (eBPF).
511 * Conversion workflow:
513 * 1) First pass for calculating the new program length:
514 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
516 * 2) 2nd pass to remap in two passes: 1st pass finds new
517 * jump offsets, 2nd pass remapping:
518 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
520 static int bpf_convert_filter(struct sock_filter *prog, int len,
521 struct bpf_prog *new_prog, int *new_len,
524 int new_flen = 0, pass = 0, target, i, stack_off;
525 struct bpf_insn *new_insn, *first_insn = NULL;
526 struct sock_filter *fp;
530 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
531 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
533 if (len <= 0 || len > BPF_MAXINSNS)
537 first_insn = new_prog->insnsi;
538 addrs = kcalloc(len, sizeof(*addrs),
539 GFP_KERNEL | __GFP_NOWARN);
545 new_insn = first_insn;
548 /* Classic BPF related prologue emission. */
550 /* Classic BPF expects A and X to be reset first. These need
551 * to be guaranteed to be the first two instructions.
553 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
554 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
556 /* All programs must keep CTX in callee saved BPF_REG_CTX.
557 * In eBPF case it's done by the compiler, here we need to
558 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
560 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
562 /* For packet access in classic BPF, cache skb->data
563 * in callee-saved BPF R8 and skb->len - skb->data_len
564 * (headlen) in BPF R9. Since classic BPF is read-only
565 * on CTX, we only need to cache it once.
567 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
568 BPF_REG_D, BPF_REG_CTX,
569 offsetof(struct sk_buff, data));
570 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
571 offsetof(struct sk_buff, len));
572 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
573 offsetof(struct sk_buff, data_len));
574 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
580 for (i = 0; i < len; fp++, i++) {
581 struct bpf_insn tmp_insns[32] = { };
582 struct bpf_insn *insn = tmp_insns;
585 addrs[i] = new_insn - first_insn;
588 /* All arithmetic insns and skb loads map as-is. */
589 case BPF_ALU | BPF_ADD | BPF_X:
590 case BPF_ALU | BPF_ADD | BPF_K:
591 case BPF_ALU | BPF_SUB | BPF_X:
592 case BPF_ALU | BPF_SUB | BPF_K:
593 case BPF_ALU | BPF_AND | BPF_X:
594 case BPF_ALU | BPF_AND | BPF_K:
595 case BPF_ALU | BPF_OR | BPF_X:
596 case BPF_ALU | BPF_OR | BPF_K:
597 case BPF_ALU | BPF_LSH | BPF_X:
598 case BPF_ALU | BPF_LSH | BPF_K:
599 case BPF_ALU | BPF_RSH | BPF_X:
600 case BPF_ALU | BPF_RSH | BPF_K:
601 case BPF_ALU | BPF_XOR | BPF_X:
602 case BPF_ALU | BPF_XOR | BPF_K:
603 case BPF_ALU | BPF_MUL | BPF_X:
604 case BPF_ALU | BPF_MUL | BPF_K:
605 case BPF_ALU | BPF_DIV | BPF_X:
606 case BPF_ALU | BPF_DIV | BPF_K:
607 case BPF_ALU | BPF_MOD | BPF_X:
608 case BPF_ALU | BPF_MOD | BPF_K:
609 case BPF_ALU | BPF_NEG:
610 case BPF_LD | BPF_ABS | BPF_W:
611 case BPF_LD | BPF_ABS | BPF_H:
612 case BPF_LD | BPF_ABS | BPF_B:
613 case BPF_LD | BPF_IND | BPF_W:
614 case BPF_LD | BPF_IND | BPF_H:
615 case BPF_LD | BPF_IND | BPF_B:
616 /* Check for overloaded BPF extension and
617 * directly convert it if found, otherwise
618 * just move on with mapping.
620 if (BPF_CLASS(fp->code) == BPF_LD &&
621 BPF_MODE(fp->code) == BPF_ABS &&
622 convert_bpf_extensions(fp, &insn))
624 if (BPF_CLASS(fp->code) == BPF_LD &&
625 convert_bpf_ld_abs(fp, &insn)) {
630 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
631 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
632 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
633 /* Error with exception code on div/mod by 0.
634 * For cBPF programs, this was always return 0.
636 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
637 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
638 *insn++ = BPF_EXIT_INSN();
641 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
644 /* Jump transformation cannot use BPF block macros
645 * everywhere as offset calculation and target updates
646 * require a bit more work than the rest, i.e. jump
647 * opcodes map as-is, but offsets need adjustment.
650 #define BPF_EMIT_JMP \
652 if (target >= len || target < 0) \
654 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
655 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
656 insn->off -= insn - tmp_insns; \
659 case BPF_JMP | BPF_JA:
660 target = i + fp->k + 1;
661 insn->code = fp->code;
665 case BPF_JMP | BPF_JEQ | BPF_K:
666 case BPF_JMP | BPF_JEQ | BPF_X:
667 case BPF_JMP | BPF_JSET | BPF_K:
668 case BPF_JMP | BPF_JSET | BPF_X:
669 case BPF_JMP | BPF_JGT | BPF_K:
670 case BPF_JMP | BPF_JGT | BPF_X:
671 case BPF_JMP | BPF_JGE | BPF_K:
672 case BPF_JMP | BPF_JGE | BPF_X:
673 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
674 /* BPF immediates are signed, zero extend
675 * immediate into tmp register and use it
678 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
680 insn->dst_reg = BPF_REG_A;
681 insn->src_reg = BPF_REG_TMP;
684 insn->dst_reg = BPF_REG_A;
686 bpf_src = BPF_SRC(fp->code);
687 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
690 /* Common case where 'jump_false' is next insn. */
692 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
693 target = i + fp->jt + 1;
698 /* Convert some jumps when 'jump_true' is next insn. */
700 switch (BPF_OP(fp->code)) {
702 insn->code = BPF_JMP | BPF_JNE | bpf_src;
705 insn->code = BPF_JMP | BPF_JLE | bpf_src;
708 insn->code = BPF_JMP | BPF_JLT | bpf_src;
714 target = i + fp->jf + 1;
719 /* Other jumps are mapped into two insns: Jxx and JA. */
720 target = i + fp->jt + 1;
721 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
725 insn->code = BPF_JMP | BPF_JA;
726 target = i + fp->jf + 1;
730 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
731 case BPF_LDX | BPF_MSH | BPF_B: {
732 struct sock_filter tmp = {
733 .code = BPF_LD | BPF_ABS | BPF_B,
740 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
741 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
742 convert_bpf_ld_abs(&tmp, &insn);
745 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
747 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
749 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
751 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
753 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
756 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
757 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
759 case BPF_RET | BPF_A:
760 case BPF_RET | BPF_K:
761 if (BPF_RVAL(fp->code) == BPF_K)
762 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
764 *insn = BPF_EXIT_INSN();
767 /* Store to stack. */
770 stack_off = fp->k * 4 + 4;
771 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
772 BPF_ST ? BPF_REG_A : BPF_REG_X,
774 /* check_load_and_stores() verifies that classic BPF can
775 * load from stack only after write, so tracking
776 * stack_depth for ST|STX insns is enough
778 if (new_prog && new_prog->aux->stack_depth < stack_off)
779 new_prog->aux->stack_depth = stack_off;
782 /* Load from stack. */
783 case BPF_LD | BPF_MEM:
784 case BPF_LDX | BPF_MEM:
785 stack_off = fp->k * 4 + 4;
786 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
787 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
792 case BPF_LD | BPF_IMM:
793 case BPF_LDX | BPF_IMM:
794 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
795 BPF_REG_A : BPF_REG_X, fp->k);
799 case BPF_MISC | BPF_TAX:
800 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
804 case BPF_MISC | BPF_TXA:
805 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
808 /* A = skb->len or X = skb->len */
809 case BPF_LD | BPF_W | BPF_LEN:
810 case BPF_LDX | BPF_W | BPF_LEN:
811 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
812 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
813 offsetof(struct sk_buff, len));
816 /* Access seccomp_data fields. */
817 case BPF_LDX | BPF_ABS | BPF_W:
818 /* A = *(u32 *) (ctx + K) */
819 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
822 /* Unknown instruction. */
829 memcpy(new_insn, tmp_insns,
830 sizeof(*insn) * (insn - tmp_insns));
831 new_insn += insn - tmp_insns;
835 /* Only calculating new length. */
836 *new_len = new_insn - first_insn;
838 *new_len += 4; /* Prologue bits. */
843 if (new_flen != new_insn - first_insn) {
844 new_flen = new_insn - first_insn;
851 BUG_ON(*new_len != new_flen);
860 * As we dont want to clear mem[] array for each packet going through
861 * __bpf_prog_run(), we check that filter loaded by user never try to read
862 * a cell if not previously written, and we check all branches to be sure
863 * a malicious user doesn't try to abuse us.
865 static int check_load_and_stores(const struct sock_filter *filter, int flen)
867 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
870 BUILD_BUG_ON(BPF_MEMWORDS > 16);
872 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
876 memset(masks, 0xff, flen * sizeof(*masks));
878 for (pc = 0; pc < flen; pc++) {
879 memvalid &= masks[pc];
881 switch (filter[pc].code) {
884 memvalid |= (1 << filter[pc].k);
886 case BPF_LD | BPF_MEM:
887 case BPF_LDX | BPF_MEM:
888 if (!(memvalid & (1 << filter[pc].k))) {
893 case BPF_JMP | BPF_JA:
894 /* A jump must set masks on target */
895 masks[pc + 1 + filter[pc].k] &= memvalid;
898 case BPF_JMP | BPF_JEQ | BPF_K:
899 case BPF_JMP | BPF_JEQ | BPF_X:
900 case BPF_JMP | BPF_JGE | BPF_K:
901 case BPF_JMP | BPF_JGE | BPF_X:
902 case BPF_JMP | BPF_JGT | BPF_K:
903 case BPF_JMP | BPF_JGT | BPF_X:
904 case BPF_JMP | BPF_JSET | BPF_K:
905 case BPF_JMP | BPF_JSET | BPF_X:
906 /* A jump must set masks on targets */
907 masks[pc + 1 + filter[pc].jt] &= memvalid;
908 masks[pc + 1 + filter[pc].jf] &= memvalid;
918 static bool chk_code_allowed(u16 code_to_probe)
920 static const bool codes[] = {
921 /* 32 bit ALU operations */
922 [BPF_ALU | BPF_ADD | BPF_K] = true,
923 [BPF_ALU | BPF_ADD | BPF_X] = true,
924 [BPF_ALU | BPF_SUB | BPF_K] = true,
925 [BPF_ALU | BPF_SUB | BPF_X] = true,
926 [BPF_ALU | BPF_MUL | BPF_K] = true,
927 [BPF_ALU | BPF_MUL | BPF_X] = true,
928 [BPF_ALU | BPF_DIV | BPF_K] = true,
929 [BPF_ALU | BPF_DIV | BPF_X] = true,
930 [BPF_ALU | BPF_MOD | BPF_K] = true,
931 [BPF_ALU | BPF_MOD | BPF_X] = true,
932 [BPF_ALU | BPF_AND | BPF_K] = true,
933 [BPF_ALU | BPF_AND | BPF_X] = true,
934 [BPF_ALU | BPF_OR | BPF_K] = true,
935 [BPF_ALU | BPF_OR | BPF_X] = true,
936 [BPF_ALU | BPF_XOR | BPF_K] = true,
937 [BPF_ALU | BPF_XOR | BPF_X] = true,
938 [BPF_ALU | BPF_LSH | BPF_K] = true,
939 [BPF_ALU | BPF_LSH | BPF_X] = true,
940 [BPF_ALU | BPF_RSH | BPF_K] = true,
941 [BPF_ALU | BPF_RSH | BPF_X] = true,
942 [BPF_ALU | BPF_NEG] = true,
943 /* Load instructions */
944 [BPF_LD | BPF_W | BPF_ABS] = true,
945 [BPF_LD | BPF_H | BPF_ABS] = true,
946 [BPF_LD | BPF_B | BPF_ABS] = true,
947 [BPF_LD | BPF_W | BPF_LEN] = true,
948 [BPF_LD | BPF_W | BPF_IND] = true,
949 [BPF_LD | BPF_H | BPF_IND] = true,
950 [BPF_LD | BPF_B | BPF_IND] = true,
951 [BPF_LD | BPF_IMM] = true,
952 [BPF_LD | BPF_MEM] = true,
953 [BPF_LDX | BPF_W | BPF_LEN] = true,
954 [BPF_LDX | BPF_B | BPF_MSH] = true,
955 [BPF_LDX | BPF_IMM] = true,
956 [BPF_LDX | BPF_MEM] = true,
957 /* Store instructions */
960 /* Misc instructions */
961 [BPF_MISC | BPF_TAX] = true,
962 [BPF_MISC | BPF_TXA] = true,
963 /* Return instructions */
964 [BPF_RET | BPF_K] = true,
965 [BPF_RET | BPF_A] = true,
966 /* Jump instructions */
967 [BPF_JMP | BPF_JA] = true,
968 [BPF_JMP | BPF_JEQ | BPF_K] = true,
969 [BPF_JMP | BPF_JEQ | BPF_X] = true,
970 [BPF_JMP | BPF_JGE | BPF_K] = true,
971 [BPF_JMP | BPF_JGE | BPF_X] = true,
972 [BPF_JMP | BPF_JGT | BPF_K] = true,
973 [BPF_JMP | BPF_JGT | BPF_X] = true,
974 [BPF_JMP | BPF_JSET | BPF_K] = true,
975 [BPF_JMP | BPF_JSET | BPF_X] = true,
978 if (code_to_probe >= ARRAY_SIZE(codes))
981 return codes[code_to_probe];
984 static bool bpf_check_basics_ok(const struct sock_filter *filter,
989 if (flen == 0 || flen > BPF_MAXINSNS)
996 * bpf_check_classic - verify socket filter code
997 * @filter: filter to verify
998 * @flen: length of filter
1000 * Check the user's filter code. If we let some ugly
1001 * filter code slip through kaboom! The filter must contain
1002 * no references or jumps that are out of range, no illegal
1003 * instructions, and must end with a RET instruction.
1005 * All jumps are forward as they are not signed.
1007 * Returns 0 if the rule set is legal or -EINVAL if not.
1009 static int bpf_check_classic(const struct sock_filter *filter,
1015 /* Check the filter code now */
1016 for (pc = 0; pc < flen; pc++) {
1017 const struct sock_filter *ftest = &filter[pc];
1019 /* May we actually operate on this code? */
1020 if (!chk_code_allowed(ftest->code))
1023 /* Some instructions need special checks */
1024 switch (ftest->code) {
1025 case BPF_ALU | BPF_DIV | BPF_K:
1026 case BPF_ALU | BPF_MOD | BPF_K:
1027 /* Check for division by zero */
1031 case BPF_ALU | BPF_LSH | BPF_K:
1032 case BPF_ALU | BPF_RSH | BPF_K:
1036 case BPF_LD | BPF_MEM:
1037 case BPF_LDX | BPF_MEM:
1040 /* Check for invalid memory addresses */
1041 if (ftest->k >= BPF_MEMWORDS)
1044 case BPF_JMP | BPF_JA:
1045 /* Note, the large ftest->k might cause loops.
1046 * Compare this with conditional jumps below,
1047 * where offsets are limited. --ANK (981016)
1049 if (ftest->k >= (unsigned int)(flen - pc - 1))
1052 case BPF_JMP | BPF_JEQ | BPF_K:
1053 case BPF_JMP | BPF_JEQ | BPF_X:
1054 case BPF_JMP | BPF_JGE | BPF_K:
1055 case BPF_JMP | BPF_JGE | BPF_X:
1056 case BPF_JMP | BPF_JGT | BPF_K:
1057 case BPF_JMP | BPF_JGT | BPF_X:
1058 case BPF_JMP | BPF_JSET | BPF_K:
1059 case BPF_JMP | BPF_JSET | BPF_X:
1060 /* Both conditionals must be safe */
1061 if (pc + ftest->jt + 1 >= flen ||
1062 pc + ftest->jf + 1 >= flen)
1065 case BPF_LD | BPF_W | BPF_ABS:
1066 case BPF_LD | BPF_H | BPF_ABS:
1067 case BPF_LD | BPF_B | BPF_ABS:
1069 if (bpf_anc_helper(ftest) & BPF_ANC)
1071 /* Ancillary operation unknown or unsupported */
1072 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1077 /* Last instruction must be a RET code */
1078 switch (filter[flen - 1].code) {
1079 case BPF_RET | BPF_K:
1080 case BPF_RET | BPF_A:
1081 return check_load_and_stores(filter, flen);
1087 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1088 const struct sock_fprog *fprog)
1090 unsigned int fsize = bpf_classic_proglen(fprog);
1091 struct sock_fprog_kern *fkprog;
1093 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1097 fkprog = fp->orig_prog;
1098 fkprog->len = fprog->len;
1100 fkprog->filter = kmemdup(fp->insns, fsize,
1101 GFP_KERNEL | __GFP_NOWARN);
1102 if (!fkprog->filter) {
1103 kfree(fp->orig_prog);
1110 static void bpf_release_orig_filter(struct bpf_prog *fp)
1112 struct sock_fprog_kern *fprog = fp->orig_prog;
1115 kfree(fprog->filter);
1120 static void __bpf_prog_release(struct bpf_prog *prog)
1122 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1125 bpf_release_orig_filter(prog);
1126 bpf_prog_free(prog);
1130 static void __sk_filter_release(struct sk_filter *fp)
1132 __bpf_prog_release(fp->prog);
1137 * sk_filter_release_rcu - Release a socket filter by rcu_head
1138 * @rcu: rcu_head that contains the sk_filter to free
1140 static void sk_filter_release_rcu(struct rcu_head *rcu)
1142 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1144 __sk_filter_release(fp);
1148 * sk_filter_release - release a socket filter
1149 * @fp: filter to remove
1151 * Remove a filter from a socket and release its resources.
1153 static void sk_filter_release(struct sk_filter *fp)
1155 if (refcount_dec_and_test(&fp->refcnt))
1156 call_rcu(&fp->rcu, sk_filter_release_rcu);
1159 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1161 u32 filter_size = bpf_prog_size(fp->prog->len);
1163 atomic_sub(filter_size, &sk->sk_omem_alloc);
1164 sk_filter_release(fp);
1167 /* try to charge the socket memory if there is space available
1168 * return true on success
1170 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1172 u32 filter_size = bpf_prog_size(fp->prog->len);
1174 /* same check as in sock_kmalloc() */
1175 if (filter_size <= sysctl_optmem_max &&
1176 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1177 atomic_add(filter_size, &sk->sk_omem_alloc);
1183 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1185 if (!refcount_inc_not_zero(&fp->refcnt))
1188 if (!__sk_filter_charge(sk, fp)) {
1189 sk_filter_release(fp);
1195 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1197 struct sock_filter *old_prog;
1198 struct bpf_prog *old_fp;
1199 int err, new_len, old_len = fp->len;
1200 bool seen_ld_abs = false;
1202 /* We are free to overwrite insns et al right here as it
1203 * won't be used at this point in time anymore internally
1204 * after the migration to the internal BPF instruction
1207 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1208 sizeof(struct bpf_insn));
1210 /* Conversion cannot happen on overlapping memory areas,
1211 * so we need to keep the user BPF around until the 2nd
1212 * pass. At this time, the user BPF is stored in fp->insns.
1214 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1215 GFP_KERNEL | __GFP_NOWARN);
1221 /* 1st pass: calculate the new program length. */
1222 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1227 /* Expand fp for appending the new filter representation. */
1229 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1231 /* The old_fp is still around in case we couldn't
1232 * allocate new memory, so uncharge on that one.
1241 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1242 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1245 /* 2nd bpf_convert_filter() can fail only if it fails
1246 * to allocate memory, remapping must succeed. Note,
1247 * that at this time old_fp has already been released
1252 fp = bpf_prog_select_runtime(fp, &err);
1262 __bpf_prog_release(fp);
1263 return ERR_PTR(err);
1266 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1267 bpf_aux_classic_check_t trans)
1271 fp->bpf_func = NULL;
1274 err = bpf_check_classic(fp->insns, fp->len);
1276 __bpf_prog_release(fp);
1277 return ERR_PTR(err);
1280 /* There might be additional checks and transformations
1281 * needed on classic filters, f.e. in case of seccomp.
1284 err = trans(fp->insns, fp->len);
1286 __bpf_prog_release(fp);
1287 return ERR_PTR(err);
1291 /* Probe if we can JIT compile the filter and if so, do
1292 * the compilation of the filter.
1294 bpf_jit_compile(fp);
1296 /* JIT compiler couldn't process this filter, so do the
1297 * internal BPF translation for the optimized interpreter.
1300 fp = bpf_migrate_filter(fp);
1306 * bpf_prog_create - create an unattached filter
1307 * @pfp: the unattached filter that is created
1308 * @fprog: the filter program
1310 * Create a filter independent of any socket. We first run some
1311 * sanity checks on it to make sure it does not explode on us later.
1312 * If an error occurs or there is insufficient memory for the filter
1313 * a negative errno code is returned. On success the return is zero.
1315 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1317 unsigned int fsize = bpf_classic_proglen(fprog);
1318 struct bpf_prog *fp;
1320 /* Make sure new filter is there and in the right amounts. */
1321 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1324 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1328 memcpy(fp->insns, fprog->filter, fsize);
1330 fp->len = fprog->len;
1331 /* Since unattached filters are not copied back to user
1332 * space through sk_get_filter(), we do not need to hold
1333 * a copy here, and can spare us the work.
1335 fp->orig_prog = NULL;
1337 /* bpf_prepare_filter() already takes care of freeing
1338 * memory in case something goes wrong.
1340 fp = bpf_prepare_filter(fp, NULL);
1347 EXPORT_SYMBOL_GPL(bpf_prog_create);
1350 * bpf_prog_create_from_user - create an unattached filter from user buffer
1351 * @pfp: the unattached filter that is created
1352 * @fprog: the filter program
1353 * @trans: post-classic verifier transformation handler
1354 * @save_orig: save classic BPF program
1356 * This function effectively does the same as bpf_prog_create(), only
1357 * that it builds up its insns buffer from user space provided buffer.
1358 * It also allows for passing a bpf_aux_classic_check_t handler.
1360 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1361 bpf_aux_classic_check_t trans, bool save_orig)
1363 unsigned int fsize = bpf_classic_proglen(fprog);
1364 struct bpf_prog *fp;
1367 /* Make sure new filter is there and in the right amounts. */
1368 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1371 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1375 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1376 __bpf_prog_free(fp);
1380 fp->len = fprog->len;
1381 fp->orig_prog = NULL;
1384 err = bpf_prog_store_orig_filter(fp, fprog);
1386 __bpf_prog_free(fp);
1391 /* bpf_prepare_filter() already takes care of freeing
1392 * memory in case something goes wrong.
1394 fp = bpf_prepare_filter(fp, trans);
1401 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1403 void bpf_prog_destroy(struct bpf_prog *fp)
1405 __bpf_prog_release(fp);
1407 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1409 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1411 struct sk_filter *fp, *old_fp;
1413 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1419 if (!__sk_filter_charge(sk, fp)) {
1423 refcount_set(&fp->refcnt, 1);
1425 old_fp = rcu_dereference_protected(sk->sk_filter,
1426 lockdep_sock_is_held(sk));
1427 rcu_assign_pointer(sk->sk_filter, fp);
1430 sk_filter_uncharge(sk, old_fp);
1435 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1437 struct bpf_prog *old_prog;
1440 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1443 if (sk_unhashed(sk) && sk->sk_reuseport) {
1444 err = reuseport_alloc(sk);
1447 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1448 /* The socket wasn't bound with SO_REUSEPORT */
1452 old_prog = reuseport_attach_prog(sk, prog);
1454 bpf_prog_destroy(old_prog);
1460 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1462 unsigned int fsize = bpf_classic_proglen(fprog);
1463 struct bpf_prog *prog;
1466 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1467 return ERR_PTR(-EPERM);
1469 /* Make sure new filter is there and in the right amounts. */
1470 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1471 return ERR_PTR(-EINVAL);
1473 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1475 return ERR_PTR(-ENOMEM);
1477 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1478 __bpf_prog_free(prog);
1479 return ERR_PTR(-EFAULT);
1482 prog->len = fprog->len;
1484 err = bpf_prog_store_orig_filter(prog, fprog);
1486 __bpf_prog_free(prog);
1487 return ERR_PTR(-ENOMEM);
1490 /* bpf_prepare_filter() already takes care of freeing
1491 * memory in case something goes wrong.
1493 return bpf_prepare_filter(prog, NULL);
1497 * sk_attach_filter - attach a socket filter
1498 * @fprog: the filter program
1499 * @sk: the socket to use
1501 * Attach the user's filter code. We first run some sanity checks on
1502 * it to make sure it does not explode on us later. If an error
1503 * occurs or there is insufficient memory for the filter a negative
1504 * errno code is returned. On success the return is zero.
1506 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1508 struct bpf_prog *prog = __get_filter(fprog, sk);
1512 return PTR_ERR(prog);
1514 err = __sk_attach_prog(prog, sk);
1516 __bpf_prog_release(prog);
1522 EXPORT_SYMBOL_GPL(sk_attach_filter);
1524 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1526 struct bpf_prog *prog = __get_filter(fprog, sk);
1530 return PTR_ERR(prog);
1532 err = __reuseport_attach_prog(prog, sk);
1534 __bpf_prog_release(prog);
1541 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1543 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1544 return ERR_PTR(-EPERM);
1546 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1549 int sk_attach_bpf(u32 ufd, struct sock *sk)
1551 struct bpf_prog *prog = __get_bpf(ufd, sk);
1555 return PTR_ERR(prog);
1557 err = __sk_attach_prog(prog, sk);
1566 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1568 struct bpf_prog *prog = __get_bpf(ufd, sk);
1572 return PTR_ERR(prog);
1574 err = __reuseport_attach_prog(prog, sk);
1583 struct bpf_scratchpad {
1585 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1586 u8 buff[MAX_BPF_STACK];
1590 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1592 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1593 unsigned int write_len)
1595 return skb_ensure_writable(skb, write_len);
1598 static inline int bpf_try_make_writable(struct sk_buff *skb,
1599 unsigned int write_len)
1601 int err = __bpf_try_make_writable(skb, write_len);
1603 bpf_compute_data_pointers(skb);
1607 static int bpf_try_make_head_writable(struct sk_buff *skb)
1609 return bpf_try_make_writable(skb, skb_headlen(skb));
1612 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1614 if (skb_at_tc_ingress(skb))
1615 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1618 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1620 if (skb_at_tc_ingress(skb))
1621 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1624 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1625 const void *, from, u32, len, u64, flags)
1629 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1631 if (unlikely(offset > 0xffff))
1633 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1636 ptr = skb->data + offset;
1637 if (flags & BPF_F_RECOMPUTE_CSUM)
1638 __skb_postpull_rcsum(skb, ptr, len, offset);
1640 memcpy(ptr, from, len);
1642 if (flags & BPF_F_RECOMPUTE_CSUM)
1643 __skb_postpush_rcsum(skb, ptr, len, offset);
1644 if (flags & BPF_F_INVALIDATE_HASH)
1645 skb_clear_hash(skb);
1650 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1651 .func = bpf_skb_store_bytes,
1653 .ret_type = RET_INTEGER,
1654 .arg1_type = ARG_PTR_TO_CTX,
1655 .arg2_type = ARG_ANYTHING,
1656 .arg3_type = ARG_PTR_TO_MEM,
1657 .arg4_type = ARG_CONST_SIZE,
1658 .arg5_type = ARG_ANYTHING,
1661 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1662 void *, to, u32, len)
1666 if (unlikely(offset > 0xffff))
1669 ptr = skb_header_pointer(skb, offset, len, to);
1673 memcpy(to, ptr, len);
1681 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1682 .func = bpf_skb_load_bytes,
1684 .ret_type = RET_INTEGER,
1685 .arg1_type = ARG_PTR_TO_CTX,
1686 .arg2_type = ARG_ANYTHING,
1687 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1688 .arg4_type = ARG_CONST_SIZE,
1691 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1692 u32, offset, void *, to, u32, len, u32, start_header)
1696 if (unlikely(offset > 0xffff || len > skb_headlen(skb)))
1699 switch (start_header) {
1700 case BPF_HDR_START_MAC:
1701 ptr = skb_mac_header(skb) + offset;
1703 case BPF_HDR_START_NET:
1704 ptr = skb_network_header(skb) + offset;
1710 if (likely(ptr >= skb_mac_header(skb) &&
1711 ptr + len <= skb_tail_pointer(skb))) {
1712 memcpy(to, ptr, len);
1721 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1722 .func = bpf_skb_load_bytes_relative,
1724 .ret_type = RET_INTEGER,
1725 .arg1_type = ARG_PTR_TO_CTX,
1726 .arg2_type = ARG_ANYTHING,
1727 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1728 .arg4_type = ARG_CONST_SIZE,
1729 .arg5_type = ARG_ANYTHING,
1732 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1734 /* Idea is the following: should the needed direct read/write
1735 * test fail during runtime, we can pull in more data and redo
1736 * again, since implicitly, we invalidate previous checks here.
1738 * Or, since we know how much we need to make read/writeable,
1739 * this can be done once at the program beginning for direct
1740 * access case. By this we overcome limitations of only current
1741 * headroom being accessible.
1743 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1746 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1747 .func = bpf_skb_pull_data,
1749 .ret_type = RET_INTEGER,
1750 .arg1_type = ARG_PTR_TO_CTX,
1751 .arg2_type = ARG_ANYTHING,
1754 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1755 u64, from, u64, to, u64, flags)
1759 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1761 if (unlikely(offset > 0xffff || offset & 1))
1763 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1766 ptr = (__sum16 *)(skb->data + offset);
1767 switch (flags & BPF_F_HDR_FIELD_MASK) {
1769 if (unlikely(from != 0))
1772 csum_replace_by_diff(ptr, to);
1775 csum_replace2(ptr, from, to);
1778 csum_replace4(ptr, from, to);
1787 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1788 .func = bpf_l3_csum_replace,
1790 .ret_type = RET_INTEGER,
1791 .arg1_type = ARG_PTR_TO_CTX,
1792 .arg2_type = ARG_ANYTHING,
1793 .arg3_type = ARG_ANYTHING,
1794 .arg4_type = ARG_ANYTHING,
1795 .arg5_type = ARG_ANYTHING,
1798 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1799 u64, from, u64, to, u64, flags)
1801 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1802 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1803 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1806 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1807 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1809 if (unlikely(offset > 0xffff || offset & 1))
1811 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1814 ptr = (__sum16 *)(skb->data + offset);
1815 if (is_mmzero && !do_mforce && !*ptr)
1818 switch (flags & BPF_F_HDR_FIELD_MASK) {
1820 if (unlikely(from != 0))
1823 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1826 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1829 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1835 if (is_mmzero && !*ptr)
1836 *ptr = CSUM_MANGLED_0;
1840 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1841 .func = bpf_l4_csum_replace,
1843 .ret_type = RET_INTEGER,
1844 .arg1_type = ARG_PTR_TO_CTX,
1845 .arg2_type = ARG_ANYTHING,
1846 .arg3_type = ARG_ANYTHING,
1847 .arg4_type = ARG_ANYTHING,
1848 .arg5_type = ARG_ANYTHING,
1851 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1852 __be32 *, to, u32, to_size, __wsum, seed)
1854 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1855 u32 diff_size = from_size + to_size;
1858 /* This is quite flexible, some examples:
1860 * from_size == 0, to_size > 0, seed := csum --> pushing data
1861 * from_size > 0, to_size == 0, seed := csum --> pulling data
1862 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1864 * Even for diffing, from_size and to_size don't need to be equal.
1866 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1867 diff_size > sizeof(sp->diff)))
1870 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1871 sp->diff[j] = ~from[i];
1872 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1873 sp->diff[j] = to[i];
1875 return csum_partial(sp->diff, diff_size, seed);
1878 static const struct bpf_func_proto bpf_csum_diff_proto = {
1879 .func = bpf_csum_diff,
1882 .ret_type = RET_INTEGER,
1883 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1884 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1885 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1886 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1887 .arg5_type = ARG_ANYTHING,
1890 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1892 /* The interface is to be used in combination with bpf_csum_diff()
1893 * for direct packet writes. csum rotation for alignment as well
1894 * as emulating csum_sub() can be done from the eBPF program.
1896 if (skb->ip_summed == CHECKSUM_COMPLETE)
1897 return (skb->csum = csum_add(skb->csum, csum));
1902 static const struct bpf_func_proto bpf_csum_update_proto = {
1903 .func = bpf_csum_update,
1905 .ret_type = RET_INTEGER,
1906 .arg1_type = ARG_PTR_TO_CTX,
1907 .arg2_type = ARG_ANYTHING,
1910 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1912 return dev_forward_skb(dev, skb);
1915 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1916 struct sk_buff *skb)
1918 int ret = ____dev_forward_skb(dev, skb);
1922 ret = netif_rx(skb);
1928 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1932 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1933 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1940 __this_cpu_inc(xmit_recursion);
1941 ret = dev_queue_xmit(skb);
1942 __this_cpu_dec(xmit_recursion);
1947 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1950 /* skb->mac_len is not set on normal egress */
1951 unsigned int mlen = skb->network_header - skb->mac_header;
1953 __skb_pull(skb, mlen);
1955 /* At ingress, the mac header has already been pulled once.
1956 * At egress, skb_pospull_rcsum has to be done in case that
1957 * the skb is originated from ingress (i.e. a forwarded skb)
1958 * to ensure that rcsum starts at net header.
1960 if (!skb_at_tc_ingress(skb))
1961 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1962 skb_pop_mac_header(skb);
1963 skb_reset_mac_len(skb);
1964 return flags & BPF_F_INGRESS ?
1965 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1968 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1971 /* Verify that a link layer header is carried */
1972 if (unlikely(skb->mac_header >= skb->network_header)) {
1977 bpf_push_mac_rcsum(skb);
1978 return flags & BPF_F_INGRESS ?
1979 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1982 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1985 if (dev_is_mac_header_xmit(dev))
1986 return __bpf_redirect_common(skb, dev, flags);
1988 return __bpf_redirect_no_mac(skb, dev, flags);
1991 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1993 struct net_device *dev;
1994 struct sk_buff *clone;
1997 if (unlikely(flags & ~(BPF_F_INGRESS)))
2000 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2004 clone = skb_clone(skb, GFP_ATOMIC);
2005 if (unlikely(!clone))
2008 /* For direct write, we need to keep the invariant that the skbs
2009 * we're dealing with need to be uncloned. Should uncloning fail
2010 * here, we need to free the just generated clone to unclone once
2013 ret = bpf_try_make_head_writable(skb);
2014 if (unlikely(ret)) {
2019 return __bpf_redirect(clone, dev, flags);
2022 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2023 .func = bpf_clone_redirect,
2025 .ret_type = RET_INTEGER,
2026 .arg1_type = ARG_PTR_TO_CTX,
2027 .arg2_type = ARG_ANYTHING,
2028 .arg3_type = ARG_ANYTHING,
2031 struct redirect_info {
2034 struct bpf_map *map;
2035 struct bpf_map *map_to_flush;
2036 unsigned long map_owner;
2039 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
2041 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2043 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2045 if (unlikely(flags & ~(BPF_F_INGRESS)))
2048 ri->ifindex = ifindex;
2051 return TC_ACT_REDIRECT;
2054 int skb_do_redirect(struct sk_buff *skb)
2056 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2057 struct net_device *dev;
2059 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2061 if (unlikely(!dev)) {
2066 return __bpf_redirect(skb, dev, ri->flags);
2069 static const struct bpf_func_proto bpf_redirect_proto = {
2070 .func = bpf_redirect,
2072 .ret_type = RET_INTEGER,
2073 .arg1_type = ARG_ANYTHING,
2074 .arg2_type = ARG_ANYTHING,
2077 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
2078 struct bpf_map *, map, u32, key, u64, flags)
2080 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2082 /* If user passes invalid input drop the packet. */
2083 if (unlikely(flags & ~(BPF_F_INGRESS)))
2086 tcb->bpf.flags = flags;
2087 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
2088 if (!tcb->bpf.sk_redir)
2094 struct sock *do_sk_redirect_map(struct sk_buff *skb)
2096 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2098 return tcb->bpf.sk_redir;
2101 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
2102 .func = bpf_sk_redirect_map,
2104 .ret_type = RET_INTEGER,
2105 .arg1_type = ARG_PTR_TO_CTX,
2106 .arg2_type = ARG_CONST_MAP_PTR,
2107 .arg3_type = ARG_ANYTHING,
2108 .arg4_type = ARG_ANYTHING,
2111 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg_buff *, msg,
2112 struct bpf_map *, map, u32, key, u64, flags)
2114 /* If user passes invalid input drop the packet. */
2115 if (unlikely(flags & ~(BPF_F_INGRESS)))
2119 msg->sk_redir = __sock_map_lookup_elem(map, key);
2126 struct sock *do_msg_redirect_map(struct sk_msg_buff *msg)
2128 return msg->sk_redir;
2131 static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
2132 .func = bpf_msg_redirect_map,
2134 .ret_type = RET_INTEGER,
2135 .arg1_type = ARG_PTR_TO_CTX,
2136 .arg2_type = ARG_CONST_MAP_PTR,
2137 .arg3_type = ARG_ANYTHING,
2138 .arg4_type = ARG_ANYTHING,
2141 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg_buff *, msg, u32, bytes)
2143 msg->apply_bytes = bytes;
2147 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2148 .func = bpf_msg_apply_bytes,
2150 .ret_type = RET_INTEGER,
2151 .arg1_type = ARG_PTR_TO_CTX,
2152 .arg2_type = ARG_ANYTHING,
2155 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg_buff *, msg, u32, bytes)
2157 msg->cork_bytes = bytes;
2161 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2162 .func = bpf_msg_cork_bytes,
2164 .ret_type = RET_INTEGER,
2165 .arg1_type = ARG_PTR_TO_CTX,
2166 .arg2_type = ARG_ANYTHING,
2169 BPF_CALL_4(bpf_msg_pull_data,
2170 struct sk_msg_buff *, msg, u32, start, u32, end, u64, flags)
2172 unsigned int len = 0, offset = 0, copy = 0;
2173 struct scatterlist *sg = msg->sg_data;
2174 int first_sg, last_sg, i, shift;
2175 unsigned char *p, *to, *from;
2176 int bytes = end - start;
2179 if (unlikely(flags || end <= start))
2182 /* First find the starting scatterlist element */
2187 if (start < offset + len)
2190 if (i == MAX_SKB_FRAGS)
2192 } while (i != msg->sg_end);
2194 if (unlikely(start >= offset + len))
2197 if (!msg->sg_copy[i] && bytes <= len)
2202 /* At this point we need to linearize multiple scatterlist
2203 * elements or a single shared page. Either way we need to
2204 * copy into a linear buffer exclusively owned by BPF. Then
2205 * place the buffer in the scatterlist and fixup the original
2206 * entries by removing the entries now in the linear buffer
2207 * and shifting the remaining entries. For now we do not try
2208 * to copy partial entries to avoid complexity of running out
2209 * of sg_entry slots. The downside is reading a single byte
2210 * will copy the entire sg entry.
2213 copy += sg[i].length;
2215 if (i == MAX_SKB_FRAGS)
2219 } while (i != msg->sg_end);
2222 if (unlikely(copy < end - start))
2225 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC, get_order(copy));
2226 if (unlikely(!page))
2228 p = page_address(page);
2233 from = sg_virt(&sg[i]);
2237 memcpy(to, from, len);
2240 put_page(sg_page(&sg[i]));
2243 if (i == MAX_SKB_FRAGS)
2245 } while (i != last_sg);
2247 sg[first_sg].length = copy;
2248 sg_set_page(&sg[first_sg], page, copy, 0);
2250 /* To repair sg ring we need to shift entries. If we only
2251 * had a single entry though we can just replace it and
2252 * be done. Otherwise walk the ring and shift the entries.
2254 shift = last_sg - first_sg - 1;
2262 if (i + shift >= MAX_SKB_FRAGS)
2263 move_from = i + shift - MAX_SKB_FRAGS;
2265 move_from = i + shift;
2267 if (move_from == msg->sg_end)
2270 sg[i] = sg[move_from];
2271 sg[move_from].length = 0;
2272 sg[move_from].page_link = 0;
2273 sg[move_from].offset = 0;
2276 if (i == MAX_SKB_FRAGS)
2279 msg->sg_end -= shift;
2280 if (msg->sg_end < 0)
2281 msg->sg_end += MAX_SKB_FRAGS;
2283 msg->data = sg_virt(&sg[i]) + start - offset;
2284 msg->data_end = msg->data + bytes;
2289 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2290 .func = bpf_msg_pull_data,
2292 .ret_type = RET_INTEGER,
2293 .arg1_type = ARG_PTR_TO_CTX,
2294 .arg2_type = ARG_ANYTHING,
2295 .arg3_type = ARG_ANYTHING,
2296 .arg4_type = ARG_ANYTHING,
2299 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2301 return task_get_classid(skb);
2304 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2305 .func = bpf_get_cgroup_classid,
2307 .ret_type = RET_INTEGER,
2308 .arg1_type = ARG_PTR_TO_CTX,
2311 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2313 return dst_tclassid(skb);
2316 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2317 .func = bpf_get_route_realm,
2319 .ret_type = RET_INTEGER,
2320 .arg1_type = ARG_PTR_TO_CTX,
2323 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2325 /* If skb_clear_hash() was called due to mangling, we can
2326 * trigger SW recalculation here. Later access to hash
2327 * can then use the inline skb->hash via context directly
2328 * instead of calling this helper again.
2330 return skb_get_hash(skb);
2333 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2334 .func = bpf_get_hash_recalc,
2336 .ret_type = RET_INTEGER,
2337 .arg1_type = ARG_PTR_TO_CTX,
2340 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2342 /* After all direct packet write, this can be used once for
2343 * triggering a lazy recalc on next skb_get_hash() invocation.
2345 skb_clear_hash(skb);
2349 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2350 .func = bpf_set_hash_invalid,
2352 .ret_type = RET_INTEGER,
2353 .arg1_type = ARG_PTR_TO_CTX,
2356 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2358 /* Set user specified hash as L4(+), so that it gets returned
2359 * on skb_get_hash() call unless BPF prog later on triggers a
2362 __skb_set_sw_hash(skb, hash, true);
2366 static const struct bpf_func_proto bpf_set_hash_proto = {
2367 .func = bpf_set_hash,
2369 .ret_type = RET_INTEGER,
2370 .arg1_type = ARG_PTR_TO_CTX,
2371 .arg2_type = ARG_ANYTHING,
2374 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2379 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2380 vlan_proto != htons(ETH_P_8021AD)))
2381 vlan_proto = htons(ETH_P_8021Q);
2383 bpf_push_mac_rcsum(skb);
2384 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2385 bpf_pull_mac_rcsum(skb);
2387 bpf_compute_data_pointers(skb);
2391 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2392 .func = bpf_skb_vlan_push,
2394 .ret_type = RET_INTEGER,
2395 .arg1_type = ARG_PTR_TO_CTX,
2396 .arg2_type = ARG_ANYTHING,
2397 .arg3_type = ARG_ANYTHING,
2400 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2404 bpf_push_mac_rcsum(skb);
2405 ret = skb_vlan_pop(skb);
2406 bpf_pull_mac_rcsum(skb);
2408 bpf_compute_data_pointers(skb);
2412 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2413 .func = bpf_skb_vlan_pop,
2415 .ret_type = RET_INTEGER,
2416 .arg1_type = ARG_PTR_TO_CTX,
2419 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2421 /* Caller already did skb_cow() with len as headroom,
2422 * so no need to do it here.
2425 memmove(skb->data, skb->data + len, off);
2426 memset(skb->data + off, 0, len);
2428 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2429 * needed here as it does not change the skb->csum
2430 * result for checksum complete when summing over
2436 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2438 /* skb_ensure_writable() is not needed here, as we're
2439 * already working on an uncloned skb.
2441 if (unlikely(!pskb_may_pull(skb, off + len)))
2444 skb_postpull_rcsum(skb, skb->data + off, len);
2445 memmove(skb->data + len, skb->data, off);
2446 __skb_pull(skb, len);
2451 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2453 bool trans_same = skb->transport_header == skb->network_header;
2456 /* There's no need for __skb_push()/__skb_pull() pair to
2457 * get to the start of the mac header as we're guaranteed
2458 * to always start from here under eBPF.
2460 ret = bpf_skb_generic_push(skb, off, len);
2462 skb->mac_header -= len;
2463 skb->network_header -= len;
2465 skb->transport_header = skb->network_header;
2471 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2473 bool trans_same = skb->transport_header == skb->network_header;
2476 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2477 ret = bpf_skb_generic_pop(skb, off, len);
2479 skb->mac_header += len;
2480 skb->network_header += len;
2482 skb->transport_header = skb->network_header;
2488 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2490 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2491 u32 off = skb_mac_header_len(skb);
2494 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2495 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2498 ret = skb_cow(skb, len_diff);
2499 if (unlikely(ret < 0))
2502 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2503 if (unlikely(ret < 0))
2506 if (skb_is_gso(skb)) {
2507 struct skb_shared_info *shinfo = skb_shinfo(skb);
2509 /* SKB_GSO_TCPV4 needs to be changed into
2512 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2513 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2514 shinfo->gso_type |= SKB_GSO_TCPV6;
2517 /* Due to IPv6 header, MSS needs to be downgraded. */
2518 skb_decrease_gso_size(shinfo, len_diff);
2519 /* Header must be checked, and gso_segs recomputed. */
2520 shinfo->gso_type |= SKB_GSO_DODGY;
2521 shinfo->gso_segs = 0;
2524 skb->protocol = htons(ETH_P_IPV6);
2525 skb_clear_hash(skb);
2530 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2532 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2533 u32 off = skb_mac_header_len(skb);
2536 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2537 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2540 ret = skb_unclone(skb, GFP_ATOMIC);
2541 if (unlikely(ret < 0))
2544 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2545 if (unlikely(ret < 0))
2548 if (skb_is_gso(skb)) {
2549 struct skb_shared_info *shinfo = skb_shinfo(skb);
2551 /* SKB_GSO_TCPV6 needs to be changed into
2554 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2555 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2556 shinfo->gso_type |= SKB_GSO_TCPV4;
2559 /* Due to IPv4 header, MSS can be upgraded. */
2560 skb_increase_gso_size(shinfo, len_diff);
2561 /* Header must be checked, and gso_segs recomputed. */
2562 shinfo->gso_type |= SKB_GSO_DODGY;
2563 shinfo->gso_segs = 0;
2566 skb->protocol = htons(ETH_P_IP);
2567 skb_clear_hash(skb);
2572 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2574 __be16 from_proto = skb->protocol;
2576 if (from_proto == htons(ETH_P_IP) &&
2577 to_proto == htons(ETH_P_IPV6))
2578 return bpf_skb_proto_4_to_6(skb);
2580 if (from_proto == htons(ETH_P_IPV6) &&
2581 to_proto == htons(ETH_P_IP))
2582 return bpf_skb_proto_6_to_4(skb);
2587 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2592 if (unlikely(flags))
2595 /* General idea is that this helper does the basic groundwork
2596 * needed for changing the protocol, and eBPF program fills the
2597 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2598 * and other helpers, rather than passing a raw buffer here.
2600 * The rationale is to keep this minimal and without a need to
2601 * deal with raw packet data. F.e. even if we would pass buffers
2602 * here, the program still needs to call the bpf_lX_csum_replace()
2603 * helpers anyway. Plus, this way we keep also separation of
2604 * concerns, since f.e. bpf_skb_store_bytes() should only take
2607 * Currently, additional options and extension header space are
2608 * not supported, but flags register is reserved so we can adapt
2609 * that. For offloads, we mark packet as dodgy, so that headers
2610 * need to be verified first.
2612 ret = bpf_skb_proto_xlat(skb, proto);
2613 bpf_compute_data_pointers(skb);
2617 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2618 .func = bpf_skb_change_proto,
2620 .ret_type = RET_INTEGER,
2621 .arg1_type = ARG_PTR_TO_CTX,
2622 .arg2_type = ARG_ANYTHING,
2623 .arg3_type = ARG_ANYTHING,
2626 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2628 /* We only allow a restricted subset to be changed for now. */
2629 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2630 !skb_pkt_type_ok(pkt_type)))
2633 skb->pkt_type = pkt_type;
2637 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2638 .func = bpf_skb_change_type,
2640 .ret_type = RET_INTEGER,
2641 .arg1_type = ARG_PTR_TO_CTX,
2642 .arg2_type = ARG_ANYTHING,
2645 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2647 switch (skb->protocol) {
2648 case htons(ETH_P_IP):
2649 return sizeof(struct iphdr);
2650 case htons(ETH_P_IPV6):
2651 return sizeof(struct ipv6hdr);
2657 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2659 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2662 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2663 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2666 ret = skb_cow(skb, len_diff);
2667 if (unlikely(ret < 0))
2670 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2671 if (unlikely(ret < 0))
2674 if (skb_is_gso(skb)) {
2675 struct skb_shared_info *shinfo = skb_shinfo(skb);
2677 /* Due to header grow, MSS needs to be downgraded. */
2678 skb_decrease_gso_size(shinfo, len_diff);
2679 /* Header must be checked, and gso_segs recomputed. */
2680 shinfo->gso_type |= SKB_GSO_DODGY;
2681 shinfo->gso_segs = 0;
2687 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2689 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2692 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2693 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2696 ret = skb_unclone(skb, GFP_ATOMIC);
2697 if (unlikely(ret < 0))
2700 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2701 if (unlikely(ret < 0))
2704 if (skb_is_gso(skb)) {
2705 struct skb_shared_info *shinfo = skb_shinfo(skb);
2707 /* Due to header shrink, MSS can be upgraded. */
2708 skb_increase_gso_size(shinfo, len_diff);
2709 /* Header must be checked, and gso_segs recomputed. */
2710 shinfo->gso_type |= SKB_GSO_DODGY;
2711 shinfo->gso_segs = 0;
2717 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2719 return skb->dev->mtu + skb->dev->hard_header_len;
2722 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2724 bool trans_same = skb->transport_header == skb->network_header;
2725 u32 len_cur, len_diff_abs = abs(len_diff);
2726 u32 len_min = bpf_skb_net_base_len(skb);
2727 u32 len_max = __bpf_skb_max_len(skb);
2728 __be16 proto = skb->protocol;
2729 bool shrink = len_diff < 0;
2732 if (unlikely(len_diff_abs > 0xfffU))
2734 if (unlikely(proto != htons(ETH_P_IP) &&
2735 proto != htons(ETH_P_IPV6)))
2738 len_cur = skb->len - skb_network_offset(skb);
2739 if (skb_transport_header_was_set(skb) && !trans_same)
2740 len_cur = skb_network_header_len(skb);
2741 if ((shrink && (len_diff_abs >= len_cur ||
2742 len_cur - len_diff_abs < len_min)) ||
2743 (!shrink && (skb->len + len_diff_abs > len_max &&
2747 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2748 bpf_skb_net_grow(skb, len_diff_abs);
2750 bpf_compute_data_pointers(skb);
2754 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2755 u32, mode, u64, flags)
2757 if (unlikely(flags))
2759 if (likely(mode == BPF_ADJ_ROOM_NET))
2760 return bpf_skb_adjust_net(skb, len_diff);
2765 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2766 .func = bpf_skb_adjust_room,
2768 .ret_type = RET_INTEGER,
2769 .arg1_type = ARG_PTR_TO_CTX,
2770 .arg2_type = ARG_ANYTHING,
2771 .arg3_type = ARG_ANYTHING,
2772 .arg4_type = ARG_ANYTHING,
2775 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2777 u32 min_len = skb_network_offset(skb);
2779 if (skb_transport_header_was_set(skb))
2780 min_len = skb_transport_offset(skb);
2781 if (skb->ip_summed == CHECKSUM_PARTIAL)
2782 min_len = skb_checksum_start_offset(skb) +
2783 skb->csum_offset + sizeof(__sum16);
2787 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2789 unsigned int old_len = skb->len;
2792 ret = __skb_grow_rcsum(skb, new_len);
2794 memset(skb->data + old_len, 0, new_len - old_len);
2798 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2800 return __skb_trim_rcsum(skb, new_len);
2803 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2806 u32 max_len = __bpf_skb_max_len(skb);
2807 u32 min_len = __bpf_skb_min_len(skb);
2810 if (unlikely(flags || new_len > max_len || new_len < min_len))
2812 if (skb->encapsulation)
2815 /* The basic idea of this helper is that it's performing the
2816 * needed work to either grow or trim an skb, and eBPF program
2817 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2818 * bpf_lX_csum_replace() and others rather than passing a raw
2819 * buffer here. This one is a slow path helper and intended
2820 * for replies with control messages.
2822 * Like in bpf_skb_change_proto(), we want to keep this rather
2823 * minimal and without protocol specifics so that we are able
2824 * to separate concerns as in bpf_skb_store_bytes() should only
2825 * be the one responsible for writing buffers.
2827 * It's really expected to be a slow path operation here for
2828 * control message replies, so we're implicitly linearizing,
2829 * uncloning and drop offloads from the skb by this.
2831 ret = __bpf_try_make_writable(skb, skb->len);
2833 if (new_len > skb->len)
2834 ret = bpf_skb_grow_rcsum(skb, new_len);
2835 else if (new_len < skb->len)
2836 ret = bpf_skb_trim_rcsum(skb, new_len);
2837 if (!ret && skb_is_gso(skb))
2841 bpf_compute_data_pointers(skb);
2845 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2846 .func = bpf_skb_change_tail,
2848 .ret_type = RET_INTEGER,
2849 .arg1_type = ARG_PTR_TO_CTX,
2850 .arg2_type = ARG_ANYTHING,
2851 .arg3_type = ARG_ANYTHING,
2854 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2857 u32 max_len = __bpf_skb_max_len(skb);
2858 u32 new_len = skb->len + head_room;
2861 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2862 new_len < skb->len))
2865 ret = skb_cow(skb, head_room);
2867 /* Idea for this helper is that we currently only
2868 * allow to expand on mac header. This means that
2869 * skb->protocol network header, etc, stay as is.
2870 * Compared to bpf_skb_change_tail(), we're more
2871 * flexible due to not needing to linearize or
2872 * reset GSO. Intention for this helper is to be
2873 * used by an L3 skb that needs to push mac header
2874 * for redirection into L2 device.
2876 __skb_push(skb, head_room);
2877 memset(skb->data, 0, head_room);
2878 skb_reset_mac_header(skb);
2881 bpf_compute_data_pointers(skb);
2885 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2886 .func = bpf_skb_change_head,
2888 .ret_type = RET_INTEGER,
2889 .arg1_type = ARG_PTR_TO_CTX,
2890 .arg2_type = ARG_ANYTHING,
2891 .arg3_type = ARG_ANYTHING,
2894 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
2896 return xdp_data_meta_unsupported(xdp) ? 0 :
2897 xdp->data - xdp->data_meta;
2900 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2902 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
2903 unsigned long metalen = xdp_get_metalen(xdp);
2904 void *data_start = xdp_frame_end + metalen;
2905 void *data = xdp->data + offset;
2907 if (unlikely(data < data_start ||
2908 data > xdp->data_end - ETH_HLEN))
2912 memmove(xdp->data_meta + offset,
2913 xdp->data_meta, metalen);
2914 xdp->data_meta += offset;
2920 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2921 .func = bpf_xdp_adjust_head,
2923 .ret_type = RET_INTEGER,
2924 .arg1_type = ARG_PTR_TO_CTX,
2925 .arg2_type = ARG_ANYTHING,
2928 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
2930 void *data_end = xdp->data_end + offset;
2932 /* only shrinking is allowed for now. */
2933 if (unlikely(offset >= 0))
2936 if (unlikely(data_end < xdp->data + ETH_HLEN))
2939 xdp->data_end = data_end;
2944 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
2945 .func = bpf_xdp_adjust_tail,
2947 .ret_type = RET_INTEGER,
2948 .arg1_type = ARG_PTR_TO_CTX,
2949 .arg2_type = ARG_ANYTHING,
2952 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
2954 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
2955 void *meta = xdp->data_meta + offset;
2956 unsigned long metalen = xdp->data - meta;
2958 if (xdp_data_meta_unsupported(xdp))
2960 if (unlikely(meta < xdp_frame_end ||
2963 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
2967 xdp->data_meta = meta;
2972 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
2973 .func = bpf_xdp_adjust_meta,
2975 .ret_type = RET_INTEGER,
2976 .arg1_type = ARG_PTR_TO_CTX,
2977 .arg2_type = ARG_ANYTHING,
2980 static int __bpf_tx_xdp(struct net_device *dev,
2981 struct bpf_map *map,
2982 struct xdp_buff *xdp,
2985 struct xdp_frame *xdpf;
2988 if (!dev->netdev_ops->ndo_xdp_xmit) {
2992 xdpf = convert_to_xdp_frame(xdp);
2993 if (unlikely(!xdpf))
2996 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdpf);
2999 dev->netdev_ops->ndo_xdp_flush(dev);
3003 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3004 struct bpf_map *map,
3005 struct xdp_buff *xdp,
3010 switch (map->map_type) {
3011 case BPF_MAP_TYPE_DEVMAP: {
3012 struct net_device *dev = fwd;
3013 struct xdp_frame *xdpf;
3015 if (!dev->netdev_ops->ndo_xdp_xmit)
3018 xdpf = convert_to_xdp_frame(xdp);
3019 if (unlikely(!xdpf))
3022 /* TODO: move to inside map code instead, for bulk support
3023 * err = dev_map_enqueue(dev, xdp);
3025 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdpf);
3028 __dev_map_insert_ctx(map, index);
3031 case BPF_MAP_TYPE_CPUMAP: {
3032 struct bpf_cpu_map_entry *rcpu = fwd;
3034 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3037 __cpu_map_insert_ctx(map, index);
3040 case BPF_MAP_TYPE_XSKMAP: {
3041 struct xdp_sock *xs = fwd;
3043 err = __xsk_map_redirect(map, xdp, xs);
3052 void xdp_do_flush_map(void)
3054 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3055 struct bpf_map *map = ri->map_to_flush;
3057 ri->map_to_flush = NULL;
3059 switch (map->map_type) {
3060 case BPF_MAP_TYPE_DEVMAP:
3061 __dev_map_flush(map);
3063 case BPF_MAP_TYPE_CPUMAP:
3064 __cpu_map_flush(map);
3066 case BPF_MAP_TYPE_XSKMAP:
3067 __xsk_map_flush(map);
3074 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3076 static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3078 switch (map->map_type) {
3079 case BPF_MAP_TYPE_DEVMAP:
3080 return __dev_map_lookup_elem(map, index);
3081 case BPF_MAP_TYPE_CPUMAP:
3082 return __cpu_map_lookup_elem(map, index);
3083 case BPF_MAP_TYPE_XSKMAP:
3084 return __xsk_map_lookup_elem(map, index);
3090 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
3093 return (unsigned long)xdp_prog->aux != aux;
3096 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3097 struct bpf_prog *xdp_prog)
3099 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3100 unsigned long map_owner = ri->map_owner;
3101 struct bpf_map *map = ri->map;
3102 u32 index = ri->ifindex;
3110 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3116 fwd = __xdp_map_lookup_elem(map, index);
3121 if (ri->map_to_flush && ri->map_to_flush != map)
3124 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3128 ri->map_to_flush = map;
3129 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3132 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3136 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3137 struct bpf_prog *xdp_prog)
3139 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3140 struct net_device *fwd;
3141 u32 index = ri->ifindex;
3145 return xdp_do_redirect_map(dev, xdp, xdp_prog);
3147 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3149 if (unlikely(!fwd)) {
3154 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3158 _trace_xdp_redirect(dev, xdp_prog, index);
3161 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3164 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3166 static int __xdp_generic_ok_fwd_dev(struct sk_buff *skb, struct net_device *fwd)
3170 if (unlikely(!(fwd->flags & IFF_UP)))
3173 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
3180 static int xdp_do_generic_redirect_map(struct net_device *dev,
3181 struct sk_buff *skb,
3182 struct xdp_buff *xdp,
3183 struct bpf_prog *xdp_prog)
3185 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3186 unsigned long map_owner = ri->map_owner;
3187 struct bpf_map *map = ri->map;
3188 u32 index = ri->ifindex;
3196 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3201 fwd = __xdp_map_lookup_elem(map, index);
3202 if (unlikely(!fwd)) {
3207 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3208 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
3211 generic_xdp_tx(skb, xdp_prog);
3212 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3213 struct xdp_sock *xs = fwd;
3215 err = xsk_generic_rcv(xs, xdp);
3220 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3225 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3228 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3232 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3233 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3235 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3236 u32 index = ri->ifindex;
3237 struct net_device *fwd;
3241 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog);
3244 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3245 if (unlikely(!fwd)) {
3250 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
3254 _trace_xdp_redirect(dev, xdp_prog, index);
3255 generic_xdp_tx(skb, xdp_prog);
3258 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3261 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3263 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3265 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3267 if (unlikely(flags))
3270 ri->ifindex = ifindex;
3275 return XDP_REDIRECT;
3278 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3279 .func = bpf_xdp_redirect,
3281 .ret_type = RET_INTEGER,
3282 .arg1_type = ARG_ANYTHING,
3283 .arg2_type = ARG_ANYTHING,
3286 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
3287 unsigned long, map_owner)
3289 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3291 if (unlikely(flags))
3294 ri->ifindex = ifindex;
3297 ri->map_owner = map_owner;
3299 return XDP_REDIRECT;
3302 /* Note, arg4 is hidden from users and populated by the verifier
3303 * with the right pointer.
3305 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3306 .func = bpf_xdp_redirect_map,
3308 .ret_type = RET_INTEGER,
3309 .arg1_type = ARG_CONST_MAP_PTR,
3310 .arg2_type = ARG_ANYTHING,
3311 .arg3_type = ARG_ANYTHING,
3314 bool bpf_helper_changes_pkt_data(void *func)
3316 if (func == bpf_skb_vlan_push ||
3317 func == bpf_skb_vlan_pop ||
3318 func == bpf_skb_store_bytes ||
3319 func == bpf_skb_change_proto ||
3320 func == bpf_skb_change_head ||
3321 func == bpf_skb_change_tail ||
3322 func == bpf_skb_adjust_room ||
3323 func == bpf_skb_pull_data ||
3324 func == bpf_clone_redirect ||
3325 func == bpf_l3_csum_replace ||
3326 func == bpf_l4_csum_replace ||
3327 func == bpf_xdp_adjust_head ||
3328 func == bpf_xdp_adjust_meta ||
3329 func == bpf_msg_pull_data ||
3330 func == bpf_xdp_adjust_tail)
3336 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3337 unsigned long off, unsigned long len)
3339 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3343 if (ptr != dst_buff)
3344 memcpy(dst_buff, ptr, len);
3349 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3350 u64, flags, void *, meta, u64, meta_size)
3352 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3354 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3356 if (unlikely(skb_size > skb->len))
3359 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3363 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3364 .func = bpf_skb_event_output,
3366 .ret_type = RET_INTEGER,
3367 .arg1_type = ARG_PTR_TO_CTX,
3368 .arg2_type = ARG_CONST_MAP_PTR,
3369 .arg3_type = ARG_ANYTHING,
3370 .arg4_type = ARG_PTR_TO_MEM,
3371 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3374 static unsigned short bpf_tunnel_key_af(u64 flags)
3376 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3379 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3380 u32, size, u64, flags)
3382 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3383 u8 compat[sizeof(struct bpf_tunnel_key)];
3387 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3391 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3395 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3398 case offsetof(struct bpf_tunnel_key, tunnel_label):
3399 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3401 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3402 /* Fixup deprecated structure layouts here, so we have
3403 * a common path later on.
3405 if (ip_tunnel_info_af(info) != AF_INET)
3408 to = (struct bpf_tunnel_key *)compat;
3415 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3416 to->tunnel_tos = info->key.tos;
3417 to->tunnel_ttl = info->key.ttl;
3419 if (flags & BPF_F_TUNINFO_IPV6) {
3420 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3421 sizeof(to->remote_ipv6));
3422 to->tunnel_label = be32_to_cpu(info->key.label);
3424 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3427 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3428 memcpy(to_orig, to, size);
3432 memset(to_orig, 0, size);
3436 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3437 .func = bpf_skb_get_tunnel_key,
3439 .ret_type = RET_INTEGER,
3440 .arg1_type = ARG_PTR_TO_CTX,
3441 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3442 .arg3_type = ARG_CONST_SIZE,
3443 .arg4_type = ARG_ANYTHING,
3446 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3448 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3451 if (unlikely(!info ||
3452 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3456 if (unlikely(size < info->options_len)) {
3461 ip_tunnel_info_opts_get(to, info);
3462 if (size > info->options_len)
3463 memset(to + info->options_len, 0, size - info->options_len);
3465 return info->options_len;
3467 memset(to, 0, size);
3471 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3472 .func = bpf_skb_get_tunnel_opt,
3474 .ret_type = RET_INTEGER,
3475 .arg1_type = ARG_PTR_TO_CTX,
3476 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3477 .arg3_type = ARG_CONST_SIZE,
3480 static struct metadata_dst __percpu *md_dst;
3482 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3483 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3485 struct metadata_dst *md = this_cpu_ptr(md_dst);
3486 u8 compat[sizeof(struct bpf_tunnel_key)];
3487 struct ip_tunnel_info *info;
3489 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3490 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3492 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3494 case offsetof(struct bpf_tunnel_key, tunnel_label):
3495 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3496 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3497 /* Fixup deprecated structure layouts here, so we have
3498 * a common path later on.
3500 memcpy(compat, from, size);
3501 memset(compat + size, 0, sizeof(compat) - size);
3502 from = (const struct bpf_tunnel_key *) compat;
3508 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3513 dst_hold((struct dst_entry *) md);
3514 skb_dst_set(skb, (struct dst_entry *) md);
3516 info = &md->u.tun_info;
3517 memset(info, 0, sizeof(*info));
3518 info->mode = IP_TUNNEL_INFO_TX;
3520 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3521 if (flags & BPF_F_DONT_FRAGMENT)
3522 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3523 if (flags & BPF_F_ZERO_CSUM_TX)
3524 info->key.tun_flags &= ~TUNNEL_CSUM;
3525 if (flags & BPF_F_SEQ_NUMBER)
3526 info->key.tun_flags |= TUNNEL_SEQ;
3528 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3529 info->key.tos = from->tunnel_tos;
3530 info->key.ttl = from->tunnel_ttl;
3532 if (flags & BPF_F_TUNINFO_IPV6) {
3533 info->mode |= IP_TUNNEL_INFO_IPV6;
3534 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3535 sizeof(from->remote_ipv6));
3536 info->key.label = cpu_to_be32(from->tunnel_label) &
3537 IPV6_FLOWLABEL_MASK;
3539 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3545 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3546 .func = bpf_skb_set_tunnel_key,
3548 .ret_type = RET_INTEGER,
3549 .arg1_type = ARG_PTR_TO_CTX,
3550 .arg2_type = ARG_PTR_TO_MEM,
3551 .arg3_type = ARG_CONST_SIZE,
3552 .arg4_type = ARG_ANYTHING,
3555 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3556 const u8 *, from, u32, size)
3558 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3559 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3561 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3563 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3566 ip_tunnel_info_opts_set(info, from, size);
3571 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3572 .func = bpf_skb_set_tunnel_opt,
3574 .ret_type = RET_INTEGER,
3575 .arg1_type = ARG_PTR_TO_CTX,
3576 .arg2_type = ARG_PTR_TO_MEM,
3577 .arg3_type = ARG_CONST_SIZE,
3580 static const struct bpf_func_proto *
3581 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3584 struct metadata_dst __percpu *tmp;
3586 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3591 if (cmpxchg(&md_dst, NULL, tmp))
3592 metadata_dst_free_percpu(tmp);
3596 case BPF_FUNC_skb_set_tunnel_key:
3597 return &bpf_skb_set_tunnel_key_proto;
3598 case BPF_FUNC_skb_set_tunnel_opt:
3599 return &bpf_skb_set_tunnel_opt_proto;
3605 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3608 struct bpf_array *array = container_of(map, struct bpf_array, map);
3609 struct cgroup *cgrp;
3612 sk = skb_to_full_sk(skb);
3613 if (!sk || !sk_fullsock(sk))
3615 if (unlikely(idx >= array->map.max_entries))
3618 cgrp = READ_ONCE(array->ptrs[idx]);
3619 if (unlikely(!cgrp))
3622 return sk_under_cgroup_hierarchy(sk, cgrp);
3625 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3626 .func = bpf_skb_under_cgroup,
3628 .ret_type = RET_INTEGER,
3629 .arg1_type = ARG_PTR_TO_CTX,
3630 .arg2_type = ARG_CONST_MAP_PTR,
3631 .arg3_type = ARG_ANYTHING,
3634 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3635 unsigned long off, unsigned long len)
3637 memcpy(dst_buff, src_buff + off, len);
3641 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3642 u64, flags, void *, meta, u64, meta_size)
3644 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3646 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3648 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3651 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3652 xdp_size, bpf_xdp_copy);
3655 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3656 .func = bpf_xdp_event_output,
3658 .ret_type = RET_INTEGER,
3659 .arg1_type = ARG_PTR_TO_CTX,
3660 .arg2_type = ARG_CONST_MAP_PTR,
3661 .arg3_type = ARG_ANYTHING,
3662 .arg4_type = ARG_PTR_TO_MEM,
3663 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3666 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3668 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3671 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3672 .func = bpf_get_socket_cookie,
3674 .ret_type = RET_INTEGER,
3675 .arg1_type = ARG_PTR_TO_CTX,
3678 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3680 struct sock *sk = sk_to_full_sk(skb->sk);
3683 if (!sk || !sk_fullsock(sk))
3685 kuid = sock_net_uid(sock_net(sk), sk);
3686 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3689 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3690 .func = bpf_get_socket_uid,
3692 .ret_type = RET_INTEGER,
3693 .arg1_type = ARG_PTR_TO_CTX,
3696 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3697 int, level, int, optname, char *, optval, int, optlen)
3699 struct sock *sk = bpf_sock->sk;
3703 if (!sk_fullsock(sk))
3706 if (level == SOL_SOCKET) {
3707 if (optlen != sizeof(int))
3709 val = *((int *)optval);
3711 /* Only some socketops are supported */
3714 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3715 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3718 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3719 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3721 case SO_MAX_PACING_RATE:
3722 sk->sk_max_pacing_rate = val;
3723 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3724 sk->sk_max_pacing_rate);
3727 sk->sk_priority = val;
3732 sk->sk_rcvlowat = val ? : 1;
3741 } else if (level == SOL_IP) {
3742 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3745 val = *((int *)optval);
3746 /* Only some options are supported */
3749 if (val < -1 || val > 0xff) {
3752 struct inet_sock *inet = inet_sk(sk);
3762 #if IS_ENABLED(CONFIG_IPV6)
3763 } else if (level == SOL_IPV6) {
3764 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3767 val = *((int *)optval);
3768 /* Only some options are supported */
3771 if (val < -1 || val > 0xff) {
3774 struct ipv6_pinfo *np = inet6_sk(sk);
3785 } else if (level == SOL_TCP &&
3786 sk->sk_prot->setsockopt == tcp_setsockopt) {
3787 if (optname == TCP_CONGESTION) {
3788 char name[TCP_CA_NAME_MAX];
3789 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3791 strncpy(name, optval, min_t(long, optlen,
3792 TCP_CA_NAME_MAX-1));
3793 name[TCP_CA_NAME_MAX-1] = 0;
3794 ret = tcp_set_congestion_control(sk, name, false,
3797 struct tcp_sock *tp = tcp_sk(sk);
3799 if (optlen != sizeof(int))
3802 val = *((int *)optval);
3803 /* Only some options are supported */
3806 if (val <= 0 || tp->data_segs_out > 0)
3811 case TCP_BPF_SNDCWND_CLAMP:
3815 tp->snd_cwnd_clamp = val;
3816 tp->snd_ssthresh = val;
3830 static const struct bpf_func_proto bpf_setsockopt_proto = {
3831 .func = bpf_setsockopt,
3833 .ret_type = RET_INTEGER,
3834 .arg1_type = ARG_PTR_TO_CTX,
3835 .arg2_type = ARG_ANYTHING,
3836 .arg3_type = ARG_ANYTHING,
3837 .arg4_type = ARG_PTR_TO_MEM,
3838 .arg5_type = ARG_CONST_SIZE,
3841 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3842 int, level, int, optname, char *, optval, int, optlen)
3844 struct sock *sk = bpf_sock->sk;
3846 if (!sk_fullsock(sk))
3850 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3851 if (optname == TCP_CONGESTION) {
3852 struct inet_connection_sock *icsk = inet_csk(sk);
3854 if (!icsk->icsk_ca_ops || optlen <= 1)
3856 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3857 optval[optlen - 1] = 0;
3861 } else if (level == SOL_IP) {
3862 struct inet_sock *inet = inet_sk(sk);
3864 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3867 /* Only some options are supported */
3870 *((int *)optval) = (int)inet->tos;
3875 #if IS_ENABLED(CONFIG_IPV6)
3876 } else if (level == SOL_IPV6) {
3877 struct ipv6_pinfo *np = inet6_sk(sk);
3879 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3882 /* Only some options are supported */
3885 *((int *)optval) = (int)np->tclass;
3897 memset(optval, 0, optlen);
3901 static const struct bpf_func_proto bpf_getsockopt_proto = {
3902 .func = bpf_getsockopt,
3904 .ret_type = RET_INTEGER,
3905 .arg1_type = ARG_PTR_TO_CTX,
3906 .arg2_type = ARG_ANYTHING,
3907 .arg3_type = ARG_ANYTHING,
3908 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
3909 .arg5_type = ARG_CONST_SIZE,
3912 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
3915 struct sock *sk = bpf_sock->sk;
3916 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
3918 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
3922 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
3924 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
3927 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
3928 .func = bpf_sock_ops_cb_flags_set,
3930 .ret_type = RET_INTEGER,
3931 .arg1_type = ARG_PTR_TO_CTX,
3932 .arg2_type = ARG_ANYTHING,
3935 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
3936 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
3938 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
3942 struct sock *sk = ctx->sk;
3945 /* Binding to port can be expensive so it's prohibited in the helper.
3946 * Only binding to IP is supported.
3949 if (addr->sa_family == AF_INET) {
3950 if (addr_len < sizeof(struct sockaddr_in))
3952 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3954 return __inet_bind(sk, addr, addr_len, true, false);
3955 #if IS_ENABLED(CONFIG_IPV6)
3956 } else if (addr->sa_family == AF_INET6) {
3957 if (addr_len < SIN6_LEN_RFC2133)
3959 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3961 /* ipv6_bpf_stub cannot be NULL, since it's called from
3962 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
3964 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
3965 #endif /* CONFIG_IPV6 */
3967 #endif /* CONFIG_INET */
3969 return -EAFNOSUPPORT;
3972 static const struct bpf_func_proto bpf_bind_proto = {
3975 .ret_type = RET_INTEGER,
3976 .arg1_type = ARG_PTR_TO_CTX,
3977 .arg2_type = ARG_PTR_TO_MEM,
3978 .arg3_type = ARG_CONST_SIZE,
3982 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
3983 struct bpf_xfrm_state *, to, u32, size, u64, flags)
3985 const struct sec_path *sp = skb_sec_path(skb);
3986 const struct xfrm_state *x;
3988 if (!sp || unlikely(index >= sp->len || flags))
3991 x = sp->xvec[index];
3993 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
3996 to->reqid = x->props.reqid;
3997 to->spi = x->id.spi;
3998 to->family = x->props.family;
3999 if (to->family == AF_INET6) {
4000 memcpy(to->remote_ipv6, x->props.saddr.a6,
4001 sizeof(to->remote_ipv6));
4003 to->remote_ipv4 = x->props.saddr.a4;
4008 memset(to, 0, size);
4012 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4013 .func = bpf_skb_get_xfrm_state,
4015 .ret_type = RET_INTEGER,
4016 .arg1_type = ARG_PTR_TO_CTX,
4017 .arg2_type = ARG_ANYTHING,
4018 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4019 .arg4_type = ARG_CONST_SIZE,
4020 .arg5_type = ARG_ANYTHING,
4024 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4025 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4026 const struct neighbour *neigh,
4027 const struct net_device *dev)
4029 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4030 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4031 params->h_vlan_TCI = 0;
4032 params->h_vlan_proto = 0;
4034 return dev->ifindex;
4038 #if IS_ENABLED(CONFIG_INET)
4039 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4042 struct in_device *in_dev;
4043 struct neighbour *neigh;
4044 struct net_device *dev;
4045 struct fib_result res;
4050 dev = dev_get_by_index_rcu(net, params->ifindex);
4054 /* verify forwarding is enabled on this interface */
4055 in_dev = __in_dev_get_rcu(dev);
4056 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4059 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4061 fl4.flowi4_oif = params->ifindex;
4063 fl4.flowi4_iif = params->ifindex;
4066 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4067 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4068 fl4.flowi4_flags = 0;
4070 fl4.flowi4_proto = params->l4_protocol;
4071 fl4.daddr = params->ipv4_dst;
4072 fl4.saddr = params->ipv4_src;
4073 fl4.fl4_sport = params->sport;
4074 fl4.fl4_dport = params->dport;
4076 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4077 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4078 struct fib_table *tb;
4080 tb = fib_get_table(net, tbid);
4084 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4086 fl4.flowi4_mark = 0;
4087 fl4.flowi4_secid = 0;
4088 fl4.flowi4_tun_key.tun_id = 0;
4089 fl4.flowi4_uid = sock_net_uid(net, NULL);
4091 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4094 if (err || res.type != RTN_UNICAST)
4097 if (res.fi->fib_nhs > 1)
4098 fib_select_path(net, &res, &fl4, NULL);
4100 nh = &res.fi->fib_nh[res.nh_sel];
4102 /* do not handle lwt encaps right now */
4103 if (nh->nh_lwtstate)
4111 params->ipv4_dst = nh->nh_gw;
4113 params->rt_metric = res.fi->fib_priority;
4115 /* xdp and cls_bpf programs are run in RCU-bh so
4116 * rcu_read_lock_bh is not needed here
4118 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4120 return bpf_fib_set_fwd_params(params, neigh, dev);
4126 #if IS_ENABLED(CONFIG_IPV6)
4127 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4130 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4131 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4132 struct neighbour *neigh;
4133 struct net_device *dev;
4134 struct inet6_dev *idev;
4135 struct fib6_info *f6i;
4140 /* link local addresses are never forwarded */
4141 if (rt6_need_strict(dst) || rt6_need_strict(src))
4144 dev = dev_get_by_index_rcu(net, params->ifindex);
4148 idev = __in6_dev_get_safely(dev);
4149 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4152 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4154 oif = fl6.flowi6_oif = params->ifindex;
4156 oif = fl6.flowi6_iif = params->ifindex;
4158 strict = RT6_LOOKUP_F_HAS_SADDR;
4160 fl6.flowlabel = params->flowlabel;
4161 fl6.flowi6_scope = 0;
4162 fl6.flowi6_flags = 0;
4165 fl6.flowi6_proto = params->l4_protocol;
4168 fl6.fl6_sport = params->sport;
4169 fl6.fl6_dport = params->dport;
4171 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4172 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4173 struct fib6_table *tb;
4175 tb = ipv6_stub->fib6_get_table(net, tbid);
4179 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4181 fl6.flowi6_mark = 0;
4182 fl6.flowi6_secid = 0;
4183 fl6.flowi6_tun_key.tun_id = 0;
4184 fl6.flowi6_uid = sock_net_uid(net, NULL);
4186 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4189 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4192 if (unlikely(f6i->fib6_flags & RTF_REJECT ||
4193 f6i->fib6_type != RTN_UNICAST))
4196 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4197 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4198 fl6.flowi6_oif, NULL,
4201 if (f6i->fib6_nh.nh_lwtstate)
4204 if (f6i->fib6_flags & RTF_GATEWAY)
4205 *dst = f6i->fib6_nh.nh_gw;
4207 dev = f6i->fib6_nh.nh_dev;
4208 params->rt_metric = f6i->fib6_metric;
4210 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4211 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4212 * because we need to get nd_tbl via the stub
4214 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4215 ndisc_hashfn, dst, dev);
4217 return bpf_fib_set_fwd_params(params, neigh, dev);
4223 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4224 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4226 if (plen < sizeof(*params))
4229 switch (params->family) {
4230 #if IS_ENABLED(CONFIG_INET)
4232 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4235 #if IS_ENABLED(CONFIG_IPV6)
4237 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4244 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4245 .func = bpf_xdp_fib_lookup,
4247 .ret_type = RET_INTEGER,
4248 .arg1_type = ARG_PTR_TO_CTX,
4249 .arg2_type = ARG_PTR_TO_MEM,
4250 .arg3_type = ARG_CONST_SIZE,
4251 .arg4_type = ARG_ANYTHING,
4254 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4255 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4257 if (plen < sizeof(*params))
4260 switch (params->family) {
4261 #if IS_ENABLED(CONFIG_INET)
4263 return bpf_ipv4_fib_lookup(dev_net(skb->dev), params, flags);
4265 #if IS_ENABLED(CONFIG_IPV6)
4267 return bpf_ipv6_fib_lookup(dev_net(skb->dev), params, flags);
4273 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4274 .func = bpf_skb_fib_lookup,
4276 .ret_type = RET_INTEGER,
4277 .arg1_type = ARG_PTR_TO_CTX,
4278 .arg2_type = ARG_PTR_TO_MEM,
4279 .arg3_type = ARG_CONST_SIZE,
4280 .arg4_type = ARG_ANYTHING,
4283 static const struct bpf_func_proto *
4284 bpf_base_func_proto(enum bpf_func_id func_id)
4287 case BPF_FUNC_map_lookup_elem:
4288 return &bpf_map_lookup_elem_proto;
4289 case BPF_FUNC_map_update_elem:
4290 return &bpf_map_update_elem_proto;
4291 case BPF_FUNC_map_delete_elem:
4292 return &bpf_map_delete_elem_proto;
4293 case BPF_FUNC_get_prandom_u32:
4294 return &bpf_get_prandom_u32_proto;
4295 case BPF_FUNC_get_smp_processor_id:
4296 return &bpf_get_raw_smp_processor_id_proto;
4297 case BPF_FUNC_get_numa_node_id:
4298 return &bpf_get_numa_node_id_proto;
4299 case BPF_FUNC_tail_call:
4300 return &bpf_tail_call_proto;
4301 case BPF_FUNC_ktime_get_ns:
4302 return &bpf_ktime_get_ns_proto;
4303 case BPF_FUNC_trace_printk:
4304 if (capable(CAP_SYS_ADMIN))
4305 return bpf_get_trace_printk_proto();
4311 static const struct bpf_func_proto *
4312 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4315 /* inet and inet6 sockets are created in a process
4316 * context so there is always a valid uid/gid
4318 case BPF_FUNC_get_current_uid_gid:
4319 return &bpf_get_current_uid_gid_proto;
4321 return bpf_base_func_proto(func_id);
4325 static const struct bpf_func_proto *
4326 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4329 /* inet and inet6 sockets are created in a process
4330 * context so there is always a valid uid/gid
4332 case BPF_FUNC_get_current_uid_gid:
4333 return &bpf_get_current_uid_gid_proto;
4335 switch (prog->expected_attach_type) {
4336 case BPF_CGROUP_INET4_CONNECT:
4337 case BPF_CGROUP_INET6_CONNECT:
4338 return &bpf_bind_proto;
4343 return bpf_base_func_proto(func_id);
4347 static const struct bpf_func_proto *
4348 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4351 case BPF_FUNC_skb_load_bytes:
4352 return &bpf_skb_load_bytes_proto;
4353 case BPF_FUNC_skb_load_bytes_relative:
4354 return &bpf_skb_load_bytes_relative_proto;
4355 case BPF_FUNC_get_socket_cookie:
4356 return &bpf_get_socket_cookie_proto;
4357 case BPF_FUNC_get_socket_uid:
4358 return &bpf_get_socket_uid_proto;
4360 return bpf_base_func_proto(func_id);
4364 static const struct bpf_func_proto *
4365 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4368 case BPF_FUNC_skb_store_bytes:
4369 return &bpf_skb_store_bytes_proto;
4370 case BPF_FUNC_skb_load_bytes:
4371 return &bpf_skb_load_bytes_proto;
4372 case BPF_FUNC_skb_load_bytes_relative:
4373 return &bpf_skb_load_bytes_relative_proto;
4374 case BPF_FUNC_skb_pull_data:
4375 return &bpf_skb_pull_data_proto;
4376 case BPF_FUNC_csum_diff:
4377 return &bpf_csum_diff_proto;
4378 case BPF_FUNC_csum_update:
4379 return &bpf_csum_update_proto;
4380 case BPF_FUNC_l3_csum_replace:
4381 return &bpf_l3_csum_replace_proto;
4382 case BPF_FUNC_l4_csum_replace:
4383 return &bpf_l4_csum_replace_proto;
4384 case BPF_FUNC_clone_redirect:
4385 return &bpf_clone_redirect_proto;
4386 case BPF_FUNC_get_cgroup_classid:
4387 return &bpf_get_cgroup_classid_proto;
4388 case BPF_FUNC_skb_vlan_push:
4389 return &bpf_skb_vlan_push_proto;
4390 case BPF_FUNC_skb_vlan_pop:
4391 return &bpf_skb_vlan_pop_proto;
4392 case BPF_FUNC_skb_change_proto:
4393 return &bpf_skb_change_proto_proto;
4394 case BPF_FUNC_skb_change_type:
4395 return &bpf_skb_change_type_proto;
4396 case BPF_FUNC_skb_adjust_room:
4397 return &bpf_skb_adjust_room_proto;
4398 case BPF_FUNC_skb_change_tail:
4399 return &bpf_skb_change_tail_proto;
4400 case BPF_FUNC_skb_get_tunnel_key:
4401 return &bpf_skb_get_tunnel_key_proto;
4402 case BPF_FUNC_skb_set_tunnel_key:
4403 return bpf_get_skb_set_tunnel_proto(func_id);
4404 case BPF_FUNC_skb_get_tunnel_opt:
4405 return &bpf_skb_get_tunnel_opt_proto;
4406 case BPF_FUNC_skb_set_tunnel_opt:
4407 return bpf_get_skb_set_tunnel_proto(func_id);
4408 case BPF_FUNC_redirect:
4409 return &bpf_redirect_proto;
4410 case BPF_FUNC_get_route_realm:
4411 return &bpf_get_route_realm_proto;
4412 case BPF_FUNC_get_hash_recalc:
4413 return &bpf_get_hash_recalc_proto;
4414 case BPF_FUNC_set_hash_invalid:
4415 return &bpf_set_hash_invalid_proto;
4416 case BPF_FUNC_set_hash:
4417 return &bpf_set_hash_proto;
4418 case BPF_FUNC_perf_event_output:
4419 return &bpf_skb_event_output_proto;
4420 case BPF_FUNC_get_smp_processor_id:
4421 return &bpf_get_smp_processor_id_proto;
4422 case BPF_FUNC_skb_under_cgroup:
4423 return &bpf_skb_under_cgroup_proto;
4424 case BPF_FUNC_get_socket_cookie:
4425 return &bpf_get_socket_cookie_proto;
4426 case BPF_FUNC_get_socket_uid:
4427 return &bpf_get_socket_uid_proto;
4429 case BPF_FUNC_skb_get_xfrm_state:
4430 return &bpf_skb_get_xfrm_state_proto;
4432 case BPF_FUNC_fib_lookup:
4433 return &bpf_skb_fib_lookup_proto;
4435 return bpf_base_func_proto(func_id);
4439 static const struct bpf_func_proto *
4440 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4443 case BPF_FUNC_perf_event_output:
4444 return &bpf_xdp_event_output_proto;
4445 case BPF_FUNC_get_smp_processor_id:
4446 return &bpf_get_smp_processor_id_proto;
4447 case BPF_FUNC_csum_diff:
4448 return &bpf_csum_diff_proto;
4449 case BPF_FUNC_xdp_adjust_head:
4450 return &bpf_xdp_adjust_head_proto;
4451 case BPF_FUNC_xdp_adjust_meta:
4452 return &bpf_xdp_adjust_meta_proto;
4453 case BPF_FUNC_redirect:
4454 return &bpf_xdp_redirect_proto;
4455 case BPF_FUNC_redirect_map:
4456 return &bpf_xdp_redirect_map_proto;
4457 case BPF_FUNC_xdp_adjust_tail:
4458 return &bpf_xdp_adjust_tail_proto;
4459 case BPF_FUNC_fib_lookup:
4460 return &bpf_xdp_fib_lookup_proto;
4462 return bpf_base_func_proto(func_id);
4466 static const struct bpf_func_proto *
4467 lwt_inout_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4470 case BPF_FUNC_skb_load_bytes:
4471 return &bpf_skb_load_bytes_proto;
4472 case BPF_FUNC_skb_pull_data:
4473 return &bpf_skb_pull_data_proto;
4474 case BPF_FUNC_csum_diff:
4475 return &bpf_csum_diff_proto;
4476 case BPF_FUNC_get_cgroup_classid:
4477 return &bpf_get_cgroup_classid_proto;
4478 case BPF_FUNC_get_route_realm:
4479 return &bpf_get_route_realm_proto;
4480 case BPF_FUNC_get_hash_recalc:
4481 return &bpf_get_hash_recalc_proto;
4482 case BPF_FUNC_perf_event_output:
4483 return &bpf_skb_event_output_proto;
4484 case BPF_FUNC_get_smp_processor_id:
4485 return &bpf_get_smp_processor_id_proto;
4486 case BPF_FUNC_skb_under_cgroup:
4487 return &bpf_skb_under_cgroup_proto;
4489 return bpf_base_func_proto(func_id);
4493 static const struct bpf_func_proto *
4494 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4497 case BPF_FUNC_setsockopt:
4498 return &bpf_setsockopt_proto;
4499 case BPF_FUNC_getsockopt:
4500 return &bpf_getsockopt_proto;
4501 case BPF_FUNC_sock_ops_cb_flags_set:
4502 return &bpf_sock_ops_cb_flags_set_proto;
4503 case BPF_FUNC_sock_map_update:
4504 return &bpf_sock_map_update_proto;
4506 return bpf_base_func_proto(func_id);
4510 static const struct bpf_func_proto *
4511 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4514 case BPF_FUNC_msg_redirect_map:
4515 return &bpf_msg_redirect_map_proto;
4516 case BPF_FUNC_msg_apply_bytes:
4517 return &bpf_msg_apply_bytes_proto;
4518 case BPF_FUNC_msg_cork_bytes:
4519 return &bpf_msg_cork_bytes_proto;
4520 case BPF_FUNC_msg_pull_data:
4521 return &bpf_msg_pull_data_proto;
4523 return bpf_base_func_proto(func_id);
4527 static const struct bpf_func_proto *
4528 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4531 case BPF_FUNC_skb_store_bytes:
4532 return &bpf_skb_store_bytes_proto;
4533 case BPF_FUNC_skb_load_bytes:
4534 return &bpf_skb_load_bytes_proto;
4535 case BPF_FUNC_skb_pull_data:
4536 return &bpf_skb_pull_data_proto;
4537 case BPF_FUNC_skb_change_tail:
4538 return &bpf_skb_change_tail_proto;
4539 case BPF_FUNC_skb_change_head:
4540 return &bpf_skb_change_head_proto;
4541 case BPF_FUNC_get_socket_cookie:
4542 return &bpf_get_socket_cookie_proto;
4543 case BPF_FUNC_get_socket_uid:
4544 return &bpf_get_socket_uid_proto;
4545 case BPF_FUNC_sk_redirect_map:
4546 return &bpf_sk_redirect_map_proto;
4548 return bpf_base_func_proto(func_id);
4552 static const struct bpf_func_proto *
4553 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4556 case BPF_FUNC_skb_get_tunnel_key:
4557 return &bpf_skb_get_tunnel_key_proto;
4558 case BPF_FUNC_skb_set_tunnel_key:
4559 return bpf_get_skb_set_tunnel_proto(func_id);
4560 case BPF_FUNC_skb_get_tunnel_opt:
4561 return &bpf_skb_get_tunnel_opt_proto;
4562 case BPF_FUNC_skb_set_tunnel_opt:
4563 return bpf_get_skb_set_tunnel_proto(func_id);
4564 case BPF_FUNC_redirect:
4565 return &bpf_redirect_proto;
4566 case BPF_FUNC_clone_redirect:
4567 return &bpf_clone_redirect_proto;
4568 case BPF_FUNC_skb_change_tail:
4569 return &bpf_skb_change_tail_proto;
4570 case BPF_FUNC_skb_change_head:
4571 return &bpf_skb_change_head_proto;
4572 case BPF_FUNC_skb_store_bytes:
4573 return &bpf_skb_store_bytes_proto;
4574 case BPF_FUNC_csum_update:
4575 return &bpf_csum_update_proto;
4576 case BPF_FUNC_l3_csum_replace:
4577 return &bpf_l3_csum_replace_proto;
4578 case BPF_FUNC_l4_csum_replace:
4579 return &bpf_l4_csum_replace_proto;
4580 case BPF_FUNC_set_hash_invalid:
4581 return &bpf_set_hash_invalid_proto;
4583 return lwt_inout_func_proto(func_id, prog);
4587 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
4588 const struct bpf_prog *prog,
4589 struct bpf_insn_access_aux *info)
4591 const int size_default = sizeof(__u32);
4593 if (off < 0 || off >= sizeof(struct __sk_buff))
4596 /* The verifier guarantees that size > 0. */
4597 if (off % size != 0)
4601 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
4602 if (off + size > offsetofend(struct __sk_buff, cb[4]))
4605 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
4606 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
4607 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
4608 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
4609 case bpf_ctx_range(struct __sk_buff, data):
4610 case bpf_ctx_range(struct __sk_buff, data_meta):
4611 case bpf_ctx_range(struct __sk_buff, data_end):
4612 if (size != size_default)
4616 /* Only narrow read access allowed for now. */
4617 if (type == BPF_WRITE) {
4618 if (size != size_default)
4621 bpf_ctx_record_field_size(info, size_default);
4622 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
4630 static bool sk_filter_is_valid_access(int off, int size,
4631 enum bpf_access_type type,
4632 const struct bpf_prog *prog,
4633 struct bpf_insn_access_aux *info)
4636 case bpf_ctx_range(struct __sk_buff, tc_classid):
4637 case bpf_ctx_range(struct __sk_buff, data):
4638 case bpf_ctx_range(struct __sk_buff, data_meta):
4639 case bpf_ctx_range(struct __sk_buff, data_end):
4640 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
4644 if (type == BPF_WRITE) {
4646 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
4653 return bpf_skb_is_valid_access(off, size, type, prog, info);
4656 static bool lwt_is_valid_access(int off, int size,
4657 enum bpf_access_type type,
4658 const struct bpf_prog *prog,
4659 struct bpf_insn_access_aux *info)
4662 case bpf_ctx_range(struct __sk_buff, tc_classid):
4663 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
4664 case bpf_ctx_range(struct __sk_buff, data_meta):
4668 if (type == BPF_WRITE) {
4670 case bpf_ctx_range(struct __sk_buff, mark):
4671 case bpf_ctx_range(struct __sk_buff, priority):
4672 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
4680 case bpf_ctx_range(struct __sk_buff, data):
4681 info->reg_type = PTR_TO_PACKET;
4683 case bpf_ctx_range(struct __sk_buff, data_end):
4684 info->reg_type = PTR_TO_PACKET_END;
4688 return bpf_skb_is_valid_access(off, size, type, prog, info);
4692 /* Attach type specific accesses */
4693 static bool __sock_filter_check_attach_type(int off,
4694 enum bpf_access_type access_type,
4695 enum bpf_attach_type attach_type)
4698 case offsetof(struct bpf_sock, bound_dev_if):
4699 case offsetof(struct bpf_sock, mark):
4700 case offsetof(struct bpf_sock, priority):
4701 switch (attach_type) {
4702 case BPF_CGROUP_INET_SOCK_CREATE:
4707 case bpf_ctx_range(struct bpf_sock, src_ip4):
4708 switch (attach_type) {
4709 case BPF_CGROUP_INET4_POST_BIND:
4714 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
4715 switch (attach_type) {
4716 case BPF_CGROUP_INET6_POST_BIND:
4721 case bpf_ctx_range(struct bpf_sock, src_port):
4722 switch (attach_type) {
4723 case BPF_CGROUP_INET4_POST_BIND:
4724 case BPF_CGROUP_INET6_POST_BIND:
4731 return access_type == BPF_READ;
4736 static bool __sock_filter_check_size(int off, int size,
4737 struct bpf_insn_access_aux *info)
4739 const int size_default = sizeof(__u32);
4742 case bpf_ctx_range(struct bpf_sock, src_ip4):
4743 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
4744 bpf_ctx_record_field_size(info, size_default);
4745 return bpf_ctx_narrow_access_ok(off, size, size_default);
4748 return size == size_default;
4751 static bool sock_filter_is_valid_access(int off, int size,
4752 enum bpf_access_type type,
4753 const struct bpf_prog *prog,
4754 struct bpf_insn_access_aux *info)
4756 if (off < 0 || off >= sizeof(struct bpf_sock))
4758 if (off % size != 0)
4760 if (!__sock_filter_check_attach_type(off, type,
4761 prog->expected_attach_type))
4763 if (!__sock_filter_check_size(off, size, info))
4768 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
4769 const struct bpf_prog *prog, int drop_verdict)
4771 struct bpf_insn *insn = insn_buf;
4776 /* if (!skb->cloned)
4779 * (Fast-path, otherwise approximation that we might be
4780 * a clone, do the rest in helper.)
4782 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
4783 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
4784 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
4786 /* ret = bpf_skb_pull_data(skb, 0); */
4787 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
4788 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
4789 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
4790 BPF_FUNC_skb_pull_data);
4793 * return TC_ACT_SHOT;
4795 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
4796 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
4797 *insn++ = BPF_EXIT_INSN();
4800 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
4802 *insn++ = prog->insnsi[0];
4804 return insn - insn_buf;
4807 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
4808 struct bpf_insn *insn_buf)
4810 bool indirect = BPF_MODE(orig->code) == BPF_IND;
4811 struct bpf_insn *insn = insn_buf;
4813 /* We're guaranteed here that CTX is in R6. */
4814 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
4816 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
4818 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
4820 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
4823 switch (BPF_SIZE(orig->code)) {
4825 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
4828 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
4831 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
4835 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
4836 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
4837 *insn++ = BPF_EXIT_INSN();
4839 return insn - insn_buf;
4842 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
4843 const struct bpf_prog *prog)
4845 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
4848 static bool tc_cls_act_is_valid_access(int off, int size,
4849 enum bpf_access_type type,
4850 const struct bpf_prog *prog,
4851 struct bpf_insn_access_aux *info)
4853 if (type == BPF_WRITE) {
4855 case bpf_ctx_range(struct __sk_buff, mark):
4856 case bpf_ctx_range(struct __sk_buff, tc_index):
4857 case bpf_ctx_range(struct __sk_buff, priority):
4858 case bpf_ctx_range(struct __sk_buff, tc_classid):
4859 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
4867 case bpf_ctx_range(struct __sk_buff, data):
4868 info->reg_type = PTR_TO_PACKET;
4870 case bpf_ctx_range(struct __sk_buff, data_meta):
4871 info->reg_type = PTR_TO_PACKET_META;
4873 case bpf_ctx_range(struct __sk_buff, data_end):
4874 info->reg_type = PTR_TO_PACKET_END;
4876 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
4880 return bpf_skb_is_valid_access(off, size, type, prog, info);
4883 static bool __is_valid_xdp_access(int off, int size)
4885 if (off < 0 || off >= sizeof(struct xdp_md))
4887 if (off % size != 0)
4889 if (size != sizeof(__u32))
4895 static bool xdp_is_valid_access(int off, int size,
4896 enum bpf_access_type type,
4897 const struct bpf_prog *prog,
4898 struct bpf_insn_access_aux *info)
4900 if (type == BPF_WRITE) {
4901 if (bpf_prog_is_dev_bound(prog->aux)) {
4903 case offsetof(struct xdp_md, rx_queue_index):
4904 return __is_valid_xdp_access(off, size);
4911 case offsetof(struct xdp_md, data):
4912 info->reg_type = PTR_TO_PACKET;
4914 case offsetof(struct xdp_md, data_meta):
4915 info->reg_type = PTR_TO_PACKET_META;
4917 case offsetof(struct xdp_md, data_end):
4918 info->reg_type = PTR_TO_PACKET_END;
4922 return __is_valid_xdp_access(off, size);
4925 void bpf_warn_invalid_xdp_action(u32 act)
4927 const u32 act_max = XDP_REDIRECT;
4929 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
4930 act > act_max ? "Illegal" : "Driver unsupported",
4933 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
4935 static bool sock_addr_is_valid_access(int off, int size,
4936 enum bpf_access_type type,
4937 const struct bpf_prog *prog,
4938 struct bpf_insn_access_aux *info)
4940 const int size_default = sizeof(__u32);
4942 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
4944 if (off % size != 0)
4947 /* Disallow access to IPv6 fields from IPv4 contex and vise
4951 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
4952 switch (prog->expected_attach_type) {
4953 case BPF_CGROUP_INET4_BIND:
4954 case BPF_CGROUP_INET4_CONNECT:
4960 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
4961 switch (prog->expected_attach_type) {
4962 case BPF_CGROUP_INET6_BIND:
4963 case BPF_CGROUP_INET6_CONNECT:
4972 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
4973 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
4974 /* Only narrow read access allowed for now. */
4975 if (type == BPF_READ) {
4976 bpf_ctx_record_field_size(info, size_default);
4977 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
4980 if (size != size_default)
4984 case bpf_ctx_range(struct bpf_sock_addr, user_port):
4985 if (size != size_default)
4989 if (type == BPF_READ) {
4990 if (size != size_default)
5000 static bool sock_ops_is_valid_access(int off, int size,
5001 enum bpf_access_type type,
5002 const struct bpf_prog *prog,
5003 struct bpf_insn_access_aux *info)
5005 const int size_default = sizeof(__u32);
5007 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5010 /* The verifier guarantees that size > 0. */
5011 if (off % size != 0)
5014 if (type == BPF_WRITE) {
5016 case offsetof(struct bpf_sock_ops, reply):
5017 case offsetof(struct bpf_sock_ops, sk_txhash):
5018 if (size != size_default)
5026 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5028 if (size != sizeof(__u64))
5032 if (size != size_default)
5041 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5042 const struct bpf_prog *prog)
5044 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
5047 static bool sk_skb_is_valid_access(int off, int size,
5048 enum bpf_access_type type,
5049 const struct bpf_prog *prog,
5050 struct bpf_insn_access_aux *info)
5053 case bpf_ctx_range(struct __sk_buff, tc_classid):
5054 case bpf_ctx_range(struct __sk_buff, data_meta):
5058 if (type == BPF_WRITE) {
5060 case bpf_ctx_range(struct __sk_buff, tc_index):
5061 case bpf_ctx_range(struct __sk_buff, priority):
5069 case bpf_ctx_range(struct __sk_buff, mark):
5071 case bpf_ctx_range(struct __sk_buff, data):
5072 info->reg_type = PTR_TO_PACKET;
5074 case bpf_ctx_range(struct __sk_buff, data_end):
5075 info->reg_type = PTR_TO_PACKET_END;
5079 return bpf_skb_is_valid_access(off, size, type, prog, info);
5082 static bool sk_msg_is_valid_access(int off, int size,
5083 enum bpf_access_type type,
5084 const struct bpf_prog *prog,
5085 struct bpf_insn_access_aux *info)
5087 if (type == BPF_WRITE)
5091 case offsetof(struct sk_msg_md, data):
5092 info->reg_type = PTR_TO_PACKET;
5094 case offsetof(struct sk_msg_md, data_end):
5095 info->reg_type = PTR_TO_PACKET_END;
5099 if (off < 0 || off >= sizeof(struct sk_msg_md))
5101 if (off % size != 0)
5103 if (size != sizeof(__u64))
5109 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
5110 const struct bpf_insn *si,
5111 struct bpf_insn *insn_buf,
5112 struct bpf_prog *prog, u32 *target_size)
5114 struct bpf_insn *insn = insn_buf;
5118 case offsetof(struct __sk_buff, len):
5119 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5120 bpf_target_off(struct sk_buff, len, 4,
5124 case offsetof(struct __sk_buff, protocol):
5125 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5126 bpf_target_off(struct sk_buff, protocol, 2,
5130 case offsetof(struct __sk_buff, vlan_proto):
5131 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5132 bpf_target_off(struct sk_buff, vlan_proto, 2,
5136 case offsetof(struct __sk_buff, priority):
5137 if (type == BPF_WRITE)
5138 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5139 bpf_target_off(struct sk_buff, priority, 4,
5142 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5143 bpf_target_off(struct sk_buff, priority, 4,
5147 case offsetof(struct __sk_buff, ingress_ifindex):
5148 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5149 bpf_target_off(struct sk_buff, skb_iif, 4,
5153 case offsetof(struct __sk_buff, ifindex):
5154 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
5155 si->dst_reg, si->src_reg,
5156 offsetof(struct sk_buff, dev));
5157 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
5158 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5159 bpf_target_off(struct net_device, ifindex, 4,
5163 case offsetof(struct __sk_buff, hash):
5164 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5165 bpf_target_off(struct sk_buff, hash, 4,
5169 case offsetof(struct __sk_buff, mark):
5170 if (type == BPF_WRITE)
5171 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5172 bpf_target_off(struct sk_buff, mark, 4,
5175 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5176 bpf_target_off(struct sk_buff, mark, 4,
5180 case offsetof(struct __sk_buff, pkt_type):
5182 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
5184 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
5185 #ifdef __BIG_ENDIAN_BITFIELD
5186 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
5190 case offsetof(struct __sk_buff, queue_mapping):
5191 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5192 bpf_target_off(struct sk_buff, queue_mapping, 2,
5196 case offsetof(struct __sk_buff, vlan_present):
5197 case offsetof(struct __sk_buff, vlan_tci):
5198 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
5200 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5201 bpf_target_off(struct sk_buff, vlan_tci, 2,
5203 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
5204 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
5207 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
5208 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
5212 case offsetof(struct __sk_buff, cb[0]) ...
5213 offsetofend(struct __sk_buff, cb[4]) - 1:
5214 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
5215 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
5216 offsetof(struct qdisc_skb_cb, data)) %
5219 prog->cb_access = 1;
5221 off -= offsetof(struct __sk_buff, cb[0]);
5222 off += offsetof(struct sk_buff, cb);
5223 off += offsetof(struct qdisc_skb_cb, data);
5224 if (type == BPF_WRITE)
5225 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
5228 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
5232 case offsetof(struct __sk_buff, tc_classid):
5233 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
5236 off -= offsetof(struct __sk_buff, tc_classid);
5237 off += offsetof(struct sk_buff, cb);
5238 off += offsetof(struct qdisc_skb_cb, tc_classid);
5240 if (type == BPF_WRITE)
5241 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
5244 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
5248 case offsetof(struct __sk_buff, data):
5249 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
5250 si->dst_reg, si->src_reg,
5251 offsetof(struct sk_buff, data));
5254 case offsetof(struct __sk_buff, data_meta):
5256 off -= offsetof(struct __sk_buff, data_meta);
5257 off += offsetof(struct sk_buff, cb);
5258 off += offsetof(struct bpf_skb_data_end, data_meta);
5259 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5263 case offsetof(struct __sk_buff, data_end):
5265 off -= offsetof(struct __sk_buff, data_end);
5266 off += offsetof(struct sk_buff, cb);
5267 off += offsetof(struct bpf_skb_data_end, data_end);
5268 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5272 case offsetof(struct __sk_buff, tc_index):
5273 #ifdef CONFIG_NET_SCHED
5274 if (type == BPF_WRITE)
5275 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
5276 bpf_target_off(struct sk_buff, tc_index, 2,
5279 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5280 bpf_target_off(struct sk_buff, tc_index, 2,
5284 if (type == BPF_WRITE)
5285 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
5287 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5291 case offsetof(struct __sk_buff, napi_id):
5292 #if defined(CONFIG_NET_RX_BUSY_POLL)
5293 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5294 bpf_target_off(struct sk_buff, napi_id, 4,
5296 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
5297 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5300 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5303 case offsetof(struct __sk_buff, family):
5304 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
5306 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5307 si->dst_reg, si->src_reg,
5308 offsetof(struct sk_buff, sk));
5309 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5310 bpf_target_off(struct sock_common,
5314 case offsetof(struct __sk_buff, remote_ip4):
5315 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
5317 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5318 si->dst_reg, si->src_reg,
5319 offsetof(struct sk_buff, sk));
5320 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5321 bpf_target_off(struct sock_common,
5325 case offsetof(struct __sk_buff, local_ip4):
5326 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5327 skc_rcv_saddr) != 4);
5329 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5330 si->dst_reg, si->src_reg,
5331 offsetof(struct sk_buff, sk));
5332 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5333 bpf_target_off(struct sock_common,
5337 case offsetof(struct __sk_buff, remote_ip6[0]) ...
5338 offsetof(struct __sk_buff, remote_ip6[3]):
5339 #if IS_ENABLED(CONFIG_IPV6)
5340 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5341 skc_v6_daddr.s6_addr32[0]) != 4);
5344 off -= offsetof(struct __sk_buff, remote_ip6[0]);
5346 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5347 si->dst_reg, si->src_reg,
5348 offsetof(struct sk_buff, sk));
5349 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5350 offsetof(struct sock_common,
5351 skc_v6_daddr.s6_addr32[0]) +
5354 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5357 case offsetof(struct __sk_buff, local_ip6[0]) ...
5358 offsetof(struct __sk_buff, local_ip6[3]):
5359 #if IS_ENABLED(CONFIG_IPV6)
5360 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5361 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
5364 off -= offsetof(struct __sk_buff, local_ip6[0]);
5366 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5367 si->dst_reg, si->src_reg,
5368 offsetof(struct sk_buff, sk));
5369 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5370 offsetof(struct sock_common,
5371 skc_v6_rcv_saddr.s6_addr32[0]) +
5374 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5378 case offsetof(struct __sk_buff, remote_port):
5379 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
5381 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5382 si->dst_reg, si->src_reg,
5383 offsetof(struct sk_buff, sk));
5384 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5385 bpf_target_off(struct sock_common,
5388 #ifndef __BIG_ENDIAN_BITFIELD
5389 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
5393 case offsetof(struct __sk_buff, local_port):
5394 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
5396 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5397 si->dst_reg, si->src_reg,
5398 offsetof(struct sk_buff, sk));
5399 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5400 bpf_target_off(struct sock_common,
5401 skc_num, 2, target_size));
5405 return insn - insn_buf;
5408 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
5409 const struct bpf_insn *si,
5410 struct bpf_insn *insn_buf,
5411 struct bpf_prog *prog, u32 *target_size)
5413 struct bpf_insn *insn = insn_buf;
5417 case offsetof(struct bpf_sock, bound_dev_if):
5418 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
5420 if (type == BPF_WRITE)
5421 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5422 offsetof(struct sock, sk_bound_dev_if));
5424 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5425 offsetof(struct sock, sk_bound_dev_if));
5428 case offsetof(struct bpf_sock, mark):
5429 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
5431 if (type == BPF_WRITE)
5432 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5433 offsetof(struct sock, sk_mark));
5435 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5436 offsetof(struct sock, sk_mark));
5439 case offsetof(struct bpf_sock, priority):
5440 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
5442 if (type == BPF_WRITE)
5443 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5444 offsetof(struct sock, sk_priority));
5446 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5447 offsetof(struct sock, sk_priority));
5450 case offsetof(struct bpf_sock, family):
5451 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
5453 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5454 offsetof(struct sock, sk_family));
5457 case offsetof(struct bpf_sock, type):
5458 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5459 offsetof(struct sock, __sk_flags_offset));
5460 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
5461 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
5464 case offsetof(struct bpf_sock, protocol):
5465 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5466 offsetof(struct sock, __sk_flags_offset));
5467 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
5468 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
5471 case offsetof(struct bpf_sock, src_ip4):
5472 *insn++ = BPF_LDX_MEM(
5473 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
5474 bpf_target_off(struct sock_common, skc_rcv_saddr,
5475 FIELD_SIZEOF(struct sock_common,
5480 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5481 #if IS_ENABLED(CONFIG_IPV6)
5483 off -= offsetof(struct bpf_sock, src_ip6[0]);
5484 *insn++ = BPF_LDX_MEM(
5485 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
5488 skc_v6_rcv_saddr.s6_addr32[0],
5489 FIELD_SIZEOF(struct sock_common,
5490 skc_v6_rcv_saddr.s6_addr32[0]),
5491 target_size) + off);
5494 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5498 case offsetof(struct bpf_sock, src_port):
5499 *insn++ = BPF_LDX_MEM(
5500 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
5501 si->dst_reg, si->src_reg,
5502 bpf_target_off(struct sock_common, skc_num,
5503 FIELD_SIZEOF(struct sock_common,
5509 return insn - insn_buf;
5512 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
5513 const struct bpf_insn *si,
5514 struct bpf_insn *insn_buf,
5515 struct bpf_prog *prog, u32 *target_size)
5517 struct bpf_insn *insn = insn_buf;
5520 case offsetof(struct __sk_buff, ifindex):
5521 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
5522 si->dst_reg, si->src_reg,
5523 offsetof(struct sk_buff, dev));
5524 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5525 bpf_target_off(struct net_device, ifindex, 4,
5529 return bpf_convert_ctx_access(type, si, insn_buf, prog,
5533 return insn - insn_buf;
5536 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
5537 const struct bpf_insn *si,
5538 struct bpf_insn *insn_buf,
5539 struct bpf_prog *prog, u32 *target_size)
5541 struct bpf_insn *insn = insn_buf;
5544 case offsetof(struct xdp_md, data):
5545 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
5546 si->dst_reg, si->src_reg,
5547 offsetof(struct xdp_buff, data));
5549 case offsetof(struct xdp_md, data_meta):
5550 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
5551 si->dst_reg, si->src_reg,
5552 offsetof(struct xdp_buff, data_meta));
5554 case offsetof(struct xdp_md, data_end):
5555 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
5556 si->dst_reg, si->src_reg,
5557 offsetof(struct xdp_buff, data_end));
5559 case offsetof(struct xdp_md, ingress_ifindex):
5560 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
5561 si->dst_reg, si->src_reg,
5562 offsetof(struct xdp_buff, rxq));
5563 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
5564 si->dst_reg, si->dst_reg,
5565 offsetof(struct xdp_rxq_info, dev));
5566 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5567 offsetof(struct net_device, ifindex));
5569 case offsetof(struct xdp_md, rx_queue_index):
5570 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
5571 si->dst_reg, si->src_reg,
5572 offsetof(struct xdp_buff, rxq));
5573 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5574 offsetof(struct xdp_rxq_info,
5579 return insn - insn_buf;
5582 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
5583 * context Structure, F is Field in context structure that contains a pointer
5584 * to Nested Structure of type NS that has the field NF.
5586 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
5587 * sure that SIZE is not greater than actual size of S.F.NF.
5589 * If offset OFF is provided, the load happens from that offset relative to
5592 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
5594 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
5595 si->src_reg, offsetof(S, F)); \
5596 *insn++ = BPF_LDX_MEM( \
5597 SIZE, si->dst_reg, si->dst_reg, \
5598 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
5603 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
5604 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
5605 BPF_FIELD_SIZEOF(NS, NF), 0)
5607 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
5608 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
5610 * It doesn't support SIZE argument though since narrow stores are not
5611 * supported for now.
5613 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
5614 * "register" since two registers available in convert_ctx_access are not
5615 * enough: we can't override neither SRC, since it contains value to store, nor
5616 * DST since it contains pointer to context that may be used by later
5617 * instructions. But we need a temporary place to save pointer to nested
5618 * structure whose field we want to store to.
5620 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
5622 int tmp_reg = BPF_REG_9; \
5623 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
5625 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
5627 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
5629 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
5630 si->dst_reg, offsetof(S, F)); \
5631 *insn++ = BPF_STX_MEM( \
5632 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
5633 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
5636 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
5640 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
5643 if (type == BPF_WRITE) { \
5644 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
5647 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
5648 S, NS, F, NF, SIZE, OFF); \
5652 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
5653 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
5654 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
5656 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
5657 const struct bpf_insn *si,
5658 struct bpf_insn *insn_buf,
5659 struct bpf_prog *prog, u32 *target_size)
5661 struct bpf_insn *insn = insn_buf;
5665 case offsetof(struct bpf_sock_addr, user_family):
5666 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
5667 struct sockaddr, uaddr, sa_family);
5670 case offsetof(struct bpf_sock_addr, user_ip4):
5671 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
5672 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
5673 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
5676 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5678 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
5679 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
5680 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
5681 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
5685 case offsetof(struct bpf_sock_addr, user_port):
5686 /* To get port we need to know sa_family first and then treat
5687 * sockaddr as either sockaddr_in or sockaddr_in6.
5688 * Though we can simplify since port field has same offset and
5689 * size in both structures.
5690 * Here we check this invariant and use just one of the
5691 * structures if it's true.
5693 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
5694 offsetof(struct sockaddr_in6, sin6_port));
5695 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
5696 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
5697 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
5698 struct sockaddr_in6, uaddr,
5699 sin6_port, tmp_reg);
5702 case offsetof(struct bpf_sock_addr, family):
5703 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
5704 struct sock, sk, sk_family);
5707 case offsetof(struct bpf_sock_addr, type):
5708 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
5709 struct bpf_sock_addr_kern, struct sock, sk,
5710 __sk_flags_offset, BPF_W, 0);
5711 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
5712 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
5715 case offsetof(struct bpf_sock_addr, protocol):
5716 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
5717 struct bpf_sock_addr_kern, struct sock, sk,
5718 __sk_flags_offset, BPF_W, 0);
5719 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
5720 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
5725 return insn - insn_buf;
5728 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
5729 const struct bpf_insn *si,
5730 struct bpf_insn *insn_buf,
5731 struct bpf_prog *prog,
5734 struct bpf_insn *insn = insn_buf;
5738 case offsetof(struct bpf_sock_ops, op) ...
5739 offsetof(struct bpf_sock_ops, replylong[3]):
5740 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
5741 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
5742 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
5743 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
5744 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
5745 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
5747 off -= offsetof(struct bpf_sock_ops, op);
5748 off += offsetof(struct bpf_sock_ops_kern, op);
5749 if (type == BPF_WRITE)
5750 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5753 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5757 case offsetof(struct bpf_sock_ops, family):
5758 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
5760 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5761 struct bpf_sock_ops_kern, sk),
5762 si->dst_reg, si->src_reg,
5763 offsetof(struct bpf_sock_ops_kern, sk));
5764 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5765 offsetof(struct sock_common, skc_family));
5768 case offsetof(struct bpf_sock_ops, remote_ip4):
5769 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
5771 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5772 struct bpf_sock_ops_kern, sk),
5773 si->dst_reg, si->src_reg,
5774 offsetof(struct bpf_sock_ops_kern, sk));
5775 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5776 offsetof(struct sock_common, skc_daddr));
5779 case offsetof(struct bpf_sock_ops, local_ip4):
5780 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
5782 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5783 struct bpf_sock_ops_kern, sk),
5784 si->dst_reg, si->src_reg,
5785 offsetof(struct bpf_sock_ops_kern, sk));
5786 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5787 offsetof(struct sock_common,
5791 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
5792 offsetof(struct bpf_sock_ops, remote_ip6[3]):
5793 #if IS_ENABLED(CONFIG_IPV6)
5794 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5795 skc_v6_daddr.s6_addr32[0]) != 4);
5798 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
5799 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5800 struct bpf_sock_ops_kern, sk),
5801 si->dst_reg, si->src_reg,
5802 offsetof(struct bpf_sock_ops_kern, sk));
5803 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5804 offsetof(struct sock_common,
5805 skc_v6_daddr.s6_addr32[0]) +
5808 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5812 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
5813 offsetof(struct bpf_sock_ops, local_ip6[3]):
5814 #if IS_ENABLED(CONFIG_IPV6)
5815 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5816 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
5819 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
5820 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5821 struct bpf_sock_ops_kern, sk),
5822 si->dst_reg, si->src_reg,
5823 offsetof(struct bpf_sock_ops_kern, sk));
5824 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5825 offsetof(struct sock_common,
5826 skc_v6_rcv_saddr.s6_addr32[0]) +
5829 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5833 case offsetof(struct bpf_sock_ops, remote_port):
5834 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
5836 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5837 struct bpf_sock_ops_kern, sk),
5838 si->dst_reg, si->src_reg,
5839 offsetof(struct bpf_sock_ops_kern, sk));
5840 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5841 offsetof(struct sock_common, skc_dport));
5842 #ifndef __BIG_ENDIAN_BITFIELD
5843 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
5847 case offsetof(struct bpf_sock_ops, local_port):
5848 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
5850 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5851 struct bpf_sock_ops_kern, sk),
5852 si->dst_reg, si->src_reg,
5853 offsetof(struct bpf_sock_ops_kern, sk));
5854 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5855 offsetof(struct sock_common, skc_num));
5858 case offsetof(struct bpf_sock_ops, is_fullsock):
5859 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5860 struct bpf_sock_ops_kern,
5862 si->dst_reg, si->src_reg,
5863 offsetof(struct bpf_sock_ops_kern,
5867 case offsetof(struct bpf_sock_ops, state):
5868 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
5870 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5871 struct bpf_sock_ops_kern, sk),
5872 si->dst_reg, si->src_reg,
5873 offsetof(struct bpf_sock_ops_kern, sk));
5874 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
5875 offsetof(struct sock_common, skc_state));
5878 case offsetof(struct bpf_sock_ops, rtt_min):
5879 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
5880 sizeof(struct minmax));
5881 BUILD_BUG_ON(sizeof(struct minmax) <
5882 sizeof(struct minmax_sample));
5884 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
5885 struct bpf_sock_ops_kern, sk),
5886 si->dst_reg, si->src_reg,
5887 offsetof(struct bpf_sock_ops_kern, sk));
5888 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5889 offsetof(struct tcp_sock, rtt_min) +
5890 FIELD_SIZEOF(struct minmax_sample, t));
5893 /* Helper macro for adding read access to tcp_sock or sock fields. */
5894 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
5896 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
5897 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
5898 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
5899 struct bpf_sock_ops_kern, \
5901 si->dst_reg, si->src_reg, \
5902 offsetof(struct bpf_sock_ops_kern, \
5904 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
5905 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
5906 struct bpf_sock_ops_kern, sk),\
5907 si->dst_reg, si->src_reg, \
5908 offsetof(struct bpf_sock_ops_kern, sk));\
5909 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
5911 si->dst_reg, si->dst_reg, \
5912 offsetof(OBJ, OBJ_FIELD)); \
5915 /* Helper macro for adding write access to tcp_sock or sock fields.
5916 * The macro is called with two registers, dst_reg which contains a pointer
5917 * to ctx (context) and src_reg which contains the value that should be
5918 * stored. However, we need an additional register since we cannot overwrite
5919 * dst_reg because it may be used later in the program.
5920 * Instead we "borrow" one of the other register. We first save its value
5921 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
5922 * it at the end of the macro.
5924 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
5926 int reg = BPF_REG_9; \
5927 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
5928 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
5929 if (si->dst_reg == reg || si->src_reg == reg) \
5931 if (si->dst_reg == reg || si->src_reg == reg) \
5933 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
5934 offsetof(struct bpf_sock_ops_kern, \
5936 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
5937 struct bpf_sock_ops_kern, \
5940 offsetof(struct bpf_sock_ops_kern, \
5942 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
5943 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
5944 struct bpf_sock_ops_kern, sk),\
5946 offsetof(struct bpf_sock_ops_kern, sk));\
5947 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
5949 offsetof(OBJ, OBJ_FIELD)); \
5950 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
5951 offsetof(struct bpf_sock_ops_kern, \
5955 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
5957 if (TYPE == BPF_WRITE) \
5958 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
5960 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
5963 case offsetof(struct bpf_sock_ops, snd_cwnd):
5964 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
5967 case offsetof(struct bpf_sock_ops, srtt_us):
5968 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
5971 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
5972 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
5976 case offsetof(struct bpf_sock_ops, snd_ssthresh):
5977 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
5980 case offsetof(struct bpf_sock_ops, rcv_nxt):
5981 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
5984 case offsetof(struct bpf_sock_ops, snd_nxt):
5985 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
5988 case offsetof(struct bpf_sock_ops, snd_una):
5989 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
5992 case offsetof(struct bpf_sock_ops, mss_cache):
5993 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
5996 case offsetof(struct bpf_sock_ops, ecn_flags):
5997 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6000 case offsetof(struct bpf_sock_ops, rate_delivered):
6001 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6005 case offsetof(struct bpf_sock_ops, rate_interval_us):
6006 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6010 case offsetof(struct bpf_sock_ops, packets_out):
6011 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6014 case offsetof(struct bpf_sock_ops, retrans_out):
6015 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6018 case offsetof(struct bpf_sock_ops, total_retrans):
6019 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6023 case offsetof(struct bpf_sock_ops, segs_in):
6024 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
6027 case offsetof(struct bpf_sock_ops, data_segs_in):
6028 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
6031 case offsetof(struct bpf_sock_ops, segs_out):
6032 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
6035 case offsetof(struct bpf_sock_ops, data_segs_out):
6036 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
6040 case offsetof(struct bpf_sock_ops, lost_out):
6041 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
6044 case offsetof(struct bpf_sock_ops, sacked_out):
6045 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
6048 case offsetof(struct bpf_sock_ops, sk_txhash):
6049 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
6053 case offsetof(struct bpf_sock_ops, bytes_received):
6054 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
6058 case offsetof(struct bpf_sock_ops, bytes_acked):
6059 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
6063 return insn - insn_buf;
6066 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
6067 const struct bpf_insn *si,
6068 struct bpf_insn *insn_buf,
6069 struct bpf_prog *prog, u32 *target_size)
6071 struct bpf_insn *insn = insn_buf;
6075 case offsetof(struct __sk_buff, data_end):
6077 off -= offsetof(struct __sk_buff, data_end);
6078 off += offsetof(struct sk_buff, cb);
6079 off += offsetof(struct tcp_skb_cb, bpf.data_end);
6080 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6084 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6088 return insn - insn_buf;
6091 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
6092 const struct bpf_insn *si,
6093 struct bpf_insn *insn_buf,
6094 struct bpf_prog *prog, u32 *target_size)
6096 struct bpf_insn *insn = insn_buf;
6099 case offsetof(struct sk_msg_md, data):
6100 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data),
6101 si->dst_reg, si->src_reg,
6102 offsetof(struct sk_msg_buff, data));
6104 case offsetof(struct sk_msg_md, data_end):
6105 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data_end),
6106 si->dst_reg, si->src_reg,
6107 offsetof(struct sk_msg_buff, data_end));
6111 return insn - insn_buf;
6114 const struct bpf_verifier_ops sk_filter_verifier_ops = {
6115 .get_func_proto = sk_filter_func_proto,
6116 .is_valid_access = sk_filter_is_valid_access,
6117 .convert_ctx_access = bpf_convert_ctx_access,
6118 .gen_ld_abs = bpf_gen_ld_abs,
6121 const struct bpf_prog_ops sk_filter_prog_ops = {
6122 .test_run = bpf_prog_test_run_skb,
6125 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
6126 .get_func_proto = tc_cls_act_func_proto,
6127 .is_valid_access = tc_cls_act_is_valid_access,
6128 .convert_ctx_access = tc_cls_act_convert_ctx_access,
6129 .gen_prologue = tc_cls_act_prologue,
6130 .gen_ld_abs = bpf_gen_ld_abs,
6133 const struct bpf_prog_ops tc_cls_act_prog_ops = {
6134 .test_run = bpf_prog_test_run_skb,
6137 const struct bpf_verifier_ops xdp_verifier_ops = {
6138 .get_func_proto = xdp_func_proto,
6139 .is_valid_access = xdp_is_valid_access,
6140 .convert_ctx_access = xdp_convert_ctx_access,
6143 const struct bpf_prog_ops xdp_prog_ops = {
6144 .test_run = bpf_prog_test_run_xdp,
6147 const struct bpf_verifier_ops cg_skb_verifier_ops = {
6148 .get_func_proto = sk_filter_func_proto,
6149 .is_valid_access = sk_filter_is_valid_access,
6150 .convert_ctx_access = bpf_convert_ctx_access,
6153 const struct bpf_prog_ops cg_skb_prog_ops = {
6154 .test_run = bpf_prog_test_run_skb,
6157 const struct bpf_verifier_ops lwt_inout_verifier_ops = {
6158 .get_func_proto = lwt_inout_func_proto,
6159 .is_valid_access = lwt_is_valid_access,
6160 .convert_ctx_access = bpf_convert_ctx_access,
6163 const struct bpf_prog_ops lwt_inout_prog_ops = {
6164 .test_run = bpf_prog_test_run_skb,
6167 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
6168 .get_func_proto = lwt_xmit_func_proto,
6169 .is_valid_access = lwt_is_valid_access,
6170 .convert_ctx_access = bpf_convert_ctx_access,
6171 .gen_prologue = tc_cls_act_prologue,
6174 const struct bpf_prog_ops lwt_xmit_prog_ops = {
6175 .test_run = bpf_prog_test_run_skb,
6178 const struct bpf_verifier_ops cg_sock_verifier_ops = {
6179 .get_func_proto = sock_filter_func_proto,
6180 .is_valid_access = sock_filter_is_valid_access,
6181 .convert_ctx_access = sock_filter_convert_ctx_access,
6184 const struct bpf_prog_ops cg_sock_prog_ops = {
6187 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
6188 .get_func_proto = sock_addr_func_proto,
6189 .is_valid_access = sock_addr_is_valid_access,
6190 .convert_ctx_access = sock_addr_convert_ctx_access,
6193 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
6196 const struct bpf_verifier_ops sock_ops_verifier_ops = {
6197 .get_func_proto = sock_ops_func_proto,
6198 .is_valid_access = sock_ops_is_valid_access,
6199 .convert_ctx_access = sock_ops_convert_ctx_access,
6202 const struct bpf_prog_ops sock_ops_prog_ops = {
6205 const struct bpf_verifier_ops sk_skb_verifier_ops = {
6206 .get_func_proto = sk_skb_func_proto,
6207 .is_valid_access = sk_skb_is_valid_access,
6208 .convert_ctx_access = sk_skb_convert_ctx_access,
6209 .gen_prologue = sk_skb_prologue,
6212 const struct bpf_prog_ops sk_skb_prog_ops = {
6215 const struct bpf_verifier_ops sk_msg_verifier_ops = {
6216 .get_func_proto = sk_msg_func_proto,
6217 .is_valid_access = sk_msg_is_valid_access,
6218 .convert_ctx_access = sk_msg_convert_ctx_access,
6221 const struct bpf_prog_ops sk_msg_prog_ops = {
6224 int sk_detach_filter(struct sock *sk)
6227 struct sk_filter *filter;
6229 if (sock_flag(sk, SOCK_FILTER_LOCKED))
6232 filter = rcu_dereference_protected(sk->sk_filter,
6233 lockdep_sock_is_held(sk));
6235 RCU_INIT_POINTER(sk->sk_filter, NULL);
6236 sk_filter_uncharge(sk, filter);
6242 EXPORT_SYMBOL_GPL(sk_detach_filter);
6244 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
6247 struct sock_fprog_kern *fprog;
6248 struct sk_filter *filter;
6252 filter = rcu_dereference_protected(sk->sk_filter,
6253 lockdep_sock_is_held(sk));
6257 /* We're copying the filter that has been originally attached,
6258 * so no conversion/decode needed anymore. eBPF programs that
6259 * have no original program cannot be dumped through this.
6262 fprog = filter->prog->orig_prog;
6268 /* User space only enquires number of filter blocks. */
6272 if (len < fprog->len)
6276 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
6279 /* Instead of bytes, the API requests to return the number