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