1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux Socket Filter - Kernel level socket filtering
5 * Based on the design of the Berkeley Packet Filter. The new
6 * internal format has been designed by PLUMgrid:
8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
12 * Jay Schulist <jschlst@samba.org>
13 * Alexei Starovoitov <ast@plumgrid.com>
14 * Daniel Borkmann <dborkman@redhat.com>
16 * Andi Kleen - Fix a few bad bugs and races.
17 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
20 #include <linux/module.h>
21 #include <linux/types.h>
23 #include <linux/fcntl.h>
24 #include <linux/socket.h>
25 #include <linux/sock_diag.h>
27 #include <linux/inet.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_packet.h>
30 #include <linux/if_arp.h>
31 #include <linux/gfp.h>
32 #include <net/inet_common.h>
34 #include <net/protocol.h>
35 #include <net/netlink.h>
36 #include <linux/skbuff.h>
37 #include <linux/skmsg.h>
39 #include <net/flow_dissector.h>
40 #include <linux/errno.h>
41 #include <linux/timer.h>
42 #include <linux/uaccess.h>
43 #include <asm/unaligned.h>
44 #include <asm/cmpxchg.h>
45 #include <linux/filter.h>
46 #include <linux/ratelimit.h>
47 #include <linux/seccomp.h>
48 #include <linux/if_vlan.h>
49 #include <linux/bpf.h>
50 #include <net/sch_generic.h>
51 #include <net/cls_cgroup.h>
52 #include <net/dst_metadata.h>
54 #include <net/sock_reuseport.h>
55 #include <net/busy_poll.h>
59 #include <linux/bpf_trace.h>
60 #include <net/xdp_sock.h>
61 #include <linux/inetdevice.h>
62 #include <net/inet_hashtables.h>
63 #include <net/inet6_hashtables.h>
64 #include <net/ip_fib.h>
65 #include <net/nexthop.h>
69 #include <net/net_namespace.h>
70 #include <linux/seg6_local.h>
72 #include <net/seg6_local.h>
73 #include <net/lwtunnel.h>
74 #include <net/ipv6_stubs.h>
75 #include <net/bpf_sk_storage.h>
78 * sk_filter_trim_cap - run a packet through a socket filter
79 * @sk: sock associated with &sk_buff
80 * @skb: buffer to filter
81 * @cap: limit on how short the eBPF program may trim the packet
83 * Run the eBPF program and then cut skb->data to correct size returned by
84 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
85 * than pkt_len we keep whole skb->data. This is the socket level
86 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
87 * be accepted or -EPERM if the packet should be tossed.
90 int sk_filter_trim_cap(struct sock
*sk
, struct sk_buff
*skb
, unsigned int cap
)
93 struct sk_filter
*filter
;
96 * If the skb was allocated from pfmemalloc reserves, only
97 * allow SOCK_MEMALLOC sockets to use it as this socket is
100 if (skb_pfmemalloc(skb
) && !sock_flag(sk
, SOCK_MEMALLOC
)) {
101 NET_INC_STATS(sock_net(sk
), LINUX_MIB_PFMEMALLOCDROP
);
104 err
= BPF_CGROUP_RUN_PROG_INET_INGRESS(sk
, skb
);
108 err
= security_sock_rcv_skb(sk
, skb
);
113 filter
= rcu_dereference(sk
->sk_filter
);
115 struct sock
*save_sk
= skb
->sk
;
116 unsigned int pkt_len
;
119 pkt_len
= bpf_prog_run_save_cb(filter
->prog
, skb
);
121 err
= pkt_len
? pskb_trim(skb
, max(cap
, pkt_len
)) : -EPERM
;
127 EXPORT_SYMBOL(sk_filter_trim_cap
);
129 BPF_CALL_1(bpf_skb_get_pay_offset
, struct sk_buff
*, skb
)
131 return skb_get_poff(skb
);
134 BPF_CALL_3(bpf_skb_get_nlattr
, struct sk_buff
*, skb
, u32
, a
, u32
, x
)
138 if (skb_is_nonlinear(skb
))
141 if (skb
->len
< sizeof(struct nlattr
))
144 if (a
> skb
->len
- sizeof(struct nlattr
))
147 nla
= nla_find((struct nlattr
*) &skb
->data
[a
], skb
->len
- a
, x
);
149 return (void *) nla
- (void *) skb
->data
;
154 BPF_CALL_3(bpf_skb_get_nlattr_nest
, struct sk_buff
*, skb
, u32
, a
, u32
, x
)
158 if (skb_is_nonlinear(skb
))
161 if (skb
->len
< sizeof(struct nlattr
))
164 if (a
> skb
->len
- sizeof(struct nlattr
))
167 nla
= (struct nlattr
*) &skb
->data
[a
];
168 if (nla
->nla_len
> skb
->len
- a
)
171 nla
= nla_find_nested(nla
, x
);
173 return (void *) nla
- (void *) skb
->data
;
178 BPF_CALL_4(bpf_skb_load_helper_8
, const struct sk_buff
*, skb
, const void *,
179 data
, int, headlen
, int, offset
)
182 const int len
= sizeof(tmp
);
185 if (headlen
- offset
>= len
)
186 return *(u8
*)(data
+ offset
);
187 if (!skb_copy_bits(skb
, offset
, &tmp
, sizeof(tmp
)))
190 ptr
= bpf_internal_load_pointer_neg_helper(skb
, offset
, len
);
198 BPF_CALL_2(bpf_skb_load_helper_8_no_cache
, const struct sk_buff
*, skb
,
201 return ____bpf_skb_load_helper_8(skb
, skb
->data
, skb
->len
- skb
->data_len
,
205 BPF_CALL_4(bpf_skb_load_helper_16
, const struct sk_buff
*, skb
, const void *,
206 data
, int, headlen
, int, offset
)
209 const int len
= sizeof(tmp
);
212 if (headlen
- offset
>= len
)
213 return get_unaligned_be16(data
+ offset
);
214 if (!skb_copy_bits(skb
, offset
, &tmp
, sizeof(tmp
)))
215 return be16_to_cpu(tmp
);
217 ptr
= bpf_internal_load_pointer_neg_helper(skb
, offset
, len
);
219 return get_unaligned_be16(ptr
);
225 BPF_CALL_2(bpf_skb_load_helper_16_no_cache
, const struct sk_buff
*, skb
,
228 return ____bpf_skb_load_helper_16(skb
, skb
->data
, skb
->len
- skb
->data_len
,
232 BPF_CALL_4(bpf_skb_load_helper_32
, const struct sk_buff
*, skb
, const void *,
233 data
, int, headlen
, int, offset
)
236 const int len
= sizeof(tmp
);
238 if (likely(offset
>= 0)) {
239 if (headlen
- offset
>= len
)
240 return get_unaligned_be32(data
+ offset
);
241 if (!skb_copy_bits(skb
, offset
, &tmp
, sizeof(tmp
)))
242 return be32_to_cpu(tmp
);
244 ptr
= bpf_internal_load_pointer_neg_helper(skb
, offset
, len
);
246 return get_unaligned_be32(ptr
);
252 BPF_CALL_2(bpf_skb_load_helper_32_no_cache
, const struct sk_buff
*, skb
,
255 return ____bpf_skb_load_helper_32(skb
, skb
->data
, skb
->len
- skb
->data_len
,
259 BPF_CALL_0(bpf_get_raw_cpu_id
)
261 return raw_smp_processor_id();
264 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto
= {
265 .func
= bpf_get_raw_cpu_id
,
267 .ret_type
= RET_INTEGER
,
270 static u32
convert_skb_access(int skb_field
, int dst_reg
, int src_reg
,
271 struct bpf_insn
*insn_buf
)
273 struct bpf_insn
*insn
= insn_buf
;
277 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, mark
) != 4);
279 *insn
++ = BPF_LDX_MEM(BPF_W
, dst_reg
, src_reg
,
280 offsetof(struct sk_buff
, mark
));
284 *insn
++ = BPF_LDX_MEM(BPF_B
, dst_reg
, src_reg
, PKT_TYPE_OFFSET());
285 *insn
++ = BPF_ALU32_IMM(BPF_AND
, dst_reg
, PKT_TYPE_MAX
);
286 #ifdef __BIG_ENDIAN_BITFIELD
287 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, dst_reg
, 5);
292 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, queue_mapping
) != 2);
294 *insn
++ = BPF_LDX_MEM(BPF_H
, dst_reg
, src_reg
,
295 offsetof(struct sk_buff
, queue_mapping
));
298 case SKF_AD_VLAN_TAG
:
299 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, vlan_tci
) != 2);
301 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
302 *insn
++ = BPF_LDX_MEM(BPF_H
, dst_reg
, src_reg
,
303 offsetof(struct sk_buff
, vlan_tci
));
305 case SKF_AD_VLAN_TAG_PRESENT
:
306 *insn
++ = BPF_LDX_MEM(BPF_B
, dst_reg
, src_reg
, PKT_VLAN_PRESENT_OFFSET());
307 if (PKT_VLAN_PRESENT_BIT
)
308 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, dst_reg
, PKT_VLAN_PRESENT_BIT
);
309 if (PKT_VLAN_PRESENT_BIT
< 7)
310 *insn
++ = BPF_ALU32_IMM(BPF_AND
, dst_reg
, 1);
314 return insn
- insn_buf
;
317 static bool convert_bpf_extensions(struct sock_filter
*fp
,
318 struct bpf_insn
**insnp
)
320 struct bpf_insn
*insn
= *insnp
;
324 case SKF_AD_OFF
+ SKF_AD_PROTOCOL
:
325 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, protocol
) != 2);
327 /* A = *(u16 *) (CTX + offsetof(protocol)) */
328 *insn
++ = BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_CTX
,
329 offsetof(struct sk_buff
, protocol
));
330 /* A = ntohs(A) [emitting a nop or swap16] */
331 *insn
= BPF_ENDIAN(BPF_FROM_BE
, BPF_REG_A
, 16);
334 case SKF_AD_OFF
+ SKF_AD_PKTTYPE
:
335 cnt
= convert_skb_access(SKF_AD_PKTTYPE
, BPF_REG_A
, BPF_REG_CTX
, insn
);
339 case SKF_AD_OFF
+ SKF_AD_IFINDEX
:
340 case SKF_AD_OFF
+ SKF_AD_HATYPE
:
341 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, ifindex
) != 4);
342 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, type
) != 2);
344 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, dev
),
345 BPF_REG_TMP
, BPF_REG_CTX
,
346 offsetof(struct sk_buff
, dev
));
347 /* if (tmp != 0) goto pc + 1 */
348 *insn
++ = BPF_JMP_IMM(BPF_JNE
, BPF_REG_TMP
, 0, 1);
349 *insn
++ = BPF_EXIT_INSN();
350 if (fp
->k
== SKF_AD_OFF
+ SKF_AD_IFINDEX
)
351 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_TMP
,
352 offsetof(struct net_device
, ifindex
));
354 *insn
= BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_TMP
,
355 offsetof(struct net_device
, type
));
358 case SKF_AD_OFF
+ SKF_AD_MARK
:
359 cnt
= convert_skb_access(SKF_AD_MARK
, BPF_REG_A
, BPF_REG_CTX
, insn
);
363 case SKF_AD_OFF
+ SKF_AD_RXHASH
:
364 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, hash
) != 4);
366 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_CTX
,
367 offsetof(struct sk_buff
, hash
));
370 case SKF_AD_OFF
+ SKF_AD_QUEUE
:
371 cnt
= convert_skb_access(SKF_AD_QUEUE
, BPF_REG_A
, BPF_REG_CTX
, insn
);
375 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG
:
376 cnt
= convert_skb_access(SKF_AD_VLAN_TAG
,
377 BPF_REG_A
, BPF_REG_CTX
, insn
);
381 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
:
382 cnt
= convert_skb_access(SKF_AD_VLAN_TAG_PRESENT
,
383 BPF_REG_A
, BPF_REG_CTX
, insn
);
387 case SKF_AD_OFF
+ SKF_AD_VLAN_TPID
:
388 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, vlan_proto
) != 2);
390 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
391 *insn
++ = BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_CTX
,
392 offsetof(struct sk_buff
, vlan_proto
));
393 /* A = ntohs(A) [emitting a nop or swap16] */
394 *insn
= BPF_ENDIAN(BPF_FROM_BE
, BPF_REG_A
, 16);
397 case SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
:
398 case SKF_AD_OFF
+ SKF_AD_NLATTR
:
399 case SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
:
400 case SKF_AD_OFF
+ SKF_AD_CPU
:
401 case SKF_AD_OFF
+ SKF_AD_RANDOM
:
403 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG1
, BPF_REG_CTX
);
405 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG2
, BPF_REG_A
);
407 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG3
, BPF_REG_X
);
408 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
410 case SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
:
411 *insn
= BPF_EMIT_CALL(bpf_skb_get_pay_offset
);
413 case SKF_AD_OFF
+ SKF_AD_NLATTR
:
414 *insn
= BPF_EMIT_CALL(bpf_skb_get_nlattr
);
416 case SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
:
417 *insn
= BPF_EMIT_CALL(bpf_skb_get_nlattr_nest
);
419 case SKF_AD_OFF
+ SKF_AD_CPU
:
420 *insn
= BPF_EMIT_CALL(bpf_get_raw_cpu_id
);
422 case SKF_AD_OFF
+ SKF_AD_RANDOM
:
423 *insn
= BPF_EMIT_CALL(bpf_user_rnd_u32
);
424 bpf_user_rnd_init_once();
429 case SKF_AD_OFF
+ SKF_AD_ALU_XOR_X
:
431 *insn
= BPF_ALU32_REG(BPF_XOR
, BPF_REG_A
, BPF_REG_X
);
435 /* This is just a dummy call to avoid letting the compiler
436 * evict __bpf_call_base() as an optimization. Placed here
437 * where no-one bothers.
439 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
447 static bool convert_bpf_ld_abs(struct sock_filter
*fp
, struct bpf_insn
**insnp
)
449 const bool unaligned_ok
= IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
);
450 int size
= bpf_size_to_bytes(BPF_SIZE(fp
->code
));
451 bool endian
= BPF_SIZE(fp
->code
) == BPF_H
||
452 BPF_SIZE(fp
->code
) == BPF_W
;
453 bool indirect
= BPF_MODE(fp
->code
) == BPF_IND
;
454 const int ip_align
= NET_IP_ALIGN
;
455 struct bpf_insn
*insn
= *insnp
;
459 ((unaligned_ok
&& offset
>= 0) ||
460 (!unaligned_ok
&& offset
>= 0 &&
461 offset
+ ip_align
>= 0 &&
462 offset
+ ip_align
% size
== 0))) {
463 bool ldx_off_ok
= offset
<= S16_MAX
;
465 *insn
++ = BPF_MOV64_REG(BPF_REG_TMP
, BPF_REG_H
);
467 *insn
++ = BPF_ALU64_IMM(BPF_SUB
, BPF_REG_TMP
, offset
);
468 *insn
++ = BPF_JMP_IMM(BPF_JSLT
, BPF_REG_TMP
,
469 size
, 2 + endian
+ (!ldx_off_ok
* 2));
471 *insn
++ = BPF_LDX_MEM(BPF_SIZE(fp
->code
), BPF_REG_A
,
474 *insn
++ = BPF_MOV64_REG(BPF_REG_TMP
, BPF_REG_D
);
475 *insn
++ = BPF_ALU64_IMM(BPF_ADD
, BPF_REG_TMP
, offset
);
476 *insn
++ = BPF_LDX_MEM(BPF_SIZE(fp
->code
), BPF_REG_A
,
480 *insn
++ = BPF_ENDIAN(BPF_FROM_BE
, BPF_REG_A
, size
* 8);
481 *insn
++ = BPF_JMP_A(8);
484 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG1
, BPF_REG_CTX
);
485 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG2
, BPF_REG_D
);
486 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG3
, BPF_REG_H
);
488 *insn
++ = BPF_MOV64_IMM(BPF_REG_ARG4
, offset
);
490 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG4
, BPF_REG_X
);
492 *insn
++ = BPF_ALU64_IMM(BPF_ADD
, BPF_REG_ARG4
, offset
);
495 switch (BPF_SIZE(fp
->code
)) {
497 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_8
);
500 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_16
);
503 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_32
);
509 *insn
++ = BPF_JMP_IMM(BPF_JSGE
, BPF_REG_A
, 0, 2);
510 *insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_A
, BPF_REG_A
);
511 *insn
= BPF_EXIT_INSN();
518 * bpf_convert_filter - convert filter program
519 * @prog: the user passed filter program
520 * @len: the length of the user passed filter program
521 * @new_prog: allocated 'struct bpf_prog' or NULL
522 * @new_len: pointer to store length of converted program
523 * @seen_ld_abs: bool whether we've seen ld_abs/ind
525 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
526 * style extended BPF (eBPF).
527 * Conversion workflow:
529 * 1) First pass for calculating the new program length:
530 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
532 * 2) 2nd pass to remap in two passes: 1st pass finds new
533 * jump offsets, 2nd pass remapping:
534 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
536 static int bpf_convert_filter(struct sock_filter
*prog
, int len
,
537 struct bpf_prog
*new_prog
, int *new_len
,
540 int new_flen
= 0, pass
= 0, target
, i
, stack_off
;
541 struct bpf_insn
*new_insn
, *first_insn
= NULL
;
542 struct sock_filter
*fp
;
546 BUILD_BUG_ON(BPF_MEMWORDS
* sizeof(u32
) > MAX_BPF_STACK
);
547 BUILD_BUG_ON(BPF_REG_FP
+ 1 != MAX_BPF_REG
);
549 if (len
<= 0 || len
> BPF_MAXINSNS
)
553 first_insn
= new_prog
->insnsi
;
554 addrs
= kcalloc(len
, sizeof(*addrs
),
555 GFP_KERNEL
| __GFP_NOWARN
);
561 new_insn
= first_insn
;
564 /* Classic BPF related prologue emission. */
566 /* Classic BPF expects A and X to be reset first. These need
567 * to be guaranteed to be the first two instructions.
569 *new_insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_A
, BPF_REG_A
);
570 *new_insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_X
, BPF_REG_X
);
572 /* All programs must keep CTX in callee saved BPF_REG_CTX.
573 * In eBPF case it's done by the compiler, here we need to
574 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
576 *new_insn
++ = BPF_MOV64_REG(BPF_REG_CTX
, BPF_REG_ARG1
);
578 /* For packet access in classic BPF, cache skb->data
579 * in callee-saved BPF R8 and skb->len - skb->data_len
580 * (headlen) in BPF R9. Since classic BPF is read-only
581 * on CTX, we only need to cache it once.
583 *new_insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, data
),
584 BPF_REG_D
, BPF_REG_CTX
,
585 offsetof(struct sk_buff
, data
));
586 *new_insn
++ = BPF_LDX_MEM(BPF_W
, BPF_REG_H
, BPF_REG_CTX
,
587 offsetof(struct sk_buff
, len
));
588 *new_insn
++ = BPF_LDX_MEM(BPF_W
, BPF_REG_TMP
, BPF_REG_CTX
,
589 offsetof(struct sk_buff
, data_len
));
590 *new_insn
++ = BPF_ALU32_REG(BPF_SUB
, BPF_REG_H
, BPF_REG_TMP
);
596 for (i
= 0; i
< len
; fp
++, i
++) {
597 struct bpf_insn tmp_insns
[32] = { };
598 struct bpf_insn
*insn
= tmp_insns
;
601 addrs
[i
] = new_insn
- first_insn
;
604 /* All arithmetic insns and skb loads map as-is. */
605 case BPF_ALU
| BPF_ADD
| BPF_X
:
606 case BPF_ALU
| BPF_ADD
| BPF_K
:
607 case BPF_ALU
| BPF_SUB
| BPF_X
:
608 case BPF_ALU
| BPF_SUB
| BPF_K
:
609 case BPF_ALU
| BPF_AND
| BPF_X
:
610 case BPF_ALU
| BPF_AND
| BPF_K
:
611 case BPF_ALU
| BPF_OR
| BPF_X
:
612 case BPF_ALU
| BPF_OR
| BPF_K
:
613 case BPF_ALU
| BPF_LSH
| BPF_X
:
614 case BPF_ALU
| BPF_LSH
| BPF_K
:
615 case BPF_ALU
| BPF_RSH
| BPF_X
:
616 case BPF_ALU
| BPF_RSH
| BPF_K
:
617 case BPF_ALU
| BPF_XOR
| BPF_X
:
618 case BPF_ALU
| BPF_XOR
| BPF_K
:
619 case BPF_ALU
| BPF_MUL
| BPF_X
:
620 case BPF_ALU
| BPF_MUL
| BPF_K
:
621 case BPF_ALU
| BPF_DIV
| BPF_X
:
622 case BPF_ALU
| BPF_DIV
| BPF_K
:
623 case BPF_ALU
| BPF_MOD
| BPF_X
:
624 case BPF_ALU
| BPF_MOD
| BPF_K
:
625 case BPF_ALU
| BPF_NEG
:
626 case BPF_LD
| BPF_ABS
| BPF_W
:
627 case BPF_LD
| BPF_ABS
| BPF_H
:
628 case BPF_LD
| BPF_ABS
| BPF_B
:
629 case BPF_LD
| BPF_IND
| BPF_W
:
630 case BPF_LD
| BPF_IND
| BPF_H
:
631 case BPF_LD
| BPF_IND
| BPF_B
:
632 /* Check for overloaded BPF extension and
633 * directly convert it if found, otherwise
634 * just move on with mapping.
636 if (BPF_CLASS(fp
->code
) == BPF_LD
&&
637 BPF_MODE(fp
->code
) == BPF_ABS
&&
638 convert_bpf_extensions(fp
, &insn
))
640 if (BPF_CLASS(fp
->code
) == BPF_LD
&&
641 convert_bpf_ld_abs(fp
, &insn
)) {
646 if (fp
->code
== (BPF_ALU
| BPF_DIV
| BPF_X
) ||
647 fp
->code
== (BPF_ALU
| BPF_MOD
| BPF_X
)) {
648 *insn
++ = BPF_MOV32_REG(BPF_REG_X
, BPF_REG_X
);
649 /* Error with exception code on div/mod by 0.
650 * For cBPF programs, this was always return 0.
652 *insn
++ = BPF_JMP_IMM(BPF_JNE
, BPF_REG_X
, 0, 2);
653 *insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_A
, BPF_REG_A
);
654 *insn
++ = BPF_EXIT_INSN();
657 *insn
= BPF_RAW_INSN(fp
->code
, BPF_REG_A
, BPF_REG_X
, 0, fp
->k
);
660 /* Jump transformation cannot use BPF block macros
661 * everywhere as offset calculation and target updates
662 * require a bit more work than the rest, i.e. jump
663 * opcodes map as-is, but offsets need adjustment.
666 #define BPF_EMIT_JMP \
668 const s32 off_min = S16_MIN, off_max = S16_MAX; \
671 if (target >= len || target < 0) \
673 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
674 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
675 off -= insn - tmp_insns; \
676 /* Reject anything not fitting into insn->off. */ \
677 if (off < off_min || off > off_max) \
682 case BPF_JMP
| BPF_JA
:
683 target
= i
+ fp
->k
+ 1;
684 insn
->code
= fp
->code
;
688 case BPF_JMP
| BPF_JEQ
| BPF_K
:
689 case BPF_JMP
| BPF_JEQ
| BPF_X
:
690 case BPF_JMP
| BPF_JSET
| BPF_K
:
691 case BPF_JMP
| BPF_JSET
| BPF_X
:
692 case BPF_JMP
| BPF_JGT
| BPF_K
:
693 case BPF_JMP
| BPF_JGT
| BPF_X
:
694 case BPF_JMP
| BPF_JGE
| BPF_K
:
695 case BPF_JMP
| BPF_JGE
| BPF_X
:
696 if (BPF_SRC(fp
->code
) == BPF_K
&& (int) fp
->k
< 0) {
697 /* BPF immediates are signed, zero extend
698 * immediate into tmp register and use it
701 *insn
++ = BPF_MOV32_IMM(BPF_REG_TMP
, fp
->k
);
703 insn
->dst_reg
= BPF_REG_A
;
704 insn
->src_reg
= BPF_REG_TMP
;
707 insn
->dst_reg
= BPF_REG_A
;
709 bpf_src
= BPF_SRC(fp
->code
);
710 insn
->src_reg
= bpf_src
== BPF_X
? BPF_REG_X
: 0;
713 /* Common case where 'jump_false' is next insn. */
715 insn
->code
= BPF_JMP
| BPF_OP(fp
->code
) | bpf_src
;
716 target
= i
+ fp
->jt
+ 1;
721 /* Convert some jumps when 'jump_true' is next insn. */
723 switch (BPF_OP(fp
->code
)) {
725 insn
->code
= BPF_JMP
| BPF_JNE
| bpf_src
;
728 insn
->code
= BPF_JMP
| BPF_JLE
| bpf_src
;
731 insn
->code
= BPF_JMP
| BPF_JLT
| bpf_src
;
737 target
= i
+ fp
->jf
+ 1;
742 /* Other jumps are mapped into two insns: Jxx and JA. */
743 target
= i
+ fp
->jt
+ 1;
744 insn
->code
= BPF_JMP
| BPF_OP(fp
->code
) | bpf_src
;
748 insn
->code
= BPF_JMP
| BPF_JA
;
749 target
= i
+ fp
->jf
+ 1;
753 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
754 case BPF_LDX
| BPF_MSH
| BPF_B
: {
755 struct sock_filter tmp
= {
756 .code
= BPF_LD
| BPF_ABS
| BPF_B
,
763 *insn
++ = BPF_MOV64_REG(BPF_REG_X
, BPF_REG_A
);
764 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
765 convert_bpf_ld_abs(&tmp
, &insn
);
768 *insn
++ = BPF_ALU32_IMM(BPF_AND
, BPF_REG_A
, 0xf);
770 *insn
++ = BPF_ALU32_IMM(BPF_LSH
, BPF_REG_A
, 2);
772 *insn
++ = BPF_MOV64_REG(BPF_REG_TMP
, BPF_REG_X
);
774 *insn
++ = BPF_MOV64_REG(BPF_REG_X
, BPF_REG_A
);
776 *insn
= BPF_MOV64_REG(BPF_REG_A
, BPF_REG_TMP
);
779 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
780 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
782 case BPF_RET
| BPF_A
:
783 case BPF_RET
| BPF_K
:
784 if (BPF_RVAL(fp
->code
) == BPF_K
)
785 *insn
++ = BPF_MOV32_RAW(BPF_K
, BPF_REG_0
,
787 *insn
= BPF_EXIT_INSN();
790 /* Store to stack. */
793 stack_off
= fp
->k
* 4 + 4;
794 *insn
= BPF_STX_MEM(BPF_W
, BPF_REG_FP
, BPF_CLASS(fp
->code
) ==
795 BPF_ST
? BPF_REG_A
: BPF_REG_X
,
797 /* check_load_and_stores() verifies that classic BPF can
798 * load from stack only after write, so tracking
799 * stack_depth for ST|STX insns is enough
801 if (new_prog
&& new_prog
->aux
->stack_depth
< stack_off
)
802 new_prog
->aux
->stack_depth
= stack_off
;
805 /* Load from stack. */
806 case BPF_LD
| BPF_MEM
:
807 case BPF_LDX
| BPF_MEM
:
808 stack_off
= fp
->k
* 4 + 4;
809 *insn
= BPF_LDX_MEM(BPF_W
, BPF_CLASS(fp
->code
) == BPF_LD
?
810 BPF_REG_A
: BPF_REG_X
, BPF_REG_FP
,
815 case BPF_LD
| BPF_IMM
:
816 case BPF_LDX
| BPF_IMM
:
817 *insn
= BPF_MOV32_IMM(BPF_CLASS(fp
->code
) == BPF_LD
?
818 BPF_REG_A
: BPF_REG_X
, fp
->k
);
822 case BPF_MISC
| BPF_TAX
:
823 *insn
= BPF_MOV64_REG(BPF_REG_X
, BPF_REG_A
);
827 case BPF_MISC
| BPF_TXA
:
828 *insn
= BPF_MOV64_REG(BPF_REG_A
, BPF_REG_X
);
831 /* A = skb->len or X = skb->len */
832 case BPF_LD
| BPF_W
| BPF_LEN
:
833 case BPF_LDX
| BPF_W
| BPF_LEN
:
834 *insn
= BPF_LDX_MEM(BPF_W
, BPF_CLASS(fp
->code
) == BPF_LD
?
835 BPF_REG_A
: BPF_REG_X
, BPF_REG_CTX
,
836 offsetof(struct sk_buff
, len
));
839 /* Access seccomp_data fields. */
840 case BPF_LDX
| BPF_ABS
| BPF_W
:
841 /* A = *(u32 *) (ctx + K) */
842 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_CTX
, fp
->k
);
845 /* Unknown instruction. */
852 memcpy(new_insn
, tmp_insns
,
853 sizeof(*insn
) * (insn
- tmp_insns
));
854 new_insn
+= insn
- tmp_insns
;
858 /* Only calculating new length. */
859 *new_len
= new_insn
- first_insn
;
861 *new_len
+= 4; /* Prologue bits. */
866 if (new_flen
!= new_insn
- first_insn
) {
867 new_flen
= new_insn
- first_insn
;
874 BUG_ON(*new_len
!= new_flen
);
883 * As we dont want to clear mem[] array for each packet going through
884 * __bpf_prog_run(), we check that filter loaded by user never try to read
885 * a cell if not previously written, and we check all branches to be sure
886 * a malicious user doesn't try to abuse us.
888 static int check_load_and_stores(const struct sock_filter
*filter
, int flen
)
890 u16
*masks
, memvalid
= 0; /* One bit per cell, 16 cells */
893 BUILD_BUG_ON(BPF_MEMWORDS
> 16);
895 masks
= kmalloc_array(flen
, sizeof(*masks
), GFP_KERNEL
);
899 memset(masks
, 0xff, flen
* sizeof(*masks
));
901 for (pc
= 0; pc
< flen
; pc
++) {
902 memvalid
&= masks
[pc
];
904 switch (filter
[pc
].code
) {
907 memvalid
|= (1 << filter
[pc
].k
);
909 case BPF_LD
| BPF_MEM
:
910 case BPF_LDX
| BPF_MEM
:
911 if (!(memvalid
& (1 << filter
[pc
].k
))) {
916 case BPF_JMP
| BPF_JA
:
917 /* A jump must set masks on target */
918 masks
[pc
+ 1 + filter
[pc
].k
] &= memvalid
;
921 case BPF_JMP
| BPF_JEQ
| BPF_K
:
922 case BPF_JMP
| BPF_JEQ
| BPF_X
:
923 case BPF_JMP
| BPF_JGE
| BPF_K
:
924 case BPF_JMP
| BPF_JGE
| BPF_X
:
925 case BPF_JMP
| BPF_JGT
| BPF_K
:
926 case BPF_JMP
| BPF_JGT
| BPF_X
:
927 case BPF_JMP
| BPF_JSET
| BPF_K
:
928 case BPF_JMP
| BPF_JSET
| BPF_X
:
929 /* A jump must set masks on targets */
930 masks
[pc
+ 1 + filter
[pc
].jt
] &= memvalid
;
931 masks
[pc
+ 1 + filter
[pc
].jf
] &= memvalid
;
941 static bool chk_code_allowed(u16 code_to_probe
)
943 static const bool codes
[] = {
944 /* 32 bit ALU operations */
945 [BPF_ALU
| BPF_ADD
| BPF_K
] = true,
946 [BPF_ALU
| BPF_ADD
| BPF_X
] = true,
947 [BPF_ALU
| BPF_SUB
| BPF_K
] = true,
948 [BPF_ALU
| BPF_SUB
| BPF_X
] = true,
949 [BPF_ALU
| BPF_MUL
| BPF_K
] = true,
950 [BPF_ALU
| BPF_MUL
| BPF_X
] = true,
951 [BPF_ALU
| BPF_DIV
| BPF_K
] = true,
952 [BPF_ALU
| BPF_DIV
| BPF_X
] = true,
953 [BPF_ALU
| BPF_MOD
| BPF_K
] = true,
954 [BPF_ALU
| BPF_MOD
| BPF_X
] = true,
955 [BPF_ALU
| BPF_AND
| BPF_K
] = true,
956 [BPF_ALU
| BPF_AND
| BPF_X
] = true,
957 [BPF_ALU
| BPF_OR
| BPF_K
] = true,
958 [BPF_ALU
| BPF_OR
| BPF_X
] = true,
959 [BPF_ALU
| BPF_XOR
| BPF_K
] = true,
960 [BPF_ALU
| BPF_XOR
| BPF_X
] = true,
961 [BPF_ALU
| BPF_LSH
| BPF_K
] = true,
962 [BPF_ALU
| BPF_LSH
| BPF_X
] = true,
963 [BPF_ALU
| BPF_RSH
| BPF_K
] = true,
964 [BPF_ALU
| BPF_RSH
| BPF_X
] = true,
965 [BPF_ALU
| BPF_NEG
] = true,
966 /* Load instructions */
967 [BPF_LD
| BPF_W
| BPF_ABS
] = true,
968 [BPF_LD
| BPF_H
| BPF_ABS
] = true,
969 [BPF_LD
| BPF_B
| BPF_ABS
] = true,
970 [BPF_LD
| BPF_W
| BPF_LEN
] = true,
971 [BPF_LD
| BPF_W
| BPF_IND
] = true,
972 [BPF_LD
| BPF_H
| BPF_IND
] = true,
973 [BPF_LD
| BPF_B
| BPF_IND
] = true,
974 [BPF_LD
| BPF_IMM
] = true,
975 [BPF_LD
| BPF_MEM
] = true,
976 [BPF_LDX
| BPF_W
| BPF_LEN
] = true,
977 [BPF_LDX
| BPF_B
| BPF_MSH
] = true,
978 [BPF_LDX
| BPF_IMM
] = true,
979 [BPF_LDX
| BPF_MEM
] = true,
980 /* Store instructions */
983 /* Misc instructions */
984 [BPF_MISC
| BPF_TAX
] = true,
985 [BPF_MISC
| BPF_TXA
] = true,
986 /* Return instructions */
987 [BPF_RET
| BPF_K
] = true,
988 [BPF_RET
| BPF_A
] = true,
989 /* Jump instructions */
990 [BPF_JMP
| BPF_JA
] = true,
991 [BPF_JMP
| BPF_JEQ
| BPF_K
] = true,
992 [BPF_JMP
| BPF_JEQ
| BPF_X
] = true,
993 [BPF_JMP
| BPF_JGE
| BPF_K
] = true,
994 [BPF_JMP
| BPF_JGE
| BPF_X
] = true,
995 [BPF_JMP
| BPF_JGT
| BPF_K
] = true,
996 [BPF_JMP
| BPF_JGT
| BPF_X
] = true,
997 [BPF_JMP
| BPF_JSET
| BPF_K
] = true,
998 [BPF_JMP
| BPF_JSET
| BPF_X
] = true,
1001 if (code_to_probe
>= ARRAY_SIZE(codes
))
1004 return codes
[code_to_probe
];
1007 static bool bpf_check_basics_ok(const struct sock_filter
*filter
,
1012 if (flen
== 0 || flen
> BPF_MAXINSNS
)
1019 * bpf_check_classic - verify socket filter code
1020 * @filter: filter to verify
1021 * @flen: length of filter
1023 * Check the user's filter code. If we let some ugly
1024 * filter code slip through kaboom! The filter must contain
1025 * no references or jumps that are out of range, no illegal
1026 * instructions, and must end with a RET instruction.
1028 * All jumps are forward as they are not signed.
1030 * Returns 0 if the rule set is legal or -EINVAL if not.
1032 static int bpf_check_classic(const struct sock_filter
*filter
,
1038 /* Check the filter code now */
1039 for (pc
= 0; pc
< flen
; pc
++) {
1040 const struct sock_filter
*ftest
= &filter
[pc
];
1042 /* May we actually operate on this code? */
1043 if (!chk_code_allowed(ftest
->code
))
1046 /* Some instructions need special checks */
1047 switch (ftest
->code
) {
1048 case BPF_ALU
| BPF_DIV
| BPF_K
:
1049 case BPF_ALU
| BPF_MOD
| BPF_K
:
1050 /* Check for division by zero */
1054 case BPF_ALU
| BPF_LSH
| BPF_K
:
1055 case BPF_ALU
| BPF_RSH
| BPF_K
:
1059 case BPF_LD
| BPF_MEM
:
1060 case BPF_LDX
| BPF_MEM
:
1063 /* Check for invalid memory addresses */
1064 if (ftest
->k
>= BPF_MEMWORDS
)
1067 case BPF_JMP
| BPF_JA
:
1068 /* Note, the large ftest->k might cause loops.
1069 * Compare this with conditional jumps below,
1070 * where offsets are limited. --ANK (981016)
1072 if (ftest
->k
>= (unsigned int)(flen
- pc
- 1))
1075 case BPF_JMP
| BPF_JEQ
| BPF_K
:
1076 case BPF_JMP
| BPF_JEQ
| BPF_X
:
1077 case BPF_JMP
| BPF_JGE
| BPF_K
:
1078 case BPF_JMP
| BPF_JGE
| BPF_X
:
1079 case BPF_JMP
| BPF_JGT
| BPF_K
:
1080 case BPF_JMP
| BPF_JGT
| BPF_X
:
1081 case BPF_JMP
| BPF_JSET
| BPF_K
:
1082 case BPF_JMP
| BPF_JSET
| BPF_X
:
1083 /* Both conditionals must be safe */
1084 if (pc
+ ftest
->jt
+ 1 >= flen
||
1085 pc
+ ftest
->jf
+ 1 >= flen
)
1088 case BPF_LD
| BPF_W
| BPF_ABS
:
1089 case BPF_LD
| BPF_H
| BPF_ABS
:
1090 case BPF_LD
| BPF_B
| BPF_ABS
:
1092 if (bpf_anc_helper(ftest
) & BPF_ANC
)
1094 /* Ancillary operation unknown or unsupported */
1095 if (anc_found
== false && ftest
->k
>= SKF_AD_OFF
)
1100 /* Last instruction must be a RET code */
1101 switch (filter
[flen
- 1].code
) {
1102 case BPF_RET
| BPF_K
:
1103 case BPF_RET
| BPF_A
:
1104 return check_load_and_stores(filter
, flen
);
1110 static int bpf_prog_store_orig_filter(struct bpf_prog
*fp
,
1111 const struct sock_fprog
*fprog
)
1113 unsigned int fsize
= bpf_classic_proglen(fprog
);
1114 struct sock_fprog_kern
*fkprog
;
1116 fp
->orig_prog
= kmalloc(sizeof(*fkprog
), GFP_KERNEL
);
1120 fkprog
= fp
->orig_prog
;
1121 fkprog
->len
= fprog
->len
;
1123 fkprog
->filter
= kmemdup(fp
->insns
, fsize
,
1124 GFP_KERNEL
| __GFP_NOWARN
);
1125 if (!fkprog
->filter
) {
1126 kfree(fp
->orig_prog
);
1133 static void bpf_release_orig_filter(struct bpf_prog
*fp
)
1135 struct sock_fprog_kern
*fprog
= fp
->orig_prog
;
1138 kfree(fprog
->filter
);
1143 static void __bpf_prog_release(struct bpf_prog
*prog
)
1145 if (prog
->type
== BPF_PROG_TYPE_SOCKET_FILTER
) {
1148 bpf_release_orig_filter(prog
);
1149 bpf_prog_free(prog
);
1153 static void __sk_filter_release(struct sk_filter
*fp
)
1155 __bpf_prog_release(fp
->prog
);
1160 * sk_filter_release_rcu - Release a socket filter by rcu_head
1161 * @rcu: rcu_head that contains the sk_filter to free
1163 static void sk_filter_release_rcu(struct rcu_head
*rcu
)
1165 struct sk_filter
*fp
= container_of(rcu
, struct sk_filter
, rcu
);
1167 __sk_filter_release(fp
);
1171 * sk_filter_release - release a socket filter
1172 * @fp: filter to remove
1174 * Remove a filter from a socket and release its resources.
1176 static void sk_filter_release(struct sk_filter
*fp
)
1178 if (refcount_dec_and_test(&fp
->refcnt
))
1179 call_rcu(&fp
->rcu
, sk_filter_release_rcu
);
1182 void sk_filter_uncharge(struct sock
*sk
, struct sk_filter
*fp
)
1184 u32 filter_size
= bpf_prog_size(fp
->prog
->len
);
1186 atomic_sub(filter_size
, &sk
->sk_omem_alloc
);
1187 sk_filter_release(fp
);
1190 /* try to charge the socket memory if there is space available
1191 * return true on success
1193 static bool __sk_filter_charge(struct sock
*sk
, struct sk_filter
*fp
)
1195 u32 filter_size
= bpf_prog_size(fp
->prog
->len
);
1197 /* same check as in sock_kmalloc() */
1198 if (filter_size
<= sysctl_optmem_max
&&
1199 atomic_read(&sk
->sk_omem_alloc
) + filter_size
< sysctl_optmem_max
) {
1200 atomic_add(filter_size
, &sk
->sk_omem_alloc
);
1206 bool sk_filter_charge(struct sock
*sk
, struct sk_filter
*fp
)
1208 if (!refcount_inc_not_zero(&fp
->refcnt
))
1211 if (!__sk_filter_charge(sk
, fp
)) {
1212 sk_filter_release(fp
);
1218 static struct bpf_prog
*bpf_migrate_filter(struct bpf_prog
*fp
)
1220 struct sock_filter
*old_prog
;
1221 struct bpf_prog
*old_fp
;
1222 int err
, new_len
, old_len
= fp
->len
;
1223 bool seen_ld_abs
= false;
1225 /* We are free to overwrite insns et al right here as it
1226 * won't be used at this point in time anymore internally
1227 * after the migration to the internal BPF instruction
1230 BUILD_BUG_ON(sizeof(struct sock_filter
) !=
1231 sizeof(struct bpf_insn
));
1233 /* Conversion cannot happen on overlapping memory areas,
1234 * so we need to keep the user BPF around until the 2nd
1235 * pass. At this time, the user BPF is stored in fp->insns.
1237 old_prog
= kmemdup(fp
->insns
, old_len
* sizeof(struct sock_filter
),
1238 GFP_KERNEL
| __GFP_NOWARN
);
1244 /* 1st pass: calculate the new program length. */
1245 err
= bpf_convert_filter(old_prog
, old_len
, NULL
, &new_len
,
1250 /* Expand fp for appending the new filter representation. */
1252 fp
= bpf_prog_realloc(old_fp
, bpf_prog_size(new_len
), 0);
1254 /* The old_fp is still around in case we couldn't
1255 * allocate new memory, so uncharge on that one.
1264 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1265 err
= bpf_convert_filter(old_prog
, old_len
, fp
, &new_len
,
1268 /* 2nd bpf_convert_filter() can fail only if it fails
1269 * to allocate memory, remapping must succeed. Note,
1270 * that at this time old_fp has already been released
1275 fp
= bpf_prog_select_runtime(fp
, &err
);
1285 __bpf_prog_release(fp
);
1286 return ERR_PTR(err
);
1289 static struct bpf_prog
*bpf_prepare_filter(struct bpf_prog
*fp
,
1290 bpf_aux_classic_check_t trans
)
1294 fp
->bpf_func
= NULL
;
1297 err
= bpf_check_classic(fp
->insns
, fp
->len
);
1299 __bpf_prog_release(fp
);
1300 return ERR_PTR(err
);
1303 /* There might be additional checks and transformations
1304 * needed on classic filters, f.e. in case of seccomp.
1307 err
= trans(fp
->insns
, fp
->len
);
1309 __bpf_prog_release(fp
);
1310 return ERR_PTR(err
);
1314 /* Probe if we can JIT compile the filter and if so, do
1315 * the compilation of the filter.
1317 bpf_jit_compile(fp
);
1319 /* JIT compiler couldn't process this filter, so do the
1320 * internal BPF translation for the optimized interpreter.
1323 fp
= bpf_migrate_filter(fp
);
1329 * bpf_prog_create - create an unattached filter
1330 * @pfp: the unattached filter that is created
1331 * @fprog: the filter program
1333 * Create a filter independent of any socket. We first run some
1334 * sanity checks on it to make sure it does not explode on us later.
1335 * If an error occurs or there is insufficient memory for the filter
1336 * a negative errno code is returned. On success the return is zero.
1338 int bpf_prog_create(struct bpf_prog
**pfp
, struct sock_fprog_kern
*fprog
)
1340 unsigned int fsize
= bpf_classic_proglen(fprog
);
1341 struct bpf_prog
*fp
;
1343 /* Make sure new filter is there and in the right amounts. */
1344 if (!bpf_check_basics_ok(fprog
->filter
, fprog
->len
))
1347 fp
= bpf_prog_alloc(bpf_prog_size(fprog
->len
), 0);
1351 memcpy(fp
->insns
, fprog
->filter
, fsize
);
1353 fp
->len
= fprog
->len
;
1354 /* Since unattached filters are not copied back to user
1355 * space through sk_get_filter(), we do not need to hold
1356 * a copy here, and can spare us the work.
1358 fp
->orig_prog
= NULL
;
1360 /* bpf_prepare_filter() already takes care of freeing
1361 * memory in case something goes wrong.
1363 fp
= bpf_prepare_filter(fp
, NULL
);
1370 EXPORT_SYMBOL_GPL(bpf_prog_create
);
1373 * bpf_prog_create_from_user - create an unattached filter from user buffer
1374 * @pfp: the unattached filter that is created
1375 * @fprog: the filter program
1376 * @trans: post-classic verifier transformation handler
1377 * @save_orig: save classic BPF program
1379 * This function effectively does the same as bpf_prog_create(), only
1380 * that it builds up its insns buffer from user space provided buffer.
1381 * It also allows for passing a bpf_aux_classic_check_t handler.
1383 int bpf_prog_create_from_user(struct bpf_prog
**pfp
, struct sock_fprog
*fprog
,
1384 bpf_aux_classic_check_t trans
, bool save_orig
)
1386 unsigned int fsize
= bpf_classic_proglen(fprog
);
1387 struct bpf_prog
*fp
;
1390 /* Make sure new filter is there and in the right amounts. */
1391 if (!bpf_check_basics_ok(fprog
->filter
, fprog
->len
))
1394 fp
= bpf_prog_alloc(bpf_prog_size(fprog
->len
), 0);
1398 if (copy_from_user(fp
->insns
, fprog
->filter
, fsize
)) {
1399 __bpf_prog_free(fp
);
1403 fp
->len
= fprog
->len
;
1404 fp
->orig_prog
= NULL
;
1407 err
= bpf_prog_store_orig_filter(fp
, fprog
);
1409 __bpf_prog_free(fp
);
1414 /* bpf_prepare_filter() already takes care of freeing
1415 * memory in case something goes wrong.
1417 fp
= bpf_prepare_filter(fp
, trans
);
1424 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user
);
1426 void bpf_prog_destroy(struct bpf_prog
*fp
)
1428 __bpf_prog_release(fp
);
1430 EXPORT_SYMBOL_GPL(bpf_prog_destroy
);
1432 static int __sk_attach_prog(struct bpf_prog
*prog
, struct sock
*sk
)
1434 struct sk_filter
*fp
, *old_fp
;
1436 fp
= kmalloc(sizeof(*fp
), GFP_KERNEL
);
1442 if (!__sk_filter_charge(sk
, fp
)) {
1446 refcount_set(&fp
->refcnt
, 1);
1448 old_fp
= rcu_dereference_protected(sk
->sk_filter
,
1449 lockdep_sock_is_held(sk
));
1450 rcu_assign_pointer(sk
->sk_filter
, fp
);
1453 sk_filter_uncharge(sk
, old_fp
);
1459 struct bpf_prog
*__get_filter(struct sock_fprog
*fprog
, struct sock
*sk
)
1461 unsigned int fsize
= bpf_classic_proglen(fprog
);
1462 struct bpf_prog
*prog
;
1465 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
1466 return ERR_PTR(-EPERM
);
1468 /* Make sure new filter is there and in the right amounts. */
1469 if (!bpf_check_basics_ok(fprog
->filter
, fprog
->len
))
1470 return ERR_PTR(-EINVAL
);
1472 prog
= bpf_prog_alloc(bpf_prog_size(fprog
->len
), 0);
1474 return ERR_PTR(-ENOMEM
);
1476 if (copy_from_user(prog
->insns
, fprog
->filter
, fsize
)) {
1477 __bpf_prog_free(prog
);
1478 return ERR_PTR(-EFAULT
);
1481 prog
->len
= fprog
->len
;
1483 err
= bpf_prog_store_orig_filter(prog
, fprog
);
1485 __bpf_prog_free(prog
);
1486 return ERR_PTR(-ENOMEM
);
1489 /* bpf_prepare_filter() already takes care of freeing
1490 * memory in case something goes wrong.
1492 return bpf_prepare_filter(prog
, NULL
);
1496 * sk_attach_filter - attach a socket filter
1497 * @fprog: the filter program
1498 * @sk: the socket to use
1500 * Attach the user's filter code. We first run some sanity checks on
1501 * it to make sure it does not explode on us later. If an error
1502 * occurs or there is insufficient memory for the filter a negative
1503 * errno code is returned. On success the return is zero.
1505 int sk_attach_filter(struct sock_fprog
*fprog
, struct sock
*sk
)
1507 struct bpf_prog
*prog
= __get_filter(fprog
, sk
);
1511 return PTR_ERR(prog
);
1513 err
= __sk_attach_prog(prog
, sk
);
1515 __bpf_prog_release(prog
);
1521 EXPORT_SYMBOL_GPL(sk_attach_filter
);
1523 int sk_reuseport_attach_filter(struct sock_fprog
*fprog
, struct sock
*sk
)
1525 struct bpf_prog
*prog
= __get_filter(fprog
, sk
);
1529 return PTR_ERR(prog
);
1531 if (bpf_prog_size(prog
->len
) > sysctl_optmem_max
)
1534 err
= reuseport_attach_prog(sk
, prog
);
1537 __bpf_prog_release(prog
);
1542 static struct bpf_prog
*__get_bpf(u32 ufd
, struct sock
*sk
)
1544 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
1545 return ERR_PTR(-EPERM
);
1547 return bpf_prog_get_type(ufd
, BPF_PROG_TYPE_SOCKET_FILTER
);
1550 int sk_attach_bpf(u32 ufd
, struct sock
*sk
)
1552 struct bpf_prog
*prog
= __get_bpf(ufd
, sk
);
1556 return PTR_ERR(prog
);
1558 err
= __sk_attach_prog(prog
, sk
);
1567 int sk_reuseport_attach_bpf(u32 ufd
, struct sock
*sk
)
1569 struct bpf_prog
*prog
;
1572 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
1575 prog
= bpf_prog_get_type(ufd
, BPF_PROG_TYPE_SOCKET_FILTER
);
1576 if (IS_ERR(prog
) && PTR_ERR(prog
) == -EINVAL
)
1577 prog
= bpf_prog_get_type(ufd
, BPF_PROG_TYPE_SK_REUSEPORT
);
1579 return PTR_ERR(prog
);
1581 if (prog
->type
== BPF_PROG_TYPE_SK_REUSEPORT
) {
1582 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1583 * bpf prog (e.g. sockmap). It depends on the
1584 * limitation imposed by bpf_prog_load().
1585 * Hence, sysctl_optmem_max is not checked.
1587 if ((sk
->sk_type
!= SOCK_STREAM
&&
1588 sk
->sk_type
!= SOCK_DGRAM
) ||
1589 (sk
->sk_protocol
!= IPPROTO_UDP
&&
1590 sk
->sk_protocol
!= IPPROTO_TCP
) ||
1591 (sk
->sk_family
!= AF_INET
&&
1592 sk
->sk_family
!= AF_INET6
)) {
1597 /* BPF_PROG_TYPE_SOCKET_FILTER */
1598 if (bpf_prog_size(prog
->len
) > sysctl_optmem_max
) {
1604 err
= reuseport_attach_prog(sk
, prog
);
1612 void sk_reuseport_prog_free(struct bpf_prog
*prog
)
1617 if (prog
->type
== BPF_PROG_TYPE_SK_REUSEPORT
)
1620 bpf_prog_destroy(prog
);
1623 struct bpf_scratchpad
{
1625 __be32 diff
[MAX_BPF_STACK
/ sizeof(__be32
)];
1626 u8 buff
[MAX_BPF_STACK
];
1630 static DEFINE_PER_CPU(struct bpf_scratchpad
, bpf_sp
);
1632 static inline int __bpf_try_make_writable(struct sk_buff
*skb
,
1633 unsigned int write_len
)
1635 return skb_ensure_writable(skb
, write_len
);
1638 static inline int bpf_try_make_writable(struct sk_buff
*skb
,
1639 unsigned int write_len
)
1641 int err
= __bpf_try_make_writable(skb
, write_len
);
1643 bpf_compute_data_pointers(skb
);
1647 static int bpf_try_make_head_writable(struct sk_buff
*skb
)
1649 return bpf_try_make_writable(skb
, skb_headlen(skb
));
1652 static inline void bpf_push_mac_rcsum(struct sk_buff
*skb
)
1654 if (skb_at_tc_ingress(skb
))
1655 skb_postpush_rcsum(skb
, skb_mac_header(skb
), skb
->mac_len
);
1658 static inline void bpf_pull_mac_rcsum(struct sk_buff
*skb
)
1660 if (skb_at_tc_ingress(skb
))
1661 skb_postpull_rcsum(skb
, skb_mac_header(skb
), skb
->mac_len
);
1664 BPF_CALL_5(bpf_skb_store_bytes
, struct sk_buff
*, skb
, u32
, offset
,
1665 const void *, from
, u32
, len
, u64
, flags
)
1669 if (unlikely(flags
& ~(BPF_F_RECOMPUTE_CSUM
| BPF_F_INVALIDATE_HASH
)))
1671 if (unlikely(offset
> 0xffff))
1673 if (unlikely(bpf_try_make_writable(skb
, offset
+ len
)))
1676 ptr
= skb
->data
+ offset
;
1677 if (flags
& BPF_F_RECOMPUTE_CSUM
)
1678 __skb_postpull_rcsum(skb
, ptr
, len
, offset
);
1680 memcpy(ptr
, from
, len
);
1682 if (flags
& BPF_F_RECOMPUTE_CSUM
)
1683 __skb_postpush_rcsum(skb
, ptr
, len
, offset
);
1684 if (flags
& BPF_F_INVALIDATE_HASH
)
1685 skb_clear_hash(skb
);
1690 static const struct bpf_func_proto bpf_skb_store_bytes_proto
= {
1691 .func
= bpf_skb_store_bytes
,
1693 .ret_type
= RET_INTEGER
,
1694 .arg1_type
= ARG_PTR_TO_CTX
,
1695 .arg2_type
= ARG_ANYTHING
,
1696 .arg3_type
= ARG_PTR_TO_MEM
,
1697 .arg4_type
= ARG_CONST_SIZE
,
1698 .arg5_type
= ARG_ANYTHING
,
1701 BPF_CALL_4(bpf_skb_load_bytes
, const struct sk_buff
*, skb
, u32
, offset
,
1702 void *, to
, u32
, len
)
1706 if (unlikely(offset
> 0xffff))
1709 ptr
= skb_header_pointer(skb
, offset
, len
, to
);
1713 memcpy(to
, ptr
, len
);
1721 static const struct bpf_func_proto bpf_skb_load_bytes_proto
= {
1722 .func
= bpf_skb_load_bytes
,
1724 .ret_type
= RET_INTEGER
,
1725 .arg1_type
= ARG_PTR_TO_CTX
,
1726 .arg2_type
= ARG_ANYTHING
,
1727 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
1728 .arg4_type
= ARG_CONST_SIZE
,
1731 BPF_CALL_4(bpf_flow_dissector_load_bytes
,
1732 const struct bpf_flow_dissector
*, ctx
, u32
, offset
,
1733 void *, to
, u32
, len
)
1737 if (unlikely(offset
> 0xffff))
1740 if (unlikely(!ctx
->skb
))
1743 ptr
= skb_header_pointer(ctx
->skb
, offset
, len
, to
);
1747 memcpy(to
, ptr
, len
);
1755 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto
= {
1756 .func
= bpf_flow_dissector_load_bytes
,
1758 .ret_type
= RET_INTEGER
,
1759 .arg1_type
= ARG_PTR_TO_CTX
,
1760 .arg2_type
= ARG_ANYTHING
,
1761 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
1762 .arg4_type
= ARG_CONST_SIZE
,
1765 BPF_CALL_5(bpf_skb_load_bytes_relative
, const struct sk_buff
*, skb
,
1766 u32
, offset
, void *, to
, u32
, len
, u32
, start_header
)
1768 u8
*end
= skb_tail_pointer(skb
);
1769 u8
*net
= skb_network_header(skb
);
1770 u8
*mac
= skb_mac_header(skb
);
1773 if (unlikely(offset
> 0xffff || len
> (end
- mac
)))
1776 switch (start_header
) {
1777 case BPF_HDR_START_MAC
:
1780 case BPF_HDR_START_NET
:
1787 if (likely(ptr
>= mac
&& ptr
+ len
<= end
)) {
1788 memcpy(to
, ptr
, len
);
1797 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto
= {
1798 .func
= bpf_skb_load_bytes_relative
,
1800 .ret_type
= RET_INTEGER
,
1801 .arg1_type
= ARG_PTR_TO_CTX
,
1802 .arg2_type
= ARG_ANYTHING
,
1803 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
1804 .arg4_type
= ARG_CONST_SIZE
,
1805 .arg5_type
= ARG_ANYTHING
,
1808 BPF_CALL_2(bpf_skb_pull_data
, struct sk_buff
*, skb
, u32
, len
)
1810 /* Idea is the following: should the needed direct read/write
1811 * test fail during runtime, we can pull in more data and redo
1812 * again, since implicitly, we invalidate previous checks here.
1814 * Or, since we know how much we need to make read/writeable,
1815 * this can be done once at the program beginning for direct
1816 * access case. By this we overcome limitations of only current
1817 * headroom being accessible.
1819 return bpf_try_make_writable(skb
, len
? : skb_headlen(skb
));
1822 static const struct bpf_func_proto bpf_skb_pull_data_proto
= {
1823 .func
= bpf_skb_pull_data
,
1825 .ret_type
= RET_INTEGER
,
1826 .arg1_type
= ARG_PTR_TO_CTX
,
1827 .arg2_type
= ARG_ANYTHING
,
1830 BPF_CALL_1(bpf_sk_fullsock
, struct sock
*, sk
)
1832 return sk_fullsock(sk
) ? (unsigned long)sk
: (unsigned long)NULL
;
1835 static const struct bpf_func_proto bpf_sk_fullsock_proto
= {
1836 .func
= bpf_sk_fullsock
,
1838 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
1839 .arg1_type
= ARG_PTR_TO_SOCK_COMMON
,
1842 static inline int sk_skb_try_make_writable(struct sk_buff
*skb
,
1843 unsigned int write_len
)
1845 int err
= __bpf_try_make_writable(skb
, write_len
);
1847 bpf_compute_data_end_sk_skb(skb
);
1851 BPF_CALL_2(sk_skb_pull_data
, struct sk_buff
*, skb
, u32
, len
)
1853 /* Idea is the following: should the needed direct read/write
1854 * test fail during runtime, we can pull in more data and redo
1855 * again, since implicitly, we invalidate previous checks here.
1857 * Or, since we know how much we need to make read/writeable,
1858 * this can be done once at the program beginning for direct
1859 * access case. By this we overcome limitations of only current
1860 * headroom being accessible.
1862 return sk_skb_try_make_writable(skb
, len
? : skb_headlen(skb
));
1865 static const struct bpf_func_proto sk_skb_pull_data_proto
= {
1866 .func
= sk_skb_pull_data
,
1868 .ret_type
= RET_INTEGER
,
1869 .arg1_type
= ARG_PTR_TO_CTX
,
1870 .arg2_type
= ARG_ANYTHING
,
1873 BPF_CALL_5(bpf_l3_csum_replace
, struct sk_buff
*, skb
, u32
, offset
,
1874 u64
, from
, u64
, to
, u64
, flags
)
1878 if (unlikely(flags
& ~(BPF_F_HDR_FIELD_MASK
)))
1880 if (unlikely(offset
> 0xffff || offset
& 1))
1882 if (unlikely(bpf_try_make_writable(skb
, offset
+ sizeof(*ptr
))))
1885 ptr
= (__sum16
*)(skb
->data
+ offset
);
1886 switch (flags
& BPF_F_HDR_FIELD_MASK
) {
1888 if (unlikely(from
!= 0))
1891 csum_replace_by_diff(ptr
, to
);
1894 csum_replace2(ptr
, from
, to
);
1897 csum_replace4(ptr
, from
, to
);
1906 static const struct bpf_func_proto bpf_l3_csum_replace_proto
= {
1907 .func
= bpf_l3_csum_replace
,
1909 .ret_type
= RET_INTEGER
,
1910 .arg1_type
= ARG_PTR_TO_CTX
,
1911 .arg2_type
= ARG_ANYTHING
,
1912 .arg3_type
= ARG_ANYTHING
,
1913 .arg4_type
= ARG_ANYTHING
,
1914 .arg5_type
= ARG_ANYTHING
,
1917 BPF_CALL_5(bpf_l4_csum_replace
, struct sk_buff
*, skb
, u32
, offset
,
1918 u64
, from
, u64
, to
, u64
, flags
)
1920 bool is_pseudo
= flags
& BPF_F_PSEUDO_HDR
;
1921 bool is_mmzero
= flags
& BPF_F_MARK_MANGLED_0
;
1922 bool do_mforce
= flags
& BPF_F_MARK_ENFORCE
;
1925 if (unlikely(flags
& ~(BPF_F_MARK_MANGLED_0
| BPF_F_MARK_ENFORCE
|
1926 BPF_F_PSEUDO_HDR
| BPF_F_HDR_FIELD_MASK
)))
1928 if (unlikely(offset
> 0xffff || offset
& 1))
1930 if (unlikely(bpf_try_make_writable(skb
, offset
+ sizeof(*ptr
))))
1933 ptr
= (__sum16
*)(skb
->data
+ offset
);
1934 if (is_mmzero
&& !do_mforce
&& !*ptr
)
1937 switch (flags
& BPF_F_HDR_FIELD_MASK
) {
1939 if (unlikely(from
!= 0))
1942 inet_proto_csum_replace_by_diff(ptr
, skb
, to
, is_pseudo
);
1945 inet_proto_csum_replace2(ptr
, skb
, from
, to
, is_pseudo
);
1948 inet_proto_csum_replace4(ptr
, skb
, from
, to
, is_pseudo
);
1954 if (is_mmzero
&& !*ptr
)
1955 *ptr
= CSUM_MANGLED_0
;
1959 static const struct bpf_func_proto bpf_l4_csum_replace_proto
= {
1960 .func
= bpf_l4_csum_replace
,
1962 .ret_type
= RET_INTEGER
,
1963 .arg1_type
= ARG_PTR_TO_CTX
,
1964 .arg2_type
= ARG_ANYTHING
,
1965 .arg3_type
= ARG_ANYTHING
,
1966 .arg4_type
= ARG_ANYTHING
,
1967 .arg5_type
= ARG_ANYTHING
,
1970 BPF_CALL_5(bpf_csum_diff
, __be32
*, from
, u32
, from_size
,
1971 __be32
*, to
, u32
, to_size
, __wsum
, seed
)
1973 struct bpf_scratchpad
*sp
= this_cpu_ptr(&bpf_sp
);
1974 u32 diff_size
= from_size
+ to_size
;
1977 /* This is quite flexible, some examples:
1979 * from_size == 0, to_size > 0, seed := csum --> pushing data
1980 * from_size > 0, to_size == 0, seed := csum --> pulling data
1981 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1983 * Even for diffing, from_size and to_size don't need to be equal.
1985 if (unlikely(((from_size
| to_size
) & (sizeof(__be32
) - 1)) ||
1986 diff_size
> sizeof(sp
->diff
)))
1989 for (i
= 0; i
< from_size
/ sizeof(__be32
); i
++, j
++)
1990 sp
->diff
[j
] = ~from
[i
];
1991 for (i
= 0; i
< to_size
/ sizeof(__be32
); i
++, j
++)
1992 sp
->diff
[j
] = to
[i
];
1994 return csum_partial(sp
->diff
, diff_size
, seed
);
1997 static const struct bpf_func_proto bpf_csum_diff_proto
= {
1998 .func
= bpf_csum_diff
,
2001 .ret_type
= RET_INTEGER
,
2002 .arg1_type
= ARG_PTR_TO_MEM_OR_NULL
,
2003 .arg2_type
= ARG_CONST_SIZE_OR_ZERO
,
2004 .arg3_type
= ARG_PTR_TO_MEM_OR_NULL
,
2005 .arg4_type
= ARG_CONST_SIZE_OR_ZERO
,
2006 .arg5_type
= ARG_ANYTHING
,
2009 BPF_CALL_2(bpf_csum_update
, struct sk_buff
*, skb
, __wsum
, csum
)
2011 /* The interface is to be used in combination with bpf_csum_diff()
2012 * for direct packet writes. csum rotation for alignment as well
2013 * as emulating csum_sub() can be done from the eBPF program.
2015 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
2016 return (skb
->csum
= csum_add(skb
->csum
, csum
));
2021 static const struct bpf_func_proto bpf_csum_update_proto
= {
2022 .func
= bpf_csum_update
,
2024 .ret_type
= RET_INTEGER
,
2025 .arg1_type
= ARG_PTR_TO_CTX
,
2026 .arg2_type
= ARG_ANYTHING
,
2029 static inline int __bpf_rx_skb(struct net_device
*dev
, struct sk_buff
*skb
)
2031 return dev_forward_skb(dev
, skb
);
2034 static inline int __bpf_rx_skb_no_mac(struct net_device
*dev
,
2035 struct sk_buff
*skb
)
2037 int ret
= ____dev_forward_skb(dev
, skb
);
2041 ret
= netif_rx(skb
);
2047 static inline int __bpf_tx_skb(struct net_device
*dev
, struct sk_buff
*skb
)
2051 if (dev_xmit_recursion()) {
2052 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2060 dev_xmit_recursion_inc();
2061 ret
= dev_queue_xmit(skb
);
2062 dev_xmit_recursion_dec();
2067 static int __bpf_redirect_no_mac(struct sk_buff
*skb
, struct net_device
*dev
,
2070 unsigned int mlen
= skb_network_offset(skb
);
2073 __skb_pull(skb
, mlen
);
2075 /* At ingress, the mac header has already been pulled once.
2076 * At egress, skb_pospull_rcsum has to be done in case that
2077 * the skb is originated from ingress (i.e. a forwarded skb)
2078 * to ensure that rcsum starts at net header.
2080 if (!skb_at_tc_ingress(skb
))
2081 skb_postpull_rcsum(skb
, skb_mac_header(skb
), mlen
);
2083 skb_pop_mac_header(skb
);
2084 skb_reset_mac_len(skb
);
2085 return flags
& BPF_F_INGRESS
?
2086 __bpf_rx_skb_no_mac(dev
, skb
) : __bpf_tx_skb(dev
, skb
);
2089 static int __bpf_redirect_common(struct sk_buff
*skb
, struct net_device
*dev
,
2092 /* Verify that a link layer header is carried */
2093 if (unlikely(skb
->mac_header
>= skb
->network_header
)) {
2098 bpf_push_mac_rcsum(skb
);
2099 return flags
& BPF_F_INGRESS
?
2100 __bpf_rx_skb(dev
, skb
) : __bpf_tx_skb(dev
, skb
);
2103 static int __bpf_redirect(struct sk_buff
*skb
, struct net_device
*dev
,
2106 if (dev_is_mac_header_xmit(dev
))
2107 return __bpf_redirect_common(skb
, dev
, flags
);
2109 return __bpf_redirect_no_mac(skb
, dev
, flags
);
2112 BPF_CALL_3(bpf_clone_redirect
, struct sk_buff
*, skb
, u32
, ifindex
, u64
, flags
)
2114 struct net_device
*dev
;
2115 struct sk_buff
*clone
;
2118 if (unlikely(flags
& ~(BPF_F_INGRESS
)))
2121 dev
= dev_get_by_index_rcu(dev_net(skb
->dev
), ifindex
);
2125 clone
= skb_clone(skb
, GFP_ATOMIC
);
2126 if (unlikely(!clone
))
2129 /* For direct write, we need to keep the invariant that the skbs
2130 * we're dealing with need to be uncloned. Should uncloning fail
2131 * here, we need to free the just generated clone to unclone once
2134 ret
= bpf_try_make_head_writable(skb
);
2135 if (unlikely(ret
)) {
2140 return __bpf_redirect(clone
, dev
, flags
);
2143 static const struct bpf_func_proto bpf_clone_redirect_proto
= {
2144 .func
= bpf_clone_redirect
,
2146 .ret_type
= RET_INTEGER
,
2147 .arg1_type
= ARG_PTR_TO_CTX
,
2148 .arg2_type
= ARG_ANYTHING
,
2149 .arg3_type
= ARG_ANYTHING
,
2152 DEFINE_PER_CPU(struct bpf_redirect_info
, bpf_redirect_info
);
2153 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info
);
2155 BPF_CALL_2(bpf_redirect
, u32
, ifindex
, u64
, flags
)
2157 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
2159 if (unlikely(flags
& ~(BPF_F_INGRESS
)))
2163 ri
->tgt_index
= ifindex
;
2165 return TC_ACT_REDIRECT
;
2168 int skb_do_redirect(struct sk_buff
*skb
)
2170 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
2171 struct net_device
*dev
;
2173 dev
= dev_get_by_index_rcu(dev_net(skb
->dev
), ri
->tgt_index
);
2175 if (unlikely(!dev
)) {
2180 return __bpf_redirect(skb
, dev
, ri
->flags
);
2183 static const struct bpf_func_proto bpf_redirect_proto
= {
2184 .func
= bpf_redirect
,
2186 .ret_type
= RET_INTEGER
,
2187 .arg1_type
= ARG_ANYTHING
,
2188 .arg2_type
= ARG_ANYTHING
,
2191 BPF_CALL_2(bpf_msg_apply_bytes
, struct sk_msg
*, msg
, u32
, bytes
)
2193 msg
->apply_bytes
= bytes
;
2197 static const struct bpf_func_proto bpf_msg_apply_bytes_proto
= {
2198 .func
= bpf_msg_apply_bytes
,
2200 .ret_type
= RET_INTEGER
,
2201 .arg1_type
= ARG_PTR_TO_CTX
,
2202 .arg2_type
= ARG_ANYTHING
,
2205 BPF_CALL_2(bpf_msg_cork_bytes
, struct sk_msg
*, msg
, u32
, bytes
)
2207 msg
->cork_bytes
= bytes
;
2211 static const struct bpf_func_proto bpf_msg_cork_bytes_proto
= {
2212 .func
= bpf_msg_cork_bytes
,
2214 .ret_type
= RET_INTEGER
,
2215 .arg1_type
= ARG_PTR_TO_CTX
,
2216 .arg2_type
= ARG_ANYTHING
,
2219 BPF_CALL_4(bpf_msg_pull_data
, struct sk_msg
*, msg
, u32
, start
,
2220 u32
, end
, u64
, flags
)
2222 u32 len
= 0, offset
= 0, copy
= 0, poffset
= 0, bytes
= end
- start
;
2223 u32 first_sge
, last_sge
, i
, shift
, bytes_sg_total
;
2224 struct scatterlist
*sge
;
2225 u8
*raw
, *to
, *from
;
2228 if (unlikely(flags
|| end
<= start
))
2231 /* First find the starting scatterlist element */
2235 len
= sk_msg_elem(msg
, i
)->length
;
2236 if (start
< offset
+ len
)
2238 sk_msg_iter_var_next(i
);
2239 } while (i
!= msg
->sg
.end
);
2241 if (unlikely(start
>= offset
+ len
))
2245 /* The start may point into the sg element so we need to also
2246 * account for the headroom.
2248 bytes_sg_total
= start
- offset
+ bytes
;
2249 if (!msg
->sg
.copy
[i
] && bytes_sg_total
<= len
)
2252 /* At this point we need to linearize multiple scatterlist
2253 * elements or a single shared page. Either way we need to
2254 * copy into a linear buffer exclusively owned by BPF. Then
2255 * place the buffer in the scatterlist and fixup the original
2256 * entries by removing the entries now in the linear buffer
2257 * and shifting the remaining entries. For now we do not try
2258 * to copy partial entries to avoid complexity of running out
2259 * of sg_entry slots. The downside is reading a single byte
2260 * will copy the entire sg entry.
2263 copy
+= sk_msg_elem(msg
, i
)->length
;
2264 sk_msg_iter_var_next(i
);
2265 if (bytes_sg_total
<= copy
)
2267 } while (i
!= msg
->sg
.end
);
2270 if (unlikely(bytes_sg_total
> copy
))
2273 page
= alloc_pages(__GFP_NOWARN
| GFP_ATOMIC
| __GFP_COMP
,
2275 if (unlikely(!page
))
2278 raw
= page_address(page
);
2281 sge
= sk_msg_elem(msg
, i
);
2282 from
= sg_virt(sge
);
2286 memcpy(to
, from
, len
);
2289 put_page(sg_page(sge
));
2291 sk_msg_iter_var_next(i
);
2292 } while (i
!= last_sge
);
2294 sg_set_page(&msg
->sg
.data
[first_sge
], page
, copy
, 0);
2296 /* To repair sg ring we need to shift entries. If we only
2297 * had a single entry though we can just replace it and
2298 * be done. Otherwise walk the ring and shift the entries.
2300 WARN_ON_ONCE(last_sge
== first_sge
);
2301 shift
= last_sge
> first_sge
?
2302 last_sge
- first_sge
- 1 :
2303 NR_MSG_FRAG_IDS
- first_sge
+ last_sge
- 1;
2308 sk_msg_iter_var_next(i
);
2312 if (i
+ shift
>= NR_MSG_FRAG_IDS
)
2313 move_from
= i
+ shift
- NR_MSG_FRAG_IDS
;
2315 move_from
= i
+ shift
;
2316 if (move_from
== msg
->sg
.end
)
2319 msg
->sg
.data
[i
] = msg
->sg
.data
[move_from
];
2320 msg
->sg
.data
[move_from
].length
= 0;
2321 msg
->sg
.data
[move_from
].page_link
= 0;
2322 msg
->sg
.data
[move_from
].offset
= 0;
2323 sk_msg_iter_var_next(i
);
2326 msg
->sg
.end
= msg
->sg
.end
- shift
> msg
->sg
.end
?
2327 msg
->sg
.end
- shift
+ NR_MSG_FRAG_IDS
:
2328 msg
->sg
.end
- shift
;
2330 msg
->data
= sg_virt(&msg
->sg
.data
[first_sge
]) + start
- offset
;
2331 msg
->data_end
= msg
->data
+ bytes
;
2335 static const struct bpf_func_proto bpf_msg_pull_data_proto
= {
2336 .func
= bpf_msg_pull_data
,
2338 .ret_type
= RET_INTEGER
,
2339 .arg1_type
= ARG_PTR_TO_CTX
,
2340 .arg2_type
= ARG_ANYTHING
,
2341 .arg3_type
= ARG_ANYTHING
,
2342 .arg4_type
= ARG_ANYTHING
,
2345 BPF_CALL_4(bpf_msg_push_data
, struct sk_msg
*, msg
, u32
, start
,
2346 u32
, len
, u64
, flags
)
2348 struct scatterlist sge
, nsge
, nnsge
, rsge
= {0}, *psge
;
2349 u32
new, i
= 0, l
= 0, space
, copy
= 0, offset
= 0;
2350 u8
*raw
, *to
, *from
;
2353 if (unlikely(flags
))
2356 /* First find the starting scatterlist element */
2360 l
= sk_msg_elem(msg
, i
)->length
;
2362 if (start
< offset
+ l
)
2364 sk_msg_iter_var_next(i
);
2365 } while (i
!= msg
->sg
.end
);
2367 if (start
>= offset
+ l
)
2370 space
= MAX_MSG_FRAGS
- sk_msg_elem_used(msg
);
2372 /* If no space available will fallback to copy, we need at
2373 * least one scatterlist elem available to push data into
2374 * when start aligns to the beginning of an element or two
2375 * when it falls inside an element. We handle the start equals
2376 * offset case because its the common case for inserting a
2379 if (!space
|| (space
== 1 && start
!= offset
))
2380 copy
= msg
->sg
.data
[i
].length
;
2382 page
= alloc_pages(__GFP_NOWARN
| GFP_ATOMIC
| __GFP_COMP
,
2383 get_order(copy
+ len
));
2384 if (unlikely(!page
))
2390 raw
= page_address(page
);
2392 psge
= sk_msg_elem(msg
, i
);
2393 front
= start
- offset
;
2394 back
= psge
->length
- front
;
2395 from
= sg_virt(psge
);
2398 memcpy(raw
, from
, front
);
2402 to
= raw
+ front
+ len
;
2404 memcpy(to
, from
, back
);
2407 put_page(sg_page(psge
));
2408 } else if (start
- offset
) {
2409 psge
= sk_msg_elem(msg
, i
);
2410 rsge
= sk_msg_elem_cpy(msg
, i
);
2412 psge
->length
= start
- offset
;
2413 rsge
.length
-= psge
->length
;
2414 rsge
.offset
+= start
;
2416 sk_msg_iter_var_next(i
);
2417 sg_unmark_end(psge
);
2418 sk_msg_iter_next(msg
, end
);
2421 /* Slot(s) to place newly allocated data */
2424 /* Shift one or two slots as needed */
2426 sge
= sk_msg_elem_cpy(msg
, i
);
2428 sk_msg_iter_var_next(i
);
2429 sg_unmark_end(&sge
);
2430 sk_msg_iter_next(msg
, end
);
2432 nsge
= sk_msg_elem_cpy(msg
, i
);
2434 sk_msg_iter_var_next(i
);
2435 nnsge
= sk_msg_elem_cpy(msg
, i
);
2438 while (i
!= msg
->sg
.end
) {
2439 msg
->sg
.data
[i
] = sge
;
2441 sk_msg_iter_var_next(i
);
2444 nnsge
= sk_msg_elem_cpy(msg
, i
);
2446 nsge
= sk_msg_elem_cpy(msg
, i
);
2451 /* Place newly allocated data buffer */
2452 sk_mem_charge(msg
->sk
, len
);
2453 msg
->sg
.size
+= len
;
2454 msg
->sg
.copy
[new] = false;
2455 sg_set_page(&msg
->sg
.data
[new], page
, len
+ copy
, 0);
2457 get_page(sg_page(&rsge
));
2458 sk_msg_iter_var_next(new);
2459 msg
->sg
.data
[new] = rsge
;
2462 sk_msg_compute_data_pointers(msg
);
2466 static const struct bpf_func_proto bpf_msg_push_data_proto
= {
2467 .func
= bpf_msg_push_data
,
2469 .ret_type
= RET_INTEGER
,
2470 .arg1_type
= ARG_PTR_TO_CTX
,
2471 .arg2_type
= ARG_ANYTHING
,
2472 .arg3_type
= ARG_ANYTHING
,
2473 .arg4_type
= ARG_ANYTHING
,
2476 static void sk_msg_shift_left(struct sk_msg
*msg
, int i
)
2482 sk_msg_iter_var_next(i
);
2483 msg
->sg
.data
[prev
] = msg
->sg
.data
[i
];
2484 } while (i
!= msg
->sg
.end
);
2486 sk_msg_iter_prev(msg
, end
);
2489 static void sk_msg_shift_right(struct sk_msg
*msg
, int i
)
2491 struct scatterlist tmp
, sge
;
2493 sk_msg_iter_next(msg
, end
);
2494 sge
= sk_msg_elem_cpy(msg
, i
);
2495 sk_msg_iter_var_next(i
);
2496 tmp
= sk_msg_elem_cpy(msg
, i
);
2498 while (i
!= msg
->sg
.end
) {
2499 msg
->sg
.data
[i
] = sge
;
2500 sk_msg_iter_var_next(i
);
2502 tmp
= sk_msg_elem_cpy(msg
, i
);
2506 BPF_CALL_4(bpf_msg_pop_data
, struct sk_msg
*, msg
, u32
, start
,
2507 u32
, len
, u64
, flags
)
2509 u32 i
= 0, l
= 0, space
, offset
= 0;
2510 u64 last
= start
+ len
;
2513 if (unlikely(flags
))
2516 /* First find the starting scatterlist element */
2520 l
= sk_msg_elem(msg
, i
)->length
;
2522 if (start
< offset
+ l
)
2524 sk_msg_iter_var_next(i
);
2525 } while (i
!= msg
->sg
.end
);
2527 /* Bounds checks: start and pop must be inside message */
2528 if (start
>= offset
+ l
|| last
>= msg
->sg
.size
)
2531 space
= MAX_MSG_FRAGS
- sk_msg_elem_used(msg
);
2534 /* --------------| offset
2535 * -| start |-------- len -------|
2537 * |----- a ----|-------- pop -------|----- b ----|
2538 * |______________________________________________| length
2541 * a: region at front of scatter element to save
2542 * b: region at back of scatter element to save when length > A + pop
2543 * pop: region to pop from element, same as input 'pop' here will be
2544 * decremented below per iteration.
2546 * Two top-level cases to handle when start != offset, first B is non
2547 * zero and second B is zero corresponding to when a pop includes more
2550 * Then if B is non-zero AND there is no space allocate space and
2551 * compact A, B regions into page. If there is space shift ring to
2552 * the rigth free'ing the next element in ring to place B, leaving
2553 * A untouched except to reduce length.
2555 if (start
!= offset
) {
2556 struct scatterlist
*nsge
, *sge
= sk_msg_elem(msg
, i
);
2558 int b
= sge
->length
- pop
- a
;
2560 sk_msg_iter_var_next(i
);
2562 if (pop
< sge
->length
- a
) {
2565 sk_msg_shift_right(msg
, i
);
2566 nsge
= sk_msg_elem(msg
, i
);
2567 get_page(sg_page(sge
));
2570 b
, sge
->offset
+ pop
+ a
);
2572 struct page
*page
, *orig
;
2575 page
= alloc_pages(__GFP_NOWARN
|
2576 __GFP_COMP
| GFP_ATOMIC
,
2578 if (unlikely(!page
))
2582 orig
= sg_page(sge
);
2583 from
= sg_virt(sge
);
2584 to
= page_address(page
);
2585 memcpy(to
, from
, a
);
2586 memcpy(to
+ a
, from
+ a
+ pop
, b
);
2587 sg_set_page(sge
, page
, a
+ b
, 0);
2591 } else if (pop
>= sge
->length
- a
) {
2593 pop
-= (sge
->length
- a
);
2597 /* From above the current layout _must_ be as follows,
2602 * |---- pop ---|---------------- b ------------|
2603 * |____________________________________________| length
2605 * Offset and start of the current msg elem are equal because in the
2606 * previous case we handled offset != start and either consumed the
2607 * entire element and advanced to the next element OR pop == 0.
2609 * Two cases to handle here are first pop is less than the length
2610 * leaving some remainder b above. Simply adjust the element's layout
2611 * in this case. Or pop >= length of the element so that b = 0. In this
2612 * case advance to next element decrementing pop.
2615 struct scatterlist
*sge
= sk_msg_elem(msg
, i
);
2617 if (pop
< sge
->length
) {
2623 sk_msg_shift_left(msg
, i
);
2625 sk_msg_iter_var_next(i
);
2628 sk_mem_uncharge(msg
->sk
, len
- pop
);
2629 msg
->sg
.size
-= (len
- pop
);
2630 sk_msg_compute_data_pointers(msg
);
2634 static const struct bpf_func_proto bpf_msg_pop_data_proto
= {
2635 .func
= bpf_msg_pop_data
,
2637 .ret_type
= RET_INTEGER
,
2638 .arg1_type
= ARG_PTR_TO_CTX
,
2639 .arg2_type
= ARG_ANYTHING
,
2640 .arg3_type
= ARG_ANYTHING
,
2641 .arg4_type
= ARG_ANYTHING
,
2644 BPF_CALL_1(bpf_get_cgroup_classid
, const struct sk_buff
*, skb
)
2646 return task_get_classid(skb
);
2649 static const struct bpf_func_proto bpf_get_cgroup_classid_proto
= {
2650 .func
= bpf_get_cgroup_classid
,
2652 .ret_type
= RET_INTEGER
,
2653 .arg1_type
= ARG_PTR_TO_CTX
,
2656 BPF_CALL_1(bpf_get_route_realm
, const struct sk_buff
*, skb
)
2658 return dst_tclassid(skb
);
2661 static const struct bpf_func_proto bpf_get_route_realm_proto
= {
2662 .func
= bpf_get_route_realm
,
2664 .ret_type
= RET_INTEGER
,
2665 .arg1_type
= ARG_PTR_TO_CTX
,
2668 BPF_CALL_1(bpf_get_hash_recalc
, struct sk_buff
*, skb
)
2670 /* If skb_clear_hash() was called due to mangling, we can
2671 * trigger SW recalculation here. Later access to hash
2672 * can then use the inline skb->hash via context directly
2673 * instead of calling this helper again.
2675 return skb_get_hash(skb
);
2678 static const struct bpf_func_proto bpf_get_hash_recalc_proto
= {
2679 .func
= bpf_get_hash_recalc
,
2681 .ret_type
= RET_INTEGER
,
2682 .arg1_type
= ARG_PTR_TO_CTX
,
2685 BPF_CALL_1(bpf_set_hash_invalid
, struct sk_buff
*, skb
)
2687 /* After all direct packet write, this can be used once for
2688 * triggering a lazy recalc on next skb_get_hash() invocation.
2690 skb_clear_hash(skb
);
2694 static const struct bpf_func_proto bpf_set_hash_invalid_proto
= {
2695 .func
= bpf_set_hash_invalid
,
2697 .ret_type
= RET_INTEGER
,
2698 .arg1_type
= ARG_PTR_TO_CTX
,
2701 BPF_CALL_2(bpf_set_hash
, struct sk_buff
*, skb
, u32
, hash
)
2703 /* Set user specified hash as L4(+), so that it gets returned
2704 * on skb_get_hash() call unless BPF prog later on triggers a
2707 __skb_set_sw_hash(skb
, hash
, true);
2711 static const struct bpf_func_proto bpf_set_hash_proto
= {
2712 .func
= bpf_set_hash
,
2714 .ret_type
= RET_INTEGER
,
2715 .arg1_type
= ARG_PTR_TO_CTX
,
2716 .arg2_type
= ARG_ANYTHING
,
2719 BPF_CALL_3(bpf_skb_vlan_push
, struct sk_buff
*, skb
, __be16
, vlan_proto
,
2724 if (unlikely(vlan_proto
!= htons(ETH_P_8021Q
) &&
2725 vlan_proto
!= htons(ETH_P_8021AD
)))
2726 vlan_proto
= htons(ETH_P_8021Q
);
2728 bpf_push_mac_rcsum(skb
);
2729 ret
= skb_vlan_push(skb
, vlan_proto
, vlan_tci
);
2730 bpf_pull_mac_rcsum(skb
);
2732 bpf_compute_data_pointers(skb
);
2736 static const struct bpf_func_proto bpf_skb_vlan_push_proto
= {
2737 .func
= bpf_skb_vlan_push
,
2739 .ret_type
= RET_INTEGER
,
2740 .arg1_type
= ARG_PTR_TO_CTX
,
2741 .arg2_type
= ARG_ANYTHING
,
2742 .arg3_type
= ARG_ANYTHING
,
2745 BPF_CALL_1(bpf_skb_vlan_pop
, struct sk_buff
*, skb
)
2749 bpf_push_mac_rcsum(skb
);
2750 ret
= skb_vlan_pop(skb
);
2751 bpf_pull_mac_rcsum(skb
);
2753 bpf_compute_data_pointers(skb
);
2757 static const struct bpf_func_proto bpf_skb_vlan_pop_proto
= {
2758 .func
= bpf_skb_vlan_pop
,
2760 .ret_type
= RET_INTEGER
,
2761 .arg1_type
= ARG_PTR_TO_CTX
,
2764 static int bpf_skb_generic_push(struct sk_buff
*skb
, u32 off
, u32 len
)
2766 /* Caller already did skb_cow() with len as headroom,
2767 * so no need to do it here.
2770 memmove(skb
->data
, skb
->data
+ len
, off
);
2771 memset(skb
->data
+ off
, 0, len
);
2773 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2774 * needed here as it does not change the skb->csum
2775 * result for checksum complete when summing over
2781 static int bpf_skb_generic_pop(struct sk_buff
*skb
, u32 off
, u32 len
)
2783 /* skb_ensure_writable() is not needed here, as we're
2784 * already working on an uncloned skb.
2786 if (unlikely(!pskb_may_pull(skb
, off
+ len
)))
2789 skb_postpull_rcsum(skb
, skb
->data
+ off
, len
);
2790 memmove(skb
->data
+ len
, skb
->data
, off
);
2791 __skb_pull(skb
, len
);
2796 static int bpf_skb_net_hdr_push(struct sk_buff
*skb
, u32 off
, u32 len
)
2798 bool trans_same
= skb
->transport_header
== skb
->network_header
;
2801 /* There's no need for __skb_push()/__skb_pull() pair to
2802 * get to the start of the mac header as we're guaranteed
2803 * to always start from here under eBPF.
2805 ret
= bpf_skb_generic_push(skb
, off
, len
);
2807 skb
->mac_header
-= len
;
2808 skb
->network_header
-= len
;
2810 skb
->transport_header
= skb
->network_header
;
2816 static int bpf_skb_net_hdr_pop(struct sk_buff
*skb
, u32 off
, u32 len
)
2818 bool trans_same
= skb
->transport_header
== skb
->network_header
;
2821 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2822 ret
= bpf_skb_generic_pop(skb
, off
, len
);
2824 skb
->mac_header
+= len
;
2825 skb
->network_header
+= len
;
2827 skb
->transport_header
= skb
->network_header
;
2833 static int bpf_skb_proto_4_to_6(struct sk_buff
*skb
)
2835 const u32 len_diff
= sizeof(struct ipv6hdr
) - sizeof(struct iphdr
);
2836 u32 off
= skb_mac_header_len(skb
);
2839 if (skb_is_gso(skb
) && !skb_is_gso_tcp(skb
))
2842 ret
= skb_cow(skb
, len_diff
);
2843 if (unlikely(ret
< 0))
2846 ret
= bpf_skb_net_hdr_push(skb
, off
, len_diff
);
2847 if (unlikely(ret
< 0))
2850 if (skb_is_gso(skb
)) {
2851 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
2853 /* SKB_GSO_TCPV4 needs to be changed into
2856 if (shinfo
->gso_type
& SKB_GSO_TCPV4
) {
2857 shinfo
->gso_type
&= ~SKB_GSO_TCPV4
;
2858 shinfo
->gso_type
|= SKB_GSO_TCPV6
;
2861 /* Due to IPv6 header, MSS needs to be downgraded. */
2862 skb_decrease_gso_size(shinfo
, len_diff
);
2863 /* Header must be checked, and gso_segs recomputed. */
2864 shinfo
->gso_type
|= SKB_GSO_DODGY
;
2865 shinfo
->gso_segs
= 0;
2868 skb
->protocol
= htons(ETH_P_IPV6
);
2869 skb_clear_hash(skb
);
2874 static int bpf_skb_proto_6_to_4(struct sk_buff
*skb
)
2876 const u32 len_diff
= sizeof(struct ipv6hdr
) - sizeof(struct iphdr
);
2877 u32 off
= skb_mac_header_len(skb
);
2880 if (skb_is_gso(skb
) && !skb_is_gso_tcp(skb
))
2883 ret
= skb_unclone(skb
, GFP_ATOMIC
);
2884 if (unlikely(ret
< 0))
2887 ret
= bpf_skb_net_hdr_pop(skb
, off
, len_diff
);
2888 if (unlikely(ret
< 0))
2891 if (skb_is_gso(skb
)) {
2892 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
2894 /* SKB_GSO_TCPV6 needs to be changed into
2897 if (shinfo
->gso_type
& SKB_GSO_TCPV6
) {
2898 shinfo
->gso_type
&= ~SKB_GSO_TCPV6
;
2899 shinfo
->gso_type
|= SKB_GSO_TCPV4
;
2902 /* Due to IPv4 header, MSS can be upgraded. */
2903 skb_increase_gso_size(shinfo
, len_diff
);
2904 /* Header must be checked, and gso_segs recomputed. */
2905 shinfo
->gso_type
|= SKB_GSO_DODGY
;
2906 shinfo
->gso_segs
= 0;
2909 skb
->protocol
= htons(ETH_P_IP
);
2910 skb_clear_hash(skb
);
2915 static int bpf_skb_proto_xlat(struct sk_buff
*skb
, __be16 to_proto
)
2917 __be16 from_proto
= skb
->protocol
;
2919 if (from_proto
== htons(ETH_P_IP
) &&
2920 to_proto
== htons(ETH_P_IPV6
))
2921 return bpf_skb_proto_4_to_6(skb
);
2923 if (from_proto
== htons(ETH_P_IPV6
) &&
2924 to_proto
== htons(ETH_P_IP
))
2925 return bpf_skb_proto_6_to_4(skb
);
2930 BPF_CALL_3(bpf_skb_change_proto
, struct sk_buff
*, skb
, __be16
, proto
,
2935 if (unlikely(flags
))
2938 /* General idea is that this helper does the basic groundwork
2939 * needed for changing the protocol, and eBPF program fills the
2940 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2941 * and other helpers, rather than passing a raw buffer here.
2943 * The rationale is to keep this minimal and without a need to
2944 * deal with raw packet data. F.e. even if we would pass buffers
2945 * here, the program still needs to call the bpf_lX_csum_replace()
2946 * helpers anyway. Plus, this way we keep also separation of
2947 * concerns, since f.e. bpf_skb_store_bytes() should only take
2950 * Currently, additional options and extension header space are
2951 * not supported, but flags register is reserved so we can adapt
2952 * that. For offloads, we mark packet as dodgy, so that headers
2953 * need to be verified first.
2955 ret
= bpf_skb_proto_xlat(skb
, proto
);
2956 bpf_compute_data_pointers(skb
);
2960 static const struct bpf_func_proto bpf_skb_change_proto_proto
= {
2961 .func
= bpf_skb_change_proto
,
2963 .ret_type
= RET_INTEGER
,
2964 .arg1_type
= ARG_PTR_TO_CTX
,
2965 .arg2_type
= ARG_ANYTHING
,
2966 .arg3_type
= ARG_ANYTHING
,
2969 BPF_CALL_2(bpf_skb_change_type
, struct sk_buff
*, skb
, u32
, pkt_type
)
2971 /* We only allow a restricted subset to be changed for now. */
2972 if (unlikely(!skb_pkt_type_ok(skb
->pkt_type
) ||
2973 !skb_pkt_type_ok(pkt_type
)))
2976 skb
->pkt_type
= pkt_type
;
2980 static const struct bpf_func_proto bpf_skb_change_type_proto
= {
2981 .func
= bpf_skb_change_type
,
2983 .ret_type
= RET_INTEGER
,
2984 .arg1_type
= ARG_PTR_TO_CTX
,
2985 .arg2_type
= ARG_ANYTHING
,
2988 static u32
bpf_skb_net_base_len(const struct sk_buff
*skb
)
2990 switch (skb
->protocol
) {
2991 case htons(ETH_P_IP
):
2992 return sizeof(struct iphdr
);
2993 case htons(ETH_P_IPV6
):
2994 return sizeof(struct ipv6hdr
);
3000 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3001 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3003 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3004 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3005 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3006 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3007 BPF_F_ADJ_ROOM_ENCAP_L2( \
3008 BPF_ADJ_ROOM_ENCAP_L2_MASK))
3010 static int bpf_skb_net_grow(struct sk_buff
*skb
, u32 off
, u32 len_diff
,
3013 u8 inner_mac_len
= flags
>> BPF_ADJ_ROOM_ENCAP_L2_SHIFT
;
3014 bool encap
= flags
& BPF_F_ADJ_ROOM_ENCAP_L3_MASK
;
3015 u16 mac_len
= 0, inner_net
= 0, inner_trans
= 0;
3016 unsigned int gso_type
= SKB_GSO_DODGY
;
3019 if (skb_is_gso(skb
) && !skb_is_gso_tcp(skb
)) {
3020 /* udp gso_size delineates datagrams, only allow if fixed */
3021 if (!(skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP_L4
) ||
3022 !(flags
& BPF_F_ADJ_ROOM_FIXED_GSO
))
3026 ret
= skb_cow_head(skb
, len_diff
);
3027 if (unlikely(ret
< 0))
3031 if (skb
->protocol
!= htons(ETH_P_IP
) &&
3032 skb
->protocol
!= htons(ETH_P_IPV6
))
3035 if (flags
& BPF_F_ADJ_ROOM_ENCAP_L3_IPV4
&&
3036 flags
& BPF_F_ADJ_ROOM_ENCAP_L3_IPV6
)
3039 if (flags
& BPF_F_ADJ_ROOM_ENCAP_L4_GRE
&&
3040 flags
& BPF_F_ADJ_ROOM_ENCAP_L4_UDP
)
3043 if (skb
->encapsulation
)
3046 mac_len
= skb
->network_header
- skb
->mac_header
;
3047 inner_net
= skb
->network_header
;
3048 if (inner_mac_len
> len_diff
)
3050 inner_trans
= skb
->transport_header
;
3053 ret
= bpf_skb_net_hdr_push(skb
, off
, len_diff
);
3054 if (unlikely(ret
< 0))
3058 skb
->inner_mac_header
= inner_net
- inner_mac_len
;
3059 skb
->inner_network_header
= inner_net
;
3060 skb
->inner_transport_header
= inner_trans
;
3061 skb_set_inner_protocol(skb
, skb
->protocol
);
3063 skb
->encapsulation
= 1;
3064 skb_set_network_header(skb
, mac_len
);
3066 if (flags
& BPF_F_ADJ_ROOM_ENCAP_L4_UDP
)
3067 gso_type
|= SKB_GSO_UDP_TUNNEL
;
3068 else if (flags
& BPF_F_ADJ_ROOM_ENCAP_L4_GRE
)
3069 gso_type
|= SKB_GSO_GRE
;
3070 else if (flags
& BPF_F_ADJ_ROOM_ENCAP_L3_IPV6
)
3071 gso_type
|= SKB_GSO_IPXIP6
;
3072 else if (flags
& BPF_F_ADJ_ROOM_ENCAP_L3_IPV4
)
3073 gso_type
|= SKB_GSO_IPXIP4
;
3075 if (flags
& BPF_F_ADJ_ROOM_ENCAP_L4_GRE
||
3076 flags
& BPF_F_ADJ_ROOM_ENCAP_L4_UDP
) {
3077 int nh_len
= flags
& BPF_F_ADJ_ROOM_ENCAP_L3_IPV6
?
3078 sizeof(struct ipv6hdr
) :
3079 sizeof(struct iphdr
);
3081 skb_set_transport_header(skb
, mac_len
+ nh_len
);
3084 /* Match skb->protocol to new outer l3 protocol */
3085 if (skb
->protocol
== htons(ETH_P_IP
) &&
3086 flags
& BPF_F_ADJ_ROOM_ENCAP_L3_IPV6
)
3087 skb
->protocol
= htons(ETH_P_IPV6
);
3088 else if (skb
->protocol
== htons(ETH_P_IPV6
) &&
3089 flags
& BPF_F_ADJ_ROOM_ENCAP_L3_IPV4
)
3090 skb
->protocol
= htons(ETH_P_IP
);
3093 if (skb_is_gso(skb
)) {
3094 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
3096 /* Due to header grow, MSS needs to be downgraded. */
3097 if (!(flags
& BPF_F_ADJ_ROOM_FIXED_GSO
))
3098 skb_decrease_gso_size(shinfo
, len_diff
);
3100 /* Header must be checked, and gso_segs recomputed. */
3101 shinfo
->gso_type
|= gso_type
;
3102 shinfo
->gso_segs
= 0;
3108 static int bpf_skb_net_shrink(struct sk_buff
*skb
, u32 off
, u32 len_diff
,
3113 if (flags
& ~BPF_F_ADJ_ROOM_FIXED_GSO
)
3116 if (skb_is_gso(skb
) && !skb_is_gso_tcp(skb
)) {
3117 /* udp gso_size delineates datagrams, only allow if fixed */
3118 if (!(skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP_L4
) ||
3119 !(flags
& BPF_F_ADJ_ROOM_FIXED_GSO
))
3123 ret
= skb_unclone(skb
, GFP_ATOMIC
);
3124 if (unlikely(ret
< 0))
3127 ret
= bpf_skb_net_hdr_pop(skb
, off
, len_diff
);
3128 if (unlikely(ret
< 0))
3131 if (skb_is_gso(skb
)) {
3132 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
3134 /* Due to header shrink, MSS can be upgraded. */
3135 if (!(flags
& BPF_F_ADJ_ROOM_FIXED_GSO
))
3136 skb_increase_gso_size(shinfo
, len_diff
);
3138 /* Header must be checked, and gso_segs recomputed. */
3139 shinfo
->gso_type
|= SKB_GSO_DODGY
;
3140 shinfo
->gso_segs
= 0;
3146 static u32
__bpf_skb_max_len(const struct sk_buff
*skb
)
3148 return skb
->dev
? skb
->dev
->mtu
+ skb
->dev
->hard_header_len
:
3152 BPF_CALL_4(bpf_skb_adjust_room
, struct sk_buff
*, skb
, s32
, len_diff
,
3153 u32
, mode
, u64
, flags
)
3155 u32 len_cur
, len_diff_abs
= abs(len_diff
);
3156 u32 len_min
= bpf_skb_net_base_len(skb
);
3157 u32 len_max
= __bpf_skb_max_len(skb
);
3158 __be16 proto
= skb
->protocol
;
3159 bool shrink
= len_diff
< 0;
3163 if (unlikely(flags
& ~BPF_F_ADJ_ROOM_MASK
))
3165 if (unlikely(len_diff_abs
> 0xfffU
))
3167 if (unlikely(proto
!= htons(ETH_P_IP
) &&
3168 proto
!= htons(ETH_P_IPV6
)))
3171 off
= skb_mac_header_len(skb
);
3173 case BPF_ADJ_ROOM_NET
:
3174 off
+= bpf_skb_net_base_len(skb
);
3176 case BPF_ADJ_ROOM_MAC
:
3182 len_cur
= skb
->len
- skb_network_offset(skb
);
3183 if ((shrink
&& (len_diff_abs
>= len_cur
||
3184 len_cur
- len_diff_abs
< len_min
)) ||
3185 (!shrink
&& (skb
->len
+ len_diff_abs
> len_max
&&
3189 ret
= shrink
? bpf_skb_net_shrink(skb
, off
, len_diff_abs
, flags
) :
3190 bpf_skb_net_grow(skb
, off
, len_diff_abs
, flags
);
3192 bpf_compute_data_pointers(skb
);
3196 static const struct bpf_func_proto bpf_skb_adjust_room_proto
= {
3197 .func
= bpf_skb_adjust_room
,
3199 .ret_type
= RET_INTEGER
,
3200 .arg1_type
= ARG_PTR_TO_CTX
,
3201 .arg2_type
= ARG_ANYTHING
,
3202 .arg3_type
= ARG_ANYTHING
,
3203 .arg4_type
= ARG_ANYTHING
,
3206 static u32
__bpf_skb_min_len(const struct sk_buff
*skb
)
3208 u32 min_len
= skb_network_offset(skb
);
3210 if (skb_transport_header_was_set(skb
))
3211 min_len
= skb_transport_offset(skb
);
3212 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
3213 min_len
= skb_checksum_start_offset(skb
) +
3214 skb
->csum_offset
+ sizeof(__sum16
);
3218 static int bpf_skb_grow_rcsum(struct sk_buff
*skb
, unsigned int new_len
)
3220 unsigned int old_len
= skb
->len
;
3223 ret
= __skb_grow_rcsum(skb
, new_len
);
3225 memset(skb
->data
+ old_len
, 0, new_len
- old_len
);
3229 static int bpf_skb_trim_rcsum(struct sk_buff
*skb
, unsigned int new_len
)
3231 return __skb_trim_rcsum(skb
, new_len
);
3234 static inline int __bpf_skb_change_tail(struct sk_buff
*skb
, u32 new_len
,
3237 u32 max_len
= __bpf_skb_max_len(skb
);
3238 u32 min_len
= __bpf_skb_min_len(skb
);
3241 if (unlikely(flags
|| new_len
> max_len
|| new_len
< min_len
))
3243 if (skb
->encapsulation
)
3246 /* The basic idea of this helper is that it's performing the
3247 * needed work to either grow or trim an skb, and eBPF program
3248 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3249 * bpf_lX_csum_replace() and others rather than passing a raw
3250 * buffer here. This one is a slow path helper and intended
3251 * for replies with control messages.
3253 * Like in bpf_skb_change_proto(), we want to keep this rather
3254 * minimal and without protocol specifics so that we are able
3255 * to separate concerns as in bpf_skb_store_bytes() should only
3256 * be the one responsible for writing buffers.
3258 * It's really expected to be a slow path operation here for
3259 * control message replies, so we're implicitly linearizing,
3260 * uncloning and drop offloads from the skb by this.
3262 ret
= __bpf_try_make_writable(skb
, skb
->len
);
3264 if (new_len
> skb
->len
)
3265 ret
= bpf_skb_grow_rcsum(skb
, new_len
);
3266 else if (new_len
< skb
->len
)
3267 ret
= bpf_skb_trim_rcsum(skb
, new_len
);
3268 if (!ret
&& skb_is_gso(skb
))
3274 BPF_CALL_3(bpf_skb_change_tail
, struct sk_buff
*, skb
, u32
, new_len
,
3277 int ret
= __bpf_skb_change_tail(skb
, new_len
, flags
);
3279 bpf_compute_data_pointers(skb
);
3283 static const struct bpf_func_proto bpf_skb_change_tail_proto
= {
3284 .func
= bpf_skb_change_tail
,
3286 .ret_type
= RET_INTEGER
,
3287 .arg1_type
= ARG_PTR_TO_CTX
,
3288 .arg2_type
= ARG_ANYTHING
,
3289 .arg3_type
= ARG_ANYTHING
,
3292 BPF_CALL_3(sk_skb_change_tail
, struct sk_buff
*, skb
, u32
, new_len
,
3295 int ret
= __bpf_skb_change_tail(skb
, new_len
, flags
);
3297 bpf_compute_data_end_sk_skb(skb
);
3301 static const struct bpf_func_proto sk_skb_change_tail_proto
= {
3302 .func
= sk_skb_change_tail
,
3304 .ret_type
= RET_INTEGER
,
3305 .arg1_type
= ARG_PTR_TO_CTX
,
3306 .arg2_type
= ARG_ANYTHING
,
3307 .arg3_type
= ARG_ANYTHING
,
3310 static inline int __bpf_skb_change_head(struct sk_buff
*skb
, u32 head_room
,
3313 u32 max_len
= __bpf_skb_max_len(skb
);
3314 u32 new_len
= skb
->len
+ head_room
;
3317 if (unlikely(flags
|| (!skb_is_gso(skb
) && new_len
> max_len
) ||
3318 new_len
< skb
->len
))
3321 ret
= skb_cow(skb
, head_room
);
3323 /* Idea for this helper is that we currently only
3324 * allow to expand on mac header. This means that
3325 * skb->protocol network header, etc, stay as is.
3326 * Compared to bpf_skb_change_tail(), we're more
3327 * flexible due to not needing to linearize or
3328 * reset GSO. Intention for this helper is to be
3329 * used by an L3 skb that needs to push mac header
3330 * for redirection into L2 device.
3332 __skb_push(skb
, head_room
);
3333 memset(skb
->data
, 0, head_room
);
3334 skb_reset_mac_header(skb
);
3340 BPF_CALL_3(bpf_skb_change_head
, struct sk_buff
*, skb
, u32
, head_room
,
3343 int ret
= __bpf_skb_change_head(skb
, head_room
, flags
);
3345 bpf_compute_data_pointers(skb
);
3349 static const struct bpf_func_proto bpf_skb_change_head_proto
= {
3350 .func
= bpf_skb_change_head
,
3352 .ret_type
= RET_INTEGER
,
3353 .arg1_type
= ARG_PTR_TO_CTX
,
3354 .arg2_type
= ARG_ANYTHING
,
3355 .arg3_type
= ARG_ANYTHING
,
3358 BPF_CALL_3(sk_skb_change_head
, struct sk_buff
*, skb
, u32
, head_room
,
3361 int ret
= __bpf_skb_change_head(skb
, head_room
, flags
);
3363 bpf_compute_data_end_sk_skb(skb
);
3367 static const struct bpf_func_proto sk_skb_change_head_proto
= {
3368 .func
= sk_skb_change_head
,
3370 .ret_type
= RET_INTEGER
,
3371 .arg1_type
= ARG_PTR_TO_CTX
,
3372 .arg2_type
= ARG_ANYTHING
,
3373 .arg3_type
= ARG_ANYTHING
,
3375 static unsigned long xdp_get_metalen(const struct xdp_buff
*xdp
)
3377 return xdp_data_meta_unsupported(xdp
) ? 0 :
3378 xdp
->data
- xdp
->data_meta
;
3381 BPF_CALL_2(bpf_xdp_adjust_head
, struct xdp_buff
*, xdp
, int, offset
)
3383 void *xdp_frame_end
= xdp
->data_hard_start
+ sizeof(struct xdp_frame
);
3384 unsigned long metalen
= xdp_get_metalen(xdp
);
3385 void *data_start
= xdp_frame_end
+ metalen
;
3386 void *data
= xdp
->data
+ offset
;
3388 if (unlikely(data
< data_start
||
3389 data
> xdp
->data_end
- ETH_HLEN
))
3393 memmove(xdp
->data_meta
+ offset
,
3394 xdp
->data_meta
, metalen
);
3395 xdp
->data_meta
+= offset
;
3401 static const struct bpf_func_proto bpf_xdp_adjust_head_proto
= {
3402 .func
= bpf_xdp_adjust_head
,
3404 .ret_type
= RET_INTEGER
,
3405 .arg1_type
= ARG_PTR_TO_CTX
,
3406 .arg2_type
= ARG_ANYTHING
,
3409 BPF_CALL_2(bpf_xdp_adjust_tail
, struct xdp_buff
*, xdp
, int, offset
)
3411 void *data_end
= xdp
->data_end
+ offset
;
3413 /* only shrinking is allowed for now. */
3414 if (unlikely(offset
>= 0))
3417 if (unlikely(data_end
< xdp
->data
+ ETH_HLEN
))
3420 xdp
->data_end
= data_end
;
3425 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto
= {
3426 .func
= bpf_xdp_adjust_tail
,
3428 .ret_type
= RET_INTEGER
,
3429 .arg1_type
= ARG_PTR_TO_CTX
,
3430 .arg2_type
= ARG_ANYTHING
,
3433 BPF_CALL_2(bpf_xdp_adjust_meta
, struct xdp_buff
*, xdp
, int, offset
)
3435 void *xdp_frame_end
= xdp
->data_hard_start
+ sizeof(struct xdp_frame
);
3436 void *meta
= xdp
->data_meta
+ offset
;
3437 unsigned long metalen
= xdp
->data
- meta
;
3439 if (xdp_data_meta_unsupported(xdp
))
3441 if (unlikely(meta
< xdp_frame_end
||
3444 if (unlikely((metalen
& (sizeof(__u32
) - 1)) ||
3448 xdp
->data_meta
= meta
;
3453 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto
= {
3454 .func
= bpf_xdp_adjust_meta
,
3456 .ret_type
= RET_INTEGER
,
3457 .arg1_type
= ARG_PTR_TO_CTX
,
3458 .arg2_type
= ARG_ANYTHING
,
3461 static int __bpf_tx_xdp(struct net_device
*dev
,
3462 struct bpf_map
*map
,
3463 struct xdp_buff
*xdp
,
3466 struct xdp_frame
*xdpf
;
3469 if (!dev
->netdev_ops
->ndo_xdp_xmit
) {
3473 err
= xdp_ok_fwd_dev(dev
, xdp
->data_end
- xdp
->data
);
3477 xdpf
= convert_to_xdp_frame(xdp
);
3478 if (unlikely(!xdpf
))
3481 sent
= dev
->netdev_ops
->ndo_xdp_xmit(dev
, 1, &xdpf
, XDP_XMIT_FLUSH
);
3488 xdp_do_redirect_slow(struct net_device
*dev
, struct xdp_buff
*xdp
,
3489 struct bpf_prog
*xdp_prog
, struct bpf_redirect_info
*ri
)
3491 struct net_device
*fwd
;
3492 u32 index
= ri
->tgt_index
;
3495 fwd
= dev_get_by_index_rcu(dev_net(dev
), index
);
3497 if (unlikely(!fwd
)) {
3502 err
= __bpf_tx_xdp(fwd
, NULL
, xdp
, 0);
3506 _trace_xdp_redirect(dev
, xdp_prog
, index
);
3509 _trace_xdp_redirect_err(dev
, xdp_prog
, index
, err
);
3513 static int __bpf_tx_xdp_map(struct net_device
*dev_rx
, void *fwd
,
3514 struct bpf_map
*map
,
3515 struct xdp_buff
*xdp
,
3520 switch (map
->map_type
) {
3521 case BPF_MAP_TYPE_DEVMAP
: {
3522 struct bpf_dtab_netdev
*dst
= fwd
;
3524 err
= dev_map_enqueue(dst
, xdp
, dev_rx
);
3529 case BPF_MAP_TYPE_CPUMAP
: {
3530 struct bpf_cpu_map_entry
*rcpu
= fwd
;
3532 err
= cpu_map_enqueue(rcpu
, xdp
, dev_rx
);
3537 case BPF_MAP_TYPE_XSKMAP
: {
3538 struct xdp_sock
*xs
= fwd
;
3540 err
= __xsk_map_redirect(map
, xdp
, xs
);
3549 void xdp_do_flush_map(void)
3551 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3552 struct bpf_map
*map
= ri
->map_to_flush
;
3554 ri
->map_to_flush
= NULL
;
3556 switch (map
->map_type
) {
3557 case BPF_MAP_TYPE_DEVMAP
:
3558 __dev_map_flush(map
);
3560 case BPF_MAP_TYPE_CPUMAP
:
3561 __cpu_map_flush(map
);
3563 case BPF_MAP_TYPE_XSKMAP
:
3564 __xsk_map_flush(map
);
3571 EXPORT_SYMBOL_GPL(xdp_do_flush_map
);
3573 static inline void *__xdp_map_lookup_elem(struct bpf_map
*map
, u32 index
)
3575 switch (map
->map_type
) {
3576 case BPF_MAP_TYPE_DEVMAP
:
3577 return __dev_map_lookup_elem(map
, index
);
3578 case BPF_MAP_TYPE_CPUMAP
:
3579 return __cpu_map_lookup_elem(map
, index
);
3580 case BPF_MAP_TYPE_XSKMAP
:
3581 return __xsk_map_lookup_elem(map
, index
);
3587 void bpf_clear_redirect_map(struct bpf_map
*map
)
3589 struct bpf_redirect_info
*ri
;
3592 for_each_possible_cpu(cpu
) {
3593 ri
= per_cpu_ptr(&bpf_redirect_info
, cpu
);
3594 /* Avoid polluting remote cacheline due to writes if
3595 * not needed. Once we pass this test, we need the
3596 * cmpxchg() to make sure it hasn't been changed in
3597 * the meantime by remote CPU.
3599 if (unlikely(READ_ONCE(ri
->map
) == map
))
3600 cmpxchg(&ri
->map
, map
, NULL
);
3604 static int xdp_do_redirect_map(struct net_device
*dev
, struct xdp_buff
*xdp
,
3605 struct bpf_prog
*xdp_prog
, struct bpf_map
*map
,
3606 struct bpf_redirect_info
*ri
)
3608 u32 index
= ri
->tgt_index
;
3609 void *fwd
= ri
->tgt_value
;
3613 ri
->tgt_value
= NULL
;
3614 WRITE_ONCE(ri
->map
, NULL
);
3616 if (ri
->map_to_flush
&& unlikely(ri
->map_to_flush
!= map
))
3619 err
= __bpf_tx_xdp_map(dev
, fwd
, map
, xdp
, index
);
3623 ri
->map_to_flush
= map
;
3624 _trace_xdp_redirect_map(dev
, xdp_prog
, fwd
, map
, index
);
3627 _trace_xdp_redirect_map_err(dev
, xdp_prog
, fwd
, map
, index
, err
);
3631 int xdp_do_redirect(struct net_device
*dev
, struct xdp_buff
*xdp
,
3632 struct bpf_prog
*xdp_prog
)
3634 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3635 struct bpf_map
*map
= READ_ONCE(ri
->map
);
3638 return xdp_do_redirect_map(dev
, xdp
, xdp_prog
, map
, ri
);
3640 return xdp_do_redirect_slow(dev
, xdp
, xdp_prog
, ri
);
3642 EXPORT_SYMBOL_GPL(xdp_do_redirect
);
3644 static int xdp_do_generic_redirect_map(struct net_device
*dev
,
3645 struct sk_buff
*skb
,
3646 struct xdp_buff
*xdp
,
3647 struct bpf_prog
*xdp_prog
,
3648 struct bpf_map
*map
)
3650 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3651 u32 index
= ri
->tgt_index
;
3652 void *fwd
= ri
->tgt_value
;
3656 ri
->tgt_value
= NULL
;
3657 WRITE_ONCE(ri
->map
, NULL
);
3659 if (map
->map_type
== BPF_MAP_TYPE_DEVMAP
) {
3660 struct bpf_dtab_netdev
*dst
= fwd
;
3662 err
= dev_map_generic_redirect(dst
, skb
, xdp_prog
);
3665 } else if (map
->map_type
== BPF_MAP_TYPE_XSKMAP
) {
3666 struct xdp_sock
*xs
= fwd
;
3668 err
= xsk_generic_rcv(xs
, xdp
);
3673 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3678 _trace_xdp_redirect_map(dev
, xdp_prog
, fwd
, map
, index
);
3681 _trace_xdp_redirect_map_err(dev
, xdp_prog
, fwd
, map
, index
, err
);
3685 int xdp_do_generic_redirect(struct net_device
*dev
, struct sk_buff
*skb
,
3686 struct xdp_buff
*xdp
, struct bpf_prog
*xdp_prog
)
3688 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3689 struct bpf_map
*map
= READ_ONCE(ri
->map
);
3690 u32 index
= ri
->tgt_index
;
3691 struct net_device
*fwd
;
3695 return xdp_do_generic_redirect_map(dev
, skb
, xdp
, xdp_prog
,
3698 fwd
= dev_get_by_index_rcu(dev_net(dev
), index
);
3699 if (unlikely(!fwd
)) {
3704 err
= xdp_ok_fwd_dev(fwd
, skb
->len
);
3709 _trace_xdp_redirect(dev
, xdp_prog
, index
);
3710 generic_xdp_tx(skb
, xdp_prog
);
3713 _trace_xdp_redirect_err(dev
, xdp_prog
, index
, err
);
3716 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect
);
3718 BPF_CALL_2(bpf_xdp_redirect
, u32
, ifindex
, u64
, flags
)
3720 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3722 if (unlikely(flags
))
3726 ri
->tgt_index
= ifindex
;
3727 ri
->tgt_value
= NULL
;
3728 WRITE_ONCE(ri
->map
, NULL
);
3730 return XDP_REDIRECT
;
3733 static const struct bpf_func_proto bpf_xdp_redirect_proto
= {
3734 .func
= bpf_xdp_redirect
,
3736 .ret_type
= RET_INTEGER
,
3737 .arg1_type
= ARG_ANYTHING
,
3738 .arg2_type
= ARG_ANYTHING
,
3741 BPF_CALL_3(bpf_xdp_redirect_map
, struct bpf_map
*, map
, u32
, ifindex
,
3744 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3746 /* Lower bits of the flags are used as return code on lookup failure */
3747 if (unlikely(flags
> XDP_TX
))
3750 ri
->tgt_value
= __xdp_map_lookup_elem(map
, ifindex
);
3751 if (unlikely(!ri
->tgt_value
)) {
3752 /* If the lookup fails we want to clear out the state in the
3753 * redirect_info struct completely, so that if an eBPF program
3754 * performs multiple lookups, the last one always takes
3757 WRITE_ONCE(ri
->map
, NULL
);
3762 ri
->tgt_index
= ifindex
;
3763 WRITE_ONCE(ri
->map
, map
);
3765 return XDP_REDIRECT
;
3768 static const struct bpf_func_proto bpf_xdp_redirect_map_proto
= {
3769 .func
= bpf_xdp_redirect_map
,
3771 .ret_type
= RET_INTEGER
,
3772 .arg1_type
= ARG_CONST_MAP_PTR
,
3773 .arg2_type
= ARG_ANYTHING
,
3774 .arg3_type
= ARG_ANYTHING
,
3777 static unsigned long bpf_skb_copy(void *dst_buff
, const void *skb
,
3778 unsigned long off
, unsigned long len
)
3780 void *ptr
= skb_header_pointer(skb
, off
, len
, dst_buff
);
3784 if (ptr
!= dst_buff
)
3785 memcpy(dst_buff
, ptr
, len
);
3790 BPF_CALL_5(bpf_skb_event_output
, struct sk_buff
*, skb
, struct bpf_map
*, map
,
3791 u64
, flags
, void *, meta
, u64
, meta_size
)
3793 u64 skb_size
= (flags
& BPF_F_CTXLEN_MASK
) >> 32;
3795 if (unlikely(flags
& ~(BPF_F_CTXLEN_MASK
| BPF_F_INDEX_MASK
)))
3797 if (unlikely(skb_size
> skb
->len
))
3800 return bpf_event_output(map
, flags
, meta
, meta_size
, skb
, skb_size
,
3804 static const struct bpf_func_proto bpf_skb_event_output_proto
= {
3805 .func
= bpf_skb_event_output
,
3807 .ret_type
= RET_INTEGER
,
3808 .arg1_type
= ARG_PTR_TO_CTX
,
3809 .arg2_type
= ARG_CONST_MAP_PTR
,
3810 .arg3_type
= ARG_ANYTHING
,
3811 .arg4_type
= ARG_PTR_TO_MEM
,
3812 .arg5_type
= ARG_CONST_SIZE_OR_ZERO
,
3815 static unsigned short bpf_tunnel_key_af(u64 flags
)
3817 return flags
& BPF_F_TUNINFO_IPV6
? AF_INET6
: AF_INET
;
3820 BPF_CALL_4(bpf_skb_get_tunnel_key
, struct sk_buff
*, skb
, struct bpf_tunnel_key
*, to
,
3821 u32
, size
, u64
, flags
)
3823 const struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
3824 u8 compat
[sizeof(struct bpf_tunnel_key
)];
3828 if (unlikely(!info
|| (flags
& ~(BPF_F_TUNINFO_IPV6
)))) {
3832 if (ip_tunnel_info_af(info
) != bpf_tunnel_key_af(flags
)) {
3836 if (unlikely(size
!= sizeof(struct bpf_tunnel_key
))) {
3839 case offsetof(struct bpf_tunnel_key
, tunnel_label
):
3840 case offsetof(struct bpf_tunnel_key
, tunnel_ext
):
3842 case offsetof(struct bpf_tunnel_key
, remote_ipv6
[1]):
3843 /* Fixup deprecated structure layouts here, so we have
3844 * a common path later on.
3846 if (ip_tunnel_info_af(info
) != AF_INET
)
3849 to
= (struct bpf_tunnel_key
*)compat
;
3856 to
->tunnel_id
= be64_to_cpu(info
->key
.tun_id
);
3857 to
->tunnel_tos
= info
->key
.tos
;
3858 to
->tunnel_ttl
= info
->key
.ttl
;
3861 if (flags
& BPF_F_TUNINFO_IPV6
) {
3862 memcpy(to
->remote_ipv6
, &info
->key
.u
.ipv6
.src
,
3863 sizeof(to
->remote_ipv6
));
3864 to
->tunnel_label
= be32_to_cpu(info
->key
.label
);
3866 to
->remote_ipv4
= be32_to_cpu(info
->key
.u
.ipv4
.src
);
3867 memset(&to
->remote_ipv6
[1], 0, sizeof(__u32
) * 3);
3868 to
->tunnel_label
= 0;
3871 if (unlikely(size
!= sizeof(struct bpf_tunnel_key
)))
3872 memcpy(to_orig
, to
, size
);
3876 memset(to_orig
, 0, size
);
3880 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto
= {
3881 .func
= bpf_skb_get_tunnel_key
,
3883 .ret_type
= RET_INTEGER
,
3884 .arg1_type
= ARG_PTR_TO_CTX
,
3885 .arg2_type
= ARG_PTR_TO_UNINIT_MEM
,
3886 .arg3_type
= ARG_CONST_SIZE
,
3887 .arg4_type
= ARG_ANYTHING
,
3890 BPF_CALL_3(bpf_skb_get_tunnel_opt
, struct sk_buff
*, skb
, u8
*, to
, u32
, size
)
3892 const struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
3895 if (unlikely(!info
||
3896 !(info
->key
.tun_flags
& TUNNEL_OPTIONS_PRESENT
))) {
3900 if (unlikely(size
< info
->options_len
)) {
3905 ip_tunnel_info_opts_get(to
, info
);
3906 if (size
> info
->options_len
)
3907 memset(to
+ info
->options_len
, 0, size
- info
->options_len
);
3909 return info
->options_len
;
3911 memset(to
, 0, size
);
3915 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto
= {
3916 .func
= bpf_skb_get_tunnel_opt
,
3918 .ret_type
= RET_INTEGER
,
3919 .arg1_type
= ARG_PTR_TO_CTX
,
3920 .arg2_type
= ARG_PTR_TO_UNINIT_MEM
,
3921 .arg3_type
= ARG_CONST_SIZE
,
3924 static struct metadata_dst __percpu
*md_dst
;
3926 BPF_CALL_4(bpf_skb_set_tunnel_key
, struct sk_buff
*, skb
,
3927 const struct bpf_tunnel_key
*, from
, u32
, size
, u64
, flags
)
3929 struct metadata_dst
*md
= this_cpu_ptr(md_dst
);
3930 u8 compat
[sizeof(struct bpf_tunnel_key
)];
3931 struct ip_tunnel_info
*info
;
3933 if (unlikely(flags
& ~(BPF_F_TUNINFO_IPV6
| BPF_F_ZERO_CSUM_TX
|
3934 BPF_F_DONT_FRAGMENT
| BPF_F_SEQ_NUMBER
)))
3936 if (unlikely(size
!= sizeof(struct bpf_tunnel_key
))) {
3938 case offsetof(struct bpf_tunnel_key
, tunnel_label
):
3939 case offsetof(struct bpf_tunnel_key
, tunnel_ext
):
3940 case offsetof(struct bpf_tunnel_key
, remote_ipv6
[1]):
3941 /* Fixup deprecated structure layouts here, so we have
3942 * a common path later on.
3944 memcpy(compat
, from
, size
);
3945 memset(compat
+ size
, 0, sizeof(compat
) - size
);
3946 from
= (const struct bpf_tunnel_key
*) compat
;
3952 if (unlikely((!(flags
& BPF_F_TUNINFO_IPV6
) && from
->tunnel_label
) ||
3957 dst_hold((struct dst_entry
*) md
);
3958 skb_dst_set(skb
, (struct dst_entry
*) md
);
3960 info
= &md
->u
.tun_info
;
3961 memset(info
, 0, sizeof(*info
));
3962 info
->mode
= IP_TUNNEL_INFO_TX
;
3964 info
->key
.tun_flags
= TUNNEL_KEY
| TUNNEL_CSUM
| TUNNEL_NOCACHE
;
3965 if (flags
& BPF_F_DONT_FRAGMENT
)
3966 info
->key
.tun_flags
|= TUNNEL_DONT_FRAGMENT
;
3967 if (flags
& BPF_F_ZERO_CSUM_TX
)
3968 info
->key
.tun_flags
&= ~TUNNEL_CSUM
;
3969 if (flags
& BPF_F_SEQ_NUMBER
)
3970 info
->key
.tun_flags
|= TUNNEL_SEQ
;
3972 info
->key
.tun_id
= cpu_to_be64(from
->tunnel_id
);
3973 info
->key
.tos
= from
->tunnel_tos
;
3974 info
->key
.ttl
= from
->tunnel_ttl
;
3976 if (flags
& BPF_F_TUNINFO_IPV6
) {
3977 info
->mode
|= IP_TUNNEL_INFO_IPV6
;
3978 memcpy(&info
->key
.u
.ipv6
.dst
, from
->remote_ipv6
,
3979 sizeof(from
->remote_ipv6
));
3980 info
->key
.label
= cpu_to_be32(from
->tunnel_label
) &
3981 IPV6_FLOWLABEL_MASK
;
3983 info
->key
.u
.ipv4
.dst
= cpu_to_be32(from
->remote_ipv4
);
3989 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto
= {
3990 .func
= bpf_skb_set_tunnel_key
,
3992 .ret_type
= RET_INTEGER
,
3993 .arg1_type
= ARG_PTR_TO_CTX
,
3994 .arg2_type
= ARG_PTR_TO_MEM
,
3995 .arg3_type
= ARG_CONST_SIZE
,
3996 .arg4_type
= ARG_ANYTHING
,
3999 BPF_CALL_3(bpf_skb_set_tunnel_opt
, struct sk_buff
*, skb
,
4000 const u8
*, from
, u32
, size
)
4002 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
4003 const struct metadata_dst
*md
= this_cpu_ptr(md_dst
);
4005 if (unlikely(info
!= &md
->u
.tun_info
|| (size
& (sizeof(u32
) - 1))))
4007 if (unlikely(size
> IP_TUNNEL_OPTS_MAX
))
4010 ip_tunnel_info_opts_set(info
, from
, size
, TUNNEL_OPTIONS_PRESENT
);
4015 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto
= {
4016 .func
= bpf_skb_set_tunnel_opt
,
4018 .ret_type
= RET_INTEGER
,
4019 .arg1_type
= ARG_PTR_TO_CTX
,
4020 .arg2_type
= ARG_PTR_TO_MEM
,
4021 .arg3_type
= ARG_CONST_SIZE
,
4024 static const struct bpf_func_proto
*
4025 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which
)
4028 struct metadata_dst __percpu
*tmp
;
4030 tmp
= metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX
,
4035 if (cmpxchg(&md_dst
, NULL
, tmp
))
4036 metadata_dst_free_percpu(tmp
);
4040 case BPF_FUNC_skb_set_tunnel_key
:
4041 return &bpf_skb_set_tunnel_key_proto
;
4042 case BPF_FUNC_skb_set_tunnel_opt
:
4043 return &bpf_skb_set_tunnel_opt_proto
;
4049 BPF_CALL_3(bpf_skb_under_cgroup
, struct sk_buff
*, skb
, struct bpf_map
*, map
,
4052 struct bpf_array
*array
= container_of(map
, struct bpf_array
, map
);
4053 struct cgroup
*cgrp
;
4056 sk
= skb_to_full_sk(skb
);
4057 if (!sk
|| !sk_fullsock(sk
))
4059 if (unlikely(idx
>= array
->map
.max_entries
))
4062 cgrp
= READ_ONCE(array
->ptrs
[idx
]);
4063 if (unlikely(!cgrp
))
4066 return sk_under_cgroup_hierarchy(sk
, cgrp
);
4069 static const struct bpf_func_proto bpf_skb_under_cgroup_proto
= {
4070 .func
= bpf_skb_under_cgroup
,
4072 .ret_type
= RET_INTEGER
,
4073 .arg1_type
= ARG_PTR_TO_CTX
,
4074 .arg2_type
= ARG_CONST_MAP_PTR
,
4075 .arg3_type
= ARG_ANYTHING
,
4078 #ifdef CONFIG_SOCK_CGROUP_DATA
4079 BPF_CALL_1(bpf_skb_cgroup_id
, const struct sk_buff
*, skb
)
4081 struct sock
*sk
= skb_to_full_sk(skb
);
4082 struct cgroup
*cgrp
;
4084 if (!sk
|| !sk_fullsock(sk
))
4087 cgrp
= sock_cgroup_ptr(&sk
->sk_cgrp_data
);
4088 return cgrp
->kn
->id
.id
;
4091 static const struct bpf_func_proto bpf_skb_cgroup_id_proto
= {
4092 .func
= bpf_skb_cgroup_id
,
4094 .ret_type
= RET_INTEGER
,
4095 .arg1_type
= ARG_PTR_TO_CTX
,
4098 BPF_CALL_2(bpf_skb_ancestor_cgroup_id
, const struct sk_buff
*, skb
, int,
4101 struct sock
*sk
= skb_to_full_sk(skb
);
4102 struct cgroup
*ancestor
;
4103 struct cgroup
*cgrp
;
4105 if (!sk
|| !sk_fullsock(sk
))
4108 cgrp
= sock_cgroup_ptr(&sk
->sk_cgrp_data
);
4109 ancestor
= cgroup_ancestor(cgrp
, ancestor_level
);
4113 return ancestor
->kn
->id
.id
;
4116 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto
= {
4117 .func
= bpf_skb_ancestor_cgroup_id
,
4119 .ret_type
= RET_INTEGER
,
4120 .arg1_type
= ARG_PTR_TO_CTX
,
4121 .arg2_type
= ARG_ANYTHING
,
4125 static unsigned long bpf_xdp_copy(void *dst_buff
, const void *src_buff
,
4126 unsigned long off
, unsigned long len
)
4128 memcpy(dst_buff
, src_buff
+ off
, len
);
4132 BPF_CALL_5(bpf_xdp_event_output
, struct xdp_buff
*, xdp
, struct bpf_map
*, map
,
4133 u64
, flags
, void *, meta
, u64
, meta_size
)
4135 u64 xdp_size
= (flags
& BPF_F_CTXLEN_MASK
) >> 32;
4137 if (unlikely(flags
& ~(BPF_F_CTXLEN_MASK
| BPF_F_INDEX_MASK
)))
4139 if (unlikely(xdp_size
> (unsigned long)(xdp
->data_end
- xdp
->data
)))
4142 return bpf_event_output(map
, flags
, meta
, meta_size
, xdp
->data
,
4143 xdp_size
, bpf_xdp_copy
);
4146 static const struct bpf_func_proto bpf_xdp_event_output_proto
= {
4147 .func
= bpf_xdp_event_output
,
4149 .ret_type
= RET_INTEGER
,
4150 .arg1_type
= ARG_PTR_TO_CTX
,
4151 .arg2_type
= ARG_CONST_MAP_PTR
,
4152 .arg3_type
= ARG_ANYTHING
,
4153 .arg4_type
= ARG_PTR_TO_MEM
,
4154 .arg5_type
= ARG_CONST_SIZE_OR_ZERO
,
4157 BPF_CALL_1(bpf_get_socket_cookie
, struct sk_buff
*, skb
)
4159 return skb
->sk
? sock_gen_cookie(skb
->sk
) : 0;
4162 static const struct bpf_func_proto bpf_get_socket_cookie_proto
= {
4163 .func
= bpf_get_socket_cookie
,
4165 .ret_type
= RET_INTEGER
,
4166 .arg1_type
= ARG_PTR_TO_CTX
,
4169 BPF_CALL_1(bpf_get_socket_cookie_sock_addr
, struct bpf_sock_addr_kern
*, ctx
)
4171 return sock_gen_cookie(ctx
->sk
);
4174 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto
= {
4175 .func
= bpf_get_socket_cookie_sock_addr
,
4177 .ret_type
= RET_INTEGER
,
4178 .arg1_type
= ARG_PTR_TO_CTX
,
4181 BPF_CALL_1(bpf_get_socket_cookie_sock_ops
, struct bpf_sock_ops_kern
*, ctx
)
4183 return sock_gen_cookie(ctx
->sk
);
4186 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto
= {
4187 .func
= bpf_get_socket_cookie_sock_ops
,
4189 .ret_type
= RET_INTEGER
,
4190 .arg1_type
= ARG_PTR_TO_CTX
,
4193 BPF_CALL_1(bpf_get_socket_uid
, struct sk_buff
*, skb
)
4195 struct sock
*sk
= sk_to_full_sk(skb
->sk
);
4198 if (!sk
|| !sk_fullsock(sk
))
4200 kuid
= sock_net_uid(sock_net(sk
), sk
);
4201 return from_kuid_munged(sock_net(sk
)->user_ns
, kuid
);
4204 static const struct bpf_func_proto bpf_get_socket_uid_proto
= {
4205 .func
= bpf_get_socket_uid
,
4207 .ret_type
= RET_INTEGER
,
4208 .arg1_type
= ARG_PTR_TO_CTX
,
4211 BPF_CALL_5(bpf_sockopt_event_output
, struct bpf_sock_ops_kern
*, bpf_sock
,
4212 struct bpf_map
*, map
, u64
, flags
, void *, data
, u64
, size
)
4214 if (unlikely(flags
& ~(BPF_F_INDEX_MASK
)))
4217 return bpf_event_output(map
, flags
, data
, size
, NULL
, 0, NULL
);
4220 static const struct bpf_func_proto bpf_sockopt_event_output_proto
= {
4221 .func
= bpf_sockopt_event_output
,
4223 .ret_type
= RET_INTEGER
,
4224 .arg1_type
= ARG_PTR_TO_CTX
,
4225 .arg2_type
= ARG_CONST_MAP_PTR
,
4226 .arg3_type
= ARG_ANYTHING
,
4227 .arg4_type
= ARG_PTR_TO_MEM
,
4228 .arg5_type
= ARG_CONST_SIZE_OR_ZERO
,
4231 BPF_CALL_5(bpf_setsockopt
, struct bpf_sock_ops_kern
*, bpf_sock
,
4232 int, level
, int, optname
, char *, optval
, int, optlen
)
4234 struct sock
*sk
= bpf_sock
->sk
;
4238 if (!sk_fullsock(sk
))
4241 if (level
== SOL_SOCKET
) {
4242 if (optlen
!= sizeof(int))
4244 val
= *((int *)optval
);
4246 /* Only some socketops are supported */
4249 val
= min_t(u32
, val
, sysctl_rmem_max
);
4250 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
4251 sk
->sk_rcvbuf
= max_t(int, val
* 2, SOCK_MIN_RCVBUF
);
4254 val
= min_t(u32
, val
, sysctl_wmem_max
);
4255 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
4256 sk
->sk_sndbuf
= max_t(int, val
* 2, SOCK_MIN_SNDBUF
);
4258 case SO_MAX_PACING_RATE
: /* 32bit version */
4260 cmpxchg(&sk
->sk_pacing_status
,
4263 sk
->sk_max_pacing_rate
= (val
== ~0U) ? ~0UL : val
;
4264 sk
->sk_pacing_rate
= min(sk
->sk_pacing_rate
,
4265 sk
->sk_max_pacing_rate
);
4268 sk
->sk_priority
= val
;
4273 sk
->sk_rcvlowat
= val
? : 1;
4276 if (sk
->sk_mark
!= val
) {
4285 } else if (level
== SOL_IP
) {
4286 if (optlen
!= sizeof(int) || sk
->sk_family
!= AF_INET
)
4289 val
= *((int *)optval
);
4290 /* Only some options are supported */
4293 if (val
< -1 || val
> 0xff) {
4296 struct inet_sock
*inet
= inet_sk(sk
);
4306 #if IS_ENABLED(CONFIG_IPV6)
4307 } else if (level
== SOL_IPV6
) {
4308 if (optlen
!= sizeof(int) || sk
->sk_family
!= AF_INET6
)
4311 val
= *((int *)optval
);
4312 /* Only some options are supported */
4315 if (val
< -1 || val
> 0xff) {
4318 struct ipv6_pinfo
*np
= inet6_sk(sk
);
4329 } else if (level
== SOL_TCP
&&
4330 sk
->sk_prot
->setsockopt
== tcp_setsockopt
) {
4331 if (optname
== TCP_CONGESTION
) {
4332 char name
[TCP_CA_NAME_MAX
];
4333 bool reinit
= bpf_sock
->op
> BPF_SOCK_OPS_NEEDS_ECN
;
4335 strncpy(name
, optval
, min_t(long, optlen
,
4336 TCP_CA_NAME_MAX
-1));
4337 name
[TCP_CA_NAME_MAX
-1] = 0;
4338 ret
= tcp_set_congestion_control(sk
, name
, false,
4341 struct tcp_sock
*tp
= tcp_sk(sk
);
4343 if (optlen
!= sizeof(int))
4346 val
= *((int *)optval
);
4347 /* Only some options are supported */
4350 if (val
<= 0 || tp
->data_segs_out
> tp
->syn_data
)
4355 case TCP_BPF_SNDCWND_CLAMP
:
4359 tp
->snd_cwnd_clamp
= val
;
4360 tp
->snd_ssthresh
= val
;
4364 if (val
< 0 || val
> 1)
4380 static const struct bpf_func_proto bpf_setsockopt_proto
= {
4381 .func
= bpf_setsockopt
,
4383 .ret_type
= RET_INTEGER
,
4384 .arg1_type
= ARG_PTR_TO_CTX
,
4385 .arg2_type
= ARG_ANYTHING
,
4386 .arg3_type
= ARG_ANYTHING
,
4387 .arg4_type
= ARG_PTR_TO_MEM
,
4388 .arg5_type
= ARG_CONST_SIZE
,
4391 BPF_CALL_5(bpf_getsockopt
, struct bpf_sock_ops_kern
*, bpf_sock
,
4392 int, level
, int, optname
, char *, optval
, int, optlen
)
4394 struct sock
*sk
= bpf_sock
->sk
;
4396 if (!sk_fullsock(sk
))
4399 if (level
== SOL_TCP
&& sk
->sk_prot
->getsockopt
== tcp_getsockopt
) {
4400 struct inet_connection_sock
*icsk
;
4401 struct tcp_sock
*tp
;
4404 case TCP_CONGESTION
:
4405 icsk
= inet_csk(sk
);
4407 if (!icsk
->icsk_ca_ops
|| optlen
<= 1)
4409 strncpy(optval
, icsk
->icsk_ca_ops
->name
, optlen
);
4410 optval
[optlen
- 1] = 0;
4415 if (optlen
<= 0 || !tp
->saved_syn
||
4416 optlen
> tp
->saved_syn
[0])
4418 memcpy(optval
, tp
->saved_syn
+ 1, optlen
);
4423 } else if (level
== SOL_IP
) {
4424 struct inet_sock
*inet
= inet_sk(sk
);
4426 if (optlen
!= sizeof(int) || sk
->sk_family
!= AF_INET
)
4429 /* Only some options are supported */
4432 *((int *)optval
) = (int)inet
->tos
;
4437 #if IS_ENABLED(CONFIG_IPV6)
4438 } else if (level
== SOL_IPV6
) {
4439 struct ipv6_pinfo
*np
= inet6_sk(sk
);
4441 if (optlen
!= sizeof(int) || sk
->sk_family
!= AF_INET6
)
4444 /* Only some options are supported */
4447 *((int *)optval
) = (int)np
->tclass
;
4459 memset(optval
, 0, optlen
);
4463 static const struct bpf_func_proto bpf_getsockopt_proto
= {
4464 .func
= bpf_getsockopt
,
4466 .ret_type
= RET_INTEGER
,
4467 .arg1_type
= ARG_PTR_TO_CTX
,
4468 .arg2_type
= ARG_ANYTHING
,
4469 .arg3_type
= ARG_ANYTHING
,
4470 .arg4_type
= ARG_PTR_TO_UNINIT_MEM
,
4471 .arg5_type
= ARG_CONST_SIZE
,
4474 BPF_CALL_2(bpf_sock_ops_cb_flags_set
, struct bpf_sock_ops_kern
*, bpf_sock
,
4477 struct sock
*sk
= bpf_sock
->sk
;
4478 int val
= argval
& BPF_SOCK_OPS_ALL_CB_FLAGS
;
4480 if (!IS_ENABLED(CONFIG_INET
) || !sk_fullsock(sk
))
4483 tcp_sk(sk
)->bpf_sock_ops_cb_flags
= val
;
4485 return argval
& (~BPF_SOCK_OPS_ALL_CB_FLAGS
);
4488 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto
= {
4489 .func
= bpf_sock_ops_cb_flags_set
,
4491 .ret_type
= RET_INTEGER
,
4492 .arg1_type
= ARG_PTR_TO_CTX
,
4493 .arg2_type
= ARG_ANYTHING
,
4496 const struct ipv6_bpf_stub
*ipv6_bpf_stub __read_mostly
;
4497 EXPORT_SYMBOL_GPL(ipv6_bpf_stub
);
4499 BPF_CALL_3(bpf_bind
, struct bpf_sock_addr_kern
*, ctx
, struct sockaddr
*, addr
,
4503 struct sock
*sk
= ctx
->sk
;
4506 /* Binding to port can be expensive so it's prohibited in the helper.
4507 * Only binding to IP is supported.
4510 if (addr_len
< offsetofend(struct sockaddr
, sa_family
))
4512 if (addr
->sa_family
== AF_INET
) {
4513 if (addr_len
< sizeof(struct sockaddr_in
))
4515 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
4517 return __inet_bind(sk
, addr
, addr_len
, true, false);
4518 #if IS_ENABLED(CONFIG_IPV6)
4519 } else if (addr
->sa_family
== AF_INET6
) {
4520 if (addr_len
< SIN6_LEN_RFC2133
)
4522 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
4524 /* ipv6_bpf_stub cannot be NULL, since it's called from
4525 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4527 return ipv6_bpf_stub
->inet6_bind(sk
, addr
, addr_len
, true, false);
4528 #endif /* CONFIG_IPV6 */
4530 #endif /* CONFIG_INET */
4532 return -EAFNOSUPPORT
;
4535 static const struct bpf_func_proto bpf_bind_proto
= {
4538 .ret_type
= RET_INTEGER
,
4539 .arg1_type
= ARG_PTR_TO_CTX
,
4540 .arg2_type
= ARG_PTR_TO_MEM
,
4541 .arg3_type
= ARG_CONST_SIZE
,
4545 BPF_CALL_5(bpf_skb_get_xfrm_state
, struct sk_buff
*, skb
, u32
, index
,
4546 struct bpf_xfrm_state
*, to
, u32
, size
, u64
, flags
)
4548 const struct sec_path
*sp
= skb_sec_path(skb
);
4549 const struct xfrm_state
*x
;
4551 if (!sp
|| unlikely(index
>= sp
->len
|| flags
))
4554 x
= sp
->xvec
[index
];
4556 if (unlikely(size
!= sizeof(struct bpf_xfrm_state
)))
4559 to
->reqid
= x
->props
.reqid
;
4560 to
->spi
= x
->id
.spi
;
4561 to
->family
= x
->props
.family
;
4564 if (to
->family
== AF_INET6
) {
4565 memcpy(to
->remote_ipv6
, x
->props
.saddr
.a6
,
4566 sizeof(to
->remote_ipv6
));
4568 to
->remote_ipv4
= x
->props
.saddr
.a4
;
4569 memset(&to
->remote_ipv6
[1], 0, sizeof(__u32
) * 3);
4574 memset(to
, 0, size
);
4578 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto
= {
4579 .func
= bpf_skb_get_xfrm_state
,
4581 .ret_type
= RET_INTEGER
,
4582 .arg1_type
= ARG_PTR_TO_CTX
,
4583 .arg2_type
= ARG_ANYTHING
,
4584 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
4585 .arg4_type
= ARG_CONST_SIZE
,
4586 .arg5_type
= ARG_ANYTHING
,
4590 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4591 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup
*params
,
4592 const struct neighbour
*neigh
,
4593 const struct net_device
*dev
)
4595 memcpy(params
->dmac
, neigh
->ha
, ETH_ALEN
);
4596 memcpy(params
->smac
, dev
->dev_addr
, ETH_ALEN
);
4597 params
->h_vlan_TCI
= 0;
4598 params
->h_vlan_proto
= 0;
4599 params
->ifindex
= dev
->ifindex
;
4605 #if IS_ENABLED(CONFIG_INET)
4606 static int bpf_ipv4_fib_lookup(struct net
*net
, struct bpf_fib_lookup
*params
,
4607 u32 flags
, bool check_mtu
)
4609 struct fib_nh_common
*nhc
;
4610 struct in_device
*in_dev
;
4611 struct neighbour
*neigh
;
4612 struct net_device
*dev
;
4613 struct fib_result res
;
4618 dev
= dev_get_by_index_rcu(net
, params
->ifindex
);
4622 /* verify forwarding is enabled on this interface */
4623 in_dev
= __in_dev_get_rcu(dev
);
4624 if (unlikely(!in_dev
|| !IN_DEV_FORWARD(in_dev
)))
4625 return BPF_FIB_LKUP_RET_FWD_DISABLED
;
4627 if (flags
& BPF_FIB_LOOKUP_OUTPUT
) {
4629 fl4
.flowi4_oif
= params
->ifindex
;
4631 fl4
.flowi4_iif
= params
->ifindex
;
4634 fl4
.flowi4_tos
= params
->tos
& IPTOS_RT_MASK
;
4635 fl4
.flowi4_scope
= RT_SCOPE_UNIVERSE
;
4636 fl4
.flowi4_flags
= 0;
4638 fl4
.flowi4_proto
= params
->l4_protocol
;
4639 fl4
.daddr
= params
->ipv4_dst
;
4640 fl4
.saddr
= params
->ipv4_src
;
4641 fl4
.fl4_sport
= params
->sport
;
4642 fl4
.fl4_dport
= params
->dport
;
4644 if (flags
& BPF_FIB_LOOKUP_DIRECT
) {
4645 u32 tbid
= l3mdev_fib_table_rcu(dev
) ? : RT_TABLE_MAIN
;
4646 struct fib_table
*tb
;
4648 tb
= fib_get_table(net
, tbid
);
4650 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4652 err
= fib_table_lookup(tb
, &fl4
, &res
, FIB_LOOKUP_NOREF
);
4654 fl4
.flowi4_mark
= 0;
4655 fl4
.flowi4_secid
= 0;
4656 fl4
.flowi4_tun_key
.tun_id
= 0;
4657 fl4
.flowi4_uid
= sock_net_uid(net
, NULL
);
4659 err
= fib_lookup(net
, &fl4
, &res
, FIB_LOOKUP_NOREF
);
4663 /* map fib lookup errors to RTN_ type */
4665 return BPF_FIB_LKUP_RET_BLACKHOLE
;
4666 if (err
== -EHOSTUNREACH
)
4667 return BPF_FIB_LKUP_RET_UNREACHABLE
;
4669 return BPF_FIB_LKUP_RET_PROHIBIT
;
4671 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4674 if (res
.type
!= RTN_UNICAST
)
4675 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4677 if (fib_info_num_path(res
.fi
) > 1)
4678 fib_select_path(net
, &res
, &fl4
, NULL
);
4681 mtu
= ip_mtu_from_fib_result(&res
, params
->ipv4_dst
);
4682 if (params
->tot_len
> mtu
)
4683 return BPF_FIB_LKUP_RET_FRAG_NEEDED
;
4688 /* do not handle lwt encaps right now */
4689 if (nhc
->nhc_lwtstate
)
4690 return BPF_FIB_LKUP_RET_UNSUPP_LWT
;
4694 params
->rt_metric
= res
.fi
->fib_priority
;
4696 /* xdp and cls_bpf programs are run in RCU-bh so
4697 * rcu_read_lock_bh is not needed here
4699 if (likely(nhc
->nhc_gw_family
!= AF_INET6
)) {
4700 if (nhc
->nhc_gw_family
)
4701 params
->ipv4_dst
= nhc
->nhc_gw
.ipv4
;
4703 neigh
= __ipv4_neigh_lookup_noref(dev
,
4704 (__force u32
)params
->ipv4_dst
);
4706 struct in6_addr
*dst
= (struct in6_addr
*)params
->ipv6_dst
;
4708 params
->family
= AF_INET6
;
4709 *dst
= nhc
->nhc_gw
.ipv6
;
4710 neigh
= __ipv6_neigh_lookup_noref_stub(dev
, dst
);
4714 return BPF_FIB_LKUP_RET_NO_NEIGH
;
4716 return bpf_fib_set_fwd_params(params
, neigh
, dev
);
4720 #if IS_ENABLED(CONFIG_IPV6)
4721 static int bpf_ipv6_fib_lookup(struct net
*net
, struct bpf_fib_lookup
*params
,
4722 u32 flags
, bool check_mtu
)
4724 struct in6_addr
*src
= (struct in6_addr
*) params
->ipv6_src
;
4725 struct in6_addr
*dst
= (struct in6_addr
*) params
->ipv6_dst
;
4726 struct fib6_result res
= {};
4727 struct neighbour
*neigh
;
4728 struct net_device
*dev
;
4729 struct inet6_dev
*idev
;
4735 /* link local addresses are never forwarded */
4736 if (rt6_need_strict(dst
) || rt6_need_strict(src
))
4737 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4739 dev
= dev_get_by_index_rcu(net
, params
->ifindex
);
4743 idev
= __in6_dev_get_safely(dev
);
4744 if (unlikely(!idev
|| !idev
->cnf
.forwarding
))
4745 return BPF_FIB_LKUP_RET_FWD_DISABLED
;
4747 if (flags
& BPF_FIB_LOOKUP_OUTPUT
) {
4749 oif
= fl6
.flowi6_oif
= params
->ifindex
;
4751 oif
= fl6
.flowi6_iif
= params
->ifindex
;
4753 strict
= RT6_LOOKUP_F_HAS_SADDR
;
4755 fl6
.flowlabel
= params
->flowinfo
;
4756 fl6
.flowi6_scope
= 0;
4757 fl6
.flowi6_flags
= 0;
4760 fl6
.flowi6_proto
= params
->l4_protocol
;
4763 fl6
.fl6_sport
= params
->sport
;
4764 fl6
.fl6_dport
= params
->dport
;
4766 if (flags
& BPF_FIB_LOOKUP_DIRECT
) {
4767 u32 tbid
= l3mdev_fib_table_rcu(dev
) ? : RT_TABLE_MAIN
;
4768 struct fib6_table
*tb
;
4770 tb
= ipv6_stub
->fib6_get_table(net
, tbid
);
4772 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4774 err
= ipv6_stub
->fib6_table_lookup(net
, tb
, oif
, &fl6
, &res
,
4777 fl6
.flowi6_mark
= 0;
4778 fl6
.flowi6_secid
= 0;
4779 fl6
.flowi6_tun_key
.tun_id
= 0;
4780 fl6
.flowi6_uid
= sock_net_uid(net
, NULL
);
4782 err
= ipv6_stub
->fib6_lookup(net
, oif
, &fl6
, &res
, strict
);
4785 if (unlikely(err
|| IS_ERR_OR_NULL(res
.f6i
) ||
4786 res
.f6i
== net
->ipv6
.fib6_null_entry
))
4787 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4789 switch (res
.fib6_type
) {
4790 /* only unicast is forwarded */
4794 return BPF_FIB_LKUP_RET_BLACKHOLE
;
4795 case RTN_UNREACHABLE
:
4796 return BPF_FIB_LKUP_RET_UNREACHABLE
;
4798 return BPF_FIB_LKUP_RET_PROHIBIT
;
4800 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4803 ipv6_stub
->fib6_select_path(net
, &res
, &fl6
, fl6
.flowi6_oif
,
4804 fl6
.flowi6_oif
!= 0, NULL
, strict
);
4807 mtu
= ipv6_stub
->ip6_mtu_from_fib6(&res
, dst
, src
);
4808 if (params
->tot_len
> mtu
)
4809 return BPF_FIB_LKUP_RET_FRAG_NEEDED
;
4812 if (res
.nh
->fib_nh_lws
)
4813 return BPF_FIB_LKUP_RET_UNSUPP_LWT
;
4815 if (res
.nh
->fib_nh_gw_family
)
4816 *dst
= res
.nh
->fib_nh_gw6
;
4818 dev
= res
.nh
->fib_nh_dev
;
4819 params
->rt_metric
= res
.f6i
->fib6_metric
;
4821 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4824 neigh
= __ipv6_neigh_lookup_noref_stub(dev
, dst
);
4826 return BPF_FIB_LKUP_RET_NO_NEIGH
;
4828 return bpf_fib_set_fwd_params(params
, neigh
, dev
);
4832 BPF_CALL_4(bpf_xdp_fib_lookup
, struct xdp_buff
*, ctx
,
4833 struct bpf_fib_lookup
*, params
, int, plen
, u32
, flags
)
4835 if (plen
< sizeof(*params
))
4838 if (flags
& ~(BPF_FIB_LOOKUP_DIRECT
| BPF_FIB_LOOKUP_OUTPUT
))
4841 switch (params
->family
) {
4842 #if IS_ENABLED(CONFIG_INET)
4844 return bpf_ipv4_fib_lookup(dev_net(ctx
->rxq
->dev
), params
,
4847 #if IS_ENABLED(CONFIG_IPV6)
4849 return bpf_ipv6_fib_lookup(dev_net(ctx
->rxq
->dev
), params
,
4853 return -EAFNOSUPPORT
;
4856 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto
= {
4857 .func
= bpf_xdp_fib_lookup
,
4859 .ret_type
= RET_INTEGER
,
4860 .arg1_type
= ARG_PTR_TO_CTX
,
4861 .arg2_type
= ARG_PTR_TO_MEM
,
4862 .arg3_type
= ARG_CONST_SIZE
,
4863 .arg4_type
= ARG_ANYTHING
,
4866 BPF_CALL_4(bpf_skb_fib_lookup
, struct sk_buff
*, skb
,
4867 struct bpf_fib_lookup
*, params
, int, plen
, u32
, flags
)
4869 struct net
*net
= dev_net(skb
->dev
);
4870 int rc
= -EAFNOSUPPORT
;
4872 if (plen
< sizeof(*params
))
4875 if (flags
& ~(BPF_FIB_LOOKUP_DIRECT
| BPF_FIB_LOOKUP_OUTPUT
))
4878 switch (params
->family
) {
4879 #if IS_ENABLED(CONFIG_INET)
4881 rc
= bpf_ipv4_fib_lookup(net
, params
, flags
, false);
4884 #if IS_ENABLED(CONFIG_IPV6)
4886 rc
= bpf_ipv6_fib_lookup(net
, params
, flags
, false);
4892 struct net_device
*dev
;
4894 dev
= dev_get_by_index_rcu(net
, params
->ifindex
);
4895 if (!is_skb_forwardable(dev
, skb
))
4896 rc
= BPF_FIB_LKUP_RET_FRAG_NEEDED
;
4902 static const struct bpf_func_proto bpf_skb_fib_lookup_proto
= {
4903 .func
= bpf_skb_fib_lookup
,
4905 .ret_type
= RET_INTEGER
,
4906 .arg1_type
= ARG_PTR_TO_CTX
,
4907 .arg2_type
= ARG_PTR_TO_MEM
,
4908 .arg3_type
= ARG_CONST_SIZE
,
4909 .arg4_type
= ARG_ANYTHING
,
4912 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4913 static int bpf_push_seg6_encap(struct sk_buff
*skb
, u32 type
, void *hdr
, u32 len
)
4916 struct ipv6_sr_hdr
*srh
= (struct ipv6_sr_hdr
*)hdr
;
4918 if (!seg6_validate_srh(srh
, len
))
4922 case BPF_LWT_ENCAP_SEG6_INLINE
:
4923 if (skb
->protocol
!= htons(ETH_P_IPV6
))
4926 err
= seg6_do_srh_inline(skb
, srh
);
4928 case BPF_LWT_ENCAP_SEG6
:
4929 skb_reset_inner_headers(skb
);
4930 skb
->encapsulation
= 1;
4931 err
= seg6_do_srh_encap(skb
, srh
, IPPROTO_IPV6
);
4937 bpf_compute_data_pointers(skb
);
4941 ipv6_hdr(skb
)->payload_len
= htons(skb
->len
- sizeof(struct ipv6hdr
));
4942 skb_set_transport_header(skb
, sizeof(struct ipv6hdr
));
4944 return seg6_lookup_nexthop(skb
, NULL
, 0);
4946 #endif /* CONFIG_IPV6_SEG6_BPF */
4948 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4949 static int bpf_push_ip_encap(struct sk_buff
*skb
, void *hdr
, u32 len
,
4952 return bpf_lwt_push_ip_encap(skb
, hdr
, len
, ingress
);
4956 BPF_CALL_4(bpf_lwt_in_push_encap
, struct sk_buff
*, skb
, u32
, type
, void *, hdr
,
4960 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4961 case BPF_LWT_ENCAP_SEG6
:
4962 case BPF_LWT_ENCAP_SEG6_INLINE
:
4963 return bpf_push_seg6_encap(skb
, type
, hdr
, len
);
4965 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4966 case BPF_LWT_ENCAP_IP
:
4967 return bpf_push_ip_encap(skb
, hdr
, len
, true /* ingress */);
4974 BPF_CALL_4(bpf_lwt_xmit_push_encap
, struct sk_buff
*, skb
, u32
, type
,
4975 void *, hdr
, u32
, len
)
4978 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4979 case BPF_LWT_ENCAP_IP
:
4980 return bpf_push_ip_encap(skb
, hdr
, len
, false /* egress */);
4987 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto
= {
4988 .func
= bpf_lwt_in_push_encap
,
4990 .ret_type
= RET_INTEGER
,
4991 .arg1_type
= ARG_PTR_TO_CTX
,
4992 .arg2_type
= ARG_ANYTHING
,
4993 .arg3_type
= ARG_PTR_TO_MEM
,
4994 .arg4_type
= ARG_CONST_SIZE
4997 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto
= {
4998 .func
= bpf_lwt_xmit_push_encap
,
5000 .ret_type
= RET_INTEGER
,
5001 .arg1_type
= ARG_PTR_TO_CTX
,
5002 .arg2_type
= ARG_ANYTHING
,
5003 .arg3_type
= ARG_PTR_TO_MEM
,
5004 .arg4_type
= ARG_CONST_SIZE
5007 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5008 BPF_CALL_4(bpf_lwt_seg6_store_bytes
, struct sk_buff
*, skb
, u32
, offset
,
5009 const void *, from
, u32
, len
)
5011 struct seg6_bpf_srh_state
*srh_state
=
5012 this_cpu_ptr(&seg6_bpf_srh_states
);
5013 struct ipv6_sr_hdr
*srh
= srh_state
->srh
;
5014 void *srh_tlvs
, *srh_end
, *ptr
;
5020 srh_tlvs
= (void *)((char *)srh
+ ((srh
->first_segment
+ 1) << 4));
5021 srh_end
= (void *)((char *)srh
+ sizeof(*srh
) + srh_state
->hdrlen
);
5023 ptr
= skb
->data
+ offset
;
5024 if (ptr
>= srh_tlvs
&& ptr
+ len
<= srh_end
)
5025 srh_state
->valid
= false;
5026 else if (ptr
< (void *)&srh
->flags
||
5027 ptr
+ len
> (void *)&srh
->segments
)
5030 if (unlikely(bpf_try_make_writable(skb
, offset
+ len
)))
5032 if (ipv6_find_hdr(skb
, &srhoff
, IPPROTO_ROUTING
, NULL
, NULL
) < 0)
5034 srh_state
->srh
= (struct ipv6_sr_hdr
*)(skb
->data
+ srhoff
);
5036 memcpy(skb
->data
+ offset
, from
, len
);
5040 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto
= {
5041 .func
= bpf_lwt_seg6_store_bytes
,
5043 .ret_type
= RET_INTEGER
,
5044 .arg1_type
= ARG_PTR_TO_CTX
,
5045 .arg2_type
= ARG_ANYTHING
,
5046 .arg3_type
= ARG_PTR_TO_MEM
,
5047 .arg4_type
= ARG_CONST_SIZE
5050 static void bpf_update_srh_state(struct sk_buff
*skb
)
5052 struct seg6_bpf_srh_state
*srh_state
=
5053 this_cpu_ptr(&seg6_bpf_srh_states
);
5056 if (ipv6_find_hdr(skb
, &srhoff
, IPPROTO_ROUTING
, NULL
, NULL
) < 0) {
5057 srh_state
->srh
= NULL
;
5059 srh_state
->srh
= (struct ipv6_sr_hdr
*)(skb
->data
+ srhoff
);
5060 srh_state
->hdrlen
= srh_state
->srh
->hdrlen
<< 3;
5061 srh_state
->valid
= true;
5065 BPF_CALL_4(bpf_lwt_seg6_action
, struct sk_buff
*, skb
,
5066 u32
, action
, void *, param
, u32
, param_len
)
5068 struct seg6_bpf_srh_state
*srh_state
=
5069 this_cpu_ptr(&seg6_bpf_srh_states
);
5074 case SEG6_LOCAL_ACTION_END_X
:
5075 if (!seg6_bpf_has_valid_srh(skb
))
5077 if (param_len
!= sizeof(struct in6_addr
))
5079 return seg6_lookup_nexthop(skb
, (struct in6_addr
*)param
, 0);
5080 case SEG6_LOCAL_ACTION_END_T
:
5081 if (!seg6_bpf_has_valid_srh(skb
))
5083 if (param_len
!= sizeof(int))
5085 return seg6_lookup_nexthop(skb
, NULL
, *(int *)param
);
5086 case SEG6_LOCAL_ACTION_END_DT6
:
5087 if (!seg6_bpf_has_valid_srh(skb
))
5089 if (param_len
!= sizeof(int))
5092 if (ipv6_find_hdr(skb
, &hdroff
, IPPROTO_IPV6
, NULL
, NULL
) < 0)
5094 if (!pskb_pull(skb
, hdroff
))
5097 skb_postpull_rcsum(skb
, skb_network_header(skb
), hdroff
);
5098 skb_reset_network_header(skb
);
5099 skb_reset_transport_header(skb
);
5100 skb
->encapsulation
= 0;
5102 bpf_compute_data_pointers(skb
);
5103 bpf_update_srh_state(skb
);
5104 return seg6_lookup_nexthop(skb
, NULL
, *(int *)param
);
5105 case SEG6_LOCAL_ACTION_END_B6
:
5106 if (srh_state
->srh
&& !seg6_bpf_has_valid_srh(skb
))
5108 err
= bpf_push_seg6_encap(skb
, BPF_LWT_ENCAP_SEG6_INLINE
,
5111 bpf_update_srh_state(skb
);
5114 case SEG6_LOCAL_ACTION_END_B6_ENCAP
:
5115 if (srh_state
->srh
&& !seg6_bpf_has_valid_srh(skb
))
5117 err
= bpf_push_seg6_encap(skb
, BPF_LWT_ENCAP_SEG6
,
5120 bpf_update_srh_state(skb
);
5128 static const struct bpf_func_proto bpf_lwt_seg6_action_proto
= {
5129 .func
= bpf_lwt_seg6_action
,
5131 .ret_type
= RET_INTEGER
,
5132 .arg1_type
= ARG_PTR_TO_CTX
,
5133 .arg2_type
= ARG_ANYTHING
,
5134 .arg3_type
= ARG_PTR_TO_MEM
,
5135 .arg4_type
= ARG_CONST_SIZE
5138 BPF_CALL_3(bpf_lwt_seg6_adjust_srh
, struct sk_buff
*, skb
, u32
, offset
,
5141 struct seg6_bpf_srh_state
*srh_state
=
5142 this_cpu_ptr(&seg6_bpf_srh_states
);
5143 struct ipv6_sr_hdr
*srh
= srh_state
->srh
;
5144 void *srh_end
, *srh_tlvs
, *ptr
;
5145 struct ipv6hdr
*hdr
;
5149 if (unlikely(srh
== NULL
))
5152 srh_tlvs
= (void *)((unsigned char *)srh
+ sizeof(*srh
) +
5153 ((srh
->first_segment
+ 1) << 4));
5154 srh_end
= (void *)((unsigned char *)srh
+ sizeof(*srh
) +
5156 ptr
= skb
->data
+ offset
;
5158 if (unlikely(ptr
< srh_tlvs
|| ptr
> srh_end
))
5160 if (unlikely(len
< 0 && (void *)((char *)ptr
- len
) > srh_end
))
5164 ret
= skb_cow_head(skb
, len
);
5165 if (unlikely(ret
< 0))
5168 ret
= bpf_skb_net_hdr_push(skb
, offset
, len
);
5170 ret
= bpf_skb_net_hdr_pop(skb
, offset
, -1 * len
);
5173 bpf_compute_data_pointers(skb
);
5174 if (unlikely(ret
< 0))
5177 hdr
= (struct ipv6hdr
*)skb
->data
;
5178 hdr
->payload_len
= htons(skb
->len
- sizeof(struct ipv6hdr
));
5180 if (ipv6_find_hdr(skb
, &srhoff
, IPPROTO_ROUTING
, NULL
, NULL
) < 0)
5182 srh_state
->srh
= (struct ipv6_sr_hdr
*)(skb
->data
+ srhoff
);
5183 srh_state
->hdrlen
+= len
;
5184 srh_state
->valid
= false;
5188 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto
= {
5189 .func
= bpf_lwt_seg6_adjust_srh
,
5191 .ret_type
= RET_INTEGER
,
5192 .arg1_type
= ARG_PTR_TO_CTX
,
5193 .arg2_type
= ARG_ANYTHING
,
5194 .arg3_type
= ARG_ANYTHING
,
5196 #endif /* CONFIG_IPV6_SEG6_BPF */
5199 static struct sock
*sk_lookup(struct net
*net
, struct bpf_sock_tuple
*tuple
,
5200 int dif
, int sdif
, u8 family
, u8 proto
)
5202 bool refcounted
= false;
5203 struct sock
*sk
= NULL
;
5205 if (family
== AF_INET
) {
5206 __be32 src4
= tuple
->ipv4
.saddr
;
5207 __be32 dst4
= tuple
->ipv4
.daddr
;
5209 if (proto
== IPPROTO_TCP
)
5210 sk
= __inet_lookup(net
, &tcp_hashinfo
, NULL
, 0,
5211 src4
, tuple
->ipv4
.sport
,
5212 dst4
, tuple
->ipv4
.dport
,
5213 dif
, sdif
, &refcounted
);
5215 sk
= __udp4_lib_lookup(net
, src4
, tuple
->ipv4
.sport
,
5216 dst4
, tuple
->ipv4
.dport
,
5217 dif
, sdif
, &udp_table
, NULL
);
5218 #if IS_ENABLED(CONFIG_IPV6)
5220 struct in6_addr
*src6
= (struct in6_addr
*)&tuple
->ipv6
.saddr
;
5221 struct in6_addr
*dst6
= (struct in6_addr
*)&tuple
->ipv6
.daddr
;
5223 if (proto
== IPPROTO_TCP
)
5224 sk
= __inet6_lookup(net
, &tcp_hashinfo
, NULL
, 0,
5225 src6
, tuple
->ipv6
.sport
,
5226 dst6
, ntohs(tuple
->ipv6
.dport
),
5227 dif
, sdif
, &refcounted
);
5228 else if (likely(ipv6_bpf_stub
))
5229 sk
= ipv6_bpf_stub
->udp6_lib_lookup(net
,
5230 src6
, tuple
->ipv6
.sport
,
5231 dst6
, tuple
->ipv6
.dport
,
5237 if (unlikely(sk
&& !refcounted
&& !sock_flag(sk
, SOCK_RCU_FREE
))) {
5238 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5244 /* bpf_skc_lookup performs the core lookup for different types of sockets,
5245 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5246 * Returns the socket as an 'unsigned long' to simplify the casting in the
5247 * callers to satisfy BPF_CALL declarations.
5249 static struct sock
*
5250 __bpf_skc_lookup(struct sk_buff
*skb
, struct bpf_sock_tuple
*tuple
, u32 len
,
5251 struct net
*caller_net
, u32 ifindex
, u8 proto
, u64 netns_id
,
5254 struct sock
*sk
= NULL
;
5255 u8 family
= AF_UNSPEC
;
5259 if (len
== sizeof(tuple
->ipv4
))
5261 else if (len
== sizeof(tuple
->ipv6
))
5266 if (unlikely(family
== AF_UNSPEC
|| flags
||
5267 !((s32
)netns_id
< 0 || netns_id
<= S32_MAX
)))
5270 if (family
== AF_INET
)
5271 sdif
= inet_sdif(skb
);
5273 sdif
= inet6_sdif(skb
);
5275 if ((s32
)netns_id
< 0) {
5277 sk
= sk_lookup(net
, tuple
, ifindex
, sdif
, family
, proto
);
5279 net
= get_net_ns_by_id(caller_net
, netns_id
);
5282 sk
= sk_lookup(net
, tuple
, ifindex
, sdif
, family
, proto
);
5290 static struct sock
*
5291 __bpf_sk_lookup(struct sk_buff
*skb
, struct bpf_sock_tuple
*tuple
, u32 len
,
5292 struct net
*caller_net
, u32 ifindex
, u8 proto
, u64 netns_id
,
5295 struct sock
*sk
= __bpf_skc_lookup(skb
, tuple
, len
, caller_net
,
5296 ifindex
, proto
, netns_id
, flags
);
5299 sk
= sk_to_full_sk(sk
);
5300 if (!sk_fullsock(sk
)) {
5301 if (!sock_flag(sk
, SOCK_RCU_FREE
))
5310 static struct sock
*
5311 bpf_skc_lookup(struct sk_buff
*skb
, struct bpf_sock_tuple
*tuple
, u32 len
,
5312 u8 proto
, u64 netns_id
, u64 flags
)
5314 struct net
*caller_net
;
5318 caller_net
= dev_net(skb
->dev
);
5319 ifindex
= skb
->dev
->ifindex
;
5321 caller_net
= sock_net(skb
->sk
);
5325 return __bpf_skc_lookup(skb
, tuple
, len
, caller_net
, ifindex
, proto
,
5329 static struct sock
*
5330 bpf_sk_lookup(struct sk_buff
*skb
, struct bpf_sock_tuple
*tuple
, u32 len
,
5331 u8 proto
, u64 netns_id
, u64 flags
)
5333 struct sock
*sk
= bpf_skc_lookup(skb
, tuple
, len
, proto
, netns_id
,
5337 sk
= sk_to_full_sk(sk
);
5338 if (!sk_fullsock(sk
)) {
5339 if (!sock_flag(sk
, SOCK_RCU_FREE
))
5348 BPF_CALL_5(bpf_skc_lookup_tcp
, struct sk_buff
*, skb
,
5349 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5351 return (unsigned long)bpf_skc_lookup(skb
, tuple
, len
, IPPROTO_TCP
,
5355 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto
= {
5356 .func
= bpf_skc_lookup_tcp
,
5359 .ret_type
= RET_PTR_TO_SOCK_COMMON_OR_NULL
,
5360 .arg1_type
= ARG_PTR_TO_CTX
,
5361 .arg2_type
= ARG_PTR_TO_MEM
,
5362 .arg3_type
= ARG_CONST_SIZE
,
5363 .arg4_type
= ARG_ANYTHING
,
5364 .arg5_type
= ARG_ANYTHING
,
5367 BPF_CALL_5(bpf_sk_lookup_tcp
, struct sk_buff
*, skb
,
5368 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5370 return (unsigned long)bpf_sk_lookup(skb
, tuple
, len
, IPPROTO_TCP
,
5374 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto
= {
5375 .func
= bpf_sk_lookup_tcp
,
5378 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5379 .arg1_type
= ARG_PTR_TO_CTX
,
5380 .arg2_type
= ARG_PTR_TO_MEM
,
5381 .arg3_type
= ARG_CONST_SIZE
,
5382 .arg4_type
= ARG_ANYTHING
,
5383 .arg5_type
= ARG_ANYTHING
,
5386 BPF_CALL_5(bpf_sk_lookup_udp
, struct sk_buff
*, skb
,
5387 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5389 return (unsigned long)bpf_sk_lookup(skb
, tuple
, len
, IPPROTO_UDP
,
5393 static const struct bpf_func_proto bpf_sk_lookup_udp_proto
= {
5394 .func
= bpf_sk_lookup_udp
,
5397 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5398 .arg1_type
= ARG_PTR_TO_CTX
,
5399 .arg2_type
= ARG_PTR_TO_MEM
,
5400 .arg3_type
= ARG_CONST_SIZE
,
5401 .arg4_type
= ARG_ANYTHING
,
5402 .arg5_type
= ARG_ANYTHING
,
5405 BPF_CALL_1(bpf_sk_release
, struct sock
*, sk
)
5407 if (!sock_flag(sk
, SOCK_RCU_FREE
))
5412 static const struct bpf_func_proto bpf_sk_release_proto
= {
5413 .func
= bpf_sk_release
,
5415 .ret_type
= RET_INTEGER
,
5416 .arg1_type
= ARG_PTR_TO_SOCK_COMMON
,
5419 BPF_CALL_5(bpf_xdp_sk_lookup_udp
, struct xdp_buff
*, ctx
,
5420 struct bpf_sock_tuple
*, tuple
, u32
, len
, u32
, netns_id
, u64
, flags
)
5422 struct net
*caller_net
= dev_net(ctx
->rxq
->dev
);
5423 int ifindex
= ctx
->rxq
->dev
->ifindex
;
5425 return (unsigned long)__bpf_sk_lookup(NULL
, tuple
, len
, caller_net
,
5426 ifindex
, IPPROTO_UDP
, netns_id
,
5430 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto
= {
5431 .func
= bpf_xdp_sk_lookup_udp
,
5434 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5435 .arg1_type
= ARG_PTR_TO_CTX
,
5436 .arg2_type
= ARG_PTR_TO_MEM
,
5437 .arg3_type
= ARG_CONST_SIZE
,
5438 .arg4_type
= ARG_ANYTHING
,
5439 .arg5_type
= ARG_ANYTHING
,
5442 BPF_CALL_5(bpf_xdp_skc_lookup_tcp
, struct xdp_buff
*, ctx
,
5443 struct bpf_sock_tuple
*, tuple
, u32
, len
, u32
, netns_id
, u64
, flags
)
5445 struct net
*caller_net
= dev_net(ctx
->rxq
->dev
);
5446 int ifindex
= ctx
->rxq
->dev
->ifindex
;
5448 return (unsigned long)__bpf_skc_lookup(NULL
, tuple
, len
, caller_net
,
5449 ifindex
, IPPROTO_TCP
, netns_id
,
5453 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto
= {
5454 .func
= bpf_xdp_skc_lookup_tcp
,
5457 .ret_type
= RET_PTR_TO_SOCK_COMMON_OR_NULL
,
5458 .arg1_type
= ARG_PTR_TO_CTX
,
5459 .arg2_type
= ARG_PTR_TO_MEM
,
5460 .arg3_type
= ARG_CONST_SIZE
,
5461 .arg4_type
= ARG_ANYTHING
,
5462 .arg5_type
= ARG_ANYTHING
,
5465 BPF_CALL_5(bpf_xdp_sk_lookup_tcp
, struct xdp_buff
*, ctx
,
5466 struct bpf_sock_tuple
*, tuple
, u32
, len
, u32
, netns_id
, u64
, flags
)
5468 struct net
*caller_net
= dev_net(ctx
->rxq
->dev
);
5469 int ifindex
= ctx
->rxq
->dev
->ifindex
;
5471 return (unsigned long)__bpf_sk_lookup(NULL
, tuple
, len
, caller_net
,
5472 ifindex
, IPPROTO_TCP
, netns_id
,
5476 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto
= {
5477 .func
= bpf_xdp_sk_lookup_tcp
,
5480 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5481 .arg1_type
= ARG_PTR_TO_CTX
,
5482 .arg2_type
= ARG_PTR_TO_MEM
,
5483 .arg3_type
= ARG_CONST_SIZE
,
5484 .arg4_type
= ARG_ANYTHING
,
5485 .arg5_type
= ARG_ANYTHING
,
5488 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp
, struct bpf_sock_addr_kern
*, ctx
,
5489 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5491 return (unsigned long)__bpf_skc_lookup(NULL
, tuple
, len
,
5492 sock_net(ctx
->sk
), 0,
5493 IPPROTO_TCP
, netns_id
, flags
);
5496 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto
= {
5497 .func
= bpf_sock_addr_skc_lookup_tcp
,
5499 .ret_type
= RET_PTR_TO_SOCK_COMMON_OR_NULL
,
5500 .arg1_type
= ARG_PTR_TO_CTX
,
5501 .arg2_type
= ARG_PTR_TO_MEM
,
5502 .arg3_type
= ARG_CONST_SIZE
,
5503 .arg4_type
= ARG_ANYTHING
,
5504 .arg5_type
= ARG_ANYTHING
,
5507 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp
, struct bpf_sock_addr_kern
*, ctx
,
5508 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5510 return (unsigned long)__bpf_sk_lookup(NULL
, tuple
, len
,
5511 sock_net(ctx
->sk
), 0, IPPROTO_TCP
,
5515 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto
= {
5516 .func
= bpf_sock_addr_sk_lookup_tcp
,
5518 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5519 .arg1_type
= ARG_PTR_TO_CTX
,
5520 .arg2_type
= ARG_PTR_TO_MEM
,
5521 .arg3_type
= ARG_CONST_SIZE
,
5522 .arg4_type
= ARG_ANYTHING
,
5523 .arg5_type
= ARG_ANYTHING
,
5526 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp
, struct bpf_sock_addr_kern
*, ctx
,
5527 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5529 return (unsigned long)__bpf_sk_lookup(NULL
, tuple
, len
,
5530 sock_net(ctx
->sk
), 0, IPPROTO_UDP
,
5534 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto
= {
5535 .func
= bpf_sock_addr_sk_lookup_udp
,
5537 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5538 .arg1_type
= ARG_PTR_TO_CTX
,
5539 .arg2_type
= ARG_PTR_TO_MEM
,
5540 .arg3_type
= ARG_CONST_SIZE
,
5541 .arg4_type
= ARG_ANYTHING
,
5542 .arg5_type
= ARG_ANYTHING
,
5545 bool bpf_tcp_sock_is_valid_access(int off
, int size
, enum bpf_access_type type
,
5546 struct bpf_insn_access_aux
*info
)
5548 if (off
< 0 || off
>= offsetofend(struct bpf_tcp_sock
,
5552 if (off
% size
!= 0)
5556 case offsetof(struct bpf_tcp_sock
, bytes_received
):
5557 case offsetof(struct bpf_tcp_sock
, bytes_acked
):
5558 return size
== sizeof(__u64
);
5560 return size
== sizeof(__u32
);
5564 u32
bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type
,
5565 const struct bpf_insn
*si
,
5566 struct bpf_insn
*insn_buf
,
5567 struct bpf_prog
*prog
, u32
*target_size
)
5569 struct bpf_insn
*insn
= insn_buf
;
5571 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
5573 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD) > \
5574 FIELD_SIZEOF(struct bpf_tcp_sock, FIELD)); \
5575 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
5576 si->dst_reg, si->src_reg, \
5577 offsetof(struct tcp_sock, FIELD)); \
5580 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
5582 BUILD_BUG_ON(FIELD_SIZEOF(struct inet_connection_sock, \
5584 FIELD_SIZEOF(struct bpf_tcp_sock, FIELD)); \
5585 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
5586 struct inet_connection_sock, \
5588 si->dst_reg, si->src_reg, \
5590 struct inet_connection_sock, \
5594 if (insn
> insn_buf
)
5595 return insn
- insn_buf
;
5598 case offsetof(struct bpf_tcp_sock
, rtt_min
):
5599 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock
, rtt_min
) !=
5600 sizeof(struct minmax
));
5601 BUILD_BUG_ON(sizeof(struct minmax
) <
5602 sizeof(struct minmax_sample
));
5604 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
5605 offsetof(struct tcp_sock
, rtt_min
) +
5606 offsetof(struct minmax_sample
, v
));
5608 case offsetof(struct bpf_tcp_sock
, snd_cwnd
):
5609 BPF_TCP_SOCK_GET_COMMON(snd_cwnd
);
5611 case offsetof(struct bpf_tcp_sock
, srtt_us
):
5612 BPF_TCP_SOCK_GET_COMMON(srtt_us
);
5614 case offsetof(struct bpf_tcp_sock
, snd_ssthresh
):
5615 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh
);
5617 case offsetof(struct bpf_tcp_sock
, rcv_nxt
):
5618 BPF_TCP_SOCK_GET_COMMON(rcv_nxt
);
5620 case offsetof(struct bpf_tcp_sock
, snd_nxt
):
5621 BPF_TCP_SOCK_GET_COMMON(snd_nxt
);
5623 case offsetof(struct bpf_tcp_sock
, snd_una
):
5624 BPF_TCP_SOCK_GET_COMMON(snd_una
);
5626 case offsetof(struct bpf_tcp_sock
, mss_cache
):
5627 BPF_TCP_SOCK_GET_COMMON(mss_cache
);
5629 case offsetof(struct bpf_tcp_sock
, ecn_flags
):
5630 BPF_TCP_SOCK_GET_COMMON(ecn_flags
);
5632 case offsetof(struct bpf_tcp_sock
, rate_delivered
):
5633 BPF_TCP_SOCK_GET_COMMON(rate_delivered
);
5635 case offsetof(struct bpf_tcp_sock
, rate_interval_us
):
5636 BPF_TCP_SOCK_GET_COMMON(rate_interval_us
);
5638 case offsetof(struct bpf_tcp_sock
, packets_out
):
5639 BPF_TCP_SOCK_GET_COMMON(packets_out
);
5641 case offsetof(struct bpf_tcp_sock
, retrans_out
):
5642 BPF_TCP_SOCK_GET_COMMON(retrans_out
);
5644 case offsetof(struct bpf_tcp_sock
, total_retrans
):
5645 BPF_TCP_SOCK_GET_COMMON(total_retrans
);
5647 case offsetof(struct bpf_tcp_sock
, segs_in
):
5648 BPF_TCP_SOCK_GET_COMMON(segs_in
);
5650 case offsetof(struct bpf_tcp_sock
, data_segs_in
):
5651 BPF_TCP_SOCK_GET_COMMON(data_segs_in
);
5653 case offsetof(struct bpf_tcp_sock
, segs_out
):
5654 BPF_TCP_SOCK_GET_COMMON(segs_out
);
5656 case offsetof(struct bpf_tcp_sock
, data_segs_out
):
5657 BPF_TCP_SOCK_GET_COMMON(data_segs_out
);
5659 case offsetof(struct bpf_tcp_sock
, lost_out
):
5660 BPF_TCP_SOCK_GET_COMMON(lost_out
);
5662 case offsetof(struct bpf_tcp_sock
, sacked_out
):
5663 BPF_TCP_SOCK_GET_COMMON(sacked_out
);
5665 case offsetof(struct bpf_tcp_sock
, bytes_received
):
5666 BPF_TCP_SOCK_GET_COMMON(bytes_received
);
5668 case offsetof(struct bpf_tcp_sock
, bytes_acked
):
5669 BPF_TCP_SOCK_GET_COMMON(bytes_acked
);
5671 case offsetof(struct bpf_tcp_sock
, dsack_dups
):
5672 BPF_TCP_SOCK_GET_COMMON(dsack_dups
);
5674 case offsetof(struct bpf_tcp_sock
, delivered
):
5675 BPF_TCP_SOCK_GET_COMMON(delivered
);
5677 case offsetof(struct bpf_tcp_sock
, delivered_ce
):
5678 BPF_TCP_SOCK_GET_COMMON(delivered_ce
);
5680 case offsetof(struct bpf_tcp_sock
, icsk_retransmits
):
5681 BPF_INET_SOCK_GET_COMMON(icsk_retransmits
);
5685 return insn
- insn_buf
;
5688 BPF_CALL_1(bpf_tcp_sock
, struct sock
*, sk
)
5690 if (sk_fullsock(sk
) && sk
->sk_protocol
== IPPROTO_TCP
)
5691 return (unsigned long)sk
;
5693 return (unsigned long)NULL
;
5696 const struct bpf_func_proto bpf_tcp_sock_proto
= {
5697 .func
= bpf_tcp_sock
,
5699 .ret_type
= RET_PTR_TO_TCP_SOCK_OR_NULL
,
5700 .arg1_type
= ARG_PTR_TO_SOCK_COMMON
,
5703 BPF_CALL_1(bpf_get_listener_sock
, struct sock
*, sk
)
5705 sk
= sk_to_full_sk(sk
);
5707 if (sk
->sk_state
== TCP_LISTEN
&& sock_flag(sk
, SOCK_RCU_FREE
))
5708 return (unsigned long)sk
;
5710 return (unsigned long)NULL
;
5713 static const struct bpf_func_proto bpf_get_listener_sock_proto
= {
5714 .func
= bpf_get_listener_sock
,
5716 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5717 .arg1_type
= ARG_PTR_TO_SOCK_COMMON
,
5720 BPF_CALL_1(bpf_skb_ecn_set_ce
, struct sk_buff
*, skb
)
5722 unsigned int iphdr_len
;
5724 if (skb
->protocol
== cpu_to_be16(ETH_P_IP
))
5725 iphdr_len
= sizeof(struct iphdr
);
5726 else if (skb
->protocol
== cpu_to_be16(ETH_P_IPV6
))
5727 iphdr_len
= sizeof(struct ipv6hdr
);
5731 if (skb_headlen(skb
) < iphdr_len
)
5734 if (skb_cloned(skb
) && !skb_clone_writable(skb
, iphdr_len
))
5737 return INET_ECN_set_ce(skb
);
5740 bool bpf_xdp_sock_is_valid_access(int off
, int size
, enum bpf_access_type type
,
5741 struct bpf_insn_access_aux
*info
)
5743 if (off
< 0 || off
>= offsetofend(struct bpf_xdp_sock
, queue_id
))
5746 if (off
% size
!= 0)
5751 return size
== sizeof(__u32
);
5755 u32
bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type
,
5756 const struct bpf_insn
*si
,
5757 struct bpf_insn
*insn_buf
,
5758 struct bpf_prog
*prog
, u32
*target_size
)
5760 struct bpf_insn
*insn
= insn_buf
;
5762 #define BPF_XDP_SOCK_GET(FIELD) \
5764 BUILD_BUG_ON(FIELD_SIZEOF(struct xdp_sock, FIELD) > \
5765 FIELD_SIZEOF(struct bpf_xdp_sock, FIELD)); \
5766 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
5767 si->dst_reg, si->src_reg, \
5768 offsetof(struct xdp_sock, FIELD)); \
5772 case offsetof(struct bpf_xdp_sock
, queue_id
):
5773 BPF_XDP_SOCK_GET(queue_id
);
5777 return insn
- insn_buf
;
5780 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto
= {
5781 .func
= bpf_skb_ecn_set_ce
,
5783 .ret_type
= RET_INTEGER
,
5784 .arg1_type
= ARG_PTR_TO_CTX
,
5787 BPF_CALL_5(bpf_tcp_check_syncookie
, struct sock
*, sk
, void *, iph
, u32
, iph_len
,
5788 struct tcphdr
*, th
, u32
, th_len
)
5790 #ifdef CONFIG_SYN_COOKIES
5794 if (unlikely(th_len
< sizeof(*th
)))
5797 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
5798 if (sk
->sk_protocol
!= IPPROTO_TCP
|| sk
->sk_state
!= TCP_LISTEN
)
5801 if (!sock_net(sk
)->ipv4
.sysctl_tcp_syncookies
)
5804 if (!th
->ack
|| th
->rst
|| th
->syn
)
5807 if (tcp_synq_no_recent_overflow(sk
))
5810 cookie
= ntohl(th
->ack_seq
) - 1;
5812 switch (sk
->sk_family
) {
5814 if (unlikely(iph_len
< sizeof(struct iphdr
)))
5817 ret
= __cookie_v4_check((struct iphdr
*)iph
, th
, cookie
);
5820 #if IS_BUILTIN(CONFIG_IPV6)
5822 if (unlikely(iph_len
< sizeof(struct ipv6hdr
)))
5825 ret
= __cookie_v6_check((struct ipv6hdr
*)iph
, th
, cookie
);
5827 #endif /* CONFIG_IPV6 */
5830 return -EPROTONOSUPPORT
;
5842 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto
= {
5843 .func
= bpf_tcp_check_syncookie
,
5846 .ret_type
= RET_INTEGER
,
5847 .arg1_type
= ARG_PTR_TO_SOCK_COMMON
,
5848 .arg2_type
= ARG_PTR_TO_MEM
,
5849 .arg3_type
= ARG_CONST_SIZE
,
5850 .arg4_type
= ARG_PTR_TO_MEM
,
5851 .arg5_type
= ARG_CONST_SIZE
,
5854 #endif /* CONFIG_INET */
5856 bool bpf_helper_changes_pkt_data(void *func
)
5858 if (func
== bpf_skb_vlan_push
||
5859 func
== bpf_skb_vlan_pop
||
5860 func
== bpf_skb_store_bytes
||
5861 func
== bpf_skb_change_proto
||
5862 func
== bpf_skb_change_head
||
5863 func
== sk_skb_change_head
||
5864 func
== bpf_skb_change_tail
||
5865 func
== sk_skb_change_tail
||
5866 func
== bpf_skb_adjust_room
||
5867 func
== bpf_skb_pull_data
||
5868 func
== sk_skb_pull_data
||
5869 func
== bpf_clone_redirect
||
5870 func
== bpf_l3_csum_replace
||
5871 func
== bpf_l4_csum_replace
||
5872 func
== bpf_xdp_adjust_head
||
5873 func
== bpf_xdp_adjust_meta
||
5874 func
== bpf_msg_pull_data
||
5875 func
== bpf_msg_push_data
||
5876 func
== bpf_msg_pop_data
||
5877 func
== bpf_xdp_adjust_tail
||
5878 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5879 func
== bpf_lwt_seg6_store_bytes
||
5880 func
== bpf_lwt_seg6_adjust_srh
||
5881 func
== bpf_lwt_seg6_action
||
5883 func
== bpf_lwt_in_push_encap
||
5884 func
== bpf_lwt_xmit_push_encap
)
5890 static const struct bpf_func_proto
*
5891 bpf_base_func_proto(enum bpf_func_id func_id
)
5894 case BPF_FUNC_map_lookup_elem
:
5895 return &bpf_map_lookup_elem_proto
;
5896 case BPF_FUNC_map_update_elem
:
5897 return &bpf_map_update_elem_proto
;
5898 case BPF_FUNC_map_delete_elem
:
5899 return &bpf_map_delete_elem_proto
;
5900 case BPF_FUNC_map_push_elem
:
5901 return &bpf_map_push_elem_proto
;
5902 case BPF_FUNC_map_pop_elem
:
5903 return &bpf_map_pop_elem_proto
;
5904 case BPF_FUNC_map_peek_elem
:
5905 return &bpf_map_peek_elem_proto
;
5906 case BPF_FUNC_get_prandom_u32
:
5907 return &bpf_get_prandom_u32_proto
;
5908 case BPF_FUNC_get_smp_processor_id
:
5909 return &bpf_get_raw_smp_processor_id_proto
;
5910 case BPF_FUNC_get_numa_node_id
:
5911 return &bpf_get_numa_node_id_proto
;
5912 case BPF_FUNC_tail_call
:
5913 return &bpf_tail_call_proto
;
5914 case BPF_FUNC_ktime_get_ns
:
5915 return &bpf_ktime_get_ns_proto
;
5920 if (!capable(CAP_SYS_ADMIN
))
5924 case BPF_FUNC_spin_lock
:
5925 return &bpf_spin_lock_proto
;
5926 case BPF_FUNC_spin_unlock
:
5927 return &bpf_spin_unlock_proto
;
5928 case BPF_FUNC_trace_printk
:
5929 return bpf_get_trace_printk_proto();
5935 static const struct bpf_func_proto
*
5936 sock_filter_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5939 /* inet and inet6 sockets are created in a process
5940 * context so there is always a valid uid/gid
5942 case BPF_FUNC_get_current_uid_gid
:
5943 return &bpf_get_current_uid_gid_proto
;
5944 case BPF_FUNC_get_local_storage
:
5945 return &bpf_get_local_storage_proto
;
5947 return bpf_base_func_proto(func_id
);
5951 static const struct bpf_func_proto
*
5952 sock_addr_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5955 /* inet and inet6 sockets are created in a process
5956 * context so there is always a valid uid/gid
5958 case BPF_FUNC_get_current_uid_gid
:
5959 return &bpf_get_current_uid_gid_proto
;
5961 switch (prog
->expected_attach_type
) {
5962 case BPF_CGROUP_INET4_CONNECT
:
5963 case BPF_CGROUP_INET6_CONNECT
:
5964 return &bpf_bind_proto
;
5968 case BPF_FUNC_get_socket_cookie
:
5969 return &bpf_get_socket_cookie_sock_addr_proto
;
5970 case BPF_FUNC_get_local_storage
:
5971 return &bpf_get_local_storage_proto
;
5973 case BPF_FUNC_sk_lookup_tcp
:
5974 return &bpf_sock_addr_sk_lookup_tcp_proto
;
5975 case BPF_FUNC_sk_lookup_udp
:
5976 return &bpf_sock_addr_sk_lookup_udp_proto
;
5977 case BPF_FUNC_sk_release
:
5978 return &bpf_sk_release_proto
;
5979 case BPF_FUNC_skc_lookup_tcp
:
5980 return &bpf_sock_addr_skc_lookup_tcp_proto
;
5981 #endif /* CONFIG_INET */
5982 case BPF_FUNC_sk_storage_get
:
5983 return &bpf_sk_storage_get_proto
;
5984 case BPF_FUNC_sk_storage_delete
:
5985 return &bpf_sk_storage_delete_proto
;
5987 return bpf_base_func_proto(func_id
);
5991 static const struct bpf_func_proto
*
5992 sk_filter_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5995 case BPF_FUNC_skb_load_bytes
:
5996 return &bpf_skb_load_bytes_proto
;
5997 case BPF_FUNC_skb_load_bytes_relative
:
5998 return &bpf_skb_load_bytes_relative_proto
;
5999 case BPF_FUNC_get_socket_cookie
:
6000 return &bpf_get_socket_cookie_proto
;
6001 case BPF_FUNC_get_socket_uid
:
6002 return &bpf_get_socket_uid_proto
;
6004 return bpf_base_func_proto(func_id
);
6008 const struct bpf_func_proto bpf_sk_storage_get_proto __weak
;
6009 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak
;
6011 static const struct bpf_func_proto
*
6012 cg_skb_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6015 case BPF_FUNC_get_local_storage
:
6016 return &bpf_get_local_storage_proto
;
6017 case BPF_FUNC_sk_fullsock
:
6018 return &bpf_sk_fullsock_proto
;
6019 case BPF_FUNC_sk_storage_get
:
6020 return &bpf_sk_storage_get_proto
;
6021 case BPF_FUNC_sk_storage_delete
:
6022 return &bpf_sk_storage_delete_proto
;
6023 #ifdef CONFIG_SOCK_CGROUP_DATA
6024 case BPF_FUNC_skb_cgroup_id
:
6025 return &bpf_skb_cgroup_id_proto
;
6028 case BPF_FUNC_tcp_sock
:
6029 return &bpf_tcp_sock_proto
;
6030 case BPF_FUNC_get_listener_sock
:
6031 return &bpf_get_listener_sock_proto
;
6032 case BPF_FUNC_skb_ecn_set_ce
:
6033 return &bpf_skb_ecn_set_ce_proto
;
6036 return sk_filter_func_proto(func_id
, prog
);
6040 static const struct bpf_func_proto
*
6041 tc_cls_act_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6044 case BPF_FUNC_skb_store_bytes
:
6045 return &bpf_skb_store_bytes_proto
;
6046 case BPF_FUNC_skb_load_bytes
:
6047 return &bpf_skb_load_bytes_proto
;
6048 case BPF_FUNC_skb_load_bytes_relative
:
6049 return &bpf_skb_load_bytes_relative_proto
;
6050 case BPF_FUNC_skb_pull_data
:
6051 return &bpf_skb_pull_data_proto
;
6052 case BPF_FUNC_csum_diff
:
6053 return &bpf_csum_diff_proto
;
6054 case BPF_FUNC_csum_update
:
6055 return &bpf_csum_update_proto
;
6056 case BPF_FUNC_l3_csum_replace
:
6057 return &bpf_l3_csum_replace_proto
;
6058 case BPF_FUNC_l4_csum_replace
:
6059 return &bpf_l4_csum_replace_proto
;
6060 case BPF_FUNC_clone_redirect
:
6061 return &bpf_clone_redirect_proto
;
6062 case BPF_FUNC_get_cgroup_classid
:
6063 return &bpf_get_cgroup_classid_proto
;
6064 case BPF_FUNC_skb_vlan_push
:
6065 return &bpf_skb_vlan_push_proto
;
6066 case BPF_FUNC_skb_vlan_pop
:
6067 return &bpf_skb_vlan_pop_proto
;
6068 case BPF_FUNC_skb_change_proto
:
6069 return &bpf_skb_change_proto_proto
;
6070 case BPF_FUNC_skb_change_type
:
6071 return &bpf_skb_change_type_proto
;
6072 case BPF_FUNC_skb_adjust_room
:
6073 return &bpf_skb_adjust_room_proto
;
6074 case BPF_FUNC_skb_change_tail
:
6075 return &bpf_skb_change_tail_proto
;
6076 case BPF_FUNC_skb_get_tunnel_key
:
6077 return &bpf_skb_get_tunnel_key_proto
;
6078 case BPF_FUNC_skb_set_tunnel_key
:
6079 return bpf_get_skb_set_tunnel_proto(func_id
);
6080 case BPF_FUNC_skb_get_tunnel_opt
:
6081 return &bpf_skb_get_tunnel_opt_proto
;
6082 case BPF_FUNC_skb_set_tunnel_opt
:
6083 return bpf_get_skb_set_tunnel_proto(func_id
);
6084 case BPF_FUNC_redirect
:
6085 return &bpf_redirect_proto
;
6086 case BPF_FUNC_get_route_realm
:
6087 return &bpf_get_route_realm_proto
;
6088 case BPF_FUNC_get_hash_recalc
:
6089 return &bpf_get_hash_recalc_proto
;
6090 case BPF_FUNC_set_hash_invalid
:
6091 return &bpf_set_hash_invalid_proto
;
6092 case BPF_FUNC_set_hash
:
6093 return &bpf_set_hash_proto
;
6094 case BPF_FUNC_perf_event_output
:
6095 return &bpf_skb_event_output_proto
;
6096 case BPF_FUNC_get_smp_processor_id
:
6097 return &bpf_get_smp_processor_id_proto
;
6098 case BPF_FUNC_skb_under_cgroup
:
6099 return &bpf_skb_under_cgroup_proto
;
6100 case BPF_FUNC_get_socket_cookie
:
6101 return &bpf_get_socket_cookie_proto
;
6102 case BPF_FUNC_get_socket_uid
:
6103 return &bpf_get_socket_uid_proto
;
6104 case BPF_FUNC_fib_lookup
:
6105 return &bpf_skb_fib_lookup_proto
;
6106 case BPF_FUNC_sk_fullsock
:
6107 return &bpf_sk_fullsock_proto
;
6108 case BPF_FUNC_sk_storage_get
:
6109 return &bpf_sk_storage_get_proto
;
6110 case BPF_FUNC_sk_storage_delete
:
6111 return &bpf_sk_storage_delete_proto
;
6113 case BPF_FUNC_skb_get_xfrm_state
:
6114 return &bpf_skb_get_xfrm_state_proto
;
6116 #ifdef CONFIG_SOCK_CGROUP_DATA
6117 case BPF_FUNC_skb_cgroup_id
:
6118 return &bpf_skb_cgroup_id_proto
;
6119 case BPF_FUNC_skb_ancestor_cgroup_id
:
6120 return &bpf_skb_ancestor_cgroup_id_proto
;
6123 case BPF_FUNC_sk_lookup_tcp
:
6124 return &bpf_sk_lookup_tcp_proto
;
6125 case BPF_FUNC_sk_lookup_udp
:
6126 return &bpf_sk_lookup_udp_proto
;
6127 case BPF_FUNC_sk_release
:
6128 return &bpf_sk_release_proto
;
6129 case BPF_FUNC_tcp_sock
:
6130 return &bpf_tcp_sock_proto
;
6131 case BPF_FUNC_get_listener_sock
:
6132 return &bpf_get_listener_sock_proto
;
6133 case BPF_FUNC_skc_lookup_tcp
:
6134 return &bpf_skc_lookup_tcp_proto
;
6135 case BPF_FUNC_tcp_check_syncookie
:
6136 return &bpf_tcp_check_syncookie_proto
;
6137 case BPF_FUNC_skb_ecn_set_ce
:
6138 return &bpf_skb_ecn_set_ce_proto
;
6141 return bpf_base_func_proto(func_id
);
6145 static const struct bpf_func_proto
*
6146 xdp_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6149 case BPF_FUNC_perf_event_output
:
6150 return &bpf_xdp_event_output_proto
;
6151 case BPF_FUNC_get_smp_processor_id
:
6152 return &bpf_get_smp_processor_id_proto
;
6153 case BPF_FUNC_csum_diff
:
6154 return &bpf_csum_diff_proto
;
6155 case BPF_FUNC_xdp_adjust_head
:
6156 return &bpf_xdp_adjust_head_proto
;
6157 case BPF_FUNC_xdp_adjust_meta
:
6158 return &bpf_xdp_adjust_meta_proto
;
6159 case BPF_FUNC_redirect
:
6160 return &bpf_xdp_redirect_proto
;
6161 case BPF_FUNC_redirect_map
:
6162 return &bpf_xdp_redirect_map_proto
;
6163 case BPF_FUNC_xdp_adjust_tail
:
6164 return &bpf_xdp_adjust_tail_proto
;
6165 case BPF_FUNC_fib_lookup
:
6166 return &bpf_xdp_fib_lookup_proto
;
6168 case BPF_FUNC_sk_lookup_udp
:
6169 return &bpf_xdp_sk_lookup_udp_proto
;
6170 case BPF_FUNC_sk_lookup_tcp
:
6171 return &bpf_xdp_sk_lookup_tcp_proto
;
6172 case BPF_FUNC_sk_release
:
6173 return &bpf_sk_release_proto
;
6174 case BPF_FUNC_skc_lookup_tcp
:
6175 return &bpf_xdp_skc_lookup_tcp_proto
;
6176 case BPF_FUNC_tcp_check_syncookie
:
6177 return &bpf_tcp_check_syncookie_proto
;
6180 return bpf_base_func_proto(func_id
);
6184 const struct bpf_func_proto bpf_sock_map_update_proto __weak
;
6185 const struct bpf_func_proto bpf_sock_hash_update_proto __weak
;
6187 static const struct bpf_func_proto
*
6188 sock_ops_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6191 case BPF_FUNC_setsockopt
:
6192 return &bpf_setsockopt_proto
;
6193 case BPF_FUNC_getsockopt
:
6194 return &bpf_getsockopt_proto
;
6195 case BPF_FUNC_sock_ops_cb_flags_set
:
6196 return &bpf_sock_ops_cb_flags_set_proto
;
6197 case BPF_FUNC_sock_map_update
:
6198 return &bpf_sock_map_update_proto
;
6199 case BPF_FUNC_sock_hash_update
:
6200 return &bpf_sock_hash_update_proto
;
6201 case BPF_FUNC_get_socket_cookie
:
6202 return &bpf_get_socket_cookie_sock_ops_proto
;
6203 case BPF_FUNC_get_local_storage
:
6204 return &bpf_get_local_storage_proto
;
6205 case BPF_FUNC_perf_event_output
:
6206 return &bpf_sockopt_event_output_proto
;
6207 case BPF_FUNC_sk_storage_get
:
6208 return &bpf_sk_storage_get_proto
;
6209 case BPF_FUNC_sk_storage_delete
:
6210 return &bpf_sk_storage_delete_proto
;
6212 case BPF_FUNC_tcp_sock
:
6213 return &bpf_tcp_sock_proto
;
6214 #endif /* CONFIG_INET */
6216 return bpf_base_func_proto(func_id
);
6220 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak
;
6221 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak
;
6223 static const struct bpf_func_proto
*
6224 sk_msg_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6227 case BPF_FUNC_msg_redirect_map
:
6228 return &bpf_msg_redirect_map_proto
;
6229 case BPF_FUNC_msg_redirect_hash
:
6230 return &bpf_msg_redirect_hash_proto
;
6231 case BPF_FUNC_msg_apply_bytes
:
6232 return &bpf_msg_apply_bytes_proto
;
6233 case BPF_FUNC_msg_cork_bytes
:
6234 return &bpf_msg_cork_bytes_proto
;
6235 case BPF_FUNC_msg_pull_data
:
6236 return &bpf_msg_pull_data_proto
;
6237 case BPF_FUNC_msg_push_data
:
6238 return &bpf_msg_push_data_proto
;
6239 case BPF_FUNC_msg_pop_data
:
6240 return &bpf_msg_pop_data_proto
;
6242 return bpf_base_func_proto(func_id
);
6246 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak
;
6247 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak
;
6249 static const struct bpf_func_proto
*
6250 sk_skb_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6253 case BPF_FUNC_skb_store_bytes
:
6254 return &bpf_skb_store_bytes_proto
;
6255 case BPF_FUNC_skb_load_bytes
:
6256 return &bpf_skb_load_bytes_proto
;
6257 case BPF_FUNC_skb_pull_data
:
6258 return &sk_skb_pull_data_proto
;
6259 case BPF_FUNC_skb_change_tail
:
6260 return &sk_skb_change_tail_proto
;
6261 case BPF_FUNC_skb_change_head
:
6262 return &sk_skb_change_head_proto
;
6263 case BPF_FUNC_get_socket_cookie
:
6264 return &bpf_get_socket_cookie_proto
;
6265 case BPF_FUNC_get_socket_uid
:
6266 return &bpf_get_socket_uid_proto
;
6267 case BPF_FUNC_sk_redirect_map
:
6268 return &bpf_sk_redirect_map_proto
;
6269 case BPF_FUNC_sk_redirect_hash
:
6270 return &bpf_sk_redirect_hash_proto
;
6272 case BPF_FUNC_sk_lookup_tcp
:
6273 return &bpf_sk_lookup_tcp_proto
;
6274 case BPF_FUNC_sk_lookup_udp
:
6275 return &bpf_sk_lookup_udp_proto
;
6276 case BPF_FUNC_sk_release
:
6277 return &bpf_sk_release_proto
;
6278 case BPF_FUNC_skc_lookup_tcp
:
6279 return &bpf_skc_lookup_tcp_proto
;
6282 return bpf_base_func_proto(func_id
);
6286 static const struct bpf_func_proto
*
6287 flow_dissector_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6290 case BPF_FUNC_skb_load_bytes
:
6291 return &bpf_flow_dissector_load_bytes_proto
;
6293 return bpf_base_func_proto(func_id
);
6297 static const struct bpf_func_proto
*
6298 lwt_out_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6301 case BPF_FUNC_skb_load_bytes
:
6302 return &bpf_skb_load_bytes_proto
;
6303 case BPF_FUNC_skb_pull_data
:
6304 return &bpf_skb_pull_data_proto
;
6305 case BPF_FUNC_csum_diff
:
6306 return &bpf_csum_diff_proto
;
6307 case BPF_FUNC_get_cgroup_classid
:
6308 return &bpf_get_cgroup_classid_proto
;
6309 case BPF_FUNC_get_route_realm
:
6310 return &bpf_get_route_realm_proto
;
6311 case BPF_FUNC_get_hash_recalc
:
6312 return &bpf_get_hash_recalc_proto
;
6313 case BPF_FUNC_perf_event_output
:
6314 return &bpf_skb_event_output_proto
;
6315 case BPF_FUNC_get_smp_processor_id
:
6316 return &bpf_get_smp_processor_id_proto
;
6317 case BPF_FUNC_skb_under_cgroup
:
6318 return &bpf_skb_under_cgroup_proto
;
6320 return bpf_base_func_proto(func_id
);
6324 static const struct bpf_func_proto
*
6325 lwt_in_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6328 case BPF_FUNC_lwt_push_encap
:
6329 return &bpf_lwt_in_push_encap_proto
;
6331 return lwt_out_func_proto(func_id
, prog
);
6335 static const struct bpf_func_proto
*
6336 lwt_xmit_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6339 case BPF_FUNC_skb_get_tunnel_key
:
6340 return &bpf_skb_get_tunnel_key_proto
;
6341 case BPF_FUNC_skb_set_tunnel_key
:
6342 return bpf_get_skb_set_tunnel_proto(func_id
);
6343 case BPF_FUNC_skb_get_tunnel_opt
:
6344 return &bpf_skb_get_tunnel_opt_proto
;
6345 case BPF_FUNC_skb_set_tunnel_opt
:
6346 return bpf_get_skb_set_tunnel_proto(func_id
);
6347 case BPF_FUNC_redirect
:
6348 return &bpf_redirect_proto
;
6349 case BPF_FUNC_clone_redirect
:
6350 return &bpf_clone_redirect_proto
;
6351 case BPF_FUNC_skb_change_tail
:
6352 return &bpf_skb_change_tail_proto
;
6353 case BPF_FUNC_skb_change_head
:
6354 return &bpf_skb_change_head_proto
;
6355 case BPF_FUNC_skb_store_bytes
:
6356 return &bpf_skb_store_bytes_proto
;
6357 case BPF_FUNC_csum_update
:
6358 return &bpf_csum_update_proto
;
6359 case BPF_FUNC_l3_csum_replace
:
6360 return &bpf_l3_csum_replace_proto
;
6361 case BPF_FUNC_l4_csum_replace
:
6362 return &bpf_l4_csum_replace_proto
;
6363 case BPF_FUNC_set_hash_invalid
:
6364 return &bpf_set_hash_invalid_proto
;
6365 case BPF_FUNC_lwt_push_encap
:
6366 return &bpf_lwt_xmit_push_encap_proto
;
6368 return lwt_out_func_proto(func_id
, prog
);
6372 static const struct bpf_func_proto
*
6373 lwt_seg6local_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
6376 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6377 case BPF_FUNC_lwt_seg6_store_bytes
:
6378 return &bpf_lwt_seg6_store_bytes_proto
;
6379 case BPF_FUNC_lwt_seg6_action
:
6380 return &bpf_lwt_seg6_action_proto
;
6381 case BPF_FUNC_lwt_seg6_adjust_srh
:
6382 return &bpf_lwt_seg6_adjust_srh_proto
;
6385 return lwt_out_func_proto(func_id
, prog
);
6389 static bool bpf_skb_is_valid_access(int off
, int size
, enum bpf_access_type type
,
6390 const struct bpf_prog
*prog
,
6391 struct bpf_insn_access_aux
*info
)
6393 const int size_default
= sizeof(__u32
);
6395 if (off
< 0 || off
>= sizeof(struct __sk_buff
))
6398 /* The verifier guarantees that size > 0. */
6399 if (off
% size
!= 0)
6403 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6404 if (off
+ size
> offsetofend(struct __sk_buff
, cb
[4]))
6407 case bpf_ctx_range_till(struct __sk_buff
, remote_ip6
[0], remote_ip6
[3]):
6408 case bpf_ctx_range_till(struct __sk_buff
, local_ip6
[0], local_ip6
[3]):
6409 case bpf_ctx_range_till(struct __sk_buff
, remote_ip4
, remote_ip4
):
6410 case bpf_ctx_range_till(struct __sk_buff
, local_ip4
, local_ip4
):
6411 case bpf_ctx_range(struct __sk_buff
, data
):
6412 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6413 case bpf_ctx_range(struct __sk_buff
, data_end
):
6414 if (size
!= size_default
)
6417 case bpf_ctx_range_ptr(struct __sk_buff
, flow_keys
):
6419 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6420 if (size
!= sizeof(__u64
))
6423 case offsetof(struct __sk_buff
, sk
):
6424 if (type
== BPF_WRITE
|| size
!= sizeof(__u64
))
6426 info
->reg_type
= PTR_TO_SOCK_COMMON_OR_NULL
;
6429 /* Only narrow read access allowed for now. */
6430 if (type
== BPF_WRITE
) {
6431 if (size
!= size_default
)
6434 bpf_ctx_record_field_size(info
, size_default
);
6435 if (!bpf_ctx_narrow_access_ok(off
, size
, size_default
))
6443 static bool sk_filter_is_valid_access(int off
, int size
,
6444 enum bpf_access_type type
,
6445 const struct bpf_prog
*prog
,
6446 struct bpf_insn_access_aux
*info
)
6449 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6450 case bpf_ctx_range(struct __sk_buff
, data
):
6451 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6452 case bpf_ctx_range(struct __sk_buff
, data_end
):
6453 case bpf_ctx_range_till(struct __sk_buff
, family
, local_port
):
6454 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6455 case bpf_ctx_range(struct __sk_buff
, wire_len
):
6459 if (type
== BPF_WRITE
) {
6461 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6468 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6471 static bool cg_skb_is_valid_access(int off
, int size
,
6472 enum bpf_access_type type
,
6473 const struct bpf_prog
*prog
,
6474 struct bpf_insn_access_aux
*info
)
6477 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6478 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6479 case bpf_ctx_range(struct __sk_buff
, wire_len
):
6481 case bpf_ctx_range(struct __sk_buff
, data
):
6482 case bpf_ctx_range(struct __sk_buff
, data_end
):
6483 if (!capable(CAP_SYS_ADMIN
))
6488 if (type
== BPF_WRITE
) {
6490 case bpf_ctx_range(struct __sk_buff
, mark
):
6491 case bpf_ctx_range(struct __sk_buff
, priority
):
6492 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6494 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6495 if (!capable(CAP_SYS_ADMIN
))
6504 case bpf_ctx_range(struct __sk_buff
, data
):
6505 info
->reg_type
= PTR_TO_PACKET
;
6507 case bpf_ctx_range(struct __sk_buff
, data_end
):
6508 info
->reg_type
= PTR_TO_PACKET_END
;
6512 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6515 static bool lwt_is_valid_access(int off
, int size
,
6516 enum bpf_access_type type
,
6517 const struct bpf_prog
*prog
,
6518 struct bpf_insn_access_aux
*info
)
6521 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6522 case bpf_ctx_range_till(struct __sk_buff
, family
, local_port
):
6523 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6524 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6525 case bpf_ctx_range(struct __sk_buff
, wire_len
):
6529 if (type
== BPF_WRITE
) {
6531 case bpf_ctx_range(struct __sk_buff
, mark
):
6532 case bpf_ctx_range(struct __sk_buff
, priority
):
6533 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6541 case bpf_ctx_range(struct __sk_buff
, data
):
6542 info
->reg_type
= PTR_TO_PACKET
;
6544 case bpf_ctx_range(struct __sk_buff
, data_end
):
6545 info
->reg_type
= PTR_TO_PACKET_END
;
6549 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6552 /* Attach type specific accesses */
6553 static bool __sock_filter_check_attach_type(int off
,
6554 enum bpf_access_type access_type
,
6555 enum bpf_attach_type attach_type
)
6558 case offsetof(struct bpf_sock
, bound_dev_if
):
6559 case offsetof(struct bpf_sock
, mark
):
6560 case offsetof(struct bpf_sock
, priority
):
6561 switch (attach_type
) {
6562 case BPF_CGROUP_INET_SOCK_CREATE
:
6567 case bpf_ctx_range(struct bpf_sock
, src_ip4
):
6568 switch (attach_type
) {
6569 case BPF_CGROUP_INET4_POST_BIND
:
6574 case bpf_ctx_range_till(struct bpf_sock
, src_ip6
[0], src_ip6
[3]):
6575 switch (attach_type
) {
6576 case BPF_CGROUP_INET6_POST_BIND
:
6581 case bpf_ctx_range(struct bpf_sock
, src_port
):
6582 switch (attach_type
) {
6583 case BPF_CGROUP_INET4_POST_BIND
:
6584 case BPF_CGROUP_INET6_POST_BIND
:
6591 return access_type
== BPF_READ
;
6596 bool bpf_sock_common_is_valid_access(int off
, int size
,
6597 enum bpf_access_type type
,
6598 struct bpf_insn_access_aux
*info
)
6601 case bpf_ctx_range_till(struct bpf_sock
, type
, priority
):
6604 return bpf_sock_is_valid_access(off
, size
, type
, info
);
6608 bool bpf_sock_is_valid_access(int off
, int size
, enum bpf_access_type type
,
6609 struct bpf_insn_access_aux
*info
)
6611 const int size_default
= sizeof(__u32
);
6613 if (off
< 0 || off
>= sizeof(struct bpf_sock
))
6615 if (off
% size
!= 0)
6619 case offsetof(struct bpf_sock
, state
):
6620 case offsetof(struct bpf_sock
, family
):
6621 case offsetof(struct bpf_sock
, type
):
6622 case offsetof(struct bpf_sock
, protocol
):
6623 case offsetof(struct bpf_sock
, dst_port
):
6624 case offsetof(struct bpf_sock
, src_port
):
6625 case bpf_ctx_range(struct bpf_sock
, src_ip4
):
6626 case bpf_ctx_range_till(struct bpf_sock
, src_ip6
[0], src_ip6
[3]):
6627 case bpf_ctx_range(struct bpf_sock
, dst_ip4
):
6628 case bpf_ctx_range_till(struct bpf_sock
, dst_ip6
[0], dst_ip6
[3]):
6629 bpf_ctx_record_field_size(info
, size_default
);
6630 return bpf_ctx_narrow_access_ok(off
, size
, size_default
);
6633 return size
== size_default
;
6636 static bool sock_filter_is_valid_access(int off
, int size
,
6637 enum bpf_access_type type
,
6638 const struct bpf_prog
*prog
,
6639 struct bpf_insn_access_aux
*info
)
6641 if (!bpf_sock_is_valid_access(off
, size
, type
, info
))
6643 return __sock_filter_check_attach_type(off
, type
,
6644 prog
->expected_attach_type
);
6647 static int bpf_noop_prologue(struct bpf_insn
*insn_buf
, bool direct_write
,
6648 const struct bpf_prog
*prog
)
6650 /* Neither direct read nor direct write requires any preliminary
6656 static int bpf_unclone_prologue(struct bpf_insn
*insn_buf
, bool direct_write
,
6657 const struct bpf_prog
*prog
, int drop_verdict
)
6659 struct bpf_insn
*insn
= insn_buf
;
6664 /* if (!skb->cloned)
6667 * (Fast-path, otherwise approximation that we might be
6668 * a clone, do the rest in helper.)
6670 *insn
++ = BPF_LDX_MEM(BPF_B
, BPF_REG_6
, BPF_REG_1
, CLONED_OFFSET());
6671 *insn
++ = BPF_ALU32_IMM(BPF_AND
, BPF_REG_6
, CLONED_MASK
);
6672 *insn
++ = BPF_JMP_IMM(BPF_JEQ
, BPF_REG_6
, 0, 7);
6674 /* ret = bpf_skb_pull_data(skb, 0); */
6675 *insn
++ = BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
);
6676 *insn
++ = BPF_ALU64_REG(BPF_XOR
, BPF_REG_2
, BPF_REG_2
);
6677 *insn
++ = BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0,
6678 BPF_FUNC_skb_pull_data
);
6681 * return TC_ACT_SHOT;
6683 *insn
++ = BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 2);
6684 *insn
++ = BPF_ALU32_IMM(BPF_MOV
, BPF_REG_0
, drop_verdict
);
6685 *insn
++ = BPF_EXIT_INSN();
6688 *insn
++ = BPF_MOV64_REG(BPF_REG_1
, BPF_REG_6
);
6690 *insn
++ = prog
->insnsi
[0];
6692 return insn
- insn_buf
;
6695 static int bpf_gen_ld_abs(const struct bpf_insn
*orig
,
6696 struct bpf_insn
*insn_buf
)
6698 bool indirect
= BPF_MODE(orig
->code
) == BPF_IND
;
6699 struct bpf_insn
*insn
= insn_buf
;
6701 /* We're guaranteed here that CTX is in R6. */
6702 *insn
++ = BPF_MOV64_REG(BPF_REG_1
, BPF_REG_CTX
);
6704 *insn
++ = BPF_MOV64_IMM(BPF_REG_2
, orig
->imm
);
6706 *insn
++ = BPF_MOV64_REG(BPF_REG_2
, orig
->src_reg
);
6708 *insn
++ = BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, orig
->imm
);
6711 switch (BPF_SIZE(orig
->code
)) {
6713 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache
);
6716 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache
);
6719 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache
);
6723 *insn
++ = BPF_JMP_IMM(BPF_JSGE
, BPF_REG_0
, 0, 2);
6724 *insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_0
, BPF_REG_0
);
6725 *insn
++ = BPF_EXIT_INSN();
6727 return insn
- insn_buf
;
6730 static int tc_cls_act_prologue(struct bpf_insn
*insn_buf
, bool direct_write
,
6731 const struct bpf_prog
*prog
)
6733 return bpf_unclone_prologue(insn_buf
, direct_write
, prog
, TC_ACT_SHOT
);
6736 static bool tc_cls_act_is_valid_access(int off
, int size
,
6737 enum bpf_access_type type
,
6738 const struct bpf_prog
*prog
,
6739 struct bpf_insn_access_aux
*info
)
6741 if (type
== BPF_WRITE
) {
6743 case bpf_ctx_range(struct __sk_buff
, mark
):
6744 case bpf_ctx_range(struct __sk_buff
, tc_index
):
6745 case bpf_ctx_range(struct __sk_buff
, priority
):
6746 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6747 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6748 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6749 case bpf_ctx_range(struct __sk_buff
, queue_mapping
):
6757 case bpf_ctx_range(struct __sk_buff
, data
):
6758 info
->reg_type
= PTR_TO_PACKET
;
6760 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6761 info
->reg_type
= PTR_TO_PACKET_META
;
6763 case bpf_ctx_range(struct __sk_buff
, data_end
):
6764 info
->reg_type
= PTR_TO_PACKET_END
;
6766 case bpf_ctx_range_till(struct __sk_buff
, family
, local_port
):
6770 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6773 static bool __is_valid_xdp_access(int off
, int size
)
6775 if (off
< 0 || off
>= sizeof(struct xdp_md
))
6777 if (off
% size
!= 0)
6779 if (size
!= sizeof(__u32
))
6785 static bool xdp_is_valid_access(int off
, int size
,
6786 enum bpf_access_type type
,
6787 const struct bpf_prog
*prog
,
6788 struct bpf_insn_access_aux
*info
)
6790 if (type
== BPF_WRITE
) {
6791 if (bpf_prog_is_dev_bound(prog
->aux
)) {
6793 case offsetof(struct xdp_md
, rx_queue_index
):
6794 return __is_valid_xdp_access(off
, size
);
6801 case offsetof(struct xdp_md
, data
):
6802 info
->reg_type
= PTR_TO_PACKET
;
6804 case offsetof(struct xdp_md
, data_meta
):
6805 info
->reg_type
= PTR_TO_PACKET_META
;
6807 case offsetof(struct xdp_md
, data_end
):
6808 info
->reg_type
= PTR_TO_PACKET_END
;
6812 return __is_valid_xdp_access(off
, size
);
6815 void bpf_warn_invalid_xdp_action(u32 act
)
6817 const u32 act_max
= XDP_REDIRECT
;
6819 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
6820 act
> act_max
? "Illegal" : "Driver unsupported",
6823 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action
);
6825 static bool sock_addr_is_valid_access(int off
, int size
,
6826 enum bpf_access_type type
,
6827 const struct bpf_prog
*prog
,
6828 struct bpf_insn_access_aux
*info
)
6830 const int size_default
= sizeof(__u32
);
6832 if (off
< 0 || off
>= sizeof(struct bpf_sock_addr
))
6834 if (off
% size
!= 0)
6837 /* Disallow access to IPv6 fields from IPv4 contex and vise
6841 case bpf_ctx_range(struct bpf_sock_addr
, user_ip4
):
6842 switch (prog
->expected_attach_type
) {
6843 case BPF_CGROUP_INET4_BIND
:
6844 case BPF_CGROUP_INET4_CONNECT
:
6845 case BPF_CGROUP_UDP4_SENDMSG
:
6846 case BPF_CGROUP_UDP4_RECVMSG
:
6852 case bpf_ctx_range_till(struct bpf_sock_addr
, user_ip6
[0], user_ip6
[3]):
6853 switch (prog
->expected_attach_type
) {
6854 case BPF_CGROUP_INET6_BIND
:
6855 case BPF_CGROUP_INET6_CONNECT
:
6856 case BPF_CGROUP_UDP6_SENDMSG
:
6857 case BPF_CGROUP_UDP6_RECVMSG
:
6863 case bpf_ctx_range(struct bpf_sock_addr
, msg_src_ip4
):
6864 switch (prog
->expected_attach_type
) {
6865 case BPF_CGROUP_UDP4_SENDMSG
:
6871 case bpf_ctx_range_till(struct bpf_sock_addr
, msg_src_ip6
[0],
6873 switch (prog
->expected_attach_type
) {
6874 case BPF_CGROUP_UDP6_SENDMSG
:
6883 case bpf_ctx_range(struct bpf_sock_addr
, user_ip4
):
6884 case bpf_ctx_range_till(struct bpf_sock_addr
, user_ip6
[0], user_ip6
[3]):
6885 case bpf_ctx_range(struct bpf_sock_addr
, msg_src_ip4
):
6886 case bpf_ctx_range_till(struct bpf_sock_addr
, msg_src_ip6
[0],
6888 if (type
== BPF_READ
) {
6889 bpf_ctx_record_field_size(info
, size_default
);
6891 if (bpf_ctx_wide_access_ok(off
, size
,
6892 struct bpf_sock_addr
,
6896 if (bpf_ctx_wide_access_ok(off
, size
,
6897 struct bpf_sock_addr
,
6901 if (!bpf_ctx_narrow_access_ok(off
, size
, size_default
))
6904 if (bpf_ctx_wide_access_ok(off
, size
,
6905 struct bpf_sock_addr
,
6909 if (bpf_ctx_wide_access_ok(off
, size
,
6910 struct bpf_sock_addr
,
6914 if (size
!= size_default
)
6918 case bpf_ctx_range(struct bpf_sock_addr
, user_port
):
6919 if (size
!= size_default
)
6922 case offsetof(struct bpf_sock_addr
, sk
):
6923 if (type
!= BPF_READ
)
6925 if (size
!= sizeof(__u64
))
6927 info
->reg_type
= PTR_TO_SOCKET
;
6930 if (type
== BPF_READ
) {
6931 if (size
!= size_default
)
6941 static bool sock_ops_is_valid_access(int off
, int size
,
6942 enum bpf_access_type type
,
6943 const struct bpf_prog
*prog
,
6944 struct bpf_insn_access_aux
*info
)
6946 const int size_default
= sizeof(__u32
);
6948 if (off
< 0 || off
>= sizeof(struct bpf_sock_ops
))
6951 /* The verifier guarantees that size > 0. */
6952 if (off
% size
!= 0)
6955 if (type
== BPF_WRITE
) {
6957 case offsetof(struct bpf_sock_ops
, reply
):
6958 case offsetof(struct bpf_sock_ops
, sk_txhash
):
6959 if (size
!= size_default
)
6967 case bpf_ctx_range_till(struct bpf_sock_ops
, bytes_received
,
6969 if (size
!= sizeof(__u64
))
6972 case offsetof(struct bpf_sock_ops
, sk
):
6973 if (size
!= sizeof(__u64
))
6975 info
->reg_type
= PTR_TO_SOCKET_OR_NULL
;
6978 if (size
!= size_default
)
6987 static int sk_skb_prologue(struct bpf_insn
*insn_buf
, bool direct_write
,
6988 const struct bpf_prog
*prog
)
6990 return bpf_unclone_prologue(insn_buf
, direct_write
, prog
, SK_DROP
);
6993 static bool sk_skb_is_valid_access(int off
, int size
,
6994 enum bpf_access_type type
,
6995 const struct bpf_prog
*prog
,
6996 struct bpf_insn_access_aux
*info
)
6999 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
7000 case bpf_ctx_range(struct __sk_buff
, data_meta
):
7001 case bpf_ctx_range(struct __sk_buff
, tstamp
):
7002 case bpf_ctx_range(struct __sk_buff
, wire_len
):
7006 if (type
== BPF_WRITE
) {
7008 case bpf_ctx_range(struct __sk_buff
, tc_index
):
7009 case bpf_ctx_range(struct __sk_buff
, priority
):
7017 case bpf_ctx_range(struct __sk_buff
, mark
):
7019 case bpf_ctx_range(struct __sk_buff
, data
):
7020 info
->reg_type
= PTR_TO_PACKET
;
7022 case bpf_ctx_range(struct __sk_buff
, data_end
):
7023 info
->reg_type
= PTR_TO_PACKET_END
;
7027 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
7030 static bool sk_msg_is_valid_access(int off
, int size
,
7031 enum bpf_access_type type
,
7032 const struct bpf_prog
*prog
,
7033 struct bpf_insn_access_aux
*info
)
7035 if (type
== BPF_WRITE
)
7038 if (off
% size
!= 0)
7042 case offsetof(struct sk_msg_md
, data
):
7043 info
->reg_type
= PTR_TO_PACKET
;
7044 if (size
!= sizeof(__u64
))
7047 case offsetof(struct sk_msg_md
, data_end
):
7048 info
->reg_type
= PTR_TO_PACKET_END
;
7049 if (size
!= sizeof(__u64
))
7052 case bpf_ctx_range(struct sk_msg_md
, family
):
7053 case bpf_ctx_range(struct sk_msg_md
, remote_ip4
):
7054 case bpf_ctx_range(struct sk_msg_md
, local_ip4
):
7055 case bpf_ctx_range_till(struct sk_msg_md
, remote_ip6
[0], remote_ip6
[3]):
7056 case bpf_ctx_range_till(struct sk_msg_md
, local_ip6
[0], local_ip6
[3]):
7057 case bpf_ctx_range(struct sk_msg_md
, remote_port
):
7058 case bpf_ctx_range(struct sk_msg_md
, local_port
):
7059 case bpf_ctx_range(struct sk_msg_md
, size
):
7060 if (size
!= sizeof(__u32
))
7069 static bool flow_dissector_is_valid_access(int off
, int size
,
7070 enum bpf_access_type type
,
7071 const struct bpf_prog
*prog
,
7072 struct bpf_insn_access_aux
*info
)
7074 const int size_default
= sizeof(__u32
);
7076 if (off
< 0 || off
>= sizeof(struct __sk_buff
))
7079 if (type
== BPF_WRITE
)
7083 case bpf_ctx_range(struct __sk_buff
, data
):
7084 if (size
!= size_default
)
7086 info
->reg_type
= PTR_TO_PACKET
;
7088 case bpf_ctx_range(struct __sk_buff
, data_end
):
7089 if (size
!= size_default
)
7091 info
->reg_type
= PTR_TO_PACKET_END
;
7093 case bpf_ctx_range_ptr(struct __sk_buff
, flow_keys
):
7094 if (size
!= sizeof(__u64
))
7096 info
->reg_type
= PTR_TO_FLOW_KEYS
;
7103 static u32
flow_dissector_convert_ctx_access(enum bpf_access_type type
,
7104 const struct bpf_insn
*si
,
7105 struct bpf_insn
*insn_buf
,
7106 struct bpf_prog
*prog
,
7110 struct bpf_insn
*insn
= insn_buf
;
7113 case offsetof(struct __sk_buff
, data
):
7114 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector
, data
),
7115 si
->dst_reg
, si
->src_reg
,
7116 offsetof(struct bpf_flow_dissector
, data
));
7119 case offsetof(struct __sk_buff
, data_end
):
7120 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector
, data_end
),
7121 si
->dst_reg
, si
->src_reg
,
7122 offsetof(struct bpf_flow_dissector
, data_end
));
7125 case offsetof(struct __sk_buff
, flow_keys
):
7126 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector
, flow_keys
),
7127 si
->dst_reg
, si
->src_reg
,
7128 offsetof(struct bpf_flow_dissector
, flow_keys
));
7132 return insn
- insn_buf
;
7135 static u32
bpf_convert_ctx_access(enum bpf_access_type type
,
7136 const struct bpf_insn
*si
,
7137 struct bpf_insn
*insn_buf
,
7138 struct bpf_prog
*prog
, u32
*target_size
)
7140 struct bpf_insn
*insn
= insn_buf
;
7144 case offsetof(struct __sk_buff
, len
):
7145 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7146 bpf_target_off(struct sk_buff
, len
, 4,
7150 case offsetof(struct __sk_buff
, protocol
):
7151 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
7152 bpf_target_off(struct sk_buff
, protocol
, 2,
7156 case offsetof(struct __sk_buff
, vlan_proto
):
7157 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
7158 bpf_target_off(struct sk_buff
, vlan_proto
, 2,
7162 case offsetof(struct __sk_buff
, priority
):
7163 if (type
== BPF_WRITE
)
7164 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7165 bpf_target_off(struct sk_buff
, priority
, 4,
7168 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7169 bpf_target_off(struct sk_buff
, priority
, 4,
7173 case offsetof(struct __sk_buff
, ingress_ifindex
):
7174 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7175 bpf_target_off(struct sk_buff
, skb_iif
, 4,
7179 case offsetof(struct __sk_buff
, ifindex
):
7180 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, dev
),
7181 si
->dst_reg
, si
->src_reg
,
7182 offsetof(struct sk_buff
, dev
));
7183 *insn
++ = BPF_JMP_IMM(BPF_JEQ
, si
->dst_reg
, 0, 1);
7184 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7185 bpf_target_off(struct net_device
, ifindex
, 4,
7189 case offsetof(struct __sk_buff
, hash
):
7190 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7191 bpf_target_off(struct sk_buff
, hash
, 4,
7195 case offsetof(struct __sk_buff
, mark
):
7196 if (type
== BPF_WRITE
)
7197 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7198 bpf_target_off(struct sk_buff
, mark
, 4,
7201 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7202 bpf_target_off(struct sk_buff
, mark
, 4,
7206 case offsetof(struct __sk_buff
, pkt_type
):
7208 *insn
++ = BPF_LDX_MEM(BPF_B
, si
->dst_reg
, si
->src_reg
,
7210 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, PKT_TYPE_MAX
);
7211 #ifdef __BIG_ENDIAN_BITFIELD
7212 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, 5);
7216 case offsetof(struct __sk_buff
, queue_mapping
):
7217 if (type
== BPF_WRITE
) {
7218 *insn
++ = BPF_JMP_IMM(BPF_JGE
, si
->src_reg
, NO_QUEUE_MAPPING
, 1);
7219 *insn
++ = BPF_STX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
7220 bpf_target_off(struct sk_buff
,
7224 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
7225 bpf_target_off(struct sk_buff
,
7231 case offsetof(struct __sk_buff
, vlan_present
):
7233 *insn
++ = BPF_LDX_MEM(BPF_B
, si
->dst_reg
, si
->src_reg
,
7234 PKT_VLAN_PRESENT_OFFSET());
7235 if (PKT_VLAN_PRESENT_BIT
)
7236 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, PKT_VLAN_PRESENT_BIT
);
7237 if (PKT_VLAN_PRESENT_BIT
< 7)
7238 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, 1);
7241 case offsetof(struct __sk_buff
, vlan_tci
):
7242 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
7243 bpf_target_off(struct sk_buff
, vlan_tci
, 2,
7247 case offsetof(struct __sk_buff
, cb
[0]) ...
7248 offsetofend(struct __sk_buff
, cb
[4]) - 1:
7249 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb
, data
) < 20);
7250 BUILD_BUG_ON((offsetof(struct sk_buff
, cb
) +
7251 offsetof(struct qdisc_skb_cb
, data
)) %
7254 prog
->cb_access
= 1;
7256 off
-= offsetof(struct __sk_buff
, cb
[0]);
7257 off
+= offsetof(struct sk_buff
, cb
);
7258 off
+= offsetof(struct qdisc_skb_cb
, data
);
7259 if (type
== BPF_WRITE
)
7260 *insn
++ = BPF_STX_MEM(BPF_SIZE(si
->code
), si
->dst_reg
,
7263 *insn
++ = BPF_LDX_MEM(BPF_SIZE(si
->code
), si
->dst_reg
,
7267 case offsetof(struct __sk_buff
, tc_classid
):
7268 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb
, tc_classid
) != 2);
7271 off
-= offsetof(struct __sk_buff
, tc_classid
);
7272 off
+= offsetof(struct sk_buff
, cb
);
7273 off
+= offsetof(struct qdisc_skb_cb
, tc_classid
);
7275 if (type
== BPF_WRITE
)
7276 *insn
++ = BPF_STX_MEM(BPF_H
, si
->dst_reg
,
7279 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
,
7283 case offsetof(struct __sk_buff
, data
):
7284 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, data
),
7285 si
->dst_reg
, si
->src_reg
,
7286 offsetof(struct sk_buff
, data
));
7289 case offsetof(struct __sk_buff
, data_meta
):
7291 off
-= offsetof(struct __sk_buff
, data_meta
);
7292 off
+= offsetof(struct sk_buff
, cb
);
7293 off
+= offsetof(struct bpf_skb_data_end
, data_meta
);
7294 *insn
++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si
->dst_reg
,
7298 case offsetof(struct __sk_buff
, data_end
):
7300 off
-= offsetof(struct __sk_buff
, data_end
);
7301 off
+= offsetof(struct sk_buff
, cb
);
7302 off
+= offsetof(struct bpf_skb_data_end
, data_end
);
7303 *insn
++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si
->dst_reg
,
7307 case offsetof(struct __sk_buff
, tc_index
):
7308 #ifdef CONFIG_NET_SCHED
7309 if (type
== BPF_WRITE
)
7310 *insn
++ = BPF_STX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
7311 bpf_target_off(struct sk_buff
, tc_index
, 2,
7314 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
7315 bpf_target_off(struct sk_buff
, tc_index
, 2,
7319 if (type
== BPF_WRITE
)
7320 *insn
++ = BPF_MOV64_REG(si
->dst_reg
, si
->dst_reg
);
7322 *insn
++ = BPF_MOV64_IMM(si
->dst_reg
, 0);
7326 case offsetof(struct __sk_buff
, napi_id
):
7327 #if defined(CONFIG_NET_RX_BUSY_POLL)
7328 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7329 bpf_target_off(struct sk_buff
, napi_id
, 4,
7331 *insn
++ = BPF_JMP_IMM(BPF_JGE
, si
->dst_reg
, MIN_NAPI_ID
, 1);
7332 *insn
++ = BPF_MOV64_IMM(si
->dst_reg
, 0);
7335 *insn
++ = BPF_MOV64_IMM(si
->dst_reg
, 0);
7338 case offsetof(struct __sk_buff
, family
):
7339 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_family
) != 2);
7341 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
7342 si
->dst_reg
, si
->src_reg
,
7343 offsetof(struct sk_buff
, sk
));
7344 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
7345 bpf_target_off(struct sock_common
,
7349 case offsetof(struct __sk_buff
, remote_ip4
):
7350 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_daddr
) != 4);
7352 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
7353 si
->dst_reg
, si
->src_reg
,
7354 offsetof(struct sk_buff
, sk
));
7355 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7356 bpf_target_off(struct sock_common
,
7360 case offsetof(struct __sk_buff
, local_ip4
):
7361 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
7362 skc_rcv_saddr
) != 4);
7364 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
7365 si
->dst_reg
, si
->src_reg
,
7366 offsetof(struct sk_buff
, sk
));
7367 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7368 bpf_target_off(struct sock_common
,
7372 case offsetof(struct __sk_buff
, remote_ip6
[0]) ...
7373 offsetof(struct __sk_buff
, remote_ip6
[3]):
7374 #if IS_ENABLED(CONFIG_IPV6)
7375 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
7376 skc_v6_daddr
.s6_addr32
[0]) != 4);
7379 off
-= offsetof(struct __sk_buff
, remote_ip6
[0]);
7381 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
7382 si
->dst_reg
, si
->src_reg
,
7383 offsetof(struct sk_buff
, sk
));
7384 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7385 offsetof(struct sock_common
,
7386 skc_v6_daddr
.s6_addr32
[0]) +
7389 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7392 case offsetof(struct __sk_buff
, local_ip6
[0]) ...
7393 offsetof(struct __sk_buff
, local_ip6
[3]):
7394 #if IS_ENABLED(CONFIG_IPV6)
7395 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
7396 skc_v6_rcv_saddr
.s6_addr32
[0]) != 4);
7399 off
-= offsetof(struct __sk_buff
, local_ip6
[0]);
7401 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
7402 si
->dst_reg
, si
->src_reg
,
7403 offsetof(struct sk_buff
, sk
));
7404 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7405 offsetof(struct sock_common
,
7406 skc_v6_rcv_saddr
.s6_addr32
[0]) +
7409 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7413 case offsetof(struct __sk_buff
, remote_port
):
7414 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_dport
) != 2);
7416 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
7417 si
->dst_reg
, si
->src_reg
,
7418 offsetof(struct sk_buff
, sk
));
7419 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
7420 bpf_target_off(struct sock_common
,
7423 #ifndef __BIG_ENDIAN_BITFIELD
7424 *insn
++ = BPF_ALU32_IMM(BPF_LSH
, si
->dst_reg
, 16);
7428 case offsetof(struct __sk_buff
, local_port
):
7429 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_num
) != 2);
7431 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
7432 si
->dst_reg
, si
->src_reg
,
7433 offsetof(struct sk_buff
, sk
));
7434 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
7435 bpf_target_off(struct sock_common
,
7436 skc_num
, 2, target_size
));
7439 case offsetof(struct __sk_buff
, tstamp
):
7440 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, tstamp
) != 8);
7442 if (type
== BPF_WRITE
)
7443 *insn
++ = BPF_STX_MEM(BPF_DW
,
7444 si
->dst_reg
, si
->src_reg
,
7445 bpf_target_off(struct sk_buff
,
7449 *insn
++ = BPF_LDX_MEM(BPF_DW
,
7450 si
->dst_reg
, si
->src_reg
,
7451 bpf_target_off(struct sk_buff
,
7456 case offsetof(struct __sk_buff
, gso_segs
):
7457 /* si->dst_reg = skb_shinfo(SKB); */
7458 #ifdef NET_SKBUFF_DATA_USES_OFFSET
7459 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, end
),
7460 BPF_REG_AX
, si
->src_reg
,
7461 offsetof(struct sk_buff
, end
));
7462 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, head
),
7463 si
->dst_reg
, si
->src_reg
,
7464 offsetof(struct sk_buff
, head
));
7465 *insn
++ = BPF_ALU64_REG(BPF_ADD
, si
->dst_reg
, BPF_REG_AX
);
7467 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, end
),
7468 si
->dst_reg
, si
->src_reg
,
7469 offsetof(struct sk_buff
, end
));
7471 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info
, gso_segs
),
7472 si
->dst_reg
, si
->dst_reg
,
7473 bpf_target_off(struct skb_shared_info
,
7477 case offsetof(struct __sk_buff
, wire_len
):
7478 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb
, pkt_len
) != 4);
7481 off
-= offsetof(struct __sk_buff
, wire_len
);
7482 off
+= offsetof(struct sk_buff
, cb
);
7483 off
+= offsetof(struct qdisc_skb_cb
, pkt_len
);
7485 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
, off
);
7488 case offsetof(struct __sk_buff
, sk
):
7489 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
7490 si
->dst_reg
, si
->src_reg
,
7491 offsetof(struct sk_buff
, sk
));
7495 return insn
- insn_buf
;
7498 u32
bpf_sock_convert_ctx_access(enum bpf_access_type type
,
7499 const struct bpf_insn
*si
,
7500 struct bpf_insn
*insn_buf
,
7501 struct bpf_prog
*prog
, u32
*target_size
)
7503 struct bpf_insn
*insn
= insn_buf
;
7507 case offsetof(struct bpf_sock
, bound_dev_if
):
7508 BUILD_BUG_ON(FIELD_SIZEOF(struct sock
, sk_bound_dev_if
) != 4);
7510 if (type
== BPF_WRITE
)
7511 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7512 offsetof(struct sock
, sk_bound_dev_if
));
7514 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7515 offsetof(struct sock
, sk_bound_dev_if
));
7518 case offsetof(struct bpf_sock
, mark
):
7519 BUILD_BUG_ON(FIELD_SIZEOF(struct sock
, sk_mark
) != 4);
7521 if (type
== BPF_WRITE
)
7522 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7523 offsetof(struct sock
, sk_mark
));
7525 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7526 offsetof(struct sock
, sk_mark
));
7529 case offsetof(struct bpf_sock
, priority
):
7530 BUILD_BUG_ON(FIELD_SIZEOF(struct sock
, sk_priority
) != 4);
7532 if (type
== BPF_WRITE
)
7533 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7534 offsetof(struct sock
, sk_priority
));
7536 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7537 offsetof(struct sock
, sk_priority
));
7540 case offsetof(struct bpf_sock
, family
):
7541 *insn
++ = BPF_LDX_MEM(
7542 BPF_FIELD_SIZEOF(struct sock_common
, skc_family
),
7543 si
->dst_reg
, si
->src_reg
,
7544 bpf_target_off(struct sock_common
,
7546 FIELD_SIZEOF(struct sock_common
,
7551 case offsetof(struct bpf_sock
, type
):
7552 BUILD_BUG_ON(HWEIGHT32(SK_FL_TYPE_MASK
) != BITS_PER_BYTE
* 2);
7553 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7554 offsetof(struct sock
, __sk_flags_offset
));
7555 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_TYPE_MASK
);
7556 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, SK_FL_TYPE_SHIFT
);
7560 case offsetof(struct bpf_sock
, protocol
):
7561 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK
) != BITS_PER_BYTE
);
7562 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7563 offsetof(struct sock
, __sk_flags_offset
));
7564 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_PROTO_MASK
);
7565 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, SK_FL_PROTO_SHIFT
);
7569 case offsetof(struct bpf_sock
, src_ip4
):
7570 *insn
++ = BPF_LDX_MEM(
7571 BPF_SIZE(si
->code
), si
->dst_reg
, si
->src_reg
,
7572 bpf_target_off(struct sock_common
, skc_rcv_saddr
,
7573 FIELD_SIZEOF(struct sock_common
,
7578 case offsetof(struct bpf_sock
, dst_ip4
):
7579 *insn
++ = BPF_LDX_MEM(
7580 BPF_SIZE(si
->code
), si
->dst_reg
, si
->src_reg
,
7581 bpf_target_off(struct sock_common
, skc_daddr
,
7582 FIELD_SIZEOF(struct sock_common
,
7587 case bpf_ctx_range_till(struct bpf_sock
, src_ip6
[0], src_ip6
[3]):
7588 #if IS_ENABLED(CONFIG_IPV6)
7590 off
-= offsetof(struct bpf_sock
, src_ip6
[0]);
7591 *insn
++ = BPF_LDX_MEM(
7592 BPF_SIZE(si
->code
), si
->dst_reg
, si
->src_reg
,
7595 skc_v6_rcv_saddr
.s6_addr32
[0],
7596 FIELD_SIZEOF(struct sock_common
,
7597 skc_v6_rcv_saddr
.s6_addr32
[0]),
7598 target_size
) + off
);
7601 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7605 case bpf_ctx_range_till(struct bpf_sock
, dst_ip6
[0], dst_ip6
[3]):
7606 #if IS_ENABLED(CONFIG_IPV6)
7608 off
-= offsetof(struct bpf_sock
, dst_ip6
[0]);
7609 *insn
++ = BPF_LDX_MEM(
7610 BPF_SIZE(si
->code
), si
->dst_reg
, si
->src_reg
,
7611 bpf_target_off(struct sock_common
,
7612 skc_v6_daddr
.s6_addr32
[0],
7613 FIELD_SIZEOF(struct sock_common
,
7614 skc_v6_daddr
.s6_addr32
[0]),
7615 target_size
) + off
);
7617 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7622 case offsetof(struct bpf_sock
, src_port
):
7623 *insn
++ = BPF_LDX_MEM(
7624 BPF_FIELD_SIZEOF(struct sock_common
, skc_num
),
7625 si
->dst_reg
, si
->src_reg
,
7626 bpf_target_off(struct sock_common
, skc_num
,
7627 FIELD_SIZEOF(struct sock_common
,
7632 case offsetof(struct bpf_sock
, dst_port
):
7633 *insn
++ = BPF_LDX_MEM(
7634 BPF_FIELD_SIZEOF(struct sock_common
, skc_dport
),
7635 si
->dst_reg
, si
->src_reg
,
7636 bpf_target_off(struct sock_common
, skc_dport
,
7637 FIELD_SIZEOF(struct sock_common
,
7642 case offsetof(struct bpf_sock
, state
):
7643 *insn
++ = BPF_LDX_MEM(
7644 BPF_FIELD_SIZEOF(struct sock_common
, skc_state
),
7645 si
->dst_reg
, si
->src_reg
,
7646 bpf_target_off(struct sock_common
, skc_state
,
7647 FIELD_SIZEOF(struct sock_common
,
7653 return insn
- insn_buf
;
7656 static u32
tc_cls_act_convert_ctx_access(enum bpf_access_type type
,
7657 const struct bpf_insn
*si
,
7658 struct bpf_insn
*insn_buf
,
7659 struct bpf_prog
*prog
, u32
*target_size
)
7661 struct bpf_insn
*insn
= insn_buf
;
7664 case offsetof(struct __sk_buff
, ifindex
):
7665 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, dev
),
7666 si
->dst_reg
, si
->src_reg
,
7667 offsetof(struct sk_buff
, dev
));
7668 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7669 bpf_target_off(struct net_device
, ifindex
, 4,
7673 return bpf_convert_ctx_access(type
, si
, insn_buf
, prog
,
7677 return insn
- insn_buf
;
7680 static u32
xdp_convert_ctx_access(enum bpf_access_type type
,
7681 const struct bpf_insn
*si
,
7682 struct bpf_insn
*insn_buf
,
7683 struct bpf_prog
*prog
, u32
*target_size
)
7685 struct bpf_insn
*insn
= insn_buf
;
7688 case offsetof(struct xdp_md
, data
):
7689 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, data
),
7690 si
->dst_reg
, si
->src_reg
,
7691 offsetof(struct xdp_buff
, data
));
7693 case offsetof(struct xdp_md
, data_meta
):
7694 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, data_meta
),
7695 si
->dst_reg
, si
->src_reg
,
7696 offsetof(struct xdp_buff
, data_meta
));
7698 case offsetof(struct xdp_md
, data_end
):
7699 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, data_end
),
7700 si
->dst_reg
, si
->src_reg
,
7701 offsetof(struct xdp_buff
, data_end
));
7703 case offsetof(struct xdp_md
, ingress_ifindex
):
7704 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, rxq
),
7705 si
->dst_reg
, si
->src_reg
,
7706 offsetof(struct xdp_buff
, rxq
));
7707 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info
, dev
),
7708 si
->dst_reg
, si
->dst_reg
,
7709 offsetof(struct xdp_rxq_info
, dev
));
7710 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7711 offsetof(struct net_device
, ifindex
));
7713 case offsetof(struct xdp_md
, rx_queue_index
):
7714 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, rxq
),
7715 si
->dst_reg
, si
->src_reg
,
7716 offsetof(struct xdp_buff
, rxq
));
7717 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7718 offsetof(struct xdp_rxq_info
,
7723 return insn
- insn_buf
;
7726 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
7727 * context Structure, F is Field in context structure that contains a pointer
7728 * to Nested Structure of type NS that has the field NF.
7730 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
7731 * sure that SIZE is not greater than actual size of S.F.NF.
7733 * If offset OFF is provided, the load happens from that offset relative to
7736 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
7738 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
7739 si->src_reg, offsetof(S, F)); \
7740 *insn++ = BPF_LDX_MEM( \
7741 SIZE, si->dst_reg, si->dst_reg, \
7742 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7747 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
7748 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
7749 BPF_FIELD_SIZEOF(NS, NF), 0)
7751 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
7752 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
7754 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
7755 * "register" since two registers available in convert_ctx_access are not
7756 * enough: we can't override neither SRC, since it contains value to store, nor
7757 * DST since it contains pointer to context that may be used by later
7758 * instructions. But we need a temporary place to save pointer to nested
7759 * structure whose field we want to store to.
7761 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
7763 int tmp_reg = BPF_REG_9; \
7764 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7766 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7768 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
7770 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
7771 si->dst_reg, offsetof(S, F)); \
7772 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
7773 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7776 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
7780 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
7783 if (type == BPF_WRITE) { \
7784 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
7787 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
7788 S, NS, F, NF, SIZE, OFF); \
7792 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
7793 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
7794 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
7796 static u32
sock_addr_convert_ctx_access(enum bpf_access_type type
,
7797 const struct bpf_insn
*si
,
7798 struct bpf_insn
*insn_buf
,
7799 struct bpf_prog
*prog
, u32
*target_size
)
7801 struct bpf_insn
*insn
= insn_buf
;
7805 case offsetof(struct bpf_sock_addr
, user_family
):
7806 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern
,
7807 struct sockaddr
, uaddr
, sa_family
);
7810 case offsetof(struct bpf_sock_addr
, user_ip4
):
7811 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7812 struct bpf_sock_addr_kern
, struct sockaddr_in
, uaddr
,
7813 sin_addr
, BPF_SIZE(si
->code
), 0, tmp_reg
);
7816 case bpf_ctx_range_till(struct bpf_sock_addr
, user_ip6
[0], user_ip6
[3]):
7818 off
-= offsetof(struct bpf_sock_addr
, user_ip6
[0]);
7819 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7820 struct bpf_sock_addr_kern
, struct sockaddr_in6
, uaddr
,
7821 sin6_addr
.s6_addr32
[0], BPF_SIZE(si
->code
), off
,
7825 case offsetof(struct bpf_sock_addr
, user_port
):
7826 /* To get port we need to know sa_family first and then treat
7827 * sockaddr as either sockaddr_in or sockaddr_in6.
7828 * Though we can simplify since port field has same offset and
7829 * size in both structures.
7830 * Here we check this invariant and use just one of the
7831 * structures if it's true.
7833 BUILD_BUG_ON(offsetof(struct sockaddr_in
, sin_port
) !=
7834 offsetof(struct sockaddr_in6
, sin6_port
));
7835 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in
, sin_port
) !=
7836 FIELD_SIZEOF(struct sockaddr_in6
, sin6_port
));
7837 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern
,
7838 struct sockaddr_in6
, uaddr
,
7839 sin6_port
, tmp_reg
);
7842 case offsetof(struct bpf_sock_addr
, family
):
7843 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern
,
7844 struct sock
, sk
, sk_family
);
7847 case offsetof(struct bpf_sock_addr
, type
):
7848 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7849 struct bpf_sock_addr_kern
, struct sock
, sk
,
7850 __sk_flags_offset
, BPF_W
, 0);
7851 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_TYPE_MASK
);
7852 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, SK_FL_TYPE_SHIFT
);
7855 case offsetof(struct bpf_sock_addr
, protocol
):
7856 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7857 struct bpf_sock_addr_kern
, struct sock
, sk
,
7858 __sk_flags_offset
, BPF_W
, 0);
7859 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_PROTO_MASK
);
7860 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
,
7864 case offsetof(struct bpf_sock_addr
, msg_src_ip4
):
7865 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
7866 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7867 struct bpf_sock_addr_kern
, struct in_addr
, t_ctx
,
7868 s_addr
, BPF_SIZE(si
->code
), 0, tmp_reg
);
7871 case bpf_ctx_range_till(struct bpf_sock_addr
, msg_src_ip6
[0],
7874 off
-= offsetof(struct bpf_sock_addr
, msg_src_ip6
[0]);
7875 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
7876 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7877 struct bpf_sock_addr_kern
, struct in6_addr
, t_ctx
,
7878 s6_addr32
[0], BPF_SIZE(si
->code
), off
, tmp_reg
);
7880 case offsetof(struct bpf_sock_addr
, sk
):
7881 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern
, sk
),
7882 si
->dst_reg
, si
->src_reg
,
7883 offsetof(struct bpf_sock_addr_kern
, sk
));
7887 return insn
- insn_buf
;
7890 static u32
sock_ops_convert_ctx_access(enum bpf_access_type type
,
7891 const struct bpf_insn
*si
,
7892 struct bpf_insn
*insn_buf
,
7893 struct bpf_prog
*prog
,
7896 struct bpf_insn
*insn
= insn_buf
;
7899 /* Helper macro for adding read access to tcp_sock or sock fields. */
7900 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7902 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7903 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7904 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7905 struct bpf_sock_ops_kern, \
7907 si->dst_reg, si->src_reg, \
7908 offsetof(struct bpf_sock_ops_kern, \
7910 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
7911 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7912 struct bpf_sock_ops_kern, sk),\
7913 si->dst_reg, si->src_reg, \
7914 offsetof(struct bpf_sock_ops_kern, sk));\
7915 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
7917 si->dst_reg, si->dst_reg, \
7918 offsetof(OBJ, OBJ_FIELD)); \
7921 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
7922 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
7924 /* Helper macro for adding write access to tcp_sock or sock fields.
7925 * The macro is called with two registers, dst_reg which contains a pointer
7926 * to ctx (context) and src_reg which contains the value that should be
7927 * stored. However, we need an additional register since we cannot overwrite
7928 * dst_reg because it may be used later in the program.
7929 * Instead we "borrow" one of the other register. We first save its value
7930 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
7931 * it at the end of the macro.
7933 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7935 int reg = BPF_REG_9; \
7936 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7937 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7938 if (si->dst_reg == reg || si->src_reg == reg) \
7940 if (si->dst_reg == reg || si->src_reg == reg) \
7942 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
7943 offsetof(struct bpf_sock_ops_kern, \
7945 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7946 struct bpf_sock_ops_kern, \
7949 offsetof(struct bpf_sock_ops_kern, \
7951 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
7952 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7953 struct bpf_sock_ops_kern, sk),\
7955 offsetof(struct bpf_sock_ops_kern, sk));\
7956 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
7958 offsetof(OBJ, OBJ_FIELD)); \
7959 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
7960 offsetof(struct bpf_sock_ops_kern, \
7964 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
7966 if (TYPE == BPF_WRITE) \
7967 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7969 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7972 if (insn
> insn_buf
)
7973 return insn
- insn_buf
;
7976 case offsetof(struct bpf_sock_ops
, op
) ...
7977 offsetof(struct bpf_sock_ops
, replylong
[3]):
7978 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops
, op
) !=
7979 FIELD_SIZEOF(struct bpf_sock_ops_kern
, op
));
7980 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops
, reply
) !=
7981 FIELD_SIZEOF(struct bpf_sock_ops_kern
, reply
));
7982 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops
, replylong
) !=
7983 FIELD_SIZEOF(struct bpf_sock_ops_kern
, replylong
));
7985 off
-= offsetof(struct bpf_sock_ops
, op
);
7986 off
+= offsetof(struct bpf_sock_ops_kern
, op
);
7987 if (type
== BPF_WRITE
)
7988 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7991 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7995 case offsetof(struct bpf_sock_ops
, family
):
7996 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_family
) != 2);
7998 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7999 struct bpf_sock_ops_kern
, sk
),
8000 si
->dst_reg
, si
->src_reg
,
8001 offsetof(struct bpf_sock_ops_kern
, sk
));
8002 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
8003 offsetof(struct sock_common
, skc_family
));
8006 case offsetof(struct bpf_sock_ops
, remote_ip4
):
8007 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_daddr
) != 4);
8009 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8010 struct bpf_sock_ops_kern
, sk
),
8011 si
->dst_reg
, si
->src_reg
,
8012 offsetof(struct bpf_sock_ops_kern
, sk
));
8013 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
8014 offsetof(struct sock_common
, skc_daddr
));
8017 case offsetof(struct bpf_sock_ops
, local_ip4
):
8018 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
8019 skc_rcv_saddr
) != 4);
8021 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8022 struct bpf_sock_ops_kern
, sk
),
8023 si
->dst_reg
, si
->src_reg
,
8024 offsetof(struct bpf_sock_ops_kern
, sk
));
8025 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
8026 offsetof(struct sock_common
,
8030 case offsetof(struct bpf_sock_ops
, remote_ip6
[0]) ...
8031 offsetof(struct bpf_sock_ops
, remote_ip6
[3]):
8032 #if IS_ENABLED(CONFIG_IPV6)
8033 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
8034 skc_v6_daddr
.s6_addr32
[0]) != 4);
8037 off
-= offsetof(struct bpf_sock_ops
, remote_ip6
[0]);
8038 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8039 struct bpf_sock_ops_kern
, sk
),
8040 si
->dst_reg
, si
->src_reg
,
8041 offsetof(struct bpf_sock_ops_kern
, sk
));
8042 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
8043 offsetof(struct sock_common
,
8044 skc_v6_daddr
.s6_addr32
[0]) +
8047 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
8051 case offsetof(struct bpf_sock_ops
, local_ip6
[0]) ...
8052 offsetof(struct bpf_sock_ops
, local_ip6
[3]):
8053 #if IS_ENABLED(CONFIG_IPV6)
8054 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
8055 skc_v6_rcv_saddr
.s6_addr32
[0]) != 4);
8058 off
-= offsetof(struct bpf_sock_ops
, local_ip6
[0]);
8059 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8060 struct bpf_sock_ops_kern
, sk
),
8061 si
->dst_reg
, si
->src_reg
,
8062 offsetof(struct bpf_sock_ops_kern
, sk
));
8063 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
8064 offsetof(struct sock_common
,
8065 skc_v6_rcv_saddr
.s6_addr32
[0]) +
8068 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
8072 case offsetof(struct bpf_sock_ops
, remote_port
):
8073 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_dport
) != 2);
8075 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8076 struct bpf_sock_ops_kern
, sk
),
8077 si
->dst_reg
, si
->src_reg
,
8078 offsetof(struct bpf_sock_ops_kern
, sk
));
8079 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
8080 offsetof(struct sock_common
, skc_dport
));
8081 #ifndef __BIG_ENDIAN_BITFIELD
8082 *insn
++ = BPF_ALU32_IMM(BPF_LSH
, si
->dst_reg
, 16);
8086 case offsetof(struct bpf_sock_ops
, local_port
):
8087 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_num
) != 2);
8089 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8090 struct bpf_sock_ops_kern
, sk
),
8091 si
->dst_reg
, si
->src_reg
,
8092 offsetof(struct bpf_sock_ops_kern
, sk
));
8093 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
8094 offsetof(struct sock_common
, skc_num
));
8097 case offsetof(struct bpf_sock_ops
, is_fullsock
):
8098 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8099 struct bpf_sock_ops_kern
,
8101 si
->dst_reg
, si
->src_reg
,
8102 offsetof(struct bpf_sock_ops_kern
,
8106 case offsetof(struct bpf_sock_ops
, state
):
8107 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_state
) != 1);
8109 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8110 struct bpf_sock_ops_kern
, sk
),
8111 si
->dst_reg
, si
->src_reg
,
8112 offsetof(struct bpf_sock_ops_kern
, sk
));
8113 *insn
++ = BPF_LDX_MEM(BPF_B
, si
->dst_reg
, si
->dst_reg
,
8114 offsetof(struct sock_common
, skc_state
));
8117 case offsetof(struct bpf_sock_ops
, rtt_min
):
8118 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock
, rtt_min
) !=
8119 sizeof(struct minmax
));
8120 BUILD_BUG_ON(sizeof(struct minmax
) <
8121 sizeof(struct minmax_sample
));
8123 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8124 struct bpf_sock_ops_kern
, sk
),
8125 si
->dst_reg
, si
->src_reg
,
8126 offsetof(struct bpf_sock_ops_kern
, sk
));
8127 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
8128 offsetof(struct tcp_sock
, rtt_min
) +
8129 FIELD_SIZEOF(struct minmax_sample
, t
));
8132 case offsetof(struct bpf_sock_ops
, bpf_sock_ops_cb_flags
):
8133 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags
, bpf_sock_ops_cb_flags
,
8137 case offsetof(struct bpf_sock_ops
, sk_txhash
):
8138 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash
, sk_txhash
,
8141 case offsetof(struct bpf_sock_ops
, snd_cwnd
):
8142 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd
);
8144 case offsetof(struct bpf_sock_ops
, srtt_us
):
8145 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us
);
8147 case offsetof(struct bpf_sock_ops
, snd_ssthresh
):
8148 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh
);
8150 case offsetof(struct bpf_sock_ops
, rcv_nxt
):
8151 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt
);
8153 case offsetof(struct bpf_sock_ops
, snd_nxt
):
8154 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt
);
8156 case offsetof(struct bpf_sock_ops
, snd_una
):
8157 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una
);
8159 case offsetof(struct bpf_sock_ops
, mss_cache
):
8160 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache
);
8162 case offsetof(struct bpf_sock_ops
, ecn_flags
):
8163 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags
);
8165 case offsetof(struct bpf_sock_ops
, rate_delivered
):
8166 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered
);
8168 case offsetof(struct bpf_sock_ops
, rate_interval_us
):
8169 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us
);
8171 case offsetof(struct bpf_sock_ops
, packets_out
):
8172 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out
);
8174 case offsetof(struct bpf_sock_ops
, retrans_out
):
8175 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out
);
8177 case offsetof(struct bpf_sock_ops
, total_retrans
):
8178 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans
);
8180 case offsetof(struct bpf_sock_ops
, segs_in
):
8181 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in
);
8183 case offsetof(struct bpf_sock_ops
, data_segs_in
):
8184 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in
);
8186 case offsetof(struct bpf_sock_ops
, segs_out
):
8187 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out
);
8189 case offsetof(struct bpf_sock_ops
, data_segs_out
):
8190 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out
);
8192 case offsetof(struct bpf_sock_ops
, lost_out
):
8193 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out
);
8195 case offsetof(struct bpf_sock_ops
, sacked_out
):
8196 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out
);
8198 case offsetof(struct bpf_sock_ops
, bytes_received
):
8199 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received
);
8201 case offsetof(struct bpf_sock_ops
, bytes_acked
):
8202 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked
);
8204 case offsetof(struct bpf_sock_ops
, sk
):
8205 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8206 struct bpf_sock_ops_kern
,
8208 si
->dst_reg
, si
->src_reg
,
8209 offsetof(struct bpf_sock_ops_kern
,
8211 *insn
++ = BPF_JMP_IMM(BPF_JEQ
, si
->dst_reg
, 0, 1);
8212 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8213 struct bpf_sock_ops_kern
, sk
),
8214 si
->dst_reg
, si
->src_reg
,
8215 offsetof(struct bpf_sock_ops_kern
, sk
));
8218 return insn
- insn_buf
;
8221 static u32
sk_skb_convert_ctx_access(enum bpf_access_type type
,
8222 const struct bpf_insn
*si
,
8223 struct bpf_insn
*insn_buf
,
8224 struct bpf_prog
*prog
, u32
*target_size
)
8226 struct bpf_insn
*insn
= insn_buf
;
8230 case offsetof(struct __sk_buff
, data_end
):
8232 off
-= offsetof(struct __sk_buff
, data_end
);
8233 off
+= offsetof(struct sk_buff
, cb
);
8234 off
+= offsetof(struct tcp_skb_cb
, bpf
.data_end
);
8235 *insn
++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si
->dst_reg
,
8239 return bpf_convert_ctx_access(type
, si
, insn_buf
, prog
,
8243 return insn
- insn_buf
;
8246 static u32
sk_msg_convert_ctx_access(enum bpf_access_type type
,
8247 const struct bpf_insn
*si
,
8248 struct bpf_insn
*insn_buf
,
8249 struct bpf_prog
*prog
, u32
*target_size
)
8251 struct bpf_insn
*insn
= insn_buf
;
8252 #if IS_ENABLED(CONFIG_IPV6)
8256 /* convert ctx uses the fact sg element is first in struct */
8257 BUILD_BUG_ON(offsetof(struct sk_msg
, sg
) != 0);
8260 case offsetof(struct sk_msg_md
, data
):
8261 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg
, data
),
8262 si
->dst_reg
, si
->src_reg
,
8263 offsetof(struct sk_msg
, data
));
8265 case offsetof(struct sk_msg_md
, data_end
):
8266 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg
, data_end
),
8267 si
->dst_reg
, si
->src_reg
,
8268 offsetof(struct sk_msg
, data_end
));
8270 case offsetof(struct sk_msg_md
, family
):
8271 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_family
) != 2);
8273 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8275 si
->dst_reg
, si
->src_reg
,
8276 offsetof(struct sk_msg
, sk
));
8277 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
8278 offsetof(struct sock_common
, skc_family
));
8281 case offsetof(struct sk_msg_md
, remote_ip4
):
8282 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_daddr
) != 4);
8284 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8286 si
->dst_reg
, si
->src_reg
,
8287 offsetof(struct sk_msg
, sk
));
8288 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
8289 offsetof(struct sock_common
, skc_daddr
));
8292 case offsetof(struct sk_msg_md
, local_ip4
):
8293 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
8294 skc_rcv_saddr
) != 4);
8296 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8298 si
->dst_reg
, si
->src_reg
,
8299 offsetof(struct sk_msg
, sk
));
8300 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
8301 offsetof(struct sock_common
,
8305 case offsetof(struct sk_msg_md
, remote_ip6
[0]) ...
8306 offsetof(struct sk_msg_md
, remote_ip6
[3]):
8307 #if IS_ENABLED(CONFIG_IPV6)
8308 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
8309 skc_v6_daddr
.s6_addr32
[0]) != 4);
8312 off
-= offsetof(struct sk_msg_md
, remote_ip6
[0]);
8313 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8315 si
->dst_reg
, si
->src_reg
,
8316 offsetof(struct sk_msg
, sk
));
8317 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
8318 offsetof(struct sock_common
,
8319 skc_v6_daddr
.s6_addr32
[0]) +
8322 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
8326 case offsetof(struct sk_msg_md
, local_ip6
[0]) ...
8327 offsetof(struct sk_msg_md
, local_ip6
[3]):
8328 #if IS_ENABLED(CONFIG_IPV6)
8329 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
8330 skc_v6_rcv_saddr
.s6_addr32
[0]) != 4);
8333 off
-= offsetof(struct sk_msg_md
, local_ip6
[0]);
8334 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8336 si
->dst_reg
, si
->src_reg
,
8337 offsetof(struct sk_msg
, sk
));
8338 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
8339 offsetof(struct sock_common
,
8340 skc_v6_rcv_saddr
.s6_addr32
[0]) +
8343 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
8347 case offsetof(struct sk_msg_md
, remote_port
):
8348 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_dport
) != 2);
8350 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8352 si
->dst_reg
, si
->src_reg
,
8353 offsetof(struct sk_msg
, sk
));
8354 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
8355 offsetof(struct sock_common
, skc_dport
));
8356 #ifndef __BIG_ENDIAN_BITFIELD
8357 *insn
++ = BPF_ALU32_IMM(BPF_LSH
, si
->dst_reg
, 16);
8361 case offsetof(struct sk_msg_md
, local_port
):
8362 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_num
) != 2);
8364 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8366 si
->dst_reg
, si
->src_reg
,
8367 offsetof(struct sk_msg
, sk
));
8368 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
8369 offsetof(struct sock_common
, skc_num
));
8372 case offsetof(struct sk_msg_md
, size
):
8373 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg
, size
),
8374 si
->dst_reg
, si
->src_reg
,
8375 offsetof(struct sk_msg_sg
, size
));
8379 return insn
- insn_buf
;
8382 const struct bpf_verifier_ops sk_filter_verifier_ops
= {
8383 .get_func_proto
= sk_filter_func_proto
,
8384 .is_valid_access
= sk_filter_is_valid_access
,
8385 .convert_ctx_access
= bpf_convert_ctx_access
,
8386 .gen_ld_abs
= bpf_gen_ld_abs
,
8389 const struct bpf_prog_ops sk_filter_prog_ops
= {
8390 .test_run
= bpf_prog_test_run_skb
,
8393 const struct bpf_verifier_ops tc_cls_act_verifier_ops
= {
8394 .get_func_proto
= tc_cls_act_func_proto
,
8395 .is_valid_access
= tc_cls_act_is_valid_access
,
8396 .convert_ctx_access
= tc_cls_act_convert_ctx_access
,
8397 .gen_prologue
= tc_cls_act_prologue
,
8398 .gen_ld_abs
= bpf_gen_ld_abs
,
8401 const struct bpf_prog_ops tc_cls_act_prog_ops
= {
8402 .test_run
= bpf_prog_test_run_skb
,
8405 const struct bpf_verifier_ops xdp_verifier_ops
= {
8406 .get_func_proto
= xdp_func_proto
,
8407 .is_valid_access
= xdp_is_valid_access
,
8408 .convert_ctx_access
= xdp_convert_ctx_access
,
8409 .gen_prologue
= bpf_noop_prologue
,
8412 const struct bpf_prog_ops xdp_prog_ops
= {
8413 .test_run
= bpf_prog_test_run_xdp
,
8416 const struct bpf_verifier_ops cg_skb_verifier_ops
= {
8417 .get_func_proto
= cg_skb_func_proto
,
8418 .is_valid_access
= cg_skb_is_valid_access
,
8419 .convert_ctx_access
= bpf_convert_ctx_access
,
8422 const struct bpf_prog_ops cg_skb_prog_ops
= {
8423 .test_run
= bpf_prog_test_run_skb
,
8426 const struct bpf_verifier_ops lwt_in_verifier_ops
= {
8427 .get_func_proto
= lwt_in_func_proto
,
8428 .is_valid_access
= lwt_is_valid_access
,
8429 .convert_ctx_access
= bpf_convert_ctx_access
,
8432 const struct bpf_prog_ops lwt_in_prog_ops
= {
8433 .test_run
= bpf_prog_test_run_skb
,
8436 const struct bpf_verifier_ops lwt_out_verifier_ops
= {
8437 .get_func_proto
= lwt_out_func_proto
,
8438 .is_valid_access
= lwt_is_valid_access
,
8439 .convert_ctx_access
= bpf_convert_ctx_access
,
8442 const struct bpf_prog_ops lwt_out_prog_ops
= {
8443 .test_run
= bpf_prog_test_run_skb
,
8446 const struct bpf_verifier_ops lwt_xmit_verifier_ops
= {
8447 .get_func_proto
= lwt_xmit_func_proto
,
8448 .is_valid_access
= lwt_is_valid_access
,
8449 .convert_ctx_access
= bpf_convert_ctx_access
,
8450 .gen_prologue
= tc_cls_act_prologue
,
8453 const struct bpf_prog_ops lwt_xmit_prog_ops
= {
8454 .test_run
= bpf_prog_test_run_skb
,
8457 const struct bpf_verifier_ops lwt_seg6local_verifier_ops
= {
8458 .get_func_proto
= lwt_seg6local_func_proto
,
8459 .is_valid_access
= lwt_is_valid_access
,
8460 .convert_ctx_access
= bpf_convert_ctx_access
,
8463 const struct bpf_prog_ops lwt_seg6local_prog_ops
= {
8464 .test_run
= bpf_prog_test_run_skb
,
8467 const struct bpf_verifier_ops cg_sock_verifier_ops
= {
8468 .get_func_proto
= sock_filter_func_proto
,
8469 .is_valid_access
= sock_filter_is_valid_access
,
8470 .convert_ctx_access
= bpf_sock_convert_ctx_access
,
8473 const struct bpf_prog_ops cg_sock_prog_ops
= {
8476 const struct bpf_verifier_ops cg_sock_addr_verifier_ops
= {
8477 .get_func_proto
= sock_addr_func_proto
,
8478 .is_valid_access
= sock_addr_is_valid_access
,
8479 .convert_ctx_access
= sock_addr_convert_ctx_access
,
8482 const struct bpf_prog_ops cg_sock_addr_prog_ops
= {
8485 const struct bpf_verifier_ops sock_ops_verifier_ops
= {
8486 .get_func_proto
= sock_ops_func_proto
,
8487 .is_valid_access
= sock_ops_is_valid_access
,
8488 .convert_ctx_access
= sock_ops_convert_ctx_access
,
8491 const struct bpf_prog_ops sock_ops_prog_ops
= {
8494 const struct bpf_verifier_ops sk_skb_verifier_ops
= {
8495 .get_func_proto
= sk_skb_func_proto
,
8496 .is_valid_access
= sk_skb_is_valid_access
,
8497 .convert_ctx_access
= sk_skb_convert_ctx_access
,
8498 .gen_prologue
= sk_skb_prologue
,
8501 const struct bpf_prog_ops sk_skb_prog_ops
= {
8504 const struct bpf_verifier_ops sk_msg_verifier_ops
= {
8505 .get_func_proto
= sk_msg_func_proto
,
8506 .is_valid_access
= sk_msg_is_valid_access
,
8507 .convert_ctx_access
= sk_msg_convert_ctx_access
,
8508 .gen_prologue
= bpf_noop_prologue
,
8511 const struct bpf_prog_ops sk_msg_prog_ops
= {
8514 const struct bpf_verifier_ops flow_dissector_verifier_ops
= {
8515 .get_func_proto
= flow_dissector_func_proto
,
8516 .is_valid_access
= flow_dissector_is_valid_access
,
8517 .convert_ctx_access
= flow_dissector_convert_ctx_access
,
8520 const struct bpf_prog_ops flow_dissector_prog_ops
= {
8521 .test_run
= bpf_prog_test_run_flow_dissector
,
8524 int sk_detach_filter(struct sock
*sk
)
8527 struct sk_filter
*filter
;
8529 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
8532 filter
= rcu_dereference_protected(sk
->sk_filter
,
8533 lockdep_sock_is_held(sk
));
8535 RCU_INIT_POINTER(sk
->sk_filter
, NULL
);
8536 sk_filter_uncharge(sk
, filter
);
8542 EXPORT_SYMBOL_GPL(sk_detach_filter
);
8544 int sk_get_filter(struct sock
*sk
, struct sock_filter __user
*ubuf
,
8547 struct sock_fprog_kern
*fprog
;
8548 struct sk_filter
*filter
;
8552 filter
= rcu_dereference_protected(sk
->sk_filter
,
8553 lockdep_sock_is_held(sk
));
8557 /* We're copying the filter that has been originally attached,
8558 * so no conversion/decode needed anymore. eBPF programs that
8559 * have no original program cannot be dumped through this.
8562 fprog
= filter
->prog
->orig_prog
;
8568 /* User space only enquires number of filter blocks. */
8572 if (len
< fprog
->len
)
8576 if (copy_to_user(ubuf
, fprog
->filter
, bpf_classic_proglen(fprog
)))
8579 /* Instead of bytes, the API requests to return the number
8589 struct sk_reuseport_kern
{
8590 struct sk_buff
*skb
;
8592 struct sock
*selected_sk
;
8599 static void bpf_init_reuseport_kern(struct sk_reuseport_kern
*reuse_kern
,
8600 struct sock_reuseport
*reuse
,
8601 struct sock
*sk
, struct sk_buff
*skb
,
8604 reuse_kern
->skb
= skb
;
8605 reuse_kern
->sk
= sk
;
8606 reuse_kern
->selected_sk
= NULL
;
8607 reuse_kern
->data_end
= skb
->data
+ skb_headlen(skb
);
8608 reuse_kern
->hash
= hash
;
8609 reuse_kern
->reuseport_id
= reuse
->reuseport_id
;
8610 reuse_kern
->bind_inany
= reuse
->bind_inany
;
8613 struct sock
*bpf_run_sk_reuseport(struct sock_reuseport
*reuse
, struct sock
*sk
,
8614 struct bpf_prog
*prog
, struct sk_buff
*skb
,
8617 struct sk_reuseport_kern reuse_kern
;
8618 enum sk_action action
;
8620 bpf_init_reuseport_kern(&reuse_kern
, reuse
, sk
, skb
, hash
);
8621 action
= BPF_PROG_RUN(prog
, &reuse_kern
);
8623 if (action
== SK_PASS
)
8624 return reuse_kern
.selected_sk
;
8626 return ERR_PTR(-ECONNREFUSED
);
8629 BPF_CALL_4(sk_select_reuseport
, struct sk_reuseport_kern
*, reuse_kern
,
8630 struct bpf_map
*, map
, void *, key
, u32
, flags
)
8632 struct sock_reuseport
*reuse
;
8633 struct sock
*selected_sk
;
8635 selected_sk
= map
->ops
->map_lookup_elem(map
, key
);
8639 reuse
= rcu_dereference(selected_sk
->sk_reuseport_cb
);
8641 /* selected_sk is unhashed (e.g. by close()) after the
8642 * above map_lookup_elem(). Treat selected_sk has already
8643 * been removed from the map.
8647 if (unlikely(reuse
->reuseport_id
!= reuse_kern
->reuseport_id
)) {
8650 if (unlikely(!reuse_kern
->reuseport_id
))
8651 /* There is a small race between adding the
8652 * sk to the map and setting the
8653 * reuse_kern->reuseport_id.
8654 * Treat it as the sk has not been added to
8659 sk
= reuse_kern
->sk
;
8660 if (sk
->sk_protocol
!= selected_sk
->sk_protocol
)
8662 else if (sk
->sk_family
!= selected_sk
->sk_family
)
8663 return -EAFNOSUPPORT
;
8665 /* Catch all. Likely bound to a different sockaddr. */
8669 reuse_kern
->selected_sk
= selected_sk
;
8674 static const struct bpf_func_proto sk_select_reuseport_proto
= {
8675 .func
= sk_select_reuseport
,
8677 .ret_type
= RET_INTEGER
,
8678 .arg1_type
= ARG_PTR_TO_CTX
,
8679 .arg2_type
= ARG_CONST_MAP_PTR
,
8680 .arg3_type
= ARG_PTR_TO_MAP_KEY
,
8681 .arg4_type
= ARG_ANYTHING
,
8684 BPF_CALL_4(sk_reuseport_load_bytes
,
8685 const struct sk_reuseport_kern
*, reuse_kern
, u32
, offset
,
8686 void *, to
, u32
, len
)
8688 return ____bpf_skb_load_bytes(reuse_kern
->skb
, offset
, to
, len
);
8691 static const struct bpf_func_proto sk_reuseport_load_bytes_proto
= {
8692 .func
= sk_reuseport_load_bytes
,
8694 .ret_type
= RET_INTEGER
,
8695 .arg1_type
= ARG_PTR_TO_CTX
,
8696 .arg2_type
= ARG_ANYTHING
,
8697 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
8698 .arg4_type
= ARG_CONST_SIZE
,
8701 BPF_CALL_5(sk_reuseport_load_bytes_relative
,
8702 const struct sk_reuseport_kern
*, reuse_kern
, u32
, offset
,
8703 void *, to
, u32
, len
, u32
, start_header
)
8705 return ____bpf_skb_load_bytes_relative(reuse_kern
->skb
, offset
, to
,
8709 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto
= {
8710 .func
= sk_reuseport_load_bytes_relative
,
8712 .ret_type
= RET_INTEGER
,
8713 .arg1_type
= ARG_PTR_TO_CTX
,
8714 .arg2_type
= ARG_ANYTHING
,
8715 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
8716 .arg4_type
= ARG_CONST_SIZE
,
8717 .arg5_type
= ARG_ANYTHING
,
8720 static const struct bpf_func_proto
*
8721 sk_reuseport_func_proto(enum bpf_func_id func_id
,
8722 const struct bpf_prog
*prog
)
8725 case BPF_FUNC_sk_select_reuseport
:
8726 return &sk_select_reuseport_proto
;
8727 case BPF_FUNC_skb_load_bytes
:
8728 return &sk_reuseport_load_bytes_proto
;
8729 case BPF_FUNC_skb_load_bytes_relative
:
8730 return &sk_reuseport_load_bytes_relative_proto
;
8732 return bpf_base_func_proto(func_id
);
8737 sk_reuseport_is_valid_access(int off
, int size
,
8738 enum bpf_access_type type
,
8739 const struct bpf_prog
*prog
,
8740 struct bpf_insn_access_aux
*info
)
8742 const u32 size_default
= sizeof(__u32
);
8744 if (off
< 0 || off
>= sizeof(struct sk_reuseport_md
) ||
8745 off
% size
|| type
!= BPF_READ
)
8749 case offsetof(struct sk_reuseport_md
, data
):
8750 info
->reg_type
= PTR_TO_PACKET
;
8751 return size
== sizeof(__u64
);
8753 case offsetof(struct sk_reuseport_md
, data_end
):
8754 info
->reg_type
= PTR_TO_PACKET_END
;
8755 return size
== sizeof(__u64
);
8757 case offsetof(struct sk_reuseport_md
, hash
):
8758 return size
== size_default
;
8760 /* Fields that allow narrowing */
8761 case bpf_ctx_range(struct sk_reuseport_md
, eth_protocol
):
8762 if (size
< FIELD_SIZEOF(struct sk_buff
, protocol
))
8765 case bpf_ctx_range(struct sk_reuseport_md
, ip_protocol
):
8766 case bpf_ctx_range(struct sk_reuseport_md
, bind_inany
):
8767 case bpf_ctx_range(struct sk_reuseport_md
, len
):
8768 bpf_ctx_record_field_size(info
, size_default
);
8769 return bpf_ctx_narrow_access_ok(off
, size
, size_default
);
8776 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
8777 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8778 si->dst_reg, si->src_reg, \
8779 bpf_target_off(struct sk_reuseport_kern, F, \
8780 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8784 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
8785 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
8790 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
8791 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
8794 SK_FIELD, BPF_SIZE, EXTRA_OFF)
8796 static u32
sk_reuseport_convert_ctx_access(enum bpf_access_type type
,
8797 const struct bpf_insn
*si
,
8798 struct bpf_insn
*insn_buf
,
8799 struct bpf_prog
*prog
,
8802 struct bpf_insn
*insn
= insn_buf
;
8805 case offsetof(struct sk_reuseport_md
, data
):
8806 SK_REUSEPORT_LOAD_SKB_FIELD(data
);
8809 case offsetof(struct sk_reuseport_md
, len
):
8810 SK_REUSEPORT_LOAD_SKB_FIELD(len
);
8813 case offsetof(struct sk_reuseport_md
, eth_protocol
):
8814 SK_REUSEPORT_LOAD_SKB_FIELD(protocol
);
8817 case offsetof(struct sk_reuseport_md
, ip_protocol
):
8818 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK
) != BITS_PER_BYTE
);
8819 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset
,
8821 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_PROTO_MASK
);
8822 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
,
8824 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
8825 * aware. No further narrowing or masking is needed.
8830 case offsetof(struct sk_reuseport_md
, data_end
):
8831 SK_REUSEPORT_LOAD_FIELD(data_end
);
8834 case offsetof(struct sk_reuseport_md
, hash
):
8835 SK_REUSEPORT_LOAD_FIELD(hash
);
8838 case offsetof(struct sk_reuseport_md
, bind_inany
):
8839 SK_REUSEPORT_LOAD_FIELD(bind_inany
);
8843 return insn
- insn_buf
;
8846 const struct bpf_verifier_ops sk_reuseport_verifier_ops
= {
8847 .get_func_proto
= sk_reuseport_func_proto
,
8848 .is_valid_access
= sk_reuseport_is_valid_access
,
8849 .convert_ctx_access
= sk_reuseport_convert_ctx_access
,
8852 const struct bpf_prog_ops sk_reuseport_prog_ops
= {
8854 #endif /* CONFIG_INET */