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>
36 #include <net/inet_common.h>
38 #include <net/protocol.h>
39 #include <net/netlink.h>
40 #include <linux/skbuff.h>
42 #include <net/flow_dissector.h>
43 #include <linux/errno.h>
44 #include <linux/timer.h>
45 #include <linux/uaccess.h>
46 #include <asm/unaligned.h>
47 #include <asm/cmpxchg.h>
48 #include <linux/filter.h>
49 #include <linux/ratelimit.h>
50 #include <linux/seccomp.h>
51 #include <linux/if_vlan.h>
52 #include <linux/bpf.h>
53 #include <net/sch_generic.h>
54 #include <net/cls_cgroup.h>
55 #include <net/dst_metadata.h>
57 #include <net/sock_reuseport.h>
58 #include <net/busy_poll.h>
60 #include <linux/bpf_trace.h>
63 * sk_filter_trim_cap - run a packet through a socket filter
64 * @sk: sock associated with &sk_buff
65 * @skb: buffer to filter
66 * @cap: limit on how short the eBPF program may trim the packet
68 * Run the eBPF program and then cut skb->data to correct size returned by
69 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
70 * than pkt_len we keep whole skb->data. This is the socket level
71 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
72 * be accepted or -EPERM if the packet should be tossed.
75 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
78 struct sk_filter *filter;
81 * If the skb was allocated from pfmemalloc reserves, only
82 * allow SOCK_MEMALLOC sockets to use it as this socket is
85 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
86 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
89 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
93 err = security_sock_rcv_skb(sk, skb);
98 filter = rcu_dereference(sk->sk_filter);
100 struct sock *save_sk = skb->sk;
101 unsigned int pkt_len;
104 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
106 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
112 EXPORT_SYMBOL(sk_filter_trim_cap);
114 BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
116 return skb_get_poff(skb);
119 BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
123 if (skb_is_nonlinear(skb))
126 if (skb->len < sizeof(struct nlattr))
129 if (a > skb->len - sizeof(struct nlattr))
132 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
134 return (void *) nla - (void *) skb->data;
139 BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
143 if (skb_is_nonlinear(skb))
146 if (skb->len < sizeof(struct nlattr))
149 if (a > skb->len - sizeof(struct nlattr))
152 nla = (struct nlattr *) &skb->data[a];
153 if (nla->nla_len > skb->len - a)
156 nla = nla_find_nested(nla, x);
158 return (void *) nla - (void *) skb->data;
163 BPF_CALL_0(__get_raw_cpu_id)
165 return raw_smp_processor_id();
168 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
169 .func = __get_raw_cpu_id,
171 .ret_type = RET_INTEGER,
174 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
175 struct bpf_insn *insn_buf)
177 struct bpf_insn *insn = insn_buf;
181 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
183 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
184 offsetof(struct sk_buff, mark));
188 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
189 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
190 #ifdef __BIG_ENDIAN_BITFIELD
191 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
196 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
198 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
199 offsetof(struct sk_buff, queue_mapping));
202 case SKF_AD_VLAN_TAG:
203 case SKF_AD_VLAN_TAG_PRESENT:
204 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
205 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
207 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
208 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
209 offsetof(struct sk_buff, vlan_tci));
210 if (skb_field == SKF_AD_VLAN_TAG) {
211 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
215 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
217 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
222 return insn - insn_buf;
225 static bool convert_bpf_extensions(struct sock_filter *fp,
226 struct bpf_insn **insnp)
228 struct bpf_insn *insn = *insnp;
232 case SKF_AD_OFF + SKF_AD_PROTOCOL:
233 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
235 /* A = *(u16 *) (CTX + offsetof(protocol)) */
236 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
237 offsetof(struct sk_buff, protocol));
238 /* A = ntohs(A) [emitting a nop or swap16] */
239 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
242 case SKF_AD_OFF + SKF_AD_PKTTYPE:
243 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
247 case SKF_AD_OFF + SKF_AD_IFINDEX:
248 case SKF_AD_OFF + SKF_AD_HATYPE:
249 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
250 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
252 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
253 BPF_REG_TMP, BPF_REG_CTX,
254 offsetof(struct sk_buff, dev));
255 /* if (tmp != 0) goto pc + 1 */
256 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
257 *insn++ = BPF_EXIT_INSN();
258 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
259 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
260 offsetof(struct net_device, ifindex));
262 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
263 offsetof(struct net_device, type));
266 case SKF_AD_OFF + SKF_AD_MARK:
267 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
271 case SKF_AD_OFF + SKF_AD_RXHASH:
272 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
274 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
275 offsetof(struct sk_buff, hash));
278 case SKF_AD_OFF + SKF_AD_QUEUE:
279 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
283 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
284 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
285 BPF_REG_A, BPF_REG_CTX, insn);
289 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
290 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
291 BPF_REG_A, BPF_REG_CTX, insn);
295 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
296 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
298 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
299 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
300 offsetof(struct sk_buff, vlan_proto));
301 /* A = ntohs(A) [emitting a nop or swap16] */
302 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
305 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
306 case SKF_AD_OFF + SKF_AD_NLATTR:
307 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
308 case SKF_AD_OFF + SKF_AD_CPU:
309 case SKF_AD_OFF + SKF_AD_RANDOM:
311 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
313 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
315 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
316 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
318 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
319 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
321 case SKF_AD_OFF + SKF_AD_NLATTR:
322 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
324 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
325 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
327 case SKF_AD_OFF + SKF_AD_CPU:
328 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
330 case SKF_AD_OFF + SKF_AD_RANDOM:
331 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
332 bpf_user_rnd_init_once();
337 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
339 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
343 /* This is just a dummy call to avoid letting the compiler
344 * evict __bpf_call_base() as an optimization. Placed here
345 * where no-one bothers.
347 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
356 * bpf_convert_filter - convert filter program
357 * @prog: the user passed filter program
358 * @len: the length of the user passed filter program
359 * @new_prog: allocated 'struct bpf_prog' or NULL
360 * @new_len: pointer to store length of converted program
362 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
363 * style extended BPF (eBPF).
364 * Conversion workflow:
366 * 1) First pass for calculating the new program length:
367 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
369 * 2) 2nd pass to remap in two passes: 1st pass finds new
370 * jump offsets, 2nd pass remapping:
371 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
373 static int bpf_convert_filter(struct sock_filter *prog, int len,
374 struct bpf_prog *new_prog, int *new_len)
376 int new_flen = 0, pass = 0, target, i, stack_off;
377 struct bpf_insn *new_insn, *first_insn = NULL;
378 struct sock_filter *fp;
382 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
383 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
385 if (len <= 0 || len > BPF_MAXINSNS)
389 first_insn = new_prog->insnsi;
390 addrs = kcalloc(len, sizeof(*addrs),
391 GFP_KERNEL | __GFP_NOWARN);
397 new_insn = first_insn;
400 /* Classic BPF related prologue emission. */
402 /* Classic BPF expects A and X to be reset first. These need
403 * to be guaranteed to be the first two instructions.
405 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
406 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
408 /* All programs must keep CTX in callee saved BPF_REG_CTX.
409 * In eBPF case it's done by the compiler, here we need to
410 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
412 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
417 for (i = 0; i < len; fp++, i++) {
418 struct bpf_insn tmp_insns[6] = { };
419 struct bpf_insn *insn = tmp_insns;
422 addrs[i] = new_insn - first_insn;
425 /* All arithmetic insns and skb loads map as-is. */
426 case BPF_ALU | BPF_ADD | BPF_X:
427 case BPF_ALU | BPF_ADD | BPF_K:
428 case BPF_ALU | BPF_SUB | BPF_X:
429 case BPF_ALU | BPF_SUB | BPF_K:
430 case BPF_ALU | BPF_AND | BPF_X:
431 case BPF_ALU | BPF_AND | BPF_K:
432 case BPF_ALU | BPF_OR | BPF_X:
433 case BPF_ALU | BPF_OR | BPF_K:
434 case BPF_ALU | BPF_LSH | BPF_X:
435 case BPF_ALU | BPF_LSH | BPF_K:
436 case BPF_ALU | BPF_RSH | BPF_X:
437 case BPF_ALU | BPF_RSH | BPF_K:
438 case BPF_ALU | BPF_XOR | BPF_X:
439 case BPF_ALU | BPF_XOR | BPF_K:
440 case BPF_ALU | BPF_MUL | BPF_X:
441 case BPF_ALU | BPF_MUL | BPF_K:
442 case BPF_ALU | BPF_DIV | BPF_X:
443 case BPF_ALU | BPF_DIV | BPF_K:
444 case BPF_ALU | BPF_MOD | BPF_X:
445 case BPF_ALU | BPF_MOD | BPF_K:
446 case BPF_ALU | BPF_NEG:
447 case BPF_LD | BPF_ABS | BPF_W:
448 case BPF_LD | BPF_ABS | BPF_H:
449 case BPF_LD | BPF_ABS | BPF_B:
450 case BPF_LD | BPF_IND | BPF_W:
451 case BPF_LD | BPF_IND | BPF_H:
452 case BPF_LD | BPF_IND | BPF_B:
453 /* Check for overloaded BPF extension and
454 * directly convert it if found, otherwise
455 * just move on with mapping.
457 if (BPF_CLASS(fp->code) == BPF_LD &&
458 BPF_MODE(fp->code) == BPF_ABS &&
459 convert_bpf_extensions(fp, &insn))
462 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
463 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
464 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
465 /* Error with exception code on div/mod by 0.
466 * For cBPF programs, this was always return 0.
468 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
469 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
470 *insn++ = BPF_EXIT_INSN();
473 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
476 /* Jump transformation cannot use BPF block macros
477 * everywhere as offset calculation and target updates
478 * require a bit more work than the rest, i.e. jump
479 * opcodes map as-is, but offsets need adjustment.
482 #define BPF_EMIT_JMP \
484 if (target >= len || target < 0) \
486 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
487 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
488 insn->off -= insn - tmp_insns; \
491 case BPF_JMP | BPF_JA:
492 target = i + fp->k + 1;
493 insn->code = fp->code;
497 case BPF_JMP | BPF_JEQ | BPF_K:
498 case BPF_JMP | BPF_JEQ | BPF_X:
499 case BPF_JMP | BPF_JSET | BPF_K:
500 case BPF_JMP | BPF_JSET | BPF_X:
501 case BPF_JMP | BPF_JGT | BPF_K:
502 case BPF_JMP | BPF_JGT | BPF_X:
503 case BPF_JMP | BPF_JGE | BPF_K:
504 case BPF_JMP | BPF_JGE | BPF_X:
505 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
506 /* BPF immediates are signed, zero extend
507 * immediate into tmp register and use it
510 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
512 insn->dst_reg = BPF_REG_A;
513 insn->src_reg = BPF_REG_TMP;
516 insn->dst_reg = BPF_REG_A;
518 bpf_src = BPF_SRC(fp->code);
519 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
522 /* Common case where 'jump_false' is next insn. */
524 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
525 target = i + fp->jt + 1;
530 /* Convert some jumps when 'jump_true' is next insn. */
532 switch (BPF_OP(fp->code)) {
534 insn->code = BPF_JMP | BPF_JNE | bpf_src;
537 insn->code = BPF_JMP | BPF_JLE | bpf_src;
540 insn->code = BPF_JMP | BPF_JLT | bpf_src;
546 target = i + fp->jf + 1;
551 /* Other jumps are mapped into two insns: Jxx and JA. */
552 target = i + fp->jt + 1;
553 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
557 insn->code = BPF_JMP | BPF_JA;
558 target = i + fp->jf + 1;
562 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
563 case BPF_LDX | BPF_MSH | BPF_B:
565 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
566 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
567 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
569 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
571 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
573 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
575 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
578 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
579 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
581 case BPF_RET | BPF_A:
582 case BPF_RET | BPF_K:
583 if (BPF_RVAL(fp->code) == BPF_K)
584 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
586 *insn = BPF_EXIT_INSN();
589 /* Store to stack. */
592 stack_off = fp->k * 4 + 4;
593 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
594 BPF_ST ? BPF_REG_A : BPF_REG_X,
596 /* check_load_and_stores() verifies that classic BPF can
597 * load from stack only after write, so tracking
598 * stack_depth for ST|STX insns is enough
600 if (new_prog && new_prog->aux->stack_depth < stack_off)
601 new_prog->aux->stack_depth = stack_off;
604 /* Load from stack. */
605 case BPF_LD | BPF_MEM:
606 case BPF_LDX | BPF_MEM:
607 stack_off = fp->k * 4 + 4;
608 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
609 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
614 case BPF_LD | BPF_IMM:
615 case BPF_LDX | BPF_IMM:
616 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
617 BPF_REG_A : BPF_REG_X, fp->k);
621 case BPF_MISC | BPF_TAX:
622 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
626 case BPF_MISC | BPF_TXA:
627 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
630 /* A = skb->len or X = skb->len */
631 case BPF_LD | BPF_W | BPF_LEN:
632 case BPF_LDX | BPF_W | BPF_LEN:
633 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
634 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
635 offsetof(struct sk_buff, len));
638 /* Access seccomp_data fields. */
639 case BPF_LDX | BPF_ABS | BPF_W:
640 /* A = *(u32 *) (ctx + K) */
641 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
644 /* Unknown instruction. */
651 memcpy(new_insn, tmp_insns,
652 sizeof(*insn) * (insn - tmp_insns));
653 new_insn += insn - tmp_insns;
657 /* Only calculating new length. */
658 *new_len = new_insn - first_insn;
663 if (new_flen != new_insn - first_insn) {
664 new_flen = new_insn - first_insn;
671 BUG_ON(*new_len != new_flen);
680 * As we dont want to clear mem[] array for each packet going through
681 * __bpf_prog_run(), we check that filter loaded by user never try to read
682 * a cell if not previously written, and we check all branches to be sure
683 * a malicious user doesn't try to abuse us.
685 static int check_load_and_stores(const struct sock_filter *filter, int flen)
687 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
690 BUILD_BUG_ON(BPF_MEMWORDS > 16);
692 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
696 memset(masks, 0xff, flen * sizeof(*masks));
698 for (pc = 0; pc < flen; pc++) {
699 memvalid &= masks[pc];
701 switch (filter[pc].code) {
704 memvalid |= (1 << filter[pc].k);
706 case BPF_LD | BPF_MEM:
707 case BPF_LDX | BPF_MEM:
708 if (!(memvalid & (1 << filter[pc].k))) {
713 case BPF_JMP | BPF_JA:
714 /* A jump must set masks on target */
715 masks[pc + 1 + filter[pc].k] &= memvalid;
718 case BPF_JMP | BPF_JEQ | BPF_K:
719 case BPF_JMP | BPF_JEQ | BPF_X:
720 case BPF_JMP | BPF_JGE | BPF_K:
721 case BPF_JMP | BPF_JGE | BPF_X:
722 case BPF_JMP | BPF_JGT | BPF_K:
723 case BPF_JMP | BPF_JGT | BPF_X:
724 case BPF_JMP | BPF_JSET | BPF_K:
725 case BPF_JMP | BPF_JSET | BPF_X:
726 /* A jump must set masks on targets */
727 masks[pc + 1 + filter[pc].jt] &= memvalid;
728 masks[pc + 1 + filter[pc].jf] &= memvalid;
738 static bool chk_code_allowed(u16 code_to_probe)
740 static const bool codes[] = {
741 /* 32 bit ALU operations */
742 [BPF_ALU | BPF_ADD | BPF_K] = true,
743 [BPF_ALU | BPF_ADD | BPF_X] = true,
744 [BPF_ALU | BPF_SUB | BPF_K] = true,
745 [BPF_ALU | BPF_SUB | BPF_X] = true,
746 [BPF_ALU | BPF_MUL | BPF_K] = true,
747 [BPF_ALU | BPF_MUL | BPF_X] = true,
748 [BPF_ALU | BPF_DIV | BPF_K] = true,
749 [BPF_ALU | BPF_DIV | BPF_X] = true,
750 [BPF_ALU | BPF_MOD | BPF_K] = true,
751 [BPF_ALU | BPF_MOD | BPF_X] = true,
752 [BPF_ALU | BPF_AND | BPF_K] = true,
753 [BPF_ALU | BPF_AND | BPF_X] = true,
754 [BPF_ALU | BPF_OR | BPF_K] = true,
755 [BPF_ALU | BPF_OR | BPF_X] = true,
756 [BPF_ALU | BPF_XOR | BPF_K] = true,
757 [BPF_ALU | BPF_XOR | BPF_X] = true,
758 [BPF_ALU | BPF_LSH | BPF_K] = true,
759 [BPF_ALU | BPF_LSH | BPF_X] = true,
760 [BPF_ALU | BPF_RSH | BPF_K] = true,
761 [BPF_ALU | BPF_RSH | BPF_X] = true,
762 [BPF_ALU | BPF_NEG] = true,
763 /* Load instructions */
764 [BPF_LD | BPF_W | BPF_ABS] = true,
765 [BPF_LD | BPF_H | BPF_ABS] = true,
766 [BPF_LD | BPF_B | BPF_ABS] = true,
767 [BPF_LD | BPF_W | BPF_LEN] = true,
768 [BPF_LD | BPF_W | BPF_IND] = true,
769 [BPF_LD | BPF_H | BPF_IND] = true,
770 [BPF_LD | BPF_B | BPF_IND] = true,
771 [BPF_LD | BPF_IMM] = true,
772 [BPF_LD | BPF_MEM] = true,
773 [BPF_LDX | BPF_W | BPF_LEN] = true,
774 [BPF_LDX | BPF_B | BPF_MSH] = true,
775 [BPF_LDX | BPF_IMM] = true,
776 [BPF_LDX | BPF_MEM] = true,
777 /* Store instructions */
780 /* Misc instructions */
781 [BPF_MISC | BPF_TAX] = true,
782 [BPF_MISC | BPF_TXA] = true,
783 /* Return instructions */
784 [BPF_RET | BPF_K] = true,
785 [BPF_RET | BPF_A] = true,
786 /* Jump instructions */
787 [BPF_JMP | BPF_JA] = true,
788 [BPF_JMP | BPF_JEQ | BPF_K] = true,
789 [BPF_JMP | BPF_JEQ | BPF_X] = true,
790 [BPF_JMP | BPF_JGE | BPF_K] = true,
791 [BPF_JMP | BPF_JGE | BPF_X] = true,
792 [BPF_JMP | BPF_JGT | BPF_K] = true,
793 [BPF_JMP | BPF_JGT | BPF_X] = true,
794 [BPF_JMP | BPF_JSET | BPF_K] = true,
795 [BPF_JMP | BPF_JSET | BPF_X] = true,
798 if (code_to_probe >= ARRAY_SIZE(codes))
801 return codes[code_to_probe];
804 static bool bpf_check_basics_ok(const struct sock_filter *filter,
809 if (flen == 0 || flen > BPF_MAXINSNS)
816 * bpf_check_classic - verify socket filter code
817 * @filter: filter to verify
818 * @flen: length of filter
820 * Check the user's filter code. If we let some ugly
821 * filter code slip through kaboom! The filter must contain
822 * no references or jumps that are out of range, no illegal
823 * instructions, and must end with a RET instruction.
825 * All jumps are forward as they are not signed.
827 * Returns 0 if the rule set is legal or -EINVAL if not.
829 static int bpf_check_classic(const struct sock_filter *filter,
835 /* Check the filter code now */
836 for (pc = 0; pc < flen; pc++) {
837 const struct sock_filter *ftest = &filter[pc];
839 /* May we actually operate on this code? */
840 if (!chk_code_allowed(ftest->code))
843 /* Some instructions need special checks */
844 switch (ftest->code) {
845 case BPF_ALU | BPF_DIV | BPF_K:
846 case BPF_ALU | BPF_MOD | BPF_K:
847 /* Check for division by zero */
851 case BPF_ALU | BPF_LSH | BPF_K:
852 case BPF_ALU | BPF_RSH | BPF_K:
856 case BPF_LD | BPF_MEM:
857 case BPF_LDX | BPF_MEM:
860 /* Check for invalid memory addresses */
861 if (ftest->k >= BPF_MEMWORDS)
864 case BPF_JMP | BPF_JA:
865 /* Note, the large ftest->k might cause loops.
866 * Compare this with conditional jumps below,
867 * where offsets are limited. --ANK (981016)
869 if (ftest->k >= (unsigned int)(flen - pc - 1))
872 case BPF_JMP | BPF_JEQ | BPF_K:
873 case BPF_JMP | BPF_JEQ | BPF_X:
874 case BPF_JMP | BPF_JGE | BPF_K:
875 case BPF_JMP | BPF_JGE | BPF_X:
876 case BPF_JMP | BPF_JGT | BPF_K:
877 case BPF_JMP | BPF_JGT | BPF_X:
878 case BPF_JMP | BPF_JSET | BPF_K:
879 case BPF_JMP | BPF_JSET | BPF_X:
880 /* Both conditionals must be safe */
881 if (pc + ftest->jt + 1 >= flen ||
882 pc + ftest->jf + 1 >= flen)
885 case BPF_LD | BPF_W | BPF_ABS:
886 case BPF_LD | BPF_H | BPF_ABS:
887 case BPF_LD | BPF_B | BPF_ABS:
889 if (bpf_anc_helper(ftest) & BPF_ANC)
891 /* Ancillary operation unknown or unsupported */
892 if (anc_found == false && ftest->k >= SKF_AD_OFF)
897 /* Last instruction must be a RET code */
898 switch (filter[flen - 1].code) {
899 case BPF_RET | BPF_K:
900 case BPF_RET | BPF_A:
901 return check_load_and_stores(filter, flen);
907 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
908 const struct sock_fprog *fprog)
910 unsigned int fsize = bpf_classic_proglen(fprog);
911 struct sock_fprog_kern *fkprog;
913 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
917 fkprog = fp->orig_prog;
918 fkprog->len = fprog->len;
920 fkprog->filter = kmemdup(fp->insns, fsize,
921 GFP_KERNEL | __GFP_NOWARN);
922 if (!fkprog->filter) {
923 kfree(fp->orig_prog);
930 static void bpf_release_orig_filter(struct bpf_prog *fp)
932 struct sock_fprog_kern *fprog = fp->orig_prog;
935 kfree(fprog->filter);
940 static void __bpf_prog_release(struct bpf_prog *prog)
942 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
945 bpf_release_orig_filter(prog);
950 static void __sk_filter_release(struct sk_filter *fp)
952 __bpf_prog_release(fp->prog);
957 * sk_filter_release_rcu - Release a socket filter by rcu_head
958 * @rcu: rcu_head that contains the sk_filter to free
960 static void sk_filter_release_rcu(struct rcu_head *rcu)
962 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
964 __sk_filter_release(fp);
968 * sk_filter_release - release a socket filter
969 * @fp: filter to remove
971 * Remove a filter from a socket and release its resources.
973 static void sk_filter_release(struct sk_filter *fp)
975 if (refcount_dec_and_test(&fp->refcnt))
976 call_rcu(&fp->rcu, sk_filter_release_rcu);
979 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
981 u32 filter_size = bpf_prog_size(fp->prog->len);
983 atomic_sub(filter_size, &sk->sk_omem_alloc);
984 sk_filter_release(fp);
987 /* try to charge the socket memory if there is space available
988 * return true on success
990 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
992 u32 filter_size = bpf_prog_size(fp->prog->len);
994 /* same check as in sock_kmalloc() */
995 if (filter_size <= sysctl_optmem_max &&
996 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
997 atomic_add(filter_size, &sk->sk_omem_alloc);
1003 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1005 if (!refcount_inc_not_zero(&fp->refcnt))
1008 if (!__sk_filter_charge(sk, fp)) {
1009 sk_filter_release(fp);
1015 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1017 struct sock_filter *old_prog;
1018 struct bpf_prog *old_fp;
1019 int err, new_len, old_len = fp->len;
1021 /* We are free to overwrite insns et al right here as it
1022 * won't be used at this point in time anymore internally
1023 * after the migration to the internal BPF instruction
1026 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1027 sizeof(struct bpf_insn));
1029 /* Conversion cannot happen on overlapping memory areas,
1030 * so we need to keep the user BPF around until the 2nd
1031 * pass. At this time, the user BPF is stored in fp->insns.
1033 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1034 GFP_KERNEL | __GFP_NOWARN);
1040 /* 1st pass: calculate the new program length. */
1041 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
1045 /* Expand fp for appending the new filter representation. */
1047 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1049 /* The old_fp is still around in case we couldn't
1050 * allocate new memory, so uncharge on that one.
1059 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1060 err = bpf_convert_filter(old_prog, old_len, fp, &new_len);
1062 /* 2nd bpf_convert_filter() can fail only if it fails
1063 * to allocate memory, remapping must succeed. Note,
1064 * that at this time old_fp has already been released
1069 fp = bpf_prog_select_runtime(fp, &err);
1079 __bpf_prog_release(fp);
1080 return ERR_PTR(err);
1083 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1084 bpf_aux_classic_check_t trans)
1088 fp->bpf_func = NULL;
1091 err = bpf_check_classic(fp->insns, fp->len);
1093 __bpf_prog_release(fp);
1094 return ERR_PTR(err);
1097 /* There might be additional checks and transformations
1098 * needed on classic filters, f.e. in case of seccomp.
1101 err = trans(fp->insns, fp->len);
1103 __bpf_prog_release(fp);
1104 return ERR_PTR(err);
1108 /* Probe if we can JIT compile the filter and if so, do
1109 * the compilation of the filter.
1111 bpf_jit_compile(fp);
1113 /* JIT compiler couldn't process this filter, so do the
1114 * internal BPF translation for the optimized interpreter.
1117 fp = bpf_migrate_filter(fp);
1123 * bpf_prog_create - create an unattached filter
1124 * @pfp: the unattached filter that is created
1125 * @fprog: the filter program
1127 * Create a filter independent of any socket. We first run some
1128 * sanity checks on it to make sure it does not explode on us later.
1129 * If an error occurs or there is insufficient memory for the filter
1130 * a negative errno code is returned. On success the return is zero.
1132 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1134 unsigned int fsize = bpf_classic_proglen(fprog);
1135 struct bpf_prog *fp;
1137 /* Make sure new filter is there and in the right amounts. */
1138 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1141 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1145 memcpy(fp->insns, fprog->filter, fsize);
1147 fp->len = fprog->len;
1148 /* Since unattached filters are not copied back to user
1149 * space through sk_get_filter(), we do not need to hold
1150 * a copy here, and can spare us the work.
1152 fp->orig_prog = NULL;
1154 /* bpf_prepare_filter() already takes care of freeing
1155 * memory in case something goes wrong.
1157 fp = bpf_prepare_filter(fp, NULL);
1164 EXPORT_SYMBOL_GPL(bpf_prog_create);
1167 * bpf_prog_create_from_user - create an unattached filter from user buffer
1168 * @pfp: the unattached filter that is created
1169 * @fprog: the filter program
1170 * @trans: post-classic verifier transformation handler
1171 * @save_orig: save classic BPF program
1173 * This function effectively does the same as bpf_prog_create(), only
1174 * that it builds up its insns buffer from user space provided buffer.
1175 * It also allows for passing a bpf_aux_classic_check_t handler.
1177 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1178 bpf_aux_classic_check_t trans, bool save_orig)
1180 unsigned int fsize = bpf_classic_proglen(fprog);
1181 struct bpf_prog *fp;
1184 /* Make sure new filter is there and in the right amounts. */
1185 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1188 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1192 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1193 __bpf_prog_free(fp);
1197 fp->len = fprog->len;
1198 fp->orig_prog = NULL;
1201 err = bpf_prog_store_orig_filter(fp, fprog);
1203 __bpf_prog_free(fp);
1208 /* bpf_prepare_filter() already takes care of freeing
1209 * memory in case something goes wrong.
1211 fp = bpf_prepare_filter(fp, trans);
1218 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1220 void bpf_prog_destroy(struct bpf_prog *fp)
1222 __bpf_prog_release(fp);
1224 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1226 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1228 struct sk_filter *fp, *old_fp;
1230 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1236 if (!__sk_filter_charge(sk, fp)) {
1240 refcount_set(&fp->refcnt, 1);
1242 old_fp = rcu_dereference_protected(sk->sk_filter,
1243 lockdep_sock_is_held(sk));
1244 rcu_assign_pointer(sk->sk_filter, fp);
1247 sk_filter_uncharge(sk, old_fp);
1252 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1254 struct bpf_prog *old_prog;
1257 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1260 if (sk_unhashed(sk) && sk->sk_reuseport) {
1261 err = reuseport_alloc(sk);
1264 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1265 /* The socket wasn't bound with SO_REUSEPORT */
1269 old_prog = reuseport_attach_prog(sk, prog);
1271 bpf_prog_destroy(old_prog);
1277 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1279 unsigned int fsize = bpf_classic_proglen(fprog);
1280 struct bpf_prog *prog;
1283 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1284 return ERR_PTR(-EPERM);
1286 /* Make sure new filter is there and in the right amounts. */
1287 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1288 return ERR_PTR(-EINVAL);
1290 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1292 return ERR_PTR(-ENOMEM);
1294 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1295 __bpf_prog_free(prog);
1296 return ERR_PTR(-EFAULT);
1299 prog->len = fprog->len;
1301 err = bpf_prog_store_orig_filter(prog, fprog);
1303 __bpf_prog_free(prog);
1304 return ERR_PTR(-ENOMEM);
1307 /* bpf_prepare_filter() already takes care of freeing
1308 * memory in case something goes wrong.
1310 return bpf_prepare_filter(prog, NULL);
1314 * sk_attach_filter - attach a socket filter
1315 * @fprog: the filter program
1316 * @sk: the socket to use
1318 * Attach the user's filter code. We first run some sanity checks on
1319 * it to make sure it does not explode on us later. If an error
1320 * occurs or there is insufficient memory for the filter a negative
1321 * errno code is returned. On success the return is zero.
1323 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1325 struct bpf_prog *prog = __get_filter(fprog, sk);
1329 return PTR_ERR(prog);
1331 err = __sk_attach_prog(prog, sk);
1333 __bpf_prog_release(prog);
1339 EXPORT_SYMBOL_GPL(sk_attach_filter);
1341 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1343 struct bpf_prog *prog = __get_filter(fprog, sk);
1347 return PTR_ERR(prog);
1349 err = __reuseport_attach_prog(prog, sk);
1351 __bpf_prog_release(prog);
1358 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1360 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1361 return ERR_PTR(-EPERM);
1363 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1366 int sk_attach_bpf(u32 ufd, struct sock *sk)
1368 struct bpf_prog *prog = __get_bpf(ufd, sk);
1372 return PTR_ERR(prog);
1374 err = __sk_attach_prog(prog, sk);
1383 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1385 struct bpf_prog *prog = __get_bpf(ufd, sk);
1389 return PTR_ERR(prog);
1391 err = __reuseport_attach_prog(prog, sk);
1400 struct bpf_scratchpad {
1402 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1403 u8 buff[MAX_BPF_STACK];
1407 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1409 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1410 unsigned int write_len)
1412 return skb_ensure_writable(skb, write_len);
1415 static inline int bpf_try_make_writable(struct sk_buff *skb,
1416 unsigned int write_len)
1418 int err = __bpf_try_make_writable(skb, write_len);
1420 bpf_compute_data_pointers(skb);
1424 static int bpf_try_make_head_writable(struct sk_buff *skb)
1426 return bpf_try_make_writable(skb, skb_headlen(skb));
1429 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1431 if (skb_at_tc_ingress(skb))
1432 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1435 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1437 if (skb_at_tc_ingress(skb))
1438 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1441 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1442 const void *, from, u32, len, u64, flags)
1446 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1448 if (unlikely(offset > 0xffff))
1450 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1453 ptr = skb->data + offset;
1454 if (flags & BPF_F_RECOMPUTE_CSUM)
1455 __skb_postpull_rcsum(skb, ptr, len, offset);
1457 memcpy(ptr, from, len);
1459 if (flags & BPF_F_RECOMPUTE_CSUM)
1460 __skb_postpush_rcsum(skb, ptr, len, offset);
1461 if (flags & BPF_F_INVALIDATE_HASH)
1462 skb_clear_hash(skb);
1467 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1468 .func = bpf_skb_store_bytes,
1470 .ret_type = RET_INTEGER,
1471 .arg1_type = ARG_PTR_TO_CTX,
1472 .arg2_type = ARG_ANYTHING,
1473 .arg3_type = ARG_PTR_TO_MEM,
1474 .arg4_type = ARG_CONST_SIZE,
1475 .arg5_type = ARG_ANYTHING,
1478 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1479 void *, to, u32, len)
1483 if (unlikely(offset > 0xffff))
1486 ptr = skb_header_pointer(skb, offset, len, to);
1490 memcpy(to, ptr, len);
1498 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1499 .func = bpf_skb_load_bytes,
1501 .ret_type = RET_INTEGER,
1502 .arg1_type = ARG_PTR_TO_CTX,
1503 .arg2_type = ARG_ANYTHING,
1504 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1505 .arg4_type = ARG_CONST_SIZE,
1508 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1510 /* Idea is the following: should the needed direct read/write
1511 * test fail during runtime, we can pull in more data and redo
1512 * again, since implicitly, we invalidate previous checks here.
1514 * Or, since we know how much we need to make read/writeable,
1515 * this can be done once at the program beginning for direct
1516 * access case. By this we overcome limitations of only current
1517 * headroom being accessible.
1519 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1522 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1523 .func = bpf_skb_pull_data,
1525 .ret_type = RET_INTEGER,
1526 .arg1_type = ARG_PTR_TO_CTX,
1527 .arg2_type = ARG_ANYTHING,
1530 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1531 u64, from, u64, to, u64, flags)
1535 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1537 if (unlikely(offset > 0xffff || offset & 1))
1539 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1542 ptr = (__sum16 *)(skb->data + offset);
1543 switch (flags & BPF_F_HDR_FIELD_MASK) {
1545 if (unlikely(from != 0))
1548 csum_replace_by_diff(ptr, to);
1551 csum_replace2(ptr, from, to);
1554 csum_replace4(ptr, from, to);
1563 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1564 .func = bpf_l3_csum_replace,
1566 .ret_type = RET_INTEGER,
1567 .arg1_type = ARG_PTR_TO_CTX,
1568 .arg2_type = ARG_ANYTHING,
1569 .arg3_type = ARG_ANYTHING,
1570 .arg4_type = ARG_ANYTHING,
1571 .arg5_type = ARG_ANYTHING,
1574 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1575 u64, from, u64, to, u64, flags)
1577 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1578 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1579 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1582 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1583 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1585 if (unlikely(offset > 0xffff || offset & 1))
1587 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1590 ptr = (__sum16 *)(skb->data + offset);
1591 if (is_mmzero && !do_mforce && !*ptr)
1594 switch (flags & BPF_F_HDR_FIELD_MASK) {
1596 if (unlikely(from != 0))
1599 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1602 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1605 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1611 if (is_mmzero && !*ptr)
1612 *ptr = CSUM_MANGLED_0;
1616 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1617 .func = bpf_l4_csum_replace,
1619 .ret_type = RET_INTEGER,
1620 .arg1_type = ARG_PTR_TO_CTX,
1621 .arg2_type = ARG_ANYTHING,
1622 .arg3_type = ARG_ANYTHING,
1623 .arg4_type = ARG_ANYTHING,
1624 .arg5_type = ARG_ANYTHING,
1627 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1628 __be32 *, to, u32, to_size, __wsum, seed)
1630 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1631 u32 diff_size = from_size + to_size;
1634 /* This is quite flexible, some examples:
1636 * from_size == 0, to_size > 0, seed := csum --> pushing data
1637 * from_size > 0, to_size == 0, seed := csum --> pulling data
1638 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1640 * Even for diffing, from_size and to_size don't need to be equal.
1642 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1643 diff_size > sizeof(sp->diff)))
1646 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1647 sp->diff[j] = ~from[i];
1648 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1649 sp->diff[j] = to[i];
1651 return csum_partial(sp->diff, diff_size, seed);
1654 static const struct bpf_func_proto bpf_csum_diff_proto = {
1655 .func = bpf_csum_diff,
1658 .ret_type = RET_INTEGER,
1659 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1660 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1661 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1662 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1663 .arg5_type = ARG_ANYTHING,
1666 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1668 /* The interface is to be used in combination with bpf_csum_diff()
1669 * for direct packet writes. csum rotation for alignment as well
1670 * as emulating csum_sub() can be done from the eBPF program.
1672 if (skb->ip_summed == CHECKSUM_COMPLETE)
1673 return (skb->csum = csum_add(skb->csum, csum));
1678 static const struct bpf_func_proto bpf_csum_update_proto = {
1679 .func = bpf_csum_update,
1681 .ret_type = RET_INTEGER,
1682 .arg1_type = ARG_PTR_TO_CTX,
1683 .arg2_type = ARG_ANYTHING,
1686 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1688 return dev_forward_skb(dev, skb);
1691 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1692 struct sk_buff *skb)
1694 int ret = ____dev_forward_skb(dev, skb);
1698 ret = netif_rx(skb);
1704 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1708 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1709 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1716 __this_cpu_inc(xmit_recursion);
1717 ret = dev_queue_xmit(skb);
1718 __this_cpu_dec(xmit_recursion);
1723 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1726 /* skb->mac_len is not set on normal egress */
1727 unsigned int mlen = skb->network_header - skb->mac_header;
1729 __skb_pull(skb, mlen);
1731 /* At ingress, the mac header has already been pulled once.
1732 * At egress, skb_pospull_rcsum has to be done in case that
1733 * the skb is originated from ingress (i.e. a forwarded skb)
1734 * to ensure that rcsum starts at net header.
1736 if (!skb_at_tc_ingress(skb))
1737 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1738 skb_pop_mac_header(skb);
1739 skb_reset_mac_len(skb);
1740 return flags & BPF_F_INGRESS ?
1741 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1744 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1747 /* Verify that a link layer header is carried */
1748 if (unlikely(skb->mac_header >= skb->network_header)) {
1753 bpf_push_mac_rcsum(skb);
1754 return flags & BPF_F_INGRESS ?
1755 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1758 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1761 if (dev_is_mac_header_xmit(dev))
1762 return __bpf_redirect_common(skb, dev, flags);
1764 return __bpf_redirect_no_mac(skb, dev, flags);
1767 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1769 struct net_device *dev;
1770 struct sk_buff *clone;
1773 if (unlikely(flags & ~(BPF_F_INGRESS)))
1776 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1780 clone = skb_clone(skb, GFP_ATOMIC);
1781 if (unlikely(!clone))
1784 /* For direct write, we need to keep the invariant that the skbs
1785 * we're dealing with need to be uncloned. Should uncloning fail
1786 * here, we need to free the just generated clone to unclone once
1789 ret = bpf_try_make_head_writable(skb);
1790 if (unlikely(ret)) {
1795 return __bpf_redirect(clone, dev, flags);
1798 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1799 .func = bpf_clone_redirect,
1801 .ret_type = RET_INTEGER,
1802 .arg1_type = ARG_PTR_TO_CTX,
1803 .arg2_type = ARG_ANYTHING,
1804 .arg3_type = ARG_ANYTHING,
1807 struct redirect_info {
1810 struct bpf_map *map;
1811 struct bpf_map *map_to_flush;
1812 unsigned long map_owner;
1815 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1817 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1819 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1821 if (unlikely(flags & ~(BPF_F_INGRESS)))
1824 ri->ifindex = ifindex;
1827 return TC_ACT_REDIRECT;
1830 int skb_do_redirect(struct sk_buff *skb)
1832 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1833 struct net_device *dev;
1835 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1837 if (unlikely(!dev)) {
1842 return __bpf_redirect(skb, dev, ri->flags);
1845 static const struct bpf_func_proto bpf_redirect_proto = {
1846 .func = bpf_redirect,
1848 .ret_type = RET_INTEGER,
1849 .arg1_type = ARG_ANYTHING,
1850 .arg2_type = ARG_ANYTHING,
1853 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
1854 struct bpf_map *, map, u32, key, u64, flags)
1856 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1858 /* If user passes invalid input drop the packet. */
1859 if (unlikely(flags & ~(BPF_F_INGRESS)))
1863 tcb->bpf.flags = flags;
1869 struct sock *do_sk_redirect_map(struct sk_buff *skb)
1871 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1872 struct sock *sk = NULL;
1875 sk = __sock_map_lookup_elem(tcb->bpf.map, tcb->bpf.key);
1878 tcb->bpf.map = NULL;
1884 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
1885 .func = bpf_sk_redirect_map,
1887 .ret_type = RET_INTEGER,
1888 .arg1_type = ARG_PTR_TO_CTX,
1889 .arg2_type = ARG_CONST_MAP_PTR,
1890 .arg3_type = ARG_ANYTHING,
1891 .arg4_type = ARG_ANYTHING,
1894 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg_buff *, msg,
1895 struct bpf_map *, map, u32, key, u64, flags)
1897 /* If user passes invalid input drop the packet. */
1898 if (unlikely(flags & ~(BPF_F_INGRESS)))
1908 struct sock *do_msg_redirect_map(struct sk_msg_buff *msg)
1910 struct sock *sk = NULL;
1913 sk = __sock_map_lookup_elem(msg->map, msg->key);
1922 static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
1923 .func = bpf_msg_redirect_map,
1925 .ret_type = RET_INTEGER,
1926 .arg1_type = ARG_PTR_TO_CTX,
1927 .arg2_type = ARG_CONST_MAP_PTR,
1928 .arg3_type = ARG_ANYTHING,
1929 .arg4_type = ARG_ANYTHING,
1932 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg_buff *, msg, u32, bytes)
1934 msg->apply_bytes = bytes;
1938 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
1939 .func = bpf_msg_apply_bytes,
1941 .ret_type = RET_INTEGER,
1942 .arg1_type = ARG_PTR_TO_CTX,
1943 .arg2_type = ARG_ANYTHING,
1946 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg_buff *, msg, u32, bytes)
1948 msg->cork_bytes = bytes;
1952 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
1953 .func = bpf_msg_cork_bytes,
1955 .ret_type = RET_INTEGER,
1956 .arg1_type = ARG_PTR_TO_CTX,
1957 .arg2_type = ARG_ANYTHING,
1960 BPF_CALL_4(bpf_msg_pull_data,
1961 struct sk_msg_buff *, msg, u32, start, u32, end, u64, flags)
1963 unsigned int len = 0, offset = 0, copy = 0;
1964 struct scatterlist *sg = msg->sg_data;
1965 int first_sg, last_sg, i, shift;
1966 unsigned char *p, *to, *from;
1967 int bytes = end - start;
1970 if (unlikely(flags || end <= start))
1973 /* First find the starting scatterlist element */
1978 if (start < offset + len)
1981 if (i == MAX_SKB_FRAGS)
1983 } while (i != msg->sg_end);
1985 if (unlikely(start >= offset + len))
1988 if (!msg->sg_copy[i] && bytes <= len)
1993 /* At this point we need to linearize multiple scatterlist
1994 * elements or a single shared page. Either way we need to
1995 * copy into a linear buffer exclusively owned by BPF. Then
1996 * place the buffer in the scatterlist and fixup the original
1997 * entries by removing the entries now in the linear buffer
1998 * and shifting the remaining entries. For now we do not try
1999 * to copy partial entries to avoid complexity of running out
2000 * of sg_entry slots. The downside is reading a single byte
2001 * will copy the entire sg entry.
2004 copy += sg[i].length;
2006 if (i == MAX_SKB_FRAGS)
2010 } while (i != msg->sg_end);
2013 if (unlikely(copy < end - start))
2016 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC, get_order(copy));
2017 if (unlikely(!page))
2019 p = page_address(page);
2024 from = sg_virt(&sg[i]);
2028 memcpy(to, from, len);
2031 put_page(sg_page(&sg[i]));
2034 if (i == MAX_SKB_FRAGS)
2036 } while (i != last_sg);
2038 sg[first_sg].length = copy;
2039 sg_set_page(&sg[first_sg], page, copy, 0);
2041 /* To repair sg ring we need to shift entries. If we only
2042 * had a single entry though we can just replace it and
2043 * be done. Otherwise walk the ring and shift the entries.
2045 shift = last_sg - first_sg - 1;
2053 if (i + shift >= MAX_SKB_FRAGS)
2054 move_from = i + shift - MAX_SKB_FRAGS;
2056 move_from = i + shift;
2058 if (move_from == msg->sg_end)
2061 sg[i] = sg[move_from];
2062 sg[move_from].length = 0;
2063 sg[move_from].page_link = 0;
2064 sg[move_from].offset = 0;
2067 if (i == MAX_SKB_FRAGS)
2070 msg->sg_end -= shift;
2071 if (msg->sg_end < 0)
2072 msg->sg_end += MAX_SKB_FRAGS;
2074 msg->data = sg_virt(&sg[i]) + start - offset;
2075 msg->data_end = msg->data + bytes;
2080 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2081 .func = bpf_msg_pull_data,
2083 .ret_type = RET_INTEGER,
2084 .arg1_type = ARG_PTR_TO_CTX,
2085 .arg2_type = ARG_ANYTHING,
2086 .arg3_type = ARG_ANYTHING,
2087 .arg4_type = ARG_ANYTHING,
2090 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2092 return task_get_classid(skb);
2095 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2096 .func = bpf_get_cgroup_classid,
2098 .ret_type = RET_INTEGER,
2099 .arg1_type = ARG_PTR_TO_CTX,
2102 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2104 return dst_tclassid(skb);
2107 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2108 .func = bpf_get_route_realm,
2110 .ret_type = RET_INTEGER,
2111 .arg1_type = ARG_PTR_TO_CTX,
2114 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2116 /* If skb_clear_hash() was called due to mangling, we can
2117 * trigger SW recalculation here. Later access to hash
2118 * can then use the inline skb->hash via context directly
2119 * instead of calling this helper again.
2121 return skb_get_hash(skb);
2124 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2125 .func = bpf_get_hash_recalc,
2127 .ret_type = RET_INTEGER,
2128 .arg1_type = ARG_PTR_TO_CTX,
2131 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2133 /* After all direct packet write, this can be used once for
2134 * triggering a lazy recalc on next skb_get_hash() invocation.
2136 skb_clear_hash(skb);
2140 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2141 .func = bpf_set_hash_invalid,
2143 .ret_type = RET_INTEGER,
2144 .arg1_type = ARG_PTR_TO_CTX,
2147 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2149 /* Set user specified hash as L4(+), so that it gets returned
2150 * on skb_get_hash() call unless BPF prog later on triggers a
2153 __skb_set_sw_hash(skb, hash, true);
2157 static const struct bpf_func_proto bpf_set_hash_proto = {
2158 .func = bpf_set_hash,
2160 .ret_type = RET_INTEGER,
2161 .arg1_type = ARG_PTR_TO_CTX,
2162 .arg2_type = ARG_ANYTHING,
2165 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2170 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2171 vlan_proto != htons(ETH_P_8021AD)))
2172 vlan_proto = htons(ETH_P_8021Q);
2174 bpf_push_mac_rcsum(skb);
2175 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2176 bpf_pull_mac_rcsum(skb);
2178 bpf_compute_data_pointers(skb);
2182 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2183 .func = bpf_skb_vlan_push,
2185 .ret_type = RET_INTEGER,
2186 .arg1_type = ARG_PTR_TO_CTX,
2187 .arg2_type = ARG_ANYTHING,
2188 .arg3_type = ARG_ANYTHING,
2190 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
2192 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2196 bpf_push_mac_rcsum(skb);
2197 ret = skb_vlan_pop(skb);
2198 bpf_pull_mac_rcsum(skb);
2200 bpf_compute_data_pointers(skb);
2204 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2205 .func = bpf_skb_vlan_pop,
2207 .ret_type = RET_INTEGER,
2208 .arg1_type = ARG_PTR_TO_CTX,
2210 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
2212 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2214 /* Caller already did skb_cow() with len as headroom,
2215 * so no need to do it here.
2218 memmove(skb->data, skb->data + len, off);
2219 memset(skb->data + off, 0, len);
2221 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2222 * needed here as it does not change the skb->csum
2223 * result for checksum complete when summing over
2229 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2231 /* skb_ensure_writable() is not needed here, as we're
2232 * already working on an uncloned skb.
2234 if (unlikely(!pskb_may_pull(skb, off + len)))
2237 skb_postpull_rcsum(skb, skb->data + off, len);
2238 memmove(skb->data + len, skb->data, off);
2239 __skb_pull(skb, len);
2244 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2246 bool trans_same = skb->transport_header == skb->network_header;
2249 /* There's no need for __skb_push()/__skb_pull() pair to
2250 * get to the start of the mac header as we're guaranteed
2251 * to always start from here under eBPF.
2253 ret = bpf_skb_generic_push(skb, off, len);
2255 skb->mac_header -= len;
2256 skb->network_header -= len;
2258 skb->transport_header = skb->network_header;
2264 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2266 bool trans_same = skb->transport_header == skb->network_header;
2269 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2270 ret = bpf_skb_generic_pop(skb, off, len);
2272 skb->mac_header += len;
2273 skb->network_header += len;
2275 skb->transport_header = skb->network_header;
2281 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2283 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2284 u32 off = skb_mac_header_len(skb);
2287 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2288 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2291 ret = skb_cow(skb, len_diff);
2292 if (unlikely(ret < 0))
2295 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2296 if (unlikely(ret < 0))
2299 if (skb_is_gso(skb)) {
2300 struct skb_shared_info *shinfo = skb_shinfo(skb);
2302 /* SKB_GSO_TCPV4 needs to be changed into
2305 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2306 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2307 shinfo->gso_type |= SKB_GSO_TCPV6;
2310 /* Due to IPv6 header, MSS needs to be downgraded. */
2311 skb_decrease_gso_size(shinfo, len_diff);
2312 /* Header must be checked, and gso_segs recomputed. */
2313 shinfo->gso_type |= SKB_GSO_DODGY;
2314 shinfo->gso_segs = 0;
2317 skb->protocol = htons(ETH_P_IPV6);
2318 skb_clear_hash(skb);
2323 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2325 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2326 u32 off = skb_mac_header_len(skb);
2329 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2330 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2333 ret = skb_unclone(skb, GFP_ATOMIC);
2334 if (unlikely(ret < 0))
2337 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2338 if (unlikely(ret < 0))
2341 if (skb_is_gso(skb)) {
2342 struct skb_shared_info *shinfo = skb_shinfo(skb);
2344 /* SKB_GSO_TCPV6 needs to be changed into
2347 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2348 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2349 shinfo->gso_type |= SKB_GSO_TCPV4;
2352 /* Due to IPv4 header, MSS can be upgraded. */
2353 skb_increase_gso_size(shinfo, len_diff);
2354 /* Header must be checked, and gso_segs recomputed. */
2355 shinfo->gso_type |= SKB_GSO_DODGY;
2356 shinfo->gso_segs = 0;
2359 skb->protocol = htons(ETH_P_IP);
2360 skb_clear_hash(skb);
2365 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2367 __be16 from_proto = skb->protocol;
2369 if (from_proto == htons(ETH_P_IP) &&
2370 to_proto == htons(ETH_P_IPV6))
2371 return bpf_skb_proto_4_to_6(skb);
2373 if (from_proto == htons(ETH_P_IPV6) &&
2374 to_proto == htons(ETH_P_IP))
2375 return bpf_skb_proto_6_to_4(skb);
2380 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2385 if (unlikely(flags))
2388 /* General idea is that this helper does the basic groundwork
2389 * needed for changing the protocol, and eBPF program fills the
2390 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2391 * and other helpers, rather than passing a raw buffer here.
2393 * The rationale is to keep this minimal and without a need to
2394 * deal with raw packet data. F.e. even if we would pass buffers
2395 * here, the program still needs to call the bpf_lX_csum_replace()
2396 * helpers anyway. Plus, this way we keep also separation of
2397 * concerns, since f.e. bpf_skb_store_bytes() should only take
2400 * Currently, additional options and extension header space are
2401 * not supported, but flags register is reserved so we can adapt
2402 * that. For offloads, we mark packet as dodgy, so that headers
2403 * need to be verified first.
2405 ret = bpf_skb_proto_xlat(skb, proto);
2406 bpf_compute_data_pointers(skb);
2410 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2411 .func = bpf_skb_change_proto,
2413 .ret_type = RET_INTEGER,
2414 .arg1_type = ARG_PTR_TO_CTX,
2415 .arg2_type = ARG_ANYTHING,
2416 .arg3_type = ARG_ANYTHING,
2419 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2421 /* We only allow a restricted subset to be changed for now. */
2422 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2423 !skb_pkt_type_ok(pkt_type)))
2426 skb->pkt_type = pkt_type;
2430 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2431 .func = bpf_skb_change_type,
2433 .ret_type = RET_INTEGER,
2434 .arg1_type = ARG_PTR_TO_CTX,
2435 .arg2_type = ARG_ANYTHING,
2438 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2440 switch (skb->protocol) {
2441 case htons(ETH_P_IP):
2442 return sizeof(struct iphdr);
2443 case htons(ETH_P_IPV6):
2444 return sizeof(struct ipv6hdr);
2450 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2452 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2455 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2456 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2459 ret = skb_cow(skb, len_diff);
2460 if (unlikely(ret < 0))
2463 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2464 if (unlikely(ret < 0))
2467 if (skb_is_gso(skb)) {
2468 struct skb_shared_info *shinfo = skb_shinfo(skb);
2470 /* Due to header grow, MSS needs to be downgraded. */
2471 skb_decrease_gso_size(shinfo, len_diff);
2472 /* Header must be checked, and gso_segs recomputed. */
2473 shinfo->gso_type |= SKB_GSO_DODGY;
2474 shinfo->gso_segs = 0;
2480 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2482 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2485 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2486 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2489 ret = skb_unclone(skb, GFP_ATOMIC);
2490 if (unlikely(ret < 0))
2493 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2494 if (unlikely(ret < 0))
2497 if (skb_is_gso(skb)) {
2498 struct skb_shared_info *shinfo = skb_shinfo(skb);
2500 /* Due to header shrink, MSS can be upgraded. */
2501 skb_increase_gso_size(shinfo, len_diff);
2502 /* Header must be checked, and gso_segs recomputed. */
2503 shinfo->gso_type |= SKB_GSO_DODGY;
2504 shinfo->gso_segs = 0;
2510 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2512 return skb->dev->mtu + skb->dev->hard_header_len;
2515 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2517 bool trans_same = skb->transport_header == skb->network_header;
2518 u32 len_cur, len_diff_abs = abs(len_diff);
2519 u32 len_min = bpf_skb_net_base_len(skb);
2520 u32 len_max = __bpf_skb_max_len(skb);
2521 __be16 proto = skb->protocol;
2522 bool shrink = len_diff < 0;
2525 if (unlikely(len_diff_abs > 0xfffU))
2527 if (unlikely(proto != htons(ETH_P_IP) &&
2528 proto != htons(ETH_P_IPV6)))
2531 len_cur = skb->len - skb_network_offset(skb);
2532 if (skb_transport_header_was_set(skb) && !trans_same)
2533 len_cur = skb_network_header_len(skb);
2534 if ((shrink && (len_diff_abs >= len_cur ||
2535 len_cur - len_diff_abs < len_min)) ||
2536 (!shrink && (skb->len + len_diff_abs > len_max &&
2540 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2541 bpf_skb_net_grow(skb, len_diff_abs);
2543 bpf_compute_data_pointers(skb);
2547 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2548 u32, mode, u64, flags)
2550 if (unlikely(flags))
2552 if (likely(mode == BPF_ADJ_ROOM_NET))
2553 return bpf_skb_adjust_net(skb, len_diff);
2558 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2559 .func = bpf_skb_adjust_room,
2561 .ret_type = RET_INTEGER,
2562 .arg1_type = ARG_PTR_TO_CTX,
2563 .arg2_type = ARG_ANYTHING,
2564 .arg3_type = ARG_ANYTHING,
2565 .arg4_type = ARG_ANYTHING,
2568 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2570 u32 min_len = skb_network_offset(skb);
2572 if (skb_transport_header_was_set(skb))
2573 min_len = skb_transport_offset(skb);
2574 if (skb->ip_summed == CHECKSUM_PARTIAL)
2575 min_len = skb_checksum_start_offset(skb) +
2576 skb->csum_offset + sizeof(__sum16);
2580 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2582 unsigned int old_len = skb->len;
2585 ret = __skb_grow_rcsum(skb, new_len);
2587 memset(skb->data + old_len, 0, new_len - old_len);
2591 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2593 return __skb_trim_rcsum(skb, new_len);
2596 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2599 u32 max_len = __bpf_skb_max_len(skb);
2600 u32 min_len = __bpf_skb_min_len(skb);
2603 if (unlikely(flags || new_len > max_len || new_len < min_len))
2605 if (skb->encapsulation)
2608 /* The basic idea of this helper is that it's performing the
2609 * needed work to either grow or trim an skb, and eBPF program
2610 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2611 * bpf_lX_csum_replace() and others rather than passing a raw
2612 * buffer here. This one is a slow path helper and intended
2613 * for replies with control messages.
2615 * Like in bpf_skb_change_proto(), we want to keep this rather
2616 * minimal and without protocol specifics so that we are able
2617 * to separate concerns as in bpf_skb_store_bytes() should only
2618 * be the one responsible for writing buffers.
2620 * It's really expected to be a slow path operation here for
2621 * control message replies, so we're implicitly linearizing,
2622 * uncloning and drop offloads from the skb by this.
2624 ret = __bpf_try_make_writable(skb, skb->len);
2626 if (new_len > skb->len)
2627 ret = bpf_skb_grow_rcsum(skb, new_len);
2628 else if (new_len < skb->len)
2629 ret = bpf_skb_trim_rcsum(skb, new_len);
2630 if (!ret && skb_is_gso(skb))
2634 bpf_compute_data_pointers(skb);
2638 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2639 .func = bpf_skb_change_tail,
2641 .ret_type = RET_INTEGER,
2642 .arg1_type = ARG_PTR_TO_CTX,
2643 .arg2_type = ARG_ANYTHING,
2644 .arg3_type = ARG_ANYTHING,
2647 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2650 u32 max_len = __bpf_skb_max_len(skb);
2651 u32 new_len = skb->len + head_room;
2654 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2655 new_len < skb->len))
2658 ret = skb_cow(skb, head_room);
2660 /* Idea for this helper is that we currently only
2661 * allow to expand on mac header. This means that
2662 * skb->protocol network header, etc, stay as is.
2663 * Compared to bpf_skb_change_tail(), we're more
2664 * flexible due to not needing to linearize or
2665 * reset GSO. Intention for this helper is to be
2666 * used by an L3 skb that needs to push mac header
2667 * for redirection into L2 device.
2669 __skb_push(skb, head_room);
2670 memset(skb->data, 0, head_room);
2671 skb_reset_mac_header(skb);
2674 bpf_compute_data_pointers(skb);
2678 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2679 .func = bpf_skb_change_head,
2681 .ret_type = RET_INTEGER,
2682 .arg1_type = ARG_PTR_TO_CTX,
2683 .arg2_type = ARG_ANYTHING,
2684 .arg3_type = ARG_ANYTHING,
2687 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
2689 return xdp_data_meta_unsupported(xdp) ? 0 :
2690 xdp->data - xdp->data_meta;
2693 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2695 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
2696 unsigned long metalen = xdp_get_metalen(xdp);
2697 void *data_start = xdp_frame_end + metalen;
2698 void *data = xdp->data + offset;
2700 if (unlikely(data < data_start ||
2701 data > xdp->data_end - ETH_HLEN))
2705 memmove(xdp->data_meta + offset,
2706 xdp->data_meta, metalen);
2707 xdp->data_meta += offset;
2713 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2714 .func = bpf_xdp_adjust_head,
2716 .ret_type = RET_INTEGER,
2717 .arg1_type = ARG_PTR_TO_CTX,
2718 .arg2_type = ARG_ANYTHING,
2721 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
2723 void *data_end = xdp->data_end + offset;
2725 /* only shrinking is allowed for now. */
2726 if (unlikely(offset >= 0))
2729 if (unlikely(data_end < xdp->data + ETH_HLEN))
2732 xdp->data_end = data_end;
2737 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
2738 .func = bpf_xdp_adjust_tail,
2740 .ret_type = RET_INTEGER,
2741 .arg1_type = ARG_PTR_TO_CTX,
2742 .arg2_type = ARG_ANYTHING,
2745 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
2747 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
2748 void *meta = xdp->data_meta + offset;
2749 unsigned long metalen = xdp->data - meta;
2751 if (xdp_data_meta_unsupported(xdp))
2753 if (unlikely(meta < xdp_frame_end ||
2756 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
2760 xdp->data_meta = meta;
2765 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
2766 .func = bpf_xdp_adjust_meta,
2768 .ret_type = RET_INTEGER,
2769 .arg1_type = ARG_PTR_TO_CTX,
2770 .arg2_type = ARG_ANYTHING,
2773 static int __bpf_tx_xdp(struct net_device *dev,
2774 struct bpf_map *map,
2775 struct xdp_buff *xdp,
2778 struct xdp_frame *xdpf;
2781 if (!dev->netdev_ops->ndo_xdp_xmit) {
2785 xdpf = convert_to_xdp_frame(xdp);
2786 if (unlikely(!xdpf))
2789 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdpf);
2792 dev->netdev_ops->ndo_xdp_flush(dev);
2796 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
2797 struct bpf_map *map,
2798 struct xdp_buff *xdp,
2803 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
2804 struct net_device *dev = fwd;
2805 struct xdp_frame *xdpf;
2807 if (!dev->netdev_ops->ndo_xdp_xmit)
2810 xdpf = convert_to_xdp_frame(xdp);
2811 if (unlikely(!xdpf))
2814 /* TODO: move to inside map code instead, for bulk support
2815 * err = dev_map_enqueue(dev, xdp);
2817 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdpf);
2820 __dev_map_insert_ctx(map, index);
2822 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
2823 struct bpf_cpu_map_entry *rcpu = fwd;
2825 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
2828 __cpu_map_insert_ctx(map, index);
2833 void xdp_do_flush_map(void)
2835 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2836 struct bpf_map *map = ri->map_to_flush;
2838 ri->map_to_flush = NULL;
2840 switch (map->map_type) {
2841 case BPF_MAP_TYPE_DEVMAP:
2842 __dev_map_flush(map);
2844 case BPF_MAP_TYPE_CPUMAP:
2845 __cpu_map_flush(map);
2852 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
2854 static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
2856 switch (map->map_type) {
2857 case BPF_MAP_TYPE_DEVMAP:
2858 return __dev_map_lookup_elem(map, index);
2859 case BPF_MAP_TYPE_CPUMAP:
2860 return __cpu_map_lookup_elem(map, index);
2866 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
2869 return (unsigned long)xdp_prog->aux != aux;
2872 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
2873 struct bpf_prog *xdp_prog)
2875 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2876 unsigned long map_owner = ri->map_owner;
2877 struct bpf_map *map = ri->map;
2878 u32 index = ri->ifindex;
2886 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2892 fwd = __xdp_map_lookup_elem(map, index);
2897 if (ri->map_to_flush && ri->map_to_flush != map)
2900 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
2904 ri->map_to_flush = map;
2905 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
2908 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
2912 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
2913 struct bpf_prog *xdp_prog)
2915 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2916 struct net_device *fwd;
2917 u32 index = ri->ifindex;
2921 return xdp_do_redirect_map(dev, xdp, xdp_prog);
2923 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2925 if (unlikely(!fwd)) {
2930 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
2934 _trace_xdp_redirect(dev, xdp_prog, index);
2937 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2940 EXPORT_SYMBOL_GPL(xdp_do_redirect);
2942 static int __xdp_generic_ok_fwd_dev(struct sk_buff *skb, struct net_device *fwd)
2946 if (unlikely(!(fwd->flags & IFF_UP)))
2949 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
2956 static int xdp_do_generic_redirect_map(struct net_device *dev,
2957 struct sk_buff *skb,
2958 struct bpf_prog *xdp_prog)
2960 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2961 unsigned long map_owner = ri->map_owner;
2962 struct bpf_map *map = ri->map;
2963 struct net_device *fwd = NULL;
2964 u32 index = ri->ifindex;
2971 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2976 fwd = __xdp_map_lookup_elem(map, index);
2977 if (unlikely(!fwd)) {
2982 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
2983 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
2987 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
2992 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
2995 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
2999 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3000 struct bpf_prog *xdp_prog)
3002 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3003 u32 index = ri->ifindex;
3004 struct net_device *fwd;
3008 return xdp_do_generic_redirect_map(dev, skb, xdp_prog);
3011 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3012 if (unlikely(!fwd)) {
3017 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
3021 _trace_xdp_redirect(dev, xdp_prog, index);
3024 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3027 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3029 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3031 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3033 if (unlikely(flags))
3036 ri->ifindex = ifindex;
3041 return XDP_REDIRECT;
3044 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3045 .func = bpf_xdp_redirect,
3047 .ret_type = RET_INTEGER,
3048 .arg1_type = ARG_ANYTHING,
3049 .arg2_type = ARG_ANYTHING,
3052 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
3053 unsigned long, map_owner)
3055 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3057 if (unlikely(flags))
3060 ri->ifindex = ifindex;
3063 ri->map_owner = map_owner;
3065 return XDP_REDIRECT;
3068 /* Note, arg4 is hidden from users and populated by the verifier
3069 * with the right pointer.
3071 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3072 .func = bpf_xdp_redirect_map,
3074 .ret_type = RET_INTEGER,
3075 .arg1_type = ARG_CONST_MAP_PTR,
3076 .arg2_type = ARG_ANYTHING,
3077 .arg3_type = ARG_ANYTHING,
3080 bool bpf_helper_changes_pkt_data(void *func)
3082 if (func == bpf_skb_vlan_push ||
3083 func == bpf_skb_vlan_pop ||
3084 func == bpf_skb_store_bytes ||
3085 func == bpf_skb_change_proto ||
3086 func == bpf_skb_change_head ||
3087 func == bpf_skb_change_tail ||
3088 func == bpf_skb_adjust_room ||
3089 func == bpf_skb_pull_data ||
3090 func == bpf_clone_redirect ||
3091 func == bpf_l3_csum_replace ||
3092 func == bpf_l4_csum_replace ||
3093 func == bpf_xdp_adjust_head ||
3094 func == bpf_xdp_adjust_meta ||
3095 func == bpf_msg_pull_data ||
3096 func == bpf_xdp_adjust_tail)
3102 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3103 unsigned long off, unsigned long len)
3105 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3109 if (ptr != dst_buff)
3110 memcpy(dst_buff, ptr, len);
3115 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3116 u64, flags, void *, meta, u64, meta_size)
3118 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3120 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3122 if (unlikely(skb_size > skb->len))
3125 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3129 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3130 .func = bpf_skb_event_output,
3132 .ret_type = RET_INTEGER,
3133 .arg1_type = ARG_PTR_TO_CTX,
3134 .arg2_type = ARG_CONST_MAP_PTR,
3135 .arg3_type = ARG_ANYTHING,
3136 .arg4_type = ARG_PTR_TO_MEM,
3137 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3140 static unsigned short bpf_tunnel_key_af(u64 flags)
3142 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3145 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3146 u32, size, u64, flags)
3148 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3149 u8 compat[sizeof(struct bpf_tunnel_key)];
3153 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3157 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3161 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3164 case offsetof(struct bpf_tunnel_key, tunnel_label):
3165 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3167 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3168 /* Fixup deprecated structure layouts here, so we have
3169 * a common path later on.
3171 if (ip_tunnel_info_af(info) != AF_INET)
3174 to = (struct bpf_tunnel_key *)compat;
3181 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3182 to->tunnel_tos = info->key.tos;
3183 to->tunnel_ttl = info->key.ttl;
3185 if (flags & BPF_F_TUNINFO_IPV6) {
3186 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3187 sizeof(to->remote_ipv6));
3188 to->tunnel_label = be32_to_cpu(info->key.label);
3190 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3193 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3194 memcpy(to_orig, to, size);
3198 memset(to_orig, 0, size);
3202 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3203 .func = bpf_skb_get_tunnel_key,
3205 .ret_type = RET_INTEGER,
3206 .arg1_type = ARG_PTR_TO_CTX,
3207 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3208 .arg3_type = ARG_CONST_SIZE,
3209 .arg4_type = ARG_ANYTHING,
3212 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3214 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3217 if (unlikely(!info ||
3218 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3222 if (unlikely(size < info->options_len)) {
3227 ip_tunnel_info_opts_get(to, info);
3228 if (size > info->options_len)
3229 memset(to + info->options_len, 0, size - info->options_len);
3231 return info->options_len;
3233 memset(to, 0, size);
3237 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3238 .func = bpf_skb_get_tunnel_opt,
3240 .ret_type = RET_INTEGER,
3241 .arg1_type = ARG_PTR_TO_CTX,
3242 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3243 .arg3_type = ARG_CONST_SIZE,
3246 static struct metadata_dst __percpu *md_dst;
3248 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3249 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3251 struct metadata_dst *md = this_cpu_ptr(md_dst);
3252 u8 compat[sizeof(struct bpf_tunnel_key)];
3253 struct ip_tunnel_info *info;
3255 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3256 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3258 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3260 case offsetof(struct bpf_tunnel_key, tunnel_label):
3261 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3262 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3263 /* Fixup deprecated structure layouts here, so we have
3264 * a common path later on.
3266 memcpy(compat, from, size);
3267 memset(compat + size, 0, sizeof(compat) - size);
3268 from = (const struct bpf_tunnel_key *) compat;
3274 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3279 dst_hold((struct dst_entry *) md);
3280 skb_dst_set(skb, (struct dst_entry *) md);
3282 info = &md->u.tun_info;
3283 info->mode = IP_TUNNEL_INFO_TX;
3285 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3286 if (flags & BPF_F_DONT_FRAGMENT)
3287 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3288 if (flags & BPF_F_ZERO_CSUM_TX)
3289 info->key.tun_flags &= ~TUNNEL_CSUM;
3290 if (flags & BPF_F_SEQ_NUMBER)
3291 info->key.tun_flags |= TUNNEL_SEQ;
3293 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3294 info->key.tos = from->tunnel_tos;
3295 info->key.ttl = from->tunnel_ttl;
3297 if (flags & BPF_F_TUNINFO_IPV6) {
3298 info->mode |= IP_TUNNEL_INFO_IPV6;
3299 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3300 sizeof(from->remote_ipv6));
3301 info->key.label = cpu_to_be32(from->tunnel_label) &
3302 IPV6_FLOWLABEL_MASK;
3304 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3310 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3311 .func = bpf_skb_set_tunnel_key,
3313 .ret_type = RET_INTEGER,
3314 .arg1_type = ARG_PTR_TO_CTX,
3315 .arg2_type = ARG_PTR_TO_MEM,
3316 .arg3_type = ARG_CONST_SIZE,
3317 .arg4_type = ARG_ANYTHING,
3320 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3321 const u8 *, from, u32, size)
3323 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3324 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3326 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3328 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3331 ip_tunnel_info_opts_set(info, from, size);
3336 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3337 .func = bpf_skb_set_tunnel_opt,
3339 .ret_type = RET_INTEGER,
3340 .arg1_type = ARG_PTR_TO_CTX,
3341 .arg2_type = ARG_PTR_TO_MEM,
3342 .arg3_type = ARG_CONST_SIZE,
3345 static const struct bpf_func_proto *
3346 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3349 struct metadata_dst __percpu *tmp;
3351 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3356 if (cmpxchg(&md_dst, NULL, tmp))
3357 metadata_dst_free_percpu(tmp);
3361 case BPF_FUNC_skb_set_tunnel_key:
3362 return &bpf_skb_set_tunnel_key_proto;
3363 case BPF_FUNC_skb_set_tunnel_opt:
3364 return &bpf_skb_set_tunnel_opt_proto;
3370 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3373 struct bpf_array *array = container_of(map, struct bpf_array, map);
3374 struct cgroup *cgrp;
3377 sk = skb_to_full_sk(skb);
3378 if (!sk || !sk_fullsock(sk))
3380 if (unlikely(idx >= array->map.max_entries))
3383 cgrp = READ_ONCE(array->ptrs[idx]);
3384 if (unlikely(!cgrp))
3387 return sk_under_cgroup_hierarchy(sk, cgrp);
3390 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3391 .func = bpf_skb_under_cgroup,
3393 .ret_type = RET_INTEGER,
3394 .arg1_type = ARG_PTR_TO_CTX,
3395 .arg2_type = ARG_CONST_MAP_PTR,
3396 .arg3_type = ARG_ANYTHING,
3399 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3400 unsigned long off, unsigned long len)
3402 memcpy(dst_buff, src_buff + off, len);
3406 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3407 u64, flags, void *, meta, u64, meta_size)
3409 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3411 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3413 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3416 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3417 xdp_size, bpf_xdp_copy);
3420 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3421 .func = bpf_xdp_event_output,
3423 .ret_type = RET_INTEGER,
3424 .arg1_type = ARG_PTR_TO_CTX,
3425 .arg2_type = ARG_CONST_MAP_PTR,
3426 .arg3_type = ARG_ANYTHING,
3427 .arg4_type = ARG_PTR_TO_MEM,
3428 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3431 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3433 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3436 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3437 .func = bpf_get_socket_cookie,
3439 .ret_type = RET_INTEGER,
3440 .arg1_type = ARG_PTR_TO_CTX,
3443 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3445 struct sock *sk = sk_to_full_sk(skb->sk);
3448 if (!sk || !sk_fullsock(sk))
3450 kuid = sock_net_uid(sock_net(sk), sk);
3451 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3454 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3455 .func = bpf_get_socket_uid,
3457 .ret_type = RET_INTEGER,
3458 .arg1_type = ARG_PTR_TO_CTX,
3461 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3462 int, level, int, optname, char *, optval, int, optlen)
3464 struct sock *sk = bpf_sock->sk;
3468 if (!sk_fullsock(sk))
3471 if (level == SOL_SOCKET) {
3472 if (optlen != sizeof(int))
3474 val = *((int *)optval);
3476 /* Only some socketops are supported */
3479 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3480 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3483 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3484 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3486 case SO_MAX_PACING_RATE:
3487 sk->sk_max_pacing_rate = val;
3488 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3489 sk->sk_max_pacing_rate);
3492 sk->sk_priority = val;
3497 sk->sk_rcvlowat = val ? : 1;
3506 } else if (level == SOL_IP) {
3507 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3510 val = *((int *)optval);
3511 /* Only some options are supported */
3514 if (val < -1 || val > 0xff) {
3517 struct inet_sock *inet = inet_sk(sk);
3527 #if IS_ENABLED(CONFIG_IPV6)
3528 } else if (level == SOL_IPV6) {
3529 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3532 val = *((int *)optval);
3533 /* Only some options are supported */
3536 if (val < -1 || val > 0xff) {
3539 struct ipv6_pinfo *np = inet6_sk(sk);
3550 } else if (level == SOL_TCP &&
3551 sk->sk_prot->setsockopt == tcp_setsockopt) {
3552 if (optname == TCP_CONGESTION) {
3553 char name[TCP_CA_NAME_MAX];
3554 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3556 strncpy(name, optval, min_t(long, optlen,
3557 TCP_CA_NAME_MAX-1));
3558 name[TCP_CA_NAME_MAX-1] = 0;
3559 ret = tcp_set_congestion_control(sk, name, false,
3562 struct tcp_sock *tp = tcp_sk(sk);
3564 if (optlen != sizeof(int))
3567 val = *((int *)optval);
3568 /* Only some options are supported */
3571 if (val <= 0 || tp->data_segs_out > 0)
3576 case TCP_BPF_SNDCWND_CLAMP:
3580 tp->snd_cwnd_clamp = val;
3581 tp->snd_ssthresh = val;
3595 static const struct bpf_func_proto bpf_setsockopt_proto = {
3596 .func = bpf_setsockopt,
3598 .ret_type = RET_INTEGER,
3599 .arg1_type = ARG_PTR_TO_CTX,
3600 .arg2_type = ARG_ANYTHING,
3601 .arg3_type = ARG_ANYTHING,
3602 .arg4_type = ARG_PTR_TO_MEM,
3603 .arg5_type = ARG_CONST_SIZE,
3606 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3607 int, level, int, optname, char *, optval, int, optlen)
3609 struct sock *sk = bpf_sock->sk;
3611 if (!sk_fullsock(sk))
3615 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3616 if (optname == TCP_CONGESTION) {
3617 struct inet_connection_sock *icsk = inet_csk(sk);
3619 if (!icsk->icsk_ca_ops || optlen <= 1)
3621 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3622 optval[optlen - 1] = 0;
3626 } else if (level == SOL_IP) {
3627 struct inet_sock *inet = inet_sk(sk);
3629 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3632 /* Only some options are supported */
3635 *((int *)optval) = (int)inet->tos;
3640 #if IS_ENABLED(CONFIG_IPV6)
3641 } else if (level == SOL_IPV6) {
3642 struct ipv6_pinfo *np = inet6_sk(sk);
3644 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3647 /* Only some options are supported */
3650 *((int *)optval) = (int)np->tclass;
3662 memset(optval, 0, optlen);
3666 static const struct bpf_func_proto bpf_getsockopt_proto = {
3667 .func = bpf_getsockopt,
3669 .ret_type = RET_INTEGER,
3670 .arg1_type = ARG_PTR_TO_CTX,
3671 .arg2_type = ARG_ANYTHING,
3672 .arg3_type = ARG_ANYTHING,
3673 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
3674 .arg5_type = ARG_CONST_SIZE,
3677 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
3680 struct sock *sk = bpf_sock->sk;
3681 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
3683 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
3687 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
3689 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
3692 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
3693 .func = bpf_sock_ops_cb_flags_set,
3695 .ret_type = RET_INTEGER,
3696 .arg1_type = ARG_PTR_TO_CTX,
3697 .arg2_type = ARG_ANYTHING,
3700 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
3701 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
3703 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
3707 struct sock *sk = ctx->sk;
3710 /* Binding to port can be expensive so it's prohibited in the helper.
3711 * Only binding to IP is supported.
3714 if (addr->sa_family == AF_INET) {
3715 if (addr_len < sizeof(struct sockaddr_in))
3717 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3719 return __inet_bind(sk, addr, addr_len, true, false);
3720 #if IS_ENABLED(CONFIG_IPV6)
3721 } else if (addr->sa_family == AF_INET6) {
3722 if (addr_len < SIN6_LEN_RFC2133)
3724 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3726 /* ipv6_bpf_stub cannot be NULL, since it's called from
3727 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
3729 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
3730 #endif /* CONFIG_IPV6 */
3732 #endif /* CONFIG_INET */
3734 return -EAFNOSUPPORT;
3737 static const struct bpf_func_proto bpf_bind_proto = {
3740 .ret_type = RET_INTEGER,
3741 .arg1_type = ARG_PTR_TO_CTX,
3742 .arg2_type = ARG_PTR_TO_MEM,
3743 .arg3_type = ARG_CONST_SIZE,
3746 static const struct bpf_func_proto *
3747 bpf_base_func_proto(enum bpf_func_id func_id)
3750 case BPF_FUNC_map_lookup_elem:
3751 return &bpf_map_lookup_elem_proto;
3752 case BPF_FUNC_map_update_elem:
3753 return &bpf_map_update_elem_proto;
3754 case BPF_FUNC_map_delete_elem:
3755 return &bpf_map_delete_elem_proto;
3756 case BPF_FUNC_get_prandom_u32:
3757 return &bpf_get_prandom_u32_proto;
3758 case BPF_FUNC_get_smp_processor_id:
3759 return &bpf_get_raw_smp_processor_id_proto;
3760 case BPF_FUNC_get_numa_node_id:
3761 return &bpf_get_numa_node_id_proto;
3762 case BPF_FUNC_tail_call:
3763 return &bpf_tail_call_proto;
3764 case BPF_FUNC_ktime_get_ns:
3765 return &bpf_ktime_get_ns_proto;
3766 case BPF_FUNC_trace_printk:
3767 if (capable(CAP_SYS_ADMIN))
3768 return bpf_get_trace_printk_proto();
3774 static const struct bpf_func_proto *
3775 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
3778 /* inet and inet6 sockets are created in a process
3779 * context so there is always a valid uid/gid
3781 case BPF_FUNC_get_current_uid_gid:
3782 return &bpf_get_current_uid_gid_proto;
3784 return bpf_base_func_proto(func_id);
3788 static const struct bpf_func_proto *
3789 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
3792 /* inet and inet6 sockets are created in a process
3793 * context so there is always a valid uid/gid
3795 case BPF_FUNC_get_current_uid_gid:
3796 return &bpf_get_current_uid_gid_proto;
3798 switch (prog->expected_attach_type) {
3799 case BPF_CGROUP_INET4_CONNECT:
3800 case BPF_CGROUP_INET6_CONNECT:
3801 return &bpf_bind_proto;
3806 return bpf_base_func_proto(func_id);
3810 static const struct bpf_func_proto *
3811 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
3814 case BPF_FUNC_skb_load_bytes:
3815 return &bpf_skb_load_bytes_proto;
3816 case BPF_FUNC_get_socket_cookie:
3817 return &bpf_get_socket_cookie_proto;
3818 case BPF_FUNC_get_socket_uid:
3819 return &bpf_get_socket_uid_proto;
3821 return bpf_base_func_proto(func_id);
3825 static const struct bpf_func_proto *
3826 tc_cls_act_func_proto