2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
29 #include <linux/sock_diag.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_packet.h>
34 #include <linux/if_arp.h>
35 #include <linux/gfp.h>
36 #include <net/inet_common.h>
38 #include <net/protocol.h>
39 #include <net/netlink.h>
40 #include <linux/skbuff.h>
42 #include <net/flow_dissector.h>
43 #include <linux/errno.h>
44 #include <linux/timer.h>
45 #include <linux/uaccess.h>
46 #include <asm/unaligned.h>
47 #include <asm/cmpxchg.h>
48 #include <linux/filter.h>
49 #include <linux/ratelimit.h>
50 #include <linux/seccomp.h>
51 #include <linux/if_vlan.h>
52 #include <linux/bpf.h>
53 #include <net/sch_generic.h>
54 #include <net/cls_cgroup.h>
55 #include <net/dst_metadata.h>
57 #include <net/sock_reuseport.h>
58 #include <net/busy_poll.h>
61 #include <linux/bpf_trace.h>
62 #include <net/xdp_sock.h>
63 #include <linux/inetdevice.h>
64 #include <net/ip_fib.h>
68 #include <linux/seg6_local.h>
70 #include <net/seg6_local.h>
73 * sk_filter_trim_cap - run a packet through a socket filter
74 * @sk: sock associated with &sk_buff
75 * @skb: buffer to filter
76 * @cap: limit on how short the eBPF program may trim the packet
78 * Run the eBPF program and then cut skb->data to correct size returned by
79 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
80 * than pkt_len we keep whole skb->data. This is the socket level
81 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
82 * be accepted or -EPERM if the packet should be tossed.
85 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
88 struct sk_filter *filter;
91 * If the skb was allocated from pfmemalloc reserves, only
92 * allow SOCK_MEMALLOC sockets to use it as this socket is
95 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
96 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
99 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
103 err = security_sock_rcv_skb(sk, skb);
108 filter = rcu_dereference(sk->sk_filter);
110 struct sock *save_sk = skb->sk;
111 unsigned int pkt_len;
114 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
116 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
122 EXPORT_SYMBOL(sk_filter_trim_cap);
124 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
126 return skb_get_poff(skb);
129 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
133 if (skb_is_nonlinear(skb))
136 if (skb->len < sizeof(struct nlattr))
139 if (a > skb->len - sizeof(struct nlattr))
142 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
144 return (void *) nla - (void *) skb->data;
149 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
153 if (skb_is_nonlinear(skb))
156 if (skb->len < sizeof(struct nlattr))
159 if (a > skb->len - sizeof(struct nlattr))
162 nla = (struct nlattr *) &skb->data[a];
163 if (nla->nla_len > skb->len - a)
166 nla = nla_find_nested(nla, x);
168 return (void *) nla - (void *) skb->data;
173 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
174 data, int, headlen, int, offset)
177 const int len = sizeof(tmp);
180 if (headlen - offset >= len)
181 return *(u8 *)(data + offset);
182 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
185 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
193 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
196 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
200 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
201 data, int, headlen, int, offset)
204 const int len = sizeof(tmp);
207 if (headlen - offset >= len)
208 return get_unaligned_be16(data + offset);
209 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
210 return be16_to_cpu(tmp);
212 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
214 return get_unaligned_be16(ptr);
220 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
223 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
227 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
228 data, int, headlen, int, offset)
231 const int len = sizeof(tmp);
233 if (likely(offset >= 0)) {
234 if (headlen - offset >= len)
235 return get_unaligned_be32(data + offset);
236 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
237 return be32_to_cpu(tmp);
239 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
241 return get_unaligned_be32(ptr);
247 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
250 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
254 BPF_CALL_0(bpf_get_raw_cpu_id)
256 return raw_smp_processor_id();
259 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
260 .func = bpf_get_raw_cpu_id,
262 .ret_type = RET_INTEGER,
265 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
266 struct bpf_insn *insn_buf)
268 struct bpf_insn *insn = insn_buf;
272 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
274 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
275 offsetof(struct sk_buff, mark));
279 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
280 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
281 #ifdef __BIG_ENDIAN_BITFIELD
282 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
287 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
289 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
290 offsetof(struct sk_buff, queue_mapping));
293 case SKF_AD_VLAN_TAG:
294 case SKF_AD_VLAN_TAG_PRESENT:
295 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
296 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
298 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
299 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
300 offsetof(struct sk_buff, vlan_tci));
301 if (skb_field == SKF_AD_VLAN_TAG) {
302 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
306 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
308 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
313 return insn - insn_buf;
316 static bool convert_bpf_extensions(struct sock_filter *fp,
317 struct bpf_insn **insnp)
319 struct bpf_insn *insn = *insnp;
323 case SKF_AD_OFF + SKF_AD_PROTOCOL:
324 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
326 /* A = *(u16 *) (CTX + offsetof(protocol)) */
327 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
328 offsetof(struct sk_buff, protocol));
329 /* A = ntohs(A) [emitting a nop or swap16] */
330 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
333 case SKF_AD_OFF + SKF_AD_PKTTYPE:
334 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
338 case SKF_AD_OFF + SKF_AD_IFINDEX:
339 case SKF_AD_OFF + SKF_AD_HATYPE:
340 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
341 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
343 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
344 BPF_REG_TMP, BPF_REG_CTX,
345 offsetof(struct sk_buff, dev));
346 /* if (tmp != 0) goto pc + 1 */
347 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
348 *insn++ = BPF_EXIT_INSN();
349 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
350 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
351 offsetof(struct net_device, ifindex));
353 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
354 offsetof(struct net_device, type));
357 case SKF_AD_OFF + SKF_AD_MARK:
358 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
362 case SKF_AD_OFF + SKF_AD_RXHASH:
363 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
365 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
366 offsetof(struct sk_buff, hash));
369 case SKF_AD_OFF + SKF_AD_QUEUE:
370 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
374 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
375 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
376 BPF_REG_A, BPF_REG_CTX, insn);
380 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
381 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
382 BPF_REG_A, BPF_REG_CTX, insn);
386 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
387 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
389 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
390 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
391 offsetof(struct sk_buff, vlan_proto));
392 /* A = ntohs(A) [emitting a nop or swap16] */
393 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
396 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
397 case SKF_AD_OFF + SKF_AD_NLATTR:
398 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
399 case SKF_AD_OFF + SKF_AD_CPU:
400 case SKF_AD_OFF + SKF_AD_RANDOM:
402 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
404 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
406 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
407 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
409 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
410 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
412 case SKF_AD_OFF + SKF_AD_NLATTR:
413 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
415 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
416 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
418 case SKF_AD_OFF + SKF_AD_CPU:
419 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
421 case SKF_AD_OFF + SKF_AD_RANDOM:
422 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
423 bpf_user_rnd_init_once();
428 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
430 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
434 /* This is just a dummy call to avoid letting the compiler
435 * evict __bpf_call_base() as an optimization. Placed here
436 * where no-one bothers.
438 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
446 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
448 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
449 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
450 bool endian = BPF_SIZE(fp->code) == BPF_H ||
451 BPF_SIZE(fp->code) == BPF_W;
452 bool indirect = BPF_MODE(fp->code) == BPF_IND;
453 const int ip_align = NET_IP_ALIGN;
454 struct bpf_insn *insn = *insnp;
458 ((unaligned_ok && offset >= 0) ||
459 (!unaligned_ok && offset >= 0 &&
460 offset + ip_align >= 0 &&
461 offset + ip_align % size == 0))) {
462 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
463 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
464 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP, size, 2 + endian);
465 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A, BPF_REG_D,
468 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
469 *insn++ = BPF_JMP_A(8);
472 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
473 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
474 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
476 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
478 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
480 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
483 switch (BPF_SIZE(fp->code)) {
485 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
488 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
491 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
497 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
498 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
499 *insn = BPF_EXIT_INSN();
506 * bpf_convert_filter - convert filter program
507 * @prog: the user passed filter program
508 * @len: the length of the user passed filter program
509 * @new_prog: allocated 'struct bpf_prog' or NULL
510 * @new_len: pointer to store length of converted program
511 * @seen_ld_abs: bool whether we've seen ld_abs/ind
513 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
514 * style extended BPF (eBPF).
515 * Conversion workflow:
517 * 1) First pass for calculating the new program length:
518 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
520 * 2) 2nd pass to remap in two passes: 1st pass finds new
521 * jump offsets, 2nd pass remapping:
522 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
524 static int bpf_convert_filter(struct sock_filter *prog, int len,
525 struct bpf_prog *new_prog, int *new_len,
528 int new_flen = 0, pass = 0, target, i, stack_off;
529 struct bpf_insn *new_insn, *first_insn = NULL;
530 struct sock_filter *fp;
534 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
535 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
537 if (len <= 0 || len > BPF_MAXINSNS)
541 first_insn = new_prog->insnsi;
542 addrs = kcalloc(len, sizeof(*addrs),
543 GFP_KERNEL | __GFP_NOWARN);
549 new_insn = first_insn;
552 /* Classic BPF related prologue emission. */
554 /* Classic BPF expects A and X to be reset first. These need
555 * to be guaranteed to be the first two instructions.
557 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
558 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
560 /* All programs must keep CTX in callee saved BPF_REG_CTX.
561 * In eBPF case it's done by the compiler, here we need to
562 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
564 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
566 /* For packet access in classic BPF, cache skb->data
567 * in callee-saved BPF R8 and skb->len - skb->data_len
568 * (headlen) in BPF R9. Since classic BPF is read-only
569 * on CTX, we only need to cache it once.
571 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
572 BPF_REG_D, BPF_REG_CTX,
573 offsetof(struct sk_buff, data));
574 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
575 offsetof(struct sk_buff, len));
576 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
577 offsetof(struct sk_buff, data_len));
578 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
584 for (i = 0; i < len; fp++, i++) {
585 struct bpf_insn tmp_insns[32] = { };
586 struct bpf_insn *insn = tmp_insns;
589 addrs[i] = new_insn - first_insn;
592 /* All arithmetic insns and skb loads map as-is. */
593 case BPF_ALU | BPF_ADD | BPF_X:
594 case BPF_ALU | BPF_ADD | BPF_K:
595 case BPF_ALU | BPF_SUB | BPF_X:
596 case BPF_ALU | BPF_SUB | BPF_K:
597 case BPF_ALU | BPF_AND | BPF_X:
598 case BPF_ALU | BPF_AND | BPF_K:
599 case BPF_ALU | BPF_OR | BPF_X:
600 case BPF_ALU | BPF_OR | BPF_K:
601 case BPF_ALU | BPF_LSH | BPF_X:
602 case BPF_ALU | BPF_LSH | BPF_K:
603 case BPF_ALU | BPF_RSH | BPF_X:
604 case BPF_ALU | BPF_RSH | BPF_K:
605 case BPF_ALU | BPF_XOR | BPF_X:
606 case BPF_ALU | BPF_XOR | BPF_K:
607 case BPF_ALU | BPF_MUL | BPF_X:
608 case BPF_ALU | BPF_MUL | BPF_K:
609 case BPF_ALU | BPF_DIV | BPF_X:
610 case BPF_ALU | BPF_DIV | BPF_K:
611 case BPF_ALU | BPF_MOD | BPF_X:
612 case BPF_ALU | BPF_MOD | BPF_K:
613 case BPF_ALU | BPF_NEG:
614 case BPF_LD | BPF_ABS | BPF_W:
615 case BPF_LD | BPF_ABS | BPF_H:
616 case BPF_LD | BPF_ABS | BPF_B:
617 case BPF_LD | BPF_IND | BPF_W:
618 case BPF_LD | BPF_IND | BPF_H:
619 case BPF_LD | BPF_IND | BPF_B:
620 /* Check for overloaded BPF extension and
621 * directly convert it if found, otherwise
622 * just move on with mapping.
624 if (BPF_CLASS(fp->code) == BPF_LD &&
625 BPF_MODE(fp->code) == BPF_ABS &&
626 convert_bpf_extensions(fp, &insn))
628 if (BPF_CLASS(fp->code) == BPF_LD &&
629 convert_bpf_ld_abs(fp, &insn)) {
634 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
635 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
636 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
637 /* Error with exception code on div/mod by 0.
638 * For cBPF programs, this was always return 0.
640 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
641 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
642 *insn++ = BPF_EXIT_INSN();
645 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
648 /* Jump transformation cannot use BPF block macros
649 * everywhere as offset calculation and target updates
650 * require a bit more work than the rest, i.e. jump
651 * opcodes map as-is, but offsets need adjustment.
654 #define BPF_EMIT_JMP \
656 const s32 off_min = S16_MIN, off_max = S16_MAX; \
659 if (target >= len || target < 0) \
661 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
662 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
663 off -= insn - tmp_insns; \
664 /* Reject anything not fitting into insn->off. */ \
665 if (off < off_min || off > off_max) \
670 case BPF_JMP | BPF_JA:
671 target = i + fp->k + 1;
672 insn->code = fp->code;
676 case BPF_JMP | BPF_JEQ | BPF_K:
677 case BPF_JMP | BPF_JEQ | BPF_X:
678 case BPF_JMP | BPF_JSET | BPF_K:
679 case BPF_JMP | BPF_JSET | BPF_X:
680 case BPF_JMP | BPF_JGT | BPF_K:
681 case BPF_JMP | BPF_JGT | BPF_X:
682 case BPF_JMP | BPF_JGE | BPF_K:
683 case BPF_JMP | BPF_JGE | BPF_X:
684 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
685 /* BPF immediates are signed, zero extend
686 * immediate into tmp register and use it
689 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
691 insn->dst_reg = BPF_REG_A;
692 insn->src_reg = BPF_REG_TMP;
695 insn->dst_reg = BPF_REG_A;
697 bpf_src = BPF_SRC(fp->code);
698 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
701 /* Common case where 'jump_false' is next insn. */
703 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
704 target = i + fp->jt + 1;
709 /* Convert some jumps when 'jump_true' is next insn. */
711 switch (BPF_OP(fp->code)) {
713 insn->code = BPF_JMP | BPF_JNE | bpf_src;
716 insn->code = BPF_JMP | BPF_JLE | bpf_src;
719 insn->code = BPF_JMP | BPF_JLT | bpf_src;
725 target = i + fp->jf + 1;
730 /* Other jumps are mapped into two insns: Jxx and JA. */
731 target = i + fp->jt + 1;
732 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
736 insn->code = BPF_JMP | BPF_JA;
737 target = i + fp->jf + 1;
741 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
742 case BPF_LDX | BPF_MSH | BPF_B: {
743 struct sock_filter tmp = {
744 .code = BPF_LD | BPF_ABS | BPF_B,
751 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
752 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
753 convert_bpf_ld_abs(&tmp, &insn);
756 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
758 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
760 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
762 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
764 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
767 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
768 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
770 case BPF_RET | BPF_A:
771 case BPF_RET | BPF_K:
772 if (BPF_RVAL(fp->code) == BPF_K)
773 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
775 *insn = BPF_EXIT_INSN();
778 /* Store to stack. */
781 stack_off = fp->k * 4 + 4;
782 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
783 BPF_ST ? BPF_REG_A : BPF_REG_X,
785 /* check_load_and_stores() verifies that classic BPF can
786 * load from stack only after write, so tracking
787 * stack_depth for ST|STX insns is enough
789 if (new_prog && new_prog->aux->stack_depth < stack_off)
790 new_prog->aux->stack_depth = stack_off;
793 /* Load from stack. */
794 case BPF_LD | BPF_MEM:
795 case BPF_LDX | BPF_MEM:
796 stack_off = fp->k * 4 + 4;
797 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
798 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
803 case BPF_LD | BPF_IMM:
804 case BPF_LDX | BPF_IMM:
805 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
806 BPF_REG_A : BPF_REG_X, fp->k);
810 case BPF_MISC | BPF_TAX:
811 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
815 case BPF_MISC | BPF_TXA:
816 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
819 /* A = skb->len or X = skb->len */
820 case BPF_LD | BPF_W | BPF_LEN:
821 case BPF_LDX | BPF_W | BPF_LEN:
822 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
823 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
824 offsetof(struct sk_buff, len));
827 /* Access seccomp_data fields. */
828 case BPF_LDX | BPF_ABS | BPF_W:
829 /* A = *(u32 *) (ctx + K) */
830 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
833 /* Unknown instruction. */
840 memcpy(new_insn, tmp_insns,
841 sizeof(*insn) * (insn - tmp_insns));
842 new_insn += insn - tmp_insns;
846 /* Only calculating new length. */
847 *new_len = new_insn - first_insn;
849 *new_len += 4; /* Prologue bits. */
854 if (new_flen != new_insn - first_insn) {
855 new_flen = new_insn - first_insn;
862 BUG_ON(*new_len != new_flen);
871 * As we dont want to clear mem[] array for each packet going through
872 * __bpf_prog_run(), we check that filter loaded by user never try to read
873 * a cell if not previously written, and we check all branches to be sure
874 * a malicious user doesn't try to abuse us.
876 static int check_load_and_stores(const struct sock_filter *filter, int flen)
878 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
881 BUILD_BUG_ON(BPF_MEMWORDS > 16);
883 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
887 memset(masks, 0xff, flen * sizeof(*masks));
889 for (pc = 0; pc < flen; pc++) {
890 memvalid &= masks[pc];
892 switch (filter[pc].code) {
895 memvalid |= (1 << filter[pc].k);
897 case BPF_LD | BPF_MEM:
898 case BPF_LDX | BPF_MEM:
899 if (!(memvalid & (1 << filter[pc].k))) {
904 case BPF_JMP | BPF_JA:
905 /* A jump must set masks on target */
906 masks[pc + 1 + filter[pc].k] &= memvalid;
909 case BPF_JMP | BPF_JEQ | BPF_K:
910 case BPF_JMP | BPF_JEQ | BPF_X:
911 case BPF_JMP | BPF_JGE | BPF_K:
912 case BPF_JMP | BPF_JGE | BPF_X:
913 case BPF_JMP | BPF_JGT | BPF_K:
914 case BPF_JMP | BPF_JGT | BPF_X:
915 case BPF_JMP | BPF_JSET | BPF_K:
916 case BPF_JMP | BPF_JSET | BPF_X:
917 /* A jump must set masks on targets */
918 masks[pc + 1 + filter[pc].jt] &= memvalid;
919 masks[pc + 1 + filter[pc].jf] &= memvalid;
929 static bool chk_code_allowed(u16 code_to_probe)
931 static const bool codes[] = {
932 /* 32 bit ALU operations */
933 [BPF_ALU | BPF_ADD | BPF_K] = true,
934 [BPF_ALU | BPF_ADD | BPF_X] = true,
935 [BPF_ALU | BPF_SUB | BPF_K] = true,
936 [BPF_ALU | BPF_SUB | BPF_X] = true,
937 [BPF_ALU | BPF_MUL | BPF_K] = true,
938 [BPF_ALU | BPF_MUL | BPF_X] = true,
939 [BPF_ALU | BPF_DIV | BPF_K] = true,
940 [BPF_ALU | BPF_DIV | BPF_X] = true,
941 [BPF_ALU | BPF_MOD | BPF_K] = true,
942 [BPF_ALU | BPF_MOD | BPF_X] = true,
943 [BPF_ALU | BPF_AND | BPF_K] = true,
944 [BPF_ALU | BPF_AND | BPF_X] = true,
945 [BPF_ALU | BPF_OR | BPF_K] = true,
946 [BPF_ALU | BPF_OR | BPF_X] = true,
947 [BPF_ALU | BPF_XOR | BPF_K] = true,
948 [BPF_ALU | BPF_XOR | BPF_X] = true,
949 [BPF_ALU | BPF_LSH | BPF_K] = true,
950 [BPF_ALU | BPF_LSH | BPF_X] = true,
951 [BPF_ALU | BPF_RSH | BPF_K] = true,
952 [BPF_ALU | BPF_RSH | BPF_X] = true,
953 [BPF_ALU | BPF_NEG] = true,
954 /* Load instructions */
955 [BPF_LD | BPF_W | BPF_ABS] = true,
956 [BPF_LD | BPF_H | BPF_ABS] = true,
957 [BPF_LD | BPF_B | BPF_ABS] = true,
958 [BPF_LD | BPF_W | BPF_LEN] = true,
959 [BPF_LD | BPF_W | BPF_IND] = true,
960 [BPF_LD | BPF_H | BPF_IND] = true,
961 [BPF_LD | BPF_B | BPF_IND] = true,
962 [BPF_LD | BPF_IMM] = true,
963 [BPF_LD | BPF_MEM] = true,
964 [BPF_LDX | BPF_W | BPF_LEN] = true,
965 [BPF_LDX | BPF_B | BPF_MSH] = true,
966 [BPF_LDX | BPF_IMM] = true,
967 [BPF_LDX | BPF_MEM] = true,
968 /* Store instructions */
971 /* Misc instructions */
972 [BPF_MISC | BPF_TAX] = true,
973 [BPF_MISC | BPF_TXA] = true,
974 /* Return instructions */
975 [BPF_RET | BPF_K] = true,
976 [BPF_RET | BPF_A] = true,
977 /* Jump instructions */
978 [BPF_JMP | BPF_JA] = true,
979 [BPF_JMP | BPF_JEQ | BPF_K] = true,
980 [BPF_JMP | BPF_JEQ | BPF_X] = true,
981 [BPF_JMP | BPF_JGE | BPF_K] = true,
982 [BPF_JMP | BPF_JGE | BPF_X] = true,
983 [BPF_JMP | BPF_JGT | BPF_K] = true,
984 [BPF_JMP | BPF_JGT | BPF_X] = true,
985 [BPF_JMP | BPF_JSET | BPF_K] = true,
986 [BPF_JMP | BPF_JSET | BPF_X] = true,
989 if (code_to_probe >= ARRAY_SIZE(codes))
992 return codes[code_to_probe];
995 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1000 if (flen == 0 || flen > BPF_MAXINSNS)
1007 * bpf_check_classic - verify socket filter code
1008 * @filter: filter to verify
1009 * @flen: length of filter
1011 * Check the user's filter code. If we let some ugly
1012 * filter code slip through kaboom! The filter must contain
1013 * no references or jumps that are out of range, no illegal
1014 * instructions, and must end with a RET instruction.
1016 * All jumps are forward as they are not signed.
1018 * Returns 0 if the rule set is legal or -EINVAL if not.
1020 static int bpf_check_classic(const struct sock_filter *filter,
1026 /* Check the filter code now */
1027 for (pc = 0; pc < flen; pc++) {
1028 const struct sock_filter *ftest = &filter[pc];
1030 /* May we actually operate on this code? */
1031 if (!chk_code_allowed(ftest->code))
1034 /* Some instructions need special checks */
1035 switch (ftest->code) {
1036 case BPF_ALU | BPF_DIV | BPF_K:
1037 case BPF_ALU | BPF_MOD | BPF_K:
1038 /* Check for division by zero */
1042 case BPF_ALU | BPF_LSH | BPF_K:
1043 case BPF_ALU | BPF_RSH | BPF_K:
1047 case BPF_LD | BPF_MEM:
1048 case BPF_LDX | BPF_MEM:
1051 /* Check for invalid memory addresses */
1052 if (ftest->k >= BPF_MEMWORDS)
1055 case BPF_JMP | BPF_JA:
1056 /* Note, the large ftest->k might cause loops.
1057 * Compare this with conditional jumps below,
1058 * where offsets are limited. --ANK (981016)
1060 if (ftest->k >= (unsigned int)(flen - pc - 1))
1063 case BPF_JMP | BPF_JEQ | BPF_K:
1064 case BPF_JMP | BPF_JEQ | BPF_X:
1065 case BPF_JMP | BPF_JGE | BPF_K:
1066 case BPF_JMP | BPF_JGE | BPF_X:
1067 case BPF_JMP | BPF_JGT | BPF_K:
1068 case BPF_JMP | BPF_JGT | BPF_X:
1069 case BPF_JMP | BPF_JSET | BPF_K:
1070 case BPF_JMP | BPF_JSET | BPF_X:
1071 /* Both conditionals must be safe */
1072 if (pc + ftest->jt + 1 >= flen ||
1073 pc + ftest->jf + 1 >= flen)
1076 case BPF_LD | BPF_W | BPF_ABS:
1077 case BPF_LD | BPF_H | BPF_ABS:
1078 case BPF_LD | BPF_B | BPF_ABS:
1080 if (bpf_anc_helper(ftest) & BPF_ANC)
1082 /* Ancillary operation unknown or unsupported */
1083 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1088 /* Last instruction must be a RET code */
1089 switch (filter[flen - 1].code) {
1090 case BPF_RET | BPF_K:
1091 case BPF_RET | BPF_A:
1092 return check_load_and_stores(filter, flen);
1098 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1099 const struct sock_fprog *fprog)
1101 unsigned int fsize = bpf_classic_proglen(fprog);
1102 struct sock_fprog_kern *fkprog;
1104 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1108 fkprog = fp->orig_prog;
1109 fkprog->len = fprog->len;
1111 fkprog->filter = kmemdup(fp->insns, fsize,
1112 GFP_KERNEL | __GFP_NOWARN);
1113 if (!fkprog->filter) {
1114 kfree(fp->orig_prog);
1121 static void bpf_release_orig_filter(struct bpf_prog *fp)
1123 struct sock_fprog_kern *fprog = fp->orig_prog;
1126 kfree(fprog->filter);
1131 static void __bpf_prog_release(struct bpf_prog *prog)
1133 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1136 bpf_release_orig_filter(prog);
1137 bpf_prog_free(prog);
1141 static void __sk_filter_release(struct sk_filter *fp)
1143 __bpf_prog_release(fp->prog);
1148 * sk_filter_release_rcu - Release a socket filter by rcu_head
1149 * @rcu: rcu_head that contains the sk_filter to free
1151 static void sk_filter_release_rcu(struct rcu_head *rcu)
1153 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1155 __sk_filter_release(fp);
1159 * sk_filter_release - release a socket filter
1160 * @fp: filter to remove
1162 * Remove a filter from a socket and release its resources.
1164 static void sk_filter_release(struct sk_filter *fp)
1166 if (refcount_dec_and_test(&fp->refcnt))
1167 call_rcu(&fp->rcu, sk_filter_release_rcu);
1170 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1172 u32 filter_size = bpf_prog_size(fp->prog->len);
1174 atomic_sub(filter_size, &sk->sk_omem_alloc);
1175 sk_filter_release(fp);
1178 /* try to charge the socket memory if there is space available
1179 * return true on success
1181 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1183 u32 filter_size = bpf_prog_size(fp->prog->len);
1185 /* same check as in sock_kmalloc() */
1186 if (filter_size <= sysctl_optmem_max &&
1187 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1188 atomic_add(filter_size, &sk->sk_omem_alloc);
1194 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1196 if (!refcount_inc_not_zero(&fp->refcnt))
1199 if (!__sk_filter_charge(sk, fp)) {
1200 sk_filter_release(fp);
1206 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1208 struct sock_filter *old_prog;
1209 struct bpf_prog *old_fp;
1210 int err, new_len, old_len = fp->len;
1211 bool seen_ld_abs = false;
1213 /* We are free to overwrite insns et al right here as it
1214 * won't be used at this point in time anymore internally
1215 * after the migration to the internal BPF instruction
1218 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1219 sizeof(struct bpf_insn));
1221 /* Conversion cannot happen on overlapping memory areas,
1222 * so we need to keep the user BPF around until the 2nd
1223 * pass. At this time, the user BPF is stored in fp->insns.
1225 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1226 GFP_KERNEL | __GFP_NOWARN);
1232 /* 1st pass: calculate the new program length. */
1233 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1238 /* Expand fp for appending the new filter representation. */
1240 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1242 /* The old_fp is still around in case we couldn't
1243 * allocate new memory, so uncharge on that one.
1252 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1253 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1256 /* 2nd bpf_convert_filter() can fail only if it fails
1257 * to allocate memory, remapping must succeed. Note,
1258 * that at this time old_fp has already been released
1263 fp = bpf_prog_select_runtime(fp, &err);
1273 __bpf_prog_release(fp);
1274 return ERR_PTR(err);
1277 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1278 bpf_aux_classic_check_t trans)
1282 fp->bpf_func = NULL;
1285 err = bpf_check_classic(fp->insns, fp->len);
1287 __bpf_prog_release(fp);
1288 return ERR_PTR(err);
1291 /* There might be additional checks and transformations
1292 * needed on classic filters, f.e. in case of seccomp.
1295 err = trans(fp->insns, fp->len);
1297 __bpf_prog_release(fp);
1298 return ERR_PTR(err);
1302 /* Probe if we can JIT compile the filter and if so, do
1303 * the compilation of the filter.
1305 bpf_jit_compile(fp);
1307 /* JIT compiler couldn't process this filter, so do the
1308 * internal BPF translation for the optimized interpreter.
1311 fp = bpf_migrate_filter(fp);
1317 * bpf_prog_create - create an unattached filter
1318 * @pfp: the unattached filter that is created
1319 * @fprog: the filter program
1321 * Create a filter independent of any socket. We first run some
1322 * sanity checks on it to make sure it does not explode on us later.
1323 * If an error occurs or there is insufficient memory for the filter
1324 * a negative errno code is returned. On success the return is zero.
1326 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1328 unsigned int fsize = bpf_classic_proglen(fprog);
1329 struct bpf_prog *fp;
1331 /* Make sure new filter is there and in the right amounts. */
1332 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1335 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1339 memcpy(fp->insns, fprog->filter, fsize);
1341 fp->len = fprog->len;
1342 /* Since unattached filters are not copied back to user
1343 * space through sk_get_filter(), we do not need to hold
1344 * a copy here, and can spare us the work.
1346 fp->orig_prog = NULL;
1348 /* bpf_prepare_filter() already takes care of freeing
1349 * memory in case something goes wrong.
1351 fp = bpf_prepare_filter(fp, NULL);
1358 EXPORT_SYMBOL_GPL(bpf_prog_create);
1361 * bpf_prog_create_from_user - create an unattached filter from user buffer
1362 * @pfp: the unattached filter that is created
1363 * @fprog: the filter program
1364 * @trans: post-classic verifier transformation handler
1365 * @save_orig: save classic BPF program
1367 * This function effectively does the same as bpf_prog_create(), only
1368 * that it builds up its insns buffer from user space provided buffer.
1369 * It also allows for passing a bpf_aux_classic_check_t handler.
1371 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1372 bpf_aux_classic_check_t trans, bool save_orig)
1374 unsigned int fsize = bpf_classic_proglen(fprog);
1375 struct bpf_prog *fp;
1378 /* Make sure new filter is there and in the right amounts. */
1379 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1382 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1386 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1387 __bpf_prog_free(fp);
1391 fp->len = fprog->len;
1392 fp->orig_prog = NULL;
1395 err = bpf_prog_store_orig_filter(fp, fprog);
1397 __bpf_prog_free(fp);
1402 /* bpf_prepare_filter() already takes care of freeing
1403 * memory in case something goes wrong.
1405 fp = bpf_prepare_filter(fp, trans);
1412 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1414 void bpf_prog_destroy(struct bpf_prog *fp)
1416 __bpf_prog_release(fp);
1418 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1420 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1422 struct sk_filter *fp, *old_fp;
1424 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1430 if (!__sk_filter_charge(sk, fp)) {
1434 refcount_set(&fp->refcnt, 1);
1436 old_fp = rcu_dereference_protected(sk->sk_filter,
1437 lockdep_sock_is_held(sk));
1438 rcu_assign_pointer(sk->sk_filter, fp);
1441 sk_filter_uncharge(sk, old_fp);
1446 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1448 struct bpf_prog *old_prog;
1451 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1454 if (sk_unhashed(sk) && sk->sk_reuseport) {
1455 err = reuseport_alloc(sk);
1458 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1459 /* The socket wasn't bound with SO_REUSEPORT */
1463 old_prog = reuseport_attach_prog(sk, prog);
1465 bpf_prog_destroy(old_prog);
1471 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1473 unsigned int fsize = bpf_classic_proglen(fprog);
1474 struct bpf_prog *prog;
1477 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1478 return ERR_PTR(-EPERM);
1480 /* Make sure new filter is there and in the right amounts. */
1481 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1482 return ERR_PTR(-EINVAL);
1484 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1486 return ERR_PTR(-ENOMEM);
1488 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1489 __bpf_prog_free(prog);
1490 return ERR_PTR(-EFAULT);
1493 prog->len = fprog->len;
1495 err = bpf_prog_store_orig_filter(prog, fprog);
1497 __bpf_prog_free(prog);
1498 return ERR_PTR(-ENOMEM);
1501 /* bpf_prepare_filter() already takes care of freeing
1502 * memory in case something goes wrong.
1504 return bpf_prepare_filter(prog, NULL);
1508 * sk_attach_filter - attach a socket filter
1509 * @fprog: the filter program
1510 * @sk: the socket to use
1512 * Attach the user's filter code. We first run some sanity checks on
1513 * it to make sure it does not explode on us later. If an error
1514 * occurs or there is insufficient memory for the filter a negative
1515 * errno code is returned. On success the return is zero.
1517 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1519 struct bpf_prog *prog = __get_filter(fprog, sk);
1523 return PTR_ERR(prog);
1525 err = __sk_attach_prog(prog, sk);
1527 __bpf_prog_release(prog);
1533 EXPORT_SYMBOL_GPL(sk_attach_filter);
1535 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1537 struct bpf_prog *prog = __get_filter(fprog, sk);
1541 return PTR_ERR(prog);
1543 err = __reuseport_attach_prog(prog, sk);
1545 __bpf_prog_release(prog);
1552 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1554 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1555 return ERR_PTR(-EPERM);
1557 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1560 int sk_attach_bpf(u32 ufd, struct sock *sk)
1562 struct bpf_prog *prog = __get_bpf(ufd, sk);
1566 return PTR_ERR(prog);
1568 err = __sk_attach_prog(prog, sk);
1577 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1579 struct bpf_prog *prog = __get_bpf(ufd, sk);
1583 return PTR_ERR(prog);
1585 err = __reuseport_attach_prog(prog, sk);
1594 struct bpf_scratchpad {
1596 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1597 u8 buff[MAX_BPF_STACK];
1601 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1603 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1604 unsigned int write_len)
1606 return skb_ensure_writable(skb, write_len);
1609 static inline int bpf_try_make_writable(struct sk_buff *skb,
1610 unsigned int write_len)
1612 int err = __bpf_try_make_writable(skb, write_len);
1614 bpf_compute_data_pointers(skb);
1618 static int bpf_try_make_head_writable(struct sk_buff *skb)
1620 return bpf_try_make_writable(skb, skb_headlen(skb));
1623 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1625 if (skb_at_tc_ingress(skb))
1626 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1629 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1631 if (skb_at_tc_ingress(skb))
1632 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1635 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1636 const void *, from, u32, len, u64, flags)
1640 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1642 if (unlikely(offset > 0xffff))
1644 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1647 ptr = skb->data + offset;
1648 if (flags & BPF_F_RECOMPUTE_CSUM)
1649 __skb_postpull_rcsum(skb, ptr, len, offset);
1651 memcpy(ptr, from, len);
1653 if (flags & BPF_F_RECOMPUTE_CSUM)
1654 __skb_postpush_rcsum(skb, ptr, len, offset);
1655 if (flags & BPF_F_INVALIDATE_HASH)
1656 skb_clear_hash(skb);
1661 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1662 .func = bpf_skb_store_bytes,
1664 .ret_type = RET_INTEGER,
1665 .arg1_type = ARG_PTR_TO_CTX,
1666 .arg2_type = ARG_ANYTHING,
1667 .arg3_type = ARG_PTR_TO_MEM,
1668 .arg4_type = ARG_CONST_SIZE,
1669 .arg5_type = ARG_ANYTHING,
1672 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1673 void *, to, u32, len)
1677 if (unlikely(offset > 0xffff))
1680 ptr = skb_header_pointer(skb, offset, len, to);
1684 memcpy(to, ptr, len);
1692 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1693 .func = bpf_skb_load_bytes,
1695 .ret_type = RET_INTEGER,
1696 .arg1_type = ARG_PTR_TO_CTX,
1697 .arg2_type = ARG_ANYTHING,
1698 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1699 .arg4_type = ARG_CONST_SIZE,
1702 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1703 u32, offset, void *, to, u32, len, u32, start_header)
1707 if (unlikely(offset > 0xffff || len > skb_headlen(skb)))
1710 switch (start_header) {
1711 case BPF_HDR_START_MAC:
1712 ptr = skb_mac_header(skb) + offset;
1714 case BPF_HDR_START_NET:
1715 ptr = skb_network_header(skb) + offset;
1721 if (likely(ptr >= skb_mac_header(skb) &&
1722 ptr + len <= skb_tail_pointer(skb))) {
1723 memcpy(to, ptr, len);
1732 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1733 .func = bpf_skb_load_bytes_relative,
1735 .ret_type = RET_INTEGER,
1736 .arg1_type = ARG_PTR_TO_CTX,
1737 .arg2_type = ARG_ANYTHING,
1738 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1739 .arg4_type = ARG_CONST_SIZE,
1740 .arg5_type = ARG_ANYTHING,
1743 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1745 /* Idea is the following: should the needed direct read/write
1746 * test fail during runtime, we can pull in more data and redo
1747 * again, since implicitly, we invalidate previous checks here.
1749 * Or, since we know how much we need to make read/writeable,
1750 * this can be done once at the program beginning for direct
1751 * access case. By this we overcome limitations of only current
1752 * headroom being accessible.
1754 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1757 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1758 .func = bpf_skb_pull_data,
1760 .ret_type = RET_INTEGER,
1761 .arg1_type = ARG_PTR_TO_CTX,
1762 .arg2_type = ARG_ANYTHING,
1765 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1766 u64, from, u64, to, u64, flags)
1770 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1772 if (unlikely(offset > 0xffff || offset & 1))
1774 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1777 ptr = (__sum16 *)(skb->data + offset);
1778 switch (flags & BPF_F_HDR_FIELD_MASK) {
1780 if (unlikely(from != 0))
1783 csum_replace_by_diff(ptr, to);
1786 csum_replace2(ptr, from, to);
1789 csum_replace4(ptr, from, to);
1798 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1799 .func = bpf_l3_csum_replace,
1801 .ret_type = RET_INTEGER,
1802 .arg1_type = ARG_PTR_TO_CTX,
1803 .arg2_type = ARG_ANYTHING,
1804 .arg3_type = ARG_ANYTHING,
1805 .arg4_type = ARG_ANYTHING,
1806 .arg5_type = ARG_ANYTHING,
1809 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1810 u64, from, u64, to, u64, flags)
1812 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1813 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1814 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1817 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1818 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1820 if (unlikely(offset > 0xffff || offset & 1))
1822 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1825 ptr = (__sum16 *)(skb->data + offset);
1826 if (is_mmzero && !do_mforce && !*ptr)
1829 switch (flags & BPF_F_HDR_FIELD_MASK) {
1831 if (unlikely(from != 0))
1834 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1837 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1840 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1846 if (is_mmzero && !*ptr)
1847 *ptr = CSUM_MANGLED_0;
1851 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1852 .func = bpf_l4_csum_replace,
1854 .ret_type = RET_INTEGER,
1855 .arg1_type = ARG_PTR_TO_CTX,
1856 .arg2_type = ARG_ANYTHING,
1857 .arg3_type = ARG_ANYTHING,
1858 .arg4_type = ARG_ANYTHING,
1859 .arg5_type = ARG_ANYTHING,
1862 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1863 __be32 *, to, u32, to_size, __wsum, seed)
1865 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1866 u32 diff_size = from_size + to_size;
1869 /* This is quite flexible, some examples:
1871 * from_size == 0, to_size > 0, seed := csum --> pushing data
1872 * from_size > 0, to_size == 0, seed := csum --> pulling data
1873 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1875 * Even for diffing, from_size and to_size don't need to be equal.
1877 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1878 diff_size > sizeof(sp->diff)))
1881 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1882 sp->diff[j] = ~from[i];
1883 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1884 sp->diff[j] = to[i];
1886 return csum_partial(sp->diff, diff_size, seed);
1889 static const struct bpf_func_proto bpf_csum_diff_proto = {
1890 .func = bpf_csum_diff,
1893 .ret_type = RET_INTEGER,
1894 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1895 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1896 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1897 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1898 .arg5_type = ARG_ANYTHING,
1901 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1903 /* The interface is to be used in combination with bpf_csum_diff()
1904 * for direct packet writes. csum rotation for alignment as well
1905 * as emulating csum_sub() can be done from the eBPF program.
1907 if (skb->ip_summed == CHECKSUM_COMPLETE)
1908 return (skb->csum = csum_add(skb->csum, csum));
1913 static const struct bpf_func_proto bpf_csum_update_proto = {
1914 .func = bpf_csum_update,
1916 .ret_type = RET_INTEGER,
1917 .arg1_type = ARG_PTR_TO_CTX,
1918 .arg2_type = ARG_ANYTHING,
1921 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1923 return dev_forward_skb(dev, skb);
1926 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1927 struct sk_buff *skb)
1929 int ret = ____dev_forward_skb(dev, skb);
1933 ret = netif_rx(skb);
1939 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1943 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1944 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1951 __this_cpu_inc(xmit_recursion);
1952 ret = dev_queue_xmit(skb);
1953 __this_cpu_dec(xmit_recursion);
1958 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1961 /* skb->mac_len is not set on normal egress */
1962 unsigned int mlen = skb->network_header - skb->mac_header;
1964 __skb_pull(skb, mlen);
1966 /* At ingress, the mac header has already been pulled once.
1967 * At egress, skb_pospull_rcsum has to be done in case that
1968 * the skb is originated from ingress (i.e. a forwarded skb)
1969 * to ensure that rcsum starts at net header.
1971 if (!skb_at_tc_ingress(skb))
1972 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1973 skb_pop_mac_header(skb);
1974 skb_reset_mac_len(skb);
1975 return flags & BPF_F_INGRESS ?
1976 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1979 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1982 /* Verify that a link layer header is carried */
1983 if (unlikely(skb->mac_header >= skb->network_header)) {
1988 bpf_push_mac_rcsum(skb);
1989 return flags & BPF_F_INGRESS ?
1990 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1993 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1996 if (dev_is_mac_header_xmit(dev))
1997 return __bpf_redirect_common(skb, dev, flags);
1999 return __bpf_redirect_no_mac(skb, dev, flags);
2002 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2004 struct net_device *dev;
2005 struct sk_buff *clone;
2008 if (unlikely(flags & ~(BPF_F_INGRESS)))
2011 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2015 clone = skb_clone(skb, GFP_ATOMIC);
2016 if (unlikely(!clone))
2019 /* For direct write, we need to keep the invariant that the skbs
2020 * we're dealing with need to be uncloned. Should uncloning fail
2021 * here, we need to free the just generated clone to unclone once
2024 ret = bpf_try_make_head_writable(skb);
2025 if (unlikely(ret)) {
2030 return __bpf_redirect(clone, dev, flags);
2033 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2034 .func = bpf_clone_redirect,
2036 .ret_type = RET_INTEGER,
2037 .arg1_type = ARG_PTR_TO_CTX,
2038 .arg2_type = ARG_ANYTHING,
2039 .arg3_type = ARG_ANYTHING,
2042 struct redirect_info {
2045 struct bpf_map *map;
2046 struct bpf_map *map_to_flush;
2047 unsigned long map_owner;
2050 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
2052 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2054 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2056 if (unlikely(flags & ~(BPF_F_INGRESS)))
2059 ri->ifindex = ifindex;
2062 return TC_ACT_REDIRECT;
2065 int skb_do_redirect(struct sk_buff *skb)
2067 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2068 struct net_device *dev;
2070 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2072 if (unlikely(!dev)) {
2077 return __bpf_redirect(skb, dev, ri->flags);
2080 static const struct bpf_func_proto bpf_redirect_proto = {
2081 .func = bpf_redirect,
2083 .ret_type = RET_INTEGER,
2084 .arg1_type = ARG_ANYTHING,
2085 .arg2_type = ARG_ANYTHING,
2088 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
2089 struct bpf_map *, map, void *, key, u64, flags)
2091 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2093 /* If user passes invalid input drop the packet. */
2094 if (unlikely(flags & ~(BPF_F_INGRESS)))
2097 tcb->bpf.flags = flags;
2098 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
2099 if (!tcb->bpf.sk_redir)
2105 static const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
2106 .func = bpf_sk_redirect_hash,
2108 .ret_type = RET_INTEGER,
2109 .arg1_type = ARG_PTR_TO_CTX,
2110 .arg2_type = ARG_CONST_MAP_PTR,
2111 .arg3_type = ARG_PTR_TO_MAP_KEY,
2112 .arg4_type = ARG_ANYTHING,
2115 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
2116 struct bpf_map *, map, u32, key, u64, flags)
2118 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2120 /* If user passes invalid input drop the packet. */
2121 if (unlikely(flags & ~(BPF_F_INGRESS)))
2124 tcb->bpf.flags = flags;
2125 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
2126 if (!tcb->bpf.sk_redir)
2132 struct sock *do_sk_redirect_map(struct sk_buff *skb)
2134 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2136 return tcb->bpf.sk_redir;
2139 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
2140 .func = bpf_sk_redirect_map,
2142 .ret_type = RET_INTEGER,
2143 .arg1_type = ARG_PTR_TO_CTX,
2144 .arg2_type = ARG_CONST_MAP_PTR,
2145 .arg3_type = ARG_ANYTHING,
2146 .arg4_type = ARG_ANYTHING,
2149 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg_buff *, msg,
2150 struct bpf_map *, map, void *, key, u64, flags)
2152 /* If user passes invalid input drop the packet. */
2153 if (unlikely(flags & ~(BPF_F_INGRESS)))
2157 msg->sk_redir = __sock_hash_lookup_elem(map, key);
2164 static const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
2165 .func = bpf_msg_redirect_hash,
2167 .ret_type = RET_INTEGER,
2168 .arg1_type = ARG_PTR_TO_CTX,
2169 .arg2_type = ARG_CONST_MAP_PTR,
2170 .arg3_type = ARG_PTR_TO_MAP_KEY,
2171 .arg4_type = ARG_ANYTHING,
2174 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg_buff *, msg,
2175 struct bpf_map *, map, u32, key, u64, flags)
2177 /* If user passes invalid input drop the packet. */
2178 if (unlikely(flags & ~(BPF_F_INGRESS)))
2182 msg->sk_redir = __sock_map_lookup_elem(map, key);
2189 struct sock *do_msg_redirect_map(struct sk_msg_buff *msg)
2191 return msg->sk_redir;
2194 static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
2195 .func = bpf_msg_redirect_map,
2197 .ret_type = RET_INTEGER,
2198 .arg1_type = ARG_PTR_TO_CTX,
2199 .arg2_type = ARG_CONST_MAP_PTR,
2200 .arg3_type = ARG_ANYTHING,
2201 .arg4_type = ARG_ANYTHING,
2204 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg_buff *, msg, u32, bytes)
2206 msg->apply_bytes = bytes;
2210 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2211 .func = bpf_msg_apply_bytes,
2213 .ret_type = RET_INTEGER,
2214 .arg1_type = ARG_PTR_TO_CTX,
2215 .arg2_type = ARG_ANYTHING,
2218 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg_buff *, msg, u32, bytes)
2220 msg->cork_bytes = bytes;
2224 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2225 .func = bpf_msg_cork_bytes,
2227 .ret_type = RET_INTEGER,
2228 .arg1_type = ARG_PTR_TO_CTX,
2229 .arg2_type = ARG_ANYTHING,
2232 BPF_CALL_4(bpf_msg_pull_data,
2233 struct sk_msg_buff *, msg, u32, start, u32, end, u64, flags)
2235 unsigned int len = 0, offset = 0, copy = 0;
2236 struct scatterlist *sg = msg->sg_data;
2237 int first_sg, last_sg, i, shift;
2238 unsigned char *p, *to, *from;
2239 int bytes = end - start;
2242 if (unlikely(flags || end <= start))
2245 /* First find the starting scatterlist element */
2250 if (start < offset + len)
2253 if (i == MAX_SKB_FRAGS)
2255 } while (i != msg->sg_end);
2257 if (unlikely(start >= offset + len))
2260 if (!msg->sg_copy[i] && bytes <= len)
2265 /* At this point we need to linearize multiple scatterlist
2266 * elements or a single shared page. Either way we need to
2267 * copy into a linear buffer exclusively owned by BPF. Then
2268 * place the buffer in the scatterlist and fixup the original
2269 * entries by removing the entries now in the linear buffer
2270 * and shifting the remaining entries. For now we do not try
2271 * to copy partial entries to avoid complexity of running out
2272 * of sg_entry slots. The downside is reading a single byte
2273 * will copy the entire sg entry.
2276 copy += sg[i].length;
2278 if (i == MAX_SKB_FRAGS)
2282 } while (i != msg->sg_end);
2285 if (unlikely(copy < end - start))
2288 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC, get_order(copy));
2289 if (unlikely(!page))
2291 p = page_address(page);
2296 from = sg_virt(&sg[i]);
2300 memcpy(to, from, len);
2303 put_page(sg_page(&sg[i]));
2306 if (i == MAX_SKB_FRAGS)
2308 } while (i != last_sg);
2310 sg[first_sg].length = copy;
2311 sg_set_page(&sg[first_sg], page, copy, 0);
2313 /* To repair sg ring we need to shift entries. If we only
2314 * had a single entry though we can just replace it and
2315 * be done. Otherwise walk the ring and shift the entries.
2317 shift = last_sg - first_sg - 1;
2325 if (i + shift >= MAX_SKB_FRAGS)
2326 move_from = i + shift - MAX_SKB_FRAGS;
2328 move_from = i + shift;
2330 if (move_from == msg->sg_end)
2333 sg[i] = sg[move_from];
2334 sg[move_from].length = 0;
2335 sg[move_from].page_link = 0;
2336 sg[move_from].offset = 0;
2339 if (i == MAX_SKB_FRAGS)
2342 msg->sg_end -= shift;
2343 if (msg->sg_end < 0)
2344 msg->sg_end += MAX_SKB_FRAGS;
2346 msg->data = sg_virt(&sg[i]) + start - offset;
2347 msg->data_end = msg->data + bytes;
2352 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2353 .func = bpf_msg_pull_data,
2355 .ret_type = RET_INTEGER,
2356 .arg1_type = ARG_PTR_TO_CTX,
2357 .arg2_type = ARG_ANYTHING,
2358 .arg3_type = ARG_ANYTHING,
2359 .arg4_type = ARG_ANYTHING,
2362 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2364 return task_get_classid(skb);
2367 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2368 .func = bpf_get_cgroup_classid,
2370 .ret_type = RET_INTEGER,
2371 .arg1_type = ARG_PTR_TO_CTX,
2374 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2376 return dst_tclassid(skb);
2379 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2380 .func = bpf_get_route_realm,
2382 .ret_type = RET_INTEGER,
2383 .arg1_type = ARG_PTR_TO_CTX,
2386 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2388 /* If skb_clear_hash() was called due to mangling, we can
2389 * trigger SW recalculation here. Later access to hash
2390 * can then use the inline skb->hash via context directly
2391 * instead of calling this helper again.
2393 return skb_get_hash(skb);
2396 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2397 .func = bpf_get_hash_recalc,
2399 .ret_type = RET_INTEGER,
2400 .arg1_type = ARG_PTR_TO_CTX,
2403 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2405 /* After all direct packet write, this can be used once for
2406 * triggering a lazy recalc on next skb_get_hash() invocation.
2408 skb_clear_hash(skb);
2412 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2413 .func = bpf_set_hash_invalid,
2415 .ret_type = RET_INTEGER,
2416 .arg1_type = ARG_PTR_TO_CTX,
2419 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2421 /* Set user specified hash as L4(+), so that it gets returned
2422 * on skb_get_hash() call unless BPF prog later on triggers a
2425 __skb_set_sw_hash(skb, hash, true);
2429 static const struct bpf_func_proto bpf_set_hash_proto = {
2430 .func = bpf_set_hash,
2432 .ret_type = RET_INTEGER,
2433 .arg1_type = ARG_PTR_TO_CTX,
2434 .arg2_type = ARG_ANYTHING,
2437 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2442 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2443 vlan_proto != htons(ETH_P_8021AD)))
2444 vlan_proto = htons(ETH_P_8021Q);
2446 bpf_push_mac_rcsum(skb);
2447 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2448 bpf_pull_mac_rcsum(skb);
2450 bpf_compute_data_pointers(skb);
2454 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2455 .func = bpf_skb_vlan_push,
2457 .ret_type = RET_INTEGER,
2458 .arg1_type = ARG_PTR_TO_CTX,
2459 .arg2_type = ARG_ANYTHING,
2460 .arg3_type = ARG_ANYTHING,
2463 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2467 bpf_push_mac_rcsum(skb);
2468 ret = skb_vlan_pop(skb);
2469 bpf_pull_mac_rcsum(skb);
2471 bpf_compute_data_pointers(skb);
2475 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2476 .func = bpf_skb_vlan_pop,
2478 .ret_type = RET_INTEGER,
2479 .arg1_type = ARG_PTR_TO_CTX,
2482 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2484 /* Caller already did skb_cow() with len as headroom,
2485 * so no need to do it here.
2488 memmove(skb->data, skb->data + len, off);
2489 memset(skb->data + off, 0, len);
2491 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2492 * needed here as it does not change the skb->csum
2493 * result for checksum complete when summing over
2499 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2501 /* skb_ensure_writable() is not needed here, as we're
2502 * already working on an uncloned skb.
2504 if (unlikely(!pskb_may_pull(skb, off + len)))
2507 skb_postpull_rcsum(skb, skb->data + off, len);
2508 memmove(skb->data + len, skb->data, off);
2509 __skb_pull(skb, len);
2514 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2516 bool trans_same = skb->transport_header == skb->network_header;
2519 /* There's no need for __skb_push()/__skb_pull() pair to
2520 * get to the start of the mac header as we're guaranteed
2521 * to always start from here under eBPF.
2523 ret = bpf_skb_generic_push(skb, off, len);
2525 skb->mac_header -= len;
2526 skb->network_header -= len;
2528 skb->transport_header = skb->network_header;
2534 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2536 bool trans_same = skb->transport_header == skb->network_header;
2539 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2540 ret = bpf_skb_generic_pop(skb, off, len);
2542 skb->mac_header += len;
2543 skb->network_header += len;
2545 skb->transport_header = skb->network_header;
2551 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2553 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2554 u32 off = skb_mac_header_len(skb);
2557 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2558 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2561 ret = skb_cow(skb, len_diff);
2562 if (unlikely(ret < 0))
2565 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2566 if (unlikely(ret < 0))
2569 if (skb_is_gso(skb)) {
2570 struct skb_shared_info *shinfo = skb_shinfo(skb);
2572 /* SKB_GSO_TCPV4 needs to be changed into
2575 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2576 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2577 shinfo->gso_type |= SKB_GSO_TCPV6;
2580 /* Due to IPv6 header, MSS needs to be downgraded. */
2581 skb_decrease_gso_size(shinfo, len_diff);
2582 /* Header must be checked, and gso_segs recomputed. */
2583 shinfo->gso_type |= SKB_GSO_DODGY;
2584 shinfo->gso_segs = 0;
2587 skb->protocol = htons(ETH_P_IPV6);
2588 skb_clear_hash(skb);
2593 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2595 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2596 u32 off = skb_mac_header_len(skb);
2599 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2600 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2603 ret = skb_unclone(skb, GFP_ATOMIC);
2604 if (unlikely(ret < 0))
2607 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2608 if (unlikely(ret < 0))
2611 if (skb_is_gso(skb)) {
2612 struct skb_shared_info *shinfo = skb_shinfo(skb);
2614 /* SKB_GSO_TCPV6 needs to be changed into
2617 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2618 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2619 shinfo->gso_type |= SKB_GSO_TCPV4;
2622 /* Due to IPv4 header, MSS can be upgraded. */
2623 skb_increase_gso_size(shinfo, len_diff);
2624 /* Header must be checked, and gso_segs recomputed. */
2625 shinfo->gso_type |= SKB_GSO_DODGY;
2626 shinfo->gso_segs = 0;
2629 skb->protocol = htons(ETH_P_IP);
2630 skb_clear_hash(skb);
2635 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2637 __be16 from_proto = skb->protocol;
2639 if (from_proto == htons(ETH_P_IP) &&
2640 to_proto == htons(ETH_P_IPV6))
2641 return bpf_skb_proto_4_to_6(skb);
2643 if (from_proto == htons(ETH_P_IPV6) &&
2644 to_proto == htons(ETH_P_IP))
2645 return bpf_skb_proto_6_to_4(skb);
2650 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2655 if (unlikely(flags))
2658 /* General idea is that this helper does the basic groundwork
2659 * needed for changing the protocol, and eBPF program fills the
2660 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2661 * and other helpers, rather than passing a raw buffer here.
2663 * The rationale is to keep this minimal and without a need to
2664 * deal with raw packet data. F.e. even if we would pass buffers
2665 * here, the program still needs to call the bpf_lX_csum_replace()
2666 * helpers anyway. Plus, this way we keep also separation of
2667 * concerns, since f.e. bpf_skb_store_bytes() should only take
2670 * Currently, additional options and extension header space are
2671 * not supported, but flags register is reserved so we can adapt
2672 * that. For offloads, we mark packet as dodgy, so that headers
2673 * need to be verified first.
2675 ret = bpf_skb_proto_xlat(skb, proto);
2676 bpf_compute_data_pointers(skb);
2680 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2681 .func = bpf_skb_change_proto,
2683 .ret_type = RET_INTEGER,
2684 .arg1_type = ARG_PTR_TO_CTX,
2685 .arg2_type = ARG_ANYTHING,
2686 .arg3_type = ARG_ANYTHING,
2689 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2691 /* We only allow a restricted subset to be changed for now. */
2692 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2693 !skb_pkt_type_ok(pkt_type)))
2696 skb->pkt_type = pkt_type;
2700 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2701 .func = bpf_skb_change_type,
2703 .ret_type = RET_INTEGER,
2704 .arg1_type = ARG_PTR_TO_CTX,
2705 .arg2_type = ARG_ANYTHING,
2708 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2710 switch (skb->protocol) {
2711 case htons(ETH_P_IP):
2712 return sizeof(struct iphdr);
2713 case htons(ETH_P_IPV6):
2714 return sizeof(struct ipv6hdr);
2720 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2722 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2725 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2726 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2729 ret = skb_cow(skb, len_diff);
2730 if (unlikely(ret < 0))
2733 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2734 if (unlikely(ret < 0))
2737 if (skb_is_gso(skb)) {
2738 struct skb_shared_info *shinfo = skb_shinfo(skb);
2740 /* Due to header grow, MSS needs to be downgraded. */
2741 skb_decrease_gso_size(shinfo, len_diff);
2742 /* Header must be checked, and gso_segs recomputed. */
2743 shinfo->gso_type |= SKB_GSO_DODGY;
2744 shinfo->gso_segs = 0;
2750 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2752 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2755 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2756 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2759 ret = skb_unclone(skb, GFP_ATOMIC);
2760 if (unlikely(ret < 0))
2763 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2764 if (unlikely(ret < 0))
2767 if (skb_is_gso(skb)) {
2768 struct skb_shared_info *shinfo = skb_shinfo(skb);
2770 /* Due to header shrink, MSS can be upgraded. */
2771 skb_increase_gso_size(shinfo, len_diff);
2772 /* Header must be checked, and gso_segs recomputed. */
2773 shinfo->gso_type |= SKB_GSO_DODGY;
2774 shinfo->gso_segs = 0;
2780 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2782 return skb->dev->mtu + skb->dev->hard_header_len;
2785 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2787 bool trans_same = skb->transport_header == skb->network_header;
2788 u32 len_cur, len_diff_abs = abs(len_diff);
2789 u32 len_min = bpf_skb_net_base_len(skb);
2790 u32 len_max = __bpf_skb_max_len(skb);
2791 __be16 proto = skb->protocol;
2792 bool shrink = len_diff < 0;
2795 if (unlikely(len_diff_abs > 0xfffU))
2797 if (unlikely(proto != htons(ETH_P_IP) &&
2798 proto != htons(ETH_P_IPV6)))
2801 len_cur = skb->len - skb_network_offset(skb);
2802 if (skb_transport_header_was_set(skb) && !trans_same)
2803 len_cur = skb_network_header_len(skb);
2804 if ((shrink && (len_diff_abs >= len_cur ||
2805 len_cur - len_diff_abs < len_min)) ||
2806 (!shrink && (skb->len + len_diff_abs > len_max &&
2810 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2811 bpf_skb_net_grow(skb, len_diff_abs);
2813 bpf_compute_data_pointers(skb);
2817 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2818 u32, mode, u64, flags)
2820 if (unlikely(flags))
2822 if (likely(mode == BPF_ADJ_ROOM_NET))
2823 return bpf_skb_adjust_net(skb, len_diff);
2828 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2829 .func = bpf_skb_adjust_room,
2831 .ret_type = RET_INTEGER,
2832 .arg1_type = ARG_PTR_TO_CTX,
2833 .arg2_type = ARG_ANYTHING,
2834 .arg3_type = ARG_ANYTHING,
2835 .arg4_type = ARG_ANYTHING,
2838 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2840 u32 min_len = skb_network_offset(skb);
2842 if (skb_transport_header_was_set(skb))
2843 min_len = skb_transport_offset(skb);
2844 if (skb->ip_summed == CHECKSUM_PARTIAL)
2845 min_len = skb_checksum_start_offset(skb) +
2846 skb->csum_offset + sizeof(__sum16);
2850 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2852 unsigned int old_len = skb->len;
2855 ret = __skb_grow_rcsum(skb, new_len);
2857 memset(skb->data + old_len, 0, new_len - old_len);
2861 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2863 return __skb_trim_rcsum(skb, new_len);
2866 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2869 u32 max_len = __bpf_skb_max_len(skb);
2870 u32 min_len = __bpf_skb_min_len(skb);
2873 if (unlikely(flags || new_len > max_len || new_len < min_len))
2875 if (skb->encapsulation)
2878 /* The basic idea of this helper is that it's performing the
2879 * needed work to either grow or trim an skb, and eBPF program
2880 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2881 * bpf_lX_csum_replace() and others rather than passing a raw
2882 * buffer here. This one is a slow path helper and intended
2883 * for replies with control messages.
2885 * Like in bpf_skb_change_proto(), we want to keep this rather
2886 * minimal and without protocol specifics so that we are able
2887 * to separate concerns as in bpf_skb_store_bytes() should only
2888 * be the one responsible for writing buffers.
2890 * It's really expected to be a slow path operation here for
2891 * control message replies, so we're implicitly linearizing,
2892 * uncloning and drop offloads from the skb by this.
2894 ret = __bpf_try_make_writable(skb, skb->len);
2896 if (new_len > skb->len)
2897 ret = bpf_skb_grow_rcsum(skb, new_len);
2898 else if (new_len < skb->len)
2899 ret = bpf_skb_trim_rcsum(skb, new_len);
2900 if (!ret && skb_is_gso(skb))
2904 bpf_compute_data_pointers(skb);
2908 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2909 .func = bpf_skb_change_tail,
2911 .ret_type = RET_INTEGER,
2912 .arg1_type = ARG_PTR_TO_CTX,
2913 .arg2_type = ARG_ANYTHING,
2914 .arg3_type = ARG_ANYTHING,
2917 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2920 u32 max_len = __bpf_skb_max_len(skb);
2921 u32 new_len = skb->len + head_room;
2924 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2925 new_len < skb->len))
2928 ret = skb_cow(skb, head_room);
2930 /* Idea for this helper is that we currently only
2931 * allow to expand on mac header. This means that
2932 * skb->protocol network header, etc, stay as is.
2933 * Compared to bpf_skb_change_tail(), we're more
2934 * flexible due to not needing to linearize or
2935 * reset GSO. Intention for this helper is to be
2936 * used by an L3 skb that needs to push mac header
2937 * for redirection into L2 device.
2939 __skb_push(skb, head_room);
2940 memset(skb->data, 0, head_room);
2941 skb_reset_mac_header(skb);
2944 bpf_compute_data_pointers(skb);
2948 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2949 .func = bpf_skb_change_head,
2951 .ret_type = RET_INTEGER,
2952 .arg1_type = ARG_PTR_TO_CTX,
2953 .arg2_type = ARG_ANYTHING,
2954 .arg3_type = ARG_ANYTHING,
2957 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
2959 return xdp_data_meta_unsupported(xdp) ? 0 :
2960 xdp->data - xdp->data_meta;
2963 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2965 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
2966 unsigned long metalen = xdp_get_metalen(xdp);
2967 void *data_start = xdp_frame_end + metalen;
2968 void *data = xdp->data + offset;
2970 if (unlikely(data < data_start ||
2971 data > xdp->data_end - ETH_HLEN))
2975 memmove(xdp->data_meta + offset,
2976 xdp->data_meta, metalen);
2977 xdp->data_meta += offset;
2983 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2984 .func = bpf_xdp_adjust_head,
2986 .ret_type = RET_INTEGER,
2987 .arg1_type = ARG_PTR_TO_CTX,
2988 .arg2_type = ARG_ANYTHING,
2991 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
2993 void *data_end = xdp->data_end + offset;
2995 /* only shrinking is allowed for now. */
2996 if (unlikely(offset >= 0))
2999 if (unlikely(data_end < xdp->data + ETH_HLEN))
3002 xdp->data_end = data_end;
3007 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3008 .func = bpf_xdp_adjust_tail,
3010 .ret_type = RET_INTEGER,
3011 .arg1_type = ARG_PTR_TO_CTX,
3012 .arg2_type = ARG_ANYTHING,
3015 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3017 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3018 void *meta = xdp->data_meta + offset;
3019 unsigned long metalen = xdp->data - meta;
3021 if (xdp_data_meta_unsupported(xdp))
3023 if (unlikely(meta < xdp_frame_end ||
3026 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3030 xdp->data_meta = meta;
3035 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3036 .func = bpf_xdp_adjust_meta,
3038 .ret_type = RET_INTEGER,
3039 .arg1_type = ARG_PTR_TO_CTX,
3040 .arg2_type = ARG_ANYTHING,
3043 static int __bpf_tx_xdp(struct net_device *dev,
3044 struct bpf_map *map,
3045 struct xdp_buff *xdp,
3048 struct xdp_frame *xdpf;
3051 if (!dev->netdev_ops->ndo_xdp_xmit) {
3055 xdpf = convert_to_xdp_frame(xdp);
3056 if (unlikely(!xdpf))
3059 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3065 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3066 struct bpf_map *map,
3067 struct xdp_buff *xdp,
3072 switch (map->map_type) {
3073 case BPF_MAP_TYPE_DEVMAP: {
3074 struct bpf_dtab_netdev *dst = fwd;
3076 err = dev_map_enqueue(dst, xdp, dev_rx);
3079 __dev_map_insert_ctx(map, index);
3082 case BPF_MAP_TYPE_CPUMAP: {
3083 struct bpf_cpu_map_entry *rcpu = fwd;
3085 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3088 __cpu_map_insert_ctx(map, index);
3091 case BPF_MAP_TYPE_XSKMAP: {
3092 struct xdp_sock *xs = fwd;
3094 err = __xsk_map_redirect(map, xdp, xs);
3103 void xdp_do_flush_map(void)
3105 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3106 struct bpf_map *map = ri->map_to_flush;
3108 ri->map_to_flush = NULL;
3110 switch (map->map_type) {
3111 case BPF_MAP_TYPE_DEVMAP:
3112 __dev_map_flush(map);
3114 case BPF_MAP_TYPE_CPUMAP:
3115 __cpu_map_flush(map);
3117 case BPF_MAP_TYPE_XSKMAP:
3118 __xsk_map_flush(map);
3125 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3127 static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3129 switch (map->map_type) {
3130 case BPF_MAP_TYPE_DEVMAP:
3131 return __dev_map_lookup_elem(map, index);
3132 case BPF_MAP_TYPE_CPUMAP:
3133 return __cpu_map_lookup_elem(map, index);
3134 case BPF_MAP_TYPE_XSKMAP:
3135 return __xsk_map_lookup_elem(map, index);
3141 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
3144 return (unsigned long)xdp_prog->aux != aux;
3147 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3148 struct bpf_prog *xdp_prog)
3150 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3151 unsigned long map_owner = ri->map_owner;
3152 struct bpf_map *map = ri->map;
3153 u32 index = ri->ifindex;
3161 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3167 fwd = __xdp_map_lookup_elem(map, index);
3172 if (ri->map_to_flush && ri->map_to_flush != map)
3175 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3179 ri->map_to_flush = map;
3180 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3183 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3187 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3188 struct bpf_prog *xdp_prog)
3190 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3191 struct net_device *fwd;
3192 u32 index = ri->ifindex;
3196 return xdp_do_redirect_map(dev, xdp, xdp_prog);
3198 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3200 if (unlikely(!fwd)) {
3205 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3209 _trace_xdp_redirect(dev, xdp_prog, index);
3212 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3215 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3217 static int xdp_do_generic_redirect_map(struct net_device *dev,
3218 struct sk_buff *skb,
3219 struct xdp_buff *xdp,
3220 struct bpf_prog *xdp_prog)
3222 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3223 unsigned long map_owner = ri->map_owner;
3224 struct bpf_map *map = ri->map;
3225 u32 index = ri->ifindex;
3233 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3238 fwd = __xdp_map_lookup_elem(map, index);
3239 if (unlikely(!fwd)) {
3244 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3245 struct bpf_dtab_netdev *dst = fwd;
3247 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3250 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3251 struct xdp_sock *xs = fwd;
3253 err = xsk_generic_rcv(xs, xdp);
3258 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3263 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3266 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3270 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3271 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3273 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3274 u32 index = ri->ifindex;
3275 struct net_device *fwd;
3279 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog);
3282 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3283 if (unlikely(!fwd)) {
3288 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
3292 _trace_xdp_redirect(dev, xdp_prog, index);
3293 generic_xdp_tx(skb, xdp_prog);
3296 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3299 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3301 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3303 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3305 if (unlikely(flags))
3308 ri->ifindex = ifindex;
3313 return XDP_REDIRECT;
3316 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3317 .func = bpf_xdp_redirect,
3319 .ret_type = RET_INTEGER,
3320 .arg1_type = ARG_ANYTHING,
3321 .arg2_type = ARG_ANYTHING,
3324 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
3325 unsigned long, map_owner)
3327 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3329 if (unlikely(flags))
3332 ri->ifindex = ifindex;
3335 ri->map_owner = map_owner;
3337 return XDP_REDIRECT;
3340 /* Note, arg4 is hidden from users and populated by the verifier
3341 * with the right pointer.
3343 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3344 .func = bpf_xdp_redirect_map,
3346 .ret_type = RET_INTEGER,
3347 .arg1_type = ARG_CONST_MAP_PTR,
3348 .arg2_type = ARG_ANYTHING,
3349 .arg3_type = ARG_ANYTHING,
3352 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3353 unsigned long off, unsigned long len)
3355 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3359 if (ptr != dst_buff)
3360 memcpy(dst_buff, ptr, len);
3365 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3366 u64, flags, void *, meta, u64, meta_size)
3368 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3370 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3372 if (unlikely(skb_size > skb->len))
3375 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3379 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3380 .func = bpf_skb_event_output,
3382 .ret_type = RET_INTEGER,
3383 .arg1_type = ARG_PTR_TO_CTX,
3384 .arg2_type = ARG_CONST_MAP_PTR,
3385 .arg3_type = ARG_ANYTHING,
3386 .arg4_type = ARG_PTR_TO_MEM,
3387 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3390 static unsigned short bpf_tunnel_key_af(u64 flags)
3392 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3395 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3396 u32, size, u64, flags)
3398 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3399 u8 compat[sizeof(struct bpf_tunnel_key)];
3403 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3407 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3411 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3414 case offsetof(struct bpf_tunnel_key, tunnel_label):
3415 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3417 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3418 /* Fixup deprecated structure layouts here, so we have
3419 * a common path later on.
3421 if (ip_tunnel_info_af(info) != AF_INET)
3424 to = (struct bpf_tunnel_key *)compat;
3431 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3432 to->tunnel_tos = info->key.tos;
3433 to->tunnel_ttl = info->key.ttl;
3436 if (flags & BPF_F_TUNINFO_IPV6) {
3437 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3438 sizeof(to->remote_ipv6));
3439 to->tunnel_label = be32_to_cpu(info->key.label);
3441 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3442 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3443 to->tunnel_label = 0;
3446 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3447 memcpy(to_orig, to, size);
3451 memset(to_orig, 0, size);
3455 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3456 .func = bpf_skb_get_tunnel_key,
3458 .ret_type = RET_INTEGER,
3459 .arg1_type = ARG_PTR_TO_CTX,
3460 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3461 .arg3_type = ARG_CONST_SIZE,
3462 .arg4_type = ARG_ANYTHING,
3465 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3467 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3470 if (unlikely(!info ||
3471 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3475 if (unlikely(size < info->options_len)) {
3480 ip_tunnel_info_opts_get(to, info);
3481 if (size > info->options_len)
3482 memset(to + info->options_len, 0, size - info->options_len);
3484 return info->options_len;
3486 memset(to, 0, size);
3490 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3491 .func = bpf_skb_get_tunnel_opt,
3493 .ret_type = RET_INTEGER,
3494 .arg1_type = ARG_PTR_TO_CTX,
3495 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3496 .arg3_type = ARG_CONST_SIZE,
3499 static struct metadata_dst __percpu *md_dst;
3501 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3502 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3504 struct metadata_dst *md = this_cpu_ptr(md_dst);
3505 u8 compat[sizeof(struct bpf_tunnel_key)];
3506 struct ip_tunnel_info *info;
3508 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3509 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3511 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3513 case offsetof(struct bpf_tunnel_key, tunnel_label):
3514 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3515 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3516 /* Fixup deprecated structure layouts here, so we have
3517 * a common path later on.
3519 memcpy(compat, from, size);
3520 memset(compat + size, 0, sizeof(compat) - size);
3521 from = (const struct bpf_tunnel_key *) compat;
3527 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3532 dst_hold((struct dst_entry *) md);
3533 skb_dst_set(skb, (struct dst_entry *) md);
3535 info = &md->u.tun_info;
3536 memset(info, 0, sizeof(*info));
3537 info->mode = IP_TUNNEL_INFO_TX;
3539 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3540 if (flags & BPF_F_DONT_FRAGMENT)
3541 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3542 if (flags & BPF_F_ZERO_CSUM_TX)
3543 info->key.tun_flags &= ~TUNNEL_CSUM;
3544 if (flags & BPF_F_SEQ_NUMBER)
3545 info->key.tun_flags |= TUNNEL_SEQ;
3547 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3548 info->key.tos = from->tunnel_tos;
3549 info->key.ttl = from->tunnel_ttl;
3551 if (flags & BPF_F_TUNINFO_IPV6) {
3552 info->mode |= IP_TUNNEL_INFO_IPV6;
3553 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3554 sizeof(from->remote_ipv6));
3555 info->key.label = cpu_to_be32(from->tunnel_label) &
3556 IPV6_FLOWLABEL_MASK;
3558 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3564 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3565 .func = bpf_skb_set_tunnel_key,
3567 .ret_type = RET_INTEGER,
3568 .arg1_type = ARG_PTR_TO_CTX,
3569 .arg2_type = ARG_PTR_TO_MEM,
3570 .arg3_type = ARG_CONST_SIZE,
3571 .arg4_type = ARG_ANYTHING,
3574 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3575 const u8 *, from, u32, size)
3577 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3578 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3580 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3582 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3585 ip_tunnel_info_opts_set(info, from, size);
3590 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3591 .func = bpf_skb_set_tunnel_opt,
3593 .ret_type = RET_INTEGER,
3594 .arg1_type = ARG_PTR_TO_CTX,
3595 .arg2_type = ARG_PTR_TO_MEM,
3596 .arg3_type = ARG_CONST_SIZE,
3599 static const struct bpf_func_proto *
3600 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3603 struct metadata_dst __percpu *tmp;
3605 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3610 if (cmpxchg(&md_dst, NULL, tmp))
3611 metadata_dst_free_percpu(tmp);
3615 case BPF_FUNC_skb_set_tunnel_key:
3616 return &bpf_skb_set_tunnel_key_proto;
3617 case BPF_FUNC_skb_set_tunnel_opt:
3618 return &bpf_skb_set_tunnel_opt_proto;
3624 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3627 struct bpf_array *array = container_of(map, struct bpf_array, map);
3628 struct cgroup *cgrp;
3631 sk = skb_to_full_sk(skb);
3632 if (!sk || !sk_fullsock(sk))
3634 if (unlikely(idx >= array->map.max_entries))
3637 cgrp = READ_ONCE(array->ptrs[idx]);
3638 if (unlikely(!cgrp))
3641 return sk_under_cgroup_hierarchy(sk, cgrp);
3644 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3645 .func = bpf_skb_under_cgroup,
3647 .ret_type = RET_INTEGER,
3648 .arg1_type = ARG_PTR_TO_CTX,
3649 .arg2_type = ARG_CONST_MAP_PTR,
3650 .arg3_type = ARG_ANYTHING,
3653 #ifdef CONFIG_SOCK_CGROUP_DATA
3654 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
3656 struct sock *sk = skb_to_full_sk(skb);
3657 struct cgroup *cgrp;
3659 if (!sk || !sk_fullsock(sk))
3662 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3663 return cgrp->kn->id.id;
3666 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
3667 .func = bpf_skb_cgroup_id,
3669 .ret_type = RET_INTEGER,
3670 .arg1_type = ARG_PTR_TO_CTX,
3674 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3675 unsigned long off, unsigned long len)
3677 memcpy(dst_buff, src_buff + off, len);
3681 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3682 u64, flags, void *, meta, u64, meta_size)
3684 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3686 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3688 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3691 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3692 xdp_size, bpf_xdp_copy);
3695 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3696 .func = bpf_xdp_event_output,
3698 .ret_type = RET_INTEGER,
3699 .arg1_type = ARG_PTR_TO_CTX,
3700 .arg2_type = ARG_CONST_MAP_PTR,
3701 .arg3_type = ARG_ANYTHING,
3702 .arg4_type = ARG_PTR_TO_MEM,
3703 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3706 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3708 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3711 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3712 .func = bpf_get_socket_cookie,
3714 .ret_type = RET_INTEGER,
3715 .arg1_type = ARG_PTR_TO_CTX,
3718 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3720 struct sock *sk = sk_to_full_sk(skb->sk);
3723 if (!sk || !sk_fullsock(sk))
3725 kuid = sock_net_uid(sock_net(sk), sk);
3726 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3729 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3730 .func = bpf_get_socket_uid,
3732 .ret_type = RET_INTEGER,
3733 .arg1_type = ARG_PTR_TO_CTX,
3736 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3737 int, level, int, optname, char *, optval, int, optlen)
3739 struct sock *sk = bpf_sock->sk;
3743 if (!sk_fullsock(sk))
3746 if (level == SOL_SOCKET) {
3747 if (optlen != sizeof(int))
3749 val = *((int *)optval);
3751 /* Only some socketops are supported */
3754 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3755 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3758 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3759 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3761 case SO_MAX_PACING_RATE:
3762 sk->sk_max_pacing_rate = val;
3763 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3764 sk->sk_max_pacing_rate);
3767 sk->sk_priority = val;
3772 sk->sk_rcvlowat = val ? : 1;
3781 } else if (level == SOL_IP) {
3782 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3785 val = *((int *)optval);
3786 /* Only some options are supported */
3789 if (val < -1 || val > 0xff) {
3792 struct inet_sock *inet = inet_sk(sk);
3802 #if IS_ENABLED(CONFIG_IPV6)
3803 } else if (level == SOL_IPV6) {
3804 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3807 val = *((int *)optval);
3808 /* Only some options are supported */
3811 if (val < -1 || val > 0xff) {
3814 struct ipv6_pinfo *np = inet6_sk(sk);
3825 } else if (level == SOL_TCP &&
3826 sk->sk_prot->setsockopt == tcp_setsockopt) {
3827 if (optname == TCP_CONGESTION) {
3828 char name[TCP_CA_NAME_MAX];
3829 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3831 strncpy(name, optval, min_t(long, optlen,
3832 TCP_CA_NAME_MAX-1));
3833 name[TCP_CA_NAME_MAX-1] = 0;
3834 ret = tcp_set_congestion_control(sk, name, false,
3837 struct tcp_sock *tp = tcp_sk(sk);
3839 if (optlen != sizeof(int))
3842 val = *((int *)optval);
3843 /* Only some options are supported */
3846 if (val <= 0 || tp->data_segs_out > 0)
3851 case TCP_BPF_SNDCWND_CLAMP:
3855 tp->snd_cwnd_clamp = val;
3856 tp->snd_ssthresh = val;
3870 static const struct bpf_func_proto bpf_setsockopt_proto = {
3871 .func = bpf_setsockopt,
3873 .ret_type = RET_INTEGER,
3874 .arg1_type = ARG_PTR_TO_CTX,
3875 .arg2_type = ARG_ANYTHING,
3876 .arg3_type = ARG_ANYTHING,
3877 .arg4_type = ARG_PTR_TO_MEM,
3878 .arg5_type = ARG_CONST_SIZE,
3881 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3882 int, level, int, optname, char *, optval, int, optlen)
3884 struct sock *sk = bpf_sock->sk;
3886 if (!sk_fullsock(sk))
3890 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3891 if (optname == TCP_CONGESTION) {
3892 struct inet_connection_sock *icsk = inet_csk(sk);
3894 if (!icsk->icsk_ca_ops || optlen <= 1)
3896 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3897 optval[optlen - 1] = 0;
3901 } else if (level == SOL_IP) {
3902 struct inet_sock *inet = inet_sk(sk);
3904 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3907 /* Only some options are supported */
3910 *((int *)optval) = (int)inet->tos;
3915 #if IS_ENABLED(CONFIG_IPV6)
3916 } else if (level == SOL_IPV6) {
3917 struct ipv6_pinfo *np = inet6_sk(sk);
3919 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3922 /* Only some options are supported */
3925 *((int *)optval) = (int)np->tclass;
3937 memset(optval, 0, optlen);
3941 static const struct bpf_func_proto bpf_getsockopt_proto = {
3942 .func = bpf_getsockopt,
3944 .ret_type = RET_INTEGER,
3945 .arg1_type = ARG_PTR_TO_CTX,
3946 .arg2_type = ARG_ANYTHING,
3947 .arg3_type = ARG_ANYTHING,
3948 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
3949 .arg5_type = ARG_CONST_SIZE,
3952 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
3955 struct sock *sk = bpf_sock->sk;
3956 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
3958 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
3962 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
3964 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
3967 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
3968 .func = bpf_sock_ops_cb_flags_set,
3970 .ret_type = RET_INTEGER,
3971 .arg1_type = ARG_PTR_TO_CTX,
3972 .arg2_type = ARG_ANYTHING,
3975 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
3976 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
3978 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
3982 struct sock *sk = ctx->sk;
3985 /* Binding to port can be expensive so it's prohibited in the helper.
3986 * Only binding to IP is supported.
3989 if (addr->sa_family == AF_INET) {
3990 if (addr_len < sizeof(struct sockaddr_in))
3992 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3994 return __inet_bind(sk, addr, addr_len, true, false);
3995 #if IS_ENABLED(CONFIG_IPV6)
3996 } else if (addr->sa_family == AF_INET6) {
3997 if (addr_len < SIN6_LEN_RFC2133)
3999 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4001 /* ipv6_bpf_stub cannot be NULL, since it's called from
4002 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4004 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4005 #endif /* CONFIG_IPV6 */
4007 #endif /* CONFIG_INET */
4009 return -EAFNOSUPPORT;
4012 static const struct bpf_func_proto bpf_bind_proto = {
4015 .ret_type = RET_INTEGER,
4016 .arg1_type = ARG_PTR_TO_CTX,
4017 .arg2_type = ARG_PTR_TO_MEM,
4018 .arg3_type = ARG_CONST_SIZE,
4022 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4023 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4025 const struct sec_path *sp = skb_sec_path(skb);
4026 const struct xfrm_state *x;
4028 if (!sp || unlikely(index >= sp->len || flags))
4031 x = sp->xvec[index];
4033 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4036 to->reqid = x->props.reqid;
4037 to->spi = x->id.spi;
4038 to->family = x->props.family;
4041 if (to->family == AF_INET6) {
4042 memcpy(to->remote_ipv6, x->props.saddr.a6,
4043 sizeof(to->remote_ipv6));
4045 to->remote_ipv4 = x->props.saddr.a4;
4046 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4051 memset(to, 0, size);
4055 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4056 .func = bpf_skb_get_xfrm_state,
4058 .ret_type = RET_INTEGER,
4059 .arg1_type = ARG_PTR_TO_CTX,
4060 .arg2_type = ARG_ANYTHING,
4061 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4062 .arg4_type = ARG_CONST_SIZE,
4063 .arg5_type = ARG_ANYTHING,
4067 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4068 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4069 const struct neighbour *neigh,
4070 const struct net_device *dev)
4072 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4073 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4074 params->h_vlan_TCI = 0;
4075 params->h_vlan_proto = 0;
4076 params->ifindex = dev->ifindex;
4082 #if IS_ENABLED(CONFIG_INET)
4083 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4084 u32 flags, bool check_mtu)
4086 struct in_device *in_dev;
4087 struct neighbour *neigh;
4088 struct net_device *dev;
4089 struct fib_result res;
4095 dev = dev_get_by_index_rcu(net, params->ifindex);
4099 /* verify forwarding is enabled on this interface */
4100 in_dev = __in_dev_get_rcu(dev);
4101 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4102 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4104 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4106 fl4.flowi4_oif = params->ifindex;
4108 fl4.flowi4_iif = params->ifindex;
4111 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4112 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4113 fl4.flowi4_flags = 0;
4115 fl4.flowi4_proto = params->l4_protocol;
4116 fl4.daddr = params->ipv4_dst;
4117 fl4.saddr = params->ipv4_src;
4118 fl4.fl4_sport = params->sport;
4119 fl4.fl4_dport = params->dport;
4121 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4122 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4123 struct fib_table *tb;
4125 tb = fib_get_table(net, tbid);
4127 return BPF_FIB_LKUP_RET_NOT_FWDED;
4129 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4131 fl4.flowi4_mark = 0;
4132 fl4.flowi4_secid = 0;
4133 fl4.flowi4_tun_key.tun_id = 0;
4134 fl4.flowi4_uid = sock_net_uid(net, NULL);
4136 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4140 /* map fib lookup errors to RTN_ type */
4142 return BPF_FIB_LKUP_RET_BLACKHOLE;
4143 if (err == -EHOSTUNREACH)
4144 return BPF_FIB_LKUP_RET_UNREACHABLE;
4146 return BPF_FIB_LKUP_RET_PROHIBIT;
4148 return BPF_FIB_LKUP_RET_NOT_FWDED;
4151 if (res.type != RTN_UNICAST)
4152 return BPF_FIB_LKUP_RET_NOT_FWDED;
4154 if (res.fi->fib_nhs > 1)
4155 fib_select_path(net, &res, &fl4, NULL);
4158 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4159 if (params->tot_len > mtu)
4160 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4163 nh = &res.fi->fib_nh[res.nh_sel];
4165 /* do not handle lwt encaps right now */
4166 if (nh->nh_lwtstate)
4167 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4171 params->ipv4_dst = nh->nh_gw;
4173 params->rt_metric = res.fi->fib_priority;
4175 /* xdp and cls_bpf programs are run in RCU-bh so
4176 * rcu_read_lock_bh is not needed here
4178 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4180 return BPF_FIB_LKUP_RET_NO_NEIGH;
4182 return bpf_fib_set_fwd_params(params, neigh, dev);
4186 #if IS_ENABLED(CONFIG_IPV6)
4187 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4188 u32 flags, bool check_mtu)
4190 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4191 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4192 struct neighbour *neigh;
4193 struct net_device *dev;
4194 struct inet6_dev *idev;
4195 struct fib6_info *f6i;
4201 /* link local addresses are never forwarded */
4202 if (rt6_need_strict(dst) || rt6_need_strict(src))
4203 return BPF_FIB_LKUP_RET_NOT_FWDED;
4205 dev = dev_get_by_index_rcu(net, params->ifindex);
4209 idev = __in6_dev_get_safely(dev);
4210 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4211 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4213 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4215 oif = fl6.flowi6_oif = params->ifindex;
4217 oif = fl6.flowi6_iif = params->ifindex;
4219 strict = RT6_LOOKUP_F_HAS_SADDR;
4221 fl6.flowlabel = params->flowinfo;
4222 fl6.flowi6_scope = 0;
4223 fl6.flowi6_flags = 0;
4226 fl6.flowi6_proto = params->l4_protocol;
4229 fl6.fl6_sport = params->sport;
4230 fl6.fl6_dport = params->dport;
4232 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4233 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4234 struct fib6_table *tb;
4236 tb = ipv6_stub->fib6_get_table(net, tbid);
4238 return BPF_FIB_LKUP_RET_NOT_FWDED;
4240 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4242 fl6.flowi6_mark = 0;
4243 fl6.flowi6_secid = 0;
4244 fl6.flowi6_tun_key.tun_id = 0;
4245 fl6.flowi6_uid = sock_net_uid(net, NULL);
4247 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4250 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4251 return BPF_FIB_LKUP_RET_NOT_FWDED;
4253 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4254 switch (f6i->fib6_type) {
4256 return BPF_FIB_LKUP_RET_BLACKHOLE;
4257 case RTN_UNREACHABLE:
4258 return BPF_FIB_LKUP_RET_UNREACHABLE;
4260 return BPF_FIB_LKUP_RET_PROHIBIT;
4262 return BPF_FIB_LKUP_RET_NOT_FWDED;
4266 if (f6i->fib6_type != RTN_UNICAST)
4267 return BPF_FIB_LKUP_RET_NOT_FWDED;
4269 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4270 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4271 fl6.flowi6_oif, NULL,
4275 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4276 if (params->tot_len > mtu)
4277 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4280 if (f6i->fib6_nh.nh_lwtstate)
4281 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4283 if (f6i->fib6_flags & RTF_GATEWAY)
4284 *dst = f6i->fib6_nh.nh_gw;
4286 dev = f6i->fib6_nh.nh_dev;
4287 params->rt_metric = f6i->fib6_metric;
4289 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4290 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4291 * because we need to get nd_tbl via the stub
4293 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4294 ndisc_hashfn, dst, dev);
4296 return BPF_FIB_LKUP_RET_NO_NEIGH;
4298 return bpf_fib_set_fwd_params(params, neigh, dev);
4302 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4303 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4305 if (plen < sizeof(*params))
4308 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4311 switch (params->family) {
4312 #if IS_ENABLED(CONFIG_INET)
4314 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4317 #if IS_ENABLED(CONFIG_IPV6)
4319 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4323 return -EAFNOSUPPORT;
4326 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4327 .func = bpf_xdp_fib_lookup,
4329 .ret_type = RET_INTEGER,
4330 .arg1_type = ARG_PTR_TO_CTX,
4331 .arg2_type = ARG_PTR_TO_MEM,
4332 .arg3_type = ARG_CONST_SIZE,
4333 .arg4_type = ARG_ANYTHING,
4336 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4337 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4339 struct net *net = dev_net(skb->dev);
4340 int rc = -EAFNOSUPPORT;
4342 if (plen < sizeof(*params))
4345 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4348 switch (params->family) {
4349 #if IS_ENABLED(CONFIG_INET)
4351 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4354 #if IS_ENABLED(CONFIG_IPV6)
4356 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4362 struct net_device *dev;
4364 dev = dev_get_by_index_rcu(net, params->ifindex);
4365 if (!is_skb_forwardable(dev, skb))
4366 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4372 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4373 .func = bpf_skb_fib_lookup,
4375 .ret_type = RET_INTEGER,
4376 .arg1_type = ARG_PTR_TO_CTX,
4377 .arg2_type = ARG_PTR_TO_MEM,
4378 .arg3_type = ARG_CONST_SIZE,
4379 .arg4_type = ARG_ANYTHING,
4382 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4383 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4386 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4388 if (!seg6_validate_srh(srh, len))
4392 case BPF_LWT_ENCAP_SEG6_INLINE:
4393 if (skb->protocol != htons(ETH_P_IPV6))
4396 err = seg6_do_srh_inline(skb, srh);
4398 case BPF_LWT_ENCAP_SEG6:
4399 skb_reset_inner_headers(skb);
4400 skb->encapsulation = 1;
4401 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4407 bpf_compute_data_pointers(skb);
4411 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4412 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4414 return seg6_lookup_nexthop(skb, NULL, 0);
4416 #endif /* CONFIG_IPV6_SEG6_BPF */
4418 BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4422 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4423 case BPF_LWT_ENCAP_SEG6:
4424 case BPF_LWT_ENCAP_SEG6_INLINE:
4425 return bpf_push_seg6_encap(skb, type, hdr, len);
4432 static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4433 .func = bpf_lwt_push_encap,
4435 .ret_type = RET_INTEGER,
4436 .arg1_type = ARG_PTR_TO_CTX,
4437 .arg2_type = ARG_ANYTHING,
4438 .arg3_type = ARG_PTR_TO_MEM,
4439 .arg4_type = ARG_CONST_SIZE
4442 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4443 const void *, from, u32, len)
4445 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4446 struct seg6_bpf_srh_state *srh_state =
4447 this_cpu_ptr(&seg6_bpf_srh_states);
4448 void *srh_tlvs, *srh_end, *ptr;
4449 struct ipv6_sr_hdr *srh;
4452 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4455 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4456 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4457 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4459 ptr = skb->data + offset;
4460 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4461 srh_state->valid = 0;
4462 else if (ptr < (void *)&srh->flags ||
4463 ptr + len > (void *)&srh->segments)
4466 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4469 memcpy(skb->data + offset, from, len);
4471 #else /* CONFIG_IPV6_SEG6_BPF */
4476 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4477 .func = bpf_lwt_seg6_store_bytes,
4479 .ret_type = RET_INTEGER,
4480 .arg1_type = ARG_PTR_TO_CTX,
4481 .arg2_type = ARG_ANYTHING,
4482 .arg3_type = ARG_PTR_TO_MEM,
4483 .arg4_type = ARG_CONST_SIZE
4486 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4487 u32, action, void *, param, u32, param_len)
4489 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4490 struct seg6_bpf_srh_state *srh_state =
4491 this_cpu_ptr(&seg6_bpf_srh_states);
4492 struct ipv6_sr_hdr *srh;
4496 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4498 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4500 if (!srh_state->valid) {
4501 if (unlikely((srh_state->hdrlen & 7) != 0))
4504 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
4505 if (unlikely(!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3)))
4508 srh_state->valid = 1;
4512 case SEG6_LOCAL_ACTION_END_X:
4513 if (param_len != sizeof(struct in6_addr))
4515 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4516 case SEG6_LOCAL_ACTION_END_T:
4517 if (param_len != sizeof(int))
4519 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4520 case SEG6_LOCAL_ACTION_END_B6:
4521 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4525 ((struct ipv6_sr_hdr *)param)->hdrlen << 3;
4527 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
4528 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4532 ((struct ipv6_sr_hdr *)param)->hdrlen << 3;
4537 #else /* CONFIG_IPV6_SEG6_BPF */
4542 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4543 .func = bpf_lwt_seg6_action,
4545 .ret_type = RET_INTEGER,
4546 .arg1_type = ARG_PTR_TO_CTX,
4547 .arg2_type = ARG_ANYTHING,
4548 .arg3_type = ARG_PTR_TO_MEM,
4549 .arg4_type = ARG_CONST_SIZE
4552 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4555 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4556 struct seg6_bpf_srh_state *srh_state =
4557 this_cpu_ptr(&seg6_bpf_srh_states);
4558 void *srh_end, *srh_tlvs, *ptr;
4559 struct ipv6_sr_hdr *srh;
4560 struct ipv6hdr *hdr;
4564 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4566 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4568 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4569 ((srh->first_segment + 1) << 4));
4570 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4572 ptr = skb->data + offset;
4574 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4576 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4580 ret = skb_cow_head(skb, len);
4581 if (unlikely(ret < 0))
4584 ret = bpf_skb_net_hdr_push(skb, offset, len);
4586 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4589 bpf_compute_data_pointers(skb);
4590 if (unlikely(ret < 0))
4593 hdr = (struct ipv6hdr *)skb->data;
4594 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4596 srh_state->hdrlen += len;
4597 srh_state->valid = 0;
4599 #else /* CONFIG_IPV6_SEG6_BPF */
4604 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
4605 .func = bpf_lwt_seg6_adjust_srh,
4607 .ret_type = RET_INTEGER,
4608 .arg1_type = ARG_PTR_TO_CTX,
4609 .arg2_type = ARG_ANYTHING,
4610 .arg3_type = ARG_ANYTHING,
4613 bool bpf_helper_changes_pkt_data(void *func)
4615 if (func == bpf_skb_vlan_push ||
4616 func == bpf_skb_vlan_pop ||
4617 func == bpf_skb_store_bytes ||
4618 func == bpf_skb_change_proto ||
4619 func == bpf_skb_change_head ||
4620 func == bpf_skb_change_tail ||
4621 func == bpf_skb_adjust_room ||
4622 func == bpf_skb_pull_data ||
4623 func == bpf_clone_redirect ||
4624 func == bpf_l3_csum_replace ||
4625 func == bpf_l4_csum_replace ||
4626 func == bpf_xdp_adjust_head ||
4627 func == bpf_xdp_adjust_meta ||
4628 func == bpf_msg_pull_data ||
4629 func == bpf_xdp_adjust_tail ||
4630 func == bpf_lwt_push_encap ||
4631 func == bpf_lwt_seg6_store_bytes ||
4632 func == bpf_lwt_seg6_adjust_srh ||
4633 func == bpf_lwt_seg6_action
4640 static const struct bpf_func_proto *
4641 bpf_base_func_proto(enum bpf_func_id func_id)
4644 case BPF_FUNC_map_lookup_elem:
4645 return &bpf_map_lookup_elem_proto;
4646 case BPF_FUNC_map_update_elem:
4647 return &bpf_map_update_elem_proto;
4648 case BPF_FUNC_map_delete_elem:
4649 return &bpf_map_delete_elem_proto;
4650 case BPF_FUNC_get_prandom_u32:
4651 return &bpf_get_prandom_u32_proto;
4652 case BPF_FUNC_get_smp_processor_id:
4653 return &bpf_get_raw_smp_processor_id_proto;
4654 case BPF_FUNC_get_numa_node_id:
4655 return &bpf_get_numa_node_id_proto;
4656 case BPF_FUNC_tail_call:
4657 return &bpf_tail_call_proto;
4658 case BPF_FUNC_ktime_get_ns:
4659 return &bpf_ktime_get_ns_proto;
4660 case BPF_FUNC_trace_printk:
4661 if (capable(CAP_SYS_ADMIN))
4662 return bpf_get_trace_printk_proto();
4668 static const struct bpf_func_proto *
4669 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4672 /* inet and inet6 sockets are created in a process
4673 * context so there is always a valid uid/gid
4675 case BPF_FUNC_get_current_uid_gid:
4676 return &bpf_get_current_uid_gid_proto;
4678 return bpf_base_func_proto(func_id);
4682 static const struct bpf_func_proto *
4683 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4686 /* inet and inet6 sockets are created in a process
4687 * context so there is always a valid uid/gid
4689 case BPF_FUNC_get_current_uid_gid:
4690 return &bpf_get_current_uid_gid_proto;
4692 switch (prog->expected_attach_type) {
4693 case BPF_CGROUP_INET4_CONNECT:
4694 case BPF_CGROUP_INET6_CONNECT:
4695 return &bpf_bind_proto;
4700 return bpf_base_func_proto(func_id);
4704 static const struct bpf_func_proto *
4705 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4708 case BPF_FUNC_skb_load_bytes:
4709 return &bpf_skb_load_bytes_proto;
4710 case BPF_FUNC_skb_load_bytes_relative:
4711 return &bpf_skb_load_bytes_relative_proto;
4712 case BPF_FUNC_get_socket_cookie:
4713 return &bpf_get_socket_cookie_proto;
4714 case BPF_FUNC_get_socket_uid:
4715 return &bpf_get_socket_uid_proto;
4717 return bpf_base_func_proto(func_id);
4721 static const struct bpf_func_proto *
4722 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4725 case BPF_FUNC_skb_store_bytes:
4726 return &bpf_skb_store_bytes_proto;
4727 case BPF_FUNC_skb_load_bytes:
4728 return &bpf_skb_load_bytes_proto;
4729 case BPF_FUNC_skb_load_bytes_relative:
4730 return &bpf_skb_load_bytes_relative_proto;
4731 case BPF_FUNC_skb_pull_data:
4732 return &bpf_skb_pull_data_proto;
4733 case BPF_FUNC_csum_diff:
4734 return &bpf_csum_diff_proto;
4735 case BPF_FUNC_csum_update:
4736 return &bpf_csum_update_proto;
4737 case BPF_FUNC_l3_csum_replace:
4738 return &bpf_l3_csum_replace_proto;
4739 case BPF_FUNC_l4_csum_replace:
4740 return &bpf_l4_csum_replace_proto;
4741 case BPF_FUNC_clone_redirect:
4742 return &bpf_clone_redirect_proto;
4743 case BPF_FUNC_get_cgroup_classid:
4744 return &bpf_get_cgroup_classid_proto;
4745 case BPF_FUNC_skb_vlan_push:
4746 return &bpf_skb_vlan_push_proto;
4747 case BPF_FUNC_skb_vlan_pop:
4748 return &bpf_skb_vlan_pop_proto;
4749 case BPF_FUNC_skb_change_proto:
4750 return &bpf_skb_change_proto_proto;
4751 case BPF_FUNC_skb_change_type:
4752 return &bpf_skb_change_type_proto;
4753 case BPF_FUNC_skb_adjust_room:
4754 return &bpf_skb_adjust_room_proto;
4755 case BPF_FUNC_skb_change_tail:
4756 return &bpf_skb_change_tail_proto;
4757 case BPF_FUNC_skb_get_tunnel_key:
4758 return &bpf_skb_get_tunnel_key_proto;
4759 case BPF_FUNC_skb_set_tunnel_key:
4760 return bpf_get_skb_set_tunnel_proto(func_id);
4761 case BPF_FUNC_skb_get_tunnel_opt:
4762 return &bpf_skb_get_tunnel_opt_proto;
4763 case BPF_FUNC_skb_set_tunnel_opt:
4764 return bpf_get_skb_set_tunnel_proto(func_id);
4765 case BPF_FUNC_redirect:
4766 return &bpf_redirect_proto;
4767 case BPF_FUNC_get_route_realm:
4768 return &bpf_get_route_realm_proto;
4769 case BPF_FUNC_get_hash_recalc:
4770 return &bpf_get_hash_recalc_proto;
4771 case BPF_FUNC_set_hash_invalid:
4772 return &bpf_set_hash_invalid_proto;
4773 case BPF_FUNC_set_hash:
4774 return &bpf_set_hash_proto;
4775 case BPF_FUNC_perf_event_output:
4776 return &bpf_skb_event_output_proto;
4777 case BPF_FUNC_get_smp_processor_id:
4778 return &bpf_get_smp_processor_id_proto;
4779 case BPF_FUNC_skb_under_cgroup:
4780 return &bpf_skb_under_cgroup_proto;
4781 case BPF_FUNC_get_socket_cookie:
4782 return &bpf_get_socket_cookie_proto;
4783 case BPF_FUNC_get_socket_uid:
4784 return &bpf_get_socket_uid_proto;
4785 case BPF_FUNC_fib_lookup:
4786 return &bpf_skb_fib_lookup_proto;
4788 case BPF_FUNC_skb_get_xfrm_state:
4789 return &bpf_skb_get_xfrm_state_proto;
4791 #ifdef CONFIG_SOCK_CGROUP_DATA
4792 case BPF_FUNC_skb_cgroup_id:
4793 return &bpf_skb_cgroup_id_proto;
4796 return bpf_base_func_proto(func_id);
4800 static const struct bpf_func_proto *
4801 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4804 case BPF_FUNC_perf_event_output:
4805 return &bpf_xdp_event_output_proto;
4806 case BPF_FUNC_get_smp_processor_id:
4807 return &bpf_get_smp_processor_id_proto;
4808 case BPF_FUNC_csum_diff:
4809 return &bpf_csum_diff_proto;
4810 case BPF_FUNC_xdp_adjust_head:
4811 return &bpf_xdp_adjust_head_proto;
4812 case BPF_FUNC_xdp_adjust_meta:
4813 return &bpf_xdp_adjust_meta_proto;
4814 case BPF_FUNC_redirect:
4815 return &bpf_xdp_redirect_proto;
4816 case BPF_FUNC_redirect_map:
4817 return &bpf_xdp_redirect_map_proto;
4818 case BPF_FUNC_xdp_adjust_tail:
4819 return &bpf_xdp_adjust_tail_proto;
4820 case BPF_FUNC_fib_lookup:
4821 return &bpf_xdp_fib_lookup_proto;
4823 return bpf_base_func_proto(func_id);
4827 static const struct bpf_func_proto *
4828 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4831 case BPF_FUNC_setsockopt:
4832 return &bpf_setsockopt_proto;
4833 case BPF_FUNC_getsockopt:
4834 return &bpf_getsockopt_proto;
4835 case BPF_FUNC_sock_ops_cb_flags_set:
4836 return &bpf_sock_ops_cb_flags_set_proto;
4837 case BPF_FUNC_sock_map_update:
4838 return &bpf_sock_map_update_proto;
4839 case BPF_FUNC_sock_hash_update:
4840 return &bpf_sock_hash_update_proto;
4842 return bpf_base_func_proto(func_id);
4846 static const struct bpf_func_proto *
4847 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4850 case BPF_FUNC_msg_redirect_map:
4851 return &bpf_msg_redirect_map_proto;
4852 case BPF_FUNC_msg_redirect_hash:
4853 return &bpf_msg_redirect_hash_proto;
4854 case BPF_FUNC_msg_apply_bytes:
4855 return &bpf_msg_apply_bytes_proto;
4856 case BPF_FUNC_msg_cork_bytes:
4857 return &bpf_msg_cork_bytes_proto;
4858 case BPF_FUNC_msg_pull_data:
4859 return &bpf_msg_pull_data_proto;
4861 return bpf_base_func_proto(func_id);
4865 static const struct bpf_func_proto *
4866 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4869 case BPF_FUNC_skb_store_bytes:
4870 return &bpf_skb_store_bytes_proto;
4871 case BPF_FUNC_skb_load_bytes:
4872 return &bpf_skb_load_bytes_proto;
4873 case BPF_FUNC_skb_pull_data:
4874 return &bpf_skb_pull_data_proto;
4875 case BPF_FUNC_skb_change_tail:
4876 return &bpf_skb_change_tail_proto;
4877 case BPF_FUNC_skb_change_head:
4878 return &bpf_skb_change_head_proto;
4879 case BPF_FUNC_get_socket_cookie:
4880 return &bpf_get_socket_cookie_proto;
4881 case BPF_FUNC_get_socket_uid:
4882 return &bpf_get_socket_uid_proto;
4883 case BPF_FUNC_sk_redirect_map:
4884 return &bpf_sk_redirect_map_proto;
4885 case BPF_FUNC_sk_redirect_hash:
4886 return &bpf_sk_redirect_hash_proto;
4888 return bpf_base_func_proto(func_id);
4892 static const struct bpf_func_proto *
4893 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4896 case BPF_FUNC_skb_load_bytes:
4897 return &bpf_skb_load_bytes_proto;
4898 case BPF_FUNC_skb_pull_data:
4899 return &bpf_skb_pull_data_proto;
4900 case BPF_FUNC_csum_diff:
4901 return &bpf_csum_diff_proto;
4902 case BPF_FUNC_get_cgroup_classid:
4903 return &bpf_get_cgroup_classid_proto;
4904 case BPF_FUNC_get_route_realm:
4905 return &bpf_get_route_realm_proto;
4906 case BPF_FUNC_get_hash_recalc:
4907 return &bpf_get_hash_recalc_proto;
4908 case BPF_FUNC_perf_event_output:
4909 return &bpf_skb_event_output_proto;
4910 case BPF_FUNC_get_smp_processor_id:
4911 return &bpf_get_smp_processor_id_proto;
4912 case BPF_FUNC_skb_under_cgroup:
4913 return &bpf_skb_under_cgroup_proto;
4915 return bpf_base_func_proto(func_id);
4919 static const struct bpf_func_proto *
4920 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4923 case BPF_FUNC_lwt_push_encap:
4924 return &bpf_lwt_push_encap_proto;
4926 return lwt_out_func_proto(func_id, prog);
4930 static const struct bpf_func_proto *
4931 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4934 case BPF_FUNC_skb_get_tunnel_key:
4935 return &bpf_skb_get_tunnel_key_proto;
4936 case BPF_FUNC_skb_set_tunnel_key:
4937 return bpf_get_skb_set_tunnel_proto(func_id);
4938 case BPF_FUNC_skb_get_tunnel_opt:
4939 return &bpf_skb_get_tunnel_opt_proto;
4940 case BPF_FUNC_skb_set_tunnel_opt:
4941 return bpf_get_skb_set_tunnel_proto(func_id);
4942 case BPF_FUNC_redirect:
4943 return &bpf_redirect_proto;
4944 case BPF_FUNC_clone_redirect:
4945 return &bpf_clone_redirect_proto;
4946 case BPF_FUNC_skb_change_tail:
4947 return &bpf_skb_change_tail_proto;
4948 case BPF_FUNC_skb_change_head:
4949 return &bpf_skb_change_head_proto;
4950 case BPF_FUNC_skb_store_bytes:
4951 return &bpf_skb_store_bytes_proto;
4952 case BPF_FUNC_csum_update:
4953 return &bpf_csum_update_proto;
4954 case BPF_FUNC_l3_csum_replace:
4955 return &bpf_l3_csum_replace_proto;
4956 case BPF_FUNC_l4_csum_replace:
4957 return &bpf_l4_csum_replace_proto;
4958 case BPF_FUNC_set_hash_invalid:
4959 return &bpf_set_hash_invalid_proto;
4961 return lwt_out_func_proto(func_id, prog);
4965 static const struct bpf_func_proto *
4966 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4969 case BPF_FUNC_lwt_seg6_store_bytes:
4970 return &bpf_lwt_seg6_store_bytes_proto;
4971 case BPF_FUNC_lwt_seg6_action:
4972 return &bpf_lwt_seg6_action_proto;
4973 case BPF_FUNC_lwt_seg6_adjust_srh:
4974 return &bpf_lwt_seg6_adjust_srh_proto;
4976 return lwt_out_func_proto(func_id, prog);
4980 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
4981 const struct bpf_prog *prog,
4982 struct bpf_insn_access_aux *info)
4984 const int size_default = sizeof(__u32);
4986 if (off < 0 || off >= sizeof(struct __sk_buff))
4989 /* The verifier guarantees that size > 0. */
4990 if (off % size != 0)
4994 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
4995 if (off + size > offsetofend(struct __sk_buff, cb[4]))
4998 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
4999 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
5000 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
5001 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
5002 case bpf_ctx_range(struct __sk_buff, data):
5003 case bpf_ctx_range(struct __sk_buff, data_meta):
5004 case bpf_ctx_range(struct __sk_buff, data_end):
5005 if (size != size_default)
5009 /* Only narrow read access allowed for now. */
5010 if (type == BPF_WRITE) {
5011 if (size != size_default)
5014 bpf_ctx_record_field_size(info, size_default);
5015 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5023 static bool sk_filter_is_valid_access(int off, int size,
5024 enum bpf_access_type type,
5025 const struct bpf_prog *prog,
5026 struct bpf_insn_access_aux *info)
5029 case bpf_ctx_range(struct __sk_buff, tc_classid):
5030 case bpf_ctx_range(struct __sk_buff, data):
5031 case bpf_ctx_range(struct __sk_buff, data_meta):
5032 case bpf_ctx_range(struct __sk_buff, data_end):
5033 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5037 if (type == BPF_WRITE) {
5039 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5046 return bpf_skb_is_valid_access(off, size, type, prog, info);
5049 static bool lwt_is_valid_access(int off, int size,
5050 enum bpf_access_type type,
5051 const struct bpf_prog *prog,
5052 struct bpf_insn_access_aux *info)
5055 case bpf_ctx_range(struct __sk_buff, tc_classid):
5056 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5057 case bpf_ctx_range(struct __sk_buff, data_meta):
5061 if (type == BPF_WRITE) {
5063 case bpf_ctx_range(struct __sk_buff, mark):
5064 case bpf_ctx_range(struct __sk_buff, priority):
5065 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5073 case bpf_ctx_range(struct __sk_buff, data):
5074 info->reg_type = PTR_TO_PACKET;
5076 case bpf_ctx_range(struct __sk_buff, data_end):
5077 info->reg_type = PTR_TO_PACKET_END;
5081 return bpf_skb_is_valid_access(off, size, type, prog, info);
5084 /* Attach type specific accesses */
5085 static bool __sock_filter_check_attach_type(int off,
5086 enum bpf_access_type access_type,
5087 enum bpf_attach_type attach_type)
5090 case offsetof(struct bpf_sock, bound_dev_if):
5091 case offsetof(struct bpf_sock, mark):
5092 case offsetof(struct bpf_sock, priority):
5093 switch (attach_type) {
5094 case BPF_CGROUP_INET_SOCK_CREATE:
5099 case bpf_ctx_range(struct bpf_sock, src_ip4):
5100 switch (attach_type) {
5101 case BPF_CGROUP_INET4_POST_BIND:
5106 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5107 switch (attach_type) {
5108 case BPF_CGROUP_INET6_POST_BIND:
5113 case bpf_ctx_range(struct bpf_sock, src_port):
5114 switch (attach_type) {
5115 case BPF_CGROUP_INET4_POST_BIND:
5116 case BPF_CGROUP_INET6_POST_BIND:
5123 return access_type == BPF_READ;
5128 static bool __sock_filter_check_size(int off, int size,
5129 struct bpf_insn_access_aux *info)
5131 const int size_default = sizeof(__u32);
5134 case bpf_ctx_range(struct bpf_sock, src_ip4):
5135 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5136 bpf_ctx_record_field_size(info, size_default);
5137 return bpf_ctx_narrow_access_ok(off, size, size_default);
5140 return size == size_default;
5143 static bool sock_filter_is_valid_access(int off, int size,
5144 enum bpf_access_type type,
5145 const struct bpf_prog *prog,
5146 struct bpf_insn_access_aux *info)
5148 if (off < 0 || off >= sizeof(struct bpf_sock))
5150 if (off % size != 0)
5152 if (!__sock_filter_check_attach_type(off, type,
5153 prog->expected_attach_type))
5155 if (!__sock_filter_check_size(off, size, info))
5160 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5161 const struct bpf_prog *prog, int drop_verdict)
5163 struct bpf_insn *insn = insn_buf;
5168 /* if (!skb->cloned)
5171 * (Fast-path, otherwise approximation that we might be
5172 * a clone, do the rest in helper.)
5174 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5175 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5176 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5178 /* ret = bpf_skb_pull_data(skb, 0); */
5179 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5180 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5181 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5182 BPF_FUNC_skb_pull_data);
5185 * return TC_ACT_SHOT;
5187 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
5188 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
5189 *insn++ = BPF_EXIT_INSN();
5192 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
5194 *insn++ = prog->insnsi[0];
5196 return insn - insn_buf;
5199 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
5200 struct bpf_insn *insn_buf)
5202 bool indirect = BPF_MODE(orig->code) == BPF_IND;
5203 struct bpf_insn *insn = insn_buf;
5205 /* We're guaranteed here that CTX is in R6. */
5206 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
5208 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
5210 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
5212 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
5215 switch (BPF_SIZE(orig->code)) {
5217 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
5220 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
5223 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
5227 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
5228 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
5229 *insn++ = BPF_EXIT_INSN();
5231 return insn - insn_buf;
5234 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
5235 const struct bpf_prog *prog)
5237 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
5240 static bool tc_cls_act_is_valid_access(int off, int size,
5241 enum bpf_access_type type,
5242 const struct bpf_prog *prog,
5243 struct bpf_insn_access_aux *info)
5245 if (type == BPF_WRITE) {
5247 case bpf_ctx_range(struct __sk_buff, mark):
5248 case bpf_ctx_range(struct __sk_buff, tc_index):
5249 case bpf_ctx_range(struct __sk_buff, priority):
5250 case bpf_ctx_range(struct __sk_buff, tc_classid):
5251 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5259 case bpf_ctx_range(struct __sk_buff, data):
5260 info->reg_type = PTR_TO_PACKET;
5262 case bpf_ctx_range(struct __sk_buff, data_meta):
5263 info->reg_type = PTR_TO_PACKET_META;
5265 case bpf_ctx_range(struct __sk_buff, data_end):
5266 info->reg_type = PTR_TO_PACKET_END;
5268 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5272 return bpf_skb_is_valid_access(off, size, type, prog, info);
5275 static bool __is_valid_xdp_access(int off, int size)
5277 if (off < 0 || off >= sizeof(struct xdp_md))
5279 if (off % size != 0)
5281 if (size != sizeof(__u32))
5287 static bool xdp_is_valid_access(int off, int size,
5288 enum bpf_access_type type,
5289 const struct bpf_prog *prog,
5290 struct bpf_insn_access_aux *info)
5292 if (type == BPF_WRITE) {
5293 if (bpf_prog_is_dev_bound(prog->aux)) {
5295 case offsetof(struct xdp_md, rx_queue_index):
5296 return __is_valid_xdp_access(off, size);
5303 case offsetof(struct xdp_md, data):
5304 info->reg_type = PTR_TO_PACKET;
5306 case offsetof(struct xdp_md, data_meta):
5307 info->reg_type = PTR_TO_PACKET_META;
5309 case offsetof(struct xdp_md, data_end):
5310 info->reg_type = PTR_TO_PACKET_END;
5314 return __is_valid_xdp_access(off, size);
5317 void bpf_warn_invalid_xdp_action(u32 act)
5319 const u32 act_max = XDP_REDIRECT;
5321 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
5322 act > act_max ? "Illegal" : "Driver unsupported",
5325 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
5327 static bool sock_addr_is_valid_access(int off, int size,
5328 enum bpf_access_type type,
5329 const struct bpf_prog *prog,
5330 struct bpf_insn_access_aux *info)
5332 const int size_default = sizeof(__u32);
5334 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
5336 if (off % size != 0)
5339 /* Disallow access to IPv6 fields from IPv4 contex and vise
5343 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5344 switch (prog->expected_attach_type) {
5345 case BPF_CGROUP_INET4_BIND:
5346 case BPF_CGROUP_INET4_CONNECT:
5347 case BPF_CGROUP_UDP4_SENDMSG:
5353 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5354 switch (prog->expected_attach_type) {
5355 case BPF_CGROUP_INET6_BIND:
5356 case BPF_CGROUP_INET6_CONNECT:
5357 case BPF_CGROUP_UDP6_SENDMSG:
5363 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5364 switch (prog->expected_attach_type) {
5365 case BPF_CGROUP_UDP4_SENDMSG:
5371 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5373 switch (prog->expected_attach_type) {
5374 case BPF_CGROUP_UDP6_SENDMSG:
5383 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5384 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5385 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5386 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5388 /* Only narrow read access allowed for now. */
5389 if (type == BPF_READ) {
5390 bpf_ctx_record_field_size(info, size_default);
5391 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5394 if (size != size_default)
5398 case bpf_ctx_range(struct bpf_sock_addr, user_port):
5399 if (size != size_default)
5403 if (type == BPF_READ) {
5404 if (size != size_default)
5414 static bool sock_ops_is_valid_access(int off, int size,
5415 enum bpf_access_type type,
5416 const struct bpf_prog *prog,
5417 struct bpf_insn_access_aux *info)
5419 const int size_default = sizeof(__u32);
5421 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5424 /* The verifier guarantees that size > 0. */
5425 if (off % size != 0)
5428 if (type == BPF_WRITE) {
5430 case offsetof(struct bpf_sock_ops, reply):
5431 case offsetof(struct bpf_sock_ops, sk_txhash):
5432 if (size != size_default)
5440 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5442 if (size != sizeof(__u64))
5446 if (size != size_default)
5455 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5456 const struct bpf_prog *prog)
5458 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
5461 static bool sk_skb_is_valid_access(int off, int size,
5462 enum bpf_access_type type,
5463 const struct bpf_prog *prog,
5464 struct bpf_insn_access_aux *info)
5467 case bpf_ctx_range(struct __sk_buff, tc_classid):
5468 case bpf_ctx_range(struct __sk_buff, data_meta):
5472 if (type == BPF_WRITE) {
5474 case bpf_ctx_range(struct __sk_buff, tc_index):
5475 case bpf_ctx_range(struct __sk_buff, priority):
5483 case bpf_ctx_range(struct __sk_buff, mark):
5485 case bpf_ctx_range(struct __sk_buff, data):
5486 info->reg_type = PTR_TO_PACKET;
5488 case bpf_ctx_range(struct __sk_buff, data_end):
5489 info->reg_type = PTR_TO_PACKET_END;
5493 return bpf_skb_is_valid_access(off, size, type, prog, info);
5496 static bool sk_msg_is_valid_access(int off, int size,
5497 enum bpf_access_type type,
5498 const struct bpf_prog *prog,
5499 struct bpf_insn_access_aux *info)
5501 if (type == BPF_WRITE)
5505 case offsetof(struct sk_msg_md, data):
5506 info->reg_type = PTR_TO_PACKET;
5507 if (size != sizeof(__u64))
5510 case offsetof(struct sk_msg_md, data_end):
5511 info->reg_type = PTR_TO_PACKET_END;
5512 if (size != sizeof(__u64))
5516 if (size != sizeof(__u32))
5520 if (off < 0 || off >= sizeof(struct sk_msg_md))
5522 if (off % size != 0)
5528 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
5529 const struct bpf_insn *si,
5530 struct bpf_insn *insn_buf,
5531 struct bpf_prog *prog, u32 *target_size)
5533 struct bpf_insn *insn = insn_buf;
5537 case offsetof(struct __sk_buff, len):
5538 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5539 bpf_target_off(struct sk_buff, len, 4,
5543 case offsetof(struct __sk_buff, protocol):
5544 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5545 bpf_target_off(struct sk_buff, protocol, 2,
5549 case offsetof(struct __sk_buff, vlan_proto):
5550 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5551 bpf_target_off(struct sk_buff, vlan_proto, 2,
5555 case offsetof(struct __sk_buff, priority):
5556 if (type == BPF_WRITE)
5557 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5558 bpf_target_off(struct sk_buff, priority, 4,
5561 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5562 bpf_target_off(struct sk_buff, priority, 4,
5566 case offsetof(struct __sk_buff, ingress_ifindex):
5567 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5568 bpf_target_off(struct sk_buff, skb_iif, 4,
5572 case offsetof(struct __sk_buff, ifindex):
5573 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
5574 si->dst_reg, si->src_reg,
5575 offsetof(struct sk_buff, dev));
5576 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
5577 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5578 bpf_target_off(struct net_device, ifindex, 4,
5582 case offsetof(struct __sk_buff, hash):
5583 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5584 bpf_target_off(struct sk_buff, hash, 4,
5588 case offsetof(struct __sk_buff, mark):
5589 if (type == BPF_WRITE)
5590 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5591 bpf_target_off(struct sk_buff, mark, 4,
5594 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5595 bpf_target_off(struct sk_buff, mark, 4,
5599 case offsetof(struct __sk_buff, pkt_type):
5601 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
5603 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
5604 #ifdef __BIG_ENDIAN_BITFIELD
5605 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
5609 case offsetof(struct __sk_buff, queue_mapping):
5610 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5611 bpf_target_off(struct sk_buff, queue_mapping, 2,
5615 case offsetof(struct __sk_buff, vlan_present):
5616 case offsetof(struct __sk_buff, vlan_tci):
5617 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
5619 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5620 bpf_target_off(struct sk_buff, vlan_tci, 2,
5622 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
5623 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
5626 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
5627 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
5631 case offsetof(struct __sk_buff, cb[0]) ...
5632 offsetofend(struct __sk_buff, cb[4]) - 1:
5633 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
5634 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
5635 offsetof(struct qdisc_skb_cb, data)) %
5638 prog->cb_access = 1;
5640 off -= offsetof(struct __sk_buff, cb[0]);
5641 off += offsetof(struct sk_buff, cb);
5642 off += offsetof(struct qdisc_skb_cb, data);
5643 if (type == BPF_WRITE)
5644 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
5647 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
5651 case offsetof(struct __sk_buff, tc_classid):
5652 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
5655 off -= offsetof(struct __sk_buff, tc_classid);
5656 off += offsetof(struct sk_buff, cb);
5657 off += offsetof(struct qdisc_skb_cb, tc_classid);
5659 if (type == BPF_WRITE)
5660 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
5663 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
5667 case offsetof(struct __sk_buff, data):
5668 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
5669 si->dst_reg, si->src_reg,
5670 offsetof(struct sk_buff, data));
5673 case offsetof(struct __sk_buff, data_meta):
5675 off -= offsetof(struct __sk_buff, data_meta);
5676 off += offsetof(struct sk_buff, cb);
5677 off += offsetof(struct bpf_skb_data_end, data_meta);
5678 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5682 case offsetof(struct __sk_buff, data_end):
5684 off -= offsetof(struct __sk_buff, data_end);
5685 off += offsetof(struct sk_buff, cb);
5686 off += offsetof(struct bpf_skb_data_end, data_end);
5687 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5691 case offsetof(struct __sk_buff, tc_index):
5692 #ifdef CONFIG_NET_SCHED
5693 if (type == BPF_WRITE)
5694 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
5695 bpf_target_off(struct sk_buff, tc_index, 2,
5698 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5699 bpf_target_off(struct sk_buff, tc_index, 2,
5703 if (type == BPF_WRITE)
5704 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
5706 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5710 case offsetof(struct __sk_buff, napi_id):
5711 #if defined(CONFIG_NET_RX_BUSY_POLL)
5712 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5713 bpf_target_off(struct sk_buff, napi_id, 4,
5715 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
5716 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5719 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5722 case offsetof(struct __sk_buff, family):
5723 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
5725 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5726 si->dst_reg, si->src_reg,
5727 offsetof(struct sk_buff, sk));
5728 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5729 bpf_target_off(struct sock_common,
5733 case offsetof(struct __sk_buff, remote_ip4):
5734 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
5736 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5737 si->dst_reg, si->src_reg,
5738 offsetof(struct sk_buff, sk));
5739 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5740 bpf_target_off(struct sock_common,
5744 case offsetof(struct __sk_buff, local_ip4):
5745 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5746 skc_rcv_saddr) != 4);
5748 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5749 si->dst_reg, si->src_reg,
5750 offsetof(struct sk_buff, sk));
5751 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5752 bpf_target_off(struct sock_common,
5756 case offsetof(struct __sk_buff, remote_ip6[0]) ...
5757 offsetof(struct __sk_buff, remote_ip6[3]):
5758 #if IS_ENABLED(CONFIG_IPV6)
5759 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5760 skc_v6_daddr.s6_addr32[0]) != 4);
5763 off -= offsetof(struct __sk_buff, remote_ip6[0]);
5765 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5766 si->dst_reg, si->src_reg,
5767 offsetof(struct sk_buff, sk));
5768 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5769 offsetof(struct sock_common,
5770 skc_v6_daddr.s6_addr32[0]) +
5773 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5776 case offsetof(struct __sk_buff, local_ip6[0]) ...
5777 offsetof(struct __sk_buff, local_ip6[3]):
5778 #if IS_ENABLED(CONFIG_IPV6)
5779 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5780 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
5783 off -= offsetof(struct __sk_buff, local_ip6[0]);
5785 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5786 si->dst_reg, si->src_reg,
5787 offsetof(struct sk_buff, sk));
5788 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5789 offsetof(struct sock_common,
5790 skc_v6_rcv_saddr.s6_addr32[0]) +
5793 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5797 case offsetof(struct __sk_buff, remote_port):
5798 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
5800 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5801 si->dst_reg, si->src_reg,
5802 offsetof(struct sk_buff, sk));
5803 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5804 bpf_target_off(struct sock_common,
5807 #ifndef __BIG_ENDIAN_BITFIELD
5808 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
5812 case offsetof(struct __sk_buff, local_port):
5813 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
5815 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5816 si->dst_reg, si->src_reg,
5817 offsetof(struct sk_buff, sk));
5818 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5819 bpf_target_off(struct sock_common,
5820 skc_num, 2, target_size));
5824 return insn - insn_buf;
5827 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
5828 const struct bpf_insn *si,
5829 struct bpf_insn *insn_buf,
5830 struct bpf_prog *prog, u32 *target_size)
5832 struct bpf_insn *insn = insn_buf;
5836 case offsetof(struct bpf_sock, bound_dev_if):
5837 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
5839 if (type == BPF_WRITE)
5840 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5841 offsetof(struct sock, sk_bound_dev_if));
5843 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5844 offsetof(struct sock, sk_bound_dev_if));
5847 case offsetof(struct bpf_sock, mark):
5848 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
5850 if (type == BPF_WRITE)
5851 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5852 offsetof(struct sock, sk_mark));
5854 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5855 offsetof(struct sock, sk_mark));
5858 case offsetof(struct bpf_sock, priority):
5859 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
5861 if (type == BPF_WRITE)
5862 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5863 offsetof(struct sock, sk_priority));
5865 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5866 offsetof(struct sock, sk_priority));
5869 case offsetof(struct bpf_sock, family):
5870 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
5872 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5873 offsetof(struct sock, sk_family));
5876 case offsetof(struct bpf_sock, type):
5877 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5878 offsetof(struct sock, __sk_flags_offset));
5879 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
5880 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
5883 case offsetof(struct bpf_sock, protocol):
5884 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5885 offsetof(struct sock, __sk_flags_offset));
5886 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
5887 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
5890 case offsetof(struct bpf_sock, src_ip4):
5891 *insn++ = BPF_LDX_MEM(
5892 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
5893 bpf_target_off(struct sock_common, skc_rcv_saddr,
5894 FIELD_SIZEOF(struct sock_common,
5899 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5900 #if IS_ENABLED(CONFIG_IPV6)
5902 off -= offsetof(struct bpf_sock, src_ip6[0]);
5903 *insn++ = BPF_LDX_MEM(
5904 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
5907 skc_v6_rcv_saddr.s6_addr32[0],
5908 FIELD_SIZEOF(struct sock_common,
5909 skc_v6_rcv_saddr.s6_addr32[0]),
5910 target_size) + off);
5913 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5917 case offsetof(struct bpf_sock, src_port):
5918 *insn++ = BPF_LDX_MEM(
5919 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
5920 si->dst_reg, si->src_reg,
5921 bpf_target_off(struct sock_common, skc_num,
5922 FIELD_SIZEOF(struct sock_common,
5928 return insn - insn_buf;
5931 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
5932 const struct bpf_insn *si,
5933 struct bpf_insn *insn_buf,
5934 struct bpf_prog *prog, u32 *target_size)
5936 struct bpf_insn *insn = insn_buf;
5939 case offsetof(struct __sk_buff, ifindex):
5940 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
5941 si->dst_reg, si->src_reg,
5942 offsetof(struct sk_buff, dev));
5943 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5944 bpf_target_off(struct net_device, ifindex, 4,
5948 return bpf_convert_ctx_access(type, si, insn_buf, prog,
5952 return insn - insn_buf;
5955 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
5956 const struct bpf_insn *si,
5957 struct bpf_insn *insn_buf,
5958 struct bpf_prog *prog, u32 *target_size)
5960 struct bpf_insn *insn = insn_buf;
5963 case offsetof(struct xdp_md, data):
5964 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
5965 si->dst_reg, si->src_reg,
5966 offsetof(struct xdp_buff, data));
5968 case offsetof(struct xdp_md, data_meta):
5969 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
5970 si->dst_reg, si->src_reg,
5971 offsetof(struct xdp_buff, data_meta));
5973 case offsetof(struct xdp_md, data_end):
5974 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
5975 si->dst_reg, si->src_reg,
5976 offsetof(struct xdp_buff, data_end));
5978 case offsetof(struct xdp_md, ingress_ifindex):
5979 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
5980 si->dst_reg, si->src_reg,
5981 offsetof(struct xdp_buff, rxq));
5982 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
5983 si->dst_reg, si->dst_reg,
5984 offsetof(struct xdp_rxq_info, dev));
5985 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5986 offsetof(struct net_device, ifindex));
5988 case offsetof(struct xdp_md, rx_queue_index):
5989 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
5990 si->dst_reg, si->src_reg,
5991 offsetof(struct xdp_buff, rxq));
5992 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5993 offsetof(struct xdp_rxq_info,
5998 return insn - insn_buf;
6001 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
6002 * context Structure, F is Field in context structure that contains a pointer
6003 * to Nested Structure of type NS that has the field NF.
6005 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
6006 * sure that SIZE is not greater than actual size of S.F.NF.
6008 * If offset OFF is provided, the load happens from that offset relative to
6011 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
6013 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
6014 si->src_reg, offsetof(S, F)); \
6015 *insn++ = BPF_LDX_MEM( \
6016 SIZE, si->dst_reg, si->dst_reg, \
6017 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6022 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
6023 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
6024 BPF_FIELD_SIZEOF(NS, NF), 0)
6026 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
6027 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
6029 * It doesn't support SIZE argument though since narrow stores are not
6030 * supported for now.
6032 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
6033 * "register" since two registers available in convert_ctx_access are not
6034 * enough: we can't override neither SRC, since it contains value to store, nor
6035 * DST since it contains pointer to context that may be used by later
6036 * instructions. But we need a temporary place to save pointer to nested
6037 * structure whose field we want to store to.
6039 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
6041 int tmp_reg = BPF_REG_9; \
6042 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6044 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6046 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
6048 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
6049 si->dst_reg, offsetof(S, F)); \
6050 *insn++ = BPF_STX_MEM( \
6051 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
6052 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6055 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
6059 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
6062 if (type == BPF_WRITE) { \
6063 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6066 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6067 S, NS, F, NF, SIZE, OFF); \
6071 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6072 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6073 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6075 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6076 const struct bpf_insn *si,
6077 struct bpf_insn *insn_buf,
6078 struct bpf_prog *prog, u32 *target_size)
6080 struct bpf_insn *insn = insn_buf;
6084 case offsetof(struct bpf_sock_addr, user_family):
6085 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6086 struct sockaddr, uaddr, sa_family);
6089 case offsetof(struct bpf_sock_addr, user_ip4):
6090 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6091 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6092 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6095 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6097 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6098 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6099 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6100 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6104 case offsetof(struct bpf_sock_addr, user_port):
6105 /* To get port we need to know sa_family first and then treat
6106 * sockaddr as either sockaddr_in or sockaddr_in6.
6107 * Though we can simplify since port field has same offset and
6108 * size in both structures.
6109 * Here we check this invariant and use just one of the
6110 * structures if it's true.
6112 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
6113 offsetof(struct sockaddr_in6, sin6_port));
6114 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
6115 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
6116 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
6117 struct sockaddr_in6, uaddr,
6118 sin6_port, tmp_reg);
6121 case offsetof(struct bpf_sock_addr, family):
6122 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6123 struct sock, sk, sk_family);
6126 case offsetof(struct bpf_sock_addr, type):
6127 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6128 struct bpf_sock_addr_kern, struct sock, sk,
6129 __sk_flags_offset, BPF_W, 0);
6130 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6131 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6134 case offsetof(struct bpf_sock_addr, protocol):
6135 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6136 struct bpf_sock_addr_kern, struct sock, sk,
6137 __sk_flags_offset, BPF_W, 0);
6138 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6139 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
6143 case offsetof(struct bpf_sock_addr, msg_src_ip4):
6144 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
6145 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6146 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
6147 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
6150 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6153 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
6154 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
6155 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6156 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
6157 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
6161 return insn - insn_buf;
6164 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
6165 const struct bpf_insn *si,
6166 struct bpf_insn *insn_buf,
6167 struct bpf_prog *prog,
6170 struct bpf_insn *insn = insn_buf;
6174 case offsetof(struct bpf_sock_ops, op) ...
6175 offsetof(struct bpf_sock_ops, replylong[3]):
6176 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
6177 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
6178 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
6179 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
6180 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
6181 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
6183 off -= offsetof(struct bpf_sock_ops, op);
6184 off += offsetof(struct bpf_sock_ops_kern, op);
6185 if (type == BPF_WRITE)
6186 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6189 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6193 case offsetof(struct bpf_sock_ops, family):
6194 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6196 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6197 struct bpf_sock_ops_kern, sk),
6198 si->dst_reg, si->src_reg,
6199 offsetof(struct bpf_sock_ops_kern, sk));
6200 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6201 offsetof(struct sock_common, skc_family));
6204 case offsetof(struct bpf_sock_ops, remote_ip4):
6205 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6207 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6208 struct bpf_sock_ops_kern, sk),
6209 si->dst_reg, si->src_reg,
6210 offsetof(struct bpf_sock_ops_kern, sk));
6211 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6212 offsetof(struct sock_common, skc_daddr));
6215 case offsetof(struct bpf_sock_ops, local_ip4):
6216 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6217 skc_rcv_saddr) != 4);
6219 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6220 struct bpf_sock_ops_kern, sk),
6221 si->dst_reg, si->src_reg,
6222 offsetof(struct bpf_sock_ops_kern, sk));
6223 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6224 offsetof(struct sock_common,
6228 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
6229 offsetof(struct bpf_sock_ops, remote_ip6[3]):
6230 #if IS_ENABLED(CONFIG_IPV6)
6231 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6232 skc_v6_daddr.s6_addr32[0]) != 4);
6235 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
6236 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6237 struct bpf_sock_ops_kern, sk),
6238 si->dst_reg, si->src_reg,
6239 offsetof(struct bpf_sock_ops_kern, sk));
6240 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6241 offsetof(struct sock_common,
6242 skc_v6_daddr.s6_addr32[0]) +
6245 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6249 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
6250 offsetof(struct bpf_sock_ops, local_ip6[3]):
6251 #if IS_ENABLED(CONFIG_IPV6)
6252 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6253 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6256 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
6257 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6258 struct bpf_sock_ops_kern, sk),
6259 si->dst_reg, si->src_reg,
6260 offsetof(struct bpf_sock_ops_kern, sk));
6261 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6262 offsetof(struct sock_common,
6263 skc_v6_rcv_saddr.s6_addr32[0]) +
6266 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6270 case offsetof(struct bpf_sock_ops, remote_port):
6271 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6273 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6274 struct bpf_sock_ops_kern, sk),
6275 si->dst_reg, si->src_reg,
6276 offsetof(struct bpf_sock_ops_kern, sk));
6277 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6278 offsetof(struct sock_common, skc_dport));
6279 #ifndef __BIG_ENDIAN_BITFIELD
6280 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6284 case offsetof(struct bpf_sock_ops, local_port):
6285 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6287 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6288 struct bpf_sock_ops_kern, sk),
6289 si->dst_reg, si->src_reg,
6290 offsetof(struct bpf_sock_ops_kern, sk));
6291 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6292 offsetof(struct sock_common, skc_num));
6295 case offsetof(struct bpf_sock_ops, is_fullsock):
6296 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6297 struct bpf_sock_ops_kern,
6299 si->dst_reg, si->src_reg,
6300 offsetof(struct bpf_sock_ops_kern,
6304 case offsetof(struct bpf_sock_ops, state):
6305 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
6307 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6308 struct bpf_sock_ops_kern, sk),
6309 si->dst_reg, si->src_reg,
6310 offsetof(struct bpf_sock_ops_kern, sk));
6311 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
6312 offsetof(struct sock_common, skc_state));
6315 case offsetof(struct bpf_sock_ops, rtt_min):
6316 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
6317 sizeof(struct minmax));
6318 BUILD_BUG_ON(sizeof(struct minmax) <
6319 sizeof(struct minmax_sample));
6321 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6322 struct bpf_sock_ops_kern, sk),
6323 si->dst_reg, si->src_reg,
6324 offsetof(struct bpf_sock_ops_kern, sk));
6325 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6326 offsetof(struct tcp_sock, rtt_min) +
6327 FIELD_SIZEOF(struct minmax_sample, t));
6330 /* Helper macro for adding read access to tcp_sock or sock fields. */
6331 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6333 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6334 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6335 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6336 struct bpf_sock_ops_kern, \
6338 si->dst_reg, si->src_reg, \
6339 offsetof(struct bpf_sock_ops_kern, \
6341 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
6342 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6343 struct bpf_sock_ops_kern, sk),\
6344 si->dst_reg, si->src_reg, \
6345 offsetof(struct bpf_sock_ops_kern, sk));\
6346 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
6348 si->dst_reg, si->dst_reg, \
6349 offsetof(OBJ, OBJ_FIELD)); \
6352 /* Helper macro for adding write access to tcp_sock or sock fields.
6353 * The macro is called with two registers, dst_reg which contains a pointer
6354 * to ctx (context) and src_reg which contains the value that should be
6355 * stored. However, we need an additional register since we cannot overwrite
6356 * dst_reg because it may be used later in the program.
6357 * Instead we "borrow" one of the other register. We first save its value
6358 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
6359 * it at the end of the macro.
6361 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6363 int reg = BPF_REG_9; \
6364 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6365 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6366 if (si->dst_reg == reg || si->src_reg == reg) \
6368 if (si->dst_reg == reg || si->src_reg == reg) \
6370 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
6371 offsetof(struct bpf_sock_ops_kern, \
6373 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6374 struct bpf_sock_ops_kern, \
6377 offsetof(struct bpf_sock_ops_kern, \
6379 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
6380 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6381 struct bpf_sock_ops_kern, sk),\
6383 offsetof(struct bpf_sock_ops_kern, sk));\
6384 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
6386 offsetof(OBJ, OBJ_FIELD)); \
6387 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
6388 offsetof(struct bpf_sock_ops_kern, \
6392 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
6394 if (TYPE == BPF_WRITE) \
6395 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6397 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6400 case offsetof(struct bpf_sock_ops, snd_cwnd):
6401 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
6404 case offsetof(struct bpf_sock_ops, srtt_us):
6405 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
6408 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
6409 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
6413 case offsetof(struct bpf_sock_ops, snd_ssthresh):
6414 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
6417 case offsetof(struct bpf_sock_ops, rcv_nxt):
6418 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
6421 case offsetof(struct bpf_sock_ops, snd_nxt):
6422 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
6425 case offsetof(struct bpf_sock_ops, snd_una):
6426 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
6429 case offsetof(struct bpf_sock_ops, mss_cache):
6430 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
6433 case offsetof(struct bpf_sock_ops, ecn_flags):
6434 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6437 case offsetof(struct bpf_sock_ops, rate_delivered):
6438 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6442 case offsetof(struct bpf_sock_ops, rate_interval_us):
6443 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6447 case offsetof(struct bpf_sock_ops, packets_out):
6448 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6451 case offsetof(struct bpf_sock_ops, retrans_out):
6452 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6455 case offsetof(struct bpf_sock_ops, total_retrans):
6456 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6460 case offsetof(struct bpf_sock_ops, segs_in):
6461 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
6464 case offsetof(struct bpf_sock_ops, data_segs_in):
6465 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
6468 case offsetof(struct bpf_sock_ops, segs_out):
6469 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
6472 case offsetof(struct bpf_sock_ops, data_segs_out):
6473 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
6477 case offsetof(struct bpf_sock_ops, lost_out):
6478 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
6481 case offsetof(struct bpf_sock_ops, sacked_out):
6482 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
6485 case offsetof(struct bpf_sock_ops, sk_txhash):
6486 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
6490 case offsetof(struct bpf_sock_ops, bytes_received):
6491 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
6495 case offsetof(struct bpf_sock_ops, bytes_acked):
6496 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
6500 return insn - insn_buf;
6503 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
6504 const struct bpf_insn *si,
6505 struct bpf_insn *insn_buf,
6506 struct bpf_prog *prog, u32 *target_size)
6508 struct bpf_insn *insn = insn_buf;
6512 case offsetof(struct __sk_buff, data_end):
6514 off -= offsetof(struct __sk_buff, data_end);
6515 off += offsetof(struct sk_buff, cb);
6516 off += offsetof(struct tcp_skb_cb, bpf.data_end);
6517 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6521 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6525 return insn - insn_buf;
6528 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
6529 const struct bpf_insn *si,
6530 struct bpf_insn *insn_buf,
6531 struct bpf_prog *prog, u32 *target_size)
6533 struct bpf_insn *insn = insn_buf;
6534 #if IS_ENABLED(CONFIG_IPV6)
6539 case offsetof(struct sk_msg_md, data):
6540 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data),
6541 si->dst_reg, si->src_reg,
6542 offsetof(struct sk_msg_buff, data));
6544 case offsetof(struct sk_msg_md, data_end):
6545 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data_end),
6546 si->dst_reg, si->src_reg,
6547 offsetof(struct sk_msg_buff, data_end));
6549 case offsetof(struct sk_msg_md, family):
6550 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6552 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6553 struct sk_msg_buff, sk),
6554 si->dst_reg, si->src_reg,
6555 offsetof(struct sk_msg_buff, sk));
6556 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6557 offsetof(struct sock_common, skc_family));
6560 case offsetof(struct sk_msg_md, remote_ip4):
6561 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6563 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6564 struct sk_msg_buff, sk),
6565 si->dst_reg, si->src_reg,
6566 offsetof(struct sk_msg_buff, sk));
6567 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6568 offsetof(struct sock_common, skc_daddr));
6571 case offsetof(struct sk_msg_md, local_ip4):
6572 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6573 skc_rcv_saddr) != 4);
6575 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6576 struct sk_msg_buff, sk),
6577 si->dst_reg, si->src_reg,
6578 offsetof(struct sk_msg_buff, sk));
6579 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6580 offsetof(struct sock_common,
6584 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
6585 offsetof(struct sk_msg_md, remote_ip6[3]):
6586 #if IS_ENABLED(CONFIG_IPV6)
6587 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6588 skc_v6_daddr.s6_addr32[0]) != 4);
6591 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
6592 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6593 struct sk_msg_buff, sk),
6594 si->dst_reg, si->src_reg,
6595 offsetof(struct sk_msg_buff, sk));
6596 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6597 offsetof(struct sock_common,
6598 skc_v6_daddr.s6_addr32[0]) +
6601 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6605 case offsetof(struct sk_msg_md, local_ip6[0]) ...
6606 offsetof(struct sk_msg_md, local_ip6[3]):
6607 #if IS_ENABLED(CONFIG_IPV6)
6608 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6609 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6612 off -= offsetof(struct sk_msg_md, local_ip6[0]);
6613 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6614 struct sk_msg_buff, sk),
6615 si->dst_reg, si->src_reg,
6616 offsetof(struct sk_msg_buff, sk));
6617 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6618 offsetof(struct sock_common,
6619 skc_v6_rcv_saddr.s6_addr32[0]) +
6622 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6626 case offsetof(struct sk_msg_md, remote_port):
6627 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6629 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6630 struct sk_msg_buff, sk),
6631 si->dst_reg, si->src_reg,
6632 offsetof(struct sk_msg_buff, sk));
6633 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6634 offsetof(struct sock_common, skc_dport));
6635 #ifndef __BIG_ENDIAN_BITFIELD
6636 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6640 case offsetof(struct sk_msg_md, local_port):
6641 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6643 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6644 struct sk_msg_buff, sk),
6645 si->dst_reg, si->src_reg,
6646 offsetof(struct sk_msg_buff, sk));
6647 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6648 offsetof(struct sock_common, skc_num));
6652 return insn - insn_buf;
6655 const struct bpf_verifier_ops sk_filter_verifier_ops = {
6656 .get_func_proto = sk_filter_func_proto,
6657 .is_valid_access = sk_filter_is_valid_access,
6658 .convert_ctx_access = bpf_convert_ctx_access,
6659 .gen_ld_abs = bpf_gen_ld_abs,
6662 const struct bpf_prog_ops sk_filter_prog_ops = {
6663 .test_run = bpf_prog_test_run_skb,
6666 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
6667 .get_func_proto = tc_cls_act_func_proto,
6668 .is_valid_access = tc_cls_act_is_valid_access,
6669 .convert_ctx_access = tc_cls_act_convert_ctx_access,
6670 .gen_prologue = tc_cls_act_prologue,
6671 .gen_ld_abs = bpf_gen_ld_abs,
6674 const struct bpf_prog_ops tc_cls_act_prog_ops = {
6675 .test_run = bpf_prog_test_run_skb,
6678 const struct bpf_verifier_ops xdp_verifier_ops = {
6679 .get_func_proto = xdp_func_proto,
6680 .is_valid_access = xdp_is_valid_access,
6681 .convert_ctx_access = xdp_convert_ctx_access,
6684 const struct bpf_prog_ops xdp_prog_ops = {
6685 .test_run = bpf_prog_test_run_xdp,
6688 const struct bpf_verifier_ops cg_skb_verifier_ops = {
6689 .get_func_proto = sk_filter_func_proto,
6690 .is_valid_access = sk_filter_is_valid_access,
6691 .convert_ctx_access = bpf_convert_ctx_access,
6694 const struct bpf_prog_ops cg_skb_prog_ops = {
6695 .test_run = bpf_prog_test_run_skb,
6698 const struct bpf_verifier_ops lwt_in_verifier_ops = {
6699 .get_func_proto = lwt_in_func_proto,
6700 .is_valid_access = lwt_is_valid_access,
6701 .convert_ctx_access = bpf_convert_ctx_access,
6704 const struct bpf_prog_ops lwt_in_prog_ops = {
6705 .test_run = bpf_prog_test_run_skb,
6708 const struct bpf_verifier_ops lwt_out_verifier_ops = {
6709 .get_func_proto = lwt_out_func_proto,
6710 .is_valid_access = lwt_is_valid_access,
6711 .convert_ctx_access = bpf_convert_ctx_access,
6714 const struct bpf_prog_ops lwt_out_prog_ops = {
6715 .test_run = bpf_prog_test_run_skb,
6718 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
6719 .get_func_proto = lwt_xmit_func_proto,
6720 .is_valid_access = lwt_is_valid_access,
6721 .convert_ctx_access = bpf_convert_ctx_access,
6722 .gen_prologue = tc_cls_act_prologue,
6725 const struct bpf_prog_ops lwt_xmit_prog_ops = {
6726 .test_run = bpf_prog_test_run_skb,
6729 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
6730 .get_func_proto = lwt_seg6local_func_proto,
6731 .is_valid_access = lwt_is_valid_access,
6732 .convert_ctx_access = bpf_convert_ctx_access,
6735 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
6736 .test_run = bpf_prog_test_run_skb,
6739 const struct bpf_verifier_ops cg_sock_verifier_ops = {
6740 .get_func_proto = sock_filter_func_proto,
6741 .is_valid_access = sock_filter_is_valid_access,
6742 .convert_ctx_access = sock_filter_convert_ctx_access,
6745 const struct bpf_prog_ops cg_sock_prog_ops = {
6748 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
6749 .get_func_proto = sock_addr_func_proto,
6750 .is_valid_access = sock_addr_is_valid_access,
6751 .convert_ctx_access = sock_addr_convert_ctx_access,
6754 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
6757 const struct bpf_verifier_ops sock_ops_verifier_ops = {
6758 .get_func_proto = sock_ops_func_proto,
6759 .is_valid_access = sock_ops_is_valid_access,
6760 .convert_ctx_access = sock_ops_convert_ctx_access,
6763 const struct bpf_prog_ops sock_ops_prog_ops = {
6766 const struct bpf_verifier_ops sk_skb_verifier_ops = {
6767 .get_func_proto = sk_skb_func_proto,
6768 .is_valid_access = sk_skb_is_valid_access,
6769 .convert_ctx_access = sk_skb_convert_ctx_access,
6770 .gen_prologue = sk_skb_prologue,
6773 const struct bpf_prog_ops sk_skb_prog_ops = {
6776 const struct bpf_verifier_ops sk_msg_verifier_ops = {
6777 .get_func_proto = sk_msg_func_proto,
6778 .is_valid_access = sk_msg_is_valid_access,
6779 .convert_ctx_access = sk_msg_convert_ctx_access,
6782 const struct bpf_prog_ops sk_msg_prog_ops = {
6785 int sk_detach_filter(struct sock *sk)
6788 struct sk_filter *filter;
6790 if (sock_flag(sk, SOCK_FILTER_LOCKED))
6793 filter = rcu_dereference_protected(sk->sk_filter,
6794 lockdep_sock_is_held(sk));
6796 RCU_INIT_POINTER(sk->sk_filter, NULL);
6797 sk_filter_uncharge(sk, filter);
6803 EXPORT_SYMBOL_GPL(sk_detach_filter);
6805 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
6808 struct sock_fprog_kern *fprog;
6809 struct sk_filter *filter;
6813 filter = rcu_dereference_protected(sk->sk_filter,
6814 lockdep_sock_is_held(sk));
6818 /* We're copying the filter that has been originally attached,
6819 * so no conversion/decode needed anymore. eBPF programs that
6820 * have no original program cannot be dumped through this.
6823 fprog = filter->prog->orig_prog;
6829 /* User space only enquires number of filter blocks. */
6833 if (len < fprog->len)
6837 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
6840 /* Instead of bytes, the API requests to return the number