2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
29 #include <linux/sock_diag.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_packet.h>
34 #include <linux/if_arp.h>
35 #include <linux/gfp.h>
36 #include <net/inet_common.h>
38 #include <net/protocol.h>
39 #include <net/netlink.h>
40 #include <linux/skbuff.h>
41 #include <linux/skmsg.h>
43 #include <net/flow_dissector.h>
44 #include <linux/errno.h>
45 #include <linux/timer.h>
46 #include <linux/uaccess.h>
47 #include <asm/unaligned.h>
48 #include <asm/cmpxchg.h>
49 #include <linux/filter.h>
50 #include <linux/ratelimit.h>
51 #include <linux/seccomp.h>
52 #include <linux/if_vlan.h>
53 #include <linux/bpf.h>
54 #include <net/sch_generic.h>
55 #include <net/cls_cgroup.h>
56 #include <net/dst_metadata.h>
58 #include <net/sock_reuseport.h>
59 #include <net/busy_poll.h>
63 #include <linux/bpf_trace.h>
64 #include <net/xdp_sock.h>
65 #include <linux/inetdevice.h>
66 #include <net/inet_hashtables.h>
67 #include <net/inet6_hashtables.h>
68 #include <net/ip_fib.h>
72 #include <net/net_namespace.h>
73 #include <linux/seg6_local.h>
75 #include <net/seg6_local.h>
76 #include <net/lwtunnel.h>
79 * sk_filter_trim_cap - run a packet through a socket filter
80 * @sk: sock associated with &sk_buff
81 * @skb: buffer to filter
82 * @cap: limit on how short the eBPF program may trim the packet
84 * Run the eBPF program and then cut skb->data to correct size returned by
85 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
86 * than pkt_len we keep whole skb->data. This is the socket level
87 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
88 * be accepted or -EPERM if the packet should be tossed.
91 int sk_filter_trim_cap(struct sock
*sk
, struct sk_buff
*skb
, unsigned int cap
)
94 struct sk_filter
*filter
;
97 * If the skb was allocated from pfmemalloc reserves, only
98 * allow SOCK_MEMALLOC sockets to use it as this socket is
101 if (skb_pfmemalloc(skb
) && !sock_flag(sk
, SOCK_MEMALLOC
)) {
102 NET_INC_STATS(sock_net(sk
), LINUX_MIB_PFMEMALLOCDROP
);
105 err
= BPF_CGROUP_RUN_PROG_INET_INGRESS(sk
, skb
);
109 err
= security_sock_rcv_skb(sk
, skb
);
114 filter
= rcu_dereference(sk
->sk_filter
);
116 struct sock
*save_sk
= skb
->sk
;
117 unsigned int pkt_len
;
120 pkt_len
= bpf_prog_run_save_cb(filter
->prog
, skb
);
122 err
= pkt_len
? pskb_trim(skb
, max(cap
, pkt_len
)) : -EPERM
;
128 EXPORT_SYMBOL(sk_filter_trim_cap
);
130 BPF_CALL_1(bpf_skb_get_pay_offset
, struct sk_buff
*, skb
)
132 return skb_get_poff(skb
);
135 BPF_CALL_3(bpf_skb_get_nlattr
, struct sk_buff
*, skb
, u32
, a
, u32
, x
)
139 if (skb_is_nonlinear(skb
))
142 if (skb
->len
< sizeof(struct nlattr
))
145 if (a
> skb
->len
- sizeof(struct nlattr
))
148 nla
= nla_find((struct nlattr
*) &skb
->data
[a
], skb
->len
- a
, x
);
150 return (void *) nla
- (void *) skb
->data
;
155 BPF_CALL_3(bpf_skb_get_nlattr_nest
, struct sk_buff
*, skb
, u32
, a
, u32
, x
)
159 if (skb_is_nonlinear(skb
))
162 if (skb
->len
< sizeof(struct nlattr
))
165 if (a
> skb
->len
- sizeof(struct nlattr
))
168 nla
= (struct nlattr
*) &skb
->data
[a
];
169 if (nla
->nla_len
> skb
->len
- a
)
172 nla
= nla_find_nested(nla
, x
);
174 return (void *) nla
- (void *) skb
->data
;
179 BPF_CALL_4(bpf_skb_load_helper_8
, const struct sk_buff
*, skb
, const void *,
180 data
, int, headlen
, int, offset
)
183 const int len
= sizeof(tmp
);
186 if (headlen
- offset
>= len
)
187 return *(u8
*)(data
+ offset
);
188 if (!skb_copy_bits(skb
, offset
, &tmp
, sizeof(tmp
)))
191 ptr
= bpf_internal_load_pointer_neg_helper(skb
, offset
, len
);
199 BPF_CALL_2(bpf_skb_load_helper_8_no_cache
, const struct sk_buff
*, skb
,
202 return ____bpf_skb_load_helper_8(skb
, skb
->data
, skb
->len
- skb
->data_len
,
206 BPF_CALL_4(bpf_skb_load_helper_16
, const struct sk_buff
*, skb
, const void *,
207 data
, int, headlen
, int, offset
)
210 const int len
= sizeof(tmp
);
213 if (headlen
- offset
>= len
)
214 return get_unaligned_be16(data
+ offset
);
215 if (!skb_copy_bits(skb
, offset
, &tmp
, sizeof(tmp
)))
216 return be16_to_cpu(tmp
);
218 ptr
= bpf_internal_load_pointer_neg_helper(skb
, offset
, len
);
220 return get_unaligned_be16(ptr
);
226 BPF_CALL_2(bpf_skb_load_helper_16_no_cache
, const struct sk_buff
*, skb
,
229 return ____bpf_skb_load_helper_16(skb
, skb
->data
, skb
->len
- skb
->data_len
,
233 BPF_CALL_4(bpf_skb_load_helper_32
, const struct sk_buff
*, skb
, const void *,
234 data
, int, headlen
, int, offset
)
237 const int len
= sizeof(tmp
);
239 if (likely(offset
>= 0)) {
240 if (headlen
- offset
>= len
)
241 return get_unaligned_be32(data
+ offset
);
242 if (!skb_copy_bits(skb
, offset
, &tmp
, sizeof(tmp
)))
243 return be32_to_cpu(tmp
);
245 ptr
= bpf_internal_load_pointer_neg_helper(skb
, offset
, len
);
247 return get_unaligned_be32(ptr
);
253 BPF_CALL_2(bpf_skb_load_helper_32_no_cache
, const struct sk_buff
*, skb
,
256 return ____bpf_skb_load_helper_32(skb
, skb
->data
, skb
->len
- skb
->data_len
,
260 BPF_CALL_0(bpf_get_raw_cpu_id
)
262 return raw_smp_processor_id();
265 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto
= {
266 .func
= bpf_get_raw_cpu_id
,
268 .ret_type
= RET_INTEGER
,
271 static u32
convert_skb_access(int skb_field
, int dst_reg
, int src_reg
,
272 struct bpf_insn
*insn_buf
)
274 struct bpf_insn
*insn
= insn_buf
;
278 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, mark
) != 4);
280 *insn
++ = BPF_LDX_MEM(BPF_W
, dst_reg
, src_reg
,
281 offsetof(struct sk_buff
, mark
));
285 *insn
++ = BPF_LDX_MEM(BPF_B
, dst_reg
, src_reg
, PKT_TYPE_OFFSET());
286 *insn
++ = BPF_ALU32_IMM(BPF_AND
, dst_reg
, PKT_TYPE_MAX
);
287 #ifdef __BIG_ENDIAN_BITFIELD
288 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, dst_reg
, 5);
293 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, queue_mapping
) != 2);
295 *insn
++ = BPF_LDX_MEM(BPF_H
, dst_reg
, src_reg
,
296 offsetof(struct sk_buff
, queue_mapping
));
299 case SKF_AD_VLAN_TAG
:
300 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, vlan_tci
) != 2);
302 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
303 *insn
++ = BPF_LDX_MEM(BPF_H
, dst_reg
, src_reg
,
304 offsetof(struct sk_buff
, vlan_tci
));
306 case SKF_AD_VLAN_TAG_PRESENT
:
307 *insn
++ = BPF_LDX_MEM(BPF_B
, dst_reg
, src_reg
, PKT_VLAN_PRESENT_OFFSET());
308 if (PKT_VLAN_PRESENT_BIT
)
309 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, dst_reg
, PKT_VLAN_PRESENT_BIT
);
310 if (PKT_VLAN_PRESENT_BIT
< 7)
311 *insn
++ = BPF_ALU32_IMM(BPF_AND
, dst_reg
, 1);
315 return insn
- insn_buf
;
318 static bool convert_bpf_extensions(struct sock_filter
*fp
,
319 struct bpf_insn
**insnp
)
321 struct bpf_insn
*insn
= *insnp
;
325 case SKF_AD_OFF
+ SKF_AD_PROTOCOL
:
326 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, protocol
) != 2);
328 /* A = *(u16 *) (CTX + offsetof(protocol)) */
329 *insn
++ = BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_CTX
,
330 offsetof(struct sk_buff
, protocol
));
331 /* A = ntohs(A) [emitting a nop or swap16] */
332 *insn
= BPF_ENDIAN(BPF_FROM_BE
, BPF_REG_A
, 16);
335 case SKF_AD_OFF
+ SKF_AD_PKTTYPE
:
336 cnt
= convert_skb_access(SKF_AD_PKTTYPE
, BPF_REG_A
, BPF_REG_CTX
, insn
);
340 case SKF_AD_OFF
+ SKF_AD_IFINDEX
:
341 case SKF_AD_OFF
+ SKF_AD_HATYPE
:
342 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, ifindex
) != 4);
343 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device
, type
) != 2);
345 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, dev
),
346 BPF_REG_TMP
, BPF_REG_CTX
,
347 offsetof(struct sk_buff
, dev
));
348 /* if (tmp != 0) goto pc + 1 */
349 *insn
++ = BPF_JMP_IMM(BPF_JNE
, BPF_REG_TMP
, 0, 1);
350 *insn
++ = BPF_EXIT_INSN();
351 if (fp
->k
== SKF_AD_OFF
+ SKF_AD_IFINDEX
)
352 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_TMP
,
353 offsetof(struct net_device
, ifindex
));
355 *insn
= BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_TMP
,
356 offsetof(struct net_device
, type
));
359 case SKF_AD_OFF
+ SKF_AD_MARK
:
360 cnt
= convert_skb_access(SKF_AD_MARK
, BPF_REG_A
, BPF_REG_CTX
, insn
);
364 case SKF_AD_OFF
+ SKF_AD_RXHASH
:
365 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, hash
) != 4);
367 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_CTX
,
368 offsetof(struct sk_buff
, hash
));
371 case SKF_AD_OFF
+ SKF_AD_QUEUE
:
372 cnt
= convert_skb_access(SKF_AD_QUEUE
, BPF_REG_A
, BPF_REG_CTX
, insn
);
376 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG
:
377 cnt
= convert_skb_access(SKF_AD_VLAN_TAG
,
378 BPF_REG_A
, BPF_REG_CTX
, insn
);
382 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
:
383 cnt
= convert_skb_access(SKF_AD_VLAN_TAG_PRESENT
,
384 BPF_REG_A
, BPF_REG_CTX
, insn
);
388 case SKF_AD_OFF
+ SKF_AD_VLAN_TPID
:
389 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, vlan_proto
) != 2);
391 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
392 *insn
++ = BPF_LDX_MEM(BPF_H
, BPF_REG_A
, BPF_REG_CTX
,
393 offsetof(struct sk_buff
, vlan_proto
));
394 /* A = ntohs(A) [emitting a nop or swap16] */
395 *insn
= BPF_ENDIAN(BPF_FROM_BE
, BPF_REG_A
, 16);
398 case SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
:
399 case SKF_AD_OFF
+ SKF_AD_NLATTR
:
400 case SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
:
401 case SKF_AD_OFF
+ SKF_AD_CPU
:
402 case SKF_AD_OFF
+ SKF_AD_RANDOM
:
404 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG1
, BPF_REG_CTX
);
406 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG2
, BPF_REG_A
);
408 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG3
, BPF_REG_X
);
409 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
411 case SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
:
412 *insn
= BPF_EMIT_CALL(bpf_skb_get_pay_offset
);
414 case SKF_AD_OFF
+ SKF_AD_NLATTR
:
415 *insn
= BPF_EMIT_CALL(bpf_skb_get_nlattr
);
417 case SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
:
418 *insn
= BPF_EMIT_CALL(bpf_skb_get_nlattr_nest
);
420 case SKF_AD_OFF
+ SKF_AD_CPU
:
421 *insn
= BPF_EMIT_CALL(bpf_get_raw_cpu_id
);
423 case SKF_AD_OFF
+ SKF_AD_RANDOM
:
424 *insn
= BPF_EMIT_CALL(bpf_user_rnd_u32
);
425 bpf_user_rnd_init_once();
430 case SKF_AD_OFF
+ SKF_AD_ALU_XOR_X
:
432 *insn
= BPF_ALU32_REG(BPF_XOR
, BPF_REG_A
, BPF_REG_X
);
436 /* This is just a dummy call to avoid letting the compiler
437 * evict __bpf_call_base() as an optimization. Placed here
438 * where no-one bothers.
440 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
448 static bool convert_bpf_ld_abs(struct sock_filter
*fp
, struct bpf_insn
**insnp
)
450 const bool unaligned_ok
= IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
);
451 int size
= bpf_size_to_bytes(BPF_SIZE(fp
->code
));
452 bool endian
= BPF_SIZE(fp
->code
) == BPF_H
||
453 BPF_SIZE(fp
->code
) == BPF_W
;
454 bool indirect
= BPF_MODE(fp
->code
) == BPF_IND
;
455 const int ip_align
= NET_IP_ALIGN
;
456 struct bpf_insn
*insn
= *insnp
;
460 ((unaligned_ok
&& offset
>= 0) ||
461 (!unaligned_ok
&& offset
>= 0 &&
462 offset
+ ip_align
>= 0 &&
463 offset
+ ip_align
% size
== 0))) {
464 bool ldx_off_ok
= offset
<= S16_MAX
;
466 *insn
++ = BPF_MOV64_REG(BPF_REG_TMP
, BPF_REG_H
);
468 *insn
++ = BPF_ALU64_IMM(BPF_SUB
, BPF_REG_TMP
, offset
);
469 *insn
++ = BPF_JMP_IMM(BPF_JSLT
, BPF_REG_TMP
,
470 size
, 2 + endian
+ (!ldx_off_ok
* 2));
472 *insn
++ = BPF_LDX_MEM(BPF_SIZE(fp
->code
), BPF_REG_A
,
475 *insn
++ = BPF_MOV64_REG(BPF_REG_TMP
, BPF_REG_D
);
476 *insn
++ = BPF_ALU64_IMM(BPF_ADD
, BPF_REG_TMP
, offset
);
477 *insn
++ = BPF_LDX_MEM(BPF_SIZE(fp
->code
), BPF_REG_A
,
481 *insn
++ = BPF_ENDIAN(BPF_FROM_BE
, BPF_REG_A
, size
* 8);
482 *insn
++ = BPF_JMP_A(8);
485 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG1
, BPF_REG_CTX
);
486 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG2
, BPF_REG_D
);
487 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG3
, BPF_REG_H
);
489 *insn
++ = BPF_MOV64_IMM(BPF_REG_ARG4
, offset
);
491 *insn
++ = BPF_MOV64_REG(BPF_REG_ARG4
, BPF_REG_X
);
493 *insn
++ = BPF_ALU64_IMM(BPF_ADD
, BPF_REG_ARG4
, offset
);
496 switch (BPF_SIZE(fp
->code
)) {
498 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_8
);
501 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_16
);
504 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_32
);
510 *insn
++ = BPF_JMP_IMM(BPF_JSGE
, BPF_REG_A
, 0, 2);
511 *insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_A
, BPF_REG_A
);
512 *insn
= BPF_EXIT_INSN();
519 * bpf_convert_filter - convert filter program
520 * @prog: the user passed filter program
521 * @len: the length of the user passed filter program
522 * @new_prog: allocated 'struct bpf_prog' or NULL
523 * @new_len: pointer to store length of converted program
524 * @seen_ld_abs: bool whether we've seen ld_abs/ind
526 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
527 * style extended BPF (eBPF).
528 * Conversion workflow:
530 * 1) First pass for calculating the new program length:
531 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
533 * 2) 2nd pass to remap in two passes: 1st pass finds new
534 * jump offsets, 2nd pass remapping:
535 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
537 static int bpf_convert_filter(struct sock_filter
*prog
, int len
,
538 struct bpf_prog
*new_prog
, int *new_len
,
541 int new_flen
= 0, pass
= 0, target
, i
, stack_off
;
542 struct bpf_insn
*new_insn
, *first_insn
= NULL
;
543 struct sock_filter
*fp
;
547 BUILD_BUG_ON(BPF_MEMWORDS
* sizeof(u32
) > MAX_BPF_STACK
);
548 BUILD_BUG_ON(BPF_REG_FP
+ 1 != MAX_BPF_REG
);
550 if (len
<= 0 || len
> BPF_MAXINSNS
)
554 first_insn
= new_prog
->insnsi
;
555 addrs
= kcalloc(len
, sizeof(*addrs
),
556 GFP_KERNEL
| __GFP_NOWARN
);
562 new_insn
= first_insn
;
565 /* Classic BPF related prologue emission. */
567 /* Classic BPF expects A and X to be reset first. These need
568 * to be guaranteed to be the first two instructions.
570 *new_insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_A
, BPF_REG_A
);
571 *new_insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_X
, BPF_REG_X
);
573 /* All programs must keep CTX in callee saved BPF_REG_CTX.
574 * In eBPF case it's done by the compiler, here we need to
575 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
577 *new_insn
++ = BPF_MOV64_REG(BPF_REG_CTX
, BPF_REG_ARG1
);
579 /* For packet access in classic BPF, cache skb->data
580 * in callee-saved BPF R8 and skb->len - skb->data_len
581 * (headlen) in BPF R9. Since classic BPF is read-only
582 * on CTX, we only need to cache it once.
584 *new_insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, data
),
585 BPF_REG_D
, BPF_REG_CTX
,
586 offsetof(struct sk_buff
, data
));
587 *new_insn
++ = BPF_LDX_MEM(BPF_W
, BPF_REG_H
, BPF_REG_CTX
,
588 offsetof(struct sk_buff
, len
));
589 *new_insn
++ = BPF_LDX_MEM(BPF_W
, BPF_REG_TMP
, BPF_REG_CTX
,
590 offsetof(struct sk_buff
, data_len
));
591 *new_insn
++ = BPF_ALU32_REG(BPF_SUB
, BPF_REG_H
, BPF_REG_TMP
);
597 for (i
= 0; i
< len
; fp
++, i
++) {
598 struct bpf_insn tmp_insns
[32] = { };
599 struct bpf_insn
*insn
= tmp_insns
;
602 addrs
[i
] = new_insn
- first_insn
;
605 /* All arithmetic insns and skb loads map as-is. */
606 case BPF_ALU
| BPF_ADD
| BPF_X
:
607 case BPF_ALU
| BPF_ADD
| BPF_K
:
608 case BPF_ALU
| BPF_SUB
| BPF_X
:
609 case BPF_ALU
| BPF_SUB
| BPF_K
:
610 case BPF_ALU
| BPF_AND
| BPF_X
:
611 case BPF_ALU
| BPF_AND
| BPF_K
:
612 case BPF_ALU
| BPF_OR
| BPF_X
:
613 case BPF_ALU
| BPF_OR
| BPF_K
:
614 case BPF_ALU
| BPF_LSH
| BPF_X
:
615 case BPF_ALU
| BPF_LSH
| BPF_K
:
616 case BPF_ALU
| BPF_RSH
| BPF_X
:
617 case BPF_ALU
| BPF_RSH
| BPF_K
:
618 case BPF_ALU
| BPF_XOR
| BPF_X
:
619 case BPF_ALU
| BPF_XOR
| BPF_K
:
620 case BPF_ALU
| BPF_MUL
| BPF_X
:
621 case BPF_ALU
| BPF_MUL
| BPF_K
:
622 case BPF_ALU
| BPF_DIV
| BPF_X
:
623 case BPF_ALU
| BPF_DIV
| BPF_K
:
624 case BPF_ALU
| BPF_MOD
| BPF_X
:
625 case BPF_ALU
| BPF_MOD
| BPF_K
:
626 case BPF_ALU
| BPF_NEG
:
627 case BPF_LD
| BPF_ABS
| BPF_W
:
628 case BPF_LD
| BPF_ABS
| BPF_H
:
629 case BPF_LD
| BPF_ABS
| BPF_B
:
630 case BPF_LD
| BPF_IND
| BPF_W
:
631 case BPF_LD
| BPF_IND
| BPF_H
:
632 case BPF_LD
| BPF_IND
| BPF_B
:
633 /* Check for overloaded BPF extension and
634 * directly convert it if found, otherwise
635 * just move on with mapping.
637 if (BPF_CLASS(fp
->code
) == BPF_LD
&&
638 BPF_MODE(fp
->code
) == BPF_ABS
&&
639 convert_bpf_extensions(fp
, &insn
))
641 if (BPF_CLASS(fp
->code
) == BPF_LD
&&
642 convert_bpf_ld_abs(fp
, &insn
)) {
647 if (fp
->code
== (BPF_ALU
| BPF_DIV
| BPF_X
) ||
648 fp
->code
== (BPF_ALU
| BPF_MOD
| BPF_X
)) {
649 *insn
++ = BPF_MOV32_REG(BPF_REG_X
, BPF_REG_X
);
650 /* Error with exception code on div/mod by 0.
651 * For cBPF programs, this was always return 0.
653 *insn
++ = BPF_JMP_IMM(BPF_JNE
, BPF_REG_X
, 0, 2);
654 *insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_A
, BPF_REG_A
);
655 *insn
++ = BPF_EXIT_INSN();
658 *insn
= BPF_RAW_INSN(fp
->code
, BPF_REG_A
, BPF_REG_X
, 0, fp
->k
);
661 /* Jump transformation cannot use BPF block macros
662 * everywhere as offset calculation and target updates
663 * require a bit more work than the rest, i.e. jump
664 * opcodes map as-is, but offsets need adjustment.
667 #define BPF_EMIT_JMP \
669 const s32 off_min = S16_MIN, off_max = S16_MAX; \
672 if (target >= len || target < 0) \
674 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
675 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
676 off -= insn - tmp_insns; \
677 /* Reject anything not fitting into insn->off. */ \
678 if (off < off_min || off > off_max) \
683 case BPF_JMP
| BPF_JA
:
684 target
= i
+ fp
->k
+ 1;
685 insn
->code
= fp
->code
;
689 case BPF_JMP
| BPF_JEQ
| BPF_K
:
690 case BPF_JMP
| BPF_JEQ
| BPF_X
:
691 case BPF_JMP
| BPF_JSET
| BPF_K
:
692 case BPF_JMP
| BPF_JSET
| BPF_X
:
693 case BPF_JMP
| BPF_JGT
| BPF_K
:
694 case BPF_JMP
| BPF_JGT
| BPF_X
:
695 case BPF_JMP
| BPF_JGE
| BPF_K
:
696 case BPF_JMP
| BPF_JGE
| BPF_X
:
697 if (BPF_SRC(fp
->code
) == BPF_K
&& (int) fp
->k
< 0) {
698 /* BPF immediates are signed, zero extend
699 * immediate into tmp register and use it
702 *insn
++ = BPF_MOV32_IMM(BPF_REG_TMP
, fp
->k
);
704 insn
->dst_reg
= BPF_REG_A
;
705 insn
->src_reg
= BPF_REG_TMP
;
708 insn
->dst_reg
= BPF_REG_A
;
710 bpf_src
= BPF_SRC(fp
->code
);
711 insn
->src_reg
= bpf_src
== BPF_X
? BPF_REG_X
: 0;
714 /* Common case where 'jump_false' is next insn. */
716 insn
->code
= BPF_JMP
| BPF_OP(fp
->code
) | bpf_src
;
717 target
= i
+ fp
->jt
+ 1;
722 /* Convert some jumps when 'jump_true' is next insn. */
724 switch (BPF_OP(fp
->code
)) {
726 insn
->code
= BPF_JMP
| BPF_JNE
| bpf_src
;
729 insn
->code
= BPF_JMP
| BPF_JLE
| bpf_src
;
732 insn
->code
= BPF_JMP
| BPF_JLT
| bpf_src
;
738 target
= i
+ fp
->jf
+ 1;
743 /* Other jumps are mapped into two insns: Jxx and JA. */
744 target
= i
+ fp
->jt
+ 1;
745 insn
->code
= BPF_JMP
| BPF_OP(fp
->code
) | bpf_src
;
749 insn
->code
= BPF_JMP
| BPF_JA
;
750 target
= i
+ fp
->jf
+ 1;
754 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
755 case BPF_LDX
| BPF_MSH
| BPF_B
: {
756 struct sock_filter tmp
= {
757 .code
= BPF_LD
| BPF_ABS
| BPF_B
,
764 *insn
++ = BPF_MOV64_REG(BPF_REG_X
, BPF_REG_A
);
765 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
766 convert_bpf_ld_abs(&tmp
, &insn
);
769 *insn
++ = BPF_ALU32_IMM(BPF_AND
, BPF_REG_A
, 0xf);
771 *insn
++ = BPF_ALU32_IMM(BPF_LSH
, BPF_REG_A
, 2);
773 *insn
++ = BPF_MOV64_REG(BPF_REG_TMP
, BPF_REG_X
);
775 *insn
++ = BPF_MOV64_REG(BPF_REG_X
, BPF_REG_A
);
777 *insn
= BPF_MOV64_REG(BPF_REG_A
, BPF_REG_TMP
);
780 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
781 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
783 case BPF_RET
| BPF_A
:
784 case BPF_RET
| BPF_K
:
785 if (BPF_RVAL(fp
->code
) == BPF_K
)
786 *insn
++ = BPF_MOV32_RAW(BPF_K
, BPF_REG_0
,
788 *insn
= BPF_EXIT_INSN();
791 /* Store to stack. */
794 stack_off
= fp
->k
* 4 + 4;
795 *insn
= BPF_STX_MEM(BPF_W
, BPF_REG_FP
, BPF_CLASS(fp
->code
) ==
796 BPF_ST
? BPF_REG_A
: BPF_REG_X
,
798 /* check_load_and_stores() verifies that classic BPF can
799 * load from stack only after write, so tracking
800 * stack_depth for ST|STX insns is enough
802 if (new_prog
&& new_prog
->aux
->stack_depth
< stack_off
)
803 new_prog
->aux
->stack_depth
= stack_off
;
806 /* Load from stack. */
807 case BPF_LD
| BPF_MEM
:
808 case BPF_LDX
| BPF_MEM
:
809 stack_off
= fp
->k
* 4 + 4;
810 *insn
= BPF_LDX_MEM(BPF_W
, BPF_CLASS(fp
->code
) == BPF_LD
?
811 BPF_REG_A
: BPF_REG_X
, BPF_REG_FP
,
816 case BPF_LD
| BPF_IMM
:
817 case BPF_LDX
| BPF_IMM
:
818 *insn
= BPF_MOV32_IMM(BPF_CLASS(fp
->code
) == BPF_LD
?
819 BPF_REG_A
: BPF_REG_X
, fp
->k
);
823 case BPF_MISC
| BPF_TAX
:
824 *insn
= BPF_MOV64_REG(BPF_REG_X
, BPF_REG_A
);
828 case BPF_MISC
| BPF_TXA
:
829 *insn
= BPF_MOV64_REG(BPF_REG_A
, BPF_REG_X
);
832 /* A = skb->len or X = skb->len */
833 case BPF_LD
| BPF_W
| BPF_LEN
:
834 case BPF_LDX
| BPF_W
| BPF_LEN
:
835 *insn
= BPF_LDX_MEM(BPF_W
, BPF_CLASS(fp
->code
) == BPF_LD
?
836 BPF_REG_A
: BPF_REG_X
, BPF_REG_CTX
,
837 offsetof(struct sk_buff
, len
));
840 /* Access seccomp_data fields. */
841 case BPF_LDX
| BPF_ABS
| BPF_W
:
842 /* A = *(u32 *) (ctx + K) */
843 *insn
= BPF_LDX_MEM(BPF_W
, BPF_REG_A
, BPF_REG_CTX
, fp
->k
);
846 /* Unknown instruction. */
853 memcpy(new_insn
, tmp_insns
,
854 sizeof(*insn
) * (insn
- tmp_insns
));
855 new_insn
+= insn
- tmp_insns
;
859 /* Only calculating new length. */
860 *new_len
= new_insn
- first_insn
;
862 *new_len
+= 4; /* Prologue bits. */
867 if (new_flen
!= new_insn
- first_insn
) {
868 new_flen
= new_insn
- first_insn
;
875 BUG_ON(*new_len
!= new_flen
);
884 * As we dont want to clear mem[] array for each packet going through
885 * __bpf_prog_run(), we check that filter loaded by user never try to read
886 * a cell if not previously written, and we check all branches to be sure
887 * a malicious user doesn't try to abuse us.
889 static int check_load_and_stores(const struct sock_filter
*filter
, int flen
)
891 u16
*masks
, memvalid
= 0; /* One bit per cell, 16 cells */
894 BUILD_BUG_ON(BPF_MEMWORDS
> 16);
896 masks
= kmalloc_array(flen
, sizeof(*masks
), GFP_KERNEL
);
900 memset(masks
, 0xff, flen
* sizeof(*masks
));
902 for (pc
= 0; pc
< flen
; pc
++) {
903 memvalid
&= masks
[pc
];
905 switch (filter
[pc
].code
) {
908 memvalid
|= (1 << filter
[pc
].k
);
910 case BPF_LD
| BPF_MEM
:
911 case BPF_LDX
| BPF_MEM
:
912 if (!(memvalid
& (1 << filter
[pc
].k
))) {
917 case BPF_JMP
| BPF_JA
:
918 /* A jump must set masks on target */
919 masks
[pc
+ 1 + filter
[pc
].k
] &= memvalid
;
922 case BPF_JMP
| BPF_JEQ
| BPF_K
:
923 case BPF_JMP
| BPF_JEQ
| BPF_X
:
924 case BPF_JMP
| BPF_JGE
| BPF_K
:
925 case BPF_JMP
| BPF_JGE
| BPF_X
:
926 case BPF_JMP
| BPF_JGT
| BPF_K
:
927 case BPF_JMP
| BPF_JGT
| BPF_X
:
928 case BPF_JMP
| BPF_JSET
| BPF_K
:
929 case BPF_JMP
| BPF_JSET
| BPF_X
:
930 /* A jump must set masks on targets */
931 masks
[pc
+ 1 + filter
[pc
].jt
] &= memvalid
;
932 masks
[pc
+ 1 + filter
[pc
].jf
] &= memvalid
;
942 static bool chk_code_allowed(u16 code_to_probe
)
944 static const bool codes
[] = {
945 /* 32 bit ALU operations */
946 [BPF_ALU
| BPF_ADD
| BPF_K
] = true,
947 [BPF_ALU
| BPF_ADD
| BPF_X
] = true,
948 [BPF_ALU
| BPF_SUB
| BPF_K
] = true,
949 [BPF_ALU
| BPF_SUB
| BPF_X
] = true,
950 [BPF_ALU
| BPF_MUL
| BPF_K
] = true,
951 [BPF_ALU
| BPF_MUL
| BPF_X
] = true,
952 [BPF_ALU
| BPF_DIV
| BPF_K
] = true,
953 [BPF_ALU
| BPF_DIV
| BPF_X
] = true,
954 [BPF_ALU
| BPF_MOD
| BPF_K
] = true,
955 [BPF_ALU
| BPF_MOD
| BPF_X
] = true,
956 [BPF_ALU
| BPF_AND
| BPF_K
] = true,
957 [BPF_ALU
| BPF_AND
| BPF_X
] = true,
958 [BPF_ALU
| BPF_OR
| BPF_K
] = true,
959 [BPF_ALU
| BPF_OR
| BPF_X
] = true,
960 [BPF_ALU
| BPF_XOR
| BPF_K
] = true,
961 [BPF_ALU
| BPF_XOR
| BPF_X
] = true,
962 [BPF_ALU
| BPF_LSH
| BPF_K
] = true,
963 [BPF_ALU
| BPF_LSH
| BPF_X
] = true,
964 [BPF_ALU
| BPF_RSH
| BPF_K
] = true,
965 [BPF_ALU
| BPF_RSH
| BPF_X
] = true,
966 [BPF_ALU
| BPF_NEG
] = true,
967 /* Load instructions */
968 [BPF_LD
| BPF_W
| BPF_ABS
] = true,
969 [BPF_LD
| BPF_H
| BPF_ABS
] = true,
970 [BPF_LD
| BPF_B
| BPF_ABS
] = true,
971 [BPF_LD
| BPF_W
| BPF_LEN
] = true,
972 [BPF_LD
| BPF_W
| BPF_IND
] = true,
973 [BPF_LD
| BPF_H
| BPF_IND
] = true,
974 [BPF_LD
| BPF_B
| BPF_IND
] = true,
975 [BPF_LD
| BPF_IMM
] = true,
976 [BPF_LD
| BPF_MEM
] = true,
977 [BPF_LDX
| BPF_W
| BPF_LEN
] = true,
978 [BPF_LDX
| BPF_B
| BPF_MSH
] = true,
979 [BPF_LDX
| BPF_IMM
] = true,
980 [BPF_LDX
| BPF_MEM
] = true,
981 /* Store instructions */
984 /* Misc instructions */
985 [BPF_MISC
| BPF_TAX
] = true,
986 [BPF_MISC
| BPF_TXA
] = true,
987 /* Return instructions */
988 [BPF_RET
| BPF_K
] = true,
989 [BPF_RET
| BPF_A
] = true,
990 /* Jump instructions */
991 [BPF_JMP
| BPF_JA
] = true,
992 [BPF_JMP
| BPF_JEQ
| BPF_K
] = true,
993 [BPF_JMP
| BPF_JEQ
| BPF_X
] = true,
994 [BPF_JMP
| BPF_JGE
| BPF_K
] = true,
995 [BPF_JMP
| BPF_JGE
| BPF_X
] = true,
996 [BPF_JMP
| BPF_JGT
| BPF_K
] = true,
997 [BPF_JMP
| BPF_JGT
| BPF_X
] = true,
998 [BPF_JMP
| BPF_JSET
| BPF_K
] = true,
999 [BPF_JMP
| BPF_JSET
| BPF_X
] = true,
1002 if (code_to_probe
>= ARRAY_SIZE(codes
))
1005 return codes
[code_to_probe
];
1008 static bool bpf_check_basics_ok(const struct sock_filter
*filter
,
1013 if (flen
== 0 || flen
> BPF_MAXINSNS
)
1020 * bpf_check_classic - verify socket filter code
1021 * @filter: filter to verify
1022 * @flen: length of filter
1024 * Check the user's filter code. If we let some ugly
1025 * filter code slip through kaboom! The filter must contain
1026 * no references or jumps that are out of range, no illegal
1027 * instructions, and must end with a RET instruction.
1029 * All jumps are forward as they are not signed.
1031 * Returns 0 if the rule set is legal or -EINVAL if not.
1033 static int bpf_check_classic(const struct sock_filter
*filter
,
1039 /* Check the filter code now */
1040 for (pc
= 0; pc
< flen
; pc
++) {
1041 const struct sock_filter
*ftest
= &filter
[pc
];
1043 /* May we actually operate on this code? */
1044 if (!chk_code_allowed(ftest
->code
))
1047 /* Some instructions need special checks */
1048 switch (ftest
->code
) {
1049 case BPF_ALU
| BPF_DIV
| BPF_K
:
1050 case BPF_ALU
| BPF_MOD
| BPF_K
:
1051 /* Check for division by zero */
1055 case BPF_ALU
| BPF_LSH
| BPF_K
:
1056 case BPF_ALU
| BPF_RSH
| BPF_K
:
1060 case BPF_LD
| BPF_MEM
:
1061 case BPF_LDX
| BPF_MEM
:
1064 /* Check for invalid memory addresses */
1065 if (ftest
->k
>= BPF_MEMWORDS
)
1068 case BPF_JMP
| BPF_JA
:
1069 /* Note, the large ftest->k might cause loops.
1070 * Compare this with conditional jumps below,
1071 * where offsets are limited. --ANK (981016)
1073 if (ftest
->k
>= (unsigned int)(flen
- pc
- 1))
1076 case BPF_JMP
| BPF_JEQ
| BPF_K
:
1077 case BPF_JMP
| BPF_JEQ
| BPF_X
:
1078 case BPF_JMP
| BPF_JGE
| BPF_K
:
1079 case BPF_JMP
| BPF_JGE
| BPF_X
:
1080 case BPF_JMP
| BPF_JGT
| BPF_K
:
1081 case BPF_JMP
| BPF_JGT
| BPF_X
:
1082 case BPF_JMP
| BPF_JSET
| BPF_K
:
1083 case BPF_JMP
| BPF_JSET
| BPF_X
:
1084 /* Both conditionals must be safe */
1085 if (pc
+ ftest
->jt
+ 1 >= flen
||
1086 pc
+ ftest
->jf
+ 1 >= flen
)
1089 case BPF_LD
| BPF_W
| BPF_ABS
:
1090 case BPF_LD
| BPF_H
| BPF_ABS
:
1091 case BPF_LD
| BPF_B
| BPF_ABS
:
1093 if (bpf_anc_helper(ftest
) & BPF_ANC
)
1095 /* Ancillary operation unknown or unsupported */
1096 if (anc_found
== false && ftest
->k
>= SKF_AD_OFF
)
1101 /* Last instruction must be a RET code */
1102 switch (filter
[flen
- 1].code
) {
1103 case BPF_RET
| BPF_K
:
1104 case BPF_RET
| BPF_A
:
1105 return check_load_and_stores(filter
, flen
);
1111 static int bpf_prog_store_orig_filter(struct bpf_prog
*fp
,
1112 const struct sock_fprog
*fprog
)
1114 unsigned int fsize
= bpf_classic_proglen(fprog
);
1115 struct sock_fprog_kern
*fkprog
;
1117 fp
->orig_prog
= kmalloc(sizeof(*fkprog
), GFP_KERNEL
);
1121 fkprog
= fp
->orig_prog
;
1122 fkprog
->len
= fprog
->len
;
1124 fkprog
->filter
= kmemdup(fp
->insns
, fsize
,
1125 GFP_KERNEL
| __GFP_NOWARN
);
1126 if (!fkprog
->filter
) {
1127 kfree(fp
->orig_prog
);
1134 static void bpf_release_orig_filter(struct bpf_prog
*fp
)
1136 struct sock_fprog_kern
*fprog
= fp
->orig_prog
;
1139 kfree(fprog
->filter
);
1144 static void __bpf_prog_release(struct bpf_prog
*prog
)
1146 if (prog
->type
== BPF_PROG_TYPE_SOCKET_FILTER
) {
1149 bpf_release_orig_filter(prog
);
1150 bpf_prog_free(prog
);
1154 static void __sk_filter_release(struct sk_filter
*fp
)
1156 __bpf_prog_release(fp
->prog
);
1161 * sk_filter_release_rcu - Release a socket filter by rcu_head
1162 * @rcu: rcu_head that contains the sk_filter to free
1164 static void sk_filter_release_rcu(struct rcu_head
*rcu
)
1166 struct sk_filter
*fp
= container_of(rcu
, struct sk_filter
, rcu
);
1168 __sk_filter_release(fp
);
1172 * sk_filter_release - release a socket filter
1173 * @fp: filter to remove
1175 * Remove a filter from a socket and release its resources.
1177 static void sk_filter_release(struct sk_filter
*fp
)
1179 if (refcount_dec_and_test(&fp
->refcnt
))
1180 call_rcu(&fp
->rcu
, sk_filter_release_rcu
);
1183 void sk_filter_uncharge(struct sock
*sk
, struct sk_filter
*fp
)
1185 u32 filter_size
= bpf_prog_size(fp
->prog
->len
);
1187 atomic_sub(filter_size
, &sk
->sk_omem_alloc
);
1188 sk_filter_release(fp
);
1191 /* try to charge the socket memory if there is space available
1192 * return true on success
1194 static bool __sk_filter_charge(struct sock
*sk
, struct sk_filter
*fp
)
1196 u32 filter_size
= bpf_prog_size(fp
->prog
->len
);
1198 /* same check as in sock_kmalloc() */
1199 if (filter_size
<= sysctl_optmem_max
&&
1200 atomic_read(&sk
->sk_omem_alloc
) + filter_size
< sysctl_optmem_max
) {
1201 atomic_add(filter_size
, &sk
->sk_omem_alloc
);
1207 bool sk_filter_charge(struct sock
*sk
, struct sk_filter
*fp
)
1209 if (!refcount_inc_not_zero(&fp
->refcnt
))
1212 if (!__sk_filter_charge(sk
, fp
)) {
1213 sk_filter_release(fp
);
1219 static struct bpf_prog
*bpf_migrate_filter(struct bpf_prog
*fp
)
1221 struct sock_filter
*old_prog
;
1222 struct bpf_prog
*old_fp
;
1223 int err
, new_len
, old_len
= fp
->len
;
1224 bool seen_ld_abs
= false;
1226 /* We are free to overwrite insns et al right here as it
1227 * won't be used at this point in time anymore internally
1228 * after the migration to the internal BPF instruction
1231 BUILD_BUG_ON(sizeof(struct sock_filter
) !=
1232 sizeof(struct bpf_insn
));
1234 /* Conversion cannot happen on overlapping memory areas,
1235 * so we need to keep the user BPF around until the 2nd
1236 * pass. At this time, the user BPF is stored in fp->insns.
1238 old_prog
= kmemdup(fp
->insns
, old_len
* sizeof(struct sock_filter
),
1239 GFP_KERNEL
| __GFP_NOWARN
);
1245 /* 1st pass: calculate the new program length. */
1246 err
= bpf_convert_filter(old_prog
, old_len
, NULL
, &new_len
,
1251 /* Expand fp for appending the new filter representation. */
1253 fp
= bpf_prog_realloc(old_fp
, bpf_prog_size(new_len
), 0);
1255 /* The old_fp is still around in case we couldn't
1256 * allocate new memory, so uncharge on that one.
1265 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1266 err
= bpf_convert_filter(old_prog
, old_len
, fp
, &new_len
,
1269 /* 2nd bpf_convert_filter() can fail only if it fails
1270 * to allocate memory, remapping must succeed. Note,
1271 * that at this time old_fp has already been released
1276 fp
= bpf_prog_select_runtime(fp
, &err
);
1286 __bpf_prog_release(fp
);
1287 return ERR_PTR(err
);
1290 static struct bpf_prog
*bpf_prepare_filter(struct bpf_prog
*fp
,
1291 bpf_aux_classic_check_t trans
)
1295 fp
->bpf_func
= NULL
;
1298 err
= bpf_check_classic(fp
->insns
, fp
->len
);
1300 __bpf_prog_release(fp
);
1301 return ERR_PTR(err
);
1304 /* There might be additional checks and transformations
1305 * needed on classic filters, f.e. in case of seccomp.
1308 err
= trans(fp
->insns
, fp
->len
);
1310 __bpf_prog_release(fp
);
1311 return ERR_PTR(err
);
1315 /* Probe if we can JIT compile the filter and if so, do
1316 * the compilation of the filter.
1318 bpf_jit_compile(fp
);
1320 /* JIT compiler couldn't process this filter, so do the
1321 * internal BPF translation for the optimized interpreter.
1324 fp
= bpf_migrate_filter(fp
);
1330 * bpf_prog_create - create an unattached filter
1331 * @pfp: the unattached filter that is created
1332 * @fprog: the filter program
1334 * Create a filter independent of any socket. We first run some
1335 * sanity checks on it to make sure it does not explode on us later.
1336 * If an error occurs or there is insufficient memory for the filter
1337 * a negative errno code is returned. On success the return is zero.
1339 int bpf_prog_create(struct bpf_prog
**pfp
, struct sock_fprog_kern
*fprog
)
1341 unsigned int fsize
= bpf_classic_proglen(fprog
);
1342 struct bpf_prog
*fp
;
1344 /* Make sure new filter is there and in the right amounts. */
1345 if (!bpf_check_basics_ok(fprog
->filter
, fprog
->len
))
1348 fp
= bpf_prog_alloc(bpf_prog_size(fprog
->len
), 0);
1352 memcpy(fp
->insns
, fprog
->filter
, fsize
);
1354 fp
->len
= fprog
->len
;
1355 /* Since unattached filters are not copied back to user
1356 * space through sk_get_filter(), we do not need to hold
1357 * a copy here, and can spare us the work.
1359 fp
->orig_prog
= NULL
;
1361 /* bpf_prepare_filter() already takes care of freeing
1362 * memory in case something goes wrong.
1364 fp
= bpf_prepare_filter(fp
, NULL
);
1371 EXPORT_SYMBOL_GPL(bpf_prog_create
);
1374 * bpf_prog_create_from_user - create an unattached filter from user buffer
1375 * @pfp: the unattached filter that is created
1376 * @fprog: the filter program
1377 * @trans: post-classic verifier transformation handler
1378 * @save_orig: save classic BPF program
1380 * This function effectively does the same as bpf_prog_create(), only
1381 * that it builds up its insns buffer from user space provided buffer.
1382 * It also allows for passing a bpf_aux_classic_check_t handler.
1384 int bpf_prog_create_from_user(struct bpf_prog
**pfp
, struct sock_fprog
*fprog
,
1385 bpf_aux_classic_check_t trans
, bool save_orig
)
1387 unsigned int fsize
= bpf_classic_proglen(fprog
);
1388 struct bpf_prog
*fp
;
1391 /* Make sure new filter is there and in the right amounts. */
1392 if (!bpf_check_basics_ok(fprog
->filter
, fprog
->len
))
1395 fp
= bpf_prog_alloc(bpf_prog_size(fprog
->len
), 0);
1399 if (copy_from_user(fp
->insns
, fprog
->filter
, fsize
)) {
1400 __bpf_prog_free(fp
);
1404 fp
->len
= fprog
->len
;
1405 fp
->orig_prog
= NULL
;
1408 err
= bpf_prog_store_orig_filter(fp
, fprog
);
1410 __bpf_prog_free(fp
);
1415 /* bpf_prepare_filter() already takes care of freeing
1416 * memory in case something goes wrong.
1418 fp
= bpf_prepare_filter(fp
, trans
);
1425 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user
);
1427 void bpf_prog_destroy(struct bpf_prog
*fp
)
1429 __bpf_prog_release(fp
);
1431 EXPORT_SYMBOL_GPL(bpf_prog_destroy
);
1433 static int __sk_attach_prog(struct bpf_prog
*prog
, struct sock
*sk
)
1435 struct sk_filter
*fp
, *old_fp
;
1437 fp
= kmalloc(sizeof(*fp
), GFP_KERNEL
);
1443 if (!__sk_filter_charge(sk
, fp
)) {
1447 refcount_set(&fp
->refcnt
, 1);
1449 old_fp
= rcu_dereference_protected(sk
->sk_filter
,
1450 lockdep_sock_is_held(sk
));
1451 rcu_assign_pointer(sk
->sk_filter
, fp
);
1454 sk_filter_uncharge(sk
, old_fp
);
1460 struct bpf_prog
*__get_filter(struct sock_fprog
*fprog
, struct sock
*sk
)
1462 unsigned int fsize
= bpf_classic_proglen(fprog
);
1463 struct bpf_prog
*prog
;
1466 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
1467 return ERR_PTR(-EPERM
);
1469 /* Make sure new filter is there and in the right amounts. */
1470 if (!bpf_check_basics_ok(fprog
->filter
, fprog
->len
))
1471 return ERR_PTR(-EINVAL
);
1473 prog
= bpf_prog_alloc(bpf_prog_size(fprog
->len
), 0);
1475 return ERR_PTR(-ENOMEM
);
1477 if (copy_from_user(prog
->insns
, fprog
->filter
, fsize
)) {
1478 __bpf_prog_free(prog
);
1479 return ERR_PTR(-EFAULT
);
1482 prog
->len
= fprog
->len
;
1484 err
= bpf_prog_store_orig_filter(prog
, fprog
);
1486 __bpf_prog_free(prog
);
1487 return ERR_PTR(-ENOMEM
);
1490 /* bpf_prepare_filter() already takes care of freeing
1491 * memory in case something goes wrong.
1493 return bpf_prepare_filter(prog
, NULL
);
1497 * sk_attach_filter - attach a socket filter
1498 * @fprog: the filter program
1499 * @sk: the socket to use
1501 * Attach the user's filter code. We first run some sanity checks on
1502 * it to make sure it does not explode on us later. If an error
1503 * occurs or there is insufficient memory for the filter a negative
1504 * errno code is returned. On success the return is zero.
1506 int sk_attach_filter(struct sock_fprog
*fprog
, struct sock
*sk
)
1508 struct bpf_prog
*prog
= __get_filter(fprog
, sk
);
1512 return PTR_ERR(prog
);
1514 err
= __sk_attach_prog(prog
, sk
);
1516 __bpf_prog_release(prog
);
1522 EXPORT_SYMBOL_GPL(sk_attach_filter
);
1524 int sk_reuseport_attach_filter(struct sock_fprog
*fprog
, struct sock
*sk
)
1526 struct bpf_prog
*prog
= __get_filter(fprog
, sk
);
1530 return PTR_ERR(prog
);
1532 if (bpf_prog_size(prog
->len
) > sysctl_optmem_max
)
1535 err
= reuseport_attach_prog(sk
, prog
);
1538 __bpf_prog_release(prog
);
1543 static struct bpf_prog
*__get_bpf(u32 ufd
, struct sock
*sk
)
1545 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
1546 return ERR_PTR(-EPERM
);
1548 return bpf_prog_get_type(ufd
, BPF_PROG_TYPE_SOCKET_FILTER
);
1551 int sk_attach_bpf(u32 ufd
, struct sock
*sk
)
1553 struct bpf_prog
*prog
= __get_bpf(ufd
, sk
);
1557 return PTR_ERR(prog
);
1559 err
= __sk_attach_prog(prog
, sk
);
1568 int sk_reuseport_attach_bpf(u32 ufd
, struct sock
*sk
)
1570 struct bpf_prog
*prog
;
1573 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
1576 prog
= bpf_prog_get_type(ufd
, BPF_PROG_TYPE_SOCKET_FILTER
);
1577 if (IS_ERR(prog
) && PTR_ERR(prog
) == -EINVAL
)
1578 prog
= bpf_prog_get_type(ufd
, BPF_PROG_TYPE_SK_REUSEPORT
);
1580 return PTR_ERR(prog
);
1582 if (prog
->type
== BPF_PROG_TYPE_SK_REUSEPORT
) {
1583 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1584 * bpf prog (e.g. sockmap). It depends on the
1585 * limitation imposed by bpf_prog_load().
1586 * Hence, sysctl_optmem_max is not checked.
1588 if ((sk
->sk_type
!= SOCK_STREAM
&&
1589 sk
->sk_type
!= SOCK_DGRAM
) ||
1590 (sk
->sk_protocol
!= IPPROTO_UDP
&&
1591 sk
->sk_protocol
!= IPPROTO_TCP
) ||
1592 (sk
->sk_family
!= AF_INET
&&
1593 sk
->sk_family
!= AF_INET6
)) {
1598 /* BPF_PROG_TYPE_SOCKET_FILTER */
1599 if (bpf_prog_size(prog
->len
) > sysctl_optmem_max
) {
1605 err
= reuseport_attach_prog(sk
, prog
);
1613 void sk_reuseport_prog_free(struct bpf_prog
*prog
)
1618 if (prog
->type
== BPF_PROG_TYPE_SK_REUSEPORT
)
1621 bpf_prog_destroy(prog
);
1624 struct bpf_scratchpad
{
1626 __be32 diff
[MAX_BPF_STACK
/ sizeof(__be32
)];
1627 u8 buff
[MAX_BPF_STACK
];
1631 static DEFINE_PER_CPU(struct bpf_scratchpad
, bpf_sp
);
1633 static inline int __bpf_try_make_writable(struct sk_buff
*skb
,
1634 unsigned int write_len
)
1636 return skb_ensure_writable(skb
, write_len
);
1639 static inline int bpf_try_make_writable(struct sk_buff
*skb
,
1640 unsigned int write_len
)
1642 int err
= __bpf_try_make_writable(skb
, write_len
);
1644 bpf_compute_data_pointers(skb
);
1648 static int bpf_try_make_head_writable(struct sk_buff
*skb
)
1650 return bpf_try_make_writable(skb
, skb_headlen(skb
));
1653 static inline void bpf_push_mac_rcsum(struct sk_buff
*skb
)
1655 if (skb_at_tc_ingress(skb
))
1656 skb_postpush_rcsum(skb
, skb_mac_header(skb
), skb
->mac_len
);
1659 static inline void bpf_pull_mac_rcsum(struct sk_buff
*skb
)
1661 if (skb_at_tc_ingress(skb
))
1662 skb_postpull_rcsum(skb
, skb_mac_header(skb
), skb
->mac_len
);
1665 BPF_CALL_5(bpf_skb_store_bytes
, struct sk_buff
*, skb
, u32
, offset
,
1666 const void *, from
, u32
, len
, u64
, flags
)
1670 if (unlikely(flags
& ~(BPF_F_RECOMPUTE_CSUM
| BPF_F_INVALIDATE_HASH
)))
1672 if (unlikely(offset
> 0xffff))
1674 if (unlikely(bpf_try_make_writable(skb
, offset
+ len
)))
1677 ptr
= skb
->data
+ offset
;
1678 if (flags
& BPF_F_RECOMPUTE_CSUM
)
1679 __skb_postpull_rcsum(skb
, ptr
, len
, offset
);
1681 memcpy(ptr
, from
, len
);
1683 if (flags
& BPF_F_RECOMPUTE_CSUM
)
1684 __skb_postpush_rcsum(skb
, ptr
, len
, offset
);
1685 if (flags
& BPF_F_INVALIDATE_HASH
)
1686 skb_clear_hash(skb
);
1691 static const struct bpf_func_proto bpf_skb_store_bytes_proto
= {
1692 .func
= bpf_skb_store_bytes
,
1694 .ret_type
= RET_INTEGER
,
1695 .arg1_type
= ARG_PTR_TO_CTX
,
1696 .arg2_type
= ARG_ANYTHING
,
1697 .arg3_type
= ARG_PTR_TO_MEM
,
1698 .arg4_type
= ARG_CONST_SIZE
,
1699 .arg5_type
= ARG_ANYTHING
,
1702 BPF_CALL_4(bpf_skb_load_bytes
, const struct sk_buff
*, skb
, u32
, offset
,
1703 void *, to
, u32
, len
)
1707 if (unlikely(offset
> 0xffff))
1710 ptr
= skb_header_pointer(skb
, offset
, len
, to
);
1714 memcpy(to
, ptr
, len
);
1722 static const struct bpf_func_proto bpf_skb_load_bytes_proto
= {
1723 .func
= bpf_skb_load_bytes
,
1725 .ret_type
= RET_INTEGER
,
1726 .arg1_type
= ARG_PTR_TO_CTX
,
1727 .arg2_type
= ARG_ANYTHING
,
1728 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
1729 .arg4_type
= ARG_CONST_SIZE
,
1732 BPF_CALL_5(bpf_skb_load_bytes_relative
, const struct sk_buff
*, skb
,
1733 u32
, offset
, void *, to
, u32
, len
, u32
, start_header
)
1735 u8
*end
= skb_tail_pointer(skb
);
1736 u8
*net
= skb_network_header(skb
);
1737 u8
*mac
= skb_mac_header(skb
);
1740 if (unlikely(offset
> 0xffff || len
> (end
- mac
)))
1743 switch (start_header
) {
1744 case BPF_HDR_START_MAC
:
1747 case BPF_HDR_START_NET
:
1754 if (likely(ptr
>= mac
&& ptr
+ len
<= end
)) {
1755 memcpy(to
, ptr
, len
);
1764 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto
= {
1765 .func
= bpf_skb_load_bytes_relative
,
1767 .ret_type
= RET_INTEGER
,
1768 .arg1_type
= ARG_PTR_TO_CTX
,
1769 .arg2_type
= ARG_ANYTHING
,
1770 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
1771 .arg4_type
= ARG_CONST_SIZE
,
1772 .arg5_type
= ARG_ANYTHING
,
1775 BPF_CALL_2(bpf_skb_pull_data
, struct sk_buff
*, skb
, u32
, len
)
1777 /* Idea is the following: should the needed direct read/write
1778 * test fail during runtime, we can pull in more data and redo
1779 * again, since implicitly, we invalidate previous checks here.
1781 * Or, since we know how much we need to make read/writeable,
1782 * this can be done once at the program beginning for direct
1783 * access case. By this we overcome limitations of only current
1784 * headroom being accessible.
1786 return bpf_try_make_writable(skb
, len
? : skb_headlen(skb
));
1789 static const struct bpf_func_proto bpf_skb_pull_data_proto
= {
1790 .func
= bpf_skb_pull_data
,
1792 .ret_type
= RET_INTEGER
,
1793 .arg1_type
= ARG_PTR_TO_CTX
,
1794 .arg2_type
= ARG_ANYTHING
,
1797 BPF_CALL_1(bpf_sk_fullsock
, struct sock
*, sk
)
1799 sk
= sk_to_full_sk(sk
);
1801 return sk_fullsock(sk
) ? (unsigned long)sk
: (unsigned long)NULL
;
1804 static const struct bpf_func_proto bpf_sk_fullsock_proto
= {
1805 .func
= bpf_sk_fullsock
,
1807 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
1808 .arg1_type
= ARG_PTR_TO_SOCK_COMMON
,
1811 static inline int sk_skb_try_make_writable(struct sk_buff
*skb
,
1812 unsigned int write_len
)
1814 int err
= __bpf_try_make_writable(skb
, write_len
);
1816 bpf_compute_data_end_sk_skb(skb
);
1820 BPF_CALL_2(sk_skb_pull_data
, struct sk_buff
*, skb
, u32
, len
)
1822 /* Idea is the following: should the needed direct read/write
1823 * test fail during runtime, we can pull in more data and redo
1824 * again, since implicitly, we invalidate previous checks here.
1826 * Or, since we know how much we need to make read/writeable,
1827 * this can be done once at the program beginning for direct
1828 * access case. By this we overcome limitations of only current
1829 * headroom being accessible.
1831 return sk_skb_try_make_writable(skb
, len
? : skb_headlen(skb
));
1834 static const struct bpf_func_proto sk_skb_pull_data_proto
= {
1835 .func
= sk_skb_pull_data
,
1837 .ret_type
= RET_INTEGER
,
1838 .arg1_type
= ARG_PTR_TO_CTX
,
1839 .arg2_type
= ARG_ANYTHING
,
1842 BPF_CALL_5(bpf_l3_csum_replace
, struct sk_buff
*, skb
, u32
, offset
,
1843 u64
, from
, u64
, to
, u64
, flags
)
1847 if (unlikely(flags
& ~(BPF_F_HDR_FIELD_MASK
)))
1849 if (unlikely(offset
> 0xffff || offset
& 1))
1851 if (unlikely(bpf_try_make_writable(skb
, offset
+ sizeof(*ptr
))))
1854 ptr
= (__sum16
*)(skb
->data
+ offset
);
1855 switch (flags
& BPF_F_HDR_FIELD_MASK
) {
1857 if (unlikely(from
!= 0))
1860 csum_replace_by_diff(ptr
, to
);
1863 csum_replace2(ptr
, from
, to
);
1866 csum_replace4(ptr
, from
, to
);
1875 static const struct bpf_func_proto bpf_l3_csum_replace_proto
= {
1876 .func
= bpf_l3_csum_replace
,
1878 .ret_type
= RET_INTEGER
,
1879 .arg1_type
= ARG_PTR_TO_CTX
,
1880 .arg2_type
= ARG_ANYTHING
,
1881 .arg3_type
= ARG_ANYTHING
,
1882 .arg4_type
= ARG_ANYTHING
,
1883 .arg5_type
= ARG_ANYTHING
,
1886 BPF_CALL_5(bpf_l4_csum_replace
, struct sk_buff
*, skb
, u32
, offset
,
1887 u64
, from
, u64
, to
, u64
, flags
)
1889 bool is_pseudo
= flags
& BPF_F_PSEUDO_HDR
;
1890 bool is_mmzero
= flags
& BPF_F_MARK_MANGLED_0
;
1891 bool do_mforce
= flags
& BPF_F_MARK_ENFORCE
;
1894 if (unlikely(flags
& ~(BPF_F_MARK_MANGLED_0
| BPF_F_MARK_ENFORCE
|
1895 BPF_F_PSEUDO_HDR
| BPF_F_HDR_FIELD_MASK
)))
1897 if (unlikely(offset
> 0xffff || offset
& 1))
1899 if (unlikely(bpf_try_make_writable(skb
, offset
+ sizeof(*ptr
))))
1902 ptr
= (__sum16
*)(skb
->data
+ offset
);
1903 if (is_mmzero
&& !do_mforce
&& !*ptr
)
1906 switch (flags
& BPF_F_HDR_FIELD_MASK
) {
1908 if (unlikely(from
!= 0))
1911 inet_proto_csum_replace_by_diff(ptr
, skb
, to
, is_pseudo
);
1914 inet_proto_csum_replace2(ptr
, skb
, from
, to
, is_pseudo
);
1917 inet_proto_csum_replace4(ptr
, skb
, from
, to
, is_pseudo
);
1923 if (is_mmzero
&& !*ptr
)
1924 *ptr
= CSUM_MANGLED_0
;
1928 static const struct bpf_func_proto bpf_l4_csum_replace_proto
= {
1929 .func
= bpf_l4_csum_replace
,
1931 .ret_type
= RET_INTEGER
,
1932 .arg1_type
= ARG_PTR_TO_CTX
,
1933 .arg2_type
= ARG_ANYTHING
,
1934 .arg3_type
= ARG_ANYTHING
,
1935 .arg4_type
= ARG_ANYTHING
,
1936 .arg5_type
= ARG_ANYTHING
,
1939 BPF_CALL_5(bpf_csum_diff
, __be32
*, from
, u32
, from_size
,
1940 __be32
*, to
, u32
, to_size
, __wsum
, seed
)
1942 struct bpf_scratchpad
*sp
= this_cpu_ptr(&bpf_sp
);
1943 u32 diff_size
= from_size
+ to_size
;
1946 /* This is quite flexible, some examples:
1948 * from_size == 0, to_size > 0, seed := csum --> pushing data
1949 * from_size > 0, to_size == 0, seed := csum --> pulling data
1950 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1952 * Even for diffing, from_size and to_size don't need to be equal.
1954 if (unlikely(((from_size
| to_size
) & (sizeof(__be32
) - 1)) ||
1955 diff_size
> sizeof(sp
->diff
)))
1958 for (i
= 0; i
< from_size
/ sizeof(__be32
); i
++, j
++)
1959 sp
->diff
[j
] = ~from
[i
];
1960 for (i
= 0; i
< to_size
/ sizeof(__be32
); i
++, j
++)
1961 sp
->diff
[j
] = to
[i
];
1963 return csum_partial(sp
->diff
, diff_size
, seed
);
1966 static const struct bpf_func_proto bpf_csum_diff_proto
= {
1967 .func
= bpf_csum_diff
,
1970 .ret_type
= RET_INTEGER
,
1971 .arg1_type
= ARG_PTR_TO_MEM_OR_NULL
,
1972 .arg2_type
= ARG_CONST_SIZE_OR_ZERO
,
1973 .arg3_type
= ARG_PTR_TO_MEM_OR_NULL
,
1974 .arg4_type
= ARG_CONST_SIZE_OR_ZERO
,
1975 .arg5_type
= ARG_ANYTHING
,
1978 BPF_CALL_2(bpf_csum_update
, struct sk_buff
*, skb
, __wsum
, csum
)
1980 /* The interface is to be used in combination with bpf_csum_diff()
1981 * for direct packet writes. csum rotation for alignment as well
1982 * as emulating csum_sub() can be done from the eBPF program.
1984 if (skb
->ip_summed
== CHECKSUM_COMPLETE
)
1985 return (skb
->csum
= csum_add(skb
->csum
, csum
));
1990 static const struct bpf_func_proto bpf_csum_update_proto
= {
1991 .func
= bpf_csum_update
,
1993 .ret_type
= RET_INTEGER
,
1994 .arg1_type
= ARG_PTR_TO_CTX
,
1995 .arg2_type
= ARG_ANYTHING
,
1998 static inline int __bpf_rx_skb(struct net_device
*dev
, struct sk_buff
*skb
)
2000 return dev_forward_skb(dev
, skb
);
2003 static inline int __bpf_rx_skb_no_mac(struct net_device
*dev
,
2004 struct sk_buff
*skb
)
2006 int ret
= ____dev_forward_skb(dev
, skb
);
2010 ret
= netif_rx(skb
);
2016 static inline int __bpf_tx_skb(struct net_device
*dev
, struct sk_buff
*skb
)
2020 if (unlikely(__this_cpu_read(xmit_recursion
) > XMIT_RECURSION_LIMIT
)) {
2021 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2028 __this_cpu_inc(xmit_recursion
);
2029 ret
= dev_queue_xmit(skb
);
2030 __this_cpu_dec(xmit_recursion
);
2035 static int __bpf_redirect_no_mac(struct sk_buff
*skb
, struct net_device
*dev
,
2038 unsigned int mlen
= skb_network_offset(skb
);
2041 __skb_pull(skb
, mlen
);
2043 /* At ingress, the mac header has already been pulled once.
2044 * At egress, skb_pospull_rcsum has to be done in case that
2045 * the skb is originated from ingress (i.e. a forwarded skb)
2046 * to ensure that rcsum starts at net header.
2048 if (!skb_at_tc_ingress(skb
))
2049 skb_postpull_rcsum(skb
, skb_mac_header(skb
), mlen
);
2051 skb_pop_mac_header(skb
);
2052 skb_reset_mac_len(skb
);
2053 return flags
& BPF_F_INGRESS
?
2054 __bpf_rx_skb_no_mac(dev
, skb
) : __bpf_tx_skb(dev
, skb
);
2057 static int __bpf_redirect_common(struct sk_buff
*skb
, struct net_device
*dev
,
2060 /* Verify that a link layer header is carried */
2061 if (unlikely(skb
->mac_header
>= skb
->network_header
)) {
2066 bpf_push_mac_rcsum(skb
);
2067 return flags
& BPF_F_INGRESS
?
2068 __bpf_rx_skb(dev
, skb
) : __bpf_tx_skb(dev
, skb
);
2071 static int __bpf_redirect(struct sk_buff
*skb
, struct net_device
*dev
,
2074 if (dev_is_mac_header_xmit(dev
))
2075 return __bpf_redirect_common(skb
, dev
, flags
);
2077 return __bpf_redirect_no_mac(skb
, dev
, flags
);
2080 BPF_CALL_3(bpf_clone_redirect
, struct sk_buff
*, skb
, u32
, ifindex
, u64
, flags
)
2082 struct net_device
*dev
;
2083 struct sk_buff
*clone
;
2086 if (unlikely(flags
& ~(BPF_F_INGRESS
)))
2089 dev
= dev_get_by_index_rcu(dev_net(skb
->dev
), ifindex
);
2093 clone
= skb_clone(skb
, GFP_ATOMIC
);
2094 if (unlikely(!clone
))
2097 /* For direct write, we need to keep the invariant that the skbs
2098 * we're dealing with need to be uncloned. Should uncloning fail
2099 * here, we need to free the just generated clone to unclone once
2102 ret
= bpf_try_make_head_writable(skb
);
2103 if (unlikely(ret
)) {
2108 return __bpf_redirect(clone
, dev
, flags
);
2111 static const struct bpf_func_proto bpf_clone_redirect_proto
= {
2112 .func
= bpf_clone_redirect
,
2114 .ret_type
= RET_INTEGER
,
2115 .arg1_type
= ARG_PTR_TO_CTX
,
2116 .arg2_type
= ARG_ANYTHING
,
2117 .arg3_type
= ARG_ANYTHING
,
2120 DEFINE_PER_CPU(struct bpf_redirect_info
, bpf_redirect_info
);
2121 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info
);
2123 BPF_CALL_2(bpf_redirect
, u32
, ifindex
, u64
, flags
)
2125 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
2127 if (unlikely(flags
& ~(BPF_F_INGRESS
)))
2130 ri
->ifindex
= ifindex
;
2133 return TC_ACT_REDIRECT
;
2136 int skb_do_redirect(struct sk_buff
*skb
)
2138 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
2139 struct net_device
*dev
;
2141 dev
= dev_get_by_index_rcu(dev_net(skb
->dev
), ri
->ifindex
);
2143 if (unlikely(!dev
)) {
2148 return __bpf_redirect(skb
, dev
, ri
->flags
);
2151 static const struct bpf_func_proto bpf_redirect_proto
= {
2152 .func
= bpf_redirect
,
2154 .ret_type
= RET_INTEGER
,
2155 .arg1_type
= ARG_ANYTHING
,
2156 .arg2_type
= ARG_ANYTHING
,
2159 BPF_CALL_2(bpf_msg_apply_bytes
, struct sk_msg
*, msg
, u32
, bytes
)
2161 msg
->apply_bytes
= bytes
;
2165 static const struct bpf_func_proto bpf_msg_apply_bytes_proto
= {
2166 .func
= bpf_msg_apply_bytes
,
2168 .ret_type
= RET_INTEGER
,
2169 .arg1_type
= ARG_PTR_TO_CTX
,
2170 .arg2_type
= ARG_ANYTHING
,
2173 BPF_CALL_2(bpf_msg_cork_bytes
, struct sk_msg
*, msg
, u32
, bytes
)
2175 msg
->cork_bytes
= bytes
;
2179 static const struct bpf_func_proto bpf_msg_cork_bytes_proto
= {
2180 .func
= bpf_msg_cork_bytes
,
2182 .ret_type
= RET_INTEGER
,
2183 .arg1_type
= ARG_PTR_TO_CTX
,
2184 .arg2_type
= ARG_ANYTHING
,
2187 BPF_CALL_4(bpf_msg_pull_data
, struct sk_msg
*, msg
, u32
, start
,
2188 u32
, end
, u64
, flags
)
2190 u32 len
= 0, offset
= 0, copy
= 0, poffset
= 0, bytes
= end
- start
;
2191 u32 first_sge
, last_sge
, i
, shift
, bytes_sg_total
;
2192 struct scatterlist
*sge
;
2193 u8
*raw
, *to
, *from
;
2196 if (unlikely(flags
|| end
<= start
))
2199 /* First find the starting scatterlist element */
2202 len
= sk_msg_elem(msg
, i
)->length
;
2203 if (start
< offset
+ len
)
2206 sk_msg_iter_var_next(i
);
2207 } while (i
!= msg
->sg
.end
);
2209 if (unlikely(start
>= offset
+ len
))
2213 /* The start may point into the sg element so we need to also
2214 * account for the headroom.
2216 bytes_sg_total
= start
- offset
+ bytes
;
2217 if (!msg
->sg
.copy
[i
] && bytes_sg_total
<= len
)
2220 /* At this point we need to linearize multiple scatterlist
2221 * elements or a single shared page. Either way we need to
2222 * copy into a linear buffer exclusively owned by BPF. Then
2223 * place the buffer in the scatterlist and fixup the original
2224 * entries by removing the entries now in the linear buffer
2225 * and shifting the remaining entries. For now we do not try
2226 * to copy partial entries to avoid complexity of running out
2227 * of sg_entry slots. The downside is reading a single byte
2228 * will copy the entire sg entry.
2231 copy
+= sk_msg_elem(msg
, i
)->length
;
2232 sk_msg_iter_var_next(i
);
2233 if (bytes_sg_total
<= copy
)
2235 } while (i
!= msg
->sg
.end
);
2238 if (unlikely(bytes_sg_total
> copy
))
2241 page
= alloc_pages(__GFP_NOWARN
| GFP_ATOMIC
| __GFP_COMP
,
2243 if (unlikely(!page
))
2246 raw
= page_address(page
);
2249 sge
= sk_msg_elem(msg
, i
);
2250 from
= sg_virt(sge
);
2254 memcpy(to
, from
, len
);
2257 put_page(sg_page(sge
));
2259 sk_msg_iter_var_next(i
);
2260 } while (i
!= last_sge
);
2262 sg_set_page(&msg
->sg
.data
[first_sge
], page
, copy
, 0);
2264 /* To repair sg ring we need to shift entries. If we only
2265 * had a single entry though we can just replace it and
2266 * be done. Otherwise walk the ring and shift the entries.
2268 WARN_ON_ONCE(last_sge
== first_sge
);
2269 shift
= last_sge
> first_sge
?
2270 last_sge
- first_sge
- 1 :
2271 MAX_SKB_FRAGS
- first_sge
+ last_sge
- 1;
2276 sk_msg_iter_var_next(i
);
2280 if (i
+ shift
>= MAX_MSG_FRAGS
)
2281 move_from
= i
+ shift
- MAX_MSG_FRAGS
;
2283 move_from
= i
+ shift
;
2284 if (move_from
== msg
->sg
.end
)
2287 msg
->sg
.data
[i
] = msg
->sg
.data
[move_from
];
2288 msg
->sg
.data
[move_from
].length
= 0;
2289 msg
->sg
.data
[move_from
].page_link
= 0;
2290 msg
->sg
.data
[move_from
].offset
= 0;
2291 sk_msg_iter_var_next(i
);
2294 msg
->sg
.end
= msg
->sg
.end
- shift
> msg
->sg
.end
?
2295 msg
->sg
.end
- shift
+ MAX_MSG_FRAGS
:
2296 msg
->sg
.end
- shift
;
2298 msg
->data
= sg_virt(&msg
->sg
.data
[first_sge
]) + start
- offset
;
2299 msg
->data_end
= msg
->data
+ bytes
;
2303 static const struct bpf_func_proto bpf_msg_pull_data_proto
= {
2304 .func
= bpf_msg_pull_data
,
2306 .ret_type
= RET_INTEGER
,
2307 .arg1_type
= ARG_PTR_TO_CTX
,
2308 .arg2_type
= ARG_ANYTHING
,
2309 .arg3_type
= ARG_ANYTHING
,
2310 .arg4_type
= ARG_ANYTHING
,
2313 BPF_CALL_4(bpf_msg_push_data
, struct sk_msg
*, msg
, u32
, start
,
2314 u32
, len
, u64
, flags
)
2316 struct scatterlist sge
, nsge
, nnsge
, rsge
= {0}, *psge
;
2317 u32
new, i
= 0, l
, space
, copy
= 0, offset
= 0;
2318 u8
*raw
, *to
, *from
;
2321 if (unlikely(flags
))
2324 /* First find the starting scatterlist element */
2327 l
= sk_msg_elem(msg
, i
)->length
;
2329 if (start
< offset
+ l
)
2332 sk_msg_iter_var_next(i
);
2333 } while (i
!= msg
->sg
.end
);
2335 if (start
>= offset
+ l
)
2338 space
= MAX_MSG_FRAGS
- sk_msg_elem_used(msg
);
2340 /* If no space available will fallback to copy, we need at
2341 * least one scatterlist elem available to push data into
2342 * when start aligns to the beginning of an element or two
2343 * when it falls inside an element. We handle the start equals
2344 * offset case because its the common case for inserting a
2347 if (!space
|| (space
== 1 && start
!= offset
))
2348 copy
= msg
->sg
.data
[i
].length
;
2350 page
= alloc_pages(__GFP_NOWARN
| GFP_ATOMIC
| __GFP_COMP
,
2351 get_order(copy
+ len
));
2352 if (unlikely(!page
))
2358 raw
= page_address(page
);
2360 psge
= sk_msg_elem(msg
, i
);
2361 front
= start
- offset
;
2362 back
= psge
->length
- front
;
2363 from
= sg_virt(psge
);
2366 memcpy(raw
, from
, front
);
2370 to
= raw
+ front
+ len
;
2372 memcpy(to
, from
, back
);
2375 put_page(sg_page(psge
));
2376 } else if (start
- offset
) {
2377 psge
= sk_msg_elem(msg
, i
);
2378 rsge
= sk_msg_elem_cpy(msg
, i
);
2380 psge
->length
= start
- offset
;
2381 rsge
.length
-= psge
->length
;
2382 rsge
.offset
+= start
;
2384 sk_msg_iter_var_next(i
);
2385 sg_unmark_end(psge
);
2386 sk_msg_iter_next(msg
, end
);
2389 /* Slot(s) to place newly allocated data */
2392 /* Shift one or two slots as needed */
2394 sge
= sk_msg_elem_cpy(msg
, i
);
2396 sk_msg_iter_var_next(i
);
2397 sg_unmark_end(&sge
);
2398 sk_msg_iter_next(msg
, end
);
2400 nsge
= sk_msg_elem_cpy(msg
, i
);
2402 sk_msg_iter_var_next(i
);
2403 nnsge
= sk_msg_elem_cpy(msg
, i
);
2406 while (i
!= msg
->sg
.end
) {
2407 msg
->sg
.data
[i
] = sge
;
2409 sk_msg_iter_var_next(i
);
2412 nnsge
= sk_msg_elem_cpy(msg
, i
);
2414 nsge
= sk_msg_elem_cpy(msg
, i
);
2419 /* Place newly allocated data buffer */
2420 sk_mem_charge(msg
->sk
, len
);
2421 msg
->sg
.size
+= len
;
2422 msg
->sg
.copy
[new] = false;
2423 sg_set_page(&msg
->sg
.data
[new], page
, len
+ copy
, 0);
2425 get_page(sg_page(&rsge
));
2426 sk_msg_iter_var_next(new);
2427 msg
->sg
.data
[new] = rsge
;
2430 sk_msg_compute_data_pointers(msg
);
2434 static const struct bpf_func_proto bpf_msg_push_data_proto
= {
2435 .func
= bpf_msg_push_data
,
2437 .ret_type
= RET_INTEGER
,
2438 .arg1_type
= ARG_PTR_TO_CTX
,
2439 .arg2_type
= ARG_ANYTHING
,
2440 .arg3_type
= ARG_ANYTHING
,
2441 .arg4_type
= ARG_ANYTHING
,
2444 static void sk_msg_shift_left(struct sk_msg
*msg
, int i
)
2450 sk_msg_iter_var_next(i
);
2451 msg
->sg
.data
[prev
] = msg
->sg
.data
[i
];
2452 } while (i
!= msg
->sg
.end
);
2454 sk_msg_iter_prev(msg
, end
);
2457 static void sk_msg_shift_right(struct sk_msg
*msg
, int i
)
2459 struct scatterlist tmp
, sge
;
2461 sk_msg_iter_next(msg
, end
);
2462 sge
= sk_msg_elem_cpy(msg
, i
);
2463 sk_msg_iter_var_next(i
);
2464 tmp
= sk_msg_elem_cpy(msg
, i
);
2466 while (i
!= msg
->sg
.end
) {
2467 msg
->sg
.data
[i
] = sge
;
2468 sk_msg_iter_var_next(i
);
2470 tmp
= sk_msg_elem_cpy(msg
, i
);
2474 BPF_CALL_4(bpf_msg_pop_data
, struct sk_msg
*, msg
, u32
, start
,
2475 u32
, len
, u64
, flags
)
2477 u32 i
= 0, l
, space
, offset
= 0;
2478 u64 last
= start
+ len
;
2481 if (unlikely(flags
))
2484 /* First find the starting scatterlist element */
2487 l
= sk_msg_elem(msg
, i
)->length
;
2489 if (start
< offset
+ l
)
2492 sk_msg_iter_var_next(i
);
2493 } while (i
!= msg
->sg
.end
);
2495 /* Bounds checks: start and pop must be inside message */
2496 if (start
>= offset
+ l
|| last
>= msg
->sg
.size
)
2499 space
= MAX_MSG_FRAGS
- sk_msg_elem_used(msg
);
2502 /* --------------| offset
2503 * -| start |-------- len -------|
2505 * |----- a ----|-------- pop -------|----- b ----|
2506 * |______________________________________________| length
2509 * a: region at front of scatter element to save
2510 * b: region at back of scatter element to save when length > A + pop
2511 * pop: region to pop from element, same as input 'pop' here will be
2512 * decremented below per iteration.
2514 * Two top-level cases to handle when start != offset, first B is non
2515 * zero and second B is zero corresponding to when a pop includes more
2518 * Then if B is non-zero AND there is no space allocate space and
2519 * compact A, B regions into page. If there is space shift ring to
2520 * the rigth free'ing the next element in ring to place B, leaving
2521 * A untouched except to reduce length.
2523 if (start
!= offset
) {
2524 struct scatterlist
*nsge
, *sge
= sk_msg_elem(msg
, i
);
2526 int b
= sge
->length
- pop
- a
;
2528 sk_msg_iter_var_next(i
);
2530 if (pop
< sge
->length
- a
) {
2533 sk_msg_shift_right(msg
, i
);
2534 nsge
= sk_msg_elem(msg
, i
);
2535 get_page(sg_page(sge
));
2538 b
, sge
->offset
+ pop
+ a
);
2540 struct page
*page
, *orig
;
2543 page
= alloc_pages(__GFP_NOWARN
|
2544 __GFP_COMP
| GFP_ATOMIC
,
2546 if (unlikely(!page
))
2550 orig
= sg_page(sge
);
2551 from
= sg_virt(sge
);
2552 to
= page_address(page
);
2553 memcpy(to
, from
, a
);
2554 memcpy(to
+ a
, from
+ a
+ pop
, b
);
2555 sg_set_page(sge
, page
, a
+ b
, 0);
2559 } else if (pop
>= sge
->length
- a
) {
2561 pop
-= (sge
->length
- a
);
2565 /* From above the current layout _must_ be as follows,
2570 * |---- pop ---|---------------- b ------------|
2571 * |____________________________________________| length
2573 * Offset and start of the current msg elem are equal because in the
2574 * previous case we handled offset != start and either consumed the
2575 * entire element and advanced to the next element OR pop == 0.
2577 * Two cases to handle here are first pop is less than the length
2578 * leaving some remainder b above. Simply adjust the element's layout
2579 * in this case. Or pop >= length of the element so that b = 0. In this
2580 * case advance to next element decrementing pop.
2583 struct scatterlist
*sge
= sk_msg_elem(msg
, i
);
2585 if (pop
< sge
->length
) {
2591 sk_msg_shift_left(msg
, i
);
2593 sk_msg_iter_var_next(i
);
2596 sk_mem_uncharge(msg
->sk
, len
- pop
);
2597 msg
->sg
.size
-= (len
- pop
);
2598 sk_msg_compute_data_pointers(msg
);
2602 static const struct bpf_func_proto bpf_msg_pop_data_proto
= {
2603 .func
= bpf_msg_pop_data
,
2605 .ret_type
= RET_INTEGER
,
2606 .arg1_type
= ARG_PTR_TO_CTX
,
2607 .arg2_type
= ARG_ANYTHING
,
2608 .arg3_type
= ARG_ANYTHING
,
2609 .arg4_type
= ARG_ANYTHING
,
2612 BPF_CALL_1(bpf_get_cgroup_classid
, const struct sk_buff
*, skb
)
2614 return task_get_classid(skb
);
2617 static const struct bpf_func_proto bpf_get_cgroup_classid_proto
= {
2618 .func
= bpf_get_cgroup_classid
,
2620 .ret_type
= RET_INTEGER
,
2621 .arg1_type
= ARG_PTR_TO_CTX
,
2624 BPF_CALL_1(bpf_get_route_realm
, const struct sk_buff
*, skb
)
2626 return dst_tclassid(skb
);
2629 static const struct bpf_func_proto bpf_get_route_realm_proto
= {
2630 .func
= bpf_get_route_realm
,
2632 .ret_type
= RET_INTEGER
,
2633 .arg1_type
= ARG_PTR_TO_CTX
,
2636 BPF_CALL_1(bpf_get_hash_recalc
, struct sk_buff
*, skb
)
2638 /* If skb_clear_hash() was called due to mangling, we can
2639 * trigger SW recalculation here. Later access to hash
2640 * can then use the inline skb->hash via context directly
2641 * instead of calling this helper again.
2643 return skb_get_hash(skb
);
2646 static const struct bpf_func_proto bpf_get_hash_recalc_proto
= {
2647 .func
= bpf_get_hash_recalc
,
2649 .ret_type
= RET_INTEGER
,
2650 .arg1_type
= ARG_PTR_TO_CTX
,
2653 BPF_CALL_1(bpf_set_hash_invalid
, struct sk_buff
*, skb
)
2655 /* After all direct packet write, this can be used once for
2656 * triggering a lazy recalc on next skb_get_hash() invocation.
2658 skb_clear_hash(skb
);
2662 static const struct bpf_func_proto bpf_set_hash_invalid_proto
= {
2663 .func
= bpf_set_hash_invalid
,
2665 .ret_type
= RET_INTEGER
,
2666 .arg1_type
= ARG_PTR_TO_CTX
,
2669 BPF_CALL_2(bpf_set_hash
, struct sk_buff
*, skb
, u32
, hash
)
2671 /* Set user specified hash as L4(+), so that it gets returned
2672 * on skb_get_hash() call unless BPF prog later on triggers a
2675 __skb_set_sw_hash(skb
, hash
, true);
2679 static const struct bpf_func_proto bpf_set_hash_proto
= {
2680 .func
= bpf_set_hash
,
2682 .ret_type
= RET_INTEGER
,
2683 .arg1_type
= ARG_PTR_TO_CTX
,
2684 .arg2_type
= ARG_ANYTHING
,
2687 BPF_CALL_3(bpf_skb_vlan_push
, struct sk_buff
*, skb
, __be16
, vlan_proto
,
2692 if (unlikely(vlan_proto
!= htons(ETH_P_8021Q
) &&
2693 vlan_proto
!= htons(ETH_P_8021AD
)))
2694 vlan_proto
= htons(ETH_P_8021Q
);
2696 bpf_push_mac_rcsum(skb
);
2697 ret
= skb_vlan_push(skb
, vlan_proto
, vlan_tci
);
2698 bpf_pull_mac_rcsum(skb
);
2700 bpf_compute_data_pointers(skb
);
2704 static const struct bpf_func_proto bpf_skb_vlan_push_proto
= {
2705 .func
= bpf_skb_vlan_push
,
2707 .ret_type
= RET_INTEGER
,
2708 .arg1_type
= ARG_PTR_TO_CTX
,
2709 .arg2_type
= ARG_ANYTHING
,
2710 .arg3_type
= ARG_ANYTHING
,
2713 BPF_CALL_1(bpf_skb_vlan_pop
, struct sk_buff
*, skb
)
2717 bpf_push_mac_rcsum(skb
);
2718 ret
= skb_vlan_pop(skb
);
2719 bpf_pull_mac_rcsum(skb
);
2721 bpf_compute_data_pointers(skb
);
2725 static const struct bpf_func_proto bpf_skb_vlan_pop_proto
= {
2726 .func
= bpf_skb_vlan_pop
,
2728 .ret_type
= RET_INTEGER
,
2729 .arg1_type
= ARG_PTR_TO_CTX
,
2732 static int bpf_skb_generic_push(struct sk_buff
*skb
, u32 off
, u32 len
)
2734 /* Caller already did skb_cow() with len as headroom,
2735 * so no need to do it here.
2738 memmove(skb
->data
, skb
->data
+ len
, off
);
2739 memset(skb
->data
+ off
, 0, len
);
2741 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2742 * needed here as it does not change the skb->csum
2743 * result for checksum complete when summing over
2749 static int bpf_skb_generic_pop(struct sk_buff
*skb
, u32 off
, u32 len
)
2751 /* skb_ensure_writable() is not needed here, as we're
2752 * already working on an uncloned skb.
2754 if (unlikely(!pskb_may_pull(skb
, off
+ len
)))
2757 skb_postpull_rcsum(skb
, skb
->data
+ off
, len
);
2758 memmove(skb
->data
+ len
, skb
->data
, off
);
2759 __skb_pull(skb
, len
);
2764 static int bpf_skb_net_hdr_push(struct sk_buff
*skb
, u32 off
, u32 len
)
2766 bool trans_same
= skb
->transport_header
== skb
->network_header
;
2769 /* There's no need for __skb_push()/__skb_pull() pair to
2770 * get to the start of the mac header as we're guaranteed
2771 * to always start from here under eBPF.
2773 ret
= bpf_skb_generic_push(skb
, off
, len
);
2775 skb
->mac_header
-= len
;
2776 skb
->network_header
-= len
;
2778 skb
->transport_header
= skb
->network_header
;
2784 static int bpf_skb_net_hdr_pop(struct sk_buff
*skb
, u32 off
, u32 len
)
2786 bool trans_same
= skb
->transport_header
== skb
->network_header
;
2789 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2790 ret
= bpf_skb_generic_pop(skb
, off
, len
);
2792 skb
->mac_header
+= len
;
2793 skb
->network_header
+= len
;
2795 skb
->transport_header
= skb
->network_header
;
2801 static int bpf_skb_proto_4_to_6(struct sk_buff
*skb
)
2803 const u32 len_diff
= sizeof(struct ipv6hdr
) - sizeof(struct iphdr
);
2804 u32 off
= skb_mac_header_len(skb
);
2807 if (skb_is_gso(skb
) && !skb_is_gso_tcp(skb
))
2810 ret
= skb_cow(skb
, len_diff
);
2811 if (unlikely(ret
< 0))
2814 ret
= bpf_skb_net_hdr_push(skb
, off
, len_diff
);
2815 if (unlikely(ret
< 0))
2818 if (skb_is_gso(skb
)) {
2819 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
2821 /* SKB_GSO_TCPV4 needs to be changed into
2824 if (shinfo
->gso_type
& SKB_GSO_TCPV4
) {
2825 shinfo
->gso_type
&= ~SKB_GSO_TCPV4
;
2826 shinfo
->gso_type
|= SKB_GSO_TCPV6
;
2829 /* Due to IPv6 header, MSS needs to be downgraded. */
2830 skb_decrease_gso_size(shinfo
, len_diff
);
2831 /* Header must be checked, and gso_segs recomputed. */
2832 shinfo
->gso_type
|= SKB_GSO_DODGY
;
2833 shinfo
->gso_segs
= 0;
2836 skb
->protocol
= htons(ETH_P_IPV6
);
2837 skb_clear_hash(skb
);
2842 static int bpf_skb_proto_6_to_4(struct sk_buff
*skb
)
2844 const u32 len_diff
= sizeof(struct ipv6hdr
) - sizeof(struct iphdr
);
2845 u32 off
= skb_mac_header_len(skb
);
2848 if (skb_is_gso(skb
) && !skb_is_gso_tcp(skb
))
2851 ret
= skb_unclone(skb
, GFP_ATOMIC
);
2852 if (unlikely(ret
< 0))
2855 ret
= bpf_skb_net_hdr_pop(skb
, off
, len_diff
);
2856 if (unlikely(ret
< 0))
2859 if (skb_is_gso(skb
)) {
2860 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
2862 /* SKB_GSO_TCPV6 needs to be changed into
2865 if (shinfo
->gso_type
& SKB_GSO_TCPV6
) {
2866 shinfo
->gso_type
&= ~SKB_GSO_TCPV6
;
2867 shinfo
->gso_type
|= SKB_GSO_TCPV4
;
2870 /* Due to IPv4 header, MSS can be upgraded. */
2871 skb_increase_gso_size(shinfo
, len_diff
);
2872 /* Header must be checked, and gso_segs recomputed. */
2873 shinfo
->gso_type
|= SKB_GSO_DODGY
;
2874 shinfo
->gso_segs
= 0;
2877 skb
->protocol
= htons(ETH_P_IP
);
2878 skb_clear_hash(skb
);
2883 static int bpf_skb_proto_xlat(struct sk_buff
*skb
, __be16 to_proto
)
2885 __be16 from_proto
= skb
->protocol
;
2887 if (from_proto
== htons(ETH_P_IP
) &&
2888 to_proto
== htons(ETH_P_IPV6
))
2889 return bpf_skb_proto_4_to_6(skb
);
2891 if (from_proto
== htons(ETH_P_IPV6
) &&
2892 to_proto
== htons(ETH_P_IP
))
2893 return bpf_skb_proto_6_to_4(skb
);
2898 BPF_CALL_3(bpf_skb_change_proto
, struct sk_buff
*, skb
, __be16
, proto
,
2903 if (unlikely(flags
))
2906 /* General idea is that this helper does the basic groundwork
2907 * needed for changing the protocol, and eBPF program fills the
2908 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2909 * and other helpers, rather than passing a raw buffer here.
2911 * The rationale is to keep this minimal and without a need to
2912 * deal with raw packet data. F.e. even if we would pass buffers
2913 * here, the program still needs to call the bpf_lX_csum_replace()
2914 * helpers anyway. Plus, this way we keep also separation of
2915 * concerns, since f.e. bpf_skb_store_bytes() should only take
2918 * Currently, additional options and extension header space are
2919 * not supported, but flags register is reserved so we can adapt
2920 * that. For offloads, we mark packet as dodgy, so that headers
2921 * need to be verified first.
2923 ret
= bpf_skb_proto_xlat(skb
, proto
);
2924 bpf_compute_data_pointers(skb
);
2928 static const struct bpf_func_proto bpf_skb_change_proto_proto
= {
2929 .func
= bpf_skb_change_proto
,
2931 .ret_type
= RET_INTEGER
,
2932 .arg1_type
= ARG_PTR_TO_CTX
,
2933 .arg2_type
= ARG_ANYTHING
,
2934 .arg3_type
= ARG_ANYTHING
,
2937 BPF_CALL_2(bpf_skb_change_type
, struct sk_buff
*, skb
, u32
, pkt_type
)
2939 /* We only allow a restricted subset to be changed for now. */
2940 if (unlikely(!skb_pkt_type_ok(skb
->pkt_type
) ||
2941 !skb_pkt_type_ok(pkt_type
)))
2944 skb
->pkt_type
= pkt_type
;
2948 static const struct bpf_func_proto bpf_skb_change_type_proto
= {
2949 .func
= bpf_skb_change_type
,
2951 .ret_type
= RET_INTEGER
,
2952 .arg1_type
= ARG_PTR_TO_CTX
,
2953 .arg2_type
= ARG_ANYTHING
,
2956 static u32
bpf_skb_net_base_len(const struct sk_buff
*skb
)
2958 switch (skb
->protocol
) {
2959 case htons(ETH_P_IP
):
2960 return sizeof(struct iphdr
);
2961 case htons(ETH_P_IPV6
):
2962 return sizeof(struct ipv6hdr
);
2968 static int bpf_skb_net_grow(struct sk_buff
*skb
, u32 len_diff
)
2970 u32 off
= skb_mac_header_len(skb
) + bpf_skb_net_base_len(skb
);
2973 if (skb_is_gso(skb
) && !skb_is_gso_tcp(skb
))
2976 ret
= skb_cow(skb
, len_diff
);
2977 if (unlikely(ret
< 0))
2980 ret
= bpf_skb_net_hdr_push(skb
, off
, len_diff
);
2981 if (unlikely(ret
< 0))
2984 if (skb_is_gso(skb
)) {
2985 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
2987 /* Due to header grow, MSS needs to be downgraded. */
2988 skb_decrease_gso_size(shinfo
, len_diff
);
2989 /* Header must be checked, and gso_segs recomputed. */
2990 shinfo
->gso_type
|= SKB_GSO_DODGY
;
2991 shinfo
->gso_segs
= 0;
2997 static int bpf_skb_net_shrink(struct sk_buff
*skb
, u32 len_diff
)
2999 u32 off
= skb_mac_header_len(skb
) + bpf_skb_net_base_len(skb
);
3002 if (skb_is_gso(skb
) && !skb_is_gso_tcp(skb
))
3005 ret
= skb_unclone(skb
, GFP_ATOMIC
);
3006 if (unlikely(ret
< 0))
3009 ret
= bpf_skb_net_hdr_pop(skb
, off
, len_diff
);
3010 if (unlikely(ret
< 0))
3013 if (skb_is_gso(skb
)) {
3014 struct skb_shared_info
*shinfo
= skb_shinfo(skb
);
3016 /* Due to header shrink, MSS can be upgraded. */
3017 skb_increase_gso_size(shinfo
, len_diff
);
3018 /* Header must be checked, and gso_segs recomputed. */
3019 shinfo
->gso_type
|= SKB_GSO_DODGY
;
3020 shinfo
->gso_segs
= 0;
3026 static u32
__bpf_skb_max_len(const struct sk_buff
*skb
)
3028 return skb
->dev
? skb
->dev
->mtu
+ skb
->dev
->hard_header_len
:
3032 static int bpf_skb_adjust_net(struct sk_buff
*skb
, s32 len_diff
)
3034 bool trans_same
= skb
->transport_header
== skb
->network_header
;
3035 u32 len_cur
, len_diff_abs
= abs(len_diff
);
3036 u32 len_min
= bpf_skb_net_base_len(skb
);
3037 u32 len_max
= __bpf_skb_max_len(skb
);
3038 __be16 proto
= skb
->protocol
;
3039 bool shrink
= len_diff
< 0;
3042 if (unlikely(len_diff_abs
> 0xfffU
))
3044 if (unlikely(proto
!= htons(ETH_P_IP
) &&
3045 proto
!= htons(ETH_P_IPV6
)))
3048 len_cur
= skb
->len
- skb_network_offset(skb
);
3049 if (skb_transport_header_was_set(skb
) && !trans_same
)
3050 len_cur
= skb_network_header_len(skb
);
3051 if ((shrink
&& (len_diff_abs
>= len_cur
||
3052 len_cur
- len_diff_abs
< len_min
)) ||
3053 (!shrink
&& (skb
->len
+ len_diff_abs
> len_max
&&
3057 ret
= shrink
? bpf_skb_net_shrink(skb
, len_diff_abs
) :
3058 bpf_skb_net_grow(skb
, len_diff_abs
);
3060 bpf_compute_data_pointers(skb
);
3064 BPF_CALL_4(bpf_skb_adjust_room
, struct sk_buff
*, skb
, s32
, len_diff
,
3065 u32
, mode
, u64
, flags
)
3067 if (unlikely(flags
))
3069 if (likely(mode
== BPF_ADJ_ROOM_NET
))
3070 return bpf_skb_adjust_net(skb
, len_diff
);
3075 static const struct bpf_func_proto bpf_skb_adjust_room_proto
= {
3076 .func
= bpf_skb_adjust_room
,
3078 .ret_type
= RET_INTEGER
,
3079 .arg1_type
= ARG_PTR_TO_CTX
,
3080 .arg2_type
= ARG_ANYTHING
,
3081 .arg3_type
= ARG_ANYTHING
,
3082 .arg4_type
= ARG_ANYTHING
,
3085 static u32
__bpf_skb_min_len(const struct sk_buff
*skb
)
3087 u32 min_len
= skb_network_offset(skb
);
3089 if (skb_transport_header_was_set(skb
))
3090 min_len
= skb_transport_offset(skb
);
3091 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
3092 min_len
= skb_checksum_start_offset(skb
) +
3093 skb
->csum_offset
+ sizeof(__sum16
);
3097 static int bpf_skb_grow_rcsum(struct sk_buff
*skb
, unsigned int new_len
)
3099 unsigned int old_len
= skb
->len
;
3102 ret
= __skb_grow_rcsum(skb
, new_len
);
3104 memset(skb
->data
+ old_len
, 0, new_len
- old_len
);
3108 static int bpf_skb_trim_rcsum(struct sk_buff
*skb
, unsigned int new_len
)
3110 return __skb_trim_rcsum(skb
, new_len
);
3113 static inline int __bpf_skb_change_tail(struct sk_buff
*skb
, u32 new_len
,
3116 u32 max_len
= __bpf_skb_max_len(skb
);
3117 u32 min_len
= __bpf_skb_min_len(skb
);
3120 if (unlikely(flags
|| new_len
> max_len
|| new_len
< min_len
))
3122 if (skb
->encapsulation
)
3125 /* The basic idea of this helper is that it's performing the
3126 * needed work to either grow or trim an skb, and eBPF program
3127 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3128 * bpf_lX_csum_replace() and others rather than passing a raw
3129 * buffer here. This one is a slow path helper and intended
3130 * for replies with control messages.
3132 * Like in bpf_skb_change_proto(), we want to keep this rather
3133 * minimal and without protocol specifics so that we are able
3134 * to separate concerns as in bpf_skb_store_bytes() should only
3135 * be the one responsible for writing buffers.
3137 * It's really expected to be a slow path operation here for
3138 * control message replies, so we're implicitly linearizing,
3139 * uncloning and drop offloads from the skb by this.
3141 ret
= __bpf_try_make_writable(skb
, skb
->len
);
3143 if (new_len
> skb
->len
)
3144 ret
= bpf_skb_grow_rcsum(skb
, new_len
);
3145 else if (new_len
< skb
->len
)
3146 ret
= bpf_skb_trim_rcsum(skb
, new_len
);
3147 if (!ret
&& skb_is_gso(skb
))
3153 BPF_CALL_3(bpf_skb_change_tail
, struct sk_buff
*, skb
, u32
, new_len
,
3156 int ret
= __bpf_skb_change_tail(skb
, new_len
, flags
);
3158 bpf_compute_data_pointers(skb
);
3162 static const struct bpf_func_proto bpf_skb_change_tail_proto
= {
3163 .func
= bpf_skb_change_tail
,
3165 .ret_type
= RET_INTEGER
,
3166 .arg1_type
= ARG_PTR_TO_CTX
,
3167 .arg2_type
= ARG_ANYTHING
,
3168 .arg3_type
= ARG_ANYTHING
,
3171 BPF_CALL_3(sk_skb_change_tail
, struct sk_buff
*, skb
, u32
, new_len
,
3174 int ret
= __bpf_skb_change_tail(skb
, new_len
, flags
);
3176 bpf_compute_data_end_sk_skb(skb
);
3180 static const struct bpf_func_proto sk_skb_change_tail_proto
= {
3181 .func
= sk_skb_change_tail
,
3183 .ret_type
= RET_INTEGER
,
3184 .arg1_type
= ARG_PTR_TO_CTX
,
3185 .arg2_type
= ARG_ANYTHING
,
3186 .arg3_type
= ARG_ANYTHING
,
3189 static inline int __bpf_skb_change_head(struct sk_buff
*skb
, u32 head_room
,
3192 u32 max_len
= __bpf_skb_max_len(skb
);
3193 u32 new_len
= skb
->len
+ head_room
;
3196 if (unlikely(flags
|| (!skb_is_gso(skb
) && new_len
> max_len
) ||
3197 new_len
< skb
->len
))
3200 ret
= skb_cow(skb
, head_room
);
3202 /* Idea for this helper is that we currently only
3203 * allow to expand on mac header. This means that
3204 * skb->protocol network header, etc, stay as is.
3205 * Compared to bpf_skb_change_tail(), we're more
3206 * flexible due to not needing to linearize or
3207 * reset GSO. Intention for this helper is to be
3208 * used by an L3 skb that needs to push mac header
3209 * for redirection into L2 device.
3211 __skb_push(skb
, head_room
);
3212 memset(skb
->data
, 0, head_room
);
3213 skb_reset_mac_header(skb
);
3219 BPF_CALL_3(bpf_skb_change_head
, struct sk_buff
*, skb
, u32
, head_room
,
3222 int ret
= __bpf_skb_change_head(skb
, head_room
, flags
);
3224 bpf_compute_data_pointers(skb
);
3228 static const struct bpf_func_proto bpf_skb_change_head_proto
= {
3229 .func
= bpf_skb_change_head
,
3231 .ret_type
= RET_INTEGER
,
3232 .arg1_type
= ARG_PTR_TO_CTX
,
3233 .arg2_type
= ARG_ANYTHING
,
3234 .arg3_type
= ARG_ANYTHING
,
3237 BPF_CALL_3(sk_skb_change_head
, struct sk_buff
*, skb
, u32
, head_room
,
3240 int ret
= __bpf_skb_change_head(skb
, head_room
, flags
);
3242 bpf_compute_data_end_sk_skb(skb
);
3246 static const struct bpf_func_proto sk_skb_change_head_proto
= {
3247 .func
= sk_skb_change_head
,
3249 .ret_type
= RET_INTEGER
,
3250 .arg1_type
= ARG_PTR_TO_CTX
,
3251 .arg2_type
= ARG_ANYTHING
,
3252 .arg3_type
= ARG_ANYTHING
,
3254 static unsigned long xdp_get_metalen(const struct xdp_buff
*xdp
)
3256 return xdp_data_meta_unsupported(xdp
) ? 0 :
3257 xdp
->data
- xdp
->data_meta
;
3260 BPF_CALL_2(bpf_xdp_adjust_head
, struct xdp_buff
*, xdp
, int, offset
)
3262 void *xdp_frame_end
= xdp
->data_hard_start
+ sizeof(struct xdp_frame
);
3263 unsigned long metalen
= xdp_get_metalen(xdp
);
3264 void *data_start
= xdp_frame_end
+ metalen
;
3265 void *data
= xdp
->data
+ offset
;
3267 if (unlikely(data
< data_start
||
3268 data
> xdp
->data_end
- ETH_HLEN
))
3272 memmove(xdp
->data_meta
+ offset
,
3273 xdp
->data_meta
, metalen
);
3274 xdp
->data_meta
+= offset
;
3280 static const struct bpf_func_proto bpf_xdp_adjust_head_proto
= {
3281 .func
= bpf_xdp_adjust_head
,
3283 .ret_type
= RET_INTEGER
,
3284 .arg1_type
= ARG_PTR_TO_CTX
,
3285 .arg2_type
= ARG_ANYTHING
,
3288 BPF_CALL_2(bpf_xdp_adjust_tail
, struct xdp_buff
*, xdp
, int, offset
)
3290 void *data_end
= xdp
->data_end
+ offset
;
3292 /* only shrinking is allowed for now. */
3293 if (unlikely(offset
>= 0))
3296 if (unlikely(data_end
< xdp
->data
+ ETH_HLEN
))
3299 xdp
->data_end
= data_end
;
3304 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto
= {
3305 .func
= bpf_xdp_adjust_tail
,
3307 .ret_type
= RET_INTEGER
,
3308 .arg1_type
= ARG_PTR_TO_CTX
,
3309 .arg2_type
= ARG_ANYTHING
,
3312 BPF_CALL_2(bpf_xdp_adjust_meta
, struct xdp_buff
*, xdp
, int, offset
)
3314 void *xdp_frame_end
= xdp
->data_hard_start
+ sizeof(struct xdp_frame
);
3315 void *meta
= xdp
->data_meta
+ offset
;
3316 unsigned long metalen
= xdp
->data
- meta
;
3318 if (xdp_data_meta_unsupported(xdp
))
3320 if (unlikely(meta
< xdp_frame_end
||
3323 if (unlikely((metalen
& (sizeof(__u32
) - 1)) ||
3327 xdp
->data_meta
= meta
;
3332 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto
= {
3333 .func
= bpf_xdp_adjust_meta
,
3335 .ret_type
= RET_INTEGER
,
3336 .arg1_type
= ARG_PTR_TO_CTX
,
3337 .arg2_type
= ARG_ANYTHING
,
3340 static int __bpf_tx_xdp(struct net_device
*dev
,
3341 struct bpf_map
*map
,
3342 struct xdp_buff
*xdp
,
3345 struct xdp_frame
*xdpf
;
3348 if (!dev
->netdev_ops
->ndo_xdp_xmit
) {
3352 err
= xdp_ok_fwd_dev(dev
, xdp
->data_end
- xdp
->data
);
3356 xdpf
= convert_to_xdp_frame(xdp
);
3357 if (unlikely(!xdpf
))
3360 sent
= dev
->netdev_ops
->ndo_xdp_xmit(dev
, 1, &xdpf
, XDP_XMIT_FLUSH
);
3367 xdp_do_redirect_slow(struct net_device
*dev
, struct xdp_buff
*xdp
,
3368 struct bpf_prog
*xdp_prog
, struct bpf_redirect_info
*ri
)
3370 struct net_device
*fwd
;
3371 u32 index
= ri
->ifindex
;
3374 fwd
= dev_get_by_index_rcu(dev_net(dev
), index
);
3376 if (unlikely(!fwd
)) {
3381 err
= __bpf_tx_xdp(fwd
, NULL
, xdp
, 0);
3385 _trace_xdp_redirect(dev
, xdp_prog
, index
);
3388 _trace_xdp_redirect_err(dev
, xdp_prog
, index
, err
);
3392 static int __bpf_tx_xdp_map(struct net_device
*dev_rx
, void *fwd
,
3393 struct bpf_map
*map
,
3394 struct xdp_buff
*xdp
,
3399 switch (map
->map_type
) {
3400 case BPF_MAP_TYPE_DEVMAP
: {
3401 struct bpf_dtab_netdev
*dst
= fwd
;
3403 err
= dev_map_enqueue(dst
, xdp
, dev_rx
);
3406 __dev_map_insert_ctx(map
, index
);
3409 case BPF_MAP_TYPE_CPUMAP
: {
3410 struct bpf_cpu_map_entry
*rcpu
= fwd
;
3412 err
= cpu_map_enqueue(rcpu
, xdp
, dev_rx
);
3415 __cpu_map_insert_ctx(map
, index
);
3418 case BPF_MAP_TYPE_XSKMAP
: {
3419 struct xdp_sock
*xs
= fwd
;
3421 err
= __xsk_map_redirect(map
, xdp
, xs
);
3430 void xdp_do_flush_map(void)
3432 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3433 struct bpf_map
*map
= ri
->map_to_flush
;
3435 ri
->map_to_flush
= NULL
;
3437 switch (map
->map_type
) {
3438 case BPF_MAP_TYPE_DEVMAP
:
3439 __dev_map_flush(map
);
3441 case BPF_MAP_TYPE_CPUMAP
:
3442 __cpu_map_flush(map
);
3444 case BPF_MAP_TYPE_XSKMAP
:
3445 __xsk_map_flush(map
);
3452 EXPORT_SYMBOL_GPL(xdp_do_flush_map
);
3454 static inline void *__xdp_map_lookup_elem(struct bpf_map
*map
, u32 index
)
3456 switch (map
->map_type
) {
3457 case BPF_MAP_TYPE_DEVMAP
:
3458 return __dev_map_lookup_elem(map
, index
);
3459 case BPF_MAP_TYPE_CPUMAP
:
3460 return __cpu_map_lookup_elem(map
, index
);
3461 case BPF_MAP_TYPE_XSKMAP
:
3462 return __xsk_map_lookup_elem(map
, index
);
3468 void bpf_clear_redirect_map(struct bpf_map
*map
)
3470 struct bpf_redirect_info
*ri
;
3473 for_each_possible_cpu(cpu
) {
3474 ri
= per_cpu_ptr(&bpf_redirect_info
, cpu
);
3475 /* Avoid polluting remote cacheline due to writes if
3476 * not needed. Once we pass this test, we need the
3477 * cmpxchg() to make sure it hasn't been changed in
3478 * the meantime by remote CPU.
3480 if (unlikely(READ_ONCE(ri
->map
) == map
))
3481 cmpxchg(&ri
->map
, map
, NULL
);
3485 static int xdp_do_redirect_map(struct net_device
*dev
, struct xdp_buff
*xdp
,
3486 struct bpf_prog
*xdp_prog
, struct bpf_map
*map
,
3487 struct bpf_redirect_info
*ri
)
3489 u32 index
= ri
->ifindex
;
3494 WRITE_ONCE(ri
->map
, NULL
);
3496 fwd
= __xdp_map_lookup_elem(map
, index
);
3497 if (unlikely(!fwd
)) {
3501 if (ri
->map_to_flush
&& unlikely(ri
->map_to_flush
!= map
))
3504 err
= __bpf_tx_xdp_map(dev
, fwd
, map
, xdp
, index
);
3508 ri
->map_to_flush
= map
;
3509 _trace_xdp_redirect_map(dev
, xdp_prog
, fwd
, map
, index
);
3512 _trace_xdp_redirect_map_err(dev
, xdp_prog
, fwd
, map
, index
, err
);
3516 int xdp_do_redirect(struct net_device
*dev
, struct xdp_buff
*xdp
,
3517 struct bpf_prog
*xdp_prog
)
3519 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3520 struct bpf_map
*map
= READ_ONCE(ri
->map
);
3523 return xdp_do_redirect_map(dev
, xdp
, xdp_prog
, map
, ri
);
3525 return xdp_do_redirect_slow(dev
, xdp
, xdp_prog
, ri
);
3527 EXPORT_SYMBOL_GPL(xdp_do_redirect
);
3529 static int xdp_do_generic_redirect_map(struct net_device
*dev
,
3530 struct sk_buff
*skb
,
3531 struct xdp_buff
*xdp
,
3532 struct bpf_prog
*xdp_prog
,
3533 struct bpf_map
*map
)
3535 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3536 u32 index
= ri
->ifindex
;
3541 WRITE_ONCE(ri
->map
, NULL
);
3543 fwd
= __xdp_map_lookup_elem(map
, index
);
3544 if (unlikely(!fwd
)) {
3549 if (map
->map_type
== BPF_MAP_TYPE_DEVMAP
) {
3550 struct bpf_dtab_netdev
*dst
= fwd
;
3552 err
= dev_map_generic_redirect(dst
, skb
, xdp_prog
);
3555 } else if (map
->map_type
== BPF_MAP_TYPE_XSKMAP
) {
3556 struct xdp_sock
*xs
= fwd
;
3558 err
= xsk_generic_rcv(xs
, xdp
);
3563 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3568 _trace_xdp_redirect_map(dev
, xdp_prog
, fwd
, map
, index
);
3571 _trace_xdp_redirect_map_err(dev
, xdp_prog
, fwd
, map
, index
, err
);
3575 int xdp_do_generic_redirect(struct net_device
*dev
, struct sk_buff
*skb
,
3576 struct xdp_buff
*xdp
, struct bpf_prog
*xdp_prog
)
3578 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3579 struct bpf_map
*map
= READ_ONCE(ri
->map
);
3580 u32 index
= ri
->ifindex
;
3581 struct net_device
*fwd
;
3585 return xdp_do_generic_redirect_map(dev
, skb
, xdp
, xdp_prog
,
3588 fwd
= dev_get_by_index_rcu(dev_net(dev
), index
);
3589 if (unlikely(!fwd
)) {
3594 err
= xdp_ok_fwd_dev(fwd
, skb
->len
);
3599 _trace_xdp_redirect(dev
, xdp_prog
, index
);
3600 generic_xdp_tx(skb
, xdp_prog
);
3603 _trace_xdp_redirect_err(dev
, xdp_prog
, index
, err
);
3606 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect
);
3608 BPF_CALL_2(bpf_xdp_redirect
, u32
, ifindex
, u64
, flags
)
3610 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3612 if (unlikely(flags
))
3615 ri
->ifindex
= ifindex
;
3617 WRITE_ONCE(ri
->map
, NULL
);
3619 return XDP_REDIRECT
;
3622 static const struct bpf_func_proto bpf_xdp_redirect_proto
= {
3623 .func
= bpf_xdp_redirect
,
3625 .ret_type
= RET_INTEGER
,
3626 .arg1_type
= ARG_ANYTHING
,
3627 .arg2_type
= ARG_ANYTHING
,
3630 BPF_CALL_3(bpf_xdp_redirect_map
, struct bpf_map
*, map
, u32
, ifindex
,
3633 struct bpf_redirect_info
*ri
= this_cpu_ptr(&bpf_redirect_info
);
3635 if (unlikely(flags
))
3638 ri
->ifindex
= ifindex
;
3640 WRITE_ONCE(ri
->map
, map
);
3642 return XDP_REDIRECT
;
3645 static const struct bpf_func_proto bpf_xdp_redirect_map_proto
= {
3646 .func
= bpf_xdp_redirect_map
,
3648 .ret_type
= RET_INTEGER
,
3649 .arg1_type
= ARG_CONST_MAP_PTR
,
3650 .arg2_type
= ARG_ANYTHING
,
3651 .arg3_type
= ARG_ANYTHING
,
3654 static unsigned long bpf_skb_copy(void *dst_buff
, const void *skb
,
3655 unsigned long off
, unsigned long len
)
3657 void *ptr
= skb_header_pointer(skb
, off
, len
, dst_buff
);
3661 if (ptr
!= dst_buff
)
3662 memcpy(dst_buff
, ptr
, len
);
3667 BPF_CALL_5(bpf_skb_event_output
, struct sk_buff
*, skb
, struct bpf_map
*, map
,
3668 u64
, flags
, void *, meta
, u64
, meta_size
)
3670 u64 skb_size
= (flags
& BPF_F_CTXLEN_MASK
) >> 32;
3672 if (unlikely(flags
& ~(BPF_F_CTXLEN_MASK
| BPF_F_INDEX_MASK
)))
3674 if (unlikely(skb_size
> skb
->len
))
3677 return bpf_event_output(map
, flags
, meta
, meta_size
, skb
, skb_size
,
3681 static const struct bpf_func_proto bpf_skb_event_output_proto
= {
3682 .func
= bpf_skb_event_output
,
3684 .ret_type
= RET_INTEGER
,
3685 .arg1_type
= ARG_PTR_TO_CTX
,
3686 .arg2_type
= ARG_CONST_MAP_PTR
,
3687 .arg3_type
= ARG_ANYTHING
,
3688 .arg4_type
= ARG_PTR_TO_MEM
,
3689 .arg5_type
= ARG_CONST_SIZE_OR_ZERO
,
3692 static unsigned short bpf_tunnel_key_af(u64 flags
)
3694 return flags
& BPF_F_TUNINFO_IPV6
? AF_INET6
: AF_INET
;
3697 BPF_CALL_4(bpf_skb_get_tunnel_key
, struct sk_buff
*, skb
, struct bpf_tunnel_key
*, to
,
3698 u32
, size
, u64
, flags
)
3700 const struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
3701 u8 compat
[sizeof(struct bpf_tunnel_key
)];
3705 if (unlikely(!info
|| (flags
& ~(BPF_F_TUNINFO_IPV6
)))) {
3709 if (ip_tunnel_info_af(info
) != bpf_tunnel_key_af(flags
)) {
3713 if (unlikely(size
!= sizeof(struct bpf_tunnel_key
))) {
3716 case offsetof(struct bpf_tunnel_key
, tunnel_label
):
3717 case offsetof(struct bpf_tunnel_key
, tunnel_ext
):
3719 case offsetof(struct bpf_tunnel_key
, remote_ipv6
[1]):
3720 /* Fixup deprecated structure layouts here, so we have
3721 * a common path later on.
3723 if (ip_tunnel_info_af(info
) != AF_INET
)
3726 to
= (struct bpf_tunnel_key
*)compat
;
3733 to
->tunnel_id
= be64_to_cpu(info
->key
.tun_id
);
3734 to
->tunnel_tos
= info
->key
.tos
;
3735 to
->tunnel_ttl
= info
->key
.ttl
;
3738 if (flags
& BPF_F_TUNINFO_IPV6
) {
3739 memcpy(to
->remote_ipv6
, &info
->key
.u
.ipv6
.src
,
3740 sizeof(to
->remote_ipv6
));
3741 to
->tunnel_label
= be32_to_cpu(info
->key
.label
);
3743 to
->remote_ipv4
= be32_to_cpu(info
->key
.u
.ipv4
.src
);
3744 memset(&to
->remote_ipv6
[1], 0, sizeof(__u32
) * 3);
3745 to
->tunnel_label
= 0;
3748 if (unlikely(size
!= sizeof(struct bpf_tunnel_key
)))
3749 memcpy(to_orig
, to
, size
);
3753 memset(to_orig
, 0, size
);
3757 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto
= {
3758 .func
= bpf_skb_get_tunnel_key
,
3760 .ret_type
= RET_INTEGER
,
3761 .arg1_type
= ARG_PTR_TO_CTX
,
3762 .arg2_type
= ARG_PTR_TO_UNINIT_MEM
,
3763 .arg3_type
= ARG_CONST_SIZE
,
3764 .arg4_type
= ARG_ANYTHING
,
3767 BPF_CALL_3(bpf_skb_get_tunnel_opt
, struct sk_buff
*, skb
, u8
*, to
, u32
, size
)
3769 const struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
3772 if (unlikely(!info
||
3773 !(info
->key
.tun_flags
& TUNNEL_OPTIONS_PRESENT
))) {
3777 if (unlikely(size
< info
->options_len
)) {
3782 ip_tunnel_info_opts_get(to
, info
);
3783 if (size
> info
->options_len
)
3784 memset(to
+ info
->options_len
, 0, size
- info
->options_len
);
3786 return info
->options_len
;
3788 memset(to
, 0, size
);
3792 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto
= {
3793 .func
= bpf_skb_get_tunnel_opt
,
3795 .ret_type
= RET_INTEGER
,
3796 .arg1_type
= ARG_PTR_TO_CTX
,
3797 .arg2_type
= ARG_PTR_TO_UNINIT_MEM
,
3798 .arg3_type
= ARG_CONST_SIZE
,
3801 static struct metadata_dst __percpu
*md_dst
;
3803 BPF_CALL_4(bpf_skb_set_tunnel_key
, struct sk_buff
*, skb
,
3804 const struct bpf_tunnel_key
*, from
, u32
, size
, u64
, flags
)
3806 struct metadata_dst
*md
= this_cpu_ptr(md_dst
);
3807 u8 compat
[sizeof(struct bpf_tunnel_key
)];
3808 struct ip_tunnel_info
*info
;
3810 if (unlikely(flags
& ~(BPF_F_TUNINFO_IPV6
| BPF_F_ZERO_CSUM_TX
|
3811 BPF_F_DONT_FRAGMENT
| BPF_F_SEQ_NUMBER
)))
3813 if (unlikely(size
!= sizeof(struct bpf_tunnel_key
))) {
3815 case offsetof(struct bpf_tunnel_key
, tunnel_label
):
3816 case offsetof(struct bpf_tunnel_key
, tunnel_ext
):
3817 case offsetof(struct bpf_tunnel_key
, remote_ipv6
[1]):
3818 /* Fixup deprecated structure layouts here, so we have
3819 * a common path later on.
3821 memcpy(compat
, from
, size
);
3822 memset(compat
+ size
, 0, sizeof(compat
) - size
);
3823 from
= (const struct bpf_tunnel_key
*) compat
;
3829 if (unlikely((!(flags
& BPF_F_TUNINFO_IPV6
) && from
->tunnel_label
) ||
3834 dst_hold((struct dst_entry
*) md
);
3835 skb_dst_set(skb
, (struct dst_entry
*) md
);
3837 info
= &md
->u
.tun_info
;
3838 memset(info
, 0, sizeof(*info
));
3839 info
->mode
= IP_TUNNEL_INFO_TX
;
3841 info
->key
.tun_flags
= TUNNEL_KEY
| TUNNEL_CSUM
| TUNNEL_NOCACHE
;
3842 if (flags
& BPF_F_DONT_FRAGMENT
)
3843 info
->key
.tun_flags
|= TUNNEL_DONT_FRAGMENT
;
3844 if (flags
& BPF_F_ZERO_CSUM_TX
)
3845 info
->key
.tun_flags
&= ~TUNNEL_CSUM
;
3846 if (flags
& BPF_F_SEQ_NUMBER
)
3847 info
->key
.tun_flags
|= TUNNEL_SEQ
;
3849 info
->key
.tun_id
= cpu_to_be64(from
->tunnel_id
);
3850 info
->key
.tos
= from
->tunnel_tos
;
3851 info
->key
.ttl
= from
->tunnel_ttl
;
3853 if (flags
& BPF_F_TUNINFO_IPV6
) {
3854 info
->mode
|= IP_TUNNEL_INFO_IPV6
;
3855 memcpy(&info
->key
.u
.ipv6
.dst
, from
->remote_ipv6
,
3856 sizeof(from
->remote_ipv6
));
3857 info
->key
.label
= cpu_to_be32(from
->tunnel_label
) &
3858 IPV6_FLOWLABEL_MASK
;
3860 info
->key
.u
.ipv4
.dst
= cpu_to_be32(from
->remote_ipv4
);
3866 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto
= {
3867 .func
= bpf_skb_set_tunnel_key
,
3869 .ret_type
= RET_INTEGER
,
3870 .arg1_type
= ARG_PTR_TO_CTX
,
3871 .arg2_type
= ARG_PTR_TO_MEM
,
3872 .arg3_type
= ARG_CONST_SIZE
,
3873 .arg4_type
= ARG_ANYTHING
,
3876 BPF_CALL_3(bpf_skb_set_tunnel_opt
, struct sk_buff
*, skb
,
3877 const u8
*, from
, u32
, size
)
3879 struct ip_tunnel_info
*info
= skb_tunnel_info(skb
);
3880 const struct metadata_dst
*md
= this_cpu_ptr(md_dst
);
3882 if (unlikely(info
!= &md
->u
.tun_info
|| (size
& (sizeof(u32
) - 1))))
3884 if (unlikely(size
> IP_TUNNEL_OPTS_MAX
))
3887 ip_tunnel_info_opts_set(info
, from
, size
, TUNNEL_OPTIONS_PRESENT
);
3892 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto
= {
3893 .func
= bpf_skb_set_tunnel_opt
,
3895 .ret_type
= RET_INTEGER
,
3896 .arg1_type
= ARG_PTR_TO_CTX
,
3897 .arg2_type
= ARG_PTR_TO_MEM
,
3898 .arg3_type
= ARG_CONST_SIZE
,
3901 static const struct bpf_func_proto
*
3902 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which
)
3905 struct metadata_dst __percpu
*tmp
;
3907 tmp
= metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX
,
3912 if (cmpxchg(&md_dst
, NULL
, tmp
))
3913 metadata_dst_free_percpu(tmp
);
3917 case BPF_FUNC_skb_set_tunnel_key
:
3918 return &bpf_skb_set_tunnel_key_proto
;
3919 case BPF_FUNC_skb_set_tunnel_opt
:
3920 return &bpf_skb_set_tunnel_opt_proto
;
3926 BPF_CALL_3(bpf_skb_under_cgroup
, struct sk_buff
*, skb
, struct bpf_map
*, map
,
3929 struct bpf_array
*array
= container_of(map
, struct bpf_array
, map
);
3930 struct cgroup
*cgrp
;
3933 sk
= skb_to_full_sk(skb
);
3934 if (!sk
|| !sk_fullsock(sk
))
3936 if (unlikely(idx
>= array
->map
.max_entries
))
3939 cgrp
= READ_ONCE(array
->ptrs
[idx
]);
3940 if (unlikely(!cgrp
))
3943 return sk_under_cgroup_hierarchy(sk
, cgrp
);
3946 static const struct bpf_func_proto bpf_skb_under_cgroup_proto
= {
3947 .func
= bpf_skb_under_cgroup
,
3949 .ret_type
= RET_INTEGER
,
3950 .arg1_type
= ARG_PTR_TO_CTX
,
3951 .arg2_type
= ARG_CONST_MAP_PTR
,
3952 .arg3_type
= ARG_ANYTHING
,
3955 #ifdef CONFIG_SOCK_CGROUP_DATA
3956 BPF_CALL_1(bpf_skb_cgroup_id
, const struct sk_buff
*, skb
)
3958 struct sock
*sk
= skb_to_full_sk(skb
);
3959 struct cgroup
*cgrp
;
3961 if (!sk
|| !sk_fullsock(sk
))
3964 cgrp
= sock_cgroup_ptr(&sk
->sk_cgrp_data
);
3965 return cgrp
->kn
->id
.id
;
3968 static const struct bpf_func_proto bpf_skb_cgroup_id_proto
= {
3969 .func
= bpf_skb_cgroup_id
,
3971 .ret_type
= RET_INTEGER
,
3972 .arg1_type
= ARG_PTR_TO_CTX
,
3975 BPF_CALL_2(bpf_skb_ancestor_cgroup_id
, const struct sk_buff
*, skb
, int,
3978 struct sock
*sk
= skb_to_full_sk(skb
);
3979 struct cgroup
*ancestor
;
3980 struct cgroup
*cgrp
;
3982 if (!sk
|| !sk_fullsock(sk
))
3985 cgrp
= sock_cgroup_ptr(&sk
->sk_cgrp_data
);
3986 ancestor
= cgroup_ancestor(cgrp
, ancestor_level
);
3990 return ancestor
->kn
->id
.id
;
3993 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto
= {
3994 .func
= bpf_skb_ancestor_cgroup_id
,
3996 .ret_type
= RET_INTEGER
,
3997 .arg1_type
= ARG_PTR_TO_CTX
,
3998 .arg2_type
= ARG_ANYTHING
,
4002 static unsigned long bpf_xdp_copy(void *dst_buff
, const void *src_buff
,
4003 unsigned long off
, unsigned long len
)
4005 memcpy(dst_buff
, src_buff
+ off
, len
);
4009 BPF_CALL_5(bpf_xdp_event_output
, struct xdp_buff
*, xdp
, struct bpf_map
*, map
,
4010 u64
, flags
, void *, meta
, u64
, meta_size
)
4012 u64 xdp_size
= (flags
& BPF_F_CTXLEN_MASK
) >> 32;
4014 if (unlikely(flags
& ~(BPF_F_CTXLEN_MASK
| BPF_F_INDEX_MASK
)))
4016 if (unlikely(xdp_size
> (unsigned long)(xdp
->data_end
- xdp
->data
)))
4019 return bpf_event_output(map
, flags
, meta
, meta_size
, xdp
->data
,
4020 xdp_size
, bpf_xdp_copy
);
4023 static const struct bpf_func_proto bpf_xdp_event_output_proto
= {
4024 .func
= bpf_xdp_event_output
,
4026 .ret_type
= RET_INTEGER
,
4027 .arg1_type
= ARG_PTR_TO_CTX
,
4028 .arg2_type
= ARG_CONST_MAP_PTR
,
4029 .arg3_type
= ARG_ANYTHING
,
4030 .arg4_type
= ARG_PTR_TO_MEM
,
4031 .arg5_type
= ARG_CONST_SIZE_OR_ZERO
,
4034 BPF_CALL_1(bpf_get_socket_cookie
, struct sk_buff
*, skb
)
4036 return skb
->sk
? sock_gen_cookie(skb
->sk
) : 0;
4039 static const struct bpf_func_proto bpf_get_socket_cookie_proto
= {
4040 .func
= bpf_get_socket_cookie
,
4042 .ret_type
= RET_INTEGER
,
4043 .arg1_type
= ARG_PTR_TO_CTX
,
4046 BPF_CALL_1(bpf_get_socket_cookie_sock_addr
, struct bpf_sock_addr_kern
*, ctx
)
4048 return sock_gen_cookie(ctx
->sk
);
4051 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto
= {
4052 .func
= bpf_get_socket_cookie_sock_addr
,
4054 .ret_type
= RET_INTEGER
,
4055 .arg1_type
= ARG_PTR_TO_CTX
,
4058 BPF_CALL_1(bpf_get_socket_cookie_sock_ops
, struct bpf_sock_ops_kern
*, ctx
)
4060 return sock_gen_cookie(ctx
->sk
);
4063 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto
= {
4064 .func
= bpf_get_socket_cookie_sock_ops
,
4066 .ret_type
= RET_INTEGER
,
4067 .arg1_type
= ARG_PTR_TO_CTX
,
4070 BPF_CALL_1(bpf_get_socket_uid
, struct sk_buff
*, skb
)
4072 struct sock
*sk
= sk_to_full_sk(skb
->sk
);
4075 if (!sk
|| !sk_fullsock(sk
))
4077 kuid
= sock_net_uid(sock_net(sk
), sk
);
4078 return from_kuid_munged(sock_net(sk
)->user_ns
, kuid
);
4081 static const struct bpf_func_proto bpf_get_socket_uid_proto
= {
4082 .func
= bpf_get_socket_uid
,
4084 .ret_type
= RET_INTEGER
,
4085 .arg1_type
= ARG_PTR_TO_CTX
,
4088 BPF_CALL_5(bpf_sockopt_event_output
, struct bpf_sock_ops_kern
*, bpf_sock
,
4089 struct bpf_map
*, map
, u64
, flags
, void *, data
, u64
, size
)
4091 if (unlikely(flags
& ~(BPF_F_INDEX_MASK
)))
4094 return bpf_event_output(map
, flags
, data
, size
, NULL
, 0, NULL
);
4097 static const struct bpf_func_proto bpf_sockopt_event_output_proto
= {
4098 .func
= bpf_sockopt_event_output
,
4100 .ret_type
= RET_INTEGER
,
4101 .arg1_type
= ARG_PTR_TO_CTX
,
4102 .arg2_type
= ARG_CONST_MAP_PTR
,
4103 .arg3_type
= ARG_ANYTHING
,
4104 .arg4_type
= ARG_PTR_TO_MEM
,
4105 .arg5_type
= ARG_CONST_SIZE_OR_ZERO
,
4108 BPF_CALL_5(bpf_setsockopt
, struct bpf_sock_ops_kern
*, bpf_sock
,
4109 int, level
, int, optname
, char *, optval
, int, optlen
)
4111 struct sock
*sk
= bpf_sock
->sk
;
4115 if (!sk_fullsock(sk
))
4118 if (level
== SOL_SOCKET
) {
4119 if (optlen
!= sizeof(int))
4121 val
= *((int *)optval
);
4123 /* Only some socketops are supported */
4126 val
= min_t(u32
, val
, sysctl_rmem_max
);
4127 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
4128 sk
->sk_rcvbuf
= max_t(int, val
* 2, SOCK_MIN_RCVBUF
);
4131 val
= min_t(u32
, val
, sysctl_wmem_max
);
4132 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
4133 sk
->sk_sndbuf
= max_t(int, val
* 2, SOCK_MIN_SNDBUF
);
4135 case SO_MAX_PACING_RATE
: /* 32bit version */
4137 cmpxchg(&sk
->sk_pacing_status
,
4140 sk
->sk_max_pacing_rate
= (val
== ~0U) ? ~0UL : val
;
4141 sk
->sk_pacing_rate
= min(sk
->sk_pacing_rate
,
4142 sk
->sk_max_pacing_rate
);
4145 sk
->sk_priority
= val
;
4150 sk
->sk_rcvlowat
= val
? : 1;
4153 if (sk
->sk_mark
!= val
) {
4162 } else if (level
== SOL_IP
) {
4163 if (optlen
!= sizeof(int) || sk
->sk_family
!= AF_INET
)
4166 val
= *((int *)optval
);
4167 /* Only some options are supported */
4170 if (val
< -1 || val
> 0xff) {
4173 struct inet_sock
*inet
= inet_sk(sk
);
4183 #if IS_ENABLED(CONFIG_IPV6)
4184 } else if (level
== SOL_IPV6
) {
4185 if (optlen
!= sizeof(int) || sk
->sk_family
!= AF_INET6
)
4188 val
= *((int *)optval
);
4189 /* Only some options are supported */
4192 if (val
< -1 || val
> 0xff) {
4195 struct ipv6_pinfo
*np
= inet6_sk(sk
);
4206 } else if (level
== SOL_TCP
&&
4207 sk
->sk_prot
->setsockopt
== tcp_setsockopt
) {
4208 if (optname
== TCP_CONGESTION
) {
4209 char name
[TCP_CA_NAME_MAX
];
4210 bool reinit
= bpf_sock
->op
> BPF_SOCK_OPS_NEEDS_ECN
;
4212 strncpy(name
, optval
, min_t(long, optlen
,
4213 TCP_CA_NAME_MAX
-1));
4214 name
[TCP_CA_NAME_MAX
-1] = 0;
4215 ret
= tcp_set_congestion_control(sk
, name
, false,
4218 struct tcp_sock
*tp
= tcp_sk(sk
);
4220 if (optlen
!= sizeof(int))
4223 val
= *((int *)optval
);
4224 /* Only some options are supported */
4227 if (val
<= 0 || tp
->data_segs_out
> tp
->syn_data
)
4232 case TCP_BPF_SNDCWND_CLAMP
:
4236 tp
->snd_cwnd_clamp
= val
;
4237 tp
->snd_ssthresh
= val
;
4241 if (val
< 0 || val
> 1)
4257 static const struct bpf_func_proto bpf_setsockopt_proto
= {
4258 .func
= bpf_setsockopt
,
4260 .ret_type
= RET_INTEGER
,
4261 .arg1_type
= ARG_PTR_TO_CTX
,
4262 .arg2_type
= ARG_ANYTHING
,
4263 .arg3_type
= ARG_ANYTHING
,
4264 .arg4_type
= ARG_PTR_TO_MEM
,
4265 .arg5_type
= ARG_CONST_SIZE
,
4268 BPF_CALL_5(bpf_getsockopt
, struct bpf_sock_ops_kern
*, bpf_sock
,
4269 int, level
, int, optname
, char *, optval
, int, optlen
)
4271 struct sock
*sk
= bpf_sock
->sk
;
4273 if (!sk_fullsock(sk
))
4276 if (level
== SOL_TCP
&& sk
->sk_prot
->getsockopt
== tcp_getsockopt
) {
4277 struct inet_connection_sock
*icsk
;
4278 struct tcp_sock
*tp
;
4281 case TCP_CONGESTION
:
4282 icsk
= inet_csk(sk
);
4284 if (!icsk
->icsk_ca_ops
|| optlen
<= 1)
4286 strncpy(optval
, icsk
->icsk_ca_ops
->name
, optlen
);
4287 optval
[optlen
- 1] = 0;
4292 if (optlen
<= 0 || !tp
->saved_syn
||
4293 optlen
> tp
->saved_syn
[0])
4295 memcpy(optval
, tp
->saved_syn
+ 1, optlen
);
4300 } else if (level
== SOL_IP
) {
4301 struct inet_sock
*inet
= inet_sk(sk
);
4303 if (optlen
!= sizeof(int) || sk
->sk_family
!= AF_INET
)
4306 /* Only some options are supported */
4309 *((int *)optval
) = (int)inet
->tos
;
4314 #if IS_ENABLED(CONFIG_IPV6)
4315 } else if (level
== SOL_IPV6
) {
4316 struct ipv6_pinfo
*np
= inet6_sk(sk
);
4318 if (optlen
!= sizeof(int) || sk
->sk_family
!= AF_INET6
)
4321 /* Only some options are supported */
4324 *((int *)optval
) = (int)np
->tclass
;
4336 memset(optval
, 0, optlen
);
4340 static const struct bpf_func_proto bpf_getsockopt_proto
= {
4341 .func
= bpf_getsockopt
,
4343 .ret_type
= RET_INTEGER
,
4344 .arg1_type
= ARG_PTR_TO_CTX
,
4345 .arg2_type
= ARG_ANYTHING
,
4346 .arg3_type
= ARG_ANYTHING
,
4347 .arg4_type
= ARG_PTR_TO_UNINIT_MEM
,
4348 .arg5_type
= ARG_CONST_SIZE
,
4351 BPF_CALL_2(bpf_sock_ops_cb_flags_set
, struct bpf_sock_ops_kern
*, bpf_sock
,
4354 struct sock
*sk
= bpf_sock
->sk
;
4355 int val
= argval
& BPF_SOCK_OPS_ALL_CB_FLAGS
;
4357 if (!IS_ENABLED(CONFIG_INET
) || !sk_fullsock(sk
))
4361 tcp_sk(sk
)->bpf_sock_ops_cb_flags
= val
;
4363 return argval
& (~BPF_SOCK_OPS_ALL_CB_FLAGS
);
4366 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto
= {
4367 .func
= bpf_sock_ops_cb_flags_set
,
4369 .ret_type
= RET_INTEGER
,
4370 .arg1_type
= ARG_PTR_TO_CTX
,
4371 .arg2_type
= ARG_ANYTHING
,
4374 const struct ipv6_bpf_stub
*ipv6_bpf_stub __read_mostly
;
4375 EXPORT_SYMBOL_GPL(ipv6_bpf_stub
);
4377 BPF_CALL_3(bpf_bind
, struct bpf_sock_addr_kern
*, ctx
, struct sockaddr
*, addr
,
4381 struct sock
*sk
= ctx
->sk
;
4384 /* Binding to port can be expensive so it's prohibited in the helper.
4385 * Only binding to IP is supported.
4388 if (addr
->sa_family
== AF_INET
) {
4389 if (addr_len
< sizeof(struct sockaddr_in
))
4391 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
4393 return __inet_bind(sk
, addr
, addr_len
, true, false);
4394 #if IS_ENABLED(CONFIG_IPV6)
4395 } else if (addr
->sa_family
== AF_INET6
) {
4396 if (addr_len
< SIN6_LEN_RFC2133
)
4398 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
4400 /* ipv6_bpf_stub cannot be NULL, since it's called from
4401 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4403 return ipv6_bpf_stub
->inet6_bind(sk
, addr
, addr_len
, true, false);
4404 #endif /* CONFIG_IPV6 */
4406 #endif /* CONFIG_INET */
4408 return -EAFNOSUPPORT
;
4411 static const struct bpf_func_proto bpf_bind_proto
= {
4414 .ret_type
= RET_INTEGER
,
4415 .arg1_type
= ARG_PTR_TO_CTX
,
4416 .arg2_type
= ARG_PTR_TO_MEM
,
4417 .arg3_type
= ARG_CONST_SIZE
,
4421 BPF_CALL_5(bpf_skb_get_xfrm_state
, struct sk_buff
*, skb
, u32
, index
,
4422 struct bpf_xfrm_state
*, to
, u32
, size
, u64
, flags
)
4424 const struct sec_path
*sp
= skb_sec_path(skb
);
4425 const struct xfrm_state
*x
;
4427 if (!sp
|| unlikely(index
>= sp
->len
|| flags
))
4430 x
= sp
->xvec
[index
];
4432 if (unlikely(size
!= sizeof(struct bpf_xfrm_state
)))
4435 to
->reqid
= x
->props
.reqid
;
4436 to
->spi
= x
->id
.spi
;
4437 to
->family
= x
->props
.family
;
4440 if (to
->family
== AF_INET6
) {
4441 memcpy(to
->remote_ipv6
, x
->props
.saddr
.a6
,
4442 sizeof(to
->remote_ipv6
));
4444 to
->remote_ipv4
= x
->props
.saddr
.a4
;
4445 memset(&to
->remote_ipv6
[1], 0, sizeof(__u32
) * 3);
4450 memset(to
, 0, size
);
4454 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto
= {
4455 .func
= bpf_skb_get_xfrm_state
,
4457 .ret_type
= RET_INTEGER
,
4458 .arg1_type
= ARG_PTR_TO_CTX
,
4459 .arg2_type
= ARG_ANYTHING
,
4460 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
4461 .arg4_type
= ARG_CONST_SIZE
,
4462 .arg5_type
= ARG_ANYTHING
,
4466 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4467 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup
*params
,
4468 const struct neighbour
*neigh
,
4469 const struct net_device
*dev
)
4471 memcpy(params
->dmac
, neigh
->ha
, ETH_ALEN
);
4472 memcpy(params
->smac
, dev
->dev_addr
, ETH_ALEN
);
4473 params
->h_vlan_TCI
= 0;
4474 params
->h_vlan_proto
= 0;
4475 params
->ifindex
= dev
->ifindex
;
4481 #if IS_ENABLED(CONFIG_INET)
4482 static int bpf_ipv4_fib_lookup(struct net
*net
, struct bpf_fib_lookup
*params
,
4483 u32 flags
, bool check_mtu
)
4485 struct in_device
*in_dev
;
4486 struct neighbour
*neigh
;
4487 struct net_device
*dev
;
4488 struct fib_result res
;
4494 dev
= dev_get_by_index_rcu(net
, params
->ifindex
);
4498 /* verify forwarding is enabled on this interface */
4499 in_dev
= __in_dev_get_rcu(dev
);
4500 if (unlikely(!in_dev
|| !IN_DEV_FORWARD(in_dev
)))
4501 return BPF_FIB_LKUP_RET_FWD_DISABLED
;
4503 if (flags
& BPF_FIB_LOOKUP_OUTPUT
) {
4505 fl4
.flowi4_oif
= params
->ifindex
;
4507 fl4
.flowi4_iif
= params
->ifindex
;
4510 fl4
.flowi4_tos
= params
->tos
& IPTOS_RT_MASK
;
4511 fl4
.flowi4_scope
= RT_SCOPE_UNIVERSE
;
4512 fl4
.flowi4_flags
= 0;
4514 fl4
.flowi4_proto
= params
->l4_protocol
;
4515 fl4
.daddr
= params
->ipv4_dst
;
4516 fl4
.saddr
= params
->ipv4_src
;
4517 fl4
.fl4_sport
= params
->sport
;
4518 fl4
.fl4_dport
= params
->dport
;
4520 if (flags
& BPF_FIB_LOOKUP_DIRECT
) {
4521 u32 tbid
= l3mdev_fib_table_rcu(dev
) ? : RT_TABLE_MAIN
;
4522 struct fib_table
*tb
;
4524 tb
= fib_get_table(net
, tbid
);
4526 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4528 err
= fib_table_lookup(tb
, &fl4
, &res
, FIB_LOOKUP_NOREF
);
4530 fl4
.flowi4_mark
= 0;
4531 fl4
.flowi4_secid
= 0;
4532 fl4
.flowi4_tun_key
.tun_id
= 0;
4533 fl4
.flowi4_uid
= sock_net_uid(net
, NULL
);
4535 err
= fib_lookup(net
, &fl4
, &res
, FIB_LOOKUP_NOREF
);
4539 /* map fib lookup errors to RTN_ type */
4541 return BPF_FIB_LKUP_RET_BLACKHOLE
;
4542 if (err
== -EHOSTUNREACH
)
4543 return BPF_FIB_LKUP_RET_UNREACHABLE
;
4545 return BPF_FIB_LKUP_RET_PROHIBIT
;
4547 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4550 if (res
.type
!= RTN_UNICAST
)
4551 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4553 if (res
.fi
->fib_nhs
> 1)
4554 fib_select_path(net
, &res
, &fl4
, NULL
);
4557 mtu
= ip_mtu_from_fib_result(&res
, params
->ipv4_dst
);
4558 if (params
->tot_len
> mtu
)
4559 return BPF_FIB_LKUP_RET_FRAG_NEEDED
;
4562 nh
= &res
.fi
->fib_nh
[res
.nh_sel
];
4564 /* do not handle lwt encaps right now */
4565 if (nh
->nh_lwtstate
)
4566 return BPF_FIB_LKUP_RET_UNSUPP_LWT
;
4570 params
->ipv4_dst
= nh
->nh_gw
;
4572 params
->rt_metric
= res
.fi
->fib_priority
;
4574 /* xdp and cls_bpf programs are run in RCU-bh so
4575 * rcu_read_lock_bh is not needed here
4577 neigh
= __ipv4_neigh_lookup_noref(dev
, (__force u32
)params
->ipv4_dst
);
4579 return BPF_FIB_LKUP_RET_NO_NEIGH
;
4581 return bpf_fib_set_fwd_params(params
, neigh
, dev
);
4585 #if IS_ENABLED(CONFIG_IPV6)
4586 static int bpf_ipv6_fib_lookup(struct net
*net
, struct bpf_fib_lookup
*params
,
4587 u32 flags
, bool check_mtu
)
4589 struct in6_addr
*src
= (struct in6_addr
*) params
->ipv6_src
;
4590 struct in6_addr
*dst
= (struct in6_addr
*) params
->ipv6_dst
;
4591 struct neighbour
*neigh
;
4592 struct net_device
*dev
;
4593 struct inet6_dev
*idev
;
4594 struct fib6_info
*f6i
;
4600 /* link local addresses are never forwarded */
4601 if (rt6_need_strict(dst
) || rt6_need_strict(src
))
4602 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4604 dev
= dev_get_by_index_rcu(net
, params
->ifindex
);
4608 idev
= __in6_dev_get_safely(dev
);
4609 if (unlikely(!idev
|| !net
->ipv6
.devconf_all
->forwarding
))
4610 return BPF_FIB_LKUP_RET_FWD_DISABLED
;
4612 if (flags
& BPF_FIB_LOOKUP_OUTPUT
) {
4614 oif
= fl6
.flowi6_oif
= params
->ifindex
;
4616 oif
= fl6
.flowi6_iif
= params
->ifindex
;
4618 strict
= RT6_LOOKUP_F_HAS_SADDR
;
4620 fl6
.flowlabel
= params
->flowinfo
;
4621 fl6
.flowi6_scope
= 0;
4622 fl6
.flowi6_flags
= 0;
4625 fl6
.flowi6_proto
= params
->l4_protocol
;
4628 fl6
.fl6_sport
= params
->sport
;
4629 fl6
.fl6_dport
= params
->dport
;
4631 if (flags
& BPF_FIB_LOOKUP_DIRECT
) {
4632 u32 tbid
= l3mdev_fib_table_rcu(dev
) ? : RT_TABLE_MAIN
;
4633 struct fib6_table
*tb
;
4635 tb
= ipv6_stub
->fib6_get_table(net
, tbid
);
4637 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4639 f6i
= ipv6_stub
->fib6_table_lookup(net
, tb
, oif
, &fl6
, strict
);
4641 fl6
.flowi6_mark
= 0;
4642 fl6
.flowi6_secid
= 0;
4643 fl6
.flowi6_tun_key
.tun_id
= 0;
4644 fl6
.flowi6_uid
= sock_net_uid(net
, NULL
);
4646 f6i
= ipv6_stub
->fib6_lookup(net
, oif
, &fl6
, strict
);
4649 if (unlikely(IS_ERR_OR_NULL(f6i
) || f6i
== net
->ipv6
.fib6_null_entry
))
4650 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4652 if (unlikely(f6i
->fib6_flags
& RTF_REJECT
)) {
4653 switch (f6i
->fib6_type
) {
4655 return BPF_FIB_LKUP_RET_BLACKHOLE
;
4656 case RTN_UNREACHABLE
:
4657 return BPF_FIB_LKUP_RET_UNREACHABLE
;
4659 return BPF_FIB_LKUP_RET_PROHIBIT
;
4661 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4665 if (f6i
->fib6_type
!= RTN_UNICAST
)
4666 return BPF_FIB_LKUP_RET_NOT_FWDED
;
4668 if (f6i
->fib6_nsiblings
&& fl6
.flowi6_oif
== 0)
4669 f6i
= ipv6_stub
->fib6_multipath_select(net
, f6i
, &fl6
,
4670 fl6
.flowi6_oif
, NULL
,
4674 mtu
= ipv6_stub
->ip6_mtu_from_fib6(f6i
, dst
, src
);
4675 if (params
->tot_len
> mtu
)
4676 return BPF_FIB_LKUP_RET_FRAG_NEEDED
;
4679 if (f6i
->fib6_nh
.nh_lwtstate
)
4680 return BPF_FIB_LKUP_RET_UNSUPP_LWT
;
4682 if (f6i
->fib6_flags
& RTF_GATEWAY
)
4683 *dst
= f6i
->fib6_nh
.nh_gw
;
4685 dev
= f6i
->fib6_nh
.nh_dev
;
4686 params
->rt_metric
= f6i
->fib6_metric
;
4688 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4689 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4690 * because we need to get nd_tbl via the stub
4692 neigh
= ___neigh_lookup_noref(ipv6_stub
->nd_tbl
, neigh_key_eq128
,
4693 ndisc_hashfn
, dst
, dev
);
4695 return BPF_FIB_LKUP_RET_NO_NEIGH
;
4697 return bpf_fib_set_fwd_params(params
, neigh
, dev
);
4701 BPF_CALL_4(bpf_xdp_fib_lookup
, struct xdp_buff
*, ctx
,
4702 struct bpf_fib_lookup
*, params
, int, plen
, u32
, flags
)
4704 if (plen
< sizeof(*params
))
4707 if (flags
& ~(BPF_FIB_LOOKUP_DIRECT
| BPF_FIB_LOOKUP_OUTPUT
))
4710 switch (params
->family
) {
4711 #if IS_ENABLED(CONFIG_INET)
4713 return bpf_ipv4_fib_lookup(dev_net(ctx
->rxq
->dev
), params
,
4716 #if IS_ENABLED(CONFIG_IPV6)
4718 return bpf_ipv6_fib_lookup(dev_net(ctx
->rxq
->dev
), params
,
4722 return -EAFNOSUPPORT
;
4725 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto
= {
4726 .func
= bpf_xdp_fib_lookup
,
4728 .ret_type
= RET_INTEGER
,
4729 .arg1_type
= ARG_PTR_TO_CTX
,
4730 .arg2_type
= ARG_PTR_TO_MEM
,
4731 .arg3_type
= ARG_CONST_SIZE
,
4732 .arg4_type
= ARG_ANYTHING
,
4735 BPF_CALL_4(bpf_skb_fib_lookup
, struct sk_buff
*, skb
,
4736 struct bpf_fib_lookup
*, params
, int, plen
, u32
, flags
)
4738 struct net
*net
= dev_net(skb
->dev
);
4739 int rc
= -EAFNOSUPPORT
;
4741 if (plen
< sizeof(*params
))
4744 if (flags
& ~(BPF_FIB_LOOKUP_DIRECT
| BPF_FIB_LOOKUP_OUTPUT
))
4747 switch (params
->family
) {
4748 #if IS_ENABLED(CONFIG_INET)
4750 rc
= bpf_ipv4_fib_lookup(net
, params
, flags
, false);
4753 #if IS_ENABLED(CONFIG_IPV6)
4755 rc
= bpf_ipv6_fib_lookup(net
, params
, flags
, false);
4761 struct net_device
*dev
;
4763 dev
= dev_get_by_index_rcu(net
, params
->ifindex
);
4764 if (!is_skb_forwardable(dev
, skb
))
4765 rc
= BPF_FIB_LKUP_RET_FRAG_NEEDED
;
4771 static const struct bpf_func_proto bpf_skb_fib_lookup_proto
= {
4772 .func
= bpf_skb_fib_lookup
,
4774 .ret_type
= RET_INTEGER
,
4775 .arg1_type
= ARG_PTR_TO_CTX
,
4776 .arg2_type
= ARG_PTR_TO_MEM
,
4777 .arg3_type
= ARG_CONST_SIZE
,
4778 .arg4_type
= ARG_ANYTHING
,
4781 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4782 static int bpf_push_seg6_encap(struct sk_buff
*skb
, u32 type
, void *hdr
, u32 len
)
4785 struct ipv6_sr_hdr
*srh
= (struct ipv6_sr_hdr
*)hdr
;
4787 if (!seg6_validate_srh(srh
, len
))
4791 case BPF_LWT_ENCAP_SEG6_INLINE
:
4792 if (skb
->protocol
!= htons(ETH_P_IPV6
))
4795 err
= seg6_do_srh_inline(skb
, srh
);
4797 case BPF_LWT_ENCAP_SEG6
:
4798 skb_reset_inner_headers(skb
);
4799 skb
->encapsulation
= 1;
4800 err
= seg6_do_srh_encap(skb
, srh
, IPPROTO_IPV6
);
4806 bpf_compute_data_pointers(skb
);
4810 ipv6_hdr(skb
)->payload_len
= htons(skb
->len
- sizeof(struct ipv6hdr
));
4811 skb_set_transport_header(skb
, sizeof(struct ipv6hdr
));
4813 return seg6_lookup_nexthop(skb
, NULL
, 0);
4815 #endif /* CONFIG_IPV6_SEG6_BPF */
4817 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4818 static int bpf_push_ip_encap(struct sk_buff
*skb
, void *hdr
, u32 len
,
4821 return bpf_lwt_push_ip_encap(skb
, hdr
, len
, ingress
);
4825 BPF_CALL_4(bpf_lwt_in_push_encap
, struct sk_buff
*, skb
, u32
, type
, void *, hdr
,
4829 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4830 case BPF_LWT_ENCAP_SEG6
:
4831 case BPF_LWT_ENCAP_SEG6_INLINE
:
4832 return bpf_push_seg6_encap(skb
, type
, hdr
, len
);
4834 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4835 case BPF_LWT_ENCAP_IP
:
4836 return bpf_push_ip_encap(skb
, hdr
, len
, true /* ingress */);
4843 BPF_CALL_4(bpf_lwt_xmit_push_encap
, struct sk_buff
*, skb
, u32
, type
,
4844 void *, hdr
, u32
, len
)
4847 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4848 case BPF_LWT_ENCAP_IP
:
4849 return bpf_push_ip_encap(skb
, hdr
, len
, false /* egress */);
4856 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto
= {
4857 .func
= bpf_lwt_in_push_encap
,
4859 .ret_type
= RET_INTEGER
,
4860 .arg1_type
= ARG_PTR_TO_CTX
,
4861 .arg2_type
= ARG_ANYTHING
,
4862 .arg3_type
= ARG_PTR_TO_MEM
,
4863 .arg4_type
= ARG_CONST_SIZE
4866 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto
= {
4867 .func
= bpf_lwt_xmit_push_encap
,
4869 .ret_type
= RET_INTEGER
,
4870 .arg1_type
= ARG_PTR_TO_CTX
,
4871 .arg2_type
= ARG_ANYTHING
,
4872 .arg3_type
= ARG_PTR_TO_MEM
,
4873 .arg4_type
= ARG_CONST_SIZE
4876 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4877 BPF_CALL_4(bpf_lwt_seg6_store_bytes
, struct sk_buff
*, skb
, u32
, offset
,
4878 const void *, from
, u32
, len
)
4880 struct seg6_bpf_srh_state
*srh_state
=
4881 this_cpu_ptr(&seg6_bpf_srh_states
);
4882 struct ipv6_sr_hdr
*srh
= srh_state
->srh
;
4883 void *srh_tlvs
, *srh_end
, *ptr
;
4889 srh_tlvs
= (void *)((char *)srh
+ ((srh
->first_segment
+ 1) << 4));
4890 srh_end
= (void *)((char *)srh
+ sizeof(*srh
) + srh_state
->hdrlen
);
4892 ptr
= skb
->data
+ offset
;
4893 if (ptr
>= srh_tlvs
&& ptr
+ len
<= srh_end
)
4894 srh_state
->valid
= false;
4895 else if (ptr
< (void *)&srh
->flags
||
4896 ptr
+ len
> (void *)&srh
->segments
)
4899 if (unlikely(bpf_try_make_writable(skb
, offset
+ len
)))
4901 if (ipv6_find_hdr(skb
, &srhoff
, IPPROTO_ROUTING
, NULL
, NULL
) < 0)
4903 srh_state
->srh
= (struct ipv6_sr_hdr
*)(skb
->data
+ srhoff
);
4905 memcpy(skb
->data
+ offset
, from
, len
);
4909 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto
= {
4910 .func
= bpf_lwt_seg6_store_bytes
,
4912 .ret_type
= RET_INTEGER
,
4913 .arg1_type
= ARG_PTR_TO_CTX
,
4914 .arg2_type
= ARG_ANYTHING
,
4915 .arg3_type
= ARG_PTR_TO_MEM
,
4916 .arg4_type
= ARG_CONST_SIZE
4919 static void bpf_update_srh_state(struct sk_buff
*skb
)
4921 struct seg6_bpf_srh_state
*srh_state
=
4922 this_cpu_ptr(&seg6_bpf_srh_states
);
4925 if (ipv6_find_hdr(skb
, &srhoff
, IPPROTO_ROUTING
, NULL
, NULL
) < 0) {
4926 srh_state
->srh
= NULL
;
4928 srh_state
->srh
= (struct ipv6_sr_hdr
*)(skb
->data
+ srhoff
);
4929 srh_state
->hdrlen
= srh_state
->srh
->hdrlen
<< 3;
4930 srh_state
->valid
= true;
4934 BPF_CALL_4(bpf_lwt_seg6_action
, struct sk_buff
*, skb
,
4935 u32
, action
, void *, param
, u32
, param_len
)
4937 struct seg6_bpf_srh_state
*srh_state
=
4938 this_cpu_ptr(&seg6_bpf_srh_states
);
4943 case SEG6_LOCAL_ACTION_END_X
:
4944 if (!seg6_bpf_has_valid_srh(skb
))
4946 if (param_len
!= sizeof(struct in6_addr
))
4948 return seg6_lookup_nexthop(skb
, (struct in6_addr
*)param
, 0);
4949 case SEG6_LOCAL_ACTION_END_T
:
4950 if (!seg6_bpf_has_valid_srh(skb
))
4952 if (param_len
!= sizeof(int))
4954 return seg6_lookup_nexthop(skb
, NULL
, *(int *)param
);
4955 case SEG6_LOCAL_ACTION_END_DT6
:
4956 if (!seg6_bpf_has_valid_srh(skb
))
4958 if (param_len
!= sizeof(int))
4961 if (ipv6_find_hdr(skb
, &hdroff
, IPPROTO_IPV6
, NULL
, NULL
) < 0)
4963 if (!pskb_pull(skb
, hdroff
))
4966 skb_postpull_rcsum(skb
, skb_network_header(skb
), hdroff
);
4967 skb_reset_network_header(skb
);
4968 skb_reset_transport_header(skb
);
4969 skb
->encapsulation
= 0;
4971 bpf_compute_data_pointers(skb
);
4972 bpf_update_srh_state(skb
);
4973 return seg6_lookup_nexthop(skb
, NULL
, *(int *)param
);
4974 case SEG6_LOCAL_ACTION_END_B6
:
4975 if (srh_state
->srh
&& !seg6_bpf_has_valid_srh(skb
))
4977 err
= bpf_push_seg6_encap(skb
, BPF_LWT_ENCAP_SEG6_INLINE
,
4980 bpf_update_srh_state(skb
);
4983 case SEG6_LOCAL_ACTION_END_B6_ENCAP
:
4984 if (srh_state
->srh
&& !seg6_bpf_has_valid_srh(skb
))
4986 err
= bpf_push_seg6_encap(skb
, BPF_LWT_ENCAP_SEG6
,
4989 bpf_update_srh_state(skb
);
4997 static const struct bpf_func_proto bpf_lwt_seg6_action_proto
= {
4998 .func
= bpf_lwt_seg6_action
,
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 BPF_CALL_3(bpf_lwt_seg6_adjust_srh
, struct sk_buff
*, skb
, u32
, offset
,
5010 struct seg6_bpf_srh_state
*srh_state
=
5011 this_cpu_ptr(&seg6_bpf_srh_states
);
5012 struct ipv6_sr_hdr
*srh
= srh_state
->srh
;
5013 void *srh_end
, *srh_tlvs
, *ptr
;
5014 struct ipv6hdr
*hdr
;
5018 if (unlikely(srh
== NULL
))
5021 srh_tlvs
= (void *)((unsigned char *)srh
+ sizeof(*srh
) +
5022 ((srh
->first_segment
+ 1) << 4));
5023 srh_end
= (void *)((unsigned char *)srh
+ sizeof(*srh
) +
5025 ptr
= skb
->data
+ offset
;
5027 if (unlikely(ptr
< srh_tlvs
|| ptr
> srh_end
))
5029 if (unlikely(len
< 0 && (void *)((char *)ptr
- len
) > srh_end
))
5033 ret
= skb_cow_head(skb
, len
);
5034 if (unlikely(ret
< 0))
5037 ret
= bpf_skb_net_hdr_push(skb
, offset
, len
);
5039 ret
= bpf_skb_net_hdr_pop(skb
, offset
, -1 * len
);
5042 bpf_compute_data_pointers(skb
);
5043 if (unlikely(ret
< 0))
5046 hdr
= (struct ipv6hdr
*)skb
->data
;
5047 hdr
->payload_len
= htons(skb
->len
- sizeof(struct ipv6hdr
));
5049 if (ipv6_find_hdr(skb
, &srhoff
, IPPROTO_ROUTING
, NULL
, NULL
) < 0)
5051 srh_state
->srh
= (struct ipv6_sr_hdr
*)(skb
->data
+ srhoff
);
5052 srh_state
->hdrlen
+= len
;
5053 srh_state
->valid
= false;
5057 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto
= {
5058 .func
= bpf_lwt_seg6_adjust_srh
,
5060 .ret_type
= RET_INTEGER
,
5061 .arg1_type
= ARG_PTR_TO_CTX
,
5062 .arg2_type
= ARG_ANYTHING
,
5063 .arg3_type
= ARG_ANYTHING
,
5065 #endif /* CONFIG_IPV6_SEG6_BPF */
5067 #define CONVERT_COMMON_TCP_SOCK_FIELDS(md_type, CONVERT) \
5069 switch (si->off) { \
5070 case offsetof(md_type, snd_cwnd): \
5071 CONVERT(snd_cwnd); break; \
5072 case offsetof(md_type, srtt_us): \
5073 CONVERT(srtt_us); break; \
5074 case offsetof(md_type, snd_ssthresh): \
5075 CONVERT(snd_ssthresh); break; \
5076 case offsetof(md_type, rcv_nxt): \
5077 CONVERT(rcv_nxt); break; \
5078 case offsetof(md_type, snd_nxt): \
5079 CONVERT(snd_nxt); break; \
5080 case offsetof(md_type, snd_una): \
5081 CONVERT(snd_una); break; \
5082 case offsetof(md_type, mss_cache): \
5083 CONVERT(mss_cache); break; \
5084 case offsetof(md_type, ecn_flags): \
5085 CONVERT(ecn_flags); break; \
5086 case offsetof(md_type, rate_delivered): \
5087 CONVERT(rate_delivered); break; \
5088 case offsetof(md_type, rate_interval_us): \
5089 CONVERT(rate_interval_us); break; \
5090 case offsetof(md_type, packets_out): \
5091 CONVERT(packets_out); break; \
5092 case offsetof(md_type, retrans_out): \
5093 CONVERT(retrans_out); break; \
5094 case offsetof(md_type, total_retrans): \
5095 CONVERT(total_retrans); break; \
5096 case offsetof(md_type, segs_in): \
5097 CONVERT(segs_in); break; \
5098 case offsetof(md_type, data_segs_in): \
5099 CONVERT(data_segs_in); break; \
5100 case offsetof(md_type, segs_out): \
5101 CONVERT(segs_out); break; \
5102 case offsetof(md_type, data_segs_out): \
5103 CONVERT(data_segs_out); break; \
5104 case offsetof(md_type, lost_out): \
5105 CONVERT(lost_out); break; \
5106 case offsetof(md_type, sacked_out): \
5107 CONVERT(sacked_out); break; \
5108 case offsetof(md_type, bytes_received): \
5109 CONVERT(bytes_received); break; \
5110 case offsetof(md_type, bytes_acked): \
5111 CONVERT(bytes_acked); break; \
5116 static struct sock
*sk_lookup(struct net
*net
, struct bpf_sock_tuple
*tuple
,
5117 int dif
, int sdif
, u8 family
, u8 proto
)
5119 bool refcounted
= false;
5120 struct sock
*sk
= NULL
;
5122 if (family
== AF_INET
) {
5123 __be32 src4
= tuple
->ipv4
.saddr
;
5124 __be32 dst4
= tuple
->ipv4
.daddr
;
5126 if (proto
== IPPROTO_TCP
)
5127 sk
= __inet_lookup(net
, &tcp_hashinfo
, NULL
, 0,
5128 src4
, tuple
->ipv4
.sport
,
5129 dst4
, tuple
->ipv4
.dport
,
5130 dif
, sdif
, &refcounted
);
5132 sk
= __udp4_lib_lookup(net
, src4
, tuple
->ipv4
.sport
,
5133 dst4
, tuple
->ipv4
.dport
,
5134 dif
, sdif
, &udp_table
, NULL
);
5135 #if IS_ENABLED(CONFIG_IPV6)
5137 struct in6_addr
*src6
= (struct in6_addr
*)&tuple
->ipv6
.saddr
;
5138 struct in6_addr
*dst6
= (struct in6_addr
*)&tuple
->ipv6
.daddr
;
5140 if (proto
== IPPROTO_TCP
)
5141 sk
= __inet6_lookup(net
, &tcp_hashinfo
, NULL
, 0,
5142 src6
, tuple
->ipv6
.sport
,
5143 dst6
, ntohs(tuple
->ipv6
.dport
),
5144 dif
, sdif
, &refcounted
);
5145 else if (likely(ipv6_bpf_stub
))
5146 sk
= ipv6_bpf_stub
->udp6_lib_lookup(net
,
5147 src6
, tuple
->ipv6
.sport
,
5148 dst6
, tuple
->ipv6
.dport
,
5154 if (unlikely(sk
&& !refcounted
&& !sock_flag(sk
, SOCK_RCU_FREE
))) {
5155 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5161 /* bpf_sk_lookup performs the core lookup for different types of sockets,
5162 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5163 * Returns the socket as an 'unsigned long' to simplify the casting in the
5164 * callers to satisfy BPF_CALL declarations.
5166 static unsigned long
5167 __bpf_sk_lookup(struct sk_buff
*skb
, struct bpf_sock_tuple
*tuple
, u32 len
,
5168 struct net
*caller_net
, u32 ifindex
, u8 proto
, u64 netns_id
,
5171 struct sock
*sk
= NULL
;
5172 u8 family
= AF_UNSPEC
;
5176 family
= len
== sizeof(tuple
->ipv4
) ? AF_INET
: AF_INET6
;
5177 if (unlikely(family
== AF_UNSPEC
|| flags
||
5178 !((s32
)netns_id
< 0 || netns_id
<= S32_MAX
)))
5181 if (family
== AF_INET
)
5182 sdif
= inet_sdif(skb
);
5184 sdif
= inet6_sdif(skb
);
5186 if ((s32
)netns_id
< 0) {
5188 sk
= sk_lookup(net
, tuple
, ifindex
, sdif
, family
, proto
);
5190 net
= get_net_ns_by_id(caller_net
, netns_id
);
5193 sk
= sk_lookup(net
, tuple
, ifindex
, sdif
, family
, proto
);
5198 sk
= sk_to_full_sk(sk
);
5200 return (unsigned long) sk
;
5203 static unsigned long
5204 bpf_sk_lookup(struct sk_buff
*skb
, struct bpf_sock_tuple
*tuple
, u32 len
,
5205 u8 proto
, u64 netns_id
, u64 flags
)
5207 struct net
*caller_net
;
5211 caller_net
= dev_net(skb
->dev
);
5212 ifindex
= skb
->dev
->ifindex
;
5214 caller_net
= sock_net(skb
->sk
);
5218 return __bpf_sk_lookup(skb
, tuple
, len
, caller_net
, ifindex
,
5219 proto
, netns_id
, flags
);
5222 BPF_CALL_5(bpf_sk_lookup_tcp
, struct sk_buff
*, skb
,
5223 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5225 return bpf_sk_lookup(skb
, tuple
, len
, IPPROTO_TCP
, netns_id
, flags
);
5228 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto
= {
5229 .func
= bpf_sk_lookup_tcp
,
5232 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5233 .arg1_type
= ARG_PTR_TO_CTX
,
5234 .arg2_type
= ARG_PTR_TO_MEM
,
5235 .arg3_type
= ARG_CONST_SIZE
,
5236 .arg4_type
= ARG_ANYTHING
,
5237 .arg5_type
= ARG_ANYTHING
,
5240 BPF_CALL_5(bpf_sk_lookup_udp
, struct sk_buff
*, skb
,
5241 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5243 return bpf_sk_lookup(skb
, tuple
, len
, IPPROTO_UDP
, netns_id
, flags
);
5246 static const struct bpf_func_proto bpf_sk_lookup_udp_proto
= {
5247 .func
= bpf_sk_lookup_udp
,
5250 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5251 .arg1_type
= ARG_PTR_TO_CTX
,
5252 .arg2_type
= ARG_PTR_TO_MEM
,
5253 .arg3_type
= ARG_CONST_SIZE
,
5254 .arg4_type
= ARG_ANYTHING
,
5255 .arg5_type
= ARG_ANYTHING
,
5258 BPF_CALL_1(bpf_sk_release
, struct sock
*, sk
)
5260 if (!sock_flag(sk
, SOCK_RCU_FREE
))
5265 static const struct bpf_func_proto bpf_sk_release_proto
= {
5266 .func
= bpf_sk_release
,
5268 .ret_type
= RET_INTEGER
,
5269 .arg1_type
= ARG_PTR_TO_SOCKET
,
5272 BPF_CALL_5(bpf_xdp_sk_lookup_udp
, struct xdp_buff
*, ctx
,
5273 struct bpf_sock_tuple
*, tuple
, u32
, len
, u32
, netns_id
, u64
, flags
)
5275 struct net
*caller_net
= dev_net(ctx
->rxq
->dev
);
5276 int ifindex
= ctx
->rxq
->dev
->ifindex
;
5278 return __bpf_sk_lookup(NULL
, tuple
, len
, caller_net
, ifindex
,
5279 IPPROTO_UDP
, netns_id
, flags
);
5282 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto
= {
5283 .func
= bpf_xdp_sk_lookup_udp
,
5286 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5287 .arg1_type
= ARG_PTR_TO_CTX
,
5288 .arg2_type
= ARG_PTR_TO_MEM
,
5289 .arg3_type
= ARG_CONST_SIZE
,
5290 .arg4_type
= ARG_ANYTHING
,
5291 .arg5_type
= ARG_ANYTHING
,
5294 BPF_CALL_5(bpf_xdp_sk_lookup_tcp
, struct xdp_buff
*, ctx
,
5295 struct bpf_sock_tuple
*, tuple
, u32
, len
, u32
, netns_id
, u64
, flags
)
5297 struct net
*caller_net
= dev_net(ctx
->rxq
->dev
);
5298 int ifindex
= ctx
->rxq
->dev
->ifindex
;
5300 return __bpf_sk_lookup(NULL
, tuple
, len
, caller_net
, ifindex
,
5301 IPPROTO_TCP
, netns_id
, flags
);
5304 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto
= {
5305 .func
= bpf_xdp_sk_lookup_tcp
,
5308 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5309 .arg1_type
= ARG_PTR_TO_CTX
,
5310 .arg2_type
= ARG_PTR_TO_MEM
,
5311 .arg3_type
= ARG_CONST_SIZE
,
5312 .arg4_type
= ARG_ANYTHING
,
5313 .arg5_type
= ARG_ANYTHING
,
5316 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp
, struct bpf_sock_addr_kern
*, ctx
,
5317 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5319 return __bpf_sk_lookup(NULL
, tuple
, len
, sock_net(ctx
->sk
), 0,
5320 IPPROTO_TCP
, netns_id
, flags
);
5323 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto
= {
5324 .func
= bpf_sock_addr_sk_lookup_tcp
,
5326 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5327 .arg1_type
= ARG_PTR_TO_CTX
,
5328 .arg2_type
= ARG_PTR_TO_MEM
,
5329 .arg3_type
= ARG_CONST_SIZE
,
5330 .arg4_type
= ARG_ANYTHING
,
5331 .arg5_type
= ARG_ANYTHING
,
5334 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp
, struct bpf_sock_addr_kern
*, ctx
,
5335 struct bpf_sock_tuple
*, tuple
, u32
, len
, u64
, netns_id
, u64
, flags
)
5337 return __bpf_sk_lookup(NULL
, tuple
, len
, sock_net(ctx
->sk
), 0,
5338 IPPROTO_UDP
, netns_id
, flags
);
5341 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto
= {
5342 .func
= bpf_sock_addr_sk_lookup_udp
,
5344 .ret_type
= RET_PTR_TO_SOCKET_OR_NULL
,
5345 .arg1_type
= ARG_PTR_TO_CTX
,
5346 .arg2_type
= ARG_PTR_TO_MEM
,
5347 .arg3_type
= ARG_CONST_SIZE
,
5348 .arg4_type
= ARG_ANYTHING
,
5349 .arg5_type
= ARG_ANYTHING
,
5352 bool bpf_tcp_sock_is_valid_access(int off
, int size
, enum bpf_access_type type
,
5353 struct bpf_insn_access_aux
*info
)
5355 if (off
< 0 || off
>= offsetofend(struct bpf_tcp_sock
, bytes_acked
))
5358 if (off
% size
!= 0)
5362 case offsetof(struct bpf_tcp_sock
, bytes_received
):
5363 case offsetof(struct bpf_tcp_sock
, bytes_acked
):
5364 return size
== sizeof(__u64
);
5366 return size
== sizeof(__u32
);
5370 u32
bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type
,
5371 const struct bpf_insn
*si
,
5372 struct bpf_insn
*insn_buf
,
5373 struct bpf_prog
*prog
, u32
*target_size
)
5375 struct bpf_insn
*insn
= insn_buf
;
5377 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
5379 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD) > \
5380 FIELD_SIZEOF(struct bpf_tcp_sock, FIELD)); \
5381 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
5382 si->dst_reg, si->src_reg, \
5383 offsetof(struct tcp_sock, FIELD)); \
5386 CONVERT_COMMON_TCP_SOCK_FIELDS(struct bpf_tcp_sock
,
5387 BPF_TCP_SOCK_GET_COMMON
);
5389 if (insn
> insn_buf
)
5390 return insn
- insn_buf
;
5393 case offsetof(struct bpf_tcp_sock
, rtt_min
):
5394 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock
, rtt_min
) !=
5395 sizeof(struct minmax
));
5396 BUILD_BUG_ON(sizeof(struct minmax
) <
5397 sizeof(struct minmax_sample
));
5399 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
5400 offsetof(struct tcp_sock
, rtt_min
) +
5401 offsetof(struct minmax_sample
, v
));
5405 return insn
- insn_buf
;
5408 BPF_CALL_1(bpf_tcp_sock
, struct sock
*, sk
)
5410 sk
= sk_to_full_sk(sk
);
5412 if (sk_fullsock(sk
) && sk
->sk_protocol
== IPPROTO_TCP
)
5413 return (unsigned long)sk
;
5415 return (unsigned long)NULL
;
5418 static const struct bpf_func_proto bpf_tcp_sock_proto
= {
5419 .func
= bpf_tcp_sock
,
5421 .ret_type
= RET_PTR_TO_TCP_SOCK_OR_NULL
,
5422 .arg1_type
= ARG_PTR_TO_SOCK_COMMON
,
5425 BPF_CALL_1(bpf_skb_ecn_set_ce
, struct sk_buff
*, skb
)
5427 unsigned int iphdr_len
;
5429 if (skb
->protocol
== cpu_to_be16(ETH_P_IP
))
5430 iphdr_len
= sizeof(struct iphdr
);
5431 else if (skb
->protocol
== cpu_to_be16(ETH_P_IPV6
))
5432 iphdr_len
= sizeof(struct ipv6hdr
);
5436 if (skb_headlen(skb
) < iphdr_len
)
5439 if (skb_cloned(skb
) && !skb_clone_writable(skb
, iphdr_len
))
5442 return INET_ECN_set_ce(skb
);
5445 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto
= {
5446 .func
= bpf_skb_ecn_set_ce
,
5448 .ret_type
= RET_INTEGER
,
5449 .arg1_type
= ARG_PTR_TO_CTX
,
5451 #endif /* CONFIG_INET */
5453 bool bpf_helper_changes_pkt_data(void *func
)
5455 if (func
== bpf_skb_vlan_push
||
5456 func
== bpf_skb_vlan_pop
||
5457 func
== bpf_skb_store_bytes
||
5458 func
== bpf_skb_change_proto
||
5459 func
== bpf_skb_change_head
||
5460 func
== sk_skb_change_head
||
5461 func
== bpf_skb_change_tail
||
5462 func
== sk_skb_change_tail
||
5463 func
== bpf_skb_adjust_room
||
5464 func
== bpf_skb_pull_data
||
5465 func
== sk_skb_pull_data
||
5466 func
== bpf_clone_redirect
||
5467 func
== bpf_l3_csum_replace
||
5468 func
== bpf_l4_csum_replace
||
5469 func
== bpf_xdp_adjust_head
||
5470 func
== bpf_xdp_adjust_meta
||
5471 func
== bpf_msg_pull_data
||
5472 func
== bpf_msg_push_data
||
5473 func
== bpf_msg_pop_data
||
5474 func
== bpf_xdp_adjust_tail
||
5475 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5476 func
== bpf_lwt_seg6_store_bytes
||
5477 func
== bpf_lwt_seg6_adjust_srh
||
5478 func
== bpf_lwt_seg6_action
||
5480 func
== bpf_lwt_in_push_encap
||
5481 func
== bpf_lwt_xmit_push_encap
)
5487 static const struct bpf_func_proto
*
5488 bpf_base_func_proto(enum bpf_func_id func_id
)
5491 case BPF_FUNC_map_lookup_elem
:
5492 return &bpf_map_lookup_elem_proto
;
5493 case BPF_FUNC_map_update_elem
:
5494 return &bpf_map_update_elem_proto
;
5495 case BPF_FUNC_map_delete_elem
:
5496 return &bpf_map_delete_elem_proto
;
5497 case BPF_FUNC_map_push_elem
:
5498 return &bpf_map_push_elem_proto
;
5499 case BPF_FUNC_map_pop_elem
:
5500 return &bpf_map_pop_elem_proto
;
5501 case BPF_FUNC_map_peek_elem
:
5502 return &bpf_map_peek_elem_proto
;
5503 case BPF_FUNC_get_prandom_u32
:
5504 return &bpf_get_prandom_u32_proto
;
5505 case BPF_FUNC_get_smp_processor_id
:
5506 return &bpf_get_raw_smp_processor_id_proto
;
5507 case BPF_FUNC_get_numa_node_id
:
5508 return &bpf_get_numa_node_id_proto
;
5509 case BPF_FUNC_tail_call
:
5510 return &bpf_tail_call_proto
;
5511 case BPF_FUNC_ktime_get_ns
:
5512 return &bpf_ktime_get_ns_proto
;
5517 if (!capable(CAP_SYS_ADMIN
))
5521 case BPF_FUNC_spin_lock
:
5522 return &bpf_spin_lock_proto
;
5523 case BPF_FUNC_spin_unlock
:
5524 return &bpf_spin_unlock_proto
;
5525 case BPF_FUNC_trace_printk
:
5526 return bpf_get_trace_printk_proto();
5532 static const struct bpf_func_proto
*
5533 sock_filter_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5536 /* inet and inet6 sockets are created in a process
5537 * context so there is always a valid uid/gid
5539 case BPF_FUNC_get_current_uid_gid
:
5540 return &bpf_get_current_uid_gid_proto
;
5541 case BPF_FUNC_get_local_storage
:
5542 return &bpf_get_local_storage_proto
;
5544 return bpf_base_func_proto(func_id
);
5548 static const struct bpf_func_proto
*
5549 sock_addr_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5552 /* inet and inet6 sockets are created in a process
5553 * context so there is always a valid uid/gid
5555 case BPF_FUNC_get_current_uid_gid
:
5556 return &bpf_get_current_uid_gid_proto
;
5558 switch (prog
->expected_attach_type
) {
5559 case BPF_CGROUP_INET4_CONNECT
:
5560 case BPF_CGROUP_INET6_CONNECT
:
5561 return &bpf_bind_proto
;
5565 case BPF_FUNC_get_socket_cookie
:
5566 return &bpf_get_socket_cookie_sock_addr_proto
;
5567 case BPF_FUNC_get_local_storage
:
5568 return &bpf_get_local_storage_proto
;
5570 case BPF_FUNC_sk_lookup_tcp
:
5571 return &bpf_sock_addr_sk_lookup_tcp_proto
;
5572 case BPF_FUNC_sk_lookup_udp
:
5573 return &bpf_sock_addr_sk_lookup_udp_proto
;
5574 case BPF_FUNC_sk_release
:
5575 return &bpf_sk_release_proto
;
5576 #endif /* CONFIG_INET */
5578 return bpf_base_func_proto(func_id
);
5582 static const struct bpf_func_proto
*
5583 sk_filter_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5586 case BPF_FUNC_skb_load_bytes
:
5587 return &bpf_skb_load_bytes_proto
;
5588 case BPF_FUNC_skb_load_bytes_relative
:
5589 return &bpf_skb_load_bytes_relative_proto
;
5590 case BPF_FUNC_get_socket_cookie
:
5591 return &bpf_get_socket_cookie_proto
;
5592 case BPF_FUNC_get_socket_uid
:
5593 return &bpf_get_socket_uid_proto
;
5595 return bpf_base_func_proto(func_id
);
5599 static const struct bpf_func_proto
*
5600 cg_skb_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5603 case BPF_FUNC_get_local_storage
:
5604 return &bpf_get_local_storage_proto
;
5605 case BPF_FUNC_sk_fullsock
:
5606 return &bpf_sk_fullsock_proto
;
5608 case BPF_FUNC_tcp_sock
:
5609 return &bpf_tcp_sock_proto
;
5610 case BPF_FUNC_skb_ecn_set_ce
:
5611 return &bpf_skb_ecn_set_ce_proto
;
5614 return sk_filter_func_proto(func_id
, prog
);
5618 static const struct bpf_func_proto
*
5619 tc_cls_act_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5622 case BPF_FUNC_skb_store_bytes
:
5623 return &bpf_skb_store_bytes_proto
;
5624 case BPF_FUNC_skb_load_bytes
:
5625 return &bpf_skb_load_bytes_proto
;
5626 case BPF_FUNC_skb_load_bytes_relative
:
5627 return &bpf_skb_load_bytes_relative_proto
;
5628 case BPF_FUNC_skb_pull_data
:
5629 return &bpf_skb_pull_data_proto
;
5630 case BPF_FUNC_csum_diff
:
5631 return &bpf_csum_diff_proto
;
5632 case BPF_FUNC_csum_update
:
5633 return &bpf_csum_update_proto
;
5634 case BPF_FUNC_l3_csum_replace
:
5635 return &bpf_l3_csum_replace_proto
;
5636 case BPF_FUNC_l4_csum_replace
:
5637 return &bpf_l4_csum_replace_proto
;
5638 case BPF_FUNC_clone_redirect
:
5639 return &bpf_clone_redirect_proto
;
5640 case BPF_FUNC_get_cgroup_classid
:
5641 return &bpf_get_cgroup_classid_proto
;
5642 case BPF_FUNC_skb_vlan_push
:
5643 return &bpf_skb_vlan_push_proto
;
5644 case BPF_FUNC_skb_vlan_pop
:
5645 return &bpf_skb_vlan_pop_proto
;
5646 case BPF_FUNC_skb_change_proto
:
5647 return &bpf_skb_change_proto_proto
;
5648 case BPF_FUNC_skb_change_type
:
5649 return &bpf_skb_change_type_proto
;
5650 case BPF_FUNC_skb_adjust_room
:
5651 return &bpf_skb_adjust_room_proto
;
5652 case BPF_FUNC_skb_change_tail
:
5653 return &bpf_skb_change_tail_proto
;
5654 case BPF_FUNC_skb_get_tunnel_key
:
5655 return &bpf_skb_get_tunnel_key_proto
;
5656 case BPF_FUNC_skb_set_tunnel_key
:
5657 return bpf_get_skb_set_tunnel_proto(func_id
);
5658 case BPF_FUNC_skb_get_tunnel_opt
:
5659 return &bpf_skb_get_tunnel_opt_proto
;
5660 case BPF_FUNC_skb_set_tunnel_opt
:
5661 return bpf_get_skb_set_tunnel_proto(func_id
);
5662 case BPF_FUNC_redirect
:
5663 return &bpf_redirect_proto
;
5664 case BPF_FUNC_get_route_realm
:
5665 return &bpf_get_route_realm_proto
;
5666 case BPF_FUNC_get_hash_recalc
:
5667 return &bpf_get_hash_recalc_proto
;
5668 case BPF_FUNC_set_hash_invalid
:
5669 return &bpf_set_hash_invalid_proto
;
5670 case BPF_FUNC_set_hash
:
5671 return &bpf_set_hash_proto
;
5672 case BPF_FUNC_perf_event_output
:
5673 return &bpf_skb_event_output_proto
;
5674 case BPF_FUNC_get_smp_processor_id
:
5675 return &bpf_get_smp_processor_id_proto
;
5676 case BPF_FUNC_skb_under_cgroup
:
5677 return &bpf_skb_under_cgroup_proto
;
5678 case BPF_FUNC_get_socket_cookie
:
5679 return &bpf_get_socket_cookie_proto
;
5680 case BPF_FUNC_get_socket_uid
:
5681 return &bpf_get_socket_uid_proto
;
5682 case BPF_FUNC_fib_lookup
:
5683 return &bpf_skb_fib_lookup_proto
;
5684 case BPF_FUNC_sk_fullsock
:
5685 return &bpf_sk_fullsock_proto
;
5687 case BPF_FUNC_skb_get_xfrm_state
:
5688 return &bpf_skb_get_xfrm_state_proto
;
5690 #ifdef CONFIG_SOCK_CGROUP_DATA
5691 case BPF_FUNC_skb_cgroup_id
:
5692 return &bpf_skb_cgroup_id_proto
;
5693 case BPF_FUNC_skb_ancestor_cgroup_id
:
5694 return &bpf_skb_ancestor_cgroup_id_proto
;
5697 case BPF_FUNC_sk_lookup_tcp
:
5698 return &bpf_sk_lookup_tcp_proto
;
5699 case BPF_FUNC_sk_lookup_udp
:
5700 return &bpf_sk_lookup_udp_proto
;
5701 case BPF_FUNC_sk_release
:
5702 return &bpf_sk_release_proto
;
5703 case BPF_FUNC_tcp_sock
:
5704 return &bpf_tcp_sock_proto
;
5707 return bpf_base_func_proto(func_id
);
5711 static const struct bpf_func_proto
*
5712 xdp_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5715 case BPF_FUNC_perf_event_output
:
5716 return &bpf_xdp_event_output_proto
;
5717 case BPF_FUNC_get_smp_processor_id
:
5718 return &bpf_get_smp_processor_id_proto
;
5719 case BPF_FUNC_csum_diff
:
5720 return &bpf_csum_diff_proto
;
5721 case BPF_FUNC_xdp_adjust_head
:
5722 return &bpf_xdp_adjust_head_proto
;
5723 case BPF_FUNC_xdp_adjust_meta
:
5724 return &bpf_xdp_adjust_meta_proto
;
5725 case BPF_FUNC_redirect
:
5726 return &bpf_xdp_redirect_proto
;
5727 case BPF_FUNC_redirect_map
:
5728 return &bpf_xdp_redirect_map_proto
;
5729 case BPF_FUNC_xdp_adjust_tail
:
5730 return &bpf_xdp_adjust_tail_proto
;
5731 case BPF_FUNC_fib_lookup
:
5732 return &bpf_xdp_fib_lookup_proto
;
5734 case BPF_FUNC_sk_lookup_udp
:
5735 return &bpf_xdp_sk_lookup_udp_proto
;
5736 case BPF_FUNC_sk_lookup_tcp
:
5737 return &bpf_xdp_sk_lookup_tcp_proto
;
5738 case BPF_FUNC_sk_release
:
5739 return &bpf_sk_release_proto
;
5742 return bpf_base_func_proto(func_id
);
5746 const struct bpf_func_proto bpf_sock_map_update_proto __weak
;
5747 const struct bpf_func_proto bpf_sock_hash_update_proto __weak
;
5749 static const struct bpf_func_proto
*
5750 sock_ops_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5753 case BPF_FUNC_setsockopt
:
5754 return &bpf_setsockopt_proto
;
5755 case BPF_FUNC_getsockopt
:
5756 return &bpf_getsockopt_proto
;
5757 case BPF_FUNC_sock_ops_cb_flags_set
:
5758 return &bpf_sock_ops_cb_flags_set_proto
;
5759 case BPF_FUNC_sock_map_update
:
5760 return &bpf_sock_map_update_proto
;
5761 case BPF_FUNC_sock_hash_update
:
5762 return &bpf_sock_hash_update_proto
;
5763 case BPF_FUNC_get_socket_cookie
:
5764 return &bpf_get_socket_cookie_sock_ops_proto
;
5765 case BPF_FUNC_get_local_storage
:
5766 return &bpf_get_local_storage_proto
;
5767 case BPF_FUNC_perf_event_output
:
5768 return &bpf_sockopt_event_output_proto
;
5770 return bpf_base_func_proto(func_id
);
5774 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak
;
5775 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak
;
5777 static const struct bpf_func_proto
*
5778 sk_msg_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5781 case BPF_FUNC_msg_redirect_map
:
5782 return &bpf_msg_redirect_map_proto
;
5783 case BPF_FUNC_msg_redirect_hash
:
5784 return &bpf_msg_redirect_hash_proto
;
5785 case BPF_FUNC_msg_apply_bytes
:
5786 return &bpf_msg_apply_bytes_proto
;
5787 case BPF_FUNC_msg_cork_bytes
:
5788 return &bpf_msg_cork_bytes_proto
;
5789 case BPF_FUNC_msg_pull_data
:
5790 return &bpf_msg_pull_data_proto
;
5791 case BPF_FUNC_msg_push_data
:
5792 return &bpf_msg_push_data_proto
;
5793 case BPF_FUNC_msg_pop_data
:
5794 return &bpf_msg_pop_data_proto
;
5796 return bpf_base_func_proto(func_id
);
5800 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak
;
5801 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak
;
5803 static const struct bpf_func_proto
*
5804 sk_skb_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5807 case BPF_FUNC_skb_store_bytes
:
5808 return &bpf_skb_store_bytes_proto
;
5809 case BPF_FUNC_skb_load_bytes
:
5810 return &bpf_skb_load_bytes_proto
;
5811 case BPF_FUNC_skb_pull_data
:
5812 return &sk_skb_pull_data_proto
;
5813 case BPF_FUNC_skb_change_tail
:
5814 return &sk_skb_change_tail_proto
;
5815 case BPF_FUNC_skb_change_head
:
5816 return &sk_skb_change_head_proto
;
5817 case BPF_FUNC_get_socket_cookie
:
5818 return &bpf_get_socket_cookie_proto
;
5819 case BPF_FUNC_get_socket_uid
:
5820 return &bpf_get_socket_uid_proto
;
5821 case BPF_FUNC_sk_redirect_map
:
5822 return &bpf_sk_redirect_map_proto
;
5823 case BPF_FUNC_sk_redirect_hash
:
5824 return &bpf_sk_redirect_hash_proto
;
5826 case BPF_FUNC_sk_lookup_tcp
:
5827 return &bpf_sk_lookup_tcp_proto
;
5828 case BPF_FUNC_sk_lookup_udp
:
5829 return &bpf_sk_lookup_udp_proto
;
5830 case BPF_FUNC_sk_release
:
5831 return &bpf_sk_release_proto
;
5834 return bpf_base_func_proto(func_id
);
5838 static const struct bpf_func_proto
*
5839 flow_dissector_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5842 case BPF_FUNC_skb_load_bytes
:
5843 return &bpf_skb_load_bytes_proto
;
5845 return bpf_base_func_proto(func_id
);
5849 static const struct bpf_func_proto
*
5850 lwt_out_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5853 case BPF_FUNC_skb_load_bytes
:
5854 return &bpf_skb_load_bytes_proto
;
5855 case BPF_FUNC_skb_pull_data
:
5856 return &bpf_skb_pull_data_proto
;
5857 case BPF_FUNC_csum_diff
:
5858 return &bpf_csum_diff_proto
;
5859 case BPF_FUNC_get_cgroup_classid
:
5860 return &bpf_get_cgroup_classid_proto
;
5861 case BPF_FUNC_get_route_realm
:
5862 return &bpf_get_route_realm_proto
;
5863 case BPF_FUNC_get_hash_recalc
:
5864 return &bpf_get_hash_recalc_proto
;
5865 case BPF_FUNC_perf_event_output
:
5866 return &bpf_skb_event_output_proto
;
5867 case BPF_FUNC_get_smp_processor_id
:
5868 return &bpf_get_smp_processor_id_proto
;
5869 case BPF_FUNC_skb_under_cgroup
:
5870 return &bpf_skb_under_cgroup_proto
;
5872 return bpf_base_func_proto(func_id
);
5876 static const struct bpf_func_proto
*
5877 lwt_in_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5880 case BPF_FUNC_lwt_push_encap
:
5881 return &bpf_lwt_in_push_encap_proto
;
5883 return lwt_out_func_proto(func_id
, prog
);
5887 static const struct bpf_func_proto
*
5888 lwt_xmit_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5891 case BPF_FUNC_skb_get_tunnel_key
:
5892 return &bpf_skb_get_tunnel_key_proto
;
5893 case BPF_FUNC_skb_set_tunnel_key
:
5894 return bpf_get_skb_set_tunnel_proto(func_id
);
5895 case BPF_FUNC_skb_get_tunnel_opt
:
5896 return &bpf_skb_get_tunnel_opt_proto
;
5897 case BPF_FUNC_skb_set_tunnel_opt
:
5898 return bpf_get_skb_set_tunnel_proto(func_id
);
5899 case BPF_FUNC_redirect
:
5900 return &bpf_redirect_proto
;
5901 case BPF_FUNC_clone_redirect
:
5902 return &bpf_clone_redirect_proto
;
5903 case BPF_FUNC_skb_change_tail
:
5904 return &bpf_skb_change_tail_proto
;
5905 case BPF_FUNC_skb_change_head
:
5906 return &bpf_skb_change_head_proto
;
5907 case BPF_FUNC_skb_store_bytes
:
5908 return &bpf_skb_store_bytes_proto
;
5909 case BPF_FUNC_csum_update
:
5910 return &bpf_csum_update_proto
;
5911 case BPF_FUNC_l3_csum_replace
:
5912 return &bpf_l3_csum_replace_proto
;
5913 case BPF_FUNC_l4_csum_replace
:
5914 return &bpf_l4_csum_replace_proto
;
5915 case BPF_FUNC_set_hash_invalid
:
5916 return &bpf_set_hash_invalid_proto
;
5917 case BPF_FUNC_lwt_push_encap
:
5918 return &bpf_lwt_xmit_push_encap_proto
;
5920 return lwt_out_func_proto(func_id
, prog
);
5924 static const struct bpf_func_proto
*
5925 lwt_seg6local_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
5928 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5929 case BPF_FUNC_lwt_seg6_store_bytes
:
5930 return &bpf_lwt_seg6_store_bytes_proto
;
5931 case BPF_FUNC_lwt_seg6_action
:
5932 return &bpf_lwt_seg6_action_proto
;
5933 case BPF_FUNC_lwt_seg6_adjust_srh
:
5934 return &bpf_lwt_seg6_adjust_srh_proto
;
5937 return lwt_out_func_proto(func_id
, prog
);
5941 static bool bpf_skb_is_valid_access(int off
, int size
, enum bpf_access_type type
,
5942 const struct bpf_prog
*prog
,
5943 struct bpf_insn_access_aux
*info
)
5945 const int size_default
= sizeof(__u32
);
5947 if (off
< 0 || off
>= sizeof(struct __sk_buff
))
5950 /* The verifier guarantees that size > 0. */
5951 if (off
% size
!= 0)
5955 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
5956 if (off
+ size
> offsetofend(struct __sk_buff
, cb
[4]))
5959 case bpf_ctx_range_till(struct __sk_buff
, remote_ip6
[0], remote_ip6
[3]):
5960 case bpf_ctx_range_till(struct __sk_buff
, local_ip6
[0], local_ip6
[3]):
5961 case bpf_ctx_range_till(struct __sk_buff
, remote_ip4
, remote_ip4
):
5962 case bpf_ctx_range_till(struct __sk_buff
, local_ip4
, local_ip4
):
5963 case bpf_ctx_range(struct __sk_buff
, data
):
5964 case bpf_ctx_range(struct __sk_buff
, data_meta
):
5965 case bpf_ctx_range(struct __sk_buff
, data_end
):
5966 if (size
!= size_default
)
5969 case bpf_ctx_range_ptr(struct __sk_buff
, flow_keys
):
5970 if (size
!= sizeof(__u64
))
5973 case bpf_ctx_range(struct __sk_buff
, tstamp
):
5974 if (size
!= sizeof(__u64
))
5977 case offsetof(struct __sk_buff
, sk
):
5978 if (type
== BPF_WRITE
|| size
!= sizeof(__u64
))
5980 info
->reg_type
= PTR_TO_SOCK_COMMON_OR_NULL
;
5983 /* Only narrow read access allowed for now. */
5984 if (type
== BPF_WRITE
) {
5985 if (size
!= size_default
)
5988 bpf_ctx_record_field_size(info
, size_default
);
5989 if (!bpf_ctx_narrow_access_ok(off
, size
, size_default
))
5997 static bool sk_filter_is_valid_access(int off
, int size
,
5998 enum bpf_access_type type
,
5999 const struct bpf_prog
*prog
,
6000 struct bpf_insn_access_aux
*info
)
6003 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6004 case bpf_ctx_range(struct __sk_buff
, data
):
6005 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6006 case bpf_ctx_range(struct __sk_buff
, data_end
):
6007 case bpf_ctx_range_ptr(struct __sk_buff
, flow_keys
):
6008 case bpf_ctx_range_till(struct __sk_buff
, family
, local_port
):
6009 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6010 case bpf_ctx_range(struct __sk_buff
, wire_len
):
6014 if (type
== BPF_WRITE
) {
6016 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6023 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6026 static bool cg_skb_is_valid_access(int off
, int size
,
6027 enum bpf_access_type type
,
6028 const struct bpf_prog
*prog
,
6029 struct bpf_insn_access_aux
*info
)
6032 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6033 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6034 case bpf_ctx_range_ptr(struct __sk_buff
, flow_keys
):
6035 case bpf_ctx_range(struct __sk_buff
, wire_len
):
6037 case bpf_ctx_range(struct __sk_buff
, data
):
6038 case bpf_ctx_range(struct __sk_buff
, data_end
):
6039 if (!capable(CAP_SYS_ADMIN
))
6044 if (type
== BPF_WRITE
) {
6046 case bpf_ctx_range(struct __sk_buff
, mark
):
6047 case bpf_ctx_range(struct __sk_buff
, priority
):
6048 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6050 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6051 if (!capable(CAP_SYS_ADMIN
))
6060 case bpf_ctx_range(struct __sk_buff
, data
):
6061 info
->reg_type
= PTR_TO_PACKET
;
6063 case bpf_ctx_range(struct __sk_buff
, data_end
):
6064 info
->reg_type
= PTR_TO_PACKET_END
;
6068 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6071 static bool lwt_is_valid_access(int off
, int size
,
6072 enum bpf_access_type type
,
6073 const struct bpf_prog
*prog
,
6074 struct bpf_insn_access_aux
*info
)
6077 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6078 case bpf_ctx_range_till(struct __sk_buff
, family
, local_port
):
6079 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6080 case bpf_ctx_range_ptr(struct __sk_buff
, flow_keys
):
6081 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6082 case bpf_ctx_range(struct __sk_buff
, wire_len
):
6086 if (type
== BPF_WRITE
) {
6088 case bpf_ctx_range(struct __sk_buff
, mark
):
6089 case bpf_ctx_range(struct __sk_buff
, priority
):
6090 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6098 case bpf_ctx_range(struct __sk_buff
, data
):
6099 info
->reg_type
= PTR_TO_PACKET
;
6101 case bpf_ctx_range(struct __sk_buff
, data_end
):
6102 info
->reg_type
= PTR_TO_PACKET_END
;
6106 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6109 /* Attach type specific accesses */
6110 static bool __sock_filter_check_attach_type(int off
,
6111 enum bpf_access_type access_type
,
6112 enum bpf_attach_type attach_type
)
6115 case offsetof(struct bpf_sock
, bound_dev_if
):
6116 case offsetof(struct bpf_sock
, mark
):
6117 case offsetof(struct bpf_sock
, priority
):
6118 switch (attach_type
) {
6119 case BPF_CGROUP_INET_SOCK_CREATE
:
6124 case bpf_ctx_range(struct bpf_sock
, src_ip4
):
6125 switch (attach_type
) {
6126 case BPF_CGROUP_INET4_POST_BIND
:
6131 case bpf_ctx_range_till(struct bpf_sock
, src_ip6
[0], src_ip6
[3]):
6132 switch (attach_type
) {
6133 case BPF_CGROUP_INET6_POST_BIND
:
6138 case bpf_ctx_range(struct bpf_sock
, src_port
):
6139 switch (attach_type
) {
6140 case BPF_CGROUP_INET4_POST_BIND
:
6141 case BPF_CGROUP_INET6_POST_BIND
:
6148 return access_type
== BPF_READ
;
6153 bool bpf_sock_common_is_valid_access(int off
, int size
,
6154 enum bpf_access_type type
,
6155 struct bpf_insn_access_aux
*info
)
6158 case bpf_ctx_range_till(struct bpf_sock
, type
, priority
):
6161 return bpf_sock_is_valid_access(off
, size
, type
, info
);
6165 bool bpf_sock_is_valid_access(int off
, int size
, enum bpf_access_type type
,
6166 struct bpf_insn_access_aux
*info
)
6168 const int size_default
= sizeof(__u32
);
6170 if (off
< 0 || off
>= sizeof(struct bpf_sock
))
6172 if (off
% size
!= 0)
6176 case offsetof(struct bpf_sock
, state
):
6177 case offsetof(struct bpf_sock
, family
):
6178 case offsetof(struct bpf_sock
, type
):
6179 case offsetof(struct bpf_sock
, protocol
):
6180 case offsetof(struct bpf_sock
, dst_port
):
6181 case offsetof(struct bpf_sock
, src_port
):
6182 case bpf_ctx_range(struct bpf_sock
, src_ip4
):
6183 case bpf_ctx_range_till(struct bpf_sock
, src_ip6
[0], src_ip6
[3]):
6184 case bpf_ctx_range(struct bpf_sock
, dst_ip4
):
6185 case bpf_ctx_range_till(struct bpf_sock
, dst_ip6
[0], dst_ip6
[3]):
6186 bpf_ctx_record_field_size(info
, size_default
);
6187 return bpf_ctx_narrow_access_ok(off
, size
, size_default
);
6190 return size
== size_default
;
6193 static bool sock_filter_is_valid_access(int off
, int size
,
6194 enum bpf_access_type type
,
6195 const struct bpf_prog
*prog
,
6196 struct bpf_insn_access_aux
*info
)
6198 if (!bpf_sock_is_valid_access(off
, size
, type
, info
))
6200 return __sock_filter_check_attach_type(off
, type
,
6201 prog
->expected_attach_type
);
6204 static int bpf_noop_prologue(struct bpf_insn
*insn_buf
, bool direct_write
,
6205 const struct bpf_prog
*prog
)
6207 /* Neither direct read nor direct write requires any preliminary
6213 static int bpf_unclone_prologue(struct bpf_insn
*insn_buf
, bool direct_write
,
6214 const struct bpf_prog
*prog
, int drop_verdict
)
6216 struct bpf_insn
*insn
= insn_buf
;
6221 /* if (!skb->cloned)
6224 * (Fast-path, otherwise approximation that we might be
6225 * a clone, do the rest in helper.)
6227 *insn
++ = BPF_LDX_MEM(BPF_B
, BPF_REG_6
, BPF_REG_1
, CLONED_OFFSET());
6228 *insn
++ = BPF_ALU32_IMM(BPF_AND
, BPF_REG_6
, CLONED_MASK
);
6229 *insn
++ = BPF_JMP_IMM(BPF_JEQ
, BPF_REG_6
, 0, 7);
6231 /* ret = bpf_skb_pull_data(skb, 0); */
6232 *insn
++ = BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
);
6233 *insn
++ = BPF_ALU64_REG(BPF_XOR
, BPF_REG_2
, BPF_REG_2
);
6234 *insn
++ = BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0,
6235 BPF_FUNC_skb_pull_data
);
6238 * return TC_ACT_SHOT;
6240 *insn
++ = BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 2);
6241 *insn
++ = BPF_ALU32_IMM(BPF_MOV
, BPF_REG_0
, drop_verdict
);
6242 *insn
++ = BPF_EXIT_INSN();
6245 *insn
++ = BPF_MOV64_REG(BPF_REG_1
, BPF_REG_6
);
6247 *insn
++ = prog
->insnsi
[0];
6249 return insn
- insn_buf
;
6252 static int bpf_gen_ld_abs(const struct bpf_insn
*orig
,
6253 struct bpf_insn
*insn_buf
)
6255 bool indirect
= BPF_MODE(orig
->code
) == BPF_IND
;
6256 struct bpf_insn
*insn
= insn_buf
;
6258 /* We're guaranteed here that CTX is in R6. */
6259 *insn
++ = BPF_MOV64_REG(BPF_REG_1
, BPF_REG_CTX
);
6261 *insn
++ = BPF_MOV64_IMM(BPF_REG_2
, orig
->imm
);
6263 *insn
++ = BPF_MOV64_REG(BPF_REG_2
, orig
->src_reg
);
6265 *insn
++ = BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, orig
->imm
);
6268 switch (BPF_SIZE(orig
->code
)) {
6270 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache
);
6273 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache
);
6276 *insn
++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache
);
6280 *insn
++ = BPF_JMP_IMM(BPF_JSGE
, BPF_REG_0
, 0, 2);
6281 *insn
++ = BPF_ALU32_REG(BPF_XOR
, BPF_REG_0
, BPF_REG_0
);
6282 *insn
++ = BPF_EXIT_INSN();
6284 return insn
- insn_buf
;
6287 static int tc_cls_act_prologue(struct bpf_insn
*insn_buf
, bool direct_write
,
6288 const struct bpf_prog
*prog
)
6290 return bpf_unclone_prologue(insn_buf
, direct_write
, prog
, TC_ACT_SHOT
);
6293 static bool tc_cls_act_is_valid_access(int off
, int size
,
6294 enum bpf_access_type type
,
6295 const struct bpf_prog
*prog
,
6296 struct bpf_insn_access_aux
*info
)
6298 if (type
== BPF_WRITE
) {
6300 case bpf_ctx_range(struct __sk_buff
, mark
):
6301 case bpf_ctx_range(struct __sk_buff
, tc_index
):
6302 case bpf_ctx_range(struct __sk_buff
, priority
):
6303 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6304 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6305 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6306 case bpf_ctx_range(struct __sk_buff
, queue_mapping
):
6314 case bpf_ctx_range(struct __sk_buff
, data
):
6315 info
->reg_type
= PTR_TO_PACKET
;
6317 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6318 info
->reg_type
= PTR_TO_PACKET_META
;
6320 case bpf_ctx_range(struct __sk_buff
, data_end
):
6321 info
->reg_type
= PTR_TO_PACKET_END
;
6323 case bpf_ctx_range_ptr(struct __sk_buff
, flow_keys
):
6324 case bpf_ctx_range_till(struct __sk_buff
, family
, local_port
):
6328 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6331 static bool __is_valid_xdp_access(int off
, int size
)
6333 if (off
< 0 || off
>= sizeof(struct xdp_md
))
6335 if (off
% size
!= 0)
6337 if (size
!= sizeof(__u32
))
6343 static bool xdp_is_valid_access(int off
, int size
,
6344 enum bpf_access_type type
,
6345 const struct bpf_prog
*prog
,
6346 struct bpf_insn_access_aux
*info
)
6348 if (type
== BPF_WRITE
) {
6349 if (bpf_prog_is_dev_bound(prog
->aux
)) {
6351 case offsetof(struct xdp_md
, rx_queue_index
):
6352 return __is_valid_xdp_access(off
, size
);
6359 case offsetof(struct xdp_md
, data
):
6360 info
->reg_type
= PTR_TO_PACKET
;
6362 case offsetof(struct xdp_md
, data_meta
):
6363 info
->reg_type
= PTR_TO_PACKET_META
;
6365 case offsetof(struct xdp_md
, data_end
):
6366 info
->reg_type
= PTR_TO_PACKET_END
;
6370 return __is_valid_xdp_access(off
, size
);
6373 void bpf_warn_invalid_xdp_action(u32 act
)
6375 const u32 act_max
= XDP_REDIRECT
;
6377 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
6378 act
> act_max
? "Illegal" : "Driver unsupported",
6381 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action
);
6383 static bool sock_addr_is_valid_access(int off
, int size
,
6384 enum bpf_access_type type
,
6385 const struct bpf_prog
*prog
,
6386 struct bpf_insn_access_aux
*info
)
6388 const int size_default
= sizeof(__u32
);
6390 if (off
< 0 || off
>= sizeof(struct bpf_sock_addr
))
6392 if (off
% size
!= 0)
6395 /* Disallow access to IPv6 fields from IPv4 contex and vise
6399 case bpf_ctx_range(struct bpf_sock_addr
, user_ip4
):
6400 switch (prog
->expected_attach_type
) {
6401 case BPF_CGROUP_INET4_BIND
:
6402 case BPF_CGROUP_INET4_CONNECT
:
6403 case BPF_CGROUP_UDP4_SENDMSG
:
6409 case bpf_ctx_range_till(struct bpf_sock_addr
, user_ip6
[0], user_ip6
[3]):
6410 switch (prog
->expected_attach_type
) {
6411 case BPF_CGROUP_INET6_BIND
:
6412 case BPF_CGROUP_INET6_CONNECT
:
6413 case BPF_CGROUP_UDP6_SENDMSG
:
6419 case bpf_ctx_range(struct bpf_sock_addr
, msg_src_ip4
):
6420 switch (prog
->expected_attach_type
) {
6421 case BPF_CGROUP_UDP4_SENDMSG
:
6427 case bpf_ctx_range_till(struct bpf_sock_addr
, msg_src_ip6
[0],
6429 switch (prog
->expected_attach_type
) {
6430 case BPF_CGROUP_UDP6_SENDMSG
:
6439 case bpf_ctx_range(struct bpf_sock_addr
, user_ip4
):
6440 case bpf_ctx_range_till(struct bpf_sock_addr
, user_ip6
[0], user_ip6
[3]):
6441 case bpf_ctx_range(struct bpf_sock_addr
, msg_src_ip4
):
6442 case bpf_ctx_range_till(struct bpf_sock_addr
, msg_src_ip6
[0],
6444 /* Only narrow read access allowed for now. */
6445 if (type
== BPF_READ
) {
6446 bpf_ctx_record_field_size(info
, size_default
);
6447 if (!bpf_ctx_narrow_access_ok(off
, size
, size_default
))
6450 if (size
!= size_default
)
6454 case bpf_ctx_range(struct bpf_sock_addr
, user_port
):
6455 if (size
!= size_default
)
6459 if (type
== BPF_READ
) {
6460 if (size
!= size_default
)
6470 static bool sock_ops_is_valid_access(int off
, int size
,
6471 enum bpf_access_type type
,
6472 const struct bpf_prog
*prog
,
6473 struct bpf_insn_access_aux
*info
)
6475 const int size_default
= sizeof(__u32
);
6477 if (off
< 0 || off
>= sizeof(struct bpf_sock_ops
))
6480 /* The verifier guarantees that size > 0. */
6481 if (off
% size
!= 0)
6484 if (type
== BPF_WRITE
) {
6486 case offsetof(struct bpf_sock_ops
, reply
):
6487 case offsetof(struct bpf_sock_ops
, sk_txhash
):
6488 if (size
!= size_default
)
6496 case bpf_ctx_range_till(struct bpf_sock_ops
, bytes_received
,
6498 if (size
!= sizeof(__u64
))
6502 if (size
!= size_default
)
6511 static int sk_skb_prologue(struct bpf_insn
*insn_buf
, bool direct_write
,
6512 const struct bpf_prog
*prog
)
6514 return bpf_unclone_prologue(insn_buf
, direct_write
, prog
, SK_DROP
);
6517 static bool sk_skb_is_valid_access(int off
, int size
,
6518 enum bpf_access_type type
,
6519 const struct bpf_prog
*prog
,
6520 struct bpf_insn_access_aux
*info
)
6523 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6524 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6525 case bpf_ctx_range_ptr(struct __sk_buff
, flow_keys
):
6526 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6527 case bpf_ctx_range(struct __sk_buff
, wire_len
):
6531 if (type
== BPF_WRITE
) {
6533 case bpf_ctx_range(struct __sk_buff
, tc_index
):
6534 case bpf_ctx_range(struct __sk_buff
, priority
):
6542 case bpf_ctx_range(struct __sk_buff
, mark
):
6544 case bpf_ctx_range(struct __sk_buff
, data
):
6545 info
->reg_type
= PTR_TO_PACKET
;
6547 case bpf_ctx_range(struct __sk_buff
, data_end
):
6548 info
->reg_type
= PTR_TO_PACKET_END
;
6552 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6555 static bool sk_msg_is_valid_access(int off
, int size
,
6556 enum bpf_access_type type
,
6557 const struct bpf_prog
*prog
,
6558 struct bpf_insn_access_aux
*info
)
6560 if (type
== BPF_WRITE
)
6563 if (off
% size
!= 0)
6567 case offsetof(struct sk_msg_md
, data
):
6568 info
->reg_type
= PTR_TO_PACKET
;
6569 if (size
!= sizeof(__u64
))
6572 case offsetof(struct sk_msg_md
, data_end
):
6573 info
->reg_type
= PTR_TO_PACKET_END
;
6574 if (size
!= sizeof(__u64
))
6577 case bpf_ctx_range(struct sk_msg_md
, family
):
6578 case bpf_ctx_range(struct sk_msg_md
, remote_ip4
):
6579 case bpf_ctx_range(struct sk_msg_md
, local_ip4
):
6580 case bpf_ctx_range_till(struct sk_msg_md
, remote_ip6
[0], remote_ip6
[3]):
6581 case bpf_ctx_range_till(struct sk_msg_md
, local_ip6
[0], local_ip6
[3]):
6582 case bpf_ctx_range(struct sk_msg_md
, remote_port
):
6583 case bpf_ctx_range(struct sk_msg_md
, local_port
):
6584 case bpf_ctx_range(struct sk_msg_md
, size
):
6585 if (size
!= sizeof(__u32
))
6594 static bool flow_dissector_is_valid_access(int off
, int size
,
6595 enum bpf_access_type type
,
6596 const struct bpf_prog
*prog
,
6597 struct bpf_insn_access_aux
*info
)
6599 if (type
== BPF_WRITE
) {
6601 case bpf_ctx_range_till(struct __sk_buff
, cb
[0], cb
[4]):
6609 case bpf_ctx_range(struct __sk_buff
, data
):
6610 info
->reg_type
= PTR_TO_PACKET
;
6612 case bpf_ctx_range(struct __sk_buff
, data_end
):
6613 info
->reg_type
= PTR_TO_PACKET_END
;
6615 case bpf_ctx_range_ptr(struct __sk_buff
, flow_keys
):
6616 info
->reg_type
= PTR_TO_FLOW_KEYS
;
6618 case bpf_ctx_range(struct __sk_buff
, tc_classid
):
6619 case bpf_ctx_range(struct __sk_buff
, data_meta
):
6620 case bpf_ctx_range_till(struct __sk_buff
, family
, local_port
):
6621 case bpf_ctx_range(struct __sk_buff
, tstamp
):
6622 case bpf_ctx_range(struct __sk_buff
, wire_len
):
6626 return bpf_skb_is_valid_access(off
, size
, type
, prog
, info
);
6629 static u32
bpf_convert_ctx_access(enum bpf_access_type type
,
6630 const struct bpf_insn
*si
,
6631 struct bpf_insn
*insn_buf
,
6632 struct bpf_prog
*prog
, u32
*target_size
)
6634 struct bpf_insn
*insn
= insn_buf
;
6638 case offsetof(struct __sk_buff
, len
):
6639 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
6640 bpf_target_off(struct sk_buff
, len
, 4,
6644 case offsetof(struct __sk_buff
, protocol
):
6645 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
6646 bpf_target_off(struct sk_buff
, protocol
, 2,
6650 case offsetof(struct __sk_buff
, vlan_proto
):
6651 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
6652 bpf_target_off(struct sk_buff
, vlan_proto
, 2,
6656 case offsetof(struct __sk_buff
, priority
):
6657 if (type
== BPF_WRITE
)
6658 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
6659 bpf_target_off(struct sk_buff
, priority
, 4,
6662 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
6663 bpf_target_off(struct sk_buff
, priority
, 4,
6667 case offsetof(struct __sk_buff
, ingress_ifindex
):
6668 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
6669 bpf_target_off(struct sk_buff
, skb_iif
, 4,
6673 case offsetof(struct __sk_buff
, ifindex
):
6674 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, dev
),
6675 si
->dst_reg
, si
->src_reg
,
6676 offsetof(struct sk_buff
, dev
));
6677 *insn
++ = BPF_JMP_IMM(BPF_JEQ
, si
->dst_reg
, 0, 1);
6678 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
6679 bpf_target_off(struct net_device
, ifindex
, 4,
6683 case offsetof(struct __sk_buff
, hash
):
6684 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
6685 bpf_target_off(struct sk_buff
, hash
, 4,
6689 case offsetof(struct __sk_buff
, mark
):
6690 if (type
== BPF_WRITE
)
6691 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
6692 bpf_target_off(struct sk_buff
, mark
, 4,
6695 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
6696 bpf_target_off(struct sk_buff
, mark
, 4,
6700 case offsetof(struct __sk_buff
, pkt_type
):
6702 *insn
++ = BPF_LDX_MEM(BPF_B
, si
->dst_reg
, si
->src_reg
,
6704 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, PKT_TYPE_MAX
);
6705 #ifdef __BIG_ENDIAN_BITFIELD
6706 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, 5);
6710 case offsetof(struct __sk_buff
, queue_mapping
):
6711 if (type
== BPF_WRITE
) {
6712 *insn
++ = BPF_JMP_IMM(BPF_JGE
, si
->src_reg
, NO_QUEUE_MAPPING
, 1);
6713 *insn
++ = BPF_STX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
6714 bpf_target_off(struct sk_buff
,
6718 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
6719 bpf_target_off(struct sk_buff
,
6725 case offsetof(struct __sk_buff
, vlan_present
):
6727 *insn
++ = BPF_LDX_MEM(BPF_B
, si
->dst_reg
, si
->src_reg
,
6728 PKT_VLAN_PRESENT_OFFSET());
6729 if (PKT_VLAN_PRESENT_BIT
)
6730 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, PKT_VLAN_PRESENT_BIT
);
6731 if (PKT_VLAN_PRESENT_BIT
< 7)
6732 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, 1);
6735 case offsetof(struct __sk_buff
, vlan_tci
):
6736 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
6737 bpf_target_off(struct sk_buff
, vlan_tci
, 2,
6741 case offsetof(struct __sk_buff
, cb
[0]) ...
6742 offsetofend(struct __sk_buff
, cb
[4]) - 1:
6743 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb
, data
) < 20);
6744 BUILD_BUG_ON((offsetof(struct sk_buff
, cb
) +
6745 offsetof(struct qdisc_skb_cb
, data
)) %
6748 prog
->cb_access
= 1;
6750 off
-= offsetof(struct __sk_buff
, cb
[0]);
6751 off
+= offsetof(struct sk_buff
, cb
);
6752 off
+= offsetof(struct qdisc_skb_cb
, data
);
6753 if (type
== BPF_WRITE
)
6754 *insn
++ = BPF_STX_MEM(BPF_SIZE(si
->code
), si
->dst_reg
,
6757 *insn
++ = BPF_LDX_MEM(BPF_SIZE(si
->code
), si
->dst_reg
,
6761 case offsetof(struct __sk_buff
, tc_classid
):
6762 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb
, tc_classid
) != 2);
6765 off
-= offsetof(struct __sk_buff
, tc_classid
);
6766 off
+= offsetof(struct sk_buff
, cb
);
6767 off
+= offsetof(struct qdisc_skb_cb
, tc_classid
);
6769 if (type
== BPF_WRITE
)
6770 *insn
++ = BPF_STX_MEM(BPF_H
, si
->dst_reg
,
6773 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
,
6777 case offsetof(struct __sk_buff
, data
):
6778 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, data
),
6779 si
->dst_reg
, si
->src_reg
,
6780 offsetof(struct sk_buff
, data
));
6783 case offsetof(struct __sk_buff
, data_meta
):
6785 off
-= offsetof(struct __sk_buff
, data_meta
);
6786 off
+= offsetof(struct sk_buff
, cb
);
6787 off
+= offsetof(struct bpf_skb_data_end
, data_meta
);
6788 *insn
++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si
->dst_reg
,
6792 case offsetof(struct __sk_buff
, data_end
):
6794 off
-= offsetof(struct __sk_buff
, data_end
);
6795 off
+= offsetof(struct sk_buff
, cb
);
6796 off
+= offsetof(struct bpf_skb_data_end
, data_end
);
6797 *insn
++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si
->dst_reg
,
6801 case offsetof(struct __sk_buff
, tc_index
):
6802 #ifdef CONFIG_NET_SCHED
6803 if (type
== BPF_WRITE
)
6804 *insn
++ = BPF_STX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
6805 bpf_target_off(struct sk_buff
, tc_index
, 2,
6808 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->src_reg
,
6809 bpf_target_off(struct sk_buff
, tc_index
, 2,
6813 if (type
== BPF_WRITE
)
6814 *insn
++ = BPF_MOV64_REG(si
->dst_reg
, si
->dst_reg
);
6816 *insn
++ = BPF_MOV64_IMM(si
->dst_reg
, 0);
6820 case offsetof(struct __sk_buff
, napi_id
):
6821 #if defined(CONFIG_NET_RX_BUSY_POLL)
6822 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
6823 bpf_target_off(struct sk_buff
, napi_id
, 4,
6825 *insn
++ = BPF_JMP_IMM(BPF_JGE
, si
->dst_reg
, MIN_NAPI_ID
, 1);
6826 *insn
++ = BPF_MOV64_IMM(si
->dst_reg
, 0);
6829 *insn
++ = BPF_MOV64_IMM(si
->dst_reg
, 0);
6832 case offsetof(struct __sk_buff
, family
):
6833 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_family
) != 2);
6835 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
6836 si
->dst_reg
, si
->src_reg
,
6837 offsetof(struct sk_buff
, sk
));
6838 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
6839 bpf_target_off(struct sock_common
,
6843 case offsetof(struct __sk_buff
, remote_ip4
):
6844 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_daddr
) != 4);
6846 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
6847 si
->dst_reg
, si
->src_reg
,
6848 offsetof(struct sk_buff
, sk
));
6849 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
6850 bpf_target_off(struct sock_common
,
6854 case offsetof(struct __sk_buff
, local_ip4
):
6855 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
6856 skc_rcv_saddr
) != 4);
6858 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
6859 si
->dst_reg
, si
->src_reg
,
6860 offsetof(struct sk_buff
, sk
));
6861 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
6862 bpf_target_off(struct sock_common
,
6866 case offsetof(struct __sk_buff
, remote_ip6
[0]) ...
6867 offsetof(struct __sk_buff
, remote_ip6
[3]):
6868 #if IS_ENABLED(CONFIG_IPV6)
6869 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
6870 skc_v6_daddr
.s6_addr32
[0]) != 4);
6873 off
-= offsetof(struct __sk_buff
, remote_ip6
[0]);
6875 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
6876 si
->dst_reg
, si
->src_reg
,
6877 offsetof(struct sk_buff
, sk
));
6878 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
6879 offsetof(struct sock_common
,
6880 skc_v6_daddr
.s6_addr32
[0]) +
6883 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
6886 case offsetof(struct __sk_buff
, local_ip6
[0]) ...
6887 offsetof(struct __sk_buff
, local_ip6
[3]):
6888 #if IS_ENABLED(CONFIG_IPV6)
6889 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
6890 skc_v6_rcv_saddr
.s6_addr32
[0]) != 4);
6893 off
-= offsetof(struct __sk_buff
, local_ip6
[0]);
6895 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
6896 si
->dst_reg
, si
->src_reg
,
6897 offsetof(struct sk_buff
, sk
));
6898 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
6899 offsetof(struct sock_common
,
6900 skc_v6_rcv_saddr
.s6_addr32
[0]) +
6903 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
6907 case offsetof(struct __sk_buff
, remote_port
):
6908 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_dport
) != 2);
6910 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
6911 si
->dst_reg
, si
->src_reg
,
6912 offsetof(struct sk_buff
, sk
));
6913 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
6914 bpf_target_off(struct sock_common
,
6917 #ifndef __BIG_ENDIAN_BITFIELD
6918 *insn
++ = BPF_ALU32_IMM(BPF_LSH
, si
->dst_reg
, 16);
6922 case offsetof(struct __sk_buff
, local_port
):
6923 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_num
) != 2);
6925 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
6926 si
->dst_reg
, si
->src_reg
,
6927 offsetof(struct sk_buff
, sk
));
6928 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
6929 bpf_target_off(struct sock_common
,
6930 skc_num
, 2, target_size
));
6933 case offsetof(struct __sk_buff
, flow_keys
):
6935 off
-= offsetof(struct __sk_buff
, flow_keys
);
6936 off
+= offsetof(struct sk_buff
, cb
);
6937 off
+= offsetof(struct qdisc_skb_cb
, flow_keys
);
6938 *insn
++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si
->dst_reg
,
6942 case offsetof(struct __sk_buff
, tstamp
):
6943 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff
, tstamp
) != 8);
6945 if (type
== BPF_WRITE
)
6946 *insn
++ = BPF_STX_MEM(BPF_DW
,
6947 si
->dst_reg
, si
->src_reg
,
6948 bpf_target_off(struct sk_buff
,
6952 *insn
++ = BPF_LDX_MEM(BPF_DW
,
6953 si
->dst_reg
, si
->src_reg
,
6954 bpf_target_off(struct sk_buff
,
6959 case offsetof(struct __sk_buff
, gso_segs
):
6960 /* si->dst_reg = skb_shinfo(SKB); */
6961 #ifdef NET_SKBUFF_DATA_USES_OFFSET
6962 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, head
),
6963 si
->dst_reg
, si
->src_reg
,
6964 offsetof(struct sk_buff
, head
));
6965 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, end
),
6966 BPF_REG_AX
, si
->src_reg
,
6967 offsetof(struct sk_buff
, end
));
6968 *insn
++ = BPF_ALU64_REG(BPF_ADD
, si
->dst_reg
, BPF_REG_AX
);
6970 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, end
),
6971 si
->dst_reg
, si
->src_reg
,
6972 offsetof(struct sk_buff
, end
));
6974 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info
, gso_segs
),
6975 si
->dst_reg
, si
->dst_reg
,
6976 bpf_target_off(struct skb_shared_info
,
6980 case offsetof(struct __sk_buff
, wire_len
):
6981 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb
, pkt_len
) != 4);
6984 off
-= offsetof(struct __sk_buff
, wire_len
);
6985 off
+= offsetof(struct sk_buff
, cb
);
6986 off
+= offsetof(struct qdisc_skb_cb
, pkt_len
);
6988 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
, off
);
6991 case offsetof(struct __sk_buff
, sk
):
6992 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, sk
),
6993 si
->dst_reg
, si
->src_reg
,
6994 offsetof(struct sk_buff
, sk
));
6998 return insn
- insn_buf
;
7001 u32
bpf_sock_convert_ctx_access(enum bpf_access_type type
,
7002 const struct bpf_insn
*si
,
7003 struct bpf_insn
*insn_buf
,
7004 struct bpf_prog
*prog
, u32
*target_size
)
7006 struct bpf_insn
*insn
= insn_buf
;
7010 case offsetof(struct bpf_sock
, bound_dev_if
):
7011 BUILD_BUG_ON(FIELD_SIZEOF(struct sock
, sk_bound_dev_if
) != 4);
7013 if (type
== BPF_WRITE
)
7014 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7015 offsetof(struct sock
, sk_bound_dev_if
));
7017 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7018 offsetof(struct sock
, sk_bound_dev_if
));
7021 case offsetof(struct bpf_sock
, mark
):
7022 BUILD_BUG_ON(FIELD_SIZEOF(struct sock
, sk_mark
) != 4);
7024 if (type
== BPF_WRITE
)
7025 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7026 offsetof(struct sock
, sk_mark
));
7028 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7029 offsetof(struct sock
, sk_mark
));
7032 case offsetof(struct bpf_sock
, priority
):
7033 BUILD_BUG_ON(FIELD_SIZEOF(struct sock
, sk_priority
) != 4);
7035 if (type
== BPF_WRITE
)
7036 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7037 offsetof(struct sock
, sk_priority
));
7039 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7040 offsetof(struct sock
, sk_priority
));
7043 case offsetof(struct bpf_sock
, family
):
7044 *insn
++ = BPF_LDX_MEM(
7045 BPF_FIELD_SIZEOF(struct sock_common
, skc_family
),
7046 si
->dst_reg
, si
->src_reg
,
7047 bpf_target_off(struct sock_common
,
7049 FIELD_SIZEOF(struct sock_common
,
7054 case offsetof(struct bpf_sock
, type
):
7055 BUILD_BUG_ON(HWEIGHT32(SK_FL_TYPE_MASK
) != BITS_PER_BYTE
* 2);
7056 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7057 offsetof(struct sock
, __sk_flags_offset
));
7058 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_TYPE_MASK
);
7059 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, SK_FL_TYPE_SHIFT
);
7063 case offsetof(struct bpf_sock
, protocol
):
7064 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK
) != BITS_PER_BYTE
);
7065 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7066 offsetof(struct sock
, __sk_flags_offset
));
7067 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_PROTO_MASK
);
7068 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, SK_FL_PROTO_SHIFT
);
7072 case offsetof(struct bpf_sock
, src_ip4
):
7073 *insn
++ = BPF_LDX_MEM(
7074 BPF_SIZE(si
->code
), si
->dst_reg
, si
->src_reg
,
7075 bpf_target_off(struct sock_common
, skc_rcv_saddr
,
7076 FIELD_SIZEOF(struct sock_common
,
7081 case offsetof(struct bpf_sock
, dst_ip4
):
7082 *insn
++ = BPF_LDX_MEM(
7083 BPF_SIZE(si
->code
), si
->dst_reg
, si
->src_reg
,
7084 bpf_target_off(struct sock_common
, skc_daddr
,
7085 FIELD_SIZEOF(struct sock_common
,
7090 case bpf_ctx_range_till(struct bpf_sock
, src_ip6
[0], src_ip6
[3]):
7091 #if IS_ENABLED(CONFIG_IPV6)
7093 off
-= offsetof(struct bpf_sock
, src_ip6
[0]);
7094 *insn
++ = BPF_LDX_MEM(
7095 BPF_SIZE(si
->code
), si
->dst_reg
, si
->src_reg
,
7098 skc_v6_rcv_saddr
.s6_addr32
[0],
7099 FIELD_SIZEOF(struct sock_common
,
7100 skc_v6_rcv_saddr
.s6_addr32
[0]),
7101 target_size
) + off
);
7104 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7108 case bpf_ctx_range_till(struct bpf_sock
, dst_ip6
[0], dst_ip6
[3]):
7109 #if IS_ENABLED(CONFIG_IPV6)
7111 off
-= offsetof(struct bpf_sock
, dst_ip6
[0]);
7112 *insn
++ = BPF_LDX_MEM(
7113 BPF_SIZE(si
->code
), si
->dst_reg
, si
->src_reg
,
7114 bpf_target_off(struct sock_common
,
7115 skc_v6_daddr
.s6_addr32
[0],
7116 FIELD_SIZEOF(struct sock_common
,
7117 skc_v6_daddr
.s6_addr32
[0]),
7118 target_size
) + off
);
7120 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7125 case offsetof(struct bpf_sock
, src_port
):
7126 *insn
++ = BPF_LDX_MEM(
7127 BPF_FIELD_SIZEOF(struct sock_common
, skc_num
),
7128 si
->dst_reg
, si
->src_reg
,
7129 bpf_target_off(struct sock_common
, skc_num
,
7130 FIELD_SIZEOF(struct sock_common
,
7135 case offsetof(struct bpf_sock
, dst_port
):
7136 *insn
++ = BPF_LDX_MEM(
7137 BPF_FIELD_SIZEOF(struct sock_common
, skc_dport
),
7138 si
->dst_reg
, si
->src_reg
,
7139 bpf_target_off(struct sock_common
, skc_dport
,
7140 FIELD_SIZEOF(struct sock_common
,
7145 case offsetof(struct bpf_sock
, state
):
7146 *insn
++ = BPF_LDX_MEM(
7147 BPF_FIELD_SIZEOF(struct sock_common
, skc_state
),
7148 si
->dst_reg
, si
->src_reg
,
7149 bpf_target_off(struct sock_common
, skc_state
,
7150 FIELD_SIZEOF(struct sock_common
,
7156 return insn
- insn_buf
;
7159 static u32
tc_cls_act_convert_ctx_access(enum bpf_access_type type
,
7160 const struct bpf_insn
*si
,
7161 struct bpf_insn
*insn_buf
,
7162 struct bpf_prog
*prog
, u32
*target_size
)
7164 struct bpf_insn
*insn
= insn_buf
;
7167 case offsetof(struct __sk_buff
, ifindex
):
7168 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff
, dev
),
7169 si
->dst_reg
, si
->src_reg
,
7170 offsetof(struct sk_buff
, dev
));
7171 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7172 bpf_target_off(struct net_device
, ifindex
, 4,
7176 return bpf_convert_ctx_access(type
, si
, insn_buf
, prog
,
7180 return insn
- insn_buf
;
7183 static u32
xdp_convert_ctx_access(enum bpf_access_type type
,
7184 const struct bpf_insn
*si
,
7185 struct bpf_insn
*insn_buf
,
7186 struct bpf_prog
*prog
, u32
*target_size
)
7188 struct bpf_insn
*insn
= insn_buf
;
7191 case offsetof(struct xdp_md
, data
):
7192 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, data
),
7193 si
->dst_reg
, si
->src_reg
,
7194 offsetof(struct xdp_buff
, data
));
7196 case offsetof(struct xdp_md
, data_meta
):
7197 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, data_meta
),
7198 si
->dst_reg
, si
->src_reg
,
7199 offsetof(struct xdp_buff
, data_meta
));
7201 case offsetof(struct xdp_md
, data_end
):
7202 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, data_end
),
7203 si
->dst_reg
, si
->src_reg
,
7204 offsetof(struct xdp_buff
, data_end
));
7206 case offsetof(struct xdp_md
, ingress_ifindex
):
7207 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, rxq
),
7208 si
->dst_reg
, si
->src_reg
,
7209 offsetof(struct xdp_buff
, rxq
));
7210 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info
, dev
),
7211 si
->dst_reg
, si
->dst_reg
,
7212 offsetof(struct xdp_rxq_info
, dev
));
7213 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7214 offsetof(struct net_device
, ifindex
));
7216 case offsetof(struct xdp_md
, rx_queue_index
):
7217 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff
, rxq
),
7218 si
->dst_reg
, si
->src_reg
,
7219 offsetof(struct xdp_buff
, rxq
));
7220 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7221 offsetof(struct xdp_rxq_info
,
7226 return insn
- insn_buf
;
7229 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
7230 * context Structure, F is Field in context structure that contains a pointer
7231 * to Nested Structure of type NS that has the field NF.
7233 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
7234 * sure that SIZE is not greater than actual size of S.F.NF.
7236 * If offset OFF is provided, the load happens from that offset relative to
7239 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
7241 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
7242 si->src_reg, offsetof(S, F)); \
7243 *insn++ = BPF_LDX_MEM( \
7244 SIZE, si->dst_reg, si->dst_reg, \
7245 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7250 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
7251 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
7252 BPF_FIELD_SIZEOF(NS, NF), 0)
7254 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
7255 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
7257 * It doesn't support SIZE argument though since narrow stores are not
7258 * supported for now.
7260 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
7261 * "register" since two registers available in convert_ctx_access are not
7262 * enough: we can't override neither SRC, since it contains value to store, nor
7263 * DST since it contains pointer to context that may be used by later
7264 * instructions. But we need a temporary place to save pointer to nested
7265 * structure whose field we want to store to.
7267 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
7269 int tmp_reg = BPF_REG_9; \
7270 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7272 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7274 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
7276 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
7277 si->dst_reg, offsetof(S, F)); \
7278 *insn++ = BPF_STX_MEM( \
7279 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
7280 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7283 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
7287 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
7290 if (type == BPF_WRITE) { \
7291 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
7294 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
7295 S, NS, F, NF, SIZE, OFF); \
7299 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
7300 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
7301 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
7303 static u32
sock_addr_convert_ctx_access(enum bpf_access_type type
,
7304 const struct bpf_insn
*si
,
7305 struct bpf_insn
*insn_buf
,
7306 struct bpf_prog
*prog
, u32
*target_size
)
7308 struct bpf_insn
*insn
= insn_buf
;
7312 case offsetof(struct bpf_sock_addr
, user_family
):
7313 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern
,
7314 struct sockaddr
, uaddr
, sa_family
);
7317 case offsetof(struct bpf_sock_addr
, user_ip4
):
7318 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7319 struct bpf_sock_addr_kern
, struct sockaddr_in
, uaddr
,
7320 sin_addr
, BPF_SIZE(si
->code
), 0, tmp_reg
);
7323 case bpf_ctx_range_till(struct bpf_sock_addr
, user_ip6
[0], user_ip6
[3]):
7325 off
-= offsetof(struct bpf_sock_addr
, user_ip6
[0]);
7326 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7327 struct bpf_sock_addr_kern
, struct sockaddr_in6
, uaddr
,
7328 sin6_addr
.s6_addr32
[0], BPF_SIZE(si
->code
), off
,
7332 case offsetof(struct bpf_sock_addr
, user_port
):
7333 /* To get port we need to know sa_family first and then treat
7334 * sockaddr as either sockaddr_in or sockaddr_in6.
7335 * Though we can simplify since port field has same offset and
7336 * size in both structures.
7337 * Here we check this invariant and use just one of the
7338 * structures if it's true.
7340 BUILD_BUG_ON(offsetof(struct sockaddr_in
, sin_port
) !=
7341 offsetof(struct sockaddr_in6
, sin6_port
));
7342 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in
, sin_port
) !=
7343 FIELD_SIZEOF(struct sockaddr_in6
, sin6_port
));
7344 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern
,
7345 struct sockaddr_in6
, uaddr
,
7346 sin6_port
, tmp_reg
);
7349 case offsetof(struct bpf_sock_addr
, family
):
7350 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern
,
7351 struct sock
, sk
, sk_family
);
7354 case offsetof(struct bpf_sock_addr
, type
):
7355 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7356 struct bpf_sock_addr_kern
, struct sock
, sk
,
7357 __sk_flags_offset
, BPF_W
, 0);
7358 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_TYPE_MASK
);
7359 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
, SK_FL_TYPE_SHIFT
);
7362 case offsetof(struct bpf_sock_addr
, protocol
):
7363 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7364 struct bpf_sock_addr_kern
, struct sock
, sk
,
7365 __sk_flags_offset
, BPF_W
, 0);
7366 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_PROTO_MASK
);
7367 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
,
7371 case offsetof(struct bpf_sock_addr
, msg_src_ip4
):
7372 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
7373 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7374 struct bpf_sock_addr_kern
, struct in_addr
, t_ctx
,
7375 s_addr
, BPF_SIZE(si
->code
), 0, tmp_reg
);
7378 case bpf_ctx_range_till(struct bpf_sock_addr
, msg_src_ip6
[0],
7381 off
-= offsetof(struct bpf_sock_addr
, msg_src_ip6
[0]);
7382 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
7383 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7384 struct bpf_sock_addr_kern
, struct in6_addr
, t_ctx
,
7385 s6_addr32
[0], BPF_SIZE(si
->code
), off
, tmp_reg
);
7389 return insn
- insn_buf
;
7392 static u32
sock_ops_convert_ctx_access(enum bpf_access_type type
,
7393 const struct bpf_insn
*si
,
7394 struct bpf_insn
*insn_buf
,
7395 struct bpf_prog
*prog
,
7398 struct bpf_insn
*insn
= insn_buf
;
7401 /* Helper macro for adding read access to tcp_sock or sock fields. */
7402 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7404 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7405 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7406 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7407 struct bpf_sock_ops_kern, \
7409 si->dst_reg, si->src_reg, \
7410 offsetof(struct bpf_sock_ops_kern, \
7412 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
7413 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7414 struct bpf_sock_ops_kern, sk),\
7415 si->dst_reg, si->src_reg, \
7416 offsetof(struct bpf_sock_ops_kern, sk));\
7417 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
7419 si->dst_reg, si->dst_reg, \
7420 offsetof(OBJ, OBJ_FIELD)); \
7423 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
7424 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
7426 /* Helper macro for adding write access to tcp_sock or sock fields.
7427 * The macro is called with two registers, dst_reg which contains a pointer
7428 * to ctx (context) and src_reg which contains the value that should be
7429 * stored. However, we need an additional register since we cannot overwrite
7430 * dst_reg because it may be used later in the program.
7431 * Instead we "borrow" one of the other register. We first save its value
7432 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
7433 * it at the end of the macro.
7435 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7437 int reg = BPF_REG_9; \
7438 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7439 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7440 if (si->dst_reg == reg || si->src_reg == reg) \
7442 if (si->dst_reg == reg || si->src_reg == reg) \
7444 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
7445 offsetof(struct bpf_sock_ops_kern, \
7447 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7448 struct bpf_sock_ops_kern, \
7451 offsetof(struct bpf_sock_ops_kern, \
7453 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
7454 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7455 struct bpf_sock_ops_kern, sk),\
7457 offsetof(struct bpf_sock_ops_kern, sk));\
7458 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
7460 offsetof(OBJ, OBJ_FIELD)); \
7461 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
7462 offsetof(struct bpf_sock_ops_kern, \
7466 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
7468 if (TYPE == BPF_WRITE) \
7469 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7471 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7474 CONVERT_COMMON_TCP_SOCK_FIELDS(struct bpf_sock_ops
,
7475 SOCK_OPS_GET_TCP_SOCK_FIELD
);
7477 if (insn
> insn_buf
)
7478 return insn
- insn_buf
;
7481 case offsetof(struct bpf_sock_ops
, op
) ...
7482 offsetof(struct bpf_sock_ops
, replylong
[3]):
7483 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops
, op
) !=
7484 FIELD_SIZEOF(struct bpf_sock_ops_kern
, op
));
7485 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops
, reply
) !=
7486 FIELD_SIZEOF(struct bpf_sock_ops_kern
, reply
));
7487 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops
, replylong
) !=
7488 FIELD_SIZEOF(struct bpf_sock_ops_kern
, replylong
));
7490 off
-= offsetof(struct bpf_sock_ops
, op
);
7491 off
+= offsetof(struct bpf_sock_ops_kern
, op
);
7492 if (type
== BPF_WRITE
)
7493 *insn
++ = BPF_STX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7496 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->src_reg
,
7500 case offsetof(struct bpf_sock_ops
, family
):
7501 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_family
) != 2);
7503 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7504 struct bpf_sock_ops_kern
, sk
),
7505 si
->dst_reg
, si
->src_reg
,
7506 offsetof(struct bpf_sock_ops_kern
, sk
));
7507 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
7508 offsetof(struct sock_common
, skc_family
));
7511 case offsetof(struct bpf_sock_ops
, remote_ip4
):
7512 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_daddr
) != 4);
7514 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7515 struct bpf_sock_ops_kern
, sk
),
7516 si
->dst_reg
, si
->src_reg
,
7517 offsetof(struct bpf_sock_ops_kern
, sk
));
7518 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7519 offsetof(struct sock_common
, skc_daddr
));
7522 case offsetof(struct bpf_sock_ops
, local_ip4
):
7523 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
7524 skc_rcv_saddr
) != 4);
7526 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7527 struct bpf_sock_ops_kern
, sk
),
7528 si
->dst_reg
, si
->src_reg
,
7529 offsetof(struct bpf_sock_ops_kern
, sk
));
7530 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7531 offsetof(struct sock_common
,
7535 case offsetof(struct bpf_sock_ops
, remote_ip6
[0]) ...
7536 offsetof(struct bpf_sock_ops
, remote_ip6
[3]):
7537 #if IS_ENABLED(CONFIG_IPV6)
7538 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
7539 skc_v6_daddr
.s6_addr32
[0]) != 4);
7542 off
-= offsetof(struct bpf_sock_ops
, remote_ip6
[0]);
7543 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7544 struct bpf_sock_ops_kern
, sk
),
7545 si
->dst_reg
, si
->src_reg
,
7546 offsetof(struct bpf_sock_ops_kern
, sk
));
7547 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7548 offsetof(struct sock_common
,
7549 skc_v6_daddr
.s6_addr32
[0]) +
7552 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7556 case offsetof(struct bpf_sock_ops
, local_ip6
[0]) ...
7557 offsetof(struct bpf_sock_ops
, local_ip6
[3]):
7558 #if IS_ENABLED(CONFIG_IPV6)
7559 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
7560 skc_v6_rcv_saddr
.s6_addr32
[0]) != 4);
7563 off
-= offsetof(struct bpf_sock_ops
, local_ip6
[0]);
7564 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7565 struct bpf_sock_ops_kern
, sk
),
7566 si
->dst_reg
, si
->src_reg
,
7567 offsetof(struct bpf_sock_ops_kern
, sk
));
7568 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7569 offsetof(struct sock_common
,
7570 skc_v6_rcv_saddr
.s6_addr32
[0]) +
7573 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7577 case offsetof(struct bpf_sock_ops
, remote_port
):
7578 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_dport
) != 2);
7580 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7581 struct bpf_sock_ops_kern
, sk
),
7582 si
->dst_reg
, si
->src_reg
,
7583 offsetof(struct bpf_sock_ops_kern
, sk
));
7584 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
7585 offsetof(struct sock_common
, skc_dport
));
7586 #ifndef __BIG_ENDIAN_BITFIELD
7587 *insn
++ = BPF_ALU32_IMM(BPF_LSH
, si
->dst_reg
, 16);
7591 case offsetof(struct bpf_sock_ops
, local_port
):
7592 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_num
) != 2);
7594 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7595 struct bpf_sock_ops_kern
, sk
),
7596 si
->dst_reg
, si
->src_reg
,
7597 offsetof(struct bpf_sock_ops_kern
, sk
));
7598 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
7599 offsetof(struct sock_common
, skc_num
));
7602 case offsetof(struct bpf_sock_ops
, is_fullsock
):
7603 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7604 struct bpf_sock_ops_kern
,
7606 si
->dst_reg
, si
->src_reg
,
7607 offsetof(struct bpf_sock_ops_kern
,
7611 case offsetof(struct bpf_sock_ops
, state
):
7612 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_state
) != 1);
7614 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7615 struct bpf_sock_ops_kern
, sk
),
7616 si
->dst_reg
, si
->src_reg
,
7617 offsetof(struct bpf_sock_ops_kern
, sk
));
7618 *insn
++ = BPF_LDX_MEM(BPF_B
, si
->dst_reg
, si
->dst_reg
,
7619 offsetof(struct sock_common
, skc_state
));
7622 case offsetof(struct bpf_sock_ops
, rtt_min
):
7623 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock
, rtt_min
) !=
7624 sizeof(struct minmax
));
7625 BUILD_BUG_ON(sizeof(struct minmax
) <
7626 sizeof(struct minmax_sample
));
7628 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7629 struct bpf_sock_ops_kern
, sk
),
7630 si
->dst_reg
, si
->src_reg
,
7631 offsetof(struct bpf_sock_ops_kern
, sk
));
7632 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7633 offsetof(struct tcp_sock
, rtt_min
) +
7634 FIELD_SIZEOF(struct minmax_sample
, t
));
7637 case offsetof(struct bpf_sock_ops
, bpf_sock_ops_cb_flags
):
7638 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags
, bpf_sock_ops_cb_flags
,
7642 case offsetof(struct bpf_sock_ops
, sk_txhash
):
7643 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash
, sk_txhash
,
7647 return insn
- insn_buf
;
7650 static u32
sk_skb_convert_ctx_access(enum bpf_access_type type
,
7651 const struct bpf_insn
*si
,
7652 struct bpf_insn
*insn_buf
,
7653 struct bpf_prog
*prog
, u32
*target_size
)
7655 struct bpf_insn
*insn
= insn_buf
;
7659 case offsetof(struct __sk_buff
, data_end
):
7661 off
-= offsetof(struct __sk_buff
, data_end
);
7662 off
+= offsetof(struct sk_buff
, cb
);
7663 off
+= offsetof(struct tcp_skb_cb
, bpf
.data_end
);
7664 *insn
++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si
->dst_reg
,
7668 return bpf_convert_ctx_access(type
, si
, insn_buf
, prog
,
7672 return insn
- insn_buf
;
7675 static u32
sk_msg_convert_ctx_access(enum bpf_access_type type
,
7676 const struct bpf_insn
*si
,
7677 struct bpf_insn
*insn_buf
,
7678 struct bpf_prog
*prog
, u32
*target_size
)
7680 struct bpf_insn
*insn
= insn_buf
;
7681 #if IS_ENABLED(CONFIG_IPV6)
7685 /* convert ctx uses the fact sg element is first in struct */
7686 BUILD_BUG_ON(offsetof(struct sk_msg
, sg
) != 0);
7689 case offsetof(struct sk_msg_md
, data
):
7690 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg
, data
),
7691 si
->dst_reg
, si
->src_reg
,
7692 offsetof(struct sk_msg
, data
));
7694 case offsetof(struct sk_msg_md
, data_end
):
7695 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg
, data_end
),
7696 si
->dst_reg
, si
->src_reg
,
7697 offsetof(struct sk_msg
, data_end
));
7699 case offsetof(struct sk_msg_md
, family
):
7700 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_family
) != 2);
7702 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7704 si
->dst_reg
, si
->src_reg
,
7705 offsetof(struct sk_msg
, sk
));
7706 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
7707 offsetof(struct sock_common
, skc_family
));
7710 case offsetof(struct sk_msg_md
, remote_ip4
):
7711 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_daddr
) != 4);
7713 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7715 si
->dst_reg
, si
->src_reg
,
7716 offsetof(struct sk_msg
, sk
));
7717 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7718 offsetof(struct sock_common
, skc_daddr
));
7721 case offsetof(struct sk_msg_md
, local_ip4
):
7722 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
7723 skc_rcv_saddr
) != 4);
7725 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7727 si
->dst_reg
, si
->src_reg
,
7728 offsetof(struct sk_msg
, sk
));
7729 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7730 offsetof(struct sock_common
,
7734 case offsetof(struct sk_msg_md
, remote_ip6
[0]) ...
7735 offsetof(struct sk_msg_md
, remote_ip6
[3]):
7736 #if IS_ENABLED(CONFIG_IPV6)
7737 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
7738 skc_v6_daddr
.s6_addr32
[0]) != 4);
7741 off
-= offsetof(struct sk_msg_md
, remote_ip6
[0]);
7742 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7744 si
->dst_reg
, si
->src_reg
,
7745 offsetof(struct sk_msg
, sk
));
7746 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7747 offsetof(struct sock_common
,
7748 skc_v6_daddr
.s6_addr32
[0]) +
7751 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7755 case offsetof(struct sk_msg_md
, local_ip6
[0]) ...
7756 offsetof(struct sk_msg_md
, local_ip6
[3]):
7757 #if IS_ENABLED(CONFIG_IPV6)
7758 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
,
7759 skc_v6_rcv_saddr
.s6_addr32
[0]) != 4);
7762 off
-= offsetof(struct sk_msg_md
, local_ip6
[0]);
7763 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7765 si
->dst_reg
, si
->src_reg
,
7766 offsetof(struct sk_msg
, sk
));
7767 *insn
++ = BPF_LDX_MEM(BPF_W
, si
->dst_reg
, si
->dst_reg
,
7768 offsetof(struct sock_common
,
7769 skc_v6_rcv_saddr
.s6_addr32
[0]) +
7772 *insn
++ = BPF_MOV32_IMM(si
->dst_reg
, 0);
7776 case offsetof(struct sk_msg_md
, remote_port
):
7777 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_dport
) != 2);
7779 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7781 si
->dst_reg
, si
->src_reg
,
7782 offsetof(struct sk_msg
, sk
));
7783 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
7784 offsetof(struct sock_common
, skc_dport
));
7785 #ifndef __BIG_ENDIAN_BITFIELD
7786 *insn
++ = BPF_ALU32_IMM(BPF_LSH
, si
->dst_reg
, 16);
7790 case offsetof(struct sk_msg_md
, local_port
):
7791 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common
, skc_num
) != 2);
7793 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7795 si
->dst_reg
, si
->src_reg
,
7796 offsetof(struct sk_msg
, sk
));
7797 *insn
++ = BPF_LDX_MEM(BPF_H
, si
->dst_reg
, si
->dst_reg
,
7798 offsetof(struct sock_common
, skc_num
));
7801 case offsetof(struct sk_msg_md
, size
):
7802 *insn
++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg
, size
),
7803 si
->dst_reg
, si
->src_reg
,
7804 offsetof(struct sk_msg_sg
, size
));
7808 return insn
- insn_buf
;
7811 const struct bpf_verifier_ops sk_filter_verifier_ops
= {
7812 .get_func_proto
= sk_filter_func_proto
,
7813 .is_valid_access
= sk_filter_is_valid_access
,
7814 .convert_ctx_access
= bpf_convert_ctx_access
,
7815 .gen_ld_abs
= bpf_gen_ld_abs
,
7818 const struct bpf_prog_ops sk_filter_prog_ops
= {
7819 .test_run
= bpf_prog_test_run_skb
,
7822 const struct bpf_verifier_ops tc_cls_act_verifier_ops
= {
7823 .get_func_proto
= tc_cls_act_func_proto
,
7824 .is_valid_access
= tc_cls_act_is_valid_access
,
7825 .convert_ctx_access
= tc_cls_act_convert_ctx_access
,
7826 .gen_prologue
= tc_cls_act_prologue
,
7827 .gen_ld_abs
= bpf_gen_ld_abs
,
7830 const struct bpf_prog_ops tc_cls_act_prog_ops
= {
7831 .test_run
= bpf_prog_test_run_skb
,
7834 const struct bpf_verifier_ops xdp_verifier_ops
= {
7835 .get_func_proto
= xdp_func_proto
,
7836 .is_valid_access
= xdp_is_valid_access
,
7837 .convert_ctx_access
= xdp_convert_ctx_access
,
7838 .gen_prologue
= bpf_noop_prologue
,
7841 const struct bpf_prog_ops xdp_prog_ops
= {
7842 .test_run
= bpf_prog_test_run_xdp
,
7845 const struct bpf_verifier_ops cg_skb_verifier_ops
= {
7846 .get_func_proto
= cg_skb_func_proto
,
7847 .is_valid_access
= cg_skb_is_valid_access
,
7848 .convert_ctx_access
= bpf_convert_ctx_access
,
7851 const struct bpf_prog_ops cg_skb_prog_ops
= {
7852 .test_run
= bpf_prog_test_run_skb
,
7855 const struct bpf_verifier_ops lwt_in_verifier_ops
= {
7856 .get_func_proto
= lwt_in_func_proto
,
7857 .is_valid_access
= lwt_is_valid_access
,
7858 .convert_ctx_access
= bpf_convert_ctx_access
,
7861 const struct bpf_prog_ops lwt_in_prog_ops
= {
7862 .test_run
= bpf_prog_test_run_skb
,
7865 const struct bpf_verifier_ops lwt_out_verifier_ops
= {
7866 .get_func_proto
= lwt_out_func_proto
,
7867 .is_valid_access
= lwt_is_valid_access
,
7868 .convert_ctx_access
= bpf_convert_ctx_access
,
7871 const struct bpf_prog_ops lwt_out_prog_ops
= {
7872 .test_run
= bpf_prog_test_run_skb
,
7875 const struct bpf_verifier_ops lwt_xmit_verifier_ops
= {
7876 .get_func_proto
= lwt_xmit_func_proto
,
7877 .is_valid_access
= lwt_is_valid_access
,
7878 .convert_ctx_access
= bpf_convert_ctx_access
,
7879 .gen_prologue
= tc_cls_act_prologue
,
7882 const struct bpf_prog_ops lwt_xmit_prog_ops
= {
7883 .test_run
= bpf_prog_test_run_skb
,
7886 const struct bpf_verifier_ops lwt_seg6local_verifier_ops
= {
7887 .get_func_proto
= lwt_seg6local_func_proto
,
7888 .is_valid_access
= lwt_is_valid_access
,
7889 .convert_ctx_access
= bpf_convert_ctx_access
,
7892 const struct bpf_prog_ops lwt_seg6local_prog_ops
= {
7893 .test_run
= bpf_prog_test_run_skb
,
7896 const struct bpf_verifier_ops cg_sock_verifier_ops
= {
7897 .get_func_proto
= sock_filter_func_proto
,
7898 .is_valid_access
= sock_filter_is_valid_access
,
7899 .convert_ctx_access
= bpf_sock_convert_ctx_access
,
7902 const struct bpf_prog_ops cg_sock_prog_ops
= {
7905 const struct bpf_verifier_ops cg_sock_addr_verifier_ops
= {
7906 .get_func_proto
= sock_addr_func_proto
,
7907 .is_valid_access
= sock_addr_is_valid_access
,
7908 .convert_ctx_access
= sock_addr_convert_ctx_access
,
7911 const struct bpf_prog_ops cg_sock_addr_prog_ops
= {
7914 const struct bpf_verifier_ops sock_ops_verifier_ops
= {
7915 .get_func_proto
= sock_ops_func_proto
,
7916 .is_valid_access
= sock_ops_is_valid_access
,
7917 .convert_ctx_access
= sock_ops_convert_ctx_access
,
7920 const struct bpf_prog_ops sock_ops_prog_ops
= {
7923 const struct bpf_verifier_ops sk_skb_verifier_ops
= {
7924 .get_func_proto
= sk_skb_func_proto
,
7925 .is_valid_access
= sk_skb_is_valid_access
,
7926 .convert_ctx_access
= sk_skb_convert_ctx_access
,
7927 .gen_prologue
= sk_skb_prologue
,
7930 const struct bpf_prog_ops sk_skb_prog_ops
= {
7933 const struct bpf_verifier_ops sk_msg_verifier_ops
= {
7934 .get_func_proto
= sk_msg_func_proto
,
7935 .is_valid_access
= sk_msg_is_valid_access
,
7936 .convert_ctx_access
= sk_msg_convert_ctx_access
,
7937 .gen_prologue
= bpf_noop_prologue
,
7940 const struct bpf_prog_ops sk_msg_prog_ops
= {
7943 const struct bpf_verifier_ops flow_dissector_verifier_ops
= {
7944 .get_func_proto
= flow_dissector_func_proto
,
7945 .is_valid_access
= flow_dissector_is_valid_access
,
7946 .convert_ctx_access
= bpf_convert_ctx_access
,
7949 const struct bpf_prog_ops flow_dissector_prog_ops
= {
7950 .test_run
= bpf_prog_test_run_flow_dissector
,
7953 int sk_detach_filter(struct sock
*sk
)
7956 struct sk_filter
*filter
;
7958 if (sock_flag(sk
, SOCK_FILTER_LOCKED
))
7961 filter
= rcu_dereference_protected(sk
->sk_filter
,
7962 lockdep_sock_is_held(sk
));
7964 RCU_INIT_POINTER(sk
->sk_filter
, NULL
);
7965 sk_filter_uncharge(sk
, filter
);
7971 EXPORT_SYMBOL_GPL(sk_detach_filter
);
7973 int sk_get_filter(struct sock
*sk
, struct sock_filter __user
*ubuf
,
7976 struct sock_fprog_kern
*fprog
;
7977 struct sk_filter
*filter
;
7981 filter
= rcu_dereference_protected(sk
->sk_filter
,
7982 lockdep_sock_is_held(sk
));
7986 /* We're copying the filter that has been originally attached,
7987 * so no conversion/decode needed anymore. eBPF programs that
7988 * have no original program cannot be dumped through this.
7991 fprog
= filter
->prog
->orig_prog
;
7997 /* User space only enquires number of filter blocks. */
8001 if (len
< fprog
->len
)
8005 if (copy_to_user(ubuf
, fprog
->filter
, bpf_classic_proglen(fprog
)))
8008 /* Instead of bytes, the API requests to return the number
8018 struct sk_reuseport_kern
{
8019 struct sk_buff
*skb
;
8021 struct sock
*selected_sk
;
8028 static void bpf_init_reuseport_kern(struct sk_reuseport_kern
*reuse_kern
,
8029 struct sock_reuseport
*reuse
,
8030 struct sock
*sk
, struct sk_buff
*skb
,
8033 reuse_kern
->skb
= skb
;
8034 reuse_kern
->sk
= sk
;
8035 reuse_kern
->selected_sk
= NULL
;
8036 reuse_kern
->data_end
= skb
->data
+ skb_headlen(skb
);
8037 reuse_kern
->hash
= hash
;
8038 reuse_kern
->reuseport_id
= reuse
->reuseport_id
;
8039 reuse_kern
->bind_inany
= reuse
->bind_inany
;
8042 struct sock
*bpf_run_sk_reuseport(struct sock_reuseport
*reuse
, struct sock
*sk
,
8043 struct bpf_prog
*prog
, struct sk_buff
*skb
,
8046 struct sk_reuseport_kern reuse_kern
;
8047 enum sk_action action
;
8049 bpf_init_reuseport_kern(&reuse_kern
, reuse
, sk
, skb
, hash
);
8050 action
= BPF_PROG_RUN(prog
, &reuse_kern
);
8052 if (action
== SK_PASS
)
8053 return reuse_kern
.selected_sk
;
8055 return ERR_PTR(-ECONNREFUSED
);
8058 BPF_CALL_4(sk_select_reuseport
, struct sk_reuseport_kern
*, reuse_kern
,
8059 struct bpf_map
*, map
, void *, key
, u32
, flags
)
8061 struct sock_reuseport
*reuse
;
8062 struct sock
*selected_sk
;
8064 selected_sk
= map
->ops
->map_lookup_elem(map
, key
);
8068 reuse
= rcu_dereference(selected_sk
->sk_reuseport_cb
);
8070 /* selected_sk is unhashed (e.g. by close()) after the
8071 * above map_lookup_elem(). Treat selected_sk has already
8072 * been removed from the map.
8076 if (unlikely(reuse
->reuseport_id
!= reuse_kern
->reuseport_id
)) {
8079 if (unlikely(!reuse_kern
->reuseport_id
))
8080 /* There is a small race between adding the
8081 * sk to the map and setting the
8082 * reuse_kern->reuseport_id.
8083 * Treat it as the sk has not been added to
8088 sk
= reuse_kern
->sk
;
8089 if (sk
->sk_protocol
!= selected_sk
->sk_protocol
)
8091 else if (sk
->sk_family
!= selected_sk
->sk_family
)
8092 return -EAFNOSUPPORT
;
8094 /* Catch all. Likely bound to a different sockaddr. */
8098 reuse_kern
->selected_sk
= selected_sk
;
8103 static const struct bpf_func_proto sk_select_reuseport_proto
= {
8104 .func
= sk_select_reuseport
,
8106 .ret_type
= RET_INTEGER
,
8107 .arg1_type
= ARG_PTR_TO_CTX
,
8108 .arg2_type
= ARG_CONST_MAP_PTR
,
8109 .arg3_type
= ARG_PTR_TO_MAP_KEY
,
8110 .arg4_type
= ARG_ANYTHING
,
8113 BPF_CALL_4(sk_reuseport_load_bytes
,
8114 const struct sk_reuseport_kern
*, reuse_kern
, u32
, offset
,
8115 void *, to
, u32
, len
)
8117 return ____bpf_skb_load_bytes(reuse_kern
->skb
, offset
, to
, len
);
8120 static const struct bpf_func_proto sk_reuseport_load_bytes_proto
= {
8121 .func
= sk_reuseport_load_bytes
,
8123 .ret_type
= RET_INTEGER
,
8124 .arg1_type
= ARG_PTR_TO_CTX
,
8125 .arg2_type
= ARG_ANYTHING
,
8126 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
8127 .arg4_type
= ARG_CONST_SIZE
,
8130 BPF_CALL_5(sk_reuseport_load_bytes_relative
,
8131 const struct sk_reuseport_kern
*, reuse_kern
, u32
, offset
,
8132 void *, to
, u32
, len
, u32
, start_header
)
8134 return ____bpf_skb_load_bytes_relative(reuse_kern
->skb
, offset
, to
,
8138 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto
= {
8139 .func
= sk_reuseport_load_bytes_relative
,
8141 .ret_type
= RET_INTEGER
,
8142 .arg1_type
= ARG_PTR_TO_CTX
,
8143 .arg2_type
= ARG_ANYTHING
,
8144 .arg3_type
= ARG_PTR_TO_UNINIT_MEM
,
8145 .arg4_type
= ARG_CONST_SIZE
,
8146 .arg5_type
= ARG_ANYTHING
,
8149 static const struct bpf_func_proto
*
8150 sk_reuseport_func_proto(enum bpf_func_id func_id
,
8151 const struct bpf_prog
*prog
)
8154 case BPF_FUNC_sk_select_reuseport
:
8155 return &sk_select_reuseport_proto
;
8156 case BPF_FUNC_skb_load_bytes
:
8157 return &sk_reuseport_load_bytes_proto
;
8158 case BPF_FUNC_skb_load_bytes_relative
:
8159 return &sk_reuseport_load_bytes_relative_proto
;
8161 return bpf_base_func_proto(func_id
);
8166 sk_reuseport_is_valid_access(int off
, int size
,
8167 enum bpf_access_type type
,
8168 const struct bpf_prog
*prog
,
8169 struct bpf_insn_access_aux
*info
)
8171 const u32 size_default
= sizeof(__u32
);
8173 if (off
< 0 || off
>= sizeof(struct sk_reuseport_md
) ||
8174 off
% size
|| type
!= BPF_READ
)
8178 case offsetof(struct sk_reuseport_md
, data
):
8179 info
->reg_type
= PTR_TO_PACKET
;
8180 return size
== sizeof(__u64
);
8182 case offsetof(struct sk_reuseport_md
, data_end
):
8183 info
->reg_type
= PTR_TO_PACKET_END
;
8184 return size
== sizeof(__u64
);
8186 case offsetof(struct sk_reuseport_md
, hash
):
8187 return size
== size_default
;
8189 /* Fields that allow narrowing */
8190 case offsetof(struct sk_reuseport_md
, eth_protocol
):
8191 if (size
< FIELD_SIZEOF(struct sk_buff
, protocol
))
8194 case offsetof(struct sk_reuseport_md
, ip_protocol
):
8195 case offsetof(struct sk_reuseport_md
, bind_inany
):
8196 case offsetof(struct sk_reuseport_md
, len
):
8197 bpf_ctx_record_field_size(info
, size_default
);
8198 return bpf_ctx_narrow_access_ok(off
, size
, size_default
);
8205 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
8206 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8207 si->dst_reg, si->src_reg, \
8208 bpf_target_off(struct sk_reuseport_kern, F, \
8209 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8213 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
8214 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
8219 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
8220 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
8223 SK_FIELD, BPF_SIZE, EXTRA_OFF)
8225 static u32
sk_reuseport_convert_ctx_access(enum bpf_access_type type
,
8226 const struct bpf_insn
*si
,
8227 struct bpf_insn
*insn_buf
,
8228 struct bpf_prog
*prog
,
8231 struct bpf_insn
*insn
= insn_buf
;
8234 case offsetof(struct sk_reuseport_md
, data
):
8235 SK_REUSEPORT_LOAD_SKB_FIELD(data
);
8238 case offsetof(struct sk_reuseport_md
, len
):
8239 SK_REUSEPORT_LOAD_SKB_FIELD(len
);
8242 case offsetof(struct sk_reuseport_md
, eth_protocol
):
8243 SK_REUSEPORT_LOAD_SKB_FIELD(protocol
);
8246 case offsetof(struct sk_reuseport_md
, ip_protocol
):
8247 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK
) != BITS_PER_BYTE
);
8248 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset
,
8250 *insn
++ = BPF_ALU32_IMM(BPF_AND
, si
->dst_reg
, SK_FL_PROTO_MASK
);
8251 *insn
++ = BPF_ALU32_IMM(BPF_RSH
, si
->dst_reg
,
8253 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
8254 * aware. No further narrowing or masking is needed.
8259 case offsetof(struct sk_reuseport_md
, data_end
):
8260 SK_REUSEPORT_LOAD_FIELD(data_end
);
8263 case offsetof(struct sk_reuseport_md
, hash
):
8264 SK_REUSEPORT_LOAD_FIELD(hash
);
8267 case offsetof(struct sk_reuseport_md
, bind_inany
):
8268 SK_REUSEPORT_LOAD_FIELD(bind_inany
);
8272 return insn
- insn_buf
;
8275 const struct bpf_verifier_ops sk_reuseport_verifier_ops
= {
8276 .get_func_proto
= sk_reuseport_func_proto
,
8277 .is_valid_access
= sk_reuseport_is_valid_access
,
8278 .convert_ctx_access
= sk_reuseport_convert_ctx_access
,
8281 const struct bpf_prog_ops sk_reuseport_prog_ops
= {
8283 #endif /* CONFIG_INET */