2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
29 #include <linux/sock_diag.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_packet.h>
34 #include <linux/if_arp.h>
35 #include <linux/gfp.h>
36 #include <net/inet_common.h>
38 #include <net/protocol.h>
39 #include <net/netlink.h>
40 #include <linux/skbuff.h>
42 #include <net/flow_dissector.h>
43 #include <linux/errno.h>
44 #include <linux/timer.h>
45 #include <linux/uaccess.h>
46 #include <asm/unaligned.h>
47 #include <asm/cmpxchg.h>
48 #include <linux/filter.h>
49 #include <linux/ratelimit.h>
50 #include <linux/seccomp.h>
51 #include <linux/if_vlan.h>
52 #include <linux/bpf.h>
53 #include <net/sch_generic.h>
54 #include <net/cls_cgroup.h>
55 #include <net/dst_metadata.h>
57 #include <net/sock_reuseport.h>
58 #include <net/busy_poll.h>
61 #include <linux/bpf_trace.h>
62 #include <net/xdp_sock.h>
63 #include <linux/inetdevice.h>
64 #include <net/ip_fib.h>
68 #include <linux/seg6_local.h>
70 #include <net/seg6_local.h>
73 * sk_filter_trim_cap - run a packet through a socket filter
74 * @sk: sock associated with &sk_buff
75 * @skb: buffer to filter
76 * @cap: limit on how short the eBPF program may trim the packet
78 * Run the eBPF program and then cut skb->data to correct size returned by
79 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
80 * than pkt_len we keep whole skb->data. This is the socket level
81 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
82 * be accepted or -EPERM if the packet should be tossed.
85 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
88 struct sk_filter *filter;
91 * If the skb was allocated from pfmemalloc reserves, only
92 * allow SOCK_MEMALLOC sockets to use it as this socket is
95 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
96 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
99 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
103 err = security_sock_rcv_skb(sk, skb);
108 filter = rcu_dereference(sk->sk_filter);
110 struct sock *save_sk = skb->sk;
111 unsigned int pkt_len;
114 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
116 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
122 EXPORT_SYMBOL(sk_filter_trim_cap);
124 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
126 return skb_get_poff(skb);
129 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
133 if (skb_is_nonlinear(skb))
136 if (skb->len < sizeof(struct nlattr))
139 if (a > skb->len - sizeof(struct nlattr))
142 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
144 return (void *) nla - (void *) skb->data;
149 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
153 if (skb_is_nonlinear(skb))
156 if (skb->len < sizeof(struct nlattr))
159 if (a > skb->len - sizeof(struct nlattr))
162 nla = (struct nlattr *) &skb->data[a];
163 if (nla->nla_len > skb->len - a)
166 nla = nla_find_nested(nla, x);
168 return (void *) nla - (void *) skb->data;
173 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
174 data, int, headlen, int, offset)
177 const int len = sizeof(tmp);
180 if (headlen - offset >= len)
181 return *(u8 *)(data + offset);
182 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
185 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
193 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
196 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
200 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
201 data, int, headlen, int, offset)
204 const int len = sizeof(tmp);
207 if (headlen - offset >= len)
208 return get_unaligned_be16(data + offset);
209 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
210 return be16_to_cpu(tmp);
212 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
214 return get_unaligned_be16(ptr);
220 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
223 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
227 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
228 data, int, headlen, int, offset)
231 const int len = sizeof(tmp);
233 if (likely(offset >= 0)) {
234 if (headlen - offset >= len)
235 return get_unaligned_be32(data + offset);
236 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
237 return be32_to_cpu(tmp);
239 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
241 return get_unaligned_be32(ptr);
247 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
250 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
254 BPF_CALL_0(bpf_get_raw_cpu_id)
256 return raw_smp_processor_id();
259 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
260 .func = bpf_get_raw_cpu_id,
262 .ret_type = RET_INTEGER,
265 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
266 struct bpf_insn *insn_buf)
268 struct bpf_insn *insn = insn_buf;
272 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
274 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
275 offsetof(struct sk_buff, mark));
279 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
280 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
281 #ifdef __BIG_ENDIAN_BITFIELD
282 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
287 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
289 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
290 offsetof(struct sk_buff, queue_mapping));
293 case SKF_AD_VLAN_TAG:
294 case SKF_AD_VLAN_TAG_PRESENT:
295 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
296 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
298 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
299 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
300 offsetof(struct sk_buff, vlan_tci));
301 if (skb_field == SKF_AD_VLAN_TAG) {
302 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
306 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
308 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
313 return insn - insn_buf;
316 static bool convert_bpf_extensions(struct sock_filter *fp,
317 struct bpf_insn **insnp)
319 struct bpf_insn *insn = *insnp;
323 case SKF_AD_OFF + SKF_AD_PROTOCOL:
324 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
326 /* A = *(u16 *) (CTX + offsetof(protocol)) */
327 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
328 offsetof(struct sk_buff, protocol));
329 /* A = ntohs(A) [emitting a nop or swap16] */
330 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
333 case SKF_AD_OFF + SKF_AD_PKTTYPE:
334 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
338 case SKF_AD_OFF + SKF_AD_IFINDEX:
339 case SKF_AD_OFF + SKF_AD_HATYPE:
340 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
341 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
343 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
344 BPF_REG_TMP, BPF_REG_CTX,
345 offsetof(struct sk_buff, dev));
346 /* if (tmp != 0) goto pc + 1 */
347 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
348 *insn++ = BPF_EXIT_INSN();
349 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
350 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
351 offsetof(struct net_device, ifindex));
353 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
354 offsetof(struct net_device, type));
357 case SKF_AD_OFF + SKF_AD_MARK:
358 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
362 case SKF_AD_OFF + SKF_AD_RXHASH:
363 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
365 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
366 offsetof(struct sk_buff, hash));
369 case SKF_AD_OFF + SKF_AD_QUEUE:
370 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
374 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
375 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
376 BPF_REG_A, BPF_REG_CTX, insn);
380 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
381 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
382 BPF_REG_A, BPF_REG_CTX, insn);
386 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
387 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
389 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
390 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
391 offsetof(struct sk_buff, vlan_proto));
392 /* A = ntohs(A) [emitting a nop or swap16] */
393 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
396 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
397 case SKF_AD_OFF + SKF_AD_NLATTR:
398 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
399 case SKF_AD_OFF + SKF_AD_CPU:
400 case SKF_AD_OFF + SKF_AD_RANDOM:
402 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
404 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
406 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
407 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
409 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
410 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
412 case SKF_AD_OFF + SKF_AD_NLATTR:
413 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
415 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
416 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
418 case SKF_AD_OFF + SKF_AD_CPU:
419 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
421 case SKF_AD_OFF + SKF_AD_RANDOM:
422 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
423 bpf_user_rnd_init_once();
428 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
430 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
434 /* This is just a dummy call to avoid letting the compiler
435 * evict __bpf_call_base() as an optimization. Placed here
436 * where no-one bothers.
438 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
446 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
448 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
449 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
450 bool endian = BPF_SIZE(fp->code) == BPF_H ||
451 BPF_SIZE(fp->code) == BPF_W;
452 bool indirect = BPF_MODE(fp->code) == BPF_IND;
453 const int ip_align = NET_IP_ALIGN;
454 struct bpf_insn *insn = *insnp;
458 ((unaligned_ok && offset >= 0) ||
459 (!unaligned_ok && offset >= 0 &&
460 offset + ip_align >= 0 &&
461 offset + ip_align % size == 0))) {
462 bool ldx_off_ok = offset <= S16_MAX;
464 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
465 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
466 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
467 size, 2 + endian + (!ldx_off_ok * 2));
469 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
472 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
473 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
474 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
478 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
479 *insn++ = BPF_JMP_A(8);
482 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
483 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
484 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
486 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
488 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
490 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
493 switch (BPF_SIZE(fp->code)) {
495 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
498 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
501 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
507 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
508 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
509 *insn = BPF_EXIT_INSN();
516 * bpf_convert_filter - convert filter program
517 * @prog: the user passed filter program
518 * @len: the length of the user passed filter program
519 * @new_prog: allocated 'struct bpf_prog' or NULL
520 * @new_len: pointer to store length of converted program
521 * @seen_ld_abs: bool whether we've seen ld_abs/ind
523 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
524 * style extended BPF (eBPF).
525 * Conversion workflow:
527 * 1) First pass for calculating the new program length:
528 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
530 * 2) 2nd pass to remap in two passes: 1st pass finds new
531 * jump offsets, 2nd pass remapping:
532 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
534 static int bpf_convert_filter(struct sock_filter *prog, int len,
535 struct bpf_prog *new_prog, int *new_len,
538 int new_flen = 0, pass = 0, target, i, stack_off;
539 struct bpf_insn *new_insn, *first_insn = NULL;
540 struct sock_filter *fp;
544 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
545 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
547 if (len <= 0 || len > BPF_MAXINSNS)
551 first_insn = new_prog->insnsi;
552 addrs = kcalloc(len, sizeof(*addrs),
553 GFP_KERNEL | __GFP_NOWARN);
559 new_insn = first_insn;
562 /* Classic BPF related prologue emission. */
564 /* Classic BPF expects A and X to be reset first. These need
565 * to be guaranteed to be the first two instructions.
567 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
568 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
570 /* All programs must keep CTX in callee saved BPF_REG_CTX.
571 * In eBPF case it's done by the compiler, here we need to
572 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
574 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
576 /* For packet access in classic BPF, cache skb->data
577 * in callee-saved BPF R8 and skb->len - skb->data_len
578 * (headlen) in BPF R9. Since classic BPF is read-only
579 * on CTX, we only need to cache it once.
581 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
582 BPF_REG_D, BPF_REG_CTX,
583 offsetof(struct sk_buff, data));
584 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
585 offsetof(struct sk_buff, len));
586 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
587 offsetof(struct sk_buff, data_len));
588 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
594 for (i = 0; i < len; fp++, i++) {
595 struct bpf_insn tmp_insns[32] = { };
596 struct bpf_insn *insn = tmp_insns;
599 addrs[i] = new_insn - first_insn;
602 /* All arithmetic insns and skb loads map as-is. */
603 case BPF_ALU | BPF_ADD | BPF_X:
604 case BPF_ALU | BPF_ADD | BPF_K:
605 case BPF_ALU | BPF_SUB | BPF_X:
606 case BPF_ALU | BPF_SUB | BPF_K:
607 case BPF_ALU | BPF_AND | BPF_X:
608 case BPF_ALU | BPF_AND | BPF_K:
609 case BPF_ALU | BPF_OR | BPF_X:
610 case BPF_ALU | BPF_OR | BPF_K:
611 case BPF_ALU | BPF_LSH | BPF_X:
612 case BPF_ALU | BPF_LSH | BPF_K:
613 case BPF_ALU | BPF_RSH | BPF_X:
614 case BPF_ALU | BPF_RSH | BPF_K:
615 case BPF_ALU | BPF_XOR | BPF_X:
616 case BPF_ALU | BPF_XOR | BPF_K:
617 case BPF_ALU | BPF_MUL | BPF_X:
618 case BPF_ALU | BPF_MUL | BPF_K:
619 case BPF_ALU | BPF_DIV | BPF_X:
620 case BPF_ALU | BPF_DIV | BPF_K:
621 case BPF_ALU | BPF_MOD | BPF_X:
622 case BPF_ALU | BPF_MOD | BPF_K:
623 case BPF_ALU | BPF_NEG:
624 case BPF_LD | BPF_ABS | BPF_W:
625 case BPF_LD | BPF_ABS | BPF_H:
626 case BPF_LD | BPF_ABS | BPF_B:
627 case BPF_LD | BPF_IND | BPF_W:
628 case BPF_LD | BPF_IND | BPF_H:
629 case BPF_LD | BPF_IND | BPF_B:
630 /* Check for overloaded BPF extension and
631 * directly convert it if found, otherwise
632 * just move on with mapping.
634 if (BPF_CLASS(fp->code) == BPF_LD &&
635 BPF_MODE(fp->code) == BPF_ABS &&
636 convert_bpf_extensions(fp, &insn))
638 if (BPF_CLASS(fp->code) == BPF_LD &&
639 convert_bpf_ld_abs(fp, &insn)) {
644 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
645 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
646 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
647 /* Error with exception code on div/mod by 0.
648 * For cBPF programs, this was always return 0.
650 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
651 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
652 *insn++ = BPF_EXIT_INSN();
655 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
658 /* Jump transformation cannot use BPF block macros
659 * everywhere as offset calculation and target updates
660 * require a bit more work than the rest, i.e. jump
661 * opcodes map as-is, but offsets need adjustment.
664 #define BPF_EMIT_JMP \
666 const s32 off_min = S16_MIN, off_max = S16_MAX; \
669 if (target >= len || target < 0) \
671 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
672 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
673 off -= insn - tmp_insns; \
674 /* Reject anything not fitting into insn->off. */ \
675 if (off < off_min || off > off_max) \
680 case BPF_JMP | BPF_JA:
681 target = i + fp->k + 1;
682 insn->code = fp->code;
686 case BPF_JMP | BPF_JEQ | BPF_K:
687 case BPF_JMP | BPF_JEQ | BPF_X:
688 case BPF_JMP | BPF_JSET | BPF_K:
689 case BPF_JMP | BPF_JSET | BPF_X:
690 case BPF_JMP | BPF_JGT | BPF_K:
691 case BPF_JMP | BPF_JGT | BPF_X:
692 case BPF_JMP | BPF_JGE | BPF_K:
693 case BPF_JMP | BPF_JGE | BPF_X:
694 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
695 /* BPF immediates are signed, zero extend
696 * immediate into tmp register and use it
699 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
701 insn->dst_reg = BPF_REG_A;
702 insn->src_reg = BPF_REG_TMP;
705 insn->dst_reg = BPF_REG_A;
707 bpf_src = BPF_SRC(fp->code);
708 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
711 /* Common case where 'jump_false' is next insn. */
713 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
714 target = i + fp->jt + 1;
719 /* Convert some jumps when 'jump_true' is next insn. */
721 switch (BPF_OP(fp->code)) {
723 insn->code = BPF_JMP | BPF_JNE | bpf_src;
726 insn->code = BPF_JMP | BPF_JLE | bpf_src;
729 insn->code = BPF_JMP | BPF_JLT | bpf_src;
735 target = i + fp->jf + 1;
740 /* Other jumps are mapped into two insns: Jxx and JA. */
741 target = i + fp->jt + 1;
742 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
746 insn->code = BPF_JMP | BPF_JA;
747 target = i + fp->jf + 1;
751 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
752 case BPF_LDX | BPF_MSH | BPF_B: {
753 struct sock_filter tmp = {
754 .code = BPF_LD | BPF_ABS | BPF_B,
761 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
762 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
763 convert_bpf_ld_abs(&tmp, &insn);
766 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
768 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
770 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
772 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
774 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
777 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
778 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
780 case BPF_RET | BPF_A:
781 case BPF_RET | BPF_K:
782 if (BPF_RVAL(fp->code) == BPF_K)
783 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
785 *insn = BPF_EXIT_INSN();
788 /* Store to stack. */
791 stack_off = fp->k * 4 + 4;
792 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
793 BPF_ST ? BPF_REG_A : BPF_REG_X,
795 /* check_load_and_stores() verifies that classic BPF can
796 * load from stack only after write, so tracking
797 * stack_depth for ST|STX insns is enough
799 if (new_prog && new_prog->aux->stack_depth < stack_off)
800 new_prog->aux->stack_depth = stack_off;
803 /* Load from stack. */
804 case BPF_LD | BPF_MEM:
805 case BPF_LDX | BPF_MEM:
806 stack_off = fp->k * 4 + 4;
807 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
808 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
813 case BPF_LD | BPF_IMM:
814 case BPF_LDX | BPF_IMM:
815 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
816 BPF_REG_A : BPF_REG_X, fp->k);
820 case BPF_MISC | BPF_TAX:
821 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
825 case BPF_MISC | BPF_TXA:
826 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
829 /* A = skb->len or X = skb->len */
830 case BPF_LD | BPF_W | BPF_LEN:
831 case BPF_LDX | BPF_W | BPF_LEN:
832 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
833 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
834 offsetof(struct sk_buff, len));
837 /* Access seccomp_data fields. */
838 case BPF_LDX | BPF_ABS | BPF_W:
839 /* A = *(u32 *) (ctx + K) */
840 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
843 /* Unknown instruction. */
850 memcpy(new_insn, tmp_insns,
851 sizeof(*insn) * (insn - tmp_insns));
852 new_insn += insn - tmp_insns;
856 /* Only calculating new length. */
857 *new_len = new_insn - first_insn;
859 *new_len += 4; /* Prologue bits. */
864 if (new_flen != new_insn - first_insn) {
865 new_flen = new_insn - first_insn;
872 BUG_ON(*new_len != new_flen);
881 * As we dont want to clear mem[] array for each packet going through
882 * __bpf_prog_run(), we check that filter loaded by user never try to read
883 * a cell if not previously written, and we check all branches to be sure
884 * a malicious user doesn't try to abuse us.
886 static int check_load_and_stores(const struct sock_filter *filter, int flen)
888 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
891 BUILD_BUG_ON(BPF_MEMWORDS > 16);
893 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
897 memset(masks, 0xff, flen * sizeof(*masks));
899 for (pc = 0; pc < flen; pc++) {
900 memvalid &= masks[pc];
902 switch (filter[pc].code) {
905 memvalid |= (1 << filter[pc].k);
907 case BPF_LD | BPF_MEM:
908 case BPF_LDX | BPF_MEM:
909 if (!(memvalid & (1 << filter[pc].k))) {
914 case BPF_JMP | BPF_JA:
915 /* A jump must set masks on target */
916 masks[pc + 1 + filter[pc].k] &= memvalid;
919 case BPF_JMP | BPF_JEQ | BPF_K:
920 case BPF_JMP | BPF_JEQ | BPF_X:
921 case BPF_JMP | BPF_JGE | BPF_K:
922 case BPF_JMP | BPF_JGE | BPF_X:
923 case BPF_JMP | BPF_JGT | BPF_K:
924 case BPF_JMP | BPF_JGT | BPF_X:
925 case BPF_JMP | BPF_JSET | BPF_K:
926 case BPF_JMP | BPF_JSET | BPF_X:
927 /* A jump must set masks on targets */
928 masks[pc + 1 + filter[pc].jt] &= memvalid;
929 masks[pc + 1 + filter[pc].jf] &= memvalid;
939 static bool chk_code_allowed(u16 code_to_probe)
941 static const bool codes[] = {
942 /* 32 bit ALU operations */
943 [BPF_ALU | BPF_ADD | BPF_K] = true,
944 [BPF_ALU | BPF_ADD | BPF_X] = true,
945 [BPF_ALU | BPF_SUB | BPF_K] = true,
946 [BPF_ALU | BPF_SUB | BPF_X] = true,
947 [BPF_ALU | BPF_MUL | BPF_K] = true,
948 [BPF_ALU | BPF_MUL | BPF_X] = true,
949 [BPF_ALU | BPF_DIV | BPF_K] = true,
950 [BPF_ALU | BPF_DIV | BPF_X] = true,
951 [BPF_ALU | BPF_MOD | BPF_K] = true,
952 [BPF_ALU | BPF_MOD | BPF_X] = true,
953 [BPF_ALU | BPF_AND | BPF_K] = true,
954 [BPF_ALU | BPF_AND | BPF_X] = true,
955 [BPF_ALU | BPF_OR | BPF_K] = true,
956 [BPF_ALU | BPF_OR | BPF_X] = true,
957 [BPF_ALU | BPF_XOR | BPF_K] = true,
958 [BPF_ALU | BPF_XOR | BPF_X] = true,
959 [BPF_ALU | BPF_LSH | BPF_K] = true,
960 [BPF_ALU | BPF_LSH | BPF_X] = true,
961 [BPF_ALU | BPF_RSH | BPF_K] = true,
962 [BPF_ALU | BPF_RSH | BPF_X] = true,
963 [BPF_ALU | BPF_NEG] = true,
964 /* Load instructions */
965 [BPF_LD | BPF_W | BPF_ABS] = true,
966 [BPF_LD | BPF_H | BPF_ABS] = true,
967 [BPF_LD | BPF_B | BPF_ABS] = true,
968 [BPF_LD | BPF_W | BPF_LEN] = true,
969 [BPF_LD | BPF_W | BPF_IND] = true,
970 [BPF_LD | BPF_H | BPF_IND] = true,
971 [BPF_LD | BPF_B | BPF_IND] = true,
972 [BPF_LD | BPF_IMM] = true,
973 [BPF_LD | BPF_MEM] = true,
974 [BPF_LDX | BPF_W | BPF_LEN] = true,
975 [BPF_LDX | BPF_B | BPF_MSH] = true,
976 [BPF_LDX | BPF_IMM] = true,
977 [BPF_LDX | BPF_MEM] = true,
978 /* Store instructions */
981 /* Misc instructions */
982 [BPF_MISC | BPF_TAX] = true,
983 [BPF_MISC | BPF_TXA] = true,
984 /* Return instructions */
985 [BPF_RET | BPF_K] = true,
986 [BPF_RET | BPF_A] = true,
987 /* Jump instructions */
988 [BPF_JMP | BPF_JA] = true,
989 [BPF_JMP | BPF_JEQ | BPF_K] = true,
990 [BPF_JMP | BPF_JEQ | BPF_X] = true,
991 [BPF_JMP | BPF_JGE | BPF_K] = true,
992 [BPF_JMP | BPF_JGE | BPF_X] = true,
993 [BPF_JMP | BPF_JGT | BPF_K] = true,
994 [BPF_JMP | BPF_JGT | BPF_X] = true,
995 [BPF_JMP | BPF_JSET | BPF_K] = true,
996 [BPF_JMP | BPF_JSET | BPF_X] = true,
999 if (code_to_probe >= ARRAY_SIZE(codes))
1002 return codes[code_to_probe];
1005 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1010 if (flen == 0 || flen > BPF_MAXINSNS)
1017 * bpf_check_classic - verify socket filter code
1018 * @filter: filter to verify
1019 * @flen: length of filter
1021 * Check the user's filter code. If we let some ugly
1022 * filter code slip through kaboom! The filter must contain
1023 * no references or jumps that are out of range, no illegal
1024 * instructions, and must end with a RET instruction.
1026 * All jumps are forward as they are not signed.
1028 * Returns 0 if the rule set is legal or -EINVAL if not.
1030 static int bpf_check_classic(const struct sock_filter *filter,
1036 /* Check the filter code now */
1037 for (pc = 0; pc < flen; pc++) {
1038 const struct sock_filter *ftest = &filter[pc];
1040 /* May we actually operate on this code? */
1041 if (!chk_code_allowed(ftest->code))
1044 /* Some instructions need special checks */
1045 switch (ftest->code) {
1046 case BPF_ALU | BPF_DIV | BPF_K:
1047 case BPF_ALU | BPF_MOD | BPF_K:
1048 /* Check for division by zero */
1052 case BPF_ALU | BPF_LSH | BPF_K:
1053 case BPF_ALU | BPF_RSH | BPF_K:
1057 case BPF_LD | BPF_MEM:
1058 case BPF_LDX | BPF_MEM:
1061 /* Check for invalid memory addresses */
1062 if (ftest->k >= BPF_MEMWORDS)
1065 case BPF_JMP | BPF_JA:
1066 /* Note, the large ftest->k might cause loops.
1067 * Compare this with conditional jumps below,
1068 * where offsets are limited. --ANK (981016)
1070 if (ftest->k >= (unsigned int)(flen - pc - 1))
1073 case BPF_JMP | BPF_JEQ | BPF_K:
1074 case BPF_JMP | BPF_JEQ | BPF_X:
1075 case BPF_JMP | BPF_JGE | BPF_K:
1076 case BPF_JMP | BPF_JGE | BPF_X:
1077 case BPF_JMP | BPF_JGT | BPF_K:
1078 case BPF_JMP | BPF_JGT | BPF_X:
1079 case BPF_JMP | BPF_JSET | BPF_K:
1080 case BPF_JMP | BPF_JSET | BPF_X:
1081 /* Both conditionals must be safe */
1082 if (pc + ftest->jt + 1 >= flen ||
1083 pc + ftest->jf + 1 >= flen)
1086 case BPF_LD | BPF_W | BPF_ABS:
1087 case BPF_LD | BPF_H | BPF_ABS:
1088 case BPF_LD | BPF_B | BPF_ABS:
1090 if (bpf_anc_helper(ftest) & BPF_ANC)
1092 /* Ancillary operation unknown or unsupported */
1093 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1098 /* Last instruction must be a RET code */
1099 switch (filter[flen - 1].code) {
1100 case BPF_RET | BPF_K:
1101 case BPF_RET | BPF_A:
1102 return check_load_and_stores(filter, flen);
1108 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1109 const struct sock_fprog *fprog)
1111 unsigned int fsize = bpf_classic_proglen(fprog);
1112 struct sock_fprog_kern *fkprog;
1114 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1118 fkprog = fp->orig_prog;
1119 fkprog->len = fprog->len;
1121 fkprog->filter = kmemdup(fp->insns, fsize,
1122 GFP_KERNEL | __GFP_NOWARN);
1123 if (!fkprog->filter) {
1124 kfree(fp->orig_prog);
1131 static void bpf_release_orig_filter(struct bpf_prog *fp)
1133 struct sock_fprog_kern *fprog = fp->orig_prog;
1136 kfree(fprog->filter);
1141 static void __bpf_prog_release(struct bpf_prog *prog)
1143 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1146 bpf_release_orig_filter(prog);
1147 bpf_prog_free(prog);
1151 static void __sk_filter_release(struct sk_filter *fp)
1153 __bpf_prog_release(fp->prog);
1158 * sk_filter_release_rcu - Release a socket filter by rcu_head
1159 * @rcu: rcu_head that contains the sk_filter to free
1161 static void sk_filter_release_rcu(struct rcu_head *rcu)
1163 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1165 __sk_filter_release(fp);
1169 * sk_filter_release - release a socket filter
1170 * @fp: filter to remove
1172 * Remove a filter from a socket and release its resources.
1174 static void sk_filter_release(struct sk_filter *fp)
1176 if (refcount_dec_and_test(&fp->refcnt))
1177 call_rcu(&fp->rcu, sk_filter_release_rcu);
1180 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1182 u32 filter_size = bpf_prog_size(fp->prog->len);
1184 atomic_sub(filter_size, &sk->sk_omem_alloc);
1185 sk_filter_release(fp);
1188 /* try to charge the socket memory if there is space available
1189 * return true on success
1191 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1193 u32 filter_size = bpf_prog_size(fp->prog->len);
1195 /* same check as in sock_kmalloc() */
1196 if (filter_size <= sysctl_optmem_max &&
1197 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1198 atomic_add(filter_size, &sk->sk_omem_alloc);
1204 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1206 if (!refcount_inc_not_zero(&fp->refcnt))
1209 if (!__sk_filter_charge(sk, fp)) {
1210 sk_filter_release(fp);
1216 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1218 struct sock_filter *old_prog;
1219 struct bpf_prog *old_fp;
1220 int err, new_len, old_len = fp->len;
1221 bool seen_ld_abs = false;
1223 /* We are free to overwrite insns et al right here as it
1224 * won't be used at this point in time anymore internally
1225 * after the migration to the internal BPF instruction
1228 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1229 sizeof(struct bpf_insn));
1231 /* Conversion cannot happen on overlapping memory areas,
1232 * so we need to keep the user BPF around until the 2nd
1233 * pass. At this time, the user BPF is stored in fp->insns.
1235 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1236 GFP_KERNEL | __GFP_NOWARN);
1242 /* 1st pass: calculate the new program length. */
1243 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1248 /* Expand fp for appending the new filter representation. */
1250 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1252 /* The old_fp is still around in case we couldn't
1253 * allocate new memory, so uncharge on that one.
1262 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1263 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1266 /* 2nd bpf_convert_filter() can fail only if it fails
1267 * to allocate memory, remapping must succeed. Note,
1268 * that at this time old_fp has already been released
1273 fp = bpf_prog_select_runtime(fp, &err);
1283 __bpf_prog_release(fp);
1284 return ERR_PTR(err);
1287 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1288 bpf_aux_classic_check_t trans)
1292 fp->bpf_func = NULL;
1295 err = bpf_check_classic(fp->insns, fp->len);
1297 __bpf_prog_release(fp);
1298 return ERR_PTR(err);
1301 /* There might be additional checks and transformations
1302 * needed on classic filters, f.e. in case of seccomp.
1305 err = trans(fp->insns, fp->len);
1307 __bpf_prog_release(fp);
1308 return ERR_PTR(err);
1312 /* Probe if we can JIT compile the filter and if so, do
1313 * the compilation of the filter.
1315 bpf_jit_compile(fp);
1317 /* JIT compiler couldn't process this filter, so do the
1318 * internal BPF translation for the optimized interpreter.
1321 fp = bpf_migrate_filter(fp);
1327 * bpf_prog_create - create an unattached filter
1328 * @pfp: the unattached filter that is created
1329 * @fprog: the filter program
1331 * Create a filter independent of any socket. We first run some
1332 * sanity checks on it to make sure it does not explode on us later.
1333 * If an error occurs or there is insufficient memory for the filter
1334 * a negative errno code is returned. On success the return is zero.
1336 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1338 unsigned int fsize = bpf_classic_proglen(fprog);
1339 struct bpf_prog *fp;
1341 /* Make sure new filter is there and in the right amounts. */
1342 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1345 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1349 memcpy(fp->insns, fprog->filter, fsize);
1351 fp->len = fprog->len;
1352 /* Since unattached filters are not copied back to user
1353 * space through sk_get_filter(), we do not need to hold
1354 * a copy here, and can spare us the work.
1356 fp->orig_prog = NULL;
1358 /* bpf_prepare_filter() already takes care of freeing
1359 * memory in case something goes wrong.
1361 fp = bpf_prepare_filter(fp, NULL);
1368 EXPORT_SYMBOL_GPL(bpf_prog_create);
1371 * bpf_prog_create_from_user - create an unattached filter from user buffer
1372 * @pfp: the unattached filter that is created
1373 * @fprog: the filter program
1374 * @trans: post-classic verifier transformation handler
1375 * @save_orig: save classic BPF program
1377 * This function effectively does the same as bpf_prog_create(), only
1378 * that it builds up its insns buffer from user space provided buffer.
1379 * It also allows for passing a bpf_aux_classic_check_t handler.
1381 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1382 bpf_aux_classic_check_t trans, bool save_orig)
1384 unsigned int fsize = bpf_classic_proglen(fprog);
1385 struct bpf_prog *fp;
1388 /* Make sure new filter is there and in the right amounts. */
1389 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1392 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1396 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1397 __bpf_prog_free(fp);
1401 fp->len = fprog->len;
1402 fp->orig_prog = NULL;
1405 err = bpf_prog_store_orig_filter(fp, fprog);
1407 __bpf_prog_free(fp);
1412 /* bpf_prepare_filter() already takes care of freeing
1413 * memory in case something goes wrong.
1415 fp = bpf_prepare_filter(fp, trans);
1422 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1424 void bpf_prog_destroy(struct bpf_prog *fp)
1426 __bpf_prog_release(fp);
1428 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1430 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1432 struct sk_filter *fp, *old_fp;
1434 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1440 if (!__sk_filter_charge(sk, fp)) {
1444 refcount_set(&fp->refcnt, 1);
1446 old_fp = rcu_dereference_protected(sk->sk_filter,
1447 lockdep_sock_is_held(sk));
1448 rcu_assign_pointer(sk->sk_filter, fp);
1451 sk_filter_uncharge(sk, old_fp);
1457 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1459 unsigned int fsize = bpf_classic_proglen(fprog);
1460 struct bpf_prog *prog;
1463 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1464 return ERR_PTR(-EPERM);
1466 /* Make sure new filter is there and in the right amounts. */
1467 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1468 return ERR_PTR(-EINVAL);
1470 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1472 return ERR_PTR(-ENOMEM);
1474 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1475 __bpf_prog_free(prog);
1476 return ERR_PTR(-EFAULT);
1479 prog->len = fprog->len;
1481 err = bpf_prog_store_orig_filter(prog, fprog);
1483 __bpf_prog_free(prog);
1484 return ERR_PTR(-ENOMEM);
1487 /* bpf_prepare_filter() already takes care of freeing
1488 * memory in case something goes wrong.
1490 return bpf_prepare_filter(prog, NULL);
1494 * sk_attach_filter - attach a socket filter
1495 * @fprog: the filter program
1496 * @sk: the socket to use
1498 * Attach the user's filter code. We first run some sanity checks on
1499 * it to make sure it does not explode on us later. If an error
1500 * occurs or there is insufficient memory for the filter a negative
1501 * errno code is returned. On success the return is zero.
1503 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1505 struct bpf_prog *prog = __get_filter(fprog, sk);
1509 return PTR_ERR(prog);
1511 err = __sk_attach_prog(prog, sk);
1513 __bpf_prog_release(prog);
1519 EXPORT_SYMBOL_GPL(sk_attach_filter);
1521 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1523 struct bpf_prog *prog = __get_filter(fprog, sk);
1527 return PTR_ERR(prog);
1529 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1532 err = reuseport_attach_prog(sk, prog);
1535 __bpf_prog_release(prog);
1540 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1542 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1543 return ERR_PTR(-EPERM);
1545 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1548 int sk_attach_bpf(u32 ufd, struct sock *sk)
1550 struct bpf_prog *prog = __get_bpf(ufd, sk);
1554 return PTR_ERR(prog);
1556 err = __sk_attach_prog(prog, sk);
1565 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1567 struct bpf_prog *prog;
1570 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1573 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1574 if (IS_ERR(prog) && PTR_ERR(prog) == -EINVAL)
1575 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1577 return PTR_ERR(prog);
1579 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1580 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1581 * bpf prog (e.g. sockmap). It depends on the
1582 * limitation imposed by bpf_prog_load().
1583 * Hence, sysctl_optmem_max is not checked.
1585 if ((sk->sk_type != SOCK_STREAM &&
1586 sk->sk_type != SOCK_DGRAM) ||
1587 (sk->sk_protocol != IPPROTO_UDP &&
1588 sk->sk_protocol != IPPROTO_TCP) ||
1589 (sk->sk_family != AF_INET &&
1590 sk->sk_family != AF_INET6)) {
1595 /* BPF_PROG_TYPE_SOCKET_FILTER */
1596 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1602 err = reuseport_attach_prog(sk, prog);
1610 void sk_reuseport_prog_free(struct bpf_prog *prog)
1615 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1618 bpf_prog_destroy(prog);
1621 struct bpf_scratchpad {
1623 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1624 u8 buff[MAX_BPF_STACK];
1628 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1630 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1631 unsigned int write_len)
1633 return skb_ensure_writable(skb, write_len);
1636 static inline int bpf_try_make_writable(struct sk_buff *skb,
1637 unsigned int write_len)
1639 int err = __bpf_try_make_writable(skb, write_len);
1641 bpf_compute_data_pointers(skb);
1645 static int bpf_try_make_head_writable(struct sk_buff *skb)
1647 return bpf_try_make_writable(skb, skb_headlen(skb));
1650 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1652 if (skb_at_tc_ingress(skb))
1653 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1656 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1658 if (skb_at_tc_ingress(skb))
1659 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1662 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1663 const void *, from, u32, len, u64, flags)
1667 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1669 if (unlikely(offset > 0xffff))
1671 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1674 ptr = skb->data + offset;
1675 if (flags & BPF_F_RECOMPUTE_CSUM)
1676 __skb_postpull_rcsum(skb, ptr, len, offset);
1678 memcpy(ptr, from, len);
1680 if (flags & BPF_F_RECOMPUTE_CSUM)
1681 __skb_postpush_rcsum(skb, ptr, len, offset);
1682 if (flags & BPF_F_INVALIDATE_HASH)
1683 skb_clear_hash(skb);
1688 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1689 .func = bpf_skb_store_bytes,
1691 .ret_type = RET_INTEGER,
1692 .arg1_type = ARG_PTR_TO_CTX,
1693 .arg2_type = ARG_ANYTHING,
1694 .arg3_type = ARG_PTR_TO_MEM,
1695 .arg4_type = ARG_CONST_SIZE,
1696 .arg5_type = ARG_ANYTHING,
1699 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1700 void *, to, u32, len)
1704 if (unlikely(offset > 0xffff))
1707 ptr = skb_header_pointer(skb, offset, len, to);
1711 memcpy(to, ptr, len);
1719 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1720 .func = bpf_skb_load_bytes,
1722 .ret_type = RET_INTEGER,
1723 .arg1_type = ARG_PTR_TO_CTX,
1724 .arg2_type = ARG_ANYTHING,
1725 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1726 .arg4_type = ARG_CONST_SIZE,
1729 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1730 u32, offset, void *, to, u32, len, u32, start_header)
1732 u8 *end = skb_tail_pointer(skb);
1733 u8 *net = skb_network_header(skb);
1734 u8 *mac = skb_mac_header(skb);
1737 if (unlikely(offset > 0xffff || len > (end - mac)))
1740 switch (start_header) {
1741 case BPF_HDR_START_MAC:
1744 case BPF_HDR_START_NET:
1751 if (likely(ptr >= mac && ptr + len <= end)) {
1752 memcpy(to, ptr, len);
1761 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1762 .func = bpf_skb_load_bytes_relative,
1764 .ret_type = RET_INTEGER,
1765 .arg1_type = ARG_PTR_TO_CTX,
1766 .arg2_type = ARG_ANYTHING,
1767 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1768 .arg4_type = ARG_CONST_SIZE,
1769 .arg5_type = ARG_ANYTHING,
1772 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1774 /* Idea is the following: should the needed direct read/write
1775 * test fail during runtime, we can pull in more data and redo
1776 * again, since implicitly, we invalidate previous checks here.
1778 * Or, since we know how much we need to make read/writeable,
1779 * this can be done once at the program beginning for direct
1780 * access case. By this we overcome limitations of only current
1781 * headroom being accessible.
1783 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1786 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1787 .func = bpf_skb_pull_data,
1789 .ret_type = RET_INTEGER,
1790 .arg1_type = ARG_PTR_TO_CTX,
1791 .arg2_type = ARG_ANYTHING,
1794 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1795 unsigned int write_len)
1797 int err = __bpf_try_make_writable(skb, write_len);
1799 bpf_compute_data_end_sk_skb(skb);
1803 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1805 /* Idea is the following: should the needed direct read/write
1806 * test fail during runtime, we can pull in more data and redo
1807 * again, since implicitly, we invalidate previous checks here.
1809 * Or, since we know how much we need to make read/writeable,
1810 * this can be done once at the program beginning for direct
1811 * access case. By this we overcome limitations of only current
1812 * headroom being accessible.
1814 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1817 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1818 .func = sk_skb_pull_data,
1820 .ret_type = RET_INTEGER,
1821 .arg1_type = ARG_PTR_TO_CTX,
1822 .arg2_type = ARG_ANYTHING,
1825 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1826 u64, from, u64, to, u64, flags)
1830 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1832 if (unlikely(offset > 0xffff || offset & 1))
1834 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1837 ptr = (__sum16 *)(skb->data + offset);
1838 switch (flags & BPF_F_HDR_FIELD_MASK) {
1840 if (unlikely(from != 0))
1843 csum_replace_by_diff(ptr, to);
1846 csum_replace2(ptr, from, to);
1849 csum_replace4(ptr, from, to);
1858 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1859 .func = bpf_l3_csum_replace,
1861 .ret_type = RET_INTEGER,
1862 .arg1_type = ARG_PTR_TO_CTX,
1863 .arg2_type = ARG_ANYTHING,
1864 .arg3_type = ARG_ANYTHING,
1865 .arg4_type = ARG_ANYTHING,
1866 .arg5_type = ARG_ANYTHING,
1869 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1870 u64, from, u64, to, u64, flags)
1872 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1873 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1874 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1877 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1878 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1880 if (unlikely(offset > 0xffff || offset & 1))
1882 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1885 ptr = (__sum16 *)(skb->data + offset);
1886 if (is_mmzero && !do_mforce && !*ptr)
1889 switch (flags & BPF_F_HDR_FIELD_MASK) {
1891 if (unlikely(from != 0))
1894 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1897 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1900 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1906 if (is_mmzero && !*ptr)
1907 *ptr = CSUM_MANGLED_0;
1911 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1912 .func = bpf_l4_csum_replace,
1914 .ret_type = RET_INTEGER,
1915 .arg1_type = ARG_PTR_TO_CTX,
1916 .arg2_type = ARG_ANYTHING,
1917 .arg3_type = ARG_ANYTHING,
1918 .arg4_type = ARG_ANYTHING,
1919 .arg5_type = ARG_ANYTHING,
1922 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1923 __be32 *, to, u32, to_size, __wsum, seed)
1925 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1926 u32 diff_size = from_size + to_size;
1929 /* This is quite flexible, some examples:
1931 * from_size == 0, to_size > 0, seed := csum --> pushing data
1932 * from_size > 0, to_size == 0, seed := csum --> pulling data
1933 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1935 * Even for diffing, from_size and to_size don't need to be equal.
1937 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1938 diff_size > sizeof(sp->diff)))
1941 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1942 sp->diff[j] = ~from[i];
1943 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1944 sp->diff[j] = to[i];
1946 return csum_partial(sp->diff, diff_size, seed);
1949 static const struct bpf_func_proto bpf_csum_diff_proto = {
1950 .func = bpf_csum_diff,
1953 .ret_type = RET_INTEGER,
1954 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1955 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1956 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1957 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1958 .arg5_type = ARG_ANYTHING,
1961 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1963 /* The interface is to be used in combination with bpf_csum_diff()
1964 * for direct packet writes. csum rotation for alignment as well
1965 * as emulating csum_sub() can be done from the eBPF program.
1967 if (skb->ip_summed == CHECKSUM_COMPLETE)
1968 return (skb->csum = csum_add(skb->csum, csum));
1973 static const struct bpf_func_proto bpf_csum_update_proto = {
1974 .func = bpf_csum_update,
1976 .ret_type = RET_INTEGER,
1977 .arg1_type = ARG_PTR_TO_CTX,
1978 .arg2_type = ARG_ANYTHING,
1981 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1983 return dev_forward_skb(dev, skb);
1986 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1987 struct sk_buff *skb)
1989 int ret = ____dev_forward_skb(dev, skb);
1993 ret = netif_rx(skb);
1999 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2003 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
2004 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2011 __this_cpu_inc(xmit_recursion);
2012 ret = dev_queue_xmit(skb);
2013 __this_cpu_dec(xmit_recursion);
2018 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2021 /* skb->mac_len is not set on normal egress */
2022 unsigned int mlen = skb->network_header - skb->mac_header;
2024 __skb_pull(skb, mlen);
2026 /* At ingress, the mac header has already been pulled once.
2027 * At egress, skb_pospull_rcsum has to be done in case that
2028 * the skb is originated from ingress (i.e. a forwarded skb)
2029 * to ensure that rcsum starts at net header.
2031 if (!skb_at_tc_ingress(skb))
2032 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2033 skb_pop_mac_header(skb);
2034 skb_reset_mac_len(skb);
2035 return flags & BPF_F_INGRESS ?
2036 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2039 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2042 /* Verify that a link layer header is carried */
2043 if (unlikely(skb->mac_header >= skb->network_header)) {
2048 bpf_push_mac_rcsum(skb);
2049 return flags & BPF_F_INGRESS ?
2050 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2053 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2056 if (dev_is_mac_header_xmit(dev))
2057 return __bpf_redirect_common(skb, dev, flags);
2059 return __bpf_redirect_no_mac(skb, dev, flags);
2062 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2064 struct net_device *dev;
2065 struct sk_buff *clone;
2068 if (unlikely(flags & ~(BPF_F_INGRESS)))
2071 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2075 clone = skb_clone(skb, GFP_ATOMIC);
2076 if (unlikely(!clone))
2079 /* For direct write, we need to keep the invariant that the skbs
2080 * we're dealing with need to be uncloned. Should uncloning fail
2081 * here, we need to free the just generated clone to unclone once
2084 ret = bpf_try_make_head_writable(skb);
2085 if (unlikely(ret)) {
2090 return __bpf_redirect(clone, dev, flags);
2093 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2094 .func = bpf_clone_redirect,
2096 .ret_type = RET_INTEGER,
2097 .arg1_type = ARG_PTR_TO_CTX,
2098 .arg2_type = ARG_ANYTHING,
2099 .arg3_type = ARG_ANYTHING,
2102 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2103 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2105 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2107 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2109 if (unlikely(flags & ~(BPF_F_INGRESS)))
2112 ri->ifindex = ifindex;
2115 return TC_ACT_REDIRECT;
2118 int skb_do_redirect(struct sk_buff *skb)
2120 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2121 struct net_device *dev;
2123 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2125 if (unlikely(!dev)) {
2130 return __bpf_redirect(skb, dev, ri->flags);
2133 static const struct bpf_func_proto bpf_redirect_proto = {
2134 .func = bpf_redirect,
2136 .ret_type = RET_INTEGER,
2137 .arg1_type = ARG_ANYTHING,
2138 .arg2_type = ARG_ANYTHING,
2141 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
2142 struct bpf_map *, map, void *, key, u64, flags)
2144 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2146 /* If user passes invalid input drop the packet. */
2147 if (unlikely(flags & ~(BPF_F_INGRESS)))
2150 tcb->bpf.flags = flags;
2151 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
2152 if (!tcb->bpf.sk_redir)
2158 static const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
2159 .func = bpf_sk_redirect_hash,
2161 .ret_type = RET_INTEGER,
2162 .arg1_type = ARG_PTR_TO_CTX,
2163 .arg2_type = ARG_CONST_MAP_PTR,
2164 .arg3_type = ARG_PTR_TO_MAP_KEY,
2165 .arg4_type = ARG_ANYTHING,
2168 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
2169 struct bpf_map *, map, u32, key, u64, flags)
2171 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2173 /* If user passes invalid input drop the packet. */
2174 if (unlikely(flags & ~(BPF_F_INGRESS)))
2177 tcb->bpf.flags = flags;
2178 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
2179 if (!tcb->bpf.sk_redir)
2185 struct sock *do_sk_redirect_map(struct sk_buff *skb)
2187 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2189 return tcb->bpf.sk_redir;
2192 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
2193 .func = bpf_sk_redirect_map,
2195 .ret_type = RET_INTEGER,
2196 .arg1_type = ARG_PTR_TO_CTX,
2197 .arg2_type = ARG_CONST_MAP_PTR,
2198 .arg3_type = ARG_ANYTHING,
2199 .arg4_type = ARG_ANYTHING,
2202 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg_buff *, msg,
2203 struct bpf_map *, map, void *, key, u64, flags)
2205 /* If user passes invalid input drop the packet. */
2206 if (unlikely(flags & ~(BPF_F_INGRESS)))
2210 msg->sk_redir = __sock_hash_lookup_elem(map, key);
2217 static const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
2218 .func = bpf_msg_redirect_hash,
2220 .ret_type = RET_INTEGER,
2221 .arg1_type = ARG_PTR_TO_CTX,
2222 .arg2_type = ARG_CONST_MAP_PTR,
2223 .arg3_type = ARG_PTR_TO_MAP_KEY,
2224 .arg4_type = ARG_ANYTHING,
2227 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg_buff *, msg,
2228 struct bpf_map *, map, u32, key, u64, flags)
2230 /* If user passes invalid input drop the packet. */
2231 if (unlikely(flags & ~(BPF_F_INGRESS)))
2235 msg->sk_redir = __sock_map_lookup_elem(map, key);
2242 struct sock *do_msg_redirect_map(struct sk_msg_buff *msg)
2244 return msg->sk_redir;
2247 static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
2248 .func = bpf_msg_redirect_map,
2250 .ret_type = RET_INTEGER,
2251 .arg1_type = ARG_PTR_TO_CTX,
2252 .arg2_type = ARG_CONST_MAP_PTR,
2253 .arg3_type = ARG_ANYTHING,
2254 .arg4_type = ARG_ANYTHING,
2257 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg_buff *, msg, u32, bytes)
2259 msg->apply_bytes = bytes;
2263 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2264 .func = bpf_msg_apply_bytes,
2266 .ret_type = RET_INTEGER,
2267 .arg1_type = ARG_PTR_TO_CTX,
2268 .arg2_type = ARG_ANYTHING,
2271 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg_buff *, msg, u32, bytes)
2273 msg->cork_bytes = bytes;
2277 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2278 .func = bpf_msg_cork_bytes,
2280 .ret_type = RET_INTEGER,
2281 .arg1_type = ARG_PTR_TO_CTX,
2282 .arg2_type = ARG_ANYTHING,
2285 BPF_CALL_4(bpf_msg_pull_data,
2286 struct sk_msg_buff *, msg, u32, start, u32, end, u64, flags)
2288 unsigned int len = 0, offset = 0, copy = 0;
2289 struct scatterlist *sg = msg->sg_data;
2290 int first_sg, last_sg, i, shift;
2291 unsigned char *p, *to, *from;
2292 int bytes = end - start;
2295 if (unlikely(flags || end <= start))
2298 /* First find the starting scatterlist element */
2303 if (start < offset + len)
2306 if (i == MAX_SKB_FRAGS)
2308 } while (i != msg->sg_end);
2310 if (unlikely(start >= offset + len))
2313 if (!msg->sg_copy[i] && bytes <= len)
2318 /* At this point we need to linearize multiple scatterlist
2319 * elements or a single shared page. Either way we need to
2320 * copy into a linear buffer exclusively owned by BPF. Then
2321 * place the buffer in the scatterlist and fixup the original
2322 * entries by removing the entries now in the linear buffer
2323 * and shifting the remaining entries. For now we do not try
2324 * to copy partial entries to avoid complexity of running out
2325 * of sg_entry slots. The downside is reading a single byte
2326 * will copy the entire sg entry.
2329 copy += sg[i].length;
2331 if (i == MAX_SKB_FRAGS)
2335 } while (i != msg->sg_end);
2338 if (unlikely(copy < end - start))
2341 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC, get_order(copy));
2342 if (unlikely(!page))
2344 p = page_address(page);
2349 from = sg_virt(&sg[i]);
2353 memcpy(to, from, len);
2356 put_page(sg_page(&sg[i]));
2359 if (i == MAX_SKB_FRAGS)
2361 } while (i != last_sg);
2363 sg[first_sg].length = copy;
2364 sg_set_page(&sg[first_sg], page, copy, 0);
2366 /* To repair sg ring we need to shift entries. If we only
2367 * had a single entry though we can just replace it and
2368 * be done. Otherwise walk the ring and shift the entries.
2370 shift = last_sg - first_sg - 1;
2378 if (i + shift >= MAX_SKB_FRAGS)
2379 move_from = i + shift - MAX_SKB_FRAGS;
2381 move_from = i + shift;
2383 if (move_from == msg->sg_end)
2386 sg[i] = sg[move_from];
2387 sg[move_from].length = 0;
2388 sg[move_from].page_link = 0;
2389 sg[move_from].offset = 0;
2392 if (i == MAX_SKB_FRAGS)
2395 msg->sg_end -= shift;
2396 if (msg->sg_end < 0)
2397 msg->sg_end += MAX_SKB_FRAGS;
2399 msg->data = sg_virt(&sg[i]) + start - offset;
2400 msg->data_end = msg->data + bytes;
2405 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2406 .func = bpf_msg_pull_data,
2408 .ret_type = RET_INTEGER,
2409 .arg1_type = ARG_PTR_TO_CTX,
2410 .arg2_type = ARG_ANYTHING,
2411 .arg3_type = ARG_ANYTHING,
2412 .arg4_type = ARG_ANYTHING,
2415 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2417 return task_get_classid(skb);
2420 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2421 .func = bpf_get_cgroup_classid,
2423 .ret_type = RET_INTEGER,
2424 .arg1_type = ARG_PTR_TO_CTX,
2427 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2429 return dst_tclassid(skb);
2432 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2433 .func = bpf_get_route_realm,
2435 .ret_type = RET_INTEGER,
2436 .arg1_type = ARG_PTR_TO_CTX,
2439 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2441 /* If skb_clear_hash() was called due to mangling, we can
2442 * trigger SW recalculation here. Later access to hash
2443 * can then use the inline skb->hash via context directly
2444 * instead of calling this helper again.
2446 return skb_get_hash(skb);
2449 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2450 .func = bpf_get_hash_recalc,
2452 .ret_type = RET_INTEGER,
2453 .arg1_type = ARG_PTR_TO_CTX,
2456 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2458 /* After all direct packet write, this can be used once for
2459 * triggering a lazy recalc on next skb_get_hash() invocation.
2461 skb_clear_hash(skb);
2465 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2466 .func = bpf_set_hash_invalid,
2468 .ret_type = RET_INTEGER,
2469 .arg1_type = ARG_PTR_TO_CTX,
2472 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2474 /* Set user specified hash as L4(+), so that it gets returned
2475 * on skb_get_hash() call unless BPF prog later on triggers a
2478 __skb_set_sw_hash(skb, hash, true);
2482 static const struct bpf_func_proto bpf_set_hash_proto = {
2483 .func = bpf_set_hash,
2485 .ret_type = RET_INTEGER,
2486 .arg1_type = ARG_PTR_TO_CTX,
2487 .arg2_type = ARG_ANYTHING,
2490 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2495 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2496 vlan_proto != htons(ETH_P_8021AD)))
2497 vlan_proto = htons(ETH_P_8021Q);
2499 bpf_push_mac_rcsum(skb);
2500 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2501 bpf_pull_mac_rcsum(skb);
2503 bpf_compute_data_pointers(skb);
2507 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2508 .func = bpf_skb_vlan_push,
2510 .ret_type = RET_INTEGER,
2511 .arg1_type = ARG_PTR_TO_CTX,
2512 .arg2_type = ARG_ANYTHING,
2513 .arg3_type = ARG_ANYTHING,
2516 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2520 bpf_push_mac_rcsum(skb);
2521 ret = skb_vlan_pop(skb);
2522 bpf_pull_mac_rcsum(skb);
2524 bpf_compute_data_pointers(skb);
2528 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2529 .func = bpf_skb_vlan_pop,
2531 .ret_type = RET_INTEGER,
2532 .arg1_type = ARG_PTR_TO_CTX,
2535 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2537 /* Caller already did skb_cow() with len as headroom,
2538 * so no need to do it here.
2541 memmove(skb->data, skb->data + len, off);
2542 memset(skb->data + off, 0, len);
2544 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2545 * needed here as it does not change the skb->csum
2546 * result for checksum complete when summing over
2552 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2554 /* skb_ensure_writable() is not needed here, as we're
2555 * already working on an uncloned skb.
2557 if (unlikely(!pskb_may_pull(skb, off + len)))
2560 skb_postpull_rcsum(skb, skb->data + off, len);
2561 memmove(skb->data + len, skb->data, off);
2562 __skb_pull(skb, len);
2567 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2569 bool trans_same = skb->transport_header == skb->network_header;
2572 /* There's no need for __skb_push()/__skb_pull() pair to
2573 * get to the start of the mac header as we're guaranteed
2574 * to always start from here under eBPF.
2576 ret = bpf_skb_generic_push(skb, off, len);
2578 skb->mac_header -= len;
2579 skb->network_header -= len;
2581 skb->transport_header = skb->network_header;
2587 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2589 bool trans_same = skb->transport_header == skb->network_header;
2592 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2593 ret = bpf_skb_generic_pop(skb, off, len);
2595 skb->mac_header += len;
2596 skb->network_header += len;
2598 skb->transport_header = skb->network_header;
2604 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2606 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2607 u32 off = skb_mac_header_len(skb);
2610 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2611 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2614 ret = skb_cow(skb, len_diff);
2615 if (unlikely(ret < 0))
2618 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2619 if (unlikely(ret < 0))
2622 if (skb_is_gso(skb)) {
2623 struct skb_shared_info *shinfo = skb_shinfo(skb);
2625 /* SKB_GSO_TCPV4 needs to be changed into
2628 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2629 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2630 shinfo->gso_type |= SKB_GSO_TCPV6;
2633 /* Due to IPv6 header, MSS needs to be downgraded. */
2634 skb_decrease_gso_size(shinfo, len_diff);
2635 /* Header must be checked, and gso_segs recomputed. */
2636 shinfo->gso_type |= SKB_GSO_DODGY;
2637 shinfo->gso_segs = 0;
2640 skb->protocol = htons(ETH_P_IPV6);
2641 skb_clear_hash(skb);
2646 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2648 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2649 u32 off = skb_mac_header_len(skb);
2652 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2653 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2656 ret = skb_unclone(skb, GFP_ATOMIC);
2657 if (unlikely(ret < 0))
2660 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2661 if (unlikely(ret < 0))
2664 if (skb_is_gso(skb)) {
2665 struct skb_shared_info *shinfo = skb_shinfo(skb);
2667 /* SKB_GSO_TCPV6 needs to be changed into
2670 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2671 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2672 shinfo->gso_type |= SKB_GSO_TCPV4;
2675 /* Due to IPv4 header, MSS can be upgraded. */
2676 skb_increase_gso_size(shinfo, len_diff);
2677 /* Header must be checked, and gso_segs recomputed. */
2678 shinfo->gso_type |= SKB_GSO_DODGY;
2679 shinfo->gso_segs = 0;
2682 skb->protocol = htons(ETH_P_IP);
2683 skb_clear_hash(skb);
2688 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2690 __be16 from_proto = skb->protocol;
2692 if (from_proto == htons(ETH_P_IP) &&
2693 to_proto == htons(ETH_P_IPV6))
2694 return bpf_skb_proto_4_to_6(skb);
2696 if (from_proto == htons(ETH_P_IPV6) &&
2697 to_proto == htons(ETH_P_IP))
2698 return bpf_skb_proto_6_to_4(skb);
2703 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2708 if (unlikely(flags))
2711 /* General idea is that this helper does the basic groundwork
2712 * needed for changing the protocol, and eBPF program fills the
2713 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2714 * and other helpers, rather than passing a raw buffer here.
2716 * The rationale is to keep this minimal and without a need to
2717 * deal with raw packet data. F.e. even if we would pass buffers
2718 * here, the program still needs to call the bpf_lX_csum_replace()
2719 * helpers anyway. Plus, this way we keep also separation of
2720 * concerns, since f.e. bpf_skb_store_bytes() should only take
2723 * Currently, additional options and extension header space are
2724 * not supported, but flags register is reserved so we can adapt
2725 * that. For offloads, we mark packet as dodgy, so that headers
2726 * need to be verified first.
2728 ret = bpf_skb_proto_xlat(skb, proto);
2729 bpf_compute_data_pointers(skb);
2733 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2734 .func = bpf_skb_change_proto,
2736 .ret_type = RET_INTEGER,
2737 .arg1_type = ARG_PTR_TO_CTX,
2738 .arg2_type = ARG_ANYTHING,
2739 .arg3_type = ARG_ANYTHING,
2742 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2744 /* We only allow a restricted subset to be changed for now. */
2745 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2746 !skb_pkt_type_ok(pkt_type)))
2749 skb->pkt_type = pkt_type;
2753 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2754 .func = bpf_skb_change_type,
2756 .ret_type = RET_INTEGER,
2757 .arg1_type = ARG_PTR_TO_CTX,
2758 .arg2_type = ARG_ANYTHING,
2761 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2763 switch (skb->protocol) {
2764 case htons(ETH_P_IP):
2765 return sizeof(struct iphdr);
2766 case htons(ETH_P_IPV6):
2767 return sizeof(struct ipv6hdr);
2773 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2775 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2778 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2779 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2782 ret = skb_cow(skb, len_diff);
2783 if (unlikely(ret < 0))
2786 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2787 if (unlikely(ret < 0))
2790 if (skb_is_gso(skb)) {
2791 struct skb_shared_info *shinfo = skb_shinfo(skb);
2793 /* Due to header grow, MSS needs to be downgraded. */
2794 skb_decrease_gso_size(shinfo, len_diff);
2795 /* Header must be checked, and gso_segs recomputed. */
2796 shinfo->gso_type |= SKB_GSO_DODGY;
2797 shinfo->gso_segs = 0;
2803 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2805 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2808 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2809 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2812 ret = skb_unclone(skb, GFP_ATOMIC);
2813 if (unlikely(ret < 0))
2816 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2817 if (unlikely(ret < 0))
2820 if (skb_is_gso(skb)) {
2821 struct skb_shared_info *shinfo = skb_shinfo(skb);
2823 /* Due to header shrink, MSS can be upgraded. */
2824 skb_increase_gso_size(shinfo, len_diff);
2825 /* Header must be checked, and gso_segs recomputed. */
2826 shinfo->gso_type |= SKB_GSO_DODGY;
2827 shinfo->gso_segs = 0;
2833 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2835 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
2839 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2841 bool trans_same = skb->transport_header == skb->network_header;
2842 u32 len_cur, len_diff_abs = abs(len_diff);
2843 u32 len_min = bpf_skb_net_base_len(skb);
2844 u32 len_max = __bpf_skb_max_len(skb);
2845 __be16 proto = skb->protocol;
2846 bool shrink = len_diff < 0;
2849 if (unlikely(len_diff_abs > 0xfffU))
2851 if (unlikely(proto != htons(ETH_P_IP) &&
2852 proto != htons(ETH_P_IPV6)))
2855 len_cur = skb->len - skb_network_offset(skb);
2856 if (skb_transport_header_was_set(skb) && !trans_same)
2857 len_cur = skb_network_header_len(skb);
2858 if ((shrink && (len_diff_abs >= len_cur ||
2859 len_cur - len_diff_abs < len_min)) ||
2860 (!shrink && (skb->len + len_diff_abs > len_max &&
2864 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2865 bpf_skb_net_grow(skb, len_diff_abs);
2867 bpf_compute_data_pointers(skb);
2871 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2872 u32, mode, u64, flags)
2874 if (unlikely(flags))
2876 if (likely(mode == BPF_ADJ_ROOM_NET))
2877 return bpf_skb_adjust_net(skb, len_diff);
2882 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2883 .func = bpf_skb_adjust_room,
2885 .ret_type = RET_INTEGER,
2886 .arg1_type = ARG_PTR_TO_CTX,
2887 .arg2_type = ARG_ANYTHING,
2888 .arg3_type = ARG_ANYTHING,
2889 .arg4_type = ARG_ANYTHING,
2892 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2894 u32 min_len = skb_network_offset(skb);
2896 if (skb_transport_header_was_set(skb))
2897 min_len = skb_transport_offset(skb);
2898 if (skb->ip_summed == CHECKSUM_PARTIAL)
2899 min_len = skb_checksum_start_offset(skb) +
2900 skb->csum_offset + sizeof(__sum16);
2904 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2906 unsigned int old_len = skb->len;
2909 ret = __skb_grow_rcsum(skb, new_len);
2911 memset(skb->data + old_len, 0, new_len - old_len);
2915 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2917 return __skb_trim_rcsum(skb, new_len);
2920 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
2923 u32 max_len = __bpf_skb_max_len(skb);
2924 u32 min_len = __bpf_skb_min_len(skb);
2927 if (unlikely(flags || new_len > max_len || new_len < min_len))
2929 if (skb->encapsulation)
2932 /* The basic idea of this helper is that it's performing the
2933 * needed work to either grow or trim an skb, and eBPF program
2934 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2935 * bpf_lX_csum_replace() and others rather than passing a raw
2936 * buffer here. This one is a slow path helper and intended
2937 * for replies with control messages.
2939 * Like in bpf_skb_change_proto(), we want to keep this rather
2940 * minimal and without protocol specifics so that we are able
2941 * to separate concerns as in bpf_skb_store_bytes() should only
2942 * be the one responsible for writing buffers.
2944 * It's really expected to be a slow path operation here for
2945 * control message replies, so we're implicitly linearizing,
2946 * uncloning and drop offloads from the skb by this.
2948 ret = __bpf_try_make_writable(skb, skb->len);
2950 if (new_len > skb->len)
2951 ret = bpf_skb_grow_rcsum(skb, new_len);
2952 else if (new_len < skb->len)
2953 ret = bpf_skb_trim_rcsum(skb, new_len);
2954 if (!ret && skb_is_gso(skb))
2960 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2963 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2965 bpf_compute_data_pointers(skb);
2969 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2970 .func = bpf_skb_change_tail,
2972 .ret_type = RET_INTEGER,
2973 .arg1_type = ARG_PTR_TO_CTX,
2974 .arg2_type = ARG_ANYTHING,
2975 .arg3_type = ARG_ANYTHING,
2978 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2981 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2983 bpf_compute_data_end_sk_skb(skb);
2987 static const struct bpf_func_proto sk_skb_change_tail_proto = {
2988 .func = sk_skb_change_tail,
2990 .ret_type = RET_INTEGER,
2991 .arg1_type = ARG_PTR_TO_CTX,
2992 .arg2_type = ARG_ANYTHING,
2993 .arg3_type = ARG_ANYTHING,
2996 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
2999 u32 max_len = __bpf_skb_max_len(skb);
3000 u32 new_len = skb->len + head_room;
3003 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3004 new_len < skb->len))
3007 ret = skb_cow(skb, head_room);
3009 /* Idea for this helper is that we currently only
3010 * allow to expand on mac header. This means that
3011 * skb->protocol network header, etc, stay as is.
3012 * Compared to bpf_skb_change_tail(), we're more
3013 * flexible due to not needing to linearize or
3014 * reset GSO. Intention for this helper is to be
3015 * used by an L3 skb that needs to push mac header
3016 * for redirection into L2 device.
3018 __skb_push(skb, head_room);
3019 memset(skb->data, 0, head_room);
3020 skb_reset_mac_header(skb);
3026 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3029 int ret = __bpf_skb_change_head(skb, head_room, flags);
3031 bpf_compute_data_pointers(skb);
3035 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3036 .func = bpf_skb_change_head,
3038 .ret_type = RET_INTEGER,
3039 .arg1_type = ARG_PTR_TO_CTX,
3040 .arg2_type = ARG_ANYTHING,
3041 .arg3_type = ARG_ANYTHING,
3044 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3047 int ret = __bpf_skb_change_head(skb, head_room, flags);
3049 bpf_compute_data_end_sk_skb(skb);
3053 static const struct bpf_func_proto sk_skb_change_head_proto = {
3054 .func = sk_skb_change_head,
3056 .ret_type = RET_INTEGER,
3057 .arg1_type = ARG_PTR_TO_CTX,
3058 .arg2_type = ARG_ANYTHING,
3059 .arg3_type = ARG_ANYTHING,
3061 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3063 return xdp_data_meta_unsupported(xdp) ? 0 :
3064 xdp->data - xdp->data_meta;
3067 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3069 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3070 unsigned long metalen = xdp_get_metalen(xdp);
3071 void *data_start = xdp_frame_end + metalen;
3072 void *data = xdp->data + offset;
3074 if (unlikely(data < data_start ||
3075 data > xdp->data_end - ETH_HLEN))
3079 memmove(xdp->data_meta + offset,
3080 xdp->data_meta, metalen);
3081 xdp->data_meta += offset;
3087 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3088 .func = bpf_xdp_adjust_head,
3090 .ret_type = RET_INTEGER,
3091 .arg1_type = ARG_PTR_TO_CTX,
3092 .arg2_type = ARG_ANYTHING,
3095 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3097 void *data_end = xdp->data_end + offset;
3099 /* only shrinking is allowed for now. */
3100 if (unlikely(offset >= 0))
3103 if (unlikely(data_end < xdp->data + ETH_HLEN))
3106 xdp->data_end = data_end;
3111 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3112 .func = bpf_xdp_adjust_tail,
3114 .ret_type = RET_INTEGER,
3115 .arg1_type = ARG_PTR_TO_CTX,
3116 .arg2_type = ARG_ANYTHING,
3119 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3121 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3122 void *meta = xdp->data_meta + offset;
3123 unsigned long metalen = xdp->data - meta;
3125 if (xdp_data_meta_unsupported(xdp))
3127 if (unlikely(meta < xdp_frame_end ||
3130 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3134 xdp->data_meta = meta;
3139 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3140 .func = bpf_xdp_adjust_meta,
3142 .ret_type = RET_INTEGER,
3143 .arg1_type = ARG_PTR_TO_CTX,
3144 .arg2_type = ARG_ANYTHING,
3147 static int __bpf_tx_xdp(struct net_device *dev,
3148 struct bpf_map *map,
3149 struct xdp_buff *xdp,
3152 struct xdp_frame *xdpf;
3155 if (!dev->netdev_ops->ndo_xdp_xmit) {
3159 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3163 xdpf = convert_to_xdp_frame(xdp);
3164 if (unlikely(!xdpf))
3167 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3173 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3174 struct bpf_map *map,
3175 struct xdp_buff *xdp,
3180 switch (map->map_type) {
3181 case BPF_MAP_TYPE_DEVMAP: {
3182 struct bpf_dtab_netdev *dst = fwd;
3184 err = dev_map_enqueue(dst, xdp, dev_rx);
3187 __dev_map_insert_ctx(map, index);
3190 case BPF_MAP_TYPE_CPUMAP: {
3191 struct bpf_cpu_map_entry *rcpu = fwd;
3193 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3196 __cpu_map_insert_ctx(map, index);
3199 case BPF_MAP_TYPE_XSKMAP: {
3200 struct xdp_sock *xs = fwd;
3202 err = __xsk_map_redirect(map, xdp, xs);
3211 void xdp_do_flush_map(void)
3213 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3214 struct bpf_map *map = ri->map_to_flush;
3216 ri->map_to_flush = NULL;
3218 switch (map->map_type) {
3219 case BPF_MAP_TYPE_DEVMAP:
3220 __dev_map_flush(map);
3222 case BPF_MAP_TYPE_CPUMAP:
3223 __cpu_map_flush(map);
3225 case BPF_MAP_TYPE_XSKMAP:
3226 __xsk_map_flush(map);
3233 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3235 static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3237 switch (map->map_type) {
3238 case BPF_MAP_TYPE_DEVMAP:
3239 return __dev_map_lookup_elem(map, index);
3240 case BPF_MAP_TYPE_CPUMAP:
3241 return __cpu_map_lookup_elem(map, index);
3242 case BPF_MAP_TYPE_XSKMAP:
3243 return __xsk_map_lookup_elem(map, index);
3249 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
3252 return (unsigned long)xdp_prog->aux != aux;
3255 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3256 struct bpf_prog *xdp_prog)
3258 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3259 unsigned long map_owner = ri->map_owner;
3260 struct bpf_map *map = ri->map;
3261 u32 index = ri->ifindex;
3269 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3275 fwd = __xdp_map_lookup_elem(map, index);
3280 if (ri->map_to_flush && ri->map_to_flush != map)
3283 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3287 ri->map_to_flush = map;
3288 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3291 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3295 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3296 struct bpf_prog *xdp_prog)
3298 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3299 struct net_device *fwd;
3300 u32 index = ri->ifindex;
3304 return xdp_do_redirect_map(dev, xdp, xdp_prog);
3306 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3308 if (unlikely(!fwd)) {
3313 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3317 _trace_xdp_redirect(dev, xdp_prog, index);
3320 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3323 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3325 static int xdp_do_generic_redirect_map(struct net_device *dev,
3326 struct sk_buff *skb,
3327 struct xdp_buff *xdp,
3328 struct bpf_prog *xdp_prog)
3330 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3331 unsigned long map_owner = ri->map_owner;
3332 struct bpf_map *map = ri->map;
3333 u32 index = ri->ifindex;
3341 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3346 fwd = __xdp_map_lookup_elem(map, index);
3347 if (unlikely(!fwd)) {
3352 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3353 struct bpf_dtab_netdev *dst = fwd;
3355 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3358 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3359 struct xdp_sock *xs = fwd;
3361 err = xsk_generic_rcv(xs, xdp);
3366 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3371 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3374 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3378 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3379 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3381 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3382 u32 index = ri->ifindex;
3383 struct net_device *fwd;
3387 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog);
3390 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3391 if (unlikely(!fwd)) {
3396 err = xdp_ok_fwd_dev(fwd, skb->len);
3401 _trace_xdp_redirect(dev, xdp_prog, index);
3402 generic_xdp_tx(skb, xdp_prog);
3405 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3408 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3410 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3412 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3414 if (unlikely(flags))
3417 ri->ifindex = ifindex;
3422 return XDP_REDIRECT;
3425 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3426 .func = bpf_xdp_redirect,
3428 .ret_type = RET_INTEGER,
3429 .arg1_type = ARG_ANYTHING,
3430 .arg2_type = ARG_ANYTHING,
3433 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
3434 unsigned long, map_owner)
3436 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3438 if (unlikely(flags))
3441 ri->ifindex = ifindex;
3444 ri->map_owner = map_owner;
3446 return XDP_REDIRECT;
3449 /* Note, arg4 is hidden from users and populated by the verifier
3450 * with the right pointer.
3452 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3453 .func = bpf_xdp_redirect_map,
3455 .ret_type = RET_INTEGER,
3456 .arg1_type = ARG_CONST_MAP_PTR,
3457 .arg2_type = ARG_ANYTHING,
3458 .arg3_type = ARG_ANYTHING,
3461 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3462 unsigned long off, unsigned long len)
3464 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3468 if (ptr != dst_buff)
3469 memcpy(dst_buff, ptr, len);
3474 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3475 u64, flags, void *, meta, u64, meta_size)
3477 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3479 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3481 if (unlikely(skb_size > skb->len))
3484 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3488 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3489 .func = bpf_skb_event_output,
3491 .ret_type = RET_INTEGER,
3492 .arg1_type = ARG_PTR_TO_CTX,
3493 .arg2_type = ARG_CONST_MAP_PTR,
3494 .arg3_type = ARG_ANYTHING,
3495 .arg4_type = ARG_PTR_TO_MEM,
3496 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3499 static unsigned short bpf_tunnel_key_af(u64 flags)
3501 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3504 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3505 u32, size, u64, flags)
3507 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3508 u8 compat[sizeof(struct bpf_tunnel_key)];
3512 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3516 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3520 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3523 case offsetof(struct bpf_tunnel_key, tunnel_label):
3524 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3526 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3527 /* Fixup deprecated structure layouts here, so we have
3528 * a common path later on.
3530 if (ip_tunnel_info_af(info) != AF_INET)
3533 to = (struct bpf_tunnel_key *)compat;
3540 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3541 to->tunnel_tos = info->key.tos;
3542 to->tunnel_ttl = info->key.ttl;
3545 if (flags & BPF_F_TUNINFO_IPV6) {
3546 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3547 sizeof(to->remote_ipv6));
3548 to->tunnel_label = be32_to_cpu(info->key.label);
3550 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3551 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3552 to->tunnel_label = 0;
3555 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3556 memcpy(to_orig, to, size);
3560 memset(to_orig, 0, size);
3564 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3565 .func = bpf_skb_get_tunnel_key,
3567 .ret_type = RET_INTEGER,
3568 .arg1_type = ARG_PTR_TO_CTX,
3569 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3570 .arg3_type = ARG_CONST_SIZE,
3571 .arg4_type = ARG_ANYTHING,
3574 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3576 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3579 if (unlikely(!info ||
3580 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3584 if (unlikely(size < info->options_len)) {
3589 ip_tunnel_info_opts_get(to, info);
3590 if (size > info->options_len)
3591 memset(to + info->options_len, 0, size - info->options_len);
3593 return info->options_len;
3595 memset(to, 0, size);
3599 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3600 .func = bpf_skb_get_tunnel_opt,
3602 .ret_type = RET_INTEGER,
3603 .arg1_type = ARG_PTR_TO_CTX,
3604 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3605 .arg3_type = ARG_CONST_SIZE,
3608 static struct metadata_dst __percpu *md_dst;
3610 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3611 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3613 struct metadata_dst *md = this_cpu_ptr(md_dst);
3614 u8 compat[sizeof(struct bpf_tunnel_key)];
3615 struct ip_tunnel_info *info;
3617 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3618 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3620 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3622 case offsetof(struct bpf_tunnel_key, tunnel_label):
3623 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3624 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3625 /* Fixup deprecated structure layouts here, so we have
3626 * a common path later on.
3628 memcpy(compat, from, size);
3629 memset(compat + size, 0, sizeof(compat) - size);
3630 from = (const struct bpf_tunnel_key *) compat;
3636 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3641 dst_hold((struct dst_entry *) md);
3642 skb_dst_set(skb, (struct dst_entry *) md);
3644 info = &md->u.tun_info;
3645 memset(info, 0, sizeof(*info));
3646 info->mode = IP_TUNNEL_INFO_TX;
3648 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3649 if (flags & BPF_F_DONT_FRAGMENT)
3650 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3651 if (flags & BPF_F_ZERO_CSUM_TX)
3652 info->key.tun_flags &= ~TUNNEL_CSUM;
3653 if (flags & BPF_F_SEQ_NUMBER)
3654 info->key.tun_flags |= TUNNEL_SEQ;
3656 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3657 info->key.tos = from->tunnel_tos;
3658 info->key.ttl = from->tunnel_ttl;
3660 if (flags & BPF_F_TUNINFO_IPV6) {
3661 info->mode |= IP_TUNNEL_INFO_IPV6;
3662 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3663 sizeof(from->remote_ipv6));
3664 info->key.label = cpu_to_be32(from->tunnel_label) &
3665 IPV6_FLOWLABEL_MASK;
3667 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3673 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3674 .func = bpf_skb_set_tunnel_key,
3676 .ret_type = RET_INTEGER,
3677 .arg1_type = ARG_PTR_TO_CTX,
3678 .arg2_type = ARG_PTR_TO_MEM,
3679 .arg3_type = ARG_CONST_SIZE,
3680 .arg4_type = ARG_ANYTHING,
3683 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3684 const u8 *, from, u32, size)
3686 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3687 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3689 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3691 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3694 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
3699 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3700 .func = bpf_skb_set_tunnel_opt,
3702 .ret_type = RET_INTEGER,
3703 .arg1_type = ARG_PTR_TO_CTX,
3704 .arg2_type = ARG_PTR_TO_MEM,
3705 .arg3_type = ARG_CONST_SIZE,
3708 static const struct bpf_func_proto *
3709 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3712 struct metadata_dst __percpu *tmp;
3714 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3719 if (cmpxchg(&md_dst, NULL, tmp))
3720 metadata_dst_free_percpu(tmp);
3724 case BPF_FUNC_skb_set_tunnel_key:
3725 return &bpf_skb_set_tunnel_key_proto;
3726 case BPF_FUNC_skb_set_tunnel_opt:
3727 return &bpf_skb_set_tunnel_opt_proto;
3733 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3736 struct bpf_array *array = container_of(map, struct bpf_array, map);
3737 struct cgroup *cgrp;
3740 sk = skb_to_full_sk(skb);
3741 if (!sk || !sk_fullsock(sk))
3743 if (unlikely(idx >= array->map.max_entries))
3746 cgrp = READ_ONCE(array->ptrs[idx]);
3747 if (unlikely(!cgrp))
3750 return sk_under_cgroup_hierarchy(sk, cgrp);
3753 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3754 .func = bpf_skb_under_cgroup,
3756 .ret_type = RET_INTEGER,
3757 .arg1_type = ARG_PTR_TO_CTX,
3758 .arg2_type = ARG_CONST_MAP_PTR,
3759 .arg3_type = ARG_ANYTHING,
3762 #ifdef CONFIG_SOCK_CGROUP_DATA
3763 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
3765 struct sock *sk = skb_to_full_sk(skb);
3766 struct cgroup *cgrp;
3768 if (!sk || !sk_fullsock(sk))
3771 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3772 return cgrp->kn->id.id;
3775 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
3776 .func = bpf_skb_cgroup_id,
3778 .ret_type = RET_INTEGER,
3779 .arg1_type = ARG_PTR_TO_CTX,
3782 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
3785 struct sock *sk = skb_to_full_sk(skb);
3786 struct cgroup *ancestor;
3787 struct cgroup *cgrp;
3789 if (!sk || !sk_fullsock(sk))
3792 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3793 ancestor = cgroup_ancestor(cgrp, ancestor_level);
3797 return ancestor->kn->id.id;
3800 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
3801 .func = bpf_skb_ancestor_cgroup_id,
3803 .ret_type = RET_INTEGER,
3804 .arg1_type = ARG_PTR_TO_CTX,
3805 .arg2_type = ARG_ANYTHING,
3809 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3810 unsigned long off, unsigned long len)
3812 memcpy(dst_buff, src_buff + off, len);
3816 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3817 u64, flags, void *, meta, u64, meta_size)
3819 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3821 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3823 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3826 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3827 xdp_size, bpf_xdp_copy);
3830 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3831 .func = bpf_xdp_event_output,
3833 .ret_type = RET_INTEGER,
3834 .arg1_type = ARG_PTR_TO_CTX,
3835 .arg2_type = ARG_CONST_MAP_PTR,
3836 .arg3_type = ARG_ANYTHING,
3837 .arg4_type = ARG_PTR_TO_MEM,
3838 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3841 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3843 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3846 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3847 .func = bpf_get_socket_cookie,
3849 .ret_type = RET_INTEGER,
3850 .arg1_type = ARG_PTR_TO_CTX,
3853 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
3855 return sock_gen_cookie(ctx->sk);
3858 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
3859 .func = bpf_get_socket_cookie_sock_addr,
3861 .ret_type = RET_INTEGER,
3862 .arg1_type = ARG_PTR_TO_CTX,
3865 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
3867 return sock_gen_cookie(ctx->sk);
3870 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
3871 .func = bpf_get_socket_cookie_sock_ops,
3873 .ret_type = RET_INTEGER,
3874 .arg1_type = ARG_PTR_TO_CTX,
3877 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3879 struct sock *sk = sk_to_full_sk(skb->sk);
3882 if (!sk || !sk_fullsock(sk))
3884 kuid = sock_net_uid(sock_net(sk), sk);
3885 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3888 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3889 .func = bpf_get_socket_uid,
3891 .ret_type = RET_INTEGER,
3892 .arg1_type = ARG_PTR_TO_CTX,
3895 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3896 int, level, int, optname, char *, optval, int, optlen)
3898 struct sock *sk = bpf_sock->sk;
3902 if (!sk_fullsock(sk))
3905 if (level == SOL_SOCKET) {
3906 if (optlen != sizeof(int))
3908 val = *((int *)optval);
3910 /* Only some socketops are supported */
3913 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3914 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3917 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3918 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3920 case SO_MAX_PACING_RATE:
3921 sk->sk_max_pacing_rate = val;
3922 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3923 sk->sk_max_pacing_rate);
3926 sk->sk_priority = val;
3931 sk->sk_rcvlowat = val ? : 1;
3940 } else if (level == SOL_IP) {
3941 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3944 val = *((int *)optval);
3945 /* Only some options are supported */
3948 if (val < -1 || val > 0xff) {
3951 struct inet_sock *inet = inet_sk(sk);
3961 #if IS_ENABLED(CONFIG_IPV6)
3962 } else if (level == SOL_IPV6) {
3963 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3966 val = *((int *)optval);
3967 /* Only some options are supported */
3970 if (val < -1 || val > 0xff) {
3973 struct ipv6_pinfo *np = inet6_sk(sk);
3984 } else if (level == SOL_TCP &&
3985 sk->sk_prot->setsockopt == tcp_setsockopt) {
3986 if (optname == TCP_CONGESTION) {
3987 char name[TCP_CA_NAME_MAX];
3988 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3990 strncpy(name, optval, min_t(long, optlen,
3991 TCP_CA_NAME_MAX-1));
3992 name[TCP_CA_NAME_MAX-1] = 0;
3993 ret = tcp_set_congestion_control(sk, name, false,
3996 struct tcp_sock *tp = tcp_sk(sk);
3998 if (optlen != sizeof(int))
4001 val = *((int *)optval);
4002 /* Only some options are supported */
4005 if (val <= 0 || tp->data_segs_out > 0)
4010 case TCP_BPF_SNDCWND_CLAMP:
4014 tp->snd_cwnd_clamp = val;
4015 tp->snd_ssthresh = val;
4029 static const struct bpf_func_proto bpf_setsockopt_proto = {
4030 .func = bpf_setsockopt,
4032 .ret_type = RET_INTEGER,
4033 .arg1_type = ARG_PTR_TO_CTX,
4034 .arg2_type = ARG_ANYTHING,
4035 .arg3_type = ARG_ANYTHING,
4036 .arg4_type = ARG_PTR_TO_MEM,
4037 .arg5_type = ARG_CONST_SIZE,
4040 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4041 int, level, int, optname, char *, optval, int, optlen)
4043 struct sock *sk = bpf_sock->sk;
4045 if (!sk_fullsock(sk))
4049 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4050 if (optname == TCP_CONGESTION) {
4051 struct inet_connection_sock *icsk = inet_csk(sk);
4053 if (!icsk->icsk_ca_ops || optlen <= 1)
4055 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4056 optval[optlen - 1] = 0;
4060 } else if (level == SOL_IP) {
4061 struct inet_sock *inet = inet_sk(sk);
4063 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4066 /* Only some options are supported */
4069 *((int *)optval) = (int)inet->tos;
4074 #if IS_ENABLED(CONFIG_IPV6)
4075 } else if (level == SOL_IPV6) {
4076 struct ipv6_pinfo *np = inet6_sk(sk);
4078 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4081 /* Only some options are supported */
4084 *((int *)optval) = (int)np->tclass;
4096 memset(optval, 0, optlen);
4100 static const struct bpf_func_proto bpf_getsockopt_proto = {
4101 .func = bpf_getsockopt,
4103 .ret_type = RET_INTEGER,
4104 .arg1_type = ARG_PTR_TO_CTX,
4105 .arg2_type = ARG_ANYTHING,
4106 .arg3_type = ARG_ANYTHING,
4107 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4108 .arg5_type = ARG_CONST_SIZE,
4111 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4114 struct sock *sk = bpf_sock->sk;
4115 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4117 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4121 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4123 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4126 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4127 .func = bpf_sock_ops_cb_flags_set,
4129 .ret_type = RET_INTEGER,
4130 .arg1_type = ARG_PTR_TO_CTX,
4131 .arg2_type = ARG_ANYTHING,
4134 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4135 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4137 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4141 struct sock *sk = ctx->sk;
4144 /* Binding to port can be expensive so it's prohibited in the helper.
4145 * Only binding to IP is supported.
4148 if (addr->sa_family == AF_INET) {
4149 if (addr_len < sizeof(struct sockaddr_in))
4151 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4153 return __inet_bind(sk, addr, addr_len, true, false);
4154 #if IS_ENABLED(CONFIG_IPV6)
4155 } else if (addr->sa_family == AF_INET6) {
4156 if (addr_len < SIN6_LEN_RFC2133)
4158 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4160 /* ipv6_bpf_stub cannot be NULL, since it's called from
4161 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4163 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4164 #endif /* CONFIG_IPV6 */
4166 #endif /* CONFIG_INET */
4168 return -EAFNOSUPPORT;
4171 static const struct bpf_func_proto bpf_bind_proto = {
4174 .ret_type = RET_INTEGER,
4175 .arg1_type = ARG_PTR_TO_CTX,
4176 .arg2_type = ARG_PTR_TO_MEM,
4177 .arg3_type = ARG_CONST_SIZE,
4181 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4182 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4184 const struct sec_path *sp = skb_sec_path(skb);
4185 const struct xfrm_state *x;
4187 if (!sp || unlikely(index >= sp->len || flags))
4190 x = sp->xvec[index];
4192 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4195 to->reqid = x->props.reqid;
4196 to->spi = x->id.spi;
4197 to->family = x->props.family;
4200 if (to->family == AF_INET6) {
4201 memcpy(to->remote_ipv6, x->props.saddr.a6,
4202 sizeof(to->remote_ipv6));
4204 to->remote_ipv4 = x->props.saddr.a4;
4205 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4210 memset(to, 0, size);
4214 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4215 .func = bpf_skb_get_xfrm_state,
4217 .ret_type = RET_INTEGER,
4218 .arg1_type = ARG_PTR_TO_CTX,
4219 .arg2_type = ARG_ANYTHING,
4220 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4221 .arg4_type = ARG_CONST_SIZE,
4222 .arg5_type = ARG_ANYTHING,
4226 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4227 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4228 const struct neighbour *neigh,
4229 const struct net_device *dev)
4231 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4232 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4233 params->h_vlan_TCI = 0;
4234 params->h_vlan_proto = 0;
4235 params->ifindex = dev->ifindex;
4241 #if IS_ENABLED(CONFIG_INET)
4242 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4243 u32 flags, bool check_mtu)
4245 struct in_device *in_dev;
4246 struct neighbour *neigh;
4247 struct net_device *dev;
4248 struct fib_result res;
4254 dev = dev_get_by_index_rcu(net, params->ifindex);
4258 /* verify forwarding is enabled on this interface */
4259 in_dev = __in_dev_get_rcu(dev);
4260 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4261 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4263 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4265 fl4.flowi4_oif = params->ifindex;
4267 fl4.flowi4_iif = params->ifindex;
4270 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4271 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4272 fl4.flowi4_flags = 0;
4274 fl4.flowi4_proto = params->l4_protocol;
4275 fl4.daddr = params->ipv4_dst;
4276 fl4.saddr = params->ipv4_src;
4277 fl4.fl4_sport = params->sport;
4278 fl4.fl4_dport = params->dport;
4280 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4281 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4282 struct fib_table *tb;
4284 tb = fib_get_table(net, tbid);
4286 return BPF_FIB_LKUP_RET_NOT_FWDED;
4288 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4290 fl4.flowi4_mark = 0;
4291 fl4.flowi4_secid = 0;
4292 fl4.flowi4_tun_key.tun_id = 0;
4293 fl4.flowi4_uid = sock_net_uid(net, NULL);
4295 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4299 /* map fib lookup errors to RTN_ type */
4301 return BPF_FIB_LKUP_RET_BLACKHOLE;
4302 if (err == -EHOSTUNREACH)
4303 return BPF_FIB_LKUP_RET_UNREACHABLE;
4305 return BPF_FIB_LKUP_RET_PROHIBIT;
4307 return BPF_FIB_LKUP_RET_NOT_FWDED;
4310 if (res.type != RTN_UNICAST)
4311 return BPF_FIB_LKUP_RET_NOT_FWDED;
4313 if (res.fi->fib_nhs > 1)
4314 fib_select_path(net, &res, &fl4, NULL);
4317 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4318 if (params->tot_len > mtu)
4319 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4322 nh = &res.fi->fib_nh[res.nh_sel];
4324 /* do not handle lwt encaps right now */
4325 if (nh->nh_lwtstate)
4326 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4330 params->ipv4_dst = nh->nh_gw;
4332 params->rt_metric = res.fi->fib_priority;
4334 /* xdp and cls_bpf programs are run in RCU-bh so
4335 * rcu_read_lock_bh is not needed here
4337 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4339 return BPF_FIB_LKUP_RET_NO_NEIGH;
4341 return bpf_fib_set_fwd_params(params, neigh, dev);
4345 #if IS_ENABLED(CONFIG_IPV6)
4346 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4347 u32 flags, bool check_mtu)
4349 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4350 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4351 struct neighbour *neigh;
4352 struct net_device *dev;
4353 struct inet6_dev *idev;
4354 struct fib6_info *f6i;
4360 /* link local addresses are never forwarded */
4361 if (rt6_need_strict(dst) || rt6_need_strict(src))
4362 return BPF_FIB_LKUP_RET_NOT_FWDED;
4364 dev = dev_get_by_index_rcu(net, params->ifindex);
4368 idev = __in6_dev_get_safely(dev);
4369 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4370 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4372 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4374 oif = fl6.flowi6_oif = params->ifindex;
4376 oif = fl6.flowi6_iif = params->ifindex;
4378 strict = RT6_LOOKUP_F_HAS_SADDR;
4380 fl6.flowlabel = params->flowinfo;
4381 fl6.flowi6_scope = 0;
4382 fl6.flowi6_flags = 0;
4385 fl6.flowi6_proto = params->l4_protocol;
4388 fl6.fl6_sport = params->sport;
4389 fl6.fl6_dport = params->dport;
4391 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4392 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4393 struct fib6_table *tb;
4395 tb = ipv6_stub->fib6_get_table(net, tbid);
4397 return BPF_FIB_LKUP_RET_NOT_FWDED;
4399 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4401 fl6.flowi6_mark = 0;
4402 fl6.flowi6_secid = 0;
4403 fl6.flowi6_tun_key.tun_id = 0;
4404 fl6.flowi6_uid = sock_net_uid(net, NULL);
4406 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4409 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4410 return BPF_FIB_LKUP_RET_NOT_FWDED;
4412 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4413 switch (f6i->fib6_type) {
4415 return BPF_FIB_LKUP_RET_BLACKHOLE;
4416 case RTN_UNREACHABLE:
4417 return BPF_FIB_LKUP_RET_UNREACHABLE;
4419 return BPF_FIB_LKUP_RET_PROHIBIT;
4421 return BPF_FIB_LKUP_RET_NOT_FWDED;
4425 if (f6i->fib6_type != RTN_UNICAST)
4426 return BPF_FIB_LKUP_RET_NOT_FWDED;
4428 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4429 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4430 fl6.flowi6_oif, NULL,
4434 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4435 if (params->tot_len > mtu)
4436 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4439 if (f6i->fib6_nh.nh_lwtstate)
4440 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4442 if (f6i->fib6_flags & RTF_GATEWAY)
4443 *dst = f6i->fib6_nh.nh_gw;
4445 dev = f6i->fib6_nh.nh_dev;
4446 params->rt_metric = f6i->fib6_metric;
4448 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4449 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4450 * because we need to get nd_tbl via the stub
4452 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4453 ndisc_hashfn, dst, dev);
4455 return BPF_FIB_LKUP_RET_NO_NEIGH;
4457 return bpf_fib_set_fwd_params(params, neigh, dev);
4461 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4462 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4464 if (plen < sizeof(*params))
4467 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4470 switch (params->family) {
4471 #if IS_ENABLED(CONFIG_INET)
4473 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4476 #if IS_ENABLED(CONFIG_IPV6)
4478 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4482 return -EAFNOSUPPORT;
4485 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4486 .func = bpf_xdp_fib_lookup,
4488 .ret_type = RET_INTEGER,
4489 .arg1_type = ARG_PTR_TO_CTX,
4490 .arg2_type = ARG_PTR_TO_MEM,
4491 .arg3_type = ARG_CONST_SIZE,
4492 .arg4_type = ARG_ANYTHING,
4495 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4496 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4498 struct net *net = dev_net(skb->dev);
4499 int rc = -EAFNOSUPPORT;
4501 if (plen < sizeof(*params))
4504 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4507 switch (params->family) {
4508 #if IS_ENABLED(CONFIG_INET)
4510 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4513 #if IS_ENABLED(CONFIG_IPV6)
4515 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4521 struct net_device *dev;
4523 dev = dev_get_by_index_rcu(net, params->ifindex);
4524 if (!is_skb_forwardable(dev, skb))
4525 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4531 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4532 .func = bpf_skb_fib_lookup,
4534 .ret_type = RET_INTEGER,
4535 .arg1_type = ARG_PTR_TO_CTX,
4536 .arg2_type = ARG_PTR_TO_MEM,
4537 .arg3_type = ARG_CONST_SIZE,
4538 .arg4_type = ARG_ANYTHING,
4541 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4542 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4545 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4547 if (!seg6_validate_srh(srh, len))
4551 case BPF_LWT_ENCAP_SEG6_INLINE:
4552 if (skb->protocol != htons(ETH_P_IPV6))
4555 err = seg6_do_srh_inline(skb, srh);
4557 case BPF_LWT_ENCAP_SEG6:
4558 skb_reset_inner_headers(skb);
4559 skb->encapsulation = 1;
4560 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4566 bpf_compute_data_pointers(skb);
4570 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4571 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4573 return seg6_lookup_nexthop(skb, NULL, 0);
4575 #endif /* CONFIG_IPV6_SEG6_BPF */
4577 BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4581 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4582 case BPF_LWT_ENCAP_SEG6:
4583 case BPF_LWT_ENCAP_SEG6_INLINE:
4584 return bpf_push_seg6_encap(skb, type, hdr, len);
4591 static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4592 .func = bpf_lwt_push_encap,
4594 .ret_type = RET_INTEGER,
4595 .arg1_type = ARG_PTR_TO_CTX,
4596 .arg2_type = ARG_ANYTHING,
4597 .arg3_type = ARG_PTR_TO_MEM,
4598 .arg4_type = ARG_CONST_SIZE
4601 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4602 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4603 const void *, from, u32, len)
4605 struct seg6_bpf_srh_state *srh_state =
4606 this_cpu_ptr(&seg6_bpf_srh_states);
4607 struct ipv6_sr_hdr *srh = srh_state->srh;
4608 void *srh_tlvs, *srh_end, *ptr;
4614 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4615 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4617 ptr = skb->data + offset;
4618 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4619 srh_state->valid = false;
4620 else if (ptr < (void *)&srh->flags ||
4621 ptr + len > (void *)&srh->segments)
4624 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4626 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4628 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4630 memcpy(skb->data + offset, from, len);
4634 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4635 .func = bpf_lwt_seg6_store_bytes,
4637 .ret_type = RET_INTEGER,
4638 .arg1_type = ARG_PTR_TO_CTX,
4639 .arg2_type = ARG_ANYTHING,
4640 .arg3_type = ARG_PTR_TO_MEM,
4641 .arg4_type = ARG_CONST_SIZE
4644 static void bpf_update_srh_state(struct sk_buff *skb)
4646 struct seg6_bpf_srh_state *srh_state =
4647 this_cpu_ptr(&seg6_bpf_srh_states);
4650 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
4651 srh_state->srh = NULL;
4653 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4654 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
4655 srh_state->valid = true;
4659 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4660 u32, action, void *, param, u32, param_len)
4662 struct seg6_bpf_srh_state *srh_state =
4663 this_cpu_ptr(&seg6_bpf_srh_states);
4668 case SEG6_LOCAL_ACTION_END_X:
4669 if (!seg6_bpf_has_valid_srh(skb))
4671 if (param_len != sizeof(struct in6_addr))
4673 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4674 case SEG6_LOCAL_ACTION_END_T:
4675 if (!seg6_bpf_has_valid_srh(skb))
4677 if (param_len != sizeof(int))
4679 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4680 case SEG6_LOCAL_ACTION_END_DT6:
4681 if (!seg6_bpf_has_valid_srh(skb))
4683 if (param_len != sizeof(int))
4686 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
4688 if (!pskb_pull(skb, hdroff))
4691 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
4692 skb_reset_network_header(skb);
4693 skb_reset_transport_header(skb);
4694 skb->encapsulation = 0;
4696 bpf_compute_data_pointers(skb);
4697 bpf_update_srh_state(skb);
4698 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4699 case SEG6_LOCAL_ACTION_END_B6:
4700 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4702 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4705 bpf_update_srh_state(skb);
4708 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
4709 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4711 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4714 bpf_update_srh_state(skb);
4722 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4723 .func = bpf_lwt_seg6_action,
4725 .ret_type = RET_INTEGER,
4726 .arg1_type = ARG_PTR_TO_CTX,
4727 .arg2_type = ARG_ANYTHING,
4728 .arg3_type = ARG_PTR_TO_MEM,
4729 .arg4_type = ARG_CONST_SIZE
4732 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4735 struct seg6_bpf_srh_state *srh_state =
4736 this_cpu_ptr(&seg6_bpf_srh_states);
4737 struct ipv6_sr_hdr *srh = srh_state->srh;
4738 void *srh_end, *srh_tlvs, *ptr;
4739 struct ipv6hdr *hdr;
4743 if (unlikely(srh == NULL))
4746 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4747 ((srh->first_segment + 1) << 4));
4748 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4750 ptr = skb->data + offset;
4752 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4754 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4758 ret = skb_cow_head(skb, len);
4759 if (unlikely(ret < 0))
4762 ret = bpf_skb_net_hdr_push(skb, offset, len);
4764 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4767 bpf_compute_data_pointers(skb);
4768 if (unlikely(ret < 0))
4771 hdr = (struct ipv6hdr *)skb->data;
4772 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4774 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4776 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4777 srh_state->hdrlen += len;
4778 srh_state->valid = false;
4782 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
4783 .func = bpf_lwt_seg6_adjust_srh,
4785 .ret_type = RET_INTEGER,
4786 .arg1_type = ARG_PTR_TO_CTX,
4787 .arg2_type = ARG_ANYTHING,
4788 .arg3_type = ARG_ANYTHING,
4790 #endif /* CONFIG_IPV6_SEG6_BPF */
4792 bool bpf_helper_changes_pkt_data(void *func)
4794 if (func == bpf_skb_vlan_push ||
4795 func == bpf_skb_vlan_pop ||
4796 func == bpf_skb_store_bytes ||
4797 func == bpf_skb_change_proto ||
4798 func == bpf_skb_change_head ||
4799 func == sk_skb_change_head ||
4800 func == bpf_skb_change_tail ||
4801 func == sk_skb_change_tail ||
4802 func == bpf_skb_adjust_room ||
4803 func == bpf_skb_pull_data ||
4804 func == sk_skb_pull_data ||
4805 func == bpf_clone_redirect ||
4806 func == bpf_l3_csum_replace ||
4807 func == bpf_l4_csum_replace ||
4808 func == bpf_xdp_adjust_head ||
4809 func == bpf_xdp_adjust_meta ||
4810 func == bpf_msg_pull_data ||
4811 func == bpf_xdp_adjust_tail ||
4812 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4813 func == bpf_lwt_seg6_store_bytes ||
4814 func == bpf_lwt_seg6_adjust_srh ||
4815 func == bpf_lwt_seg6_action ||
4817 func == bpf_lwt_push_encap)
4823 static const struct bpf_func_proto *
4824 bpf_base_func_proto(enum bpf_func_id func_id)
4827 case BPF_FUNC_map_lookup_elem:
4828 return &bpf_map_lookup_elem_proto;
4829 case BPF_FUNC_map_update_elem:
4830 return &bpf_map_update_elem_proto;
4831 case BPF_FUNC_map_delete_elem:
4832 return &bpf_map_delete_elem_proto;
4833 case BPF_FUNC_get_prandom_u32:
4834 return &bpf_get_prandom_u32_proto;
4835 case BPF_FUNC_get_smp_processor_id:
4836 return &bpf_get_raw_smp_processor_id_proto;
4837 case BPF_FUNC_get_numa_node_id:
4838 return &bpf_get_numa_node_id_proto;
4839 case BPF_FUNC_tail_call:
4840 return &bpf_tail_call_proto;
4841 case BPF_FUNC_ktime_get_ns:
4842 return &bpf_ktime_get_ns_proto;
4843 case BPF_FUNC_trace_printk:
4844 if (capable(CAP_SYS_ADMIN))
4845 return bpf_get_trace_printk_proto();
4846 /* else: fall through */
4852 static const struct bpf_func_proto *
4853 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4856 /* inet and inet6 sockets are created in a process
4857 * context so there is always a valid uid/gid
4859 case BPF_FUNC_get_current_uid_gid:
4860 return &bpf_get_current_uid_gid_proto;
4861 case BPF_FUNC_get_local_storage:
4862 return &bpf_get_local_storage_proto;
4864 return bpf_base_func_proto(func_id);
4868 static const struct bpf_func_proto *
4869 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4872 /* inet and inet6 sockets are created in a process
4873 * context so there is always a valid uid/gid
4875 case BPF_FUNC_get_current_uid_gid:
4876 return &bpf_get_current_uid_gid_proto;
4878 switch (prog->expected_attach_type) {
4879 case BPF_CGROUP_INET4_CONNECT:
4880 case BPF_CGROUP_INET6_CONNECT:
4881 return &bpf_bind_proto;
4885 case BPF_FUNC_get_socket_cookie:
4886 return &bpf_get_socket_cookie_sock_addr_proto;
4887 case BPF_FUNC_get_local_storage:
4888 return &bpf_get_local_storage_proto;
4890 return bpf_base_func_proto(func_id);
4894 static const struct bpf_func_proto *
4895 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4898 case BPF_FUNC_skb_load_bytes:
4899 return &bpf_skb_load_bytes_proto;
4900 case BPF_FUNC_skb_load_bytes_relative:
4901 return &bpf_skb_load_bytes_relative_proto;
4902 case BPF_FUNC_get_socket_cookie:
4903 return &bpf_get_socket_cookie_proto;
4904 case BPF_FUNC_get_socket_uid:
4905 return &bpf_get_socket_uid_proto;
4907 return bpf_base_func_proto(func_id);
4911 static const struct bpf_func_proto *
4912 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4915 case BPF_FUNC_get_local_storage:
4916 return &bpf_get_local_storage_proto;
4918 return sk_filter_func_proto(func_id, prog);
4922 static const struct bpf_func_proto *
4923 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4926 case BPF_FUNC_skb_store_bytes:
4927 return &bpf_skb_store_bytes_proto;
4928 case BPF_FUNC_skb_load_bytes:
4929 return &bpf_skb_load_bytes_proto;
4930 case BPF_FUNC_skb_load_bytes_relative:
4931 return &bpf_skb_load_bytes_relative_proto;
4932 case BPF_FUNC_skb_pull_data:
4933 return &bpf_skb_pull_data_proto;
4934 case BPF_FUNC_csum_diff:
4935 return &bpf_csum_diff_proto;
4936 case BPF_FUNC_csum_update:
4937 return &bpf_csum_update_proto;
4938 case BPF_FUNC_l3_csum_replace:
4939 return &bpf_l3_csum_replace_proto;
4940 case BPF_FUNC_l4_csum_replace:
4941 return &bpf_l4_csum_replace_proto;
4942 case BPF_FUNC_clone_redirect:
4943 return &bpf_clone_redirect_proto;
4944 case BPF_FUNC_get_cgroup_classid:
4945 return &bpf_get_cgroup_classid_proto;
4946 case BPF_FUNC_skb_vlan_push:
4947 return &bpf_skb_vlan_push_proto;
4948 case BPF_FUNC_skb_vlan_pop:
4949 return &bpf_skb_vlan_pop_proto;
4950 case BPF_FUNC_skb_change_proto:
4951 return &bpf_skb_change_proto_proto;
4952 case BPF_FUNC_skb_change_type:
4953 return &bpf_skb_change_type_proto;
4954 case BPF_FUNC_skb_adjust_room:
4955 return &bpf_skb_adjust_room_proto;
4956 case BPF_FUNC_skb_change_tail:
4957 return &bpf_skb_change_tail_proto;
4958 case BPF_FUNC_skb_get_tunnel_key:
4959 return &bpf_skb_get_tunnel_key_proto;
4960 case BPF_FUNC_skb_set_tunnel_key:
4961 return bpf_get_skb_set_tunnel_proto(func_id);
4962 case BPF_FUNC_skb_get_tunnel_opt:
4963 return &bpf_skb_get_tunnel_opt_proto;
4964 case BPF_FUNC_skb_set_tunnel_opt:
4965 return bpf_get_skb_set_tunnel_proto(func_id);
4966 case BPF_FUNC_redirect:
4967 return &bpf_redirect_proto;
4968 case BPF_FUNC_get_route_realm:
4969 return &bpf_get_route_realm_proto;
4970 case BPF_FUNC_get_hash_recalc:
4971 return &bpf_get_hash_recalc_proto;
4972 case BPF_FUNC_set_hash_invalid:
4973 return &bpf_set_hash_invalid_proto;
4974 case BPF_FUNC_set_hash:
4975 return &bpf_set_hash_proto;
4976 case BPF_FUNC_perf_event_output:
4977 return &bpf_skb_event_output_proto;
4978 case BPF_FUNC_get_smp_processor_id:
4979 return &bpf_get_smp_processor_id_proto;
4980 case BPF_FUNC_skb_under_cgroup:
4981 return &bpf_skb_under_cgroup_proto;
4982 case BPF_FUNC_get_socket_cookie:
4983 return &bpf_get_socket_cookie_proto;
4984 case BPF_FUNC_get_socket_uid:
4985 return &bpf_get_socket_uid_proto;
4986 case BPF_FUNC_fib_lookup:
4987 return &bpf_skb_fib_lookup_proto;
4989 case BPF_FUNC_skb_get_xfrm_state:
4990 return &bpf_skb_get_xfrm_state_proto;
4992 #ifdef CONFIG_SOCK_CGROUP_DATA
4993 case BPF_FUNC_skb_cgroup_id:
4994 return &bpf_skb_cgroup_id_proto;
4995 case BPF_FUNC_skb_ancestor_cgroup_id:
4996 return &bpf_skb_ancestor_cgroup_id_proto;
4999 return bpf_base_func_proto(func_id);
5003 static const struct bpf_func_proto *
5004 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5007 case BPF_FUNC_perf_event_output:
5008 return &bpf_xdp_event_output_proto;
5009 case BPF_FUNC_get_smp_processor_id:
5010 return &bpf_get_smp_processor_id_proto;
5011 case BPF_FUNC_csum_diff:
5012 return &bpf_csum_diff_proto;
5013 case BPF_FUNC_xdp_adjust_head:
5014 return &bpf_xdp_adjust_head_proto;
5015 case BPF_FUNC_xdp_adjust_meta:
5016 return &bpf_xdp_adjust_meta_proto;
5017 case BPF_FUNC_redirect:
5018 return &bpf_xdp_redirect_proto;
5019 case BPF_FUNC_redirect_map:
5020 return &bpf_xdp_redirect_map_proto;
5021 case BPF_FUNC_xdp_adjust_tail:
5022 return &bpf_xdp_adjust_tail_proto;
5023 case BPF_FUNC_fib_lookup:
5024 return &bpf_xdp_fib_lookup_proto;
5026 return bpf_base_func_proto(func_id);
5030 static const struct bpf_func_proto *
5031 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5034 case BPF_FUNC_setsockopt:
5035 return &bpf_setsockopt_proto;
5036 case BPF_FUNC_getsockopt:
5037 return &bpf_getsockopt_proto;
5038 case BPF_FUNC_sock_ops_cb_flags_set:
5039 return &bpf_sock_ops_cb_flags_set_proto;
5040 case BPF_FUNC_sock_map_update:
5041 return &bpf_sock_map_update_proto;
5042 case BPF_FUNC_sock_hash_update:
5043 return &bpf_sock_hash_update_proto;
5044 case BPF_FUNC_get_socket_cookie:
5045 return &bpf_get_socket_cookie_sock_ops_proto;
5046 case BPF_FUNC_get_local_storage:
5047 return &bpf_get_local_storage_proto;
5049 return bpf_base_func_proto(func_id);
5053 static const struct bpf_func_proto *
5054 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5057 case BPF_FUNC_msg_redirect_map:
5058 return &bpf_msg_redirect_map_proto;
5059 case BPF_FUNC_msg_redirect_hash:
5060 return &bpf_msg_redirect_hash_proto;
5061 case BPF_FUNC_msg_apply_bytes:
5062 return &bpf_msg_apply_bytes_proto;
5063 case BPF_FUNC_msg_cork_bytes:
5064 return &bpf_msg_cork_bytes_proto;
5065 case BPF_FUNC_msg_pull_data:
5066 return &bpf_msg_pull_data_proto;
5067 case BPF_FUNC_get_local_storage:
5068 return &bpf_get_local_storage_proto;
5070 return bpf_base_func_proto(func_id);
5074 static const struct bpf_func_proto *
5075 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5078 case BPF_FUNC_skb_store_bytes:
5079 return &bpf_skb_store_bytes_proto;
5080 case BPF_FUNC_skb_load_bytes:
5081 return &bpf_skb_load_bytes_proto;
5082 case BPF_FUNC_skb_pull_data:
5083 return &sk_skb_pull_data_proto;
5084 case BPF_FUNC_skb_change_tail:
5085 return &sk_skb_change_tail_proto;
5086 case BPF_FUNC_skb_change_head:
5087 return &sk_skb_change_head_proto;
5088 case BPF_FUNC_get_socket_cookie:
5089 return &bpf_get_socket_cookie_proto;
5090 case BPF_FUNC_get_socket_uid:
5091 return &bpf_get_socket_uid_proto;
5092 case BPF_FUNC_sk_redirect_map:
5093 return &bpf_sk_redirect_map_proto;
5094 case BPF_FUNC_sk_redirect_hash:
5095 return &bpf_sk_redirect_hash_proto;
5096 case BPF_FUNC_get_local_storage:
5097 return &bpf_get_local_storage_proto;
5099 return bpf_base_func_proto(func_id);
5103 static const struct bpf_func_proto *
5104 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5107 case BPF_FUNC_skb_load_bytes:
5108 return &bpf_skb_load_bytes_proto;
5109 case BPF_FUNC_skb_pull_data:
5110 return &bpf_skb_pull_data_proto;
5111 case BPF_FUNC_csum_diff:
5112 return &bpf_csum_diff_proto;
5113 case BPF_FUNC_get_cgroup_classid:
5114 return &bpf_get_cgroup_classid_proto;
5115 case BPF_FUNC_get_route_realm:
5116 return &bpf_get_route_realm_proto;
5117 case BPF_FUNC_get_hash_recalc:
5118 return &bpf_get_hash_recalc_proto;
5119 case BPF_FUNC_perf_event_output:
5120 return &bpf_skb_event_output_proto;
5121 case BPF_FUNC_get_smp_processor_id:
5122 return &bpf_get_smp_processor_id_proto;
5123 case BPF_FUNC_skb_under_cgroup:
5124 return &bpf_skb_under_cgroup_proto;
5126 return bpf_base_func_proto(func_id);
5130 static const struct bpf_func_proto *
5131 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5134 case BPF_FUNC_lwt_push_encap:
5135 return &bpf_lwt_push_encap_proto;
5137 return lwt_out_func_proto(func_id, prog);
5141 static const struct bpf_func_proto *
5142 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5145 case BPF_FUNC_skb_get_tunnel_key:
5146 return &bpf_skb_get_tunnel_key_proto;
5147 case BPF_FUNC_skb_set_tunnel_key:
5148 return bpf_get_skb_set_tunnel_proto(func_id);
5149 case BPF_FUNC_skb_get_tunnel_opt:
5150 return &bpf_skb_get_tunnel_opt_proto;
5151 case BPF_FUNC_skb_set_tunnel_opt:
5152 return bpf_get_skb_set_tunnel_proto(func_id);
5153 case BPF_FUNC_redirect:
5154 return &bpf_redirect_proto;
5155 case BPF_FUNC_clone_redirect:
5156 return &bpf_clone_redirect_proto;
5157 case BPF_FUNC_skb_change_tail:
5158 return &bpf_skb_change_tail_proto;
5159 case BPF_FUNC_skb_change_head:
5160 return &bpf_skb_change_head_proto;
5161 case BPF_FUNC_skb_store_bytes:
5162 return &bpf_skb_store_bytes_proto;
5163 case BPF_FUNC_csum_update:
5164 return &bpf_csum_update_proto;
5165 case BPF_FUNC_l3_csum_replace:
5166 return &bpf_l3_csum_replace_proto;
5167 case BPF_FUNC_l4_csum_replace:
5168 return &bpf_l4_csum_replace_proto;
5169 case BPF_FUNC_set_hash_invalid:
5170 return &bpf_set_hash_invalid_proto;
5172 return lwt_out_func_proto(func_id, prog);
5176 static const struct bpf_func_proto *
5177 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5180 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5181 case BPF_FUNC_lwt_seg6_store_bytes:
5182 return &bpf_lwt_seg6_store_bytes_proto;
5183 case BPF_FUNC_lwt_seg6_action:
5184 return &bpf_lwt_seg6_action_proto;
5185 case BPF_FUNC_lwt_seg6_adjust_srh:
5186 return &bpf_lwt_seg6_adjust_srh_proto;
5189 return lwt_out_func_proto(func_id, prog);
5193 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
5194 const struct bpf_prog *prog,
5195 struct bpf_insn_access_aux *info)
5197 const int size_default = sizeof(__u32);
5199 if (off < 0 || off >= sizeof(struct __sk_buff))
5202 /* The verifier guarantees that size > 0. */
5203 if (off % size != 0)
5207 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5208 if (off + size > offsetofend(struct __sk_buff, cb[4]))
5211 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
5212 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
5213 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
5214 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
5215 case bpf_ctx_range(struct __sk_buff, data):
5216 case bpf_ctx_range(struct __sk_buff, data_meta):
5217 case bpf_ctx_range(struct __sk_buff, data_end):
5218 if (size != size_default)
5222 /* Only narrow read access allowed for now. */
5223 if (type == BPF_WRITE) {
5224 if (size != size_default)
5227 bpf_ctx_record_field_size(info, size_default);
5228 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5236 static bool sk_filter_is_valid_access(int off, int size,
5237 enum bpf_access_type type,
5238 const struct bpf_prog *prog,
5239 struct bpf_insn_access_aux *info)
5242 case bpf_ctx_range(struct __sk_buff, tc_classid):
5243 case bpf_ctx_range(struct __sk_buff, data):
5244 case bpf_ctx_range(struct __sk_buff, data_meta):
5245 case bpf_ctx_range(struct __sk_buff, data_end):
5246 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5250 if (type == BPF_WRITE) {
5252 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5259 return bpf_skb_is_valid_access(off, size, type, prog, info);
5262 static bool lwt_is_valid_access(int off, int size,
5263 enum bpf_access_type type,
5264 const struct bpf_prog *prog,
5265 struct bpf_insn_access_aux *info)
5268 case bpf_ctx_range(struct __sk_buff, tc_classid):
5269 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5270 case bpf_ctx_range(struct __sk_buff, data_meta):
5274 if (type == BPF_WRITE) {
5276 case bpf_ctx_range(struct __sk_buff, mark):
5277 case bpf_ctx_range(struct __sk_buff, priority):
5278 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5286 case bpf_ctx_range(struct __sk_buff, data):
5287 info->reg_type = PTR_TO_PACKET;
5289 case bpf_ctx_range(struct __sk_buff, data_end):
5290 info->reg_type = PTR_TO_PACKET_END;
5294 return bpf_skb_is_valid_access(off, size, type, prog, info);
5297 /* Attach type specific accesses */
5298 static bool __sock_filter_check_attach_type(int off,
5299 enum bpf_access_type access_type,
5300 enum bpf_attach_type attach_type)
5303 case offsetof(struct bpf_sock, bound_dev_if):
5304 case offsetof(struct bpf_sock, mark):
5305 case offsetof(struct bpf_sock, priority):
5306 switch (attach_type) {
5307 case BPF_CGROUP_INET_SOCK_CREATE:
5312 case bpf_ctx_range(struct bpf_sock, src_ip4):
5313 switch (attach_type) {
5314 case BPF_CGROUP_INET4_POST_BIND:
5319 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5320 switch (attach_type) {
5321 case BPF_CGROUP_INET6_POST_BIND:
5326 case bpf_ctx_range(struct bpf_sock, src_port):
5327 switch (attach_type) {
5328 case BPF_CGROUP_INET4_POST_BIND:
5329 case BPF_CGROUP_INET6_POST_BIND:
5336 return access_type == BPF_READ;
5341 static bool __sock_filter_check_size(int off, int size,
5342 struct bpf_insn_access_aux *info)
5344 const int size_default = sizeof(__u32);
5347 case bpf_ctx_range(struct bpf_sock, src_ip4):
5348 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5349 bpf_ctx_record_field_size(info, size_default);
5350 return bpf_ctx_narrow_access_ok(off, size, size_default);
5353 return size == size_default;
5356 static bool sock_filter_is_valid_access(int off, int size,
5357 enum bpf_access_type type,
5358 const struct bpf_prog *prog,
5359 struct bpf_insn_access_aux *info)
5361 if (off < 0 || off >= sizeof(struct bpf_sock))
5363 if (off % size != 0)
5365 if (!__sock_filter_check_attach_type(off, type,
5366 prog->expected_attach_type))
5368 if (!__sock_filter_check_size(off, size, info))
5373 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5374 const struct bpf_prog *prog, int drop_verdict)
5376 struct bpf_insn *insn = insn_buf;
5381 /* if (!skb->cloned)
5384 * (Fast-path, otherwise approximation that we might be
5385 * a clone, do the rest in helper.)
5387 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5388 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5389 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5391 /* ret = bpf_skb_pull_data(skb, 0); */
5392 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5393 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5394 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5395 BPF_FUNC_skb_pull_data);
5398 * return TC_ACT_SHOT;
5400 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
5401 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
5402 *insn++ = BPF_EXIT_INSN();
5405 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
5407 *insn++ = prog->insnsi[0];
5409 return insn - insn_buf;
5412 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
5413 struct bpf_insn *insn_buf)
5415 bool indirect = BPF_MODE(orig->code) == BPF_IND;
5416 struct bpf_insn *insn = insn_buf;
5418 /* We're guaranteed here that CTX is in R6. */
5419 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
5421 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
5423 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
5425 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
5428 switch (BPF_SIZE(orig->code)) {
5430 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
5433 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
5436 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
5440 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
5441 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
5442 *insn++ = BPF_EXIT_INSN();
5444 return insn - insn_buf;
5447 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
5448 const struct bpf_prog *prog)
5450 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
5453 static bool tc_cls_act_is_valid_access(int off, int size,
5454 enum bpf_access_type type,
5455 const struct bpf_prog *prog,
5456 struct bpf_insn_access_aux *info)
5458 if (type == BPF_WRITE) {
5460 case bpf_ctx_range(struct __sk_buff, mark):
5461 case bpf_ctx_range(struct __sk_buff, tc_index):
5462 case bpf_ctx_range(struct __sk_buff, priority):
5463 case bpf_ctx_range(struct __sk_buff, tc_classid):
5464 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5472 case bpf_ctx_range(struct __sk_buff, data):
5473 info->reg_type = PTR_TO_PACKET;
5475 case bpf_ctx_range(struct __sk_buff, data_meta):
5476 info->reg_type = PTR_TO_PACKET_META;
5478 case bpf_ctx_range(struct __sk_buff, data_end):
5479 info->reg_type = PTR_TO_PACKET_END;
5481 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5485 return bpf_skb_is_valid_access(off, size, type, prog, info);
5488 static bool __is_valid_xdp_access(int off, int size)
5490 if (off < 0 || off >= sizeof(struct xdp_md))
5492 if (off % size != 0)
5494 if (size != sizeof(__u32))
5500 static bool xdp_is_valid_access(int off, int size,
5501 enum bpf_access_type type,
5502 const struct bpf_prog *prog,
5503 struct bpf_insn_access_aux *info)
5505 if (type == BPF_WRITE) {
5506 if (bpf_prog_is_dev_bound(prog->aux)) {
5508 case offsetof(struct xdp_md, rx_queue_index):
5509 return __is_valid_xdp_access(off, size);
5516 case offsetof(struct xdp_md, data):
5517 info->reg_type = PTR_TO_PACKET;
5519 case offsetof(struct xdp_md, data_meta):
5520 info->reg_type = PTR_TO_PACKET_META;
5522 case offsetof(struct xdp_md, data_end):
5523 info->reg_type = PTR_TO_PACKET_END;
5527 return __is_valid_xdp_access(off, size);
5530 void bpf_warn_invalid_xdp_action(u32 act)
5532 const u32 act_max = XDP_REDIRECT;
5534 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
5535 act > act_max ? "Illegal" : "Driver unsupported",
5538 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
5540 static bool sock_addr_is_valid_access(int off, int size,
5541 enum bpf_access_type type,
5542 const struct bpf_prog *prog,
5543 struct bpf_insn_access_aux *info)
5545 const int size_default = sizeof(__u32);
5547 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
5549 if (off % size != 0)
5552 /* Disallow access to IPv6 fields from IPv4 contex and vise
5556 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5557 switch (prog->expected_attach_type) {
5558 case BPF_CGROUP_INET4_BIND:
5559 case BPF_CGROUP_INET4_CONNECT:
5560 case BPF_CGROUP_UDP4_SENDMSG:
5566 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5567 switch (prog->expected_attach_type) {
5568 case BPF_CGROUP_INET6_BIND:
5569 case BPF_CGROUP_INET6_CONNECT:
5570 case BPF_CGROUP_UDP6_SENDMSG:
5576 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5577 switch (prog->expected_attach_type) {
5578 case BPF_CGROUP_UDP4_SENDMSG:
5584 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5586 switch (prog->expected_attach_type) {
5587 case BPF_CGROUP_UDP6_SENDMSG:
5596 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5597 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5598 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5599 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5601 /* Only narrow read access allowed for now. */
5602 if (type == BPF_READ) {
5603 bpf_ctx_record_field_size(info, size_default);
5604 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5607 if (size != size_default)
5611 case bpf_ctx_range(struct bpf_sock_addr, user_port):
5612 if (size != size_default)
5616 if (type == BPF_READ) {
5617 if (size != size_default)
5627 static bool sock_ops_is_valid_access(int off, int size,
5628 enum bpf_access_type type,
5629 const struct bpf_prog *prog,
5630 struct bpf_insn_access_aux *info)
5632 const int size_default = sizeof(__u32);
5634 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5637 /* The verifier guarantees that size > 0. */
5638 if (off % size != 0)
5641 if (type == BPF_WRITE) {
5643 case offsetof(struct bpf_sock_ops, reply):
5644 case offsetof(struct bpf_sock_ops, sk_txhash):
5645 if (size != size_default)
5653 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5655 if (size != sizeof(__u64))
5659 if (size != size_default)
5668 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5669 const struct bpf_prog *prog)
5671 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
5674 static bool sk_skb_is_valid_access(int off, int size,
5675 enum bpf_access_type type,
5676 const struct bpf_prog *prog,
5677 struct bpf_insn_access_aux *info)
5680 case bpf_ctx_range(struct __sk_buff, tc_classid):
5681 case bpf_ctx_range(struct __sk_buff, data_meta):
5685 if (type == BPF_WRITE) {
5687 case bpf_ctx_range(struct __sk_buff, tc_index):
5688 case bpf_ctx_range(struct __sk_buff, priority):
5696 case bpf_ctx_range(struct __sk_buff, mark):
5698 case bpf_ctx_range(struct __sk_buff, data):
5699 info->reg_type = PTR_TO_PACKET;
5701 case bpf_ctx_range(struct __sk_buff, data_end):
5702 info->reg_type = PTR_TO_PACKET_END;
5706 return bpf_skb_is_valid_access(off, size, type, prog, info);
5709 static bool sk_msg_is_valid_access(int off, int size,
5710 enum bpf_access_type type,
5711 const struct bpf_prog *prog,
5712 struct bpf_insn_access_aux *info)
5714 if (type == BPF_WRITE)
5718 case offsetof(struct sk_msg_md, data):
5719 info->reg_type = PTR_TO_PACKET;
5720 if (size != sizeof(__u64))
5723 case offsetof(struct sk_msg_md, data_end):
5724 info->reg_type = PTR_TO_PACKET_END;
5725 if (size != sizeof(__u64))
5729 if (size != sizeof(__u32))
5733 if (off < 0 || off >= sizeof(struct sk_msg_md))
5735 if (off % size != 0)
5741 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
5742 const struct bpf_insn *si,
5743 struct bpf_insn *insn_buf,
5744 struct bpf_prog *prog, u32 *target_size)
5746 struct bpf_insn *insn = insn_buf;
5750 case offsetof(struct __sk_buff, len):
5751 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5752 bpf_target_off(struct sk_buff, len, 4,
5756 case offsetof(struct __sk_buff, protocol):
5757 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5758 bpf_target_off(struct sk_buff, protocol, 2,
5762 case offsetof(struct __sk_buff, vlan_proto):
5763 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5764 bpf_target_off(struct sk_buff, vlan_proto, 2,
5768 case offsetof(struct __sk_buff, priority):
5769 if (type == BPF_WRITE)
5770 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5771 bpf_target_off(struct sk_buff, priority, 4,
5774 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5775 bpf_target_off(struct sk_buff, priority, 4,
5779 case offsetof(struct __sk_buff, ingress_ifindex):
5780 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5781 bpf_target_off(struct sk_buff, skb_iif, 4,
5785 case offsetof(struct __sk_buff, ifindex):
5786 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
5787 si->dst_reg, si->src_reg,
5788 offsetof(struct sk_buff, dev));
5789 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
5790 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5791 bpf_target_off(struct net_device, ifindex, 4,
5795 case offsetof(struct __sk_buff, hash):
5796 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5797 bpf_target_off(struct sk_buff, hash, 4,
5801 case offsetof(struct __sk_buff, mark):
5802 if (type == BPF_WRITE)
5803 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5804 bpf_target_off(struct sk_buff, mark, 4,
5807 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5808 bpf_target_off(struct sk_buff, mark, 4,
5812 case offsetof(struct __sk_buff, pkt_type):
5814 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
5816 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
5817 #ifdef __BIG_ENDIAN_BITFIELD
5818 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
5822 case offsetof(struct __sk_buff, queue_mapping):
5823 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5824 bpf_target_off(struct sk_buff, queue_mapping, 2,
5828 case offsetof(struct __sk_buff, vlan_present):
5829 case offsetof(struct __sk_buff, vlan_tci):
5830 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
5832 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5833 bpf_target_off(struct sk_buff, vlan_tci, 2,
5835 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
5836 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
5839 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
5840 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
5844 case offsetof(struct __sk_buff, cb[0]) ...
5845 offsetofend(struct __sk_buff, cb[4]) - 1:
5846 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
5847 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
5848 offsetof(struct qdisc_skb_cb, data)) %
5851 prog->cb_access = 1;
5853 off -= offsetof(struct __sk_buff, cb[0]);
5854 off += offsetof(struct sk_buff, cb);
5855 off += offsetof(struct qdisc_skb_cb, data);
5856 if (type == BPF_WRITE)
5857 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
5860 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
5864 case offsetof(struct __sk_buff, tc_classid):
5865 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
5868 off -= offsetof(struct __sk_buff, tc_classid);
5869 off += offsetof(struct sk_buff, cb);
5870 off += offsetof(struct qdisc_skb_cb, tc_classid);
5872 if (type == BPF_WRITE)
5873 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
5876 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
5880 case offsetof(struct __sk_buff, data):
5881 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
5882 si->dst_reg, si->src_reg,
5883 offsetof(struct sk_buff, data));
5886 case offsetof(struct __sk_buff, data_meta):
5888 off -= offsetof(struct __sk_buff, data_meta);
5889 off += offsetof(struct sk_buff, cb);
5890 off += offsetof(struct bpf_skb_data_end, data_meta);
5891 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5895 case offsetof(struct __sk_buff, data_end):
5897 off -= offsetof(struct __sk_buff, data_end);
5898 off += offsetof(struct sk_buff, cb);
5899 off += offsetof(struct bpf_skb_data_end, data_end);
5900 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5904 case offsetof(struct __sk_buff, tc_index):
5905 #ifdef CONFIG_NET_SCHED
5906 if (type == BPF_WRITE)
5907 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
5908 bpf_target_off(struct sk_buff, tc_index, 2,
5911 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5912 bpf_target_off(struct sk_buff, tc_index, 2,
5916 if (type == BPF_WRITE)
5917 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
5919 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5923 case offsetof(struct __sk_buff, napi_id):
5924 #if defined(CONFIG_NET_RX_BUSY_POLL)
5925 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5926 bpf_target_off(struct sk_buff, napi_id, 4,
5928 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
5929 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5932 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5935 case offsetof(struct __sk_buff, family):
5936 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
5938 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5939 si->dst_reg, si->src_reg,
5940 offsetof(struct sk_buff, sk));
5941 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5942 bpf_target_off(struct sock_common,
5946 case offsetof(struct __sk_buff, remote_ip4):
5947 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
5949 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5950 si->dst_reg, si->src_reg,
5951 offsetof(struct sk_buff, sk));
5952 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5953 bpf_target_off(struct sock_common,
5957 case offsetof(struct __sk_buff, local_ip4):
5958 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5959 skc_rcv_saddr) != 4);
5961 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5962 si->dst_reg, si->src_reg,
5963 offsetof(struct sk_buff, sk));
5964 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5965 bpf_target_off(struct sock_common,
5969 case offsetof(struct __sk_buff, remote_ip6[0]) ...
5970 offsetof(struct __sk_buff, remote_ip6[3]):
5971 #if IS_ENABLED(CONFIG_IPV6)
5972 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5973 skc_v6_daddr.s6_addr32[0]) != 4);
5976 off -= offsetof(struct __sk_buff, remote_ip6[0]);
5978 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5979 si->dst_reg, si->src_reg,
5980 offsetof(struct sk_buff, sk));
5981 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5982 offsetof(struct sock_common,
5983 skc_v6_daddr.s6_addr32[0]) +
5986 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5989 case offsetof(struct __sk_buff, local_ip6[0]) ...
5990 offsetof(struct __sk_buff, local_ip6[3]):
5991 #if IS_ENABLED(CONFIG_IPV6)
5992 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5993 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
5996 off -= offsetof(struct __sk_buff, local_ip6[0]);
5998 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5999 si->dst_reg, si->src_reg,
6000 offsetof(struct sk_buff, sk));
6001 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6002 offsetof(struct sock_common,
6003 skc_v6_rcv_saddr.s6_addr32[0]) +
6006 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6010 case offsetof(struct __sk_buff, remote_port):
6011 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6013 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6014 si->dst_reg, si->src_reg,
6015 offsetof(struct sk_buff, sk));
6016 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6017 bpf_target_off(struct sock_common,
6020 #ifndef __BIG_ENDIAN_BITFIELD
6021 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6025 case offsetof(struct __sk_buff, local_port):
6026 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6028 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6029 si->dst_reg, si->src_reg,
6030 offsetof(struct sk_buff, sk));
6031 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6032 bpf_target_off(struct sock_common,
6033 skc_num, 2, target_size));
6037 return insn - insn_buf;
6040 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
6041 const struct bpf_insn *si,
6042 struct bpf_insn *insn_buf,
6043 struct bpf_prog *prog, u32 *target_size)
6045 struct bpf_insn *insn = insn_buf;
6049 case offsetof(struct bpf_sock, bound_dev_if):
6050 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
6052 if (type == BPF_WRITE)
6053 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6054 offsetof(struct sock, sk_bound_dev_if));
6056 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6057 offsetof(struct sock, sk_bound_dev_if));
6060 case offsetof(struct bpf_sock, mark):
6061 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
6063 if (type == BPF_WRITE)
6064 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6065 offsetof(struct sock, sk_mark));
6067 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6068 offsetof(struct sock, sk_mark));
6071 case offsetof(struct bpf_sock, priority):
6072 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
6074 if (type == BPF_WRITE)
6075 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6076 offsetof(struct sock, sk_priority));
6078 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6079 offsetof(struct sock, sk_priority));
6082 case offsetof(struct bpf_sock, family):
6083 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
6085 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6086 offsetof(struct sock, sk_family));
6089 case offsetof(struct bpf_sock, type):
6090 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6091 offsetof(struct sock, __sk_flags_offset));
6092 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6093 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6096 case offsetof(struct bpf_sock, protocol):
6097 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6098 offsetof(struct sock, __sk_flags_offset));
6099 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6100 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
6103 case offsetof(struct bpf_sock, src_ip4):
6104 *insn++ = BPF_LDX_MEM(
6105 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6106 bpf_target_off(struct sock_common, skc_rcv_saddr,
6107 FIELD_SIZEOF(struct sock_common,
6112 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6113 #if IS_ENABLED(CONFIG_IPV6)
6115 off -= offsetof(struct bpf_sock, src_ip6[0]);
6116 *insn++ = BPF_LDX_MEM(
6117 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6120 skc_v6_rcv_saddr.s6_addr32[0],
6121 FIELD_SIZEOF(struct sock_common,
6122 skc_v6_rcv_saddr.s6_addr32[0]),
6123 target_size) + off);
6126 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6130 case offsetof(struct bpf_sock, src_port):
6131 *insn++ = BPF_LDX_MEM(
6132 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
6133 si->dst_reg, si->src_reg,
6134 bpf_target_off(struct sock_common, skc_num,
6135 FIELD_SIZEOF(struct sock_common,
6141 return insn - insn_buf;
6144 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
6145 const struct bpf_insn *si,
6146 struct bpf_insn *insn_buf,
6147 struct bpf_prog *prog, u32 *target_size)
6149 struct bpf_insn *insn = insn_buf;
6152 case offsetof(struct __sk_buff, ifindex):
6153 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6154 si->dst_reg, si->src_reg,
6155 offsetof(struct sk_buff, dev));
6156 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6157 bpf_target_off(struct net_device, ifindex, 4,
6161 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6165 return insn - insn_buf;
6168 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
6169 const struct bpf_insn *si,
6170 struct bpf_insn *insn_buf,
6171 struct bpf_prog *prog, u32 *target_size)
6173 struct bpf_insn *insn = insn_buf;
6176 case offsetof(struct xdp_md, data):
6177 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6178 si->dst_reg, si->src_reg,
6179 offsetof(struct xdp_buff, data));
6181 case offsetof(struct xdp_md, data_meta):
6182 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
6183 si->dst_reg, si->src_reg,
6184 offsetof(struct xdp_buff, data_meta));
6186 case offsetof(struct xdp_md, data_end):
6187 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6188 si->dst_reg, si->src_reg,
6189 offsetof(struct xdp_buff, data_end));
6191 case offsetof(struct xdp_md, ingress_ifindex):
6192 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6193 si->dst_reg, si->src_reg,
6194 offsetof(struct xdp_buff, rxq));
6195 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
6196 si->dst_reg, si->dst_reg,
6197 offsetof(struct xdp_rxq_info, dev));
6198 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6199 offsetof(struct net_device, ifindex));
6201 case offsetof(struct xdp_md, rx_queue_index):
6202 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6203 si->dst_reg, si->src_reg,
6204 offsetof(struct xdp_buff, rxq));
6205 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6206 offsetof(struct xdp_rxq_info,
6211 return insn - insn_buf;
6214 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
6215 * context Structure, F is Field in context structure that contains a pointer
6216 * to Nested Structure of type NS that has the field NF.
6218 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
6219 * sure that SIZE is not greater than actual size of S.F.NF.
6221 * If offset OFF is provided, the load happens from that offset relative to
6224 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
6226 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
6227 si->src_reg, offsetof(S, F)); \
6228 *insn++ = BPF_LDX_MEM( \
6229 SIZE, si->dst_reg, si->dst_reg, \
6230 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6235 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
6236 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
6237 BPF_FIELD_SIZEOF(NS, NF), 0)
6239 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
6240 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
6242 * It doesn't support SIZE argument though since narrow stores are not
6243 * supported for now.
6245 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
6246 * "register" since two registers available in convert_ctx_access are not
6247 * enough: we can't override neither SRC, since it contains value to store, nor
6248 * DST since it contains pointer to context that may be used by later
6249 * instructions. But we need a temporary place to save pointer to nested
6250 * structure whose field we want to store to.
6252 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
6254 int tmp_reg = BPF_REG_9; \
6255 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6257 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6259 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
6261 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
6262 si->dst_reg, offsetof(S, F)); \
6263 *insn++ = BPF_STX_MEM( \
6264 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
6265 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6268 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
6272 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
6275 if (type == BPF_WRITE) { \
6276 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6279 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6280 S, NS, F, NF, SIZE, OFF); \
6284 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6285 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6286 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6288 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6289 const struct bpf_insn *si,
6290 struct bpf_insn *insn_buf,
6291 struct bpf_prog *prog, u32 *target_size)
6293 struct bpf_insn *insn = insn_buf;
6297 case offsetof(struct bpf_sock_addr, user_family):
6298 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6299 struct sockaddr, uaddr, sa_family);
6302 case offsetof(struct bpf_sock_addr, user_ip4):
6303 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6304 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6305 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6308 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6310 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6311 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6312 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6313 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6317 case offsetof(struct bpf_sock_addr, user_port):
6318 /* To get port we need to know sa_family first and then treat
6319 * sockaddr as either sockaddr_in or sockaddr_in6.
6320 * Though we can simplify since port field has same offset and
6321 * size in both structures.
6322 * Here we check this invariant and use just one of the
6323 * structures if it's true.
6325 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
6326 offsetof(struct sockaddr_in6, sin6_port));
6327 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
6328 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
6329 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
6330 struct sockaddr_in6, uaddr,
6331 sin6_port, tmp_reg);
6334 case offsetof(struct bpf_sock_addr, family):
6335 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6336 struct sock, sk, sk_family);
6339 case offsetof(struct bpf_sock_addr, type):
6340 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6341 struct bpf_sock_addr_kern, struct sock, sk,
6342 __sk_flags_offset, BPF_W, 0);
6343 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6344 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6347 case offsetof(struct bpf_sock_addr, protocol):
6348 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6349 struct bpf_sock_addr_kern, struct sock, sk,
6350 __sk_flags_offset, BPF_W, 0);
6351 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6352 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
6356 case offsetof(struct bpf_sock_addr, msg_src_ip4):
6357 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
6358 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6359 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
6360 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
6363 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6366 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
6367 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
6368 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6369 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
6370 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
6374 return insn - insn_buf;
6377 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
6378 const struct bpf_insn *si,
6379 struct bpf_insn *insn_buf,
6380 struct bpf_prog *prog,
6383 struct bpf_insn *insn = insn_buf;
6387 case offsetof(struct bpf_sock_ops, op) ...
6388 offsetof(struct bpf_sock_ops, replylong[3]):
6389 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
6390 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
6391 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
6392 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
6393 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
6394 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
6396 off -= offsetof(struct bpf_sock_ops, op);
6397 off += offsetof(struct bpf_sock_ops_kern, op);
6398 if (type == BPF_WRITE)
6399 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6402 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6406 case offsetof(struct bpf_sock_ops, family):
6407 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6409 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6410 struct bpf_sock_ops_kern, sk),
6411 si->dst_reg, si->src_reg,
6412 offsetof(struct bpf_sock_ops_kern, sk));
6413 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6414 offsetof(struct sock_common, skc_family));
6417 case offsetof(struct bpf_sock_ops, remote_ip4):
6418 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6420 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6421 struct bpf_sock_ops_kern, sk),
6422 si->dst_reg, si->src_reg,
6423 offsetof(struct bpf_sock_ops_kern, sk));
6424 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6425 offsetof(struct sock_common, skc_daddr));
6428 case offsetof(struct bpf_sock_ops, local_ip4):
6429 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6430 skc_rcv_saddr) != 4);
6432 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6433 struct bpf_sock_ops_kern, sk),
6434 si->dst_reg, si->src_reg,
6435 offsetof(struct bpf_sock_ops_kern, sk));
6436 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6437 offsetof(struct sock_common,
6441 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
6442 offsetof(struct bpf_sock_ops, remote_ip6[3]):
6443 #if IS_ENABLED(CONFIG_IPV6)
6444 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6445 skc_v6_daddr.s6_addr32[0]) != 4);
6448 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
6449 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6450 struct bpf_sock_ops_kern, sk),
6451 si->dst_reg, si->src_reg,
6452 offsetof(struct bpf_sock_ops_kern, sk));
6453 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6454 offsetof(struct sock_common,
6455 skc_v6_daddr.s6_addr32[0]) +
6458 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6462 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
6463 offsetof(struct bpf_sock_ops, local_ip6[3]):
6464 #if IS_ENABLED(CONFIG_IPV6)
6465 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6466 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6469 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
6470 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6471 struct bpf_sock_ops_kern, sk),
6472 si->dst_reg, si->src_reg,
6473 offsetof(struct bpf_sock_ops_kern, sk));
6474 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6475 offsetof(struct sock_common,
6476 skc_v6_rcv_saddr.s6_addr32[0]) +
6479 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6483 case offsetof(struct bpf_sock_ops, remote_port):
6484 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6486 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6487 struct bpf_sock_ops_kern, sk),
6488 si->dst_reg, si->src_reg,
6489 offsetof(struct bpf_sock_ops_kern, sk));
6490 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6491 offsetof(struct sock_common, skc_dport));
6492 #ifndef __BIG_ENDIAN_BITFIELD
6493 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6497 case offsetof(struct bpf_sock_ops, local_port):
6498 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6500 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6501 struct bpf_sock_ops_kern, sk),
6502 si->dst_reg, si->src_reg,
6503 offsetof(struct bpf_sock_ops_kern, sk));
6504 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6505 offsetof(struct sock_common, skc_num));
6508 case offsetof(struct bpf_sock_ops, is_fullsock):
6509 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6510 struct bpf_sock_ops_kern,
6512 si->dst_reg, si->src_reg,
6513 offsetof(struct bpf_sock_ops_kern,
6517 case offsetof(struct bpf_sock_ops, state):
6518 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
6520 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6521 struct bpf_sock_ops_kern, sk),
6522 si->dst_reg, si->src_reg,
6523 offsetof(struct bpf_sock_ops_kern, sk));
6524 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
6525 offsetof(struct sock_common, skc_state));
6528 case offsetof(struct bpf_sock_ops, rtt_min):
6529 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
6530 sizeof(struct minmax));
6531 BUILD_BUG_ON(sizeof(struct minmax) <
6532 sizeof(struct minmax_sample));
6534 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6535 struct bpf_sock_ops_kern, sk),
6536 si->dst_reg, si->src_reg,
6537 offsetof(struct bpf_sock_ops_kern, sk));
6538 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6539 offsetof(struct tcp_sock, rtt_min) +
6540 FIELD_SIZEOF(struct minmax_sample, t));
6543 /* Helper macro for adding read access to tcp_sock or sock fields. */
6544 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6546 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6547 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6548 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6549 struct bpf_sock_ops_kern, \
6551 si->dst_reg, si->src_reg, \
6552 offsetof(struct bpf_sock_ops_kern, \
6554 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
6555 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6556 struct bpf_sock_ops_kern, sk),\
6557 si->dst_reg, si->src_reg, \
6558 offsetof(struct bpf_sock_ops_kern, sk));\
6559 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
6561 si->dst_reg, si->dst_reg, \
6562 offsetof(OBJ, OBJ_FIELD)); \
6565 /* Helper macro for adding write access to tcp_sock or sock fields.
6566 * The macro is called with two registers, dst_reg which contains a pointer
6567 * to ctx (context) and src_reg which contains the value that should be
6568 * stored. However, we need an additional register since we cannot overwrite
6569 * dst_reg because it may be used later in the program.
6570 * Instead we "borrow" one of the other register. We first save its value
6571 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
6572 * it at the end of the macro.
6574 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6576 int reg = BPF_REG_9; \
6577 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6578 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6579 if (si->dst_reg == reg || si->src_reg == reg) \
6581 if (si->dst_reg == reg || si->src_reg == reg) \
6583 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
6584 offsetof(struct bpf_sock_ops_kern, \
6586 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6587 struct bpf_sock_ops_kern, \
6590 offsetof(struct bpf_sock_ops_kern, \
6592 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
6593 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6594 struct bpf_sock_ops_kern, sk),\
6596 offsetof(struct bpf_sock_ops_kern, sk));\
6597 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
6599 offsetof(OBJ, OBJ_FIELD)); \
6600 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
6601 offsetof(struct bpf_sock_ops_kern, \
6605 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
6607 if (TYPE == BPF_WRITE) \
6608 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6610 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6613 case offsetof(struct bpf_sock_ops, snd_cwnd):
6614 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
6617 case offsetof(struct bpf_sock_ops, srtt_us):
6618 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
6621 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
6622 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
6626 case offsetof(struct bpf_sock_ops, snd_ssthresh):
6627 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
6630 case offsetof(struct bpf_sock_ops, rcv_nxt):
6631 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
6634 case offsetof(struct bpf_sock_ops, snd_nxt):
6635 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
6638 case offsetof(struct bpf_sock_ops, snd_una):
6639 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
6642 case offsetof(struct bpf_sock_ops, mss_cache):
6643 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
6646 case offsetof(struct bpf_sock_ops, ecn_flags):
6647 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6650 case offsetof(struct bpf_sock_ops, rate_delivered):
6651 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6655 case offsetof(struct bpf_sock_ops, rate_interval_us):
6656 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6660 case offsetof(struct bpf_sock_ops, packets_out):
6661 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6664 case offsetof(struct bpf_sock_ops, retrans_out):
6665 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6668 case offsetof(struct bpf_sock_ops, total_retrans):
6669 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6673 case offsetof(struct bpf_sock_ops, segs_in):
6674 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
6677 case offsetof(struct bpf_sock_ops, data_segs_in):
6678 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
6681 case offsetof(struct bpf_sock_ops, segs_out):
6682 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
6685 case offsetof(struct bpf_sock_ops, data_segs_out):
6686 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
6690 case offsetof(struct bpf_sock_ops, lost_out):
6691 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
6694 case offsetof(struct bpf_sock_ops, sacked_out):
6695 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
6698 case offsetof(struct bpf_sock_ops, sk_txhash):
6699 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
6703 case offsetof(struct bpf_sock_ops, bytes_received):
6704 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
6708 case offsetof(struct bpf_sock_ops, bytes_acked):
6709 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
6713 return insn - insn_buf;
6716 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
6717 const struct bpf_insn *si,
6718 struct bpf_insn *insn_buf,
6719 struct bpf_prog *prog, u32 *target_size)
6721 struct bpf_insn *insn = insn_buf;
6725 case offsetof(struct __sk_buff, data_end):
6727 off -= offsetof(struct __sk_buff, data_end);
6728 off += offsetof(struct sk_buff, cb);
6729 off += offsetof(struct tcp_skb_cb, bpf.data_end);
6730 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6734 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6738 return insn - insn_buf;
6741 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
6742 const struct bpf_insn *si,
6743 struct bpf_insn *insn_buf,
6744 struct bpf_prog *prog, u32 *target_size)
6746 struct bpf_insn *insn = insn_buf;
6747 #if IS_ENABLED(CONFIG_IPV6)
6752 case offsetof(struct sk_msg_md, data):
6753 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data),
6754 si->dst_reg, si->src_reg,
6755 offsetof(struct sk_msg_buff, data));
6757 case offsetof(struct sk_msg_md, data_end):
6758 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data_end),
6759 si->dst_reg, si->src_reg,
6760 offsetof(struct sk_msg_buff, data_end));
6762 case offsetof(struct sk_msg_md, family):
6763 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6765 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6766 struct sk_msg_buff, sk),
6767 si->dst_reg, si->src_reg,
6768 offsetof(struct sk_msg_buff, sk));
6769 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6770 offsetof(struct sock_common, skc_family));
6773 case offsetof(struct sk_msg_md, remote_ip4):
6774 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6776 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6777 struct sk_msg_buff, sk),
6778 si->dst_reg, si->src_reg,
6779 offsetof(struct sk_msg_buff, sk));
6780 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6781 offsetof(struct sock_common, skc_daddr));
6784 case offsetof(struct sk_msg_md, local_ip4):
6785 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6786 skc_rcv_saddr) != 4);
6788 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6789 struct sk_msg_buff, sk),
6790 si->dst_reg, si->src_reg,
6791 offsetof(struct sk_msg_buff, sk));
6792 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6793 offsetof(struct sock_common,
6797 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
6798 offsetof(struct sk_msg_md, remote_ip6[3]):
6799 #if IS_ENABLED(CONFIG_IPV6)
6800 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6801 skc_v6_daddr.s6_addr32[0]) != 4);
6804 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
6805 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6806 struct sk_msg_buff, sk),
6807 si->dst_reg, si->src_reg,
6808 offsetof(struct sk_msg_buff, sk));
6809 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6810 offsetof(struct sock_common,
6811 skc_v6_daddr.s6_addr32[0]) +
6814 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6818 case offsetof(struct sk_msg_md, local_ip6[0]) ...
6819 offsetof(struct sk_msg_md, local_ip6[3]):
6820 #if IS_ENABLED(CONFIG_IPV6)
6821 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6822 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6825 off -= offsetof(struct sk_msg_md, local_ip6[0]);
6826 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6827 struct sk_msg_buff, sk),
6828 si->dst_reg, si->src_reg,
6829 offsetof(struct sk_msg_buff, sk));
6830 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6831 offsetof(struct sock_common,
6832 skc_v6_rcv_saddr.s6_addr32[0]) +
6835 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6839 case offsetof(struct sk_msg_md, remote_port):
6840 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6842 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6843 struct sk_msg_buff, sk),
6844 si->dst_reg, si->src_reg,
6845 offsetof(struct sk_msg_buff, sk));
6846 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6847 offsetof(struct sock_common, skc_dport));
6848 #ifndef __BIG_ENDIAN_BITFIELD
6849 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6853 case offsetof(struct sk_msg_md, local_port):
6854 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6856 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6857 struct sk_msg_buff, sk),
6858 si->dst_reg, si->src_reg,
6859 offsetof(struct sk_msg_buff, sk));
6860 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6861 offsetof(struct sock_common, skc_num));
6865 return insn - insn_buf;
6868 const struct bpf_verifier_ops sk_filter_verifier_ops = {
6869 .get_func_proto = sk_filter_func_proto,
6870 .is_valid_access = sk_filter_is_valid_access,
6871 .convert_ctx_access = bpf_convert_ctx_access,
6872 .gen_ld_abs = bpf_gen_ld_abs,
6875 const struct bpf_prog_ops sk_filter_prog_ops = {
6876 .test_run = bpf_prog_test_run_skb,
6879 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
6880 .get_func_proto = tc_cls_act_func_proto,
6881 .is_valid_access = tc_cls_act_is_valid_access,
6882 .convert_ctx_access = tc_cls_act_convert_ctx_access,
6883 .gen_prologue = tc_cls_act_prologue,
6884 .gen_ld_abs = bpf_gen_ld_abs,
6887 const struct bpf_prog_ops tc_cls_act_prog_ops = {
6888 .test_run = bpf_prog_test_run_skb,
6891 const struct bpf_verifier_ops xdp_verifier_ops = {
6892 .get_func_proto = xdp_func_proto,
6893 .is_valid_access = xdp_is_valid_access,
6894 .convert_ctx_access = xdp_convert_ctx_access,
6897 const struct bpf_prog_ops xdp_prog_ops = {
6898 .test_run = bpf_prog_test_run_xdp,
6901 const struct bpf_verifier_ops cg_skb_verifier_ops = {
6902 .get_func_proto = cg_skb_func_proto,
6903 .is_valid_access = sk_filter_is_valid_access,
6904 .convert_ctx_access = bpf_convert_ctx_access,
6907 const struct bpf_prog_ops cg_skb_prog_ops = {
6908 .test_run = bpf_prog_test_run_skb,
6911 const struct bpf_verifier_ops lwt_in_verifier_ops = {
6912 .get_func_proto = lwt_in_func_proto,
6913 .is_valid_access = lwt_is_valid_access,
6914 .convert_ctx_access = bpf_convert_ctx_access,
6917 const struct bpf_prog_ops lwt_in_prog_ops = {
6918 .test_run = bpf_prog_test_run_skb,
6921 const struct bpf_verifier_ops lwt_out_verifier_ops = {
6922 .get_func_proto = lwt_out_func_proto,
6923 .is_valid_access = lwt_is_valid_access,
6924 .convert_ctx_access = bpf_convert_ctx_access,
6927 const struct bpf_prog_ops lwt_out_prog_ops = {
6928 .test_run = bpf_prog_test_run_skb,
6931 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
6932 .get_func_proto = lwt_xmit_func_proto,
6933 .is_valid_access = lwt_is_valid_access,
6934 .convert_ctx_access = bpf_convert_ctx_access,
6935 .gen_prologue = tc_cls_act_prologue,
6938 const struct bpf_prog_ops lwt_xmit_prog_ops = {
6939 .test_run = bpf_prog_test_run_skb,
6942 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
6943 .get_func_proto = lwt_seg6local_func_proto,
6944 .is_valid_access = lwt_is_valid_access,
6945 .convert_ctx_access = bpf_convert_ctx_access,
6948 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
6949 .test_run = bpf_prog_test_run_skb,
6952 const struct bpf_verifier_ops cg_sock_verifier_ops = {
6953 .get_func_proto = sock_filter_func_proto,
6954 .is_valid_access = sock_filter_is_valid_access,
6955 .convert_ctx_access = sock_filter_convert_ctx_access,
6958 const struct bpf_prog_ops cg_sock_prog_ops = {
6961 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
6962 .get_func_proto = sock_addr_func_proto,
6963 .is_valid_access = sock_addr_is_valid_access,
6964 .convert_ctx_access = sock_addr_convert_ctx_access,
6967 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
6970 const struct bpf_verifier_ops sock_ops_verifier_ops = {
6971 .get_func_proto = sock_ops_func_proto,
6972 .is_valid_access = sock_ops_is_valid_access,
6973 .convert_ctx_access = sock_ops_convert_ctx_access,
6976 const struct bpf_prog_ops sock_ops_prog_ops = {
6979 const struct bpf_verifier_ops sk_skb_verifier_ops = {
6980 .get_func_proto = sk_skb_func_proto,
6981 .is_valid_access = sk_skb_is_valid_access,
6982 .convert_ctx_access = sk_skb_convert_ctx_access,
6983 .gen_prologue = sk_skb_prologue,
6986 const struct bpf_prog_ops sk_skb_prog_ops = {
6989 const struct bpf_verifier_ops sk_msg_verifier_ops = {
6990 .get_func_proto = sk_msg_func_proto,
6991 .is_valid_access = sk_msg_is_valid_access,
6992 .convert_ctx_access = sk_msg_convert_ctx_access,
6995 const struct bpf_prog_ops sk_msg_prog_ops = {
6998 int sk_detach_filter(struct sock *sk)
7001 struct sk_filter *filter;
7003 if (sock_flag(sk, SOCK_FILTER_LOCKED))
7006 filter = rcu_dereference_protected(sk->sk_filter,
7007 lockdep_sock_is_held(sk));
7009 RCU_INIT_POINTER(sk->sk_filter, NULL);
7010 sk_filter_uncharge(sk, filter);
7016 EXPORT_SYMBOL_GPL(sk_detach_filter);
7018 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
7021 struct sock_fprog_kern *fprog;
7022 struct sk_filter *filter;
7026 filter = rcu_dereference_protected(sk->sk_filter,
7027 lockdep_sock_is_held(sk));
7031 /* We're copying the filter that has been originally attached,
7032 * so no conversion/decode needed anymore. eBPF programs that
7033 * have no original program cannot be dumped through this.
7036 fprog = filter->prog->orig_prog;
7042 /* User space only enquires number of filter blocks. */
7046 if (len < fprog->len)
7050 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
7053 /* Instead of bytes, the API requests to return the number
7063 struct sk_reuseport_kern {
7064 struct sk_buff *skb;
7066 struct sock *selected_sk;
7073 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
7074 struct sock_reuseport *reuse,
7075 struct sock *sk, struct sk_buff *skb,
7078 reuse_kern->skb = skb;
7079 reuse_kern->sk = sk;
7080 reuse_kern->selected_sk = NULL;
7081 reuse_kern->data_end = skb->data + skb_headlen(skb);
7082 reuse_kern->hash = hash;
7083 reuse_kern->reuseport_id = reuse->reuseport_id;
7084 reuse_kern->bind_inany = reuse->bind_inany;
7087 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
7088 struct bpf_prog *prog, struct sk_buff *skb,
7091 struct sk_reuseport_kern reuse_kern;
7092 enum sk_action action;
7094 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
7095 action = BPF_PROG_RUN(prog, &reuse_kern);
7097 if (action == SK_PASS)
7098 return reuse_kern.selected_sk;
7100 return ERR_PTR(-ECONNREFUSED);
7103 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
7104 struct bpf_map *, map, void *, key, u32, flags)
7106 struct sock_reuseport *reuse;
7107 struct sock *selected_sk;
7109 selected_sk = map->ops->map_lookup_elem(map, key);
7113 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
7115 /* selected_sk is unhashed (e.g. by close()) after the
7116 * above map_lookup_elem(). Treat selected_sk has already
7117 * been removed from the map.
7121 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
7124 if (unlikely(!reuse_kern->reuseport_id))
7125 /* There is a small race between adding the
7126 * sk to the map and setting the
7127 * reuse_kern->reuseport_id.
7128 * Treat it as the sk has not been added to
7133 sk = reuse_kern->sk;
7134 if (sk->sk_protocol != selected_sk->sk_protocol)
7136 else if (sk->sk_family != selected_sk->sk_family)
7137 return -EAFNOSUPPORT;
7139 /* Catch all. Likely bound to a different sockaddr. */
7143 reuse_kern->selected_sk = selected_sk;
7148 static const struct bpf_func_proto sk_select_reuseport_proto = {
7149 .func = sk_select_reuseport,
7151 .ret_type = RET_INTEGER,
7152 .arg1_type = ARG_PTR_TO_CTX,
7153 .arg2_type = ARG_CONST_MAP_PTR,
7154 .arg3_type = ARG_PTR_TO_MAP_KEY,
7155 .arg4_type = ARG_ANYTHING,
7158 BPF_CALL_4(sk_reuseport_load_bytes,
7159 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7160 void *, to, u32, len)
7162 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
7165 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
7166 .func = sk_reuseport_load_bytes,
7168 .ret_type = RET_INTEGER,
7169 .arg1_type = ARG_PTR_TO_CTX,
7170 .arg2_type = ARG_ANYTHING,
7171 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7172 .arg4_type = ARG_CONST_SIZE,
7175 BPF_CALL_5(sk_reuseport_load_bytes_relative,
7176 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7177 void *, to, u32, len, u32, start_header)
7179 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
7183 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
7184 .func = sk_reuseport_load_bytes_relative,
7186 .ret_type = RET_INTEGER,
7187 .arg1_type = ARG_PTR_TO_CTX,
7188 .arg2_type = ARG_ANYTHING,
7189 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7190 .arg4_type = ARG_CONST_SIZE,
7191 .arg5_type = ARG_ANYTHING,
7194 static const struct bpf_func_proto *
7195 sk_reuseport_func_proto(enum bpf_func_id func_id,
7196 const struct bpf_prog *prog)
7199 case BPF_FUNC_sk_select_reuseport:
7200 return &sk_select_reuseport_proto;
7201 case BPF_FUNC_skb_load_bytes:
7202 return &sk_reuseport_load_bytes_proto;
7203 case BPF_FUNC_skb_load_bytes_relative:
7204 return &sk_reuseport_load_bytes_relative_proto;
7206 return bpf_base_func_proto(func_id);
7211 sk_reuseport_is_valid_access(int off, int size,
7212 enum bpf_access_type type,
7213 const struct bpf_prog *prog,
7214 struct bpf_insn_access_aux *info)
7216 const u32 size_default = sizeof(__u32);
7218 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
7219 off % size || type != BPF_READ)
7223 case offsetof(struct sk_reuseport_md, data):
7224 info->reg_type = PTR_TO_PACKET;
7225 return size == sizeof(__u64);
7227 case offsetof(struct sk_reuseport_md, data_end):
7228 info->reg_type = PTR_TO_PACKET_END;
7229 return size == sizeof(__u64);
7231 case offsetof(struct sk_reuseport_md, hash):
7232 return size == size_default;
7234 /* Fields that allow narrowing */
7235 case offsetof(struct sk_reuseport_md, eth_protocol):
7236 if (size < FIELD_SIZEOF(struct sk_buff, protocol))
7238 case offsetof(struct sk_reuseport_md, ip_protocol):
7239 case offsetof(struct sk_reuseport_md, bind_inany):
7240 case offsetof(struct sk_reuseport_md, len):
7241 bpf_ctx_record_field_size(info, size_default);
7242 return bpf_ctx_narrow_access_ok(off, size, size_default);
7249 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
7250 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7251 si->dst_reg, si->src_reg, \
7252 bpf_target_off(struct sk_reuseport_kern, F, \
7253 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7257 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
7258 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
7263 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
7264 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
7267 SK_FIELD, BPF_SIZE, EXTRA_OFF)
7269 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
7270 const struct bpf_insn *si,
7271 struct bpf_insn *insn_buf,
7272 struct bpf_prog *prog,
7275 struct bpf_insn *insn = insn_buf;
7278 case offsetof(struct sk_reuseport_md, data):
7279 SK_REUSEPORT_LOAD_SKB_FIELD(data);
7282 case offsetof(struct sk_reuseport_md, len):
7283 SK_REUSEPORT_LOAD_SKB_FIELD(len);
7286 case offsetof(struct sk_reuseport_md, eth_protocol):
7287 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
7290 case offsetof(struct sk_reuseport_md, ip_protocol):
7291 BUILD_BUG_ON(hweight_long(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
7292 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset,
7294 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7295 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7297 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
7298 * aware. No further narrowing or masking is needed.
7303 case offsetof(struct sk_reuseport_md, data_end):
7304 SK_REUSEPORT_LOAD_FIELD(data_end);
7307 case offsetof(struct sk_reuseport_md, hash):
7308 SK_REUSEPORT_LOAD_FIELD(hash);
7311 case offsetof(struct sk_reuseport_md, bind_inany):
7312 SK_REUSEPORT_LOAD_FIELD(bind_inany);
7316 return insn - insn_buf;
7319 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
7320 .get_func_proto = sk_reuseport_func_proto,
7321 .is_valid_access = sk_reuseport_is_valid_access,
7322 .convert_ctx_access = sk_reuseport_convert_ctx_access,
7325 const struct bpf_prog_ops sk_reuseport_prog_ops = {
7327 #endif /* CONFIG_INET */