2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
29 #include <linux/sock_diag.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_packet.h>
34 #include <linux/if_arp.h>
35 #include <linux/gfp.h>
37 #include <net/protocol.h>
38 #include <net/netlink.h>
39 #include <linux/skbuff.h>
41 #include <net/flow_dissector.h>
42 #include <linux/errno.h>
43 #include <linux/timer.h>
44 #include <linux/uaccess.h>
45 #include <asm/unaligned.h>
46 #include <linux/filter.h>
47 #include <linux/ratelimit.h>
48 #include <linux/seccomp.h>
49 #include <linux/if_vlan.h>
50 #include <linux/bpf.h>
51 #include <net/sch_generic.h>
52 #include <net/cls_cgroup.h>
53 #include <net/dst_metadata.h>
55 #include <net/sock_reuseport.h>
56 #include <net/busy_poll.h>
58 #include <linux/bpf_trace.h>
61 * sk_filter_trim_cap - run a packet through a socket filter
62 * @sk: sock associated with &sk_buff
63 * @skb: buffer to filter
64 * @cap: limit on how short the eBPF program may trim the packet
66 * Run the eBPF program and then cut skb->data to correct size returned by
67 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
68 * than pkt_len we keep whole skb->data. This is the socket level
69 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
70 * be accepted or -EPERM if the packet should be tossed.
73 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
76 struct sk_filter *filter;
79 * If the skb was allocated from pfmemalloc reserves, only
80 * allow SOCK_MEMALLOC sockets to use it as this socket is
83 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
84 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
87 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
91 err = security_sock_rcv_skb(sk, skb);
96 filter = rcu_dereference(sk->sk_filter);
98 struct sock *save_sk = skb->sk;
102 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
104 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
110 EXPORT_SYMBOL(sk_filter_trim_cap);
112 BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
114 return skb_get_poff(skb);
117 BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
121 if (skb_is_nonlinear(skb))
124 if (skb->len < sizeof(struct nlattr))
127 if (a > skb->len - sizeof(struct nlattr))
130 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
132 return (void *) nla - (void *) skb->data;
137 BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
141 if (skb_is_nonlinear(skb))
144 if (skb->len < sizeof(struct nlattr))
147 if (a > skb->len - sizeof(struct nlattr))
150 nla = (struct nlattr *) &skb->data[a];
151 if (nla->nla_len > skb->len - a)
154 nla = nla_find_nested(nla, x);
156 return (void *) nla - (void *) skb->data;
161 BPF_CALL_0(__get_raw_cpu_id)
163 return raw_smp_processor_id();
166 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
167 .func = __get_raw_cpu_id,
169 .ret_type = RET_INTEGER,
172 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
173 struct bpf_insn *insn_buf)
175 struct bpf_insn *insn = insn_buf;
179 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
181 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
182 offsetof(struct sk_buff, mark));
186 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
187 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
188 #ifdef __BIG_ENDIAN_BITFIELD
189 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
194 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
196 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
197 offsetof(struct sk_buff, queue_mapping));
200 case SKF_AD_VLAN_TAG:
201 case SKF_AD_VLAN_TAG_PRESENT:
202 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
203 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
205 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
206 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
207 offsetof(struct sk_buff, vlan_tci));
208 if (skb_field == SKF_AD_VLAN_TAG) {
209 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
213 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
215 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
220 return insn - insn_buf;
223 static bool convert_bpf_extensions(struct sock_filter *fp,
224 struct bpf_insn **insnp)
226 struct bpf_insn *insn = *insnp;
230 case SKF_AD_OFF + SKF_AD_PROTOCOL:
231 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
233 /* A = *(u16 *) (CTX + offsetof(protocol)) */
234 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
235 offsetof(struct sk_buff, protocol));
236 /* A = ntohs(A) [emitting a nop or swap16] */
237 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
240 case SKF_AD_OFF + SKF_AD_PKTTYPE:
241 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
245 case SKF_AD_OFF + SKF_AD_IFINDEX:
246 case SKF_AD_OFF + SKF_AD_HATYPE:
247 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
248 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
250 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
251 BPF_REG_TMP, BPF_REG_CTX,
252 offsetof(struct sk_buff, dev));
253 /* if (tmp != 0) goto pc + 1 */
254 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
255 *insn++ = BPF_EXIT_INSN();
256 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
257 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
258 offsetof(struct net_device, ifindex));
260 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
261 offsetof(struct net_device, type));
264 case SKF_AD_OFF + SKF_AD_MARK:
265 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
269 case SKF_AD_OFF + SKF_AD_RXHASH:
270 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
272 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
273 offsetof(struct sk_buff, hash));
276 case SKF_AD_OFF + SKF_AD_QUEUE:
277 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
281 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
282 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
283 BPF_REG_A, BPF_REG_CTX, insn);
287 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
288 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
289 BPF_REG_A, BPF_REG_CTX, insn);
293 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
294 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
296 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
297 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
298 offsetof(struct sk_buff, vlan_proto));
299 /* A = ntohs(A) [emitting a nop or swap16] */
300 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
303 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
304 case SKF_AD_OFF + SKF_AD_NLATTR:
305 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
306 case SKF_AD_OFF + SKF_AD_CPU:
307 case SKF_AD_OFF + SKF_AD_RANDOM:
309 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
311 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
313 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
314 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
316 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
317 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
319 case SKF_AD_OFF + SKF_AD_NLATTR:
320 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
322 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
323 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
325 case SKF_AD_OFF + SKF_AD_CPU:
326 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
328 case SKF_AD_OFF + SKF_AD_RANDOM:
329 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
330 bpf_user_rnd_init_once();
335 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
337 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
341 /* This is just a dummy call to avoid letting the compiler
342 * evict __bpf_call_base() as an optimization. Placed here
343 * where no-one bothers.
345 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
354 * bpf_convert_filter - convert filter program
355 * @prog: the user passed filter program
356 * @len: the length of the user passed filter program
357 * @new_prog: allocated 'struct bpf_prog' or NULL
358 * @new_len: pointer to store length of converted program
360 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
361 * style extended BPF (eBPF).
362 * Conversion workflow:
364 * 1) First pass for calculating the new program length:
365 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
367 * 2) 2nd pass to remap in two passes: 1st pass finds new
368 * jump offsets, 2nd pass remapping:
369 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
371 static int bpf_convert_filter(struct sock_filter *prog, int len,
372 struct bpf_prog *new_prog, int *new_len)
374 int new_flen = 0, pass = 0, target, i, stack_off;
375 struct bpf_insn *new_insn, *first_insn = NULL;
376 struct sock_filter *fp;
380 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
381 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
383 if (len <= 0 || len > BPF_MAXINSNS)
387 first_insn = new_prog->insnsi;
388 addrs = kcalloc(len, sizeof(*addrs),
389 GFP_KERNEL | __GFP_NOWARN);
395 new_insn = first_insn;
398 /* Classic BPF related prologue emission. */
400 /* Classic BPF expects A and X to be reset first. These need
401 * to be guaranteed to be the first two instructions.
403 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
404 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
406 /* All programs must keep CTX in callee saved BPF_REG_CTX.
407 * In eBPF case it's done by the compiler, here we need to
408 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
410 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
415 for (i = 0; i < len; fp++, i++) {
416 struct bpf_insn tmp_insns[6] = { };
417 struct bpf_insn *insn = tmp_insns;
420 addrs[i] = new_insn - first_insn;
423 /* All arithmetic insns and skb loads map as-is. */
424 case BPF_ALU | BPF_ADD | BPF_X:
425 case BPF_ALU | BPF_ADD | BPF_K:
426 case BPF_ALU | BPF_SUB | BPF_X:
427 case BPF_ALU | BPF_SUB | BPF_K:
428 case BPF_ALU | BPF_AND | BPF_X:
429 case BPF_ALU | BPF_AND | BPF_K:
430 case BPF_ALU | BPF_OR | BPF_X:
431 case BPF_ALU | BPF_OR | BPF_K:
432 case BPF_ALU | BPF_LSH | BPF_X:
433 case BPF_ALU | BPF_LSH | BPF_K:
434 case BPF_ALU | BPF_RSH | BPF_X:
435 case BPF_ALU | BPF_RSH | BPF_K:
436 case BPF_ALU | BPF_XOR | BPF_X:
437 case BPF_ALU | BPF_XOR | BPF_K:
438 case BPF_ALU | BPF_MUL | BPF_X:
439 case BPF_ALU | BPF_MUL | BPF_K:
440 case BPF_ALU | BPF_DIV | BPF_X:
441 case BPF_ALU | BPF_DIV | BPF_K:
442 case BPF_ALU | BPF_MOD | BPF_X:
443 case BPF_ALU | BPF_MOD | BPF_K:
444 case BPF_ALU | BPF_NEG:
445 case BPF_LD | BPF_ABS | BPF_W:
446 case BPF_LD | BPF_ABS | BPF_H:
447 case BPF_LD | BPF_ABS | BPF_B:
448 case BPF_LD | BPF_IND | BPF_W:
449 case BPF_LD | BPF_IND | BPF_H:
450 case BPF_LD | BPF_IND | BPF_B:
451 /* Check for overloaded BPF extension and
452 * directly convert it if found, otherwise
453 * just move on with mapping.
455 if (BPF_CLASS(fp->code) == BPF_LD &&
456 BPF_MODE(fp->code) == BPF_ABS &&
457 convert_bpf_extensions(fp, &insn))
460 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
463 /* Jump transformation cannot use BPF block macros
464 * everywhere as offset calculation and target updates
465 * require a bit more work than the rest, i.e. jump
466 * opcodes map as-is, but offsets need adjustment.
469 #define BPF_EMIT_JMP \
471 if (target >= len || target < 0) \
473 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
474 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
475 insn->off -= insn - tmp_insns; \
478 case BPF_JMP | BPF_JA:
479 target = i + fp->k + 1;
480 insn->code = fp->code;
484 case BPF_JMP | BPF_JEQ | BPF_K:
485 case BPF_JMP | BPF_JEQ | BPF_X:
486 case BPF_JMP | BPF_JSET | BPF_K:
487 case BPF_JMP | BPF_JSET | BPF_X:
488 case BPF_JMP | BPF_JGT | BPF_K:
489 case BPF_JMP | BPF_JGT | BPF_X:
490 case BPF_JMP | BPF_JGE | BPF_K:
491 case BPF_JMP | BPF_JGE | BPF_X:
492 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
493 /* BPF immediates are signed, zero extend
494 * immediate into tmp register and use it
497 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
499 insn->dst_reg = BPF_REG_A;
500 insn->src_reg = BPF_REG_TMP;
503 insn->dst_reg = BPF_REG_A;
505 bpf_src = BPF_SRC(fp->code);
506 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
509 /* Common case where 'jump_false' is next insn. */
511 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
512 target = i + fp->jt + 1;
517 /* Convert some jumps when 'jump_true' is next insn. */
519 switch (BPF_OP(fp->code)) {
521 insn->code = BPF_JMP | BPF_JNE | bpf_src;
524 insn->code = BPF_JMP | BPF_JLE | bpf_src;
527 insn->code = BPF_JMP | BPF_JLT | bpf_src;
533 target = i + fp->jf + 1;
538 /* Other jumps are mapped into two insns: Jxx and JA. */
539 target = i + fp->jt + 1;
540 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
544 insn->code = BPF_JMP | BPF_JA;
545 target = i + fp->jf + 1;
549 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
550 case BPF_LDX | BPF_MSH | BPF_B:
552 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
553 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
554 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
556 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
558 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
560 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
562 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
565 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
566 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
568 case BPF_RET | BPF_A:
569 case BPF_RET | BPF_K:
570 if (BPF_RVAL(fp->code) == BPF_K)
571 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
573 *insn = BPF_EXIT_INSN();
576 /* Store to stack. */
579 stack_off = fp->k * 4 + 4;
580 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
581 BPF_ST ? BPF_REG_A : BPF_REG_X,
583 /* check_load_and_stores() verifies that classic BPF can
584 * load from stack only after write, so tracking
585 * stack_depth for ST|STX insns is enough
587 if (new_prog && new_prog->aux->stack_depth < stack_off)
588 new_prog->aux->stack_depth = stack_off;
591 /* Load from stack. */
592 case BPF_LD | BPF_MEM:
593 case BPF_LDX | BPF_MEM:
594 stack_off = fp->k * 4 + 4;
595 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
596 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
601 case BPF_LD | BPF_IMM:
602 case BPF_LDX | BPF_IMM:
603 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
604 BPF_REG_A : BPF_REG_X, fp->k);
608 case BPF_MISC | BPF_TAX:
609 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
613 case BPF_MISC | BPF_TXA:
614 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
617 /* A = skb->len or X = skb->len */
618 case BPF_LD | BPF_W | BPF_LEN:
619 case BPF_LDX | BPF_W | BPF_LEN:
620 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
621 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
622 offsetof(struct sk_buff, len));
625 /* Access seccomp_data fields. */
626 case BPF_LDX | BPF_ABS | BPF_W:
627 /* A = *(u32 *) (ctx + K) */
628 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
631 /* Unknown instruction. */
638 memcpy(new_insn, tmp_insns,
639 sizeof(*insn) * (insn - tmp_insns));
640 new_insn += insn - tmp_insns;
644 /* Only calculating new length. */
645 *new_len = new_insn - first_insn;
650 if (new_flen != new_insn - first_insn) {
651 new_flen = new_insn - first_insn;
658 BUG_ON(*new_len != new_flen);
667 * As we dont want to clear mem[] array for each packet going through
668 * __bpf_prog_run(), we check that filter loaded by user never try to read
669 * a cell if not previously written, and we check all branches to be sure
670 * a malicious user doesn't try to abuse us.
672 static int check_load_and_stores(const struct sock_filter *filter, int flen)
674 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
677 BUILD_BUG_ON(BPF_MEMWORDS > 16);
679 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
683 memset(masks, 0xff, flen * sizeof(*masks));
685 for (pc = 0; pc < flen; pc++) {
686 memvalid &= masks[pc];
688 switch (filter[pc].code) {
691 memvalid |= (1 << filter[pc].k);
693 case BPF_LD | BPF_MEM:
694 case BPF_LDX | BPF_MEM:
695 if (!(memvalid & (1 << filter[pc].k))) {
700 case BPF_JMP | BPF_JA:
701 /* A jump must set masks on target */
702 masks[pc + 1 + filter[pc].k] &= memvalid;
705 case BPF_JMP | BPF_JEQ | BPF_K:
706 case BPF_JMP | BPF_JEQ | BPF_X:
707 case BPF_JMP | BPF_JGE | BPF_K:
708 case BPF_JMP | BPF_JGE | BPF_X:
709 case BPF_JMP | BPF_JGT | BPF_K:
710 case BPF_JMP | BPF_JGT | BPF_X:
711 case BPF_JMP | BPF_JSET | BPF_K:
712 case BPF_JMP | BPF_JSET | BPF_X:
713 /* A jump must set masks on targets */
714 masks[pc + 1 + filter[pc].jt] &= memvalid;
715 masks[pc + 1 + filter[pc].jf] &= memvalid;
725 static bool chk_code_allowed(u16 code_to_probe)
727 static const bool codes[] = {
728 /* 32 bit ALU operations */
729 [BPF_ALU | BPF_ADD | BPF_K] = true,
730 [BPF_ALU | BPF_ADD | BPF_X] = true,
731 [BPF_ALU | BPF_SUB | BPF_K] = true,
732 [BPF_ALU | BPF_SUB | BPF_X] = true,
733 [BPF_ALU | BPF_MUL | BPF_K] = true,
734 [BPF_ALU | BPF_MUL | BPF_X] = true,
735 [BPF_ALU | BPF_DIV | BPF_K] = true,
736 [BPF_ALU | BPF_DIV | BPF_X] = true,
737 [BPF_ALU | BPF_MOD | BPF_K] = true,
738 [BPF_ALU | BPF_MOD | BPF_X] = true,
739 [BPF_ALU | BPF_AND | BPF_K] = true,
740 [BPF_ALU | BPF_AND | BPF_X] = true,
741 [BPF_ALU | BPF_OR | BPF_K] = true,
742 [BPF_ALU | BPF_OR | BPF_X] = true,
743 [BPF_ALU | BPF_XOR | BPF_K] = true,
744 [BPF_ALU | BPF_XOR | BPF_X] = true,
745 [BPF_ALU | BPF_LSH | BPF_K] = true,
746 [BPF_ALU | BPF_LSH | BPF_X] = true,
747 [BPF_ALU | BPF_RSH | BPF_K] = true,
748 [BPF_ALU | BPF_RSH | BPF_X] = true,
749 [BPF_ALU | BPF_NEG] = true,
750 /* Load instructions */
751 [BPF_LD | BPF_W | BPF_ABS] = true,
752 [BPF_LD | BPF_H | BPF_ABS] = true,
753 [BPF_LD | BPF_B | BPF_ABS] = true,
754 [BPF_LD | BPF_W | BPF_LEN] = true,
755 [BPF_LD | BPF_W | BPF_IND] = true,
756 [BPF_LD | BPF_H | BPF_IND] = true,
757 [BPF_LD | BPF_B | BPF_IND] = true,
758 [BPF_LD | BPF_IMM] = true,
759 [BPF_LD | BPF_MEM] = true,
760 [BPF_LDX | BPF_W | BPF_LEN] = true,
761 [BPF_LDX | BPF_B | BPF_MSH] = true,
762 [BPF_LDX | BPF_IMM] = true,
763 [BPF_LDX | BPF_MEM] = true,
764 /* Store instructions */
767 /* Misc instructions */
768 [BPF_MISC | BPF_TAX] = true,
769 [BPF_MISC | BPF_TXA] = true,
770 /* Return instructions */
771 [BPF_RET | BPF_K] = true,
772 [BPF_RET | BPF_A] = true,
773 /* Jump instructions */
774 [BPF_JMP | BPF_JA] = true,
775 [BPF_JMP | BPF_JEQ | BPF_K] = true,
776 [BPF_JMP | BPF_JEQ | BPF_X] = true,
777 [BPF_JMP | BPF_JGE | BPF_K] = true,
778 [BPF_JMP | BPF_JGE | BPF_X] = true,
779 [BPF_JMP | BPF_JGT | BPF_K] = true,
780 [BPF_JMP | BPF_JGT | BPF_X] = true,
781 [BPF_JMP | BPF_JSET | BPF_K] = true,
782 [BPF_JMP | BPF_JSET | BPF_X] = true,
785 if (code_to_probe >= ARRAY_SIZE(codes))
788 return codes[code_to_probe];
791 static bool bpf_check_basics_ok(const struct sock_filter *filter,
796 if (flen == 0 || flen > BPF_MAXINSNS)
803 * bpf_check_classic - verify socket filter code
804 * @filter: filter to verify
805 * @flen: length of filter
807 * Check the user's filter code. If we let some ugly
808 * filter code slip through kaboom! The filter must contain
809 * no references or jumps that are out of range, no illegal
810 * instructions, and must end with a RET instruction.
812 * All jumps are forward as they are not signed.
814 * Returns 0 if the rule set is legal or -EINVAL if not.
816 static int bpf_check_classic(const struct sock_filter *filter,
822 /* Check the filter code now */
823 for (pc = 0; pc < flen; pc++) {
824 const struct sock_filter *ftest = &filter[pc];
826 /* May we actually operate on this code? */
827 if (!chk_code_allowed(ftest->code))
830 /* Some instructions need special checks */
831 switch (ftest->code) {
832 case BPF_ALU | BPF_DIV | BPF_K:
833 case BPF_ALU | BPF_MOD | BPF_K:
834 /* Check for division by zero */
838 case BPF_ALU | BPF_LSH | BPF_K:
839 case BPF_ALU | BPF_RSH | BPF_K:
843 case BPF_LD | BPF_MEM:
844 case BPF_LDX | BPF_MEM:
847 /* Check for invalid memory addresses */
848 if (ftest->k >= BPF_MEMWORDS)
851 case BPF_JMP | BPF_JA:
852 /* Note, the large ftest->k might cause loops.
853 * Compare this with conditional jumps below,
854 * where offsets are limited. --ANK (981016)
856 if (ftest->k >= (unsigned int)(flen - pc - 1))
859 case BPF_JMP | BPF_JEQ | BPF_K:
860 case BPF_JMP | BPF_JEQ | BPF_X:
861 case BPF_JMP | BPF_JGE | BPF_K:
862 case BPF_JMP | BPF_JGE | BPF_X:
863 case BPF_JMP | BPF_JGT | BPF_K:
864 case BPF_JMP | BPF_JGT | BPF_X:
865 case BPF_JMP | BPF_JSET | BPF_K:
866 case BPF_JMP | BPF_JSET | BPF_X:
867 /* Both conditionals must be safe */
868 if (pc + ftest->jt + 1 >= flen ||
869 pc + ftest->jf + 1 >= flen)
872 case BPF_LD | BPF_W | BPF_ABS:
873 case BPF_LD | BPF_H | BPF_ABS:
874 case BPF_LD | BPF_B | BPF_ABS:
876 if (bpf_anc_helper(ftest) & BPF_ANC)
878 /* Ancillary operation unknown or unsupported */
879 if (anc_found == false && ftest->k >= SKF_AD_OFF)
884 /* Last instruction must be a RET code */
885 switch (filter[flen - 1].code) {
886 case BPF_RET | BPF_K:
887 case BPF_RET | BPF_A:
888 return check_load_and_stores(filter, flen);
894 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
895 const struct sock_fprog *fprog)
897 unsigned int fsize = bpf_classic_proglen(fprog);
898 struct sock_fprog_kern *fkprog;
900 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
904 fkprog = fp->orig_prog;
905 fkprog->len = fprog->len;
907 fkprog->filter = kmemdup(fp->insns, fsize,
908 GFP_KERNEL | __GFP_NOWARN);
909 if (!fkprog->filter) {
910 kfree(fp->orig_prog);
917 static void bpf_release_orig_filter(struct bpf_prog *fp)
919 struct sock_fprog_kern *fprog = fp->orig_prog;
922 kfree(fprog->filter);
927 static void __bpf_prog_release(struct bpf_prog *prog)
929 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
932 bpf_release_orig_filter(prog);
937 static void __sk_filter_release(struct sk_filter *fp)
939 __bpf_prog_release(fp->prog);
944 * sk_filter_release_rcu - Release a socket filter by rcu_head
945 * @rcu: rcu_head that contains the sk_filter to free
947 static void sk_filter_release_rcu(struct rcu_head *rcu)
949 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
951 __sk_filter_release(fp);
955 * sk_filter_release - release a socket filter
956 * @fp: filter to remove
958 * Remove a filter from a socket and release its resources.
960 static void sk_filter_release(struct sk_filter *fp)
962 if (refcount_dec_and_test(&fp->refcnt))
963 call_rcu(&fp->rcu, sk_filter_release_rcu);
966 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
968 u32 filter_size = bpf_prog_size(fp->prog->len);
970 atomic_sub(filter_size, &sk->sk_omem_alloc);
971 sk_filter_release(fp);
974 /* try to charge the socket memory if there is space available
975 * return true on success
977 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
979 u32 filter_size = bpf_prog_size(fp->prog->len);
981 /* same check as in sock_kmalloc() */
982 if (filter_size <= sysctl_optmem_max &&
983 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
984 atomic_add(filter_size, &sk->sk_omem_alloc);
990 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
992 bool ret = __sk_filter_charge(sk, fp);
994 refcount_inc(&fp->refcnt);
998 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1000 struct sock_filter *old_prog;
1001 struct bpf_prog *old_fp;
1002 int err, new_len, old_len = fp->len;
1004 /* We are free to overwrite insns et al right here as it
1005 * won't be used at this point in time anymore internally
1006 * after the migration to the internal BPF instruction
1009 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1010 sizeof(struct bpf_insn));
1012 /* Conversion cannot happen on overlapping memory areas,
1013 * so we need to keep the user BPF around until the 2nd
1014 * pass. At this time, the user BPF is stored in fp->insns.
1016 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1017 GFP_KERNEL | __GFP_NOWARN);
1023 /* 1st pass: calculate the new program length. */
1024 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
1028 /* Expand fp for appending the new filter representation. */
1030 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1032 /* The old_fp is still around in case we couldn't
1033 * allocate new memory, so uncharge on that one.
1042 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1043 err = bpf_convert_filter(old_prog, old_len, fp, &new_len);
1045 /* 2nd bpf_convert_filter() can fail only if it fails
1046 * to allocate memory, remapping must succeed. Note,
1047 * that at this time old_fp has already been released
1052 /* We are guaranteed to never error here with cBPF to eBPF
1053 * transitions, since there's no issue with type compatibility
1054 * checks on program arrays.
1056 fp = bpf_prog_select_runtime(fp, &err);
1064 __bpf_prog_release(fp);
1065 return ERR_PTR(err);
1068 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1069 bpf_aux_classic_check_t trans)
1073 fp->bpf_func = NULL;
1076 err = bpf_check_classic(fp->insns, fp->len);
1078 __bpf_prog_release(fp);
1079 return ERR_PTR(err);
1082 /* There might be additional checks and transformations
1083 * needed on classic filters, f.e. in case of seccomp.
1086 err = trans(fp->insns, fp->len);
1088 __bpf_prog_release(fp);
1089 return ERR_PTR(err);
1093 /* Probe if we can JIT compile the filter and if so, do
1094 * the compilation of the filter.
1096 bpf_jit_compile(fp);
1098 /* JIT compiler couldn't process this filter, so do the
1099 * internal BPF translation for the optimized interpreter.
1102 fp = bpf_migrate_filter(fp);
1108 * bpf_prog_create - create an unattached filter
1109 * @pfp: the unattached filter that is created
1110 * @fprog: the filter program
1112 * Create a filter independent of any socket. We first run some
1113 * sanity checks on it to make sure it does not explode on us later.
1114 * If an error occurs or there is insufficient memory for the filter
1115 * a negative errno code is returned. On success the return is zero.
1117 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1119 unsigned int fsize = bpf_classic_proglen(fprog);
1120 struct bpf_prog *fp;
1122 /* Make sure new filter is there and in the right amounts. */
1123 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1126 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1130 memcpy(fp->insns, fprog->filter, fsize);
1132 fp->len = fprog->len;
1133 /* Since unattached filters are not copied back to user
1134 * space through sk_get_filter(), we do not need to hold
1135 * a copy here, and can spare us the work.
1137 fp->orig_prog = NULL;
1139 /* bpf_prepare_filter() already takes care of freeing
1140 * memory in case something goes wrong.
1142 fp = bpf_prepare_filter(fp, NULL);
1149 EXPORT_SYMBOL_GPL(bpf_prog_create);
1152 * bpf_prog_create_from_user - create an unattached filter from user buffer
1153 * @pfp: the unattached filter that is created
1154 * @fprog: the filter program
1155 * @trans: post-classic verifier transformation handler
1156 * @save_orig: save classic BPF program
1158 * This function effectively does the same as bpf_prog_create(), only
1159 * that it builds up its insns buffer from user space provided buffer.
1160 * It also allows for passing a bpf_aux_classic_check_t handler.
1162 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1163 bpf_aux_classic_check_t trans, bool save_orig)
1165 unsigned int fsize = bpf_classic_proglen(fprog);
1166 struct bpf_prog *fp;
1169 /* Make sure new filter is there and in the right amounts. */
1170 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1173 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1177 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1178 __bpf_prog_free(fp);
1182 fp->len = fprog->len;
1183 fp->orig_prog = NULL;
1186 err = bpf_prog_store_orig_filter(fp, fprog);
1188 __bpf_prog_free(fp);
1193 /* bpf_prepare_filter() already takes care of freeing
1194 * memory in case something goes wrong.
1196 fp = bpf_prepare_filter(fp, trans);
1203 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1205 void bpf_prog_destroy(struct bpf_prog *fp)
1207 __bpf_prog_release(fp);
1209 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1211 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1213 struct sk_filter *fp, *old_fp;
1215 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1221 if (!__sk_filter_charge(sk, fp)) {
1225 refcount_set(&fp->refcnt, 1);
1227 old_fp = rcu_dereference_protected(sk->sk_filter,
1228 lockdep_sock_is_held(sk));
1229 rcu_assign_pointer(sk->sk_filter, fp);
1232 sk_filter_uncharge(sk, old_fp);
1237 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1239 struct bpf_prog *old_prog;
1242 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1245 if (sk_unhashed(sk) && sk->sk_reuseport) {
1246 err = reuseport_alloc(sk);
1249 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1250 /* The socket wasn't bound with SO_REUSEPORT */
1254 old_prog = reuseport_attach_prog(sk, prog);
1256 bpf_prog_destroy(old_prog);
1262 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1264 unsigned int fsize = bpf_classic_proglen(fprog);
1265 struct bpf_prog *prog;
1268 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1269 return ERR_PTR(-EPERM);
1271 /* Make sure new filter is there and in the right amounts. */
1272 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1273 return ERR_PTR(-EINVAL);
1275 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1277 return ERR_PTR(-ENOMEM);
1279 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1280 __bpf_prog_free(prog);
1281 return ERR_PTR(-EFAULT);
1284 prog->len = fprog->len;
1286 err = bpf_prog_store_orig_filter(prog, fprog);
1288 __bpf_prog_free(prog);
1289 return ERR_PTR(-ENOMEM);
1292 /* bpf_prepare_filter() already takes care of freeing
1293 * memory in case something goes wrong.
1295 return bpf_prepare_filter(prog, NULL);
1299 * sk_attach_filter - attach a socket filter
1300 * @fprog: the filter program
1301 * @sk: the socket to use
1303 * Attach the user's filter code. We first run some sanity checks on
1304 * it to make sure it does not explode on us later. If an error
1305 * occurs or there is insufficient memory for the filter a negative
1306 * errno code is returned. On success the return is zero.
1308 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1310 struct bpf_prog *prog = __get_filter(fprog, sk);
1314 return PTR_ERR(prog);
1316 err = __sk_attach_prog(prog, sk);
1318 __bpf_prog_release(prog);
1324 EXPORT_SYMBOL_GPL(sk_attach_filter);
1326 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1328 struct bpf_prog *prog = __get_filter(fprog, sk);
1332 return PTR_ERR(prog);
1334 err = __reuseport_attach_prog(prog, sk);
1336 __bpf_prog_release(prog);
1343 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1345 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1346 return ERR_PTR(-EPERM);
1348 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1351 int sk_attach_bpf(u32 ufd, struct sock *sk)
1353 struct bpf_prog *prog = __get_bpf(ufd, sk);
1357 return PTR_ERR(prog);
1359 err = __sk_attach_prog(prog, sk);
1368 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1370 struct bpf_prog *prog = __get_bpf(ufd, sk);
1374 return PTR_ERR(prog);
1376 err = __reuseport_attach_prog(prog, sk);
1385 struct bpf_scratchpad {
1387 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1388 u8 buff[MAX_BPF_STACK];
1392 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1394 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1395 unsigned int write_len)
1397 return skb_ensure_writable(skb, write_len);
1400 static inline int bpf_try_make_writable(struct sk_buff *skb,
1401 unsigned int write_len)
1403 int err = __bpf_try_make_writable(skb, write_len);
1405 bpf_compute_data_end(skb);
1409 static int bpf_try_make_head_writable(struct sk_buff *skb)
1411 return bpf_try_make_writable(skb, skb_headlen(skb));
1414 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1416 if (skb_at_tc_ingress(skb))
1417 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1420 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1422 if (skb_at_tc_ingress(skb))
1423 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1426 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1427 const void *, from, u32, len, u64, flags)
1431 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1433 if (unlikely(offset > 0xffff))
1435 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1438 ptr = skb->data + offset;
1439 if (flags & BPF_F_RECOMPUTE_CSUM)
1440 __skb_postpull_rcsum(skb, ptr, len, offset);
1442 memcpy(ptr, from, len);
1444 if (flags & BPF_F_RECOMPUTE_CSUM)
1445 __skb_postpush_rcsum(skb, ptr, len, offset);
1446 if (flags & BPF_F_INVALIDATE_HASH)
1447 skb_clear_hash(skb);
1452 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1453 .func = bpf_skb_store_bytes,
1455 .ret_type = RET_INTEGER,
1456 .arg1_type = ARG_PTR_TO_CTX,
1457 .arg2_type = ARG_ANYTHING,
1458 .arg3_type = ARG_PTR_TO_MEM,
1459 .arg4_type = ARG_CONST_SIZE,
1460 .arg5_type = ARG_ANYTHING,
1463 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1464 void *, to, u32, len)
1468 if (unlikely(offset > 0xffff))
1471 ptr = skb_header_pointer(skb, offset, len, to);
1475 memcpy(to, ptr, len);
1483 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1484 .func = bpf_skb_load_bytes,
1486 .ret_type = RET_INTEGER,
1487 .arg1_type = ARG_PTR_TO_CTX,
1488 .arg2_type = ARG_ANYTHING,
1489 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1490 .arg4_type = ARG_CONST_SIZE,
1493 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1495 /* Idea is the following: should the needed direct read/write
1496 * test fail during runtime, we can pull in more data and redo
1497 * again, since implicitly, we invalidate previous checks here.
1499 * Or, since we know how much we need to make read/writeable,
1500 * this can be done once at the program beginning for direct
1501 * access case. By this we overcome limitations of only current
1502 * headroom being accessible.
1504 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1507 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1508 .func = bpf_skb_pull_data,
1510 .ret_type = RET_INTEGER,
1511 .arg1_type = ARG_PTR_TO_CTX,
1512 .arg2_type = ARG_ANYTHING,
1515 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1516 u64, from, u64, to, u64, flags)
1520 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1522 if (unlikely(offset > 0xffff || offset & 1))
1524 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1527 ptr = (__sum16 *)(skb->data + offset);
1528 switch (flags & BPF_F_HDR_FIELD_MASK) {
1530 if (unlikely(from != 0))
1533 csum_replace_by_diff(ptr, to);
1536 csum_replace2(ptr, from, to);
1539 csum_replace4(ptr, from, to);
1548 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1549 .func = bpf_l3_csum_replace,
1551 .ret_type = RET_INTEGER,
1552 .arg1_type = ARG_PTR_TO_CTX,
1553 .arg2_type = ARG_ANYTHING,
1554 .arg3_type = ARG_ANYTHING,
1555 .arg4_type = ARG_ANYTHING,
1556 .arg5_type = ARG_ANYTHING,
1559 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1560 u64, from, u64, to, u64, flags)
1562 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1563 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1564 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1567 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1568 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1570 if (unlikely(offset > 0xffff || offset & 1))
1572 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1575 ptr = (__sum16 *)(skb->data + offset);
1576 if (is_mmzero && !do_mforce && !*ptr)
1579 switch (flags & BPF_F_HDR_FIELD_MASK) {
1581 if (unlikely(from != 0))
1584 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1587 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1590 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1596 if (is_mmzero && !*ptr)
1597 *ptr = CSUM_MANGLED_0;
1601 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1602 .func = bpf_l4_csum_replace,
1604 .ret_type = RET_INTEGER,
1605 .arg1_type = ARG_PTR_TO_CTX,
1606 .arg2_type = ARG_ANYTHING,
1607 .arg3_type = ARG_ANYTHING,
1608 .arg4_type = ARG_ANYTHING,
1609 .arg5_type = ARG_ANYTHING,
1612 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1613 __be32 *, to, u32, to_size, __wsum, seed)
1615 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1616 u32 diff_size = from_size + to_size;
1619 /* This is quite flexible, some examples:
1621 * from_size == 0, to_size > 0, seed := csum --> pushing data
1622 * from_size > 0, to_size == 0, seed := csum --> pulling data
1623 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1625 * Even for diffing, from_size and to_size don't need to be equal.
1627 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1628 diff_size > sizeof(sp->diff)))
1631 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1632 sp->diff[j] = ~from[i];
1633 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1634 sp->diff[j] = to[i];
1636 return csum_partial(sp->diff, diff_size, seed);
1639 static const struct bpf_func_proto bpf_csum_diff_proto = {
1640 .func = bpf_csum_diff,
1643 .ret_type = RET_INTEGER,
1644 .arg1_type = ARG_PTR_TO_MEM,
1645 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1646 .arg3_type = ARG_PTR_TO_MEM,
1647 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1648 .arg5_type = ARG_ANYTHING,
1651 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1653 /* The interface is to be used in combination with bpf_csum_diff()
1654 * for direct packet writes. csum rotation for alignment as well
1655 * as emulating csum_sub() can be done from the eBPF program.
1657 if (skb->ip_summed == CHECKSUM_COMPLETE)
1658 return (skb->csum = csum_add(skb->csum, csum));
1663 static const struct bpf_func_proto bpf_csum_update_proto = {
1664 .func = bpf_csum_update,
1666 .ret_type = RET_INTEGER,
1667 .arg1_type = ARG_PTR_TO_CTX,
1668 .arg2_type = ARG_ANYTHING,
1671 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1673 return dev_forward_skb(dev, skb);
1676 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1677 struct sk_buff *skb)
1679 int ret = ____dev_forward_skb(dev, skb);
1683 ret = netif_rx(skb);
1689 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1693 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1694 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1701 __this_cpu_inc(xmit_recursion);
1702 ret = dev_queue_xmit(skb);
1703 __this_cpu_dec(xmit_recursion);
1708 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1711 /* skb->mac_len is not set on normal egress */
1712 unsigned int mlen = skb->network_header - skb->mac_header;
1714 __skb_pull(skb, mlen);
1716 /* At ingress, the mac header has already been pulled once.
1717 * At egress, skb_pospull_rcsum has to be done in case that
1718 * the skb is originated from ingress (i.e. a forwarded skb)
1719 * to ensure that rcsum starts at net header.
1721 if (!skb_at_tc_ingress(skb))
1722 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1723 skb_pop_mac_header(skb);
1724 skb_reset_mac_len(skb);
1725 return flags & BPF_F_INGRESS ?
1726 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1729 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1732 /* Verify that a link layer header is carried */
1733 if (unlikely(skb->mac_header >= skb->network_header)) {
1738 bpf_push_mac_rcsum(skb);
1739 return flags & BPF_F_INGRESS ?
1740 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1743 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1746 if (dev_is_mac_header_xmit(dev))
1747 return __bpf_redirect_common(skb, dev, flags);
1749 return __bpf_redirect_no_mac(skb, dev, flags);
1752 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1754 struct net_device *dev;
1755 struct sk_buff *clone;
1758 if (unlikely(flags & ~(BPF_F_INGRESS)))
1761 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1765 clone = skb_clone(skb, GFP_ATOMIC);
1766 if (unlikely(!clone))
1769 /* For direct write, we need to keep the invariant that the skbs
1770 * we're dealing with need to be uncloned. Should uncloning fail
1771 * here, we need to free the just generated clone to unclone once
1774 ret = bpf_try_make_head_writable(skb);
1775 if (unlikely(ret)) {
1780 return __bpf_redirect(clone, dev, flags);
1783 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1784 .func = bpf_clone_redirect,
1786 .ret_type = RET_INTEGER,
1787 .arg1_type = ARG_PTR_TO_CTX,
1788 .arg2_type = ARG_ANYTHING,
1789 .arg3_type = ARG_ANYTHING,
1792 struct redirect_info {
1795 struct bpf_map *map;
1796 struct bpf_map *map_to_flush;
1797 unsigned long map_owner;
1800 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1802 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1804 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1806 if (unlikely(flags & ~(BPF_F_INGRESS)))
1809 ri->ifindex = ifindex;
1812 return TC_ACT_REDIRECT;
1815 int skb_do_redirect(struct sk_buff *skb)
1817 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1818 struct net_device *dev;
1820 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1822 if (unlikely(!dev)) {
1827 return __bpf_redirect(skb, dev, ri->flags);
1830 static const struct bpf_func_proto bpf_redirect_proto = {
1831 .func = bpf_redirect,
1833 .ret_type = RET_INTEGER,
1834 .arg1_type = ARG_ANYTHING,
1835 .arg2_type = ARG_ANYTHING,
1838 BPF_CALL_3(bpf_sk_redirect_map, struct bpf_map *, map, u32, key, u64, flags)
1840 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1842 if (unlikely(flags))
1852 struct sock *do_sk_redirect_map(void)
1854 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1855 struct sock *sk = NULL;
1858 sk = __sock_map_lookup_elem(ri->map, ri->ifindex);
1862 /* we do not clear flags for future lookup */
1868 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
1869 .func = bpf_sk_redirect_map,
1871 .ret_type = RET_INTEGER,
1872 .arg1_type = ARG_CONST_MAP_PTR,
1873 .arg2_type = ARG_ANYTHING,
1874 .arg3_type = ARG_ANYTHING,
1877 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
1879 return task_get_classid(skb);
1882 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1883 .func = bpf_get_cgroup_classid,
1885 .ret_type = RET_INTEGER,
1886 .arg1_type = ARG_PTR_TO_CTX,
1889 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
1891 return dst_tclassid(skb);
1894 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1895 .func = bpf_get_route_realm,
1897 .ret_type = RET_INTEGER,
1898 .arg1_type = ARG_PTR_TO_CTX,
1901 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
1903 /* If skb_clear_hash() was called due to mangling, we can
1904 * trigger SW recalculation here. Later access to hash
1905 * can then use the inline skb->hash via context directly
1906 * instead of calling this helper again.
1908 return skb_get_hash(skb);
1911 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1912 .func = bpf_get_hash_recalc,
1914 .ret_type = RET_INTEGER,
1915 .arg1_type = ARG_PTR_TO_CTX,
1918 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1920 /* After all direct packet write, this can be used once for
1921 * triggering a lazy recalc on next skb_get_hash() invocation.
1923 skb_clear_hash(skb);
1927 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1928 .func = bpf_set_hash_invalid,
1930 .ret_type = RET_INTEGER,
1931 .arg1_type = ARG_PTR_TO_CTX,
1934 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
1936 /* Set user specified hash as L4(+), so that it gets returned
1937 * on skb_get_hash() call unless BPF prog later on triggers a
1940 __skb_set_sw_hash(skb, hash, true);
1944 static const struct bpf_func_proto bpf_set_hash_proto = {
1945 .func = bpf_set_hash,
1947 .ret_type = RET_INTEGER,
1948 .arg1_type = ARG_PTR_TO_CTX,
1949 .arg2_type = ARG_ANYTHING,
1952 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1957 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1958 vlan_proto != htons(ETH_P_8021AD)))
1959 vlan_proto = htons(ETH_P_8021Q);
1961 bpf_push_mac_rcsum(skb);
1962 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1963 bpf_pull_mac_rcsum(skb);
1965 bpf_compute_data_end(skb);
1969 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1970 .func = bpf_skb_vlan_push,
1972 .ret_type = RET_INTEGER,
1973 .arg1_type = ARG_PTR_TO_CTX,
1974 .arg2_type = ARG_ANYTHING,
1975 .arg3_type = ARG_ANYTHING,
1977 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1979 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
1983 bpf_push_mac_rcsum(skb);
1984 ret = skb_vlan_pop(skb);
1985 bpf_pull_mac_rcsum(skb);
1987 bpf_compute_data_end(skb);
1991 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1992 .func = bpf_skb_vlan_pop,
1994 .ret_type = RET_INTEGER,
1995 .arg1_type = ARG_PTR_TO_CTX,
1997 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
1999 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2001 /* Caller already did skb_cow() with len as headroom,
2002 * so no need to do it here.
2005 memmove(skb->data, skb->data + len, off);
2006 memset(skb->data + off, 0, len);
2008 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2009 * needed here as it does not change the skb->csum
2010 * result for checksum complete when summing over
2016 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2018 /* skb_ensure_writable() is not needed here, as we're
2019 * already working on an uncloned skb.
2021 if (unlikely(!pskb_may_pull(skb, off + len)))
2024 skb_postpull_rcsum(skb, skb->data + off, len);
2025 memmove(skb->data + len, skb->data, off);
2026 __skb_pull(skb, len);
2031 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2033 bool trans_same = skb->transport_header == skb->network_header;
2036 /* There's no need for __skb_push()/__skb_pull() pair to
2037 * get to the start of the mac header as we're guaranteed
2038 * to always start from here under eBPF.
2040 ret = bpf_skb_generic_push(skb, off, len);
2042 skb->mac_header -= len;
2043 skb->network_header -= len;
2045 skb->transport_header = skb->network_header;
2051 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2053 bool trans_same = skb->transport_header == skb->network_header;
2056 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2057 ret = bpf_skb_generic_pop(skb, off, len);
2059 skb->mac_header += len;
2060 skb->network_header += len;
2062 skb->transport_header = skb->network_header;
2068 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2070 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2071 u32 off = skb_mac_header_len(skb);
2074 ret = skb_cow(skb, len_diff);
2075 if (unlikely(ret < 0))
2078 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2079 if (unlikely(ret < 0))
2082 if (skb_is_gso(skb)) {
2083 /* SKB_GSO_TCPV4 needs to be changed into
2086 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2087 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
2088 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
2091 /* Due to IPv6 header, MSS needs to be downgraded. */
2092 skb_shinfo(skb)->gso_size -= len_diff;
2093 /* Header must be checked, and gso_segs recomputed. */
2094 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2095 skb_shinfo(skb)->gso_segs = 0;
2098 skb->protocol = htons(ETH_P_IPV6);
2099 skb_clear_hash(skb);
2104 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2106 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2107 u32 off = skb_mac_header_len(skb);
2110 ret = skb_unclone(skb, GFP_ATOMIC);
2111 if (unlikely(ret < 0))
2114 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2115 if (unlikely(ret < 0))
2118 if (skb_is_gso(skb)) {
2119 /* SKB_GSO_TCPV6 needs to be changed into
2122 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2123 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2124 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2127 /* Due to IPv4 header, MSS can be upgraded. */
2128 skb_shinfo(skb)->gso_size += len_diff;
2129 /* Header must be checked, and gso_segs recomputed. */
2130 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2131 skb_shinfo(skb)->gso_segs = 0;
2134 skb->protocol = htons(ETH_P_IP);
2135 skb_clear_hash(skb);
2140 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2142 __be16 from_proto = skb->protocol;
2144 if (from_proto == htons(ETH_P_IP) &&
2145 to_proto == htons(ETH_P_IPV6))
2146 return bpf_skb_proto_4_to_6(skb);
2148 if (from_proto == htons(ETH_P_IPV6) &&
2149 to_proto == htons(ETH_P_IP))
2150 return bpf_skb_proto_6_to_4(skb);
2155 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2160 if (unlikely(flags))
2163 /* General idea is that this helper does the basic groundwork
2164 * needed for changing the protocol, and eBPF program fills the
2165 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2166 * and other helpers, rather than passing a raw buffer here.
2168 * The rationale is to keep this minimal and without a need to
2169 * deal with raw packet data. F.e. even if we would pass buffers
2170 * here, the program still needs to call the bpf_lX_csum_replace()
2171 * helpers anyway. Plus, this way we keep also separation of
2172 * concerns, since f.e. bpf_skb_store_bytes() should only take
2175 * Currently, additional options and extension header space are
2176 * not supported, but flags register is reserved so we can adapt
2177 * that. For offloads, we mark packet as dodgy, so that headers
2178 * need to be verified first.
2180 ret = bpf_skb_proto_xlat(skb, proto);
2181 bpf_compute_data_end(skb);
2185 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2186 .func = bpf_skb_change_proto,
2188 .ret_type = RET_INTEGER,
2189 .arg1_type = ARG_PTR_TO_CTX,
2190 .arg2_type = ARG_ANYTHING,
2191 .arg3_type = ARG_ANYTHING,
2194 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2196 /* We only allow a restricted subset to be changed for now. */
2197 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2198 !skb_pkt_type_ok(pkt_type)))
2201 skb->pkt_type = pkt_type;
2205 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2206 .func = bpf_skb_change_type,
2208 .ret_type = RET_INTEGER,
2209 .arg1_type = ARG_PTR_TO_CTX,
2210 .arg2_type = ARG_ANYTHING,
2213 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2215 switch (skb->protocol) {
2216 case htons(ETH_P_IP):
2217 return sizeof(struct iphdr);
2218 case htons(ETH_P_IPV6):
2219 return sizeof(struct ipv6hdr);
2225 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2227 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2230 ret = skb_cow(skb, len_diff);
2231 if (unlikely(ret < 0))
2234 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2235 if (unlikely(ret < 0))
2238 if (skb_is_gso(skb)) {
2239 /* Due to header grow, MSS needs to be downgraded. */
2240 skb_shinfo(skb)->gso_size -= len_diff;
2241 /* Header must be checked, and gso_segs recomputed. */
2242 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2243 skb_shinfo(skb)->gso_segs = 0;
2249 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2251 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2254 ret = skb_unclone(skb, GFP_ATOMIC);
2255 if (unlikely(ret < 0))
2258 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2259 if (unlikely(ret < 0))
2262 if (skb_is_gso(skb)) {
2263 /* Due to header shrink, MSS can be upgraded. */
2264 skb_shinfo(skb)->gso_size += len_diff;
2265 /* Header must be checked, and gso_segs recomputed. */
2266 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2267 skb_shinfo(skb)->gso_segs = 0;
2273 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2275 return skb->dev->mtu + skb->dev->hard_header_len;
2278 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2280 bool trans_same = skb->transport_header == skb->network_header;
2281 u32 len_cur, len_diff_abs = abs(len_diff);
2282 u32 len_min = bpf_skb_net_base_len(skb);
2283 u32 len_max = __bpf_skb_max_len(skb);
2284 __be16 proto = skb->protocol;
2285 bool shrink = len_diff < 0;
2288 if (unlikely(len_diff_abs > 0xfffU))
2290 if (unlikely(proto != htons(ETH_P_IP) &&
2291 proto != htons(ETH_P_IPV6)))
2294 len_cur = skb->len - skb_network_offset(skb);
2295 if (skb_transport_header_was_set(skb) && !trans_same)
2296 len_cur = skb_network_header_len(skb);
2297 if ((shrink && (len_diff_abs >= len_cur ||
2298 len_cur - len_diff_abs < len_min)) ||
2299 (!shrink && (skb->len + len_diff_abs > len_max &&
2303 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2304 bpf_skb_net_grow(skb, len_diff_abs);
2306 bpf_compute_data_end(skb);
2310 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2311 u32, mode, u64, flags)
2313 if (unlikely(flags))
2315 if (likely(mode == BPF_ADJ_ROOM_NET))
2316 return bpf_skb_adjust_net(skb, len_diff);
2321 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2322 .func = bpf_skb_adjust_room,
2324 .ret_type = RET_INTEGER,
2325 .arg1_type = ARG_PTR_TO_CTX,
2326 .arg2_type = ARG_ANYTHING,
2327 .arg3_type = ARG_ANYTHING,
2328 .arg4_type = ARG_ANYTHING,
2331 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2333 u32 min_len = skb_network_offset(skb);
2335 if (skb_transport_header_was_set(skb))
2336 min_len = skb_transport_offset(skb);
2337 if (skb->ip_summed == CHECKSUM_PARTIAL)
2338 min_len = skb_checksum_start_offset(skb) +
2339 skb->csum_offset + sizeof(__sum16);
2343 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2345 unsigned int old_len = skb->len;
2348 ret = __skb_grow_rcsum(skb, new_len);
2350 memset(skb->data + old_len, 0, new_len - old_len);
2354 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2356 return __skb_trim_rcsum(skb, new_len);
2359 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2362 u32 max_len = __bpf_skb_max_len(skb);
2363 u32 min_len = __bpf_skb_min_len(skb);
2366 if (unlikely(flags || new_len > max_len || new_len < min_len))
2368 if (skb->encapsulation)
2371 /* The basic idea of this helper is that it's performing the
2372 * needed work to either grow or trim an skb, and eBPF program
2373 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2374 * bpf_lX_csum_replace() and others rather than passing a raw
2375 * buffer here. This one is a slow path helper and intended
2376 * for replies with control messages.
2378 * Like in bpf_skb_change_proto(), we want to keep this rather
2379 * minimal and without protocol specifics so that we are able
2380 * to separate concerns as in bpf_skb_store_bytes() should only
2381 * be the one responsible for writing buffers.
2383 * It's really expected to be a slow path operation here for
2384 * control message replies, so we're implicitly linearizing,
2385 * uncloning and drop offloads from the skb by this.
2387 ret = __bpf_try_make_writable(skb, skb->len);
2389 if (new_len > skb->len)
2390 ret = bpf_skb_grow_rcsum(skb, new_len);
2391 else if (new_len < skb->len)
2392 ret = bpf_skb_trim_rcsum(skb, new_len);
2393 if (!ret && skb_is_gso(skb))
2397 bpf_compute_data_end(skb);
2401 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2402 .func = bpf_skb_change_tail,
2404 .ret_type = RET_INTEGER,
2405 .arg1_type = ARG_PTR_TO_CTX,
2406 .arg2_type = ARG_ANYTHING,
2407 .arg3_type = ARG_ANYTHING,
2410 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2413 u32 max_len = __bpf_skb_max_len(skb);
2414 u32 new_len = skb->len + head_room;
2417 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2418 new_len < skb->len))
2421 ret = skb_cow(skb, head_room);
2423 /* Idea for this helper is that we currently only
2424 * allow to expand on mac header. This means that
2425 * skb->protocol network header, etc, stay as is.
2426 * Compared to bpf_skb_change_tail(), we're more
2427 * flexible due to not needing to linearize or
2428 * reset GSO. Intention for this helper is to be
2429 * used by an L3 skb that needs to push mac header
2430 * for redirection into L2 device.
2432 __skb_push(skb, head_room);
2433 memset(skb->data, 0, head_room);
2434 skb_reset_mac_header(skb);
2437 bpf_compute_data_end(skb);
2441 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2442 .func = bpf_skb_change_head,
2444 .ret_type = RET_INTEGER,
2445 .arg1_type = ARG_PTR_TO_CTX,
2446 .arg2_type = ARG_ANYTHING,
2447 .arg3_type = ARG_ANYTHING,
2450 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2452 void *data = xdp->data + offset;
2454 if (unlikely(data < xdp->data_hard_start ||
2455 data > xdp->data_end - ETH_HLEN))
2463 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2464 .func = bpf_xdp_adjust_head,
2466 .ret_type = RET_INTEGER,
2467 .arg1_type = ARG_PTR_TO_CTX,
2468 .arg2_type = ARG_ANYTHING,
2471 static int __bpf_tx_xdp(struct net_device *dev,
2472 struct bpf_map *map,
2473 struct xdp_buff *xdp,
2478 if (!dev->netdev_ops->ndo_xdp_xmit) {
2482 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
2486 __dev_map_insert_ctx(map, index);
2488 dev->netdev_ops->ndo_xdp_flush(dev);
2492 void xdp_do_flush_map(void)
2494 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2495 struct bpf_map *map = ri->map_to_flush;
2497 ri->map_to_flush = NULL;
2499 __dev_map_flush(map);
2501 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
2503 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
2506 return (unsigned long)xdp_prog->aux != aux;
2509 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
2510 struct bpf_prog *xdp_prog)
2512 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2513 unsigned long map_owner = ri->map_owner;
2514 struct bpf_map *map = ri->map;
2515 struct net_device *fwd = NULL;
2516 u32 index = ri->ifindex;
2523 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2529 fwd = __dev_map_lookup_elem(map, index);
2534 if (ri->map_to_flush && ri->map_to_flush != map)
2537 err = __bpf_tx_xdp(fwd, map, xdp, index);
2541 ri->map_to_flush = map;
2542 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
2545 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
2549 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
2550 struct bpf_prog *xdp_prog)
2552 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2553 struct net_device *fwd;
2554 u32 index = ri->ifindex;
2558 return xdp_do_redirect_map(dev, xdp, xdp_prog);
2560 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2562 if (unlikely(!fwd)) {
2567 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
2571 _trace_xdp_redirect(dev, xdp_prog, index);
2574 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2577 EXPORT_SYMBOL_GPL(xdp_do_redirect);
2579 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
2580 struct bpf_prog *xdp_prog)
2582 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2583 unsigned long map_owner = ri->map_owner;
2584 struct bpf_map *map = ri->map;
2585 struct net_device *fwd = NULL;
2586 u32 index = ri->ifindex;
2595 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2600 fwd = __dev_map_lookup_elem(map, index);
2602 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2604 if (unlikely(!fwd)) {
2609 if (unlikely(!(fwd->flags & IFF_UP))) {
2614 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
2615 if (skb->len > len) {
2621 map ? _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index)
2622 : _trace_xdp_redirect(dev, xdp_prog, index);
2625 map ? _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err)
2626 : _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2629 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
2631 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
2633 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2635 if (unlikely(flags))
2638 ri->ifindex = ifindex;
2643 return XDP_REDIRECT;
2646 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
2647 .func = bpf_xdp_redirect,
2649 .ret_type = RET_INTEGER,
2650 .arg1_type = ARG_ANYTHING,
2651 .arg2_type = ARG_ANYTHING,
2654 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
2655 unsigned long, map_owner)
2657 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2659 if (unlikely(flags))
2662 ri->ifindex = ifindex;
2665 ri->map_owner = map_owner;
2667 return XDP_REDIRECT;
2670 /* Note, arg4 is hidden from users and populated by the verifier
2671 * with the right pointer.
2673 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
2674 .func = bpf_xdp_redirect_map,
2676 .ret_type = RET_INTEGER,
2677 .arg1_type = ARG_CONST_MAP_PTR,
2678 .arg2_type = ARG_ANYTHING,
2679 .arg3_type = ARG_ANYTHING,
2682 bool bpf_helper_changes_pkt_data(void *func)
2684 if (func == bpf_skb_vlan_push ||
2685 func == bpf_skb_vlan_pop ||
2686 func == bpf_skb_store_bytes ||
2687 func == bpf_skb_change_proto ||
2688 func == bpf_skb_change_head ||
2689 func == bpf_skb_change_tail ||
2690 func == bpf_skb_adjust_room ||
2691 func == bpf_skb_pull_data ||
2692 func == bpf_clone_redirect ||
2693 func == bpf_l3_csum_replace ||
2694 func == bpf_l4_csum_replace ||
2695 func == bpf_xdp_adjust_head)
2701 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2702 unsigned long off, unsigned long len)
2704 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
2708 if (ptr != dst_buff)
2709 memcpy(dst_buff, ptr, len);
2714 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2715 u64, flags, void *, meta, u64, meta_size)
2717 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2719 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2721 if (unlikely(skb_size > skb->len))
2724 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2728 static const struct bpf_func_proto bpf_skb_event_output_proto = {
2729 .func = bpf_skb_event_output,
2731 .ret_type = RET_INTEGER,
2732 .arg1_type = ARG_PTR_TO_CTX,
2733 .arg2_type = ARG_CONST_MAP_PTR,
2734 .arg3_type = ARG_ANYTHING,
2735 .arg4_type = ARG_PTR_TO_MEM,
2736 .arg5_type = ARG_CONST_SIZE,
2739 static unsigned short bpf_tunnel_key_af(u64 flags)
2741 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2744 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2745 u32, size, u64, flags)
2747 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2748 u8 compat[sizeof(struct bpf_tunnel_key)];
2752 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2756 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2760 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2763 case offsetof(struct bpf_tunnel_key, tunnel_label):
2764 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2766 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2767 /* Fixup deprecated structure layouts here, so we have
2768 * a common path later on.
2770 if (ip_tunnel_info_af(info) != AF_INET)
2773 to = (struct bpf_tunnel_key *)compat;
2780 to->tunnel_id = be64_to_cpu(info->key.tun_id);
2781 to->tunnel_tos = info->key.tos;
2782 to->tunnel_ttl = info->key.ttl;
2784 if (flags & BPF_F_TUNINFO_IPV6) {
2785 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2786 sizeof(to->remote_ipv6));
2787 to->tunnel_label = be32_to_cpu(info->key.label);
2789 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
2792 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
2793 memcpy(to_orig, to, size);
2797 memset(to_orig, 0, size);
2801 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
2802 .func = bpf_skb_get_tunnel_key,
2804 .ret_type = RET_INTEGER,
2805 .arg1_type = ARG_PTR_TO_CTX,
2806 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2807 .arg3_type = ARG_CONST_SIZE,
2808 .arg4_type = ARG_ANYTHING,
2811 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
2813 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2816 if (unlikely(!info ||
2817 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2821 if (unlikely(size < info->options_len)) {
2826 ip_tunnel_info_opts_get(to, info);
2827 if (size > info->options_len)
2828 memset(to + info->options_len, 0, size - info->options_len);
2830 return info->options_len;
2832 memset(to, 0, size);
2836 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2837 .func = bpf_skb_get_tunnel_opt,
2839 .ret_type = RET_INTEGER,
2840 .arg1_type = ARG_PTR_TO_CTX,
2841 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2842 .arg3_type = ARG_CONST_SIZE,
2845 static struct metadata_dst __percpu *md_dst;
2847 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2848 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
2850 struct metadata_dst *md = this_cpu_ptr(md_dst);
2851 u8 compat[sizeof(struct bpf_tunnel_key)];
2852 struct ip_tunnel_info *info;
2854 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2855 BPF_F_DONT_FRAGMENT)))
2857 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2859 case offsetof(struct bpf_tunnel_key, tunnel_label):
2860 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2861 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2862 /* Fixup deprecated structure layouts here, so we have
2863 * a common path later on.
2865 memcpy(compat, from, size);
2866 memset(compat + size, 0, sizeof(compat) - size);
2867 from = (const struct bpf_tunnel_key *) compat;
2873 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2878 dst_hold((struct dst_entry *) md);
2879 skb_dst_set(skb, (struct dst_entry *) md);
2881 info = &md->u.tun_info;
2882 info->mode = IP_TUNNEL_INFO_TX;
2884 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
2885 if (flags & BPF_F_DONT_FRAGMENT)
2886 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2888 info->key.tun_id = cpu_to_be64(from->tunnel_id);
2889 info->key.tos = from->tunnel_tos;
2890 info->key.ttl = from->tunnel_ttl;
2892 if (flags & BPF_F_TUNINFO_IPV6) {
2893 info->mode |= IP_TUNNEL_INFO_IPV6;
2894 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2895 sizeof(from->remote_ipv6));
2896 info->key.label = cpu_to_be32(from->tunnel_label) &
2897 IPV6_FLOWLABEL_MASK;
2899 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2900 if (flags & BPF_F_ZERO_CSUM_TX)
2901 info->key.tun_flags &= ~TUNNEL_CSUM;
2907 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
2908 .func = bpf_skb_set_tunnel_key,
2910 .ret_type = RET_INTEGER,
2911 .arg1_type = ARG_PTR_TO_CTX,
2912 .arg2_type = ARG_PTR_TO_MEM,
2913 .arg3_type = ARG_CONST_SIZE,
2914 .arg4_type = ARG_ANYTHING,
2917 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2918 const u8 *, from, u32, size)
2920 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2921 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2923 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2925 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
2928 ip_tunnel_info_opts_set(info, from, size);
2933 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2934 .func = bpf_skb_set_tunnel_opt,
2936 .ret_type = RET_INTEGER,
2937 .arg1_type = ARG_PTR_TO_CTX,
2938 .arg2_type = ARG_PTR_TO_MEM,
2939 .arg3_type = ARG_CONST_SIZE,
2942 static const struct bpf_func_proto *
2943 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
2946 /* Race is not possible, since it's called from verifier
2947 * that is holding verifier mutex.
2949 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
2957 case BPF_FUNC_skb_set_tunnel_key:
2958 return &bpf_skb_set_tunnel_key_proto;
2959 case BPF_FUNC_skb_set_tunnel_opt:
2960 return &bpf_skb_set_tunnel_opt_proto;
2966 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2969 struct bpf_array *array = container_of(map, struct bpf_array, map);
2970 struct cgroup *cgrp;
2973 sk = skb_to_full_sk(skb);
2974 if (!sk || !sk_fullsock(sk))
2976 if (unlikely(idx >= array->map.max_entries))
2979 cgrp = READ_ONCE(array->ptrs[idx]);
2980 if (unlikely(!cgrp))
2983 return sk_under_cgroup_hierarchy(sk, cgrp);
2986 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2987 .func = bpf_skb_under_cgroup,
2989 .ret_type = RET_INTEGER,
2990 .arg1_type = ARG_PTR_TO_CTX,
2991 .arg2_type = ARG_CONST_MAP_PTR,
2992 .arg3_type = ARG_ANYTHING,
2995 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2996 unsigned long off, unsigned long len)
2998 memcpy(dst_buff, src_buff + off, len);
3002 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3003 u64, flags, void *, meta, u64, meta_size)
3005 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3007 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3009 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3012 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3013 xdp_size, bpf_xdp_copy);
3016 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3017 .func = bpf_xdp_event_output,
3019 .ret_type = RET_INTEGER,
3020 .arg1_type = ARG_PTR_TO_CTX,
3021 .arg2_type = ARG_CONST_MAP_PTR,
3022 .arg3_type = ARG_ANYTHING,
3023 .arg4_type = ARG_PTR_TO_MEM,
3024 .arg5_type = ARG_CONST_SIZE,
3027 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3029 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3032 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3033 .func = bpf_get_socket_cookie,
3035 .ret_type = RET_INTEGER,
3036 .arg1_type = ARG_PTR_TO_CTX,
3039 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3041 struct sock *sk = sk_to_full_sk(skb->sk);
3044 if (!sk || !sk_fullsock(sk))
3046 kuid = sock_net_uid(sock_net(sk), sk);
3047 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3050 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3051 .func = bpf_get_socket_uid,
3053 .ret_type = RET_INTEGER,
3054 .arg1_type = ARG_PTR_TO_CTX,
3057 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3058 int, level, int, optname, char *, optval, int, optlen)
3060 struct sock *sk = bpf_sock->sk;
3064 if (!sk_fullsock(sk))
3067 if (level == SOL_SOCKET) {
3068 if (optlen != sizeof(int))
3070 val = *((int *)optval);
3072 /* Only some socketops are supported */
3075 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3076 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3079 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3080 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3082 case SO_MAX_PACING_RATE:
3083 sk->sk_max_pacing_rate = val;
3084 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3085 sk->sk_max_pacing_rate);
3088 sk->sk_priority = val;
3093 sk->sk_rcvlowat = val ? : 1;
3102 } else if (level == SOL_TCP &&
3103 sk->sk_prot->setsockopt == tcp_setsockopt) {
3104 if (optname == TCP_CONGESTION) {
3105 char name[TCP_CA_NAME_MAX];
3106 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3108 strncpy(name, optval, min_t(long, optlen,
3109 TCP_CA_NAME_MAX-1));
3110 name[TCP_CA_NAME_MAX-1] = 0;
3111 ret = tcp_set_congestion_control(sk, name, false, reinit);
3113 struct tcp_sock *tp = tcp_sk(sk);
3115 if (optlen != sizeof(int))
3118 val = *((int *)optval);
3119 /* Only some options are supported */
3122 if (val <= 0 || tp->data_segs_out > 0)
3127 case TCP_BPF_SNDCWND_CLAMP:
3131 tp->snd_cwnd_clamp = val;
3132 tp->snd_ssthresh = val;
3146 static const struct bpf_func_proto bpf_setsockopt_proto = {
3147 .func = bpf_setsockopt,
3149 .ret_type = RET_INTEGER,
3150 .arg1_type = ARG_PTR_TO_CTX,
3151 .arg2_type = ARG_ANYTHING,
3152 .arg3_type = ARG_ANYTHING,
3153 .arg4_type = ARG_PTR_TO_MEM,
3154 .arg5_type = ARG_CONST_SIZE,
3157 static const struct bpf_func_proto *
3158 bpf_base_func_proto(enum bpf_func_id func_id)
3161 case BPF_FUNC_map_lookup_elem:
3162 return &bpf_map_lookup_elem_proto;
3163 case BPF_FUNC_map_update_elem:
3164 return &bpf_map_update_elem_proto;
3165 case BPF_FUNC_map_delete_elem:
3166 return &bpf_map_delete_elem_proto;
3167 case BPF_FUNC_get_prandom_u32:
3168 return &bpf_get_prandom_u32_proto;
3169 case BPF_FUNC_get_smp_processor_id:
3170 return &bpf_get_raw_smp_processor_id_proto;
3171 case BPF_FUNC_get_numa_node_id:
3172 return &bpf_get_numa_node_id_proto;
3173 case BPF_FUNC_tail_call:
3174 return &bpf_tail_call_proto;
3175 case BPF_FUNC_ktime_get_ns:
3176 return &bpf_ktime_get_ns_proto;
3177 case BPF_FUNC_trace_printk:
3178 if (capable(CAP_SYS_ADMIN))
3179 return bpf_get_trace_printk_proto();
3185 static const struct bpf_func_proto *
3186 sock_filter_func_proto(enum bpf_func_id func_id)
3189 /* inet and inet6 sockets are created in a process
3190 * context so there is always a valid uid/gid
3192 case BPF_FUNC_get_current_uid_gid:
3193 return &bpf_get_current_uid_gid_proto;
3195 return bpf_base_func_proto(func_id);
3199 static const struct bpf_func_proto *
3200 sk_filter_func_proto(enum bpf_func_id func_id)
3203 case BPF_FUNC_skb_load_bytes:
3204 return &bpf_skb_load_bytes_proto;
3205 case BPF_FUNC_get_socket_cookie:
3206 return &bpf_get_socket_cookie_proto;
3207 case BPF_FUNC_get_socket_uid:
3208 return &bpf_get_socket_uid_proto;
3210 return bpf_base_func_proto(func_id);
3214 static const struct bpf_func_proto *
3215 tc_cls_act_func_proto(enum bpf_func_id func_id)
3218 case BPF_FUNC_skb_store_bytes:
3219 return &bpf_skb_store_bytes_proto;
3220 case BPF_FUNC_skb_load_bytes:
3221 return &bpf_skb_load_bytes_proto;
3222 case BPF_FUNC_skb_pull_data:
3223 return &bpf_skb_pull_data_proto;
3224 case BPF_FUNC_csum_diff:
3225 return &bpf_csum_diff_proto;
3226 case BPF_FUNC_csum_update:
3227 return &bpf_csum_update_proto;
3228 case BPF_FUNC_l3_csum_replace:
3229 return &bpf_l3_csum_replace_proto;
3230 case BPF_FUNC_l4_csum_replace:
3231 return &bpf_l4_csum_replace_proto;
3232 case BPF_FUNC_clone_redirect:
3233 return &bpf_clone_redirect_proto;
3234 case BPF_FUNC_get_cgroup_classid:
3235 return &bpf_get_cgroup_classid_proto;
3236 case BPF_FUNC_skb_vlan_push:
3237 return &bpf_skb_vlan_push_proto;
3238 case BPF_FUNC_skb_vlan_pop:
3239 return &bpf_skb_vlan_pop_proto;
3240 case BPF_FUNC_skb_change_proto:
3241 return &bpf_skb_change_proto_proto;
3242 case BPF_FUNC_skb_change_type:
3243 return &bpf_skb_change_type_proto;
3244 case BPF_FUNC_skb_adjust_room:
3245 return &bpf_skb_adjust_room_proto;
3246 case BPF_FUNC_skb_change_tail:
3247 return &bpf_skb_change_tail_proto;
3248 case BPF_FUNC_skb_get_tunnel_key:
3249 return &bpf_skb_get_tunnel_key_proto;
3250 case BPF_FUNC_skb_set_tunnel_key:
3251 return bpf_get_skb_set_tunnel_proto(func_id);
3252 case BPF_FUNC_skb_get_tunnel_opt:
3253 return &bpf_skb_get_tunnel_opt_proto;
3254 case BPF_FUNC_skb_set_tunnel_opt:
3255 return bpf_get_skb_set_tunnel_proto(func_id);
3256 case BPF_FUNC_redirect:
3257 return &bpf_redirect_proto;
3258 case BPF_FUNC_get_route_realm:
3259 return &bpf_get_route_realm_proto;
3260 case BPF_FUNC_get_hash_recalc:
3261 return &bpf_get_hash_recalc_proto;
3262 case BPF_FUNC_set_hash_invalid:
3263 return &bpf_set_hash_invalid_proto;
3264 case BPF_FUNC_set_hash:
3265 return &bpf_set_hash_proto;
3266 case BPF_FUNC_perf_event_output:
3267 return &bpf_skb_event_output_proto;
3268 case BPF_FUNC_get_smp_processor_id:
3269 return &bpf_get_smp_processor_id_proto;
3270 case BPF_FUNC_skb_under_cgroup:
3271 return &bpf_skb_under_cgroup_proto;
3272 case BPF_FUNC_get_socket_cookie:
3273 return &bpf_get_socket_cookie_proto;
3274 case BPF_FUNC_get_socket_uid:
3275 return &bpf_get_socket_uid_proto;
3277 return bpf_base_func_proto(func_id);
3281 static const struct bpf_func_proto *
3282 xdp_func_proto(enum bpf_func_id func_id)
3285 case BPF_FUNC_perf_event_output:
3286 return &bpf_xdp_event_output_proto;
3287 case BPF_FUNC_get_smp_processor_id:
3288 return &bpf_get_smp_processor_id_proto;
3289 case BPF_FUNC_xdp_adjust_head:
3290 return &bpf_xdp_adjust_head_proto;
3291 case BPF_FUNC_redirect:
3292 return &bpf_xdp_redirect_proto;
3293 case BPF_FUNC_redirect_map:
3294 return &bpf_xdp_redirect_map_proto;
3296 return bpf_base_func_proto(func_id);
3300 static const struct bpf_func_proto *
3301 lwt_inout_func_proto(enum bpf_func_id func_id)
3304 case BPF_FUNC_skb_load_bytes:
3305 return &bpf_skb_load_bytes_proto;
3306 case BPF_FUNC_skb_pull_data:
3307 return &bpf_skb_pull_data_proto;
3308 case BPF_FUNC_csum_diff:
3309 return &bpf_csum_diff_proto;
3310 case BPF_FUNC_get_cgroup_classid:
3311 return &bpf_get_cgroup_classid_proto;
3312 case BPF_FUNC_get_route_realm:
3313 return &bpf_get_route_realm_proto;
3314 case BPF_FUNC_get_hash_recalc:
3315 return &bpf_get_hash_recalc_proto;
3316 case BPF_FUNC_perf_event_output:
3317 return &bpf_skb_event_output_proto;
3318 case BPF_FUNC_get_smp_processor_id:
3319 return &bpf_get_smp_processor_id_proto;
3320 case BPF_FUNC_skb_under_cgroup:
3321 return &bpf_skb_under_cgroup_proto;
3323 return bpf_base_func_proto(func_id);
3327 static const struct bpf_func_proto *
3328 sock_ops_func_proto(enum bpf_func_id func_id)
3331 case BPF_FUNC_setsockopt:
3332 return &bpf_setsockopt_proto;
3333 case BPF_FUNC_sock_map_update:
3334 return &bpf_sock_map_update_proto;
3336 return bpf_base_func_proto(func_id);
3340 static const struct bpf_func_proto *sk_skb_func_proto(enum bpf_func_id func_id)
3343 case BPF_FUNC_skb_store_bytes:
3344 return &bpf_skb_store_bytes_proto;
3345 case BPF_FUNC_skb_load_bytes:
3346 return &bpf_skb_load_bytes_proto;
3347 case BPF_FUNC_skb_pull_data:
3348 return &bpf_skb_pull_data_proto;
3349 case BPF_FUNC_skb_change_tail:
3350 return &bpf_skb_change_tail_proto;
3351 case BPF_FUNC_skb_change_head:
3352 return &bpf_skb_change_head_proto;
3353 case BPF_FUNC_get_socket_cookie:
3354 return &bpf_get_socket_cookie_proto;
3355 case BPF_FUNC_get_socket_uid:
3356 return &bpf_get_socket_uid_proto;
3357 case BPF_FUNC_sk_redirect_map:
3358 return &bpf_sk_redirect_map_proto;
3360 return bpf_base_func_proto(func_id);
3364 static const struct bpf_func_proto *
3365 lwt_xmit_func_proto(enum bpf_func_id func_id)
3368 case BPF_FUNC_skb_get_tunnel_key:
3369 return &bpf_skb_get_tunnel_key_proto;
3370 case BPF_FUNC_skb_set_tunnel_key:
3371 return bpf_get_skb_set_tunnel_proto(func_id);
3372 case BPF_FUNC_skb_get_tunnel_opt:
3373 return &bpf_skb_get_tunnel_opt_proto;
3374 case BPF_FUNC_skb_set_tunnel_opt:
3375 return bpf_get_skb_set_tunnel_proto(func_id);
3376 case BPF_FUNC_redirect:
3377 return &bpf_redirect_proto;
3378 case BPF_FUNC_clone_redirect:
3379 return &bpf_clone_redirect_proto;
3380 case BPF_FUNC_skb_change_tail:
3381 return &bpf_skb_change_tail_proto;
3382 case BPF_FUNC_skb_change_head:
3383 return &bpf_skb_change_head_proto;
3384 case BPF_FUNC_skb_store_bytes:
3385 return &bpf_skb_store_bytes_proto;
3386 case BPF_FUNC_csum_update:
3387 return &bpf_csum_update_proto;
3388 case BPF_FUNC_l3_csum_replace:
3389 return &bpf_l3_csum_replace_proto;
3390 case BPF_FUNC_l4_csum_replace:
3391 return &bpf_l4_csum_replace_proto;
3392 case BPF_FUNC_set_hash_invalid:
3393 return &bpf_set_hash_invalid_proto;
3395 return lwt_inout_func_proto(func_id);
3399 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
3400 struct bpf_insn_access_aux *info)
3402 const int size_default = sizeof(__u32);
3404 if (off < 0 || off >= sizeof(struct __sk_buff))
3407 /* The verifier guarantees that size > 0. */
3408 if (off % size != 0)
3412 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3413 if (off + size > offsetofend(struct __sk_buff, cb[4]))
3416 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
3417 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
3418 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
3419 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
3420 case bpf_ctx_range(struct __sk_buff, data):
3421 case bpf_ctx_range(struct __sk_buff, data_end):
3422 if (size != size_default)
3426 /* Only narrow read access allowed for now. */
3427 if (type == BPF_WRITE) {
3428 if (size != size_default)
3431 bpf_ctx_record_field_size(info, size_default);
3432 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
3440 static bool sk_filter_is_valid_access(int off, int size,
3441 enum bpf_access_type type,
3442 struct bpf_insn_access_aux *info)
3445 case bpf_ctx_range(struct __sk_buff, tc_classid):
3446 case bpf_ctx_range(struct __sk_buff, data):
3447 case bpf_ctx_range(struct __sk_buff, data_end):
3448 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3452 if (type == BPF_WRITE) {
3454 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3461 return bpf_skb_is_valid_access(off, size, type, info);
3464 static bool lwt_is_valid_access(int off, int size,
3465 enum bpf_access_type type,
3466 struct bpf_insn_access_aux *info)
3469 case bpf_ctx_range(struct __sk_buff, tc_classid):
3470 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3474 if (type == BPF_WRITE) {
3476 case bpf_ctx_range(struct __sk_buff, mark):
3477 case bpf_ctx_range(struct __sk_buff, priority):
3478 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3486 case bpf_ctx_range(struct __sk_buff, data):
3487 info->reg_type = PTR_TO_PACKET;
3489 case bpf_ctx_range(struct __sk_buff, data_end):
3490 info->reg_type = PTR_TO_PACKET_END;
3494 return bpf_skb_is_valid_access(off, size, type, info);
3497 static bool sock_filter_is_valid_access(int off, int size,
3498 enum bpf_access_type type,
3499 struct bpf_insn_access_aux *info)
3501 if (type == BPF_WRITE) {
3503 case offsetof(struct bpf_sock, bound_dev_if):
3504 case offsetof(struct bpf_sock, mark):
3505 case offsetof(struct bpf_sock, priority):
3512 if (off < 0 || off + size > sizeof(struct bpf_sock))
3514 /* The verifier guarantees that size > 0. */
3515 if (off % size != 0)
3517 if (size != sizeof(__u32))
3523 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
3524 const struct bpf_prog *prog, int drop_verdict)
3526 struct bpf_insn *insn = insn_buf;
3531 /* if (!skb->cloned)
3534 * (Fast-path, otherwise approximation that we might be
3535 * a clone, do the rest in helper.)
3537 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
3538 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
3539 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
3541 /* ret = bpf_skb_pull_data(skb, 0); */
3542 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
3543 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
3544 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
3545 BPF_FUNC_skb_pull_data);
3548 * return TC_ACT_SHOT;
3550 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
3551 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
3552 *insn++ = BPF_EXIT_INSN();
3555 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
3557 *insn++ = prog->insnsi[0];
3559 return insn - insn_buf;
3562 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
3563 const struct bpf_prog *prog)
3565 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
3568 static bool tc_cls_act_is_valid_access(int off, int size,
3569 enum bpf_access_type type,
3570 struct bpf_insn_access_aux *info)
3572 if (type == BPF_WRITE) {
3574 case bpf_ctx_range(struct __sk_buff, mark):
3575 case bpf_ctx_range(struct __sk_buff, tc_index):
3576 case bpf_ctx_range(struct __sk_buff, priority):
3577 case bpf_ctx_range(struct __sk_buff, tc_classid):
3578 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3586 case bpf_ctx_range(struct __sk_buff, data):
3587 info->reg_type = PTR_TO_PACKET;
3589 case bpf_ctx_range(struct __sk_buff, data_end):
3590 info->reg_type = PTR_TO_PACKET_END;
3592 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3596 return bpf_skb_is_valid_access(off, size, type, info);
3599 static bool __is_valid_xdp_access(int off, int size)
3601 if (off < 0 || off >= sizeof(struct xdp_md))
3603 if (off % size != 0)
3605 if (size != sizeof(__u32))
3611 static bool xdp_is_valid_access(int off, int size,
3612 enum bpf_access_type type,
3613 struct bpf_insn_access_aux *info)
3615 if (type == BPF_WRITE)
3619 case offsetof(struct xdp_md, data):
3620 info->reg_type = PTR_TO_PACKET;
3622 case offsetof(struct xdp_md, data_end):
3623 info->reg_type = PTR_TO_PACKET_END;
3627 return __is_valid_xdp_access(off, size);
3630 void bpf_warn_invalid_xdp_action(u32 act)
3632 const u32 act_max = XDP_REDIRECT;
3634 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
3635 act > act_max ? "Illegal" : "Driver unsupported",
3638 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
3640 static bool __is_valid_sock_ops_access(int off, int size)
3642 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
3644 /* The verifier guarantees that size > 0. */
3645 if (off % size != 0)
3647 if (size != sizeof(__u32))
3653 static bool sock_ops_is_valid_access(int off, int size,
3654 enum bpf_access_type type,
3655 struct bpf_insn_access_aux *info)
3657 if (type == BPF_WRITE) {
3659 case offsetof(struct bpf_sock_ops, op) ...
3660 offsetof(struct bpf_sock_ops, replylong[3]):
3667 return __is_valid_sock_ops_access(off, size);
3670 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
3671 const struct bpf_prog *prog)
3673 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
3676 static bool sk_skb_is_valid_access(int off, int size,
3677 enum bpf_access_type type,
3678 struct bpf_insn_access_aux *info)
3680 if (type == BPF_WRITE) {
3682 case bpf_ctx_range(struct __sk_buff, mark):
3683 case bpf_ctx_range(struct __sk_buff, tc_index):
3684 case bpf_ctx_range(struct __sk_buff, priority):
3692 case bpf_ctx_range(struct __sk_buff, tc_classid):
3694 case bpf_ctx_range(struct __sk_buff, data):
3695 info->reg_type = PTR_TO_PACKET;
3697 case bpf_ctx_range(struct __sk_buff, data_end):
3698 info->reg_type = PTR_TO_PACKET_END;
3702 return bpf_skb_is_valid_access(off, size, type, info);
3705 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
3706 const struct bpf_insn *si,
3707 struct bpf_insn *insn_buf,
3708 struct bpf_prog *prog, u32 *target_size)
3710 struct bpf_insn *insn = insn_buf;
3714 case offsetof(struct __sk_buff, len):
3715 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3716 bpf_target_off(struct sk_buff, len, 4,
3720 case offsetof(struct __sk_buff, protocol):
3721 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3722 bpf_target_off(struct sk_buff, protocol, 2,
3726 case offsetof(struct __sk_buff, vlan_proto):
3727 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3728 bpf_target_off(struct sk_buff, vlan_proto, 2,
3732 case offsetof(struct __sk_buff, priority):
3733 if (type == BPF_WRITE)
3734 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3735 bpf_target_off(struct sk_buff, priority, 4,
3738 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3739 bpf_target_off(struct sk_buff, priority, 4,
3743 case offsetof(struct __sk_buff, ingress_ifindex):
3744 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3745 bpf_target_off(struct sk_buff, skb_iif, 4,
3749 case offsetof(struct __sk_buff, ifindex):
3750 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
3751 si->dst_reg, si->src_reg,
3752 offsetof(struct sk_buff, dev));
3753 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
3754 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3755 bpf_target_off(struct net_device, ifindex, 4,
3759 case offsetof(struct __sk_buff, hash):
3760 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3761 bpf_target_off(struct sk_buff, hash, 4,
3765 case offsetof(struct __sk_buff, mark):
3766 if (type == BPF_WRITE)
3767 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3768 bpf_target_off(struct sk_buff, mark, 4,
3771 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3772 bpf_target_off(struct sk_buff, mark, 4,
3776 case offsetof(struct __sk_buff, pkt_type):
3778 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
3780 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
3781 #ifdef __BIG_ENDIAN_BITFIELD
3782 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
3786 case offsetof(struct __sk_buff, queue_mapping):
3787 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3788 bpf_target_off(struct sk_buff, queue_mapping, 2,
3792 case offsetof(struct __sk_buff, vlan_present):
3793 case offsetof(struct __sk_buff, vlan_tci):
3794 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
3796 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3797 bpf_target_off(struct sk_buff, vlan_tci, 2,
3799 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
3800 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
3803 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
3804 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
3808 case offsetof(struct __sk_buff, cb[0]) ...
3809 offsetofend(struct __sk_buff, cb[4]) - 1:
3810 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
3811 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
3812 offsetof(struct qdisc_skb_cb, data)) %
3815 prog->cb_access = 1;
3817 off -= offsetof(struct __sk_buff, cb[0]);
3818 off += offsetof(struct sk_buff, cb);
3819 off += offsetof(struct qdisc_skb_cb, data);
3820 if (type == BPF_WRITE)
3821 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
3824 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
3828 case offsetof(struct __sk_buff, tc_classid):
3829 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
3832 off -= offsetof(struct __sk_buff, tc_classid);
3833 off += offsetof(struct sk_buff, cb);
3834 off += offsetof(struct qdisc_skb_cb, tc_classid);
3836 if (type == BPF_WRITE)
3837 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
3840 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
3844 case offsetof(struct __sk_buff, data):
3845 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
3846 si->dst_reg, si->src_reg,
3847 offsetof(struct sk_buff, data));
3850 case offsetof(struct __sk_buff, data_end):
3852 off -= offsetof(struct __sk_buff, data_end);
3853 off += offsetof(struct sk_buff, cb);
3854 off += offsetof(struct bpf_skb_data_end, data_end);
3855 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
3859 case offsetof(struct __sk_buff, tc_index):
3860 #ifdef CONFIG_NET_SCHED
3861 if (type == BPF_WRITE)
3862 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
3863 bpf_target_off(struct sk_buff, tc_index, 2,
3866 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3867 bpf_target_off(struct sk_buff, tc_index, 2,
3871 if (type == BPF_WRITE)
3872 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
3874 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3878 case offsetof(struct __sk_buff, napi_id):
3879 #if defined(CONFIG_NET_RX_BUSY_POLL)
3880 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3881 bpf_target_off(struct sk_buff, napi_id, 4,
3883 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
3884 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3887 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3890 case offsetof(struct __sk_buff, family):
3891 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
3893 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3894 si->dst_reg, si->src_reg,
3895 offsetof(struct sk_buff, sk));
3896 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3897 bpf_target_off(struct sock_common,
3901 case offsetof(struct __sk_buff, remote_ip4):
3902 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
3904 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3905 si->dst_reg, si->src_reg,
3906 offsetof(struct sk_buff, sk));
3907 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3908 bpf_target_off(struct sock_common,
3912 case offsetof(struct __sk_buff, local_ip4):
3913 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3914 skc_rcv_saddr) != 4);
3916 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3917 si->dst_reg, si->src_reg,
3918 offsetof(struct sk_buff, sk));
3919 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3920 bpf_target_off(struct sock_common,
3924 case offsetof(struct __sk_buff, remote_ip6[0]) ...
3925 offsetof(struct __sk_buff, remote_ip6[3]):
3926 #if IS_ENABLED(CONFIG_IPV6)
3927 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3928 skc_v6_daddr.s6_addr32[0]) != 4);
3931 off -= offsetof(struct __sk_buff, remote_ip6[0]);
3933 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3934 si->dst_reg, si->src_reg,
3935 offsetof(struct sk_buff, sk));
3936 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3937 offsetof(struct sock_common,
3938 skc_v6_daddr.s6_addr32[0]) +
3941 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3944 case offsetof(struct __sk_buff, local_ip6[0]) ...
3945 offsetof(struct __sk_buff, local_ip6[3]):
3946 #if IS_ENABLED(CONFIG_IPV6)
3947 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3948 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
3951 off -= offsetof(struct __sk_buff, local_ip6[0]);
3953 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3954 si->dst_reg, si->src_reg,
3955 offsetof(struct sk_buff, sk));
3956 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3957 offsetof(struct sock_common,
3958 skc_v6_rcv_saddr.s6_addr32[0]) +
3961 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3965 case offsetof(struct __sk_buff, remote_port):
3966 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
3968 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3969 si->dst_reg, si->src_reg,
3970 offsetof(struct sk_buff, sk));
3971 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3972 bpf_target_off(struct sock_common,
3975 #ifndef __BIG_ENDIAN_BITFIELD
3976 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
3980 case offsetof(struct __sk_buff, local_port):
3981 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
3983 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3984 si->dst_reg, si->src_reg,
3985 offsetof(struct sk_buff, sk));
3986 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3987 bpf_target_off(struct sock_common,
3988 skc_num, 2, target_size));
3992 return insn - insn_buf;
3995 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
3996 const struct bpf_insn *si,
3997 struct bpf_insn *insn_buf,
3998 struct bpf_prog *prog, u32 *target_size)
4000 struct bpf_insn *insn = insn_buf;
4003 case offsetof(struct bpf_sock, bound_dev_if):
4004 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
4006 if (type == BPF_WRITE)
4007 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4008 offsetof(struct sock, sk_bound_dev_if));
4010 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4011 offsetof(struct sock, sk_bound_dev_if));
4014 case offsetof(struct bpf_sock, mark):
4015 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
4017 if (type == BPF_WRITE)
4018 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4019 offsetof(struct sock, sk_mark));
4021 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4022 offsetof(struct sock, sk_mark));
4025 case offsetof(struct bpf_sock, priority):
4026 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
4028 if (type == BPF_WRITE)
4029 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4030 offsetof(struct sock, sk_priority));
4032 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4033 offsetof(struct sock, sk_priority));
4036 case offsetof(struct bpf_sock, family):
4037 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
4039 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4040 offsetof(struct sock, sk_family));
4043 case offsetof(struct bpf_sock, type):
4044 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4045 offsetof(struct sock, __sk_flags_offset));
4046 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
4047 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
4050 case offsetof(struct bpf_sock, protocol):
4051 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4052 offsetof(struct sock, __sk_flags_offset));
4053 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
4054 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
4058 return insn - insn_buf;
4061 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
4062 const struct bpf_insn *si,
4063 struct bpf_insn *insn_buf,
4064 struct bpf_prog *prog, u32 *target_size)
4066 struct bpf_insn *insn = insn_buf;
4069 case offsetof(struct __sk_buff, ifindex):
4070 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
4071 si->dst_reg, si->src_reg,
4072 offsetof(struct sk_buff, dev));
4073 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4074 bpf_target_off(struct net_device, ifindex, 4,
4078 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4082 return insn - insn_buf;
4085 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
4086 const struct bpf_insn *si,
4087 struct bpf_insn *insn_buf,
4088 struct bpf_prog *prog, u32 *target_size)
4090 struct bpf_insn *insn = insn_buf;
4093 case offsetof(struct xdp_md, data):
4094 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
4095 si->dst_reg, si->src_reg,
4096 offsetof(struct xdp_buff, data));
4098 case offsetof(struct xdp_md, data_end):
4099 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
4100 si->dst_reg, si->src_reg,
4101 offsetof(struct xdp_buff, data_end));
4105 return insn - insn_buf;
4108 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
4109 const struct bpf_insn *si,
4110 struct bpf_insn *insn_buf,
4111 struct bpf_prog *prog,
4114 struct bpf_insn *insn = insn_buf;
4118 case offsetof(struct bpf_sock_ops, op) ...
4119 offsetof(struct bpf_sock_ops, replylong[3]):
4120 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
4121 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
4122 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
4123 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
4124 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
4125 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
4127 off -= offsetof(struct bpf_sock_ops, op);
4128 off += offsetof(struct bpf_sock_ops_kern, op);
4129 if (type == BPF_WRITE)
4130 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4133 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4137 case offsetof(struct bpf_sock_ops, family):
4138 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
4140 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4141 struct bpf_sock_ops_kern, sk),
4142 si->dst_reg, si->src_reg,
4143 offsetof(struct bpf_sock_ops_kern, sk));
4144 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4145 offsetof(struct sock_common, skc_family));
4148 case offsetof(struct bpf_sock_ops, remote_ip4):
4149 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
4151 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4152 struct bpf_sock_ops_kern, sk),
4153 si->dst_reg, si->src_reg,
4154 offsetof(struct bpf_sock_ops_kern, sk));
4155 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4156 offsetof(struct sock_common, skc_daddr));
4159 case offsetof(struct bpf_sock_ops, local_ip4):
4160 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
4162 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4163 struct bpf_sock_ops_kern, sk),
4164 si->dst_reg, si->src_reg,
4165 offsetof(struct bpf_sock_ops_kern, sk));
4166 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4167 offsetof(struct sock_common,
4171 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
4172 offsetof(struct bpf_sock_ops, remote_ip6[3]):
4173 #if IS_ENABLED(CONFIG_IPV6)
4174 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4175 skc_v6_daddr.s6_addr32[0]) != 4);
4178 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
4179 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4180 struct bpf_sock_ops_kern, sk),
4181 si->dst_reg, si->src_reg,
4182 offsetof(struct bpf_sock_ops_kern, sk));
4183 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4184 offsetof(struct sock_common,
4185 skc_v6_daddr.s6_addr32[0]) +
4188 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4192 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
4193 offsetof(struct bpf_sock_ops, local_ip6[3]):
4194 #if IS_ENABLED(CONFIG_IPV6)
4195 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4196 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
4199 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
4200 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4201 struct bpf_sock_ops_kern, sk),
4202 si->dst_reg, si->src_reg,
4203 offsetof(struct bpf_sock_ops_kern, sk));
4204 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4205 offsetof(struct sock_common,
4206 skc_v6_rcv_saddr.s6_addr32[0]) +
4209 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4213 case offsetof(struct bpf_sock_ops, remote_port):
4214 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
4216 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4217 struct bpf_sock_ops_kern, sk),
4218 si->dst_reg, si->src_reg,
4219 offsetof(struct bpf_sock_ops_kern, sk));
4220 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4221 offsetof(struct sock_common, skc_dport));
4222 #ifndef __BIG_ENDIAN_BITFIELD
4223 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
4227 case offsetof(struct bpf_sock_ops, local_port):
4228 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
4230 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4231 struct bpf_sock_ops_kern, sk),
4232 si->dst_reg, si->src_reg,
4233 offsetof(struct bpf_sock_ops_kern, sk));
4234 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4235 offsetof(struct sock_common, skc_num));
4238 return insn - insn_buf;
4241 const struct bpf_verifier_ops sk_filter_prog_ops = {
4242 .get_func_proto = sk_filter_func_proto,
4243 .is_valid_access = sk_filter_is_valid_access,
4244 .convert_ctx_access = bpf_convert_ctx_access,
4247 const struct bpf_verifier_ops tc_cls_act_prog_ops = {
4248 .get_func_proto = tc_cls_act_func_proto,
4249 .is_valid_access = tc_cls_act_is_valid_access,
4250 .convert_ctx_access = tc_cls_act_convert_ctx_access,
4251 .gen_prologue = tc_cls_act_prologue,
4252 .test_run = bpf_prog_test_run_skb,
4255 const struct bpf_verifier_ops xdp_prog_ops = {
4256 .get_func_proto = xdp_func_proto,
4257 .is_valid_access = xdp_is_valid_access,
4258 .convert_ctx_access = xdp_convert_ctx_access,
4259 .test_run = bpf_prog_test_run_xdp,
4262 const struct bpf_verifier_ops cg_skb_prog_ops = {
4263 .get_func_proto = sk_filter_func_proto,
4264 .is_valid_access = sk_filter_is_valid_access,
4265 .convert_ctx_access = bpf_convert_ctx_access,
4266 .test_run = bpf_prog_test_run_skb,
4269 const struct bpf_verifier_ops lwt_inout_prog_ops = {
4270 .get_func_proto = lwt_inout_func_proto,
4271 .is_valid_access = lwt_is_valid_access,
4272 .convert_ctx_access = bpf_convert_ctx_access,
4273 .test_run = bpf_prog_test_run_skb,
4276 const struct bpf_verifier_ops lwt_xmit_prog_ops = {
4277 .get_func_proto = lwt_xmit_func_proto,
4278 .is_valid_access = lwt_is_valid_access,
4279 .convert_ctx_access = bpf_convert_ctx_access,
4280 .gen_prologue = tc_cls_act_prologue,
4281 .test_run = bpf_prog_test_run_skb,
4284 const struct bpf_verifier_ops cg_sock_prog_ops = {
4285 .get_func_proto = sock_filter_func_proto,
4286 .is_valid_access = sock_filter_is_valid_access,
4287 .convert_ctx_access = sock_filter_convert_ctx_access,
4290 const struct bpf_verifier_ops sock_ops_prog_ops = {
4291 .get_func_proto = sock_ops_func_proto,
4292 .is_valid_access = sock_ops_is_valid_access,
4293 .convert_ctx_access = sock_ops_convert_ctx_access,
4296 const struct bpf_verifier_ops sk_skb_prog_ops = {
4297 .get_func_proto = sk_skb_func_proto,
4298 .is_valid_access = sk_skb_is_valid_access,
4299 .convert_ctx_access = bpf_convert_ctx_access,
4300 .gen_prologue = sk_skb_prologue,
4303 int sk_detach_filter(struct sock *sk)
4306 struct sk_filter *filter;
4308 if (sock_flag(sk, SOCK_FILTER_LOCKED))
4311 filter = rcu_dereference_protected(sk->sk_filter,
4312 lockdep_sock_is_held(sk));
4314 RCU_INIT_POINTER(sk->sk_filter, NULL);
4315 sk_filter_uncharge(sk, filter);
4321 EXPORT_SYMBOL_GPL(sk_detach_filter);
4323 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
4326 struct sock_fprog_kern *fprog;
4327 struct sk_filter *filter;
4331 filter = rcu_dereference_protected(sk->sk_filter,
4332 lockdep_sock_is_held(sk));
4336 /* We're copying the filter that has been originally attached,
4337 * so no conversion/decode needed anymore. eBPF programs that
4338 * have no original program cannot be dumped through this.
4341 fprog = filter->prog->orig_prog;
4347 /* User space only enquires number of filter blocks. */
4351 if (len < fprog->len)
4355 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
4358 /* Instead of bytes, the API requests to return the number