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>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/if_packet.h>
33 #include <linux/if_arp.h>
34 #include <linux/gfp.h>
36 #include <net/protocol.h>
37 #include <net/netlink.h>
38 #include <linux/skbuff.h>
40 #include <net/flow_dissector.h>
41 #include <linux/errno.h>
42 #include <linux/timer.h>
43 #include <asm/uaccess.h>
44 #include <asm/unaligned.h>
45 #include <linux/filter.h>
46 #include <linux/ratelimit.h>
47 #include <linux/seccomp.h>
48 #include <linux/if_vlan.h>
49 #include <linux/bpf.h>
50 #include <net/sch_generic.h>
51 #include <net/cls_cgroup.h>
52 #include <net/dst_metadata.h>
54 #include <net/sock_reuseport.h>
57 * sk_filter_trim_cap - run a packet through a socket filter
58 * @sk: sock associated with &sk_buff
59 * @skb: buffer to filter
60 * @cap: limit on how short the eBPF program may trim the packet
62 * Run the eBPF program and then cut skb->data to correct size returned by
63 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
64 * than pkt_len we keep whole skb->data. This is the socket level
65 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
66 * be accepted or -EPERM if the packet should be tossed.
69 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
72 struct sk_filter *filter;
75 * If the skb was allocated from pfmemalloc reserves, only
76 * allow SOCK_MEMALLOC sockets to use it as this socket is
79 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
82 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
86 err = security_sock_rcv_skb(sk, skb);
91 filter = rcu_dereference(sk->sk_filter);
93 unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
94 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
100 EXPORT_SYMBOL(sk_filter_trim_cap);
102 BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
104 return skb_get_poff(skb);
107 BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
111 if (skb_is_nonlinear(skb))
114 if (skb->len < sizeof(struct nlattr))
117 if (a > skb->len - sizeof(struct nlattr))
120 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
122 return (void *) nla - (void *) skb->data;
127 BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
131 if (skb_is_nonlinear(skb))
134 if (skb->len < sizeof(struct nlattr))
137 if (a > skb->len - sizeof(struct nlattr))
140 nla = (struct nlattr *) &skb->data[a];
141 if (nla->nla_len > skb->len - a)
144 nla = nla_find_nested(nla, x);
146 return (void *) nla - (void *) skb->data;
151 BPF_CALL_0(__get_raw_cpu_id)
153 return raw_smp_processor_id();
156 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
157 .func = __get_raw_cpu_id,
159 .ret_type = RET_INTEGER,
162 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
163 struct bpf_insn *insn_buf)
165 struct bpf_insn *insn = insn_buf;
169 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
171 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
172 offsetof(struct sk_buff, mark));
176 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
177 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
178 #ifdef __BIG_ENDIAN_BITFIELD
179 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
184 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
186 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
187 offsetof(struct sk_buff, queue_mapping));
190 case SKF_AD_VLAN_TAG:
191 case SKF_AD_VLAN_TAG_PRESENT:
192 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
193 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
195 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
196 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
197 offsetof(struct sk_buff, vlan_tci));
198 if (skb_field == SKF_AD_VLAN_TAG) {
199 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
203 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
205 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
210 return insn - insn_buf;
213 static bool convert_bpf_extensions(struct sock_filter *fp,
214 struct bpf_insn **insnp)
216 struct bpf_insn *insn = *insnp;
220 case SKF_AD_OFF + SKF_AD_PROTOCOL:
221 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
223 /* A = *(u16 *) (CTX + offsetof(protocol)) */
224 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
225 offsetof(struct sk_buff, protocol));
226 /* A = ntohs(A) [emitting a nop or swap16] */
227 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
230 case SKF_AD_OFF + SKF_AD_PKTTYPE:
231 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
235 case SKF_AD_OFF + SKF_AD_IFINDEX:
236 case SKF_AD_OFF + SKF_AD_HATYPE:
237 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
238 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
240 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
241 BPF_REG_TMP, BPF_REG_CTX,
242 offsetof(struct sk_buff, dev));
243 /* if (tmp != 0) goto pc + 1 */
244 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
245 *insn++ = BPF_EXIT_INSN();
246 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
247 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
248 offsetof(struct net_device, ifindex));
250 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
251 offsetof(struct net_device, type));
254 case SKF_AD_OFF + SKF_AD_MARK:
255 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
259 case SKF_AD_OFF + SKF_AD_RXHASH:
260 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
262 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
263 offsetof(struct sk_buff, hash));
266 case SKF_AD_OFF + SKF_AD_QUEUE:
267 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
271 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
272 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
273 BPF_REG_A, BPF_REG_CTX, insn);
277 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
278 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
279 BPF_REG_A, BPF_REG_CTX, insn);
283 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
284 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
286 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
287 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
288 offsetof(struct sk_buff, vlan_proto));
289 /* A = ntohs(A) [emitting a nop or swap16] */
290 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
293 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
294 case SKF_AD_OFF + SKF_AD_NLATTR:
295 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
296 case SKF_AD_OFF + SKF_AD_CPU:
297 case SKF_AD_OFF + SKF_AD_RANDOM:
299 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
301 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
303 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
304 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
306 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
307 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
309 case SKF_AD_OFF + SKF_AD_NLATTR:
310 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
312 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
313 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
315 case SKF_AD_OFF + SKF_AD_CPU:
316 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
318 case SKF_AD_OFF + SKF_AD_RANDOM:
319 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
320 bpf_user_rnd_init_once();
325 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
327 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
331 /* This is just a dummy call to avoid letting the compiler
332 * evict __bpf_call_base() as an optimization. Placed here
333 * where no-one bothers.
335 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
344 * bpf_convert_filter - convert filter program
345 * @prog: the user passed filter program
346 * @len: the length of the user passed filter program
347 * @new_prog: buffer where converted program will be stored
348 * @new_len: pointer to store length of converted program
350 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
351 * Conversion workflow:
353 * 1) First pass for calculating the new program length:
354 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
356 * 2) 2nd pass to remap in two passes: 1st pass finds new
357 * jump offsets, 2nd pass remapping:
358 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
359 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
361 static int bpf_convert_filter(struct sock_filter *prog, int len,
362 struct bpf_insn *new_prog, int *new_len)
364 int new_flen = 0, pass = 0, target, i;
365 struct bpf_insn *new_insn;
366 struct sock_filter *fp;
370 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
371 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
373 if (len <= 0 || len > BPF_MAXINSNS)
377 addrs = kcalloc(len, sizeof(*addrs),
378 GFP_KERNEL | __GFP_NOWARN);
387 /* Classic BPF related prologue emission. */
389 /* Classic BPF expects A and X to be reset first. These need
390 * to be guaranteed to be the first two instructions.
392 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
393 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
395 /* All programs must keep CTX in callee saved BPF_REG_CTX.
396 * In eBPF case it's done by the compiler, here we need to
397 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
399 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
404 for (i = 0; i < len; fp++, i++) {
405 struct bpf_insn tmp_insns[6] = { };
406 struct bpf_insn *insn = tmp_insns;
409 addrs[i] = new_insn - new_prog;
412 /* All arithmetic insns and skb loads map as-is. */
413 case BPF_ALU | BPF_ADD | BPF_X:
414 case BPF_ALU | BPF_ADD | BPF_K:
415 case BPF_ALU | BPF_SUB | BPF_X:
416 case BPF_ALU | BPF_SUB | BPF_K:
417 case BPF_ALU | BPF_AND | BPF_X:
418 case BPF_ALU | BPF_AND | BPF_K:
419 case BPF_ALU | BPF_OR | BPF_X:
420 case BPF_ALU | BPF_OR | BPF_K:
421 case BPF_ALU | BPF_LSH | BPF_X:
422 case BPF_ALU | BPF_LSH | BPF_K:
423 case BPF_ALU | BPF_RSH | BPF_X:
424 case BPF_ALU | BPF_RSH | BPF_K:
425 case BPF_ALU | BPF_XOR | BPF_X:
426 case BPF_ALU | BPF_XOR | BPF_K:
427 case BPF_ALU | BPF_MUL | BPF_X:
428 case BPF_ALU | BPF_MUL | BPF_K:
429 case BPF_ALU | BPF_DIV | BPF_X:
430 case BPF_ALU | BPF_DIV | BPF_K:
431 case BPF_ALU | BPF_MOD | BPF_X:
432 case BPF_ALU | BPF_MOD | BPF_K:
433 case BPF_ALU | BPF_NEG:
434 case BPF_LD | BPF_ABS | BPF_W:
435 case BPF_LD | BPF_ABS | BPF_H:
436 case BPF_LD | BPF_ABS | BPF_B:
437 case BPF_LD | BPF_IND | BPF_W:
438 case BPF_LD | BPF_IND | BPF_H:
439 case BPF_LD | BPF_IND | BPF_B:
440 /* Check for overloaded BPF extension and
441 * directly convert it if found, otherwise
442 * just move on with mapping.
444 if (BPF_CLASS(fp->code) == BPF_LD &&
445 BPF_MODE(fp->code) == BPF_ABS &&
446 convert_bpf_extensions(fp, &insn))
449 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
452 /* Jump transformation cannot use BPF block macros
453 * everywhere as offset calculation and target updates
454 * require a bit more work than the rest, i.e. jump
455 * opcodes map as-is, but offsets need adjustment.
458 #define BPF_EMIT_JMP \
460 if (target >= len || target < 0) \
462 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
463 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
464 insn->off -= insn - tmp_insns; \
467 case BPF_JMP | BPF_JA:
468 target = i + fp->k + 1;
469 insn->code = fp->code;
473 case BPF_JMP | BPF_JEQ | BPF_K:
474 case BPF_JMP | BPF_JEQ | BPF_X:
475 case BPF_JMP | BPF_JSET | BPF_K:
476 case BPF_JMP | BPF_JSET | BPF_X:
477 case BPF_JMP | BPF_JGT | BPF_K:
478 case BPF_JMP | BPF_JGT | BPF_X:
479 case BPF_JMP | BPF_JGE | BPF_K:
480 case BPF_JMP | BPF_JGE | BPF_X:
481 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
482 /* BPF immediates are signed, zero extend
483 * immediate into tmp register and use it
486 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
488 insn->dst_reg = BPF_REG_A;
489 insn->src_reg = BPF_REG_TMP;
492 insn->dst_reg = BPF_REG_A;
494 bpf_src = BPF_SRC(fp->code);
495 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
498 /* Common case where 'jump_false' is next insn. */
500 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
501 target = i + fp->jt + 1;
506 /* Convert JEQ into JNE when 'jump_true' is next insn. */
507 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
508 insn->code = BPF_JMP | BPF_JNE | bpf_src;
509 target = i + fp->jf + 1;
514 /* Other jumps are mapped into two insns: Jxx and JA. */
515 target = i + fp->jt + 1;
516 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
520 insn->code = BPF_JMP | BPF_JA;
521 target = i + fp->jf + 1;
525 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
526 case BPF_LDX | BPF_MSH | BPF_B:
528 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
529 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
530 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
532 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
534 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
536 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
538 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
541 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
542 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
544 case BPF_RET | BPF_A:
545 case BPF_RET | BPF_K:
546 if (BPF_RVAL(fp->code) == BPF_K)
547 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
549 *insn = BPF_EXIT_INSN();
552 /* Store to stack. */
555 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
556 BPF_ST ? BPF_REG_A : BPF_REG_X,
557 -(BPF_MEMWORDS - fp->k) * 4);
560 /* Load from stack. */
561 case BPF_LD | BPF_MEM:
562 case BPF_LDX | BPF_MEM:
563 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
564 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
565 -(BPF_MEMWORDS - fp->k) * 4);
569 case BPF_LD | BPF_IMM:
570 case BPF_LDX | BPF_IMM:
571 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
572 BPF_REG_A : BPF_REG_X, fp->k);
576 case BPF_MISC | BPF_TAX:
577 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
581 case BPF_MISC | BPF_TXA:
582 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
585 /* A = skb->len or X = skb->len */
586 case BPF_LD | BPF_W | BPF_LEN:
587 case BPF_LDX | BPF_W | BPF_LEN:
588 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
589 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
590 offsetof(struct sk_buff, len));
593 /* Access seccomp_data fields. */
594 case BPF_LDX | BPF_ABS | BPF_W:
595 /* A = *(u32 *) (ctx + K) */
596 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
599 /* Unknown instruction. */
606 memcpy(new_insn, tmp_insns,
607 sizeof(*insn) * (insn - tmp_insns));
608 new_insn += insn - tmp_insns;
612 /* Only calculating new length. */
613 *new_len = new_insn - new_prog;
618 if (new_flen != new_insn - new_prog) {
619 new_flen = new_insn - new_prog;
626 BUG_ON(*new_len != new_flen);
635 * As we dont want to clear mem[] array for each packet going through
636 * __bpf_prog_run(), we check that filter loaded by user never try to read
637 * a cell if not previously written, and we check all branches to be sure
638 * a malicious user doesn't try to abuse us.
640 static int check_load_and_stores(const struct sock_filter *filter, int flen)
642 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
645 BUILD_BUG_ON(BPF_MEMWORDS > 16);
647 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
651 memset(masks, 0xff, flen * sizeof(*masks));
653 for (pc = 0; pc < flen; pc++) {
654 memvalid &= masks[pc];
656 switch (filter[pc].code) {
659 memvalid |= (1 << filter[pc].k);
661 case BPF_LD | BPF_MEM:
662 case BPF_LDX | BPF_MEM:
663 if (!(memvalid & (1 << filter[pc].k))) {
668 case BPF_JMP | BPF_JA:
669 /* A jump must set masks on target */
670 masks[pc + 1 + filter[pc].k] &= memvalid;
673 case BPF_JMP | BPF_JEQ | BPF_K:
674 case BPF_JMP | BPF_JEQ | BPF_X:
675 case BPF_JMP | BPF_JGE | BPF_K:
676 case BPF_JMP | BPF_JGE | BPF_X:
677 case BPF_JMP | BPF_JGT | BPF_K:
678 case BPF_JMP | BPF_JGT | BPF_X:
679 case BPF_JMP | BPF_JSET | BPF_K:
680 case BPF_JMP | BPF_JSET | BPF_X:
681 /* A jump must set masks on targets */
682 masks[pc + 1 + filter[pc].jt] &= memvalid;
683 masks[pc + 1 + filter[pc].jf] &= memvalid;
693 static bool chk_code_allowed(u16 code_to_probe)
695 static const bool codes[] = {
696 /* 32 bit ALU operations */
697 [BPF_ALU | BPF_ADD | BPF_K] = true,
698 [BPF_ALU | BPF_ADD | BPF_X] = true,
699 [BPF_ALU | BPF_SUB | BPF_K] = true,
700 [BPF_ALU | BPF_SUB | BPF_X] = true,
701 [BPF_ALU | BPF_MUL | BPF_K] = true,
702 [BPF_ALU | BPF_MUL | BPF_X] = true,
703 [BPF_ALU | BPF_DIV | BPF_K] = true,
704 [BPF_ALU | BPF_DIV | BPF_X] = true,
705 [BPF_ALU | BPF_MOD | BPF_K] = true,
706 [BPF_ALU | BPF_MOD | BPF_X] = true,
707 [BPF_ALU | BPF_AND | BPF_K] = true,
708 [BPF_ALU | BPF_AND | BPF_X] = true,
709 [BPF_ALU | BPF_OR | BPF_K] = true,
710 [BPF_ALU | BPF_OR | BPF_X] = true,
711 [BPF_ALU | BPF_XOR | BPF_K] = true,
712 [BPF_ALU | BPF_XOR | BPF_X] = true,
713 [BPF_ALU | BPF_LSH | BPF_K] = true,
714 [BPF_ALU | BPF_LSH | BPF_X] = true,
715 [BPF_ALU | BPF_RSH | BPF_K] = true,
716 [BPF_ALU | BPF_RSH | BPF_X] = true,
717 [BPF_ALU | BPF_NEG] = true,
718 /* Load instructions */
719 [BPF_LD | BPF_W | BPF_ABS] = true,
720 [BPF_LD | BPF_H | BPF_ABS] = true,
721 [BPF_LD | BPF_B | BPF_ABS] = true,
722 [BPF_LD | BPF_W | BPF_LEN] = true,
723 [BPF_LD | BPF_W | BPF_IND] = true,
724 [BPF_LD | BPF_H | BPF_IND] = true,
725 [BPF_LD | BPF_B | BPF_IND] = true,
726 [BPF_LD | BPF_IMM] = true,
727 [BPF_LD | BPF_MEM] = true,
728 [BPF_LDX | BPF_W | BPF_LEN] = true,
729 [BPF_LDX | BPF_B | BPF_MSH] = true,
730 [BPF_LDX | BPF_IMM] = true,
731 [BPF_LDX | BPF_MEM] = true,
732 /* Store instructions */
735 /* Misc instructions */
736 [BPF_MISC | BPF_TAX] = true,
737 [BPF_MISC | BPF_TXA] = true,
738 /* Return instructions */
739 [BPF_RET | BPF_K] = true,
740 [BPF_RET | BPF_A] = true,
741 /* Jump instructions */
742 [BPF_JMP | BPF_JA] = true,
743 [BPF_JMP | BPF_JEQ | BPF_K] = true,
744 [BPF_JMP | BPF_JEQ | BPF_X] = true,
745 [BPF_JMP | BPF_JGE | BPF_K] = true,
746 [BPF_JMP | BPF_JGE | BPF_X] = true,
747 [BPF_JMP | BPF_JGT | BPF_K] = true,
748 [BPF_JMP | BPF_JGT | BPF_X] = true,
749 [BPF_JMP | BPF_JSET | BPF_K] = true,
750 [BPF_JMP | BPF_JSET | BPF_X] = true,
753 if (code_to_probe >= ARRAY_SIZE(codes))
756 return codes[code_to_probe];
759 static bool bpf_check_basics_ok(const struct sock_filter *filter,
764 if (flen == 0 || flen > BPF_MAXINSNS)
771 * bpf_check_classic - verify socket filter code
772 * @filter: filter to verify
773 * @flen: length of filter
775 * Check the user's filter code. If we let some ugly
776 * filter code slip through kaboom! The filter must contain
777 * no references or jumps that are out of range, no illegal
778 * instructions, and must end with a RET instruction.
780 * All jumps are forward as they are not signed.
782 * Returns 0 if the rule set is legal or -EINVAL if not.
784 static int bpf_check_classic(const struct sock_filter *filter,
790 /* Check the filter code now */
791 for (pc = 0; pc < flen; pc++) {
792 const struct sock_filter *ftest = &filter[pc];
794 /* May we actually operate on this code? */
795 if (!chk_code_allowed(ftest->code))
798 /* Some instructions need special checks */
799 switch (ftest->code) {
800 case BPF_ALU | BPF_DIV | BPF_K:
801 case BPF_ALU | BPF_MOD | BPF_K:
802 /* Check for division by zero */
806 case BPF_ALU | BPF_LSH | BPF_K:
807 case BPF_ALU | BPF_RSH | BPF_K:
811 case BPF_LD | BPF_MEM:
812 case BPF_LDX | BPF_MEM:
815 /* Check for invalid memory addresses */
816 if (ftest->k >= BPF_MEMWORDS)
819 case BPF_JMP | BPF_JA:
820 /* Note, the large ftest->k might cause loops.
821 * Compare this with conditional jumps below,
822 * where offsets are limited. --ANK (981016)
824 if (ftest->k >= (unsigned int)(flen - pc - 1))
827 case BPF_JMP | BPF_JEQ | BPF_K:
828 case BPF_JMP | BPF_JEQ | BPF_X:
829 case BPF_JMP | BPF_JGE | BPF_K:
830 case BPF_JMP | BPF_JGE | BPF_X:
831 case BPF_JMP | BPF_JGT | BPF_K:
832 case BPF_JMP | BPF_JGT | BPF_X:
833 case BPF_JMP | BPF_JSET | BPF_K:
834 case BPF_JMP | BPF_JSET | BPF_X:
835 /* Both conditionals must be safe */
836 if (pc + ftest->jt + 1 >= flen ||
837 pc + ftest->jf + 1 >= flen)
840 case BPF_LD | BPF_W | BPF_ABS:
841 case BPF_LD | BPF_H | BPF_ABS:
842 case BPF_LD | BPF_B | BPF_ABS:
844 if (bpf_anc_helper(ftest) & BPF_ANC)
846 /* Ancillary operation unknown or unsupported */
847 if (anc_found == false && ftest->k >= SKF_AD_OFF)
852 /* Last instruction must be a RET code */
853 switch (filter[flen - 1].code) {
854 case BPF_RET | BPF_K:
855 case BPF_RET | BPF_A:
856 return check_load_and_stores(filter, flen);
862 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
863 const struct sock_fprog *fprog)
865 unsigned int fsize = bpf_classic_proglen(fprog);
866 struct sock_fprog_kern *fkprog;
868 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
872 fkprog = fp->orig_prog;
873 fkprog->len = fprog->len;
875 fkprog->filter = kmemdup(fp->insns, fsize,
876 GFP_KERNEL | __GFP_NOWARN);
877 if (!fkprog->filter) {
878 kfree(fp->orig_prog);
885 static void bpf_release_orig_filter(struct bpf_prog *fp)
887 struct sock_fprog_kern *fprog = fp->orig_prog;
890 kfree(fprog->filter);
895 static void __bpf_prog_release(struct bpf_prog *prog)
897 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
900 bpf_release_orig_filter(prog);
905 static void __sk_filter_release(struct sk_filter *fp)
907 __bpf_prog_release(fp->prog);
912 * sk_filter_release_rcu - Release a socket filter by rcu_head
913 * @rcu: rcu_head that contains the sk_filter to free
915 static void sk_filter_release_rcu(struct rcu_head *rcu)
917 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
919 __sk_filter_release(fp);
923 * sk_filter_release - release a socket filter
924 * @fp: filter to remove
926 * Remove a filter from a socket and release its resources.
928 static void sk_filter_release(struct sk_filter *fp)
930 if (atomic_dec_and_test(&fp->refcnt))
931 call_rcu(&fp->rcu, sk_filter_release_rcu);
934 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
936 u32 filter_size = bpf_prog_size(fp->prog->len);
938 atomic_sub(filter_size, &sk->sk_omem_alloc);
939 sk_filter_release(fp);
942 /* try to charge the socket memory if there is space available
943 * return true on success
945 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
947 u32 filter_size = bpf_prog_size(fp->prog->len);
949 /* same check as in sock_kmalloc() */
950 if (filter_size <= sysctl_optmem_max &&
951 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
952 atomic_inc(&fp->refcnt);
953 atomic_add(filter_size, &sk->sk_omem_alloc);
959 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
961 struct sock_filter *old_prog;
962 struct bpf_prog *old_fp;
963 int err, new_len, old_len = fp->len;
965 /* We are free to overwrite insns et al right here as it
966 * won't be used at this point in time anymore internally
967 * after the migration to the internal BPF instruction
970 BUILD_BUG_ON(sizeof(struct sock_filter) !=
971 sizeof(struct bpf_insn));
973 /* Conversion cannot happen on overlapping memory areas,
974 * so we need to keep the user BPF around until the 2nd
975 * pass. At this time, the user BPF is stored in fp->insns.
977 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
978 GFP_KERNEL | __GFP_NOWARN);
984 /* 1st pass: calculate the new program length. */
985 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
989 /* Expand fp for appending the new filter representation. */
991 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
993 /* The old_fp is still around in case we couldn't
994 * allocate new memory, so uncharge on that one.
1003 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1004 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
1006 /* 2nd bpf_convert_filter() can fail only if it fails
1007 * to allocate memory, remapping must succeed. Note,
1008 * that at this time old_fp has already been released
1013 /* We are guaranteed to never error here with cBPF to eBPF
1014 * transitions, since there's no issue with type compatibility
1015 * checks on program arrays.
1017 fp = bpf_prog_select_runtime(fp, &err);
1025 __bpf_prog_release(fp);
1026 return ERR_PTR(err);
1029 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1030 bpf_aux_classic_check_t trans)
1034 fp->bpf_func = NULL;
1037 err = bpf_check_classic(fp->insns, fp->len);
1039 __bpf_prog_release(fp);
1040 return ERR_PTR(err);
1043 /* There might be additional checks and transformations
1044 * needed on classic filters, f.e. in case of seccomp.
1047 err = trans(fp->insns, fp->len);
1049 __bpf_prog_release(fp);
1050 return ERR_PTR(err);
1054 /* Probe if we can JIT compile the filter and if so, do
1055 * the compilation of the filter.
1057 bpf_jit_compile(fp);
1059 /* JIT compiler couldn't process this filter, so do the
1060 * internal BPF translation for the optimized interpreter.
1063 fp = bpf_migrate_filter(fp);
1069 * bpf_prog_create - create an unattached filter
1070 * @pfp: the unattached filter that is created
1071 * @fprog: the filter program
1073 * Create a filter independent of any socket. We first run some
1074 * sanity checks on it to make sure it does not explode on us later.
1075 * If an error occurs or there is insufficient memory for the filter
1076 * a negative errno code is returned. On success the return is zero.
1078 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1080 unsigned int fsize = bpf_classic_proglen(fprog);
1081 struct bpf_prog *fp;
1083 /* Make sure new filter is there and in the right amounts. */
1084 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1087 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1091 memcpy(fp->insns, fprog->filter, fsize);
1093 fp->len = fprog->len;
1094 /* Since unattached filters are not copied back to user
1095 * space through sk_get_filter(), we do not need to hold
1096 * a copy here, and can spare us the work.
1098 fp->orig_prog = NULL;
1100 /* bpf_prepare_filter() already takes care of freeing
1101 * memory in case something goes wrong.
1103 fp = bpf_prepare_filter(fp, NULL);
1110 EXPORT_SYMBOL_GPL(bpf_prog_create);
1113 * bpf_prog_create_from_user - create an unattached filter from user buffer
1114 * @pfp: the unattached filter that is created
1115 * @fprog: the filter program
1116 * @trans: post-classic verifier transformation handler
1117 * @save_orig: save classic BPF program
1119 * This function effectively does the same as bpf_prog_create(), only
1120 * that it builds up its insns buffer from user space provided buffer.
1121 * It also allows for passing a bpf_aux_classic_check_t handler.
1123 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1124 bpf_aux_classic_check_t trans, bool save_orig)
1126 unsigned int fsize = bpf_classic_proglen(fprog);
1127 struct bpf_prog *fp;
1130 /* Make sure new filter is there and in the right amounts. */
1131 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1134 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1138 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1139 __bpf_prog_free(fp);
1143 fp->len = fprog->len;
1144 fp->orig_prog = NULL;
1147 err = bpf_prog_store_orig_filter(fp, fprog);
1149 __bpf_prog_free(fp);
1154 /* bpf_prepare_filter() already takes care of freeing
1155 * memory in case something goes wrong.
1157 fp = bpf_prepare_filter(fp, trans);
1164 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1166 void bpf_prog_destroy(struct bpf_prog *fp)
1168 __bpf_prog_release(fp);
1170 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1172 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1174 struct sk_filter *fp, *old_fp;
1176 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1181 atomic_set(&fp->refcnt, 0);
1183 if (!sk_filter_charge(sk, fp)) {
1188 old_fp = rcu_dereference_protected(sk->sk_filter,
1189 lockdep_sock_is_held(sk));
1190 rcu_assign_pointer(sk->sk_filter, fp);
1193 sk_filter_uncharge(sk, old_fp);
1198 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1200 struct bpf_prog *old_prog;
1203 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1206 if (sk_unhashed(sk) && sk->sk_reuseport) {
1207 err = reuseport_alloc(sk);
1210 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1211 /* The socket wasn't bound with SO_REUSEPORT */
1215 old_prog = reuseport_attach_prog(sk, prog);
1217 bpf_prog_destroy(old_prog);
1223 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1225 unsigned int fsize = bpf_classic_proglen(fprog);
1226 struct bpf_prog *prog;
1229 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1230 return ERR_PTR(-EPERM);
1232 /* Make sure new filter is there and in the right amounts. */
1233 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1234 return ERR_PTR(-EINVAL);
1236 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1238 return ERR_PTR(-ENOMEM);
1240 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1241 __bpf_prog_free(prog);
1242 return ERR_PTR(-EFAULT);
1245 prog->len = fprog->len;
1247 err = bpf_prog_store_orig_filter(prog, fprog);
1249 __bpf_prog_free(prog);
1250 return ERR_PTR(-ENOMEM);
1253 /* bpf_prepare_filter() already takes care of freeing
1254 * memory in case something goes wrong.
1256 return bpf_prepare_filter(prog, NULL);
1260 * sk_attach_filter - attach a socket filter
1261 * @fprog: the filter program
1262 * @sk: the socket to use
1264 * Attach the user's filter code. We first run some sanity checks on
1265 * it to make sure it does not explode on us later. If an error
1266 * occurs or there is insufficient memory for the filter a negative
1267 * errno code is returned. On success the return is zero.
1269 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1271 struct bpf_prog *prog = __get_filter(fprog, sk);
1275 return PTR_ERR(prog);
1277 err = __sk_attach_prog(prog, sk);
1279 __bpf_prog_release(prog);
1285 EXPORT_SYMBOL_GPL(sk_attach_filter);
1287 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1289 struct bpf_prog *prog = __get_filter(fprog, sk);
1293 return PTR_ERR(prog);
1295 err = __reuseport_attach_prog(prog, sk);
1297 __bpf_prog_release(prog);
1304 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1306 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1307 return ERR_PTR(-EPERM);
1309 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1312 int sk_attach_bpf(u32 ufd, struct sock *sk)
1314 struct bpf_prog *prog = __get_bpf(ufd, sk);
1318 return PTR_ERR(prog);
1320 err = __sk_attach_prog(prog, sk);
1329 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1331 struct bpf_prog *prog = __get_bpf(ufd, sk);
1335 return PTR_ERR(prog);
1337 err = __reuseport_attach_prog(prog, sk);
1346 struct bpf_scratchpad {
1348 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1349 u8 buff[MAX_BPF_STACK];
1353 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1355 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1356 unsigned int write_len)
1358 return skb_ensure_writable(skb, write_len);
1361 static inline int bpf_try_make_writable(struct sk_buff *skb,
1362 unsigned int write_len)
1364 int err = __bpf_try_make_writable(skb, write_len);
1366 bpf_compute_data_end(skb);
1370 static int bpf_try_make_head_writable(struct sk_buff *skb)
1372 return bpf_try_make_writable(skb, skb_headlen(skb));
1375 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1377 if (skb_at_tc_ingress(skb))
1378 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1381 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1383 if (skb_at_tc_ingress(skb))
1384 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1387 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1388 const void *, from, u32, len, u64, flags)
1392 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1394 if (unlikely(offset > 0xffff))
1396 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1399 ptr = skb->data + offset;
1400 if (flags & BPF_F_RECOMPUTE_CSUM)
1401 __skb_postpull_rcsum(skb, ptr, len, offset);
1403 memcpy(ptr, from, len);
1405 if (flags & BPF_F_RECOMPUTE_CSUM)
1406 __skb_postpush_rcsum(skb, ptr, len, offset);
1407 if (flags & BPF_F_INVALIDATE_HASH)
1408 skb_clear_hash(skb);
1413 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1414 .func = bpf_skb_store_bytes,
1416 .ret_type = RET_INTEGER,
1417 .arg1_type = ARG_PTR_TO_CTX,
1418 .arg2_type = ARG_ANYTHING,
1419 .arg3_type = ARG_PTR_TO_STACK,
1420 .arg4_type = ARG_CONST_STACK_SIZE,
1421 .arg5_type = ARG_ANYTHING,
1424 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1425 void *, to, u32, len)
1429 if (unlikely(offset > 0xffff))
1432 ptr = skb_header_pointer(skb, offset, len, to);
1436 memcpy(to, ptr, len);
1444 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1445 .func = bpf_skb_load_bytes,
1447 .ret_type = RET_INTEGER,
1448 .arg1_type = ARG_PTR_TO_CTX,
1449 .arg2_type = ARG_ANYTHING,
1450 .arg3_type = ARG_PTR_TO_RAW_STACK,
1451 .arg4_type = ARG_CONST_STACK_SIZE,
1454 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1456 /* Idea is the following: should the needed direct read/write
1457 * test fail during runtime, we can pull in more data and redo
1458 * again, since implicitly, we invalidate previous checks here.
1460 * Or, since we know how much we need to make read/writeable,
1461 * this can be done once at the program beginning for direct
1462 * access case. By this we overcome limitations of only current
1463 * headroom being accessible.
1465 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1468 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1469 .func = bpf_skb_pull_data,
1471 .ret_type = RET_INTEGER,
1472 .arg1_type = ARG_PTR_TO_CTX,
1473 .arg2_type = ARG_ANYTHING,
1476 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1477 u64, from, u64, to, u64, flags)
1481 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1483 if (unlikely(offset > 0xffff || offset & 1))
1485 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1488 ptr = (__sum16 *)(skb->data + offset);
1489 switch (flags & BPF_F_HDR_FIELD_MASK) {
1491 if (unlikely(from != 0))
1494 csum_replace_by_diff(ptr, to);
1497 csum_replace2(ptr, from, to);
1500 csum_replace4(ptr, from, to);
1509 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1510 .func = bpf_l3_csum_replace,
1512 .ret_type = RET_INTEGER,
1513 .arg1_type = ARG_PTR_TO_CTX,
1514 .arg2_type = ARG_ANYTHING,
1515 .arg3_type = ARG_ANYTHING,
1516 .arg4_type = ARG_ANYTHING,
1517 .arg5_type = ARG_ANYTHING,
1520 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1521 u64, from, u64, to, u64, flags)
1523 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1524 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1527 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR |
1528 BPF_F_HDR_FIELD_MASK)))
1530 if (unlikely(offset > 0xffff || offset & 1))
1532 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1535 ptr = (__sum16 *)(skb->data + offset);
1536 if (is_mmzero && !*ptr)
1539 switch (flags & BPF_F_HDR_FIELD_MASK) {
1541 if (unlikely(from != 0))
1544 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1547 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1550 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1556 if (is_mmzero && !*ptr)
1557 *ptr = CSUM_MANGLED_0;
1561 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1562 .func = bpf_l4_csum_replace,
1564 .ret_type = RET_INTEGER,
1565 .arg1_type = ARG_PTR_TO_CTX,
1566 .arg2_type = ARG_ANYTHING,
1567 .arg3_type = ARG_ANYTHING,
1568 .arg4_type = ARG_ANYTHING,
1569 .arg5_type = ARG_ANYTHING,
1572 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1573 __be32 *, to, u32, to_size, __wsum, seed)
1575 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1576 u32 diff_size = from_size + to_size;
1579 /* This is quite flexible, some examples:
1581 * from_size == 0, to_size > 0, seed := csum --> pushing data
1582 * from_size > 0, to_size == 0, seed := csum --> pulling data
1583 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1585 * Even for diffing, from_size and to_size don't need to be equal.
1587 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1588 diff_size > sizeof(sp->diff)))
1591 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1592 sp->diff[j] = ~from[i];
1593 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1594 sp->diff[j] = to[i];
1596 return csum_partial(sp->diff, diff_size, seed);
1599 static const struct bpf_func_proto bpf_csum_diff_proto = {
1600 .func = bpf_csum_diff,
1603 .ret_type = RET_INTEGER,
1604 .arg1_type = ARG_PTR_TO_STACK,
1605 .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1606 .arg3_type = ARG_PTR_TO_STACK,
1607 .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1608 .arg5_type = ARG_ANYTHING,
1611 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1613 /* The interface is to be used in combination with bpf_csum_diff()
1614 * for direct packet writes. csum rotation for alignment as well
1615 * as emulating csum_sub() can be done from the eBPF program.
1617 if (skb->ip_summed == CHECKSUM_COMPLETE)
1618 return (skb->csum = csum_add(skb->csum, csum));
1623 static const struct bpf_func_proto bpf_csum_update_proto = {
1624 .func = bpf_csum_update,
1626 .ret_type = RET_INTEGER,
1627 .arg1_type = ARG_PTR_TO_CTX,
1628 .arg2_type = ARG_ANYTHING,
1631 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1633 return dev_forward_skb(dev, skb);
1636 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1637 struct sk_buff *skb)
1639 int ret = ____dev_forward_skb(dev, skb);
1643 ret = netif_rx(skb);
1649 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1653 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1654 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1661 __this_cpu_inc(xmit_recursion);
1662 ret = dev_queue_xmit(skb);
1663 __this_cpu_dec(xmit_recursion);
1668 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1671 /* skb->mac_len is not set on normal egress */
1672 unsigned int mlen = skb->network_header - skb->mac_header;
1674 __skb_pull(skb, mlen);
1676 /* At ingress, the mac header has already been pulled once.
1677 * At egress, skb_pospull_rcsum has to be done in case that
1678 * the skb is originated from ingress (i.e. a forwarded skb)
1679 * to ensure that rcsum starts at net header.
1681 if (!skb_at_tc_ingress(skb))
1682 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1683 skb_pop_mac_header(skb);
1684 skb_reset_mac_len(skb);
1685 return flags & BPF_F_INGRESS ?
1686 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1689 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1692 bpf_push_mac_rcsum(skb);
1693 return flags & BPF_F_INGRESS ?
1694 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1697 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1700 if (dev_is_mac_header_xmit(dev))
1701 return __bpf_redirect_common(skb, dev, flags);
1703 return __bpf_redirect_no_mac(skb, dev, flags);
1706 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1708 struct net_device *dev;
1709 struct sk_buff *clone;
1712 if (unlikely(flags & ~(BPF_F_INGRESS)))
1715 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1719 clone = skb_clone(skb, GFP_ATOMIC);
1720 if (unlikely(!clone))
1723 /* For direct write, we need to keep the invariant that the skbs
1724 * we're dealing with need to be uncloned. Should uncloning fail
1725 * here, we need to free the just generated clone to unclone once
1728 ret = bpf_try_make_head_writable(skb);
1729 if (unlikely(ret)) {
1734 return __bpf_redirect(clone, dev, flags);
1737 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1738 .func = bpf_clone_redirect,
1740 .ret_type = RET_INTEGER,
1741 .arg1_type = ARG_PTR_TO_CTX,
1742 .arg2_type = ARG_ANYTHING,
1743 .arg3_type = ARG_ANYTHING,
1746 struct redirect_info {
1751 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1753 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1755 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1757 if (unlikely(flags & ~(BPF_F_INGRESS)))
1760 ri->ifindex = ifindex;
1763 return TC_ACT_REDIRECT;
1766 int skb_do_redirect(struct sk_buff *skb)
1768 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1769 struct net_device *dev;
1771 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1773 if (unlikely(!dev)) {
1778 return __bpf_redirect(skb, dev, ri->flags);
1781 static const struct bpf_func_proto bpf_redirect_proto = {
1782 .func = bpf_redirect,
1784 .ret_type = RET_INTEGER,
1785 .arg1_type = ARG_ANYTHING,
1786 .arg2_type = ARG_ANYTHING,
1789 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
1791 return task_get_classid(skb);
1794 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1795 .func = bpf_get_cgroup_classid,
1797 .ret_type = RET_INTEGER,
1798 .arg1_type = ARG_PTR_TO_CTX,
1801 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
1803 return dst_tclassid(skb);
1806 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1807 .func = bpf_get_route_realm,
1809 .ret_type = RET_INTEGER,
1810 .arg1_type = ARG_PTR_TO_CTX,
1813 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
1815 /* If skb_clear_hash() was called due to mangling, we can
1816 * trigger SW recalculation here. Later access to hash
1817 * can then use the inline skb->hash via context directly
1818 * instead of calling this helper again.
1820 return skb_get_hash(skb);
1823 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1824 .func = bpf_get_hash_recalc,
1826 .ret_type = RET_INTEGER,
1827 .arg1_type = ARG_PTR_TO_CTX,
1830 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1832 /* After all direct packet write, this can be used once for
1833 * triggering a lazy recalc on next skb_get_hash() invocation.
1835 skb_clear_hash(skb);
1839 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1840 .func = bpf_set_hash_invalid,
1842 .ret_type = RET_INTEGER,
1843 .arg1_type = ARG_PTR_TO_CTX,
1846 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1851 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1852 vlan_proto != htons(ETH_P_8021AD)))
1853 vlan_proto = htons(ETH_P_8021Q);
1855 bpf_push_mac_rcsum(skb);
1856 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1857 bpf_pull_mac_rcsum(skb);
1859 bpf_compute_data_end(skb);
1863 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1864 .func = bpf_skb_vlan_push,
1866 .ret_type = RET_INTEGER,
1867 .arg1_type = ARG_PTR_TO_CTX,
1868 .arg2_type = ARG_ANYTHING,
1869 .arg3_type = ARG_ANYTHING,
1871 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1873 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
1877 bpf_push_mac_rcsum(skb);
1878 ret = skb_vlan_pop(skb);
1879 bpf_pull_mac_rcsum(skb);
1881 bpf_compute_data_end(skb);
1885 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1886 .func = bpf_skb_vlan_pop,
1888 .ret_type = RET_INTEGER,
1889 .arg1_type = ARG_PTR_TO_CTX,
1891 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
1893 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1895 /* Caller already did skb_cow() with len as headroom,
1896 * so no need to do it here.
1899 memmove(skb->data, skb->data + len, off);
1900 memset(skb->data + off, 0, len);
1902 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1903 * needed here as it does not change the skb->csum
1904 * result for checksum complete when summing over
1910 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1912 /* skb_ensure_writable() is not needed here, as we're
1913 * already working on an uncloned skb.
1915 if (unlikely(!pskb_may_pull(skb, off + len)))
1918 skb_postpull_rcsum(skb, skb->data + off, len);
1919 memmove(skb->data + len, skb->data, off);
1920 __skb_pull(skb, len);
1925 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1927 bool trans_same = skb->transport_header == skb->network_header;
1930 /* There's no need for __skb_push()/__skb_pull() pair to
1931 * get to the start of the mac header as we're guaranteed
1932 * to always start from here under eBPF.
1934 ret = bpf_skb_generic_push(skb, off, len);
1936 skb->mac_header -= len;
1937 skb->network_header -= len;
1939 skb->transport_header = skb->network_header;
1945 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1947 bool trans_same = skb->transport_header == skb->network_header;
1950 /* Same here, __skb_push()/__skb_pull() pair not needed. */
1951 ret = bpf_skb_generic_pop(skb, off, len);
1953 skb->mac_header += len;
1954 skb->network_header += len;
1956 skb->transport_header = skb->network_header;
1962 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
1964 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1965 u32 off = skb->network_header - skb->mac_header;
1968 ret = skb_cow(skb, len_diff);
1969 if (unlikely(ret < 0))
1972 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
1973 if (unlikely(ret < 0))
1976 if (skb_is_gso(skb)) {
1977 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
1978 * be changed into SKB_GSO_TCPV6.
1980 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1981 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
1982 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
1985 /* Due to IPv6 header, MSS needs to be downgraded. */
1986 skb_shinfo(skb)->gso_size -= len_diff;
1987 /* Header must be checked, and gso_segs recomputed. */
1988 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1989 skb_shinfo(skb)->gso_segs = 0;
1992 skb->protocol = htons(ETH_P_IPV6);
1993 skb_clear_hash(skb);
1998 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2000 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2001 u32 off = skb->network_header - skb->mac_header;
2004 ret = skb_unclone(skb, GFP_ATOMIC);
2005 if (unlikely(ret < 0))
2008 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2009 if (unlikely(ret < 0))
2012 if (skb_is_gso(skb)) {
2013 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
2014 * be changed into SKB_GSO_TCPV4.
2016 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2017 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2018 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2021 /* Due to IPv4 header, MSS can be upgraded. */
2022 skb_shinfo(skb)->gso_size += len_diff;
2023 /* Header must be checked, and gso_segs recomputed. */
2024 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2025 skb_shinfo(skb)->gso_segs = 0;
2028 skb->protocol = htons(ETH_P_IP);
2029 skb_clear_hash(skb);
2034 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2036 __be16 from_proto = skb->protocol;
2038 if (from_proto == htons(ETH_P_IP) &&
2039 to_proto == htons(ETH_P_IPV6))
2040 return bpf_skb_proto_4_to_6(skb);
2042 if (from_proto == htons(ETH_P_IPV6) &&
2043 to_proto == htons(ETH_P_IP))
2044 return bpf_skb_proto_6_to_4(skb);
2049 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2054 if (unlikely(flags))
2057 /* General idea is that this helper does the basic groundwork
2058 * needed for changing the protocol, and eBPF program fills the
2059 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2060 * and other helpers, rather than passing a raw buffer here.
2062 * The rationale is to keep this minimal and without a need to
2063 * deal with raw packet data. F.e. even if we would pass buffers
2064 * here, the program still needs to call the bpf_lX_csum_replace()
2065 * helpers anyway. Plus, this way we keep also separation of
2066 * concerns, since f.e. bpf_skb_store_bytes() should only take
2069 * Currently, additional options and extension header space are
2070 * not supported, but flags register is reserved so we can adapt
2071 * that. For offloads, we mark packet as dodgy, so that headers
2072 * need to be verified first.
2074 ret = bpf_skb_proto_xlat(skb, proto);
2075 bpf_compute_data_end(skb);
2079 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2080 .func = bpf_skb_change_proto,
2082 .ret_type = RET_INTEGER,
2083 .arg1_type = ARG_PTR_TO_CTX,
2084 .arg2_type = ARG_ANYTHING,
2085 .arg3_type = ARG_ANYTHING,
2088 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2090 /* We only allow a restricted subset to be changed for now. */
2091 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2092 !skb_pkt_type_ok(pkt_type)))
2095 skb->pkt_type = pkt_type;
2099 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2100 .func = bpf_skb_change_type,
2102 .ret_type = RET_INTEGER,
2103 .arg1_type = ARG_PTR_TO_CTX,
2104 .arg2_type = ARG_ANYTHING,
2107 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2109 u32 min_len = skb_network_offset(skb);
2111 if (skb_transport_header_was_set(skb))
2112 min_len = skb_transport_offset(skb);
2113 if (skb->ip_summed == CHECKSUM_PARTIAL)
2114 min_len = skb_checksum_start_offset(skb) +
2115 skb->csum_offset + sizeof(__sum16);
2119 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2121 return skb->dev->mtu + skb->dev->hard_header_len;
2124 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2126 unsigned int old_len = skb->len;
2129 ret = __skb_grow_rcsum(skb, new_len);
2131 memset(skb->data + old_len, 0, new_len - old_len);
2135 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2137 return __skb_trim_rcsum(skb, new_len);
2140 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2143 u32 max_len = __bpf_skb_max_len(skb);
2144 u32 min_len = __bpf_skb_min_len(skb);
2147 if (unlikely(flags || new_len > max_len || new_len < min_len))
2149 if (skb->encapsulation)
2152 /* The basic idea of this helper is that it's performing the
2153 * needed work to either grow or trim an skb, and eBPF program
2154 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2155 * bpf_lX_csum_replace() and others rather than passing a raw
2156 * buffer here. This one is a slow path helper and intended
2157 * for replies with control messages.
2159 * Like in bpf_skb_change_proto(), we want to keep this rather
2160 * minimal and without protocol specifics so that we are able
2161 * to separate concerns as in bpf_skb_store_bytes() should only
2162 * be the one responsible for writing buffers.
2164 * It's really expected to be a slow path operation here for
2165 * control message replies, so we're implicitly linearizing,
2166 * uncloning and drop offloads from the skb by this.
2168 ret = __bpf_try_make_writable(skb, skb->len);
2170 if (new_len > skb->len)
2171 ret = bpf_skb_grow_rcsum(skb, new_len);
2172 else if (new_len < skb->len)
2173 ret = bpf_skb_trim_rcsum(skb, new_len);
2174 if (!ret && skb_is_gso(skb))
2178 bpf_compute_data_end(skb);
2182 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2183 .func = bpf_skb_change_tail,
2185 .ret_type = RET_INTEGER,
2186 .arg1_type = ARG_PTR_TO_CTX,
2187 .arg2_type = ARG_ANYTHING,
2188 .arg3_type = ARG_ANYTHING,
2191 bool bpf_helper_changes_skb_data(void *func)
2193 if (func == bpf_skb_vlan_push ||
2194 func == bpf_skb_vlan_pop ||
2195 func == bpf_skb_store_bytes ||
2196 func == bpf_skb_change_proto ||
2197 func == bpf_skb_change_tail ||
2198 func == bpf_skb_pull_data ||
2199 func == bpf_l3_csum_replace ||
2200 func == bpf_l4_csum_replace)
2206 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2207 unsigned long off, unsigned long len)
2209 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
2213 if (ptr != dst_buff)
2214 memcpy(dst_buff, ptr, len);
2219 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2220 u64, flags, void *, meta, u64, meta_size)
2222 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2224 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2226 if (unlikely(skb_size > skb->len))
2229 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2233 static const struct bpf_func_proto bpf_skb_event_output_proto = {
2234 .func = bpf_skb_event_output,
2236 .ret_type = RET_INTEGER,
2237 .arg1_type = ARG_PTR_TO_CTX,
2238 .arg2_type = ARG_CONST_MAP_PTR,
2239 .arg3_type = ARG_ANYTHING,
2240 .arg4_type = ARG_PTR_TO_STACK,
2241 .arg5_type = ARG_CONST_STACK_SIZE,
2244 static unsigned short bpf_tunnel_key_af(u64 flags)
2246 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2249 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2250 u32, size, u64, flags)
2252 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2253 u8 compat[sizeof(struct bpf_tunnel_key)];
2257 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2261 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2265 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2268 case offsetof(struct bpf_tunnel_key, tunnel_label):
2269 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2271 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2272 /* Fixup deprecated structure layouts here, so we have
2273 * a common path later on.
2275 if (ip_tunnel_info_af(info) != AF_INET)
2278 to = (struct bpf_tunnel_key *)compat;
2285 to->tunnel_id = be64_to_cpu(info->key.tun_id);
2286 to->tunnel_tos = info->key.tos;
2287 to->tunnel_ttl = info->key.ttl;
2289 if (flags & BPF_F_TUNINFO_IPV6) {
2290 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2291 sizeof(to->remote_ipv6));
2292 to->tunnel_label = be32_to_cpu(info->key.label);
2294 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
2297 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
2298 memcpy(to_orig, to, size);
2302 memset(to_orig, 0, size);
2306 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
2307 .func = bpf_skb_get_tunnel_key,
2309 .ret_type = RET_INTEGER,
2310 .arg1_type = ARG_PTR_TO_CTX,
2311 .arg2_type = ARG_PTR_TO_RAW_STACK,
2312 .arg3_type = ARG_CONST_STACK_SIZE,
2313 .arg4_type = ARG_ANYTHING,
2316 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
2318 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2321 if (unlikely(!info ||
2322 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2326 if (unlikely(size < info->options_len)) {
2331 ip_tunnel_info_opts_get(to, info);
2332 if (size > info->options_len)
2333 memset(to + info->options_len, 0, size - info->options_len);
2335 return info->options_len;
2337 memset(to, 0, size);
2341 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2342 .func = bpf_skb_get_tunnel_opt,
2344 .ret_type = RET_INTEGER,
2345 .arg1_type = ARG_PTR_TO_CTX,
2346 .arg2_type = ARG_PTR_TO_RAW_STACK,
2347 .arg3_type = ARG_CONST_STACK_SIZE,
2350 static struct metadata_dst __percpu *md_dst;
2352 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2353 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
2355 struct metadata_dst *md = this_cpu_ptr(md_dst);
2356 u8 compat[sizeof(struct bpf_tunnel_key)];
2357 struct ip_tunnel_info *info;
2359 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2360 BPF_F_DONT_FRAGMENT)))
2362 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2364 case offsetof(struct bpf_tunnel_key, tunnel_label):
2365 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2366 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2367 /* Fixup deprecated structure layouts here, so we have
2368 * a common path later on.
2370 memcpy(compat, from, size);
2371 memset(compat + size, 0, sizeof(compat) - size);
2372 from = (const struct bpf_tunnel_key *) compat;
2378 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2383 dst_hold((struct dst_entry *) md);
2384 skb_dst_set(skb, (struct dst_entry *) md);
2386 info = &md->u.tun_info;
2387 info->mode = IP_TUNNEL_INFO_TX;
2389 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
2390 if (flags & BPF_F_DONT_FRAGMENT)
2391 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2393 info->key.tun_id = cpu_to_be64(from->tunnel_id);
2394 info->key.tos = from->tunnel_tos;
2395 info->key.ttl = from->tunnel_ttl;
2397 if (flags & BPF_F_TUNINFO_IPV6) {
2398 info->mode |= IP_TUNNEL_INFO_IPV6;
2399 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2400 sizeof(from->remote_ipv6));
2401 info->key.label = cpu_to_be32(from->tunnel_label) &
2402 IPV6_FLOWLABEL_MASK;
2404 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2405 if (flags & BPF_F_ZERO_CSUM_TX)
2406 info->key.tun_flags &= ~TUNNEL_CSUM;
2412 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
2413 .func = bpf_skb_set_tunnel_key,
2415 .ret_type = RET_INTEGER,
2416 .arg1_type = ARG_PTR_TO_CTX,
2417 .arg2_type = ARG_PTR_TO_STACK,
2418 .arg3_type = ARG_CONST_STACK_SIZE,
2419 .arg4_type = ARG_ANYTHING,
2422 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2423 const u8 *, from, u32, size)
2425 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2426 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2428 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2430 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
2433 ip_tunnel_info_opts_set(info, from, size);
2438 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2439 .func = bpf_skb_set_tunnel_opt,
2441 .ret_type = RET_INTEGER,
2442 .arg1_type = ARG_PTR_TO_CTX,
2443 .arg2_type = ARG_PTR_TO_STACK,
2444 .arg3_type = ARG_CONST_STACK_SIZE,
2447 static const struct bpf_func_proto *
2448 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
2451 /* Race is not possible, since it's called from verifier
2452 * that is holding verifier mutex.
2454 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
2461 case BPF_FUNC_skb_set_tunnel_key:
2462 return &bpf_skb_set_tunnel_key_proto;
2463 case BPF_FUNC_skb_set_tunnel_opt:
2464 return &bpf_skb_set_tunnel_opt_proto;
2470 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2473 struct bpf_array *array = container_of(map, struct bpf_array, map);
2474 struct cgroup *cgrp;
2477 sk = skb_to_full_sk(skb);
2478 if (!sk || !sk_fullsock(sk))
2480 if (unlikely(idx >= array->map.max_entries))
2483 cgrp = READ_ONCE(array->ptrs[idx]);
2484 if (unlikely(!cgrp))
2487 return sk_under_cgroup_hierarchy(sk, cgrp);
2490 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2491 .func = bpf_skb_under_cgroup,
2493 .ret_type = RET_INTEGER,
2494 .arg1_type = ARG_PTR_TO_CTX,
2495 .arg2_type = ARG_CONST_MAP_PTR,
2496 .arg3_type = ARG_ANYTHING,
2499 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2500 unsigned long off, unsigned long len)
2502 memcpy(dst_buff, src_buff + off, len);
2506 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2507 u64, flags, void *, meta, u64, meta_size)
2509 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2511 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2513 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2516 return bpf_event_output(map, flags, meta, meta_size, xdp, xdp_size,
2520 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2521 .func = bpf_xdp_event_output,
2523 .ret_type = RET_INTEGER,
2524 .arg1_type = ARG_PTR_TO_CTX,
2525 .arg2_type = ARG_CONST_MAP_PTR,
2526 .arg3_type = ARG_ANYTHING,
2527 .arg4_type = ARG_PTR_TO_STACK,
2528 .arg5_type = ARG_CONST_STACK_SIZE,
2531 static const struct bpf_func_proto *
2532 sk_filter_func_proto(enum bpf_func_id func_id)
2535 case BPF_FUNC_map_lookup_elem:
2536 return &bpf_map_lookup_elem_proto;
2537 case BPF_FUNC_map_update_elem:
2538 return &bpf_map_update_elem_proto;
2539 case BPF_FUNC_map_delete_elem:
2540 return &bpf_map_delete_elem_proto;
2541 case BPF_FUNC_get_prandom_u32:
2542 return &bpf_get_prandom_u32_proto;
2543 case BPF_FUNC_get_smp_processor_id:
2544 return &bpf_get_raw_smp_processor_id_proto;
2545 case BPF_FUNC_get_numa_node_id:
2546 return &bpf_get_numa_node_id_proto;
2547 case BPF_FUNC_tail_call:
2548 return &bpf_tail_call_proto;
2549 case BPF_FUNC_ktime_get_ns:
2550 return &bpf_ktime_get_ns_proto;
2551 case BPF_FUNC_trace_printk:
2552 if (capable(CAP_SYS_ADMIN))
2553 return bpf_get_trace_printk_proto();
2559 static const struct bpf_func_proto *
2560 tc_cls_act_func_proto(enum bpf_func_id func_id)
2563 case BPF_FUNC_skb_store_bytes:
2564 return &bpf_skb_store_bytes_proto;
2565 case BPF_FUNC_skb_load_bytes:
2566 return &bpf_skb_load_bytes_proto;
2567 case BPF_FUNC_skb_pull_data:
2568 return &bpf_skb_pull_data_proto;
2569 case BPF_FUNC_csum_diff:
2570 return &bpf_csum_diff_proto;
2571 case BPF_FUNC_csum_update:
2572 return &bpf_csum_update_proto;
2573 case BPF_FUNC_l3_csum_replace:
2574 return &bpf_l3_csum_replace_proto;
2575 case BPF_FUNC_l4_csum_replace:
2576 return &bpf_l4_csum_replace_proto;
2577 case BPF_FUNC_clone_redirect:
2578 return &bpf_clone_redirect_proto;
2579 case BPF_FUNC_get_cgroup_classid:
2580 return &bpf_get_cgroup_classid_proto;
2581 case BPF_FUNC_skb_vlan_push:
2582 return &bpf_skb_vlan_push_proto;
2583 case BPF_FUNC_skb_vlan_pop:
2584 return &bpf_skb_vlan_pop_proto;
2585 case BPF_FUNC_skb_change_proto:
2586 return &bpf_skb_change_proto_proto;
2587 case BPF_FUNC_skb_change_type:
2588 return &bpf_skb_change_type_proto;
2589 case BPF_FUNC_skb_change_tail:
2590 return &bpf_skb_change_tail_proto;
2591 case BPF_FUNC_skb_get_tunnel_key:
2592 return &bpf_skb_get_tunnel_key_proto;
2593 case BPF_FUNC_skb_set_tunnel_key:
2594 return bpf_get_skb_set_tunnel_proto(func_id);
2595 case BPF_FUNC_skb_get_tunnel_opt:
2596 return &bpf_skb_get_tunnel_opt_proto;
2597 case BPF_FUNC_skb_set_tunnel_opt:
2598 return bpf_get_skb_set_tunnel_proto(func_id);
2599 case BPF_FUNC_redirect:
2600 return &bpf_redirect_proto;
2601 case BPF_FUNC_get_route_realm:
2602 return &bpf_get_route_realm_proto;
2603 case BPF_FUNC_get_hash_recalc:
2604 return &bpf_get_hash_recalc_proto;
2605 case BPF_FUNC_set_hash_invalid:
2606 return &bpf_set_hash_invalid_proto;
2607 case BPF_FUNC_perf_event_output:
2608 return &bpf_skb_event_output_proto;
2609 case BPF_FUNC_get_smp_processor_id:
2610 return &bpf_get_smp_processor_id_proto;
2611 case BPF_FUNC_skb_under_cgroup:
2612 return &bpf_skb_under_cgroup_proto;
2614 return sk_filter_func_proto(func_id);
2618 static const struct bpf_func_proto *
2619 xdp_func_proto(enum bpf_func_id func_id)
2622 case BPF_FUNC_perf_event_output:
2623 return &bpf_xdp_event_output_proto;
2624 case BPF_FUNC_get_smp_processor_id:
2625 return &bpf_get_smp_processor_id_proto;
2627 return sk_filter_func_proto(func_id);
2631 static const struct bpf_func_proto *
2632 cg_skb_func_proto(enum bpf_func_id func_id)
2635 case BPF_FUNC_skb_load_bytes:
2636 return &bpf_skb_load_bytes_proto;
2638 return sk_filter_func_proto(func_id);
2642 static bool __is_valid_access(int off, int size, enum bpf_access_type type)
2644 if (off < 0 || off >= sizeof(struct __sk_buff))
2646 /* The verifier guarantees that size > 0. */
2647 if (off % size != 0)
2649 if (size != sizeof(__u32))
2655 static bool sk_filter_is_valid_access(int off, int size,
2656 enum bpf_access_type type,
2657 enum bpf_reg_type *reg_type)
2660 case offsetof(struct __sk_buff, tc_classid):
2661 case offsetof(struct __sk_buff, data):
2662 case offsetof(struct __sk_buff, data_end):
2666 if (type == BPF_WRITE) {
2668 case offsetof(struct __sk_buff, cb[0]) ...
2669 offsetof(struct __sk_buff, cb[4]):
2676 return __is_valid_access(off, size, type);
2679 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
2680 const struct bpf_prog *prog)
2682 struct bpf_insn *insn = insn_buf;
2687 /* if (!skb->cloned)
2690 * (Fast-path, otherwise approximation that we might be
2691 * a clone, do the rest in helper.)
2693 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
2694 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
2695 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
2697 /* ret = bpf_skb_pull_data(skb, 0); */
2698 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
2699 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
2700 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2701 BPF_FUNC_skb_pull_data);
2704 * return TC_ACT_SHOT;
2706 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
2707 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT);
2708 *insn++ = BPF_EXIT_INSN();
2711 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
2713 *insn++ = prog->insnsi[0];
2715 return insn - insn_buf;
2718 static bool tc_cls_act_is_valid_access(int off, int size,
2719 enum bpf_access_type type,
2720 enum bpf_reg_type *reg_type)
2722 if (type == BPF_WRITE) {
2724 case offsetof(struct __sk_buff, mark):
2725 case offsetof(struct __sk_buff, tc_index):
2726 case offsetof(struct __sk_buff, priority):
2727 case offsetof(struct __sk_buff, cb[0]) ...
2728 offsetof(struct __sk_buff, cb[4]):
2729 case offsetof(struct __sk_buff, tc_classid):
2737 case offsetof(struct __sk_buff, data):
2738 *reg_type = PTR_TO_PACKET;
2740 case offsetof(struct __sk_buff, data_end):
2741 *reg_type = PTR_TO_PACKET_END;
2745 return __is_valid_access(off, size, type);
2748 static bool __is_valid_xdp_access(int off, int size,
2749 enum bpf_access_type type)
2751 if (off < 0 || off >= sizeof(struct xdp_md))
2753 if (off % size != 0)
2755 if (size != sizeof(__u32))
2761 static bool xdp_is_valid_access(int off, int size,
2762 enum bpf_access_type type,
2763 enum bpf_reg_type *reg_type)
2765 if (type == BPF_WRITE)
2769 case offsetof(struct xdp_md, data):
2770 *reg_type = PTR_TO_PACKET;
2772 case offsetof(struct xdp_md, data_end):
2773 *reg_type = PTR_TO_PACKET_END;
2777 return __is_valid_xdp_access(off, size, type);
2780 void bpf_warn_invalid_xdp_action(u32 act)
2782 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
2784 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
2786 static u32 sk_filter_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2787 int src_reg, int ctx_off,
2788 struct bpf_insn *insn_buf,
2789 struct bpf_prog *prog)
2791 struct bpf_insn *insn = insn_buf;
2794 case offsetof(struct __sk_buff, len):
2795 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2797 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2798 offsetof(struct sk_buff, len));
2801 case offsetof(struct __sk_buff, protocol):
2802 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2804 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2805 offsetof(struct sk_buff, protocol));
2808 case offsetof(struct __sk_buff, vlan_proto):
2809 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2811 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2812 offsetof(struct sk_buff, vlan_proto));
2815 case offsetof(struct __sk_buff, priority):
2816 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2818 if (type == BPF_WRITE)
2819 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2820 offsetof(struct sk_buff, priority));
2822 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2823 offsetof(struct sk_buff, priority));
2826 case offsetof(struct __sk_buff, ingress_ifindex):
2827 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2829 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2830 offsetof(struct sk_buff, skb_iif));
2833 case offsetof(struct __sk_buff, ifindex):
2834 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2836 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
2838 offsetof(struct sk_buff, dev));
2839 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2840 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2841 offsetof(struct net_device, ifindex));
2844 case offsetof(struct __sk_buff, hash):
2845 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2847 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2848 offsetof(struct sk_buff, hash));
2851 case offsetof(struct __sk_buff, mark):
2852 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2854 if (type == BPF_WRITE)
2855 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2856 offsetof(struct sk_buff, mark));
2858 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2859 offsetof(struct sk_buff, mark));
2862 case offsetof(struct __sk_buff, pkt_type):
2863 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2865 case offsetof(struct __sk_buff, queue_mapping):
2866 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
2868 case offsetof(struct __sk_buff, vlan_present):
2869 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2870 dst_reg, src_reg, insn);
2872 case offsetof(struct __sk_buff, vlan_tci):
2873 return convert_skb_access(SKF_AD_VLAN_TAG,
2874 dst_reg, src_reg, insn);
2876 case offsetof(struct __sk_buff, cb[0]) ...
2877 offsetof(struct __sk_buff, cb[4]):
2878 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2880 prog->cb_access = 1;
2881 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2882 ctx_off += offsetof(struct sk_buff, cb);
2883 ctx_off += offsetof(struct qdisc_skb_cb, data);
2884 if (type == BPF_WRITE)
2885 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2887 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2890 case offsetof(struct __sk_buff, tc_classid):
2891 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2892 ctx_off += offsetof(struct sk_buff, cb);
2893 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
2894 if (type == BPF_WRITE)
2895 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2897 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2900 case offsetof(struct __sk_buff, data):
2901 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
2903 offsetof(struct sk_buff, data));
2906 case offsetof(struct __sk_buff, data_end):
2907 ctx_off -= offsetof(struct __sk_buff, data_end);
2908 ctx_off += offsetof(struct sk_buff, cb);
2909 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
2910 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), dst_reg, src_reg,
2914 case offsetof(struct __sk_buff, tc_index):
2915 #ifdef CONFIG_NET_SCHED
2916 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2918 if (type == BPF_WRITE)
2919 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2920 offsetof(struct sk_buff, tc_index));
2922 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2923 offsetof(struct sk_buff, tc_index));
2926 if (type == BPF_WRITE)
2927 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2929 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2934 return insn - insn_buf;
2937 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2938 int src_reg, int ctx_off,
2939 struct bpf_insn *insn_buf,
2940 struct bpf_prog *prog)
2942 struct bpf_insn *insn = insn_buf;
2945 case offsetof(struct __sk_buff, ifindex):
2946 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2948 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
2950 offsetof(struct sk_buff, dev));
2951 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2952 offsetof(struct net_device, ifindex));
2955 return sk_filter_convert_ctx_access(type, dst_reg, src_reg,
2956 ctx_off, insn_buf, prog);
2959 return insn - insn_buf;
2962 static u32 xdp_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2963 int src_reg, int ctx_off,
2964 struct bpf_insn *insn_buf,
2965 struct bpf_prog *prog)
2967 struct bpf_insn *insn = insn_buf;
2970 case offsetof(struct xdp_md, data):
2971 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
2973 offsetof(struct xdp_buff, data));
2975 case offsetof(struct xdp_md, data_end):
2976 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
2978 offsetof(struct xdp_buff, data_end));
2982 return insn - insn_buf;
2985 static const struct bpf_verifier_ops sk_filter_ops = {
2986 .get_func_proto = sk_filter_func_proto,
2987 .is_valid_access = sk_filter_is_valid_access,
2988 .convert_ctx_access = sk_filter_convert_ctx_access,
2991 static const struct bpf_verifier_ops tc_cls_act_ops = {
2992 .get_func_proto = tc_cls_act_func_proto,
2993 .is_valid_access = tc_cls_act_is_valid_access,
2994 .convert_ctx_access = tc_cls_act_convert_ctx_access,
2995 .gen_prologue = tc_cls_act_prologue,
2998 static const struct bpf_verifier_ops xdp_ops = {
2999 .get_func_proto = xdp_func_proto,
3000 .is_valid_access = xdp_is_valid_access,
3001 .convert_ctx_access = xdp_convert_ctx_access,
3004 static const struct bpf_verifier_ops cg_skb_ops = {
3005 .get_func_proto = cg_skb_func_proto,
3006 .is_valid_access = sk_filter_is_valid_access,
3007 .convert_ctx_access = sk_filter_convert_ctx_access,
3010 static struct bpf_prog_type_list sk_filter_type __read_mostly = {
3011 .ops = &sk_filter_ops,
3012 .type = BPF_PROG_TYPE_SOCKET_FILTER,
3015 static struct bpf_prog_type_list sched_cls_type __read_mostly = {
3016 .ops = &tc_cls_act_ops,
3017 .type = BPF_PROG_TYPE_SCHED_CLS,
3020 static struct bpf_prog_type_list sched_act_type __read_mostly = {
3021 .ops = &tc_cls_act_ops,
3022 .type = BPF_PROG_TYPE_SCHED_ACT,
3025 static struct bpf_prog_type_list xdp_type __read_mostly = {
3027 .type = BPF_PROG_TYPE_XDP,
3030 static struct bpf_prog_type_list cg_skb_type __read_mostly = {
3032 .type = BPF_PROG_TYPE_CGROUP_SKB,
3035 static int __init register_sk_filter_ops(void)
3037 bpf_register_prog_type(&sk_filter_type);
3038 bpf_register_prog_type(&sched_cls_type);
3039 bpf_register_prog_type(&sched_act_type);
3040 bpf_register_prog_type(&xdp_type);
3041 bpf_register_prog_type(&cg_skb_type);
3045 late_initcall(register_sk_filter_ops);
3047 int sk_detach_filter(struct sock *sk)
3050 struct sk_filter *filter;
3052 if (sock_flag(sk, SOCK_FILTER_LOCKED))
3055 filter = rcu_dereference_protected(sk->sk_filter,
3056 lockdep_sock_is_held(sk));
3058 RCU_INIT_POINTER(sk->sk_filter, NULL);
3059 sk_filter_uncharge(sk, filter);
3065 EXPORT_SYMBOL_GPL(sk_detach_filter);
3067 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
3070 struct sock_fprog_kern *fprog;
3071 struct sk_filter *filter;
3075 filter = rcu_dereference_protected(sk->sk_filter,
3076 lockdep_sock_is_held(sk));
3080 /* We're copying the filter that has been originally attached,
3081 * so no conversion/decode needed anymore. eBPF programs that
3082 * have no original program cannot be dumped through this.
3085 fprog = filter->prog->orig_prog;
3091 /* User space only enquires number of filter blocks. */
3095 if (len < fprog->len)
3099 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
3102 /* Instead of bytes, the API requests to return the number