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