bpf: add devmap, a map for storing net device references
[linux-2.6-block.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>
91b8270f 29#include <linux/sock_diag.h>
1da177e4
LT
30#include <linux/in.h>
31#include <linux/inet.h>
32#include <linux/netdevice.h>
33#include <linux/if_packet.h>
c491680f 34#include <linux/if_arp.h>
5a0e3ad6 35#include <linux/gfp.h>
1da177e4
LT
36#include <net/ip.h>
37#include <net/protocol.h>
4738c1db 38#include <net/netlink.h>
1da177e4
LT
39#include <linux/skbuff.h>
40#include <net/sock.h>
10b89ee4 41#include <net/flow_dissector.h>
1da177e4
LT
42#include <linux/errno.h>
43#include <linux/timer.h>
7c0f6ba6 44#include <linux/uaccess.h>
40daafc8 45#include <asm/unaligned.h>
1da177e4 46#include <linux/filter.h>
86e4ca66 47#include <linux/ratelimit.h>
46b325c7 48#include <linux/seccomp.h>
f3335031 49#include <linux/if_vlan.h>
89aa0758 50#include <linux/bpf.h>
d691f9e8 51#include <net/sch_generic.h>
8d20aabe 52#include <net/cls_cgroup.h>
d3aa45ce 53#include <net/dst_metadata.h>
c46646d0 54#include <net/dst.h>
538950a1 55#include <net/sock_reuseport.h>
b1d9fc41 56#include <net/busy_poll.h>
8c4b4c7e 57#include <net/tcp.h>
5acaee0a 58#include <linux/bpf_trace.h>
1da177e4 59
43db6d65 60/**
f4979fce 61 * sk_filter_trim_cap - run a packet through a socket filter
43db6d65
SH
62 * @sk: sock associated with &sk_buff
63 * @skb: buffer to filter
f4979fce 64 * @cap: limit on how short the eBPF program may trim the packet
43db6d65 65 *
ff936a04
AS
66 * Run the eBPF program and then cut skb->data to correct size returned by
67 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
43db6d65 68 * than pkt_len we keep whole skb->data. This is the socket level
ff936a04 69 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
43db6d65
SH
70 * be accepted or -EPERM if the packet should be tossed.
71 *
72 */
f4979fce 73int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
43db6d65
SH
74{
75 int err;
76 struct sk_filter *filter;
77
c93bdd0e
MG
78 /*
79 * If the skb was allocated from pfmemalloc reserves, only
80 * allow SOCK_MEMALLOC sockets to use it as this socket is
81 * helping free memory
82 */
8fe809a9
ED
83 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
84 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
c93bdd0e 85 return -ENOMEM;
8fe809a9 86 }
c11cd3a6
DM
87 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
88 if (err)
89 return err;
90
43db6d65
SH
91 err = security_sock_rcv_skb(sk, skb);
92 if (err)
93 return err;
94
80f8f102
ED
95 rcu_read_lock();
96 filter = rcu_dereference(sk->sk_filter);
43db6d65 97 if (filter) {
8f917bba
WB
98 struct sock *save_sk = skb->sk;
99 unsigned int pkt_len;
100
101 skb->sk = sk;
102 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
8f917bba 103 skb->sk = save_sk;
d1f496fd 104 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
43db6d65 105 }
80f8f102 106 rcu_read_unlock();
43db6d65
SH
107
108 return err;
109}
f4979fce 110EXPORT_SYMBOL(sk_filter_trim_cap);
43db6d65 111
f3694e00 112BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
bd4cf0ed 113{
f3694e00 114 return skb_get_poff(skb);
bd4cf0ed
AS
115}
116
f3694e00 117BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
bd4cf0ed 118{
bd4cf0ed
AS
119 struct nlattr *nla;
120
121 if (skb_is_nonlinear(skb))
122 return 0;
123
05ab8f26
MK
124 if (skb->len < sizeof(struct nlattr))
125 return 0;
126
30743837 127 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
128 return 0;
129
30743837 130 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
bd4cf0ed
AS
131 if (nla)
132 return (void *) nla - (void *) skb->data;
133
134 return 0;
135}
136
f3694e00 137BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
bd4cf0ed 138{
bd4cf0ed
AS
139 struct nlattr *nla;
140
141 if (skb_is_nonlinear(skb))
142 return 0;
143
05ab8f26
MK
144 if (skb->len < sizeof(struct nlattr))
145 return 0;
146
30743837 147 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
148 return 0;
149
30743837
DB
150 nla = (struct nlattr *) &skb->data[a];
151 if (nla->nla_len > skb->len - a)
bd4cf0ed
AS
152 return 0;
153
30743837 154 nla = nla_find_nested(nla, x);
bd4cf0ed
AS
155 if (nla)
156 return (void *) nla - (void *) skb->data;
157
158 return 0;
159}
160
f3694e00 161BPF_CALL_0(__get_raw_cpu_id)
bd4cf0ed
AS
162{
163 return raw_smp_processor_id();
164}
165
80b48c44
DB
166static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
167 .func = __get_raw_cpu_id,
168 .gpl_only = false,
169 .ret_type = RET_INTEGER,
170};
171
9bac3d6d
AS
172static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
173 struct bpf_insn *insn_buf)
174{
175 struct bpf_insn *insn = insn_buf;
176
177 switch (skb_field) {
178 case SKF_AD_MARK:
179 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
180
181 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
182 offsetof(struct sk_buff, mark));
183 break;
184
185 case SKF_AD_PKTTYPE:
186 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
187 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
188#ifdef __BIG_ENDIAN_BITFIELD
189 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
190#endif
191 break;
192
193 case SKF_AD_QUEUE:
194 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
195
196 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
197 offsetof(struct sk_buff, queue_mapping));
198 break;
c2497395 199
c2497395
AS
200 case SKF_AD_VLAN_TAG:
201 case SKF_AD_VLAN_TAG_PRESENT:
202 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
203 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
204
205 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
206 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
207 offsetof(struct sk_buff, vlan_tci));
208 if (skb_field == SKF_AD_VLAN_TAG) {
209 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
210 ~VLAN_TAG_PRESENT);
211 } else {
212 /* dst_reg >>= 12 */
213 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
214 /* dst_reg &= 1 */
215 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
216 }
217 break;
9bac3d6d
AS
218 }
219
220 return insn - insn_buf;
221}
222
bd4cf0ed 223static bool convert_bpf_extensions(struct sock_filter *fp,
2695fb55 224 struct bpf_insn **insnp)
bd4cf0ed 225{
2695fb55 226 struct bpf_insn *insn = *insnp;
9bac3d6d 227 u32 cnt;
bd4cf0ed
AS
228
229 switch (fp->k) {
230 case SKF_AD_OFF + SKF_AD_PROTOCOL:
0b8c707d
DB
231 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
232
233 /* A = *(u16 *) (CTX + offsetof(protocol)) */
234 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
235 offsetof(struct sk_buff, protocol));
236 /* A = ntohs(A) [emitting a nop or swap16] */
237 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
bd4cf0ed
AS
238 break;
239
240 case SKF_AD_OFF + SKF_AD_PKTTYPE:
9bac3d6d
AS
241 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
242 insn += cnt - 1;
bd4cf0ed
AS
243 break;
244
245 case SKF_AD_OFF + SKF_AD_IFINDEX:
246 case SKF_AD_OFF + SKF_AD_HATYPE:
bd4cf0ed
AS
247 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
248 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
f8f6d679 249
f035a515 250 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
f8f6d679
DB
251 BPF_REG_TMP, BPF_REG_CTX,
252 offsetof(struct sk_buff, dev));
253 /* if (tmp != 0) goto pc + 1 */
254 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
255 *insn++ = BPF_EXIT_INSN();
256 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
257 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
258 offsetof(struct net_device, ifindex));
259 else
260 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
261 offsetof(struct net_device, type));
bd4cf0ed
AS
262 break;
263
264 case SKF_AD_OFF + SKF_AD_MARK:
9bac3d6d
AS
265 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
266 insn += cnt - 1;
bd4cf0ed
AS
267 break;
268
269 case SKF_AD_OFF + SKF_AD_RXHASH:
270 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
271
9739eef1
AS
272 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
273 offsetof(struct sk_buff, hash));
bd4cf0ed
AS
274 break;
275
276 case SKF_AD_OFF + SKF_AD_QUEUE:
9bac3d6d
AS
277 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
278 insn += cnt - 1;
bd4cf0ed
AS
279 break;
280
281 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
c2497395
AS
282 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
283 BPF_REG_A, BPF_REG_CTX, insn);
284 insn += cnt - 1;
285 break;
bd4cf0ed 286
c2497395
AS
287 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
288 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
289 BPF_REG_A, BPF_REG_CTX, insn);
290 insn += cnt - 1;
bd4cf0ed
AS
291 break;
292
27cd5452
MS
293 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
294 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
295
296 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
297 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
298 offsetof(struct sk_buff, vlan_proto));
299 /* A = ntohs(A) [emitting a nop or swap16] */
300 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
301 break;
302
bd4cf0ed
AS
303 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
304 case SKF_AD_OFF + SKF_AD_NLATTR:
305 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
306 case SKF_AD_OFF + SKF_AD_CPU:
4cd3675e 307 case SKF_AD_OFF + SKF_AD_RANDOM:
e430f34e 308 /* arg1 = CTX */
f8f6d679 309 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
bd4cf0ed 310 /* arg2 = A */
f8f6d679 311 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
bd4cf0ed 312 /* arg3 = X */
f8f6d679 313 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
e430f34e 314 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
bd4cf0ed
AS
315 switch (fp->k) {
316 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
f8f6d679 317 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
bd4cf0ed
AS
318 break;
319 case SKF_AD_OFF + SKF_AD_NLATTR:
f8f6d679 320 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
bd4cf0ed
AS
321 break;
322 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
f8f6d679 323 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
bd4cf0ed
AS
324 break;
325 case SKF_AD_OFF + SKF_AD_CPU:
f8f6d679 326 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
bd4cf0ed 327 break;
4cd3675e 328 case SKF_AD_OFF + SKF_AD_RANDOM:
3ad00405
DB
329 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
330 bpf_user_rnd_init_once();
4cd3675e 331 break;
bd4cf0ed
AS
332 }
333 break;
334
335 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
9739eef1
AS
336 /* A ^= X */
337 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
338 break;
339
340 default:
341 /* This is just a dummy call to avoid letting the compiler
342 * evict __bpf_call_base() as an optimization. Placed here
343 * where no-one bothers.
344 */
345 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
346 return false;
347 }
348
349 *insnp = insn;
350 return true;
351}
352
353/**
8fb575ca 354 * bpf_convert_filter - convert filter program
bd4cf0ed
AS
355 * @prog: the user passed filter program
356 * @len: the length of the user passed filter program
50bbfed9 357 * @new_prog: allocated 'struct bpf_prog' or NULL
bd4cf0ed
AS
358 * @new_len: pointer to store length of converted program
359 *
1f504ec9
TK
360 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
361 * style extended BPF (eBPF).
bd4cf0ed
AS
362 * Conversion workflow:
363 *
364 * 1) First pass for calculating the new program length:
8fb575ca 365 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
bd4cf0ed
AS
366 *
367 * 2) 2nd pass to remap in two passes: 1st pass finds new
368 * jump offsets, 2nd pass remapping:
8fb575ca 369 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
bd4cf0ed 370 */
d9e12f42 371static int bpf_convert_filter(struct sock_filter *prog, int len,
50bbfed9 372 struct bpf_prog *new_prog, int *new_len)
bd4cf0ed 373{
50bbfed9
AS
374 int new_flen = 0, pass = 0, target, i, stack_off;
375 struct bpf_insn *new_insn, *first_insn = NULL;
bd4cf0ed
AS
376 struct sock_filter *fp;
377 int *addrs = NULL;
378 u8 bpf_src;
379
380 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
30743837 381 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
bd4cf0ed 382
6f9a093b 383 if (len <= 0 || len > BPF_MAXINSNS)
bd4cf0ed
AS
384 return -EINVAL;
385
386 if (new_prog) {
50bbfed9 387 first_insn = new_prog->insnsi;
658da937
DB
388 addrs = kcalloc(len, sizeof(*addrs),
389 GFP_KERNEL | __GFP_NOWARN);
bd4cf0ed
AS
390 if (!addrs)
391 return -ENOMEM;
392 }
393
394do_pass:
50bbfed9 395 new_insn = first_insn;
bd4cf0ed
AS
396 fp = prog;
397
8b614aeb 398 /* Classic BPF related prologue emission. */
50bbfed9 399 if (new_prog) {
8b614aeb
DB
400 /* Classic BPF expects A and X to be reset first. These need
401 * to be guaranteed to be the first two instructions.
402 */
403 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
404 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
405
406 /* All programs must keep CTX in callee saved BPF_REG_CTX.
407 * In eBPF case it's done by the compiler, here we need to
408 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
409 */
410 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
411 } else {
412 new_insn += 3;
413 }
bd4cf0ed
AS
414
415 for (i = 0; i < len; fp++, i++) {
2695fb55
AS
416 struct bpf_insn tmp_insns[6] = { };
417 struct bpf_insn *insn = tmp_insns;
bd4cf0ed
AS
418
419 if (addrs)
50bbfed9 420 addrs[i] = new_insn - first_insn;
bd4cf0ed
AS
421
422 switch (fp->code) {
423 /* All arithmetic insns and skb loads map as-is. */
424 case BPF_ALU | BPF_ADD | BPF_X:
425 case BPF_ALU | BPF_ADD | BPF_K:
426 case BPF_ALU | BPF_SUB | BPF_X:
427 case BPF_ALU | BPF_SUB | BPF_K:
428 case BPF_ALU | BPF_AND | BPF_X:
429 case BPF_ALU | BPF_AND | BPF_K:
430 case BPF_ALU | BPF_OR | BPF_X:
431 case BPF_ALU | BPF_OR | BPF_K:
432 case BPF_ALU | BPF_LSH | BPF_X:
433 case BPF_ALU | BPF_LSH | BPF_K:
434 case BPF_ALU | BPF_RSH | BPF_X:
435 case BPF_ALU | BPF_RSH | BPF_K:
436 case BPF_ALU | BPF_XOR | BPF_X:
437 case BPF_ALU | BPF_XOR | BPF_K:
438 case BPF_ALU | BPF_MUL | BPF_X:
439 case BPF_ALU | BPF_MUL | BPF_K:
440 case BPF_ALU | BPF_DIV | BPF_X:
441 case BPF_ALU | BPF_DIV | BPF_K:
442 case BPF_ALU | BPF_MOD | BPF_X:
443 case BPF_ALU | BPF_MOD | BPF_K:
444 case BPF_ALU | BPF_NEG:
445 case BPF_LD | BPF_ABS | BPF_W:
446 case BPF_LD | BPF_ABS | BPF_H:
447 case BPF_LD | BPF_ABS | BPF_B:
448 case BPF_LD | BPF_IND | BPF_W:
449 case BPF_LD | BPF_IND | BPF_H:
450 case BPF_LD | BPF_IND | BPF_B:
451 /* Check for overloaded BPF extension and
452 * directly convert it if found, otherwise
453 * just move on with mapping.
454 */
455 if (BPF_CLASS(fp->code) == BPF_LD &&
456 BPF_MODE(fp->code) == BPF_ABS &&
457 convert_bpf_extensions(fp, &insn))
458 break;
459
f8f6d679 460 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
bd4cf0ed
AS
461 break;
462
f8f6d679
DB
463 /* Jump transformation cannot use BPF block macros
464 * everywhere as offset calculation and target updates
465 * require a bit more work than the rest, i.e. jump
466 * opcodes map as-is, but offsets need adjustment.
467 */
468
469#define BPF_EMIT_JMP \
bd4cf0ed
AS
470 do { \
471 if (target >= len || target < 0) \
472 goto err; \
473 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
474 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
475 insn->off -= insn - tmp_insns; \
476 } while (0)
477
f8f6d679
DB
478 case BPF_JMP | BPF_JA:
479 target = i + fp->k + 1;
480 insn->code = fp->code;
481 BPF_EMIT_JMP;
bd4cf0ed
AS
482 break;
483
484 case BPF_JMP | BPF_JEQ | BPF_K:
485 case BPF_JMP | BPF_JEQ | BPF_X:
486 case BPF_JMP | BPF_JSET | BPF_K:
487 case BPF_JMP | BPF_JSET | BPF_X:
488 case BPF_JMP | BPF_JGT | BPF_K:
489 case BPF_JMP | BPF_JGT | BPF_X:
490 case BPF_JMP | BPF_JGE | BPF_K:
491 case BPF_JMP | BPF_JGE | BPF_X:
492 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
493 /* BPF immediates are signed, zero extend
494 * immediate into tmp register and use it
495 * in compare insn.
496 */
f8f6d679 497 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
bd4cf0ed 498
e430f34e
AS
499 insn->dst_reg = BPF_REG_A;
500 insn->src_reg = BPF_REG_TMP;
bd4cf0ed
AS
501 bpf_src = BPF_X;
502 } else {
e430f34e 503 insn->dst_reg = BPF_REG_A;
bd4cf0ed
AS
504 insn->imm = fp->k;
505 bpf_src = BPF_SRC(fp->code);
19539ce7 506 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
1da177e4 507 }
bd4cf0ed
AS
508
509 /* Common case where 'jump_false' is next insn. */
510 if (fp->jf == 0) {
511 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
512 target = i + fp->jt + 1;
f8f6d679 513 BPF_EMIT_JMP;
bd4cf0ed 514 break;
1da177e4 515 }
bd4cf0ed
AS
516
517 /* Convert JEQ into JNE when 'jump_true' is next insn. */
518 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
519 insn->code = BPF_JMP | BPF_JNE | bpf_src;
520 target = i + fp->jf + 1;
f8f6d679 521 BPF_EMIT_JMP;
bd4cf0ed 522 break;
0b05b2a4 523 }
bd4cf0ed
AS
524
525 /* Other jumps are mapped into two insns: Jxx and JA. */
526 target = i + fp->jt + 1;
527 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
f8f6d679 528 BPF_EMIT_JMP;
bd4cf0ed
AS
529 insn++;
530
531 insn->code = BPF_JMP | BPF_JA;
532 target = i + fp->jf + 1;
f8f6d679 533 BPF_EMIT_JMP;
bd4cf0ed
AS
534 break;
535
536 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
537 case BPF_LDX | BPF_MSH | BPF_B:
9739eef1 538 /* tmp = A */
f8f6d679 539 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
1268e253 540 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
f8f6d679 541 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
9739eef1 542 /* A &= 0xf */
f8f6d679 543 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
9739eef1 544 /* A <<= 2 */
f8f6d679 545 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
9739eef1 546 /* X = A */
f8f6d679 547 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
9739eef1 548 /* A = tmp */
f8f6d679 549 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
bd4cf0ed
AS
550 break;
551
6205b9cf
DB
552 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
553 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
554 */
bd4cf0ed
AS
555 case BPF_RET | BPF_A:
556 case BPF_RET | BPF_K:
6205b9cf
DB
557 if (BPF_RVAL(fp->code) == BPF_K)
558 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
559 0, fp->k);
9739eef1 560 *insn = BPF_EXIT_INSN();
bd4cf0ed
AS
561 break;
562
563 /* Store to stack. */
564 case BPF_ST:
565 case BPF_STX:
50bbfed9 566 stack_off = fp->k * 4 + 4;
f8f6d679
DB
567 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
568 BPF_ST ? BPF_REG_A : BPF_REG_X,
50bbfed9
AS
569 -stack_off);
570 /* check_load_and_stores() verifies that classic BPF can
571 * load from stack only after write, so tracking
572 * stack_depth for ST|STX insns is enough
573 */
574 if (new_prog && new_prog->aux->stack_depth < stack_off)
575 new_prog->aux->stack_depth = stack_off;
bd4cf0ed
AS
576 break;
577
578 /* Load from stack. */
579 case BPF_LD | BPF_MEM:
580 case BPF_LDX | BPF_MEM:
50bbfed9 581 stack_off = fp->k * 4 + 4;
f8f6d679
DB
582 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
583 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
50bbfed9 584 -stack_off);
bd4cf0ed
AS
585 break;
586
587 /* A = K or X = K */
588 case BPF_LD | BPF_IMM:
589 case BPF_LDX | BPF_IMM:
f8f6d679
DB
590 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
591 BPF_REG_A : BPF_REG_X, fp->k);
bd4cf0ed
AS
592 break;
593
594 /* X = A */
595 case BPF_MISC | BPF_TAX:
f8f6d679 596 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
bd4cf0ed
AS
597 break;
598
599 /* A = X */
600 case BPF_MISC | BPF_TXA:
f8f6d679 601 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
602 break;
603
604 /* A = skb->len or X = skb->len */
605 case BPF_LD | BPF_W | BPF_LEN:
606 case BPF_LDX | BPF_W | BPF_LEN:
f8f6d679
DB
607 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
608 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
609 offsetof(struct sk_buff, len));
bd4cf0ed
AS
610 break;
611
f8f6d679 612 /* Access seccomp_data fields. */
bd4cf0ed 613 case BPF_LDX | BPF_ABS | BPF_W:
9739eef1
AS
614 /* A = *(u32 *) (ctx + K) */
615 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
bd4cf0ed
AS
616 break;
617
ca9f1fd2 618 /* Unknown instruction. */
1da177e4 619 default:
bd4cf0ed 620 goto err;
1da177e4 621 }
bd4cf0ed
AS
622
623 insn++;
624 if (new_prog)
625 memcpy(new_insn, tmp_insns,
626 sizeof(*insn) * (insn - tmp_insns));
bd4cf0ed 627 new_insn += insn - tmp_insns;
1da177e4
LT
628 }
629
bd4cf0ed
AS
630 if (!new_prog) {
631 /* Only calculating new length. */
50bbfed9 632 *new_len = new_insn - first_insn;
bd4cf0ed
AS
633 return 0;
634 }
635
636 pass++;
50bbfed9
AS
637 if (new_flen != new_insn - first_insn) {
638 new_flen = new_insn - first_insn;
bd4cf0ed
AS
639 if (pass > 2)
640 goto err;
bd4cf0ed
AS
641 goto do_pass;
642 }
643
644 kfree(addrs);
645 BUG_ON(*new_len != new_flen);
1da177e4 646 return 0;
bd4cf0ed
AS
647err:
648 kfree(addrs);
649 return -EINVAL;
1da177e4
LT
650}
651
bd4cf0ed 652/* Security:
bd4cf0ed 653 *
2d5311e4 654 * As we dont want to clear mem[] array for each packet going through
8ea6e345 655 * __bpf_prog_run(), we check that filter loaded by user never try to read
2d5311e4 656 * a cell if not previously written, and we check all branches to be sure
25985edc 657 * a malicious user doesn't try to abuse us.
2d5311e4 658 */
ec31a05c 659static int check_load_and_stores(const struct sock_filter *filter, int flen)
2d5311e4 660{
34805931 661 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
2d5311e4
ED
662 int pc, ret = 0;
663
664 BUILD_BUG_ON(BPF_MEMWORDS > 16);
34805931 665
99e72a0f 666 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
2d5311e4
ED
667 if (!masks)
668 return -ENOMEM;
34805931 669
2d5311e4
ED
670 memset(masks, 0xff, flen * sizeof(*masks));
671
672 for (pc = 0; pc < flen; pc++) {
673 memvalid &= masks[pc];
674
675 switch (filter[pc].code) {
34805931
DB
676 case BPF_ST:
677 case BPF_STX:
2d5311e4
ED
678 memvalid |= (1 << filter[pc].k);
679 break;
34805931
DB
680 case BPF_LD | BPF_MEM:
681 case BPF_LDX | BPF_MEM:
2d5311e4
ED
682 if (!(memvalid & (1 << filter[pc].k))) {
683 ret = -EINVAL;
684 goto error;
685 }
686 break;
34805931
DB
687 case BPF_JMP | BPF_JA:
688 /* A jump must set masks on target */
2d5311e4
ED
689 masks[pc + 1 + filter[pc].k] &= memvalid;
690 memvalid = ~0;
691 break;
34805931
DB
692 case BPF_JMP | BPF_JEQ | BPF_K:
693 case BPF_JMP | BPF_JEQ | BPF_X:
694 case BPF_JMP | BPF_JGE | BPF_K:
695 case BPF_JMP | BPF_JGE | BPF_X:
696 case BPF_JMP | BPF_JGT | BPF_K:
697 case BPF_JMP | BPF_JGT | BPF_X:
698 case BPF_JMP | BPF_JSET | BPF_K:
699 case BPF_JMP | BPF_JSET | BPF_X:
700 /* A jump must set masks on targets */
2d5311e4
ED
701 masks[pc + 1 + filter[pc].jt] &= memvalid;
702 masks[pc + 1 + filter[pc].jf] &= memvalid;
703 memvalid = ~0;
704 break;
705 }
706 }
707error:
708 kfree(masks);
709 return ret;
710}
711
34805931
DB
712static bool chk_code_allowed(u16 code_to_probe)
713{
714 static const bool codes[] = {
715 /* 32 bit ALU operations */
716 [BPF_ALU | BPF_ADD | BPF_K] = true,
717 [BPF_ALU | BPF_ADD | BPF_X] = true,
718 [BPF_ALU | BPF_SUB | BPF_K] = true,
719 [BPF_ALU | BPF_SUB | BPF_X] = true,
720 [BPF_ALU | BPF_MUL | BPF_K] = true,
721 [BPF_ALU | BPF_MUL | BPF_X] = true,
722 [BPF_ALU | BPF_DIV | BPF_K] = true,
723 [BPF_ALU | BPF_DIV | BPF_X] = true,
724 [BPF_ALU | BPF_MOD | BPF_K] = true,
725 [BPF_ALU | BPF_MOD | BPF_X] = true,
726 [BPF_ALU | BPF_AND | BPF_K] = true,
727 [BPF_ALU | BPF_AND | BPF_X] = true,
728 [BPF_ALU | BPF_OR | BPF_K] = true,
729 [BPF_ALU | BPF_OR | BPF_X] = true,
730 [BPF_ALU | BPF_XOR | BPF_K] = true,
731 [BPF_ALU | BPF_XOR | BPF_X] = true,
732 [BPF_ALU | BPF_LSH | BPF_K] = true,
733 [BPF_ALU | BPF_LSH | BPF_X] = true,
734 [BPF_ALU | BPF_RSH | BPF_K] = true,
735 [BPF_ALU | BPF_RSH | BPF_X] = true,
736 [BPF_ALU | BPF_NEG] = true,
737 /* Load instructions */
738 [BPF_LD | BPF_W | BPF_ABS] = true,
739 [BPF_LD | BPF_H | BPF_ABS] = true,
740 [BPF_LD | BPF_B | BPF_ABS] = true,
741 [BPF_LD | BPF_W | BPF_LEN] = true,
742 [BPF_LD | BPF_W | BPF_IND] = true,
743 [BPF_LD | BPF_H | BPF_IND] = true,
744 [BPF_LD | BPF_B | BPF_IND] = true,
745 [BPF_LD | BPF_IMM] = true,
746 [BPF_LD | BPF_MEM] = true,
747 [BPF_LDX | BPF_W | BPF_LEN] = true,
748 [BPF_LDX | BPF_B | BPF_MSH] = true,
749 [BPF_LDX | BPF_IMM] = true,
750 [BPF_LDX | BPF_MEM] = true,
751 /* Store instructions */
752 [BPF_ST] = true,
753 [BPF_STX] = true,
754 /* Misc instructions */
755 [BPF_MISC | BPF_TAX] = true,
756 [BPF_MISC | BPF_TXA] = true,
757 /* Return instructions */
758 [BPF_RET | BPF_K] = true,
759 [BPF_RET | BPF_A] = true,
760 /* Jump instructions */
761 [BPF_JMP | BPF_JA] = true,
762 [BPF_JMP | BPF_JEQ | BPF_K] = true,
763 [BPF_JMP | BPF_JEQ | BPF_X] = true,
764 [BPF_JMP | BPF_JGE | BPF_K] = true,
765 [BPF_JMP | BPF_JGE | BPF_X] = true,
766 [BPF_JMP | BPF_JGT | BPF_K] = true,
767 [BPF_JMP | BPF_JGT | BPF_X] = true,
768 [BPF_JMP | BPF_JSET | BPF_K] = true,
769 [BPF_JMP | BPF_JSET | BPF_X] = true,
770 };
771
772 if (code_to_probe >= ARRAY_SIZE(codes))
773 return false;
774
775 return codes[code_to_probe];
776}
777
f7bd9e36
DB
778static bool bpf_check_basics_ok(const struct sock_filter *filter,
779 unsigned int flen)
780{
781 if (filter == NULL)
782 return false;
783 if (flen == 0 || flen > BPF_MAXINSNS)
784 return false;
785
786 return true;
787}
788
1da177e4 789/**
4df95ff4 790 * bpf_check_classic - verify socket filter code
1da177e4
LT
791 * @filter: filter to verify
792 * @flen: length of filter
793 *
794 * Check the user's filter code. If we let some ugly
795 * filter code slip through kaboom! The filter must contain
93699863
KK
796 * no references or jumps that are out of range, no illegal
797 * instructions, and must end with a RET instruction.
1da177e4 798 *
7b11f69f
KK
799 * All jumps are forward as they are not signed.
800 *
801 * Returns 0 if the rule set is legal or -EINVAL if not.
1da177e4 802 */
d9e12f42
NS
803static int bpf_check_classic(const struct sock_filter *filter,
804 unsigned int flen)
1da177e4 805{
aa1113d9 806 bool anc_found;
34805931 807 int pc;
1da177e4 808
34805931 809 /* Check the filter code now */
1da177e4 810 for (pc = 0; pc < flen; pc++) {
ec31a05c 811 const struct sock_filter *ftest = &filter[pc];
93699863 812
34805931
DB
813 /* May we actually operate on this code? */
814 if (!chk_code_allowed(ftest->code))
cba328fc 815 return -EINVAL;
34805931 816
93699863 817 /* Some instructions need special checks */
34805931
DB
818 switch (ftest->code) {
819 case BPF_ALU | BPF_DIV | BPF_K:
820 case BPF_ALU | BPF_MOD | BPF_K:
821 /* Check for division by zero */
b6069a95
ED
822 if (ftest->k == 0)
823 return -EINVAL;
824 break;
229394e8
RV
825 case BPF_ALU | BPF_LSH | BPF_K:
826 case BPF_ALU | BPF_RSH | BPF_K:
827 if (ftest->k >= 32)
828 return -EINVAL;
829 break;
34805931
DB
830 case BPF_LD | BPF_MEM:
831 case BPF_LDX | BPF_MEM:
832 case BPF_ST:
833 case BPF_STX:
834 /* Check for invalid memory addresses */
93699863
KK
835 if (ftest->k >= BPF_MEMWORDS)
836 return -EINVAL;
837 break;
34805931
DB
838 case BPF_JMP | BPF_JA:
839 /* Note, the large ftest->k might cause loops.
93699863
KK
840 * Compare this with conditional jumps below,
841 * where offsets are limited. --ANK (981016)
842 */
34805931 843 if (ftest->k >= (unsigned int)(flen - pc - 1))
93699863 844 return -EINVAL;
01f2f3f6 845 break;
34805931
DB
846 case BPF_JMP | BPF_JEQ | BPF_K:
847 case BPF_JMP | BPF_JEQ | BPF_X:
848 case BPF_JMP | BPF_JGE | BPF_K:
849 case BPF_JMP | BPF_JGE | BPF_X:
850 case BPF_JMP | BPF_JGT | BPF_K:
851 case BPF_JMP | BPF_JGT | BPF_X:
852 case BPF_JMP | BPF_JSET | BPF_K:
853 case BPF_JMP | BPF_JSET | BPF_X:
854 /* Both conditionals must be safe */
e35bedf3 855 if (pc + ftest->jt + 1 >= flen ||
93699863
KK
856 pc + ftest->jf + 1 >= flen)
857 return -EINVAL;
cba328fc 858 break;
34805931
DB
859 case BPF_LD | BPF_W | BPF_ABS:
860 case BPF_LD | BPF_H | BPF_ABS:
861 case BPF_LD | BPF_B | BPF_ABS:
aa1113d9 862 anc_found = false;
34805931
DB
863 if (bpf_anc_helper(ftest) & BPF_ANC)
864 anc_found = true;
865 /* Ancillary operation unknown or unsupported */
aa1113d9
DB
866 if (anc_found == false && ftest->k >= SKF_AD_OFF)
867 return -EINVAL;
01f2f3f6
HPP
868 }
869 }
93699863 870
34805931 871 /* Last instruction must be a RET code */
01f2f3f6 872 switch (filter[flen - 1].code) {
34805931
DB
873 case BPF_RET | BPF_K:
874 case BPF_RET | BPF_A:
2d5311e4 875 return check_load_and_stores(filter, flen);
cba328fc 876 }
34805931 877
cba328fc 878 return -EINVAL;
1da177e4
LT
879}
880
7ae457c1
AS
881static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
882 const struct sock_fprog *fprog)
a3ea269b 883{
009937e7 884 unsigned int fsize = bpf_classic_proglen(fprog);
a3ea269b
DB
885 struct sock_fprog_kern *fkprog;
886
887 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
888 if (!fp->orig_prog)
889 return -ENOMEM;
890
891 fkprog = fp->orig_prog;
892 fkprog->len = fprog->len;
658da937
DB
893
894 fkprog->filter = kmemdup(fp->insns, fsize,
895 GFP_KERNEL | __GFP_NOWARN);
a3ea269b
DB
896 if (!fkprog->filter) {
897 kfree(fp->orig_prog);
898 return -ENOMEM;
899 }
900
901 return 0;
902}
903
7ae457c1 904static void bpf_release_orig_filter(struct bpf_prog *fp)
a3ea269b
DB
905{
906 struct sock_fprog_kern *fprog = fp->orig_prog;
907
908 if (fprog) {
909 kfree(fprog->filter);
910 kfree(fprog);
911 }
912}
913
7ae457c1
AS
914static void __bpf_prog_release(struct bpf_prog *prog)
915{
24701ece 916 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
89aa0758
AS
917 bpf_prog_put(prog);
918 } else {
919 bpf_release_orig_filter(prog);
920 bpf_prog_free(prog);
921 }
7ae457c1
AS
922}
923
34c5bd66
PN
924static void __sk_filter_release(struct sk_filter *fp)
925{
7ae457c1
AS
926 __bpf_prog_release(fp->prog);
927 kfree(fp);
34c5bd66
PN
928}
929
47e958ea 930/**
46bcf14f 931 * sk_filter_release_rcu - Release a socket filter by rcu_head
47e958ea
PE
932 * @rcu: rcu_head that contains the sk_filter to free
933 */
fbc907f0 934static void sk_filter_release_rcu(struct rcu_head *rcu)
47e958ea
PE
935{
936 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
937
34c5bd66 938 __sk_filter_release(fp);
47e958ea 939}
fbc907f0
DB
940
941/**
942 * sk_filter_release - release a socket filter
943 * @fp: filter to remove
944 *
945 * Remove a filter from a socket and release its resources.
946 */
947static void sk_filter_release(struct sk_filter *fp)
948{
4c355cdf 949 if (refcount_dec_and_test(&fp->refcnt))
fbc907f0
DB
950 call_rcu(&fp->rcu, sk_filter_release_rcu);
951}
952
953void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
954{
7ae457c1 955 u32 filter_size = bpf_prog_size(fp->prog->len);
fbc907f0 956
278571ba
AS
957 atomic_sub(filter_size, &sk->sk_omem_alloc);
958 sk_filter_release(fp);
fbc907f0 959}
47e958ea 960
278571ba
AS
961/* try to charge the socket memory if there is space available
962 * return true on success
963 */
4c355cdf 964static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
bd4cf0ed 965{
7ae457c1 966 u32 filter_size = bpf_prog_size(fp->prog->len);
278571ba
AS
967
968 /* same check as in sock_kmalloc() */
969 if (filter_size <= sysctl_optmem_max &&
970 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
278571ba
AS
971 atomic_add(filter_size, &sk->sk_omem_alloc);
972 return true;
bd4cf0ed 973 }
278571ba 974 return false;
bd4cf0ed
AS
975}
976
4c355cdf
RE
977bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
978{
979 bool ret = __sk_filter_charge(sk, fp);
980 if (ret)
981 refcount_inc(&fp->refcnt);
982 return ret;
983}
984
7ae457c1 985static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
bd4cf0ed
AS
986{
987 struct sock_filter *old_prog;
7ae457c1 988 struct bpf_prog *old_fp;
34805931 989 int err, new_len, old_len = fp->len;
bd4cf0ed
AS
990
991 /* We are free to overwrite insns et al right here as it
992 * won't be used at this point in time anymore internally
993 * after the migration to the internal BPF instruction
994 * representation.
995 */
996 BUILD_BUG_ON(sizeof(struct sock_filter) !=
2695fb55 997 sizeof(struct bpf_insn));
bd4cf0ed 998
bd4cf0ed
AS
999 /* Conversion cannot happen on overlapping memory areas,
1000 * so we need to keep the user BPF around until the 2nd
1001 * pass. At this time, the user BPF is stored in fp->insns.
1002 */
1003 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
658da937 1004 GFP_KERNEL | __GFP_NOWARN);
bd4cf0ed
AS
1005 if (!old_prog) {
1006 err = -ENOMEM;
1007 goto out_err;
1008 }
1009
1010 /* 1st pass: calculate the new program length. */
8fb575ca 1011 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
bd4cf0ed
AS
1012 if (err)
1013 goto out_err_free;
1014
1015 /* Expand fp for appending the new filter representation. */
1016 old_fp = fp;
60a3b225 1017 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
bd4cf0ed
AS
1018 if (!fp) {
1019 /* The old_fp is still around in case we couldn't
1020 * allocate new memory, so uncharge on that one.
1021 */
1022 fp = old_fp;
1023 err = -ENOMEM;
1024 goto out_err_free;
1025 }
1026
bd4cf0ed
AS
1027 fp->len = new_len;
1028
2695fb55 1029 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
50bbfed9 1030 err = bpf_convert_filter(old_prog, old_len, fp, &new_len);
bd4cf0ed 1031 if (err)
8fb575ca 1032 /* 2nd bpf_convert_filter() can fail only if it fails
bd4cf0ed
AS
1033 * to allocate memory, remapping must succeed. Note,
1034 * that at this time old_fp has already been released
278571ba 1035 * by krealloc().
bd4cf0ed
AS
1036 */
1037 goto out_err_free;
1038
d1c55ab5
DB
1039 /* We are guaranteed to never error here with cBPF to eBPF
1040 * transitions, since there's no issue with type compatibility
1041 * checks on program arrays.
1042 */
1043 fp = bpf_prog_select_runtime(fp, &err);
5fe821a9 1044
bd4cf0ed
AS
1045 kfree(old_prog);
1046 return fp;
1047
1048out_err_free:
1049 kfree(old_prog);
1050out_err:
7ae457c1 1051 __bpf_prog_release(fp);
bd4cf0ed
AS
1052 return ERR_PTR(err);
1053}
1054
ac67eb2c
DB
1055static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1056 bpf_aux_classic_check_t trans)
302d6637
JP
1057{
1058 int err;
1059
bd4cf0ed 1060 fp->bpf_func = NULL;
a91263d5 1061 fp->jited = 0;
302d6637 1062
4df95ff4 1063 err = bpf_check_classic(fp->insns, fp->len);
418c96ac 1064 if (err) {
7ae457c1 1065 __bpf_prog_release(fp);
bd4cf0ed 1066 return ERR_PTR(err);
418c96ac 1067 }
302d6637 1068
4ae92bc7
NS
1069 /* There might be additional checks and transformations
1070 * needed on classic filters, f.e. in case of seccomp.
1071 */
1072 if (trans) {
1073 err = trans(fp->insns, fp->len);
1074 if (err) {
1075 __bpf_prog_release(fp);
1076 return ERR_PTR(err);
1077 }
1078 }
1079
bd4cf0ed
AS
1080 /* Probe if we can JIT compile the filter and if so, do
1081 * the compilation of the filter.
1082 */
302d6637 1083 bpf_jit_compile(fp);
bd4cf0ed
AS
1084
1085 /* JIT compiler couldn't process this filter, so do the
1086 * internal BPF translation for the optimized interpreter.
1087 */
5fe821a9 1088 if (!fp->jited)
7ae457c1 1089 fp = bpf_migrate_filter(fp);
bd4cf0ed
AS
1090
1091 return fp;
302d6637
JP
1092}
1093
1094/**
7ae457c1 1095 * bpf_prog_create - create an unattached filter
c6c4b97c 1096 * @pfp: the unattached filter that is created
677a9fd3 1097 * @fprog: the filter program
302d6637 1098 *
c6c4b97c 1099 * Create a filter independent of any socket. We first run some
302d6637
JP
1100 * sanity checks on it to make sure it does not explode on us later.
1101 * If an error occurs or there is insufficient memory for the filter
1102 * a negative errno code is returned. On success the return is zero.
1103 */
7ae457c1 1104int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
302d6637 1105{
009937e7 1106 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1 1107 struct bpf_prog *fp;
302d6637
JP
1108
1109 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1110 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
302d6637
JP
1111 return -EINVAL;
1112
60a3b225 1113 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
302d6637
JP
1114 if (!fp)
1115 return -ENOMEM;
a3ea269b 1116
302d6637
JP
1117 memcpy(fp->insns, fprog->filter, fsize);
1118
302d6637 1119 fp->len = fprog->len;
a3ea269b
DB
1120 /* Since unattached filters are not copied back to user
1121 * space through sk_get_filter(), we do not need to hold
1122 * a copy here, and can spare us the work.
1123 */
1124 fp->orig_prog = NULL;
302d6637 1125
7ae457c1 1126 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1127 * memory in case something goes wrong.
1128 */
4ae92bc7 1129 fp = bpf_prepare_filter(fp, NULL);
bd4cf0ed
AS
1130 if (IS_ERR(fp))
1131 return PTR_ERR(fp);
302d6637
JP
1132
1133 *pfp = fp;
1134 return 0;
302d6637 1135}
7ae457c1 1136EXPORT_SYMBOL_GPL(bpf_prog_create);
302d6637 1137
ac67eb2c
DB
1138/**
1139 * bpf_prog_create_from_user - create an unattached filter from user buffer
1140 * @pfp: the unattached filter that is created
1141 * @fprog: the filter program
1142 * @trans: post-classic verifier transformation handler
bab18991 1143 * @save_orig: save classic BPF program
ac67eb2c
DB
1144 *
1145 * This function effectively does the same as bpf_prog_create(), only
1146 * that it builds up its insns buffer from user space provided buffer.
1147 * It also allows for passing a bpf_aux_classic_check_t handler.
1148 */
1149int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
bab18991 1150 bpf_aux_classic_check_t trans, bool save_orig)
ac67eb2c
DB
1151{
1152 unsigned int fsize = bpf_classic_proglen(fprog);
1153 struct bpf_prog *fp;
bab18991 1154 int err;
ac67eb2c
DB
1155
1156 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1157 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
ac67eb2c
DB
1158 return -EINVAL;
1159
1160 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1161 if (!fp)
1162 return -ENOMEM;
1163
1164 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1165 __bpf_prog_free(fp);
1166 return -EFAULT;
1167 }
1168
1169 fp->len = fprog->len;
ac67eb2c
DB
1170 fp->orig_prog = NULL;
1171
bab18991
DB
1172 if (save_orig) {
1173 err = bpf_prog_store_orig_filter(fp, fprog);
1174 if (err) {
1175 __bpf_prog_free(fp);
1176 return -ENOMEM;
1177 }
1178 }
1179
ac67eb2c
DB
1180 /* bpf_prepare_filter() already takes care of freeing
1181 * memory in case something goes wrong.
1182 */
1183 fp = bpf_prepare_filter(fp, trans);
1184 if (IS_ERR(fp))
1185 return PTR_ERR(fp);
1186
1187 *pfp = fp;
1188 return 0;
1189}
2ea273d7 1190EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
ac67eb2c 1191
7ae457c1 1192void bpf_prog_destroy(struct bpf_prog *fp)
302d6637 1193{
7ae457c1 1194 __bpf_prog_release(fp);
302d6637 1195}
7ae457c1 1196EXPORT_SYMBOL_GPL(bpf_prog_destroy);
302d6637 1197
8ced425e 1198static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
49b31e57
DB
1199{
1200 struct sk_filter *fp, *old_fp;
1201
1202 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1203 if (!fp)
1204 return -ENOMEM;
1205
1206 fp->prog = prog;
49b31e57 1207
4c355cdf 1208 if (!__sk_filter_charge(sk, fp)) {
49b31e57
DB
1209 kfree(fp);
1210 return -ENOMEM;
1211 }
4c355cdf 1212 refcount_set(&fp->refcnt, 1);
49b31e57 1213
8ced425e
HFS
1214 old_fp = rcu_dereference_protected(sk->sk_filter,
1215 lockdep_sock_is_held(sk));
49b31e57 1216 rcu_assign_pointer(sk->sk_filter, fp);
8ced425e 1217
49b31e57
DB
1218 if (old_fp)
1219 sk_filter_uncharge(sk, old_fp);
1220
1221 return 0;
1222}
1223
538950a1
CG
1224static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1225{
1226 struct bpf_prog *old_prog;
1227 int err;
1228
1229 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1230 return -ENOMEM;
1231
fa463497 1232 if (sk_unhashed(sk) && sk->sk_reuseport) {
538950a1
CG
1233 err = reuseport_alloc(sk);
1234 if (err)
1235 return err;
1236 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1237 /* The socket wasn't bound with SO_REUSEPORT */
1238 return -EINVAL;
1239 }
1240
1241 old_prog = reuseport_attach_prog(sk, prog);
1242 if (old_prog)
1243 bpf_prog_destroy(old_prog);
1244
1245 return 0;
1246}
1247
1248static
1249struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1da177e4 1250{
009937e7 1251 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1 1252 struct bpf_prog *prog;
1da177e4
LT
1253 int err;
1254
d59577b6 1255 if (sock_flag(sk, SOCK_FILTER_LOCKED))
538950a1 1256 return ERR_PTR(-EPERM);
d59577b6 1257
1da177e4 1258 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1259 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
538950a1 1260 return ERR_PTR(-EINVAL);
1da177e4 1261
f7bd9e36 1262 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
7ae457c1 1263 if (!prog)
538950a1 1264 return ERR_PTR(-ENOMEM);
a3ea269b 1265
7ae457c1 1266 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
c0d1379a 1267 __bpf_prog_free(prog);
538950a1 1268 return ERR_PTR(-EFAULT);
1da177e4
LT
1269 }
1270
7ae457c1 1271 prog->len = fprog->len;
1da177e4 1272
7ae457c1 1273 err = bpf_prog_store_orig_filter(prog, fprog);
a3ea269b 1274 if (err) {
c0d1379a 1275 __bpf_prog_free(prog);
538950a1 1276 return ERR_PTR(-ENOMEM);
a3ea269b
DB
1277 }
1278
7ae457c1 1279 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1280 * memory in case something goes wrong.
1281 */
538950a1
CG
1282 return bpf_prepare_filter(prog, NULL);
1283}
1284
1285/**
1286 * sk_attach_filter - attach a socket filter
1287 * @fprog: the filter program
1288 * @sk: the socket to use
1289 *
1290 * Attach the user's filter code. We first run some sanity checks on
1291 * it to make sure it does not explode on us later. If an error
1292 * occurs or there is insufficient memory for the filter a negative
1293 * errno code is returned. On success the return is zero.
1294 */
8ced425e 1295int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
538950a1
CG
1296{
1297 struct bpf_prog *prog = __get_filter(fprog, sk);
1298 int err;
1299
7ae457c1
AS
1300 if (IS_ERR(prog))
1301 return PTR_ERR(prog);
1302
8ced425e 1303 err = __sk_attach_prog(prog, sk);
49b31e57 1304 if (err < 0) {
7ae457c1 1305 __bpf_prog_release(prog);
49b31e57 1306 return err;
278571ba
AS
1307 }
1308
d3904b73 1309 return 0;
1da177e4 1310}
8ced425e 1311EXPORT_SYMBOL_GPL(sk_attach_filter);
1da177e4 1312
538950a1 1313int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
89aa0758 1314{
538950a1 1315 struct bpf_prog *prog = __get_filter(fprog, sk);
49b31e57 1316 int err;
89aa0758 1317
538950a1
CG
1318 if (IS_ERR(prog))
1319 return PTR_ERR(prog);
1320
1321 err = __reuseport_attach_prog(prog, sk);
1322 if (err < 0) {
1323 __bpf_prog_release(prog);
1324 return err;
1325 }
1326
1327 return 0;
1328}
1329
1330static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1331{
89aa0758 1332 if (sock_flag(sk, SOCK_FILTER_LOCKED))
538950a1 1333 return ERR_PTR(-EPERM);
89aa0758 1334
113214be 1335 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
538950a1
CG
1336}
1337
1338int sk_attach_bpf(u32 ufd, struct sock *sk)
1339{
1340 struct bpf_prog *prog = __get_bpf(ufd, sk);
1341 int err;
1342
1343 if (IS_ERR(prog))
1344 return PTR_ERR(prog);
1345
8ced425e 1346 err = __sk_attach_prog(prog, sk);
49b31e57 1347 if (err < 0) {
89aa0758 1348 bpf_prog_put(prog);
49b31e57 1349 return err;
89aa0758
AS
1350 }
1351
89aa0758
AS
1352 return 0;
1353}
1354
538950a1
CG
1355int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1356{
1357 struct bpf_prog *prog = __get_bpf(ufd, sk);
1358 int err;
1359
1360 if (IS_ERR(prog))
1361 return PTR_ERR(prog);
1362
1363 err = __reuseport_attach_prog(prog, sk);
1364 if (err < 0) {
1365 bpf_prog_put(prog);
1366 return err;
1367 }
1368
1369 return 0;
1370}
1371
21cafc1d
DB
1372struct bpf_scratchpad {
1373 union {
1374 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1375 u8 buff[MAX_BPF_STACK];
1376 };
1377};
1378
1379static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
91bc4822 1380
5293efe6
DB
1381static inline int __bpf_try_make_writable(struct sk_buff *skb,
1382 unsigned int write_len)
1383{
1384 return skb_ensure_writable(skb, write_len);
1385}
1386
db58ba45
AS
1387static inline int bpf_try_make_writable(struct sk_buff *skb,
1388 unsigned int write_len)
1389{
5293efe6 1390 int err = __bpf_try_make_writable(skb, write_len);
db58ba45 1391
0ed661d5 1392 bpf_compute_data_end(skb);
db58ba45
AS
1393 return err;
1394}
1395
36bbef52
DB
1396static int bpf_try_make_head_writable(struct sk_buff *skb)
1397{
1398 return bpf_try_make_writable(skb, skb_headlen(skb));
1399}
1400
a2bfe6bf
DB
1401static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1402{
1403 if (skb_at_tc_ingress(skb))
1404 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1405}
1406
8065694e
DB
1407static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1408{
1409 if (skb_at_tc_ingress(skb))
1410 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1411}
1412
f3694e00
DB
1413BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1414 const void *, from, u32, len, u64, flags)
608cd71a 1415{
608cd71a
AS
1416 void *ptr;
1417
8afd54c8 1418 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
781c53bc 1419 return -EINVAL;
0ed661d5 1420 if (unlikely(offset > 0xffff))
608cd71a 1421 return -EFAULT;
db58ba45 1422 if (unlikely(bpf_try_make_writable(skb, offset + len)))
608cd71a
AS
1423 return -EFAULT;
1424
0ed661d5 1425 ptr = skb->data + offset;
781c53bc 1426 if (flags & BPF_F_RECOMPUTE_CSUM)
479ffccc 1427 __skb_postpull_rcsum(skb, ptr, len, offset);
608cd71a
AS
1428
1429 memcpy(ptr, from, len);
1430
781c53bc 1431 if (flags & BPF_F_RECOMPUTE_CSUM)
479ffccc 1432 __skb_postpush_rcsum(skb, ptr, len, offset);
8afd54c8
DB
1433 if (flags & BPF_F_INVALIDATE_HASH)
1434 skb_clear_hash(skb);
f8ffad69 1435
608cd71a
AS
1436 return 0;
1437}
1438
577c50aa 1439static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
608cd71a
AS
1440 .func = bpf_skb_store_bytes,
1441 .gpl_only = false,
1442 .ret_type = RET_INTEGER,
1443 .arg1_type = ARG_PTR_TO_CTX,
1444 .arg2_type = ARG_ANYTHING,
39f19ebb
AS
1445 .arg3_type = ARG_PTR_TO_MEM,
1446 .arg4_type = ARG_CONST_SIZE,
91bc4822
AS
1447 .arg5_type = ARG_ANYTHING,
1448};
1449
f3694e00
DB
1450BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1451 void *, to, u32, len)
05c74e5e 1452{
05c74e5e
DB
1453 void *ptr;
1454
0ed661d5 1455 if (unlikely(offset > 0xffff))
074f528e 1456 goto err_clear;
05c74e5e
DB
1457
1458 ptr = skb_header_pointer(skb, offset, len, to);
1459 if (unlikely(!ptr))
074f528e 1460 goto err_clear;
05c74e5e
DB
1461 if (ptr != to)
1462 memcpy(to, ptr, len);
1463
1464 return 0;
074f528e
DB
1465err_clear:
1466 memset(to, 0, len);
1467 return -EFAULT;
05c74e5e
DB
1468}
1469
577c50aa 1470static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
05c74e5e
DB
1471 .func = bpf_skb_load_bytes,
1472 .gpl_only = false,
1473 .ret_type = RET_INTEGER,
1474 .arg1_type = ARG_PTR_TO_CTX,
1475 .arg2_type = ARG_ANYTHING,
39f19ebb
AS
1476 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1477 .arg4_type = ARG_CONST_SIZE,
05c74e5e
DB
1478};
1479
36bbef52
DB
1480BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1481{
1482 /* Idea is the following: should the needed direct read/write
1483 * test fail during runtime, we can pull in more data and redo
1484 * again, since implicitly, we invalidate previous checks here.
1485 *
1486 * Or, since we know how much we need to make read/writeable,
1487 * this can be done once at the program beginning for direct
1488 * access case. By this we overcome limitations of only current
1489 * headroom being accessible.
1490 */
1491 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1492}
1493
1494static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1495 .func = bpf_skb_pull_data,
1496 .gpl_only = false,
1497 .ret_type = RET_INTEGER,
1498 .arg1_type = ARG_PTR_TO_CTX,
1499 .arg2_type = ARG_ANYTHING,
1500};
1501
f3694e00
DB
1502BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1503 u64, from, u64, to, u64, flags)
91bc4822 1504{
0ed661d5 1505 __sum16 *ptr;
91bc4822 1506
781c53bc
DB
1507 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
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);
781c53bc 1515 switch (flags & BPF_F_HDR_FIELD_MASK) {
8050c0f0
DB
1516 case 0:
1517 if (unlikely(from != 0))
1518 return -EINVAL;
1519
1520 csum_replace_by_diff(ptr, to);
1521 break;
91bc4822
AS
1522 case 2:
1523 csum_replace2(ptr, from, to);
1524 break;
1525 case 4:
1526 csum_replace4(ptr, from, to);
1527 break;
1528 default:
1529 return -EINVAL;
1530 }
1531
91bc4822
AS
1532 return 0;
1533}
1534
577c50aa 1535static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
91bc4822
AS
1536 .func = bpf_l3_csum_replace,
1537 .gpl_only = false,
1538 .ret_type = RET_INTEGER,
1539 .arg1_type = ARG_PTR_TO_CTX,
1540 .arg2_type = ARG_ANYTHING,
1541 .arg3_type = ARG_ANYTHING,
1542 .arg4_type = ARG_ANYTHING,
1543 .arg5_type = ARG_ANYTHING,
1544};
1545
f3694e00
DB
1546BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1547 u64, from, u64, to, u64, flags)
91bc4822 1548{
781c53bc 1549 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
2f72959a 1550 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
d1b662ad 1551 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
0ed661d5 1552 __sum16 *ptr;
91bc4822 1553
d1b662ad
DB
1554 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1555 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
781c53bc 1556 return -EINVAL;
0ed661d5 1557 if (unlikely(offset > 0xffff || offset & 1))
91bc4822 1558 return -EFAULT;
0ed661d5 1559 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
91bc4822
AS
1560 return -EFAULT;
1561
0ed661d5 1562 ptr = (__sum16 *)(skb->data + offset);
d1b662ad 1563 if (is_mmzero && !do_mforce && !*ptr)
2f72959a 1564 return 0;
91bc4822 1565
781c53bc 1566 switch (flags & BPF_F_HDR_FIELD_MASK) {
7d672345
DB
1567 case 0:
1568 if (unlikely(from != 0))
1569 return -EINVAL;
1570
1571 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1572 break;
91bc4822
AS
1573 case 2:
1574 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1575 break;
1576 case 4:
1577 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1578 break;
1579 default:
1580 return -EINVAL;
1581 }
1582
2f72959a
DB
1583 if (is_mmzero && !*ptr)
1584 *ptr = CSUM_MANGLED_0;
91bc4822
AS
1585 return 0;
1586}
1587
577c50aa 1588static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
91bc4822
AS
1589 .func = bpf_l4_csum_replace,
1590 .gpl_only = false,
1591 .ret_type = RET_INTEGER,
1592 .arg1_type = ARG_PTR_TO_CTX,
1593 .arg2_type = ARG_ANYTHING,
1594 .arg3_type = ARG_ANYTHING,
1595 .arg4_type = ARG_ANYTHING,
1596 .arg5_type = ARG_ANYTHING,
608cd71a
AS
1597};
1598
f3694e00
DB
1599BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1600 __be32 *, to, u32, to_size, __wsum, seed)
7d672345 1601{
21cafc1d 1602 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
f3694e00 1603 u32 diff_size = from_size + to_size;
7d672345
DB
1604 int i, j = 0;
1605
1606 /* This is quite flexible, some examples:
1607 *
1608 * from_size == 0, to_size > 0, seed := csum --> pushing data
1609 * from_size > 0, to_size == 0, seed := csum --> pulling data
1610 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1611 *
1612 * Even for diffing, from_size and to_size don't need to be equal.
1613 */
1614 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1615 diff_size > sizeof(sp->diff)))
1616 return -EINVAL;
1617
1618 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1619 sp->diff[j] = ~from[i];
1620 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1621 sp->diff[j] = to[i];
1622
1623 return csum_partial(sp->diff, diff_size, seed);
1624}
1625
577c50aa 1626static const struct bpf_func_proto bpf_csum_diff_proto = {
7d672345
DB
1627 .func = bpf_csum_diff,
1628 .gpl_only = false,
36bbef52 1629 .pkt_access = true,
7d672345 1630 .ret_type = RET_INTEGER,
39f19ebb
AS
1631 .arg1_type = ARG_PTR_TO_MEM,
1632 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1633 .arg3_type = ARG_PTR_TO_MEM,
1634 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
7d672345
DB
1635 .arg5_type = ARG_ANYTHING,
1636};
1637
36bbef52
DB
1638BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1639{
1640 /* The interface is to be used in combination with bpf_csum_diff()
1641 * for direct packet writes. csum rotation for alignment as well
1642 * as emulating csum_sub() can be done from the eBPF program.
1643 */
1644 if (skb->ip_summed == CHECKSUM_COMPLETE)
1645 return (skb->csum = csum_add(skb->csum, csum));
1646
1647 return -ENOTSUPP;
1648}
1649
1650static const struct bpf_func_proto bpf_csum_update_proto = {
1651 .func = bpf_csum_update,
1652 .gpl_only = false,
1653 .ret_type = RET_INTEGER,
1654 .arg1_type = ARG_PTR_TO_CTX,
1655 .arg2_type = ARG_ANYTHING,
1656};
1657
a70b506e
DB
1658static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1659{
a70b506e
DB
1660 return dev_forward_skb(dev, skb);
1661}
1662
4e3264d2
MKL
1663static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1664 struct sk_buff *skb)
1665{
1666 int ret = ____dev_forward_skb(dev, skb);
1667
1668 if (likely(!ret)) {
1669 skb->dev = dev;
1670 ret = netif_rx(skb);
1671 }
1672
1673 return ret;
1674}
1675
a70b506e
DB
1676static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1677{
1678 int ret;
1679
1680 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1681 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1682 kfree_skb(skb);
1683 return -ENETDOWN;
1684 }
1685
1686 skb->dev = dev;
1687
1688 __this_cpu_inc(xmit_recursion);
1689 ret = dev_queue_xmit(skb);
1690 __this_cpu_dec(xmit_recursion);
1691
1692 return ret;
1693}
1694
4e3264d2
MKL
1695static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1696 u32 flags)
1697{
1698 /* skb->mac_len is not set on normal egress */
1699 unsigned int mlen = skb->network_header - skb->mac_header;
1700
1701 __skb_pull(skb, mlen);
1702
1703 /* At ingress, the mac header has already been pulled once.
1704 * At egress, skb_pospull_rcsum has to be done in case that
1705 * the skb is originated from ingress (i.e. a forwarded skb)
1706 * to ensure that rcsum starts at net header.
1707 */
1708 if (!skb_at_tc_ingress(skb))
1709 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1710 skb_pop_mac_header(skb);
1711 skb_reset_mac_len(skb);
1712 return flags & BPF_F_INGRESS ?
1713 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1714}
1715
1716static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1717 u32 flags)
1718{
3a0af8fd
TG
1719 /* Verify that a link layer header is carried */
1720 if (unlikely(skb->mac_header >= skb->network_header)) {
1721 kfree_skb(skb);
1722 return -ERANGE;
1723 }
1724
4e3264d2
MKL
1725 bpf_push_mac_rcsum(skb);
1726 return flags & BPF_F_INGRESS ?
1727 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1728}
1729
1730static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1731 u32 flags)
1732{
c491680f 1733 if (dev_is_mac_header_xmit(dev))
4e3264d2 1734 return __bpf_redirect_common(skb, dev, flags);
c491680f
DB
1735 else
1736 return __bpf_redirect_no_mac(skb, dev, flags);
4e3264d2
MKL
1737}
1738
f3694e00 1739BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
3896d655 1740{
3896d655 1741 struct net_device *dev;
36bbef52
DB
1742 struct sk_buff *clone;
1743 int ret;
3896d655 1744
781c53bc
DB
1745 if (unlikely(flags & ~(BPF_F_INGRESS)))
1746 return -EINVAL;
1747
3896d655
AS
1748 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1749 if (unlikely(!dev))
1750 return -EINVAL;
1751
36bbef52
DB
1752 clone = skb_clone(skb, GFP_ATOMIC);
1753 if (unlikely(!clone))
3896d655
AS
1754 return -ENOMEM;
1755
36bbef52
DB
1756 /* For direct write, we need to keep the invariant that the skbs
1757 * we're dealing with need to be uncloned. Should uncloning fail
1758 * here, we need to free the just generated clone to unclone once
1759 * again.
1760 */
1761 ret = bpf_try_make_head_writable(skb);
1762 if (unlikely(ret)) {
1763 kfree_skb(clone);
1764 return -ENOMEM;
1765 }
1766
4e3264d2 1767 return __bpf_redirect(clone, dev, flags);
3896d655
AS
1768}
1769
577c50aa 1770static const struct bpf_func_proto bpf_clone_redirect_proto = {
3896d655
AS
1771 .func = bpf_clone_redirect,
1772 .gpl_only = false,
1773 .ret_type = RET_INTEGER,
1774 .arg1_type = ARG_PTR_TO_CTX,
1775 .arg2_type = ARG_ANYTHING,
1776 .arg3_type = ARG_ANYTHING,
1777};
1778
27b29f63
AS
1779struct redirect_info {
1780 u32 ifindex;
1781 u32 flags;
1782};
1783
1784static DEFINE_PER_CPU(struct redirect_info, redirect_info);
781c53bc 1785
f3694e00 1786BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
27b29f63
AS
1787{
1788 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1789
781c53bc
DB
1790 if (unlikely(flags & ~(BPF_F_INGRESS)))
1791 return TC_ACT_SHOT;
1792
27b29f63
AS
1793 ri->ifindex = ifindex;
1794 ri->flags = flags;
781c53bc 1795
27b29f63
AS
1796 return TC_ACT_REDIRECT;
1797}
1798
1799int skb_do_redirect(struct sk_buff *skb)
1800{
1801 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1802 struct net_device *dev;
1803
1804 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1805 ri->ifindex = 0;
1806 if (unlikely(!dev)) {
1807 kfree_skb(skb);
1808 return -EINVAL;
1809 }
1810
4e3264d2 1811 return __bpf_redirect(skb, dev, ri->flags);
27b29f63
AS
1812}
1813
577c50aa 1814static const struct bpf_func_proto bpf_redirect_proto = {
27b29f63
AS
1815 .func = bpf_redirect,
1816 .gpl_only = false,
1817 .ret_type = RET_INTEGER,
1818 .arg1_type = ARG_ANYTHING,
1819 .arg2_type = ARG_ANYTHING,
1820};
1821
f3694e00 1822BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
8d20aabe 1823{
f3694e00 1824 return task_get_classid(skb);
8d20aabe
DB
1825}
1826
1827static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1828 .func = bpf_get_cgroup_classid,
1829 .gpl_only = false,
1830 .ret_type = RET_INTEGER,
1831 .arg1_type = ARG_PTR_TO_CTX,
1832};
1833
f3694e00 1834BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
c46646d0 1835{
f3694e00 1836 return dst_tclassid(skb);
c46646d0
DB
1837}
1838
1839static const struct bpf_func_proto bpf_get_route_realm_proto = {
1840 .func = bpf_get_route_realm,
1841 .gpl_only = false,
1842 .ret_type = RET_INTEGER,
1843 .arg1_type = ARG_PTR_TO_CTX,
1844};
1845
f3694e00 1846BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
13c5c240
DB
1847{
1848 /* If skb_clear_hash() was called due to mangling, we can
1849 * trigger SW recalculation here. Later access to hash
1850 * can then use the inline skb->hash via context directly
1851 * instead of calling this helper again.
1852 */
f3694e00 1853 return skb_get_hash(skb);
13c5c240
DB
1854}
1855
1856static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1857 .func = bpf_get_hash_recalc,
1858 .gpl_only = false,
1859 .ret_type = RET_INTEGER,
1860 .arg1_type = ARG_PTR_TO_CTX,
1861};
1862
7a4b28c6
DB
1863BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1864{
1865 /* After all direct packet write, this can be used once for
1866 * triggering a lazy recalc on next skb_get_hash() invocation.
1867 */
1868 skb_clear_hash(skb);
1869 return 0;
1870}
1871
1872static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1873 .func = bpf_set_hash_invalid,
1874 .gpl_only = false,
1875 .ret_type = RET_INTEGER,
1876 .arg1_type = ARG_PTR_TO_CTX,
1877};
1878
ded092cd
DB
1879BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
1880{
1881 /* Set user specified hash as L4(+), so that it gets returned
1882 * on skb_get_hash() call unless BPF prog later on triggers a
1883 * skb_clear_hash().
1884 */
1885 __skb_set_sw_hash(skb, hash, true);
1886 return 0;
1887}
1888
1889static const struct bpf_func_proto bpf_set_hash_proto = {
1890 .func = bpf_set_hash,
1891 .gpl_only = false,
1892 .ret_type = RET_INTEGER,
1893 .arg1_type = ARG_PTR_TO_CTX,
1894 .arg2_type = ARG_ANYTHING,
1895};
1896
f3694e00
DB
1897BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1898 u16, vlan_tci)
4e10df9a 1899{
db58ba45 1900 int ret;
4e10df9a
AS
1901
1902 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1903 vlan_proto != htons(ETH_P_8021AD)))
1904 vlan_proto = htons(ETH_P_8021Q);
1905
8065694e 1906 bpf_push_mac_rcsum(skb);
db58ba45 1907 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
8065694e
DB
1908 bpf_pull_mac_rcsum(skb);
1909
db58ba45
AS
1910 bpf_compute_data_end(skb);
1911 return ret;
4e10df9a
AS
1912}
1913
1914const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1915 .func = bpf_skb_vlan_push,
1916 .gpl_only = false,
1917 .ret_type = RET_INTEGER,
1918 .arg1_type = ARG_PTR_TO_CTX,
1919 .arg2_type = ARG_ANYTHING,
1920 .arg3_type = ARG_ANYTHING,
1921};
4d9c5c53 1922EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
4e10df9a 1923
f3694e00 1924BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
4e10df9a 1925{
db58ba45 1926 int ret;
4e10df9a 1927
8065694e 1928 bpf_push_mac_rcsum(skb);
db58ba45 1929 ret = skb_vlan_pop(skb);
8065694e
DB
1930 bpf_pull_mac_rcsum(skb);
1931
db58ba45
AS
1932 bpf_compute_data_end(skb);
1933 return ret;
4e10df9a
AS
1934}
1935
1936const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1937 .func = bpf_skb_vlan_pop,
1938 .gpl_only = false,
1939 .ret_type = RET_INTEGER,
1940 .arg1_type = ARG_PTR_TO_CTX,
1941};
4d9c5c53 1942EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
4e10df9a 1943
6578171a
DB
1944static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1945{
1946 /* Caller already did skb_cow() with len as headroom,
1947 * so no need to do it here.
1948 */
1949 skb_push(skb, len);
1950 memmove(skb->data, skb->data + len, off);
1951 memset(skb->data + off, 0, len);
1952
1953 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1954 * needed here as it does not change the skb->csum
1955 * result for checksum complete when summing over
1956 * zeroed blocks.
1957 */
1958 return 0;
1959}
1960
1961static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1962{
1963 /* skb_ensure_writable() is not needed here, as we're
1964 * already working on an uncloned skb.
1965 */
1966 if (unlikely(!pskb_may_pull(skb, off + len)))
1967 return -ENOMEM;
1968
1969 skb_postpull_rcsum(skb, skb->data + off, len);
1970 memmove(skb->data + len, skb->data, off);
1971 __skb_pull(skb, len);
1972
1973 return 0;
1974}
1975
1976static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1977{
1978 bool trans_same = skb->transport_header == skb->network_header;
1979 int ret;
1980
1981 /* There's no need for __skb_push()/__skb_pull() pair to
1982 * get to the start of the mac header as we're guaranteed
1983 * to always start from here under eBPF.
1984 */
1985 ret = bpf_skb_generic_push(skb, off, len);
1986 if (likely(!ret)) {
1987 skb->mac_header -= len;
1988 skb->network_header -= len;
1989 if (trans_same)
1990 skb->transport_header = skb->network_header;
1991 }
1992
1993 return ret;
1994}
1995
1996static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1997{
1998 bool trans_same = skb->transport_header == skb->network_header;
1999 int ret;
2000
2001 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2002 ret = bpf_skb_generic_pop(skb, off, len);
2003 if (likely(!ret)) {
2004 skb->mac_header += len;
2005 skb->network_header += len;
2006 if (trans_same)
2007 skb->transport_header = skb->network_header;
2008 }
2009
2010 return ret;
2011}
2012
2013static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2014{
2015 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
0daf4349 2016 u32 off = skb_mac_header_len(skb);
6578171a
DB
2017 int ret;
2018
2019 ret = skb_cow(skb, len_diff);
2020 if (unlikely(ret < 0))
2021 return ret;
2022
2023 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2024 if (unlikely(ret < 0))
2025 return ret;
2026
2027 if (skb_is_gso(skb)) {
2028 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
2029 * be changed into SKB_GSO_TCPV6.
2030 */
2031 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2032 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
2033 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
2034 }
2035
2036 /* Due to IPv6 header, MSS needs to be downgraded. */
2037 skb_shinfo(skb)->gso_size -= len_diff;
2038 /* Header must be checked, and gso_segs recomputed. */
2039 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2040 skb_shinfo(skb)->gso_segs = 0;
2041 }
2042
2043 skb->protocol = htons(ETH_P_IPV6);
2044 skb_clear_hash(skb);
2045
2046 return 0;
2047}
2048
2049static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2050{
2051 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
0daf4349 2052 u32 off = skb_mac_header_len(skb);
6578171a
DB
2053 int ret;
2054
2055 ret = skb_unclone(skb, GFP_ATOMIC);
2056 if (unlikely(ret < 0))
2057 return ret;
2058
2059 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2060 if (unlikely(ret < 0))
2061 return ret;
2062
2063 if (skb_is_gso(skb)) {
2064 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
2065 * be changed into SKB_GSO_TCPV4.
2066 */
2067 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2068 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2069 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2070 }
2071
2072 /* Due to IPv4 header, MSS can be upgraded. */
2073 skb_shinfo(skb)->gso_size += len_diff;
2074 /* Header must be checked, and gso_segs recomputed. */
2075 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2076 skb_shinfo(skb)->gso_segs = 0;
2077 }
2078
2079 skb->protocol = htons(ETH_P_IP);
2080 skb_clear_hash(skb);
2081
2082 return 0;
2083}
2084
2085static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2086{
2087 __be16 from_proto = skb->protocol;
2088
2089 if (from_proto == htons(ETH_P_IP) &&
2090 to_proto == htons(ETH_P_IPV6))
2091 return bpf_skb_proto_4_to_6(skb);
2092
2093 if (from_proto == htons(ETH_P_IPV6) &&
2094 to_proto == htons(ETH_P_IP))
2095 return bpf_skb_proto_6_to_4(skb);
2096
2097 return -ENOTSUPP;
2098}
2099
f3694e00
DB
2100BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2101 u64, flags)
6578171a 2102{
6578171a
DB
2103 int ret;
2104
2105 if (unlikely(flags))
2106 return -EINVAL;
2107
2108 /* General idea is that this helper does the basic groundwork
2109 * needed for changing the protocol, and eBPF program fills the
2110 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2111 * and other helpers, rather than passing a raw buffer here.
2112 *
2113 * The rationale is to keep this minimal and without a need to
2114 * deal with raw packet data. F.e. even if we would pass buffers
2115 * here, the program still needs to call the bpf_lX_csum_replace()
2116 * helpers anyway. Plus, this way we keep also separation of
2117 * concerns, since f.e. bpf_skb_store_bytes() should only take
2118 * care of stores.
2119 *
2120 * Currently, additional options and extension header space are
2121 * not supported, but flags register is reserved so we can adapt
2122 * that. For offloads, we mark packet as dodgy, so that headers
2123 * need to be verified first.
2124 */
2125 ret = bpf_skb_proto_xlat(skb, proto);
2126 bpf_compute_data_end(skb);
2127 return ret;
2128}
2129
2130static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2131 .func = bpf_skb_change_proto,
2132 .gpl_only = false,
2133 .ret_type = RET_INTEGER,
2134 .arg1_type = ARG_PTR_TO_CTX,
2135 .arg2_type = ARG_ANYTHING,
2136 .arg3_type = ARG_ANYTHING,
2137};
2138
f3694e00 2139BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
d2485c42 2140{
d2485c42 2141 /* We only allow a restricted subset to be changed for now. */
45c7fffa
DB
2142 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2143 !skb_pkt_type_ok(pkt_type)))
d2485c42
DB
2144 return -EINVAL;
2145
2146 skb->pkt_type = pkt_type;
2147 return 0;
2148}
2149
2150static const struct bpf_func_proto bpf_skb_change_type_proto = {
2151 .func = bpf_skb_change_type,
2152 .gpl_only = false,
2153 .ret_type = RET_INTEGER,
2154 .arg1_type = ARG_PTR_TO_CTX,
2155 .arg2_type = ARG_ANYTHING,
2156};
2157
2be7e212
DB
2158static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2159{
2160 switch (skb->protocol) {
2161 case htons(ETH_P_IP):
2162 return sizeof(struct iphdr);
2163 case htons(ETH_P_IPV6):
2164 return sizeof(struct ipv6hdr);
2165 default:
2166 return ~0U;
2167 }
2168}
2169
2170static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2171{
2172 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2173 int ret;
2174
2175 ret = skb_cow(skb, len_diff);
2176 if (unlikely(ret < 0))
2177 return ret;
2178
2179 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2180 if (unlikely(ret < 0))
2181 return ret;
2182
2183 if (skb_is_gso(skb)) {
2184 /* Due to header grow, MSS needs to be downgraded. */
2185 skb_shinfo(skb)->gso_size -= len_diff;
2186 /* Header must be checked, and gso_segs recomputed. */
2187 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2188 skb_shinfo(skb)->gso_segs = 0;
2189 }
2190
2191 return 0;
2192}
2193
2194static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2195{
2196 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2197 int ret;
2198
2199 ret = skb_unclone(skb, GFP_ATOMIC);
2200 if (unlikely(ret < 0))
2201 return ret;
2202
2203 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2204 if (unlikely(ret < 0))
2205 return ret;
2206
2207 if (skb_is_gso(skb)) {
2208 /* Due to header shrink, MSS can be upgraded. */
2209 skb_shinfo(skb)->gso_size += len_diff;
2210 /* Header must be checked, and gso_segs recomputed. */
2211 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2212 skb_shinfo(skb)->gso_segs = 0;
2213 }
2214
2215 return 0;
2216}
2217
2218static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2219{
2220 return skb->dev->mtu + skb->dev->hard_header_len;
2221}
2222
2223static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2224{
2225 bool trans_same = skb->transport_header == skb->network_header;
2226 u32 len_cur, len_diff_abs = abs(len_diff);
2227 u32 len_min = bpf_skb_net_base_len(skb);
2228 u32 len_max = __bpf_skb_max_len(skb);
2229 __be16 proto = skb->protocol;
2230 bool shrink = len_diff < 0;
2231 int ret;
2232
2233 if (unlikely(len_diff_abs > 0xfffU))
2234 return -EFAULT;
2235 if (unlikely(proto != htons(ETH_P_IP) &&
2236 proto != htons(ETH_P_IPV6)))
2237 return -ENOTSUPP;
2238
2239 len_cur = skb->len - skb_network_offset(skb);
2240 if (skb_transport_header_was_set(skb) && !trans_same)
2241 len_cur = skb_network_header_len(skb);
2242 if ((shrink && (len_diff_abs >= len_cur ||
2243 len_cur - len_diff_abs < len_min)) ||
2244 (!shrink && (skb->len + len_diff_abs > len_max &&
2245 !skb_is_gso(skb))))
2246 return -ENOTSUPP;
2247
2248 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2249 bpf_skb_net_grow(skb, len_diff_abs);
2250
2251 bpf_compute_data_end(skb);
2252 return 0;
2253}
2254
2255BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2256 u32, mode, u64, flags)
2257{
2258 if (unlikely(flags))
2259 return -EINVAL;
2260 if (likely(mode == BPF_ADJ_ROOM_NET))
2261 return bpf_skb_adjust_net(skb, len_diff);
2262
2263 return -ENOTSUPP;
2264}
2265
2266static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2267 .func = bpf_skb_adjust_room,
2268 .gpl_only = false,
2269 .ret_type = RET_INTEGER,
2270 .arg1_type = ARG_PTR_TO_CTX,
2271 .arg2_type = ARG_ANYTHING,
2272 .arg3_type = ARG_ANYTHING,
2273 .arg4_type = ARG_ANYTHING,
2274};
2275
5293efe6
DB
2276static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2277{
2278 u32 min_len = skb_network_offset(skb);
2279
2280 if (skb_transport_header_was_set(skb))
2281 min_len = skb_transport_offset(skb);
2282 if (skb->ip_summed == CHECKSUM_PARTIAL)
2283 min_len = skb_checksum_start_offset(skb) +
2284 skb->csum_offset + sizeof(__sum16);
2285 return min_len;
2286}
2287
5293efe6
DB
2288static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2289{
2290 unsigned int old_len = skb->len;
2291 int ret;
2292
2293 ret = __skb_grow_rcsum(skb, new_len);
2294 if (!ret)
2295 memset(skb->data + old_len, 0, new_len - old_len);
2296 return ret;
2297}
2298
2299static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2300{
2301 return __skb_trim_rcsum(skb, new_len);
2302}
2303
f3694e00
DB
2304BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2305 u64, flags)
5293efe6 2306{
5293efe6
DB
2307 u32 max_len = __bpf_skb_max_len(skb);
2308 u32 min_len = __bpf_skb_min_len(skb);
5293efe6
DB
2309 int ret;
2310
2311 if (unlikely(flags || new_len > max_len || new_len < min_len))
2312 return -EINVAL;
2313 if (skb->encapsulation)
2314 return -ENOTSUPP;
2315
2316 /* The basic idea of this helper is that it's performing the
2317 * needed work to either grow or trim an skb, and eBPF program
2318 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2319 * bpf_lX_csum_replace() and others rather than passing a raw
2320 * buffer here. This one is a slow path helper and intended
2321 * for replies with control messages.
2322 *
2323 * Like in bpf_skb_change_proto(), we want to keep this rather
2324 * minimal and without protocol specifics so that we are able
2325 * to separate concerns as in bpf_skb_store_bytes() should only
2326 * be the one responsible for writing buffers.
2327 *
2328 * It's really expected to be a slow path operation here for
2329 * control message replies, so we're implicitly linearizing,
2330 * uncloning and drop offloads from the skb by this.
2331 */
2332 ret = __bpf_try_make_writable(skb, skb->len);
2333 if (!ret) {
2334 if (new_len > skb->len)
2335 ret = bpf_skb_grow_rcsum(skb, new_len);
2336 else if (new_len < skb->len)
2337 ret = bpf_skb_trim_rcsum(skb, new_len);
2338 if (!ret && skb_is_gso(skb))
2339 skb_gso_reset(skb);
2340 }
2341
2342 bpf_compute_data_end(skb);
2343 return ret;
2344}
2345
2346static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2347 .func = bpf_skb_change_tail,
2348 .gpl_only = false,
2349 .ret_type = RET_INTEGER,
2350 .arg1_type = ARG_PTR_TO_CTX,
2351 .arg2_type = ARG_ANYTHING,
2352 .arg3_type = ARG_ANYTHING,
2353};
2354
3a0af8fd
TG
2355BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2356 u64, flags)
2357{
2358 u32 max_len = __bpf_skb_max_len(skb);
2359 u32 new_len = skb->len + head_room;
2360 int ret;
2361
2362 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2363 new_len < skb->len))
2364 return -EINVAL;
2365
2366 ret = skb_cow(skb, head_room);
2367 if (likely(!ret)) {
2368 /* Idea for this helper is that we currently only
2369 * allow to expand on mac header. This means that
2370 * skb->protocol network header, etc, stay as is.
2371 * Compared to bpf_skb_change_tail(), we're more
2372 * flexible due to not needing to linearize or
2373 * reset GSO. Intention for this helper is to be
2374 * used by an L3 skb that needs to push mac header
2375 * for redirection into L2 device.
2376 */
2377 __skb_push(skb, head_room);
2378 memset(skb->data, 0, head_room);
2379 skb_reset_mac_header(skb);
2380 }
2381
2382 bpf_compute_data_end(skb);
2383 return 0;
2384}
2385
2386static const struct bpf_func_proto bpf_skb_change_head_proto = {
2387 .func = bpf_skb_change_head,
2388 .gpl_only = false,
2389 .ret_type = RET_INTEGER,
2390 .arg1_type = ARG_PTR_TO_CTX,
2391 .arg2_type = ARG_ANYTHING,
2392 .arg3_type = ARG_ANYTHING,
2393};
2394
17bedab2
MKL
2395BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2396{
2397 void *data = xdp->data + offset;
2398
2399 if (unlikely(data < xdp->data_hard_start ||
2400 data > xdp->data_end - ETH_HLEN))
2401 return -EINVAL;
2402
2403 xdp->data = data;
2404
2405 return 0;
2406}
2407
2408static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2409 .func = bpf_xdp_adjust_head,
2410 .gpl_only = false,
2411 .ret_type = RET_INTEGER,
2412 .arg1_type = ARG_PTR_TO_CTX,
2413 .arg2_type = ARG_ANYTHING,
2414};
2415
814abfab
JF
2416static int __bpf_tx_xdp(struct net_device *dev, struct xdp_buff *xdp)
2417{
2418 if (dev->netdev_ops->ndo_xdp_xmit) {
2419 dev->netdev_ops->ndo_xdp_xmit(dev, xdp);
2420 return 0;
2421 }
2422 bpf_warn_invalid_xdp_redirect(dev->ifindex);
2423 return -EOPNOTSUPP;
2424}
2425
5acaee0a
JF
2426int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
2427 struct bpf_prog *xdp_prog)
814abfab
JF
2428{
2429 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
5acaee0a 2430 struct net_device *fwd;
814abfab 2431
5acaee0a 2432 fwd = dev_get_by_index_rcu(dev_net(dev), ri->ifindex);
814abfab 2433 ri->ifindex = 0;
5acaee0a 2434 if (unlikely(!fwd)) {
814abfab
JF
2435 bpf_warn_invalid_xdp_redirect(ri->ifindex);
2436 return -EINVAL;
2437 }
2438
5acaee0a
JF
2439 trace_xdp_redirect(dev, fwd, xdp_prog, XDP_REDIRECT);
2440
2441 return __bpf_tx_xdp(fwd, xdp);
814abfab
JF
2442}
2443EXPORT_SYMBOL_GPL(xdp_do_redirect);
2444
6103aa96
JF
2445int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb)
2446{
2447 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2448 unsigned int len;
2449
2450 dev = dev_get_by_index_rcu(dev_net(dev), ri->ifindex);
2451 ri->ifindex = 0;
2452 if (unlikely(!dev)) {
2453 bpf_warn_invalid_xdp_redirect(ri->ifindex);
2454 goto err;
2455 }
2456
2457 if (unlikely(!(dev->flags & IFF_UP)))
2458 goto err;
2459
2460 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
2461 if (skb->len > len)
2462 goto err;
2463
2464 skb->dev = dev;
2465 return 0;
2466err:
2467 return -EINVAL;
2468}
2469EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
2470
814abfab
JF
2471BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
2472{
2473 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2474
2475 if (unlikely(flags))
2476 return XDP_ABORTED;
2477
2478 ri->ifindex = ifindex;
2479 ri->flags = flags;
2480 return XDP_REDIRECT;
2481}
2482
2483static const struct bpf_func_proto bpf_xdp_redirect_proto = {
2484 .func = bpf_xdp_redirect,
2485 .gpl_only = false,
2486 .ret_type = RET_INTEGER,
2487 .arg1_type = ARG_ANYTHING,
2488 .arg2_type = ARG_ANYTHING,
2489};
2490
17bedab2 2491bool bpf_helper_changes_pkt_data(void *func)
4e10df9a 2492{
36bbef52
DB
2493 if (func == bpf_skb_vlan_push ||
2494 func == bpf_skb_vlan_pop ||
2495 func == bpf_skb_store_bytes ||
2496 func == bpf_skb_change_proto ||
3a0af8fd 2497 func == bpf_skb_change_head ||
36bbef52 2498 func == bpf_skb_change_tail ||
2be7e212 2499 func == bpf_skb_adjust_room ||
36bbef52 2500 func == bpf_skb_pull_data ||
41703a73 2501 func == bpf_clone_redirect ||
36bbef52 2502 func == bpf_l3_csum_replace ||
17bedab2
MKL
2503 func == bpf_l4_csum_replace ||
2504 func == bpf_xdp_adjust_head)
3697649f
DB
2505 return true;
2506
4e10df9a
AS
2507 return false;
2508}
2509
555c8a86 2510static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
aa7145c1 2511 unsigned long off, unsigned long len)
555c8a86 2512{
aa7145c1 2513 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
555c8a86
DB
2514
2515 if (unlikely(!ptr))
2516 return len;
2517 if (ptr != dst_buff)
2518 memcpy(dst_buff, ptr, len);
2519
2520 return 0;
2521}
2522
f3694e00
DB
2523BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2524 u64, flags, void *, meta, u64, meta_size)
555c8a86 2525{
555c8a86 2526 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
555c8a86
DB
2527
2528 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2529 return -EINVAL;
2530 if (unlikely(skb_size > skb->len))
2531 return -EFAULT;
2532
2533 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2534 bpf_skb_copy);
2535}
2536
2537static const struct bpf_func_proto bpf_skb_event_output_proto = {
2538 .func = bpf_skb_event_output,
2539 .gpl_only = true,
2540 .ret_type = RET_INTEGER,
2541 .arg1_type = ARG_PTR_TO_CTX,
2542 .arg2_type = ARG_CONST_MAP_PTR,
2543 .arg3_type = ARG_ANYTHING,
39f19ebb
AS
2544 .arg4_type = ARG_PTR_TO_MEM,
2545 .arg5_type = ARG_CONST_SIZE,
555c8a86
DB
2546};
2547
c6c33454
DB
2548static unsigned short bpf_tunnel_key_af(u64 flags)
2549{
2550 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2551}
2552
f3694e00
DB
2553BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2554 u32, size, u64, flags)
d3aa45ce 2555{
c6c33454
DB
2556 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2557 u8 compat[sizeof(struct bpf_tunnel_key)];
074f528e
DB
2558 void *to_orig = to;
2559 int err;
d3aa45ce 2560
074f528e
DB
2561 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2562 err = -EINVAL;
2563 goto err_clear;
2564 }
2565 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2566 err = -EPROTO;
2567 goto err_clear;
2568 }
c6c33454 2569 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
074f528e 2570 err = -EINVAL;
c6c33454 2571 switch (size) {
4018ab18 2572 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 2573 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4018ab18 2574 goto set_compat;
c6c33454
DB
2575 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2576 /* Fixup deprecated structure layouts here, so we have
2577 * a common path later on.
2578 */
2579 if (ip_tunnel_info_af(info) != AF_INET)
074f528e 2580 goto err_clear;
4018ab18 2581set_compat:
c6c33454
DB
2582 to = (struct bpf_tunnel_key *)compat;
2583 break;
2584 default:
074f528e 2585 goto err_clear;
c6c33454
DB
2586 }
2587 }
d3aa45ce
AS
2588
2589 to->tunnel_id = be64_to_cpu(info->key.tun_id);
c6c33454
DB
2590 to->tunnel_tos = info->key.tos;
2591 to->tunnel_ttl = info->key.ttl;
2592
4018ab18 2593 if (flags & BPF_F_TUNINFO_IPV6) {
c6c33454
DB
2594 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2595 sizeof(to->remote_ipv6));
4018ab18
DB
2596 to->tunnel_label = be32_to_cpu(info->key.label);
2597 } else {
c6c33454 2598 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
4018ab18 2599 }
c6c33454
DB
2600
2601 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
074f528e 2602 memcpy(to_orig, to, size);
d3aa45ce
AS
2603
2604 return 0;
074f528e
DB
2605err_clear:
2606 memset(to_orig, 0, size);
2607 return err;
d3aa45ce
AS
2608}
2609
577c50aa 2610static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
d3aa45ce
AS
2611 .func = bpf_skb_get_tunnel_key,
2612 .gpl_only = false,
2613 .ret_type = RET_INTEGER,
2614 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
2615 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2616 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
2617 .arg4_type = ARG_ANYTHING,
2618};
2619
f3694e00 2620BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
14ca0751 2621{
14ca0751 2622 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
074f528e 2623 int err;
14ca0751
DB
2624
2625 if (unlikely(!info ||
074f528e
DB
2626 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2627 err = -ENOENT;
2628 goto err_clear;
2629 }
2630 if (unlikely(size < info->options_len)) {
2631 err = -ENOMEM;
2632 goto err_clear;
2633 }
14ca0751
DB
2634
2635 ip_tunnel_info_opts_get(to, info);
074f528e
DB
2636 if (size > info->options_len)
2637 memset(to + info->options_len, 0, size - info->options_len);
14ca0751
DB
2638
2639 return info->options_len;
074f528e
DB
2640err_clear:
2641 memset(to, 0, size);
2642 return err;
14ca0751
DB
2643}
2644
2645static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2646 .func = bpf_skb_get_tunnel_opt,
2647 .gpl_only = false,
2648 .ret_type = RET_INTEGER,
2649 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
2650 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2651 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
2652};
2653
d3aa45ce
AS
2654static struct metadata_dst __percpu *md_dst;
2655
f3694e00
DB
2656BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2657 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
d3aa45ce 2658{
d3aa45ce 2659 struct metadata_dst *md = this_cpu_ptr(md_dst);
c6c33454 2660 u8 compat[sizeof(struct bpf_tunnel_key)];
d3aa45ce
AS
2661 struct ip_tunnel_info *info;
2662
22080870
DB
2663 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2664 BPF_F_DONT_FRAGMENT)))
d3aa45ce 2665 return -EINVAL;
c6c33454
DB
2666 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2667 switch (size) {
4018ab18 2668 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 2669 case offsetof(struct bpf_tunnel_key, tunnel_ext):
c6c33454
DB
2670 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2671 /* Fixup deprecated structure layouts here, so we have
2672 * a common path later on.
2673 */
2674 memcpy(compat, from, size);
2675 memset(compat + size, 0, sizeof(compat) - size);
f3694e00 2676 from = (const struct bpf_tunnel_key *) compat;
c6c33454
DB
2677 break;
2678 default:
2679 return -EINVAL;
2680 }
2681 }
c0e760c9
DB
2682 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2683 from->tunnel_ext))
4018ab18 2684 return -EINVAL;
d3aa45ce
AS
2685
2686 skb_dst_drop(skb);
2687 dst_hold((struct dst_entry *) md);
2688 skb_dst_set(skb, (struct dst_entry *) md);
2689
2690 info = &md->u.tun_info;
2691 info->mode = IP_TUNNEL_INFO_TX;
c6c33454 2692
db3c6139 2693 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
22080870
DB
2694 if (flags & BPF_F_DONT_FRAGMENT)
2695 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2696
d3aa45ce 2697 info->key.tun_id = cpu_to_be64(from->tunnel_id);
c6c33454
DB
2698 info->key.tos = from->tunnel_tos;
2699 info->key.ttl = from->tunnel_ttl;
2700
2701 if (flags & BPF_F_TUNINFO_IPV6) {
2702 info->mode |= IP_TUNNEL_INFO_IPV6;
2703 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2704 sizeof(from->remote_ipv6));
4018ab18
DB
2705 info->key.label = cpu_to_be32(from->tunnel_label) &
2706 IPV6_FLOWLABEL_MASK;
c6c33454
DB
2707 } else {
2708 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2da897e5
DB
2709 if (flags & BPF_F_ZERO_CSUM_TX)
2710 info->key.tun_flags &= ~TUNNEL_CSUM;
c6c33454 2711 }
d3aa45ce
AS
2712
2713 return 0;
2714}
2715
577c50aa 2716static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
d3aa45ce
AS
2717 .func = bpf_skb_set_tunnel_key,
2718 .gpl_only = false,
2719 .ret_type = RET_INTEGER,
2720 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
2721 .arg2_type = ARG_PTR_TO_MEM,
2722 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
2723 .arg4_type = ARG_ANYTHING,
2724};
2725
f3694e00
DB
2726BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2727 const u8 *, from, u32, size)
14ca0751 2728{
14ca0751
DB
2729 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2730 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2731
2732 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2733 return -EINVAL;
fca5fdf6 2734 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
14ca0751
DB
2735 return -ENOMEM;
2736
2737 ip_tunnel_info_opts_set(info, from, size);
2738
2739 return 0;
2740}
2741
2742static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2743 .func = bpf_skb_set_tunnel_opt,
2744 .gpl_only = false,
2745 .ret_type = RET_INTEGER,
2746 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
2747 .arg2_type = ARG_PTR_TO_MEM,
2748 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
2749};
2750
2751static const struct bpf_func_proto *
2752bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
d3aa45ce
AS
2753{
2754 if (!md_dst) {
14ca0751
DB
2755 /* Race is not possible, since it's called from verifier
2756 * that is holding verifier mutex.
d3aa45ce 2757 */
fca5fdf6 2758 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3fcece12 2759 METADATA_IP_TUNNEL,
14ca0751 2760 GFP_KERNEL);
d3aa45ce
AS
2761 if (!md_dst)
2762 return NULL;
2763 }
14ca0751
DB
2764
2765 switch (which) {
2766 case BPF_FUNC_skb_set_tunnel_key:
2767 return &bpf_skb_set_tunnel_key_proto;
2768 case BPF_FUNC_skb_set_tunnel_opt:
2769 return &bpf_skb_set_tunnel_opt_proto;
2770 default:
2771 return NULL;
2772 }
d3aa45ce
AS
2773}
2774
f3694e00
DB
2775BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2776 u32, idx)
4a482f34 2777{
4a482f34
MKL
2778 struct bpf_array *array = container_of(map, struct bpf_array, map);
2779 struct cgroup *cgrp;
2780 struct sock *sk;
4a482f34 2781
2d48c5f9 2782 sk = skb_to_full_sk(skb);
4a482f34
MKL
2783 if (!sk || !sk_fullsock(sk))
2784 return -ENOENT;
f3694e00 2785 if (unlikely(idx >= array->map.max_entries))
4a482f34
MKL
2786 return -E2BIG;
2787
f3694e00 2788 cgrp = READ_ONCE(array->ptrs[idx]);
4a482f34
MKL
2789 if (unlikely(!cgrp))
2790 return -EAGAIN;
2791
54fd9c2d 2792 return sk_under_cgroup_hierarchy(sk, cgrp);
4a482f34
MKL
2793}
2794
747ea55e
DB
2795static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2796 .func = bpf_skb_under_cgroup,
4a482f34
MKL
2797 .gpl_only = false,
2798 .ret_type = RET_INTEGER,
2799 .arg1_type = ARG_PTR_TO_CTX,
2800 .arg2_type = ARG_CONST_MAP_PTR,
2801 .arg3_type = ARG_ANYTHING,
2802};
4a482f34 2803
4de16969
DB
2804static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2805 unsigned long off, unsigned long len)
2806{
2807 memcpy(dst_buff, src_buff + off, len);
2808 return 0;
2809}
2810
f3694e00
DB
2811BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2812 u64, flags, void *, meta, u64, meta_size)
4de16969 2813{
4de16969 2814 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4de16969
DB
2815
2816 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2817 return -EINVAL;
2818 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2819 return -EFAULT;
2820
9c471370
MKL
2821 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
2822 xdp_size, bpf_xdp_copy);
4de16969
DB
2823}
2824
2825static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2826 .func = bpf_xdp_event_output,
2827 .gpl_only = true,
2828 .ret_type = RET_INTEGER,
2829 .arg1_type = ARG_PTR_TO_CTX,
2830 .arg2_type = ARG_CONST_MAP_PTR,
2831 .arg3_type = ARG_ANYTHING,
39f19ebb
AS
2832 .arg4_type = ARG_PTR_TO_MEM,
2833 .arg5_type = ARG_CONST_SIZE,
4de16969
DB
2834};
2835
91b8270f
CF
2836BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
2837{
2838 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
2839}
2840
2841static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
2842 .func = bpf_get_socket_cookie,
2843 .gpl_only = false,
2844 .ret_type = RET_INTEGER,
2845 .arg1_type = ARG_PTR_TO_CTX,
2846};
2847
6acc5c29
CF
2848BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
2849{
2850 struct sock *sk = sk_to_full_sk(skb->sk);
2851 kuid_t kuid;
2852
2853 if (!sk || !sk_fullsock(sk))
2854 return overflowuid;
2855 kuid = sock_net_uid(sock_net(sk), sk);
2856 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
2857}
2858
2859static const struct bpf_func_proto bpf_get_socket_uid_proto = {
2860 .func = bpf_get_socket_uid,
2861 .gpl_only = false,
2862 .ret_type = RET_INTEGER,
2863 .arg1_type = ARG_PTR_TO_CTX,
2864};
2865
8c4b4c7e
LB
2866BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
2867 int, level, int, optname, char *, optval, int, optlen)
2868{
2869 struct sock *sk = bpf_sock->sk;
2870 int ret = 0;
2871 int val;
2872
2873 if (!sk_fullsock(sk))
2874 return -EINVAL;
2875
2876 if (level == SOL_SOCKET) {
2877 if (optlen != sizeof(int))
2878 return -EINVAL;
2879 val = *((int *)optval);
2880
2881 /* Only some socketops are supported */
2882 switch (optname) {
2883 case SO_RCVBUF:
2884 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
2885 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
2886 break;
2887 case SO_SNDBUF:
2888 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
2889 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
2890 break;
2891 case SO_MAX_PACING_RATE:
2892 sk->sk_max_pacing_rate = val;
2893 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
2894 sk->sk_max_pacing_rate);
2895 break;
2896 case SO_PRIORITY:
2897 sk->sk_priority = val;
2898 break;
2899 case SO_RCVLOWAT:
2900 if (val < 0)
2901 val = INT_MAX;
2902 sk->sk_rcvlowat = val ? : 1;
2903 break;
2904 case SO_MARK:
2905 sk->sk_mark = val;
2906 break;
2907 default:
2908 ret = -EINVAL;
2909 }
a5192c52 2910#ifdef CONFIG_INET
8c4b4c7e
LB
2911 } else if (level == SOL_TCP &&
2912 sk->sk_prot->setsockopt == tcp_setsockopt) {
91b5b21c
LB
2913 if (optname == TCP_CONGESTION) {
2914 char name[TCP_CA_NAME_MAX];
2915
2916 strncpy(name, optval, min_t(long, optlen,
2917 TCP_CA_NAME_MAX-1));
2918 name[TCP_CA_NAME_MAX-1] = 0;
2919 ret = tcp_set_congestion_control(sk, name, false);
2920 if (!ret && bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN)
2921 /* replacing an existing ca */
2922 tcp_reinit_congestion_control(sk,
2923 inet_csk(sk)->icsk_ca_ops);
2924 } else {
fc747810
LB
2925 struct tcp_sock *tp = tcp_sk(sk);
2926
2927 if (optlen != sizeof(int))
2928 return -EINVAL;
2929
2930 val = *((int *)optval);
2931 /* Only some options are supported */
2932 switch (optname) {
2933 case TCP_BPF_IW:
2934 if (val <= 0 || tp->data_segs_out > 0)
2935 ret = -EINVAL;
2936 else
2937 tp->snd_cwnd = val;
2938 break;
13bf9641
LB
2939 case TCP_BPF_SNDCWND_CLAMP:
2940 if (val <= 0) {
2941 ret = -EINVAL;
2942 } else {
2943 tp->snd_cwnd_clamp = val;
2944 tp->snd_ssthresh = val;
2945 }
6d3f06a0 2946 break;
fc747810
LB
2947 default:
2948 ret = -EINVAL;
2949 }
91b5b21c 2950 }
8c4b4c7e 2951 ret = -EINVAL;
91b5b21c 2952#endif
8c4b4c7e
LB
2953 } else {
2954 ret = -EINVAL;
2955 }
2956 return ret;
2957}
2958
2959static const struct bpf_func_proto bpf_setsockopt_proto = {
2960 .func = bpf_setsockopt,
2961 .gpl_only = true,
2962 .ret_type = RET_INTEGER,
2963 .arg1_type = ARG_PTR_TO_CTX,
2964 .arg2_type = ARG_ANYTHING,
2965 .arg3_type = ARG_ANYTHING,
2966 .arg4_type = ARG_PTR_TO_MEM,
2967 .arg5_type = ARG_CONST_SIZE,
2968};
2969
d4052c4a 2970static const struct bpf_func_proto *
2492d3b8 2971bpf_base_func_proto(enum bpf_func_id func_id)
89aa0758
AS
2972{
2973 switch (func_id) {
2974 case BPF_FUNC_map_lookup_elem:
2975 return &bpf_map_lookup_elem_proto;
2976 case BPF_FUNC_map_update_elem:
2977 return &bpf_map_update_elem_proto;
2978 case BPF_FUNC_map_delete_elem:
2979 return &bpf_map_delete_elem_proto;
03e69b50
DB
2980 case BPF_FUNC_get_prandom_u32:
2981 return &bpf_get_prandom_u32_proto;
c04167ce 2982 case BPF_FUNC_get_smp_processor_id:
80b48c44 2983 return &bpf_get_raw_smp_processor_id_proto;
2d0e30c3
DB
2984 case BPF_FUNC_get_numa_node_id:
2985 return &bpf_get_numa_node_id_proto;
04fd61ab
AS
2986 case BPF_FUNC_tail_call:
2987 return &bpf_tail_call_proto;
17ca8cbf
DB
2988 case BPF_FUNC_ktime_get_ns:
2989 return &bpf_ktime_get_ns_proto;
0756ea3e 2990 case BPF_FUNC_trace_printk:
1be7f75d
AS
2991 if (capable(CAP_SYS_ADMIN))
2992 return bpf_get_trace_printk_proto();
89aa0758
AS
2993 default:
2994 return NULL;
2995 }
2996}
2997
2492d3b8
DB
2998static const struct bpf_func_proto *
2999sk_filter_func_proto(enum bpf_func_id func_id)
3000{
3001 switch (func_id) {
3002 case BPF_FUNC_skb_load_bytes:
3003 return &bpf_skb_load_bytes_proto;
91b8270f
CF
3004 case BPF_FUNC_get_socket_cookie:
3005 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
3006 case BPF_FUNC_get_socket_uid:
3007 return &bpf_get_socket_uid_proto;
2492d3b8
DB
3008 default:
3009 return bpf_base_func_proto(func_id);
3010 }
3011}
3012
608cd71a
AS
3013static const struct bpf_func_proto *
3014tc_cls_act_func_proto(enum bpf_func_id func_id)
3015{
3016 switch (func_id) {
3017 case BPF_FUNC_skb_store_bytes:
3018 return &bpf_skb_store_bytes_proto;
05c74e5e
DB
3019 case BPF_FUNC_skb_load_bytes:
3020 return &bpf_skb_load_bytes_proto;
36bbef52
DB
3021 case BPF_FUNC_skb_pull_data:
3022 return &bpf_skb_pull_data_proto;
7d672345
DB
3023 case BPF_FUNC_csum_diff:
3024 return &bpf_csum_diff_proto;
36bbef52
DB
3025 case BPF_FUNC_csum_update:
3026 return &bpf_csum_update_proto;
91bc4822
AS
3027 case BPF_FUNC_l3_csum_replace:
3028 return &bpf_l3_csum_replace_proto;
3029 case BPF_FUNC_l4_csum_replace:
3030 return &bpf_l4_csum_replace_proto;
3896d655
AS
3031 case BPF_FUNC_clone_redirect:
3032 return &bpf_clone_redirect_proto;
8d20aabe
DB
3033 case BPF_FUNC_get_cgroup_classid:
3034 return &bpf_get_cgroup_classid_proto;
4e10df9a
AS
3035 case BPF_FUNC_skb_vlan_push:
3036 return &bpf_skb_vlan_push_proto;
3037 case BPF_FUNC_skb_vlan_pop:
3038 return &bpf_skb_vlan_pop_proto;
6578171a
DB
3039 case BPF_FUNC_skb_change_proto:
3040 return &bpf_skb_change_proto_proto;
d2485c42
DB
3041 case BPF_FUNC_skb_change_type:
3042 return &bpf_skb_change_type_proto;
2be7e212
DB
3043 case BPF_FUNC_skb_adjust_room:
3044 return &bpf_skb_adjust_room_proto;
5293efe6
DB
3045 case BPF_FUNC_skb_change_tail:
3046 return &bpf_skb_change_tail_proto;
d3aa45ce
AS
3047 case BPF_FUNC_skb_get_tunnel_key:
3048 return &bpf_skb_get_tunnel_key_proto;
3049 case BPF_FUNC_skb_set_tunnel_key:
14ca0751
DB
3050 return bpf_get_skb_set_tunnel_proto(func_id);
3051 case BPF_FUNC_skb_get_tunnel_opt:
3052 return &bpf_skb_get_tunnel_opt_proto;
3053 case BPF_FUNC_skb_set_tunnel_opt:
3054 return bpf_get_skb_set_tunnel_proto(func_id);
27b29f63
AS
3055 case BPF_FUNC_redirect:
3056 return &bpf_redirect_proto;
c46646d0
DB
3057 case BPF_FUNC_get_route_realm:
3058 return &bpf_get_route_realm_proto;
13c5c240
DB
3059 case BPF_FUNC_get_hash_recalc:
3060 return &bpf_get_hash_recalc_proto;
7a4b28c6
DB
3061 case BPF_FUNC_set_hash_invalid:
3062 return &bpf_set_hash_invalid_proto;
ded092cd
DB
3063 case BPF_FUNC_set_hash:
3064 return &bpf_set_hash_proto;
bd570ff9 3065 case BPF_FUNC_perf_event_output:
555c8a86 3066 return &bpf_skb_event_output_proto;
80b48c44
DB
3067 case BPF_FUNC_get_smp_processor_id:
3068 return &bpf_get_smp_processor_id_proto;
747ea55e
DB
3069 case BPF_FUNC_skb_under_cgroup:
3070 return &bpf_skb_under_cgroup_proto;
91b8270f
CF
3071 case BPF_FUNC_get_socket_cookie:
3072 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
3073 case BPF_FUNC_get_socket_uid:
3074 return &bpf_get_socket_uid_proto;
608cd71a 3075 default:
2492d3b8 3076 return bpf_base_func_proto(func_id);
608cd71a
AS
3077 }
3078}
3079
6a773a15
BB
3080static const struct bpf_func_proto *
3081xdp_func_proto(enum bpf_func_id func_id)
3082{
4de16969
DB
3083 switch (func_id) {
3084 case BPF_FUNC_perf_event_output:
3085 return &bpf_xdp_event_output_proto;
669dc4d7
DB
3086 case BPF_FUNC_get_smp_processor_id:
3087 return &bpf_get_smp_processor_id_proto;
17bedab2
MKL
3088 case BPF_FUNC_xdp_adjust_head:
3089 return &bpf_xdp_adjust_head_proto;
814abfab
JF
3090 case BPF_FUNC_redirect:
3091 return &bpf_xdp_redirect_proto;
4de16969 3092 default:
2492d3b8 3093 return bpf_base_func_proto(func_id);
4de16969 3094 }
6a773a15
BB
3095}
3096
3a0af8fd
TG
3097static const struct bpf_func_proto *
3098lwt_inout_func_proto(enum bpf_func_id func_id)
3099{
3100 switch (func_id) {
3101 case BPF_FUNC_skb_load_bytes:
3102 return &bpf_skb_load_bytes_proto;
3103 case BPF_FUNC_skb_pull_data:
3104 return &bpf_skb_pull_data_proto;
3105 case BPF_FUNC_csum_diff:
3106 return &bpf_csum_diff_proto;
3107 case BPF_FUNC_get_cgroup_classid:
3108 return &bpf_get_cgroup_classid_proto;
3109 case BPF_FUNC_get_route_realm:
3110 return &bpf_get_route_realm_proto;
3111 case BPF_FUNC_get_hash_recalc:
3112 return &bpf_get_hash_recalc_proto;
3113 case BPF_FUNC_perf_event_output:
3114 return &bpf_skb_event_output_proto;
3115 case BPF_FUNC_get_smp_processor_id:
3116 return &bpf_get_smp_processor_id_proto;
3117 case BPF_FUNC_skb_under_cgroup:
3118 return &bpf_skb_under_cgroup_proto;
3119 default:
2492d3b8 3120 return bpf_base_func_proto(func_id);
3a0af8fd
TG
3121 }
3122}
3123
8c4b4c7e
LB
3124static const struct bpf_func_proto *
3125 sock_ops_func_proto(enum bpf_func_id func_id)
3126{
3127 switch (func_id) {
3128 case BPF_FUNC_setsockopt:
3129 return &bpf_setsockopt_proto;
3130 default:
3131 return bpf_base_func_proto(func_id);
3132 }
3133}
3134
3a0af8fd
TG
3135static const struct bpf_func_proto *
3136lwt_xmit_func_proto(enum bpf_func_id func_id)
3137{
3138 switch (func_id) {
3139 case BPF_FUNC_skb_get_tunnel_key:
3140 return &bpf_skb_get_tunnel_key_proto;
3141 case BPF_FUNC_skb_set_tunnel_key:
3142 return bpf_get_skb_set_tunnel_proto(func_id);
3143 case BPF_FUNC_skb_get_tunnel_opt:
3144 return &bpf_skb_get_tunnel_opt_proto;
3145 case BPF_FUNC_skb_set_tunnel_opt:
3146 return bpf_get_skb_set_tunnel_proto(func_id);
3147 case BPF_FUNC_redirect:
3148 return &bpf_redirect_proto;
3149 case BPF_FUNC_clone_redirect:
3150 return &bpf_clone_redirect_proto;
3151 case BPF_FUNC_skb_change_tail:
3152 return &bpf_skb_change_tail_proto;
3153 case BPF_FUNC_skb_change_head:
3154 return &bpf_skb_change_head_proto;
3155 case BPF_FUNC_skb_store_bytes:
3156 return &bpf_skb_store_bytes_proto;
3157 case BPF_FUNC_csum_update:
3158 return &bpf_csum_update_proto;
3159 case BPF_FUNC_l3_csum_replace:
3160 return &bpf_l3_csum_replace_proto;
3161 case BPF_FUNC_l4_csum_replace:
3162 return &bpf_l4_csum_replace_proto;
3163 case BPF_FUNC_set_hash_invalid:
3164 return &bpf_set_hash_invalid_proto;
3165 default:
3166 return lwt_inout_func_proto(func_id);
3167 }
3168}
3169
f96da094
DB
3170static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
3171 struct bpf_insn_access_aux *info)
23994631 3172{
f96da094 3173 const int size_default = sizeof(__u32);
23994631 3174
9bac3d6d
AS
3175 if (off < 0 || off >= sizeof(struct __sk_buff))
3176 return false;
62c7989b 3177
4936e352 3178 /* The verifier guarantees that size > 0. */
9bac3d6d
AS
3179 if (off % size != 0)
3180 return false;
62c7989b
DB
3181
3182 switch (off) {
f96da094
DB
3183 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3184 if (off + size > offsetofend(struct __sk_buff, cb[4]))
62c7989b
DB
3185 return false;
3186 break;
f96da094
DB
3187 case bpf_ctx_range(struct __sk_buff, data):
3188 case bpf_ctx_range(struct __sk_buff, data_end):
3189 if (size != size_default)
23994631 3190 return false;
31fd8581
YS
3191 break;
3192 default:
f96da094 3193 /* Only narrow read access allowed for now. */
31fd8581 3194 if (type == BPF_WRITE) {
f96da094 3195 if (size != size_default)
31fd8581
YS
3196 return false;
3197 } else {
f96da094
DB
3198 bpf_ctx_record_field_size(info, size_default);
3199 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
23994631 3200 return false;
31fd8581 3201 }
62c7989b 3202 }
9bac3d6d
AS
3203
3204 return true;
3205}
3206
d691f9e8 3207static bool sk_filter_is_valid_access(int off, int size,
19de99f7 3208 enum bpf_access_type type,
23994631 3209 struct bpf_insn_access_aux *info)
d691f9e8 3210{
db58ba45 3211 switch (off) {
f96da094
DB
3212 case bpf_ctx_range(struct __sk_buff, tc_classid):
3213 case bpf_ctx_range(struct __sk_buff, data):
3214 case bpf_ctx_range(struct __sk_buff, data_end):
045efa82 3215 return false;
db58ba45 3216 }
045efa82 3217
d691f9e8
AS
3218 if (type == BPF_WRITE) {
3219 switch (off) {
f96da094 3220 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
d691f9e8
AS
3221 break;
3222 default:
3223 return false;
3224 }
3225 }
3226
f96da094 3227 return bpf_skb_is_valid_access(off, size, type, info);
d691f9e8
AS
3228}
3229
3a0af8fd
TG
3230static bool lwt_is_valid_access(int off, int size,
3231 enum bpf_access_type type,
23994631 3232 struct bpf_insn_access_aux *info)
3a0af8fd
TG
3233{
3234 switch (off) {
f96da094 3235 case bpf_ctx_range(struct __sk_buff, tc_classid):
3a0af8fd
TG
3236 return false;
3237 }
3238
3239 if (type == BPF_WRITE) {
3240 switch (off) {
f96da094
DB
3241 case bpf_ctx_range(struct __sk_buff, mark):
3242 case bpf_ctx_range(struct __sk_buff, priority):
3243 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3a0af8fd
TG
3244 break;
3245 default:
3246 return false;
3247 }
3248 }
3249
f96da094
DB
3250 switch (off) {
3251 case bpf_ctx_range(struct __sk_buff, data):
3252 info->reg_type = PTR_TO_PACKET;
3253 break;
3254 case bpf_ctx_range(struct __sk_buff, data_end):
3255 info->reg_type = PTR_TO_PACKET_END;
3256 break;
3257 }
3258
3259 return bpf_skb_is_valid_access(off, size, type, info);
3a0af8fd
TG
3260}
3261
61023658
DA
3262static bool sock_filter_is_valid_access(int off, int size,
3263 enum bpf_access_type type,
23994631 3264 struct bpf_insn_access_aux *info)
61023658
DA
3265{
3266 if (type == BPF_WRITE) {
3267 switch (off) {
3268 case offsetof(struct bpf_sock, bound_dev_if):
3269 break;
3270 default:
3271 return false;
3272 }
3273 }
3274
3275 if (off < 0 || off + size > sizeof(struct bpf_sock))
3276 return false;
61023658
DA
3277 /* The verifier guarantees that size > 0. */
3278 if (off % size != 0)
3279 return false;
61023658
DA
3280 if (size != sizeof(__u32))
3281 return false;
3282
3283 return true;
3284}
3285
36bbef52
DB
3286static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
3287 const struct bpf_prog *prog)
3288{
3289 struct bpf_insn *insn = insn_buf;
3290
3291 if (!direct_write)
3292 return 0;
3293
3294 /* if (!skb->cloned)
3295 * goto start;
3296 *
3297 * (Fast-path, otherwise approximation that we might be
3298 * a clone, do the rest in helper.)
3299 */
3300 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
3301 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
3302 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
3303
3304 /* ret = bpf_skb_pull_data(skb, 0); */
3305 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
3306 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
3307 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
3308 BPF_FUNC_skb_pull_data);
3309 /* if (!ret)
3310 * goto restore;
3311 * return TC_ACT_SHOT;
3312 */
3313 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
3314 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT);
3315 *insn++ = BPF_EXIT_INSN();
3316
3317 /* restore: */
3318 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
3319 /* start: */
3320 *insn++ = prog->insnsi[0];
3321
3322 return insn - insn_buf;
3323}
3324
d691f9e8 3325static bool tc_cls_act_is_valid_access(int off, int size,
19de99f7 3326 enum bpf_access_type type,
23994631 3327 struct bpf_insn_access_aux *info)
d691f9e8
AS
3328{
3329 if (type == BPF_WRITE) {
3330 switch (off) {
f96da094
DB
3331 case bpf_ctx_range(struct __sk_buff, mark):
3332 case bpf_ctx_range(struct __sk_buff, tc_index):
3333 case bpf_ctx_range(struct __sk_buff, priority):
3334 case bpf_ctx_range(struct __sk_buff, tc_classid):
3335 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
d691f9e8
AS
3336 break;
3337 default:
3338 return false;
3339 }
3340 }
19de99f7 3341
f96da094
DB
3342 switch (off) {
3343 case bpf_ctx_range(struct __sk_buff, data):
3344 info->reg_type = PTR_TO_PACKET;
3345 break;
3346 case bpf_ctx_range(struct __sk_buff, data_end):
3347 info->reg_type = PTR_TO_PACKET_END;
3348 break;
3349 }
3350
3351 return bpf_skb_is_valid_access(off, size, type, info);
d691f9e8
AS
3352}
3353
1afaf661 3354static bool __is_valid_xdp_access(int off, int size)
6a773a15
BB
3355{
3356 if (off < 0 || off >= sizeof(struct xdp_md))
3357 return false;
3358 if (off % size != 0)
3359 return false;
6088b582 3360 if (size != sizeof(__u32))
6a773a15
BB
3361 return false;
3362
3363 return true;
3364}
3365
3366static bool xdp_is_valid_access(int off, int size,
3367 enum bpf_access_type type,
23994631 3368 struct bpf_insn_access_aux *info)
6a773a15
BB
3369{
3370 if (type == BPF_WRITE)
3371 return false;
3372
3373 switch (off) {
3374 case offsetof(struct xdp_md, data):
23994631 3375 info->reg_type = PTR_TO_PACKET;
6a773a15
BB
3376 break;
3377 case offsetof(struct xdp_md, data_end):
23994631 3378 info->reg_type = PTR_TO_PACKET_END;
6a773a15
BB
3379 break;
3380 }
3381
1afaf661 3382 return __is_valid_xdp_access(off, size);
6a773a15
BB
3383}
3384
3385void bpf_warn_invalid_xdp_action(u32 act)
3386{
3387 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
3388}
3389EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
3390
814abfab
JF
3391void bpf_warn_invalid_xdp_redirect(u32 ifindex)
3392{
3393 WARN_ONCE(1, "Illegal XDP redirect to unsupported device ifindex(%i)\n", ifindex);
3394}
3395
40304b2a
LB
3396static bool __is_valid_sock_ops_access(int off, int size)
3397{
3398 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
3399 return false;
3400 /* The verifier guarantees that size > 0. */
3401 if (off % size != 0)
3402 return false;
3403 if (size != sizeof(__u32))
3404 return false;
3405
3406 return true;
3407}
3408
3409static bool sock_ops_is_valid_access(int off, int size,
3410 enum bpf_access_type type,
3411 struct bpf_insn_access_aux *info)
3412{
3413 if (type == BPF_WRITE) {
3414 switch (off) {
3415 case offsetof(struct bpf_sock_ops, op) ...
3416 offsetof(struct bpf_sock_ops, replylong[3]):
3417 break;
3418 default:
3419 return false;
3420 }
3421 }
3422
3423 return __is_valid_sock_ops_access(off, size);
3424}
3425
2492d3b8
DB
3426static u32 bpf_convert_ctx_access(enum bpf_access_type type,
3427 const struct bpf_insn *si,
3428 struct bpf_insn *insn_buf,
f96da094 3429 struct bpf_prog *prog, u32 *target_size)
9bac3d6d
AS
3430{
3431 struct bpf_insn *insn = insn_buf;
6b8cc1d1 3432 int off;
9bac3d6d 3433
6b8cc1d1 3434 switch (si->off) {
9bac3d6d 3435 case offsetof(struct __sk_buff, len):
6b8cc1d1 3436 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3437 bpf_target_off(struct sk_buff, len, 4,
3438 target_size));
9bac3d6d
AS
3439 break;
3440
0b8c707d 3441 case offsetof(struct __sk_buff, protocol):
6b8cc1d1 3442 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
3443 bpf_target_off(struct sk_buff, protocol, 2,
3444 target_size));
0b8c707d
DB
3445 break;
3446
27cd5452 3447 case offsetof(struct __sk_buff, vlan_proto):
6b8cc1d1 3448 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
3449 bpf_target_off(struct sk_buff, vlan_proto, 2,
3450 target_size));
27cd5452
MS
3451 break;
3452
bcad5718 3453 case offsetof(struct __sk_buff, priority):
754f1e6a 3454 if (type == BPF_WRITE)
6b8cc1d1 3455 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3456 bpf_target_off(struct sk_buff, priority, 4,
3457 target_size));
754f1e6a 3458 else
6b8cc1d1 3459 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3460 bpf_target_off(struct sk_buff, priority, 4,
3461 target_size));
bcad5718
DB
3462 break;
3463
37e82c2f 3464 case offsetof(struct __sk_buff, ingress_ifindex):
6b8cc1d1 3465 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3466 bpf_target_off(struct sk_buff, skb_iif, 4,
3467 target_size));
37e82c2f
AS
3468 break;
3469
3470 case offsetof(struct __sk_buff, ifindex):
f035a515 3471 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 3472 si->dst_reg, si->src_reg,
37e82c2f 3473 offsetof(struct sk_buff, dev));
6b8cc1d1
DB
3474 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
3475 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
3476 bpf_target_off(struct net_device, ifindex, 4,
3477 target_size));
37e82c2f
AS
3478 break;
3479
ba7591d8 3480 case offsetof(struct __sk_buff, hash):
6b8cc1d1 3481 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3482 bpf_target_off(struct sk_buff, hash, 4,
3483 target_size));
ba7591d8
DB
3484 break;
3485
9bac3d6d 3486 case offsetof(struct __sk_buff, mark):
d691f9e8 3487 if (type == BPF_WRITE)
6b8cc1d1 3488 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3489 bpf_target_off(struct sk_buff, mark, 4,
3490 target_size));
d691f9e8 3491 else
6b8cc1d1 3492 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3493 bpf_target_off(struct sk_buff, mark, 4,
3494 target_size));
d691f9e8 3495 break;
9bac3d6d
AS
3496
3497 case offsetof(struct __sk_buff, pkt_type):
f96da094
DB
3498 *target_size = 1;
3499 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
3500 PKT_TYPE_OFFSET());
3501 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
3502#ifdef __BIG_ENDIAN_BITFIELD
3503 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
3504#endif
3505 break;
9bac3d6d
AS
3506
3507 case offsetof(struct __sk_buff, queue_mapping):
f96da094
DB
3508 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3509 bpf_target_off(struct sk_buff, queue_mapping, 2,
3510 target_size));
3511 break;
c2497395 3512
c2497395 3513 case offsetof(struct __sk_buff, vlan_present):
c2497395 3514 case offsetof(struct __sk_buff, vlan_tci):
f96da094
DB
3515 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
3516
3517 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3518 bpf_target_off(struct sk_buff, vlan_tci, 2,
3519 target_size));
3520 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
3521 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
3522 ~VLAN_TAG_PRESENT);
3523 } else {
3524 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
3525 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
3526 }
3527 break;
d691f9e8
AS
3528
3529 case offsetof(struct __sk_buff, cb[0]) ...
f96da094 3530 offsetofend(struct __sk_buff, cb[4]) - 1:
d691f9e8 3531 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
62c7989b
DB
3532 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
3533 offsetof(struct qdisc_skb_cb, data)) %
3534 sizeof(__u64));
d691f9e8 3535
ff936a04 3536 prog->cb_access = 1;
6b8cc1d1
DB
3537 off = si->off;
3538 off -= offsetof(struct __sk_buff, cb[0]);
3539 off += offsetof(struct sk_buff, cb);
3540 off += offsetof(struct qdisc_skb_cb, data);
d691f9e8 3541 if (type == BPF_WRITE)
62c7989b 3542 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 3543 si->src_reg, off);
d691f9e8 3544 else
62c7989b 3545 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 3546 si->src_reg, off);
d691f9e8
AS
3547 break;
3548
045efa82 3549 case offsetof(struct __sk_buff, tc_classid):
6b8cc1d1
DB
3550 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
3551
3552 off = si->off;
3553 off -= offsetof(struct __sk_buff, tc_classid);
3554 off += offsetof(struct sk_buff, cb);
3555 off += offsetof(struct qdisc_skb_cb, tc_classid);
f96da094 3556 *target_size = 2;
09c37a2c 3557 if (type == BPF_WRITE)
6b8cc1d1
DB
3558 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
3559 si->src_reg, off);
09c37a2c 3560 else
6b8cc1d1
DB
3561 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
3562 si->src_reg, off);
045efa82
DB
3563 break;
3564
db58ba45 3565 case offsetof(struct __sk_buff, data):
f035a515 3566 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
6b8cc1d1 3567 si->dst_reg, si->src_reg,
db58ba45
AS
3568 offsetof(struct sk_buff, data));
3569 break;
3570
3571 case offsetof(struct __sk_buff, data_end):
6b8cc1d1
DB
3572 off = si->off;
3573 off -= offsetof(struct __sk_buff, data_end);
3574 off += offsetof(struct sk_buff, cb);
3575 off += offsetof(struct bpf_skb_data_end, data_end);
3576 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
3577 si->src_reg, off);
db58ba45
AS
3578 break;
3579
d691f9e8
AS
3580 case offsetof(struct __sk_buff, tc_index):
3581#ifdef CONFIG_NET_SCHED
d691f9e8 3582 if (type == BPF_WRITE)
6b8cc1d1 3583 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
3584 bpf_target_off(struct sk_buff, tc_index, 2,
3585 target_size));
d691f9e8 3586 else
6b8cc1d1 3587 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
3588 bpf_target_off(struct sk_buff, tc_index, 2,
3589 target_size));
d691f9e8
AS
3590#else
3591 if (type == BPF_WRITE)
6b8cc1d1 3592 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
d691f9e8 3593 else
6b8cc1d1 3594 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
b1d9fc41
DB
3595#endif
3596 break;
3597
3598 case offsetof(struct __sk_buff, napi_id):
3599#if defined(CONFIG_NET_RX_BUSY_POLL)
b1d9fc41 3600 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3601 bpf_target_off(struct sk_buff, napi_id, 4,
3602 target_size));
b1d9fc41
DB
3603 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
3604 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3605#else
3606 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
d691f9e8 3607#endif
6b8cc1d1 3608 break;
9bac3d6d
AS
3609 }
3610
3611 return insn - insn_buf;
89aa0758
AS
3612}
3613
61023658 3614static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
6b8cc1d1 3615 const struct bpf_insn *si,
61023658 3616 struct bpf_insn *insn_buf,
f96da094 3617 struct bpf_prog *prog, u32 *target_size)
61023658
DA
3618{
3619 struct bpf_insn *insn = insn_buf;
3620
6b8cc1d1 3621 switch (si->off) {
61023658
DA
3622 case offsetof(struct bpf_sock, bound_dev_if):
3623 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
3624
3625 if (type == BPF_WRITE)
6b8cc1d1 3626 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
3627 offsetof(struct sock, sk_bound_dev_if));
3628 else
6b8cc1d1 3629 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
3630 offsetof(struct sock, sk_bound_dev_if));
3631 break;
aa4c1037
DA
3632
3633 case offsetof(struct bpf_sock, family):
3634 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
3635
6b8cc1d1 3636 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
aa4c1037
DA
3637 offsetof(struct sock, sk_family));
3638 break;
3639
3640 case offsetof(struct bpf_sock, type):
6b8cc1d1 3641 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
aa4c1037 3642 offsetof(struct sock, __sk_flags_offset));
6b8cc1d1
DB
3643 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
3644 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
aa4c1037
DA
3645 break;
3646
3647 case offsetof(struct bpf_sock, protocol):
6b8cc1d1 3648 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
aa4c1037 3649 offsetof(struct sock, __sk_flags_offset));
6b8cc1d1
DB
3650 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
3651 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
aa4c1037 3652 break;
61023658
DA
3653 }
3654
3655 return insn - insn_buf;
3656}
3657
6b8cc1d1
DB
3658static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
3659 const struct bpf_insn *si,
374fb54e 3660 struct bpf_insn *insn_buf,
f96da094 3661 struct bpf_prog *prog, u32 *target_size)
374fb54e
DB
3662{
3663 struct bpf_insn *insn = insn_buf;
3664
6b8cc1d1 3665 switch (si->off) {
374fb54e 3666 case offsetof(struct __sk_buff, ifindex):
374fb54e 3667 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 3668 si->dst_reg, si->src_reg,
374fb54e 3669 offsetof(struct sk_buff, dev));
6b8cc1d1 3670 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
3671 bpf_target_off(struct net_device, ifindex, 4,
3672 target_size));
374fb54e
DB
3673 break;
3674 default:
f96da094
DB
3675 return bpf_convert_ctx_access(type, si, insn_buf, prog,
3676 target_size);
374fb54e
DB
3677 }
3678
3679 return insn - insn_buf;
3680}
3681
6b8cc1d1
DB
3682static u32 xdp_convert_ctx_access(enum bpf_access_type type,
3683 const struct bpf_insn *si,
6a773a15 3684 struct bpf_insn *insn_buf,
f96da094 3685 struct bpf_prog *prog, u32 *target_size)
6a773a15
BB
3686{
3687 struct bpf_insn *insn = insn_buf;
3688
6b8cc1d1 3689 switch (si->off) {
6a773a15 3690 case offsetof(struct xdp_md, data):
f035a515 3691 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6b8cc1d1 3692 si->dst_reg, si->src_reg,
6a773a15
BB
3693 offsetof(struct xdp_buff, data));
3694 break;
3695 case offsetof(struct xdp_md, data_end):
f035a515 3696 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6b8cc1d1 3697 si->dst_reg, si->src_reg,
6a773a15
BB
3698 offsetof(struct xdp_buff, data_end));
3699 break;
3700 }
3701
3702 return insn - insn_buf;
3703}
3704
40304b2a
LB
3705static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
3706 const struct bpf_insn *si,
3707 struct bpf_insn *insn_buf,
f96da094
DB
3708 struct bpf_prog *prog,
3709 u32 *target_size)
40304b2a
LB
3710{
3711 struct bpf_insn *insn = insn_buf;
3712 int off;
3713
3714 switch (si->off) {
3715 case offsetof(struct bpf_sock_ops, op) ...
3716 offsetof(struct bpf_sock_ops, replylong[3]):
3717 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
3718 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
3719 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
3720 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
3721 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
3722 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
3723 off = si->off;
3724 off -= offsetof(struct bpf_sock_ops, op);
3725 off += offsetof(struct bpf_sock_ops_kern, op);
3726 if (type == BPF_WRITE)
3727 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3728 off);
3729 else
3730 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3731 off);
3732 break;
3733
3734 case offsetof(struct bpf_sock_ops, family):
3735 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
3736
3737 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3738 struct bpf_sock_ops_kern, sk),
3739 si->dst_reg, si->src_reg,
3740 offsetof(struct bpf_sock_ops_kern, sk));
3741 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3742 offsetof(struct sock_common, skc_family));
3743 break;
3744
3745 case offsetof(struct bpf_sock_ops, remote_ip4):
3746 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
3747
3748 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3749 struct bpf_sock_ops_kern, sk),
3750 si->dst_reg, si->src_reg,
3751 offsetof(struct bpf_sock_ops_kern, sk));
3752 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3753 offsetof(struct sock_common, skc_daddr));
3754 break;
3755
3756 case offsetof(struct bpf_sock_ops, local_ip4):
3757 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
3758
3759 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3760 struct bpf_sock_ops_kern, sk),
3761 si->dst_reg, si->src_reg,
3762 offsetof(struct bpf_sock_ops_kern, sk));
3763 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3764 offsetof(struct sock_common,
3765 skc_rcv_saddr));
3766 break;
3767
3768 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
3769 offsetof(struct bpf_sock_ops, remote_ip6[3]):
3770#if IS_ENABLED(CONFIG_IPV6)
3771 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3772 skc_v6_daddr.s6_addr32[0]) != 4);
3773
3774 off = si->off;
3775 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
3776 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3777 struct bpf_sock_ops_kern, sk),
3778 si->dst_reg, si->src_reg,
3779 offsetof(struct bpf_sock_ops_kern, sk));
3780 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3781 offsetof(struct sock_common,
3782 skc_v6_daddr.s6_addr32[0]) +
3783 off);
3784#else
3785 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3786#endif
3787 break;
3788
3789 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
3790 offsetof(struct bpf_sock_ops, local_ip6[3]):
3791#if IS_ENABLED(CONFIG_IPV6)
3792 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3793 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
3794
3795 off = si->off;
3796 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
3797 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3798 struct bpf_sock_ops_kern, sk),
3799 si->dst_reg, si->src_reg,
3800 offsetof(struct bpf_sock_ops_kern, sk));
3801 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3802 offsetof(struct sock_common,
3803 skc_v6_rcv_saddr.s6_addr32[0]) +
3804 off);
3805#else
3806 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3807#endif
3808 break;
3809
3810 case offsetof(struct bpf_sock_ops, remote_port):
3811 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
3812
3813 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3814 struct bpf_sock_ops_kern, sk),
3815 si->dst_reg, si->src_reg,
3816 offsetof(struct bpf_sock_ops_kern, sk));
3817 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3818 offsetof(struct sock_common, skc_dport));
3819#ifndef __BIG_ENDIAN_BITFIELD
3820 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
3821#endif
3822 break;
3823
3824 case offsetof(struct bpf_sock_ops, local_port):
3825 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
3826
3827 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3828 struct bpf_sock_ops_kern, sk),
3829 si->dst_reg, si->src_reg,
3830 offsetof(struct bpf_sock_ops_kern, sk));
3831 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3832 offsetof(struct sock_common, skc_num));
3833 break;
3834 }
3835 return insn - insn_buf;
3836}
3837
be9370a7 3838const struct bpf_verifier_ops sk_filter_prog_ops = {
4936e352
DB
3839 .get_func_proto = sk_filter_func_proto,
3840 .is_valid_access = sk_filter_is_valid_access,
2492d3b8 3841 .convert_ctx_access = bpf_convert_ctx_access,
89aa0758
AS
3842};
3843
be9370a7 3844const struct bpf_verifier_ops tc_cls_act_prog_ops = {
4936e352
DB
3845 .get_func_proto = tc_cls_act_func_proto,
3846 .is_valid_access = tc_cls_act_is_valid_access,
374fb54e 3847 .convert_ctx_access = tc_cls_act_convert_ctx_access,
36bbef52 3848 .gen_prologue = tc_cls_act_prologue,
1cf1cae9 3849 .test_run = bpf_prog_test_run_skb,
608cd71a
AS
3850};
3851
be9370a7 3852const struct bpf_verifier_ops xdp_prog_ops = {
6a773a15
BB
3853 .get_func_proto = xdp_func_proto,
3854 .is_valid_access = xdp_is_valid_access,
3855 .convert_ctx_access = xdp_convert_ctx_access,
1cf1cae9 3856 .test_run = bpf_prog_test_run_xdp,
6a773a15
BB
3857};
3858
be9370a7 3859const struct bpf_verifier_ops cg_skb_prog_ops = {
966789fb 3860 .get_func_proto = sk_filter_func_proto,
0e33661d 3861 .is_valid_access = sk_filter_is_valid_access,
2492d3b8 3862 .convert_ctx_access = bpf_convert_ctx_access,
1cf1cae9 3863 .test_run = bpf_prog_test_run_skb,
0e33661d
DM
3864};
3865
be9370a7 3866const struct bpf_verifier_ops lwt_inout_prog_ops = {
3a0af8fd
TG
3867 .get_func_proto = lwt_inout_func_proto,
3868 .is_valid_access = lwt_is_valid_access,
2492d3b8 3869 .convert_ctx_access = bpf_convert_ctx_access,
1cf1cae9 3870 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
3871};
3872
be9370a7 3873const struct bpf_verifier_ops lwt_xmit_prog_ops = {
3a0af8fd
TG
3874 .get_func_proto = lwt_xmit_func_proto,
3875 .is_valid_access = lwt_is_valid_access,
2492d3b8 3876 .convert_ctx_access = bpf_convert_ctx_access,
3a0af8fd 3877 .gen_prologue = tc_cls_act_prologue,
1cf1cae9 3878 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
3879};
3880
be9370a7 3881const struct bpf_verifier_ops cg_sock_prog_ops = {
2492d3b8 3882 .get_func_proto = bpf_base_func_proto,
61023658
DA
3883 .is_valid_access = sock_filter_is_valid_access,
3884 .convert_ctx_access = sock_filter_convert_ctx_access,
3885};
3886
40304b2a 3887const struct bpf_verifier_ops sock_ops_prog_ops = {
8c4b4c7e 3888 .get_func_proto = sock_ops_func_proto,
40304b2a
LB
3889 .is_valid_access = sock_ops_is_valid_access,
3890 .convert_ctx_access = sock_ops_convert_ctx_access,
3891};
3892
8ced425e 3893int sk_detach_filter(struct sock *sk)
55b33325
PE
3894{
3895 int ret = -ENOENT;
3896 struct sk_filter *filter;
3897
d59577b6
VB
3898 if (sock_flag(sk, SOCK_FILTER_LOCKED))
3899 return -EPERM;
3900
8ced425e
HFS
3901 filter = rcu_dereference_protected(sk->sk_filter,
3902 lockdep_sock_is_held(sk));
55b33325 3903 if (filter) {
a9b3cd7f 3904 RCU_INIT_POINTER(sk->sk_filter, NULL);
46bcf14f 3905 sk_filter_uncharge(sk, filter);
55b33325
PE
3906 ret = 0;
3907 }
a3ea269b 3908
55b33325
PE
3909 return ret;
3910}
8ced425e 3911EXPORT_SYMBOL_GPL(sk_detach_filter);
a8fc9277 3912
a3ea269b
DB
3913int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
3914 unsigned int len)
a8fc9277 3915{
a3ea269b 3916 struct sock_fprog_kern *fprog;
a8fc9277 3917 struct sk_filter *filter;
a3ea269b 3918 int ret = 0;
a8fc9277
PE
3919
3920 lock_sock(sk);
3921 filter = rcu_dereference_protected(sk->sk_filter,
8ced425e 3922 lockdep_sock_is_held(sk));
a8fc9277
PE
3923 if (!filter)
3924 goto out;
a3ea269b
DB
3925
3926 /* We're copying the filter that has been originally attached,
93d08b69
DB
3927 * so no conversion/decode needed anymore. eBPF programs that
3928 * have no original program cannot be dumped through this.
a3ea269b 3929 */
93d08b69 3930 ret = -EACCES;
7ae457c1 3931 fprog = filter->prog->orig_prog;
93d08b69
DB
3932 if (!fprog)
3933 goto out;
a3ea269b
DB
3934
3935 ret = fprog->len;
a8fc9277 3936 if (!len)
a3ea269b 3937 /* User space only enquires number of filter blocks. */
a8fc9277 3938 goto out;
a3ea269b 3939
a8fc9277 3940 ret = -EINVAL;
a3ea269b 3941 if (len < fprog->len)
a8fc9277
PE
3942 goto out;
3943
3944 ret = -EFAULT;
009937e7 3945 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
a3ea269b 3946 goto out;
a8fc9277 3947
a3ea269b
DB
3948 /* Instead of bytes, the API requests to return the number
3949 * of filter blocks.
3950 */
3951 ret = fprog->len;
a8fc9277
PE
3952out:
3953 release_sock(sk);
3954 return ret;
3955}