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 <linux/filter.h>
47 #include <linux/ratelimit.h>
48 #include <linux/seccomp.h>
49 #include <linux/if_vlan.h>
50 #include <linux/bpf.h>
51 #include <net/sch_generic.h>
52 #include <net/cls_cgroup.h>
53 #include <net/dst_metadata.h>
55 #include <net/sock_reuseport.h>
56 #include <net/busy_poll.h>
60 * sk_filter_trim_cap - run a packet through a socket filter
61 * @sk: sock associated with &sk_buff
62 * @skb: buffer to filter
63 * @cap: limit on how short the eBPF program may trim the packet
65 * Run the eBPF program and then cut skb->data to correct size returned by
66 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
67 * than pkt_len we keep whole skb->data. This is the socket level
68 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
69 * be accepted or -EPERM if the packet should be tossed.
72 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
75 struct sk_filter *filter;
78 * If the skb was allocated from pfmemalloc reserves, only
79 * allow SOCK_MEMALLOC sockets to use it as this socket is
82 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
83 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
86 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
90 err = security_sock_rcv_skb(sk, skb);
95 filter = rcu_dereference(sk->sk_filter);
97 struct sock *save_sk = skb->sk;
101 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
103 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
109 EXPORT_SYMBOL(sk_filter_trim_cap);
111 BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
113 return skb_get_poff(skb);
116 BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
120 if (skb_is_nonlinear(skb))
123 if (skb->len < sizeof(struct nlattr))
126 if (a > skb->len - sizeof(struct nlattr))
129 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
131 return (void *) nla - (void *) skb->data;
136 BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
140 if (skb_is_nonlinear(skb))
143 if (skb->len < sizeof(struct nlattr))
146 if (a > skb->len - sizeof(struct nlattr))
149 nla = (struct nlattr *) &skb->data[a];
150 if (nla->nla_len > skb->len - a)
153 nla = nla_find_nested(nla, x);
155 return (void *) nla - (void *) skb->data;
160 BPF_CALL_0(__get_raw_cpu_id)
162 return raw_smp_processor_id();
165 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
166 .func = __get_raw_cpu_id,
168 .ret_type = RET_INTEGER,
171 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
172 struct bpf_insn *insn_buf)
174 struct bpf_insn *insn = insn_buf;
178 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
180 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
181 offsetof(struct sk_buff, mark));
185 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
186 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
187 #ifdef __BIG_ENDIAN_BITFIELD
188 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
193 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
195 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
196 offsetof(struct sk_buff, queue_mapping));
199 case SKF_AD_VLAN_TAG:
200 case SKF_AD_VLAN_TAG_PRESENT:
201 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
202 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
204 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
205 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
206 offsetof(struct sk_buff, vlan_tci));
207 if (skb_field == SKF_AD_VLAN_TAG) {
208 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
212 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
214 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
219 return insn - insn_buf;
222 static bool convert_bpf_extensions(struct sock_filter *fp,
223 struct bpf_insn **insnp)
225 struct bpf_insn *insn = *insnp;
229 case SKF_AD_OFF + SKF_AD_PROTOCOL:
230 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
232 /* A = *(u16 *) (CTX + offsetof(protocol)) */
233 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
234 offsetof(struct sk_buff, protocol));
235 /* A = ntohs(A) [emitting a nop or swap16] */
236 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
239 case SKF_AD_OFF + SKF_AD_PKTTYPE:
240 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
244 case SKF_AD_OFF + SKF_AD_IFINDEX:
245 case SKF_AD_OFF + SKF_AD_HATYPE:
246 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
247 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
249 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
250 BPF_REG_TMP, BPF_REG_CTX,
251 offsetof(struct sk_buff, dev));
252 /* if (tmp != 0) goto pc + 1 */
253 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
254 *insn++ = BPF_EXIT_INSN();
255 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
256 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
257 offsetof(struct net_device, ifindex));
259 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
260 offsetof(struct net_device, type));
263 case SKF_AD_OFF + SKF_AD_MARK:
264 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
268 case SKF_AD_OFF + SKF_AD_RXHASH:
269 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
271 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
272 offsetof(struct sk_buff, hash));
275 case SKF_AD_OFF + SKF_AD_QUEUE:
276 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
280 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
281 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
282 BPF_REG_A, BPF_REG_CTX, insn);
286 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
287 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
288 BPF_REG_A, BPF_REG_CTX, insn);
292 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
293 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
295 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
296 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
297 offsetof(struct sk_buff, vlan_proto));
298 /* A = ntohs(A) [emitting a nop or swap16] */
299 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
302 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
303 case SKF_AD_OFF + SKF_AD_NLATTR:
304 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
305 case SKF_AD_OFF + SKF_AD_CPU:
306 case SKF_AD_OFF + SKF_AD_RANDOM:
308 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
310 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
312 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
313 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
315 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
316 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
318 case SKF_AD_OFF + SKF_AD_NLATTR:
319 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
321 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
322 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
324 case SKF_AD_OFF + SKF_AD_CPU:
325 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
327 case SKF_AD_OFF + SKF_AD_RANDOM:
328 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
329 bpf_user_rnd_init_once();
334 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
336 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
340 /* This is just a dummy call to avoid letting the compiler
341 * evict __bpf_call_base() as an optimization. Placed here
342 * where no-one bothers.
344 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
353 * bpf_convert_filter - convert filter program
354 * @prog: the user passed filter program
355 * @len: the length of the user passed filter program
356 * @new_prog: allocated 'struct bpf_prog' or NULL
357 * @new_len: pointer to store length of converted program
359 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
360 * style extended BPF (eBPF).
361 * Conversion workflow:
363 * 1) First pass for calculating the new program length:
364 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
366 * 2) 2nd pass to remap in two passes: 1st pass finds new
367 * jump offsets, 2nd pass remapping:
368 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
370 static int bpf_convert_filter(struct sock_filter *prog, int len,
371 struct bpf_prog *new_prog, int *new_len)
373 int new_flen = 0, pass = 0, target, i, stack_off;
374 struct bpf_insn *new_insn, *first_insn = NULL;
375 struct sock_filter *fp;
379 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
380 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
382 if (len <= 0 || len > BPF_MAXINSNS)
386 first_insn = new_prog->insnsi;
387 addrs = kcalloc(len, sizeof(*addrs),
388 GFP_KERNEL | __GFP_NOWARN);
394 new_insn = first_insn;
397 /* Classic BPF related prologue emission. */
399 /* Classic BPF expects A and X to be reset first. These need
400 * to be guaranteed to be the first two instructions.
402 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
403 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
405 /* All programs must keep CTX in callee saved BPF_REG_CTX.
406 * In eBPF case it's done by the compiler, here we need to
407 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
409 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
414 for (i = 0; i < len; fp++, i++) {
415 struct bpf_insn tmp_insns[6] = { };
416 struct bpf_insn *insn = tmp_insns;
419 addrs[i] = new_insn - first_insn;
422 /* All arithmetic insns and skb loads map as-is. */
423 case BPF_ALU | BPF_ADD | BPF_X:
424 case BPF_ALU | BPF_ADD | BPF_K:
425 case BPF_ALU | BPF_SUB | BPF_X:
426 case BPF_ALU | BPF_SUB | BPF_K:
427 case BPF_ALU | BPF_AND | BPF_X:
428 case BPF_ALU | BPF_AND | BPF_K:
429 case BPF_ALU | BPF_OR | BPF_X:
430 case BPF_ALU | BPF_OR | BPF_K:
431 case BPF_ALU | BPF_LSH | BPF_X:
432 case BPF_ALU | BPF_LSH | BPF_K:
433 case BPF_ALU | BPF_RSH | BPF_X:
434 case BPF_ALU | BPF_RSH | BPF_K:
435 case BPF_ALU | BPF_XOR | BPF_X:
436 case BPF_ALU | BPF_XOR | BPF_K:
437 case BPF_ALU | BPF_MUL | BPF_X:
438 case BPF_ALU | BPF_MUL | BPF_K:
439 case BPF_ALU | BPF_DIV | BPF_X:
440 case BPF_ALU | BPF_DIV | BPF_K:
441 case BPF_ALU | BPF_MOD | BPF_X:
442 case BPF_ALU | BPF_MOD | BPF_K:
443 case BPF_ALU | BPF_NEG:
444 case BPF_LD | BPF_ABS | BPF_W:
445 case BPF_LD | BPF_ABS | BPF_H:
446 case BPF_LD | BPF_ABS | BPF_B:
447 case BPF_LD | BPF_IND | BPF_W:
448 case BPF_LD | BPF_IND | BPF_H:
449 case BPF_LD | BPF_IND | BPF_B:
450 /* Check for overloaded BPF extension and
451 * directly convert it if found, otherwise
452 * just move on with mapping.
454 if (BPF_CLASS(fp->code) == BPF_LD &&
455 BPF_MODE(fp->code) == BPF_ABS &&
456 convert_bpf_extensions(fp, &insn))
459 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
462 /* Jump transformation cannot use BPF block macros
463 * everywhere as offset calculation and target updates
464 * require a bit more work than the rest, i.e. jump
465 * opcodes map as-is, but offsets need adjustment.
468 #define BPF_EMIT_JMP \
470 if (target >= len || target < 0) \
472 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
473 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
474 insn->off -= insn - tmp_insns; \
477 case BPF_JMP | BPF_JA:
478 target = i + fp->k + 1;
479 insn->code = fp->code;
483 case BPF_JMP | BPF_JEQ | BPF_K:
484 case BPF_JMP | BPF_JEQ | BPF_X:
485 case BPF_JMP | BPF_JSET | BPF_K:
486 case BPF_JMP | BPF_JSET | BPF_X:
487 case BPF_JMP | BPF_JGT | BPF_K:
488 case BPF_JMP | BPF_JGT | BPF_X:
489 case BPF_JMP | BPF_JGE | BPF_K:
490 case BPF_JMP | BPF_JGE | BPF_X:
491 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
492 /* BPF immediates are signed, zero extend
493 * immediate into tmp register and use it
496 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
498 insn->dst_reg = BPF_REG_A;
499 insn->src_reg = BPF_REG_TMP;
502 insn->dst_reg = BPF_REG_A;
504 bpf_src = BPF_SRC(fp->code);
505 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
508 /* Common case where 'jump_false' is next insn. */
510 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
511 target = i + fp->jt + 1;
516 /* Convert JEQ into JNE when 'jump_true' is next insn. */
517 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
518 insn->code = BPF_JMP | BPF_JNE | bpf_src;
519 target = i + fp->jf + 1;
524 /* Other jumps are mapped into two insns: Jxx and JA. */
525 target = i + fp->jt + 1;
526 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
530 insn->code = BPF_JMP | BPF_JA;
531 target = i + fp->jf + 1;
535 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
536 case BPF_LDX | BPF_MSH | BPF_B:
538 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
539 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
540 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
542 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
544 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
546 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
548 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
551 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
552 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
554 case BPF_RET | BPF_A:
555 case BPF_RET | BPF_K:
556 if (BPF_RVAL(fp->code) == BPF_K)
557 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
559 *insn = BPF_EXIT_INSN();
562 /* Store to stack. */
565 stack_off = fp->k * 4 + 4;
566 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
567 BPF_ST ? BPF_REG_A : BPF_REG_X,
569 /* check_load_and_stores() verifies that classic BPF can
570 * load from stack only after write, so tracking
571 * stack_depth for ST|STX insns is enough
573 if (new_prog && new_prog->aux->stack_depth < stack_off)
574 new_prog->aux->stack_depth = stack_off;
577 /* Load from stack. */
578 case BPF_LD | BPF_MEM:
579 case BPF_LDX | BPF_MEM:
580 stack_off = fp->k * 4 + 4;
581 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
582 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
587 case BPF_LD | BPF_IMM:
588 case BPF_LDX | BPF_IMM:
589 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
590 BPF_REG_A : BPF_REG_X, fp->k);
594 case BPF_MISC | BPF_TAX:
595 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
599 case BPF_MISC | BPF_TXA:
600 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
603 /* A = skb->len or X = skb->len */
604 case BPF_LD | BPF_W | BPF_LEN:
605 case BPF_LDX | BPF_W | BPF_LEN:
606 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
607 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
608 offsetof(struct sk_buff, len));
611 /* Access seccomp_data fields. */
612 case BPF_LDX | BPF_ABS | BPF_W:
613 /* A = *(u32 *) (ctx + K) */
614 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
617 /* Unknown instruction. */
624 memcpy(new_insn, tmp_insns,
625 sizeof(*insn) * (insn - tmp_insns));
626 new_insn += insn - tmp_insns;
630 /* Only calculating new length. */
631 *new_len = new_insn - first_insn;
636 if (new_flen != new_insn - first_insn) {
637 new_flen = new_insn - first_insn;
644 BUG_ON(*new_len != new_flen);
653 * As we dont want to clear mem[] array for each packet going through
654 * __bpf_prog_run(), we check that filter loaded by user never try to read
655 * a cell if not previously written, and we check all branches to be sure
656 * a malicious user doesn't try to abuse us.
658 static int check_load_and_stores(const struct sock_filter *filter, int flen)
660 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
663 BUILD_BUG_ON(BPF_MEMWORDS > 16);
665 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
669 memset(masks, 0xff, flen * sizeof(*masks));
671 for (pc = 0; pc < flen; pc++) {
672 memvalid &= masks[pc];
674 switch (filter[pc].code) {
677 memvalid |= (1 << filter[pc].k);
679 case BPF_LD | BPF_MEM:
680 case BPF_LDX | BPF_MEM:
681 if (!(memvalid & (1 << filter[pc].k))) {
686 case BPF_JMP | BPF_JA:
687 /* A jump must set masks on target */
688 masks[pc + 1 + filter[pc].k] &= memvalid;
691 case BPF_JMP | BPF_JEQ | BPF_K:
692 case BPF_JMP | BPF_JEQ | BPF_X:
693 case BPF_JMP | BPF_JGE | BPF_K:
694 case BPF_JMP | BPF_JGE | BPF_X:
695 case BPF_JMP | BPF_JGT | BPF_K:
696 case BPF_JMP | BPF_JGT | BPF_X:
697 case BPF_JMP | BPF_JSET | BPF_K:
698 case BPF_JMP | BPF_JSET | BPF_X:
699 /* A jump must set masks on targets */
700 masks[pc + 1 + filter[pc].jt] &= memvalid;
701 masks[pc + 1 + filter[pc].jf] &= memvalid;
711 static bool chk_code_allowed(u16 code_to_probe)
713 static const bool codes[] = {
714 /* 32 bit ALU operations */
715 [BPF_ALU | BPF_ADD | BPF_K] = true,
716 [BPF_ALU | BPF_ADD | BPF_X] = true,
717 [BPF_ALU | BPF_SUB | BPF_K] = true,
718 [BPF_ALU | BPF_SUB | BPF_X] = true,
719 [BPF_ALU | BPF_MUL | BPF_K] = true,
720 [BPF_ALU | BPF_MUL | BPF_X] = true,
721 [BPF_ALU | BPF_DIV | BPF_K] = true,
722 [BPF_ALU | BPF_DIV | BPF_X] = true,
723 [BPF_ALU | BPF_MOD | BPF_K] = true,
724 [BPF_ALU | BPF_MOD | BPF_X] = true,
725 [BPF_ALU | BPF_AND | BPF_K] = true,
726 [BPF_ALU | BPF_AND | BPF_X] = true,
727 [BPF_ALU | BPF_OR | BPF_K] = true,
728 [BPF_ALU | BPF_OR | BPF_X] = true,
729 [BPF_ALU | BPF_XOR | BPF_K] = true,
730 [BPF_ALU | BPF_XOR | BPF_X] = true,
731 [BPF_ALU | BPF_LSH | BPF_K] = true,
732 [BPF_ALU | BPF_LSH | BPF_X] = true,
733 [BPF_ALU | BPF_RSH | BPF_K] = true,
734 [BPF_ALU | BPF_RSH | BPF_X] = true,
735 [BPF_ALU | BPF_NEG] = true,
736 /* Load instructions */
737 [BPF_LD | BPF_W | BPF_ABS] = true,
738 [BPF_LD | BPF_H | BPF_ABS] = true,
739 [BPF_LD | BPF_B | BPF_ABS] = true,
740 [BPF_LD | BPF_W | BPF_LEN] = true,
741 [BPF_LD | BPF_W | BPF_IND] = true,
742 [BPF_LD | BPF_H | BPF_IND] = true,
743 [BPF_LD | BPF_B | BPF_IND] = true,
744 [BPF_LD | BPF_IMM] = true,
745 [BPF_LD | BPF_MEM] = true,
746 [BPF_LDX | BPF_W | BPF_LEN] = true,
747 [BPF_LDX | BPF_B | BPF_MSH] = true,
748 [BPF_LDX | BPF_IMM] = true,
749 [BPF_LDX | BPF_MEM] = true,
750 /* Store instructions */
753 /* Misc instructions */
754 [BPF_MISC | BPF_TAX] = true,
755 [BPF_MISC | BPF_TXA] = true,
756 /* Return instructions */
757 [BPF_RET | BPF_K] = true,
758 [BPF_RET | BPF_A] = true,
759 /* Jump instructions */
760 [BPF_JMP | BPF_JA] = true,
761 [BPF_JMP | BPF_JEQ | BPF_K] = true,
762 [BPF_JMP | BPF_JEQ | BPF_X] = true,
763 [BPF_JMP | BPF_JGE | BPF_K] = true,
764 [BPF_JMP | BPF_JGE | BPF_X] = true,
765 [BPF_JMP | BPF_JGT | BPF_K] = true,
766 [BPF_JMP | BPF_JGT | BPF_X] = true,
767 [BPF_JMP | BPF_JSET | BPF_K] = true,
768 [BPF_JMP | BPF_JSET | BPF_X] = true,
771 if (code_to_probe >= ARRAY_SIZE(codes))
774 return codes[code_to_probe];
777 static bool bpf_check_basics_ok(const struct sock_filter *filter,
782 if (flen == 0 || flen > BPF_MAXINSNS)
789 * bpf_check_classic - verify socket filter code
790 * @filter: filter to verify
791 * @flen: length of filter
793 * Check the user's filter code. If we let some ugly
794 * filter code slip through kaboom! The filter must contain
795 * no references or jumps that are out of range, no illegal
796 * instructions, and must end with a RET instruction.
798 * All jumps are forward as they are not signed.
800 * Returns 0 if the rule set is legal or -EINVAL if not.
802 static int bpf_check_classic(const struct sock_filter *filter,
808 /* Check the filter code now */
809 for (pc = 0; pc < flen; pc++) {
810 const struct sock_filter *ftest = &filter[pc];
812 /* May we actually operate on this code? */
813 if (!chk_code_allowed(ftest->code))
816 /* Some instructions need special checks */
817 switch (ftest->code) {
818 case BPF_ALU | BPF_DIV | BPF_K:
819 case BPF_ALU | BPF_MOD | BPF_K:
820 /* Check for division by zero */
824 case BPF_ALU | BPF_LSH | BPF_K:
825 case BPF_ALU | BPF_RSH | BPF_K:
829 case BPF_LD | BPF_MEM:
830 case BPF_LDX | BPF_MEM:
833 /* Check for invalid memory addresses */
834 if (ftest->k >= BPF_MEMWORDS)
837 case BPF_JMP | BPF_JA:
838 /* Note, the large ftest->k might cause loops.
839 * Compare this with conditional jumps below,
840 * where offsets are limited. --ANK (981016)
842 if (ftest->k >= (unsigned int)(flen - pc - 1))
845 case BPF_JMP | BPF_JEQ | BPF_K:
846 case BPF_JMP | BPF_JEQ | BPF_X:
847 case BPF_JMP | BPF_JGE | BPF_K:
848 case BPF_JMP | BPF_JGE | BPF_X:
849 case BPF_JMP | BPF_JGT | BPF_K:
850 case BPF_JMP | BPF_JGT | BPF_X:
851 case BPF_JMP | BPF_JSET | BPF_K:
852 case BPF_JMP | BPF_JSET | BPF_X:
853 /* Both conditionals must be safe */
854 if (pc + ftest->jt + 1 >= flen ||
855 pc + ftest->jf + 1 >= flen)
858 case BPF_LD | BPF_W | BPF_ABS:
859 case BPF_LD | BPF_H | BPF_ABS:
860 case BPF_LD | BPF_B | BPF_ABS:
862 if (bpf_anc_helper(ftest) & BPF_ANC)
864 /* Ancillary operation unknown or unsupported */
865 if (anc_found == false && ftest->k >= SKF_AD_OFF)
870 /* Last instruction must be a RET code */
871 switch (filter[flen - 1].code) {
872 case BPF_RET | BPF_K:
873 case BPF_RET | BPF_A:
874 return check_load_and_stores(filter, flen);
880 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
881 const struct sock_fprog *fprog)
883 unsigned int fsize = bpf_classic_proglen(fprog);
884 struct sock_fprog_kern *fkprog;
886 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
890 fkprog = fp->orig_prog;
891 fkprog->len = fprog->len;
893 fkprog->filter = kmemdup(fp->insns, fsize,
894 GFP_KERNEL | __GFP_NOWARN);
895 if (!fkprog->filter) {
896 kfree(fp->orig_prog);
903 static void bpf_release_orig_filter(struct bpf_prog *fp)
905 struct sock_fprog_kern *fprog = fp->orig_prog;
908 kfree(fprog->filter);
913 static void __bpf_prog_release(struct bpf_prog *prog)
915 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
918 bpf_release_orig_filter(prog);
923 static void __sk_filter_release(struct sk_filter *fp)
925 __bpf_prog_release(fp->prog);
930 * sk_filter_release_rcu - Release a socket filter by rcu_head
931 * @rcu: rcu_head that contains the sk_filter to free
933 static void sk_filter_release_rcu(struct rcu_head *rcu)
935 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
937 __sk_filter_release(fp);
941 * sk_filter_release - release a socket filter
942 * @fp: filter to remove
944 * Remove a filter from a socket and release its resources.
946 static void sk_filter_release(struct sk_filter *fp)
948 if (refcount_dec_and_test(&fp->refcnt))
949 call_rcu(&fp->rcu, sk_filter_release_rcu);
952 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
954 u32 filter_size = bpf_prog_size(fp->prog->len);
956 atomic_sub(filter_size, &sk->sk_omem_alloc);
957 sk_filter_release(fp);
960 /* try to charge the socket memory if there is space available
961 * return true on success
963 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
965 u32 filter_size = bpf_prog_size(fp->prog->len);
967 /* same check as in sock_kmalloc() */
968 if (filter_size <= sysctl_optmem_max &&
969 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
970 atomic_add(filter_size, &sk->sk_omem_alloc);
976 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
978 bool ret = __sk_filter_charge(sk, fp);
980 refcount_inc(&fp->refcnt);
984 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
986 struct sock_filter *old_prog;
987 struct bpf_prog *old_fp;
988 int err, new_len, old_len = fp->len;
990 /* We are free to overwrite insns et al right here as it
991 * won't be used at this point in time anymore internally
992 * after the migration to the internal BPF instruction
995 BUILD_BUG_ON(sizeof(struct sock_filter) !=
996 sizeof(struct bpf_insn));
998 /* Conversion cannot happen on overlapping memory areas,
999 * so we need to keep the user BPF around until the 2nd
1000 * pass. At this time, the user BPF is stored in fp->insns.
1002 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1003 GFP_KERNEL | __GFP_NOWARN);
1009 /* 1st pass: calculate the new program length. */
1010 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
1014 /* Expand fp for appending the new filter representation. */
1016 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1018 /* The old_fp is still around in case we couldn't
1019 * allocate new memory, so uncharge on that one.
1028 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1029 err = bpf_convert_filter(old_prog, old_len, fp, &new_len);
1031 /* 2nd bpf_convert_filter() can fail only if it fails
1032 * to allocate memory, remapping must succeed. Note,
1033 * that at this time old_fp has already been released
1038 /* We are guaranteed to never error here with cBPF to eBPF
1039 * transitions, since there's no issue with type compatibility
1040 * checks on program arrays.
1042 fp = bpf_prog_select_runtime(fp, &err);
1050 __bpf_prog_release(fp);
1051 return ERR_PTR(err);
1054 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1055 bpf_aux_classic_check_t trans)
1059 fp->bpf_func = NULL;
1062 err = bpf_check_classic(fp->insns, fp->len);
1064 __bpf_prog_release(fp);
1065 return ERR_PTR(err);
1068 /* There might be additional checks and transformations
1069 * needed on classic filters, f.e. in case of seccomp.
1072 err = trans(fp->insns, fp->len);
1074 __bpf_prog_release(fp);
1075 return ERR_PTR(err);
1079 /* Probe if we can JIT compile the filter and if so, do
1080 * the compilation of the filter.
1082 bpf_jit_compile(fp);
1084 /* JIT compiler couldn't process this filter, so do the
1085 * internal BPF translation for the optimized interpreter.
1088 fp = bpf_migrate_filter(fp);
1094 * bpf_prog_create - create an unattached filter
1095 * @pfp: the unattached filter that is created
1096 * @fprog: the filter program
1098 * Create a filter independent of any socket. We first run some
1099 * sanity checks on it to make sure it does not explode on us later.
1100 * If an error occurs or there is insufficient memory for the filter
1101 * a negative errno code is returned. On success the return is zero.
1103 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1105 unsigned int fsize = bpf_classic_proglen(fprog);
1106 struct bpf_prog *fp;
1108 /* Make sure new filter is there and in the right amounts. */
1109 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1112 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1116 memcpy(fp->insns, fprog->filter, fsize);
1118 fp->len = fprog->len;
1119 /* Since unattached filters are not copied back to user
1120 * space through sk_get_filter(), we do not need to hold
1121 * a copy here, and can spare us the work.
1123 fp->orig_prog = NULL;
1125 /* bpf_prepare_filter() already takes care of freeing
1126 * memory in case something goes wrong.
1128 fp = bpf_prepare_filter(fp, NULL);
1135 EXPORT_SYMBOL_GPL(bpf_prog_create);
1138 * bpf_prog_create_from_user - create an unattached filter from user buffer
1139 * @pfp: the unattached filter that is created
1140 * @fprog: the filter program
1141 * @trans: post-classic verifier transformation handler
1142 * @save_orig: save classic BPF program
1144 * This function effectively does the same as bpf_prog_create(), only
1145 * that it builds up its insns buffer from user space provided buffer.
1146 * It also allows for passing a bpf_aux_classic_check_t handler.
1148 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1149 bpf_aux_classic_check_t trans, bool save_orig)
1151 unsigned int fsize = bpf_classic_proglen(fprog);
1152 struct bpf_prog *fp;
1155 /* Make sure new filter is there and in the right amounts. */
1156 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1159 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1163 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1164 __bpf_prog_free(fp);
1168 fp->len = fprog->len;
1169 fp->orig_prog = NULL;
1172 err = bpf_prog_store_orig_filter(fp, fprog);
1174 __bpf_prog_free(fp);
1179 /* bpf_prepare_filter() already takes care of freeing
1180 * memory in case something goes wrong.
1182 fp = bpf_prepare_filter(fp, trans);
1189 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1191 void bpf_prog_destroy(struct bpf_prog *fp)
1193 __bpf_prog_release(fp);
1195 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1197 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1199 struct sk_filter *fp, *old_fp;
1201 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1207 if (!__sk_filter_charge(sk, fp)) {
1211 refcount_set(&fp->refcnt, 1);
1213 old_fp = rcu_dereference_protected(sk->sk_filter,
1214 lockdep_sock_is_held(sk));
1215 rcu_assign_pointer(sk->sk_filter, fp);
1218 sk_filter_uncharge(sk, old_fp);
1223 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1225 struct bpf_prog *old_prog;
1228 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1231 if (sk_unhashed(sk) && sk->sk_reuseport) {
1232 err = reuseport_alloc(sk);
1235 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1236 /* The socket wasn't bound with SO_REUSEPORT */
1240 old_prog = reuseport_attach_prog(sk, prog);
1242 bpf_prog_destroy(old_prog);
1248 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1250 unsigned int fsize = bpf_classic_proglen(fprog);
1251 struct bpf_prog *prog;
1254 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1255 return ERR_PTR(-EPERM);
1257 /* Make sure new filter is there and in the right amounts. */
1258 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1259 return ERR_PTR(-EINVAL);
1261 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1263 return ERR_PTR(-ENOMEM);
1265 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1266 __bpf_prog_free(prog);
1267 return ERR_PTR(-EFAULT);
1270 prog->len = fprog->len;
1272 err = bpf_prog_store_orig_filter(prog, fprog);
1274 __bpf_prog_free(prog);
1275 return ERR_PTR(-ENOMEM);
1278 /* bpf_prepare_filter() already takes care of freeing
1279 * memory in case something goes wrong.
1281 return bpf_prepare_filter(prog, NULL);
1285 * sk_attach_filter - attach a socket filter
1286 * @fprog: the filter program
1287 * @sk: the socket to use
1289 * Attach the user's filter code. We first run some sanity checks on
1290 * it to make sure it does not explode on us later. If an error
1291 * occurs or there is insufficient memory for the filter a negative
1292 * errno code is returned. On success the return is zero.
1294 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1296 struct bpf_prog *prog = __get_filter(fprog, sk);
1300 return PTR_ERR(prog);
1302 err = __sk_attach_prog(prog, sk);
1304 __bpf_prog_release(prog);
1310 EXPORT_SYMBOL_GPL(sk_attach_filter);
1312 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1314 struct bpf_prog *prog = __get_filter(fprog, sk);
1318 return PTR_ERR(prog);
1320 err = __reuseport_attach_prog(prog, sk);
1322 __bpf_prog_release(prog);
1329 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1331 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1332 return ERR_PTR(-EPERM);
1334 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1337 int sk_attach_bpf(u32 ufd, struct sock *sk)
1339 struct bpf_prog *prog = __get_bpf(ufd, sk);
1343 return PTR_ERR(prog);
1345 err = __sk_attach_prog(prog, sk);
1354 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1356 struct bpf_prog *prog = __get_bpf(ufd, sk);
1360 return PTR_ERR(prog);
1362 err = __reuseport_attach_prog(prog, sk);
1371 struct bpf_scratchpad {
1373 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1374 u8 buff[MAX_BPF_STACK];
1378 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1380 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1381 unsigned int write_len)
1383 return skb_ensure_writable(skb, write_len);
1386 static inline int bpf_try_make_writable(struct sk_buff *skb,
1387 unsigned int write_len)
1389 int err = __bpf_try_make_writable(skb, write_len);
1391 bpf_compute_data_end(skb);
1395 static int bpf_try_make_head_writable(struct sk_buff *skb)
1397 return bpf_try_make_writable(skb, skb_headlen(skb));
1400 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1402 if (skb_at_tc_ingress(skb))
1403 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1406 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1408 if (skb_at_tc_ingress(skb))
1409 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1412 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1413 const void *, from, u32, len, u64, flags)
1417 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1419 if (unlikely(offset > 0xffff))
1421 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1424 ptr = skb->data + offset;
1425 if (flags & BPF_F_RECOMPUTE_CSUM)
1426 __skb_postpull_rcsum(skb, ptr, len, offset);
1428 memcpy(ptr, from, len);
1430 if (flags & BPF_F_RECOMPUTE_CSUM)
1431 __skb_postpush_rcsum(skb, ptr, len, offset);
1432 if (flags & BPF_F_INVALIDATE_HASH)
1433 skb_clear_hash(skb);
1438 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1439 .func = bpf_skb_store_bytes,
1441 .ret_type = RET_INTEGER,
1442 .arg1_type = ARG_PTR_TO_CTX,
1443 .arg2_type = ARG_ANYTHING,
1444 .arg3_type = ARG_PTR_TO_MEM,
1445 .arg4_type = ARG_CONST_SIZE,
1446 .arg5_type = ARG_ANYTHING,
1449 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1450 void *, to, u32, len)
1454 if (unlikely(offset > 0xffff))
1457 ptr = skb_header_pointer(skb, offset, len, to);
1461 memcpy(to, ptr, len);
1469 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1470 .func = bpf_skb_load_bytes,
1472 .ret_type = RET_INTEGER,
1473 .arg1_type = ARG_PTR_TO_CTX,
1474 .arg2_type = ARG_ANYTHING,
1475 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1476 .arg4_type = ARG_CONST_SIZE,
1479 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1481 /* Idea is the following: should the needed direct read/write
1482 * test fail during runtime, we can pull in more data and redo
1483 * again, since implicitly, we invalidate previous checks here.
1485 * Or, since we know how much we need to make read/writeable,
1486 * this can be done once at the program beginning for direct
1487 * access case. By this we overcome limitations of only current
1488 * headroom being accessible.
1490 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1493 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1494 .func = bpf_skb_pull_data,
1496 .ret_type = RET_INTEGER,
1497 .arg1_type = ARG_PTR_TO_CTX,
1498 .arg2_type = ARG_ANYTHING,
1501 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1502 u64, from, u64, to, u64, flags)
1506 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1508 if (unlikely(offset > 0xffff || offset & 1))
1510 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1513 ptr = (__sum16 *)(skb->data + offset);
1514 switch (flags & BPF_F_HDR_FIELD_MASK) {
1516 if (unlikely(from != 0))
1519 csum_replace_by_diff(ptr, to);
1522 csum_replace2(ptr, from, to);
1525 csum_replace4(ptr, from, to);
1534 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1535 .func = bpf_l3_csum_replace,
1537 .ret_type = RET_INTEGER,
1538 .arg1_type = ARG_PTR_TO_CTX,
1539 .arg2_type = ARG_ANYTHING,
1540 .arg3_type = ARG_ANYTHING,
1541 .arg4_type = ARG_ANYTHING,
1542 .arg5_type = ARG_ANYTHING,
1545 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1546 u64, from, u64, to, u64, flags)
1548 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1549 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1550 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1553 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1554 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1556 if (unlikely(offset > 0xffff || offset & 1))
1558 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1561 ptr = (__sum16 *)(skb->data + offset);
1562 if (is_mmzero && !do_mforce && !*ptr)
1565 switch (flags & BPF_F_HDR_FIELD_MASK) {
1567 if (unlikely(from != 0))
1570 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1573 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1576 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1582 if (is_mmzero && !*ptr)
1583 *ptr = CSUM_MANGLED_0;
1587 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1588 .func = bpf_l4_csum_replace,
1590 .ret_type = RET_INTEGER,
1591 .arg1_type = ARG_PTR_TO_CTX,
1592 .arg2_type = ARG_ANYTHING,
1593 .arg3_type = ARG_ANYTHING,
1594 .arg4_type = ARG_ANYTHING,
1595 .arg5_type = ARG_ANYTHING,
1598 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1599 __be32 *, to, u32, to_size, __wsum, seed)
1601 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1602 u32 diff_size = from_size + to_size;
1605 /* This is quite flexible, some examples:
1607 * from_size == 0, to_size > 0, seed := csum --> pushing data
1608 * from_size > 0, to_size == 0, seed := csum --> pulling data
1609 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1611 * Even for diffing, from_size and to_size don't need to be equal.
1613 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1614 diff_size > sizeof(sp->diff)))
1617 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1618 sp->diff[j] = ~from[i];
1619 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1620 sp->diff[j] = to[i];
1622 return csum_partial(sp->diff, diff_size, seed);
1625 static const struct bpf_func_proto bpf_csum_diff_proto = {
1626 .func = bpf_csum_diff,
1629 .ret_type = RET_INTEGER,
1630 .arg1_type = ARG_PTR_TO_MEM,
1631 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1632 .arg3_type = ARG_PTR_TO_MEM,
1633 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1634 .arg5_type = ARG_ANYTHING,
1637 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1639 /* The interface is to be used in combination with bpf_csum_diff()
1640 * for direct packet writes. csum rotation for alignment as well
1641 * as emulating csum_sub() can be done from the eBPF program.
1643 if (skb->ip_summed == CHECKSUM_COMPLETE)
1644 return (skb->csum = csum_add(skb->csum, csum));
1649 static const struct bpf_func_proto bpf_csum_update_proto = {
1650 .func = bpf_csum_update,
1652 .ret_type = RET_INTEGER,
1653 .arg1_type = ARG_PTR_TO_CTX,
1654 .arg2_type = ARG_ANYTHING,
1657 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1659 return dev_forward_skb(dev, skb);
1662 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1663 struct sk_buff *skb)
1665 int ret = ____dev_forward_skb(dev, skb);
1669 ret = netif_rx(skb);
1675 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1679 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1680 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1687 __this_cpu_inc(xmit_recursion);
1688 ret = dev_queue_xmit(skb);
1689 __this_cpu_dec(xmit_recursion);
1694 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1697 /* skb->mac_len is not set on normal egress */
1698 unsigned int mlen = skb->network_header - skb->mac_header;
1700 __skb_pull(skb, mlen);
1702 /* At ingress, the mac header has already been pulled once.
1703 * At egress, skb_pospull_rcsum has to be done in case that
1704 * the skb is originated from ingress (i.e. a forwarded skb)
1705 * to ensure that rcsum starts at net header.
1707 if (!skb_at_tc_ingress(skb))
1708 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1709 skb_pop_mac_header(skb);
1710 skb_reset_mac_len(skb);
1711 return flags & BPF_F_INGRESS ?
1712 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1715 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1718 /* Verify that a link layer header is carried */
1719 if (unlikely(skb->mac_header >= skb->network_header)) {
1724 bpf_push_mac_rcsum(skb);
1725 return flags & BPF_F_INGRESS ?
1726 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1729 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1732 if (dev_is_mac_header_xmit(dev))
1733 return __bpf_redirect_common(skb, dev, flags);
1735 return __bpf_redirect_no_mac(skb, dev, flags);
1738 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1740 struct net_device *dev;
1741 struct sk_buff *clone;
1744 if (unlikely(flags & ~(BPF_F_INGRESS)))
1747 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1751 clone = skb_clone(skb, GFP_ATOMIC);
1752 if (unlikely(!clone))
1755 /* For direct write, we need to keep the invariant that the skbs
1756 * we're dealing with need to be uncloned. Should uncloning fail
1757 * here, we need to free the just generated clone to unclone once
1760 ret = bpf_try_make_head_writable(skb);
1761 if (unlikely(ret)) {
1766 return __bpf_redirect(clone, dev, flags);
1769 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1770 .func = bpf_clone_redirect,
1772 .ret_type = RET_INTEGER,
1773 .arg1_type = ARG_PTR_TO_CTX,
1774 .arg2_type = ARG_ANYTHING,
1775 .arg3_type = ARG_ANYTHING,
1778 struct redirect_info {
1783 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1785 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1787 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1789 if (unlikely(flags & ~(BPF_F_INGRESS)))
1792 ri->ifindex = ifindex;
1795 return TC_ACT_REDIRECT;
1798 int skb_do_redirect(struct sk_buff *skb)
1800 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1801 struct net_device *dev;
1803 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1805 if (unlikely(!dev)) {
1810 return __bpf_redirect(skb, dev, ri->flags);
1813 static const struct bpf_func_proto bpf_redirect_proto = {
1814 .func = bpf_redirect,
1816 .ret_type = RET_INTEGER,
1817 .arg1_type = ARG_ANYTHING,
1818 .arg2_type = ARG_ANYTHING,
1821 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
1823 return task_get_classid(skb);
1826 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1827 .func = bpf_get_cgroup_classid,
1829 .ret_type = RET_INTEGER,
1830 .arg1_type = ARG_PTR_TO_CTX,
1833 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
1835 return dst_tclassid(skb);
1838 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1839 .func = bpf_get_route_realm,
1841 .ret_type = RET_INTEGER,
1842 .arg1_type = ARG_PTR_TO_CTX,
1845 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
1847 /* If skb_clear_hash() was called due to mangling, we can
1848 * trigger SW recalculation here. Later access to hash
1849 * can then use the inline skb->hash via context directly
1850 * instead of calling this helper again.
1852 return skb_get_hash(skb);
1855 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1856 .func = bpf_get_hash_recalc,
1858 .ret_type = RET_INTEGER,
1859 .arg1_type = ARG_PTR_TO_CTX,
1862 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1864 /* After all direct packet write, this can be used once for
1865 * triggering a lazy recalc on next skb_get_hash() invocation.
1867 skb_clear_hash(skb);
1871 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1872 .func = bpf_set_hash_invalid,
1874 .ret_type = RET_INTEGER,
1875 .arg1_type = ARG_PTR_TO_CTX,
1878 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
1880 /* Set user specified hash as L4(+), so that it gets returned
1881 * on skb_get_hash() call unless BPF prog later on triggers a
1884 __skb_set_sw_hash(skb, hash, true);
1888 static const struct bpf_func_proto bpf_set_hash_proto = {
1889 .func = bpf_set_hash,
1891 .ret_type = RET_INTEGER,
1892 .arg1_type = ARG_PTR_TO_CTX,
1893 .arg2_type = ARG_ANYTHING,
1896 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1901 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1902 vlan_proto != htons(ETH_P_8021AD)))
1903 vlan_proto = htons(ETH_P_8021Q);
1905 bpf_push_mac_rcsum(skb);
1906 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1907 bpf_pull_mac_rcsum(skb);
1909 bpf_compute_data_end(skb);
1913 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1914 .func = bpf_skb_vlan_push,
1916 .ret_type = RET_INTEGER,
1917 .arg1_type = ARG_PTR_TO_CTX,
1918 .arg2_type = ARG_ANYTHING,
1919 .arg3_type = ARG_ANYTHING,
1921 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1923 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
1927 bpf_push_mac_rcsum(skb);
1928 ret = skb_vlan_pop(skb);
1929 bpf_pull_mac_rcsum(skb);
1931 bpf_compute_data_end(skb);
1935 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1936 .func = bpf_skb_vlan_pop,
1938 .ret_type = RET_INTEGER,
1939 .arg1_type = ARG_PTR_TO_CTX,
1941 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
1943 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1945 /* Caller already did skb_cow() with len as headroom,
1946 * so no need to do it here.
1949 memmove(skb->data, skb->data + len, off);
1950 memset(skb->data + off, 0, len);
1952 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1953 * needed here as it does not change the skb->csum
1954 * result for checksum complete when summing over
1960 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1962 /* skb_ensure_writable() is not needed here, as we're
1963 * already working on an uncloned skb.
1965 if (unlikely(!pskb_may_pull(skb, off + len)))
1968 skb_postpull_rcsum(skb, skb->data + off, len);
1969 memmove(skb->data + len, skb->data, off);
1970 __skb_pull(skb, len);
1975 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1977 bool trans_same = skb->transport_header == skb->network_header;
1980 /* There's no need for __skb_push()/__skb_pull() pair to
1981 * get to the start of the mac header as we're guaranteed
1982 * to always start from here under eBPF.
1984 ret = bpf_skb_generic_push(skb, off, len);
1986 skb->mac_header -= len;
1987 skb->network_header -= len;
1989 skb->transport_header = skb->network_header;
1995 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1997 bool trans_same = skb->transport_header == skb->network_header;
2000 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2001 ret = bpf_skb_generic_pop(skb, off, len);
2003 skb->mac_header += len;
2004 skb->network_header += len;
2006 skb->transport_header = skb->network_header;
2012 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2014 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2015 u32 off = skb->network_header - skb->mac_header;
2018 ret = skb_cow(skb, len_diff);
2019 if (unlikely(ret < 0))
2022 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2023 if (unlikely(ret < 0))
2026 if (skb_is_gso(skb)) {
2027 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
2028 * be changed into SKB_GSO_TCPV6.
2030 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2031 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
2032 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
2035 /* Due to IPv6 header, MSS needs to be downgraded. */
2036 skb_shinfo(skb)->gso_size -= len_diff;
2037 /* Header must be checked, and gso_segs recomputed. */
2038 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2039 skb_shinfo(skb)->gso_segs = 0;
2042 skb->protocol = htons(ETH_P_IPV6);
2043 skb_clear_hash(skb);
2048 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2050 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2051 u32 off = skb->network_header - skb->mac_header;
2054 ret = skb_unclone(skb, GFP_ATOMIC);
2055 if (unlikely(ret < 0))
2058 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2059 if (unlikely(ret < 0))
2062 if (skb_is_gso(skb)) {
2063 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
2064 * be changed into SKB_GSO_TCPV4.
2066 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2067 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2068 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2071 /* Due to IPv4 header, MSS can be upgraded. */
2072 skb_shinfo(skb)->gso_size += len_diff;
2073 /* Header must be checked, and gso_segs recomputed. */
2074 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2075 skb_shinfo(skb)->gso_segs = 0;
2078 skb->protocol = htons(ETH_P_IP);
2079 skb_clear_hash(skb);
2084 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2086 __be16 from_proto = skb->protocol;
2088 if (from_proto == htons(ETH_P_IP) &&
2089 to_proto == htons(ETH_P_IPV6))
2090 return bpf_skb_proto_4_to_6(skb);
2092 if (from_proto == htons(ETH_P_IPV6) &&
2093 to_proto == htons(ETH_P_IP))
2094 return bpf_skb_proto_6_to_4(skb);
2099 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2104 if (unlikely(flags))
2107 /* General idea is that this helper does the basic groundwork
2108 * needed for changing the protocol, and eBPF program fills the
2109 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2110 * and other helpers, rather than passing a raw buffer here.
2112 * The rationale is to keep this minimal and without a need to
2113 * deal with raw packet data. F.e. even if we would pass buffers
2114 * here, the program still needs to call the bpf_lX_csum_replace()
2115 * helpers anyway. Plus, this way we keep also separation of
2116 * concerns, since f.e. bpf_skb_store_bytes() should only take
2119 * Currently, additional options and extension header space are
2120 * not supported, but flags register is reserved so we can adapt
2121 * that. For offloads, we mark packet as dodgy, so that headers
2122 * need to be verified first.
2124 ret = bpf_skb_proto_xlat(skb, proto);
2125 bpf_compute_data_end(skb);
2129 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2130 .func = bpf_skb_change_proto,
2132 .ret_type = RET_INTEGER,
2133 .arg1_type = ARG_PTR_TO_CTX,
2134 .arg2_type = ARG_ANYTHING,
2135 .arg3_type = ARG_ANYTHING,
2138 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2140 /* We only allow a restricted subset to be changed for now. */
2141 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2142 !skb_pkt_type_ok(pkt_type)))
2145 skb->pkt_type = pkt_type;
2149 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2150 .func = bpf_skb_change_type,
2152 .ret_type = RET_INTEGER,
2153 .arg1_type = ARG_PTR_TO_CTX,
2154 .arg2_type = ARG_ANYTHING,
2157 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2159 u32 min_len = skb_network_offset(skb);
2161 if (skb_transport_header_was_set(skb))
2162 min_len = skb_transport_offset(skb);
2163 if (skb->ip_summed == CHECKSUM_PARTIAL)
2164 min_len = skb_checksum_start_offset(skb) +
2165 skb->csum_offset + sizeof(__sum16);
2169 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2171 return skb->dev->mtu + skb->dev->hard_header_len;
2174 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2176 unsigned int old_len = skb->len;
2179 ret = __skb_grow_rcsum(skb, new_len);
2181 memset(skb->data + old_len, 0, new_len - old_len);
2185 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2187 return __skb_trim_rcsum(skb, new_len);
2190 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2193 u32 max_len = __bpf_skb_max_len(skb);
2194 u32 min_len = __bpf_skb_min_len(skb);
2197 if (unlikely(flags || new_len > max_len || new_len < min_len))
2199 if (skb->encapsulation)
2202 /* The basic idea of this helper is that it's performing the
2203 * needed work to either grow or trim an skb, and eBPF program
2204 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2205 * bpf_lX_csum_replace() and others rather than passing a raw
2206 * buffer here. This one is a slow path helper and intended
2207 * for replies with control messages.
2209 * Like in bpf_skb_change_proto(), we want to keep this rather
2210 * minimal and without protocol specifics so that we are able
2211 * to separate concerns as in bpf_skb_store_bytes() should only
2212 * be the one responsible for writing buffers.
2214 * It's really expected to be a slow path operation here for
2215 * control message replies, so we're implicitly linearizing,
2216 * uncloning and drop offloads from the skb by this.
2218 ret = __bpf_try_make_writable(skb, skb->len);
2220 if (new_len > skb->len)
2221 ret = bpf_skb_grow_rcsum(skb, new_len);
2222 else if (new_len < skb->len)
2223 ret = bpf_skb_trim_rcsum(skb, new_len);
2224 if (!ret && skb_is_gso(skb))
2228 bpf_compute_data_end(skb);
2232 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2233 .func = bpf_skb_change_tail,
2235 .ret_type = RET_INTEGER,
2236 .arg1_type = ARG_PTR_TO_CTX,
2237 .arg2_type = ARG_ANYTHING,
2238 .arg3_type = ARG_ANYTHING,
2241 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2244 u32 max_len = __bpf_skb_max_len(skb);
2245 u32 new_len = skb->len + head_room;
2248 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2249 new_len < skb->len))
2252 ret = skb_cow(skb, head_room);
2254 /* Idea for this helper is that we currently only
2255 * allow to expand on mac header. This means that
2256 * skb->protocol network header, etc, stay as is.
2257 * Compared to bpf_skb_change_tail(), we're more
2258 * flexible due to not needing to linearize or
2259 * reset GSO. Intention for this helper is to be
2260 * used by an L3 skb that needs to push mac header
2261 * for redirection into L2 device.
2263 __skb_push(skb, head_room);
2264 memset(skb->data, 0, head_room);
2265 skb_reset_mac_header(skb);
2268 bpf_compute_data_end(skb);
2272 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2273 .func = bpf_skb_change_head,
2275 .ret_type = RET_INTEGER,
2276 .arg1_type = ARG_PTR_TO_CTX,
2277 .arg2_type = ARG_ANYTHING,
2278 .arg3_type = ARG_ANYTHING,
2281 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2283 void *data = xdp->data + offset;
2285 if (unlikely(data < xdp->data_hard_start ||
2286 data > xdp->data_end - ETH_HLEN))
2294 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2295 .func = bpf_xdp_adjust_head,
2297 .ret_type = RET_INTEGER,
2298 .arg1_type = ARG_PTR_TO_CTX,
2299 .arg2_type = ARG_ANYTHING,
2302 bool bpf_helper_changes_pkt_data(void *func)
2304 if (func == bpf_skb_vlan_push ||
2305 func == bpf_skb_vlan_pop ||
2306 func == bpf_skb_store_bytes ||
2307 func == bpf_skb_change_proto ||
2308 func == bpf_skb_change_head ||
2309 func == bpf_skb_change_tail ||
2310 func == bpf_skb_pull_data ||
2311 func == bpf_clone_redirect ||
2312 func == bpf_l3_csum_replace ||
2313 func == bpf_l4_csum_replace ||
2314 func == bpf_xdp_adjust_head)
2320 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2321 unsigned long off, unsigned long len)
2323 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
2327 if (ptr != dst_buff)
2328 memcpy(dst_buff, ptr, len);
2333 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2334 u64, flags, void *, meta, u64, meta_size)
2336 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2338 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2340 if (unlikely(skb_size > skb->len))
2343 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2347 static const struct bpf_func_proto bpf_skb_event_output_proto = {
2348 .func = bpf_skb_event_output,
2350 .ret_type = RET_INTEGER,
2351 .arg1_type = ARG_PTR_TO_CTX,
2352 .arg2_type = ARG_CONST_MAP_PTR,
2353 .arg3_type = ARG_ANYTHING,
2354 .arg4_type = ARG_PTR_TO_MEM,
2355 .arg5_type = ARG_CONST_SIZE,
2358 static unsigned short bpf_tunnel_key_af(u64 flags)
2360 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2363 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2364 u32, size, u64, flags)
2366 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2367 u8 compat[sizeof(struct bpf_tunnel_key)];
2371 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2375 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2379 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2382 case offsetof(struct bpf_tunnel_key, tunnel_label):
2383 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2385 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2386 /* Fixup deprecated structure layouts here, so we have
2387 * a common path later on.
2389 if (ip_tunnel_info_af(info) != AF_INET)
2392 to = (struct bpf_tunnel_key *)compat;
2399 to->tunnel_id = be64_to_cpu(info->key.tun_id);
2400 to->tunnel_tos = info->key.tos;
2401 to->tunnel_ttl = info->key.ttl;
2403 if (flags & BPF_F_TUNINFO_IPV6) {
2404 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2405 sizeof(to->remote_ipv6));
2406 to->tunnel_label = be32_to_cpu(info->key.label);
2408 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
2411 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
2412 memcpy(to_orig, to, size);
2416 memset(to_orig, 0, size);
2420 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
2421 .func = bpf_skb_get_tunnel_key,
2423 .ret_type = RET_INTEGER,
2424 .arg1_type = ARG_PTR_TO_CTX,
2425 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2426 .arg3_type = ARG_CONST_SIZE,
2427 .arg4_type = ARG_ANYTHING,
2430 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
2432 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2435 if (unlikely(!info ||
2436 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2440 if (unlikely(size < info->options_len)) {
2445 ip_tunnel_info_opts_get(to, info);
2446 if (size > info->options_len)
2447 memset(to + info->options_len, 0, size - info->options_len);
2449 return info->options_len;
2451 memset(to, 0, size);
2455 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2456 .func = bpf_skb_get_tunnel_opt,
2458 .ret_type = RET_INTEGER,
2459 .arg1_type = ARG_PTR_TO_CTX,
2460 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2461 .arg3_type = ARG_CONST_SIZE,
2464 static struct metadata_dst __percpu *md_dst;
2466 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2467 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
2469 struct metadata_dst *md = this_cpu_ptr(md_dst);
2470 u8 compat[sizeof(struct bpf_tunnel_key)];
2471 struct ip_tunnel_info *info;
2473 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2474 BPF_F_DONT_FRAGMENT)))
2476 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2478 case offsetof(struct bpf_tunnel_key, tunnel_label):
2479 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2480 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2481 /* Fixup deprecated structure layouts here, so we have
2482 * a common path later on.
2484 memcpy(compat, from, size);
2485 memset(compat + size, 0, sizeof(compat) - size);
2486 from = (const struct bpf_tunnel_key *) compat;
2492 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2497 dst_hold((struct dst_entry *) md);
2498 skb_dst_set(skb, (struct dst_entry *) md);
2500 info = &md->u.tun_info;
2501 info->mode = IP_TUNNEL_INFO_TX;
2503 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
2504 if (flags & BPF_F_DONT_FRAGMENT)
2505 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2507 info->key.tun_id = cpu_to_be64(from->tunnel_id);
2508 info->key.tos = from->tunnel_tos;
2509 info->key.ttl = from->tunnel_ttl;
2511 if (flags & BPF_F_TUNINFO_IPV6) {
2512 info->mode |= IP_TUNNEL_INFO_IPV6;
2513 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2514 sizeof(from->remote_ipv6));
2515 info->key.label = cpu_to_be32(from->tunnel_label) &
2516 IPV6_FLOWLABEL_MASK;
2518 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2519 if (flags & BPF_F_ZERO_CSUM_TX)
2520 info->key.tun_flags &= ~TUNNEL_CSUM;
2526 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
2527 .func = bpf_skb_set_tunnel_key,
2529 .ret_type = RET_INTEGER,
2530 .arg1_type = ARG_PTR_TO_CTX,
2531 .arg2_type = ARG_PTR_TO_MEM,
2532 .arg3_type = ARG_CONST_SIZE,
2533 .arg4_type = ARG_ANYTHING,
2536 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2537 const u8 *, from, u32, size)
2539 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2540 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2542 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2544 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
2547 ip_tunnel_info_opts_set(info, from, size);
2552 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2553 .func = bpf_skb_set_tunnel_opt,
2555 .ret_type = RET_INTEGER,
2556 .arg1_type = ARG_PTR_TO_CTX,
2557 .arg2_type = ARG_PTR_TO_MEM,
2558 .arg3_type = ARG_CONST_SIZE,
2561 static const struct bpf_func_proto *
2562 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
2565 /* Race is not possible, since it's called from verifier
2566 * that is holding verifier mutex.
2568 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
2576 case BPF_FUNC_skb_set_tunnel_key:
2577 return &bpf_skb_set_tunnel_key_proto;
2578 case BPF_FUNC_skb_set_tunnel_opt:
2579 return &bpf_skb_set_tunnel_opt_proto;
2585 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2588 struct bpf_array *array = container_of(map, struct bpf_array, map);
2589 struct cgroup *cgrp;
2592 sk = skb_to_full_sk(skb);
2593 if (!sk || !sk_fullsock(sk))
2595 if (unlikely(idx >= array->map.max_entries))
2598 cgrp = READ_ONCE(array->ptrs[idx]);
2599 if (unlikely(!cgrp))
2602 return sk_under_cgroup_hierarchy(sk, cgrp);
2605 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2606 .func = bpf_skb_under_cgroup,
2608 .ret_type = RET_INTEGER,
2609 .arg1_type = ARG_PTR_TO_CTX,
2610 .arg2_type = ARG_CONST_MAP_PTR,
2611 .arg3_type = ARG_ANYTHING,
2614 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2615 unsigned long off, unsigned long len)
2617 memcpy(dst_buff, src_buff + off, len);
2621 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2622 u64, flags, void *, meta, u64, meta_size)
2624 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2626 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2628 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2631 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
2632 xdp_size, bpf_xdp_copy);
2635 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2636 .func = bpf_xdp_event_output,
2638 .ret_type = RET_INTEGER,
2639 .arg1_type = ARG_PTR_TO_CTX,
2640 .arg2_type = ARG_CONST_MAP_PTR,
2641 .arg3_type = ARG_ANYTHING,
2642 .arg4_type = ARG_PTR_TO_MEM,
2643 .arg5_type = ARG_CONST_SIZE,
2646 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
2648 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
2651 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
2652 .func = bpf_get_socket_cookie,
2654 .ret_type = RET_INTEGER,
2655 .arg1_type = ARG_PTR_TO_CTX,
2658 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
2660 struct sock *sk = sk_to_full_sk(skb->sk);
2663 if (!sk || !sk_fullsock(sk))
2665 kuid = sock_net_uid(sock_net(sk), sk);
2666 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
2669 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
2670 .func = bpf_get_socket_uid,
2672 .ret_type = RET_INTEGER,
2673 .arg1_type = ARG_PTR_TO_CTX,
2676 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
2677 int, level, int, optname, char *, optval, int, optlen)
2679 struct sock *sk = bpf_sock->sk;
2683 if (!sk_fullsock(sk))
2686 if (level == SOL_SOCKET) {
2687 if (optlen != sizeof(int))
2689 val = *((int *)optval);
2691 /* Only some socketops are supported */
2694 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
2695 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
2698 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
2699 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
2701 case SO_MAX_PACING_RATE:
2702 sk->sk_max_pacing_rate = val;
2703 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
2704 sk->sk_max_pacing_rate);
2707 sk->sk_priority = val;
2712 sk->sk_rcvlowat = val ? : 1;
2720 } else if (level == SOL_TCP &&
2721 sk->sk_prot->setsockopt == tcp_setsockopt) {
2730 static const struct bpf_func_proto bpf_setsockopt_proto = {
2731 .func = bpf_setsockopt,
2733 .ret_type = RET_INTEGER,
2734 .arg1_type = ARG_PTR_TO_CTX,
2735 .arg2_type = ARG_ANYTHING,
2736 .arg3_type = ARG_ANYTHING,
2737 .arg4_type = ARG_PTR_TO_MEM,
2738 .arg5_type = ARG_CONST_SIZE,
2741 static const struct bpf_func_proto *
2742 bpf_base_func_proto(enum bpf_func_id func_id)
2745 case BPF_FUNC_map_lookup_elem:
2746 return &bpf_map_lookup_elem_proto;
2747 case BPF_FUNC_map_update_elem:
2748 return &bpf_map_update_elem_proto;
2749 case BPF_FUNC_map_delete_elem:
2750 return &bpf_map_delete_elem_proto;
2751 case BPF_FUNC_get_prandom_u32:
2752 return &bpf_get_prandom_u32_proto;
2753 case BPF_FUNC_get_smp_processor_id:
2754 return &bpf_get_raw_smp_processor_id_proto;
2755 case BPF_FUNC_get_numa_node_id:
2756 return &bpf_get_numa_node_id_proto;
2757 case BPF_FUNC_tail_call:
2758 return &bpf_tail_call_proto;
2759 case BPF_FUNC_ktime_get_ns:
2760 return &bpf_ktime_get_ns_proto;
2761 case BPF_FUNC_trace_printk:
2762 if (capable(CAP_SYS_ADMIN))
2763 return bpf_get_trace_printk_proto();
2769 static const struct bpf_func_proto *
2770 sk_filter_func_proto(enum bpf_func_id func_id)
2773 case BPF_FUNC_skb_load_bytes:
2774 return &bpf_skb_load_bytes_proto;
2775 case BPF_FUNC_get_socket_cookie:
2776 return &bpf_get_socket_cookie_proto;
2777 case BPF_FUNC_get_socket_uid:
2778 return &bpf_get_socket_uid_proto;
2780 return bpf_base_func_proto(func_id);
2784 static const struct bpf_func_proto *
2785 tc_cls_act_func_proto(enum bpf_func_id func_id)
2788 case BPF_FUNC_skb_store_bytes:
2789 return &bpf_skb_store_bytes_proto;
2790 case BPF_FUNC_skb_load_bytes:
2791 return &bpf_skb_load_bytes_proto;
2792 case BPF_FUNC_skb_pull_data:
2793 return &bpf_skb_pull_data_proto;
2794 case BPF_FUNC_csum_diff:
2795 return &bpf_csum_diff_proto;
2796 case BPF_FUNC_csum_update:
2797 return &bpf_csum_update_proto;
2798 case BPF_FUNC_l3_csum_replace:
2799 return &bpf_l3_csum_replace_proto;
2800 case BPF_FUNC_l4_csum_replace:
2801 return &bpf_l4_csum_replace_proto;
2802 case BPF_FUNC_clone_redirect:
2803 return &bpf_clone_redirect_proto;
2804 case BPF_FUNC_get_cgroup_classid:
2805 return &bpf_get_cgroup_classid_proto;
2806 case BPF_FUNC_skb_vlan_push:
2807 return &bpf_skb_vlan_push_proto;
2808 case BPF_FUNC_skb_vlan_pop:
2809 return &bpf_skb_vlan_pop_proto;
2810 case BPF_FUNC_skb_change_proto:
2811 return &bpf_skb_change_proto_proto;
2812 case BPF_FUNC_skb_change_type:
2813 return &bpf_skb_change_type_proto;
2814 case BPF_FUNC_skb_change_tail:
2815 return &bpf_skb_change_tail_proto;
2816 case BPF_FUNC_skb_get_tunnel_key:
2817 return &bpf_skb_get_tunnel_key_proto;
2818 case BPF_FUNC_skb_set_tunnel_key:
2819 return bpf_get_skb_set_tunnel_proto(func_id);
2820 case BPF_FUNC_skb_get_tunnel_opt:
2821 return &bpf_skb_get_tunnel_opt_proto;
2822 case BPF_FUNC_skb_set_tunnel_opt:
2823 return bpf_get_skb_set_tunnel_proto(func_id);
2824 case BPF_FUNC_redirect:
2825 return &bpf_redirect_proto;
2826 case BPF_FUNC_get_route_realm:
2827 return &bpf_get_route_realm_proto;
2828 case BPF_FUNC_get_hash_recalc:
2829 return &bpf_get_hash_recalc_proto;
2830 case BPF_FUNC_set_hash_invalid:
2831 return &bpf_set_hash_invalid_proto;
2832 case BPF_FUNC_set_hash:
2833 return &bpf_set_hash_proto;
2834 case BPF_FUNC_perf_event_output:
2835 return &bpf_skb_event_output_proto;
2836 case BPF_FUNC_get_smp_processor_id:
2837 return &bpf_get_smp_processor_id_proto;
2838 case BPF_FUNC_skb_under_cgroup:
2839 return &bpf_skb_under_cgroup_proto;
2840 case BPF_FUNC_get_socket_cookie:
2841 return &bpf_get_socket_cookie_proto;
2842 case BPF_FUNC_get_socket_uid:
2843 return &bpf_get_socket_uid_proto;
2845 return bpf_base_func_proto(func_id);
2849 static const struct bpf_func_proto *
2850 xdp_func_proto(enum bpf_func_id func_id)
2853 case BPF_FUNC_perf_event_output:
2854 return &bpf_xdp_event_output_proto;
2855 case BPF_FUNC_get_smp_processor_id:
2856 return &bpf_get_smp_processor_id_proto;
2857 case BPF_FUNC_xdp_adjust_head:
2858 return &bpf_xdp_adjust_head_proto;
2860 return bpf_base_func_proto(func_id);
2864 static const struct bpf_func_proto *
2865 lwt_inout_func_proto(enum bpf_func_id func_id)
2868 case BPF_FUNC_skb_load_bytes:
2869 return &bpf_skb_load_bytes_proto;
2870 case BPF_FUNC_skb_pull_data:
2871 return &bpf_skb_pull_data_proto;
2872 case BPF_FUNC_csum_diff:
2873 return &bpf_csum_diff_proto;
2874 case BPF_FUNC_get_cgroup_classid:
2875 return &bpf_get_cgroup_classid_proto;
2876 case BPF_FUNC_get_route_realm:
2877 return &bpf_get_route_realm_proto;
2878 case BPF_FUNC_get_hash_recalc:
2879 return &bpf_get_hash_recalc_proto;
2880 case BPF_FUNC_perf_event_output:
2881 return &bpf_skb_event_output_proto;
2882 case BPF_FUNC_get_smp_processor_id:
2883 return &bpf_get_smp_processor_id_proto;
2884 case BPF_FUNC_skb_under_cgroup:
2885 return &bpf_skb_under_cgroup_proto;
2887 return bpf_base_func_proto(func_id);
2891 static const struct bpf_func_proto *
2892 sock_ops_func_proto(enum bpf_func_id func_id)
2895 case BPF_FUNC_setsockopt:
2896 return &bpf_setsockopt_proto;
2898 return bpf_base_func_proto(func_id);
2902 static const struct bpf_func_proto *
2903 lwt_xmit_func_proto(enum bpf_func_id func_id)
2906 case BPF_FUNC_skb_get_tunnel_key:
2907 return &bpf_skb_get_tunnel_key_proto;
2908 case BPF_FUNC_skb_set_tunnel_key:
2909 return bpf_get_skb_set_tunnel_proto(func_id);
2910 case BPF_FUNC_skb_get_tunnel_opt:
2911 return &bpf_skb_get_tunnel_opt_proto;
2912 case BPF_FUNC_skb_set_tunnel_opt:
2913 return bpf_get_skb_set_tunnel_proto(func_id);
2914 case BPF_FUNC_redirect:
2915 return &bpf_redirect_proto;
2916 case BPF_FUNC_clone_redirect:
2917 return &bpf_clone_redirect_proto;
2918 case BPF_FUNC_skb_change_tail:
2919 return &bpf_skb_change_tail_proto;
2920 case BPF_FUNC_skb_change_head:
2921 return &bpf_skb_change_head_proto;
2922 case BPF_FUNC_skb_store_bytes:
2923 return &bpf_skb_store_bytes_proto;
2924 case BPF_FUNC_csum_update:
2925 return &bpf_csum_update_proto;
2926 case BPF_FUNC_l3_csum_replace:
2927 return &bpf_l3_csum_replace_proto;
2928 case BPF_FUNC_l4_csum_replace:
2929 return &bpf_l4_csum_replace_proto;
2930 case BPF_FUNC_set_hash_invalid:
2931 return &bpf_set_hash_invalid_proto;
2933 return lwt_inout_func_proto(func_id);
2937 static void __set_access_aux_info(int off, struct bpf_insn_access_aux *info)
2939 info->ctx_field_size = 4;
2941 case offsetof(struct __sk_buff, pkt_type) ...
2942 offsetof(struct __sk_buff, pkt_type) + sizeof(__u32) - 1:
2943 case offsetof(struct __sk_buff, vlan_present) ...
2944 offsetof(struct __sk_buff, vlan_present) + sizeof(__u32) - 1:
2945 info->converted_op_size = 1;
2947 case offsetof(struct __sk_buff, queue_mapping) ...
2948 offsetof(struct __sk_buff, queue_mapping) + sizeof(__u32) - 1:
2949 case offsetof(struct __sk_buff, protocol) ...
2950 offsetof(struct __sk_buff, protocol) + sizeof(__u32) - 1:
2951 case offsetof(struct __sk_buff, vlan_tci) ...
2952 offsetof(struct __sk_buff, vlan_tci) + sizeof(__u32) - 1:
2953 case offsetof(struct __sk_buff, vlan_proto) ...
2954 offsetof(struct __sk_buff, vlan_proto) + sizeof(__u32) - 1:
2955 case offsetof(struct __sk_buff, tc_index) ...
2956 offsetof(struct __sk_buff, tc_index) + sizeof(__u32) - 1:
2957 case offsetof(struct __sk_buff, tc_classid) ...
2958 offsetof(struct __sk_buff, tc_classid) + sizeof(__u32) - 1:
2959 info->converted_op_size = 2;
2962 info->converted_op_size = 4;
2966 static bool __is_valid_access(int off, int size, enum bpf_access_type type,
2967 struct bpf_insn_access_aux *info)
2969 if (off < 0 || off >= sizeof(struct __sk_buff))
2972 /* The verifier guarantees that size > 0. */
2973 if (off % size != 0)
2977 case offsetof(struct __sk_buff, cb[0]) ...
2978 offsetof(struct __sk_buff, cb[4]) + sizeof(__u32) - 1:
2980 offsetof(struct __sk_buff, cb[4]) + sizeof(__u32))
2983 case offsetof(struct __sk_buff, data) ...
2984 offsetof(struct __sk_buff, data) + sizeof(__u32) - 1:
2985 if (size != sizeof(__u32))
2987 info->reg_type = PTR_TO_PACKET;
2989 case offsetof(struct __sk_buff, data_end) ...
2990 offsetof(struct __sk_buff, data_end) + sizeof(__u32) - 1:
2991 if (size != sizeof(__u32))
2993 info->reg_type = PTR_TO_PACKET_END;
2996 if (type == BPF_WRITE) {
2997 if (size != sizeof(__u32))
3002 /* permit narrower load for not cb/data/data_end fields */
3003 #ifdef __LITTLE_ENDIAN
3004 allowed = (off & 0x3) == 0 && size <= 4 && (size & (size - 1)) == 0;
3006 allowed = (off & 0x3) + size == 4 && size <= 4 && (size & (size - 1)) == 0;
3010 __set_access_aux_info(off, info);
3017 static bool sk_filter_is_valid_access(int off, int size,
3018 enum bpf_access_type type,
3019 struct bpf_insn_access_aux *info)
3022 case offsetof(struct __sk_buff, tc_classid) ...
3023 offsetof(struct __sk_buff, tc_classid) + sizeof(__u32) - 1:
3024 case offsetof(struct __sk_buff, data) ...
3025 offsetof(struct __sk_buff, data) + sizeof(__u32) - 1:
3026 case offsetof(struct __sk_buff, data_end) ...
3027 offsetof(struct __sk_buff, data_end) + sizeof(__u32) - 1:
3031 if (type == BPF_WRITE) {
3033 case offsetof(struct __sk_buff, cb[0]) ...
3034 offsetof(struct __sk_buff, cb[4]) + sizeof(__u32) - 1:
3041 return __is_valid_access(off, size, type, info);
3044 static bool lwt_is_valid_access(int off, int size,
3045 enum bpf_access_type type,
3046 struct bpf_insn_access_aux *info)
3049 case offsetof(struct __sk_buff, tc_classid) ...
3050 offsetof(struct __sk_buff, tc_classid) + sizeof(__u32) - 1:
3054 if (type == BPF_WRITE) {
3056 case offsetof(struct __sk_buff, mark):
3057 case offsetof(struct __sk_buff, priority):
3058 case offsetof(struct __sk_buff, cb[0]) ...
3059 offsetof(struct __sk_buff, cb[4]) + sizeof(__u32) - 1:
3066 return __is_valid_access(off, size, type, info);
3069 static bool sock_filter_is_valid_access(int off, int size,
3070 enum bpf_access_type type,
3071 struct bpf_insn_access_aux *info)
3073 if (type == BPF_WRITE) {
3075 case offsetof(struct bpf_sock, bound_dev_if):
3082 if (off < 0 || off + size > sizeof(struct bpf_sock))
3084 /* The verifier guarantees that size > 0. */
3085 if (off % size != 0)
3087 if (size != sizeof(__u32))
3093 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
3094 const struct bpf_prog *prog)
3096 struct bpf_insn *insn = insn_buf;
3101 /* if (!skb->cloned)
3104 * (Fast-path, otherwise approximation that we might be
3105 * a clone, do the rest in helper.)
3107 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
3108 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
3109 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
3111 /* ret = bpf_skb_pull_data(skb, 0); */
3112 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
3113 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
3114 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
3115 BPF_FUNC_skb_pull_data);
3118 * return TC_ACT_SHOT;
3120 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
3121 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT);
3122 *insn++ = BPF_EXIT_INSN();
3125 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
3127 *insn++ = prog->insnsi[0];
3129 return insn - insn_buf;
3132 static bool tc_cls_act_is_valid_access(int off, int size,
3133 enum bpf_access_type type,
3134 struct bpf_insn_access_aux *info)
3136 if (type == BPF_WRITE) {
3138 case offsetof(struct __sk_buff, mark):
3139 case offsetof(struct __sk_buff, tc_index):
3140 case offsetof(struct __sk_buff, priority):
3141 case offsetof(struct __sk_buff, cb[0]) ...
3142 offsetof(struct __sk_buff, cb[4]) + sizeof(__u32) - 1:
3143 case offsetof(struct __sk_buff, tc_classid):
3150 return __is_valid_access(off, size, type, info);
3153 static bool __is_valid_xdp_access(int off, int size)
3155 if (off < 0 || off >= sizeof(struct xdp_md))
3157 if (off % size != 0)
3159 if (size != sizeof(__u32))
3165 static bool xdp_is_valid_access(int off, int size,
3166 enum bpf_access_type type,
3167 struct bpf_insn_access_aux *info)
3169 if (type == BPF_WRITE)
3173 case offsetof(struct xdp_md, data):
3174 info->reg_type = PTR_TO_PACKET;
3176 case offsetof(struct xdp_md, data_end):
3177 info->reg_type = PTR_TO_PACKET_END;
3181 return __is_valid_xdp_access(off, size);
3184 void bpf_warn_invalid_xdp_action(u32 act)
3186 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
3188 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
3190 static bool __is_valid_sock_ops_access(int off, int size)
3192 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
3194 /* The verifier guarantees that size > 0. */
3195 if (off % size != 0)
3197 if (size != sizeof(__u32))
3203 static bool sock_ops_is_valid_access(int off, int size,
3204 enum bpf_access_type type,
3205 struct bpf_insn_access_aux *info)
3207 if (type == BPF_WRITE) {
3209 case offsetof(struct bpf_sock_ops, op) ...
3210 offsetof(struct bpf_sock_ops, replylong[3]):
3217 return __is_valid_sock_ops_access(off, size);
3220 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
3221 const struct bpf_insn *si,
3222 struct bpf_insn *insn_buf,
3223 struct bpf_prog *prog)
3225 struct bpf_insn *insn = insn_buf;
3229 case offsetof(struct __sk_buff, len):
3230 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
3232 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3233 offsetof(struct sk_buff, len));
3236 case offsetof(struct __sk_buff, protocol):
3237 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
3239 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3240 offsetof(struct sk_buff, protocol));
3243 case offsetof(struct __sk_buff, vlan_proto):
3244 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
3246 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3247 offsetof(struct sk_buff, vlan_proto));
3250 case offsetof(struct __sk_buff, priority):
3251 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
3253 if (type == BPF_WRITE)
3254 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3255 offsetof(struct sk_buff, priority));
3257 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3258 offsetof(struct sk_buff, priority));
3261 case offsetof(struct __sk_buff, ingress_ifindex):
3262 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
3264 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3265 offsetof(struct sk_buff, skb_iif));
3268 case offsetof(struct __sk_buff, ifindex):
3269 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
3271 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
3272 si->dst_reg, si->src_reg,
3273 offsetof(struct sk_buff, dev));
3274 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
3275 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3276 offsetof(struct net_device, ifindex));
3279 case offsetof(struct __sk_buff, hash):
3280 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
3282 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3283 offsetof(struct sk_buff, hash));
3286 case offsetof(struct __sk_buff, mark):
3287 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
3289 if (type == BPF_WRITE)
3290 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3291 offsetof(struct sk_buff, mark));
3293 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3294 offsetof(struct sk_buff, mark));
3297 case offsetof(struct __sk_buff, pkt_type):
3298 return convert_skb_access(SKF_AD_PKTTYPE, si->dst_reg,
3301 case offsetof(struct __sk_buff, queue_mapping):
3302 return convert_skb_access(SKF_AD_QUEUE, si->dst_reg,
3305 case offsetof(struct __sk_buff, vlan_present):
3306 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
3307 si->dst_reg, si->src_reg, insn);
3309 case offsetof(struct __sk_buff, vlan_tci):
3310 return convert_skb_access(SKF_AD_VLAN_TAG,
3311 si->dst_reg, si->src_reg, insn);
3313 case offsetof(struct __sk_buff, cb[0]) ...
3314 offsetof(struct __sk_buff, cb[4]) + sizeof(__u32) - 1:
3315 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
3316 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
3317 offsetof(struct qdisc_skb_cb, data)) %
3320 prog->cb_access = 1;
3322 off -= offsetof(struct __sk_buff, cb[0]);
3323 off += offsetof(struct sk_buff, cb);
3324 off += offsetof(struct qdisc_skb_cb, data);
3325 if (type == BPF_WRITE)
3326 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
3329 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
3333 case offsetof(struct __sk_buff, tc_classid):
3334 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
3337 off -= offsetof(struct __sk_buff, tc_classid);
3338 off += offsetof(struct sk_buff, cb);
3339 off += offsetof(struct qdisc_skb_cb, tc_classid);
3340 if (type == BPF_WRITE)
3341 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
3344 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
3348 case offsetof(struct __sk_buff, data):
3349 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
3350 si->dst_reg, si->src_reg,
3351 offsetof(struct sk_buff, data));
3354 case offsetof(struct __sk_buff, data_end):
3356 off -= offsetof(struct __sk_buff, data_end);
3357 off += offsetof(struct sk_buff, cb);
3358 off += offsetof(struct bpf_skb_data_end, data_end);
3359 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
3363 case offsetof(struct __sk_buff, tc_index):
3364 #ifdef CONFIG_NET_SCHED
3365 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
3367 if (type == BPF_WRITE)
3368 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
3369 offsetof(struct sk_buff, tc_index));
3371 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3372 offsetof(struct sk_buff, tc_index));
3374 if (type == BPF_WRITE)
3375 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
3377 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3381 case offsetof(struct __sk_buff, napi_id):
3382 #if defined(CONFIG_NET_RX_BUSY_POLL)
3383 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, napi_id) != 4);
3385 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3386 offsetof(struct sk_buff, napi_id));
3387 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
3388 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3390 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3395 return insn - insn_buf;
3398 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
3399 const struct bpf_insn *si,
3400 struct bpf_insn *insn_buf,
3401 struct bpf_prog *prog)
3403 struct bpf_insn *insn = insn_buf;
3406 case offsetof(struct bpf_sock, bound_dev_if):
3407 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
3409 if (type == BPF_WRITE)
3410 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3411 offsetof(struct sock, sk_bound_dev_if));
3413 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3414 offsetof(struct sock, sk_bound_dev_if));
3417 case offsetof(struct bpf_sock, family):
3418 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
3420 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3421 offsetof(struct sock, sk_family));
3424 case offsetof(struct bpf_sock, type):
3425 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3426 offsetof(struct sock, __sk_flags_offset));
3427 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
3428 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
3431 case offsetof(struct bpf_sock, protocol):
3432 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3433 offsetof(struct sock, __sk_flags_offset));
3434 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
3435 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
3439 return insn - insn_buf;
3442 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
3443 const struct bpf_insn *si,
3444 struct bpf_insn *insn_buf,
3445 struct bpf_prog *prog)
3447 struct bpf_insn *insn = insn_buf;
3450 case offsetof(struct __sk_buff, ifindex):
3451 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
3453 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
3454 si->dst_reg, si->src_reg,
3455 offsetof(struct sk_buff, dev));
3456 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3457 offsetof(struct net_device, ifindex));
3460 return bpf_convert_ctx_access(type, si, insn_buf, prog);
3463 return insn - insn_buf;
3466 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
3467 const struct bpf_insn *si,
3468 struct bpf_insn *insn_buf,
3469 struct bpf_prog *prog)
3471 struct bpf_insn *insn = insn_buf;
3474 case offsetof(struct xdp_md, data):
3475 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
3476 si->dst_reg, si->src_reg,
3477 offsetof(struct xdp_buff, data));
3479 case offsetof(struct xdp_md, data_end):
3480 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
3481 si->dst_reg, si->src_reg,
3482 offsetof(struct xdp_buff, data_end));
3486 return insn - insn_buf;
3489 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
3490 const struct bpf_insn *si,
3491 struct bpf_insn *insn_buf,
3492 struct bpf_prog *prog)
3494 struct bpf_insn *insn = insn_buf;
3498 case offsetof(struct bpf_sock_ops, op) ...
3499 offsetof(struct bpf_sock_ops, replylong[3]):
3500 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
3501 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
3502 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
3503 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
3504 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
3505 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
3507 off -= offsetof(struct bpf_sock_ops, op);
3508 off += offsetof(struct bpf_sock_ops_kern, op);
3509 if (type == BPF_WRITE)
3510 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3513 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3517 case offsetof(struct bpf_sock_ops, family):
3518 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
3520 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3521 struct bpf_sock_ops_kern, sk),
3522 si->dst_reg, si->src_reg,
3523 offsetof(struct bpf_sock_ops_kern, sk));
3524 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3525 offsetof(struct sock_common, skc_family));
3528 case offsetof(struct bpf_sock_ops, remote_ip4):
3529 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
3531 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3532 struct bpf_sock_ops_kern, sk),
3533 si->dst_reg, si->src_reg,
3534 offsetof(struct bpf_sock_ops_kern, sk));
3535 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3536 offsetof(struct sock_common, skc_daddr));
3539 case offsetof(struct bpf_sock_ops, local_ip4):
3540 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
3542 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3543 struct bpf_sock_ops_kern, sk),
3544 si->dst_reg, si->src_reg,
3545 offsetof(struct bpf_sock_ops_kern, sk));
3546 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3547 offsetof(struct sock_common,
3551 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
3552 offsetof(struct bpf_sock_ops, remote_ip6[3]):
3553 #if IS_ENABLED(CONFIG_IPV6)
3554 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3555 skc_v6_daddr.s6_addr32[0]) != 4);
3558 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
3559 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3560 struct bpf_sock_ops_kern, sk),
3561 si->dst_reg, si->src_reg,
3562 offsetof(struct bpf_sock_ops_kern, sk));
3563 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3564 offsetof(struct sock_common,
3565 skc_v6_daddr.s6_addr32[0]) +
3568 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3572 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
3573 offsetof(struct bpf_sock_ops, local_ip6[3]):
3574 #if IS_ENABLED(CONFIG_IPV6)
3575 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3576 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
3579 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
3580 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3581 struct bpf_sock_ops_kern, sk),
3582 si->dst_reg, si->src_reg,
3583 offsetof(struct bpf_sock_ops_kern, sk));
3584 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3585 offsetof(struct sock_common,
3586 skc_v6_rcv_saddr.s6_addr32[0]) +
3589 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3593 case offsetof(struct bpf_sock_ops, remote_port):
3594 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
3596 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3597 struct bpf_sock_ops_kern, sk),
3598 si->dst_reg, si->src_reg,
3599 offsetof(struct bpf_sock_ops_kern, sk));
3600 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3601 offsetof(struct sock_common, skc_dport));
3602 #ifndef __BIG_ENDIAN_BITFIELD
3603 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
3607 case offsetof(struct bpf_sock_ops, local_port):
3608 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
3610 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3611 struct bpf_sock_ops_kern, sk),
3612 si->dst_reg, si->src_reg,
3613 offsetof(struct bpf_sock_ops_kern, sk));
3614 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3615 offsetof(struct sock_common, skc_num));
3618 return insn - insn_buf;
3621 const struct bpf_verifier_ops sk_filter_prog_ops = {
3622 .get_func_proto = sk_filter_func_proto,
3623 .is_valid_access = sk_filter_is_valid_access,
3624 .convert_ctx_access = bpf_convert_ctx_access,
3627 const struct bpf_verifier_ops tc_cls_act_prog_ops = {
3628 .get_func_proto = tc_cls_act_func_proto,
3629 .is_valid_access = tc_cls_act_is_valid_access,
3630 .convert_ctx_access = tc_cls_act_convert_ctx_access,
3631 .gen_prologue = tc_cls_act_prologue,
3632 .test_run = bpf_prog_test_run_skb,
3635 const struct bpf_verifier_ops xdp_prog_ops = {
3636 .get_func_proto = xdp_func_proto,
3637 .is_valid_access = xdp_is_valid_access,
3638 .convert_ctx_access = xdp_convert_ctx_access,
3639 .test_run = bpf_prog_test_run_xdp,
3642 const struct bpf_verifier_ops cg_skb_prog_ops = {
3643 .get_func_proto = sk_filter_func_proto,
3644 .is_valid_access = sk_filter_is_valid_access,
3645 .convert_ctx_access = bpf_convert_ctx_access,
3646 .test_run = bpf_prog_test_run_skb,
3649 const struct bpf_verifier_ops lwt_inout_prog_ops = {
3650 .get_func_proto = lwt_inout_func_proto,
3651 .is_valid_access = lwt_is_valid_access,
3652 .convert_ctx_access = bpf_convert_ctx_access,
3653 .test_run = bpf_prog_test_run_skb,
3656 const struct bpf_verifier_ops lwt_xmit_prog_ops = {
3657 .get_func_proto = lwt_xmit_func_proto,
3658 .is_valid_access = lwt_is_valid_access,
3659 .convert_ctx_access = bpf_convert_ctx_access,
3660 .gen_prologue = tc_cls_act_prologue,
3661 .test_run = bpf_prog_test_run_skb,
3664 const struct bpf_verifier_ops cg_sock_prog_ops = {
3665 .get_func_proto = bpf_base_func_proto,
3666 .is_valid_access = sock_filter_is_valid_access,
3667 .convert_ctx_access = sock_filter_convert_ctx_access,
3670 const struct bpf_verifier_ops sock_ops_prog_ops = {
3671 .get_func_proto = sock_ops_func_proto,
3672 .is_valid_access = sock_ops_is_valid_access,
3673 .convert_ctx_access = sock_ops_convert_ctx_access,
3676 int sk_detach_filter(struct sock *sk)
3679 struct sk_filter *filter;
3681 if (sock_flag(sk, SOCK_FILTER_LOCKED))
3684 filter = rcu_dereference_protected(sk->sk_filter,
3685 lockdep_sock_is_held(sk));
3687 RCU_INIT_POINTER(sk->sk_filter, NULL);
3688 sk_filter_uncharge(sk, filter);
3694 EXPORT_SYMBOL_GPL(sk_detach_filter);
3696 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
3699 struct sock_fprog_kern *fprog;
3700 struct sk_filter *filter;
3704 filter = rcu_dereference_protected(sk->sk_filter,
3705 lockdep_sock_is_held(sk));
3709 /* We're copying the filter that has been originally attached,
3710 * so no conversion/decode needed anymore. eBPF programs that
3711 * have no original program cannot be dumped through this.
3714 fprog = filter->prog->orig_prog;
3720 /* User space only enquires number of filter blocks. */
3724 if (len < fprog->len)
3728 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
3731 /* Instead of bytes, the API requests to return the number