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 <asm/cmpxchg.h>
47 #include <linux/filter.h>
48 #include <linux/ratelimit.h>
49 #include <linux/seccomp.h>
50 #include <linux/if_vlan.h>
51 #include <linux/bpf.h>
52 #include <net/sch_generic.h>
53 #include <net/cls_cgroup.h>
54 #include <net/dst_metadata.h>
56 #include <net/sock_reuseport.h>
57 #include <net/busy_poll.h>
59 #include <linux/bpf_trace.h>
62 * sk_filter_trim_cap - run a packet through a socket filter
63 * @sk: sock associated with &sk_buff
64 * @skb: buffer to filter
65 * @cap: limit on how short the eBPF program may trim the packet
67 * Run the eBPF program and then cut skb->data to correct size returned by
68 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
69 * than pkt_len we keep whole skb->data. This is the socket level
70 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
71 * be accepted or -EPERM if the packet should be tossed.
74 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
77 struct sk_filter *filter;
80 * If the skb was allocated from pfmemalloc reserves, only
81 * allow SOCK_MEMALLOC sockets to use it as this socket is
84 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
85 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
88 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
92 err = security_sock_rcv_skb(sk, skb);
97 filter = rcu_dereference(sk->sk_filter);
99 struct sock *save_sk = skb->sk;
100 unsigned int pkt_len;
103 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
105 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
111 EXPORT_SYMBOL(sk_filter_trim_cap);
113 BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
115 return skb_get_poff(skb);
118 BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
122 if (skb_is_nonlinear(skb))
125 if (skb->len < sizeof(struct nlattr))
128 if (a > skb->len - sizeof(struct nlattr))
131 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
133 return (void *) nla - (void *) skb->data;
138 BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
142 if (skb_is_nonlinear(skb))
145 if (skb->len < sizeof(struct nlattr))
148 if (a > skb->len - sizeof(struct nlattr))
151 nla = (struct nlattr *) &skb->data[a];
152 if (nla->nla_len > skb->len - a)
155 nla = nla_find_nested(nla, x);
157 return (void *) nla - (void *) skb->data;
162 BPF_CALL_0(__get_raw_cpu_id)
164 return raw_smp_processor_id();
167 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
168 .func = __get_raw_cpu_id,
170 .ret_type = RET_INTEGER,
173 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
174 struct bpf_insn *insn_buf)
176 struct bpf_insn *insn = insn_buf;
180 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
182 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
183 offsetof(struct sk_buff, mark));
187 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
188 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
189 #ifdef __BIG_ENDIAN_BITFIELD
190 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
195 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
197 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
198 offsetof(struct sk_buff, queue_mapping));
201 case SKF_AD_VLAN_TAG:
202 case SKF_AD_VLAN_TAG_PRESENT:
203 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
204 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
206 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
207 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
208 offsetof(struct sk_buff, vlan_tci));
209 if (skb_field == SKF_AD_VLAN_TAG) {
210 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
214 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
216 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
221 return insn - insn_buf;
224 static bool convert_bpf_extensions(struct sock_filter *fp,
225 struct bpf_insn **insnp)
227 struct bpf_insn *insn = *insnp;
231 case SKF_AD_OFF + SKF_AD_PROTOCOL:
232 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
234 /* A = *(u16 *) (CTX + offsetof(protocol)) */
235 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
236 offsetof(struct sk_buff, protocol));
237 /* A = ntohs(A) [emitting a nop or swap16] */
238 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
241 case SKF_AD_OFF + SKF_AD_PKTTYPE:
242 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
246 case SKF_AD_OFF + SKF_AD_IFINDEX:
247 case SKF_AD_OFF + SKF_AD_HATYPE:
248 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
249 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
251 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
252 BPF_REG_TMP, BPF_REG_CTX,
253 offsetof(struct sk_buff, dev));
254 /* if (tmp != 0) goto pc + 1 */
255 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
256 *insn++ = BPF_EXIT_INSN();
257 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
258 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
259 offsetof(struct net_device, ifindex));
261 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
262 offsetof(struct net_device, type));
265 case SKF_AD_OFF + SKF_AD_MARK:
266 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
270 case SKF_AD_OFF + SKF_AD_RXHASH:
271 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
273 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
274 offsetof(struct sk_buff, hash));
277 case SKF_AD_OFF + SKF_AD_QUEUE:
278 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
282 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
283 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
284 BPF_REG_A, BPF_REG_CTX, insn);
288 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
289 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
290 BPF_REG_A, BPF_REG_CTX, insn);
294 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
295 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
297 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
298 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
299 offsetof(struct sk_buff, vlan_proto));
300 /* A = ntohs(A) [emitting a nop or swap16] */
301 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
304 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
305 case SKF_AD_OFF + SKF_AD_NLATTR:
306 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
307 case SKF_AD_OFF + SKF_AD_CPU:
308 case SKF_AD_OFF + SKF_AD_RANDOM:
310 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
312 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
314 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
315 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
317 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
318 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
320 case SKF_AD_OFF + SKF_AD_NLATTR:
321 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
323 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
324 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
326 case SKF_AD_OFF + SKF_AD_CPU:
327 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
329 case SKF_AD_OFF + SKF_AD_RANDOM:
330 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
331 bpf_user_rnd_init_once();
336 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
338 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
342 /* This is just a dummy call to avoid letting the compiler
343 * evict __bpf_call_base() as an optimization. Placed here
344 * where no-one bothers.
346 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
355 * bpf_convert_filter - convert filter program
356 * @prog: the user passed filter program
357 * @len: the length of the user passed filter program
358 * @new_prog: allocated 'struct bpf_prog' or NULL
359 * @new_len: pointer to store length of converted program
361 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
362 * style extended BPF (eBPF).
363 * Conversion workflow:
365 * 1) First pass for calculating the new program length:
366 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
368 * 2) 2nd pass to remap in two passes: 1st pass finds new
369 * jump offsets, 2nd pass remapping:
370 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
372 static int bpf_convert_filter(struct sock_filter *prog, int len,
373 struct bpf_prog *new_prog, int *new_len)
375 int new_flen = 0, pass = 0, target, i, stack_off;
376 struct bpf_insn *new_insn, *first_insn = NULL;
377 struct sock_filter *fp;
381 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
382 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
384 if (len <= 0 || len > BPF_MAXINSNS)
388 first_insn = new_prog->insnsi;
389 addrs = kcalloc(len, sizeof(*addrs),
390 GFP_KERNEL | __GFP_NOWARN);
396 new_insn = first_insn;
399 /* Classic BPF related prologue emission. */
401 /* Classic BPF expects A and X to be reset first. These need
402 * to be guaranteed to be the first two instructions.
404 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
405 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
407 /* All programs must keep CTX in callee saved BPF_REG_CTX.
408 * In eBPF case it's done by the compiler, here we need to
409 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
411 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
416 for (i = 0; i < len; fp++, i++) {
417 struct bpf_insn tmp_insns[6] = { };
418 struct bpf_insn *insn = tmp_insns;
421 addrs[i] = new_insn - first_insn;
424 /* All arithmetic insns and skb loads map as-is. */
425 case BPF_ALU | BPF_ADD | BPF_X:
426 case BPF_ALU | BPF_ADD | BPF_K:
427 case BPF_ALU | BPF_SUB | BPF_X:
428 case BPF_ALU | BPF_SUB | BPF_K:
429 case BPF_ALU | BPF_AND | BPF_X:
430 case BPF_ALU | BPF_AND | BPF_K:
431 case BPF_ALU | BPF_OR | BPF_X:
432 case BPF_ALU | BPF_OR | BPF_K:
433 case BPF_ALU | BPF_LSH | BPF_X:
434 case BPF_ALU | BPF_LSH | BPF_K:
435 case BPF_ALU | BPF_RSH | BPF_X:
436 case BPF_ALU | BPF_RSH | BPF_K:
437 case BPF_ALU | BPF_XOR | BPF_X:
438 case BPF_ALU | BPF_XOR | BPF_K:
439 case BPF_ALU | BPF_MUL | BPF_X:
440 case BPF_ALU | BPF_MUL | BPF_K:
441 case BPF_ALU | BPF_DIV | BPF_X:
442 case BPF_ALU | BPF_DIV | BPF_K:
443 case BPF_ALU | BPF_MOD | BPF_X:
444 case BPF_ALU | BPF_MOD | BPF_K:
445 case BPF_ALU | BPF_NEG:
446 case BPF_LD | BPF_ABS | BPF_W:
447 case BPF_LD | BPF_ABS | BPF_H:
448 case BPF_LD | BPF_ABS | BPF_B:
449 case BPF_LD | BPF_IND | BPF_W:
450 case BPF_LD | BPF_IND | BPF_H:
451 case BPF_LD | BPF_IND | BPF_B:
452 /* Check for overloaded BPF extension and
453 * directly convert it if found, otherwise
454 * just move on with mapping.
456 if (BPF_CLASS(fp->code) == BPF_LD &&
457 BPF_MODE(fp->code) == BPF_ABS &&
458 convert_bpf_extensions(fp, &insn))
461 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
464 /* Jump transformation cannot use BPF block macros
465 * everywhere as offset calculation and target updates
466 * require a bit more work than the rest, i.e. jump
467 * opcodes map as-is, but offsets need adjustment.
470 #define BPF_EMIT_JMP \
472 if (target >= len || target < 0) \
474 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
475 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
476 insn->off -= insn - tmp_insns; \
479 case BPF_JMP | BPF_JA:
480 target = i + fp->k + 1;
481 insn->code = fp->code;
485 case BPF_JMP | BPF_JEQ | BPF_K:
486 case BPF_JMP | BPF_JEQ | BPF_X:
487 case BPF_JMP | BPF_JSET | BPF_K:
488 case BPF_JMP | BPF_JSET | BPF_X:
489 case BPF_JMP | BPF_JGT | BPF_K:
490 case BPF_JMP | BPF_JGT | BPF_X:
491 case BPF_JMP | BPF_JGE | BPF_K:
492 case BPF_JMP | BPF_JGE | BPF_X:
493 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
494 /* BPF immediates are signed, zero extend
495 * immediate into tmp register and use it
498 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
500 insn->dst_reg = BPF_REG_A;
501 insn->src_reg = BPF_REG_TMP;
504 insn->dst_reg = BPF_REG_A;
506 bpf_src = BPF_SRC(fp->code);
507 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
510 /* Common case where 'jump_false' is next insn. */
512 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
513 target = i + fp->jt + 1;
518 /* Convert some jumps when 'jump_true' is next insn. */
520 switch (BPF_OP(fp->code)) {
522 insn->code = BPF_JMP | BPF_JNE | bpf_src;
525 insn->code = BPF_JMP | BPF_JLE | bpf_src;
528 insn->code = BPF_JMP | BPF_JLT | bpf_src;
534 target = i + fp->jf + 1;
539 /* Other jumps are mapped into two insns: Jxx and JA. */
540 target = i + fp->jt + 1;
541 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
545 insn->code = BPF_JMP | BPF_JA;
546 target = i + fp->jf + 1;
550 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
551 case BPF_LDX | BPF_MSH | BPF_B:
553 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
554 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
555 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
557 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
559 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
561 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
563 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
566 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
567 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
569 case BPF_RET | BPF_A:
570 case BPF_RET | BPF_K:
571 if (BPF_RVAL(fp->code) == BPF_K)
572 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
574 *insn = BPF_EXIT_INSN();
577 /* Store to stack. */
580 stack_off = fp->k * 4 + 4;
581 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
582 BPF_ST ? BPF_REG_A : BPF_REG_X,
584 /* check_load_and_stores() verifies that classic BPF can
585 * load from stack only after write, so tracking
586 * stack_depth for ST|STX insns is enough
588 if (new_prog && new_prog->aux->stack_depth < stack_off)
589 new_prog->aux->stack_depth = stack_off;
592 /* Load from stack. */
593 case BPF_LD | BPF_MEM:
594 case BPF_LDX | BPF_MEM:
595 stack_off = fp->k * 4 + 4;
596 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
597 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
602 case BPF_LD | BPF_IMM:
603 case BPF_LDX | BPF_IMM:
604 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
605 BPF_REG_A : BPF_REG_X, fp->k);
609 case BPF_MISC | BPF_TAX:
610 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
614 case BPF_MISC | BPF_TXA:
615 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
618 /* A = skb->len or X = skb->len */
619 case BPF_LD | BPF_W | BPF_LEN:
620 case BPF_LDX | BPF_W | BPF_LEN:
621 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
622 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
623 offsetof(struct sk_buff, len));
626 /* Access seccomp_data fields. */
627 case BPF_LDX | BPF_ABS | BPF_W:
628 /* A = *(u32 *) (ctx + K) */
629 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
632 /* Unknown instruction. */
639 memcpy(new_insn, tmp_insns,
640 sizeof(*insn) * (insn - tmp_insns));
641 new_insn += insn - tmp_insns;
645 /* Only calculating new length. */
646 *new_len = new_insn - first_insn;
651 if (new_flen != new_insn - first_insn) {
652 new_flen = new_insn - first_insn;
659 BUG_ON(*new_len != new_flen);
668 * As we dont want to clear mem[] array for each packet going through
669 * __bpf_prog_run(), we check that filter loaded by user never try to read
670 * a cell if not previously written, and we check all branches to be sure
671 * a malicious user doesn't try to abuse us.
673 static int check_load_and_stores(const struct sock_filter *filter, int flen)
675 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
678 BUILD_BUG_ON(BPF_MEMWORDS > 16);
680 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
684 memset(masks, 0xff, flen * sizeof(*masks));
686 for (pc = 0; pc < flen; pc++) {
687 memvalid &= masks[pc];
689 switch (filter[pc].code) {
692 memvalid |= (1 << filter[pc].k);
694 case BPF_LD | BPF_MEM:
695 case BPF_LDX | BPF_MEM:
696 if (!(memvalid & (1 << filter[pc].k))) {
701 case BPF_JMP | BPF_JA:
702 /* A jump must set masks on target */
703 masks[pc + 1 + filter[pc].k] &= memvalid;
706 case BPF_JMP | BPF_JEQ | BPF_K:
707 case BPF_JMP | BPF_JEQ | BPF_X:
708 case BPF_JMP | BPF_JGE | BPF_K:
709 case BPF_JMP | BPF_JGE | BPF_X:
710 case BPF_JMP | BPF_JGT | BPF_K:
711 case BPF_JMP | BPF_JGT | BPF_X:
712 case BPF_JMP | BPF_JSET | BPF_K:
713 case BPF_JMP | BPF_JSET | BPF_X:
714 /* A jump must set masks on targets */
715 masks[pc + 1 + filter[pc].jt] &= memvalid;
716 masks[pc + 1 + filter[pc].jf] &= memvalid;
726 static bool chk_code_allowed(u16 code_to_probe)
728 static const bool codes[] = {
729 /* 32 bit ALU operations */
730 [BPF_ALU | BPF_ADD | BPF_K] = true,
731 [BPF_ALU | BPF_ADD | BPF_X] = true,
732 [BPF_ALU | BPF_SUB | BPF_K] = true,
733 [BPF_ALU | BPF_SUB | BPF_X] = true,
734 [BPF_ALU | BPF_MUL | BPF_K] = true,
735 [BPF_ALU | BPF_MUL | BPF_X] = true,
736 [BPF_ALU | BPF_DIV | BPF_K] = true,
737 [BPF_ALU | BPF_DIV | BPF_X] = true,
738 [BPF_ALU | BPF_MOD | BPF_K] = true,
739 [BPF_ALU | BPF_MOD | BPF_X] = true,
740 [BPF_ALU | BPF_AND | BPF_K] = true,
741 [BPF_ALU | BPF_AND | BPF_X] = true,
742 [BPF_ALU | BPF_OR | BPF_K] = true,
743 [BPF_ALU | BPF_OR | BPF_X] = true,
744 [BPF_ALU | BPF_XOR | BPF_K] = true,
745 [BPF_ALU | BPF_XOR | BPF_X] = true,
746 [BPF_ALU | BPF_LSH | BPF_K] = true,
747 [BPF_ALU | BPF_LSH | BPF_X] = true,
748 [BPF_ALU | BPF_RSH | BPF_K] = true,
749 [BPF_ALU | BPF_RSH | BPF_X] = true,
750 [BPF_ALU | BPF_NEG] = true,
751 /* Load instructions */
752 [BPF_LD | BPF_W | BPF_ABS] = true,
753 [BPF_LD | BPF_H | BPF_ABS] = true,
754 [BPF_LD | BPF_B | BPF_ABS] = true,
755 [BPF_LD | BPF_W | BPF_LEN] = true,
756 [BPF_LD | BPF_W | BPF_IND] = true,
757 [BPF_LD | BPF_H | BPF_IND] = true,
758 [BPF_LD | BPF_B | BPF_IND] = true,
759 [BPF_LD | BPF_IMM] = true,
760 [BPF_LD | BPF_MEM] = true,
761 [BPF_LDX | BPF_W | BPF_LEN] = true,
762 [BPF_LDX | BPF_B | BPF_MSH] = true,
763 [BPF_LDX | BPF_IMM] = true,
764 [BPF_LDX | BPF_MEM] = true,
765 /* Store instructions */
768 /* Misc instructions */
769 [BPF_MISC | BPF_TAX] = true,
770 [BPF_MISC | BPF_TXA] = true,
771 /* Return instructions */
772 [BPF_RET | BPF_K] = true,
773 [BPF_RET | BPF_A] = true,
774 /* Jump instructions */
775 [BPF_JMP | BPF_JA] = true,
776 [BPF_JMP | BPF_JEQ | BPF_K] = true,
777 [BPF_JMP | BPF_JEQ | BPF_X] = true,
778 [BPF_JMP | BPF_JGE | BPF_K] = true,
779 [BPF_JMP | BPF_JGE | BPF_X] = true,
780 [BPF_JMP | BPF_JGT | BPF_K] = true,
781 [BPF_JMP | BPF_JGT | BPF_X] = true,
782 [BPF_JMP | BPF_JSET | BPF_K] = true,
783 [BPF_JMP | BPF_JSET | BPF_X] = true,
786 if (code_to_probe >= ARRAY_SIZE(codes))
789 return codes[code_to_probe];
792 static bool bpf_check_basics_ok(const struct sock_filter *filter,
797 if (flen == 0 || flen > BPF_MAXINSNS)
804 * bpf_check_classic - verify socket filter code
805 * @filter: filter to verify
806 * @flen: length of filter
808 * Check the user's filter code. If we let some ugly
809 * filter code slip through kaboom! The filter must contain
810 * no references or jumps that are out of range, no illegal
811 * instructions, and must end with a RET instruction.
813 * All jumps are forward as they are not signed.
815 * Returns 0 if the rule set is legal or -EINVAL if not.
817 static int bpf_check_classic(const struct sock_filter *filter,
823 /* Check the filter code now */
824 for (pc = 0; pc < flen; pc++) {
825 const struct sock_filter *ftest = &filter[pc];
827 /* May we actually operate on this code? */
828 if (!chk_code_allowed(ftest->code))
831 /* Some instructions need special checks */
832 switch (ftest->code) {
833 case BPF_ALU | BPF_DIV | BPF_K:
834 case BPF_ALU | BPF_MOD | BPF_K:
835 /* Check for division by zero */
839 case BPF_ALU | BPF_LSH | BPF_K:
840 case BPF_ALU | BPF_RSH | BPF_K:
844 case BPF_LD | BPF_MEM:
845 case BPF_LDX | BPF_MEM:
848 /* Check for invalid memory addresses */
849 if (ftest->k >= BPF_MEMWORDS)
852 case BPF_JMP | BPF_JA:
853 /* Note, the large ftest->k might cause loops.
854 * Compare this with conditional jumps below,
855 * where offsets are limited. --ANK (981016)
857 if (ftest->k >= (unsigned int)(flen - pc - 1))
860 case BPF_JMP | BPF_JEQ | BPF_K:
861 case BPF_JMP | BPF_JEQ | BPF_X:
862 case BPF_JMP | BPF_JGE | BPF_K:
863 case BPF_JMP | BPF_JGE | BPF_X:
864 case BPF_JMP | BPF_JGT | BPF_K:
865 case BPF_JMP | BPF_JGT | BPF_X:
866 case BPF_JMP | BPF_JSET | BPF_K:
867 case BPF_JMP | BPF_JSET | BPF_X:
868 /* Both conditionals must be safe */
869 if (pc + ftest->jt + 1 >= flen ||
870 pc + ftest->jf + 1 >= flen)
873 case BPF_LD | BPF_W | BPF_ABS:
874 case BPF_LD | BPF_H | BPF_ABS:
875 case BPF_LD | BPF_B | BPF_ABS:
877 if (bpf_anc_helper(ftest) & BPF_ANC)
879 /* Ancillary operation unknown or unsupported */
880 if (anc_found == false && ftest->k >= SKF_AD_OFF)
885 /* Last instruction must be a RET code */
886 switch (filter[flen - 1].code) {
887 case BPF_RET | BPF_K:
888 case BPF_RET | BPF_A:
889 return check_load_and_stores(filter, flen);
895 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
896 const struct sock_fprog *fprog)
898 unsigned int fsize = bpf_classic_proglen(fprog);
899 struct sock_fprog_kern *fkprog;
901 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
905 fkprog = fp->orig_prog;
906 fkprog->len = fprog->len;
908 fkprog->filter = kmemdup(fp->insns, fsize,
909 GFP_KERNEL | __GFP_NOWARN);
910 if (!fkprog->filter) {
911 kfree(fp->orig_prog);
918 static void bpf_release_orig_filter(struct bpf_prog *fp)
920 struct sock_fprog_kern *fprog = fp->orig_prog;
923 kfree(fprog->filter);
928 static void __bpf_prog_release(struct bpf_prog *prog)
930 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
933 bpf_release_orig_filter(prog);
938 static void __sk_filter_release(struct sk_filter *fp)
940 __bpf_prog_release(fp->prog);
945 * sk_filter_release_rcu - Release a socket filter by rcu_head
946 * @rcu: rcu_head that contains the sk_filter to free
948 static void sk_filter_release_rcu(struct rcu_head *rcu)
950 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
952 __sk_filter_release(fp);
956 * sk_filter_release - release a socket filter
957 * @fp: filter to remove
959 * Remove a filter from a socket and release its resources.
961 static void sk_filter_release(struct sk_filter *fp)
963 if (refcount_dec_and_test(&fp->refcnt))
964 call_rcu(&fp->rcu, sk_filter_release_rcu);
967 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
969 u32 filter_size = bpf_prog_size(fp->prog->len);
971 atomic_sub(filter_size, &sk->sk_omem_alloc);
972 sk_filter_release(fp);
975 /* try to charge the socket memory if there is space available
976 * return true on success
978 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
980 u32 filter_size = bpf_prog_size(fp->prog->len);
982 /* same check as in sock_kmalloc() */
983 if (filter_size <= sysctl_optmem_max &&
984 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
985 atomic_add(filter_size, &sk->sk_omem_alloc);
991 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
993 if (!refcount_inc_not_zero(&fp->refcnt))
996 if (!__sk_filter_charge(sk, fp)) {
997 sk_filter_release(fp);
1003 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1005 struct sock_filter *old_prog;
1006 struct bpf_prog *old_fp;
1007 int err, new_len, old_len = fp->len;
1009 /* We are free to overwrite insns et al right here as it
1010 * won't be used at this point in time anymore internally
1011 * after the migration to the internal BPF instruction
1014 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1015 sizeof(struct bpf_insn));
1017 /* Conversion cannot happen on overlapping memory areas,
1018 * so we need to keep the user BPF around until the 2nd
1019 * pass. At this time, the user BPF is stored in fp->insns.
1021 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1022 GFP_KERNEL | __GFP_NOWARN);
1028 /* 1st pass: calculate the new program length. */
1029 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
1033 /* Expand fp for appending the new filter representation. */
1035 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1037 /* The old_fp is still around in case we couldn't
1038 * allocate new memory, so uncharge on that one.
1047 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1048 err = bpf_convert_filter(old_prog, old_len, fp, &new_len);
1050 /* 2nd bpf_convert_filter() can fail only if it fails
1051 * to allocate memory, remapping must succeed. Note,
1052 * that at this time old_fp has already been released
1057 /* We are guaranteed to never error here with cBPF to eBPF
1058 * transitions, since there's no issue with type compatibility
1059 * checks on program arrays.
1061 fp = bpf_prog_select_runtime(fp, &err);
1069 __bpf_prog_release(fp);
1070 return ERR_PTR(err);
1073 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1074 bpf_aux_classic_check_t trans)
1078 fp->bpf_func = NULL;
1081 err = bpf_check_classic(fp->insns, fp->len);
1083 __bpf_prog_release(fp);
1084 return ERR_PTR(err);
1087 /* There might be additional checks and transformations
1088 * needed on classic filters, f.e. in case of seccomp.
1091 err = trans(fp->insns, fp->len);
1093 __bpf_prog_release(fp);
1094 return ERR_PTR(err);
1098 /* Probe if we can JIT compile the filter and if so, do
1099 * the compilation of the filter.
1101 bpf_jit_compile(fp);
1103 /* JIT compiler couldn't process this filter, so do the
1104 * internal BPF translation for the optimized interpreter.
1107 fp = bpf_migrate_filter(fp);
1113 * bpf_prog_create - create an unattached filter
1114 * @pfp: the unattached filter that is created
1115 * @fprog: the filter program
1117 * Create a filter independent of any socket. We first run some
1118 * sanity checks on it to make sure it does not explode on us later.
1119 * If an error occurs or there is insufficient memory for the filter
1120 * a negative errno code is returned. On success the return is zero.
1122 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1124 unsigned int fsize = bpf_classic_proglen(fprog);
1125 struct bpf_prog *fp;
1127 /* Make sure new filter is there and in the right amounts. */
1128 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1131 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1135 memcpy(fp->insns, fprog->filter, fsize);
1137 fp->len = fprog->len;
1138 /* Since unattached filters are not copied back to user
1139 * space through sk_get_filter(), we do not need to hold
1140 * a copy here, and can spare us the work.
1142 fp->orig_prog = NULL;
1144 /* bpf_prepare_filter() already takes care of freeing
1145 * memory in case something goes wrong.
1147 fp = bpf_prepare_filter(fp, NULL);
1154 EXPORT_SYMBOL_GPL(bpf_prog_create);
1157 * bpf_prog_create_from_user - create an unattached filter from user buffer
1158 * @pfp: the unattached filter that is created
1159 * @fprog: the filter program
1160 * @trans: post-classic verifier transformation handler
1161 * @save_orig: save classic BPF program
1163 * This function effectively does the same as bpf_prog_create(), only
1164 * that it builds up its insns buffer from user space provided buffer.
1165 * It also allows for passing a bpf_aux_classic_check_t handler.
1167 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1168 bpf_aux_classic_check_t trans, bool save_orig)
1170 unsigned int fsize = bpf_classic_proglen(fprog);
1171 struct bpf_prog *fp;
1174 /* Make sure new filter is there and in the right amounts. */
1175 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1178 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1182 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1183 __bpf_prog_free(fp);
1187 fp->len = fprog->len;
1188 fp->orig_prog = NULL;
1191 err = bpf_prog_store_orig_filter(fp, fprog);
1193 __bpf_prog_free(fp);
1198 /* bpf_prepare_filter() already takes care of freeing
1199 * memory in case something goes wrong.
1201 fp = bpf_prepare_filter(fp, trans);
1208 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1210 void bpf_prog_destroy(struct bpf_prog *fp)
1212 __bpf_prog_release(fp);
1214 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1216 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1218 struct sk_filter *fp, *old_fp;
1220 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1226 if (!__sk_filter_charge(sk, fp)) {
1230 refcount_set(&fp->refcnt, 1);
1232 old_fp = rcu_dereference_protected(sk->sk_filter,
1233 lockdep_sock_is_held(sk));
1234 rcu_assign_pointer(sk->sk_filter, fp);
1237 sk_filter_uncharge(sk, old_fp);
1242 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1244 struct bpf_prog *old_prog;
1247 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1250 if (sk_unhashed(sk) && sk->sk_reuseport) {
1251 err = reuseport_alloc(sk);
1254 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1255 /* The socket wasn't bound with SO_REUSEPORT */
1259 old_prog = reuseport_attach_prog(sk, prog);
1261 bpf_prog_destroy(old_prog);
1267 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1269 unsigned int fsize = bpf_classic_proglen(fprog);
1270 struct bpf_prog *prog;
1273 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1274 return ERR_PTR(-EPERM);
1276 /* Make sure new filter is there and in the right amounts. */
1277 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1278 return ERR_PTR(-EINVAL);
1280 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1282 return ERR_PTR(-ENOMEM);
1284 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1285 __bpf_prog_free(prog);
1286 return ERR_PTR(-EFAULT);
1289 prog->len = fprog->len;
1291 err = bpf_prog_store_orig_filter(prog, fprog);
1293 __bpf_prog_free(prog);
1294 return ERR_PTR(-ENOMEM);
1297 /* bpf_prepare_filter() already takes care of freeing
1298 * memory in case something goes wrong.
1300 return bpf_prepare_filter(prog, NULL);
1304 * sk_attach_filter - attach a socket filter
1305 * @fprog: the filter program
1306 * @sk: the socket to use
1308 * Attach the user's filter code. We first run some sanity checks on
1309 * it to make sure it does not explode on us later. If an error
1310 * occurs or there is insufficient memory for the filter a negative
1311 * errno code is returned. On success the return is zero.
1313 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1315 struct bpf_prog *prog = __get_filter(fprog, sk);
1319 return PTR_ERR(prog);
1321 err = __sk_attach_prog(prog, sk);
1323 __bpf_prog_release(prog);
1329 EXPORT_SYMBOL_GPL(sk_attach_filter);
1331 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1333 struct bpf_prog *prog = __get_filter(fprog, sk);
1337 return PTR_ERR(prog);
1339 err = __reuseport_attach_prog(prog, sk);
1341 __bpf_prog_release(prog);
1348 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1350 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1351 return ERR_PTR(-EPERM);
1353 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1356 int sk_attach_bpf(u32 ufd, struct sock *sk)
1358 struct bpf_prog *prog = __get_bpf(ufd, sk);
1362 return PTR_ERR(prog);
1364 err = __sk_attach_prog(prog, sk);
1373 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1375 struct bpf_prog *prog = __get_bpf(ufd, sk);
1379 return PTR_ERR(prog);
1381 err = __reuseport_attach_prog(prog, sk);
1390 struct bpf_scratchpad {
1392 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1393 u8 buff[MAX_BPF_STACK];
1397 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1399 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1400 unsigned int write_len)
1402 return skb_ensure_writable(skb, write_len);
1405 static inline int bpf_try_make_writable(struct sk_buff *skb,
1406 unsigned int write_len)
1408 int err = __bpf_try_make_writable(skb, write_len);
1410 bpf_compute_data_pointers(skb);
1414 static int bpf_try_make_head_writable(struct sk_buff *skb)
1416 return bpf_try_make_writable(skb, skb_headlen(skb));
1419 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1421 if (skb_at_tc_ingress(skb))
1422 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1425 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1427 if (skb_at_tc_ingress(skb))
1428 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1431 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1432 const void *, from, u32, len, u64, flags)
1436 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1438 if (unlikely(offset > 0xffff))
1440 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1443 ptr = skb->data + offset;
1444 if (flags & BPF_F_RECOMPUTE_CSUM)
1445 __skb_postpull_rcsum(skb, ptr, len, offset);
1447 memcpy(ptr, from, len);
1449 if (flags & BPF_F_RECOMPUTE_CSUM)
1450 __skb_postpush_rcsum(skb, ptr, len, offset);
1451 if (flags & BPF_F_INVALIDATE_HASH)
1452 skb_clear_hash(skb);
1457 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1458 .func = bpf_skb_store_bytes,
1460 .ret_type = RET_INTEGER,
1461 .arg1_type = ARG_PTR_TO_CTX,
1462 .arg2_type = ARG_ANYTHING,
1463 .arg3_type = ARG_PTR_TO_MEM,
1464 .arg4_type = ARG_CONST_SIZE,
1465 .arg5_type = ARG_ANYTHING,
1468 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1469 void *, to, u32, len)
1473 if (unlikely(offset > 0xffff))
1476 ptr = skb_header_pointer(skb, offset, len, to);
1480 memcpy(to, ptr, len);
1488 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1489 .func = bpf_skb_load_bytes,
1491 .ret_type = RET_INTEGER,
1492 .arg1_type = ARG_PTR_TO_CTX,
1493 .arg2_type = ARG_ANYTHING,
1494 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1495 .arg4_type = ARG_CONST_SIZE,
1498 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1500 /* Idea is the following: should the needed direct read/write
1501 * test fail during runtime, we can pull in more data and redo
1502 * again, since implicitly, we invalidate previous checks here.
1504 * Or, since we know how much we need to make read/writeable,
1505 * this can be done once at the program beginning for direct
1506 * access case. By this we overcome limitations of only current
1507 * headroom being accessible.
1509 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1512 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1513 .func = bpf_skb_pull_data,
1515 .ret_type = RET_INTEGER,
1516 .arg1_type = ARG_PTR_TO_CTX,
1517 .arg2_type = ARG_ANYTHING,
1520 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1521 u64, from, u64, to, u64, flags)
1525 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1527 if (unlikely(offset > 0xffff || offset & 1))
1529 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1532 ptr = (__sum16 *)(skb->data + offset);
1533 switch (flags & BPF_F_HDR_FIELD_MASK) {
1535 if (unlikely(from != 0))
1538 csum_replace_by_diff(ptr, to);
1541 csum_replace2(ptr, from, to);
1544 csum_replace4(ptr, from, to);
1553 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1554 .func = bpf_l3_csum_replace,
1556 .ret_type = RET_INTEGER,
1557 .arg1_type = ARG_PTR_TO_CTX,
1558 .arg2_type = ARG_ANYTHING,
1559 .arg3_type = ARG_ANYTHING,
1560 .arg4_type = ARG_ANYTHING,
1561 .arg5_type = ARG_ANYTHING,
1564 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1565 u64, from, u64, to, u64, flags)
1567 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1568 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1569 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1572 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1573 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1575 if (unlikely(offset > 0xffff || offset & 1))
1577 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1580 ptr = (__sum16 *)(skb->data + offset);
1581 if (is_mmzero && !do_mforce && !*ptr)
1584 switch (flags & BPF_F_HDR_FIELD_MASK) {
1586 if (unlikely(from != 0))
1589 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1592 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1595 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1601 if (is_mmzero && !*ptr)
1602 *ptr = CSUM_MANGLED_0;
1606 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1607 .func = bpf_l4_csum_replace,
1609 .ret_type = RET_INTEGER,
1610 .arg1_type = ARG_PTR_TO_CTX,
1611 .arg2_type = ARG_ANYTHING,
1612 .arg3_type = ARG_ANYTHING,
1613 .arg4_type = ARG_ANYTHING,
1614 .arg5_type = ARG_ANYTHING,
1617 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1618 __be32 *, to, u32, to_size, __wsum, seed)
1620 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1621 u32 diff_size = from_size + to_size;
1624 /* This is quite flexible, some examples:
1626 * from_size == 0, to_size > 0, seed := csum --> pushing data
1627 * from_size > 0, to_size == 0, seed := csum --> pulling data
1628 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1630 * Even for diffing, from_size and to_size don't need to be equal.
1632 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1633 diff_size > sizeof(sp->diff)))
1636 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1637 sp->diff[j] = ~from[i];
1638 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1639 sp->diff[j] = to[i];
1641 return csum_partial(sp->diff, diff_size, seed);
1644 static const struct bpf_func_proto bpf_csum_diff_proto = {
1645 .func = bpf_csum_diff,
1648 .ret_type = RET_INTEGER,
1649 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1650 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1651 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1652 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1653 .arg5_type = ARG_ANYTHING,
1656 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1658 /* The interface is to be used in combination with bpf_csum_diff()
1659 * for direct packet writes. csum rotation for alignment as well
1660 * as emulating csum_sub() can be done from the eBPF program.
1662 if (skb->ip_summed == CHECKSUM_COMPLETE)
1663 return (skb->csum = csum_add(skb->csum, csum));
1668 static const struct bpf_func_proto bpf_csum_update_proto = {
1669 .func = bpf_csum_update,
1671 .ret_type = RET_INTEGER,
1672 .arg1_type = ARG_PTR_TO_CTX,
1673 .arg2_type = ARG_ANYTHING,
1676 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1678 return dev_forward_skb(dev, skb);
1681 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1682 struct sk_buff *skb)
1684 int ret = ____dev_forward_skb(dev, skb);
1688 ret = netif_rx(skb);
1694 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1698 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1699 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1706 __this_cpu_inc(xmit_recursion);
1707 ret = dev_queue_xmit(skb);
1708 __this_cpu_dec(xmit_recursion);
1713 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1716 /* skb->mac_len is not set on normal egress */
1717 unsigned int mlen = skb->network_header - skb->mac_header;
1719 __skb_pull(skb, mlen);
1721 /* At ingress, the mac header has already been pulled once.
1722 * At egress, skb_pospull_rcsum has to be done in case that
1723 * the skb is originated from ingress (i.e. a forwarded skb)
1724 * to ensure that rcsum starts at net header.
1726 if (!skb_at_tc_ingress(skb))
1727 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1728 skb_pop_mac_header(skb);
1729 skb_reset_mac_len(skb);
1730 return flags & BPF_F_INGRESS ?
1731 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1734 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1737 /* Verify that a link layer header is carried */
1738 if (unlikely(skb->mac_header >= skb->network_header)) {
1743 bpf_push_mac_rcsum(skb);
1744 return flags & BPF_F_INGRESS ?
1745 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1748 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1751 if (dev_is_mac_header_xmit(dev))
1752 return __bpf_redirect_common(skb, dev, flags);
1754 return __bpf_redirect_no_mac(skb, dev, flags);
1757 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1759 struct net_device *dev;
1760 struct sk_buff *clone;
1763 if (unlikely(flags & ~(BPF_F_INGRESS)))
1766 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1770 clone = skb_clone(skb, GFP_ATOMIC);
1771 if (unlikely(!clone))
1774 /* For direct write, we need to keep the invariant that the skbs
1775 * we're dealing with need to be uncloned. Should uncloning fail
1776 * here, we need to free the just generated clone to unclone once
1779 ret = bpf_try_make_head_writable(skb);
1780 if (unlikely(ret)) {
1785 return __bpf_redirect(clone, dev, flags);
1788 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1789 .func = bpf_clone_redirect,
1791 .ret_type = RET_INTEGER,
1792 .arg1_type = ARG_PTR_TO_CTX,
1793 .arg2_type = ARG_ANYTHING,
1794 .arg3_type = ARG_ANYTHING,
1797 struct redirect_info {
1800 struct bpf_map *map;
1801 struct bpf_map *map_to_flush;
1802 unsigned long map_owner;
1805 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1807 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1809 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1811 if (unlikely(flags & ~(BPF_F_INGRESS)))
1814 ri->ifindex = ifindex;
1817 return TC_ACT_REDIRECT;
1820 int skb_do_redirect(struct sk_buff *skb)
1822 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1823 struct net_device *dev;
1825 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1827 if (unlikely(!dev)) {
1832 return __bpf_redirect(skb, dev, ri->flags);
1835 static const struct bpf_func_proto bpf_redirect_proto = {
1836 .func = bpf_redirect,
1838 .ret_type = RET_INTEGER,
1839 .arg1_type = ARG_ANYTHING,
1840 .arg2_type = ARG_ANYTHING,
1843 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
1844 struct bpf_map *, map, u32, key, u64, flags)
1846 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1848 /* If user passes invalid input drop the packet. */
1849 if (unlikely(flags))
1853 tcb->bpf.flags = flags;
1859 struct sock *do_sk_redirect_map(struct sk_buff *skb)
1861 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
1862 struct sock *sk = NULL;
1865 sk = __sock_map_lookup_elem(tcb->bpf.map, tcb->bpf.key);
1868 tcb->bpf.map = NULL;
1874 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
1875 .func = bpf_sk_redirect_map,
1877 .ret_type = RET_INTEGER,
1878 .arg1_type = ARG_PTR_TO_CTX,
1879 .arg2_type = ARG_CONST_MAP_PTR,
1880 .arg3_type = ARG_ANYTHING,
1881 .arg4_type = ARG_ANYTHING,
1884 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
1886 return task_get_classid(skb);
1889 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1890 .func = bpf_get_cgroup_classid,
1892 .ret_type = RET_INTEGER,
1893 .arg1_type = ARG_PTR_TO_CTX,
1896 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
1898 return dst_tclassid(skb);
1901 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1902 .func = bpf_get_route_realm,
1904 .ret_type = RET_INTEGER,
1905 .arg1_type = ARG_PTR_TO_CTX,
1908 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
1910 /* If skb_clear_hash() was called due to mangling, we can
1911 * trigger SW recalculation here. Later access to hash
1912 * can then use the inline skb->hash via context directly
1913 * instead of calling this helper again.
1915 return skb_get_hash(skb);
1918 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1919 .func = bpf_get_hash_recalc,
1921 .ret_type = RET_INTEGER,
1922 .arg1_type = ARG_PTR_TO_CTX,
1925 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1927 /* After all direct packet write, this can be used once for
1928 * triggering a lazy recalc on next skb_get_hash() invocation.
1930 skb_clear_hash(skb);
1934 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1935 .func = bpf_set_hash_invalid,
1937 .ret_type = RET_INTEGER,
1938 .arg1_type = ARG_PTR_TO_CTX,
1941 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
1943 /* Set user specified hash as L4(+), so that it gets returned
1944 * on skb_get_hash() call unless BPF prog later on triggers a
1947 __skb_set_sw_hash(skb, hash, true);
1951 static const struct bpf_func_proto bpf_set_hash_proto = {
1952 .func = bpf_set_hash,
1954 .ret_type = RET_INTEGER,
1955 .arg1_type = ARG_PTR_TO_CTX,
1956 .arg2_type = ARG_ANYTHING,
1959 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1964 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1965 vlan_proto != htons(ETH_P_8021AD)))
1966 vlan_proto = htons(ETH_P_8021Q);
1968 bpf_push_mac_rcsum(skb);
1969 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1970 bpf_pull_mac_rcsum(skb);
1972 bpf_compute_data_pointers(skb);
1976 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1977 .func = bpf_skb_vlan_push,
1979 .ret_type = RET_INTEGER,
1980 .arg1_type = ARG_PTR_TO_CTX,
1981 .arg2_type = ARG_ANYTHING,
1982 .arg3_type = ARG_ANYTHING,
1984 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1986 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
1990 bpf_push_mac_rcsum(skb);
1991 ret = skb_vlan_pop(skb);
1992 bpf_pull_mac_rcsum(skb);
1994 bpf_compute_data_pointers(skb);
1998 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1999 .func = bpf_skb_vlan_pop,
2001 .ret_type = RET_INTEGER,
2002 .arg1_type = ARG_PTR_TO_CTX,
2004 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
2006 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2008 /* Caller already did skb_cow() with len as headroom,
2009 * so no need to do it here.
2012 memmove(skb->data, skb->data + len, off);
2013 memset(skb->data + off, 0, len);
2015 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2016 * needed here as it does not change the skb->csum
2017 * result for checksum complete when summing over
2023 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2025 /* skb_ensure_writable() is not needed here, as we're
2026 * already working on an uncloned skb.
2028 if (unlikely(!pskb_may_pull(skb, off + len)))
2031 skb_postpull_rcsum(skb, skb->data + off, len);
2032 memmove(skb->data + len, skb->data, off);
2033 __skb_pull(skb, len);
2038 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2040 bool trans_same = skb->transport_header == skb->network_header;
2043 /* There's no need for __skb_push()/__skb_pull() pair to
2044 * get to the start of the mac header as we're guaranteed
2045 * to always start from here under eBPF.
2047 ret = bpf_skb_generic_push(skb, off, len);
2049 skb->mac_header -= len;
2050 skb->network_header -= len;
2052 skb->transport_header = skb->network_header;
2058 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2060 bool trans_same = skb->transport_header == skb->network_header;
2063 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2064 ret = bpf_skb_generic_pop(skb, off, len);
2066 skb->mac_header += len;
2067 skb->network_header += len;
2069 skb->transport_header = skb->network_header;
2075 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2077 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2078 u32 off = skb_mac_header_len(skb);
2081 ret = skb_cow(skb, len_diff);
2082 if (unlikely(ret < 0))
2085 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2086 if (unlikely(ret < 0))
2089 if (skb_is_gso(skb)) {
2090 /* SKB_GSO_TCPV4 needs to be changed into
2093 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2094 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
2095 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
2098 /* Due to IPv6 header, MSS needs to be downgraded. */
2099 skb_shinfo(skb)->gso_size -= len_diff;
2100 /* Header must be checked, and gso_segs recomputed. */
2101 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2102 skb_shinfo(skb)->gso_segs = 0;
2105 skb->protocol = htons(ETH_P_IPV6);
2106 skb_clear_hash(skb);
2111 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2113 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2114 u32 off = skb_mac_header_len(skb);
2117 ret = skb_unclone(skb, GFP_ATOMIC);
2118 if (unlikely(ret < 0))
2121 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2122 if (unlikely(ret < 0))
2125 if (skb_is_gso(skb)) {
2126 /* SKB_GSO_TCPV6 needs to be changed into
2129 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2130 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2131 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2134 /* Due to IPv4 header, MSS can be upgraded. */
2135 skb_shinfo(skb)->gso_size += len_diff;
2136 /* Header must be checked, and gso_segs recomputed. */
2137 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2138 skb_shinfo(skb)->gso_segs = 0;
2141 skb->protocol = htons(ETH_P_IP);
2142 skb_clear_hash(skb);
2147 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2149 __be16 from_proto = skb->protocol;
2151 if (from_proto == htons(ETH_P_IP) &&
2152 to_proto == htons(ETH_P_IPV6))
2153 return bpf_skb_proto_4_to_6(skb);
2155 if (from_proto == htons(ETH_P_IPV6) &&
2156 to_proto == htons(ETH_P_IP))
2157 return bpf_skb_proto_6_to_4(skb);
2162 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2167 if (unlikely(flags))
2170 /* General idea is that this helper does the basic groundwork
2171 * needed for changing the protocol, and eBPF program fills the
2172 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2173 * and other helpers, rather than passing a raw buffer here.
2175 * The rationale is to keep this minimal and without a need to
2176 * deal with raw packet data. F.e. even if we would pass buffers
2177 * here, the program still needs to call the bpf_lX_csum_replace()
2178 * helpers anyway. Plus, this way we keep also separation of
2179 * concerns, since f.e. bpf_skb_store_bytes() should only take
2182 * Currently, additional options and extension header space are
2183 * not supported, but flags register is reserved so we can adapt
2184 * that. For offloads, we mark packet as dodgy, so that headers
2185 * need to be verified first.
2187 ret = bpf_skb_proto_xlat(skb, proto);
2188 bpf_compute_data_pointers(skb);
2192 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2193 .func = bpf_skb_change_proto,
2195 .ret_type = RET_INTEGER,
2196 .arg1_type = ARG_PTR_TO_CTX,
2197 .arg2_type = ARG_ANYTHING,
2198 .arg3_type = ARG_ANYTHING,
2201 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2203 /* We only allow a restricted subset to be changed for now. */
2204 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2205 !skb_pkt_type_ok(pkt_type)))
2208 skb->pkt_type = pkt_type;
2212 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2213 .func = bpf_skb_change_type,
2215 .ret_type = RET_INTEGER,
2216 .arg1_type = ARG_PTR_TO_CTX,
2217 .arg2_type = ARG_ANYTHING,
2220 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2222 switch (skb->protocol) {
2223 case htons(ETH_P_IP):
2224 return sizeof(struct iphdr);
2225 case htons(ETH_P_IPV6):
2226 return sizeof(struct ipv6hdr);
2232 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2234 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2237 ret = skb_cow(skb, len_diff);
2238 if (unlikely(ret < 0))
2241 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2242 if (unlikely(ret < 0))
2245 if (skb_is_gso(skb)) {
2246 /* Due to header grow, MSS needs to be downgraded. */
2247 skb_shinfo(skb)->gso_size -= len_diff;
2248 /* Header must be checked, and gso_segs recomputed. */
2249 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2250 skb_shinfo(skb)->gso_segs = 0;
2256 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2258 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2261 ret = skb_unclone(skb, GFP_ATOMIC);
2262 if (unlikely(ret < 0))
2265 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2266 if (unlikely(ret < 0))
2269 if (skb_is_gso(skb)) {
2270 /* Due to header shrink, MSS can be upgraded. */
2271 skb_shinfo(skb)->gso_size += len_diff;
2272 /* Header must be checked, and gso_segs recomputed. */
2273 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2274 skb_shinfo(skb)->gso_segs = 0;
2280 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2282 return skb->dev->mtu + skb->dev->hard_header_len;
2285 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2287 bool trans_same = skb->transport_header == skb->network_header;
2288 u32 len_cur, len_diff_abs = abs(len_diff);
2289 u32 len_min = bpf_skb_net_base_len(skb);
2290 u32 len_max = __bpf_skb_max_len(skb);
2291 __be16 proto = skb->protocol;
2292 bool shrink = len_diff < 0;
2295 if (unlikely(len_diff_abs > 0xfffU))
2297 if (unlikely(proto != htons(ETH_P_IP) &&
2298 proto != htons(ETH_P_IPV6)))
2301 len_cur = skb->len - skb_network_offset(skb);
2302 if (skb_transport_header_was_set(skb) && !trans_same)
2303 len_cur = skb_network_header_len(skb);
2304 if ((shrink && (len_diff_abs >= len_cur ||
2305 len_cur - len_diff_abs < len_min)) ||
2306 (!shrink && (skb->len + len_diff_abs > len_max &&
2310 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2311 bpf_skb_net_grow(skb, len_diff_abs);
2313 bpf_compute_data_pointers(skb);
2317 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2318 u32, mode, u64, flags)
2320 if (unlikely(flags))
2322 if (likely(mode == BPF_ADJ_ROOM_NET))
2323 return bpf_skb_adjust_net(skb, len_diff);
2328 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2329 .func = bpf_skb_adjust_room,
2331 .ret_type = RET_INTEGER,
2332 .arg1_type = ARG_PTR_TO_CTX,
2333 .arg2_type = ARG_ANYTHING,
2334 .arg3_type = ARG_ANYTHING,
2335 .arg4_type = ARG_ANYTHING,
2338 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2340 u32 min_len = skb_network_offset(skb);
2342 if (skb_transport_header_was_set(skb))
2343 min_len = skb_transport_offset(skb);
2344 if (skb->ip_summed == CHECKSUM_PARTIAL)
2345 min_len = skb_checksum_start_offset(skb) +
2346 skb->csum_offset + sizeof(__sum16);
2350 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2352 unsigned int old_len = skb->len;
2355 ret = __skb_grow_rcsum(skb, new_len);
2357 memset(skb->data + old_len, 0, new_len - old_len);
2361 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2363 return __skb_trim_rcsum(skb, new_len);
2366 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2369 u32 max_len = __bpf_skb_max_len(skb);
2370 u32 min_len = __bpf_skb_min_len(skb);
2373 if (unlikely(flags || new_len > max_len || new_len < min_len))
2375 if (skb->encapsulation)
2378 /* The basic idea of this helper is that it's performing the
2379 * needed work to either grow or trim an skb, and eBPF program
2380 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2381 * bpf_lX_csum_replace() and others rather than passing a raw
2382 * buffer here. This one is a slow path helper and intended
2383 * for replies with control messages.
2385 * Like in bpf_skb_change_proto(), we want to keep this rather
2386 * minimal and without protocol specifics so that we are able
2387 * to separate concerns as in bpf_skb_store_bytes() should only
2388 * be the one responsible for writing buffers.
2390 * It's really expected to be a slow path operation here for
2391 * control message replies, so we're implicitly linearizing,
2392 * uncloning and drop offloads from the skb by this.
2394 ret = __bpf_try_make_writable(skb, skb->len);
2396 if (new_len > skb->len)
2397 ret = bpf_skb_grow_rcsum(skb, new_len);
2398 else if (new_len < skb->len)
2399 ret = bpf_skb_trim_rcsum(skb, new_len);
2400 if (!ret && skb_is_gso(skb))
2404 bpf_compute_data_pointers(skb);
2408 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2409 .func = bpf_skb_change_tail,
2411 .ret_type = RET_INTEGER,
2412 .arg1_type = ARG_PTR_TO_CTX,
2413 .arg2_type = ARG_ANYTHING,
2414 .arg3_type = ARG_ANYTHING,
2417 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2420 u32 max_len = __bpf_skb_max_len(skb);
2421 u32 new_len = skb->len + head_room;
2424 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2425 new_len < skb->len))
2428 ret = skb_cow(skb, head_room);
2430 /* Idea for this helper is that we currently only
2431 * allow to expand on mac header. This means that
2432 * skb->protocol network header, etc, stay as is.
2433 * Compared to bpf_skb_change_tail(), we're more
2434 * flexible due to not needing to linearize or
2435 * reset GSO. Intention for this helper is to be
2436 * used by an L3 skb that needs to push mac header
2437 * for redirection into L2 device.
2439 __skb_push(skb, head_room);
2440 memset(skb->data, 0, head_room);
2441 skb_reset_mac_header(skb);
2444 bpf_compute_data_pointers(skb);
2448 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2449 .func = bpf_skb_change_head,
2451 .ret_type = RET_INTEGER,
2452 .arg1_type = ARG_PTR_TO_CTX,
2453 .arg2_type = ARG_ANYTHING,
2454 .arg3_type = ARG_ANYTHING,
2457 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
2459 return xdp_data_meta_unsupported(xdp) ? 0 :
2460 xdp->data - xdp->data_meta;
2463 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2465 unsigned long metalen = xdp_get_metalen(xdp);
2466 void *data_start = xdp->data_hard_start + metalen;
2467 void *data = xdp->data + offset;
2469 if (unlikely(data < data_start ||
2470 data > xdp->data_end - ETH_HLEN))
2474 memmove(xdp->data_meta + offset,
2475 xdp->data_meta, metalen);
2476 xdp->data_meta += offset;
2482 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2483 .func = bpf_xdp_adjust_head,
2485 .ret_type = RET_INTEGER,
2486 .arg1_type = ARG_PTR_TO_CTX,
2487 .arg2_type = ARG_ANYTHING,
2490 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
2492 void *meta = xdp->data_meta + offset;
2493 unsigned long metalen = xdp->data - meta;
2495 if (xdp_data_meta_unsupported(xdp))
2497 if (unlikely(meta < xdp->data_hard_start ||
2500 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
2504 xdp->data_meta = meta;
2509 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
2510 .func = bpf_xdp_adjust_meta,
2512 .ret_type = RET_INTEGER,
2513 .arg1_type = ARG_PTR_TO_CTX,
2514 .arg2_type = ARG_ANYTHING,
2517 static int __bpf_tx_xdp(struct net_device *dev,
2518 struct bpf_map *map,
2519 struct xdp_buff *xdp,
2524 if (!dev->netdev_ops->ndo_xdp_xmit) {
2528 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
2531 dev->netdev_ops->ndo_xdp_flush(dev);
2535 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
2536 struct bpf_map *map,
2537 struct xdp_buff *xdp,
2542 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
2543 struct net_device *dev = fwd;
2545 if (!dev->netdev_ops->ndo_xdp_xmit)
2548 err = dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
2551 __dev_map_insert_ctx(map, index);
2553 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
2554 struct bpf_cpu_map_entry *rcpu = fwd;
2556 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
2559 __cpu_map_insert_ctx(map, index);
2564 void xdp_do_flush_map(void)
2566 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2567 struct bpf_map *map = ri->map_to_flush;
2569 ri->map_to_flush = NULL;
2571 switch (map->map_type) {
2572 case BPF_MAP_TYPE_DEVMAP:
2573 __dev_map_flush(map);
2575 case BPF_MAP_TYPE_CPUMAP:
2576 __cpu_map_flush(map);
2583 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
2585 static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
2587 switch (map->map_type) {
2588 case BPF_MAP_TYPE_DEVMAP:
2589 return __dev_map_lookup_elem(map, index);
2590 case BPF_MAP_TYPE_CPUMAP:
2591 return __cpu_map_lookup_elem(map, index);
2597 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
2600 return (unsigned long)xdp_prog->aux != aux;
2603 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
2604 struct bpf_prog *xdp_prog)
2606 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2607 unsigned long map_owner = ri->map_owner;
2608 struct bpf_map *map = ri->map;
2609 u32 index = ri->ifindex;
2617 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2623 fwd = __xdp_map_lookup_elem(map, index);
2628 if (ri->map_to_flush && ri->map_to_flush != map)
2631 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
2635 ri->map_to_flush = map;
2636 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
2639 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
2643 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
2644 struct bpf_prog *xdp_prog)
2646 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2647 struct net_device *fwd;
2648 u32 index = ri->ifindex;
2652 return xdp_do_redirect_map(dev, xdp, xdp_prog);
2654 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2656 if (unlikely(!fwd)) {
2661 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
2665 _trace_xdp_redirect(dev, xdp_prog, index);
2668 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2671 EXPORT_SYMBOL_GPL(xdp_do_redirect);
2673 static int __xdp_generic_ok_fwd_dev(struct sk_buff *skb, struct net_device *fwd)
2677 if (unlikely(!(fwd->flags & IFF_UP)))
2680 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
2687 static int xdp_do_generic_redirect_map(struct net_device *dev,
2688 struct sk_buff *skb,
2689 struct bpf_prog *xdp_prog)
2691 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2692 unsigned long map_owner = ri->map_owner;
2693 struct bpf_map *map = ri->map;
2694 struct net_device *fwd = NULL;
2695 u32 index = ri->ifindex;
2702 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
2707 fwd = __xdp_map_lookup_elem(map, index);
2708 if (unlikely(!fwd)) {
2713 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
2714 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
2718 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
2723 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
2726 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
2730 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
2731 struct bpf_prog *xdp_prog)
2733 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2734 u32 index = ri->ifindex;
2735 struct net_device *fwd;
2739 return xdp_do_generic_redirect_map(dev, skb, xdp_prog);
2742 fwd = dev_get_by_index_rcu(dev_net(dev), index);
2743 if (unlikely(!fwd)) {
2748 if (unlikely((err = __xdp_generic_ok_fwd_dev(skb, fwd))))
2752 _trace_xdp_redirect(dev, xdp_prog, index);
2755 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2758 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
2760 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
2762 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2764 if (unlikely(flags))
2767 ri->ifindex = ifindex;
2772 return XDP_REDIRECT;
2775 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
2776 .func = bpf_xdp_redirect,
2778 .ret_type = RET_INTEGER,
2779 .arg1_type = ARG_ANYTHING,
2780 .arg2_type = ARG_ANYTHING,
2783 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
2784 unsigned long, map_owner)
2786 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2788 if (unlikely(flags))
2791 ri->ifindex = ifindex;
2794 ri->map_owner = map_owner;
2796 return XDP_REDIRECT;
2799 /* Note, arg4 is hidden from users and populated by the verifier
2800 * with the right pointer.
2802 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
2803 .func = bpf_xdp_redirect_map,
2805 .ret_type = RET_INTEGER,
2806 .arg1_type = ARG_CONST_MAP_PTR,
2807 .arg2_type = ARG_ANYTHING,
2808 .arg3_type = ARG_ANYTHING,
2811 bool bpf_helper_changes_pkt_data(void *func)
2813 if (func == bpf_skb_vlan_push ||
2814 func == bpf_skb_vlan_pop ||
2815 func == bpf_skb_store_bytes ||
2816 func == bpf_skb_change_proto ||
2817 func == bpf_skb_change_head ||
2818 func == bpf_skb_change_tail ||
2819 func == bpf_skb_adjust_room ||
2820 func == bpf_skb_pull_data ||
2821 func == bpf_clone_redirect ||
2822 func == bpf_l3_csum_replace ||
2823 func == bpf_l4_csum_replace ||
2824 func == bpf_xdp_adjust_head ||
2825 func == bpf_xdp_adjust_meta)
2831 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2832 unsigned long off, unsigned long len)
2834 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
2838 if (ptr != dst_buff)
2839 memcpy(dst_buff, ptr, len);
2844 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2845 u64, flags, void *, meta, u64, meta_size)
2847 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2849 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2851 if (unlikely(skb_size > skb->len))
2854 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2858 static const struct bpf_func_proto bpf_skb_event_output_proto = {
2859 .func = bpf_skb_event_output,
2861 .ret_type = RET_INTEGER,
2862 .arg1_type = ARG_PTR_TO_CTX,
2863 .arg2_type = ARG_CONST_MAP_PTR,
2864 .arg3_type = ARG_ANYTHING,
2865 .arg4_type = ARG_PTR_TO_MEM,
2866 .arg5_type = ARG_CONST_SIZE,
2869 static unsigned short bpf_tunnel_key_af(u64 flags)
2871 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2874 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2875 u32, size, u64, flags)
2877 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2878 u8 compat[sizeof(struct bpf_tunnel_key)];
2882 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2886 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2890 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2893 case offsetof(struct bpf_tunnel_key, tunnel_label):
2894 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2896 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2897 /* Fixup deprecated structure layouts here, so we have
2898 * a common path later on.
2900 if (ip_tunnel_info_af(info) != AF_INET)
2903 to = (struct bpf_tunnel_key *)compat;
2910 to->tunnel_id = be64_to_cpu(info->key.tun_id);
2911 to->tunnel_tos = info->key.tos;
2912 to->tunnel_ttl = info->key.ttl;
2914 if (flags & BPF_F_TUNINFO_IPV6) {
2915 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2916 sizeof(to->remote_ipv6));
2917 to->tunnel_label = be32_to_cpu(info->key.label);
2919 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
2922 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
2923 memcpy(to_orig, to, size);
2927 memset(to_orig, 0, size);
2931 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
2932 .func = bpf_skb_get_tunnel_key,
2934 .ret_type = RET_INTEGER,
2935 .arg1_type = ARG_PTR_TO_CTX,
2936 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2937 .arg3_type = ARG_CONST_SIZE,
2938 .arg4_type = ARG_ANYTHING,
2941 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
2943 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2946 if (unlikely(!info ||
2947 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2951 if (unlikely(size < info->options_len)) {
2956 ip_tunnel_info_opts_get(to, info);
2957 if (size > info->options_len)
2958 memset(to + info->options_len, 0, size - info->options_len);
2960 return info->options_len;
2962 memset(to, 0, size);
2966 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2967 .func = bpf_skb_get_tunnel_opt,
2969 .ret_type = RET_INTEGER,
2970 .arg1_type = ARG_PTR_TO_CTX,
2971 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2972 .arg3_type = ARG_CONST_SIZE,
2975 static struct metadata_dst __percpu *md_dst;
2977 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2978 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
2980 struct metadata_dst *md = this_cpu_ptr(md_dst);
2981 u8 compat[sizeof(struct bpf_tunnel_key)];
2982 struct ip_tunnel_info *info;
2984 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2985 BPF_F_DONT_FRAGMENT)))
2987 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2989 case offsetof(struct bpf_tunnel_key, tunnel_label):
2990 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2991 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2992 /* Fixup deprecated structure layouts here, so we have
2993 * a common path later on.
2995 memcpy(compat, from, size);
2996 memset(compat + size, 0, sizeof(compat) - size);
2997 from = (const struct bpf_tunnel_key *) compat;
3003 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3008 dst_hold((struct dst_entry *) md);
3009 skb_dst_set(skb, (struct dst_entry *) md);
3011 info = &md->u.tun_info;
3012 info->mode = IP_TUNNEL_INFO_TX;
3014 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3015 if (flags & BPF_F_DONT_FRAGMENT)
3016 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3017 if (flags & BPF_F_ZERO_CSUM_TX)
3018 info->key.tun_flags &= ~TUNNEL_CSUM;
3020 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3021 info->key.tos = from->tunnel_tos;
3022 info->key.ttl = from->tunnel_ttl;
3024 if (flags & BPF_F_TUNINFO_IPV6) {
3025 info->mode |= IP_TUNNEL_INFO_IPV6;
3026 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3027 sizeof(from->remote_ipv6));
3028 info->key.label = cpu_to_be32(from->tunnel_label) &
3029 IPV6_FLOWLABEL_MASK;
3031 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3037 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3038 .func = bpf_skb_set_tunnel_key,
3040 .ret_type = RET_INTEGER,
3041 .arg1_type = ARG_PTR_TO_CTX,
3042 .arg2_type = ARG_PTR_TO_MEM,
3043 .arg3_type = ARG_CONST_SIZE,
3044 .arg4_type = ARG_ANYTHING,
3047 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3048 const u8 *, from, u32, size)
3050 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3051 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3053 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3055 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3058 ip_tunnel_info_opts_set(info, from, size);
3063 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3064 .func = bpf_skb_set_tunnel_opt,
3066 .ret_type = RET_INTEGER,
3067 .arg1_type = ARG_PTR_TO_CTX,
3068 .arg2_type = ARG_PTR_TO_MEM,
3069 .arg3_type = ARG_CONST_SIZE,
3072 static const struct bpf_func_proto *
3073 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3076 struct metadata_dst __percpu *tmp;
3078 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3083 if (cmpxchg(&md_dst, NULL, tmp))
3084 metadata_dst_free_percpu(tmp);
3088 case BPF_FUNC_skb_set_tunnel_key:
3089 return &bpf_skb_set_tunnel_key_proto;
3090 case BPF_FUNC_skb_set_tunnel_opt:
3091 return &bpf_skb_set_tunnel_opt_proto;
3097 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3100 struct bpf_array *array = container_of(map, struct bpf_array, map);
3101 struct cgroup *cgrp;
3104 sk = skb_to_full_sk(skb);
3105 if (!sk || !sk_fullsock(sk))
3107 if (unlikely(idx >= array->map.max_entries))
3110 cgrp = READ_ONCE(array->ptrs[idx]);
3111 if (unlikely(!cgrp))
3114 return sk_under_cgroup_hierarchy(sk, cgrp);
3117 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3118 .func = bpf_skb_under_cgroup,
3120 .ret_type = RET_INTEGER,
3121 .arg1_type = ARG_PTR_TO_CTX,
3122 .arg2_type = ARG_CONST_MAP_PTR,
3123 .arg3_type = ARG_ANYTHING,
3126 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3127 unsigned long off, unsigned long len)
3129 memcpy(dst_buff, src_buff + off, len);
3133 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3134 u64, flags, void *, meta, u64, meta_size)
3136 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3138 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3140 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3143 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3144 xdp_size, bpf_xdp_copy);
3147 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3148 .func = bpf_xdp_event_output,
3150 .ret_type = RET_INTEGER,
3151 .arg1_type = ARG_PTR_TO_CTX,
3152 .arg2_type = ARG_CONST_MAP_PTR,
3153 .arg3_type = ARG_ANYTHING,
3154 .arg4_type = ARG_PTR_TO_MEM,
3155 .arg5_type = ARG_CONST_SIZE,
3158 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3160 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3163 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3164 .func = bpf_get_socket_cookie,
3166 .ret_type = RET_INTEGER,
3167 .arg1_type = ARG_PTR_TO_CTX,
3170 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3172 struct sock *sk = sk_to_full_sk(skb->sk);
3175 if (!sk || !sk_fullsock(sk))
3177 kuid = sock_net_uid(sock_net(sk), sk);
3178 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3181 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3182 .func = bpf_get_socket_uid,
3184 .ret_type = RET_INTEGER,
3185 .arg1_type = ARG_PTR_TO_CTX,
3188 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3189 int, level, int, optname, char *, optval, int, optlen)
3191 struct sock *sk = bpf_sock->sk;
3195 if (!sk_fullsock(sk))
3198 if (level == SOL_SOCKET) {
3199 if (optlen != sizeof(int))
3201 val = *((int *)optval);
3203 /* Only some socketops are supported */
3206 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3207 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3210 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3211 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3213 case SO_MAX_PACING_RATE:
3214 sk->sk_max_pacing_rate = val;
3215 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3216 sk->sk_max_pacing_rate);
3219 sk->sk_priority = val;
3224 sk->sk_rcvlowat = val ? : 1;
3233 } else if (level == SOL_TCP &&
3234 sk->sk_prot->setsockopt == tcp_setsockopt) {
3235 if (optname == TCP_CONGESTION) {
3236 char name[TCP_CA_NAME_MAX];
3237 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3239 strncpy(name, optval, min_t(long, optlen,
3240 TCP_CA_NAME_MAX-1));
3241 name[TCP_CA_NAME_MAX-1] = 0;
3242 ret = tcp_set_congestion_control(sk, name, false, reinit);
3244 struct tcp_sock *tp = tcp_sk(sk);
3246 if (optlen != sizeof(int))
3249 val = *((int *)optval);
3250 /* Only some options are supported */
3253 if (val <= 0 || tp->data_segs_out > 0)
3258 case TCP_BPF_SNDCWND_CLAMP:
3262 tp->snd_cwnd_clamp = val;
3263 tp->snd_ssthresh = val;
3277 static const struct bpf_func_proto bpf_setsockopt_proto = {
3278 .func = bpf_setsockopt,
3280 .ret_type = RET_INTEGER,
3281 .arg1_type = ARG_PTR_TO_CTX,
3282 .arg2_type = ARG_ANYTHING,
3283 .arg3_type = ARG_ANYTHING,
3284 .arg4_type = ARG_PTR_TO_MEM,
3285 .arg5_type = ARG_CONST_SIZE,
3288 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3289 int, level, int, optname, char *, optval, int, optlen)
3291 struct sock *sk = bpf_sock->sk;
3293 if (!sk_fullsock(sk))
3297 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3298 if (optname == TCP_CONGESTION) {
3299 struct inet_connection_sock *icsk = inet_csk(sk);
3301 if (!icsk->icsk_ca_ops || optlen <= 1)
3303 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3304 optval[optlen - 1] = 0;
3314 memset(optval, 0, optlen);
3318 static const struct bpf_func_proto bpf_getsockopt_proto = {
3319 .func = bpf_getsockopt,
3321 .ret_type = RET_INTEGER,
3322 .arg1_type = ARG_PTR_TO_CTX,
3323 .arg2_type = ARG_ANYTHING,
3324 .arg3_type = ARG_ANYTHING,
3325 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
3326 .arg5_type = ARG_CONST_SIZE,
3329 static const struct bpf_func_proto *
3330 bpf_base_func_proto(enum bpf_func_id func_id)
3333 case BPF_FUNC_map_lookup_elem:
3334 return &bpf_map_lookup_elem_proto;
3335 case BPF_FUNC_map_update_elem:
3336 return &bpf_map_update_elem_proto;
3337 case BPF_FUNC_map_delete_elem:
3338 return &bpf_map_delete_elem_proto;
3339 case BPF_FUNC_get_prandom_u32:
3340 return &bpf_get_prandom_u32_proto;
3341 case BPF_FUNC_get_smp_processor_id:
3342 return &bpf_get_raw_smp_processor_id_proto;
3343 case BPF_FUNC_get_numa_node_id:
3344 return &bpf_get_numa_node_id_proto;
3345 case BPF_FUNC_tail_call:
3346 return &bpf_tail_call_proto;
3347 case BPF_FUNC_ktime_get_ns:
3348 return &bpf_ktime_get_ns_proto;
3349 case BPF_FUNC_trace_printk:
3350 if (capable(CAP_SYS_ADMIN))
3351 return bpf_get_trace_printk_proto();
3357 static const struct bpf_func_proto *
3358 sock_filter_func_proto(enum bpf_func_id func_id)
3361 /* inet and inet6 sockets are created in a process
3362 * context so there is always a valid uid/gid
3364 case BPF_FUNC_get_current_uid_gid:
3365 return &bpf_get_current_uid_gid_proto;
3367 return bpf_base_func_proto(func_id);
3371 static const struct bpf_func_proto *
3372 sk_filter_func_proto(enum bpf_func_id func_id)
3375 case BPF_FUNC_skb_load_bytes:
3376 return &bpf_skb_load_bytes_proto;
3377 case BPF_FUNC_get_socket_cookie:
3378 return &bpf_get_socket_cookie_proto;
3379 case BPF_FUNC_get_socket_uid:
3380 return &bpf_get_socket_uid_proto;
3382 return bpf_base_func_proto(func_id);
3386 static const struct bpf_func_proto *
3387 tc_cls_act_func_proto(enum bpf_func_id func_id)
3390 case BPF_FUNC_skb_store_bytes:
3391 return &bpf_skb_store_bytes_proto;
3392 case BPF_FUNC_skb_load_bytes:
3393 return &bpf_skb_load_bytes_proto;
3394 case BPF_FUNC_skb_pull_data:
3395 return &bpf_skb_pull_data_proto;
3396 case BPF_FUNC_csum_diff:
3397 return &bpf_csum_diff_proto;
3398 case BPF_FUNC_csum_update:
3399 return &bpf_csum_update_proto;
3400 case BPF_FUNC_l3_csum_replace:
3401 return &bpf_l3_csum_replace_proto;
3402 case BPF_FUNC_l4_csum_replace:
3403 return &bpf_l4_csum_replace_proto;
3404 case BPF_FUNC_clone_redirect:
3405 return &bpf_clone_redirect_proto;
3406 case BPF_FUNC_get_cgroup_classid:
3407 return &bpf_get_cgroup_classid_proto;
3408 case BPF_FUNC_skb_vlan_push:
3409 return &bpf_skb_vlan_push_proto;
3410 case BPF_FUNC_skb_vlan_pop:
3411 return &bpf_skb_vlan_pop_proto;
3412 case BPF_FUNC_skb_change_proto:
3413 return &bpf_skb_change_proto_proto;
3414 case BPF_FUNC_skb_change_type:
3415 return &bpf_skb_change_type_proto;
3416 case BPF_FUNC_skb_adjust_room:
3417 return &bpf_skb_adjust_room_proto;
3418 case BPF_FUNC_skb_change_tail:
3419 return &bpf_skb_change_tail_proto;
3420 case BPF_FUNC_skb_get_tunnel_key:
3421 return &bpf_skb_get_tunnel_key_proto;
3422 case BPF_FUNC_skb_set_tunnel_key:
3423 return bpf_get_skb_set_tunnel_proto(func_id);
3424 case BPF_FUNC_skb_get_tunnel_opt:
3425 return &bpf_skb_get_tunnel_opt_proto;
3426 case BPF_FUNC_skb_set_tunnel_opt:
3427 return bpf_get_skb_set_tunnel_proto(func_id);
3428 case BPF_FUNC_redirect:
3429 return &bpf_redirect_proto;
3430 case BPF_FUNC_get_route_realm:
3431 return &bpf_get_route_realm_proto;
3432 case BPF_FUNC_get_hash_recalc:
3433 return &bpf_get_hash_recalc_proto;
3434 case BPF_FUNC_set_hash_invalid:
3435 return &bpf_set_hash_invalid_proto;
3436 case BPF_FUNC_set_hash:
3437 return &bpf_set_hash_proto;
3438 case BPF_FUNC_perf_event_output:
3439 return &bpf_skb_event_output_proto;
3440 case BPF_FUNC_get_smp_processor_id:
3441 return &bpf_get_smp_processor_id_proto;
3442 case BPF_FUNC_skb_under_cgroup:
3443 return &bpf_skb_under_cgroup_proto;
3444 case BPF_FUNC_get_socket_cookie:
3445 return &bpf_get_socket_cookie_proto;
3446 case BPF_FUNC_get_socket_uid:
3447 return &bpf_get_socket_uid_proto;
3449 return bpf_base_func_proto(func_id);
3453 static const struct bpf_func_proto *
3454 xdp_func_proto(enum bpf_func_id func_id)
3457 case BPF_FUNC_perf_event_output:
3458 return &bpf_xdp_event_output_proto;
3459 case BPF_FUNC_get_smp_processor_id:
3460 return &bpf_get_smp_processor_id_proto;
3461 case BPF_FUNC_xdp_adjust_head:
3462 return &bpf_xdp_adjust_head_proto;
3463 case BPF_FUNC_xdp_adjust_meta:
3464 return &bpf_xdp_adjust_meta_proto;
3465 case BPF_FUNC_redirect:
3466 return &bpf_xdp_redirect_proto;
3467 case BPF_FUNC_redirect_map:
3468 return &bpf_xdp_redirect_map_proto;
3470 return bpf_base_func_proto(func_id);
3474 static const struct bpf_func_proto *
3475 lwt_inout_func_proto(enum bpf_func_id func_id)
3478 case BPF_FUNC_skb_load_bytes:
3479 return &bpf_skb_load_bytes_proto;
3480 case BPF_FUNC_skb_pull_data:
3481 return &bpf_skb_pull_data_proto;
3482 case BPF_FUNC_csum_diff:
3483 return &bpf_csum_diff_proto;
3484 case BPF_FUNC_get_cgroup_classid:
3485 return &bpf_get_cgroup_classid_proto;
3486 case BPF_FUNC_get_route_realm:
3487 return &bpf_get_route_realm_proto;
3488 case BPF_FUNC_get_hash_recalc:
3489 return &bpf_get_hash_recalc_proto;
3490 case BPF_FUNC_perf_event_output:
3491 return &bpf_skb_event_output_proto;
3492 case BPF_FUNC_get_smp_processor_id:
3493 return &bpf_get_smp_processor_id_proto;
3494 case BPF_FUNC_skb_under_cgroup:
3495 return &bpf_skb_under_cgroup_proto;
3497 return bpf_base_func_proto(func_id);
3501 static const struct bpf_func_proto *
3502 sock_ops_func_proto(enum bpf_func_id func_id)
3505 case BPF_FUNC_setsockopt:
3506 return &bpf_setsockopt_proto;
3507 case BPF_FUNC_getsockopt:
3508 return &bpf_getsockopt_proto;
3509 case BPF_FUNC_sock_map_update:
3510 return &bpf_sock_map_update_proto;
3512 return bpf_base_func_proto(func_id);
3516 static const struct bpf_func_proto *sk_skb_func_proto(enum bpf_func_id func_id)
3519 case BPF_FUNC_skb_store_bytes:
3520 return &bpf_skb_store_bytes_proto;
3521 case BPF_FUNC_skb_load_bytes:
3522 return &bpf_skb_load_bytes_proto;
3523 case BPF_FUNC_skb_pull_data:
3524 return &bpf_skb_pull_data_proto;
3525 case BPF_FUNC_skb_change_tail:
3526 return &bpf_skb_change_tail_proto;
3527 case BPF_FUNC_skb_change_head:
3528 return &bpf_skb_change_head_proto;
3529 case BPF_FUNC_get_socket_cookie:
3530 return &bpf_get_socket_cookie_proto;
3531 case BPF_FUNC_get_socket_uid:
3532 return &bpf_get_socket_uid_proto;
3533 case BPF_FUNC_sk_redirect_map:
3534 return &bpf_sk_redirect_map_proto;
3536 return bpf_base_func_proto(func_id);
3540 static const struct bpf_func_proto *
3541 lwt_xmit_func_proto(enum bpf_func_id func_id)
3544 case BPF_FUNC_skb_get_tunnel_key:
3545 return &bpf_skb_get_tunnel_key_proto;
3546 case BPF_FUNC_skb_set_tunnel_key:
3547 return bpf_get_skb_set_tunnel_proto(func_id);
3548 case BPF_FUNC_skb_get_tunnel_opt:
3549 return &bpf_skb_get_tunnel_opt_proto;
3550 case BPF_FUNC_skb_set_tunnel_opt:
3551 return bpf_get_skb_set_tunnel_proto(func_id);
3552 case BPF_FUNC_redirect:
3553 return &bpf_redirect_proto;
3554 case BPF_FUNC_clone_redirect:
3555 return &bpf_clone_redirect_proto;
3556 case BPF_FUNC_skb_change_tail:
3557 return &bpf_skb_change_tail_proto;
3558 case BPF_FUNC_skb_change_head:
3559 return &bpf_skb_change_head_proto;
3560 case BPF_FUNC_skb_store_bytes:
3561 return &bpf_skb_store_bytes_proto;
3562 case BPF_FUNC_csum_update:
3563 return &bpf_csum_update_proto;
3564 case BPF_FUNC_l3_csum_replace:
3565 return &bpf_l3_csum_replace_proto;
3566 case BPF_FUNC_l4_csum_replace:
3567 return &bpf_l4_csum_replace_proto;
3568 case BPF_FUNC_set_hash_invalid:
3569 return &bpf_set_hash_invalid_proto;
3571 return lwt_inout_func_proto(func_id);
3575 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
3576 struct bpf_insn_access_aux *info)
3578 const int size_default = sizeof(__u32);
3580 if (off < 0 || off >= sizeof(struct __sk_buff))
3583 /* The verifier guarantees that size > 0. */
3584 if (off % size != 0)
3588 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3589 if (off + size > offsetofend(struct __sk_buff, cb[4]))
3592 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
3593 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
3594 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
3595 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
3596 case bpf_ctx_range(struct __sk_buff, data):
3597 case bpf_ctx_range(struct __sk_buff, data_meta):
3598 case bpf_ctx_range(struct __sk_buff, data_end):
3599 if (size != size_default)
3603 /* Only narrow read access allowed for now. */
3604 if (type == BPF_WRITE) {
3605 if (size != size_default)
3608 bpf_ctx_record_field_size(info, size_default);
3609 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
3617 static bool sk_filter_is_valid_access(int off, int size,
3618 enum bpf_access_type type,
3619 struct bpf_insn_access_aux *info)
3622 case bpf_ctx_range(struct __sk_buff, tc_classid):
3623 case bpf_ctx_range(struct __sk_buff, data):
3624 case bpf_ctx_range(struct __sk_buff, data_meta):
3625 case bpf_ctx_range(struct __sk_buff, data_end):
3626 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3630 if (type == BPF_WRITE) {
3632 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3639 return bpf_skb_is_valid_access(off, size, type, info);
3642 static bool lwt_is_valid_access(int off, int size,
3643 enum bpf_access_type type,
3644 struct bpf_insn_access_aux *info)
3647 case bpf_ctx_range(struct __sk_buff, tc_classid):
3648 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3649 case bpf_ctx_range(struct __sk_buff, data_meta):
3653 if (type == BPF_WRITE) {
3655 case bpf_ctx_range(struct __sk_buff, mark):
3656 case bpf_ctx_range(struct __sk_buff, priority):
3657 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3665 case bpf_ctx_range(struct __sk_buff, data):
3666 info->reg_type = PTR_TO_PACKET;
3668 case bpf_ctx_range(struct __sk_buff, data_end):
3669 info->reg_type = PTR_TO_PACKET_END;
3673 return bpf_skb_is_valid_access(off, size, type, info);
3676 static bool sock_filter_is_valid_access(int off, int size,
3677 enum bpf_access_type type,
3678 struct bpf_insn_access_aux *info)
3680 if (type == BPF_WRITE) {
3682 case offsetof(struct bpf_sock, bound_dev_if):
3683 case offsetof(struct bpf_sock, mark):
3684 case offsetof(struct bpf_sock, priority):
3691 if (off < 0 || off + size > sizeof(struct bpf_sock))
3693 /* The verifier guarantees that size > 0. */
3694 if (off % size != 0)
3696 if (size != sizeof(__u32))
3702 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
3703 const struct bpf_prog *prog, int drop_verdict)
3705 struct bpf_insn *insn = insn_buf;
3710 /* if (!skb->cloned)
3713 * (Fast-path, otherwise approximation that we might be
3714 * a clone, do the rest in helper.)
3716 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
3717 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
3718 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
3720 /* ret = bpf_skb_pull_data(skb, 0); */
3721 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
3722 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
3723 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
3724 BPF_FUNC_skb_pull_data);
3727 * return TC_ACT_SHOT;
3729 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
3730 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
3731 *insn++ = BPF_EXIT_INSN();
3734 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
3736 *insn++ = prog->insnsi[0];
3738 return insn - insn_buf;
3741 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
3742 const struct bpf_prog *prog)
3744 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
3747 static bool tc_cls_act_is_valid_access(int off, int size,
3748 enum bpf_access_type type,
3749 struct bpf_insn_access_aux *info)
3751 if (type == BPF_WRITE) {
3753 case bpf_ctx_range(struct __sk_buff, mark):
3754 case bpf_ctx_range(struct __sk_buff, tc_index):
3755 case bpf_ctx_range(struct __sk_buff, priority):
3756 case bpf_ctx_range(struct __sk_buff, tc_classid):
3757 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3765 case bpf_ctx_range(struct __sk_buff, data):
3766 info->reg_type = PTR_TO_PACKET;
3768 case bpf_ctx_range(struct __sk_buff, data_meta):
3769 info->reg_type = PTR_TO_PACKET_META;
3771 case bpf_ctx_range(struct __sk_buff, data_end):
3772 info->reg_type = PTR_TO_PACKET_END;
3774 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3778 return bpf_skb_is_valid_access(off, size, type, info);
3781 static bool __is_valid_xdp_access(int off, int size)
3783 if (off < 0 || off >= sizeof(struct xdp_md))
3785 if (off % size != 0)
3787 if (size != sizeof(__u32))
3793 static bool xdp_is_valid_access(int off, int size,
3794 enum bpf_access_type type,
3795 struct bpf_insn_access_aux *info)
3797 if (type == BPF_WRITE)
3801 case offsetof(struct xdp_md, data):
3802 info->reg_type = PTR_TO_PACKET;
3804 case offsetof(struct xdp_md, data_meta):
3805 info->reg_type = PTR_TO_PACKET_META;
3807 case offsetof(struct xdp_md, data_end):
3808 info->reg_type = PTR_TO_PACKET_END;
3812 return __is_valid_xdp_access(off, size);
3815 void bpf_warn_invalid_xdp_action(u32 act)
3817 const u32 act_max = XDP_REDIRECT;
3819 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
3820 act > act_max ? "Illegal" : "Driver unsupported",
3823 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
3825 static bool __is_valid_sock_ops_access(int off, int size)
3827 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
3829 /* The verifier guarantees that size > 0. */
3830 if (off % size != 0)
3832 if (size != sizeof(__u32))
3838 static bool sock_ops_is_valid_access(int off, int size,
3839 enum bpf_access_type type,
3840 struct bpf_insn_access_aux *info)
3842 if (type == BPF_WRITE) {
3844 case offsetof(struct bpf_sock_ops, op) ...
3845 offsetof(struct bpf_sock_ops, replylong[3]):
3852 return __is_valid_sock_ops_access(off, size);
3855 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
3856 const struct bpf_prog *prog)
3858 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
3861 static bool sk_skb_is_valid_access(int off, int size,
3862 enum bpf_access_type type,
3863 struct bpf_insn_access_aux *info)
3866 case bpf_ctx_range(struct __sk_buff, tc_classid):
3867 case bpf_ctx_range(struct __sk_buff, data_meta):
3871 if (type == BPF_WRITE) {
3873 case bpf_ctx_range(struct __sk_buff, tc_index):
3874 case bpf_ctx_range(struct __sk_buff, priority):
3882 case bpf_ctx_range(struct __sk_buff, mark):
3884 case bpf_ctx_range(struct __sk_buff, data):
3885 info->reg_type = PTR_TO_PACKET;
3887 case bpf_ctx_range(struct __sk_buff, data_end):
3888 info->reg_type = PTR_TO_PACKET_END;
3892 return bpf_skb_is_valid_access(off, size, type, info);
3895 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
3896 const struct bpf_insn *si,
3897 struct bpf_insn *insn_buf,
3898 struct bpf_prog *prog, u32 *target_size)
3900 struct bpf_insn *insn = insn_buf;
3904 case offsetof(struct __sk_buff, len):
3905 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3906 bpf_target_off(struct sk_buff, len, 4,
3910 case offsetof(struct __sk_buff, protocol):
3911 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3912 bpf_target_off(struct sk_buff, protocol, 2,
3916 case offsetof(struct __sk_buff, vlan_proto):
3917 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3918 bpf_target_off(struct sk_buff, vlan_proto, 2,
3922 case offsetof(struct __sk_buff, priority):
3923 if (type == BPF_WRITE)
3924 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3925 bpf_target_off(struct sk_buff, priority, 4,
3928 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3929 bpf_target_off(struct sk_buff, priority, 4,
3933 case offsetof(struct __sk_buff, ingress_ifindex):
3934 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3935 bpf_target_off(struct sk_buff, skb_iif, 4,
3939 case offsetof(struct __sk_buff, ifindex):
3940 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
3941 si->dst_reg, si->src_reg,
3942 offsetof(struct sk_buff, dev));
3943 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
3944 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3945 bpf_target_off(struct net_device, ifindex, 4,
3949 case offsetof(struct __sk_buff, hash):
3950 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3951 bpf_target_off(struct sk_buff, hash, 4,
3955 case offsetof(struct __sk_buff, mark):
3956 if (type == BPF_WRITE)
3957 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3958 bpf_target_off(struct sk_buff, mark, 4,
3961 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3962 bpf_target_off(struct sk_buff, mark, 4,
3966 case offsetof(struct __sk_buff, pkt_type):
3968 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
3970 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
3971 #ifdef __BIG_ENDIAN_BITFIELD
3972 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
3976 case offsetof(struct __sk_buff, queue_mapping):
3977 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3978 bpf_target_off(struct sk_buff, queue_mapping, 2,
3982 case offsetof(struct __sk_buff, vlan_present):
3983 case offsetof(struct __sk_buff, vlan_tci):
3984 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
3986 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3987 bpf_target_off(struct sk_buff, vlan_tci, 2,
3989 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
3990 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
3993 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
3994 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
3998 case offsetof(struct __sk_buff, cb[0]) ...
3999 offsetofend(struct __sk_buff, cb[4]) - 1:
4000 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
4001 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
4002 offsetof(struct qdisc_skb_cb, data)) %
4005 prog->cb_access = 1;
4007 off -= offsetof(struct __sk_buff, cb[0]);
4008 off += offsetof(struct sk_buff, cb);
4009 off += offsetof(struct qdisc_skb_cb, data);
4010 if (type == BPF_WRITE)
4011 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
4014 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
4018 case offsetof(struct __sk_buff, tc_classid):
4019 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
4022 off -= offsetof(struct __sk_buff, tc_classid);
4023 off += offsetof(struct sk_buff, cb);
4024 off += offsetof(struct qdisc_skb_cb, tc_classid);
4026 if (type == BPF_WRITE)
4027 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
4030 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
4034 case offsetof(struct __sk_buff, data):
4035 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
4036 si->dst_reg, si->src_reg,
4037 offsetof(struct sk_buff, data));
4040 case offsetof(struct __sk_buff, data_meta):
4042 off -= offsetof(struct __sk_buff, data_meta);
4043 off += offsetof(struct sk_buff, cb);
4044 off += offsetof(struct bpf_skb_data_end, data_meta);
4045 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
4049 case offsetof(struct __sk_buff, data_end):
4051 off -= offsetof(struct __sk_buff, data_end);
4052 off += offsetof(struct sk_buff, cb);
4053 off += offsetof(struct bpf_skb_data_end, data_end);
4054 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
4058 case offsetof(struct __sk_buff, tc_index):
4059 #ifdef CONFIG_NET_SCHED
4060 if (type == BPF_WRITE)
4061 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
4062 bpf_target_off(struct sk_buff, tc_index, 2,
4065 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4066 bpf_target_off(struct sk_buff, tc_index, 2,
4070 if (type == BPF_WRITE)
4071 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
4073 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
4077 case offsetof(struct __sk_buff, napi_id):
4078 #if defined(CONFIG_NET_RX_BUSY_POLL)
4079 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4080 bpf_target_off(struct sk_buff, napi_id, 4,
4082 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
4083 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
4086 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
4089 case offsetof(struct __sk_buff, family):
4090 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
4092 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4093 si->dst_reg, si->src_reg,
4094 offsetof(struct sk_buff, sk));
4095 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4096 bpf_target_off(struct sock_common,
4100 case offsetof(struct __sk_buff, remote_ip4):
4101 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
4103 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4104 si->dst_reg, si->src_reg,
4105 offsetof(struct sk_buff, sk));
4106 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4107 bpf_target_off(struct sock_common,
4111 case offsetof(struct __sk_buff, local_ip4):
4112 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4113 skc_rcv_saddr) != 4);
4115 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4116 si->dst_reg, si->src_reg,
4117 offsetof(struct sk_buff, sk));
4118 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4119 bpf_target_off(struct sock_common,
4123 case offsetof(struct __sk_buff, remote_ip6[0]) ...
4124 offsetof(struct __sk_buff, remote_ip6[3]):
4125 #if IS_ENABLED(CONFIG_IPV6)
4126 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4127 skc_v6_daddr.s6_addr32[0]) != 4);
4130 off -= offsetof(struct __sk_buff, remote_ip6[0]);
4132 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4133 si->dst_reg, si->src_reg,
4134 offsetof(struct sk_buff, sk));
4135 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4136 offsetof(struct sock_common,
4137 skc_v6_daddr.s6_addr32[0]) +
4140 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4143 case offsetof(struct __sk_buff, local_ip6[0]) ...
4144 offsetof(struct __sk_buff, local_ip6[3]):
4145 #if IS_ENABLED(CONFIG_IPV6)
4146 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4147 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
4150 off -= offsetof(struct __sk_buff, local_ip6[0]);
4152 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4153 si->dst_reg, si->src_reg,
4154 offsetof(struct sk_buff, sk));
4155 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4156 offsetof(struct sock_common,
4157 skc_v6_rcv_saddr.s6_addr32[0]) +
4160 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4164 case offsetof(struct __sk_buff, remote_port):
4165 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
4167 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4168 si->dst_reg, si->src_reg,
4169 offsetof(struct sk_buff, sk));
4170 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4171 bpf_target_off(struct sock_common,
4174 #ifndef __BIG_ENDIAN_BITFIELD
4175 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
4179 case offsetof(struct __sk_buff, local_port):
4180 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
4182 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
4183 si->dst_reg, si->src_reg,
4184 offsetof(struct sk_buff, sk));
4185 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4186 bpf_target_off(struct sock_common,
4187 skc_num, 2, target_size));
4191 return insn - insn_buf;
4194 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
4195 const struct bpf_insn *si,
4196 struct bpf_insn *insn_buf,
4197 struct bpf_prog *prog, u32 *target_size)
4199 struct bpf_insn *insn = insn_buf;
4202 case offsetof(struct bpf_sock, bound_dev_if):
4203 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
4205 if (type == BPF_WRITE)
4206 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4207 offsetof(struct sock, sk_bound_dev_if));
4209 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4210 offsetof(struct sock, sk_bound_dev_if));
4213 case offsetof(struct bpf_sock, mark):
4214 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
4216 if (type == BPF_WRITE)
4217 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4218 offsetof(struct sock, sk_mark));
4220 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4221 offsetof(struct sock, sk_mark));
4224 case offsetof(struct bpf_sock, priority):
4225 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
4227 if (type == BPF_WRITE)
4228 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4229 offsetof(struct sock, sk_priority));
4231 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4232 offsetof(struct sock, sk_priority));
4235 case offsetof(struct bpf_sock, family):
4236 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
4238 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
4239 offsetof(struct sock, sk_family));
4242 case offsetof(struct bpf_sock, type):
4243 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4244 offsetof(struct sock, __sk_flags_offset));
4245 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
4246 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
4249 case offsetof(struct bpf_sock, protocol):
4250 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4251 offsetof(struct sock, __sk_flags_offset));
4252 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
4253 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
4257 return insn - insn_buf;
4260 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
4261 const struct bpf_insn *si,
4262 struct bpf_insn *insn_buf,
4263 struct bpf_prog *prog, u32 *target_size)
4265 struct bpf_insn *insn = insn_buf;
4268 case offsetof(struct __sk_buff, ifindex):
4269 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
4270 si->dst_reg, si->src_reg,
4271 offsetof(struct sk_buff, dev));
4272 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4273 bpf_target_off(struct net_device, ifindex, 4,
4277 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4281 return insn - insn_buf;
4284 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
4285 const struct bpf_insn *si,
4286 struct bpf_insn *insn_buf,
4287 struct bpf_prog *prog, u32 *target_size)
4289 struct bpf_insn *insn = insn_buf;
4292 case offsetof(struct xdp_md, data):
4293 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
4294 si->dst_reg, si->src_reg,
4295 offsetof(struct xdp_buff, data));
4297 case offsetof(struct xdp_md, data_meta):
4298 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
4299 si->dst_reg, si->src_reg,
4300 offsetof(struct xdp_buff, data_meta));
4302 case offsetof(struct xdp_md, data_end):
4303 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
4304 si->dst_reg, si->src_reg,
4305 offsetof(struct xdp_buff, data_end));
4309 return insn - insn_buf;
4312 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
4313 const struct bpf_insn *si,
4314 struct bpf_insn *insn_buf,
4315 struct bpf_prog *prog,
4318 struct bpf_insn *insn = insn_buf;
4322 case offsetof(struct bpf_sock_ops, op) ...
4323 offsetof(struct bpf_sock_ops, replylong[3]):
4324 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
4325 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
4326 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
4327 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
4328 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
4329 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
4331 off -= offsetof(struct bpf_sock_ops, op);
4332 off += offsetof(struct bpf_sock_ops_kern, op);
4333 if (type == BPF_WRITE)
4334 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4337 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4341 case offsetof(struct bpf_sock_ops, family):
4342 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
4344 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4345 struct bpf_sock_ops_kern, sk),
4346 si->dst_reg, si->src_reg,
4347 offsetof(struct bpf_sock_ops_kern, sk));
4348 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4349 offsetof(struct sock_common, skc_family));
4352 case offsetof(struct bpf_sock_ops, remote_ip4):
4353 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
4355 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4356 struct bpf_sock_ops_kern, sk),
4357 si->dst_reg, si->src_reg,
4358 offsetof(struct bpf_sock_ops_kern, sk));
4359 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4360 offsetof(struct sock_common, skc_daddr));
4363 case offsetof(struct bpf_sock_ops, local_ip4):
4364 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
4366 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4367 struct bpf_sock_ops_kern, sk),
4368 si->dst_reg, si->src_reg,
4369 offsetof(struct bpf_sock_ops_kern, sk));
4370 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4371 offsetof(struct sock_common,
4375 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
4376 offsetof(struct bpf_sock_ops, remote_ip6[3]):
4377 #if IS_ENABLED(CONFIG_IPV6)
4378 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4379 skc_v6_daddr.s6_addr32[0]) != 4);
4382 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
4383 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4384 struct bpf_sock_ops_kern, sk),
4385 si->dst_reg, si->src_reg,
4386 offsetof(struct bpf_sock_ops_kern, sk));
4387 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4388 offsetof(struct sock_common,
4389 skc_v6_daddr.s6_addr32[0]) +
4392 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4396 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
4397 offsetof(struct bpf_sock_ops, local_ip6[3]):
4398 #if IS_ENABLED(CONFIG_IPV6)
4399 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4400 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
4403 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
4404 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4405 struct bpf_sock_ops_kern, sk),
4406 si->dst_reg, si->src_reg,
4407 offsetof(struct bpf_sock_ops_kern, sk));
4408 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4409 offsetof(struct sock_common,
4410 skc_v6_rcv_saddr.s6_addr32[0]) +
4413 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4417 case offsetof(struct bpf_sock_ops, remote_port):
4418 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
4420 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4421 struct bpf_sock_ops_kern, sk),
4422 si->dst_reg, si->src_reg,
4423 offsetof(struct bpf_sock_ops_kern, sk));
4424 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4425 offsetof(struct sock_common, skc_dport));
4426 #ifndef __BIG_ENDIAN_BITFIELD
4427 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
4431 case offsetof(struct bpf_sock_ops, local_port):
4432 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
4434 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4435 struct bpf_sock_ops_kern, sk),
4436 si->dst_reg, si->src_reg,
4437 offsetof(struct bpf_sock_ops_kern, sk));
4438 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4439 offsetof(struct sock_common, skc_num));
4442 case offsetof(struct bpf_sock_ops, is_fullsock):
4443 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4444 struct bpf_sock_ops_kern,
4446 si->dst_reg, si->src_reg,
4447 offsetof(struct bpf_sock_ops_kern,
4451 /* Helper macro for adding read access to tcp_sock fields. */
4452 #define SOCK_OPS_GET_TCP32(FIELD_NAME) \
4454 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD_NAME) != 4); \
4455 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
4456 struct bpf_sock_ops_kern, \
4458 si->dst_reg, si->src_reg, \
4459 offsetof(struct bpf_sock_ops_kern, \
4461 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
4462 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
4463 struct bpf_sock_ops_kern, sk),\
4464 si->dst_reg, si->src_reg, \
4465 offsetof(struct bpf_sock_ops_kern, sk));\
4466 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, \
4467 offsetof(struct tcp_sock, FIELD_NAME)); \
4470 case offsetof(struct bpf_sock_ops, snd_cwnd):
4471 SOCK_OPS_GET_TCP32(snd_cwnd);
4474 case offsetof(struct bpf_sock_ops, srtt_us):
4475 SOCK_OPS_GET_TCP32(srtt_us);
4478 return insn - insn_buf;
4481 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
4482 const struct bpf_insn *si,
4483 struct bpf_insn *insn_buf,
4484 struct bpf_prog *prog, u32 *target_size)
4486 struct bpf_insn *insn = insn_buf;
4490 case offsetof(struct __sk_buff, data_end):
4492 off -= offsetof(struct __sk_buff, data_end);
4493 off += offsetof(struct sk_buff, cb);
4494 off += offsetof(struct tcp_skb_cb, bpf.data_end);
4495 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
4499 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4503 return insn - insn_buf;
4506 const struct bpf_verifier_ops sk_filter_verifier_ops = {
4507 .get_func_proto = sk_filter_func_proto,
4508 .is_valid_access = sk_filter_is_valid_access,
4509 .convert_ctx_access = bpf_convert_ctx_access,
4512 const struct bpf_prog_ops sk_filter_prog_ops = {
4515 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
4516 .get_func_proto = tc_cls_act_func_proto,
4517 .is_valid_access = tc_cls_act_is_valid_access,
4518 .convert_ctx_access = tc_cls_act_convert_ctx_access,
4519 .gen_prologue = tc_cls_act_prologue,
4522 const struct bpf_prog_ops tc_cls_act_prog_ops = {
4523 .test_run = bpf_prog_test_run_skb,
4526 const struct bpf_verifier_ops xdp_verifier_ops = {
4527 .get_func_proto = xdp_func_proto,
4528 .is_valid_access = xdp_is_valid_access,
4529 .convert_ctx_access = xdp_convert_ctx_access,
4532 const struct bpf_prog_ops xdp_prog_ops = {
4533 .test_run = bpf_prog_test_run_xdp,
4536 const struct bpf_verifier_ops cg_skb_verifier_ops = {
4537 .get_func_proto = sk_filter_func_proto,
4538 .is_valid_access = sk_filter_is_valid_access,
4539 .convert_ctx_access = bpf_convert_ctx_access,
4542 const struct bpf_prog_ops cg_skb_prog_ops = {
4543 .test_run = bpf_prog_test_run_skb,
4546 const struct bpf_verifier_ops lwt_inout_verifier_ops = {
4547 .get_func_proto = lwt_inout_func_proto,
4548 .is_valid_access = lwt_is_valid_access,
4549 .convert_ctx_access = bpf_convert_ctx_access,
4552 const struct bpf_prog_ops lwt_inout_prog_ops = {
4553 .test_run = bpf_prog_test_run_skb,
4556 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
4557 .get_func_proto = lwt_xmit_func_proto,
4558 .is_valid_access = lwt_is_valid_access,
4559 .convert_ctx_access = bpf_convert_ctx_access,
4560 .gen_prologue = tc_cls_act_prologue,
4563 const struct bpf_prog_ops lwt_xmit_prog_ops = {
4564 .test_run = bpf_prog_test_run_skb,
4567 const struct bpf_verifier_ops cg_sock_verifier_ops = {
4568 .get_func_proto = sock_filter_func_proto,
4569 .is_valid_access = sock_filter_is_valid_access,
4570 .convert_ctx_access = sock_filter_convert_ctx_access,
4573 const struct bpf_prog_ops cg_sock_prog_ops = {
4576 const struct bpf_verifier_ops sock_ops_verifier_ops = {
4577 .get_func_proto = sock_ops_func_proto,
4578 .is_valid_access = sock_ops_is_valid_access,
4579 .convert_ctx_access = sock_ops_convert_ctx_access,
4582 const struct bpf_prog_ops sock_ops_prog_ops = {
4585 const struct bpf_verifier_ops sk_skb_verifier_ops = {
4586 .get_func_proto = sk_skb_func_proto,
4587 .is_valid_access = sk_skb_is_valid_access,
4588 .convert_ctx_access = sk_skb_convert_ctx_access,
4589 .gen_prologue = sk_skb_prologue,
4592 const struct bpf_prog_ops sk_skb_prog_ops = {
4595 int sk_detach_filter(struct sock *sk)
4598 struct sk_filter *filter;
4600 if (sock_flag(sk, SOCK_FILTER_LOCKED))
4603 filter = rcu_dereference_protected(sk->sk_filter,
4604 lockdep_sock_is_held(sk));
4606 RCU_INIT_POINTER(sk->sk_filter, NULL);
4607 sk_filter_uncharge(sk, filter);
4613 EXPORT_SYMBOL_GPL(sk_detach_filter);
4615 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
4618 struct sock_fprog_kern *fprog;
4619 struct sk_filter *filter;
4623 filter = rcu_dereference_protected(sk->sk_filter,
4624 lockdep_sock_is_held(sk));
4628 /* We're copying the filter that has been originally attached,
4629 * so no conversion/decode needed anymore. eBPF programs that
4630 * have no original program cannot be dumped through this.
4633 fprog = filter->prog->orig_prog;
4639 /* User space only enquires number of filter blocks. */
4643 if (len < fprog->len)
4647 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
4650 /* Instead of bytes, the API requests to return the number