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/gfp.h>
35 #include <net/protocol.h>
36 #include <net/netlink.h>
37 #include <linux/skbuff.h>
39 #include <net/flow_dissector.h>
40 #include <linux/errno.h>
41 #include <linux/timer.h>
42 #include <asm/uaccess.h>
43 #include <asm/unaligned.h>
44 #include <linux/filter.h>
45 #include <linux/ratelimit.h>
46 #include <linux/seccomp.h>
47 #include <linux/if_vlan.h>
48 #include <linux/bpf.h>
49 #include <net/sch_generic.h>
50 #include <net/cls_cgroup.h>
51 #include <net/dst_metadata.h>
53 #include <net/sock_reuseport.h>
56 * sk_filter_trim_cap - run a packet through a socket filter
57 * @sk: sock associated with &sk_buff
58 * @skb: buffer to filter
59 * @cap: limit on how short the eBPF program may trim the packet
61 * Run the eBPF program and then cut skb->data to correct size returned by
62 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
63 * than pkt_len we keep whole skb->data. This is the socket level
64 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
65 * be accepted or -EPERM if the packet should be tossed.
68 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
71 struct sk_filter *filter;
74 * If the skb was allocated from pfmemalloc reserves, only
75 * allow SOCK_MEMALLOC sockets to use it as this socket is
78 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
81 err = security_sock_rcv_skb(sk, skb);
86 filter = rcu_dereference(sk->sk_filter);
88 unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
89 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
95 EXPORT_SYMBOL(sk_filter_trim_cap);
97 BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
99 return skb_get_poff(skb);
102 BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
106 if (skb_is_nonlinear(skb))
109 if (skb->len < sizeof(struct nlattr))
112 if (a > skb->len - sizeof(struct nlattr))
115 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
117 return (void *) nla - (void *) skb->data;
122 BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
126 if (skb_is_nonlinear(skb))
129 if (skb->len < sizeof(struct nlattr))
132 if (a > skb->len - sizeof(struct nlattr))
135 nla = (struct nlattr *) &skb->data[a];
136 if (nla->nla_len > skb->len - a)
139 nla = nla_find_nested(nla, x);
141 return (void *) nla - (void *) skb->data;
146 BPF_CALL_0(__get_raw_cpu_id)
148 return raw_smp_processor_id();
151 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
152 .func = __get_raw_cpu_id,
154 .ret_type = RET_INTEGER,
157 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
158 struct bpf_insn *insn_buf)
160 struct bpf_insn *insn = insn_buf;
164 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
166 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
167 offsetof(struct sk_buff, mark));
171 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
172 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
173 #ifdef __BIG_ENDIAN_BITFIELD
174 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
179 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
181 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
182 offsetof(struct sk_buff, queue_mapping));
185 case SKF_AD_VLAN_TAG:
186 case SKF_AD_VLAN_TAG_PRESENT:
187 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
188 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
190 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
191 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
192 offsetof(struct sk_buff, vlan_tci));
193 if (skb_field == SKF_AD_VLAN_TAG) {
194 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
198 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
200 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
205 return insn - insn_buf;
208 static bool convert_bpf_extensions(struct sock_filter *fp,
209 struct bpf_insn **insnp)
211 struct bpf_insn *insn = *insnp;
215 case SKF_AD_OFF + SKF_AD_PROTOCOL:
216 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
218 /* A = *(u16 *) (CTX + offsetof(protocol)) */
219 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
220 offsetof(struct sk_buff, protocol));
221 /* A = ntohs(A) [emitting a nop or swap16] */
222 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
225 case SKF_AD_OFF + SKF_AD_PKTTYPE:
226 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
230 case SKF_AD_OFF + SKF_AD_IFINDEX:
231 case SKF_AD_OFF + SKF_AD_HATYPE:
232 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
233 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
235 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
236 BPF_REG_TMP, BPF_REG_CTX,
237 offsetof(struct sk_buff, dev));
238 /* if (tmp != 0) goto pc + 1 */
239 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
240 *insn++ = BPF_EXIT_INSN();
241 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
242 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
243 offsetof(struct net_device, ifindex));
245 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
246 offsetof(struct net_device, type));
249 case SKF_AD_OFF + SKF_AD_MARK:
250 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
254 case SKF_AD_OFF + SKF_AD_RXHASH:
255 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
257 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
258 offsetof(struct sk_buff, hash));
261 case SKF_AD_OFF + SKF_AD_QUEUE:
262 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
266 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
267 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
268 BPF_REG_A, BPF_REG_CTX, insn);
272 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
273 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
274 BPF_REG_A, BPF_REG_CTX, insn);
278 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
279 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
281 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
282 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
283 offsetof(struct sk_buff, vlan_proto));
284 /* A = ntohs(A) [emitting a nop or swap16] */
285 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
288 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
289 case SKF_AD_OFF + SKF_AD_NLATTR:
290 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
291 case SKF_AD_OFF + SKF_AD_CPU:
292 case SKF_AD_OFF + SKF_AD_RANDOM:
294 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
296 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
298 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
299 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
301 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
302 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
304 case SKF_AD_OFF + SKF_AD_NLATTR:
305 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
307 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
308 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
310 case SKF_AD_OFF + SKF_AD_CPU:
311 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
313 case SKF_AD_OFF + SKF_AD_RANDOM:
314 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
315 bpf_user_rnd_init_once();
320 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
322 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
326 /* This is just a dummy call to avoid letting the compiler
327 * evict __bpf_call_base() as an optimization. Placed here
328 * where no-one bothers.
330 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
339 * bpf_convert_filter - convert filter program
340 * @prog: the user passed filter program
341 * @len: the length of the user passed filter program
342 * @new_prog: buffer where converted program will be stored
343 * @new_len: pointer to store length of converted program
345 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
346 * Conversion workflow:
348 * 1) First pass for calculating the new program length:
349 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
351 * 2) 2nd pass to remap in two passes: 1st pass finds new
352 * jump offsets, 2nd pass remapping:
353 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
354 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
356 static int bpf_convert_filter(struct sock_filter *prog, int len,
357 struct bpf_insn *new_prog, int *new_len)
359 int new_flen = 0, pass = 0, target, i;
360 struct bpf_insn *new_insn;
361 struct sock_filter *fp;
365 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
366 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
368 if (len <= 0 || len > BPF_MAXINSNS)
372 addrs = kcalloc(len, sizeof(*addrs),
373 GFP_KERNEL | __GFP_NOWARN);
382 /* Classic BPF related prologue emission. */
384 /* Classic BPF expects A and X to be reset first. These need
385 * to be guaranteed to be the first two instructions.
387 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
388 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
390 /* All programs must keep CTX in callee saved BPF_REG_CTX.
391 * In eBPF case it's done by the compiler, here we need to
392 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
394 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
399 for (i = 0; i < len; fp++, i++) {
400 struct bpf_insn tmp_insns[6] = { };
401 struct bpf_insn *insn = tmp_insns;
404 addrs[i] = new_insn - new_prog;
407 /* All arithmetic insns and skb loads map as-is. */
408 case BPF_ALU | BPF_ADD | BPF_X:
409 case BPF_ALU | BPF_ADD | BPF_K:
410 case BPF_ALU | BPF_SUB | BPF_X:
411 case BPF_ALU | BPF_SUB | BPF_K:
412 case BPF_ALU | BPF_AND | BPF_X:
413 case BPF_ALU | BPF_AND | BPF_K:
414 case BPF_ALU | BPF_OR | BPF_X:
415 case BPF_ALU | BPF_OR | BPF_K:
416 case BPF_ALU | BPF_LSH | BPF_X:
417 case BPF_ALU | BPF_LSH | BPF_K:
418 case BPF_ALU | BPF_RSH | BPF_X:
419 case BPF_ALU | BPF_RSH | BPF_K:
420 case BPF_ALU | BPF_XOR | BPF_X:
421 case BPF_ALU | BPF_XOR | BPF_K:
422 case BPF_ALU | BPF_MUL | BPF_X:
423 case BPF_ALU | BPF_MUL | BPF_K:
424 case BPF_ALU | BPF_DIV | BPF_X:
425 case BPF_ALU | BPF_DIV | BPF_K:
426 case BPF_ALU | BPF_MOD | BPF_X:
427 case BPF_ALU | BPF_MOD | BPF_K:
428 case BPF_ALU | BPF_NEG:
429 case BPF_LD | BPF_ABS | BPF_W:
430 case BPF_LD | BPF_ABS | BPF_H:
431 case BPF_LD | BPF_ABS | BPF_B:
432 case BPF_LD | BPF_IND | BPF_W:
433 case BPF_LD | BPF_IND | BPF_H:
434 case BPF_LD | BPF_IND | BPF_B:
435 /* Check for overloaded BPF extension and
436 * directly convert it if found, otherwise
437 * just move on with mapping.
439 if (BPF_CLASS(fp->code) == BPF_LD &&
440 BPF_MODE(fp->code) == BPF_ABS &&
441 convert_bpf_extensions(fp, &insn))
444 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
445 fp->code == (BPF_ALU | BPF_MOD | BPF_X))
446 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
448 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
451 /* Jump transformation cannot use BPF block macros
452 * everywhere as offset calculation and target updates
453 * require a bit more work than the rest, i.e. jump
454 * opcodes map as-is, but offsets need adjustment.
457 #define BPF_EMIT_JMP \
459 if (target >= len || target < 0) \
461 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
462 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
463 insn->off -= insn - tmp_insns; \
466 case BPF_JMP | BPF_JA:
467 target = i + fp->k + 1;
468 insn->code = fp->code;
472 case BPF_JMP | BPF_JEQ | BPF_K:
473 case BPF_JMP | BPF_JEQ | BPF_X:
474 case BPF_JMP | BPF_JSET | BPF_K:
475 case BPF_JMP | BPF_JSET | BPF_X:
476 case BPF_JMP | BPF_JGT | BPF_K:
477 case BPF_JMP | BPF_JGT | BPF_X:
478 case BPF_JMP | BPF_JGE | BPF_K:
479 case BPF_JMP | BPF_JGE | BPF_X:
480 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
481 /* BPF immediates are signed, zero extend
482 * immediate into tmp register and use it
485 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
487 insn->dst_reg = BPF_REG_A;
488 insn->src_reg = BPF_REG_TMP;
491 insn->dst_reg = BPF_REG_A;
493 bpf_src = BPF_SRC(fp->code);
494 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
497 /* Common case where 'jump_false' is next insn. */
499 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
500 target = i + fp->jt + 1;
505 /* Convert JEQ into JNE when 'jump_true' is next insn. */
506 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
507 insn->code = BPF_JMP | BPF_JNE | bpf_src;
508 target = i + fp->jf + 1;
513 /* Other jumps are mapped into two insns: Jxx and JA. */
514 target = i + fp->jt + 1;
515 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
519 insn->code = BPF_JMP | BPF_JA;
520 target = i + fp->jf + 1;
524 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
525 case BPF_LDX | BPF_MSH | BPF_B:
527 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
528 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
529 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
531 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
533 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
535 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
537 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
540 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
541 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
543 case BPF_RET | BPF_A:
544 case BPF_RET | BPF_K:
545 if (BPF_RVAL(fp->code) == BPF_K)
546 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
548 *insn = BPF_EXIT_INSN();
551 /* Store to stack. */
554 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
555 BPF_ST ? BPF_REG_A : BPF_REG_X,
556 -(BPF_MEMWORDS - fp->k) * 4);
559 /* Load from stack. */
560 case BPF_LD | BPF_MEM:
561 case BPF_LDX | BPF_MEM:
562 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
563 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
564 -(BPF_MEMWORDS - fp->k) * 4);
568 case BPF_LD | BPF_IMM:
569 case BPF_LDX | BPF_IMM:
570 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
571 BPF_REG_A : BPF_REG_X, fp->k);
575 case BPF_MISC | BPF_TAX:
576 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
580 case BPF_MISC | BPF_TXA:
581 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
584 /* A = skb->len or X = skb->len */
585 case BPF_LD | BPF_W | BPF_LEN:
586 case BPF_LDX | BPF_W | BPF_LEN:
587 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
588 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
589 offsetof(struct sk_buff, len));
592 /* Access seccomp_data fields. */
593 case BPF_LDX | BPF_ABS | BPF_W:
594 /* A = *(u32 *) (ctx + K) */
595 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
598 /* Unknown instruction. */
605 memcpy(new_insn, tmp_insns,
606 sizeof(*insn) * (insn - tmp_insns));
607 new_insn += insn - tmp_insns;
611 /* Only calculating new length. */
612 *new_len = new_insn - new_prog;
617 if (new_flen != new_insn - new_prog) {
618 new_flen = new_insn - new_prog;
625 BUG_ON(*new_len != new_flen);
634 * As we dont want to clear mem[] array for each packet going through
635 * __bpf_prog_run(), we check that filter loaded by user never try to read
636 * a cell if not previously written, and we check all branches to be sure
637 * a malicious user doesn't try to abuse us.
639 static int check_load_and_stores(const struct sock_filter *filter, int flen)
641 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
644 BUILD_BUG_ON(BPF_MEMWORDS > 16);
646 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
650 memset(masks, 0xff, flen * sizeof(*masks));
652 for (pc = 0; pc < flen; pc++) {
653 memvalid &= masks[pc];
655 switch (filter[pc].code) {
658 memvalid |= (1 << filter[pc].k);
660 case BPF_LD | BPF_MEM:
661 case BPF_LDX | BPF_MEM:
662 if (!(memvalid & (1 << filter[pc].k))) {
667 case BPF_JMP | BPF_JA:
668 /* A jump must set masks on target */
669 masks[pc + 1 + filter[pc].k] &= memvalid;
672 case BPF_JMP | BPF_JEQ | BPF_K:
673 case BPF_JMP | BPF_JEQ | BPF_X:
674 case BPF_JMP | BPF_JGE | BPF_K:
675 case BPF_JMP | BPF_JGE | BPF_X:
676 case BPF_JMP | BPF_JGT | BPF_K:
677 case BPF_JMP | BPF_JGT | BPF_X:
678 case BPF_JMP | BPF_JSET | BPF_K:
679 case BPF_JMP | BPF_JSET | BPF_X:
680 /* A jump must set masks on targets */
681 masks[pc + 1 + filter[pc].jt] &= memvalid;
682 masks[pc + 1 + filter[pc].jf] &= memvalid;
692 static bool chk_code_allowed(u16 code_to_probe)
694 static const bool codes[] = {
695 /* 32 bit ALU operations */
696 [BPF_ALU | BPF_ADD | BPF_K] = true,
697 [BPF_ALU | BPF_ADD | BPF_X] = true,
698 [BPF_ALU | BPF_SUB | BPF_K] = true,
699 [BPF_ALU | BPF_SUB | BPF_X] = true,
700 [BPF_ALU | BPF_MUL | BPF_K] = true,
701 [BPF_ALU | BPF_MUL | BPF_X] = true,
702 [BPF_ALU | BPF_DIV | BPF_K] = true,
703 [BPF_ALU | BPF_DIV | BPF_X] = true,
704 [BPF_ALU | BPF_MOD | BPF_K] = true,
705 [BPF_ALU | BPF_MOD | BPF_X] = true,
706 [BPF_ALU | BPF_AND | BPF_K] = true,
707 [BPF_ALU | BPF_AND | BPF_X] = true,
708 [BPF_ALU | BPF_OR | BPF_K] = true,
709 [BPF_ALU | BPF_OR | BPF_X] = true,
710 [BPF_ALU | BPF_XOR | BPF_K] = true,
711 [BPF_ALU | BPF_XOR | BPF_X] = true,
712 [BPF_ALU | BPF_LSH | BPF_K] = true,
713 [BPF_ALU | BPF_LSH | BPF_X] = true,
714 [BPF_ALU | BPF_RSH | BPF_K] = true,
715 [BPF_ALU | BPF_RSH | BPF_X] = true,
716 [BPF_ALU | BPF_NEG] = true,
717 /* Load instructions */
718 [BPF_LD | BPF_W | BPF_ABS] = true,
719 [BPF_LD | BPF_H | BPF_ABS] = true,
720 [BPF_LD | BPF_B | BPF_ABS] = true,
721 [BPF_LD | BPF_W | BPF_LEN] = true,
722 [BPF_LD | BPF_W | BPF_IND] = true,
723 [BPF_LD | BPF_H | BPF_IND] = true,
724 [BPF_LD | BPF_B | BPF_IND] = true,
725 [BPF_LD | BPF_IMM] = true,
726 [BPF_LD | BPF_MEM] = true,
727 [BPF_LDX | BPF_W | BPF_LEN] = true,
728 [BPF_LDX | BPF_B | BPF_MSH] = true,
729 [BPF_LDX | BPF_IMM] = true,
730 [BPF_LDX | BPF_MEM] = true,
731 /* Store instructions */
734 /* Misc instructions */
735 [BPF_MISC | BPF_TAX] = true,
736 [BPF_MISC | BPF_TXA] = true,
737 /* Return instructions */
738 [BPF_RET | BPF_K] = true,
739 [BPF_RET | BPF_A] = true,
740 /* Jump instructions */
741 [BPF_JMP | BPF_JA] = true,
742 [BPF_JMP | BPF_JEQ | BPF_K] = true,
743 [BPF_JMP | BPF_JEQ | BPF_X] = true,
744 [BPF_JMP | BPF_JGE | BPF_K] = true,
745 [BPF_JMP | BPF_JGE | BPF_X] = true,
746 [BPF_JMP | BPF_JGT | BPF_K] = true,
747 [BPF_JMP | BPF_JGT | BPF_X] = true,
748 [BPF_JMP | BPF_JSET | BPF_K] = true,
749 [BPF_JMP | BPF_JSET | BPF_X] = true,
752 if (code_to_probe >= ARRAY_SIZE(codes))
755 return codes[code_to_probe];
758 static bool bpf_check_basics_ok(const struct sock_filter *filter,
763 if (flen == 0 || flen > BPF_MAXINSNS)
770 * bpf_check_classic - verify socket filter code
771 * @filter: filter to verify
772 * @flen: length of filter
774 * Check the user's filter code. If we let some ugly
775 * filter code slip through kaboom! The filter must contain
776 * no references or jumps that are out of range, no illegal
777 * instructions, and must end with a RET instruction.
779 * All jumps are forward as they are not signed.
781 * Returns 0 if the rule set is legal or -EINVAL if not.
783 static int bpf_check_classic(const struct sock_filter *filter,
789 /* Check the filter code now */
790 for (pc = 0; pc < flen; pc++) {
791 const struct sock_filter *ftest = &filter[pc];
793 /* May we actually operate on this code? */
794 if (!chk_code_allowed(ftest->code))
797 /* Some instructions need special checks */
798 switch (ftest->code) {
799 case BPF_ALU | BPF_DIV | BPF_K:
800 case BPF_ALU | BPF_MOD | BPF_K:
801 /* Check for division by zero */
805 case BPF_ALU | BPF_LSH | BPF_K:
806 case BPF_ALU | BPF_RSH | BPF_K:
810 case BPF_LD | BPF_MEM:
811 case BPF_LDX | BPF_MEM:
814 /* Check for invalid memory addresses */
815 if (ftest->k >= BPF_MEMWORDS)
818 case BPF_JMP | BPF_JA:
819 /* Note, the large ftest->k might cause loops.
820 * Compare this with conditional jumps below,
821 * where offsets are limited. --ANK (981016)
823 if (ftest->k >= (unsigned int)(flen - pc - 1))
826 case BPF_JMP | BPF_JEQ | BPF_K:
827 case BPF_JMP | BPF_JEQ | BPF_X:
828 case BPF_JMP | BPF_JGE | BPF_K:
829 case BPF_JMP | BPF_JGE | BPF_X:
830 case BPF_JMP | BPF_JGT | BPF_K:
831 case BPF_JMP | BPF_JGT | BPF_X:
832 case BPF_JMP | BPF_JSET | BPF_K:
833 case BPF_JMP | BPF_JSET | BPF_X:
834 /* Both conditionals must be safe */
835 if (pc + ftest->jt + 1 >= flen ||
836 pc + ftest->jf + 1 >= flen)
839 case BPF_LD | BPF_W | BPF_ABS:
840 case BPF_LD | BPF_H | BPF_ABS:
841 case BPF_LD | BPF_B | BPF_ABS:
843 if (bpf_anc_helper(ftest) & BPF_ANC)
845 /* Ancillary operation unknown or unsupported */
846 if (anc_found == false && ftest->k >= SKF_AD_OFF)
851 /* Last instruction must be a RET code */
852 switch (filter[flen - 1].code) {
853 case BPF_RET | BPF_K:
854 case BPF_RET | BPF_A:
855 return check_load_and_stores(filter, flen);
861 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
862 const struct sock_fprog *fprog)
864 unsigned int fsize = bpf_classic_proglen(fprog);
865 struct sock_fprog_kern *fkprog;
867 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
871 fkprog = fp->orig_prog;
872 fkprog->len = fprog->len;
874 fkprog->filter = kmemdup(fp->insns, fsize,
875 GFP_KERNEL | __GFP_NOWARN);
876 if (!fkprog->filter) {
877 kfree(fp->orig_prog);
884 static void bpf_release_orig_filter(struct bpf_prog *fp)
886 struct sock_fprog_kern *fprog = fp->orig_prog;
889 kfree(fprog->filter);
894 static void __bpf_prog_release(struct bpf_prog *prog)
896 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
899 bpf_release_orig_filter(prog);
904 static void __sk_filter_release(struct sk_filter *fp)
906 __bpf_prog_release(fp->prog);
911 * sk_filter_release_rcu - Release a socket filter by rcu_head
912 * @rcu: rcu_head that contains the sk_filter to free
914 static void sk_filter_release_rcu(struct rcu_head *rcu)
916 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
918 __sk_filter_release(fp);
922 * sk_filter_release - release a socket filter
923 * @fp: filter to remove
925 * Remove a filter from a socket and release its resources.
927 static void sk_filter_release(struct sk_filter *fp)
929 if (atomic_dec_and_test(&fp->refcnt))
930 call_rcu(&fp->rcu, sk_filter_release_rcu);
933 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
935 u32 filter_size = bpf_prog_size(fp->prog->len);
937 atomic_sub(filter_size, &sk->sk_omem_alloc);
938 sk_filter_release(fp);
941 /* try to charge the socket memory if there is space available
942 * return true on success
944 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
946 u32 filter_size = bpf_prog_size(fp->prog->len);
948 /* same check as in sock_kmalloc() */
949 if (filter_size <= sysctl_optmem_max &&
950 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
951 atomic_inc(&fp->refcnt);
952 atomic_add(filter_size, &sk->sk_omem_alloc);
958 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
960 struct sock_filter *old_prog;
961 struct bpf_prog *old_fp;
962 int err, new_len, old_len = fp->len;
964 /* We are free to overwrite insns et al right here as it
965 * won't be used at this point in time anymore internally
966 * after the migration to the internal BPF instruction
969 BUILD_BUG_ON(sizeof(struct sock_filter) !=
970 sizeof(struct bpf_insn));
972 /* Conversion cannot happen on overlapping memory areas,
973 * so we need to keep the user BPF around until the 2nd
974 * pass. At this time, the user BPF is stored in fp->insns.
976 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
977 GFP_KERNEL | __GFP_NOWARN);
983 /* 1st pass: calculate the new program length. */
984 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
988 /* Expand fp for appending the new filter representation. */
990 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
992 /* The old_fp is still around in case we couldn't
993 * allocate new memory, so uncharge on that one.
1002 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1003 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
1005 /* 2nd bpf_convert_filter() can fail only if it fails
1006 * to allocate memory, remapping must succeed. Note,
1007 * that at this time old_fp has already been released
1012 fp = bpf_prog_select_runtime(fp, &err);
1022 __bpf_prog_release(fp);
1023 return ERR_PTR(err);
1026 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1027 bpf_aux_classic_check_t trans)
1031 fp->bpf_func = NULL;
1034 err = bpf_check_classic(fp->insns, fp->len);
1036 __bpf_prog_release(fp);
1037 return ERR_PTR(err);
1040 /* There might be additional checks and transformations
1041 * needed on classic filters, f.e. in case of seccomp.
1044 err = trans(fp->insns, fp->len);
1046 __bpf_prog_release(fp);
1047 return ERR_PTR(err);
1051 /* Probe if we can JIT compile the filter and if so, do
1052 * the compilation of the filter.
1054 bpf_jit_compile(fp);
1056 /* JIT compiler couldn't process this filter, so do the
1057 * internal BPF translation for the optimized interpreter.
1060 fp = bpf_migrate_filter(fp);
1066 * bpf_prog_create - create an unattached filter
1067 * @pfp: the unattached filter that is created
1068 * @fprog: the filter program
1070 * Create a filter independent of any socket. We first run some
1071 * sanity checks on it to make sure it does not explode on us later.
1072 * If an error occurs or there is insufficient memory for the filter
1073 * a negative errno code is returned. On success the return is zero.
1075 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1077 unsigned int fsize = bpf_classic_proglen(fprog);
1078 struct bpf_prog *fp;
1080 /* Make sure new filter is there and in the right amounts. */
1081 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1084 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1088 memcpy(fp->insns, fprog->filter, fsize);
1090 fp->len = fprog->len;
1091 /* Since unattached filters are not copied back to user
1092 * space through sk_get_filter(), we do not need to hold
1093 * a copy here, and can spare us the work.
1095 fp->orig_prog = NULL;
1097 /* bpf_prepare_filter() already takes care of freeing
1098 * memory in case something goes wrong.
1100 fp = bpf_prepare_filter(fp, NULL);
1107 EXPORT_SYMBOL_GPL(bpf_prog_create);
1110 * bpf_prog_create_from_user - create an unattached filter from user buffer
1111 * @pfp: the unattached filter that is created
1112 * @fprog: the filter program
1113 * @trans: post-classic verifier transformation handler
1114 * @save_orig: save classic BPF program
1116 * This function effectively does the same as bpf_prog_create(), only
1117 * that it builds up its insns buffer from user space provided buffer.
1118 * It also allows for passing a bpf_aux_classic_check_t handler.
1120 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1121 bpf_aux_classic_check_t trans, bool save_orig)
1123 unsigned int fsize = bpf_classic_proglen(fprog);
1124 struct bpf_prog *fp;
1127 /* Make sure new filter is there and in the right amounts. */
1128 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1131 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1135 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1136 __bpf_prog_free(fp);
1140 fp->len = fprog->len;
1141 fp->orig_prog = NULL;
1144 err = bpf_prog_store_orig_filter(fp, fprog);
1146 __bpf_prog_free(fp);
1151 /* bpf_prepare_filter() already takes care of freeing
1152 * memory in case something goes wrong.
1154 fp = bpf_prepare_filter(fp, trans);
1161 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1163 void bpf_prog_destroy(struct bpf_prog *fp)
1165 __bpf_prog_release(fp);
1167 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1169 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1171 struct sk_filter *fp, *old_fp;
1173 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1178 atomic_set(&fp->refcnt, 0);
1180 if (!sk_filter_charge(sk, fp)) {
1185 old_fp = rcu_dereference_protected(sk->sk_filter,
1186 lockdep_sock_is_held(sk));
1187 rcu_assign_pointer(sk->sk_filter, fp);
1190 sk_filter_uncharge(sk, old_fp);
1195 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1197 struct bpf_prog *old_prog;
1200 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1203 if (sk_unhashed(sk) && sk->sk_reuseport) {
1204 err = reuseport_alloc(sk);
1207 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1208 /* The socket wasn't bound with SO_REUSEPORT */
1212 old_prog = reuseport_attach_prog(sk, prog);
1214 bpf_prog_destroy(old_prog);
1220 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1222 unsigned int fsize = bpf_classic_proglen(fprog);
1223 struct bpf_prog *prog;
1226 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1227 return ERR_PTR(-EPERM);
1229 /* Make sure new filter is there and in the right amounts. */
1230 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1231 return ERR_PTR(-EINVAL);
1233 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1235 return ERR_PTR(-ENOMEM);
1237 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1238 __bpf_prog_free(prog);
1239 return ERR_PTR(-EFAULT);
1242 prog->len = fprog->len;
1244 err = bpf_prog_store_orig_filter(prog, fprog);
1246 __bpf_prog_free(prog);
1247 return ERR_PTR(-ENOMEM);
1250 /* bpf_prepare_filter() already takes care of freeing
1251 * memory in case something goes wrong.
1253 return bpf_prepare_filter(prog, NULL);
1257 * sk_attach_filter - attach a socket filter
1258 * @fprog: the filter program
1259 * @sk: the socket to use
1261 * Attach the user's filter code. We first run some sanity checks on
1262 * it to make sure it does not explode on us later. If an error
1263 * occurs or there is insufficient memory for the filter a negative
1264 * errno code is returned. On success the return is zero.
1266 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1268 struct bpf_prog *prog = __get_filter(fprog, sk);
1272 return PTR_ERR(prog);
1274 err = __sk_attach_prog(prog, sk);
1276 __bpf_prog_release(prog);
1282 EXPORT_SYMBOL_GPL(sk_attach_filter);
1284 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1286 struct bpf_prog *prog = __get_filter(fprog, sk);
1290 return PTR_ERR(prog);
1292 err = __reuseport_attach_prog(prog, sk);
1294 __bpf_prog_release(prog);
1301 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1303 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1304 return ERR_PTR(-EPERM);
1306 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1309 int sk_attach_bpf(u32 ufd, struct sock *sk)
1311 struct bpf_prog *prog = __get_bpf(ufd, sk);
1315 return PTR_ERR(prog);
1317 err = __sk_attach_prog(prog, sk);
1326 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1328 struct bpf_prog *prog = __get_bpf(ufd, sk);
1332 return PTR_ERR(prog);
1334 err = __reuseport_attach_prog(prog, sk);
1343 struct bpf_scratchpad {
1345 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1346 u8 buff[MAX_BPF_STACK];
1350 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1352 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1353 unsigned int write_len)
1355 return skb_ensure_writable(skb, write_len);
1358 static inline int bpf_try_make_writable(struct sk_buff *skb,
1359 unsigned int write_len)
1361 int err = __bpf_try_make_writable(skb, write_len);
1363 bpf_compute_data_end(skb);
1367 static int bpf_try_make_head_writable(struct sk_buff *skb)
1369 return bpf_try_make_writable(skb, skb_headlen(skb));
1372 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1374 if (skb_at_tc_ingress(skb))
1375 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1378 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1380 if (skb_at_tc_ingress(skb))
1381 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1384 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1385 const void *, from, u32, len, u64, flags)
1389 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1391 if (unlikely(offset > INT_MAX))
1393 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1396 ptr = skb->data + offset;
1397 if (flags & BPF_F_RECOMPUTE_CSUM)
1398 __skb_postpull_rcsum(skb, ptr, len, offset);
1400 memcpy(ptr, from, len);
1402 if (flags & BPF_F_RECOMPUTE_CSUM)
1403 __skb_postpush_rcsum(skb, ptr, len, offset);
1404 if (flags & BPF_F_INVALIDATE_HASH)
1405 skb_clear_hash(skb);
1410 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1411 .func = bpf_skb_store_bytes,
1413 .ret_type = RET_INTEGER,
1414 .arg1_type = ARG_PTR_TO_CTX,
1415 .arg2_type = ARG_ANYTHING,
1416 .arg3_type = ARG_PTR_TO_STACK,
1417 .arg4_type = ARG_CONST_STACK_SIZE,
1418 .arg5_type = ARG_ANYTHING,
1421 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1422 void *, to, u32, len)
1426 if (unlikely(offset > INT_MAX))
1429 ptr = skb_header_pointer(skb, offset, len, to);
1433 memcpy(to, ptr, len);
1441 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1442 .func = bpf_skb_load_bytes,
1444 .ret_type = RET_INTEGER,
1445 .arg1_type = ARG_PTR_TO_CTX,
1446 .arg2_type = ARG_ANYTHING,
1447 .arg3_type = ARG_PTR_TO_RAW_STACK,
1448 .arg4_type = ARG_CONST_STACK_SIZE,
1451 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1453 /* Idea is the following: should the needed direct read/write
1454 * test fail during runtime, we can pull in more data and redo
1455 * again, since implicitly, we invalidate previous checks here.
1457 * Or, since we know how much we need to make read/writeable,
1458 * this can be done once at the program beginning for direct
1459 * access case. By this we overcome limitations of only current
1460 * headroom being accessible.
1462 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1465 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1466 .func = bpf_skb_pull_data,
1468 .ret_type = RET_INTEGER,
1469 .arg1_type = ARG_PTR_TO_CTX,
1470 .arg2_type = ARG_ANYTHING,
1473 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1474 u64, from, u64, to, u64, flags)
1478 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1480 if (unlikely(offset > 0xffff || offset & 1))
1482 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1485 ptr = (__sum16 *)(skb->data + offset);
1486 switch (flags & BPF_F_HDR_FIELD_MASK) {
1488 if (unlikely(from != 0))
1491 csum_replace_by_diff(ptr, to);
1494 csum_replace2(ptr, from, to);
1497 csum_replace4(ptr, from, to);
1506 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1507 .func = bpf_l3_csum_replace,
1509 .ret_type = RET_INTEGER,
1510 .arg1_type = ARG_PTR_TO_CTX,
1511 .arg2_type = ARG_ANYTHING,
1512 .arg3_type = ARG_ANYTHING,
1513 .arg4_type = ARG_ANYTHING,
1514 .arg5_type = ARG_ANYTHING,
1517 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1518 u64, from, u64, to, u64, flags)
1520 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1521 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1524 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR |
1525 BPF_F_HDR_FIELD_MASK)))
1527 if (unlikely(offset > 0xffff || offset & 1))
1529 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1532 ptr = (__sum16 *)(skb->data + offset);
1533 if (is_mmzero && !*ptr)
1536 switch (flags & BPF_F_HDR_FIELD_MASK) {
1538 if (unlikely(from != 0))
1541 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1544 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1547 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1553 if (is_mmzero && !*ptr)
1554 *ptr = CSUM_MANGLED_0;
1558 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1559 .func = bpf_l4_csum_replace,
1561 .ret_type = RET_INTEGER,
1562 .arg1_type = ARG_PTR_TO_CTX,
1563 .arg2_type = ARG_ANYTHING,
1564 .arg3_type = ARG_ANYTHING,
1565 .arg4_type = ARG_ANYTHING,
1566 .arg5_type = ARG_ANYTHING,
1569 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1570 __be32 *, to, u32, to_size, __wsum, seed)
1572 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1573 u32 diff_size = from_size + to_size;
1576 /* This is quite flexible, some examples:
1578 * from_size == 0, to_size > 0, seed := csum --> pushing data
1579 * from_size > 0, to_size == 0, seed := csum --> pulling data
1580 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1582 * Even for diffing, from_size and to_size don't need to be equal.
1584 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1585 diff_size > sizeof(sp->diff)))
1588 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1589 sp->diff[j] = ~from[i];
1590 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1591 sp->diff[j] = to[i];
1593 return csum_partial(sp->diff, diff_size, seed);
1596 static const struct bpf_func_proto bpf_csum_diff_proto = {
1597 .func = bpf_csum_diff,
1600 .ret_type = RET_INTEGER,
1601 .arg1_type = ARG_PTR_TO_STACK,
1602 .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1603 .arg3_type = ARG_PTR_TO_STACK,
1604 .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1605 .arg5_type = ARG_ANYTHING,
1608 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1610 /* The interface is to be used in combination with bpf_csum_diff()
1611 * for direct packet writes. csum rotation for alignment as well
1612 * as emulating csum_sub() can be done from the eBPF program.
1614 if (skb->ip_summed == CHECKSUM_COMPLETE)
1615 return (skb->csum = csum_add(skb->csum, csum));
1620 static const struct bpf_func_proto bpf_csum_update_proto = {
1621 .func = bpf_csum_update,
1623 .ret_type = RET_INTEGER,
1624 .arg1_type = ARG_PTR_TO_CTX,
1625 .arg2_type = ARG_ANYTHING,
1628 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1630 return dev_forward_skb(dev, skb);
1633 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1634 struct sk_buff *skb)
1636 int ret = ____dev_forward_skb(dev, skb);
1640 ret = netif_rx(skb);
1646 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1650 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1651 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1658 __this_cpu_inc(xmit_recursion);
1659 ret = dev_queue_xmit(skb);
1660 __this_cpu_dec(xmit_recursion);
1665 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1668 /* skb->mac_len is not set on normal egress */
1669 unsigned int mlen = skb->network_header - skb->mac_header;
1671 __skb_pull(skb, mlen);
1673 /* At ingress, the mac header has already been pulled once.
1674 * At egress, skb_pospull_rcsum has to be done in case that
1675 * the skb is originated from ingress (i.e. a forwarded skb)
1676 * to ensure that rcsum starts at net header.
1678 if (!skb_at_tc_ingress(skb))
1679 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1680 skb_pop_mac_header(skb);
1681 skb_reset_mac_len(skb);
1682 return flags & BPF_F_INGRESS ?
1683 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1686 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1689 bpf_push_mac_rcsum(skb);
1690 return flags & BPF_F_INGRESS ?
1691 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1694 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1697 switch (dev->type) {
1699 case ARPHRD_TUNNEL6:
1704 return __bpf_redirect_no_mac(skb, dev, flags);
1706 return __bpf_redirect_common(skb, dev, flags);
1710 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1712 struct net_device *dev;
1713 struct sk_buff *clone;
1716 if (unlikely(flags & ~(BPF_F_INGRESS)))
1719 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1723 clone = skb_clone(skb, GFP_ATOMIC);
1724 if (unlikely(!clone))
1727 /* For direct write, we need to keep the invariant that the skbs
1728 * we're dealing with need to be uncloned. Should uncloning fail
1729 * here, we need to free the just generated clone to unclone once
1732 ret = bpf_try_make_head_writable(skb);
1733 if (unlikely(ret)) {
1738 return __bpf_redirect(clone, dev, flags);
1741 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1742 .func = bpf_clone_redirect,
1744 .ret_type = RET_INTEGER,
1745 .arg1_type = ARG_PTR_TO_CTX,
1746 .arg2_type = ARG_ANYTHING,
1747 .arg3_type = ARG_ANYTHING,
1750 struct redirect_info {
1755 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1757 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1759 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1761 if (unlikely(flags & ~(BPF_F_INGRESS)))
1764 ri->ifindex = ifindex;
1767 return TC_ACT_REDIRECT;
1770 int skb_do_redirect(struct sk_buff *skb)
1772 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1773 struct net_device *dev;
1775 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1777 if (unlikely(!dev)) {
1782 return __bpf_redirect(skb, dev, ri->flags);
1785 static const struct bpf_func_proto bpf_redirect_proto = {
1786 .func = bpf_redirect,
1788 .ret_type = RET_INTEGER,
1789 .arg1_type = ARG_ANYTHING,
1790 .arg2_type = ARG_ANYTHING,
1793 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
1795 return task_get_classid(skb);
1798 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1799 .func = bpf_get_cgroup_classid,
1801 .ret_type = RET_INTEGER,
1802 .arg1_type = ARG_PTR_TO_CTX,
1805 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
1807 return dst_tclassid(skb);
1810 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1811 .func = bpf_get_route_realm,
1813 .ret_type = RET_INTEGER,
1814 .arg1_type = ARG_PTR_TO_CTX,
1817 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
1819 /* If skb_clear_hash() was called due to mangling, we can
1820 * trigger SW recalculation here. Later access to hash
1821 * can then use the inline skb->hash via context directly
1822 * instead of calling this helper again.
1824 return skb_get_hash(skb);
1827 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1828 .func = bpf_get_hash_recalc,
1830 .ret_type = RET_INTEGER,
1831 .arg1_type = ARG_PTR_TO_CTX,
1834 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1836 /* After all direct packet write, this can be used once for
1837 * triggering a lazy recalc on next skb_get_hash() invocation.
1839 skb_clear_hash(skb);
1843 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1844 .func = bpf_set_hash_invalid,
1846 .ret_type = RET_INTEGER,
1847 .arg1_type = ARG_PTR_TO_CTX,
1850 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1855 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1856 vlan_proto != htons(ETH_P_8021AD)))
1857 vlan_proto = htons(ETH_P_8021Q);
1859 bpf_push_mac_rcsum(skb);
1860 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1861 bpf_pull_mac_rcsum(skb);
1863 bpf_compute_data_end(skb);
1867 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1868 .func = bpf_skb_vlan_push,
1870 .ret_type = RET_INTEGER,
1871 .arg1_type = ARG_PTR_TO_CTX,
1872 .arg2_type = ARG_ANYTHING,
1873 .arg3_type = ARG_ANYTHING,
1875 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1877 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
1881 bpf_push_mac_rcsum(skb);
1882 ret = skb_vlan_pop(skb);
1883 bpf_pull_mac_rcsum(skb);
1885 bpf_compute_data_end(skb);
1889 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1890 .func = bpf_skb_vlan_pop,
1892 .ret_type = RET_INTEGER,
1893 .arg1_type = ARG_PTR_TO_CTX,
1895 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
1897 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1899 /* Caller already did skb_cow() with len as headroom,
1900 * so no need to do it here.
1903 memmove(skb->data, skb->data + len, off);
1904 memset(skb->data + off, 0, len);
1906 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1907 * needed here as it does not change the skb->csum
1908 * result for checksum complete when summing over
1914 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1916 /* skb_ensure_writable() is not needed here, as we're
1917 * already working on an uncloned skb.
1919 if (unlikely(!pskb_may_pull(skb, off + len)))
1922 skb_postpull_rcsum(skb, skb->data + off, len);
1923 memmove(skb->data + len, skb->data, off);
1924 __skb_pull(skb, len);
1929 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1931 bool trans_same = skb->transport_header == skb->network_header;
1934 /* There's no need for __skb_push()/__skb_pull() pair to
1935 * get to the start of the mac header as we're guaranteed
1936 * to always start from here under eBPF.
1938 ret = bpf_skb_generic_push(skb, off, len);
1940 skb->mac_header -= len;
1941 skb->network_header -= len;
1943 skb->transport_header = skb->network_header;
1949 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1951 bool trans_same = skb->transport_header == skb->network_header;
1954 /* Same here, __skb_push()/__skb_pull() pair not needed. */
1955 ret = bpf_skb_generic_pop(skb, off, len);
1957 skb->mac_header += len;
1958 skb->network_header += len;
1960 skb->transport_header = skb->network_header;
1966 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
1968 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1969 u32 off = skb->network_header - skb->mac_header;
1972 ret = skb_cow(skb, len_diff);
1973 if (unlikely(ret < 0))
1976 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
1977 if (unlikely(ret < 0))
1980 if (skb_is_gso(skb)) {
1981 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
1982 * be changed into SKB_GSO_TCPV6.
1984 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1985 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
1986 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
1989 /* Due to IPv6 header, MSS needs to be downgraded. */
1990 skb_shinfo(skb)->gso_size -= len_diff;
1991 /* Header must be checked, and gso_segs recomputed. */
1992 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1993 skb_shinfo(skb)->gso_segs = 0;
1996 skb->protocol = htons(ETH_P_IPV6);
1997 skb_clear_hash(skb);
2002 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2004 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2005 u32 off = skb->network_header - skb->mac_header;
2008 ret = skb_unclone(skb, GFP_ATOMIC);
2009 if (unlikely(ret < 0))
2012 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2013 if (unlikely(ret < 0))
2016 if (skb_is_gso(skb)) {
2017 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
2018 * be changed into SKB_GSO_TCPV4.
2020 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2021 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2022 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2025 /* Due to IPv4 header, MSS can be upgraded. */
2026 skb_shinfo(skb)->gso_size += len_diff;
2027 /* Header must be checked, and gso_segs recomputed. */
2028 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2029 skb_shinfo(skb)->gso_segs = 0;
2032 skb->protocol = htons(ETH_P_IP);
2033 skb_clear_hash(skb);
2038 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2040 __be16 from_proto = skb->protocol;
2042 if (from_proto == htons(ETH_P_IP) &&
2043 to_proto == htons(ETH_P_IPV6))
2044 return bpf_skb_proto_4_to_6(skb);
2046 if (from_proto == htons(ETH_P_IPV6) &&
2047 to_proto == htons(ETH_P_IP))
2048 return bpf_skb_proto_6_to_4(skb);
2053 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2058 if (unlikely(flags))
2061 /* General idea is that this helper does the basic groundwork
2062 * needed for changing the protocol, and eBPF program fills the
2063 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2064 * and other helpers, rather than passing a raw buffer here.
2066 * The rationale is to keep this minimal and without a need to
2067 * deal with raw packet data. F.e. even if we would pass buffers
2068 * here, the program still needs to call the bpf_lX_csum_replace()
2069 * helpers anyway. Plus, this way we keep also separation of
2070 * concerns, since f.e. bpf_skb_store_bytes() should only take
2073 * Currently, additional options and extension header space are
2074 * not supported, but flags register is reserved so we can adapt
2075 * that. For offloads, we mark packet as dodgy, so that headers
2076 * need to be verified first.
2078 ret = bpf_skb_proto_xlat(skb, proto);
2079 bpf_compute_data_end(skb);
2083 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2084 .func = bpf_skb_change_proto,
2086 .ret_type = RET_INTEGER,
2087 .arg1_type = ARG_PTR_TO_CTX,
2088 .arg2_type = ARG_ANYTHING,
2089 .arg3_type = ARG_ANYTHING,
2092 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2094 /* We only allow a restricted subset to be changed for now. */
2095 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2096 !skb_pkt_type_ok(pkt_type)))
2099 skb->pkt_type = pkt_type;
2103 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2104 .func = bpf_skb_change_type,
2106 .ret_type = RET_INTEGER,
2107 .arg1_type = ARG_PTR_TO_CTX,
2108 .arg2_type = ARG_ANYTHING,
2111 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2113 u32 min_len = skb_network_offset(skb);
2115 if (skb_transport_header_was_set(skb))
2116 min_len = skb_transport_offset(skb);
2117 if (skb->ip_summed == CHECKSUM_PARTIAL)
2118 min_len = skb_checksum_start_offset(skb) +
2119 skb->csum_offset + sizeof(__sum16);
2123 #define BPF_SKB_MAX_LEN SKB_MAX_ALLOC
2125 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2127 unsigned int old_len = skb->len;
2130 ret = __skb_grow_rcsum(skb, new_len);
2132 memset(skb->data + old_len, 0, new_len - old_len);
2136 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2138 return __skb_trim_rcsum(skb, new_len);
2141 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2144 u32 max_len = BPF_SKB_MAX_LEN;
2145 u32 min_len = __bpf_skb_min_len(skb);
2148 if (unlikely(flags || new_len > max_len || new_len < min_len))
2150 if (skb->encapsulation)
2153 /* The basic idea of this helper is that it's performing the
2154 * needed work to either grow or trim an skb, and eBPF program
2155 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2156 * bpf_lX_csum_replace() and others rather than passing a raw
2157 * buffer here. This one is a slow path helper and intended
2158 * for replies with control messages.
2160 * Like in bpf_skb_change_proto(), we want to keep this rather
2161 * minimal and without protocol specifics so that we are able
2162 * to separate concerns as in bpf_skb_store_bytes() should only
2163 * be the one responsible for writing buffers.
2165 * It's really expected to be a slow path operation here for
2166 * control message replies, so we're implicitly linearizing,
2167 * uncloning and drop offloads from the skb by this.
2169 ret = __bpf_try_make_writable(skb, skb->len);
2171 if (new_len > skb->len)
2172 ret = bpf_skb_grow_rcsum(skb, new_len);
2173 else if (new_len < skb->len)
2174 ret = bpf_skb_trim_rcsum(skb, new_len);
2175 if (!ret && skb_is_gso(skb))
2179 bpf_compute_data_end(skb);
2183 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2184 .func = bpf_skb_change_tail,
2186 .ret_type = RET_INTEGER,
2187 .arg1_type = ARG_PTR_TO_CTX,
2188 .arg2_type = ARG_ANYTHING,
2189 .arg3_type = ARG_ANYTHING,
2192 bool bpf_helper_changes_skb_data(void *func)
2194 if (func == bpf_skb_vlan_push ||
2195 func == bpf_skb_vlan_pop ||
2196 func == bpf_skb_store_bytes ||
2197 func == bpf_skb_change_proto ||
2198 func == bpf_skb_change_tail ||
2199 func == bpf_skb_pull_data ||
2200 func == bpf_clone_redirect ||
2201 func == bpf_l3_csum_replace ||
2202 func == bpf_l4_csum_replace)
2208 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2209 unsigned long off, unsigned long len)
2211 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
2215 if (ptr != dst_buff)
2216 memcpy(dst_buff, ptr, len);
2221 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2222 u64, flags, void *, meta, u64, meta_size)
2224 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2226 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2228 if (unlikely(skb_size > skb->len))
2231 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2235 static const struct bpf_func_proto bpf_skb_event_output_proto = {
2236 .func = bpf_skb_event_output,
2238 .ret_type = RET_INTEGER,
2239 .arg1_type = ARG_PTR_TO_CTX,
2240 .arg2_type = ARG_CONST_MAP_PTR,
2241 .arg3_type = ARG_ANYTHING,
2242 .arg4_type = ARG_PTR_TO_STACK,
2243 .arg5_type = ARG_CONST_STACK_SIZE,
2246 static unsigned short bpf_tunnel_key_af(u64 flags)
2248 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2251 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2252 u32, size, u64, flags)
2254 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2255 u8 compat[sizeof(struct bpf_tunnel_key)];
2259 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2263 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2267 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2270 case offsetof(struct bpf_tunnel_key, tunnel_label):
2271 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2273 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2274 /* Fixup deprecated structure layouts here, so we have
2275 * a common path later on.
2277 if (ip_tunnel_info_af(info) != AF_INET)
2280 to = (struct bpf_tunnel_key *)compat;
2287 to->tunnel_id = be64_to_cpu(info->key.tun_id);
2288 to->tunnel_tos = info->key.tos;
2289 to->tunnel_ttl = info->key.ttl;
2291 if (flags & BPF_F_TUNINFO_IPV6) {
2292 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2293 sizeof(to->remote_ipv6));
2294 to->tunnel_label = be32_to_cpu(info->key.label);
2296 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
2299 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
2300 memcpy(to_orig, to, size);
2304 memset(to_orig, 0, size);
2308 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
2309 .func = bpf_skb_get_tunnel_key,
2311 .ret_type = RET_INTEGER,
2312 .arg1_type = ARG_PTR_TO_CTX,
2313 .arg2_type = ARG_PTR_TO_RAW_STACK,
2314 .arg3_type = ARG_CONST_STACK_SIZE,
2315 .arg4_type = ARG_ANYTHING,
2318 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
2320 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2323 if (unlikely(!info ||
2324 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2328 if (unlikely(size < info->options_len)) {
2333 ip_tunnel_info_opts_get(to, info);
2334 if (size > info->options_len)
2335 memset(to + info->options_len, 0, size - info->options_len);
2337 return info->options_len;
2339 memset(to, 0, size);
2343 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2344 .func = bpf_skb_get_tunnel_opt,
2346 .ret_type = RET_INTEGER,
2347 .arg1_type = ARG_PTR_TO_CTX,
2348 .arg2_type = ARG_PTR_TO_RAW_STACK,
2349 .arg3_type = ARG_CONST_STACK_SIZE,
2352 static struct metadata_dst __percpu *md_dst;
2354 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2355 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
2357 struct metadata_dst *md = this_cpu_ptr(md_dst);
2358 u8 compat[sizeof(struct bpf_tunnel_key)];
2359 struct ip_tunnel_info *info;
2361 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2362 BPF_F_DONT_FRAGMENT)))
2364 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2366 case offsetof(struct bpf_tunnel_key, tunnel_label):
2367 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2368 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2369 /* Fixup deprecated structure layouts here, so we have
2370 * a common path later on.
2372 memcpy(compat, from, size);
2373 memset(compat + size, 0, sizeof(compat) - size);
2374 from = (const struct bpf_tunnel_key *) compat;
2380 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2385 dst_hold((struct dst_entry *) md);
2386 skb_dst_set(skb, (struct dst_entry *) md);
2388 info = &md->u.tun_info;
2389 info->mode = IP_TUNNEL_INFO_TX;
2391 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
2392 if (flags & BPF_F_DONT_FRAGMENT)
2393 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2395 info->key.tun_id = cpu_to_be64(from->tunnel_id);
2396 info->key.tos = from->tunnel_tos;
2397 info->key.ttl = from->tunnel_ttl;
2399 if (flags & BPF_F_TUNINFO_IPV6) {
2400 info->mode |= IP_TUNNEL_INFO_IPV6;
2401 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2402 sizeof(from->remote_ipv6));
2403 info->key.label = cpu_to_be32(from->tunnel_label) &
2404 IPV6_FLOWLABEL_MASK;
2406 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2407 if (flags & BPF_F_ZERO_CSUM_TX)
2408 info->key.tun_flags &= ~TUNNEL_CSUM;
2414 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
2415 .func = bpf_skb_set_tunnel_key,
2417 .ret_type = RET_INTEGER,
2418 .arg1_type = ARG_PTR_TO_CTX,
2419 .arg2_type = ARG_PTR_TO_STACK,
2420 .arg3_type = ARG_CONST_STACK_SIZE,
2421 .arg4_type = ARG_ANYTHING,
2424 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2425 const u8 *, from, u32, size)
2427 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2428 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2430 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2432 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
2435 ip_tunnel_info_opts_set(info, from, size);
2440 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2441 .func = bpf_skb_set_tunnel_opt,
2443 .ret_type = RET_INTEGER,
2444 .arg1_type = ARG_PTR_TO_CTX,
2445 .arg2_type = ARG_PTR_TO_STACK,
2446 .arg3_type = ARG_CONST_STACK_SIZE,
2449 static const struct bpf_func_proto *
2450 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
2453 /* Race is not possible, since it's called from verifier
2454 * that is holding verifier mutex.
2456 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
2463 case BPF_FUNC_skb_set_tunnel_key:
2464 return &bpf_skb_set_tunnel_key_proto;
2465 case BPF_FUNC_skb_set_tunnel_opt:
2466 return &bpf_skb_set_tunnel_opt_proto;
2472 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2475 struct bpf_array *array = container_of(map, struct bpf_array, map);
2476 struct cgroup *cgrp;
2479 sk = skb_to_full_sk(skb);
2480 if (!sk || !sk_fullsock(sk))
2482 if (unlikely(idx >= array->map.max_entries))
2485 cgrp = READ_ONCE(array->ptrs[idx]);
2486 if (unlikely(!cgrp))
2489 return sk_under_cgroup_hierarchy(sk, cgrp);
2492 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2493 .func = bpf_skb_under_cgroup,
2495 .ret_type = RET_INTEGER,
2496 .arg1_type = ARG_PTR_TO_CTX,
2497 .arg2_type = ARG_CONST_MAP_PTR,
2498 .arg3_type = ARG_ANYTHING,
2501 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2502 unsigned long off, unsigned long len)
2504 memcpy(dst_buff, src_buff + off, len);
2508 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2509 u64, flags, void *, meta, u64, meta_size)
2511 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2513 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2515 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2518 return bpf_event_output(map, flags, meta, meta_size, xdp, xdp_size,
2522 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2523 .func = bpf_xdp_event_output,
2525 .ret_type = RET_INTEGER,
2526 .arg1_type = ARG_PTR_TO_CTX,
2527 .arg2_type = ARG_CONST_MAP_PTR,
2528 .arg3_type = ARG_ANYTHING,
2529 .arg4_type = ARG_PTR_TO_STACK,
2530 .arg5_type = ARG_CONST_STACK_SIZE,
2533 static const struct bpf_func_proto *
2534 sk_filter_func_proto(enum bpf_func_id func_id)
2537 case BPF_FUNC_map_lookup_elem:
2538 return &bpf_map_lookup_elem_proto;
2539 case BPF_FUNC_map_update_elem:
2540 return &bpf_map_update_elem_proto;
2541 case BPF_FUNC_map_delete_elem:
2542 return &bpf_map_delete_elem_proto;
2543 case BPF_FUNC_get_prandom_u32:
2544 return &bpf_get_prandom_u32_proto;
2545 case BPF_FUNC_get_smp_processor_id:
2546 return &bpf_get_raw_smp_processor_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 bool __is_valid_access(int off, int size, enum bpf_access_type type)
2633 if (off < 0 || off >= sizeof(struct __sk_buff))
2635 /* The verifier guarantees that size > 0. */
2636 if (off % size != 0)
2638 if (size != sizeof(__u32))
2644 static bool sk_filter_is_valid_access(int off, int size,
2645 enum bpf_access_type type,
2646 enum bpf_reg_type *reg_type)
2649 case offsetof(struct __sk_buff, tc_classid):
2650 case offsetof(struct __sk_buff, data):
2651 case offsetof(struct __sk_buff, data_end):
2655 if (type == BPF_WRITE) {
2657 case offsetof(struct __sk_buff, cb[0]) ...
2658 offsetof(struct __sk_buff, cb[4]):
2665 return __is_valid_access(off, size, type);
2668 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
2669 const struct bpf_prog *prog)
2671 struct bpf_insn *insn = insn_buf;
2676 /* if (!skb->cloned)
2679 * (Fast-path, otherwise approximation that we might be
2680 * a clone, do the rest in helper.)
2682 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
2683 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
2684 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
2686 /* ret = bpf_skb_pull_data(skb, 0); */
2687 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
2688 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
2689 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2690 BPF_FUNC_skb_pull_data);
2693 * return TC_ACT_SHOT;
2695 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
2696 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT);
2697 *insn++ = BPF_EXIT_INSN();
2700 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
2702 *insn++ = prog->insnsi[0];
2704 return insn - insn_buf;
2707 static bool tc_cls_act_is_valid_access(int off, int size,
2708 enum bpf_access_type type,
2709 enum bpf_reg_type *reg_type)
2711 if (type == BPF_WRITE) {
2713 case offsetof(struct __sk_buff, mark):
2714 case offsetof(struct __sk_buff, tc_index):
2715 case offsetof(struct __sk_buff, priority):
2716 case offsetof(struct __sk_buff, cb[0]) ...
2717 offsetof(struct __sk_buff, cb[4]):
2718 case offsetof(struct __sk_buff, tc_classid):
2726 case offsetof(struct __sk_buff, data):
2727 *reg_type = PTR_TO_PACKET;
2729 case offsetof(struct __sk_buff, data_end):
2730 *reg_type = PTR_TO_PACKET_END;
2734 return __is_valid_access(off, size, type);
2737 static bool __is_valid_xdp_access(int off, int size,
2738 enum bpf_access_type type)
2740 if (off < 0 || off >= sizeof(struct xdp_md))
2742 if (off % size != 0)
2744 if (size != sizeof(__u32))
2750 static bool xdp_is_valid_access(int off, int size,
2751 enum bpf_access_type type,
2752 enum bpf_reg_type *reg_type)
2754 if (type == BPF_WRITE)
2758 case offsetof(struct xdp_md, data):
2759 *reg_type = PTR_TO_PACKET;
2761 case offsetof(struct xdp_md, data_end):
2762 *reg_type = PTR_TO_PACKET_END;
2766 return __is_valid_xdp_access(off, size, type);
2769 void bpf_warn_invalid_xdp_action(u32 act)
2771 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
2773 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
2775 static u32 sk_filter_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2776 int src_reg, int ctx_off,
2777 struct bpf_insn *insn_buf,
2778 struct bpf_prog *prog)
2780 struct bpf_insn *insn = insn_buf;
2783 case offsetof(struct __sk_buff, len):
2784 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2786 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2787 offsetof(struct sk_buff, len));
2790 case offsetof(struct __sk_buff, protocol):
2791 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2793 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2794 offsetof(struct sk_buff, protocol));
2797 case offsetof(struct __sk_buff, vlan_proto):
2798 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2800 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2801 offsetof(struct sk_buff, vlan_proto));
2804 case offsetof(struct __sk_buff, priority):
2805 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2807 if (type == BPF_WRITE)
2808 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2809 offsetof(struct sk_buff, priority));
2811 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2812 offsetof(struct sk_buff, priority));
2815 case offsetof(struct __sk_buff, ingress_ifindex):
2816 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2818 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2819 offsetof(struct sk_buff, skb_iif));
2822 case offsetof(struct __sk_buff, ifindex):
2823 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2825 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
2827 offsetof(struct sk_buff, dev));
2828 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2829 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2830 offsetof(struct net_device, ifindex));
2833 case offsetof(struct __sk_buff, hash):
2834 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2836 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2837 offsetof(struct sk_buff, hash));
2840 case offsetof(struct __sk_buff, mark):
2841 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2843 if (type == BPF_WRITE)
2844 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2845 offsetof(struct sk_buff, mark));
2847 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2848 offsetof(struct sk_buff, mark));
2851 case offsetof(struct __sk_buff, pkt_type):
2852 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2854 case offsetof(struct __sk_buff, queue_mapping):
2855 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
2857 case offsetof(struct __sk_buff, vlan_present):
2858 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2859 dst_reg, src_reg, insn);
2861 case offsetof(struct __sk_buff, vlan_tci):
2862 return convert_skb_access(SKF_AD_VLAN_TAG,
2863 dst_reg, src_reg, insn);
2865 case offsetof(struct __sk_buff, cb[0]) ...
2866 offsetof(struct __sk_buff, cb[4]):
2867 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2869 prog->cb_access = 1;
2870 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2871 ctx_off += offsetof(struct sk_buff, cb);
2872 ctx_off += offsetof(struct qdisc_skb_cb, data);
2873 if (type == BPF_WRITE)
2874 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2876 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2879 case offsetof(struct __sk_buff, tc_classid):
2880 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2881 ctx_off += offsetof(struct sk_buff, cb);
2882 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
2883 if (type == BPF_WRITE)
2884 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2886 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2889 case offsetof(struct __sk_buff, data):
2890 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
2892 offsetof(struct sk_buff, data));
2895 case offsetof(struct __sk_buff, data_end):
2896 ctx_off -= offsetof(struct __sk_buff, data_end);
2897 ctx_off += offsetof(struct sk_buff, cb);
2898 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
2899 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), dst_reg, src_reg,
2903 case offsetof(struct __sk_buff, tc_index):
2904 #ifdef CONFIG_NET_SCHED
2905 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2907 if (type == BPF_WRITE)
2908 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2909 offsetof(struct sk_buff, tc_index));
2911 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2912 offsetof(struct sk_buff, tc_index));
2915 if (type == BPF_WRITE)
2916 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2918 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2923 return insn - insn_buf;
2926 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2927 int src_reg, int ctx_off,
2928 struct bpf_insn *insn_buf,
2929 struct bpf_prog *prog)
2931 struct bpf_insn *insn = insn_buf;
2934 case offsetof(struct __sk_buff, ifindex):
2935 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2937 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
2939 offsetof(struct sk_buff, dev));
2940 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2941 offsetof(struct net_device, ifindex));
2944 return sk_filter_convert_ctx_access(type, dst_reg, src_reg,
2945 ctx_off, insn_buf, prog);
2948 return insn - insn_buf;
2951 static u32 xdp_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2952 int src_reg, int ctx_off,
2953 struct bpf_insn *insn_buf,
2954 struct bpf_prog *prog)
2956 struct bpf_insn *insn = insn_buf;
2959 case offsetof(struct xdp_md, data):
2960 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
2962 offsetof(struct xdp_buff, data));
2964 case offsetof(struct xdp_md, data_end):
2965 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
2967 offsetof(struct xdp_buff, data_end));
2971 return insn - insn_buf;
2974 static const struct bpf_verifier_ops sk_filter_ops = {
2975 .get_func_proto = sk_filter_func_proto,
2976 .is_valid_access = sk_filter_is_valid_access,
2977 .convert_ctx_access = sk_filter_convert_ctx_access,
2980 static const struct bpf_verifier_ops tc_cls_act_ops = {
2981 .get_func_proto = tc_cls_act_func_proto,
2982 .is_valid_access = tc_cls_act_is_valid_access,
2983 .convert_ctx_access = tc_cls_act_convert_ctx_access,
2984 .gen_prologue = tc_cls_act_prologue,
2987 static const struct bpf_verifier_ops xdp_ops = {
2988 .get_func_proto = xdp_func_proto,
2989 .is_valid_access = xdp_is_valid_access,
2990 .convert_ctx_access = xdp_convert_ctx_access,
2993 static struct bpf_prog_type_list sk_filter_type __read_mostly = {
2994 .ops = &sk_filter_ops,
2995 .type = BPF_PROG_TYPE_SOCKET_FILTER,
2998 static struct bpf_prog_type_list sched_cls_type __read_mostly = {
2999 .ops = &tc_cls_act_ops,
3000 .type = BPF_PROG_TYPE_SCHED_CLS,
3003 static struct bpf_prog_type_list sched_act_type __read_mostly = {
3004 .ops = &tc_cls_act_ops,
3005 .type = BPF_PROG_TYPE_SCHED_ACT,
3008 static struct bpf_prog_type_list xdp_type __read_mostly = {
3010 .type = BPF_PROG_TYPE_XDP,
3013 static int __init register_sk_filter_ops(void)
3015 bpf_register_prog_type(&sk_filter_type);
3016 bpf_register_prog_type(&sched_cls_type);
3017 bpf_register_prog_type(&sched_act_type);
3018 bpf_register_prog_type(&xdp_type);
3022 late_initcall(register_sk_filter_ops);
3024 int sk_detach_filter(struct sock *sk)
3027 struct sk_filter *filter;
3029 if (sock_flag(sk, SOCK_FILTER_LOCKED))
3032 filter = rcu_dereference_protected(sk->sk_filter,
3033 lockdep_sock_is_held(sk));
3035 RCU_INIT_POINTER(sk->sk_filter, NULL);
3036 sk_filter_uncharge(sk, filter);
3042 EXPORT_SYMBOL_GPL(sk_detach_filter);
3044 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
3047 struct sock_fprog_kern *fprog;
3048 struct sk_filter *filter;
3052 filter = rcu_dereference_protected(sk->sk_filter,
3053 lockdep_sock_is_held(sk));
3057 /* We're copying the filter that has been originally attached,
3058 * so no conversion/decode needed anymore. eBPF programs that
3059 * have no original program cannot be dumped through this.
3062 fprog = filter->prog->orig_prog;
3068 /* User space only enquires number of filter blocks. */
3072 if (len < fprog->len)
3076 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
3079 /* Instead of bytes, the API requests to return the number