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 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
461 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
462 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
463 /* Error with exception code on div/mod by 0.
464 * For cBPF programs, this was always return 0.
466 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
467 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
468 *insn++ = BPF_EXIT_INSN();
471 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
474 /* Jump transformation cannot use BPF block macros
475 * everywhere as offset calculation and target updates
476 * require a bit more work than the rest, i.e. jump
477 * opcodes map as-is, but offsets need adjustment.
480 #define BPF_EMIT_JMP \
482 const s32 off_min = S16_MIN, off_max = S16_MAX; \
485 if (target >= len || target < 0) \
487 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
488 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
489 off -= insn - tmp_insns; \
490 /* Reject anything not fitting into insn->off. */ \
491 if (off < off_min || off > off_max) \
496 case BPF_JMP | BPF_JA:
497 target = i + fp->k + 1;
498 insn->code = fp->code;
502 case BPF_JMP | BPF_JEQ | BPF_K:
503 case BPF_JMP | BPF_JEQ | BPF_X:
504 case BPF_JMP | BPF_JSET | BPF_K:
505 case BPF_JMP | BPF_JSET | BPF_X:
506 case BPF_JMP | BPF_JGT | BPF_K:
507 case BPF_JMP | BPF_JGT | BPF_X:
508 case BPF_JMP | BPF_JGE | BPF_K:
509 case BPF_JMP | BPF_JGE | BPF_X:
510 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
511 /* BPF immediates are signed, zero extend
512 * immediate into tmp register and use it
515 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
517 insn->dst_reg = BPF_REG_A;
518 insn->src_reg = BPF_REG_TMP;
521 insn->dst_reg = BPF_REG_A;
523 bpf_src = BPF_SRC(fp->code);
524 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
527 /* Common case where 'jump_false' is next insn. */
529 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
530 target = i + fp->jt + 1;
535 /* Convert some jumps when 'jump_true' is next insn. */
537 switch (BPF_OP(fp->code)) {
539 insn->code = BPF_JMP | BPF_JNE | bpf_src;
542 insn->code = BPF_JMP | BPF_JLE | bpf_src;
545 insn->code = BPF_JMP | BPF_JLT | bpf_src;
551 target = i + fp->jf + 1;
556 /* Other jumps are mapped into two insns: Jxx and JA. */
557 target = i + fp->jt + 1;
558 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
562 insn->code = BPF_JMP | BPF_JA;
563 target = i + fp->jf + 1;
567 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
568 case BPF_LDX | BPF_MSH | BPF_B:
570 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
571 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
572 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
574 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
576 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
578 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
580 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
583 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
584 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
586 case BPF_RET | BPF_A:
587 case BPF_RET | BPF_K:
588 if (BPF_RVAL(fp->code) == BPF_K)
589 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
591 *insn = BPF_EXIT_INSN();
594 /* Store to stack. */
597 stack_off = fp->k * 4 + 4;
598 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
599 BPF_ST ? BPF_REG_A : BPF_REG_X,
601 /* check_load_and_stores() verifies that classic BPF can
602 * load from stack only after write, so tracking
603 * stack_depth for ST|STX insns is enough
605 if (new_prog && new_prog->aux->stack_depth < stack_off)
606 new_prog->aux->stack_depth = stack_off;
609 /* Load from stack. */
610 case BPF_LD | BPF_MEM:
611 case BPF_LDX | BPF_MEM:
612 stack_off = fp->k * 4 + 4;
613 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
614 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
619 case BPF_LD | BPF_IMM:
620 case BPF_LDX | BPF_IMM:
621 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
622 BPF_REG_A : BPF_REG_X, fp->k);
626 case BPF_MISC | BPF_TAX:
627 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
631 case BPF_MISC | BPF_TXA:
632 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
635 /* A = skb->len or X = skb->len */
636 case BPF_LD | BPF_W | BPF_LEN:
637 case BPF_LDX | BPF_W | BPF_LEN:
638 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
639 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
640 offsetof(struct sk_buff, len));
643 /* Access seccomp_data fields. */
644 case BPF_LDX | BPF_ABS | BPF_W:
645 /* A = *(u32 *) (ctx + K) */
646 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
649 /* Unknown instruction. */
656 memcpy(new_insn, tmp_insns,
657 sizeof(*insn) * (insn - tmp_insns));
658 new_insn += insn - tmp_insns;
662 /* Only calculating new length. */
663 *new_len = new_insn - first_insn;
668 if (new_flen != new_insn - first_insn) {
669 new_flen = new_insn - first_insn;
676 BUG_ON(*new_len != new_flen);
685 * As we dont want to clear mem[] array for each packet going through
686 * __bpf_prog_run(), we check that filter loaded by user never try to read
687 * a cell if not previously written, and we check all branches to be sure
688 * a malicious user doesn't try to abuse us.
690 static int check_load_and_stores(const struct sock_filter *filter, int flen)
692 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
695 BUILD_BUG_ON(BPF_MEMWORDS > 16);
697 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
701 memset(masks, 0xff, flen * sizeof(*masks));
703 for (pc = 0; pc < flen; pc++) {
704 memvalid &= masks[pc];
706 switch (filter[pc].code) {
709 memvalid |= (1 << filter[pc].k);
711 case BPF_LD | BPF_MEM:
712 case BPF_LDX | BPF_MEM:
713 if (!(memvalid & (1 << filter[pc].k))) {
718 case BPF_JMP | BPF_JA:
719 /* A jump must set masks on target */
720 masks[pc + 1 + filter[pc].k] &= memvalid;
723 case BPF_JMP | BPF_JEQ | BPF_K:
724 case BPF_JMP | BPF_JEQ | BPF_X:
725 case BPF_JMP | BPF_JGE | BPF_K:
726 case BPF_JMP | BPF_JGE | BPF_X:
727 case BPF_JMP | BPF_JGT | BPF_K:
728 case BPF_JMP | BPF_JGT | BPF_X:
729 case BPF_JMP | BPF_JSET | BPF_K:
730 case BPF_JMP | BPF_JSET | BPF_X:
731 /* A jump must set masks on targets */
732 masks[pc + 1 + filter[pc].jt] &= memvalid;
733 masks[pc + 1 + filter[pc].jf] &= memvalid;
743 static bool chk_code_allowed(u16 code_to_probe)
745 static const bool codes[] = {
746 /* 32 bit ALU operations */
747 [BPF_ALU | BPF_ADD | BPF_K] = true,
748 [BPF_ALU | BPF_ADD | BPF_X] = true,
749 [BPF_ALU | BPF_SUB | BPF_K] = true,
750 [BPF_ALU | BPF_SUB | BPF_X] = true,
751 [BPF_ALU | BPF_MUL | BPF_K] = true,
752 [BPF_ALU | BPF_MUL | BPF_X] = true,
753 [BPF_ALU | BPF_DIV | BPF_K] = true,
754 [BPF_ALU | BPF_DIV | BPF_X] = true,
755 [BPF_ALU | BPF_MOD | BPF_K] = true,
756 [BPF_ALU | BPF_MOD | BPF_X] = true,
757 [BPF_ALU | BPF_AND | BPF_K] = true,
758 [BPF_ALU | BPF_AND | BPF_X] = true,
759 [BPF_ALU | BPF_OR | BPF_K] = true,
760 [BPF_ALU | BPF_OR | BPF_X] = true,
761 [BPF_ALU | BPF_XOR | BPF_K] = true,
762 [BPF_ALU | BPF_XOR | BPF_X] = true,
763 [BPF_ALU | BPF_LSH | BPF_K] = true,
764 [BPF_ALU | BPF_LSH | BPF_X] = true,
765 [BPF_ALU | BPF_RSH | BPF_K] = true,
766 [BPF_ALU | BPF_RSH | BPF_X] = true,
767 [BPF_ALU | BPF_NEG] = true,
768 /* Load instructions */
769 [BPF_LD | BPF_W | BPF_ABS] = true,
770 [BPF_LD | BPF_H | BPF_ABS] = true,
771 [BPF_LD | BPF_B | BPF_ABS] = true,
772 [BPF_LD | BPF_W | BPF_LEN] = true,
773 [BPF_LD | BPF_W | BPF_IND] = true,
774 [BPF_LD | BPF_H | BPF_IND] = true,
775 [BPF_LD | BPF_B | BPF_IND] = true,
776 [BPF_LD | BPF_IMM] = true,
777 [BPF_LD | BPF_MEM] = true,
778 [BPF_LDX | BPF_W | BPF_LEN] = true,
779 [BPF_LDX | BPF_B | BPF_MSH] = true,
780 [BPF_LDX | BPF_IMM] = true,
781 [BPF_LDX | BPF_MEM] = true,
782 /* Store instructions */
785 /* Misc instructions */
786 [BPF_MISC | BPF_TAX] = true,
787 [BPF_MISC | BPF_TXA] = true,
788 /* Return instructions */
789 [BPF_RET | BPF_K] = true,
790 [BPF_RET | BPF_A] = true,
791 /* Jump instructions */
792 [BPF_JMP | BPF_JA] = true,
793 [BPF_JMP | BPF_JEQ | BPF_K] = true,
794 [BPF_JMP | BPF_JEQ | BPF_X] = true,
795 [BPF_JMP | BPF_JGE | BPF_K] = true,
796 [BPF_JMP | BPF_JGE | BPF_X] = true,
797 [BPF_JMP | BPF_JGT | BPF_K] = true,
798 [BPF_JMP | BPF_JGT | BPF_X] = true,
799 [BPF_JMP | BPF_JSET | BPF_K] = true,
800 [BPF_JMP | BPF_JSET | BPF_X] = true,
803 if (code_to_probe >= ARRAY_SIZE(codes))
806 return codes[code_to_probe];
809 static bool bpf_check_basics_ok(const struct sock_filter *filter,
814 if (flen == 0 || flen > BPF_MAXINSNS)
821 * bpf_check_classic - verify socket filter code
822 * @filter: filter to verify
823 * @flen: length of filter
825 * Check the user's filter code. If we let some ugly
826 * filter code slip through kaboom! The filter must contain
827 * no references or jumps that are out of range, no illegal
828 * instructions, and must end with a RET instruction.
830 * All jumps are forward as they are not signed.
832 * Returns 0 if the rule set is legal or -EINVAL if not.
834 static int bpf_check_classic(const struct sock_filter *filter,
840 /* Check the filter code now */
841 for (pc = 0; pc < flen; pc++) {
842 const struct sock_filter *ftest = &filter[pc];
844 /* May we actually operate on this code? */
845 if (!chk_code_allowed(ftest->code))
848 /* Some instructions need special checks */
849 switch (ftest->code) {
850 case BPF_ALU | BPF_DIV | BPF_K:
851 case BPF_ALU | BPF_MOD | BPF_K:
852 /* Check for division by zero */
856 case BPF_ALU | BPF_LSH | BPF_K:
857 case BPF_ALU | BPF_RSH | BPF_K:
861 case BPF_LD | BPF_MEM:
862 case BPF_LDX | BPF_MEM:
865 /* Check for invalid memory addresses */
866 if (ftest->k >= BPF_MEMWORDS)
869 case BPF_JMP | BPF_JA:
870 /* Note, the large ftest->k might cause loops.
871 * Compare this with conditional jumps below,
872 * where offsets are limited. --ANK (981016)
874 if (ftest->k >= (unsigned int)(flen - pc - 1))
877 case BPF_JMP | BPF_JEQ | BPF_K:
878 case BPF_JMP | BPF_JEQ | BPF_X:
879 case BPF_JMP | BPF_JGE | BPF_K:
880 case BPF_JMP | BPF_JGE | BPF_X:
881 case BPF_JMP | BPF_JGT | BPF_K:
882 case BPF_JMP | BPF_JGT | BPF_X:
883 case BPF_JMP | BPF_JSET | BPF_K:
884 case BPF_JMP | BPF_JSET | BPF_X:
885 /* Both conditionals must be safe */
886 if (pc + ftest->jt + 1 >= flen ||
887 pc + ftest->jf + 1 >= flen)
890 case BPF_LD | BPF_W | BPF_ABS:
891 case BPF_LD | BPF_H | BPF_ABS:
892 case BPF_LD | BPF_B | BPF_ABS:
894 if (bpf_anc_helper(ftest) & BPF_ANC)
896 /* Ancillary operation unknown or unsupported */
897 if (anc_found == false && ftest->k >= SKF_AD_OFF)
902 /* Last instruction must be a RET code */
903 switch (filter[flen - 1].code) {
904 case BPF_RET | BPF_K:
905 case BPF_RET | BPF_A:
906 return check_load_and_stores(filter, flen);
912 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
913 const struct sock_fprog *fprog)
915 unsigned int fsize = bpf_classic_proglen(fprog);
916 struct sock_fprog_kern *fkprog;
918 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
922 fkprog = fp->orig_prog;
923 fkprog->len = fprog->len;
925 fkprog->filter = kmemdup(fp->insns, fsize,
926 GFP_KERNEL | __GFP_NOWARN);
927 if (!fkprog->filter) {
928 kfree(fp->orig_prog);
935 static void bpf_release_orig_filter(struct bpf_prog *fp)
937 struct sock_fprog_kern *fprog = fp->orig_prog;
940 kfree(fprog->filter);
945 static void __bpf_prog_release(struct bpf_prog *prog)
947 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
950 bpf_release_orig_filter(prog);
955 static void __sk_filter_release(struct sk_filter *fp)
957 __bpf_prog_release(fp->prog);
962 * sk_filter_release_rcu - Release a socket filter by rcu_head
963 * @rcu: rcu_head that contains the sk_filter to free
965 static void sk_filter_release_rcu(struct rcu_head *rcu)
967 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
969 __sk_filter_release(fp);
973 * sk_filter_release - release a socket filter
974 * @fp: filter to remove
976 * Remove a filter from a socket and release its resources.
978 static void sk_filter_release(struct sk_filter *fp)
980 if (refcount_dec_and_test(&fp->refcnt))
981 call_rcu(&fp->rcu, sk_filter_release_rcu);
984 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
986 u32 filter_size = bpf_prog_size(fp->prog->len);
988 atomic_sub(filter_size, &sk->sk_omem_alloc);
989 sk_filter_release(fp);
992 /* try to charge the socket memory if there is space available
993 * return true on success
995 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
997 u32 filter_size = bpf_prog_size(fp->prog->len);
999 /* same check as in sock_kmalloc() */
1000 if (filter_size <= sysctl_optmem_max &&
1001 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1002 atomic_add(filter_size, &sk->sk_omem_alloc);
1008 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1010 if (!refcount_inc_not_zero(&fp->refcnt))
1013 if (!__sk_filter_charge(sk, fp)) {
1014 sk_filter_release(fp);
1020 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1022 struct sock_filter *old_prog;
1023 struct bpf_prog *old_fp;
1024 int err, new_len, old_len = fp->len;
1026 /* We are free to overwrite insns et al right here as it
1027 * won't be used at this point in time anymore internally
1028 * after the migration to the internal BPF instruction
1031 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1032 sizeof(struct bpf_insn));
1034 /* Conversion cannot happen on overlapping memory areas,
1035 * so we need to keep the user BPF around until the 2nd
1036 * pass. At this time, the user BPF is stored in fp->insns.
1038 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1039 GFP_KERNEL | __GFP_NOWARN);
1045 /* 1st pass: calculate the new program length. */
1046 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
1050 /* Expand fp for appending the new filter representation. */
1052 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1054 /* The old_fp is still around in case we couldn't
1055 * allocate new memory, so uncharge on that one.
1064 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1065 err = bpf_convert_filter(old_prog, old_len, fp, &new_len);
1067 /* 2nd bpf_convert_filter() can fail only if it fails
1068 * to allocate memory, remapping must succeed. Note,
1069 * that at this time old_fp has already been released
1074 fp = bpf_prog_select_runtime(fp, &err);
1084 __bpf_prog_release(fp);
1085 return ERR_PTR(err);
1088 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1089 bpf_aux_classic_check_t trans)
1093 fp->bpf_func = NULL;
1096 err = bpf_check_classic(fp->insns, fp->len);
1098 __bpf_prog_release(fp);
1099 return ERR_PTR(err);
1102 /* There might be additional checks and transformations
1103 * needed on classic filters, f.e. in case of seccomp.
1106 err = trans(fp->insns, fp->len);
1108 __bpf_prog_release(fp);
1109 return ERR_PTR(err);
1113 /* Probe if we can JIT compile the filter and if so, do
1114 * the compilation of the filter.
1116 bpf_jit_compile(fp);
1118 /* JIT compiler couldn't process this filter, so do the
1119 * internal BPF translation for the optimized interpreter.
1122 fp = bpf_migrate_filter(fp);
1128 * bpf_prog_create - create an unattached filter
1129 * @pfp: the unattached filter that is created
1130 * @fprog: the filter program
1132 * Create a filter independent of any socket. We first run some
1133 * sanity checks on it to make sure it does not explode on us later.
1134 * If an error occurs or there is insufficient memory for the filter
1135 * a negative errno code is returned. On success the return is zero.
1137 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1139 unsigned int fsize = bpf_classic_proglen(fprog);
1140 struct bpf_prog *fp;
1142 /* Make sure new filter is there and in the right amounts. */
1143 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1146 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1150 memcpy(fp->insns, fprog->filter, fsize);
1152 fp->len = fprog->len;
1153 /* Since unattached filters are not copied back to user
1154 * space through sk_get_filter(), we do not need to hold
1155 * a copy here, and can spare us the work.
1157 fp->orig_prog = NULL;
1159 /* bpf_prepare_filter() already takes care of freeing
1160 * memory in case something goes wrong.
1162 fp = bpf_prepare_filter(fp, NULL);
1169 EXPORT_SYMBOL_GPL(bpf_prog_create);
1172 * bpf_prog_create_from_user - create an unattached filter from user buffer
1173 * @pfp: the unattached filter that is created
1174 * @fprog: the filter program
1175 * @trans: post-classic verifier transformation handler
1176 * @save_orig: save classic BPF program
1178 * This function effectively does the same as bpf_prog_create(), only
1179 * that it builds up its insns buffer from user space provided buffer.
1180 * It also allows for passing a bpf_aux_classic_check_t handler.
1182 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1183 bpf_aux_classic_check_t trans, bool save_orig)
1185 unsigned int fsize = bpf_classic_proglen(fprog);
1186 struct bpf_prog *fp;
1189 /* Make sure new filter is there and in the right amounts. */
1190 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1193 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1197 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1198 __bpf_prog_free(fp);
1202 fp->len = fprog->len;
1203 fp->orig_prog = NULL;
1206 err = bpf_prog_store_orig_filter(fp, fprog);
1208 __bpf_prog_free(fp);
1213 /* bpf_prepare_filter() already takes care of freeing
1214 * memory in case something goes wrong.
1216 fp = bpf_prepare_filter(fp, trans);
1223 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1225 void bpf_prog_destroy(struct bpf_prog *fp)
1227 __bpf_prog_release(fp);
1229 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1231 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1233 struct sk_filter *fp, *old_fp;
1235 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1241 if (!__sk_filter_charge(sk, fp)) {
1245 refcount_set(&fp->refcnt, 1);
1247 old_fp = rcu_dereference_protected(sk->sk_filter,
1248 lockdep_sock_is_held(sk));
1249 rcu_assign_pointer(sk->sk_filter, fp);
1252 sk_filter_uncharge(sk, old_fp);
1257 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1259 struct bpf_prog *old_prog;
1262 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1265 if (sk_unhashed(sk) && sk->sk_reuseport) {
1266 err = reuseport_alloc(sk);
1269 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1270 /* The socket wasn't bound with SO_REUSEPORT */
1274 old_prog = reuseport_attach_prog(sk, prog);
1276 bpf_prog_destroy(old_prog);
1282 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1284 unsigned int fsize = bpf_classic_proglen(fprog);
1285 struct bpf_prog *prog;
1288 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1289 return ERR_PTR(-EPERM);
1291 /* Make sure new filter is there and in the right amounts. */
1292 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1293 return ERR_PTR(-EINVAL);
1295 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1297 return ERR_PTR(-ENOMEM);
1299 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1300 __bpf_prog_free(prog);
1301 return ERR_PTR(-EFAULT);
1304 prog->len = fprog->len;
1306 err = bpf_prog_store_orig_filter(prog, fprog);
1308 __bpf_prog_free(prog);
1309 return ERR_PTR(-ENOMEM);
1312 /* bpf_prepare_filter() already takes care of freeing
1313 * memory in case something goes wrong.
1315 return bpf_prepare_filter(prog, NULL);
1319 * sk_attach_filter - attach a socket filter
1320 * @fprog: the filter program
1321 * @sk: the socket to use
1323 * Attach the user's filter code. We first run some sanity checks on
1324 * it to make sure it does not explode on us later. If an error
1325 * occurs or there is insufficient memory for the filter a negative
1326 * errno code is returned. On success the return is zero.
1328 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1330 struct bpf_prog *prog = __get_filter(fprog, sk);
1334 return PTR_ERR(prog);
1336 err = __sk_attach_prog(prog, sk);
1338 __bpf_prog_release(prog);
1344 EXPORT_SYMBOL_GPL(sk_attach_filter);
1346 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1348 struct bpf_prog *prog = __get_filter(fprog, sk);
1352 return PTR_ERR(prog);
1354 err = __reuseport_attach_prog(prog, sk);
1356 __bpf_prog_release(prog);
1363 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1365 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1366 return ERR_PTR(-EPERM);
1368 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1371 int sk_attach_bpf(u32 ufd, struct sock *sk)
1373 struct bpf_prog *prog = __get_bpf(ufd, sk);
1377 return PTR_ERR(prog);
1379 err = __sk_attach_prog(prog, sk);
1388 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1390 struct bpf_prog *prog = __get_bpf(ufd, sk);
1394 return PTR_ERR(prog);
1396 err = __reuseport_attach_prog(prog, sk);
1405 struct bpf_scratchpad {
1407 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1408 u8 buff[MAX_BPF_STACK];
1412 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1414 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1415 unsigned int write_len)
1417 return skb_ensure_writable(skb, write_len);
1420 static inline int bpf_try_make_writable(struct sk_buff *skb,
1421 unsigned int write_len)
1423 int err = __bpf_try_make_writable(skb, write_len);
1425 bpf_compute_data_end(skb);
1429 static int bpf_try_make_head_writable(struct sk_buff *skb)
1431 return bpf_try_make_writable(skb, skb_headlen(skb));
1434 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1436 if (skb_at_tc_ingress(skb))
1437 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1440 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1442 if (skb_at_tc_ingress(skb))
1443 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1446 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1447 const void *, from, u32, len, u64, flags)
1451 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1453 if (unlikely(offset > INT_MAX))
1455 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1458 ptr = skb->data + offset;
1459 if (flags & BPF_F_RECOMPUTE_CSUM)
1460 __skb_postpull_rcsum(skb, ptr, len, offset);
1462 memcpy(ptr, from, len);
1464 if (flags & BPF_F_RECOMPUTE_CSUM)
1465 __skb_postpush_rcsum(skb, ptr, len, offset);
1466 if (flags & BPF_F_INVALIDATE_HASH)
1467 skb_clear_hash(skb);
1472 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1473 .func = bpf_skb_store_bytes,
1475 .ret_type = RET_INTEGER,
1476 .arg1_type = ARG_PTR_TO_CTX,
1477 .arg2_type = ARG_ANYTHING,
1478 .arg3_type = ARG_PTR_TO_MEM,
1479 .arg4_type = ARG_CONST_SIZE,
1480 .arg5_type = ARG_ANYTHING,
1483 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1484 void *, to, u32, len)
1488 if (unlikely(offset > INT_MAX))
1491 ptr = skb_header_pointer(skb, offset, len, to);
1495 memcpy(to, ptr, len);
1503 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1504 .func = bpf_skb_load_bytes,
1506 .ret_type = RET_INTEGER,
1507 .arg1_type = ARG_PTR_TO_CTX,
1508 .arg2_type = ARG_ANYTHING,
1509 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1510 .arg4_type = ARG_CONST_SIZE,
1513 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1515 /* Idea is the following: should the needed direct read/write
1516 * test fail during runtime, we can pull in more data and redo
1517 * again, since implicitly, we invalidate previous checks here.
1519 * Or, since we know how much we need to make read/writeable,
1520 * this can be done once at the program beginning for direct
1521 * access case. By this we overcome limitations of only current
1522 * headroom being accessible.
1524 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1527 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1528 .func = bpf_skb_pull_data,
1530 .ret_type = RET_INTEGER,
1531 .arg1_type = ARG_PTR_TO_CTX,
1532 .arg2_type = ARG_ANYTHING,
1535 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1536 u64, from, u64, to, u64, flags)
1540 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1542 if (unlikely(offset > 0xffff || offset & 1))
1544 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1547 ptr = (__sum16 *)(skb->data + offset);
1548 switch (flags & BPF_F_HDR_FIELD_MASK) {
1550 if (unlikely(from != 0))
1553 csum_replace_by_diff(ptr, to);
1556 csum_replace2(ptr, from, to);
1559 csum_replace4(ptr, from, to);
1568 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1569 .func = bpf_l3_csum_replace,
1571 .ret_type = RET_INTEGER,
1572 .arg1_type = ARG_PTR_TO_CTX,
1573 .arg2_type = ARG_ANYTHING,
1574 .arg3_type = ARG_ANYTHING,
1575 .arg4_type = ARG_ANYTHING,
1576 .arg5_type = ARG_ANYTHING,
1579 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1580 u64, from, u64, to, u64, flags)
1582 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1583 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1584 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1587 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1588 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1590 if (unlikely(offset > 0xffff || offset & 1))
1592 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1595 ptr = (__sum16 *)(skb->data + offset);
1596 if (is_mmzero && !do_mforce && !*ptr)
1599 switch (flags & BPF_F_HDR_FIELD_MASK) {
1601 if (unlikely(from != 0))
1604 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1607 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1610 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1616 if (is_mmzero && !*ptr)
1617 *ptr = CSUM_MANGLED_0;
1621 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1622 .func = bpf_l4_csum_replace,
1624 .ret_type = RET_INTEGER,
1625 .arg1_type = ARG_PTR_TO_CTX,
1626 .arg2_type = ARG_ANYTHING,
1627 .arg3_type = ARG_ANYTHING,
1628 .arg4_type = ARG_ANYTHING,
1629 .arg5_type = ARG_ANYTHING,
1632 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1633 __be32 *, to, u32, to_size, __wsum, seed)
1635 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1636 u32 diff_size = from_size + to_size;
1639 /* This is quite flexible, some examples:
1641 * from_size == 0, to_size > 0, seed := csum --> pushing data
1642 * from_size > 0, to_size == 0, seed := csum --> pulling data
1643 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1645 * Even for diffing, from_size and to_size don't need to be equal.
1647 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1648 diff_size > sizeof(sp->diff)))
1651 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1652 sp->diff[j] = ~from[i];
1653 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1654 sp->diff[j] = to[i];
1656 return csum_partial(sp->diff, diff_size, seed);
1659 static const struct bpf_func_proto bpf_csum_diff_proto = {
1660 .func = bpf_csum_diff,
1663 .ret_type = RET_INTEGER,
1664 .arg1_type = ARG_PTR_TO_MEM,
1665 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1666 .arg3_type = ARG_PTR_TO_MEM,
1667 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1668 .arg5_type = ARG_ANYTHING,
1671 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1673 /* The interface is to be used in combination with bpf_csum_diff()
1674 * for direct packet writes. csum rotation for alignment as well
1675 * as emulating csum_sub() can be done from the eBPF program.
1677 if (skb->ip_summed == CHECKSUM_COMPLETE)
1678 return (skb->csum = csum_add(skb->csum, csum));
1683 static const struct bpf_func_proto bpf_csum_update_proto = {
1684 .func = bpf_csum_update,
1686 .ret_type = RET_INTEGER,
1687 .arg1_type = ARG_PTR_TO_CTX,
1688 .arg2_type = ARG_ANYTHING,
1691 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1693 return dev_forward_skb(dev, skb);
1696 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1697 struct sk_buff *skb)
1699 int ret = ____dev_forward_skb(dev, skb);
1703 ret = netif_rx(skb);
1709 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1713 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1714 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1721 __this_cpu_inc(xmit_recursion);
1722 ret = dev_queue_xmit(skb);
1723 __this_cpu_dec(xmit_recursion);
1728 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1731 unsigned int mlen = skb_network_offset(skb);
1734 __skb_pull(skb, mlen);
1735 if (unlikely(!skb->len)) {
1740 /* At ingress, the mac header has already been pulled once.
1741 * At egress, skb_pospull_rcsum has to be done in case that
1742 * the skb is originated from ingress (i.e. a forwarded skb)
1743 * to ensure that rcsum starts at net header.
1745 if (!skb_at_tc_ingress(skb))
1746 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1748 skb_pop_mac_header(skb);
1749 skb_reset_mac_len(skb);
1750 return flags & BPF_F_INGRESS ?
1751 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1754 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1757 /* Verify that a link layer header is carried */
1758 if (unlikely(skb->mac_header >= skb->network_header)) {
1763 bpf_push_mac_rcsum(skb);
1764 return flags & BPF_F_INGRESS ?
1765 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1768 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1771 if (dev_is_mac_header_xmit(dev))
1772 return __bpf_redirect_common(skb, dev, flags);
1774 return __bpf_redirect_no_mac(skb, dev, flags);
1777 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1779 struct net_device *dev;
1780 struct sk_buff *clone;
1783 if (unlikely(flags & ~(BPF_F_INGRESS)))
1786 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1790 clone = skb_clone(skb, GFP_ATOMIC);
1791 if (unlikely(!clone))
1794 /* For direct write, we need to keep the invariant that the skbs
1795 * we're dealing with need to be uncloned. Should uncloning fail
1796 * here, we need to free the just generated clone to unclone once
1799 ret = bpf_try_make_head_writable(skb);
1800 if (unlikely(ret)) {
1805 return __bpf_redirect(clone, dev, flags);
1808 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1809 .func = bpf_clone_redirect,
1811 .ret_type = RET_INTEGER,
1812 .arg1_type = ARG_PTR_TO_CTX,
1813 .arg2_type = ARG_ANYTHING,
1814 .arg3_type = ARG_ANYTHING,
1817 struct redirect_info {
1820 struct bpf_map *map;
1821 struct bpf_map *map_to_flush;
1822 unsigned long map_owner;
1825 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1827 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1829 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1831 if (unlikely(flags & ~(BPF_F_INGRESS)))
1834 ri->ifindex = ifindex;
1837 return TC_ACT_REDIRECT;
1840 int skb_do_redirect(struct sk_buff *skb)
1842 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1843 struct net_device *dev;
1845 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1847 if (unlikely(!dev)) {
1852 return __bpf_redirect(skb, dev, ri->flags);
1855 static const struct bpf_func_proto bpf_redirect_proto = {
1856 .func = bpf_redirect,
1858 .ret_type = RET_INTEGER,
1859 .arg1_type = ARG_ANYTHING,
1860 .arg2_type = ARG_ANYTHING,
1863 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
1864 struct bpf_map *, map, u32, key, u64, flags)
1866 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1868 /* If user passes invalid input drop the packet. */
1869 if (unlikely(flags))
1873 tcb->bpf.flags = flags;
1879 struct sock *do_sk_redirect_map(struct sk_buff *skb)
1881 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1882 struct sock *sk = NULL;
1885 sk = __sock_map_lookup_elem(tcb->bpf.map, tcb->bpf.key);
1888 tcb->bpf.map = NULL;
1894 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
1895 .func = bpf_sk_redirect_map,
1897 .ret_type = RET_INTEGER,
1898 .arg1_type = ARG_PTR_TO_CTX,
1899 .arg2_type = ARG_CONST_MAP_PTR,
1900 .arg3_type = ARG_ANYTHING,
1901 .arg4_type = ARG_ANYTHING,
1904 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
1906 return task_get_classid(skb);
1909 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1910 .func = bpf_get_cgroup_classid,
1912 .ret_type = RET_INTEGER,
1913 .arg1_type = ARG_PTR_TO_CTX,
1916 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
1918 return dst_tclassid(skb);
1921 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1922 .func = bpf_get_route_realm,
1924 .ret_type = RET_INTEGER,
1925 .arg1_type = ARG_PTR_TO_CTX,
1928 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
1930 /* If skb_clear_hash() was called due to mangling, we can
1931 * trigger SW recalculation here. Later access to hash
1932 * can then use the inline skb->hash via context directly
1933 * instead of calling this helper again.
1935 return skb_get_hash(skb);
1938 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1939 .func = bpf_get_hash_recalc,
1941 .ret_type = RET_INTEGER,
1942 .arg1_type = ARG_PTR_TO_CTX,
1945 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1947 /* After all direct packet write, this can be used once for
1948 * triggering a lazy recalc on next skb_get_hash() invocation.
1950 skb_clear_hash(skb);
1954 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1955 .func = bpf_set_hash_invalid,
1957 .ret_type = RET_INTEGER,
1958 .arg1_type = ARG_PTR_TO_CTX,
1961 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
1963 /* Set user specified hash as L4(+), so that it gets returned
1964 * on skb_get_hash() call unless BPF prog later on triggers a
1967 __skb_set_sw_hash(skb, hash, true);
1971 static const struct bpf_func_proto bpf_set_hash_proto = {
1972 .func = bpf_set_hash,
1974 .ret_type = RET_INTEGER,
1975 .arg1_type = ARG_PTR_TO_CTX,
1976 .arg2_type = ARG_ANYTHING,
1979 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1984 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1985 vlan_proto != htons(ETH_P_8021AD)))
1986 vlan_proto = htons(ETH_P_8021Q);
1988 bpf_push_mac_rcsum(skb);
1989 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1990 bpf_pull_mac_rcsum(skb);
1992 bpf_compute_data_end(skb);
1996 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1997 .func = bpf_skb_vlan_push,
1999 .ret_type = RET_INTEGER,
2000 .arg1_type = ARG_PTR_TO_CTX,
2001 .arg2_type = ARG_ANYTHING,
2002 .arg3_type = ARG_ANYTHING,
2004 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
2006 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2010 bpf_push_mac_rcsum(skb);
2011 ret = skb_vlan_pop(skb);
2012 bpf_pull_mac_rcsum(skb);
2014 bpf_compute_data_end(skb);
2018 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2019 .func = bpf_skb_vlan_pop,
2021 .ret_type = RET_INTEGER,
2022 .arg1_type = ARG_PTR_TO_CTX,
2024 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
2026 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2028 /* Caller already did skb_cow() with len as headroom,
2029 * so no need to do it here.
2032 memmove(skb->data, skb->data + len, off);
2033 memset(skb->data + off, 0, len);
2035 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2036 * needed here as it does not change the skb->csum
2037 * result for checksum complete when summing over
2043 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2047 /* skb_ensure_writable() is not needed here, as we're
2048 * already working on an uncloned skb.
2050 if (unlikely(!pskb_may_pull(skb, off + len)))
2053 old_data = skb->data;
2054 __skb_pull(skb, len);
2055 skb_postpull_rcsum(skb, old_data + off, len);
2056 memmove(skb->data, old_data, off);
2061 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2063 bool trans_same = skb->transport_header == skb->network_header;
2066 /* There's no need for __skb_push()/__skb_pull() pair to
2067 * get to the start of the mac header as we're guaranteed
2068 * to always start from here under eBPF.
2070 ret = bpf_skb_generic_push(skb, off, len);
2072 skb->mac_header -= len;
2073 skb->network_header -= len;
2075 skb->transport_header = skb->network_header;
2081 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2083 bool trans_same = skb->transport_header == skb->network_header;
2086 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2087 ret = bpf_skb_generic_pop(skb, off, len);
2089 skb->mac_header += len;
2090 skb->network_header += len;
2092 skb->transport_header = skb->network_header;
2098 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2100 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2101 u32 off = skb_mac_header_len(skb);
2104 ret = skb_cow(skb, len_diff);
2105 if (unlikely(ret < 0))
2108 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2109 if (unlikely(ret < 0))
2112 if (skb_is_gso(skb)) {
2113 /* SKB_GSO_TCPV4 needs to be changed into
2116 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2117 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
2118 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
2121 /* Due to IPv6 header, MSS needs to be downgraded. */
2122 skb_shinfo(skb)->gso_size -= len_diff;
2123 /* Header must be checked, and gso_segs recomputed. */
2124 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2125 skb_shinfo(skb)->gso_segs = 0;
2128 skb->protocol = htons(ETH_P_IPV6);
2129 skb_clear_hash(skb);
2134 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2136 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2137 u32 off = skb_mac_header_len(skb);
2140 ret = skb_unclone(skb, GFP_ATOMIC);
2141 if (unlikely(ret < 0))
2144 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2145 if (unlikely(ret < 0))
2148 if (skb_is_gso(skb)) {
2149 /* SKB_GSO_TCPV6 needs to be changed into
2152 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2153 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2154 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2157 /* Due to IPv4 header, MSS can be upgraded. */
2158 skb_shinfo(skb)->gso_size += len_diff;
2159 /* Header must be checked, and gso_segs recomputed. */
2160 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2161 skb_shinfo(skb)->gso_segs = 0;
2164 skb->protocol = htons(ETH_P_IP);
2165 skb_clear_hash(skb);
2170 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2172 __be16 from_proto = skb->protocol;
2174 if (from_proto == htons(ETH_P_IP) &&
2175 to_proto == htons(ETH_P_IPV6))
2176 return bpf_skb_proto_4_to_6(skb);
2178 if (from_proto == htons(ETH_P_IPV6) &&
2179 to_proto == htons(ETH_P_IP))
2180 return bpf_skb_proto_6_to_4(skb);
2185 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2190 if (unlikely(flags))
2193 /* General idea is that this helper does the basic groundwork
2194 * needed for changing the protocol, and eBPF program fills the
2195 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2196 * and other helpers, rather than passing a raw buffer here.
2198 * The rationale is to keep this minimal and without a need to
2199 * deal with raw packet data. F.e. even if we would pass buffers
2200 * here, the program still needs to call the bpf_lX_csum_replace()
2201 * helpers anyway. Plus, this way we keep also separation of
2202 * concerns, since f.e. bpf_skb_store_bytes() should only take
2205 * Currently, additional options and extension header space are
2206 * not supported, but flags register is reserved so we can adapt
2207 * that. For offloads, we mark packet as dodgy, so that headers
2208 * need to be verified first.
2210 ret = bpf_skb_proto_xlat(skb, proto);
2211 bpf_compute_data_end(skb);
2215 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2216 .func = bpf_skb_change_proto,
2218 .ret_type = RET_INTEGER,
2219 .arg1_type = ARG_PTR_TO_CTX,
2220 .arg2_type = ARG_ANYTHING,
2221 .arg3_type = ARG_ANYTHING,
2224 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2226 /* We only allow a restricted subset to be changed for now. */
2227 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2228 !skb_pkt_type_ok(pkt_type)))
2231 skb->pkt_type = pkt_type;
2235 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2236 .func = bpf_skb_change_type,
2238 .ret_type = RET_INTEGER,
2239 .arg1_type = ARG_PTR_TO_CTX,
2240 .arg2_type = ARG_ANYTHING,
2243 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2245 switch (skb->protocol) {
2246 case htons(ETH_P_IP):
2247 return sizeof(struct iphdr);
2248 case htons(ETH_P_IPV6):
2249 return sizeof(struct ipv6hdr);
2255 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2257 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2260 ret = skb_cow(skb, len_diff);
2261 if (unlikely(ret < 0))
2264 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2265 if (unlikely(ret < 0))
2268 if (skb_is_gso(skb)) {
2269 /* Due to header grow, MSS needs to be downgraded. */
2270 skb_shinfo(skb)->gso_size -= len_diff;
2271 /* Header must be checked, and gso_segs recomputed. */
2272 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2273 skb_shinfo(skb)->gso_segs = 0;
2279 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2281 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2284 ret = skb_unclone(skb, GFP_ATOMIC);
2285 if (unlikely(ret < 0))
2288 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2289 if (unlikely(ret < 0))
2292 if (skb_is_gso(skb)) {
2293 /* Due to header shrink, MSS can be upgraded. */
2294 skb_shinfo(skb)->gso_size += len_diff;
2295 /* Header must be checked, and gso_segs recomputed. */
2296 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2297 skb_shinfo(skb)->gso_segs = 0;
2303 #define BPF_SKB_MAX_LEN SKB_MAX_ALLOC
2305 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2307 bool trans_same = skb->transport_header == skb->network_header;
2308 u32 len_cur, len_diff_abs = abs(len_diff);
2309 u32 len_min = bpf_skb_net_base_len(skb);
2310 u32 len_max = BPF_SKB_MAX_LEN;
2311 __be16 proto = skb->protocol;
2312 bool shrink = len_diff < 0;
2315 if (unlikely(len_diff_abs > 0xfffU))
2317 if (unlikely(proto != htons(ETH_P_IP) &&
2318 proto != htons(ETH_P_IPV6)))
2321 len_cur = skb->len - skb_network_offset(skb);
2322 if (skb_transport_header_was_set(skb) && !trans_same)
2323 len_cur = skb_network_header_len(skb);
2324 if ((shrink && (len_diff_abs >= len_cur ||
2325 len_cur - len_diff_abs < len_min)) ||
2326 (!shrink && (skb->len + len_diff_abs > len_max &&
2330 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2331 bpf_skb_net_grow(skb, len_diff_abs);
2333 bpf_compute_data_end(skb);
2337 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2338 u32, mode, u64, flags)
2340 if (unlikely(flags))
2342 if (likely(mode == BPF_ADJ_ROOM_NET))
2343 return bpf_skb_adjust_net(skb, len_diff);
2348 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2349 .func = bpf_skb_adjust_room,
2351 .ret_type = RET_INTEGER,
2352 .arg1_type = ARG_PTR_TO_CTX,
2353 .arg2_type = ARG_ANYTHING,
2354 .arg3_type = ARG_ANYTHING,
2355 .arg4_type = ARG_ANYTHING,
2358 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2360 u32 min_len = skb_network_offset(skb);
2362 if (skb_transport_header_was_set(skb))
2363 min_len = skb_transport_offset(skb);
2364 if (skb->ip_summed == CHECKSUM_PARTIAL)
2365 min_len = skb_checksum_start_offset(skb) +
2366 skb->csum_offset + sizeof(__sum16);
2370 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2372 unsigned int old_len = skb->len;
2375 ret = __skb_grow_rcsum(skb, new_len);
2377 memset(skb->data + old_len, 0, new_len - old_len);
2381 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2383 return __skb_trim_rcsum(skb, new_len);
2386 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2389 u32 max_len = BPF_SKB_MAX_LEN;
2390 u32 min_len = __bpf_skb_min_len(skb);
2393 if (unlikely(flags || new_len > max_len || new_len < min_len))
2395 if (skb->encapsulation)
2398 /* The basic idea of this helper is that it's performing the
2399 * needed work to either grow or trim an skb, and eBPF program
2400 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2401 * bpf_lX_csum_replace() and others rather than passing a raw
2402 * buffer here. This one is a slow path helper and intended
2403 * for replies with control messages.
2405 * Like in bpf_skb_change_proto(), we want to keep this rather
2406 * minimal and without protocol specifics so that we are able
2407 * to separate concerns as in bpf_skb_store_bytes() should only
2408 * be the one responsible for writing buffers.
2410 * It's really expected to be a slow path operation here for
2411 * control message replies, so we're implicitly linearizing,
2412 * uncloning and drop offloads from the skb by this.
2414 ret = __bpf_try_make_writable(skb, skb->len);
2416 if (new_len > skb->len)
2417 ret = bpf_skb_grow_rcsum(skb, new_len);
2418 else if (new_len < skb->len)
2419 ret = bpf_skb_trim_rcsum(skb, new_len);
2420 if (!ret && skb_is_gso(skb))
2424 bpf_compute_data_end(skb);
2428 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2429 .func = bpf_skb_change_tail,
2431 .ret_type = RET_INTEGER,
2432 .arg1_type = ARG_PTR_TO_CTX,
2433 .arg2_type = ARG_ANYTHING,
2434 .arg3_type = ARG_ANYTHING,
2437 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2440 u32 max_len = BPF_SKB_MAX_LEN;
2441 u32 new_len = skb->len + head_room;
2444 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2445 new_len < skb->len))
2448 ret = skb_cow(skb, head_room);
2450 /* Idea for this helper is that we currently only
2451 * allow to expand on mac header. This means that
2452 * skb->protocol network header, etc, stay as is.
2453 * Compared to bpf_skb_change_tail(), we're more
2454 * flexible due to not needing to linearize or
2455 * reset GSO. Intention for this helper is to be
2456 * used by an L3 skb that needs to push mac header
2457 * for redirection into L2 device.
2459 __skb_push(skb, head_room);
2460 memset(skb->data, 0, head_room);
2461 skb_reset_mac_header(skb);
2462 skb_reset_mac_len(skb);
2465 bpf_compute_data_end(skb);
2469 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2470 .func = bpf_skb_change_head,
2472 .ret_type = RET_INTEGER,
2473 .arg1_type = ARG_PTR_TO_CTX,
2474 .arg2_type = ARG_ANYTHING,
2475 .arg3_type = ARG_ANYTHING,
2478 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2480 void *data = xdp->data + offset;
2482 if (unlikely(data < xdp->data_hard_start ||
2483 data > xdp->data_end - ETH_HLEN))
2491 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2492 .func = bpf_xdp_adjust_head,
2494 .ret_type = RET_INTEGER,
2495 .arg1_type = ARG_PTR_TO_CTX,
2496 .arg2_type = ARG_ANYTHING,
2499 static int __bpf_tx_xdp(struct net_device *dev,
2500 struct bpf_map *map,
2501 struct xdp_buff *xdp,
2506 if (!dev->netdev_ops->ndo_xdp_xmit) {
2510 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
2514 __dev_map_insert_ctx(map, index);
2516 dev->netdev_ops->ndo_xdp_flush(dev);
2520 void xdp_do_flush_map(void)
2522 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2523 struct bpf_map *map = ri->map_to_flush;
2525 ri->map_to_flush = NULL;
2527 __dev_map_flush(map);
2529 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
2531 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
2534 return (unsigned long)xdp_prog->aux != aux;
2537 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
2538 struct bpf_prog *xdp_prog)
2540 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2541 unsigned long map_owner = ri->map_owner;
2542 struct bpf_map *map = ri->map;
2543 struct net_device *fwd = NULL;
2544 u32 index = ri->ifindex;
2551 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2557 fwd = __dev_map_lookup_elem(map, index);
2562 if (ri->map_to_flush && ri->map_to_flush != map)
2565 err = __bpf_tx_xdp(fwd, map, xdp, index);
2569 ri->map_to_flush = map;
2570 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
2573 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
2577 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
2578 struct bpf_prog *xdp_prog)
2580 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2581 struct net_device *fwd;
2582 u32 index = ri->ifindex;
2586 return xdp_do_redirect_map(dev, xdp, xdp_prog);
2588 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2590 if (unlikely(!fwd)) {
2595 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
2599 _trace_xdp_redirect(dev, xdp_prog, index);
2602 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2605 EXPORT_SYMBOL_GPL(xdp_do_redirect);
2607 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
2608 struct bpf_prog *xdp_prog)
2610 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2611 unsigned long map_owner = ri->map_owner;
2612 struct bpf_map *map = ri->map;
2613 struct net_device *fwd = NULL;
2614 u32 index = ri->ifindex;
2623 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2628 fwd = __dev_map_lookup_elem(map, index);
2630 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2632 if (unlikely(!fwd)) {
2637 if (unlikely(!(fwd->flags & IFF_UP))) {
2642 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
2643 if (skb->len > len) {
2649 map ? _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index)
2650 : _trace_xdp_redirect(dev, xdp_prog, index);
2653 map ? _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err)
2654 : _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2657 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
2659 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
2661 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2663 if (unlikely(flags))
2666 ri->ifindex = ifindex;
2671 return XDP_REDIRECT;
2674 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
2675 .func = bpf_xdp_redirect,
2677 .ret_type = RET_INTEGER,
2678 .arg1_type = ARG_ANYTHING,
2679 .arg2_type = ARG_ANYTHING,
2682 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
2683 unsigned long, map_owner)
2685 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2687 if (unlikely(flags))
2690 ri->ifindex = ifindex;
2693 ri->map_owner = map_owner;
2695 return XDP_REDIRECT;
2698 /* Note, arg4 is hidden from users and populated by the verifier
2699 * with the right pointer.
2701 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
2702 .func = bpf_xdp_redirect_map,
2704 .ret_type = RET_INTEGER,
2705 .arg1_type = ARG_CONST_MAP_PTR,
2706 .arg2_type = ARG_ANYTHING,
2707 .arg3_type = ARG_ANYTHING,
2710 bool bpf_helper_changes_pkt_data(void *func)
2712 if (func == bpf_skb_vlan_push ||
2713 func == bpf_skb_vlan_pop ||
2714 func == bpf_skb_store_bytes ||
2715 func == bpf_skb_change_proto ||
2716 func == bpf_skb_change_head ||
2717 func == bpf_skb_change_tail ||
2718 func == bpf_skb_adjust_room ||
2719 func == bpf_skb_pull_data ||
2720 func == bpf_clone_redirect ||
2721 func == bpf_l3_csum_replace ||
2722 func == bpf_l4_csum_replace ||
2723 func == bpf_xdp_adjust_head)
2729 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2730 unsigned long off, unsigned long len)
2732 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
2736 if (ptr != dst_buff)
2737 memcpy(dst_buff, ptr, len);
2742 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2743 u64, flags, void *, meta, u64, meta_size)
2745 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2747 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2749 if (unlikely(skb_size > skb->len))
2752 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2756 static const struct bpf_func_proto bpf_skb_event_output_proto = {
2757 .func = bpf_skb_event_output,
2759 .ret_type = RET_INTEGER,
2760 .arg1_type = ARG_PTR_TO_CTX,
2761 .arg2_type = ARG_CONST_MAP_PTR,
2762 .arg3_type = ARG_ANYTHING,
2763 .arg4_type = ARG_PTR_TO_MEM,
2764 .arg5_type = ARG_CONST_SIZE,
2767 static unsigned short bpf_tunnel_key_af(u64 flags)
2769 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2772 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2773 u32, size, u64, flags)
2775 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2776 u8 compat[sizeof(struct bpf_tunnel_key)];
2780 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2784 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2788 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2791 case offsetof(struct bpf_tunnel_key, tunnel_label):
2792 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2794 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2795 /* Fixup deprecated structure layouts here, so we have
2796 * a common path later on.
2798 if (ip_tunnel_info_af(info) != AF_INET)
2801 to = (struct bpf_tunnel_key *)compat;
2808 to->tunnel_id = be64_to_cpu(info->key.tun_id);
2809 to->tunnel_tos = info->key.tos;
2810 to->tunnel_ttl = info->key.ttl;
2812 if (flags & BPF_F_TUNINFO_IPV6) {
2813 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2814 sizeof(to->remote_ipv6));
2815 to->tunnel_label = be32_to_cpu(info->key.label);
2817 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
2820 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
2821 memcpy(to_orig, to, size);
2825 memset(to_orig, 0, size);
2829 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
2830 .func = bpf_skb_get_tunnel_key,
2832 .ret_type = RET_INTEGER,
2833 .arg1_type = ARG_PTR_TO_CTX,
2834 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2835 .arg3_type = ARG_CONST_SIZE,
2836 .arg4_type = ARG_ANYTHING,
2839 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
2841 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2844 if (unlikely(!info ||
2845 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2849 if (unlikely(size < info->options_len)) {
2854 ip_tunnel_info_opts_get(to, info);
2855 if (size > info->options_len)
2856 memset(to + info->options_len, 0, size - info->options_len);
2858 return info->options_len;
2860 memset(to, 0, size);
2864 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2865 .func = bpf_skb_get_tunnel_opt,
2867 .ret_type = RET_INTEGER,
2868 .arg1_type = ARG_PTR_TO_CTX,
2869 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2870 .arg3_type = ARG_CONST_SIZE,
2873 static struct metadata_dst __percpu *md_dst;
2875 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2876 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
2878 struct metadata_dst *md = this_cpu_ptr(md_dst);
2879 u8 compat[sizeof(struct bpf_tunnel_key)];
2880 struct ip_tunnel_info *info;
2882 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2883 BPF_F_DONT_FRAGMENT)))
2885 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2887 case offsetof(struct bpf_tunnel_key, tunnel_label):
2888 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2889 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2890 /* Fixup deprecated structure layouts here, so we have
2891 * a common path later on.
2893 memcpy(compat, from, size);
2894 memset(compat + size, 0, sizeof(compat) - size);
2895 from = (const struct bpf_tunnel_key *) compat;
2901 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2906 dst_hold((struct dst_entry *) md);
2907 skb_dst_set(skb, (struct dst_entry *) md);
2909 info = &md->u.tun_info;
2910 info->mode = IP_TUNNEL_INFO_TX;
2912 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
2913 if (flags & BPF_F_DONT_FRAGMENT)
2914 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2916 info->key.tun_id = cpu_to_be64(from->tunnel_id);
2917 info->key.tos = from->tunnel_tos;
2918 info->key.ttl = from->tunnel_ttl;
2920 if (flags & BPF_F_TUNINFO_IPV6) {
2921 info->mode |= IP_TUNNEL_INFO_IPV6;
2922 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2923 sizeof(from->remote_ipv6));
2924 info->key.label = cpu_to_be32(from->tunnel_label) &
2925 IPV6_FLOWLABEL_MASK;
2927 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2928 if (flags & BPF_F_ZERO_CSUM_TX)
2929 info->key.tun_flags &= ~TUNNEL_CSUM;
2935 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
2936 .func = bpf_skb_set_tunnel_key,
2938 .ret_type = RET_INTEGER,
2939 .arg1_type = ARG_PTR_TO_CTX,
2940 .arg2_type = ARG_PTR_TO_MEM,
2941 .arg3_type = ARG_CONST_SIZE,
2942 .arg4_type = ARG_ANYTHING,
2945 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2946 const u8 *, from, u32, size)
2948 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2949 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2951 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2953 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
2956 ip_tunnel_info_opts_set(info, from, size);
2961 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2962 .func = bpf_skb_set_tunnel_opt,
2964 .ret_type = RET_INTEGER,
2965 .arg1_type = ARG_PTR_TO_CTX,
2966 .arg2_type = ARG_PTR_TO_MEM,
2967 .arg3_type = ARG_CONST_SIZE,
2970 static const struct bpf_func_proto *
2971 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
2974 /* Race is not possible, since it's called from verifier
2975 * that is holding verifier mutex.
2977 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
2985 case BPF_FUNC_skb_set_tunnel_key:
2986 return &bpf_skb_set_tunnel_key_proto;
2987 case BPF_FUNC_skb_set_tunnel_opt:
2988 return &bpf_skb_set_tunnel_opt_proto;
2994 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2997 struct bpf_array *array = container_of(map, struct bpf_array, map);
2998 struct cgroup *cgrp;
3001 sk = skb_to_full_sk(skb);
3002 if (!sk || !sk_fullsock(sk))
3004 if (unlikely(idx >= array->map.max_entries))
3007 cgrp = READ_ONCE(array->ptrs[idx]);
3008 if (unlikely(!cgrp))
3011 return sk_under_cgroup_hierarchy(sk, cgrp);
3014 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3015 .func = bpf_skb_under_cgroup,
3017 .ret_type = RET_INTEGER,
3018 .arg1_type = ARG_PTR_TO_CTX,
3019 .arg2_type = ARG_CONST_MAP_PTR,
3020 .arg3_type = ARG_ANYTHING,
3023 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3024 unsigned long off, unsigned long len)
3026 memcpy(dst_buff, src_buff + off, len);
3030 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3031 u64, flags, void *, meta, u64, meta_size)
3033 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3035 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3037 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3040 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3041 xdp_size, bpf_xdp_copy);
3044 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3045 .func = bpf_xdp_event_output,
3047 .ret_type = RET_INTEGER,
3048 .arg1_type = ARG_PTR_TO_CTX,
3049 .arg2_type = ARG_CONST_MAP_PTR,
3050 .arg3_type = ARG_ANYTHING,
3051 .arg4_type = ARG_PTR_TO_MEM,
3052 .arg5_type = ARG_CONST_SIZE,
3055 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3057 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3060 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3061 .func = bpf_get_socket_cookie,
3063 .ret_type = RET_INTEGER,
3064 .arg1_type = ARG_PTR_TO_CTX,
3067 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3069 struct sock *sk = sk_to_full_sk(skb->sk);
3072 if (!sk || !sk_fullsock(sk))
3074 kuid = sock_net_uid(sock_net(sk), sk);
3075 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3078 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3079 .func = bpf_get_socket_uid,
3081 .ret_type = RET_INTEGER,
3082 .arg1_type = ARG_PTR_TO_CTX,
3085 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3086 int, level, int, optname, char *, optval, int, optlen)
3088 struct sock *sk = bpf_sock->sk;
3092 if (!sk_fullsock(sk))
3095 if (level == SOL_SOCKET) {
3096 if (optlen != sizeof(int))
3098 val = *((int *)optval);
3100 /* Only some socketops are supported */
3103 val = min_t(u32, val, sysctl_rmem_max);
3104 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3105 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3108 val = min_t(u32, val, sysctl_wmem_max);
3109 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3110 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3112 case SO_MAX_PACING_RATE:
3113 sk->sk_max_pacing_rate = val;
3114 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3115 sk->sk_max_pacing_rate);
3118 sk->sk_priority = val;
3123 sk->sk_rcvlowat = val ? : 1;
3126 if (sk->sk_mark != val) {
3135 } else if (level == SOL_TCP &&
3136 sk->sk_prot->setsockopt == tcp_setsockopt) {
3137 if (optname == TCP_CONGESTION) {
3138 char name[TCP_CA_NAME_MAX];
3139 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3141 strncpy(name, optval, min_t(long, optlen,
3142 TCP_CA_NAME_MAX-1));
3143 name[TCP_CA_NAME_MAX-1] = 0;
3144 ret = tcp_set_congestion_control(sk, name, false,
3147 struct tcp_sock *tp = tcp_sk(sk);
3149 if (optlen != sizeof(int))
3152 val = *((int *)optval);
3153 /* Only some options are supported */
3156 if (val <= 0 || tp->data_segs_out > tp->syn_data)
3161 case TCP_BPF_SNDCWND_CLAMP:
3165 tp->snd_cwnd_clamp = val;
3166 tp->snd_ssthresh = val;
3180 static const struct bpf_func_proto bpf_setsockopt_proto = {
3181 .func = bpf_setsockopt,
3183 .ret_type = RET_INTEGER,
3184 .arg1_type = ARG_PTR_TO_CTX,
3185 .arg2_type = ARG_ANYTHING,
3186 .arg3_type = ARG_ANYTHING,
3187 .arg4_type = ARG_PTR_TO_MEM,
3188 .arg5_type = ARG_CONST_SIZE,
3191 static const struct bpf_func_proto *
3192 bpf_base_func_proto(enum bpf_func_id func_id)
3195 case BPF_FUNC_map_lookup_elem:
3196 return &bpf_map_lookup_elem_proto;
3197 case BPF_FUNC_map_update_elem:
3198 return &bpf_map_update_elem_proto;
3199 case BPF_FUNC_map_delete_elem:
3200 return &bpf_map_delete_elem_proto;
3201 case BPF_FUNC_get_prandom_u32:
3202 return &bpf_get_prandom_u32_proto;
3203 case BPF_FUNC_get_smp_processor_id:
3204 return &bpf_get_raw_smp_processor_id_proto;
3205 case BPF_FUNC_get_numa_node_id:
3206 return &bpf_get_numa_node_id_proto;
3207 case BPF_FUNC_tail_call:
3208 return &bpf_tail_call_proto;
3209 case BPF_FUNC_ktime_get_ns:
3210 return &bpf_ktime_get_ns_proto;
3211 case BPF_FUNC_trace_printk:
3212 if (capable(CAP_SYS_ADMIN))
3213 return bpf_get_trace_printk_proto();
3219 static const struct bpf_func_proto *
3220 sock_filter_func_proto(enum bpf_func_id func_id)
3223 /* inet and inet6 sockets are created in a process
3224 * context so there is always a valid uid/gid
3226 case BPF_FUNC_get_current_uid_gid:
3227 return &bpf_get_current_uid_gid_proto;
3229 return bpf_base_func_proto(func_id);
3233 static const struct bpf_func_proto *
3234 sk_filter_func_proto(enum bpf_func_id func_id)
3237 case BPF_FUNC_skb_load_bytes:
3238 return &bpf_skb_load_bytes_proto;
3239 case BPF_FUNC_get_socket_cookie:
3240 return &bpf_get_socket_cookie_proto;
3241 case BPF_FUNC_get_socket_uid:
3242 return &bpf_get_socket_uid_proto;
3244 return bpf_base_func_proto(func_id);
3248 static const struct bpf_func_proto *
3249 tc_cls_act_func_proto(enum bpf_func_id func_id)
3252 case BPF_FUNC_skb_store_bytes:
3253 return &bpf_skb_store_bytes_proto;
3254 case BPF_FUNC_skb_load_bytes:
3255 return &bpf_skb_load_bytes_proto;
3256 case BPF_FUNC_skb_pull_data:
3257 return &bpf_skb_pull_data_proto;
3258 case BPF_FUNC_csum_diff:
3259 return &bpf_csum_diff_proto;
3260 case BPF_FUNC_csum_update:
3261 return &bpf_csum_update_proto;
3262 case BPF_FUNC_l3_csum_replace:
3263 return &bpf_l3_csum_replace_proto;
3264 case BPF_FUNC_l4_csum_replace:
3265 return &bpf_l4_csum_replace_proto;
3266 case BPF_FUNC_clone_redirect:
3267 return &bpf_clone_redirect_proto;
3268 case BPF_FUNC_get_cgroup_classid:
3269 return &bpf_get_cgroup_classid_proto;
3270 case BPF_FUNC_skb_vlan_push:
3271 return &bpf_skb_vlan_push_proto;
3272 case BPF_FUNC_skb_vlan_pop:
3273 return &bpf_skb_vlan_pop_proto;
3274 case BPF_FUNC_skb_change_proto:
3275 return &bpf_skb_change_proto_proto;
3276 case BPF_FUNC_skb_change_type:
3277 return &bpf_skb_change_type_proto;
3278 case BPF_FUNC_skb_adjust_room:
3279 return &bpf_skb_adjust_room_proto;
3280 case BPF_FUNC_skb_change_tail:
3281 return &bpf_skb_change_tail_proto;
3282 case BPF_FUNC_skb_get_tunnel_key:
3283 return &bpf_skb_get_tunnel_key_proto;
3284 case BPF_FUNC_skb_set_tunnel_key:
3285 return bpf_get_skb_set_tunnel_proto(func_id);
3286 case BPF_FUNC_skb_get_tunnel_opt:
3287 return &bpf_skb_get_tunnel_opt_proto;
3288 case BPF_FUNC_skb_set_tunnel_opt:
3289 return bpf_get_skb_set_tunnel_proto(func_id);
3290 case BPF_FUNC_redirect:
3291 return &bpf_redirect_proto;
3292 case BPF_FUNC_get_route_realm:
3293 return &bpf_get_route_realm_proto;
3294 case BPF_FUNC_get_hash_recalc:
3295 return &bpf_get_hash_recalc_proto;
3296 case BPF_FUNC_set_hash_invalid:
3297 return &bpf_set_hash_invalid_proto;
3298 case BPF_FUNC_set_hash:
3299 return &bpf_set_hash_proto;
3300 case BPF_FUNC_perf_event_output:
3301 return &bpf_skb_event_output_proto;
3302 case BPF_FUNC_get_smp_processor_id:
3303 return &bpf_get_smp_processor_id_proto;
3304 case BPF_FUNC_skb_under_cgroup:
3305 return &bpf_skb_under_cgroup_proto;
3306 case BPF_FUNC_get_socket_cookie:
3307 return &bpf_get_socket_cookie_proto;
3308 case BPF_FUNC_get_socket_uid:
3309 return &bpf_get_socket_uid_proto;
3311 return bpf_base_func_proto(func_id);
3315 static const struct bpf_func_proto *
3316 xdp_func_proto(enum bpf_func_id func_id)
3319 case BPF_FUNC_perf_event_output:
3320 return &bpf_xdp_event_output_proto;
3321 case BPF_FUNC_get_smp_processor_id:
3322 return &bpf_get_smp_processor_id_proto;
3323 case BPF_FUNC_xdp_adjust_head:
3324 return &bpf_xdp_adjust_head_proto;
3325 case BPF_FUNC_redirect:
3326 return &bpf_xdp_redirect_proto;
3327 case BPF_FUNC_redirect_map:
3328 return &bpf_xdp_redirect_map_proto;
3330 return bpf_base_func_proto(func_id);
3334 static const struct bpf_func_proto *
3335 lwt_inout_func_proto(enum bpf_func_id func_id)
3338 case BPF_FUNC_skb_load_bytes:
3339 return &bpf_skb_load_bytes_proto;
3340 case BPF_FUNC_skb_pull_data:
3341 return &bpf_skb_pull_data_proto;
3342 case BPF_FUNC_csum_diff:
3343 return &bpf_csum_diff_proto;
3344 case BPF_FUNC_get_cgroup_classid:
3345 return &bpf_get_cgroup_classid_proto;
3346 case BPF_FUNC_get_route_realm:
3347 return &bpf_get_route_realm_proto;
3348 case BPF_FUNC_get_hash_recalc:
3349 return &bpf_get_hash_recalc_proto;
3350 case BPF_FUNC_perf_event_output:
3351 return &bpf_skb_event_output_proto;
3352 case BPF_FUNC_get_smp_processor_id:
3353 return &bpf_get_smp_processor_id_proto;
3354 case BPF_FUNC_skb_under_cgroup:
3355 return &bpf_skb_under_cgroup_proto;
3357 return bpf_base_func_proto(func_id);
3361 static const struct bpf_func_proto *
3362 sock_ops_func_proto(enum bpf_func_id func_id)
3365 case BPF_FUNC_setsockopt:
3366 return &bpf_setsockopt_proto;
3367 case BPF_FUNC_sock_map_update:
3368 return &bpf_sock_map_update_proto;
3370 return bpf_base_func_proto(func_id);
3374 static const struct bpf_func_proto *sk_skb_func_proto(enum bpf_func_id func_id)
3377 case BPF_FUNC_skb_store_bytes:
3378 return &bpf_skb_store_bytes_proto;
3379 case BPF_FUNC_skb_load_bytes:
3380 return &bpf_skb_load_bytes_proto;
3381 case BPF_FUNC_skb_pull_data:
3382 return &bpf_skb_pull_data_proto;
3383 case BPF_FUNC_skb_change_tail:
3384 return &bpf_skb_change_tail_proto;
3385 case BPF_FUNC_skb_change_head:
3386 return &bpf_skb_change_head_proto;
3387 case BPF_FUNC_get_socket_cookie:
3388 return &bpf_get_socket_cookie_proto;
3389 case BPF_FUNC_get_socket_uid:
3390 return &bpf_get_socket_uid_proto;
3391 case BPF_FUNC_sk_redirect_map:
3392 return &bpf_sk_redirect_map_proto;
3394 return bpf_base_func_proto(func_id);
3398 static const struct bpf_func_proto *
3399 lwt_xmit_func_proto(enum bpf_func_id func_id)
3402 case BPF_FUNC_skb_get_tunnel_key:
3403 return &bpf_skb_get_tunnel_key_proto;
3404 case BPF_FUNC_skb_set_tunnel_key:
3405 return bpf_get_skb_set_tunnel_proto(func_id);
3406 case BPF_FUNC_skb_get_tunnel_opt:
3407 return &bpf_skb_get_tunnel_opt_proto;
3408 case BPF_FUNC_skb_set_tunnel_opt:
3409 return bpf_get_skb_set_tunnel_proto(func_id);
3410 case BPF_FUNC_redirect:
3411 return &bpf_redirect_proto;
3412 case BPF_FUNC_clone_redirect:
3413 return &bpf_clone_redirect_proto;
3414 case BPF_FUNC_skb_change_tail:
3415 return &bpf_skb_change_tail_proto;
3416 case BPF_FUNC_skb_change_head:
3417 return &bpf_skb_change_head_proto;
3418 case BPF_FUNC_skb_store_bytes:
3419 return &bpf_skb_store_bytes_proto;
3420 case BPF_FUNC_csum_update:
3421 return &bpf_csum_update_proto;
3422 case BPF_FUNC_l3_csum_replace:
3423 return &bpf_l3_csum_replace_proto;
3424 case BPF_FUNC_l4_csum_replace:
3425 return &bpf_l4_csum_replace_proto;
3426 case BPF_FUNC_set_hash_invalid:
3427 return &bpf_set_hash_invalid_proto;
3429 return lwt_inout_func_proto(func_id);
3433 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
3434 struct bpf_insn_access_aux *info)
3436 const int size_default = sizeof(__u32);
3438 if (off < 0 || off >= sizeof(struct __sk_buff))
3441 /* The verifier guarantees that size > 0. */
3442 if (off % size != 0)
3446 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3447 if (off + size > offsetofend(struct __sk_buff, cb[4]))
3450 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
3451 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
3452 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
3453 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
3454 case bpf_ctx_range(struct __sk_buff, data):
3455 case bpf_ctx_range(struct __sk_buff, data_end):
3456 if (size != size_default)
3460 /* Only narrow read access allowed for now. */
3461 if (type == BPF_WRITE) {
3462 if (size != size_default)
3465 bpf_ctx_record_field_size(info, size_default);
3466 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
3474 static bool sk_filter_is_valid_access(int off, int size,
3475 enum bpf_access_type type,
3476 struct bpf_insn_access_aux *info)
3479 case bpf_ctx_range(struct __sk_buff, tc_classid):
3480 case bpf_ctx_range(struct __sk_buff, data):
3481 case bpf_ctx_range(struct __sk_buff, data_end):
3482 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3486 if (type == BPF_WRITE) {
3488 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3495 return bpf_skb_is_valid_access(off, size, type, info);
3498 static bool lwt_is_valid_access(int off, int size,
3499 enum bpf_access_type type,
3500 struct bpf_insn_access_aux *info)
3503 case bpf_ctx_range(struct __sk_buff, tc_classid):
3504 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3508 if (type == BPF_WRITE) {
3510 case bpf_ctx_range(struct __sk_buff, mark):
3511 case bpf_ctx_range(struct __sk_buff, priority):
3512 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3520 case bpf_ctx_range(struct __sk_buff, data):
3521 info->reg_type = PTR_TO_PACKET;
3523 case bpf_ctx_range(struct __sk_buff, data_end):
3524 info->reg_type = PTR_TO_PACKET_END;
3528 return bpf_skb_is_valid_access(off, size, type, info);
3531 static bool sock_filter_is_valid_access(int off, int size,
3532 enum bpf_access_type type,
3533 struct bpf_insn_access_aux *info)
3535 if (type == BPF_WRITE) {
3537 case offsetof(struct bpf_sock, bound_dev_if):
3538 case offsetof(struct bpf_sock, mark):
3539 case offsetof(struct bpf_sock, priority):
3546 if (off < 0 || off + size > sizeof(struct bpf_sock))
3548 /* The verifier guarantees that size > 0. */
3549 if (off % size != 0)
3551 if (size != sizeof(__u32))
3557 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
3558 const struct bpf_prog *prog, int drop_verdict)
3560 struct bpf_insn *insn = insn_buf;
3565 /* if (!skb->cloned)
3568 * (Fast-path, otherwise approximation that we might be
3569 * a clone, do the rest in helper.)
3571 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
3572 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
3573 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
3575 /* ret = bpf_skb_pull_data(skb, 0); */
3576 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
3577 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
3578 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
3579 BPF_FUNC_skb_pull_data);
3582 * return TC_ACT_SHOT;
3584 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
3585 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
3586 *insn++ = BPF_EXIT_INSN();
3589 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
3591 *insn++ = prog->insnsi[0];
3593 return insn - insn_buf;
3596 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
3597 const struct bpf_prog *prog)
3599 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
3602 static bool tc_cls_act_is_valid_access(int off, int size,
3603 enum bpf_access_type type,
3604 struct bpf_insn_access_aux *info)
3606 if (type == BPF_WRITE) {
3608 case bpf_ctx_range(struct __sk_buff, mark):
3609 case bpf_ctx_range(struct __sk_buff, tc_index):
3610 case bpf_ctx_range(struct __sk_buff, priority):
3611 case bpf_ctx_range(struct __sk_buff, tc_classid):
3612 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3620 case bpf_ctx_range(struct __sk_buff, data):
3621 info->reg_type = PTR_TO_PACKET;
3623 case bpf_ctx_range(struct __sk_buff, data_end):
3624 info->reg_type = PTR_TO_PACKET_END;
3626 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3630 return bpf_skb_is_valid_access(off, size, type, info);
3633 static bool __is_valid_xdp_access(int off, int size)
3635 if (off < 0 || off >= sizeof(struct xdp_md))
3637 if (off % size != 0)
3639 if (size != sizeof(__u32))
3645 static bool xdp_is_valid_access(int off, int size,
3646 enum bpf_access_type type,
3647 struct bpf_insn_access_aux *info)
3649 if (type == BPF_WRITE)
3653 case offsetof(struct xdp_md, data):
3654 info->reg_type = PTR_TO_PACKET;
3656 case offsetof(struct xdp_md, data_end):
3657 info->reg_type = PTR_TO_PACKET_END;
3661 return __is_valid_xdp_access(off, size);
3664 void bpf_warn_invalid_xdp_action(u32 act)
3666 const u32 act_max = XDP_REDIRECT;
3668 pr_warn_once("%s XDP return value %u, expect packet loss!\n",
3669 act > act_max ? "Illegal" : "Driver unsupported",
3672 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
3674 static bool __is_valid_sock_ops_access(int off, int size)
3676 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
3678 /* The verifier guarantees that size > 0. */
3679 if (off % size != 0)
3681 if (size != sizeof(__u32))
3687 static bool sock_ops_is_valid_access(int off, int size,
3688 enum bpf_access_type type,
3689 struct bpf_insn_access_aux *info)
3691 if (type == BPF_WRITE) {
3693 case offsetof(struct bpf_sock_ops, op) ...
3694 offsetof(struct bpf_sock_ops, replylong[3]):
3701 return __is_valid_sock_ops_access(off, size);
3704 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
3705 const struct bpf_prog *prog)
3707 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
3710 static bool sk_skb_is_valid_access(int off, int size,
3711 enum bpf_access_type type,
3712 struct bpf_insn_access_aux *info)
3714 if (type == BPF_WRITE) {
3716 case bpf_ctx_range(struct __sk_buff, tc_index):
3717 case bpf_ctx_range(struct __sk_buff, priority):
3725 case bpf_ctx_range(struct __sk_buff, mark):
3726 case bpf_ctx_range(struct __sk_buff, tc_classid):
3728 case bpf_ctx_range(struct __sk_buff, data):
3729 info->reg_type = PTR_TO_PACKET;
3731 case bpf_ctx_range(struct __sk_buff, data_end):
3732 info->reg_type = PTR_TO_PACKET_END;
3736 return bpf_skb_is_valid_access(off, size, type, info);
3739 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
3740 const struct bpf_insn *si,
3741 struct bpf_insn *insn_buf,
3742 struct bpf_prog *prog, u32 *target_size)
3744 struct bpf_insn *insn = insn_buf;
3748 case offsetof(struct __sk_buff, len):
3749 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3750 bpf_target_off(struct sk_buff, len, 4,
3754 case offsetof(struct __sk_buff, protocol):
3755 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3756 bpf_target_off(struct sk_buff, protocol, 2,
3760 case offsetof(struct __sk_buff, vlan_proto):
3761 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3762 bpf_target_off(struct sk_buff, vlan_proto, 2,
3766 case offsetof(struct __sk_buff, priority):
3767 if (type == BPF_WRITE)
3768 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3769 bpf_target_off(struct sk_buff, priority, 4,
3772 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3773 bpf_target_off(struct sk_buff, priority, 4,
3777 case offsetof(struct __sk_buff, ingress_ifindex):
3778 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3779 bpf_target_off(struct sk_buff, skb_iif, 4,
3783 case offsetof(struct __sk_buff, ifindex):
3784 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
3785 si->dst_reg, si->src_reg,
3786 offsetof(struct sk_buff, dev));
3787 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
3788 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3789 bpf_target_off(struct net_device, ifindex, 4,
3793 case offsetof(struct __sk_buff, hash):
3794 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3795 bpf_target_off(struct sk_buff, hash, 4,
3799 case offsetof(struct __sk_buff, mark):
3800 if (type == BPF_WRITE)
3801 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3802 bpf_target_off(struct sk_buff, mark, 4,
3805 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3806 bpf_target_off(struct sk_buff, mark, 4,
3810 case offsetof(struct __sk_buff, pkt_type):
3812 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
3814 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
3815 #ifdef __BIG_ENDIAN_BITFIELD
3816 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
3820 case offsetof(struct __sk_buff, queue_mapping):
3821 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3822 bpf_target_off(struct sk_buff, queue_mapping, 2,
3826 case offsetof(struct __sk_buff, vlan_present):
3827 case offsetof(struct __sk_buff, vlan_tci):
3828 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
3830 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3831 bpf_target_off(struct sk_buff, vlan_tci, 2,
3833 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
3834 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
3837 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
3838 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
3842 case offsetof(struct __sk_buff, cb[0]) ...
3843 offsetofend(struct __sk_buff, cb[4]) - 1:
3844 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
3845 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
3846 offsetof(struct qdisc_skb_cb, data)) %
3849 prog->cb_access = 1;
3851 off -= offsetof(struct __sk_buff, cb[0]);
3852 off += offsetof(struct sk_buff, cb);
3853 off += offsetof(struct qdisc_skb_cb, data);
3854 if (type == BPF_WRITE)
3855 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
3858 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
3862 case offsetof(struct __sk_buff, tc_classid):
3863 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
3866 off -= offsetof(struct __sk_buff, tc_classid);
3867 off += offsetof(struct sk_buff, cb);
3868 off += offsetof(struct qdisc_skb_cb, tc_classid);
3870 if (type == BPF_WRITE)
3871 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
3874 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
3878 case offsetof(struct __sk_buff, data):
3879 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
3880 si->dst_reg, si->src_reg,
3881 offsetof(struct sk_buff, data));
3884 case offsetof(struct __sk_buff, data_end):
3886 off -= offsetof(struct __sk_buff, data_end);
3887 off += offsetof(struct sk_buff, cb);
3888 off += offsetof(struct bpf_skb_data_end, data_end);
3889 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
3893 case offsetof(struct __sk_buff, tc_index):
3894 #ifdef CONFIG_NET_SCHED
3895 if (type == BPF_WRITE)
3896 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
3897 bpf_target_off(struct sk_buff, tc_index, 2,
3900 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3901 bpf_target_off(struct sk_buff, tc_index, 2,
3905 if (type == BPF_WRITE)
3906 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
3908 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3912 case offsetof(struct __sk_buff, napi_id):
3913 #if defined(CONFIG_NET_RX_BUSY_POLL)
3914 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3915 bpf_target_off(struct sk_buff, napi_id, 4,
3917 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
3918 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3921 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3924 case offsetof(struct __sk_buff, family):
3925 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
3927 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3928 si->dst_reg, si->src_reg,
3929 offsetof(struct sk_buff, sk));
3930 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3931 bpf_target_off(struct sock_common,
3935 case offsetof(struct __sk_buff, remote_ip4):
3936 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
3938 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3939 si->dst_reg, si->src_reg,
3940 offsetof(struct sk_buff, sk));
3941 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3942 bpf_target_off(struct sock_common,
3946 case offsetof(struct __sk_buff, local_ip4):
3947 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3948 skc_rcv_saddr) != 4);
3950 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3951 si->dst_reg, si->src_reg,
3952 offsetof(struct sk_buff, sk));
3953 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3954 bpf_target_off(struct sock_common,
3958 case offsetof(struct __sk_buff, remote_ip6[0]) ...
3959 offsetof(struct __sk_buff, remote_ip6[3]):
3960 #if IS_ENABLED(CONFIG_IPV6)
3961 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3962 skc_v6_daddr.s6_addr32[0]) != 4);
3965 off -= offsetof(struct __sk_buff, remote_ip6[0]);
3967 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3968 si->dst_reg, si->src_reg,
3969 offsetof(struct sk_buff, sk));
3970 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3971 offsetof(struct sock_common,
3972 skc_v6_daddr.s6_addr32[0]) +
3975 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3978 case offsetof(struct __sk_buff, local_ip6[0]) ...
3979 offsetof(struct __sk_buff, local_ip6[3]):
3980 #if IS_ENABLED(CONFIG_IPV6)
3981 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3982 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
3985 off -= offsetof(struct __sk_buff, local_ip6[0]);
3987 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3988 si->dst_reg, si->src_reg,
3989 offsetof(struct sk_buff, sk));
3990 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3991 offsetof(struct sock_common,
3992 skc_v6_rcv_saddr.s6_addr32[0]) +
3995 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3999 case offsetof(struct __sk_buff, remote_port):
4000 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
4002 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4003 si->dst_reg, si->src_reg,
4004 offsetof(struct sk_buff, sk));
4005 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4006 bpf_target_off(struct sock_common,
4009 #ifndef __BIG_ENDIAN_BITFIELD
4010 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
4014 case offsetof(struct __sk_buff, local_port):
4015 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
4017 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4018 si->dst_reg, si->src_reg,
4019 offsetof(struct sk_buff, sk));
4020 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4021 bpf_target_off(struct sock_common,
4022 skc_num, 2, target_size));
4026 return insn - insn_buf;
4029 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
4030 const struct bpf_insn *si,
4031 struct bpf_insn *insn_buf,
4032 struct bpf_prog *prog, u32 *target_size)
4034 struct bpf_insn *insn = insn_buf;
4037 case offsetof(struct bpf_sock, bound_dev_if):
4038 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
4040 if (type == BPF_WRITE)
4041 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4042 offsetof(struct sock, sk_bound_dev_if));
4044 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4045 offsetof(struct sock, sk_bound_dev_if));
4048 case offsetof(struct bpf_sock, mark):
4049 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
4051 if (type == BPF_WRITE)
4052 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4053 offsetof(struct sock, sk_mark));
4055 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4056 offsetof(struct sock, sk_mark));
4059 case offsetof(struct bpf_sock, priority):
4060 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
4062 if (type == BPF_WRITE)
4063 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4064 offsetof(struct sock, sk_priority));
4066 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4067 offsetof(struct sock, sk_priority));
4070 case offsetof(struct bpf_sock, family):
4071 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
4073 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4074 offsetof(struct sock, sk_family));
4077 case offsetof(struct bpf_sock, type):
4078 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4079 offsetof(struct sock, __sk_flags_offset));
4080 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
4081 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
4084 case offsetof(struct bpf_sock, protocol):
4085 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4086 offsetof(struct sock, __sk_flags_offset));
4087 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
4088 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
4092 return insn - insn_buf;
4095 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
4096 const struct bpf_insn *si,
4097 struct bpf_insn *insn_buf,
4098 struct bpf_prog *prog, u32 *target_size)
4100 struct bpf_insn *insn = insn_buf;
4103 case offsetof(struct __sk_buff, ifindex):
4104 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
4105 si->dst_reg, si->src_reg,
4106 offsetof(struct sk_buff, dev));
4107 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4108 bpf_target_off(struct net_device, ifindex, 4,
4112 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4116 return insn - insn_buf;
4119 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
4120 const struct bpf_insn *si,
4121 struct bpf_insn *insn_buf,
4122 struct bpf_prog *prog, u32 *target_size)
4124 struct bpf_insn *insn = insn_buf;
4127 case offsetof(struct xdp_md, data):
4128 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
4129 si->dst_reg, si->src_reg,
4130 offsetof(struct xdp_buff, data));
4132 case offsetof(struct xdp_md, data_end):
4133 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
4134 si->dst_reg, si->src_reg,
4135 offsetof(struct xdp_buff, data_end));
4139 return insn - insn_buf;
4142 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
4143 const struct bpf_insn *si,
4144 struct bpf_insn *insn_buf,
4145 struct bpf_prog *prog,
4148 struct bpf_insn *insn = insn_buf;
4152 case offsetof(struct bpf_sock_ops, op) ...
4153 offsetof(struct bpf_sock_ops, replylong[3]):
4154 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
4155 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
4156 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
4157 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
4158 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
4159 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
4161 off -= offsetof(struct bpf_sock_ops, op);
4162 off += offsetof(struct bpf_sock_ops_kern, op);
4163 if (type == BPF_WRITE)
4164 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4167 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4171 case offsetof(struct bpf_sock_ops, family):
4172 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
4174 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4175 struct bpf_sock_ops_kern, sk),
4176 si->dst_reg, si->src_reg,
4177 offsetof(struct bpf_sock_ops_kern, sk));
4178 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4179 offsetof(struct sock_common, skc_family));
4182 case offsetof(struct bpf_sock_ops, remote_ip4):
4183 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
4185 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4186 struct bpf_sock_ops_kern, sk),
4187 si->dst_reg, si->src_reg,
4188 offsetof(struct bpf_sock_ops_kern, sk));
4189 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4190 offsetof(struct sock_common, skc_daddr));
4193 case offsetof(struct bpf_sock_ops, local_ip4):
4194 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
4196 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4197 struct bpf_sock_ops_kern, sk),
4198 si->dst_reg, si->src_reg,
4199 offsetof(struct bpf_sock_ops_kern, sk));
4200 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4201 offsetof(struct sock_common,
4205 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
4206 offsetof(struct bpf_sock_ops, remote_ip6[3]):
4207 #if IS_ENABLED(CONFIG_IPV6)
4208 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4209 skc_v6_daddr.s6_addr32[0]) != 4);
4212 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
4213 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4214 struct bpf_sock_ops_kern, sk),
4215 si->dst_reg, si->src_reg,
4216 offsetof(struct bpf_sock_ops_kern, sk));
4217 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4218 offsetof(struct sock_common,
4219 skc_v6_daddr.s6_addr32[0]) +
4222 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4226 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
4227 offsetof(struct bpf_sock_ops, local_ip6[3]):
4228 #if IS_ENABLED(CONFIG_IPV6)
4229 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4230 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
4233 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
4234 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4235 struct bpf_sock_ops_kern, sk),
4236 si->dst_reg, si->src_reg,
4237 offsetof(struct bpf_sock_ops_kern, sk));
4238 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4239 offsetof(struct sock_common,
4240 skc_v6_rcv_saddr.s6_addr32[0]) +
4243 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4247 case offsetof(struct bpf_sock_ops, remote_port):
4248 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
4250 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4251 struct bpf_sock_ops_kern, sk),
4252 si->dst_reg, si->src_reg,
4253 offsetof(struct bpf_sock_ops_kern, sk));
4254 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4255 offsetof(struct sock_common, skc_dport));
4256 #ifndef __BIG_ENDIAN_BITFIELD
4257 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
4261 case offsetof(struct bpf_sock_ops, local_port):
4262 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
4264 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4265 struct bpf_sock_ops_kern, sk),
4266 si->dst_reg, si->src_reg,
4267 offsetof(struct bpf_sock_ops_kern, sk));
4268 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4269 offsetof(struct sock_common, skc_num));
4272 return insn - insn_buf;
4275 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
4276 const struct bpf_insn *si,
4277 struct bpf_insn *insn_buf,
4278 struct bpf_prog *prog, u32 *target_size)
4280 struct bpf_insn *insn = insn_buf;
4284 case offsetof(struct __sk_buff, data_end):
4286 off -= offsetof(struct __sk_buff, data_end);
4287 off += offsetof(struct sk_buff, cb);
4288 off += offsetof(struct tcp_skb_cb, bpf.data_end);
4289 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
4293 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4297 return insn - insn_buf;
4300 const struct bpf_verifier_ops sk_filter_prog_ops = {
4301 .get_func_proto = sk_filter_func_proto,
4302 .is_valid_access = sk_filter_is_valid_access,
4303 .convert_ctx_access = bpf_convert_ctx_access,
4306 const struct bpf_verifier_ops tc_cls_act_prog_ops = {
4307 .get_func_proto = tc_cls_act_func_proto,
4308 .is_valid_access = tc_cls_act_is_valid_access,
4309 .convert_ctx_access = tc_cls_act_convert_ctx_access,
4310 .gen_prologue = tc_cls_act_prologue,
4311 .test_run = bpf_prog_test_run_skb,
4314 const struct bpf_verifier_ops xdp_prog_ops = {
4315 .get_func_proto = xdp_func_proto,
4316 .is_valid_access = xdp_is_valid_access,
4317 .convert_ctx_access = xdp_convert_ctx_access,
4318 .test_run = bpf_prog_test_run_xdp,
4321 const struct bpf_verifier_ops cg_skb_prog_ops = {
4322 .get_func_proto = sk_filter_func_proto,
4323 .is_valid_access = sk_filter_is_valid_access,
4324 .convert_ctx_access = bpf_convert_ctx_access,
4325 .test_run = bpf_prog_test_run_skb,
4328 const struct bpf_verifier_ops lwt_inout_prog_ops = {
4329 .get_func_proto = lwt_inout_func_proto,
4330 .is_valid_access = lwt_is_valid_access,
4331 .convert_ctx_access = bpf_convert_ctx_access,
4332 .test_run = bpf_prog_test_run_skb,
4335 const struct bpf_verifier_ops lwt_xmit_prog_ops = {
4336 .get_func_proto = lwt_xmit_func_proto,
4337 .is_valid_access = lwt_is_valid_access,
4338 .convert_ctx_access = bpf_convert_ctx_access,
4339 .gen_prologue = tc_cls_act_prologue,
4340 .test_run = bpf_prog_test_run_skb,
4343 const struct bpf_verifier_ops cg_sock_prog_ops = {
4344 .get_func_proto = sock_filter_func_proto,
4345 .is_valid_access = sock_filter_is_valid_access,
4346 .convert_ctx_access = sock_filter_convert_ctx_access,
4349 const struct bpf_verifier_ops sock_ops_prog_ops = {
4350 .get_func_proto = sock_ops_func_proto,
4351 .is_valid_access = sock_ops_is_valid_access,
4352 .convert_ctx_access = sock_ops_convert_ctx_access,
4355 const struct bpf_verifier_ops sk_skb_prog_ops = {
4356 .get_func_proto = sk_skb_func_proto,
4357 .is_valid_access = sk_skb_is_valid_access,
4358 .convert_ctx_access = sk_skb_convert_ctx_access,
4359 .gen_prologue = sk_skb_prologue,
4362 int sk_detach_filter(struct sock *sk)
4365 struct sk_filter *filter;
4367 if (sock_flag(sk, SOCK_FILTER_LOCKED))
4370 filter = rcu_dereference_protected(sk->sk_filter,
4371 lockdep_sock_is_held(sk));
4373 RCU_INIT_POINTER(sk->sk_filter, NULL);
4374 sk_filter_uncharge(sk, filter);
4380 EXPORT_SYMBOL_GPL(sk_detach_filter);
4382 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
4385 struct sock_fprog_kern *fprog;
4386 struct sk_filter *filter;
4390 filter = rcu_dereference_protected(sk->sk_filter,
4391 lockdep_sock_is_held(sk));
4395 /* We're copying the filter that has been originally attached,
4396 * so no conversion/decode needed anymore. eBPF programs that
4397 * have no original program cannot be dumped through this.
4400 fprog = filter->prog->orig_prog;
4406 /* User space only enquires number of filter blocks. */
4410 if (len < fprog->len)
4414 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
4417 /* Instead of bytes, the API requests to return the number