]> git.ipfire.org Git - thirdparty/linux.git/blame - net/core/filter.c
bpf: minor cleanups in helpers
[thirdparty/linux.git] / net / core / filter.c
CommitLineData
1da177e4
LT
1/*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
bd4cf0ed
AS
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
1da177e4 6 *
bd4cf0ed
AS
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
8 *
9 * Authors:
10 *
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
1da177e4
LT
14 *
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.
19 *
20 * Andi Kleen - Fix a few bad bugs and races.
4df95ff4 21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
1da177e4
LT
22 */
23
24#include <linux/module.h>
25#include <linux/types.h>
1da177e4
LT
26#include <linux/mm.h>
27#include <linux/fcntl.h>
28#include <linux/socket.h>
29#include <linux/in.h>
30#include <linux/inet.h>
31#include <linux/netdevice.h>
32#include <linux/if_packet.h>
5a0e3ad6 33#include <linux/gfp.h>
1da177e4
LT
34#include <net/ip.h>
35#include <net/protocol.h>
4738c1db 36#include <net/netlink.h>
1da177e4
LT
37#include <linux/skbuff.h>
38#include <net/sock.h>
10b89ee4 39#include <net/flow_dissector.h>
1da177e4
LT
40#include <linux/errno.h>
41#include <linux/timer.h>
1da177e4 42#include <asm/uaccess.h>
40daafc8 43#include <asm/unaligned.h>
1da177e4 44#include <linux/filter.h>
86e4ca66 45#include <linux/ratelimit.h>
46b325c7 46#include <linux/seccomp.h>
f3335031 47#include <linux/if_vlan.h>
89aa0758 48#include <linux/bpf.h>
d691f9e8 49#include <net/sch_generic.h>
8d20aabe 50#include <net/cls_cgroup.h>
d3aa45ce 51#include <net/dst_metadata.h>
c46646d0 52#include <net/dst.h>
538950a1 53#include <net/sock_reuseport.h>
1da177e4 54
43db6d65 55/**
f4979fce 56 * sk_filter_trim_cap - run a packet through a socket filter
43db6d65
SH
57 * @sk: sock associated with &sk_buff
58 * @skb: buffer to filter
f4979fce 59 * @cap: limit on how short the eBPF program may trim the packet
43db6d65 60 *
ff936a04
AS
61 * Run the eBPF program and then cut skb->data to correct size returned by
62 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
43db6d65 63 * than pkt_len we keep whole skb->data. This is the socket level
ff936a04 64 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
43db6d65
SH
65 * be accepted or -EPERM if the packet should be tossed.
66 *
67 */
f4979fce 68int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
43db6d65
SH
69{
70 int err;
71 struct sk_filter *filter;
72
c93bdd0e
MG
73 /*
74 * If the skb was allocated from pfmemalloc reserves, only
75 * allow SOCK_MEMALLOC sockets to use it as this socket is
76 * helping free memory
77 */
78 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
79 return -ENOMEM;
80
43db6d65
SH
81 err = security_sock_rcv_skb(sk, skb);
82 if (err)
83 return err;
84
80f8f102
ED
85 rcu_read_lock();
86 filter = rcu_dereference(sk->sk_filter);
43db6d65 87 if (filter) {
ff936a04 88 unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
f4979fce 89 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
43db6d65 90 }
80f8f102 91 rcu_read_unlock();
43db6d65
SH
92
93 return err;
94}
f4979fce 95EXPORT_SYMBOL(sk_filter_trim_cap);
43db6d65 96
30743837 97static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
bd4cf0ed 98{
56193d1b 99 return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
bd4cf0ed
AS
100}
101
30743837 102static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
bd4cf0ed 103{
eb9672f4 104 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
bd4cf0ed
AS
105 struct nlattr *nla;
106
107 if (skb_is_nonlinear(skb))
108 return 0;
109
05ab8f26
MK
110 if (skb->len < sizeof(struct nlattr))
111 return 0;
112
30743837 113 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
114 return 0;
115
30743837 116 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
bd4cf0ed
AS
117 if (nla)
118 return (void *) nla - (void *) skb->data;
119
120 return 0;
121}
122
30743837 123static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
bd4cf0ed 124{
eb9672f4 125 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
bd4cf0ed
AS
126 struct nlattr *nla;
127
128 if (skb_is_nonlinear(skb))
129 return 0;
130
05ab8f26
MK
131 if (skb->len < sizeof(struct nlattr))
132 return 0;
133
30743837 134 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
135 return 0;
136
30743837
DB
137 nla = (struct nlattr *) &skb->data[a];
138 if (nla->nla_len > skb->len - a)
bd4cf0ed
AS
139 return 0;
140
30743837 141 nla = nla_find_nested(nla, x);
bd4cf0ed
AS
142 if (nla)
143 return (void *) nla - (void *) skb->data;
144
145 return 0;
146}
147
30743837 148static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
bd4cf0ed
AS
149{
150 return raw_smp_processor_id();
151}
152
80b48c44
DB
153static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
154 .func = __get_raw_cpu_id,
155 .gpl_only = false,
156 .ret_type = RET_INTEGER,
157};
158
9bac3d6d
AS
159static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
160 struct bpf_insn *insn_buf)
161{
162 struct bpf_insn *insn = insn_buf;
163
164 switch (skb_field) {
165 case SKF_AD_MARK:
166 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
167
168 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
169 offsetof(struct sk_buff, mark));
170 break;
171
172 case SKF_AD_PKTTYPE:
173 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
174 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
175#ifdef __BIG_ENDIAN_BITFIELD
176 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
177#endif
178 break;
179
180 case SKF_AD_QUEUE:
181 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
182
183 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
184 offsetof(struct sk_buff, queue_mapping));
185 break;
c2497395 186
c2497395
AS
187 case SKF_AD_VLAN_TAG:
188 case SKF_AD_VLAN_TAG_PRESENT:
189 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
190 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
191
192 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
193 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
194 offsetof(struct sk_buff, vlan_tci));
195 if (skb_field == SKF_AD_VLAN_TAG) {
196 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
197 ~VLAN_TAG_PRESENT);
198 } else {
199 /* dst_reg >>= 12 */
200 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
201 /* dst_reg &= 1 */
202 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
203 }
204 break;
9bac3d6d
AS
205 }
206
207 return insn - insn_buf;
208}
209
bd4cf0ed 210static bool convert_bpf_extensions(struct sock_filter *fp,
2695fb55 211 struct bpf_insn **insnp)
bd4cf0ed 212{
2695fb55 213 struct bpf_insn *insn = *insnp;
9bac3d6d 214 u32 cnt;
bd4cf0ed
AS
215
216 switch (fp->k) {
217 case SKF_AD_OFF + SKF_AD_PROTOCOL:
0b8c707d
DB
218 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
219
220 /* A = *(u16 *) (CTX + offsetof(protocol)) */
221 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
222 offsetof(struct sk_buff, protocol));
223 /* A = ntohs(A) [emitting a nop or swap16] */
224 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
bd4cf0ed
AS
225 break;
226
227 case SKF_AD_OFF + SKF_AD_PKTTYPE:
9bac3d6d
AS
228 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
229 insn += cnt - 1;
bd4cf0ed
AS
230 break;
231
232 case SKF_AD_OFF + SKF_AD_IFINDEX:
233 case SKF_AD_OFF + SKF_AD_HATYPE:
bd4cf0ed
AS
234 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
235 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
f8f6d679
DB
236 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
237
238 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
239 BPF_REG_TMP, BPF_REG_CTX,
240 offsetof(struct sk_buff, dev));
241 /* if (tmp != 0) goto pc + 1 */
242 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
243 *insn++ = BPF_EXIT_INSN();
244 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
245 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
246 offsetof(struct net_device, ifindex));
247 else
248 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
249 offsetof(struct net_device, type));
bd4cf0ed
AS
250 break;
251
252 case SKF_AD_OFF + SKF_AD_MARK:
9bac3d6d
AS
253 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
254 insn += cnt - 1;
bd4cf0ed
AS
255 break;
256
257 case SKF_AD_OFF + SKF_AD_RXHASH:
258 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
259
9739eef1
AS
260 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
261 offsetof(struct sk_buff, hash));
bd4cf0ed
AS
262 break;
263
264 case SKF_AD_OFF + SKF_AD_QUEUE:
9bac3d6d
AS
265 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
266 insn += cnt - 1;
bd4cf0ed
AS
267 break;
268
269 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
c2497395
AS
270 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
271 BPF_REG_A, BPF_REG_CTX, insn);
272 insn += cnt - 1;
273 break;
bd4cf0ed 274
c2497395
AS
275 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
276 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
277 BPF_REG_A, BPF_REG_CTX, insn);
278 insn += cnt - 1;
bd4cf0ed
AS
279 break;
280
27cd5452
MS
281 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
282 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
283
284 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
285 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
286 offsetof(struct sk_buff, vlan_proto));
287 /* A = ntohs(A) [emitting a nop or swap16] */
288 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
289 break;
290
bd4cf0ed
AS
291 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
292 case SKF_AD_OFF + SKF_AD_NLATTR:
293 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
294 case SKF_AD_OFF + SKF_AD_CPU:
4cd3675e 295 case SKF_AD_OFF + SKF_AD_RANDOM:
e430f34e 296 /* arg1 = CTX */
f8f6d679 297 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
bd4cf0ed 298 /* arg2 = A */
f8f6d679 299 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
bd4cf0ed 300 /* arg3 = X */
f8f6d679 301 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
e430f34e 302 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
bd4cf0ed
AS
303 switch (fp->k) {
304 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
f8f6d679 305 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
bd4cf0ed
AS
306 break;
307 case SKF_AD_OFF + SKF_AD_NLATTR:
f8f6d679 308 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
bd4cf0ed
AS
309 break;
310 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
f8f6d679 311 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
bd4cf0ed
AS
312 break;
313 case SKF_AD_OFF + SKF_AD_CPU:
f8f6d679 314 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
bd4cf0ed 315 break;
4cd3675e 316 case SKF_AD_OFF + SKF_AD_RANDOM:
3ad00405
DB
317 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
318 bpf_user_rnd_init_once();
4cd3675e 319 break;
bd4cf0ed
AS
320 }
321 break;
322
323 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
9739eef1
AS
324 /* A ^= X */
325 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
326 break;
327
328 default:
329 /* This is just a dummy call to avoid letting the compiler
330 * evict __bpf_call_base() as an optimization. Placed here
331 * where no-one bothers.
332 */
333 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
334 return false;
335 }
336
337 *insnp = insn;
338 return true;
339}
340
341/**
8fb575ca 342 * bpf_convert_filter - convert filter program
bd4cf0ed
AS
343 * @prog: the user passed filter program
344 * @len: the length of the user passed filter program
345 * @new_prog: buffer where converted program will be stored
346 * @new_len: pointer to store length of converted program
347 *
348 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
349 * Conversion workflow:
350 *
351 * 1) First pass for calculating the new program length:
8fb575ca 352 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
bd4cf0ed
AS
353 *
354 * 2) 2nd pass to remap in two passes: 1st pass finds new
355 * jump offsets, 2nd pass remapping:
2695fb55 356 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
8fb575ca 357 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
bd4cf0ed 358 */
d9e12f42
NS
359static int bpf_convert_filter(struct sock_filter *prog, int len,
360 struct bpf_insn *new_prog, int *new_len)
bd4cf0ed
AS
361{
362 int new_flen = 0, pass = 0, target, i;
2695fb55 363 struct bpf_insn *new_insn;
bd4cf0ed
AS
364 struct sock_filter *fp;
365 int *addrs = NULL;
366 u8 bpf_src;
367
368 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
30743837 369 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
bd4cf0ed 370
6f9a093b 371 if (len <= 0 || len > BPF_MAXINSNS)
bd4cf0ed
AS
372 return -EINVAL;
373
374 if (new_prog) {
658da937
DB
375 addrs = kcalloc(len, sizeof(*addrs),
376 GFP_KERNEL | __GFP_NOWARN);
bd4cf0ed
AS
377 if (!addrs)
378 return -ENOMEM;
379 }
380
381do_pass:
382 new_insn = new_prog;
383 fp = prog;
384
8b614aeb
DB
385 /* Classic BPF related prologue emission. */
386 if (new_insn) {
387 /* Classic BPF expects A and X to be reset first. These need
388 * to be guaranteed to be the first two instructions.
389 */
390 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
391 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
392
393 /* All programs must keep CTX in callee saved BPF_REG_CTX.
394 * In eBPF case it's done by the compiler, here we need to
395 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
396 */
397 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
398 } else {
399 new_insn += 3;
400 }
bd4cf0ed
AS
401
402 for (i = 0; i < len; fp++, i++) {
2695fb55
AS
403 struct bpf_insn tmp_insns[6] = { };
404 struct bpf_insn *insn = tmp_insns;
bd4cf0ed
AS
405
406 if (addrs)
407 addrs[i] = new_insn - new_prog;
408
409 switch (fp->code) {
410 /* All arithmetic insns and skb loads map as-is. */
411 case BPF_ALU | BPF_ADD | BPF_X:
412 case BPF_ALU | BPF_ADD | BPF_K:
413 case BPF_ALU | BPF_SUB | BPF_X:
414 case BPF_ALU | BPF_SUB | BPF_K:
415 case BPF_ALU | BPF_AND | BPF_X:
416 case BPF_ALU | BPF_AND | BPF_K:
417 case BPF_ALU | BPF_OR | BPF_X:
418 case BPF_ALU | BPF_OR | BPF_K:
419 case BPF_ALU | BPF_LSH | BPF_X:
420 case BPF_ALU | BPF_LSH | BPF_K:
421 case BPF_ALU | BPF_RSH | BPF_X:
422 case BPF_ALU | BPF_RSH | BPF_K:
423 case BPF_ALU | BPF_XOR | BPF_X:
424 case BPF_ALU | BPF_XOR | BPF_K:
425 case BPF_ALU | BPF_MUL | BPF_X:
426 case BPF_ALU | BPF_MUL | BPF_K:
427 case BPF_ALU | BPF_DIV | BPF_X:
428 case BPF_ALU | BPF_DIV | BPF_K:
429 case BPF_ALU | BPF_MOD | BPF_X:
430 case BPF_ALU | BPF_MOD | BPF_K:
431 case BPF_ALU | BPF_NEG:
432 case BPF_LD | BPF_ABS | BPF_W:
433 case BPF_LD | BPF_ABS | BPF_H:
434 case BPF_LD | BPF_ABS | BPF_B:
435 case BPF_LD | BPF_IND | BPF_W:
436 case BPF_LD | BPF_IND | BPF_H:
437 case BPF_LD | BPF_IND | BPF_B:
438 /* Check for overloaded BPF extension and
439 * directly convert it if found, otherwise
440 * just move on with mapping.
441 */
442 if (BPF_CLASS(fp->code) == BPF_LD &&
443 BPF_MODE(fp->code) == BPF_ABS &&
444 convert_bpf_extensions(fp, &insn))
445 break;
446
f8f6d679 447 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
bd4cf0ed
AS
448 break;
449
f8f6d679
DB
450 /* Jump transformation cannot use BPF block macros
451 * everywhere as offset calculation and target updates
452 * require a bit more work than the rest, i.e. jump
453 * opcodes map as-is, but offsets need adjustment.
454 */
455
456#define BPF_EMIT_JMP \
bd4cf0ed
AS
457 do { \
458 if (target >= len || target < 0) \
459 goto err; \
460 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
461 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
462 insn->off -= insn - tmp_insns; \
463 } while (0)
464
f8f6d679
DB
465 case BPF_JMP | BPF_JA:
466 target = i + fp->k + 1;
467 insn->code = fp->code;
468 BPF_EMIT_JMP;
bd4cf0ed
AS
469 break;
470
471 case BPF_JMP | BPF_JEQ | BPF_K:
472 case BPF_JMP | BPF_JEQ | BPF_X:
473 case BPF_JMP | BPF_JSET | BPF_K:
474 case BPF_JMP | BPF_JSET | BPF_X:
475 case BPF_JMP | BPF_JGT | BPF_K:
476 case BPF_JMP | BPF_JGT | BPF_X:
477 case BPF_JMP | BPF_JGE | BPF_K:
478 case BPF_JMP | BPF_JGE | BPF_X:
479 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
480 /* BPF immediates are signed, zero extend
481 * immediate into tmp register and use it
482 * in compare insn.
483 */
f8f6d679 484 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
bd4cf0ed 485
e430f34e
AS
486 insn->dst_reg = BPF_REG_A;
487 insn->src_reg = BPF_REG_TMP;
bd4cf0ed
AS
488 bpf_src = BPF_X;
489 } else {
e430f34e 490 insn->dst_reg = BPF_REG_A;
bd4cf0ed
AS
491 insn->imm = fp->k;
492 bpf_src = BPF_SRC(fp->code);
19539ce7 493 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
1da177e4 494 }
bd4cf0ed
AS
495
496 /* Common case where 'jump_false' is next insn. */
497 if (fp->jf == 0) {
498 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
499 target = i + fp->jt + 1;
f8f6d679 500 BPF_EMIT_JMP;
bd4cf0ed 501 break;
1da177e4 502 }
bd4cf0ed
AS
503
504 /* Convert JEQ into JNE when 'jump_true' is next insn. */
505 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
506 insn->code = BPF_JMP | BPF_JNE | bpf_src;
507 target = i + fp->jf + 1;
f8f6d679 508 BPF_EMIT_JMP;
bd4cf0ed 509 break;
0b05b2a4 510 }
bd4cf0ed
AS
511
512 /* Other jumps are mapped into two insns: Jxx and JA. */
513 target = i + fp->jt + 1;
514 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
f8f6d679 515 BPF_EMIT_JMP;
bd4cf0ed
AS
516 insn++;
517
518 insn->code = BPF_JMP | BPF_JA;
519 target = i + fp->jf + 1;
f8f6d679 520 BPF_EMIT_JMP;
bd4cf0ed
AS
521 break;
522
523 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
524 case BPF_LDX | BPF_MSH | BPF_B:
9739eef1 525 /* tmp = A */
f8f6d679 526 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
1268e253 527 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
f8f6d679 528 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
9739eef1 529 /* A &= 0xf */
f8f6d679 530 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
9739eef1 531 /* A <<= 2 */
f8f6d679 532 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
9739eef1 533 /* X = A */
f8f6d679 534 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
9739eef1 535 /* A = tmp */
f8f6d679 536 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
bd4cf0ed
AS
537 break;
538
6205b9cf
DB
539 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
540 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
541 */
bd4cf0ed
AS
542 case BPF_RET | BPF_A:
543 case BPF_RET | BPF_K:
6205b9cf
DB
544 if (BPF_RVAL(fp->code) == BPF_K)
545 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
546 0, fp->k);
9739eef1 547 *insn = BPF_EXIT_INSN();
bd4cf0ed
AS
548 break;
549
550 /* Store to stack. */
551 case BPF_ST:
552 case BPF_STX:
f8f6d679
DB
553 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
554 BPF_ST ? BPF_REG_A : BPF_REG_X,
555 -(BPF_MEMWORDS - fp->k) * 4);
bd4cf0ed
AS
556 break;
557
558 /* Load from stack. */
559 case BPF_LD | BPF_MEM:
560 case BPF_LDX | BPF_MEM:
f8f6d679
DB
561 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
562 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
563 -(BPF_MEMWORDS - fp->k) * 4);
bd4cf0ed
AS
564 break;
565
566 /* A = K or X = K */
567 case BPF_LD | BPF_IMM:
568 case BPF_LDX | BPF_IMM:
f8f6d679
DB
569 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
570 BPF_REG_A : BPF_REG_X, fp->k);
bd4cf0ed
AS
571 break;
572
573 /* X = A */
574 case BPF_MISC | BPF_TAX:
f8f6d679 575 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
bd4cf0ed
AS
576 break;
577
578 /* A = X */
579 case BPF_MISC | BPF_TXA:
f8f6d679 580 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
581 break;
582
583 /* A = skb->len or X = skb->len */
584 case BPF_LD | BPF_W | BPF_LEN:
585 case BPF_LDX | BPF_W | BPF_LEN:
f8f6d679
DB
586 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
587 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
588 offsetof(struct sk_buff, len));
bd4cf0ed
AS
589 break;
590
f8f6d679 591 /* Access seccomp_data fields. */
bd4cf0ed 592 case BPF_LDX | BPF_ABS | BPF_W:
9739eef1
AS
593 /* A = *(u32 *) (ctx + K) */
594 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
bd4cf0ed
AS
595 break;
596
ca9f1fd2 597 /* Unknown instruction. */
1da177e4 598 default:
bd4cf0ed 599 goto err;
1da177e4 600 }
bd4cf0ed
AS
601
602 insn++;
603 if (new_prog)
604 memcpy(new_insn, tmp_insns,
605 sizeof(*insn) * (insn - tmp_insns));
bd4cf0ed 606 new_insn += insn - tmp_insns;
1da177e4
LT
607 }
608
bd4cf0ed
AS
609 if (!new_prog) {
610 /* Only calculating new length. */
611 *new_len = new_insn - new_prog;
612 return 0;
613 }
614
615 pass++;
616 if (new_flen != new_insn - new_prog) {
617 new_flen = new_insn - new_prog;
618 if (pass > 2)
619 goto err;
bd4cf0ed
AS
620 goto do_pass;
621 }
622
623 kfree(addrs);
624 BUG_ON(*new_len != new_flen);
1da177e4 625 return 0;
bd4cf0ed
AS
626err:
627 kfree(addrs);
628 return -EINVAL;
1da177e4
LT
629}
630
bd4cf0ed 631/* Security:
bd4cf0ed 632 *
2d5311e4 633 * As we dont want to clear mem[] array for each packet going through
8ea6e345 634 * __bpf_prog_run(), we check that filter loaded by user never try to read
2d5311e4 635 * a cell if not previously written, and we check all branches to be sure
25985edc 636 * a malicious user doesn't try to abuse us.
2d5311e4 637 */
ec31a05c 638static int check_load_and_stores(const struct sock_filter *filter, int flen)
2d5311e4 639{
34805931 640 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
2d5311e4
ED
641 int pc, ret = 0;
642
643 BUILD_BUG_ON(BPF_MEMWORDS > 16);
34805931 644
99e72a0f 645 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
2d5311e4
ED
646 if (!masks)
647 return -ENOMEM;
34805931 648
2d5311e4
ED
649 memset(masks, 0xff, flen * sizeof(*masks));
650
651 for (pc = 0; pc < flen; pc++) {
652 memvalid &= masks[pc];
653
654 switch (filter[pc].code) {
34805931
DB
655 case BPF_ST:
656 case BPF_STX:
2d5311e4
ED
657 memvalid |= (1 << filter[pc].k);
658 break;
34805931
DB
659 case BPF_LD | BPF_MEM:
660 case BPF_LDX | BPF_MEM:
2d5311e4
ED
661 if (!(memvalid & (1 << filter[pc].k))) {
662 ret = -EINVAL;
663 goto error;
664 }
665 break;
34805931
DB
666 case BPF_JMP | BPF_JA:
667 /* A jump must set masks on target */
2d5311e4
ED
668 masks[pc + 1 + filter[pc].k] &= memvalid;
669 memvalid = ~0;
670 break;
34805931
DB
671 case BPF_JMP | BPF_JEQ | BPF_K:
672 case BPF_JMP | BPF_JEQ | BPF_X:
673 case BPF_JMP | BPF_JGE | BPF_K:
674 case BPF_JMP | BPF_JGE | BPF_X:
675 case BPF_JMP | BPF_JGT | BPF_K:
676 case BPF_JMP | BPF_JGT | BPF_X:
677 case BPF_JMP | BPF_JSET | BPF_K:
678 case BPF_JMP | BPF_JSET | BPF_X:
679 /* A jump must set masks on targets */
2d5311e4
ED
680 masks[pc + 1 + filter[pc].jt] &= memvalid;
681 masks[pc + 1 + filter[pc].jf] &= memvalid;
682 memvalid = ~0;
683 break;
684 }
685 }
686error:
687 kfree(masks);
688 return ret;
689}
690
34805931
DB
691static bool chk_code_allowed(u16 code_to_probe)
692{
693 static const bool codes[] = {
694 /* 32 bit ALU operations */
695 [BPF_ALU | BPF_ADD | BPF_K] = true,
696 [BPF_ALU | BPF_ADD | BPF_X] = true,
697 [BPF_ALU | BPF_SUB | BPF_K] = true,
698 [BPF_ALU | BPF_SUB | BPF_X] = true,
699 [BPF_ALU | BPF_MUL | BPF_K] = true,
700 [BPF_ALU | BPF_MUL | BPF_X] = true,
701 [BPF_ALU | BPF_DIV | BPF_K] = true,
702 [BPF_ALU | BPF_DIV | BPF_X] = true,
703 [BPF_ALU | BPF_MOD | BPF_K] = true,
704 [BPF_ALU | BPF_MOD | BPF_X] = true,
705 [BPF_ALU | BPF_AND | BPF_K] = true,
706 [BPF_ALU | BPF_AND | BPF_X] = true,
707 [BPF_ALU | BPF_OR | BPF_K] = true,
708 [BPF_ALU | BPF_OR | BPF_X] = true,
709 [BPF_ALU | BPF_XOR | BPF_K] = true,
710 [BPF_ALU | BPF_XOR | BPF_X] = true,
711 [BPF_ALU | BPF_LSH | BPF_K] = true,
712 [BPF_ALU | BPF_LSH | BPF_X] = true,
713 [BPF_ALU | BPF_RSH | BPF_K] = true,
714 [BPF_ALU | BPF_RSH | BPF_X] = true,
715 [BPF_ALU | BPF_NEG] = true,
716 /* Load instructions */
717 [BPF_LD | BPF_W | BPF_ABS] = true,
718 [BPF_LD | BPF_H | BPF_ABS] = true,
719 [BPF_LD | BPF_B | BPF_ABS] = true,
720 [BPF_LD | BPF_W | BPF_LEN] = true,
721 [BPF_LD | BPF_W | BPF_IND] = true,
722 [BPF_LD | BPF_H | BPF_IND] = true,
723 [BPF_LD | BPF_B | BPF_IND] = true,
724 [BPF_LD | BPF_IMM] = true,
725 [BPF_LD | BPF_MEM] = true,
726 [BPF_LDX | BPF_W | BPF_LEN] = true,
727 [BPF_LDX | BPF_B | BPF_MSH] = true,
728 [BPF_LDX | BPF_IMM] = true,
729 [BPF_LDX | BPF_MEM] = true,
730 /* Store instructions */
731 [BPF_ST] = true,
732 [BPF_STX] = true,
733 /* Misc instructions */
734 [BPF_MISC | BPF_TAX] = true,
735 [BPF_MISC | BPF_TXA] = true,
736 /* Return instructions */
737 [BPF_RET | BPF_K] = true,
738 [BPF_RET | BPF_A] = true,
739 /* Jump instructions */
740 [BPF_JMP | BPF_JA] = true,
741 [BPF_JMP | BPF_JEQ | BPF_K] = true,
742 [BPF_JMP | BPF_JEQ | BPF_X] = true,
743 [BPF_JMP | BPF_JGE | BPF_K] = true,
744 [BPF_JMP | BPF_JGE | BPF_X] = true,
745 [BPF_JMP | BPF_JGT | BPF_K] = true,
746 [BPF_JMP | BPF_JGT | BPF_X] = true,
747 [BPF_JMP | BPF_JSET | BPF_K] = true,
748 [BPF_JMP | BPF_JSET | BPF_X] = true,
749 };
750
751 if (code_to_probe >= ARRAY_SIZE(codes))
752 return false;
753
754 return codes[code_to_probe];
755}
756
f7bd9e36
DB
757static bool bpf_check_basics_ok(const struct sock_filter *filter,
758 unsigned int flen)
759{
760 if (filter == NULL)
761 return false;
762 if (flen == 0 || flen > BPF_MAXINSNS)
763 return false;
764
765 return true;
766}
767
1da177e4 768/**
4df95ff4 769 * bpf_check_classic - verify socket filter code
1da177e4
LT
770 * @filter: filter to verify
771 * @flen: length of filter
772 *
773 * Check the user's filter code. If we let some ugly
774 * filter code slip through kaboom! The filter must contain
93699863
KK
775 * no references or jumps that are out of range, no illegal
776 * instructions, and must end with a RET instruction.
1da177e4 777 *
7b11f69f
KK
778 * All jumps are forward as they are not signed.
779 *
780 * Returns 0 if the rule set is legal or -EINVAL if not.
1da177e4 781 */
d9e12f42
NS
782static int bpf_check_classic(const struct sock_filter *filter,
783 unsigned int flen)
1da177e4 784{
aa1113d9 785 bool anc_found;
34805931 786 int pc;
1da177e4 787
34805931 788 /* Check the filter code now */
1da177e4 789 for (pc = 0; pc < flen; pc++) {
ec31a05c 790 const struct sock_filter *ftest = &filter[pc];
93699863 791
34805931
DB
792 /* May we actually operate on this code? */
793 if (!chk_code_allowed(ftest->code))
cba328fc 794 return -EINVAL;
34805931 795
93699863 796 /* Some instructions need special checks */
34805931
DB
797 switch (ftest->code) {
798 case BPF_ALU | BPF_DIV | BPF_K:
799 case BPF_ALU | BPF_MOD | BPF_K:
800 /* Check for division by zero */
b6069a95
ED
801 if (ftest->k == 0)
802 return -EINVAL;
803 break;
229394e8
RV
804 case BPF_ALU | BPF_LSH | BPF_K:
805 case BPF_ALU | BPF_RSH | BPF_K:
806 if (ftest->k >= 32)
807 return -EINVAL;
808 break;
34805931
DB
809 case BPF_LD | BPF_MEM:
810 case BPF_LDX | BPF_MEM:
811 case BPF_ST:
812 case BPF_STX:
813 /* Check for invalid memory addresses */
93699863
KK
814 if (ftest->k >= BPF_MEMWORDS)
815 return -EINVAL;
816 break;
34805931
DB
817 case BPF_JMP | BPF_JA:
818 /* Note, the large ftest->k might cause loops.
93699863
KK
819 * Compare this with conditional jumps below,
820 * where offsets are limited. --ANK (981016)
821 */
34805931 822 if (ftest->k >= (unsigned int)(flen - pc - 1))
93699863 823 return -EINVAL;
01f2f3f6 824 break;
34805931
DB
825 case BPF_JMP | BPF_JEQ | BPF_K:
826 case BPF_JMP | BPF_JEQ | BPF_X:
827 case BPF_JMP | BPF_JGE | BPF_K:
828 case BPF_JMP | BPF_JGE | BPF_X:
829 case BPF_JMP | BPF_JGT | BPF_K:
830 case BPF_JMP | BPF_JGT | BPF_X:
831 case BPF_JMP | BPF_JSET | BPF_K:
832 case BPF_JMP | BPF_JSET | BPF_X:
833 /* Both conditionals must be safe */
e35bedf3 834 if (pc + ftest->jt + 1 >= flen ||
93699863
KK
835 pc + ftest->jf + 1 >= flen)
836 return -EINVAL;
cba328fc 837 break;
34805931
DB
838 case BPF_LD | BPF_W | BPF_ABS:
839 case BPF_LD | BPF_H | BPF_ABS:
840 case BPF_LD | BPF_B | BPF_ABS:
aa1113d9 841 anc_found = false;
34805931
DB
842 if (bpf_anc_helper(ftest) & BPF_ANC)
843 anc_found = true;
844 /* Ancillary operation unknown or unsupported */
aa1113d9
DB
845 if (anc_found == false && ftest->k >= SKF_AD_OFF)
846 return -EINVAL;
01f2f3f6
HPP
847 }
848 }
93699863 849
34805931 850 /* Last instruction must be a RET code */
01f2f3f6 851 switch (filter[flen - 1].code) {
34805931
DB
852 case BPF_RET | BPF_K:
853 case BPF_RET | BPF_A:
2d5311e4 854 return check_load_and_stores(filter, flen);
cba328fc 855 }
34805931 856
cba328fc 857 return -EINVAL;
1da177e4
LT
858}
859
7ae457c1
AS
860static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
861 const struct sock_fprog *fprog)
a3ea269b 862{
009937e7 863 unsigned int fsize = bpf_classic_proglen(fprog);
a3ea269b
DB
864 struct sock_fprog_kern *fkprog;
865
866 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
867 if (!fp->orig_prog)
868 return -ENOMEM;
869
870 fkprog = fp->orig_prog;
871 fkprog->len = fprog->len;
658da937
DB
872
873 fkprog->filter = kmemdup(fp->insns, fsize,
874 GFP_KERNEL | __GFP_NOWARN);
a3ea269b
DB
875 if (!fkprog->filter) {
876 kfree(fp->orig_prog);
877 return -ENOMEM;
878 }
879
880 return 0;
881}
882
7ae457c1 883static void bpf_release_orig_filter(struct bpf_prog *fp)
a3ea269b
DB
884{
885 struct sock_fprog_kern *fprog = fp->orig_prog;
886
887 if (fprog) {
888 kfree(fprog->filter);
889 kfree(fprog);
890 }
891}
892
7ae457c1
AS
893static void __bpf_prog_release(struct bpf_prog *prog)
894{
24701ece 895 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
89aa0758
AS
896 bpf_prog_put(prog);
897 } else {
898 bpf_release_orig_filter(prog);
899 bpf_prog_free(prog);
900 }
7ae457c1
AS
901}
902
34c5bd66
PN
903static void __sk_filter_release(struct sk_filter *fp)
904{
7ae457c1
AS
905 __bpf_prog_release(fp->prog);
906 kfree(fp);
34c5bd66
PN
907}
908
47e958ea 909/**
46bcf14f 910 * sk_filter_release_rcu - Release a socket filter by rcu_head
47e958ea
PE
911 * @rcu: rcu_head that contains the sk_filter to free
912 */
fbc907f0 913static void sk_filter_release_rcu(struct rcu_head *rcu)
47e958ea
PE
914{
915 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
916
34c5bd66 917 __sk_filter_release(fp);
47e958ea 918}
fbc907f0
DB
919
920/**
921 * sk_filter_release - release a socket filter
922 * @fp: filter to remove
923 *
924 * Remove a filter from a socket and release its resources.
925 */
926static void sk_filter_release(struct sk_filter *fp)
927{
928 if (atomic_dec_and_test(&fp->refcnt))
929 call_rcu(&fp->rcu, sk_filter_release_rcu);
930}
931
932void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
933{
7ae457c1 934 u32 filter_size = bpf_prog_size(fp->prog->len);
fbc907f0 935
278571ba
AS
936 atomic_sub(filter_size, &sk->sk_omem_alloc);
937 sk_filter_release(fp);
fbc907f0 938}
47e958ea 939
278571ba
AS
940/* try to charge the socket memory if there is space available
941 * return true on success
942 */
943bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
bd4cf0ed 944{
7ae457c1 945 u32 filter_size = bpf_prog_size(fp->prog->len);
278571ba
AS
946
947 /* same check as in sock_kmalloc() */
948 if (filter_size <= sysctl_optmem_max &&
949 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
950 atomic_inc(&fp->refcnt);
951 atomic_add(filter_size, &sk->sk_omem_alloc);
952 return true;
bd4cf0ed 953 }
278571ba 954 return false;
bd4cf0ed
AS
955}
956
7ae457c1 957static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
bd4cf0ed
AS
958{
959 struct sock_filter *old_prog;
7ae457c1 960 struct bpf_prog *old_fp;
34805931 961 int err, new_len, old_len = fp->len;
bd4cf0ed
AS
962
963 /* We are free to overwrite insns et al right here as it
964 * won't be used at this point in time anymore internally
965 * after the migration to the internal BPF instruction
966 * representation.
967 */
968 BUILD_BUG_ON(sizeof(struct sock_filter) !=
2695fb55 969 sizeof(struct bpf_insn));
bd4cf0ed 970
bd4cf0ed
AS
971 /* Conversion cannot happen on overlapping memory areas,
972 * so we need to keep the user BPF around until the 2nd
973 * pass. At this time, the user BPF is stored in fp->insns.
974 */
975 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
658da937 976 GFP_KERNEL | __GFP_NOWARN);
bd4cf0ed
AS
977 if (!old_prog) {
978 err = -ENOMEM;
979 goto out_err;
980 }
981
982 /* 1st pass: calculate the new program length. */
8fb575ca 983 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
bd4cf0ed
AS
984 if (err)
985 goto out_err_free;
986
987 /* Expand fp for appending the new filter representation. */
988 old_fp = fp;
60a3b225 989 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
bd4cf0ed
AS
990 if (!fp) {
991 /* The old_fp is still around in case we couldn't
992 * allocate new memory, so uncharge on that one.
993 */
994 fp = old_fp;
995 err = -ENOMEM;
996 goto out_err_free;
997 }
998
bd4cf0ed
AS
999 fp->len = new_len;
1000
2695fb55 1001 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
8fb575ca 1002 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
bd4cf0ed 1003 if (err)
8fb575ca 1004 /* 2nd bpf_convert_filter() can fail only if it fails
bd4cf0ed
AS
1005 * to allocate memory, remapping must succeed. Note,
1006 * that at this time old_fp has already been released
278571ba 1007 * by krealloc().
bd4cf0ed
AS
1008 */
1009 goto out_err_free;
1010
d1c55ab5
DB
1011 /* We are guaranteed to never error here with cBPF to eBPF
1012 * transitions, since there's no issue with type compatibility
1013 * checks on program arrays.
1014 */
1015 fp = bpf_prog_select_runtime(fp, &err);
5fe821a9 1016
bd4cf0ed
AS
1017 kfree(old_prog);
1018 return fp;
1019
1020out_err_free:
1021 kfree(old_prog);
1022out_err:
7ae457c1 1023 __bpf_prog_release(fp);
bd4cf0ed
AS
1024 return ERR_PTR(err);
1025}
1026
ac67eb2c
DB
1027static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1028 bpf_aux_classic_check_t trans)
302d6637
JP
1029{
1030 int err;
1031
bd4cf0ed 1032 fp->bpf_func = NULL;
a91263d5 1033 fp->jited = 0;
302d6637 1034
4df95ff4 1035 err = bpf_check_classic(fp->insns, fp->len);
418c96ac 1036 if (err) {
7ae457c1 1037 __bpf_prog_release(fp);
bd4cf0ed 1038 return ERR_PTR(err);
418c96ac 1039 }
302d6637 1040
4ae92bc7
NS
1041 /* There might be additional checks and transformations
1042 * needed on classic filters, f.e. in case of seccomp.
1043 */
1044 if (trans) {
1045 err = trans(fp->insns, fp->len);
1046 if (err) {
1047 __bpf_prog_release(fp);
1048 return ERR_PTR(err);
1049 }
1050 }
1051
bd4cf0ed
AS
1052 /* Probe if we can JIT compile the filter and if so, do
1053 * the compilation of the filter.
1054 */
302d6637 1055 bpf_jit_compile(fp);
bd4cf0ed
AS
1056
1057 /* JIT compiler couldn't process this filter, so do the
1058 * internal BPF translation for the optimized interpreter.
1059 */
5fe821a9 1060 if (!fp->jited)
7ae457c1 1061 fp = bpf_migrate_filter(fp);
bd4cf0ed
AS
1062
1063 return fp;
302d6637
JP
1064}
1065
1066/**
7ae457c1 1067 * bpf_prog_create - create an unattached filter
c6c4b97c 1068 * @pfp: the unattached filter that is created
677a9fd3 1069 * @fprog: the filter program
302d6637 1070 *
c6c4b97c 1071 * Create a filter independent of any socket. We first run some
302d6637
JP
1072 * sanity checks on it to make sure it does not explode on us later.
1073 * If an error occurs or there is insufficient memory for the filter
1074 * a negative errno code is returned. On success the return is zero.
1075 */
7ae457c1 1076int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
302d6637 1077{
009937e7 1078 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1 1079 struct bpf_prog *fp;
302d6637
JP
1080
1081 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1082 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
302d6637
JP
1083 return -EINVAL;
1084
60a3b225 1085 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
302d6637
JP
1086 if (!fp)
1087 return -ENOMEM;
a3ea269b 1088
302d6637
JP
1089 memcpy(fp->insns, fprog->filter, fsize);
1090
302d6637 1091 fp->len = fprog->len;
a3ea269b
DB
1092 /* Since unattached filters are not copied back to user
1093 * space through sk_get_filter(), we do not need to hold
1094 * a copy here, and can spare us the work.
1095 */
1096 fp->orig_prog = NULL;
302d6637 1097
7ae457c1 1098 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1099 * memory in case something goes wrong.
1100 */
4ae92bc7 1101 fp = bpf_prepare_filter(fp, NULL);
bd4cf0ed
AS
1102 if (IS_ERR(fp))
1103 return PTR_ERR(fp);
302d6637
JP
1104
1105 *pfp = fp;
1106 return 0;
302d6637 1107}
7ae457c1 1108EXPORT_SYMBOL_GPL(bpf_prog_create);
302d6637 1109
ac67eb2c
DB
1110/**
1111 * bpf_prog_create_from_user - create an unattached filter from user buffer
1112 * @pfp: the unattached filter that is created
1113 * @fprog: the filter program
1114 * @trans: post-classic verifier transformation handler
bab18991 1115 * @save_orig: save classic BPF program
ac67eb2c
DB
1116 *
1117 * This function effectively does the same as bpf_prog_create(), only
1118 * that it builds up its insns buffer from user space provided buffer.
1119 * It also allows for passing a bpf_aux_classic_check_t handler.
1120 */
1121int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
bab18991 1122 bpf_aux_classic_check_t trans, bool save_orig)
ac67eb2c
DB
1123{
1124 unsigned int fsize = bpf_classic_proglen(fprog);
1125 struct bpf_prog *fp;
bab18991 1126 int err;
ac67eb2c
DB
1127
1128 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1129 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
ac67eb2c
DB
1130 return -EINVAL;
1131
1132 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1133 if (!fp)
1134 return -ENOMEM;
1135
1136 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1137 __bpf_prog_free(fp);
1138 return -EFAULT;
1139 }
1140
1141 fp->len = fprog->len;
ac67eb2c
DB
1142 fp->orig_prog = NULL;
1143
bab18991
DB
1144 if (save_orig) {
1145 err = bpf_prog_store_orig_filter(fp, fprog);
1146 if (err) {
1147 __bpf_prog_free(fp);
1148 return -ENOMEM;
1149 }
1150 }
1151
ac67eb2c
DB
1152 /* bpf_prepare_filter() already takes care of freeing
1153 * memory in case something goes wrong.
1154 */
1155 fp = bpf_prepare_filter(fp, trans);
1156 if (IS_ERR(fp))
1157 return PTR_ERR(fp);
1158
1159 *pfp = fp;
1160 return 0;
1161}
2ea273d7 1162EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
ac67eb2c 1163
7ae457c1 1164void bpf_prog_destroy(struct bpf_prog *fp)
302d6637 1165{
7ae457c1 1166 __bpf_prog_release(fp);
302d6637 1167}
7ae457c1 1168EXPORT_SYMBOL_GPL(bpf_prog_destroy);
302d6637 1169
8ced425e 1170static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
49b31e57
DB
1171{
1172 struct sk_filter *fp, *old_fp;
1173
1174 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1175 if (!fp)
1176 return -ENOMEM;
1177
1178 fp->prog = prog;
1179 atomic_set(&fp->refcnt, 0);
1180
1181 if (!sk_filter_charge(sk, fp)) {
1182 kfree(fp);
1183 return -ENOMEM;
1184 }
1185
8ced425e
HFS
1186 old_fp = rcu_dereference_protected(sk->sk_filter,
1187 lockdep_sock_is_held(sk));
49b31e57 1188 rcu_assign_pointer(sk->sk_filter, fp);
8ced425e 1189
49b31e57
DB
1190 if (old_fp)
1191 sk_filter_uncharge(sk, old_fp);
1192
1193 return 0;
1194}
1195
538950a1
CG
1196static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1197{
1198 struct bpf_prog *old_prog;
1199 int err;
1200
1201 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1202 return -ENOMEM;
1203
fa463497 1204 if (sk_unhashed(sk) && sk->sk_reuseport) {
538950a1
CG
1205 err = reuseport_alloc(sk);
1206 if (err)
1207 return err;
1208 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1209 /* The socket wasn't bound with SO_REUSEPORT */
1210 return -EINVAL;
1211 }
1212
1213 old_prog = reuseport_attach_prog(sk, prog);
1214 if (old_prog)
1215 bpf_prog_destroy(old_prog);
1216
1217 return 0;
1218}
1219
1220static
1221struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1da177e4 1222{
009937e7 1223 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1 1224 struct bpf_prog *prog;
1da177e4
LT
1225 int err;
1226
d59577b6 1227 if (sock_flag(sk, SOCK_FILTER_LOCKED))
538950a1 1228 return ERR_PTR(-EPERM);
d59577b6 1229
1da177e4 1230 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1231 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
538950a1 1232 return ERR_PTR(-EINVAL);
1da177e4 1233
f7bd9e36 1234 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
7ae457c1 1235 if (!prog)
538950a1 1236 return ERR_PTR(-ENOMEM);
a3ea269b 1237
7ae457c1 1238 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
c0d1379a 1239 __bpf_prog_free(prog);
538950a1 1240 return ERR_PTR(-EFAULT);
1da177e4
LT
1241 }
1242
7ae457c1 1243 prog->len = fprog->len;
1da177e4 1244
7ae457c1 1245 err = bpf_prog_store_orig_filter(prog, fprog);
a3ea269b 1246 if (err) {
c0d1379a 1247 __bpf_prog_free(prog);
538950a1 1248 return ERR_PTR(-ENOMEM);
a3ea269b
DB
1249 }
1250
7ae457c1 1251 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1252 * memory in case something goes wrong.
1253 */
538950a1
CG
1254 return bpf_prepare_filter(prog, NULL);
1255}
1256
1257/**
1258 * sk_attach_filter - attach a socket filter
1259 * @fprog: the filter program
1260 * @sk: the socket to use
1261 *
1262 * Attach the user's filter code. We first run some sanity checks on
1263 * it to make sure it does not explode on us later. If an error
1264 * occurs or there is insufficient memory for the filter a negative
1265 * errno code is returned. On success the return is zero.
1266 */
8ced425e 1267int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
538950a1
CG
1268{
1269 struct bpf_prog *prog = __get_filter(fprog, sk);
1270 int err;
1271
7ae457c1
AS
1272 if (IS_ERR(prog))
1273 return PTR_ERR(prog);
1274
8ced425e 1275 err = __sk_attach_prog(prog, sk);
49b31e57 1276 if (err < 0) {
7ae457c1 1277 __bpf_prog_release(prog);
49b31e57 1278 return err;
278571ba
AS
1279 }
1280
d3904b73 1281 return 0;
1da177e4 1282}
8ced425e 1283EXPORT_SYMBOL_GPL(sk_attach_filter);
1da177e4 1284
538950a1 1285int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
89aa0758 1286{
538950a1 1287 struct bpf_prog *prog = __get_filter(fprog, sk);
49b31e57 1288 int err;
89aa0758 1289
538950a1
CG
1290 if (IS_ERR(prog))
1291 return PTR_ERR(prog);
1292
1293 err = __reuseport_attach_prog(prog, sk);
1294 if (err < 0) {
1295 __bpf_prog_release(prog);
1296 return err;
1297 }
1298
1299 return 0;
1300}
1301
1302static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1303{
89aa0758 1304 if (sock_flag(sk, SOCK_FILTER_LOCKED))
538950a1 1305 return ERR_PTR(-EPERM);
89aa0758 1306
113214be 1307 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
538950a1
CG
1308}
1309
1310int sk_attach_bpf(u32 ufd, struct sock *sk)
1311{
1312 struct bpf_prog *prog = __get_bpf(ufd, sk);
1313 int err;
1314
1315 if (IS_ERR(prog))
1316 return PTR_ERR(prog);
1317
8ced425e 1318 err = __sk_attach_prog(prog, sk);
49b31e57 1319 if (err < 0) {
89aa0758 1320 bpf_prog_put(prog);
49b31e57 1321 return err;
89aa0758
AS
1322 }
1323
89aa0758
AS
1324 return 0;
1325}
1326
538950a1
CG
1327int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1328{
1329 struct bpf_prog *prog = __get_bpf(ufd, sk);
1330 int err;
1331
1332 if (IS_ERR(prog))
1333 return PTR_ERR(prog);
1334
1335 err = __reuseport_attach_prog(prog, sk);
1336 if (err < 0) {
1337 bpf_prog_put(prog);
1338 return err;
1339 }
1340
1341 return 0;
1342}
1343
21cafc1d
DB
1344struct bpf_scratchpad {
1345 union {
1346 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1347 u8 buff[MAX_BPF_STACK];
1348 };
1349};
1350
1351static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
91bc4822 1352
5293efe6
DB
1353static inline int __bpf_try_make_writable(struct sk_buff *skb,
1354 unsigned int write_len)
1355{
1356 return skb_ensure_writable(skb, write_len);
1357}
1358
db58ba45
AS
1359static inline int bpf_try_make_writable(struct sk_buff *skb,
1360 unsigned int write_len)
1361{
5293efe6 1362 int err = __bpf_try_make_writable(skb, write_len);
db58ba45 1363
0ed661d5 1364 bpf_compute_data_end(skb);
db58ba45
AS
1365 return err;
1366}
1367
a2bfe6bf
DB
1368static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1369{
1370 if (skb_at_tc_ingress(skb))
1371 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1372}
1373
8065694e
DB
1374static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1375{
1376 if (skb_at_tc_ingress(skb))
1377 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1378}
1379
91bc4822 1380static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
608cd71a
AS
1381{
1382 struct sk_buff *skb = (struct sk_buff *) (long) r1;
0ed661d5 1383 unsigned int offset = (unsigned int) r2;
608cd71a
AS
1384 void *from = (void *) (long) r3;
1385 unsigned int len = (unsigned int) r4;
608cd71a
AS
1386 void *ptr;
1387
8afd54c8 1388 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
781c53bc 1389 return -EINVAL;
0ed661d5 1390 if (unlikely(offset > 0xffff))
608cd71a 1391 return -EFAULT;
db58ba45 1392 if (unlikely(bpf_try_make_writable(skb, offset + len)))
608cd71a
AS
1393 return -EFAULT;
1394
0ed661d5 1395 ptr = skb->data + offset;
781c53bc 1396 if (flags & BPF_F_RECOMPUTE_CSUM)
479ffccc 1397 __skb_postpull_rcsum(skb, ptr, len, offset);
608cd71a
AS
1398
1399 memcpy(ptr, from, len);
1400
781c53bc 1401 if (flags & BPF_F_RECOMPUTE_CSUM)
479ffccc 1402 __skb_postpush_rcsum(skb, ptr, len, offset);
8afd54c8
DB
1403 if (flags & BPF_F_INVALIDATE_HASH)
1404 skb_clear_hash(skb);
f8ffad69 1405
608cd71a
AS
1406 return 0;
1407}
1408
577c50aa 1409static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
608cd71a
AS
1410 .func = bpf_skb_store_bytes,
1411 .gpl_only = false,
1412 .ret_type = RET_INTEGER,
1413 .arg1_type = ARG_PTR_TO_CTX,
1414 .arg2_type = ARG_ANYTHING,
1415 .arg3_type = ARG_PTR_TO_STACK,
1416 .arg4_type = ARG_CONST_STACK_SIZE,
91bc4822
AS
1417 .arg5_type = ARG_ANYTHING,
1418};
1419
05c74e5e
DB
1420static u64 bpf_skb_load_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1421{
1422 const struct sk_buff *skb = (const struct sk_buff *)(unsigned long) r1;
0ed661d5 1423 unsigned int offset = (unsigned int) r2;
05c74e5e
DB
1424 void *to = (void *)(unsigned long) r3;
1425 unsigned int len = (unsigned int) r4;
1426 void *ptr;
1427
0ed661d5 1428 if (unlikely(offset > 0xffff))
074f528e 1429 goto err_clear;
05c74e5e
DB
1430
1431 ptr = skb_header_pointer(skb, offset, len, to);
1432 if (unlikely(!ptr))
074f528e 1433 goto err_clear;
05c74e5e
DB
1434 if (ptr != to)
1435 memcpy(to, ptr, len);
1436
1437 return 0;
074f528e
DB
1438err_clear:
1439 memset(to, 0, len);
1440 return -EFAULT;
05c74e5e
DB
1441}
1442
577c50aa 1443static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
05c74e5e
DB
1444 .func = bpf_skb_load_bytes,
1445 .gpl_only = false,
1446 .ret_type = RET_INTEGER,
1447 .arg1_type = ARG_PTR_TO_CTX,
1448 .arg2_type = ARG_ANYTHING,
074f528e 1449 .arg3_type = ARG_PTR_TO_RAW_STACK,
05c74e5e
DB
1450 .arg4_type = ARG_CONST_STACK_SIZE,
1451};
1452
a166151c 1453static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
91bc4822
AS
1454{
1455 struct sk_buff *skb = (struct sk_buff *) (long) r1;
0ed661d5
DB
1456 unsigned int offset = (unsigned int) r2;
1457 __sum16 *ptr;
91bc4822 1458
781c53bc
DB
1459 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1460 return -EINVAL;
0ed661d5 1461 if (unlikely(offset > 0xffff || offset & 1))
91bc4822 1462 return -EFAULT;
0ed661d5 1463 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
91bc4822
AS
1464 return -EFAULT;
1465
0ed661d5 1466 ptr = (__sum16 *)(skb->data + offset);
781c53bc 1467 switch (flags & BPF_F_HDR_FIELD_MASK) {
8050c0f0
DB
1468 case 0:
1469 if (unlikely(from != 0))
1470 return -EINVAL;
1471
1472 csum_replace_by_diff(ptr, to);
1473 break;
91bc4822
AS
1474 case 2:
1475 csum_replace2(ptr, from, to);
1476 break;
1477 case 4:
1478 csum_replace4(ptr, from, to);
1479 break;
1480 default:
1481 return -EINVAL;
1482 }
1483
91bc4822
AS
1484 return 0;
1485}
1486
577c50aa 1487static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
91bc4822
AS
1488 .func = bpf_l3_csum_replace,
1489 .gpl_only = false,
1490 .ret_type = RET_INTEGER,
1491 .arg1_type = ARG_PTR_TO_CTX,
1492 .arg2_type = ARG_ANYTHING,
1493 .arg3_type = ARG_ANYTHING,
1494 .arg4_type = ARG_ANYTHING,
1495 .arg5_type = ARG_ANYTHING,
1496};
1497
a166151c 1498static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
91bc4822
AS
1499{
1500 struct sk_buff *skb = (struct sk_buff *) (long) r1;
781c53bc 1501 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
2f72959a 1502 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
0ed661d5
DB
1503 unsigned int offset = (unsigned int) r2;
1504 __sum16 *ptr;
91bc4822 1505
2f72959a
DB
1506 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR |
1507 BPF_F_HDR_FIELD_MASK)))
781c53bc 1508 return -EINVAL;
0ed661d5 1509 if (unlikely(offset > 0xffff || offset & 1))
91bc4822 1510 return -EFAULT;
0ed661d5 1511 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
91bc4822
AS
1512 return -EFAULT;
1513
0ed661d5 1514 ptr = (__sum16 *)(skb->data + offset);
2f72959a
DB
1515 if (is_mmzero && !*ptr)
1516 return 0;
91bc4822 1517
781c53bc 1518 switch (flags & BPF_F_HDR_FIELD_MASK) {
7d672345
DB
1519 case 0:
1520 if (unlikely(from != 0))
1521 return -EINVAL;
1522
1523 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1524 break;
91bc4822
AS
1525 case 2:
1526 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1527 break;
1528 case 4:
1529 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1530 break;
1531 default:
1532 return -EINVAL;
1533 }
1534
2f72959a
DB
1535 if (is_mmzero && !*ptr)
1536 *ptr = CSUM_MANGLED_0;
91bc4822
AS
1537 return 0;
1538}
1539
577c50aa 1540static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
91bc4822
AS
1541 .func = bpf_l4_csum_replace,
1542 .gpl_only = false,
1543 .ret_type = RET_INTEGER,
1544 .arg1_type = ARG_PTR_TO_CTX,
1545 .arg2_type = ARG_ANYTHING,
1546 .arg3_type = ARG_ANYTHING,
1547 .arg4_type = ARG_ANYTHING,
1548 .arg5_type = ARG_ANYTHING,
608cd71a
AS
1549};
1550
7d672345
DB
1551static u64 bpf_csum_diff(u64 r1, u64 from_size, u64 r3, u64 to_size, u64 seed)
1552{
21cafc1d 1553 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
7d672345
DB
1554 u64 diff_size = from_size + to_size;
1555 __be32 *from = (__be32 *) (long) r1;
1556 __be32 *to = (__be32 *) (long) r3;
1557 int i, j = 0;
1558
1559 /* This is quite flexible, some examples:
1560 *
1561 * from_size == 0, to_size > 0, seed := csum --> pushing data
1562 * from_size > 0, to_size == 0, seed := csum --> pulling data
1563 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1564 *
1565 * Even for diffing, from_size and to_size don't need to be equal.
1566 */
1567 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1568 diff_size > sizeof(sp->diff)))
1569 return -EINVAL;
1570
1571 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1572 sp->diff[j] = ~from[i];
1573 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1574 sp->diff[j] = to[i];
1575
1576 return csum_partial(sp->diff, diff_size, seed);
1577}
1578
577c50aa 1579static const struct bpf_func_proto bpf_csum_diff_proto = {
7d672345
DB
1580 .func = bpf_csum_diff,
1581 .gpl_only = false,
1582 .ret_type = RET_INTEGER,
1583 .arg1_type = ARG_PTR_TO_STACK,
1584 .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1585 .arg3_type = ARG_PTR_TO_STACK,
1586 .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1587 .arg5_type = ARG_ANYTHING,
1588};
1589
a70b506e
DB
1590static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1591{
a70b506e
DB
1592 return dev_forward_skb(dev, skb);
1593}
1594
1595static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1596{
1597 int ret;
1598
1599 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1600 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1601 kfree_skb(skb);
1602 return -ENETDOWN;
1603 }
1604
1605 skb->dev = dev;
1606
1607 __this_cpu_inc(xmit_recursion);
1608 ret = dev_queue_xmit(skb);
1609 __this_cpu_dec(xmit_recursion);
1610
1611 return ret;
1612}
1613
3896d655
AS
1614static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1615{
a70b506e 1616 struct sk_buff *skb = (struct sk_buff *) (long) r1;
3896d655
AS
1617 struct net_device *dev;
1618
781c53bc
DB
1619 if (unlikely(flags & ~(BPF_F_INGRESS)))
1620 return -EINVAL;
1621
3896d655
AS
1622 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1623 if (unlikely(!dev))
1624 return -EINVAL;
1625
a70b506e
DB
1626 skb = skb_clone(skb, GFP_ATOMIC);
1627 if (unlikely(!skb))
3896d655
AS
1628 return -ENOMEM;
1629
a2bfe6bf
DB
1630 bpf_push_mac_rcsum(skb);
1631
a70b506e
DB
1632 return flags & BPF_F_INGRESS ?
1633 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
3896d655
AS
1634}
1635
577c50aa 1636static const struct bpf_func_proto bpf_clone_redirect_proto = {
3896d655
AS
1637 .func = bpf_clone_redirect,
1638 .gpl_only = false,
1639 .ret_type = RET_INTEGER,
1640 .arg1_type = ARG_PTR_TO_CTX,
1641 .arg2_type = ARG_ANYTHING,
1642 .arg3_type = ARG_ANYTHING,
1643};
1644
27b29f63
AS
1645struct redirect_info {
1646 u32 ifindex;
1647 u32 flags;
1648};
1649
1650static DEFINE_PER_CPU(struct redirect_info, redirect_info);
781c53bc 1651
27b29f63
AS
1652static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5)
1653{
1654 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1655
781c53bc
DB
1656 if (unlikely(flags & ~(BPF_F_INGRESS)))
1657 return TC_ACT_SHOT;
1658
27b29f63
AS
1659 ri->ifindex = ifindex;
1660 ri->flags = flags;
781c53bc 1661
27b29f63
AS
1662 return TC_ACT_REDIRECT;
1663}
1664
1665int skb_do_redirect(struct sk_buff *skb)
1666{
1667 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1668 struct net_device *dev;
1669
1670 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1671 ri->ifindex = 0;
1672 if (unlikely(!dev)) {
1673 kfree_skb(skb);
1674 return -EINVAL;
1675 }
1676
a2bfe6bf
DB
1677 bpf_push_mac_rcsum(skb);
1678
a70b506e
DB
1679 return ri->flags & BPF_F_INGRESS ?
1680 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
27b29f63
AS
1681}
1682
577c50aa 1683static const struct bpf_func_proto bpf_redirect_proto = {
27b29f63
AS
1684 .func = bpf_redirect,
1685 .gpl_only = false,
1686 .ret_type = RET_INTEGER,
1687 .arg1_type = ARG_ANYTHING,
1688 .arg2_type = ARG_ANYTHING,
1689};
1690
8d20aabe
DB
1691static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1692{
1693 return task_get_classid((struct sk_buff *) (unsigned long) r1);
1694}
1695
1696static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1697 .func = bpf_get_cgroup_classid,
1698 .gpl_only = false,
1699 .ret_type = RET_INTEGER,
1700 .arg1_type = ARG_PTR_TO_CTX,
1701};
1702
c46646d0
DB
1703static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1704{
808c1b69 1705 return dst_tclassid((struct sk_buff *) (unsigned long) r1);
c46646d0
DB
1706}
1707
1708static const struct bpf_func_proto bpf_get_route_realm_proto = {
1709 .func = bpf_get_route_realm,
1710 .gpl_only = false,
1711 .ret_type = RET_INTEGER,
1712 .arg1_type = ARG_PTR_TO_CTX,
1713};
1714
13c5c240
DB
1715static u64 bpf_get_hash_recalc(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1716{
1717 /* If skb_clear_hash() was called due to mangling, we can
1718 * trigger SW recalculation here. Later access to hash
1719 * can then use the inline skb->hash via context directly
1720 * instead of calling this helper again.
1721 */
1722 return skb_get_hash((struct sk_buff *) (unsigned long) r1);
1723}
1724
1725static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1726 .func = bpf_get_hash_recalc,
1727 .gpl_only = false,
1728 .ret_type = RET_INTEGER,
1729 .arg1_type = ARG_PTR_TO_CTX,
1730};
1731
4e10df9a
AS
1732static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
1733{
1734 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1735 __be16 vlan_proto = (__force __be16) r2;
db58ba45 1736 int ret;
4e10df9a
AS
1737
1738 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1739 vlan_proto != htons(ETH_P_8021AD)))
1740 vlan_proto = htons(ETH_P_8021Q);
1741
8065694e 1742 bpf_push_mac_rcsum(skb);
db58ba45 1743 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
8065694e
DB
1744 bpf_pull_mac_rcsum(skb);
1745
db58ba45
AS
1746 bpf_compute_data_end(skb);
1747 return ret;
4e10df9a
AS
1748}
1749
1750const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1751 .func = bpf_skb_vlan_push,
1752 .gpl_only = false,
1753 .ret_type = RET_INTEGER,
1754 .arg1_type = ARG_PTR_TO_CTX,
1755 .arg2_type = ARG_ANYTHING,
1756 .arg3_type = ARG_ANYTHING,
1757};
4d9c5c53 1758EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
4e10df9a
AS
1759
1760static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1761{
1762 struct sk_buff *skb = (struct sk_buff *) (long) r1;
db58ba45 1763 int ret;
4e10df9a 1764
8065694e 1765 bpf_push_mac_rcsum(skb);
db58ba45 1766 ret = skb_vlan_pop(skb);
8065694e
DB
1767 bpf_pull_mac_rcsum(skb);
1768
db58ba45
AS
1769 bpf_compute_data_end(skb);
1770 return ret;
4e10df9a
AS
1771}
1772
1773const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1774 .func = bpf_skb_vlan_pop,
1775 .gpl_only = false,
1776 .ret_type = RET_INTEGER,
1777 .arg1_type = ARG_PTR_TO_CTX,
1778};
4d9c5c53 1779EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
4e10df9a 1780
6578171a
DB
1781static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1782{
1783 /* Caller already did skb_cow() with len as headroom,
1784 * so no need to do it here.
1785 */
1786 skb_push(skb, len);
1787 memmove(skb->data, skb->data + len, off);
1788 memset(skb->data + off, 0, len);
1789
1790 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1791 * needed here as it does not change the skb->csum
1792 * result for checksum complete when summing over
1793 * zeroed blocks.
1794 */
1795 return 0;
1796}
1797
1798static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1799{
1800 /* skb_ensure_writable() is not needed here, as we're
1801 * already working on an uncloned skb.
1802 */
1803 if (unlikely(!pskb_may_pull(skb, off + len)))
1804 return -ENOMEM;
1805
1806 skb_postpull_rcsum(skb, skb->data + off, len);
1807 memmove(skb->data + len, skb->data, off);
1808 __skb_pull(skb, len);
1809
1810 return 0;
1811}
1812
1813static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1814{
1815 bool trans_same = skb->transport_header == skb->network_header;
1816 int ret;
1817
1818 /* There's no need for __skb_push()/__skb_pull() pair to
1819 * get to the start of the mac header as we're guaranteed
1820 * to always start from here under eBPF.
1821 */
1822 ret = bpf_skb_generic_push(skb, off, len);
1823 if (likely(!ret)) {
1824 skb->mac_header -= len;
1825 skb->network_header -= len;
1826 if (trans_same)
1827 skb->transport_header = skb->network_header;
1828 }
1829
1830 return ret;
1831}
1832
1833static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1834{
1835 bool trans_same = skb->transport_header == skb->network_header;
1836 int ret;
1837
1838 /* Same here, __skb_push()/__skb_pull() pair not needed. */
1839 ret = bpf_skb_generic_pop(skb, off, len);
1840 if (likely(!ret)) {
1841 skb->mac_header += len;
1842 skb->network_header += len;
1843 if (trans_same)
1844 skb->transport_header = skb->network_header;
1845 }
1846
1847 return ret;
1848}
1849
1850static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
1851{
1852 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1853 u32 off = skb->network_header - skb->mac_header;
1854 int ret;
1855
1856 ret = skb_cow(skb, len_diff);
1857 if (unlikely(ret < 0))
1858 return ret;
1859
1860 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
1861 if (unlikely(ret < 0))
1862 return ret;
1863
1864 if (skb_is_gso(skb)) {
1865 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
1866 * be changed into SKB_GSO_TCPV6.
1867 */
1868 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1869 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
1870 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
1871 }
1872
1873 /* Due to IPv6 header, MSS needs to be downgraded. */
1874 skb_shinfo(skb)->gso_size -= len_diff;
1875 /* Header must be checked, and gso_segs recomputed. */
1876 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1877 skb_shinfo(skb)->gso_segs = 0;
1878 }
1879
1880 skb->protocol = htons(ETH_P_IPV6);
1881 skb_clear_hash(skb);
1882
1883 return 0;
1884}
1885
1886static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
1887{
1888 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1889 u32 off = skb->network_header - skb->mac_header;
1890 int ret;
1891
1892 ret = skb_unclone(skb, GFP_ATOMIC);
1893 if (unlikely(ret < 0))
1894 return ret;
1895
1896 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
1897 if (unlikely(ret < 0))
1898 return ret;
1899
1900 if (skb_is_gso(skb)) {
1901 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
1902 * be changed into SKB_GSO_TCPV4.
1903 */
1904 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
1905 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
1906 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
1907 }
1908
1909 /* Due to IPv4 header, MSS can be upgraded. */
1910 skb_shinfo(skb)->gso_size += len_diff;
1911 /* Header must be checked, and gso_segs recomputed. */
1912 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1913 skb_shinfo(skb)->gso_segs = 0;
1914 }
1915
1916 skb->protocol = htons(ETH_P_IP);
1917 skb_clear_hash(skb);
1918
1919 return 0;
1920}
1921
1922static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
1923{
1924 __be16 from_proto = skb->protocol;
1925
1926 if (from_proto == htons(ETH_P_IP) &&
1927 to_proto == htons(ETH_P_IPV6))
1928 return bpf_skb_proto_4_to_6(skb);
1929
1930 if (from_proto == htons(ETH_P_IPV6) &&
1931 to_proto == htons(ETH_P_IP))
1932 return bpf_skb_proto_6_to_4(skb);
1933
1934 return -ENOTSUPP;
1935}
1936
1937static u64 bpf_skb_change_proto(u64 r1, u64 r2, u64 flags, u64 r4, u64 r5)
1938{
1939 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1940 __be16 proto = (__force __be16) r2;
1941 int ret;
1942
1943 if (unlikely(flags))
1944 return -EINVAL;
1945
1946 /* General idea is that this helper does the basic groundwork
1947 * needed for changing the protocol, and eBPF program fills the
1948 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
1949 * and other helpers, rather than passing a raw buffer here.
1950 *
1951 * The rationale is to keep this minimal and without a need to
1952 * deal with raw packet data. F.e. even if we would pass buffers
1953 * here, the program still needs to call the bpf_lX_csum_replace()
1954 * helpers anyway. Plus, this way we keep also separation of
1955 * concerns, since f.e. bpf_skb_store_bytes() should only take
1956 * care of stores.
1957 *
1958 * Currently, additional options and extension header space are
1959 * not supported, but flags register is reserved so we can adapt
1960 * that. For offloads, we mark packet as dodgy, so that headers
1961 * need to be verified first.
1962 */
1963 ret = bpf_skb_proto_xlat(skb, proto);
1964 bpf_compute_data_end(skb);
1965 return ret;
1966}
1967
1968static const struct bpf_func_proto bpf_skb_change_proto_proto = {
1969 .func = bpf_skb_change_proto,
1970 .gpl_only = false,
1971 .ret_type = RET_INTEGER,
1972 .arg1_type = ARG_PTR_TO_CTX,
1973 .arg2_type = ARG_ANYTHING,
1974 .arg3_type = ARG_ANYTHING,
1975};
1976
d2485c42
DB
1977static u64 bpf_skb_change_type(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1978{
1979 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1980 u32 pkt_type = r2;
1981
1982 /* We only allow a restricted subset to be changed for now. */
45c7fffa
DB
1983 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
1984 !skb_pkt_type_ok(pkt_type)))
d2485c42
DB
1985 return -EINVAL;
1986
1987 skb->pkt_type = pkt_type;
1988 return 0;
1989}
1990
1991static const struct bpf_func_proto bpf_skb_change_type_proto = {
1992 .func = bpf_skb_change_type,
1993 .gpl_only = false,
1994 .ret_type = RET_INTEGER,
1995 .arg1_type = ARG_PTR_TO_CTX,
1996 .arg2_type = ARG_ANYTHING,
1997};
1998
5293efe6
DB
1999static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2000{
2001 u32 min_len = skb_network_offset(skb);
2002
2003 if (skb_transport_header_was_set(skb))
2004 min_len = skb_transport_offset(skb);
2005 if (skb->ip_summed == CHECKSUM_PARTIAL)
2006 min_len = skb_checksum_start_offset(skb) +
2007 skb->csum_offset + sizeof(__sum16);
2008 return min_len;
2009}
2010
2011static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2012{
6088b582 2013 return skb->dev->mtu + skb->dev->hard_header_len;
5293efe6
DB
2014}
2015
2016static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2017{
2018 unsigned int old_len = skb->len;
2019 int ret;
2020
2021 ret = __skb_grow_rcsum(skb, new_len);
2022 if (!ret)
2023 memset(skb->data + old_len, 0, new_len - old_len);
2024 return ret;
2025}
2026
2027static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2028{
2029 return __skb_trim_rcsum(skb, new_len);
2030}
2031
2032static u64 bpf_skb_change_tail(u64 r1, u64 r2, u64 flags, u64 r4, u64 r5)
2033{
2034 struct sk_buff *skb = (struct sk_buff *)(long) r1;
2035 u32 max_len = __bpf_skb_max_len(skb);
2036 u32 min_len = __bpf_skb_min_len(skb);
2037 u32 new_len = (u32) r2;
2038 int ret;
2039
2040 if (unlikely(flags || new_len > max_len || new_len < min_len))
2041 return -EINVAL;
2042 if (skb->encapsulation)
2043 return -ENOTSUPP;
2044
2045 /* The basic idea of this helper is that it's performing the
2046 * needed work to either grow or trim an skb, and eBPF program
2047 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2048 * bpf_lX_csum_replace() and others rather than passing a raw
2049 * buffer here. This one is a slow path helper and intended
2050 * for replies with control messages.
2051 *
2052 * Like in bpf_skb_change_proto(), we want to keep this rather
2053 * minimal and without protocol specifics so that we are able
2054 * to separate concerns as in bpf_skb_store_bytes() should only
2055 * be the one responsible for writing buffers.
2056 *
2057 * It's really expected to be a slow path operation here for
2058 * control message replies, so we're implicitly linearizing,
2059 * uncloning and drop offloads from the skb by this.
2060 */
2061 ret = __bpf_try_make_writable(skb, skb->len);
2062 if (!ret) {
2063 if (new_len > skb->len)
2064 ret = bpf_skb_grow_rcsum(skb, new_len);
2065 else if (new_len < skb->len)
2066 ret = bpf_skb_trim_rcsum(skb, new_len);
2067 if (!ret && skb_is_gso(skb))
2068 skb_gso_reset(skb);
2069 }
2070
2071 bpf_compute_data_end(skb);
2072 return ret;
2073}
2074
2075static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2076 .func = bpf_skb_change_tail,
2077 .gpl_only = false,
2078 .ret_type = RET_INTEGER,
2079 .arg1_type = ARG_PTR_TO_CTX,
2080 .arg2_type = ARG_ANYTHING,
2081 .arg3_type = ARG_ANYTHING,
2082};
2083
4e10df9a
AS
2084bool bpf_helper_changes_skb_data(void *func)
2085{
2086 if (func == bpf_skb_vlan_push)
2087 return true;
2088 if (func == bpf_skb_vlan_pop)
2089 return true;
3697649f
DB
2090 if (func == bpf_skb_store_bytes)
2091 return true;
6578171a
DB
2092 if (func == bpf_skb_change_proto)
2093 return true;
5293efe6
DB
2094 if (func == bpf_skb_change_tail)
2095 return true;
3697649f
DB
2096 if (func == bpf_l3_csum_replace)
2097 return true;
2098 if (func == bpf_l4_csum_replace)
2099 return true;
2100
4e10df9a
AS
2101 return false;
2102}
2103
555c8a86 2104static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
aa7145c1 2105 unsigned long off, unsigned long len)
555c8a86 2106{
aa7145c1 2107 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
555c8a86
DB
2108
2109 if (unlikely(!ptr))
2110 return len;
2111 if (ptr != dst_buff)
2112 memcpy(dst_buff, ptr, len);
2113
2114 return 0;
2115}
2116
2117static u64 bpf_skb_event_output(u64 r1, u64 r2, u64 flags, u64 r4,
2118 u64 meta_size)
2119{
2120 struct sk_buff *skb = (struct sk_buff *)(long) r1;
2121 struct bpf_map *map = (struct bpf_map *)(long) r2;
2122 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2123 void *meta = (void *)(long) r4;
2124
2125 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2126 return -EINVAL;
2127 if (unlikely(skb_size > skb->len))
2128 return -EFAULT;
2129
2130 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2131 bpf_skb_copy);
2132}
2133
2134static const struct bpf_func_proto bpf_skb_event_output_proto = {
2135 .func = bpf_skb_event_output,
2136 .gpl_only = true,
2137 .ret_type = RET_INTEGER,
2138 .arg1_type = ARG_PTR_TO_CTX,
2139 .arg2_type = ARG_CONST_MAP_PTR,
2140 .arg3_type = ARG_ANYTHING,
2141 .arg4_type = ARG_PTR_TO_STACK,
2142 .arg5_type = ARG_CONST_STACK_SIZE,
2143};
2144
c6c33454
DB
2145static unsigned short bpf_tunnel_key_af(u64 flags)
2146{
2147 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2148}
2149
d3aa45ce
AS
2150static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
2151{
2152 struct sk_buff *skb = (struct sk_buff *) (long) r1;
2153 struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2;
c6c33454
DB
2154 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2155 u8 compat[sizeof(struct bpf_tunnel_key)];
074f528e
DB
2156 void *to_orig = to;
2157 int err;
d3aa45ce 2158
074f528e
DB
2159 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2160 err = -EINVAL;
2161 goto err_clear;
2162 }
2163 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2164 err = -EPROTO;
2165 goto err_clear;
2166 }
c6c33454 2167 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
074f528e 2168 err = -EINVAL;
c6c33454 2169 switch (size) {
4018ab18 2170 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 2171 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4018ab18 2172 goto set_compat;
c6c33454
DB
2173 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2174 /* Fixup deprecated structure layouts here, so we have
2175 * a common path later on.
2176 */
2177 if (ip_tunnel_info_af(info) != AF_INET)
074f528e 2178 goto err_clear;
4018ab18 2179set_compat:
c6c33454
DB
2180 to = (struct bpf_tunnel_key *)compat;
2181 break;
2182 default:
074f528e 2183 goto err_clear;
c6c33454
DB
2184 }
2185 }
d3aa45ce
AS
2186
2187 to->tunnel_id = be64_to_cpu(info->key.tun_id);
c6c33454
DB
2188 to->tunnel_tos = info->key.tos;
2189 to->tunnel_ttl = info->key.ttl;
2190
4018ab18 2191 if (flags & BPF_F_TUNINFO_IPV6) {
c6c33454
DB
2192 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2193 sizeof(to->remote_ipv6));
4018ab18
DB
2194 to->tunnel_label = be32_to_cpu(info->key.label);
2195 } else {
c6c33454 2196 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
4018ab18 2197 }
c6c33454
DB
2198
2199 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
074f528e 2200 memcpy(to_orig, to, size);
d3aa45ce
AS
2201
2202 return 0;
074f528e
DB
2203err_clear:
2204 memset(to_orig, 0, size);
2205 return err;
d3aa45ce
AS
2206}
2207
577c50aa 2208static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
d3aa45ce
AS
2209 .func = bpf_skb_get_tunnel_key,
2210 .gpl_only = false,
2211 .ret_type = RET_INTEGER,
2212 .arg1_type = ARG_PTR_TO_CTX,
074f528e 2213 .arg2_type = ARG_PTR_TO_RAW_STACK,
d3aa45ce
AS
2214 .arg3_type = ARG_CONST_STACK_SIZE,
2215 .arg4_type = ARG_ANYTHING,
2216};
2217
14ca0751
DB
2218static u64 bpf_skb_get_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
2219{
2220 struct sk_buff *skb = (struct sk_buff *) (long) r1;
2221 u8 *to = (u8 *) (long) r2;
2222 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
074f528e 2223 int err;
14ca0751
DB
2224
2225 if (unlikely(!info ||
074f528e
DB
2226 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2227 err = -ENOENT;
2228 goto err_clear;
2229 }
2230 if (unlikely(size < info->options_len)) {
2231 err = -ENOMEM;
2232 goto err_clear;
2233 }
14ca0751
DB
2234
2235 ip_tunnel_info_opts_get(to, info);
074f528e
DB
2236 if (size > info->options_len)
2237 memset(to + info->options_len, 0, size - info->options_len);
14ca0751
DB
2238
2239 return info->options_len;
074f528e
DB
2240err_clear:
2241 memset(to, 0, size);
2242 return err;
14ca0751
DB
2243}
2244
2245static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2246 .func = bpf_skb_get_tunnel_opt,
2247 .gpl_only = false,
2248 .ret_type = RET_INTEGER,
2249 .arg1_type = ARG_PTR_TO_CTX,
074f528e 2250 .arg2_type = ARG_PTR_TO_RAW_STACK,
14ca0751
DB
2251 .arg3_type = ARG_CONST_STACK_SIZE,
2252};
2253
d3aa45ce
AS
2254static struct metadata_dst __percpu *md_dst;
2255
2256static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
2257{
2258 struct sk_buff *skb = (struct sk_buff *) (long) r1;
2259 struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2;
2260 struct metadata_dst *md = this_cpu_ptr(md_dst);
c6c33454 2261 u8 compat[sizeof(struct bpf_tunnel_key)];
d3aa45ce
AS
2262 struct ip_tunnel_info *info;
2263
22080870
DB
2264 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2265 BPF_F_DONT_FRAGMENT)))
d3aa45ce 2266 return -EINVAL;
c6c33454
DB
2267 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2268 switch (size) {
4018ab18 2269 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 2270 case offsetof(struct bpf_tunnel_key, tunnel_ext):
c6c33454
DB
2271 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2272 /* Fixup deprecated structure layouts here, so we have
2273 * a common path later on.
2274 */
2275 memcpy(compat, from, size);
2276 memset(compat + size, 0, sizeof(compat) - size);
2277 from = (struct bpf_tunnel_key *)compat;
2278 break;
2279 default:
2280 return -EINVAL;
2281 }
2282 }
c0e760c9
DB
2283 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2284 from->tunnel_ext))
4018ab18 2285 return -EINVAL;
d3aa45ce
AS
2286
2287 skb_dst_drop(skb);
2288 dst_hold((struct dst_entry *) md);
2289 skb_dst_set(skb, (struct dst_entry *) md);
2290
2291 info = &md->u.tun_info;
2292 info->mode = IP_TUNNEL_INFO_TX;
c6c33454 2293
db3c6139 2294 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
22080870
DB
2295 if (flags & BPF_F_DONT_FRAGMENT)
2296 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2297
d3aa45ce 2298 info->key.tun_id = cpu_to_be64(from->tunnel_id);
c6c33454
DB
2299 info->key.tos = from->tunnel_tos;
2300 info->key.ttl = from->tunnel_ttl;
2301
2302 if (flags & BPF_F_TUNINFO_IPV6) {
2303 info->mode |= IP_TUNNEL_INFO_IPV6;
2304 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2305 sizeof(from->remote_ipv6));
4018ab18
DB
2306 info->key.label = cpu_to_be32(from->tunnel_label) &
2307 IPV6_FLOWLABEL_MASK;
c6c33454
DB
2308 } else {
2309 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2da897e5
DB
2310 if (flags & BPF_F_ZERO_CSUM_TX)
2311 info->key.tun_flags &= ~TUNNEL_CSUM;
c6c33454 2312 }
d3aa45ce
AS
2313
2314 return 0;
2315}
2316
577c50aa 2317static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
d3aa45ce
AS
2318 .func = bpf_skb_set_tunnel_key,
2319 .gpl_only = false,
2320 .ret_type = RET_INTEGER,
2321 .arg1_type = ARG_PTR_TO_CTX,
2322 .arg2_type = ARG_PTR_TO_STACK,
2323 .arg3_type = ARG_CONST_STACK_SIZE,
2324 .arg4_type = ARG_ANYTHING,
2325};
2326
14ca0751
DB
2327static u64 bpf_skb_set_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
2328{
2329 struct sk_buff *skb = (struct sk_buff *) (long) r1;
2330 u8 *from = (u8 *) (long) r2;
2331 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2332 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2333
2334 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2335 return -EINVAL;
fca5fdf6 2336 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
14ca0751
DB
2337 return -ENOMEM;
2338
2339 ip_tunnel_info_opts_set(info, from, size);
2340
2341 return 0;
2342}
2343
2344static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2345 .func = bpf_skb_set_tunnel_opt,
2346 .gpl_only = false,
2347 .ret_type = RET_INTEGER,
2348 .arg1_type = ARG_PTR_TO_CTX,
2349 .arg2_type = ARG_PTR_TO_STACK,
2350 .arg3_type = ARG_CONST_STACK_SIZE,
2351};
2352
2353static const struct bpf_func_proto *
2354bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
d3aa45ce
AS
2355{
2356 if (!md_dst) {
14ca0751
DB
2357 /* Race is not possible, since it's called from verifier
2358 * that is holding verifier mutex.
d3aa45ce 2359 */
fca5fdf6 2360 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
14ca0751 2361 GFP_KERNEL);
d3aa45ce
AS
2362 if (!md_dst)
2363 return NULL;
2364 }
14ca0751
DB
2365
2366 switch (which) {
2367 case BPF_FUNC_skb_set_tunnel_key:
2368 return &bpf_skb_set_tunnel_key_proto;
2369 case BPF_FUNC_skb_set_tunnel_opt:
2370 return &bpf_skb_set_tunnel_opt_proto;
2371 default:
2372 return NULL;
2373 }
d3aa45ce
AS
2374}
2375
747ea55e 2376static u64 bpf_skb_under_cgroup(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
4a482f34
MKL
2377{
2378 struct sk_buff *skb = (struct sk_buff *)(long)r1;
2379 struct bpf_map *map = (struct bpf_map *)(long)r2;
2380 struct bpf_array *array = container_of(map, struct bpf_array, map);
2381 struct cgroup *cgrp;
2382 struct sock *sk;
2383 u32 i = (u32)r3;
2384
2385 sk = skb->sk;
2386 if (!sk || !sk_fullsock(sk))
2387 return -ENOENT;
2388
2389 if (unlikely(i >= array->map.max_entries))
2390 return -E2BIG;
2391
2392 cgrp = READ_ONCE(array->ptrs[i]);
2393 if (unlikely(!cgrp))
2394 return -EAGAIN;
2395
54fd9c2d 2396 return sk_under_cgroup_hierarchy(sk, cgrp);
4a482f34
MKL
2397}
2398
747ea55e
DB
2399static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2400 .func = bpf_skb_under_cgroup,
4a482f34
MKL
2401 .gpl_only = false,
2402 .ret_type = RET_INTEGER,
2403 .arg1_type = ARG_PTR_TO_CTX,
2404 .arg2_type = ARG_CONST_MAP_PTR,
2405 .arg3_type = ARG_ANYTHING,
2406};
4a482f34 2407
4de16969
DB
2408static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2409 unsigned long off, unsigned long len)
2410{
2411 memcpy(dst_buff, src_buff + off, len);
2412 return 0;
2413}
2414
2415static u64 bpf_xdp_event_output(u64 r1, u64 r2, u64 flags, u64 r4,
2416 u64 meta_size)
2417{
2418 struct xdp_buff *xdp = (struct xdp_buff *)(long) r1;
2419 struct bpf_map *map = (struct bpf_map *)(long) r2;
2420 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2421 void *meta = (void *)(long) r4;
2422
2423 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2424 return -EINVAL;
2425 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2426 return -EFAULT;
2427
2428 return bpf_event_output(map, flags, meta, meta_size, xdp, xdp_size,
2429 bpf_xdp_copy);
2430}
2431
2432static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2433 .func = bpf_xdp_event_output,
2434 .gpl_only = true,
2435 .ret_type = RET_INTEGER,
2436 .arg1_type = ARG_PTR_TO_CTX,
2437 .arg2_type = ARG_CONST_MAP_PTR,
2438 .arg3_type = ARG_ANYTHING,
2439 .arg4_type = ARG_PTR_TO_STACK,
2440 .arg5_type = ARG_CONST_STACK_SIZE,
2441};
2442
d4052c4a
DB
2443static const struct bpf_func_proto *
2444sk_filter_func_proto(enum bpf_func_id func_id)
89aa0758
AS
2445{
2446 switch (func_id) {
2447 case BPF_FUNC_map_lookup_elem:
2448 return &bpf_map_lookup_elem_proto;
2449 case BPF_FUNC_map_update_elem:
2450 return &bpf_map_update_elem_proto;
2451 case BPF_FUNC_map_delete_elem:
2452 return &bpf_map_delete_elem_proto;
03e69b50
DB
2453 case BPF_FUNC_get_prandom_u32:
2454 return &bpf_get_prandom_u32_proto;
c04167ce 2455 case BPF_FUNC_get_smp_processor_id:
80b48c44 2456 return &bpf_get_raw_smp_processor_id_proto;
04fd61ab
AS
2457 case BPF_FUNC_tail_call:
2458 return &bpf_tail_call_proto;
17ca8cbf
DB
2459 case BPF_FUNC_ktime_get_ns:
2460 return &bpf_ktime_get_ns_proto;
0756ea3e 2461 case BPF_FUNC_trace_printk:
1be7f75d
AS
2462 if (capable(CAP_SYS_ADMIN))
2463 return bpf_get_trace_printk_proto();
89aa0758
AS
2464 default:
2465 return NULL;
2466 }
2467}
2468
608cd71a
AS
2469static const struct bpf_func_proto *
2470tc_cls_act_func_proto(enum bpf_func_id func_id)
2471{
2472 switch (func_id) {
2473 case BPF_FUNC_skb_store_bytes:
2474 return &bpf_skb_store_bytes_proto;
05c74e5e
DB
2475 case BPF_FUNC_skb_load_bytes:
2476 return &bpf_skb_load_bytes_proto;
7d672345
DB
2477 case BPF_FUNC_csum_diff:
2478 return &bpf_csum_diff_proto;
91bc4822
AS
2479 case BPF_FUNC_l3_csum_replace:
2480 return &bpf_l3_csum_replace_proto;
2481 case BPF_FUNC_l4_csum_replace:
2482 return &bpf_l4_csum_replace_proto;
3896d655
AS
2483 case BPF_FUNC_clone_redirect:
2484 return &bpf_clone_redirect_proto;
8d20aabe
DB
2485 case BPF_FUNC_get_cgroup_classid:
2486 return &bpf_get_cgroup_classid_proto;
4e10df9a
AS
2487 case BPF_FUNC_skb_vlan_push:
2488 return &bpf_skb_vlan_push_proto;
2489 case BPF_FUNC_skb_vlan_pop:
2490 return &bpf_skb_vlan_pop_proto;
6578171a
DB
2491 case BPF_FUNC_skb_change_proto:
2492 return &bpf_skb_change_proto_proto;
d2485c42
DB
2493 case BPF_FUNC_skb_change_type:
2494 return &bpf_skb_change_type_proto;
5293efe6
DB
2495 case BPF_FUNC_skb_change_tail:
2496 return &bpf_skb_change_tail_proto;
d3aa45ce
AS
2497 case BPF_FUNC_skb_get_tunnel_key:
2498 return &bpf_skb_get_tunnel_key_proto;
2499 case BPF_FUNC_skb_set_tunnel_key:
14ca0751
DB
2500 return bpf_get_skb_set_tunnel_proto(func_id);
2501 case BPF_FUNC_skb_get_tunnel_opt:
2502 return &bpf_skb_get_tunnel_opt_proto;
2503 case BPF_FUNC_skb_set_tunnel_opt:
2504 return bpf_get_skb_set_tunnel_proto(func_id);
27b29f63
AS
2505 case BPF_FUNC_redirect:
2506 return &bpf_redirect_proto;
c46646d0
DB
2507 case BPF_FUNC_get_route_realm:
2508 return &bpf_get_route_realm_proto;
13c5c240
DB
2509 case BPF_FUNC_get_hash_recalc:
2510 return &bpf_get_hash_recalc_proto;
bd570ff9 2511 case BPF_FUNC_perf_event_output:
555c8a86 2512 return &bpf_skb_event_output_proto;
80b48c44
DB
2513 case BPF_FUNC_get_smp_processor_id:
2514 return &bpf_get_smp_processor_id_proto;
747ea55e
DB
2515 case BPF_FUNC_skb_under_cgroup:
2516 return &bpf_skb_under_cgroup_proto;
608cd71a
AS
2517 default:
2518 return sk_filter_func_proto(func_id);
2519 }
2520}
2521
6a773a15
BB
2522static const struct bpf_func_proto *
2523xdp_func_proto(enum bpf_func_id func_id)
2524{
4de16969
DB
2525 switch (func_id) {
2526 case BPF_FUNC_perf_event_output:
2527 return &bpf_xdp_event_output_proto;
2528 default:
2529 return sk_filter_func_proto(func_id);
2530 }
6a773a15
BB
2531}
2532
d691f9e8 2533static bool __is_valid_access(int off, int size, enum bpf_access_type type)
89aa0758 2534{
9bac3d6d
AS
2535 if (off < 0 || off >= sizeof(struct __sk_buff))
2536 return false;
4936e352 2537 /* The verifier guarantees that size > 0. */
9bac3d6d
AS
2538 if (off % size != 0)
2539 return false;
4936e352 2540 if (size != sizeof(__u32))
9bac3d6d
AS
2541 return false;
2542
2543 return true;
2544}
2545
d691f9e8 2546static bool sk_filter_is_valid_access(int off, int size,
19de99f7
AS
2547 enum bpf_access_type type,
2548 enum bpf_reg_type *reg_type)
d691f9e8 2549{
db58ba45
AS
2550 switch (off) {
2551 case offsetof(struct __sk_buff, tc_classid):
2552 case offsetof(struct __sk_buff, data):
2553 case offsetof(struct __sk_buff, data_end):
045efa82 2554 return false;
db58ba45 2555 }
045efa82 2556
d691f9e8
AS
2557 if (type == BPF_WRITE) {
2558 switch (off) {
2559 case offsetof(struct __sk_buff, cb[0]) ...
4936e352 2560 offsetof(struct __sk_buff, cb[4]):
d691f9e8
AS
2561 break;
2562 default:
2563 return false;
2564 }
2565 }
2566
2567 return __is_valid_access(off, size, type);
2568}
2569
2570static bool tc_cls_act_is_valid_access(int off, int size,
19de99f7
AS
2571 enum bpf_access_type type,
2572 enum bpf_reg_type *reg_type)
d691f9e8
AS
2573{
2574 if (type == BPF_WRITE) {
2575 switch (off) {
2576 case offsetof(struct __sk_buff, mark):
2577 case offsetof(struct __sk_buff, tc_index):
754f1e6a 2578 case offsetof(struct __sk_buff, priority):
d691f9e8 2579 case offsetof(struct __sk_buff, cb[0]) ...
09c37a2c
DB
2580 offsetof(struct __sk_buff, cb[4]):
2581 case offsetof(struct __sk_buff, tc_classid):
d691f9e8
AS
2582 break;
2583 default:
2584 return false;
2585 }
2586 }
19de99f7
AS
2587
2588 switch (off) {
2589 case offsetof(struct __sk_buff, data):
2590 *reg_type = PTR_TO_PACKET;
2591 break;
2592 case offsetof(struct __sk_buff, data_end):
2593 *reg_type = PTR_TO_PACKET_END;
2594 break;
2595 }
2596
d691f9e8
AS
2597 return __is_valid_access(off, size, type);
2598}
2599
6a773a15
BB
2600static bool __is_valid_xdp_access(int off, int size,
2601 enum bpf_access_type type)
2602{
2603 if (off < 0 || off >= sizeof(struct xdp_md))
2604 return false;
2605 if (off % size != 0)
2606 return false;
6088b582 2607 if (size != sizeof(__u32))
6a773a15
BB
2608 return false;
2609
2610 return true;
2611}
2612
2613static bool xdp_is_valid_access(int off, int size,
2614 enum bpf_access_type type,
2615 enum bpf_reg_type *reg_type)
2616{
2617 if (type == BPF_WRITE)
2618 return false;
2619
2620 switch (off) {
2621 case offsetof(struct xdp_md, data):
2622 *reg_type = PTR_TO_PACKET;
2623 break;
2624 case offsetof(struct xdp_md, data_end):
2625 *reg_type = PTR_TO_PACKET_END;
2626 break;
2627 }
2628
2629 return __is_valid_xdp_access(off, size, type);
2630}
2631
2632void bpf_warn_invalid_xdp_action(u32 act)
2633{
2634 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
2635}
2636EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
2637
d691f9e8
AS
2638static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2639 int src_reg, int ctx_off,
ff936a04
AS
2640 struct bpf_insn *insn_buf,
2641 struct bpf_prog *prog)
9bac3d6d
AS
2642{
2643 struct bpf_insn *insn = insn_buf;
2644
2645 switch (ctx_off) {
2646 case offsetof(struct __sk_buff, len):
2647 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2648
2649 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2650 offsetof(struct sk_buff, len));
2651 break;
2652
0b8c707d
DB
2653 case offsetof(struct __sk_buff, protocol):
2654 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2655
2656 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2657 offsetof(struct sk_buff, protocol));
2658 break;
2659
27cd5452
MS
2660 case offsetof(struct __sk_buff, vlan_proto):
2661 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2662
2663 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2664 offsetof(struct sk_buff, vlan_proto));
2665 break;
2666
bcad5718
DB
2667 case offsetof(struct __sk_buff, priority):
2668 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2669
754f1e6a
DB
2670 if (type == BPF_WRITE)
2671 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2672 offsetof(struct sk_buff, priority));
2673 else
2674 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2675 offsetof(struct sk_buff, priority));
bcad5718
DB
2676 break;
2677
37e82c2f
AS
2678 case offsetof(struct __sk_buff, ingress_ifindex):
2679 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2680
2681 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2682 offsetof(struct sk_buff, skb_iif));
2683 break;
2684
2685 case offsetof(struct __sk_buff, ifindex):
2686 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2687
2688 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
2689 dst_reg, src_reg,
2690 offsetof(struct sk_buff, dev));
2691 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2692 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2693 offsetof(struct net_device, ifindex));
2694 break;
2695
ba7591d8
DB
2696 case offsetof(struct __sk_buff, hash):
2697 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2698
2699 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2700 offsetof(struct sk_buff, hash));
2701 break;
2702
9bac3d6d 2703 case offsetof(struct __sk_buff, mark):
d691f9e8
AS
2704 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2705
2706 if (type == BPF_WRITE)
2707 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2708 offsetof(struct sk_buff, mark));
2709 else
2710 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2711 offsetof(struct sk_buff, mark));
2712 break;
9bac3d6d
AS
2713
2714 case offsetof(struct __sk_buff, pkt_type):
2715 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2716
2717 case offsetof(struct __sk_buff, queue_mapping):
2718 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
c2497395 2719
c2497395
AS
2720 case offsetof(struct __sk_buff, vlan_present):
2721 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2722 dst_reg, src_reg, insn);
2723
2724 case offsetof(struct __sk_buff, vlan_tci):
2725 return convert_skb_access(SKF_AD_VLAN_TAG,
2726 dst_reg, src_reg, insn);
d691f9e8
AS
2727
2728 case offsetof(struct __sk_buff, cb[0]) ...
6088b582 2729 offsetof(struct __sk_buff, cb[4]):
d691f9e8
AS
2730 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2731
ff936a04 2732 prog->cb_access = 1;
d691f9e8
AS
2733 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2734 ctx_off += offsetof(struct sk_buff, cb);
2735 ctx_off += offsetof(struct qdisc_skb_cb, data);
2736 if (type == BPF_WRITE)
2737 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2738 else
2739 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2740 break;
2741
045efa82
DB
2742 case offsetof(struct __sk_buff, tc_classid):
2743 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2744 ctx_off += offsetof(struct sk_buff, cb);
2745 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
09c37a2c
DB
2746 if (type == BPF_WRITE)
2747 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2748 else
2749 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
045efa82
DB
2750 break;
2751
db58ba45
AS
2752 case offsetof(struct __sk_buff, data):
2753 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, data)),
2754 dst_reg, src_reg,
2755 offsetof(struct sk_buff, data));
2756 break;
2757
2758 case offsetof(struct __sk_buff, data_end):
2759 ctx_off -= offsetof(struct __sk_buff, data_end);
2760 ctx_off += offsetof(struct sk_buff, cb);
2761 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
2762 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(sizeof(void *)),
2763 dst_reg, src_reg, ctx_off);
2764 break;
2765
d691f9e8
AS
2766 case offsetof(struct __sk_buff, tc_index):
2767#ifdef CONFIG_NET_SCHED
2768 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2769
2770 if (type == BPF_WRITE)
2771 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2772 offsetof(struct sk_buff, tc_index));
2773 else
2774 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2775 offsetof(struct sk_buff, tc_index));
2776 break;
2777#else
2778 if (type == BPF_WRITE)
2779 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2780 else
2781 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2782 break;
2783#endif
9bac3d6d
AS
2784 }
2785
2786 return insn - insn_buf;
89aa0758
AS
2787}
2788
6a773a15
BB
2789static u32 xdp_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2790 int src_reg, int ctx_off,
2791 struct bpf_insn *insn_buf,
2792 struct bpf_prog *prog)
2793{
2794 struct bpf_insn *insn = insn_buf;
2795
2796 switch (ctx_off) {
2797 case offsetof(struct xdp_md, data):
2798 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct xdp_buff, data)),
2799 dst_reg, src_reg,
2800 offsetof(struct xdp_buff, data));
2801 break;
2802 case offsetof(struct xdp_md, data_end):
2803 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct xdp_buff, data_end)),
2804 dst_reg, src_reg,
2805 offsetof(struct xdp_buff, data_end));
2806 break;
2807 }
2808
2809 return insn - insn_buf;
2810}
2811
d4052c4a 2812static const struct bpf_verifier_ops sk_filter_ops = {
4936e352
DB
2813 .get_func_proto = sk_filter_func_proto,
2814 .is_valid_access = sk_filter_is_valid_access,
2815 .convert_ctx_access = bpf_net_convert_ctx_access,
89aa0758
AS
2816};
2817
608cd71a 2818static const struct bpf_verifier_ops tc_cls_act_ops = {
4936e352
DB
2819 .get_func_proto = tc_cls_act_func_proto,
2820 .is_valid_access = tc_cls_act_is_valid_access,
2821 .convert_ctx_access = bpf_net_convert_ctx_access,
608cd71a
AS
2822};
2823
6a773a15
BB
2824static const struct bpf_verifier_ops xdp_ops = {
2825 .get_func_proto = xdp_func_proto,
2826 .is_valid_access = xdp_is_valid_access,
2827 .convert_ctx_access = xdp_convert_ctx_access,
2828};
2829
d4052c4a 2830static struct bpf_prog_type_list sk_filter_type __read_mostly = {
4936e352
DB
2831 .ops = &sk_filter_ops,
2832 .type = BPF_PROG_TYPE_SOCKET_FILTER,
89aa0758
AS
2833};
2834
96be4325 2835static struct bpf_prog_type_list sched_cls_type __read_mostly = {
4936e352
DB
2836 .ops = &tc_cls_act_ops,
2837 .type = BPF_PROG_TYPE_SCHED_CLS,
96be4325
DB
2838};
2839
94caee8c 2840static struct bpf_prog_type_list sched_act_type __read_mostly = {
4936e352
DB
2841 .ops = &tc_cls_act_ops,
2842 .type = BPF_PROG_TYPE_SCHED_ACT,
94caee8c
DB
2843};
2844
6a773a15
BB
2845static struct bpf_prog_type_list xdp_type __read_mostly = {
2846 .ops = &xdp_ops,
2847 .type = BPF_PROG_TYPE_XDP,
2848};
2849
d4052c4a 2850static int __init register_sk_filter_ops(void)
89aa0758 2851{
d4052c4a 2852 bpf_register_prog_type(&sk_filter_type);
96be4325 2853 bpf_register_prog_type(&sched_cls_type);
94caee8c 2854 bpf_register_prog_type(&sched_act_type);
6a773a15 2855 bpf_register_prog_type(&xdp_type);
96be4325 2856
89aa0758
AS
2857 return 0;
2858}
d4052c4a
DB
2859late_initcall(register_sk_filter_ops);
2860
8ced425e 2861int sk_detach_filter(struct sock *sk)
55b33325
PE
2862{
2863 int ret = -ENOENT;
2864 struct sk_filter *filter;
2865
d59577b6
VB
2866 if (sock_flag(sk, SOCK_FILTER_LOCKED))
2867 return -EPERM;
2868
8ced425e
HFS
2869 filter = rcu_dereference_protected(sk->sk_filter,
2870 lockdep_sock_is_held(sk));
55b33325 2871 if (filter) {
a9b3cd7f 2872 RCU_INIT_POINTER(sk->sk_filter, NULL);
46bcf14f 2873 sk_filter_uncharge(sk, filter);
55b33325
PE
2874 ret = 0;
2875 }
a3ea269b 2876
55b33325
PE
2877 return ret;
2878}
8ced425e 2879EXPORT_SYMBOL_GPL(sk_detach_filter);
a8fc9277 2880
a3ea269b
DB
2881int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
2882 unsigned int len)
a8fc9277 2883{
a3ea269b 2884 struct sock_fprog_kern *fprog;
a8fc9277 2885 struct sk_filter *filter;
a3ea269b 2886 int ret = 0;
a8fc9277
PE
2887
2888 lock_sock(sk);
2889 filter = rcu_dereference_protected(sk->sk_filter,
8ced425e 2890 lockdep_sock_is_held(sk));
a8fc9277
PE
2891 if (!filter)
2892 goto out;
a3ea269b
DB
2893
2894 /* We're copying the filter that has been originally attached,
93d08b69
DB
2895 * so no conversion/decode needed anymore. eBPF programs that
2896 * have no original program cannot be dumped through this.
a3ea269b 2897 */
93d08b69 2898 ret = -EACCES;
7ae457c1 2899 fprog = filter->prog->orig_prog;
93d08b69
DB
2900 if (!fprog)
2901 goto out;
a3ea269b
DB
2902
2903 ret = fprog->len;
a8fc9277 2904 if (!len)
a3ea269b 2905 /* User space only enquires number of filter blocks. */
a8fc9277 2906 goto out;
a3ea269b 2907
a8fc9277 2908 ret = -EINVAL;
a3ea269b 2909 if (len < fprog->len)
a8fc9277
PE
2910 goto out;
2911
2912 ret = -EFAULT;
009937e7 2913 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
a3ea269b 2914 goto out;
a8fc9277 2915
a3ea269b
DB
2916 /* Instead of bytes, the API requests to return the number
2917 * of filter blocks.
2918 */
2919 ret = fprog->len;
a8fc9277
PE
2920out:
2921 release_sock(sk);
2922 return ret;
2923}