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>
37 #include <net/protocol.h>
38 #include <net/netlink.h>
39 #include <linux/skbuff.h>
41 #include <net/flow_dissector.h>
42 #include <linux/errno.h>
43 #include <linux/timer.h>
44 #include <linux/uaccess.h>
45 #include <asm/unaligned.h>
46 #include <asm/cmpxchg.h>
47 #include <linux/filter.h>
48 #include <linux/ratelimit.h>
49 #include <linux/seccomp.h>
50 #include <linux/if_vlan.h>
51 #include <linux/bpf.h>
52 #include <net/sch_generic.h>
53 #include <net/cls_cgroup.h>
54 #include <net/dst_metadata.h>
56 #include <net/sock_reuseport.h>
57 #include <net/busy_poll.h>
59 #include <linux/bpf_trace.h>
62 * sk_filter_trim_cap - run a packet through a socket filter
63 * @sk: sock associated with &sk_buff
64 * @skb: buffer to filter
65 * @cap: limit on how short the eBPF program may trim the packet
67 * Run the eBPF program and then cut skb->data to correct size returned by
68 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
69 * than pkt_len we keep whole skb->data. This is the socket level
70 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
71 * be accepted or -EPERM if the packet should be tossed.
74 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
77 struct sk_filter *filter;
80 * If the skb was allocated from pfmemalloc reserves, only
81 * allow SOCK_MEMALLOC sockets to use it as this socket is
84 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
85 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
88 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
92 err = security_sock_rcv_skb(sk, skb);
97 filter = rcu_dereference(sk->sk_filter);
99 struct sock *save_sk = skb->sk;
100 unsigned int pkt_len;
103 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
105 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
111 EXPORT_SYMBOL(sk_filter_trim_cap);
113 BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
115 return skb_get_poff(skb);
118 BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
122 if (skb_is_nonlinear(skb))
125 if (skb->len < sizeof(struct nlattr))
128 if (a > skb->len - sizeof(struct nlattr))
131 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
133 return (void *) nla - (void *) skb->data;
138 BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
142 if (skb_is_nonlinear(skb))
145 if (skb->len < sizeof(struct nlattr))
148 if (a > skb->len - sizeof(struct nlattr))
151 nla = (struct nlattr *) &skb->data[a];
152 if (nla->nla_len > skb->len - a)
155 nla = nla_find_nested(nla, x);
157 return (void *) nla - (void *) skb->data;
162 BPF_CALL_0(__get_raw_cpu_id)
164 return raw_smp_processor_id();
167 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
168 .func = __get_raw_cpu_id,
170 .ret_type = RET_INTEGER,
173 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
174 struct bpf_insn *insn_buf)
176 struct bpf_insn *insn = insn_buf;
180 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
182 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
183 offsetof(struct sk_buff, mark));
187 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
188 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
189 #ifdef __BIG_ENDIAN_BITFIELD
190 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
195 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
197 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
198 offsetof(struct sk_buff, queue_mapping));
201 case SKF_AD_VLAN_TAG:
202 case SKF_AD_VLAN_TAG_PRESENT:
203 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
204 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
206 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
207 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
208 offsetof(struct sk_buff, vlan_tci));
209 if (skb_field == SKF_AD_VLAN_TAG) {
210 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
214 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
216 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
221 return insn - insn_buf;
224 static bool convert_bpf_extensions(struct sock_filter *fp,
225 struct bpf_insn **insnp)
227 struct bpf_insn *insn = *insnp;
231 case SKF_AD_OFF + SKF_AD_PROTOCOL:
232 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
234 /* A = *(u16 *) (CTX + offsetof(protocol)) */
235 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
236 offsetof(struct sk_buff, protocol));
237 /* A = ntohs(A) [emitting a nop or swap16] */
238 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
241 case SKF_AD_OFF + SKF_AD_PKTTYPE:
242 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
246 case SKF_AD_OFF + SKF_AD_IFINDEX:
247 case SKF_AD_OFF + SKF_AD_HATYPE:
248 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
249 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
251 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
252 BPF_REG_TMP, BPF_REG_CTX,
253 offsetof(struct sk_buff, dev));
254 /* if (tmp != 0) goto pc + 1 */
255 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
256 *insn++ = BPF_EXIT_INSN();
257 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
258 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
259 offsetof(struct net_device, ifindex));
261 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
262 offsetof(struct net_device, type));
265 case SKF_AD_OFF + SKF_AD_MARK:
266 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
270 case SKF_AD_OFF + SKF_AD_RXHASH:
271 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
273 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
274 offsetof(struct sk_buff, hash));
277 case SKF_AD_OFF + SKF_AD_QUEUE:
278 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
282 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
283 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
284 BPF_REG_A, BPF_REG_CTX, insn);
288 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
289 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
290 BPF_REG_A, BPF_REG_CTX, insn);
294 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
295 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
297 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
298 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
299 offsetof(struct sk_buff, vlan_proto));
300 /* A = ntohs(A) [emitting a nop or swap16] */
301 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
304 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
305 case SKF_AD_OFF + SKF_AD_NLATTR:
306 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
307 case SKF_AD_OFF + SKF_AD_CPU:
308 case SKF_AD_OFF + SKF_AD_RANDOM:
310 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
312 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
314 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
315 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
317 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
318 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
320 case SKF_AD_OFF + SKF_AD_NLATTR:
321 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
323 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
324 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
326 case SKF_AD_OFF + SKF_AD_CPU:
327 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
329 case SKF_AD_OFF + SKF_AD_RANDOM:
330 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
331 bpf_user_rnd_init_once();
336 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
338 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
342 /* This is just a dummy call to avoid letting the compiler
343 * evict __bpf_call_base() as an optimization. Placed here
344 * where no-one bothers.
346 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
355 * bpf_convert_filter - convert filter program
356 * @prog: the user passed filter program
357 * @len: the length of the user passed filter program
358 * @new_prog: allocated 'struct bpf_prog' or NULL
359 * @new_len: pointer to store length of converted program
361 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
362 * style extended BPF (eBPF).
363 * Conversion workflow:
365 * 1) First pass for calculating the new program length:
366 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
368 * 2) 2nd pass to remap in two passes: 1st pass finds new
369 * jump offsets, 2nd pass remapping:
370 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
372 static int bpf_convert_filter(struct sock_filter *prog, int len,
373 struct bpf_prog *new_prog, int *new_len)
375 int new_flen = 0, pass = 0, target, i, stack_off;
376 struct bpf_insn *new_insn, *first_insn = NULL;
377 struct sock_filter *fp;
381 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
382 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
384 if (len <= 0 || len > BPF_MAXINSNS)
388 first_insn = new_prog->insnsi;
389 addrs = kcalloc(len, sizeof(*addrs),
390 GFP_KERNEL | __GFP_NOWARN);
396 new_insn = first_insn;
399 /* Classic BPF related prologue emission. */
401 /* Classic BPF expects A and X to be reset first. These need
402 * to be guaranteed to be the first two instructions.
404 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
405 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
407 /* All programs must keep CTX in callee saved BPF_REG_CTX.
408 * In eBPF case it's done by the compiler, here we need to
409 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
411 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
416 for (i = 0; i < len; fp++, i++) {
417 struct bpf_insn tmp_insns[6] = { };
418 struct bpf_insn *insn = tmp_insns;
421 addrs[i] = new_insn - first_insn;
424 /* All arithmetic insns and skb loads map as-is. */
425 case BPF_ALU | BPF_ADD | BPF_X:
426 case BPF_ALU | BPF_ADD | BPF_K:
427 case BPF_ALU | BPF_SUB | BPF_X:
428 case BPF_ALU | BPF_SUB | BPF_K:
429 case BPF_ALU | BPF_AND | BPF_X:
430 case BPF_ALU | BPF_AND | BPF_K:
431 case BPF_ALU | BPF_OR | BPF_X:
432 case BPF_ALU | BPF_OR | BPF_K:
433 case BPF_ALU | BPF_LSH | BPF_X:
434 case BPF_ALU | BPF_LSH | BPF_K:
435 case BPF_ALU | BPF_RSH | BPF_X:
436 case BPF_ALU | BPF_RSH | BPF_K:
437 case BPF_ALU | BPF_XOR | BPF_X:
438 case BPF_ALU | BPF_XOR | BPF_K:
439 case BPF_ALU | BPF_MUL | BPF_X:
440 case BPF_ALU | BPF_MUL | BPF_K:
441 case BPF_ALU | BPF_DIV | BPF_X:
442 case BPF_ALU | BPF_DIV | BPF_K:
443 case BPF_ALU | BPF_MOD | BPF_X:
444 case BPF_ALU | BPF_MOD | BPF_K:
445 case BPF_ALU | BPF_NEG:
446 case BPF_LD | BPF_ABS | BPF_W:
447 case BPF_LD | BPF_ABS | BPF_H:
448 case BPF_LD | BPF_ABS | BPF_B:
449 case BPF_LD | BPF_IND | BPF_W:
450 case BPF_LD | BPF_IND | BPF_H:
451 case BPF_LD | BPF_IND | BPF_B:
452 /* Check for overloaded BPF extension and
453 * directly convert it if found, otherwise
454 * just move on with mapping.
456 if (BPF_CLASS(fp->code) == BPF_LD &&
457 BPF_MODE(fp->code) == BPF_ABS &&
458 convert_bpf_extensions(fp, &insn))
461 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
462 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
463 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
464 /* Error with exception code on div/mod by 0.
465 * For cBPF programs, this was always return 0.
467 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
468 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
469 *insn++ = BPF_EXIT_INSN();
472 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
475 /* Jump transformation cannot use BPF block macros
476 * everywhere as offset calculation and target updates
477 * require a bit more work than the rest, i.e. jump
478 * opcodes map as-is, but offsets need adjustment.
481 #define BPF_EMIT_JMP \
483 if (target >= len || target < 0) \
485 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
486 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
487 insn->off -= insn - tmp_insns; \
490 case BPF_JMP | BPF_JA:
491 target = i + fp->k + 1;
492 insn->code = fp->code;
496 case BPF_JMP | BPF_JEQ | BPF_K:
497 case BPF_JMP | BPF_JEQ | BPF_X:
498 case BPF_JMP | BPF_JSET | BPF_K:
499 case BPF_JMP | BPF_JSET | BPF_X:
500 case BPF_JMP | BPF_JGT | BPF_K:
501 case BPF_JMP | BPF_JGT | BPF_X:
502 case BPF_JMP | BPF_JGE | BPF_K:
503 case BPF_JMP | BPF_JGE | BPF_X:
504 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
505 /* BPF immediates are signed, zero extend
506 * immediate into tmp register and use it
509 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
511 insn->dst_reg = BPF_REG_A;
512 insn->src_reg = BPF_REG_TMP;
515 insn->dst_reg = BPF_REG_A;
517 bpf_src = BPF_SRC(fp->code);
518 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
521 /* Common case where 'jump_false' is next insn. */
523 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
524 target = i + fp->jt + 1;
529 /* Convert some jumps when 'jump_true' is next insn. */
531 switch (BPF_OP(fp->code)) {
533 insn->code = BPF_JMP | BPF_JNE | bpf_src;
536 insn->code = BPF_JMP | BPF_JLE | bpf_src;
539 insn->code = BPF_JMP | BPF_JLT | bpf_src;
545 target = i + fp->jf + 1;
550 /* Other jumps are mapped into two insns: Jxx and JA. */
551 target = i + fp->jt + 1;
552 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
556 insn->code = BPF_JMP | BPF_JA;
557 target = i + fp->jf + 1;
561 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
562 case BPF_LDX | BPF_MSH | BPF_B:
564 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
565 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
566 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
568 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
570 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
572 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
574 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
577 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
578 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
580 case BPF_RET | BPF_A:
581 case BPF_RET | BPF_K:
582 if (BPF_RVAL(fp->code) == BPF_K)
583 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
585 *insn = BPF_EXIT_INSN();
588 /* Store to stack. */
591 stack_off = fp->k * 4 + 4;
592 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
593 BPF_ST ? BPF_REG_A : BPF_REG_X,
595 /* check_load_and_stores() verifies that classic BPF can
596 * load from stack only after write, so tracking
597 * stack_depth for ST|STX insns is enough
599 if (new_prog && new_prog->aux->stack_depth < stack_off)
600 new_prog->aux->stack_depth = stack_off;
603 /* Load from stack. */
604 case BPF_LD | BPF_MEM:
605 case BPF_LDX | BPF_MEM:
606 stack_off = fp->k * 4 + 4;
607 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
608 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
613 case BPF_LD | BPF_IMM:
614 case BPF_LDX | BPF_IMM:
615 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
616 BPF_REG_A : BPF_REG_X, fp->k);
620 case BPF_MISC | BPF_TAX:
621 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
625 case BPF_MISC | BPF_TXA:
626 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
629 /* A = skb->len or X = skb->len */
630 case BPF_LD | BPF_W | BPF_LEN:
631 case BPF_LDX | BPF_W | BPF_LEN:
632 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
633 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
634 offsetof(struct sk_buff, len));
637 /* Access seccomp_data fields. */
638 case BPF_LDX | BPF_ABS | BPF_W:
639 /* A = *(u32 *) (ctx + K) */
640 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
643 /* Unknown instruction. */
650 memcpy(new_insn, tmp_insns,
651 sizeof(*insn) * (insn - tmp_insns));
652 new_insn += insn - tmp_insns;
656 /* Only calculating new length. */
657 *new_len = new_insn - first_insn;
662 if (new_flen != new_insn - first_insn) {
663 new_flen = new_insn - first_insn;
670 BUG_ON(*new_len != new_flen);
679 * As we dont want to clear mem[] array for each packet going through
680 * __bpf_prog_run(), we check that filter loaded by user never try to read
681 * a cell if not previously written, and we check all branches to be sure
682 * a malicious user doesn't try to abuse us.
684 static int check_load_and_stores(const struct sock_filter *filter, int flen)
686 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
689 BUILD_BUG_ON(BPF_MEMWORDS > 16);
691 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
695 memset(masks, 0xff, flen * sizeof(*masks));
697 for (pc = 0; pc < flen; pc++) {
698 memvalid &= masks[pc];
700 switch (filter[pc].code) {
703 memvalid |= (1 << filter[pc].k);
705 case BPF_LD | BPF_MEM:
706 case BPF_LDX | BPF_MEM:
707 if (!(memvalid & (1 << filter[pc].k))) {
712 case BPF_JMP | BPF_JA:
713 /* A jump must set masks on target */
714 masks[pc + 1 + filter[pc].k] &= memvalid;
717 case BPF_JMP | BPF_JEQ | BPF_K:
718 case BPF_JMP | BPF_JEQ | BPF_X:
719 case BPF_JMP | BPF_JGE | BPF_K:
720 case BPF_JMP | BPF_JGE | BPF_X:
721 case BPF_JMP | BPF_JGT | BPF_K:
722 case BPF_JMP | BPF_JGT | BPF_X:
723 case BPF_JMP | BPF_JSET | BPF_K:
724 case BPF_JMP | BPF_JSET | BPF_X:
725 /* A jump must set masks on targets */
726 masks[pc + 1 + filter[pc].jt] &= memvalid;
727 masks[pc + 1 + filter[pc].jf] &= memvalid;
737 static bool chk_code_allowed(u16 code_to_probe)
739 static const bool codes[] = {
740 /* 32 bit ALU operations */
741 [BPF_ALU | BPF_ADD | BPF_K] = true,
742 [BPF_ALU | BPF_ADD | BPF_X] = true,
743 [BPF_ALU | BPF_SUB | BPF_K] = true,
744 [BPF_ALU | BPF_SUB | BPF_X] = true,
745 [BPF_ALU | BPF_MUL | BPF_K] = true,
746 [BPF_ALU | BPF_MUL | BPF_X] = true,
747 [BPF_ALU | BPF_DIV | BPF_K] = true,
748 [BPF_ALU | BPF_DIV | BPF_X] = true,
749 [BPF_ALU | BPF_MOD | BPF_K] = true,
750 [BPF_ALU | BPF_MOD | BPF_X] = true,
751 [BPF_ALU | BPF_AND | BPF_K] = true,
752 [BPF_ALU | BPF_AND | BPF_X] = true,
753 [BPF_ALU | BPF_OR | BPF_K] = true,
754 [BPF_ALU | BPF_OR | BPF_X] = true,
755 [BPF_ALU | BPF_XOR | BPF_K] = true,
756 [BPF_ALU | BPF_XOR | BPF_X] = true,
757 [BPF_ALU | BPF_LSH | BPF_K] = true,
758 [BPF_ALU | BPF_LSH | BPF_X] = true,
759 [BPF_ALU | BPF_RSH | BPF_K] = true,
760 [BPF_ALU | BPF_RSH | BPF_X] = true,
761 [BPF_ALU | BPF_NEG] = true,
762 /* Load instructions */
763 [BPF_LD | BPF_W | BPF_ABS] = true,
764 [BPF_LD | BPF_H | BPF_ABS] = true,
765 [BPF_LD | BPF_B | BPF_ABS] = true,
766 [BPF_LD | BPF_W | BPF_LEN] = true,
767 [BPF_LD | BPF_W | BPF_IND] = true,
768 [BPF_LD | BPF_H | BPF_IND] = true,
769 [BPF_LD | BPF_B | BPF_IND] = true,
770 [BPF_LD | BPF_IMM] = true,
771 [BPF_LD | BPF_MEM] = true,
772 [BPF_LDX | BPF_W | BPF_LEN] = true,
773 [BPF_LDX | BPF_B | BPF_MSH] = true,
774 [BPF_LDX | BPF_IMM] = true,
775 [BPF_LDX | BPF_MEM] = true,
776 /* Store instructions */
779 /* Misc instructions */
780 [BPF_MISC | BPF_TAX] = true,
781 [BPF_MISC | BPF_TXA] = true,
782 /* Return instructions */
783 [BPF_RET | BPF_K] = true,
784 [BPF_RET | BPF_A] = true,
785 /* Jump instructions */
786 [BPF_JMP | BPF_JA] = true,
787 [BPF_JMP | BPF_JEQ | BPF_K] = true,
788 [BPF_JMP | BPF_JEQ | BPF_X] = true,
789 [BPF_JMP | BPF_JGE | BPF_K] = true,
790 [BPF_JMP | BPF_JGE | BPF_X] = true,
791 [BPF_JMP | BPF_JGT | BPF_K] = true,
792 [BPF_JMP | BPF_JGT | BPF_X] = true,
793 [BPF_JMP | BPF_JSET | BPF_K] = true,
794 [BPF_JMP | BPF_JSET | BPF_X] = true,
797 if (code_to_probe >= ARRAY_SIZE(codes))
800 return codes[code_to_probe];
803 static bool bpf_check_basics_ok(const struct sock_filter *filter,
808 if (flen == 0 || flen > BPF_MAXINSNS)
815 * bpf_check_classic - verify socket filter code
816 * @filter: filter to verify
817 * @flen: length of filter
819 * Check the user's filter code. If we let some ugly
820 * filter code slip through kaboom! The filter must contain
821 * no references or jumps that are out of range, no illegal
822 * instructions, and must end with a RET instruction.
824 * All jumps are forward as they are not signed.
826 * Returns 0 if the rule set is legal or -EINVAL if not.
828 static int bpf_check_classic(const struct sock_filter *filter,
834 /* Check the filter code now */
835 for (pc = 0; pc < flen; pc++) {
836 const struct sock_filter *ftest = &filter[pc];
838 /* May we actually operate on this code? */
839 if (!chk_code_allowed(ftest->code))
842 /* Some instructions need special checks */
843 switch (ftest->code) {
844 case BPF_ALU | BPF_DIV | BPF_K:
845 case BPF_ALU | BPF_MOD | BPF_K:
846 /* Check for division by zero */
850 case BPF_ALU | BPF_LSH | BPF_K:
851 case BPF_ALU | BPF_RSH | BPF_K:
855 case BPF_LD | BPF_MEM:
856 case BPF_LDX | BPF_MEM:
859 /* Check for invalid memory addresses */
860 if (ftest->k >= BPF_MEMWORDS)
863 case BPF_JMP | BPF_JA:
864 /* Note, the large ftest->k might cause loops.
865 * Compare this with conditional jumps below,
866 * where offsets are limited. --ANK (981016)
868 if (ftest->k >= (unsigned int)(flen - pc - 1))
871 case BPF_JMP | BPF_JEQ | BPF_K:
872 case BPF_JMP | BPF_JEQ | BPF_X:
873 case BPF_JMP | BPF_JGE | BPF_K:
874 case BPF_JMP | BPF_JGE | BPF_X:
875 case BPF_JMP | BPF_JGT | BPF_K:
876 case BPF_JMP | BPF_JGT | BPF_X:
877 case BPF_JMP | BPF_JSET | BPF_K:
878 case BPF_JMP | BPF_JSET | BPF_X:
879 /* Both conditionals must be safe */
880 if (pc + ftest->jt + 1 >= flen ||
881 pc + ftest->jf + 1 >= flen)
884 case BPF_LD | BPF_W | BPF_ABS:
885 case BPF_LD | BPF_H | BPF_ABS:
886 case BPF_LD | BPF_B | BPF_ABS:
888 if (bpf_anc_helper(ftest) & BPF_ANC)
890 /* Ancillary operation unknown or unsupported */
891 if (anc_found == false && ftest->k >= SKF_AD_OFF)
896 /* Last instruction must be a RET code */
897 switch (filter[flen - 1].code) {
898 case BPF_RET | BPF_K:
899 case BPF_RET | BPF_A:
900 return check_load_and_stores(filter, flen);
906 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
907 const struct sock_fprog *fprog)
909 unsigned int fsize = bpf_classic_proglen(fprog);
910 struct sock_fprog_kern *fkprog;
912 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
916 fkprog = fp->orig_prog;
917 fkprog->len = fprog->len;
919 fkprog->filter = kmemdup(fp->insns, fsize,
920 GFP_KERNEL | __GFP_NOWARN);
921 if (!fkprog->filter) {
922 kfree(fp->orig_prog);
929 static void bpf_release_orig_filter(struct bpf_prog *fp)
931 struct sock_fprog_kern *fprog = fp->orig_prog;
934 kfree(fprog->filter);
939 static void __bpf_prog_release(struct bpf_prog *prog)
941 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
944 bpf_release_orig_filter(prog);
949 static void __sk_filter_release(struct sk_filter *fp)
951 __bpf_prog_release(fp->prog);
956 * sk_filter_release_rcu - Release a socket filter by rcu_head
957 * @rcu: rcu_head that contains the sk_filter to free
959 static void sk_filter_release_rcu(struct rcu_head *rcu)
961 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
963 __sk_filter_release(fp);
967 * sk_filter_release - release a socket filter
968 * @fp: filter to remove
970 * Remove a filter from a socket and release its resources.
972 static void sk_filter_release(struct sk_filter *fp)
974 if (refcount_dec_and_test(&fp->refcnt))
975 call_rcu(&fp->rcu, sk_filter_release_rcu);
978 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
980 u32 filter_size = bpf_prog_size(fp->prog->len);
982 atomic_sub(filter_size, &sk->sk_omem_alloc);
983 sk_filter_release(fp);
986 /* try to charge the socket memory if there is space available
987 * return true on success
989 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
991 u32 filter_size = bpf_prog_size(fp->prog->len);
993 /* same check as in sock_kmalloc() */
994 if (filter_size <= sysctl_optmem_max &&
995 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
996 atomic_add(filter_size, &sk->sk_omem_alloc);
1002 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1004 if (!refcount_inc_not_zero(&fp->refcnt))
1007 if (!__sk_filter_charge(sk, fp)) {
1008 sk_filter_release(fp);
1014 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1016 struct sock_filter *old_prog;
1017 struct bpf_prog *old_fp;
1018 int err, new_len, old_len = fp->len;
1020 /* We are free to overwrite insns et al right here as it
1021 * won't be used at this point in time anymore internally
1022 * after the migration to the internal BPF instruction
1025 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1026 sizeof(struct bpf_insn));
1028 /* Conversion cannot happen on overlapping memory areas,
1029 * so we need to keep the user BPF around until the 2nd
1030 * pass. At this time, the user BPF is stored in fp->insns.
1032 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1033 GFP_KERNEL | __GFP_NOWARN);
1039 /* 1st pass: calculate the new program length. */
1040 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
1044 /* Expand fp for appending the new filter representation. */
1046 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1048 /* The old_fp is still around in case we couldn't
1049 * allocate new memory, so uncharge on that one.
1058 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1059 err = bpf_convert_filter(old_prog, old_len, fp, &new_len);
1061 /* 2nd bpf_convert_filter() can fail only if it fails
1062 * to allocate memory, remapping must succeed. Note,
1063 * that at this time old_fp has already been released
1068 fp = bpf_prog_select_runtime(fp, &err);
1078 __bpf_prog_release(fp);
1079 return ERR_PTR(err);
1082 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1083 bpf_aux_classic_check_t trans)
1087 fp->bpf_func = NULL;
1090 err = bpf_check_classic(fp->insns, fp->len);
1092 __bpf_prog_release(fp);
1093 return ERR_PTR(err);
1096 /* There might be additional checks and transformations
1097 * needed on classic filters, f.e. in case of seccomp.
1100 err = trans(fp->insns, fp->len);
1102 __bpf_prog_release(fp);
1103 return ERR_PTR(err);
1107 /* Probe if we can JIT compile the filter and if so, do
1108 * the compilation of the filter.
1110 bpf_jit_compile(fp);
1112 /* JIT compiler couldn't process this filter, so do the
1113 * internal BPF translation for the optimized interpreter.
1116 fp = bpf_migrate_filter(fp);
1122 * bpf_prog_create - create an unattached filter
1123 * @pfp: the unattached filter that is created
1124 * @fprog: the filter program
1126 * Create a filter independent of any socket. We first run some
1127 * sanity checks on it to make sure it does not explode on us later.
1128 * If an error occurs or there is insufficient memory for the filter
1129 * a negative errno code is returned. On success the return is zero.
1131 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1133 unsigned int fsize = bpf_classic_proglen(fprog);
1134 struct bpf_prog *fp;
1136 /* Make sure new filter is there and in the right amounts. */
1137 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1140 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1144 memcpy(fp->insns, fprog->filter, fsize);
1146 fp->len = fprog->len;
1147 /* Since unattached filters are not copied back to user
1148 * space through sk_get_filter(), we do not need to hold
1149 * a copy here, and can spare us the work.
1151 fp->orig_prog = NULL;
1153 /* bpf_prepare_filter() already takes care of freeing
1154 * memory in case something goes wrong.
1156 fp = bpf_prepare_filter(fp, NULL);
1163 EXPORT_SYMBOL_GPL(bpf_prog_create);
1166 * bpf_prog_create_from_user - create an unattached filter from user buffer
1167 * @pfp: the unattached filter that is created
1168 * @fprog: the filter program
1169 * @trans: post-classic verifier transformation handler
1170 * @save_orig: save classic BPF program
1172 * This function effectively does the same as bpf_prog_create(), only
1173 * that it builds up its insns buffer from user space provided buffer.
1174 * It also allows for passing a bpf_aux_classic_check_t handler.
1176 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1177 bpf_aux_classic_check_t trans, bool save_orig)
1179 unsigned int fsize = bpf_classic_proglen(fprog);
1180 struct bpf_prog *fp;
1183 /* Make sure new filter is there and in the right amounts. */
1184 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1187 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1191 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1192 __bpf_prog_free(fp);
1196 fp->len = fprog->len;
1197 fp->orig_prog = NULL;
1200 err = bpf_prog_store_orig_filter(fp, fprog);
1202 __bpf_prog_free(fp);
1207 /* bpf_prepare_filter() already takes care of freeing
1208 * memory in case something goes wrong.
1210 fp = bpf_prepare_filter(fp, trans);
1217 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1219 void bpf_prog_destroy(struct bpf_prog *fp)
1221 __bpf_prog_release(fp);
1223 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1225 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1227 struct sk_filter *fp, *old_fp;
1229 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1235 if (!__sk_filter_charge(sk, fp)) {
1239 refcount_set(&fp->refcnt, 1);
1241 old_fp = rcu_dereference_protected(sk->sk_filter,
1242 lockdep_sock_is_held(sk));
1243 rcu_assign_pointer(sk->sk_filter, fp);
1246 sk_filter_uncharge(sk, old_fp);
1251 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1253 struct bpf_prog *old_prog;
1256 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1259 if (sk_unhashed(sk) && sk->sk_reuseport) {
1260 err = reuseport_alloc(sk);
1263 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1264 /* The socket wasn't bound with SO_REUSEPORT */
1268 old_prog = reuseport_attach_prog(sk, prog);
1270 bpf_prog_destroy(old_prog);
1276 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1278 unsigned int fsize = bpf_classic_proglen(fprog);
1279 struct bpf_prog *prog;
1282 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1283 return ERR_PTR(-EPERM);
1285 /* Make sure new filter is there and in the right amounts. */
1286 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1287 return ERR_PTR(-EINVAL);
1289 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1291 return ERR_PTR(-ENOMEM);
1293 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1294 __bpf_prog_free(prog);
1295 return ERR_PTR(-EFAULT);
1298 prog->len = fprog->len;
1300 err = bpf_prog_store_orig_filter(prog, fprog);
1302 __bpf_prog_free(prog);
1303 return ERR_PTR(-ENOMEM);
1306 /* bpf_prepare_filter() already takes care of freeing
1307 * memory in case something goes wrong.
1309 return bpf_prepare_filter(prog, NULL);
1313 * sk_attach_filter - attach a socket filter
1314 * @fprog: the filter program
1315 * @sk: the socket to use
1317 * Attach the user's filter code. We first run some sanity checks on
1318 * it to make sure it does not explode on us later. If an error
1319 * occurs or there is insufficient memory for the filter a negative
1320 * errno code is returned. On success the return is zero.
1322 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1324 struct bpf_prog *prog = __get_filter(fprog, sk);
1328 return PTR_ERR(prog);
1330 err = __sk_attach_prog(prog, sk);
1332 __bpf_prog_release(prog);
1338 EXPORT_SYMBOL_GPL(sk_attach_filter);
1340 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1342 struct bpf_prog *prog = __get_filter(fprog, sk);
1346 return PTR_ERR(prog);
1348 err = __reuseport_attach_prog(prog, sk);
1350 __bpf_prog_release(prog);
1357 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1359 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1360 return ERR_PTR(-EPERM);
1362 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1365 int sk_attach_bpf(u32 ufd, struct sock *sk)
1367 struct bpf_prog *prog = __get_bpf(ufd, sk);
1371 return PTR_ERR(prog);
1373 err = __sk_attach_prog(prog, sk);
1382 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1384 struct bpf_prog *prog = __get_bpf(ufd, sk);
1388 return PTR_ERR(prog);
1390 err = __reuseport_attach_prog(prog, sk);
1399 struct bpf_scratchpad {
1401 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1402 u8 buff[MAX_BPF_STACK];
1406 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1408 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1409 unsigned int write_len)
1411 return skb_ensure_writable(skb, write_len);
1414 static inline int bpf_try_make_writable(struct sk_buff *skb,
1415 unsigned int write_len)
1417 int err = __bpf_try_make_writable(skb, write_len);
1419 bpf_compute_data_pointers(skb);
1423 static int bpf_try_make_head_writable(struct sk_buff *skb)
1425 return bpf_try_make_writable(skb, skb_headlen(skb));
1428 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1430 if (skb_at_tc_ingress(skb))
1431 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1434 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1436 if (skb_at_tc_ingress(skb))
1437 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1440 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1441 const void *, from, u32, len, u64, flags)
1445 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1447 if (unlikely(offset > 0xffff))
1449 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1452 ptr = skb->data + offset;
1453 if (flags & BPF_F_RECOMPUTE_CSUM)
1454 __skb_postpull_rcsum(skb, ptr, len, offset);
1456 memcpy(ptr, from, len);
1458 if (flags & BPF_F_RECOMPUTE_CSUM)
1459 __skb_postpush_rcsum(skb, ptr, len, offset);
1460 if (flags & BPF_F_INVALIDATE_HASH)
1461 skb_clear_hash(skb);
1466 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1467 .func = bpf_skb_store_bytes,
1469 .ret_type = RET_INTEGER,
1470 .arg1_type = ARG_PTR_TO_CTX,
1471 .arg2_type = ARG_ANYTHING,
1472 .arg3_type = ARG_PTR_TO_MEM,
1473 .arg4_type = ARG_CONST_SIZE,
1474 .arg5_type = ARG_ANYTHING,
1477 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1478 void *, to, u32, len)
1482 if (unlikely(offset > 0xffff))
1485 ptr = skb_header_pointer(skb, offset, len, to);
1489 memcpy(to, ptr, len);
1497 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1498 .func = bpf_skb_load_bytes,
1500 .ret_type = RET_INTEGER,
1501 .arg1_type = ARG_PTR_TO_CTX,
1502 .arg2_type = ARG_ANYTHING,
1503 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1504 .arg4_type = ARG_CONST_SIZE,
1507 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1509 /* Idea is the following: should the needed direct read/write
1510 * test fail during runtime, we can pull in more data and redo
1511 * again, since implicitly, we invalidate previous checks here.
1513 * Or, since we know how much we need to make read/writeable,
1514 * this can be done once at the program beginning for direct
1515 * access case. By this we overcome limitations of only current
1516 * headroom being accessible.
1518 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1521 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1522 .func = bpf_skb_pull_data,
1524 .ret_type = RET_INTEGER,
1525 .arg1_type = ARG_PTR_TO_CTX,
1526 .arg2_type = ARG_ANYTHING,
1529 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1530 u64, from, u64, to, u64, flags)
1534 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1536 if (unlikely(offset > 0xffff || offset & 1))
1538 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1541 ptr = (__sum16 *)(skb->data + offset);
1542 switch (flags & BPF_F_HDR_FIELD_MASK) {
1544 if (unlikely(from != 0))
1547 csum_replace_by_diff(ptr, to);
1550 csum_replace2(ptr, from, to);
1553 csum_replace4(ptr, from, to);
1562 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1563 .func = bpf_l3_csum_replace,
1565 .ret_type = RET_INTEGER,
1566 .arg1_type = ARG_PTR_TO_CTX,
1567 .arg2_type = ARG_ANYTHING,
1568 .arg3_type = ARG_ANYTHING,
1569 .arg4_type = ARG_ANYTHING,
1570 .arg5_type = ARG_ANYTHING,
1573 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1574 u64, from, u64, to, u64, flags)
1576 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1577 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1578 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1581 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1582 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1584 if (unlikely(offset > 0xffff || offset & 1))
1586 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1589 ptr = (__sum16 *)(skb->data + offset);
1590 if (is_mmzero && !do_mforce && !*ptr)
1593 switch (flags & BPF_F_HDR_FIELD_MASK) {
1595 if (unlikely(from != 0))
1598 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1601 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1604 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1610 if (is_mmzero && !*ptr)
1611 *ptr = CSUM_MANGLED_0;
1615 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1616 .func = bpf_l4_csum_replace,
1618 .ret_type = RET_INTEGER,
1619 .arg1_type = ARG_PTR_TO_CTX,
1620 .arg2_type = ARG_ANYTHING,
1621 .arg3_type = ARG_ANYTHING,
1622 .arg4_type = ARG_ANYTHING,
1623 .arg5_type = ARG_ANYTHING,
1626 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1627 __be32 *, to, u32, to_size, __wsum, seed)
1629 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1630 u32 diff_size = from_size + to_size;
1633 /* This is quite flexible, some examples:
1635 * from_size == 0, to_size > 0, seed := csum --> pushing data
1636 * from_size > 0, to_size == 0, seed := csum --> pulling data
1637 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1639 * Even for diffing, from_size and to_size don't need to be equal.
1641 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1642 diff_size > sizeof(sp->diff)))
1645 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1646 sp->diff[j] = ~from[i];
1647 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1648 sp->diff[j] = to[i];
1650 return csum_partial(sp->diff, diff_size, seed);
1653 static const struct bpf_func_proto bpf_csum_diff_proto = {
1654 .func = bpf_csum_diff,
1657 .ret_type = RET_INTEGER,
1658 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1659 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1660 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1661 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1662 .arg5_type = ARG_ANYTHING,
1665 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1667 /* The interface is to be used in combination with bpf_csum_diff()
1668 * for direct packet writes. csum rotation for alignment as well
1669 * as emulating csum_sub() can be done from the eBPF program.
1671 if (skb->ip_summed == CHECKSUM_COMPLETE)
1672 return (skb->csum = csum_add(skb->csum, csum));
1677 static const struct bpf_func_proto bpf_csum_update_proto = {
1678 .func = bpf_csum_update,
1680 .ret_type = RET_INTEGER,
1681 .arg1_type = ARG_PTR_TO_CTX,
1682 .arg2_type = ARG_ANYTHING,
1685 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1687 return dev_forward_skb(dev, skb);
1690 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1691 struct sk_buff *skb)
1693 int ret = ____dev_forward_skb(dev, skb);
1697 ret = netif_rx(skb);
1703 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1707 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1708 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1715 __this_cpu_inc(xmit_recursion);
1716 ret = dev_queue_xmit(skb);
1717 __this_cpu_dec(xmit_recursion);
1722 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1725 /* skb->mac_len is not set on normal egress */
1726 unsigned int mlen = skb->network_header - skb->mac_header;
1728 __skb_pull(skb, mlen);
1730 /* At ingress, the mac header has already been pulled once.
1731 * At egress, skb_pospull_rcsum has to be done in case that
1732 * the skb is originated from ingress (i.e. a forwarded skb)
1733 * to ensure that rcsum starts at net header.
1735 if (!skb_at_tc_ingress(skb))
1736 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1737 skb_pop_mac_header(skb);
1738 skb_reset_mac_len(skb);
1739 return flags & BPF_F_INGRESS ?
1740 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1743 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1746 /* Verify that a link layer header is carried */
1747 if (unlikely(skb->mac_header >= skb->network_header)) {
1752 bpf_push_mac_rcsum(skb);
1753 return flags & BPF_F_INGRESS ?
1754 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1757 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1760 if (dev_is_mac_header_xmit(dev))
1761 return __bpf_redirect_common(skb, dev, flags);
1763 return __bpf_redirect_no_mac(skb, dev, flags);
1766 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1768 struct net_device *dev;
1769 struct sk_buff *clone;
1772 if (unlikely(flags & ~(BPF_F_INGRESS)))
1775 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1779 clone = skb_clone(skb, GFP_ATOMIC);
1780 if (unlikely(!clone))
1783 /* For direct write, we need to keep the invariant that the skbs
1784 * we're dealing with need to be uncloned. Should uncloning fail
1785 * here, we need to free the just generated clone to unclone once
1788 ret = bpf_try_make_head_writable(skb);
1789 if (unlikely(ret)) {
1794 return __bpf_redirect(clone, dev, flags);
1797 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1798 .func = bpf_clone_redirect,
1800 .ret_type = RET_INTEGER,
1801 .arg1_type = ARG_PTR_TO_CTX,
1802 .arg2_type = ARG_ANYTHING,
1803 .arg3_type = ARG_ANYTHING,
1806 struct redirect_info {
1809 struct bpf_map *map;
1810 struct bpf_map *map_to_flush;
1811 unsigned long map_owner;
1814 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1816 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1818 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1820 if (unlikely(flags & ~(BPF_F_INGRESS)))
1823 ri->ifindex = ifindex;
1826 return TC_ACT_REDIRECT;
1829 int skb_do_redirect(struct sk_buff *skb)
1831 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1832 struct net_device *dev;
1834 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1836 if (unlikely(!dev)) {
1841 return __bpf_redirect(skb, dev, ri->flags);
1844 static const struct bpf_func_proto bpf_redirect_proto = {
1845 .func = bpf_redirect,
1847 .ret_type = RET_INTEGER,
1848 .arg1_type = ARG_ANYTHING,
1849 .arg2_type = ARG_ANYTHING,
1852 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
1853 struct bpf_map *, map, u32, key, u64, flags)
1855 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1857 /* If user passes invalid input drop the packet. */
1858 if (unlikely(flags))
1862 tcb->bpf.flags = flags;
1868 struct sock *do_sk_redirect_map(struct sk_buff *skb)
1870 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1871 struct sock *sk = NULL;
1874 sk = __sock_map_lookup_elem(tcb->bpf.map, tcb->bpf.key);
1877 tcb->bpf.map = NULL;
1883 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
1884 .func = bpf_sk_redirect_map,
1886 .ret_type = RET_INTEGER,
1887 .arg1_type = ARG_PTR_TO_CTX,
1888 .arg2_type = ARG_CONST_MAP_PTR,
1889 .arg3_type = ARG_ANYTHING,
1890 .arg4_type = ARG_ANYTHING,
1893 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
1895 return task_get_classid(skb);
1898 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1899 .func = bpf_get_cgroup_classid,
1901 .ret_type = RET_INTEGER,
1902 .arg1_type = ARG_PTR_TO_CTX,
1905 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
1907 return dst_tclassid(skb);
1910 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1911 .func = bpf_get_route_realm,
1913 .ret_type = RET_INTEGER,
1914 .arg1_type = ARG_PTR_TO_CTX,
1917 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
1919 /* If skb_clear_hash() was called due to mangling, we can
1920 * trigger SW recalculation here. Later access to hash
1921 * can then use the inline skb->hash via context directly
1922 * instead of calling this helper again.
1924 return skb_get_hash(skb);
1927 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1928 .func = bpf_get_hash_recalc,
1930 .ret_type = RET_INTEGER,
1931 .arg1_type = ARG_PTR_TO_CTX,
1934 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1936 /* After all direct packet write, this can be used once for
1937 * triggering a lazy recalc on next skb_get_hash() invocation.
1939 skb_clear_hash(skb);
1943 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1944 .func = bpf_set_hash_invalid,
1946 .ret_type = RET_INTEGER,
1947 .arg1_type = ARG_PTR_TO_CTX,
1950 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
1952 /* Set user specified hash as L4(+), so that it gets returned
1953 * on skb_get_hash() call unless BPF prog later on triggers a
1956 __skb_set_sw_hash(skb, hash, true);
1960 static const struct bpf_func_proto bpf_set_hash_proto = {
1961 .func = bpf_set_hash,
1963 .ret_type = RET_INTEGER,
1964 .arg1_type = ARG_PTR_TO_CTX,
1965 .arg2_type = ARG_ANYTHING,
1968 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1973 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1974 vlan_proto != htons(ETH_P_8021AD)))
1975 vlan_proto = htons(ETH_P_8021Q);
1977 bpf_push_mac_rcsum(skb);
1978 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1979 bpf_pull_mac_rcsum(skb);
1981 bpf_compute_data_pointers(skb);
1985 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1986 .func = bpf_skb_vlan_push,
1988 .ret_type = RET_INTEGER,
1989 .arg1_type = ARG_PTR_TO_CTX,
1990 .arg2_type = ARG_ANYTHING,
1991 .arg3_type = ARG_ANYTHING,
1993 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1995 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
1999 bpf_push_mac_rcsum(skb);
2000 ret = skb_vlan_pop(skb);
2001 bpf_pull_mac_rcsum(skb);
2003 bpf_compute_data_pointers(skb);
2007 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2008 .func = bpf_skb_vlan_pop,
2010 .ret_type = RET_INTEGER,
2011 .arg1_type = ARG_PTR_TO_CTX,
2013 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
2015 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2017 /* Caller already did skb_cow() with len as headroom,
2018 * so no need to do it here.
2021 memmove(skb->data, skb->data + len, off);
2022 memset(skb->data + off, 0, len);
2024 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2025 * needed here as it does not change the skb->csum
2026 * result for checksum complete when summing over
2032 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2034 /* skb_ensure_writable() is not needed here, as we're
2035 * already working on an uncloned skb.
2037 if (unlikely(!pskb_may_pull(skb, off + len)))
2040 skb_postpull_rcsum(skb, skb->data + off, len);
2041 memmove(skb->data + len, skb->data, off);
2042 __skb_pull(skb, len);
2047 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2049 bool trans_same = skb->transport_header == skb->network_header;
2052 /* There's no need for __skb_push()/__skb_pull() pair to
2053 * get to the start of the mac header as we're guaranteed
2054 * to always start from here under eBPF.
2056 ret = bpf_skb_generic_push(skb, off, len);
2058 skb->mac_header -= len;
2059 skb->network_header -= len;
2061 skb->transport_header = skb->network_header;
2067 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2069 bool trans_same = skb->transport_header == skb->network_header;
2072 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2073 ret = bpf_skb_generic_pop(skb, off, len);
2075 skb->mac_header += len;
2076 skb->network_header += len;
2078 skb->transport_header = skb->network_header;
2084 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2086 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2087 u32 off = skb_mac_header_len(skb);
2090 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2091 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2094 ret = skb_cow(skb, len_diff);
2095 if (unlikely(ret < 0))
2098 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2099 if (unlikely(ret < 0))
2102 if (skb_is_gso(skb)) {
2103 struct skb_shared_info *shinfo = skb_shinfo(skb);
2105 /* SKB_GSO_TCPV4 needs to be changed into
2108 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2109 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2110 shinfo->gso_type |= SKB_GSO_TCPV6;
2113 /* Due to IPv6 header, MSS needs to be downgraded. */
2114 skb_decrease_gso_size(shinfo, len_diff);
2115 /* Header must be checked, and gso_segs recomputed. */
2116 shinfo->gso_type |= SKB_GSO_DODGY;
2117 shinfo->gso_segs = 0;
2120 skb->protocol = htons(ETH_P_IPV6);
2121 skb_clear_hash(skb);
2126 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2128 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2129 u32 off = skb_mac_header_len(skb);
2132 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2133 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2136 ret = skb_unclone(skb, GFP_ATOMIC);
2137 if (unlikely(ret < 0))
2140 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2141 if (unlikely(ret < 0))
2144 if (skb_is_gso(skb)) {
2145 struct skb_shared_info *shinfo = skb_shinfo(skb);
2147 /* SKB_GSO_TCPV6 needs to be changed into
2150 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2151 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2152 shinfo->gso_type |= SKB_GSO_TCPV4;
2155 /* Due to IPv4 header, MSS can be upgraded. */
2156 skb_increase_gso_size(shinfo, len_diff);
2157 /* Header must be checked, and gso_segs recomputed. */
2158 shinfo->gso_type |= SKB_GSO_DODGY;
2159 shinfo->gso_segs = 0;
2162 skb->protocol = htons(ETH_P_IP);
2163 skb_clear_hash(skb);
2168 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2170 __be16 from_proto = skb->protocol;
2172 if (from_proto == htons(ETH_P_IP) &&
2173 to_proto == htons(ETH_P_IPV6))
2174 return bpf_skb_proto_4_to_6(skb);
2176 if (from_proto == htons(ETH_P_IPV6) &&
2177 to_proto == htons(ETH_P_IP))
2178 return bpf_skb_proto_6_to_4(skb);
2183 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2188 if (unlikely(flags))
2191 /* General idea is that this helper does the basic groundwork
2192 * needed for changing the protocol, and eBPF program fills the
2193 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2194 * and other helpers, rather than passing a raw buffer here.
2196 * The rationale is to keep this minimal and without a need to
2197 * deal with raw packet data. F.e. even if we would pass buffers
2198 * here, the program still needs to call the bpf_lX_csum_replace()
2199 * helpers anyway. Plus, this way we keep also separation of
2200 * concerns, since f.e. bpf_skb_store_bytes() should only take
2203 * Currently, additional options and extension header space are
2204 * not supported, but flags register is reserved so we can adapt
2205 * that. For offloads, we mark packet as dodgy, so that headers
2206 * need to be verified first.
2208 ret = bpf_skb_proto_xlat(skb, proto);
2209 bpf_compute_data_pointers(skb);
2213 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2214 .func = bpf_skb_change_proto,
2216 .ret_type = RET_INTEGER,
2217 .arg1_type = ARG_PTR_TO_CTX,
2218 .arg2_type = ARG_ANYTHING,
2219 .arg3_type = ARG_ANYTHING,
2222 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2224 /* We only allow a restricted subset to be changed for now. */
2225 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2226 !skb_pkt_type_ok(pkt_type)))
2229 skb->pkt_type = pkt_type;
2233 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2234 .func = bpf_skb_change_type,
2236 .ret_type = RET_INTEGER,
2237 .arg1_type = ARG_PTR_TO_CTX,
2238 .arg2_type = ARG_ANYTHING,
2241 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2243 switch (skb->protocol) {
2244 case htons(ETH_P_IP):
2245 return sizeof(struct iphdr);
2246 case htons(ETH_P_IPV6):
2247 return sizeof(struct ipv6hdr);
2253 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2255 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2258 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2259 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2262 ret = skb_cow(skb, len_diff);
2263 if (unlikely(ret < 0))
2266 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2267 if (unlikely(ret < 0))
2270 if (skb_is_gso(skb)) {
2271 struct skb_shared_info *shinfo = skb_shinfo(skb);
2273 /* Due to header grow, MSS needs to be downgraded. */
2274 skb_decrease_gso_size(shinfo, len_diff);
2275 /* Header must be checked, and gso_segs recomputed. */
2276 shinfo->gso_type |= SKB_GSO_DODGY;
2277 shinfo->gso_segs = 0;
2283 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2285 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2288 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2289 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2292 ret = skb_unclone(skb, GFP_ATOMIC);
2293 if (unlikely(ret < 0))
2296 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2297 if (unlikely(ret < 0))
2300 if (skb_is_gso(skb)) {
2301 struct skb_shared_info *shinfo = skb_shinfo(skb);
2303 /* Due to header shrink, MSS can be upgraded. */
2304 skb_increase_gso_size(shinfo, len_diff);
2305 /* Header must be checked, and gso_segs recomputed. */
2306 shinfo->gso_type |= SKB_GSO_DODGY;
2307 shinfo->gso_segs = 0;
2313 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2315 return skb->dev->mtu + skb->dev->hard_header_len;
2318 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2320 bool trans_same = skb->transport_header == skb->network_header;
2321 u32 len_cur, len_diff_abs = abs(len_diff);
2322 u32 len_min = bpf_skb_net_base_len(skb);
2323 u32 len_max = __bpf_skb_max_len(skb);
2324 __be16 proto = skb->protocol;
2325 bool shrink = len_diff < 0;
2328 if (unlikely(len_diff_abs > 0xfffU))
2330 if (unlikely(proto != htons(ETH_P_IP) &&
2331 proto != htons(ETH_P_IPV6)))
2334 len_cur = skb->len - skb_network_offset(skb);
2335 if (skb_transport_header_was_set(skb) && !trans_same)
2336 len_cur = skb_network_header_len(skb);
2337 if ((shrink && (len_diff_abs >= len_cur ||
2338 len_cur - len_diff_abs < len_min)) ||
2339 (!shrink && (skb->len + len_diff_abs > len_max &&
2343 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2344 bpf_skb_net_grow(skb, len_diff_abs);
2346 bpf_compute_data_pointers(skb);
2350 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2351 u32, mode, u64, flags)
2353 if (unlikely(flags))
2355 if (likely(mode == BPF_ADJ_ROOM_NET))
2356 return bpf_skb_adjust_net(skb, len_diff);
2361 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2362 .func = bpf_skb_adjust_room,
2364 .ret_type = RET_INTEGER,
2365 .arg1_type = ARG_PTR_TO_CTX,
2366 .arg2_type = ARG_ANYTHING,
2367 .arg3_type = ARG_ANYTHING,
2368 .arg4_type = ARG_ANYTHING,
2371 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2373 u32 min_len = skb_network_offset(skb);
2375 if (skb_transport_header_was_set(skb))
2376 min_len = skb_transport_offset(skb);
2377 if (skb->ip_summed == CHECKSUM_PARTIAL)
2378 min_len = skb_checksum_start_offset(skb) +
2379 skb->csum_offset + sizeof(__sum16);
2383 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2385 unsigned int old_len = skb->len;
2388 ret = __skb_grow_rcsum(skb, new_len);
2390 memset(skb->data + old_len, 0, new_len - old_len);
2394 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2396 return __skb_trim_rcsum(skb, new_len);
2399 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2402 u32 max_len = __bpf_skb_max_len(skb);
2403 u32 min_len = __bpf_skb_min_len(skb);
2406 if (unlikely(flags || new_len > max_len || new_len < min_len))
2408 if (skb->encapsulation)
2411 /* The basic idea of this helper is that it's performing the
2412 * needed work to either grow or trim an skb, and eBPF program
2413 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2414 * bpf_lX_csum_replace() and others rather than passing a raw
2415 * buffer here. This one is a slow path helper and intended
2416 * for replies with control messages.
2418 * Like in bpf_skb_change_proto(), we want to keep this rather
2419 * minimal and without protocol specifics so that we are able
2420 * to separate concerns as in bpf_skb_store_bytes() should only
2421 * be the one responsible for writing buffers.
2423 * It's really expected to be a slow path operation here for
2424 * control message replies, so we're implicitly linearizing,
2425 * uncloning and drop offloads from the skb by this.
2427 ret = __bpf_try_make_writable(skb, skb->len);
2429 if (new_len > skb->len)
2430 ret = bpf_skb_grow_rcsum(skb, new_len);
2431 else if (new_len < skb->len)
2432 ret = bpf_skb_trim_rcsum(skb, new_len);
2433 if (!ret && skb_is_gso(skb))
2437 bpf_compute_data_pointers(skb);
2441 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2442 .func = bpf_skb_change_tail,
2444 .ret_type = RET_INTEGER,
2445 .arg1_type = ARG_PTR_TO_CTX,
2446 .arg2_type = ARG_ANYTHING,
2447 .arg3_type = ARG_ANYTHING,
2450 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2453 u32 max_len = __bpf_skb_max_len(skb);
2454 u32 new_len = skb->len + head_room;
2457 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2458 new_len < skb->len))
2461 ret = skb_cow(skb, head_room);
2463 /* Idea for this helper is that we currently only
2464 * allow to expand on mac header. This means that
2465 * skb->protocol network header, etc, stay as is.
2466 * Compared to bpf_skb_change_tail(), we're more
2467 * flexible due to not needing to linearize or
2468 * reset GSO. Intention for this helper is to be
2469 * used by an L3 skb that needs to push mac header
2470 * for redirection into L2 device.
2472 __skb_push(skb, head_room);
2473 memset(skb->data, 0, head_room);
2474 skb_reset_mac_header(skb);
2477 bpf_compute_data_pointers(skb);
2481 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2482 .func = bpf_skb_change_head,
2484 .ret_type = RET_INTEGER,
2485 .arg1_type = ARG_PTR_TO_CTX,
2486 .arg2_type = ARG_ANYTHING,
2487 .arg3_type = ARG_ANYTHING,
2490 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
2492 return xdp_data_meta_unsupported(xdp) ? 0 :
2493 xdp->data - xdp->data_meta;
2496 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2498 unsigned long metalen = xdp_get_metalen(xdp);
2499 void *data_start = xdp->data_hard_start + metalen;
2500 void *data = xdp->data + offset;
2502 if (unlikely(data < data_start ||
2503 data > xdp->data_end - ETH_HLEN))
2507 memmove(xdp->data_meta + offset,
2508 xdp->data_meta, metalen);
2509 xdp->data_meta += offset;
2515 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2516 .func = bpf_xdp_adjust_head,
2518 .ret_type = RET_INTEGER,
2519 .arg1_type = ARG_PTR_TO_CTX,
2520 .arg2_type = ARG_ANYTHING,
2523 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
2525 void *meta = xdp->data_meta + offset;
2526 unsigned long metalen = xdp->data - meta;
2528 if (xdp_data_meta_unsupported(xdp))
2530 if (unlikely(meta < xdp->data_hard_start ||
2533 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
2537 xdp->data_meta = meta;
2542 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
2543 .func = bpf_xdp_adjust_meta,
2545 .ret_type = RET_INTEGER,
2546 .arg1_type = ARG_PTR_TO_CTX,
2547 .arg2_type = ARG_ANYTHING,
2550 static int __bpf_tx_xdp(struct net_device *dev,
2551 struct bpf_map *map,
2552 struct xdp_buff *xdp,
2557 if (!dev->netdev_ops->ndo_xdp_xmit) {
2561 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
2564 dev->netdev_ops->ndo_xdp_flush(dev);
2568 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
2569 struct bpf_map *map,
2570 struct xdp_buff *xdp,
2575 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
2576 struct net_device *dev = fwd;
2578 if (!dev->netdev_ops->ndo_xdp_xmit)
2581 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
2584 __dev_map_insert_ctx(map, index);
2586 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
2587 struct bpf_cpu_map_entry *rcpu = fwd;
2589 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
2592 __cpu_map_insert_ctx(map, index);
2597 void xdp_do_flush_map(void)
2599 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2600 struct bpf_map *map = ri->map_to_flush;
2602 ri->map_to_flush = NULL;
2604 switch (map->map_type) {
2605 case BPF_MAP_TYPE_DEVMAP:
2606 __dev_map_flush(map);
2608 case BPF_MAP_TYPE_CPUMAP:
2609 __cpu_map_flush(map);
2616 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
2618 static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
2620 switch (map->map_type) {
2621 case BPF_MAP_TYPE_DEVMAP:
2622 return __dev_map_lookup_elem(map, index);
2623 case BPF_MAP_TYPE_CPUMAP:
2624 return __cpu_map_lookup_elem(map, index);
2630 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
2633 return (unsigned long)xdp_prog->aux != aux;
2636 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
2637 struct bpf_prog *xdp_prog)
2639 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2640 unsigned long map_owner = ri->map_owner;
2641 struct bpf_map *map = ri->map;
2642 u32 index = ri->ifindex;
2650 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2656 fwd = __xdp_map_lookup_elem(map, index);
2661 if (ri->map_to_flush && ri->map_to_flush != map)
2664 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
2668 ri->map_to_flush = map;
2669 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
2672 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
2676 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
2677 struct bpf_prog *xdp_prog)
2679 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2680 struct net_device *fwd;
2681 u32 index = ri->ifindex;
2685 return xdp_do_redirect_map(dev, xdp, xdp_prog);
2687 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2689 if (unlikely(!fwd)) {
2694 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
2698 _trace_xdp_redirect(dev, xdp_prog, index);
2701 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2704 EXPORT_SYMBOL_GPL(xdp_do_redirect);
2706 static int __xdp_generic_ok_fwd_dev(struct sk_buff *skb, struct net_device *fwd)
2710 if (unlikely(!(fwd->flags & IFF_UP)))
2713 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
2720 static int xdp_do_generic_redirect_map(struct net_device *dev,
2721 struct sk_buff *skb,
2722 struct bpf_prog *xdp_prog)
2724 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2725 unsigned long map_owner = ri->map_owner;
2726 struct bpf_map *map = ri->map;
2727 struct net_device *fwd = NULL;
2728 u32 index = ri->ifindex;
2735 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2740 fwd = __xdp_map_lookup_elem(map, index);
2741 if (unlikely(!fwd)) {
2746 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
2747 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
2751 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
2756 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
2759 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
2763 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
2764 struct bpf_prog *xdp_prog)
2766 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2767 u32 index = ri->ifindex;
2768 struct net_device *fwd;
2772 return xdp_do_generic_redirect_map(dev, skb, xdp_prog);
2775 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2776 if (unlikely(!fwd)) {
2781 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
2785 _trace_xdp_redirect(dev, xdp_prog, index);
2788 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2791 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
2793 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
2795 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2797 if (unlikely(flags))
2800 ri->ifindex = ifindex;
2805 return XDP_REDIRECT;
2808 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
2809 .func = bpf_xdp_redirect,
2811 .ret_type = RET_INTEGER,
2812 .arg1_type = ARG_ANYTHING,
2813 .arg2_type = ARG_ANYTHING,
2816 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
2817 unsigned long, map_owner)
2819 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2821 if (unlikely(flags))
2824 ri->ifindex = ifindex;
2827 ri->map_owner = map_owner;
2829 return XDP_REDIRECT;
2832 /* Note, arg4 is hidden from users and populated by the verifier
2833 * with the right pointer.
2835 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
2836 .func = bpf_xdp_redirect_map,
2838 .ret_type = RET_INTEGER,
2839 .arg1_type = ARG_CONST_MAP_PTR,
2840 .arg2_type = ARG_ANYTHING,
2841 .arg3_type = ARG_ANYTHING,
2844 bool bpf_helper_changes_pkt_data(void *func)
2846 if (func == bpf_skb_vlan_push ||
2847 func == bpf_skb_vlan_pop ||
2848 func == bpf_skb_store_bytes ||
2849 func == bpf_skb_change_proto ||
2850 func == bpf_skb_change_head ||
2851 func == bpf_skb_change_tail ||
2852 func == bpf_skb_adjust_room ||
2853 func == bpf_skb_pull_data ||
2854 func == bpf_clone_redirect ||
2855 func == bpf_l3_csum_replace ||
2856 func == bpf_l4_csum_replace ||
2857 func == bpf_xdp_adjust_head ||
2858 func == bpf_xdp_adjust_meta)
2864 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2865 unsigned long off, unsigned long len)
2867 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
2871 if (ptr != dst_buff)
2872 memcpy(dst_buff, ptr, len);
2877 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2878 u64, flags, void *, meta, u64, meta_size)
2880 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2882 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2884 if (unlikely(skb_size > skb->len))
2887 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2891 static const struct bpf_func_proto bpf_skb_event_output_proto = {
2892 .func = bpf_skb_event_output,
2894 .ret_type = RET_INTEGER,
2895 .arg1_type = ARG_PTR_TO_CTX,
2896 .arg2_type = ARG_CONST_MAP_PTR,
2897 .arg3_type = ARG_ANYTHING,
2898 .arg4_type = ARG_PTR_TO_MEM,
2899 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
2902 static unsigned short bpf_tunnel_key_af(u64 flags)
2904 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2907 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2908 u32, size, u64, flags)
2910 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2911 u8 compat[sizeof(struct bpf_tunnel_key)];
2915 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2919 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2923 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2926 case offsetof(struct bpf_tunnel_key, tunnel_label):
2927 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2929 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2930 /* Fixup deprecated structure layouts here, so we have
2931 * a common path later on.
2933 if (ip_tunnel_info_af(info) != AF_INET)
2936 to = (struct bpf_tunnel_key *)compat;
2943 to->tunnel_id = be64_to_cpu(info->key.tun_id);
2944 to->tunnel_tos = info->key.tos;
2945 to->tunnel_ttl = info->key.ttl;
2947 if (flags & BPF_F_TUNINFO_IPV6) {
2948 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2949 sizeof(to->remote_ipv6));
2950 to->tunnel_label = be32_to_cpu(info->key.label);
2952 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
2955 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
2956 memcpy(to_orig, to, size);
2960 memset(to_orig, 0, size);
2964 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
2965 .func = bpf_skb_get_tunnel_key,
2967 .ret_type = RET_INTEGER,
2968 .arg1_type = ARG_PTR_TO_CTX,
2969 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2970 .arg3_type = ARG_CONST_SIZE,
2971 .arg4_type = ARG_ANYTHING,
2974 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
2976 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2979 if (unlikely(!info ||
2980 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2984 if (unlikely(size < info->options_len)) {
2989 ip_tunnel_info_opts_get(to, info);
2990 if (size > info->options_len)
2991 memset(to + info->options_len, 0, size - info->options_len);
2993 return info->options_len;
2995 memset(to, 0, size);
2999 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3000 .func = bpf_skb_get_tunnel_opt,
3002 .ret_type = RET_INTEGER,
3003 .arg1_type = ARG_PTR_TO_CTX,
3004 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3005 .arg3_type = ARG_CONST_SIZE,
3008 static struct metadata_dst __percpu *md_dst;
3010 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3011 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3013 struct metadata_dst *md = this_cpu_ptr(md_dst);
3014 u8 compat[sizeof(struct bpf_tunnel_key)];
3015 struct ip_tunnel_info *info;
3017 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3018 BPF_F_DONT_FRAGMENT)))
3020 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3022 case offsetof(struct bpf_tunnel_key, tunnel_label):
3023 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3024 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3025 /* Fixup deprecated structure layouts here, so we have
3026 * a common path later on.
3028 memcpy(compat, from, size);
3029 memset(compat + size, 0, sizeof(compat) - size);
3030 from = (const struct bpf_tunnel_key *) compat;
3036 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3041 dst_hold((struct dst_entry *) md);
3042 skb_dst_set(skb, (struct dst_entry *) md);
3044 info = &md->u.tun_info;
3045 info->mode = IP_TUNNEL_INFO_TX;
3047 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3048 if (flags & BPF_F_DONT_FRAGMENT)
3049 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3050 if (flags & BPF_F_ZERO_CSUM_TX)
3051 info->key.tun_flags &= ~TUNNEL_CSUM;
3053 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3054 info->key.tos = from->tunnel_tos;
3055 info->key.ttl = from->tunnel_ttl;
3057 if (flags & BPF_F_TUNINFO_IPV6) {
3058 info->mode |= IP_TUNNEL_INFO_IPV6;
3059 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3060 sizeof(from->remote_ipv6));
3061 info->key.label = cpu_to_be32(from->tunnel_label) &
3062 IPV6_FLOWLABEL_MASK;
3064 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3070 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3071 .func = bpf_skb_set_tunnel_key,
3073 .ret_type = RET_INTEGER,
3074 .arg1_type = ARG_PTR_TO_CTX,
3075 .arg2_type = ARG_PTR_TO_MEM,
3076 .arg3_type = ARG_CONST_SIZE,
3077 .arg4_type = ARG_ANYTHING,
3080 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3081 const u8 *, from, u32, size)
3083 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3084 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3086 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3088 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3091 ip_tunnel_info_opts_set(info, from, size);
3096 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3097 .func = bpf_skb_set_tunnel_opt,
3099 .ret_type = RET_INTEGER,
3100 .arg1_type = ARG_PTR_TO_CTX,
3101 .arg2_type = ARG_PTR_TO_MEM,
3102 .arg3_type = ARG_CONST_SIZE,
3105 static const struct bpf_func_proto *
3106 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3109 struct metadata_dst __percpu *tmp;
3111 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3116 if (cmpxchg(&md_dst, NULL, tmp))
3117 metadata_dst_free_percpu(tmp);
3121 case BPF_FUNC_skb_set_tunnel_key:
3122 return &bpf_skb_set_tunnel_key_proto;
3123 case BPF_FUNC_skb_set_tunnel_opt:
3124 return &bpf_skb_set_tunnel_opt_proto;
3130 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3133 struct bpf_array *array = container_of(map, struct bpf_array, map);
3134 struct cgroup *cgrp;
3137 sk = skb_to_full_sk(skb);
3138 if (!sk || !sk_fullsock(sk))
3140 if (unlikely(idx >= array->map.max_entries))
3143 cgrp = READ_ONCE(array->ptrs[idx]);
3144 if (unlikely(!cgrp))
3147 return sk_under_cgroup_hierarchy(sk, cgrp);
3150 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3151 .func = bpf_skb_under_cgroup,
3153 .ret_type = RET_INTEGER,
3154 .arg1_type = ARG_PTR_TO_CTX,
3155 .arg2_type = ARG_CONST_MAP_PTR,
3156 .arg3_type = ARG_ANYTHING,
3159 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3160 unsigned long off, unsigned long len)
3162 memcpy(dst_buff, src_buff + off, len);
3166 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3167 u64, flags, void *, meta, u64, meta_size)
3169 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3171 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3173 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3176 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3177 xdp_size, bpf_xdp_copy);
3180 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3181 .func = bpf_xdp_event_output,
3183 .ret_type = RET_INTEGER,
3184 .arg1_type = ARG_PTR_TO_CTX,
3185 .arg2_type = ARG_CONST_MAP_PTR,
3186 .arg3_type = ARG_ANYTHING,
3187 .arg4_type = ARG_PTR_TO_MEM,
3188 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3191 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3193 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3196 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3197 .func = bpf_get_socket_cookie,
3199 .ret_type = RET_INTEGER,
3200 .arg1_type = ARG_PTR_TO_CTX,
3203 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3205 struct sock *sk = sk_to_full_sk(skb->sk);
3208 if (!sk || !sk_fullsock(sk))
3210 kuid = sock_net_uid(sock_net(sk), sk);
3211 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3214 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3215 .func = bpf_get_socket_uid,
3217 .ret_type = RET_INTEGER,
3218 .arg1_type = ARG_PTR_TO_CTX,
3221 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3222 int, level, int, optname, char *, optval, int, optlen)
3224 struct sock *sk = bpf_sock->sk;
3228 if (!sk_fullsock(sk))
3231 if (level == SOL_SOCKET) {
3232 if (optlen != sizeof(int))
3234 val = *((int *)optval);
3236 /* Only some socketops are supported */
3239 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3240 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3243 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3244 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3246 case SO_MAX_PACING_RATE:
3247 sk->sk_max_pacing_rate = val;
3248 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3249 sk->sk_max_pacing_rate);
3252 sk->sk_priority = val;
3257 sk->sk_rcvlowat = val ? : 1;
3266 #if IS_ENABLED(CONFIG_IPV6)
3267 } else if (level == SOL_IPV6) {
3268 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3271 val = *((int *)optval);
3272 /* Only some options are supported */
3275 if (val < -1 || val > 0xff) {
3278 struct ipv6_pinfo *np = inet6_sk(sk);
3289 } else if (level == SOL_TCP &&
3290 sk->sk_prot->setsockopt == tcp_setsockopt) {
3291 if (optname == TCP_CONGESTION) {
3292 char name[TCP_CA_NAME_MAX];
3293 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3295 strncpy(name, optval, min_t(long, optlen,
3296 TCP_CA_NAME_MAX-1));
3297 name[TCP_CA_NAME_MAX-1] = 0;
3298 ret = tcp_set_congestion_control(sk, name, false,
3301 struct tcp_sock *tp = tcp_sk(sk);
3303 if (optlen != sizeof(int))
3306 val = *((int *)optval);
3307 /* Only some options are supported */
3310 if (val <= 0 || tp->data_segs_out > 0)
3315 case TCP_BPF_SNDCWND_CLAMP:
3319 tp->snd_cwnd_clamp = val;
3320 tp->snd_ssthresh = val;
3334 static const struct bpf_func_proto bpf_setsockopt_proto = {
3335 .func = bpf_setsockopt,
3337 .ret_type = RET_INTEGER,
3338 .arg1_type = ARG_PTR_TO_CTX,
3339 .arg2_type = ARG_ANYTHING,
3340 .arg3_type = ARG_ANYTHING,
3341 .arg4_type = ARG_PTR_TO_MEM,
3342 .arg5_type = ARG_CONST_SIZE,
3345 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3346 int, level, int, optname, char *, optval, int, optlen)
3348 struct sock *sk = bpf_sock->sk;
3350 if (!sk_fullsock(sk))
3354 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3355 if (optname == TCP_CONGESTION) {
3356 struct inet_connection_sock *icsk = inet_csk(sk);
3358 if (!icsk->icsk_ca_ops || optlen <= 1)
3360 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3361 optval[optlen - 1] = 0;
3365 #if IS_ENABLED(CONFIG_IPV6)
3366 } else if (level == SOL_IPV6) {
3367 struct ipv6_pinfo *np = inet6_sk(sk);
3369 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3372 /* Only some options are supported */
3375 *((int *)optval) = (int)np->tclass;
3387 memset(optval, 0, optlen);
3391 static const struct bpf_func_proto bpf_getsockopt_proto = {
3392 .func = bpf_getsockopt,
3394 .ret_type = RET_INTEGER,
3395 .arg1_type = ARG_PTR_TO_CTX,
3396 .arg2_type = ARG_ANYTHING,
3397 .arg3_type = ARG_ANYTHING,
3398 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
3399 .arg5_type = ARG_CONST_SIZE,
3402 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
3405 struct sock *sk = bpf_sock->sk;
3406 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
3408 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
3412 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
3414 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
3417 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
3418 .func = bpf_sock_ops_cb_flags_set,
3420 .ret_type = RET_INTEGER,
3421 .arg1_type = ARG_PTR_TO_CTX,
3422 .arg2_type = ARG_ANYTHING,
3425 static const struct bpf_func_proto *
3426 bpf_base_func_proto(enum bpf_func_id func_id)
3429 case BPF_FUNC_map_lookup_elem:
3430 return &bpf_map_lookup_elem_proto;
3431 case BPF_FUNC_map_update_elem:
3432 return &bpf_map_update_elem_proto;
3433 case BPF_FUNC_map_delete_elem:
3434 return &bpf_map_delete_elem_proto;
3435 case BPF_FUNC_get_prandom_u32:
3436 return &bpf_get_prandom_u32_proto;
3437 case BPF_FUNC_get_smp_processor_id:
3438 return &bpf_get_raw_smp_processor_id_proto;
3439 case BPF_FUNC_get_numa_node_id:
3440 return &bpf_get_numa_node_id_proto;
3441 case BPF_FUNC_tail_call:
3442 return &bpf_tail_call_proto;
3443 case BPF_FUNC_ktime_get_ns:
3444 return &bpf_ktime_get_ns_proto;
3445 case BPF_FUNC_trace_printk:
3446 if (capable(CAP_SYS_ADMIN))
3447 return bpf_get_trace_printk_proto();
3453 static const struct bpf_func_proto *
3454 sock_filter_func_proto(enum bpf_func_id func_id)
3457 /* inet and inet6 sockets are created in a process
3458 * context so there is always a valid uid/gid
3460 case BPF_FUNC_get_current_uid_gid:
3461 return &bpf_get_current_uid_gid_proto;
3463 return bpf_base_func_proto(func_id);
3467 static const struct bpf_func_proto *
3468 sk_filter_func_proto(enum bpf_func_id func_id)
3471 case BPF_FUNC_skb_load_bytes:
3472 return &bpf_skb_load_bytes_proto;
3473 case BPF_FUNC_get_socket_cookie:
3474 return &bpf_get_socket_cookie_proto;
3475 case BPF_FUNC_get_socket_uid:
3476 return &bpf_get_socket_uid_proto;
3478 return bpf_base_func_proto(func_id);
3482 static const struct bpf_func_proto *
3483 tc_cls_act_func_proto(enum bpf_func_id func_id)
3486 case BPF_FUNC_skb_store_bytes:
3487 return &bpf_skb_store_bytes_proto;
3488 case BPF_FUNC_skb_load_bytes:
3489 return &bpf_skb_load_bytes_proto;
3490 case BPF_FUNC_skb_pull_data:
3491 return &bpf_skb_pull_data_proto;
3492 case BPF_FUNC_csum_diff:
3493 return &bpf_csum_diff_proto;
3494 case BPF_FUNC_csum_update:
3495 return &bpf_csum_update_proto;
3496 case BPF_FUNC_l3_csum_replace:
3497 return &bpf_l3_csum_replace_proto;
3498 case BPF_FUNC_l4_csum_replace:
3499 return &bpf_l4_csum_replace_proto;
3500 case BPF_FUNC_clone_redirect:
3501 return &bpf_clone_redirect_proto;
3502 case BPF_FUNC_get_cgroup_classid:
3503 return &bpf_get_cgroup_classid_proto;
3504 case BPF_FUNC_skb_vlan_push:
3505 return &bpf_skb_vlan_push_proto;
3506 case BPF_FUNC_skb_vlan_pop:
3507 return &bpf_skb_vlan_pop_proto;
3508 case BPF_FUNC_skb_change_proto:
3509 return &bpf_skb_change_proto_proto;
3510 case BPF_FUNC_skb_change_type:
3511 return &bpf_skb_change_type_proto;
3512 case BPF_FUNC_skb_adjust_room:
3513 return &bpf_skb_adjust_room_proto;
3514 case BPF_FUNC_skb_change_tail:
3515 return &bpf_skb_change_tail_proto;
3516 case BPF_FUNC_skb_get_tunnel_key:
3517 return &bpf_skb_get_tunnel_key_proto;
3518 case BPF_FUNC_skb_set_tunnel_key:
3519 return bpf_get_skb_set_tunnel_proto(func_id);
3520 case BPF_FUNC_skb_get_tunnel_opt:
3521 return &bpf_skb_get_tunnel_opt_proto;
3522 case BPF_FUNC_skb_set_tunnel_opt:
3523 return bpf_get_skb_set_tunnel_proto(func_id);
3524 case BPF_FUNC_redirect:
3525 return &bpf_redirect_proto;
3526 case BPF_FUNC_get_route_realm:
3527 return &bpf_get_route_realm_proto;
3528 case BPF_FUNC_get_hash_recalc:
3529 return &bpf_get_hash_recalc_proto;
3530 case BPF_FUNC_set_hash_invalid:
3531 return &bpf_set_hash_invalid_proto;
3532 case BPF_FUNC_set_hash:
3533 return &bpf_set_hash_proto;
3534 case BPF_FUNC_perf_event_output:
3535 return &bpf_skb_event_output_proto;
3536 case BPF_FUNC_get_smp_processor_id:
3537 return &bpf_get_smp_processor_id_proto;
3538 case BPF_FUNC_skb_under_cgroup:
3539 return &bpf_skb_under_cgroup_proto;
3540 case BPF_FUNC_get_socket_cookie:
3541 return &bpf_get_socket_cookie_proto;
3542 case BPF_FUNC_get_socket_uid:
3543 return &bpf_get_socket_uid_proto;
3545 return bpf_base_func_proto(func_id);
3549 static const struct bpf_func_proto *
3550 xdp_func_proto(enum bpf_func_id func_id)
3553 case BPF_FUNC_perf_event_output:
3554 return &bpf_xdp_event_output_proto;
3555 case BPF_FUNC_get_smp_processor_id:
3556 return &bpf_get_smp_processor_id_proto;
3557 case BPF_FUNC_csum_diff:
3558 return &bpf_csum_diff_proto;
3559 case BPF_FUNC_xdp_adjust_head:
3560 return &bpf_xdp_adjust_head_proto;
3561 case BPF_FUNC_xdp_adjust_meta:
3562 return &bpf_xdp_adjust_meta_proto;
3563 case BPF_FUNC_redirect:
3564 return &bpf_xdp_redirect_proto;
3565 case BPF_FUNC_redirect_map:
3566 return &bpf_xdp_redirect_map_proto;
3568 return bpf_base_func_proto(func_id);
3572 static const struct bpf_func_proto *
3573 lwt_inout_func_proto(enum bpf_func_id func_id)
3576 case BPF_FUNC_skb_load_bytes:
3577 return &bpf_skb_load_bytes_proto;
3578 case BPF_FUNC_skb_pull_data:
3579 return &bpf_skb_pull_data_proto;
3580 case BPF_FUNC_csum_diff:
3581 return &bpf_csum_diff_proto;
3582 case BPF_FUNC_get_cgroup_classid:
3583 return &bpf_get_cgroup_classid_proto;
3584 case BPF_FUNC_get_route_realm:
3585 return &bpf_get_route_realm_proto;
3586 case BPF_FUNC_get_hash_recalc:
3587 return &bpf_get_hash_recalc_proto;
3588 case BPF_FUNC_perf_event_output:
3589 return &bpf_skb_event_output_proto;
3590 case BPF_FUNC_get_smp_processor_id:
3591 return &bpf_get_smp_processor_id_proto;
3592 case BPF_FUNC_skb_under_cgroup:
3593 return &bpf_skb_under_cgroup_proto;
3595 return bpf_base_func_proto(func_id);
3599 static const struct bpf_func_proto *
3600 sock_ops_func_proto(enum bpf_func_id func_id)
3603 case BPF_FUNC_setsockopt:
3604 return &bpf_setsockopt_proto;
3605 case BPF_FUNC_getsockopt:
3606 return &bpf_getsockopt_proto;
3607 case BPF_FUNC_sock_ops_cb_flags_set:
3608 return &bpf_sock_ops_cb_flags_set_proto;
3609 case BPF_FUNC_sock_map_update:
3610 return &bpf_sock_map_update_proto;
3612 return bpf_base_func_proto(func_id);
3616 static const struct bpf_func_proto *sk_skb_func_proto(enum bpf_func_id func_id)
3619 case BPF_FUNC_skb_store_bytes:
3620 return &bpf_skb_store_bytes_proto;
3621 case BPF_FUNC_skb_load_bytes:
3622 return &bpf_skb_load_bytes_proto;
3623 case BPF_FUNC_skb_pull_data:
3624 return &bpf_skb_pull_data_proto;
3625 case BPF_FUNC_skb_change_tail:
3626 return &bpf_skb_change_tail_proto;
3627 case BPF_FUNC_skb_change_head:
3628 return &bpf_skb_change_head_proto;
3629 case BPF_FUNC_get_socket_cookie:
3630 return &bpf_get_socket_cookie_proto;
3631 case BPF_FUNC_get_socket_uid:
3632 return &bpf_get_socket_uid_proto;
3633 case BPF_FUNC_sk_redirect_map:
3634 return &bpf_sk_redirect_map_proto;
3636 return bpf_base_func_proto(func_id);
3640 static const struct bpf_func_proto *
3641 lwt_xmit_func_proto(enum bpf_func_id func_id)
3644 case BPF_FUNC_skb_get_tunnel_key:
3645 return &bpf_skb_get_tunnel_key_proto;
3646 case BPF_FUNC_skb_set_tunnel_key:
3647 return bpf_get_skb_set_tunnel_proto(func_id);
3648 case BPF_FUNC_skb_get_tunnel_opt:
3649 return &bpf_skb_get_tunnel_opt_proto;
3650 case BPF_FUNC_skb_set_tunnel_opt:
3651 return bpf_get_skb_set_tunnel_proto(func_id);
3652 case BPF_FUNC_redirect:
3653 return &bpf_redirect_proto;
3654 case BPF_FUNC_clone_redirect:
3655 return &bpf_clone_redirect_proto;
3656 case BPF_FUNC_skb_change_tail:
3657 return &bpf_skb_change_tail_proto;
3658 case BPF_FUNC_skb_change_head:
3659 return &bpf_skb_change_head_proto;
3660 case BPF_FUNC_skb_store_bytes:
3661 return &bpf_skb_store_bytes_proto;
3662 case BPF_FUNC_csum_update:
3663 return &bpf_csum_update_proto;
3664 case BPF_FUNC_l3_csum_replace:
3665 return &bpf_l3_csum_replace_proto;
3666 case BPF_FUNC_l4_csum_replace:
3667 return &bpf_l4_csum_replace_proto;
3668 case BPF_FUNC_set_hash_invalid:
3669 return &bpf_set_hash_invalid_proto;
3671 return lwt_inout_func_proto(func_id);
3675 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
3676 struct bpf_insn_access_aux *info)
3678 const int size_default = sizeof(__u32);
3680 if (off < 0 || off >= sizeof(struct __sk_buff))
3683 /* The verifier guarantees that size > 0. */
3684 if (off % size != 0)
3688 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3689 if (off + size > offsetofend(struct __sk_buff, cb[4]))
3692 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
3693 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
3694 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
3695 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
3696 case bpf_ctx_range(struct __sk_buff, data):
3697 case bpf_ctx_range(struct __sk_buff, data_meta):
3698 case bpf_ctx_range(struct __sk_buff, data_end):
3699 if (size != size_default)
3703 /* Only narrow read access allowed for now. */
3704 if (type == BPF_WRITE) {
3705 if (size != size_default)
3708 bpf_ctx_record_field_size(info, size_default);
3709 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
3717 static bool sk_filter_is_valid_access(int off, int size,
3718 enum bpf_access_type type,
3719 struct bpf_insn_access_aux *info)
3722 case bpf_ctx_range(struct __sk_buff, tc_classid):
3723 case bpf_ctx_range(struct __sk_buff, data):
3724 case bpf_ctx_range(struct __sk_buff, data_meta):
3725 case bpf_ctx_range(struct __sk_buff, data_end):
3726 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3730 if (type == BPF_WRITE) {
3732 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3739 return bpf_skb_is_valid_access(off, size, type, info);
3742 static bool lwt_is_valid_access(int off, int size,
3743 enum bpf_access_type type,
3744 struct bpf_insn_access_aux *info)
3747 case bpf_ctx_range(struct __sk_buff, tc_classid):
3748 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3749 case bpf_ctx_range(struct __sk_buff, data_meta):
3753 if (type == BPF_WRITE) {
3755 case bpf_ctx_range(struct __sk_buff, mark):
3756 case bpf_ctx_range(struct __sk_buff, priority):
3757 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3765 case bpf_ctx_range(struct __sk_buff, data):
3766 info->reg_type = PTR_TO_PACKET;
3768 case bpf_ctx_range(struct __sk_buff, data_end):
3769 info->reg_type = PTR_TO_PACKET_END;
3773 return bpf_skb_is_valid_access(off, size, type, info);
3776 static bool sock_filter_is_valid_access(int off, int size,
3777 enum bpf_access_type type,
3778 struct bpf_insn_access_aux *info)
3780 if (type == BPF_WRITE) {
3782 case offsetof(struct bpf_sock, bound_dev_if):
3783 case offsetof(struct bpf_sock, mark):
3784 case offsetof(struct bpf_sock, priority):
3791 if (off < 0 || off + size > sizeof(struct bpf_sock))
3793 /* The verifier guarantees that size > 0. */
3794 if (off % size != 0)
3796 if (size != sizeof(__u32))
3802 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
3803 const struct bpf_prog *prog, int drop_verdict)
3805 struct bpf_insn *insn = insn_buf;
3810 /* if (!skb->cloned)
3813 * (Fast-path, otherwise approximation that we might be
3814 * a clone, do the rest in helper.)
3816 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
3817 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
3818 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
3820 /* ret = bpf_skb_pull_data(skb, 0); */
3821 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
3822 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
3823 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
3824 BPF_FUNC_skb_pull_data);
3827 * return TC_ACT_SHOT;
3829 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
3830 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
3831 *insn++ = BPF_EXIT_INSN();
3834 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
3836 *insn++ = prog->insnsi[0];
3838 return insn - insn_buf;
3841 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
3842 const struct bpf_prog *prog)
3844 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
3847 static bool tc_cls_act_is_valid_access(int off, int size,
3848 enum bpf_access_type type,
3849 struct bpf_insn_access_aux *info)
3851 if (type == BPF_WRITE) {
3853 case bpf_ctx_range(struct __sk_buff, mark):
3854 case bpf_ctx_range(struct __sk_buff, tc_index):
3855 case bpf_ctx_range(struct __sk_buff, priority):
3856 case bpf_ctx_range(struct __sk_buff, tc_classid):
3857 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3865 case bpf_ctx_range(struct __sk_buff, data):
3866 info->reg_type = PTR_TO_PACKET;
3868 case bpf_ctx_range(struct __sk_buff, data_meta):
3869 info->reg_type = PTR_TO_PACKET_META;
3871 case bpf_ctx_range(struct __sk_buff, data_end):
3872 info->reg_type = PTR_TO_PACKET_END;
3874 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3878 return bpf_skb_is_valid_access(off, size, type, info);
3881 static bool __is_valid_xdp_access(int off, int size)
3883 if (off < 0 || off >= sizeof(struct xdp_md))
3885 if (off % size != 0)
3887 if (size != sizeof(__u32))
3893 static bool xdp_is_valid_access(int off, int size,
3894 enum bpf_access_type type,
3895 struct bpf_insn_access_aux *info)
3897 if (type == BPF_WRITE)
3901 case offsetof(struct xdp_md, data):
3902 info->reg_type = PTR_TO_PACKET;
3904 case offsetof(struct xdp_md, data_meta):
3905 info->reg_type = PTR_TO_PACKET_META;
3907 case offsetof(struct xdp_md, data_end):
3908 info->reg_type = PTR_TO_PACKET_END;
3912 return __is_valid_xdp_access(off, size);
3915 void bpf_warn_invalid_xdp_action(u32 act)
3917 const u32 act_max = XDP_REDIRECT;
3919 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
3920 act > act_max ? "Illegal" : "Driver unsupported",
3923 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
3925 static bool sock_ops_is_valid_access(int off, int size,
3926 enum bpf_access_type type,
3927 struct bpf_insn_access_aux *info)
3929 const int size_default = sizeof(__u32);
3931 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
3934 /* The verifier guarantees that size > 0. */
3935 if (off % size != 0)
3938 if (type == BPF_WRITE) {
3940 case offsetof(struct bpf_sock_ops, reply):
3941 case offsetof(struct bpf_sock_ops, sk_txhash):
3942 if (size != size_default)
3950 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
3952 if (size != sizeof(__u64))
3956 if (size != size_default)
3965 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
3966 const struct bpf_prog *prog)
3968 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
3971 static bool sk_skb_is_valid_access(int off, int size,
3972 enum bpf_access_type type,
3973 struct bpf_insn_access_aux *info)
3976 case bpf_ctx_range(struct __sk_buff, tc_classid):
3977 case bpf_ctx_range(struct __sk_buff, data_meta):
3981 if (type == BPF_WRITE) {
3983 case bpf_ctx_range(struct __sk_buff, tc_index):
3984 case bpf_ctx_range(struct __sk_buff, priority):
3992 case bpf_ctx_range(struct __sk_buff, mark):
3994 case bpf_ctx_range(struct __sk_buff, data):
3995 info->reg_type = PTR_TO_PACKET;
3997 case bpf_ctx_range(struct __sk_buff, data_end):
3998 info->reg_type = PTR_TO_PACKET_END;
4002 return bpf_skb_is_valid_access(off, size, type, info);
4005 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
4006 const struct bpf_insn *si,
4007 struct bpf_insn *insn_buf,
4008 struct bpf_prog *prog, u32 *target_size)
4010 struct bpf_insn *insn = insn_buf;
4014 case offsetof(struct __sk_buff, len):
4015 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4016 bpf_target_off(struct sk_buff, len, 4,
4020 case offsetof(struct __sk_buff, protocol):
4021 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4022 bpf_target_off(struct sk_buff, protocol, 2,
4026 case offsetof(struct __sk_buff, vlan_proto):
4027 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4028 bpf_target_off(struct sk_buff, vlan_proto, 2,
4032 case offsetof(struct __sk_buff, priority):
4033 if (type == BPF_WRITE)
4034 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4035 bpf_target_off(struct sk_buff, priority, 4,
4038 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4039 bpf_target_off(struct sk_buff, priority, 4,
4043 case offsetof(struct __sk_buff, ingress_ifindex):
4044 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4045 bpf_target_off(struct sk_buff, skb_iif, 4,
4049 case offsetof(struct __sk_buff, ifindex):
4050 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
4051 si->dst_reg, si->src_reg,
4052 offsetof(struct sk_buff, dev));
4053 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
4054 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4055 bpf_target_off(struct net_device, ifindex, 4,
4059 case offsetof(struct __sk_buff, hash):
4060 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4061 bpf_target_off(struct sk_buff, hash, 4,
4065 case offsetof(struct __sk_buff, mark):
4066 if (type == BPF_WRITE)
4067 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4068 bpf_target_off(struct sk_buff, mark, 4,
4071 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4072 bpf_target_off(struct sk_buff, mark, 4,
4076 case offsetof(struct __sk_buff, pkt_type):
4078 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
4080 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
4081 #ifdef __BIG_ENDIAN_BITFIELD
4082 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
4086 case offsetof(struct __sk_buff, queue_mapping):
4087 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4088 bpf_target_off(struct sk_buff, queue_mapping, 2,
4092 case offsetof(struct __sk_buff, vlan_present):
4093 case offsetof(struct __sk_buff, vlan_tci):
4094 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
4096 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4097 bpf_target_off(struct sk_buff, vlan_tci, 2,
4099 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
4100 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
4103 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
4104 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
4108 case offsetof(struct __sk_buff, cb[0]) ...
4109 offsetofend(struct __sk_buff, cb[4]) - 1:
4110 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
4111 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
4112 offsetof(struct qdisc_skb_cb, data)) %
4115 prog->cb_access = 1;
4117 off -= offsetof(struct __sk_buff, cb[0]);
4118 off += offsetof(struct sk_buff, cb);
4119 off += offsetof(struct qdisc_skb_cb, data);
4120 if (type == BPF_WRITE)
4121 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
4124 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
4128 case offsetof(struct __sk_buff, tc_classid):
4129 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
4132 off -= offsetof(struct __sk_buff, tc_classid);
4133 off += offsetof(struct sk_buff, cb);
4134 off += offsetof(struct qdisc_skb_cb, tc_classid);
4136 if (type == BPF_WRITE)
4137 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
4140 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
4144 case offsetof(struct __sk_buff, data):
4145 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
4146 si->dst_reg, si->src_reg,
4147 offsetof(struct sk_buff, data));
4150 case offsetof(struct __sk_buff, data_meta):
4152 off -= offsetof(struct __sk_buff, data_meta);
4153 off += offsetof(struct sk_buff, cb);
4154 off += offsetof(struct bpf_skb_data_end, data_meta);
4155 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
4159 case offsetof(struct __sk_buff, data_end):
4161 off -= offsetof(struct __sk_buff, data_end);
4162 off += offsetof(struct sk_buff, cb);
4163 off += offsetof(struct bpf_skb_data_end, data_end);
4164 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
4168 case offsetof(struct __sk_buff, tc_index):
4169 #ifdef CONFIG_NET_SCHED
4170 if (type == BPF_WRITE)
4171 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
4172 bpf_target_off(struct sk_buff, tc_index, 2,
4175 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4176 bpf_target_off(struct sk_buff, tc_index, 2,
4180 if (type == BPF_WRITE)
4181 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
4183 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
4187 case offsetof(struct __sk_buff, napi_id):
4188 #if defined(CONFIG_NET_RX_BUSY_POLL)
4189 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4190 bpf_target_off(struct sk_buff, napi_id, 4,
4192 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
4193 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
4196 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
4199 case offsetof(struct __sk_buff, family):
4200 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
4202 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4203 si->dst_reg, si->src_reg,
4204 offsetof(struct sk_buff, sk));
4205 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4206 bpf_target_off(struct sock_common,
4210 case offsetof(struct __sk_buff, remote_ip4):
4211 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
4213 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4214 si->dst_reg, si->src_reg,
4215 offsetof(struct sk_buff, sk));
4216 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4217 bpf_target_off(struct sock_common,
4221 case offsetof(struct __sk_buff, local_ip4):
4222 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4223 skc_rcv_saddr) != 4);
4225 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4226 si->dst_reg, si->src_reg,
4227 offsetof(struct sk_buff, sk));
4228 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4229 bpf_target_off(struct sock_common,
4233 case offsetof(struct __sk_buff, remote_ip6[0]) ...
4234 offsetof(struct __sk_buff, remote_ip6[3]):
4235 #if IS_ENABLED(CONFIG_IPV6)
4236 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4237 skc_v6_daddr.s6_addr32[0]) != 4);
4240 off -= offsetof(struct __sk_buff, remote_ip6[0]);
4242 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4243 si->dst_reg, si->src_reg,
4244 offsetof(struct sk_buff, sk));
4245 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4246 offsetof(struct sock_common,
4247 skc_v6_daddr.s6_addr32[0]) +
4250 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4253 case offsetof(struct __sk_buff, local_ip6[0]) ...
4254 offsetof(struct __sk_buff, local_ip6[3]):
4255 #if IS_ENABLED(CONFIG_IPV6)
4256 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4257 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
4260 off -= offsetof(struct __sk_buff, local_ip6[0]);
4262 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4263 si->dst_reg, si->src_reg,
4264 offsetof(struct sk_buff, sk));
4265 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4266 offsetof(struct sock_common,
4267 skc_v6_rcv_saddr.s6_addr32[0]) +
4270 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4274 case offsetof(struct __sk_buff, remote_port):
4275 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
4277 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4278 si->dst_reg, si->src_reg,
4279 offsetof(struct sk_buff, sk));
4280 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4281 bpf_target_off(struct sock_common,
4284 #ifndef __BIG_ENDIAN_BITFIELD
4285 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
4289 case offsetof(struct __sk_buff, local_port):
4290 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
4292 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4293 si->dst_reg, si->src_reg,
4294 offsetof(struct sk_buff, sk));
4295 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4296 bpf_target_off(struct sock_common,
4297 skc_num, 2, target_size));
4301 return insn - insn_buf;
4304 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
4305 const struct bpf_insn *si,
4306 struct bpf_insn *insn_buf,
4307 struct bpf_prog *prog, u32 *target_size)
4309 struct bpf_insn *insn = insn_buf;
4312 case offsetof(struct bpf_sock, bound_dev_if):
4313 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
4315 if (type == BPF_WRITE)
4316 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4317 offsetof(struct sock, sk_bound_dev_if));
4319 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4320 offsetof(struct sock, sk_bound_dev_if));
4323 case offsetof(struct bpf_sock, mark):
4324 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
4326 if (type == BPF_WRITE)
4327 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4328 offsetof(struct sock, sk_mark));
4330 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4331 offsetof(struct sock, sk_mark));
4334 case offsetof(struct bpf_sock, priority):
4335 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
4337 if (type == BPF_WRITE)
4338 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4339 offsetof(struct sock, sk_priority));
4341 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4342 offsetof(struct sock, sk_priority));
4345 case offsetof(struct bpf_sock, family):
4346 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
4348 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4349 offsetof(struct sock, sk_family));
4352 case offsetof(struct bpf_sock, type):
4353 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4354 offsetof(struct sock, __sk_flags_offset));
4355 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
4356 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
4359 case offsetof(struct bpf_sock, protocol):
4360 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4361 offsetof(struct sock, __sk_flags_offset));
4362 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
4363 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
4367 return insn - insn_buf;
4370 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
4371 const struct bpf_insn *si,
4372 struct bpf_insn *insn_buf,
4373 struct bpf_prog *prog, u32 *target_size)
4375 struct bpf_insn *insn = insn_buf;
4378 case offsetof(struct __sk_buff, ifindex):
4379 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
4380 si->dst_reg, si->src_reg,
4381 offsetof(struct sk_buff, dev));
4382 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4383 bpf_target_off(struct net_device, ifindex, 4,
4387 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4391 return insn - insn_buf;
4394 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
4395 const struct bpf_insn *si,
4396 struct bpf_insn *insn_buf,
4397 struct bpf_prog *prog, u32 *target_size)
4399 struct bpf_insn *insn = insn_buf;
4402 case offsetof(struct xdp_md, data):
4403 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
4404 si->dst_reg, si->src_reg,
4405 offsetof(struct xdp_buff, data));
4407 case offsetof(struct xdp_md, data_meta):
4408 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
4409 si->dst_reg, si->src_reg,
4410 offsetof(struct xdp_buff, data_meta));
4412 case offsetof(struct xdp_md, data_end):
4413 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
4414 si->dst_reg, si->src_reg,
4415 offsetof(struct xdp_buff, data_end));
4417 case offsetof(struct xdp_md, ingress_ifindex):
4418 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
4419 si->dst_reg, si->src_reg,
4420 offsetof(struct xdp_buff, rxq));
4421 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
4422 si->dst_reg, si->dst_reg,
4423 offsetof(struct xdp_rxq_info, dev));
4424 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4425 offsetof(struct net_device, ifindex));
4427 case offsetof(struct xdp_md, rx_queue_index):
4428 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
4429 si->dst_reg, si->src_reg,
4430 offsetof(struct xdp_buff, rxq));
4431 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4432 offsetof(struct xdp_rxq_info,
4437 return insn - insn_buf;
4440 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
4441 const struct bpf_insn *si,
4442 struct bpf_insn *insn_buf,
4443 struct bpf_prog *prog,
4446 struct bpf_insn *insn = insn_buf;
4450 case offsetof(struct bpf_sock_ops, op) ...
4451 offsetof(struct bpf_sock_ops, replylong[3]):
4452 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
4453 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
4454 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
4455 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
4456 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
4457 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
4459 off -= offsetof(struct bpf_sock_ops, op);
4460 off += offsetof(struct bpf_sock_ops_kern, op);
4461 if (type == BPF_WRITE)
4462 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4465 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4469 case offsetof(struct bpf_sock_ops, family):
4470 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
4472 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4473 struct bpf_sock_ops_kern, sk),
4474 si->dst_reg, si->src_reg,
4475 offsetof(struct bpf_sock_ops_kern, sk));
4476 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4477 offsetof(struct sock_common, skc_family));
4480 case offsetof(struct bpf_sock_ops, remote_ip4):
4481 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
4483 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4484 struct bpf_sock_ops_kern, sk),
4485 si->dst_reg, si->src_reg,
4486 offsetof(struct bpf_sock_ops_kern, sk));
4487 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4488 offsetof(struct sock_common, skc_daddr));
4491 case offsetof(struct bpf_sock_ops, local_ip4):
4492 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
4494 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4495 struct bpf_sock_ops_kern, sk),
4496 si->dst_reg, si->src_reg,
4497 offsetof(struct bpf_sock_ops_kern, sk));
4498 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4499 offsetof(struct sock_common,
4503 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
4504 offsetof(struct bpf_sock_ops, remote_ip6[3]):
4505 #if IS_ENABLED(CONFIG_IPV6)
4506 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4507 skc_v6_daddr.s6_addr32[0]) != 4);
4510 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
4511 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4512 struct bpf_sock_ops_kern, sk),
4513 si->dst_reg, si->src_reg,
4514 offsetof(struct bpf_sock_ops_kern, sk));
4515 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4516 offsetof(struct sock_common,
4517 skc_v6_daddr.s6_addr32[0]) +
4520 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4524 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
4525 offsetof(struct bpf_sock_ops, local_ip6[3]):
4526 #if IS_ENABLED(CONFIG_IPV6)
4527 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4528 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
4531 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
4532 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4533 struct bpf_sock_ops_kern, sk),
4534 si->dst_reg, si->src_reg,
4535 offsetof(struct bpf_sock_ops_kern, sk));
4536 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4537 offsetof(struct sock_common,
4538 skc_v6_rcv_saddr.s6_addr32[0]) +
4541 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4545 case offsetof(struct bpf_sock_ops, remote_port):
4546 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
4548 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4549 struct bpf_sock_ops_kern, sk),
4550 si->dst_reg, si->src_reg,
4551 offsetof(struct bpf_sock_ops_kern, sk));
4552 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4553 offsetof(struct sock_common, skc_dport));
4554 #ifndef __BIG_ENDIAN_BITFIELD
4555 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
4559 case offsetof(struct bpf_sock_ops, local_port):
4560 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
4562 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4563 struct bpf_sock_ops_kern, sk),
4564 si->dst_reg, si->src_reg,
4565 offsetof(struct bpf_sock_ops_kern, sk));
4566 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4567 offsetof(struct sock_common, skc_num));
4570 case offsetof(struct bpf_sock_ops, is_fullsock):
4571 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4572 struct bpf_sock_ops_kern,
4574 si->dst_reg, si->src_reg,
4575 offsetof(struct bpf_sock_ops_kern,
4579 case offsetof(struct bpf_sock_ops, state):
4580 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
4582 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4583 struct bpf_sock_ops_kern, sk),
4584 si->dst_reg, si->src_reg,
4585 offsetof(struct bpf_sock_ops_kern, sk));
4586 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
4587 offsetof(struct sock_common, skc_state));
4590 case offsetof(struct bpf_sock_ops, rtt_min):
4591 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
4592 sizeof(struct minmax));
4593 BUILD_BUG_ON(sizeof(struct minmax) <
4594 sizeof(struct minmax_sample));
4596 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4597 struct bpf_sock_ops_kern, sk),
4598 si->dst_reg, si->src_reg,
4599 offsetof(struct bpf_sock_ops_kern, sk));
4600 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4601 offsetof(struct tcp_sock, rtt_min) +
4602 FIELD_SIZEOF(struct minmax_sample, t));
4605 /* Helper macro for adding read access to tcp_sock or sock fields. */
4606 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
4608 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
4609 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
4610 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
4611 struct bpf_sock_ops_kern, \
4613 si->dst_reg, si->src_reg, \
4614 offsetof(struct bpf_sock_ops_kern, \
4616 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
4617 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
4618 struct bpf_sock_ops_kern, sk),\
4619 si->dst_reg, si->src_reg, \
4620 offsetof(struct bpf_sock_ops_kern, sk));\
4621 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
4623 si->dst_reg, si->dst_reg, \
4624 offsetof(OBJ, OBJ_FIELD)); \
4627 /* Helper macro for adding write access to tcp_sock or sock fields.
4628 * The macro is called with two registers, dst_reg which contains a pointer
4629 * to ctx (context) and src_reg which contains the value that should be
4630 * stored. However, we need an additional register since we cannot overwrite
4631 * dst_reg because it may be used later in the program.
4632 * Instead we "borrow" one of the other register. We first save its value
4633 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
4634 * it at the end of the macro.
4636 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
4638 int reg = BPF_REG_9; \
4639 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
4640 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
4641 if (si->dst_reg == reg || si->src_reg == reg) \
4643 if (si->dst_reg == reg || si->src_reg == reg) \
4645 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
4646 offsetof(struct bpf_sock_ops_kern, \
4648 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
4649 struct bpf_sock_ops_kern, \
4652 offsetof(struct bpf_sock_ops_kern, \
4654 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
4655 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
4656 struct bpf_sock_ops_kern, sk),\
4658 offsetof(struct bpf_sock_ops_kern, sk));\
4659 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
4661 offsetof(OBJ, OBJ_FIELD)); \
4662 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
4663 offsetof(struct bpf_sock_ops_kern, \
4667 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
4669 if (TYPE == BPF_WRITE) \
4670 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
4672 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
4675 case offsetof(struct bpf_sock_ops, snd_cwnd):
4676 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
4679 case offsetof(struct bpf_sock_ops, srtt_us):
4680 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
4683 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
4684 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
4688 case offsetof(struct bpf_sock_ops, snd_ssthresh):
4689 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
4692 case offsetof(struct bpf_sock_ops, rcv_nxt):
4693 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
4696 case offsetof(struct bpf_sock_ops, snd_nxt):
4697 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
4700 case offsetof(struct bpf_sock_ops, snd_una):
4701 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
4704 case offsetof(struct bpf_sock_ops, mss_cache):
4705 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
4708 case offsetof(struct bpf_sock_ops, ecn_flags):
4709 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
4712 case offsetof(struct bpf_sock_ops, rate_delivered):
4713 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
4717 case offsetof(struct bpf_sock_ops, rate_interval_us):
4718 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
4722 case offsetof(struct bpf_sock_ops, packets_out):
4723 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
4726 case offsetof(struct bpf_sock_ops, retrans_out):
4727 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
4730 case offsetof(struct bpf_sock_ops, total_retrans):
4731 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
4735 case offsetof(struct bpf_sock_ops, segs_in):
4736 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
4739 case offsetof(struct bpf_sock_ops, data_segs_in):
4740 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
4743 case offsetof(struct bpf_sock_ops, segs_out):
4744 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
4747 case offsetof(struct bpf_sock_ops, data_segs_out):
4748 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
4752 case offsetof(struct bpf_sock_ops, lost_out):
4753 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
4756 case offsetof(struct bpf_sock_ops, sacked_out):
4757 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
4760 case offsetof(struct bpf_sock_ops, sk_txhash):
4761 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
4765 case offsetof(struct bpf_sock_ops, bytes_received):
4766 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
4770 case offsetof(struct bpf_sock_ops, bytes_acked):
4771 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
4775 return insn - insn_buf;
4778 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
4779 const struct bpf_insn *si,
4780 struct bpf_insn *insn_buf,
4781 struct bpf_prog *prog, u32 *target_size)
4783 struct bpf_insn *insn = insn_buf;
4787 case offsetof(struct __sk_buff, data_end):
4789 off -= offsetof(struct __sk_buff, data_end);
4790 off += offsetof(struct sk_buff, cb);
4791 off += offsetof(struct tcp_skb_cb, bpf.data_end);
4792 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
4796 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4800 return insn - insn_buf;
4803 const struct bpf_verifier_ops sk_filter_verifier_ops = {
4804 .get_func_proto = sk_filter_func_proto,
4805 .is_valid_access = sk_filter_is_valid_access,
4806 .convert_ctx_access = bpf_convert_ctx_access,
4809 const struct bpf_prog_ops sk_filter_prog_ops = {
4810 .test_run = bpf_prog_test_run_skb,
4813 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
4814 .get_func_proto = tc_cls_act_func_proto,
4815 .is_valid_access = tc_cls_act_is_valid_access,
4816 .convert_ctx_access = tc_cls_act_convert_ctx_access,
4817 .gen_prologue = tc_cls_act_prologue,
4820 const struct bpf_prog_ops tc_cls_act_prog_ops = {
4821 .test_run = bpf_prog_test_run_skb,
4824 const struct bpf_verifier_ops xdp_verifier_ops = {
4825 .get_func_proto = xdp_func_proto,
4826 .is_valid_access = xdp_is_valid_access,
4827 .convert_ctx_access = xdp_convert_ctx_access,
4830 const struct bpf_prog_ops xdp_prog_ops = {
4831 .test_run = bpf_prog_test_run_xdp,
4834 const struct bpf_verifier_ops cg_skb_verifier_ops = {
4835 .get_func_proto = sk_filter_func_proto,
4836 .is_valid_access = sk_filter_is_valid_access,
4837 .convert_ctx_access = bpf_convert_ctx_access,
4840 const struct bpf_prog_ops cg_skb_prog_ops = {
4841 .test_run = bpf_prog_test_run_skb,
4844 const struct bpf_verifier_ops lwt_inout_verifier_ops = {
4845 .get_func_proto = lwt_inout_func_proto,
4846 .is_valid_access = lwt_is_valid_access,
4847 .convert_ctx_access = bpf_convert_ctx_access,
4850 const struct bpf_prog_ops lwt_inout_prog_ops = {
4851 .test_run = bpf_prog_test_run_skb,
4854 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
4855 .get_func_proto = lwt_xmit_func_proto,
4856 .is_valid_access = lwt_is_valid_access,
4857 .convert_ctx_access = bpf_convert_ctx_access,
4858 .gen_prologue = tc_cls_act_prologue,
4861 const struct bpf_prog_ops lwt_xmit_prog_ops = {
4862 .test_run = bpf_prog_test_run_skb,
4865 const struct bpf_verifier_ops cg_sock_verifier_ops = {
4866 .get_func_proto = sock_filter_func_proto,
4867 .is_valid_access = sock_filter_is_valid_access,
4868 .convert_ctx_access = sock_filter_convert_ctx_access,
4871 const struct bpf_prog_ops cg_sock_prog_ops = {
4874 const struct bpf_verifier_ops sock_ops_verifier_ops = {
4875 .get_func_proto = sock_ops_func_proto,
4876 .is_valid_access = sock_ops_is_valid_access,
4877 .convert_ctx_access = sock_ops_convert_ctx_access,
4880 const struct bpf_prog_ops sock_ops_prog_ops = {
4883 const struct bpf_verifier_ops sk_skb_verifier_ops = {
4884 .get_func_proto = sk_skb_func_proto,
4885 .is_valid_access = sk_skb_is_valid_access,
4886 .convert_ctx_access = sk_skb_convert_ctx_access,
4887 .gen_prologue = sk_skb_prologue,
4890 const struct bpf_prog_ops sk_skb_prog_ops = {
4893 int sk_detach_filter(struct sock *sk)
4896 struct sk_filter *filter;
4898 if (sock_flag(sk, SOCK_FILTER_LOCKED))
4901 filter = rcu_dereference_protected(sk->sk_filter,
4902 lockdep_sock_is_held(sk));
4904 RCU_INIT_POINTER(sk->sk_filter, NULL);
4905 sk_filter_uncharge(sk, filter);
4911 EXPORT_SYMBOL_GPL(sk_detach_filter);
4913 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
4916 struct sock_fprog_kern *fprog;
4917 struct sk_filter *filter;
4921 filter = rcu_dereference_protected(sk->sk_filter,
4922 lockdep_sock_is_held(sk));
4926 /* We're copying the filter that has been originally attached,
4927 * so no conversion/decode needed anymore. eBPF programs that
4928 * have no original program cannot be dumped through this.
4931 fprog = filter->prog->orig_prog;
4937 /* User space only enquires number of filter blocks. */
4941 if (len < fprog->len)
4945 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
4948 /* Instead of bytes, the API requests to return the number