Revert "mdio_bus: Remove unneeded gpiod NULL check"
[linux-2.6-block.git] / net / core / filter.c
CommitLineData
1da177e4
LT
1/*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
bd4cf0ed
AS
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
1da177e4 6 *
bd4cf0ed
AS
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
8 *
9 * Authors:
10 *
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
1da177e4
LT
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 * Andi Kleen - Fix a few bad bugs and races.
4df95ff4 21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
1da177e4
LT
22 */
23
24#include <linux/module.h>
25#include <linux/types.h>
1da177e4
LT
26#include <linux/mm.h>
27#include <linux/fcntl.h>
28#include <linux/socket.h>
91b8270f 29#include <linux/sock_diag.h>
1da177e4
LT
30#include <linux/in.h>
31#include <linux/inet.h>
32#include <linux/netdevice.h>
33#include <linux/if_packet.h>
c491680f 34#include <linux/if_arp.h>
5a0e3ad6 35#include <linux/gfp.h>
1da177e4
LT
36#include <net/ip.h>
37#include <net/protocol.h>
4738c1db 38#include <net/netlink.h>
1da177e4
LT
39#include <linux/skbuff.h>
40#include <net/sock.h>
10b89ee4 41#include <net/flow_dissector.h>
1da177e4
LT
42#include <linux/errno.h>
43#include <linux/timer.h>
7c0f6ba6 44#include <linux/uaccess.h>
40daafc8 45#include <asm/unaligned.h>
1da177e4 46#include <linux/filter.h>
86e4ca66 47#include <linux/ratelimit.h>
46b325c7 48#include <linux/seccomp.h>
f3335031 49#include <linux/if_vlan.h>
89aa0758 50#include <linux/bpf.h>
d691f9e8 51#include <net/sch_generic.h>
8d20aabe 52#include <net/cls_cgroup.h>
d3aa45ce 53#include <net/dst_metadata.h>
c46646d0 54#include <net/dst.h>
538950a1 55#include <net/sock_reuseport.h>
b1d9fc41 56#include <net/busy_poll.h>
8c4b4c7e 57#include <net/tcp.h>
5acaee0a 58#include <linux/bpf_trace.h>
1da177e4 59
43db6d65 60/**
f4979fce 61 * sk_filter_trim_cap - run a packet through a socket filter
43db6d65
SH
62 * @sk: sock associated with &sk_buff
63 * @skb: buffer to filter
f4979fce 64 * @cap: limit on how short the eBPF program may trim the packet
43db6d65 65 *
ff936a04
AS
66 * Run the eBPF program and then cut skb->data to correct size returned by
67 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
43db6d65 68 * than pkt_len we keep whole skb->data. This is the socket level
ff936a04 69 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
43db6d65
SH
70 * be accepted or -EPERM if the packet should be tossed.
71 *
72 */
f4979fce 73int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
43db6d65
SH
74{
75 int err;
76 struct sk_filter *filter;
77
c93bdd0e
MG
78 /*
79 * If the skb was allocated from pfmemalloc reserves, only
80 * allow SOCK_MEMALLOC sockets to use it as this socket is
81 * helping free memory
82 */
8fe809a9
ED
83 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
84 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
c93bdd0e 85 return -ENOMEM;
8fe809a9 86 }
c11cd3a6
DM
87 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
88 if (err)
89 return err;
90
43db6d65
SH
91 err = security_sock_rcv_skb(sk, skb);
92 if (err)
93 return err;
94
80f8f102
ED
95 rcu_read_lock();
96 filter = rcu_dereference(sk->sk_filter);
43db6d65 97 if (filter) {
8f917bba
WB
98 struct sock *save_sk = skb->sk;
99 unsigned int pkt_len;
100
101 skb->sk = sk;
102 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
8f917bba 103 skb->sk = save_sk;
d1f496fd 104 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
43db6d65 105 }
80f8f102 106 rcu_read_unlock();
43db6d65
SH
107
108 return err;
109}
f4979fce 110EXPORT_SYMBOL(sk_filter_trim_cap);
43db6d65 111
f3694e00 112BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
bd4cf0ed 113{
f3694e00 114 return skb_get_poff(skb);
bd4cf0ed
AS
115}
116
f3694e00 117BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
bd4cf0ed 118{
bd4cf0ed
AS
119 struct nlattr *nla;
120
121 if (skb_is_nonlinear(skb))
122 return 0;
123
05ab8f26
MK
124 if (skb->len < sizeof(struct nlattr))
125 return 0;
126
30743837 127 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
128 return 0;
129
30743837 130 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
bd4cf0ed
AS
131 if (nla)
132 return (void *) nla - (void *) skb->data;
133
134 return 0;
135}
136
f3694e00 137BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
bd4cf0ed 138{
bd4cf0ed
AS
139 struct nlattr *nla;
140
141 if (skb_is_nonlinear(skb))
142 return 0;
143
05ab8f26
MK
144 if (skb->len < sizeof(struct nlattr))
145 return 0;
146
30743837 147 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
148 return 0;
149
30743837
DB
150 nla = (struct nlattr *) &skb->data[a];
151 if (nla->nla_len > skb->len - a)
bd4cf0ed
AS
152 return 0;
153
30743837 154 nla = nla_find_nested(nla, x);
bd4cf0ed
AS
155 if (nla)
156 return (void *) nla - (void *) skb->data;
157
158 return 0;
159}
160
f3694e00 161BPF_CALL_0(__get_raw_cpu_id)
bd4cf0ed
AS
162{
163 return raw_smp_processor_id();
164}
165
80b48c44
DB
166static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
167 .func = __get_raw_cpu_id,
168 .gpl_only = false,
169 .ret_type = RET_INTEGER,
170};
171
9bac3d6d
AS
172static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
173 struct bpf_insn *insn_buf)
174{
175 struct bpf_insn *insn = insn_buf;
176
177 switch (skb_field) {
178 case SKF_AD_MARK:
179 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
180
181 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
182 offsetof(struct sk_buff, mark));
183 break;
184
185 case SKF_AD_PKTTYPE:
186 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
187 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
188#ifdef __BIG_ENDIAN_BITFIELD
189 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
190#endif
191 break;
192
193 case SKF_AD_QUEUE:
194 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
195
196 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
197 offsetof(struct sk_buff, queue_mapping));
198 break;
c2497395 199
c2497395
AS
200 case SKF_AD_VLAN_TAG:
201 case SKF_AD_VLAN_TAG_PRESENT:
202 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
203 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
204
205 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
206 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
207 offsetof(struct sk_buff, vlan_tci));
208 if (skb_field == SKF_AD_VLAN_TAG) {
209 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
210 ~VLAN_TAG_PRESENT);
211 } else {
212 /* dst_reg >>= 12 */
213 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
214 /* dst_reg &= 1 */
215 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
216 }
217 break;
9bac3d6d
AS
218 }
219
220 return insn - insn_buf;
221}
222
bd4cf0ed 223static bool convert_bpf_extensions(struct sock_filter *fp,
2695fb55 224 struct bpf_insn **insnp)
bd4cf0ed 225{
2695fb55 226 struct bpf_insn *insn = *insnp;
9bac3d6d 227 u32 cnt;
bd4cf0ed
AS
228
229 switch (fp->k) {
230 case SKF_AD_OFF + SKF_AD_PROTOCOL:
0b8c707d
DB
231 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
232
233 /* A = *(u16 *) (CTX + offsetof(protocol)) */
234 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
235 offsetof(struct sk_buff, protocol));
236 /* A = ntohs(A) [emitting a nop or swap16] */
237 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
bd4cf0ed
AS
238 break;
239
240 case SKF_AD_OFF + SKF_AD_PKTTYPE:
9bac3d6d
AS
241 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
242 insn += cnt - 1;
bd4cf0ed
AS
243 break;
244
245 case SKF_AD_OFF + SKF_AD_IFINDEX:
246 case SKF_AD_OFF + SKF_AD_HATYPE:
bd4cf0ed
AS
247 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
248 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
f8f6d679 249
f035a515 250 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
f8f6d679
DB
251 BPF_REG_TMP, BPF_REG_CTX,
252 offsetof(struct sk_buff, dev));
253 /* if (tmp != 0) goto pc + 1 */
254 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
255 *insn++ = BPF_EXIT_INSN();
256 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
257 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
258 offsetof(struct net_device, ifindex));
259 else
260 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
261 offsetof(struct net_device, type));
bd4cf0ed
AS
262 break;
263
264 case SKF_AD_OFF + SKF_AD_MARK:
9bac3d6d
AS
265 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
266 insn += cnt - 1;
bd4cf0ed
AS
267 break;
268
269 case SKF_AD_OFF + SKF_AD_RXHASH:
270 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
271
9739eef1
AS
272 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
273 offsetof(struct sk_buff, hash));
bd4cf0ed
AS
274 break;
275
276 case SKF_AD_OFF + SKF_AD_QUEUE:
9bac3d6d
AS
277 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
278 insn += cnt - 1;
bd4cf0ed
AS
279 break;
280
281 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
c2497395
AS
282 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
283 BPF_REG_A, BPF_REG_CTX, insn);
284 insn += cnt - 1;
285 break;
bd4cf0ed 286
c2497395
AS
287 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
288 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
289 BPF_REG_A, BPF_REG_CTX, insn);
290 insn += cnt - 1;
bd4cf0ed
AS
291 break;
292
27cd5452
MS
293 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
294 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
295
296 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
297 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
298 offsetof(struct sk_buff, vlan_proto));
299 /* A = ntohs(A) [emitting a nop or swap16] */
300 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
301 break;
302
bd4cf0ed
AS
303 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
304 case SKF_AD_OFF + SKF_AD_NLATTR:
305 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
306 case SKF_AD_OFF + SKF_AD_CPU:
4cd3675e 307 case SKF_AD_OFF + SKF_AD_RANDOM:
e430f34e 308 /* arg1 = CTX */
f8f6d679 309 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
bd4cf0ed 310 /* arg2 = A */
f8f6d679 311 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
bd4cf0ed 312 /* arg3 = X */
f8f6d679 313 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
e430f34e 314 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
bd4cf0ed
AS
315 switch (fp->k) {
316 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
f8f6d679 317 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
bd4cf0ed
AS
318 break;
319 case SKF_AD_OFF + SKF_AD_NLATTR:
f8f6d679 320 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
bd4cf0ed
AS
321 break;
322 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
f8f6d679 323 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
bd4cf0ed
AS
324 break;
325 case SKF_AD_OFF + SKF_AD_CPU:
f8f6d679 326 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
bd4cf0ed 327 break;
4cd3675e 328 case SKF_AD_OFF + SKF_AD_RANDOM:
3ad00405
DB
329 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
330 bpf_user_rnd_init_once();
4cd3675e 331 break;
bd4cf0ed
AS
332 }
333 break;
334
335 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
9739eef1
AS
336 /* A ^= X */
337 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
338 break;
339
340 default:
341 /* This is just a dummy call to avoid letting the compiler
342 * evict __bpf_call_base() as an optimization. Placed here
343 * where no-one bothers.
344 */
345 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
346 return false;
347 }
348
349 *insnp = insn;
350 return true;
351}
352
353/**
8fb575ca 354 * bpf_convert_filter - convert filter program
bd4cf0ed
AS
355 * @prog: the user passed filter program
356 * @len: the length of the user passed filter program
50bbfed9 357 * @new_prog: allocated 'struct bpf_prog' or NULL
bd4cf0ed
AS
358 * @new_len: pointer to store length of converted program
359 *
1f504ec9
TK
360 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
361 * style extended BPF (eBPF).
bd4cf0ed
AS
362 * Conversion workflow:
363 *
364 * 1) First pass for calculating the new program length:
8fb575ca 365 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
bd4cf0ed
AS
366 *
367 * 2) 2nd pass to remap in two passes: 1st pass finds new
368 * jump offsets, 2nd pass remapping:
8fb575ca 369 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
bd4cf0ed 370 */
d9e12f42 371static int bpf_convert_filter(struct sock_filter *prog, int len,
50bbfed9 372 struct bpf_prog *new_prog, int *new_len)
bd4cf0ed 373{
50bbfed9
AS
374 int new_flen = 0, pass = 0, target, i, stack_off;
375 struct bpf_insn *new_insn, *first_insn = NULL;
bd4cf0ed
AS
376 struct sock_filter *fp;
377 int *addrs = NULL;
378 u8 bpf_src;
379
380 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
30743837 381 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
bd4cf0ed 382
6f9a093b 383 if (len <= 0 || len > BPF_MAXINSNS)
bd4cf0ed
AS
384 return -EINVAL;
385
386 if (new_prog) {
50bbfed9 387 first_insn = new_prog->insnsi;
658da937
DB
388 addrs = kcalloc(len, sizeof(*addrs),
389 GFP_KERNEL | __GFP_NOWARN);
bd4cf0ed
AS
390 if (!addrs)
391 return -ENOMEM;
392 }
393
394do_pass:
50bbfed9 395 new_insn = first_insn;
bd4cf0ed
AS
396 fp = prog;
397
8b614aeb 398 /* Classic BPF related prologue emission. */
50bbfed9 399 if (new_prog) {
8b614aeb
DB
400 /* Classic BPF expects A and X to be reset first. These need
401 * to be guaranteed to be the first two instructions.
402 */
403 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
404 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
405
406 /* All programs must keep CTX in callee saved BPF_REG_CTX.
407 * In eBPF case it's done by the compiler, here we need to
408 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
409 */
410 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
411 } else {
412 new_insn += 3;
413 }
bd4cf0ed
AS
414
415 for (i = 0; i < len; fp++, i++) {
2695fb55
AS
416 struct bpf_insn tmp_insns[6] = { };
417 struct bpf_insn *insn = tmp_insns;
bd4cf0ed
AS
418
419 if (addrs)
50bbfed9 420 addrs[i] = new_insn - first_insn;
bd4cf0ed
AS
421
422 switch (fp->code) {
423 /* All arithmetic insns and skb loads map as-is. */
424 case BPF_ALU | BPF_ADD | BPF_X:
425 case BPF_ALU | BPF_ADD | BPF_K:
426 case BPF_ALU | BPF_SUB | BPF_X:
427 case BPF_ALU | BPF_SUB | BPF_K:
428 case BPF_ALU | BPF_AND | BPF_X:
429 case BPF_ALU | BPF_AND | BPF_K:
430 case BPF_ALU | BPF_OR | BPF_X:
431 case BPF_ALU | BPF_OR | BPF_K:
432 case BPF_ALU | BPF_LSH | BPF_X:
433 case BPF_ALU | BPF_LSH | BPF_K:
434 case BPF_ALU | BPF_RSH | BPF_X:
435 case BPF_ALU | BPF_RSH | BPF_K:
436 case BPF_ALU | BPF_XOR | BPF_X:
437 case BPF_ALU | BPF_XOR | BPF_K:
438 case BPF_ALU | BPF_MUL | BPF_X:
439 case BPF_ALU | BPF_MUL | BPF_K:
440 case BPF_ALU | BPF_DIV | BPF_X:
441 case BPF_ALU | BPF_DIV | BPF_K:
442 case BPF_ALU | BPF_MOD | BPF_X:
443 case BPF_ALU | BPF_MOD | BPF_K:
444 case BPF_ALU | BPF_NEG:
445 case BPF_LD | BPF_ABS | BPF_W:
446 case BPF_LD | BPF_ABS | BPF_H:
447 case BPF_LD | BPF_ABS | BPF_B:
448 case BPF_LD | BPF_IND | BPF_W:
449 case BPF_LD | BPF_IND | BPF_H:
450 case BPF_LD | BPF_IND | BPF_B:
451 /* Check for overloaded BPF extension and
452 * directly convert it if found, otherwise
453 * just move on with mapping.
454 */
455 if (BPF_CLASS(fp->code) == BPF_LD &&
456 BPF_MODE(fp->code) == BPF_ABS &&
457 convert_bpf_extensions(fp, &insn))
458 break;
459
f8f6d679 460 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
bd4cf0ed
AS
461 break;
462
f8f6d679
DB
463 /* Jump transformation cannot use BPF block macros
464 * everywhere as offset calculation and target updates
465 * require a bit more work than the rest, i.e. jump
466 * opcodes map as-is, but offsets need adjustment.
467 */
468
469#define BPF_EMIT_JMP \
bd4cf0ed
AS
470 do { \
471 if (target >= len || target < 0) \
472 goto err; \
473 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
474 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
475 insn->off -= insn - tmp_insns; \
476 } while (0)
477
f8f6d679
DB
478 case BPF_JMP | BPF_JA:
479 target = i + fp->k + 1;
480 insn->code = fp->code;
481 BPF_EMIT_JMP;
bd4cf0ed
AS
482 break;
483
484 case BPF_JMP | BPF_JEQ | BPF_K:
485 case BPF_JMP | BPF_JEQ | BPF_X:
486 case BPF_JMP | BPF_JSET | BPF_K:
487 case BPF_JMP | BPF_JSET | BPF_X:
488 case BPF_JMP | BPF_JGT | BPF_K:
489 case BPF_JMP | BPF_JGT | BPF_X:
490 case BPF_JMP | BPF_JGE | BPF_K:
491 case BPF_JMP | BPF_JGE | BPF_X:
492 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
493 /* BPF immediates are signed, zero extend
494 * immediate into tmp register and use it
495 * in compare insn.
496 */
f8f6d679 497 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
bd4cf0ed 498
e430f34e
AS
499 insn->dst_reg = BPF_REG_A;
500 insn->src_reg = BPF_REG_TMP;
bd4cf0ed
AS
501 bpf_src = BPF_X;
502 } else {
e430f34e 503 insn->dst_reg = BPF_REG_A;
bd4cf0ed
AS
504 insn->imm = fp->k;
505 bpf_src = BPF_SRC(fp->code);
19539ce7 506 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
1da177e4 507 }
bd4cf0ed
AS
508
509 /* Common case where 'jump_false' is next insn. */
510 if (fp->jf == 0) {
511 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
512 target = i + fp->jt + 1;
f8f6d679 513 BPF_EMIT_JMP;
bd4cf0ed 514 break;
1da177e4 515 }
bd4cf0ed 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;
109980b8 1797 const struct bpf_prog *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
e4a8e817
DB
2503static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
2504 struct bpf_prog *xdp_prog)
97f91a7c
JF
2505{
2506 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
109980b8 2507 const struct bpf_prog *map_owner = ri->map_owner;
97f91a7c 2508 struct bpf_map *map = ri->map;
11393cc9 2509 u32 index = ri->ifindex;
97f91a7c 2510 struct net_device *fwd;
4c03bdd7 2511 int err;
97f91a7c
JF
2512
2513 ri->ifindex = 0;
2514 ri->map = NULL;
109980b8
DB
2515 ri->map_owner = NULL;
2516
2517 /* This is really only caused by a deliberately crappy
2518 * BPF program, normally we would never hit that case,
2519 * so no need to inform someone via tracepoints either,
2520 * just bail out.
2521 */
2522 if (unlikely(map_owner != xdp_prog))
2523 return -EINVAL;
97f91a7c 2524
11393cc9 2525 fwd = __dev_map_lookup_elem(map, index);
4c03bdd7
JDB
2526 if (!fwd) {
2527 err = -EINVAL;
f5836ca5 2528 goto err;
4c03bdd7 2529 }
e4a8e817 2530 if (ri->map_to_flush && ri->map_to_flush != map)
11393cc9
JF
2531 xdp_do_flush_map();
2532
2533 err = __bpf_tx_xdp(fwd, map, xdp, index);
f5836ca5
JDB
2534 if (unlikely(err))
2535 goto err;
2536
2537 ri->map_to_flush = map;
59a30896 2538 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
f5836ca5
JDB
2539 return 0;
2540err:
59a30896 2541 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
97f91a7c
JF
2542 return err;
2543}
2544
5acaee0a
JF
2545int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
2546 struct bpf_prog *xdp_prog)
814abfab
JF
2547{
2548 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
5acaee0a 2549 struct net_device *fwd;
eb48d682 2550 u32 index = ri->ifindex;
4c03bdd7 2551 int err;
814abfab 2552
97f91a7c
JF
2553 if (ri->map)
2554 return xdp_do_redirect_map(dev, xdp, xdp_prog);
2555
eb48d682 2556 fwd = dev_get_by_index_rcu(dev_net(dev), index);
814abfab 2557 ri->ifindex = 0;
5acaee0a 2558 if (unlikely(!fwd)) {
4c03bdd7 2559 err = -EINVAL;
f5836ca5 2560 goto err;
814abfab
JF
2561 }
2562
4c03bdd7 2563 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
f5836ca5
JDB
2564 if (unlikely(err))
2565 goto err;
2566
2567 _trace_xdp_redirect(dev, xdp_prog, index);
2568 return 0;
2569err:
2570 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
4c03bdd7 2571 return err;
814abfab
JF
2572}
2573EXPORT_SYMBOL_GPL(xdp_do_redirect);
2574
2facaad6
JDB
2575int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
2576 struct bpf_prog *xdp_prog)
6103aa96
JF
2577{
2578 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
eb48d682 2579 u32 index = ri->ifindex;
2facaad6
JDB
2580 struct net_device *fwd;
2581 unsigned int len;
2582 int err = 0;
6103aa96 2583
2facaad6 2584 fwd = dev_get_by_index_rcu(dev_net(dev), index);
6103aa96 2585 ri->ifindex = 0;
2facaad6
JDB
2586 if (unlikely(!fwd)) {
2587 err = -EINVAL;
f5836ca5 2588 goto err;
6103aa96
JF
2589 }
2590
2facaad6
JDB
2591 if (unlikely(!(fwd->flags & IFF_UP))) {
2592 err = -ENETDOWN;
f5836ca5 2593 goto err;
2facaad6 2594 }
6103aa96 2595
2facaad6
JDB
2596 len = fwd->mtu + fwd->hard_header_len + VLAN_HLEN;
2597 if (skb->len > len) {
2598 err = -EMSGSIZE;
f5836ca5 2599 goto err;
2facaad6
JDB
2600 }
2601
2602 skb->dev = fwd;
f5836ca5
JDB
2603 _trace_xdp_redirect(dev, xdp_prog, index);
2604 return 0;
2605err:
2606 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2facaad6 2607 return err;
6103aa96
JF
2608}
2609EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
2610
814abfab
JF
2611BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
2612{
2613 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2614
2615 if (unlikely(flags))
2616 return XDP_ABORTED;
2617
2618 ri->ifindex = ifindex;
2619 ri->flags = flags;
109980b8
DB
2620 ri->map = NULL;
2621 ri->map_owner = NULL;
e4a8e817 2622
814abfab
JF
2623 return XDP_REDIRECT;
2624}
2625
2626static const struct bpf_func_proto bpf_xdp_redirect_proto = {
2627 .func = bpf_xdp_redirect,
2628 .gpl_only = false,
2629 .ret_type = RET_INTEGER,
2630 .arg1_type = ARG_ANYTHING,
2631 .arg2_type = ARG_ANYTHING,
2632};
2633
109980b8
DB
2634BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
2635 const struct bpf_prog *, map_owner)
e4a8e817
DB
2636{
2637 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2638
2639 if (unlikely(flags))
2640 return XDP_ABORTED;
2641
2642 ri->ifindex = ifindex;
2643 ri->flags = flags;
2644 ri->map = map;
109980b8 2645 ri->map_owner = map_owner;
e4a8e817
DB
2646
2647 return XDP_REDIRECT;
2648}
2649
109980b8
DB
2650/* Note, arg4 is hidden from users and populated by the verifier
2651 * with the right pointer.
2652 */
e4a8e817
DB
2653static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
2654 .func = bpf_xdp_redirect_map,
2655 .gpl_only = false,
2656 .ret_type = RET_INTEGER,
2657 .arg1_type = ARG_CONST_MAP_PTR,
2658 .arg2_type = ARG_ANYTHING,
2659 .arg3_type = ARG_ANYTHING,
2660};
2661
17bedab2 2662bool bpf_helper_changes_pkt_data(void *func)
4e10df9a 2663{
36bbef52
DB
2664 if (func == bpf_skb_vlan_push ||
2665 func == bpf_skb_vlan_pop ||
2666 func == bpf_skb_store_bytes ||
2667 func == bpf_skb_change_proto ||
3a0af8fd 2668 func == bpf_skb_change_head ||
36bbef52 2669 func == bpf_skb_change_tail ||
2be7e212 2670 func == bpf_skb_adjust_room ||
36bbef52 2671 func == bpf_skb_pull_data ||
41703a73 2672 func == bpf_clone_redirect ||
36bbef52 2673 func == bpf_l3_csum_replace ||
17bedab2
MKL
2674 func == bpf_l4_csum_replace ||
2675 func == bpf_xdp_adjust_head)
3697649f
DB
2676 return true;
2677
4e10df9a
AS
2678 return false;
2679}
2680
555c8a86 2681static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
aa7145c1 2682 unsigned long off, unsigned long len)
555c8a86 2683{
aa7145c1 2684 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
555c8a86
DB
2685
2686 if (unlikely(!ptr))
2687 return len;
2688 if (ptr != dst_buff)
2689 memcpy(dst_buff, ptr, len);
2690
2691 return 0;
2692}
2693
f3694e00
DB
2694BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2695 u64, flags, void *, meta, u64, meta_size)
555c8a86 2696{
555c8a86 2697 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
555c8a86
DB
2698
2699 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2700 return -EINVAL;
2701 if (unlikely(skb_size > skb->len))
2702 return -EFAULT;
2703
2704 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2705 bpf_skb_copy);
2706}
2707
2708static const struct bpf_func_proto bpf_skb_event_output_proto = {
2709 .func = bpf_skb_event_output,
2710 .gpl_only = true,
2711 .ret_type = RET_INTEGER,
2712 .arg1_type = ARG_PTR_TO_CTX,
2713 .arg2_type = ARG_CONST_MAP_PTR,
2714 .arg3_type = ARG_ANYTHING,
39f19ebb
AS
2715 .arg4_type = ARG_PTR_TO_MEM,
2716 .arg5_type = ARG_CONST_SIZE,
555c8a86
DB
2717};
2718
c6c33454
DB
2719static unsigned short bpf_tunnel_key_af(u64 flags)
2720{
2721 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2722}
2723
f3694e00
DB
2724BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2725 u32, size, u64, flags)
d3aa45ce 2726{
c6c33454
DB
2727 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2728 u8 compat[sizeof(struct bpf_tunnel_key)];
074f528e
DB
2729 void *to_orig = to;
2730 int err;
d3aa45ce 2731
074f528e
DB
2732 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2733 err = -EINVAL;
2734 goto err_clear;
2735 }
2736 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2737 err = -EPROTO;
2738 goto err_clear;
2739 }
c6c33454 2740 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
074f528e 2741 err = -EINVAL;
c6c33454 2742 switch (size) {
4018ab18 2743 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 2744 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4018ab18 2745 goto set_compat;
c6c33454
DB
2746 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2747 /* Fixup deprecated structure layouts here, so we have
2748 * a common path later on.
2749 */
2750 if (ip_tunnel_info_af(info) != AF_INET)
074f528e 2751 goto err_clear;
4018ab18 2752set_compat:
c6c33454
DB
2753 to = (struct bpf_tunnel_key *)compat;
2754 break;
2755 default:
074f528e 2756 goto err_clear;
c6c33454
DB
2757 }
2758 }
d3aa45ce
AS
2759
2760 to->tunnel_id = be64_to_cpu(info->key.tun_id);
c6c33454
DB
2761 to->tunnel_tos = info->key.tos;
2762 to->tunnel_ttl = info->key.ttl;
2763
4018ab18 2764 if (flags & BPF_F_TUNINFO_IPV6) {
c6c33454
DB
2765 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2766 sizeof(to->remote_ipv6));
4018ab18
DB
2767 to->tunnel_label = be32_to_cpu(info->key.label);
2768 } else {
c6c33454 2769 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
4018ab18 2770 }
c6c33454
DB
2771
2772 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
074f528e 2773 memcpy(to_orig, to, size);
d3aa45ce
AS
2774
2775 return 0;
074f528e
DB
2776err_clear:
2777 memset(to_orig, 0, size);
2778 return err;
d3aa45ce
AS
2779}
2780
577c50aa 2781static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
d3aa45ce
AS
2782 .func = bpf_skb_get_tunnel_key,
2783 .gpl_only = false,
2784 .ret_type = RET_INTEGER,
2785 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
2786 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2787 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
2788 .arg4_type = ARG_ANYTHING,
2789};
2790
f3694e00 2791BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
14ca0751 2792{
14ca0751 2793 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
074f528e 2794 int err;
14ca0751
DB
2795
2796 if (unlikely(!info ||
074f528e
DB
2797 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2798 err = -ENOENT;
2799 goto err_clear;
2800 }
2801 if (unlikely(size < info->options_len)) {
2802 err = -ENOMEM;
2803 goto err_clear;
2804 }
14ca0751
DB
2805
2806 ip_tunnel_info_opts_get(to, info);
074f528e
DB
2807 if (size > info->options_len)
2808 memset(to + info->options_len, 0, size - info->options_len);
14ca0751
DB
2809
2810 return info->options_len;
074f528e
DB
2811err_clear:
2812 memset(to, 0, size);
2813 return err;
14ca0751
DB
2814}
2815
2816static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2817 .func = bpf_skb_get_tunnel_opt,
2818 .gpl_only = false,
2819 .ret_type = RET_INTEGER,
2820 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
2821 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2822 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
2823};
2824
d3aa45ce
AS
2825static struct metadata_dst __percpu *md_dst;
2826
f3694e00
DB
2827BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2828 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
d3aa45ce 2829{
d3aa45ce 2830 struct metadata_dst *md = this_cpu_ptr(md_dst);
c6c33454 2831 u8 compat[sizeof(struct bpf_tunnel_key)];
d3aa45ce
AS
2832 struct ip_tunnel_info *info;
2833
22080870
DB
2834 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2835 BPF_F_DONT_FRAGMENT)))
d3aa45ce 2836 return -EINVAL;
c6c33454
DB
2837 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2838 switch (size) {
4018ab18 2839 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 2840 case offsetof(struct bpf_tunnel_key, tunnel_ext):
c6c33454
DB
2841 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2842 /* Fixup deprecated structure layouts here, so we have
2843 * a common path later on.
2844 */
2845 memcpy(compat, from, size);
2846 memset(compat + size, 0, sizeof(compat) - size);
f3694e00 2847 from = (const struct bpf_tunnel_key *) compat;
c6c33454
DB
2848 break;
2849 default:
2850 return -EINVAL;
2851 }
2852 }
c0e760c9
DB
2853 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2854 from->tunnel_ext))
4018ab18 2855 return -EINVAL;
d3aa45ce
AS
2856
2857 skb_dst_drop(skb);
2858 dst_hold((struct dst_entry *) md);
2859 skb_dst_set(skb, (struct dst_entry *) md);
2860
2861 info = &md->u.tun_info;
2862 info->mode = IP_TUNNEL_INFO_TX;
c6c33454 2863
db3c6139 2864 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
22080870
DB
2865 if (flags & BPF_F_DONT_FRAGMENT)
2866 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2867
d3aa45ce 2868 info->key.tun_id = cpu_to_be64(from->tunnel_id);
c6c33454
DB
2869 info->key.tos = from->tunnel_tos;
2870 info->key.ttl = from->tunnel_ttl;
2871
2872 if (flags & BPF_F_TUNINFO_IPV6) {
2873 info->mode |= IP_TUNNEL_INFO_IPV6;
2874 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2875 sizeof(from->remote_ipv6));
4018ab18
DB
2876 info->key.label = cpu_to_be32(from->tunnel_label) &
2877 IPV6_FLOWLABEL_MASK;
c6c33454
DB
2878 } else {
2879 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2da897e5
DB
2880 if (flags & BPF_F_ZERO_CSUM_TX)
2881 info->key.tun_flags &= ~TUNNEL_CSUM;
c6c33454 2882 }
d3aa45ce
AS
2883
2884 return 0;
2885}
2886
577c50aa 2887static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
d3aa45ce
AS
2888 .func = bpf_skb_set_tunnel_key,
2889 .gpl_only = false,
2890 .ret_type = RET_INTEGER,
2891 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
2892 .arg2_type = ARG_PTR_TO_MEM,
2893 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
2894 .arg4_type = ARG_ANYTHING,
2895};
2896
f3694e00
DB
2897BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2898 const u8 *, from, u32, size)
14ca0751 2899{
14ca0751
DB
2900 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2901 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2902
2903 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2904 return -EINVAL;
fca5fdf6 2905 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
14ca0751
DB
2906 return -ENOMEM;
2907
2908 ip_tunnel_info_opts_set(info, from, size);
2909
2910 return 0;
2911}
2912
2913static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2914 .func = bpf_skb_set_tunnel_opt,
2915 .gpl_only = false,
2916 .ret_type = RET_INTEGER,
2917 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
2918 .arg2_type = ARG_PTR_TO_MEM,
2919 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
2920};
2921
2922static const struct bpf_func_proto *
2923bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
d3aa45ce
AS
2924{
2925 if (!md_dst) {
14ca0751
DB
2926 /* Race is not possible, since it's called from verifier
2927 * that is holding verifier mutex.
d3aa45ce 2928 */
fca5fdf6 2929 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3fcece12 2930 METADATA_IP_TUNNEL,
14ca0751 2931 GFP_KERNEL);
d3aa45ce
AS
2932 if (!md_dst)
2933 return NULL;
2934 }
14ca0751
DB
2935
2936 switch (which) {
2937 case BPF_FUNC_skb_set_tunnel_key:
2938 return &bpf_skb_set_tunnel_key_proto;
2939 case BPF_FUNC_skb_set_tunnel_opt:
2940 return &bpf_skb_set_tunnel_opt_proto;
2941 default:
2942 return NULL;
2943 }
d3aa45ce
AS
2944}
2945
f3694e00
DB
2946BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2947 u32, idx)
4a482f34 2948{
4a482f34
MKL
2949 struct bpf_array *array = container_of(map, struct bpf_array, map);
2950 struct cgroup *cgrp;
2951 struct sock *sk;
4a482f34 2952
2d48c5f9 2953 sk = skb_to_full_sk(skb);
4a482f34
MKL
2954 if (!sk || !sk_fullsock(sk))
2955 return -ENOENT;
f3694e00 2956 if (unlikely(idx >= array->map.max_entries))
4a482f34
MKL
2957 return -E2BIG;
2958
f3694e00 2959 cgrp = READ_ONCE(array->ptrs[idx]);
4a482f34
MKL
2960 if (unlikely(!cgrp))
2961 return -EAGAIN;
2962
54fd9c2d 2963 return sk_under_cgroup_hierarchy(sk, cgrp);
4a482f34
MKL
2964}
2965
747ea55e
DB
2966static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2967 .func = bpf_skb_under_cgroup,
4a482f34
MKL
2968 .gpl_only = false,
2969 .ret_type = RET_INTEGER,
2970 .arg1_type = ARG_PTR_TO_CTX,
2971 .arg2_type = ARG_CONST_MAP_PTR,
2972 .arg3_type = ARG_ANYTHING,
2973};
4a482f34 2974
4de16969
DB
2975static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2976 unsigned long off, unsigned long len)
2977{
2978 memcpy(dst_buff, src_buff + off, len);
2979 return 0;
2980}
2981
f3694e00
DB
2982BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2983 u64, flags, void *, meta, u64, meta_size)
4de16969 2984{
4de16969 2985 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4de16969
DB
2986
2987 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2988 return -EINVAL;
2989 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2990 return -EFAULT;
2991
9c471370
MKL
2992 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
2993 xdp_size, bpf_xdp_copy);
4de16969
DB
2994}
2995
2996static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2997 .func = bpf_xdp_event_output,
2998 .gpl_only = true,
2999 .ret_type = RET_INTEGER,
3000 .arg1_type = ARG_PTR_TO_CTX,
3001 .arg2_type = ARG_CONST_MAP_PTR,
3002 .arg3_type = ARG_ANYTHING,
39f19ebb
AS
3003 .arg4_type = ARG_PTR_TO_MEM,
3004 .arg5_type = ARG_CONST_SIZE,
4de16969
DB
3005};
3006
91b8270f
CF
3007BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3008{
3009 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3010}
3011
3012static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3013 .func = bpf_get_socket_cookie,
3014 .gpl_only = false,
3015 .ret_type = RET_INTEGER,
3016 .arg1_type = ARG_PTR_TO_CTX,
3017};
3018
6acc5c29
CF
3019BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3020{
3021 struct sock *sk = sk_to_full_sk(skb->sk);
3022 kuid_t kuid;
3023
3024 if (!sk || !sk_fullsock(sk))
3025 return overflowuid;
3026 kuid = sock_net_uid(sock_net(sk), sk);
3027 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3028}
3029
3030static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3031 .func = bpf_get_socket_uid,
3032 .gpl_only = false,
3033 .ret_type = RET_INTEGER,
3034 .arg1_type = ARG_PTR_TO_CTX,
3035};
3036
8c4b4c7e
LB
3037BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3038 int, level, int, optname, char *, optval, int, optlen)
3039{
3040 struct sock *sk = bpf_sock->sk;
3041 int ret = 0;
3042 int val;
3043
3044 if (!sk_fullsock(sk))
3045 return -EINVAL;
3046
3047 if (level == SOL_SOCKET) {
3048 if (optlen != sizeof(int))
3049 return -EINVAL;
3050 val = *((int *)optval);
3051
3052 /* Only some socketops are supported */
3053 switch (optname) {
3054 case SO_RCVBUF:
3055 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3056 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3057 break;
3058 case SO_SNDBUF:
3059 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3060 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3061 break;
3062 case SO_MAX_PACING_RATE:
3063 sk->sk_max_pacing_rate = val;
3064 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3065 sk->sk_max_pacing_rate);
3066 break;
3067 case SO_PRIORITY:
3068 sk->sk_priority = val;
3069 break;
3070 case SO_RCVLOWAT:
3071 if (val < 0)
3072 val = INT_MAX;
3073 sk->sk_rcvlowat = val ? : 1;
3074 break;
3075 case SO_MARK:
3076 sk->sk_mark = val;
3077 break;
3078 default:
3079 ret = -EINVAL;
3080 }
a5192c52 3081#ifdef CONFIG_INET
8c4b4c7e
LB
3082 } else if (level == SOL_TCP &&
3083 sk->sk_prot->setsockopt == tcp_setsockopt) {
91b5b21c
LB
3084 if (optname == TCP_CONGESTION) {
3085 char name[TCP_CA_NAME_MAX];
ebfa00c5 3086 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
91b5b21c
LB
3087
3088 strncpy(name, optval, min_t(long, optlen,
3089 TCP_CA_NAME_MAX-1));
3090 name[TCP_CA_NAME_MAX-1] = 0;
ebfa00c5 3091 ret = tcp_set_congestion_control(sk, name, false, reinit);
91b5b21c 3092 } else {
fc747810
LB
3093 struct tcp_sock *tp = tcp_sk(sk);
3094
3095 if (optlen != sizeof(int))
3096 return -EINVAL;
3097
3098 val = *((int *)optval);
3099 /* Only some options are supported */
3100 switch (optname) {
3101 case TCP_BPF_IW:
3102 if (val <= 0 || tp->data_segs_out > 0)
3103 ret = -EINVAL;
3104 else
3105 tp->snd_cwnd = val;
3106 break;
13bf9641
LB
3107 case TCP_BPF_SNDCWND_CLAMP:
3108 if (val <= 0) {
3109 ret = -EINVAL;
3110 } else {
3111 tp->snd_cwnd_clamp = val;
3112 tp->snd_ssthresh = val;
3113 }
6d3f06a0 3114 break;
fc747810
LB
3115 default:
3116 ret = -EINVAL;
3117 }
91b5b21c 3118 }
91b5b21c 3119#endif
8c4b4c7e
LB
3120 } else {
3121 ret = -EINVAL;
3122 }
3123 return ret;
3124}
3125
3126static const struct bpf_func_proto bpf_setsockopt_proto = {
3127 .func = bpf_setsockopt,
3128 .gpl_only = true,
3129 .ret_type = RET_INTEGER,
3130 .arg1_type = ARG_PTR_TO_CTX,
3131 .arg2_type = ARG_ANYTHING,
3132 .arg3_type = ARG_ANYTHING,
3133 .arg4_type = ARG_PTR_TO_MEM,
3134 .arg5_type = ARG_CONST_SIZE,
3135};
3136
d4052c4a 3137static const struct bpf_func_proto *
2492d3b8 3138bpf_base_func_proto(enum bpf_func_id func_id)
89aa0758
AS
3139{
3140 switch (func_id) {
3141 case BPF_FUNC_map_lookup_elem:
3142 return &bpf_map_lookup_elem_proto;
3143 case BPF_FUNC_map_update_elem:
3144 return &bpf_map_update_elem_proto;
3145 case BPF_FUNC_map_delete_elem:
3146 return &bpf_map_delete_elem_proto;
03e69b50
DB
3147 case BPF_FUNC_get_prandom_u32:
3148 return &bpf_get_prandom_u32_proto;
c04167ce 3149 case BPF_FUNC_get_smp_processor_id:
80b48c44 3150 return &bpf_get_raw_smp_processor_id_proto;
2d0e30c3
DB
3151 case BPF_FUNC_get_numa_node_id:
3152 return &bpf_get_numa_node_id_proto;
04fd61ab
AS
3153 case BPF_FUNC_tail_call:
3154 return &bpf_tail_call_proto;
17ca8cbf
DB
3155 case BPF_FUNC_ktime_get_ns:
3156 return &bpf_ktime_get_ns_proto;
0756ea3e 3157 case BPF_FUNC_trace_printk:
1be7f75d
AS
3158 if (capable(CAP_SYS_ADMIN))
3159 return bpf_get_trace_printk_proto();
89aa0758
AS
3160 default:
3161 return NULL;
3162 }
3163}
3164
ae2cf1c4
DA
3165static const struct bpf_func_proto *
3166sock_filter_func_proto(enum bpf_func_id func_id)
3167{
3168 switch (func_id) {
3169 /* inet and inet6 sockets are created in a process
3170 * context so there is always a valid uid/gid
3171 */
3172 case BPF_FUNC_get_current_uid_gid:
3173 return &bpf_get_current_uid_gid_proto;
3174 default:
3175 return bpf_base_func_proto(func_id);
3176 }
3177}
3178
2492d3b8
DB
3179static const struct bpf_func_proto *
3180sk_filter_func_proto(enum bpf_func_id func_id)
3181{
3182 switch (func_id) {
3183 case BPF_FUNC_skb_load_bytes:
3184 return &bpf_skb_load_bytes_proto;
91b8270f
CF
3185 case BPF_FUNC_get_socket_cookie:
3186 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
3187 case BPF_FUNC_get_socket_uid:
3188 return &bpf_get_socket_uid_proto;
2492d3b8
DB
3189 default:
3190 return bpf_base_func_proto(func_id);
3191 }
3192}
3193
608cd71a
AS
3194static const struct bpf_func_proto *
3195tc_cls_act_func_proto(enum bpf_func_id func_id)
3196{
3197 switch (func_id) {
3198 case BPF_FUNC_skb_store_bytes:
3199 return &bpf_skb_store_bytes_proto;
05c74e5e
DB
3200 case BPF_FUNC_skb_load_bytes:
3201 return &bpf_skb_load_bytes_proto;
36bbef52
DB
3202 case BPF_FUNC_skb_pull_data:
3203 return &bpf_skb_pull_data_proto;
7d672345
DB
3204 case BPF_FUNC_csum_diff:
3205 return &bpf_csum_diff_proto;
36bbef52
DB
3206 case BPF_FUNC_csum_update:
3207 return &bpf_csum_update_proto;
91bc4822
AS
3208 case BPF_FUNC_l3_csum_replace:
3209 return &bpf_l3_csum_replace_proto;
3210 case BPF_FUNC_l4_csum_replace:
3211 return &bpf_l4_csum_replace_proto;
3896d655
AS
3212 case BPF_FUNC_clone_redirect:
3213 return &bpf_clone_redirect_proto;
8d20aabe
DB
3214 case BPF_FUNC_get_cgroup_classid:
3215 return &bpf_get_cgroup_classid_proto;
4e10df9a
AS
3216 case BPF_FUNC_skb_vlan_push:
3217 return &bpf_skb_vlan_push_proto;
3218 case BPF_FUNC_skb_vlan_pop:
3219 return &bpf_skb_vlan_pop_proto;
6578171a
DB
3220 case BPF_FUNC_skb_change_proto:
3221 return &bpf_skb_change_proto_proto;
d2485c42
DB
3222 case BPF_FUNC_skb_change_type:
3223 return &bpf_skb_change_type_proto;
2be7e212
DB
3224 case BPF_FUNC_skb_adjust_room:
3225 return &bpf_skb_adjust_room_proto;
5293efe6
DB
3226 case BPF_FUNC_skb_change_tail:
3227 return &bpf_skb_change_tail_proto;
d3aa45ce
AS
3228 case BPF_FUNC_skb_get_tunnel_key:
3229 return &bpf_skb_get_tunnel_key_proto;
3230 case BPF_FUNC_skb_set_tunnel_key:
14ca0751
DB
3231 return bpf_get_skb_set_tunnel_proto(func_id);
3232 case BPF_FUNC_skb_get_tunnel_opt:
3233 return &bpf_skb_get_tunnel_opt_proto;
3234 case BPF_FUNC_skb_set_tunnel_opt:
3235 return bpf_get_skb_set_tunnel_proto(func_id);
27b29f63
AS
3236 case BPF_FUNC_redirect:
3237 return &bpf_redirect_proto;
c46646d0
DB
3238 case BPF_FUNC_get_route_realm:
3239 return &bpf_get_route_realm_proto;
13c5c240
DB
3240 case BPF_FUNC_get_hash_recalc:
3241 return &bpf_get_hash_recalc_proto;
7a4b28c6
DB
3242 case BPF_FUNC_set_hash_invalid:
3243 return &bpf_set_hash_invalid_proto;
ded092cd
DB
3244 case BPF_FUNC_set_hash:
3245 return &bpf_set_hash_proto;
bd570ff9 3246 case BPF_FUNC_perf_event_output:
555c8a86 3247 return &bpf_skb_event_output_proto;
80b48c44
DB
3248 case BPF_FUNC_get_smp_processor_id:
3249 return &bpf_get_smp_processor_id_proto;
747ea55e
DB
3250 case BPF_FUNC_skb_under_cgroup:
3251 return &bpf_skb_under_cgroup_proto;
91b8270f
CF
3252 case BPF_FUNC_get_socket_cookie:
3253 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
3254 case BPF_FUNC_get_socket_uid:
3255 return &bpf_get_socket_uid_proto;
608cd71a 3256 default:
2492d3b8 3257 return bpf_base_func_proto(func_id);
608cd71a
AS
3258 }
3259}
3260
6a773a15
BB
3261static const struct bpf_func_proto *
3262xdp_func_proto(enum bpf_func_id func_id)
3263{
4de16969
DB
3264 switch (func_id) {
3265 case BPF_FUNC_perf_event_output:
3266 return &bpf_xdp_event_output_proto;
669dc4d7
DB
3267 case BPF_FUNC_get_smp_processor_id:
3268 return &bpf_get_smp_processor_id_proto;
17bedab2
MKL
3269 case BPF_FUNC_xdp_adjust_head:
3270 return &bpf_xdp_adjust_head_proto;
814abfab
JF
3271 case BPF_FUNC_redirect:
3272 return &bpf_xdp_redirect_proto;
97f91a7c 3273 case BPF_FUNC_redirect_map:
e4a8e817 3274 return &bpf_xdp_redirect_map_proto;
4de16969 3275 default:
2492d3b8 3276 return bpf_base_func_proto(func_id);
4de16969 3277 }
6a773a15
BB
3278}
3279
3a0af8fd
TG
3280static const struct bpf_func_proto *
3281lwt_inout_func_proto(enum bpf_func_id func_id)
3282{
3283 switch (func_id) {
3284 case BPF_FUNC_skb_load_bytes:
3285 return &bpf_skb_load_bytes_proto;
3286 case BPF_FUNC_skb_pull_data:
3287 return &bpf_skb_pull_data_proto;
3288 case BPF_FUNC_csum_diff:
3289 return &bpf_csum_diff_proto;
3290 case BPF_FUNC_get_cgroup_classid:
3291 return &bpf_get_cgroup_classid_proto;
3292 case BPF_FUNC_get_route_realm:
3293 return &bpf_get_route_realm_proto;
3294 case BPF_FUNC_get_hash_recalc:
3295 return &bpf_get_hash_recalc_proto;
3296 case BPF_FUNC_perf_event_output:
3297 return &bpf_skb_event_output_proto;
3298 case BPF_FUNC_get_smp_processor_id:
3299 return &bpf_get_smp_processor_id_proto;
3300 case BPF_FUNC_skb_under_cgroup:
3301 return &bpf_skb_under_cgroup_proto;
3302 default:
2492d3b8 3303 return bpf_base_func_proto(func_id);
3a0af8fd
TG
3304 }
3305}
3306
8c4b4c7e
LB
3307static const struct bpf_func_proto *
3308 sock_ops_func_proto(enum bpf_func_id func_id)
3309{
3310 switch (func_id) {
3311 case BPF_FUNC_setsockopt:
3312 return &bpf_setsockopt_proto;
174a79ff
JF
3313 case BPF_FUNC_sock_map_update:
3314 return &bpf_sock_map_update_proto;
8c4b4c7e
LB
3315 default:
3316 return bpf_base_func_proto(func_id);
3317 }
3318}
3319
b005fd18
JF
3320static const struct bpf_func_proto *sk_skb_func_proto(enum bpf_func_id func_id)
3321{
3322 switch (func_id) {
8a31db56
JF
3323 case BPF_FUNC_skb_store_bytes:
3324 return &bpf_skb_store_bytes_proto;
b005fd18
JF
3325 case BPF_FUNC_skb_load_bytes:
3326 return &bpf_skb_load_bytes_proto;
8a31db56
JF
3327 case BPF_FUNC_skb_pull_data:
3328 return &bpf_skb_pull_data_proto;
3329 case BPF_FUNC_skb_change_tail:
3330 return &bpf_skb_change_tail_proto;
3331 case BPF_FUNC_skb_change_head:
3332 return &bpf_skb_change_head_proto;
b005fd18
JF
3333 case BPF_FUNC_get_socket_cookie:
3334 return &bpf_get_socket_cookie_proto;
3335 case BPF_FUNC_get_socket_uid:
3336 return &bpf_get_socket_uid_proto;
174a79ff
JF
3337 case BPF_FUNC_sk_redirect_map:
3338 return &bpf_sk_redirect_map_proto;
b005fd18
JF
3339 default:
3340 return bpf_base_func_proto(func_id);
3341 }
3342}
3343
3a0af8fd
TG
3344static const struct bpf_func_proto *
3345lwt_xmit_func_proto(enum bpf_func_id func_id)
3346{
3347 switch (func_id) {
3348 case BPF_FUNC_skb_get_tunnel_key:
3349 return &bpf_skb_get_tunnel_key_proto;
3350 case BPF_FUNC_skb_set_tunnel_key:
3351 return bpf_get_skb_set_tunnel_proto(func_id);
3352 case BPF_FUNC_skb_get_tunnel_opt:
3353 return &bpf_skb_get_tunnel_opt_proto;
3354 case BPF_FUNC_skb_set_tunnel_opt:
3355 return bpf_get_skb_set_tunnel_proto(func_id);
3356 case BPF_FUNC_redirect:
3357 return &bpf_redirect_proto;
3358 case BPF_FUNC_clone_redirect:
3359 return &bpf_clone_redirect_proto;
3360 case BPF_FUNC_skb_change_tail:
3361 return &bpf_skb_change_tail_proto;
3362 case BPF_FUNC_skb_change_head:
3363 return &bpf_skb_change_head_proto;
3364 case BPF_FUNC_skb_store_bytes:
3365 return &bpf_skb_store_bytes_proto;
3366 case BPF_FUNC_csum_update:
3367 return &bpf_csum_update_proto;
3368 case BPF_FUNC_l3_csum_replace:
3369 return &bpf_l3_csum_replace_proto;
3370 case BPF_FUNC_l4_csum_replace:
3371 return &bpf_l4_csum_replace_proto;
3372 case BPF_FUNC_set_hash_invalid:
3373 return &bpf_set_hash_invalid_proto;
3374 default:
3375 return lwt_inout_func_proto(func_id);
3376 }
3377}
3378
f96da094
DB
3379static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
3380 struct bpf_insn_access_aux *info)
23994631 3381{
f96da094 3382 const int size_default = sizeof(__u32);
23994631 3383
9bac3d6d
AS
3384 if (off < 0 || off >= sizeof(struct __sk_buff))
3385 return false;
62c7989b 3386
4936e352 3387 /* The verifier guarantees that size > 0. */
9bac3d6d
AS
3388 if (off % size != 0)
3389 return false;
62c7989b
DB
3390
3391 switch (off) {
f96da094
DB
3392 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3393 if (off + size > offsetofend(struct __sk_buff, cb[4]))
62c7989b
DB
3394 return false;
3395 break;
8a31db56
JF
3396 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
3397 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
3398 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
3399 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
f96da094
DB
3400 case bpf_ctx_range(struct __sk_buff, data):
3401 case bpf_ctx_range(struct __sk_buff, data_end):
3402 if (size != size_default)
23994631 3403 return false;
31fd8581
YS
3404 break;
3405 default:
f96da094 3406 /* Only narrow read access allowed for now. */
31fd8581 3407 if (type == BPF_WRITE) {
f96da094 3408 if (size != size_default)
31fd8581
YS
3409 return false;
3410 } else {
f96da094
DB
3411 bpf_ctx_record_field_size(info, size_default);
3412 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
23994631 3413 return false;
31fd8581 3414 }
62c7989b 3415 }
9bac3d6d
AS
3416
3417 return true;
3418}
3419
d691f9e8 3420static bool sk_filter_is_valid_access(int off, int size,
19de99f7 3421 enum bpf_access_type type,
23994631 3422 struct bpf_insn_access_aux *info)
d691f9e8 3423{
db58ba45 3424 switch (off) {
f96da094
DB
3425 case bpf_ctx_range(struct __sk_buff, tc_classid):
3426 case bpf_ctx_range(struct __sk_buff, data):
3427 case bpf_ctx_range(struct __sk_buff, data_end):
8a31db56 3428 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
045efa82 3429 return false;
db58ba45 3430 }
045efa82 3431
d691f9e8
AS
3432 if (type == BPF_WRITE) {
3433 switch (off) {
f96da094 3434 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
d691f9e8
AS
3435 break;
3436 default:
3437 return false;
3438 }
3439 }
3440
f96da094 3441 return bpf_skb_is_valid_access(off, size, type, info);
d691f9e8
AS
3442}
3443
3a0af8fd
TG
3444static bool lwt_is_valid_access(int off, int size,
3445 enum bpf_access_type type,
23994631 3446 struct bpf_insn_access_aux *info)
3a0af8fd
TG
3447{
3448 switch (off) {
f96da094 3449 case bpf_ctx_range(struct __sk_buff, tc_classid):
8a31db56 3450 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3a0af8fd
TG
3451 return false;
3452 }
3453
3454 if (type == BPF_WRITE) {
3455 switch (off) {
f96da094
DB
3456 case bpf_ctx_range(struct __sk_buff, mark):
3457 case bpf_ctx_range(struct __sk_buff, priority):
3458 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3a0af8fd
TG
3459 break;
3460 default:
3461 return false;
3462 }
3463 }
3464
f96da094
DB
3465 switch (off) {
3466 case bpf_ctx_range(struct __sk_buff, data):
3467 info->reg_type = PTR_TO_PACKET;
3468 break;
3469 case bpf_ctx_range(struct __sk_buff, data_end):
3470 info->reg_type = PTR_TO_PACKET_END;
3471 break;
3472 }
3473
3474 return bpf_skb_is_valid_access(off, size, type, info);
3a0af8fd
TG
3475}
3476
61023658
DA
3477static bool sock_filter_is_valid_access(int off, int size,
3478 enum bpf_access_type type,
23994631 3479 struct bpf_insn_access_aux *info)
61023658
DA
3480{
3481 if (type == BPF_WRITE) {
3482 switch (off) {
3483 case offsetof(struct bpf_sock, bound_dev_if):
482dca93 3484 case offsetof(struct bpf_sock, mark):
482dca93
DA
3485 case offsetof(struct bpf_sock, priority):
3486 break;
61023658
DA
3487 default:
3488 return false;
3489 }
3490 }
3491
3492 if (off < 0 || off + size > sizeof(struct bpf_sock))
3493 return false;
61023658
DA
3494 /* The verifier guarantees that size > 0. */
3495 if (off % size != 0)
3496 return false;
61023658
DA
3497 if (size != sizeof(__u32))
3498 return false;
3499
3500 return true;
3501}
3502
047b0ecd
DB
3503static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
3504 const struct bpf_prog *prog, int drop_verdict)
36bbef52
DB
3505{
3506 struct bpf_insn *insn = insn_buf;
3507
3508 if (!direct_write)
3509 return 0;
3510
3511 /* if (!skb->cloned)
3512 * goto start;
3513 *
3514 * (Fast-path, otherwise approximation that we might be
3515 * a clone, do the rest in helper.)
3516 */
3517 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
3518 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
3519 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
3520
3521 /* ret = bpf_skb_pull_data(skb, 0); */
3522 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
3523 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
3524 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
3525 BPF_FUNC_skb_pull_data);
3526 /* if (!ret)
3527 * goto restore;
3528 * return TC_ACT_SHOT;
3529 */
3530 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
047b0ecd 3531 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
36bbef52
DB
3532 *insn++ = BPF_EXIT_INSN();
3533
3534 /* restore: */
3535 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
3536 /* start: */
3537 *insn++ = prog->insnsi[0];
3538
3539 return insn - insn_buf;
3540}
3541
047b0ecd
DB
3542static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
3543 const struct bpf_prog *prog)
3544{
3545 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
3546}
3547
d691f9e8 3548static bool tc_cls_act_is_valid_access(int off, int size,
19de99f7 3549 enum bpf_access_type type,
23994631 3550 struct bpf_insn_access_aux *info)
d691f9e8
AS
3551{
3552 if (type == BPF_WRITE) {
3553 switch (off) {
f96da094
DB
3554 case bpf_ctx_range(struct __sk_buff, mark):
3555 case bpf_ctx_range(struct __sk_buff, tc_index):
3556 case bpf_ctx_range(struct __sk_buff, priority):
3557 case bpf_ctx_range(struct __sk_buff, tc_classid):
3558 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
d691f9e8
AS
3559 break;
3560 default:
3561 return false;
3562 }
3563 }
19de99f7 3564
f96da094
DB
3565 switch (off) {
3566 case bpf_ctx_range(struct __sk_buff, data):
3567 info->reg_type = PTR_TO_PACKET;
3568 break;
3569 case bpf_ctx_range(struct __sk_buff, data_end):
3570 info->reg_type = PTR_TO_PACKET_END;
3571 break;
8a31db56
JF
3572 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
3573 return false;
f96da094
DB
3574 }
3575
3576 return bpf_skb_is_valid_access(off, size, type, info);
d691f9e8
AS
3577}
3578
1afaf661 3579static bool __is_valid_xdp_access(int off, int size)
6a773a15
BB
3580{
3581 if (off < 0 || off >= sizeof(struct xdp_md))
3582 return false;
3583 if (off % size != 0)
3584 return false;
6088b582 3585 if (size != sizeof(__u32))
6a773a15
BB
3586 return false;
3587
3588 return true;
3589}
3590
3591static bool xdp_is_valid_access(int off, int size,
3592 enum bpf_access_type type,
23994631 3593 struct bpf_insn_access_aux *info)
6a773a15
BB
3594{
3595 if (type == BPF_WRITE)
3596 return false;
3597
3598 switch (off) {
3599 case offsetof(struct xdp_md, data):
23994631 3600 info->reg_type = PTR_TO_PACKET;
6a773a15
BB
3601 break;
3602 case offsetof(struct xdp_md, data_end):
23994631 3603 info->reg_type = PTR_TO_PACKET_END;
6a773a15
BB
3604 break;
3605 }
3606
1afaf661 3607 return __is_valid_xdp_access(off, size);
6a773a15
BB
3608}
3609
3610void bpf_warn_invalid_xdp_action(u32 act)
3611{
3612 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
3613}
3614EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
3615
40304b2a
LB
3616static bool __is_valid_sock_ops_access(int off, int size)
3617{
3618 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
3619 return false;
3620 /* The verifier guarantees that size > 0. */
3621 if (off % size != 0)
3622 return false;
3623 if (size != sizeof(__u32))
3624 return false;
3625
3626 return true;
3627}
3628
3629static bool sock_ops_is_valid_access(int off, int size,
3630 enum bpf_access_type type,
3631 struct bpf_insn_access_aux *info)
3632{
3633 if (type == BPF_WRITE) {
3634 switch (off) {
3635 case offsetof(struct bpf_sock_ops, op) ...
3636 offsetof(struct bpf_sock_ops, replylong[3]):
3637 break;
3638 default:
3639 return false;
3640 }
3641 }
3642
3643 return __is_valid_sock_ops_access(off, size);
3644}
3645
8a31db56
JF
3646static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
3647 const struct bpf_prog *prog)
3648{
047b0ecd 3649 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
8a31db56
JF
3650}
3651
b005fd18
JF
3652static bool sk_skb_is_valid_access(int off, int size,
3653 enum bpf_access_type type,
3654 struct bpf_insn_access_aux *info)
3655{
8a31db56
JF
3656 if (type == BPF_WRITE) {
3657 switch (off) {
3658 case bpf_ctx_range(struct __sk_buff, mark):
3659 case bpf_ctx_range(struct __sk_buff, tc_index):
3660 case bpf_ctx_range(struct __sk_buff, priority):
3661 break;
3662 default:
3663 return false;
3664 }
3665 }
3666
b005fd18 3667 switch (off) {
8a31db56
JF
3668 case bpf_ctx_range(struct __sk_buff, tc_classid):
3669 return false;
b005fd18
JF
3670 case bpf_ctx_range(struct __sk_buff, data):
3671 info->reg_type = PTR_TO_PACKET;
3672 break;
3673 case bpf_ctx_range(struct __sk_buff, data_end):
3674 info->reg_type = PTR_TO_PACKET_END;
3675 break;
3676 }
3677
3678 return bpf_skb_is_valid_access(off, size, type, info);
3679}
3680
2492d3b8
DB
3681static u32 bpf_convert_ctx_access(enum bpf_access_type type,
3682 const struct bpf_insn *si,
3683 struct bpf_insn *insn_buf,
f96da094 3684 struct bpf_prog *prog, u32 *target_size)
9bac3d6d
AS
3685{
3686 struct bpf_insn *insn = insn_buf;
6b8cc1d1 3687 int off;
9bac3d6d 3688
6b8cc1d1 3689 switch (si->off) {
9bac3d6d 3690 case offsetof(struct __sk_buff, len):
6b8cc1d1 3691 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3692 bpf_target_off(struct sk_buff, len, 4,
3693 target_size));
9bac3d6d
AS
3694 break;
3695
0b8c707d 3696 case offsetof(struct __sk_buff, protocol):
6b8cc1d1 3697 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
3698 bpf_target_off(struct sk_buff, protocol, 2,
3699 target_size));
0b8c707d
DB
3700 break;
3701
27cd5452 3702 case offsetof(struct __sk_buff, vlan_proto):
6b8cc1d1 3703 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
3704 bpf_target_off(struct sk_buff, vlan_proto, 2,
3705 target_size));
27cd5452
MS
3706 break;
3707
bcad5718 3708 case offsetof(struct __sk_buff, priority):
754f1e6a 3709 if (type == BPF_WRITE)
6b8cc1d1 3710 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3711 bpf_target_off(struct sk_buff, priority, 4,
3712 target_size));
754f1e6a 3713 else
6b8cc1d1 3714 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3715 bpf_target_off(struct sk_buff, priority, 4,
3716 target_size));
bcad5718
DB
3717 break;
3718
37e82c2f 3719 case offsetof(struct __sk_buff, ingress_ifindex):
6b8cc1d1 3720 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3721 bpf_target_off(struct sk_buff, skb_iif, 4,
3722 target_size));
37e82c2f
AS
3723 break;
3724
3725 case offsetof(struct __sk_buff, ifindex):
f035a515 3726 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 3727 si->dst_reg, si->src_reg,
37e82c2f 3728 offsetof(struct sk_buff, dev));
6b8cc1d1
DB
3729 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
3730 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
3731 bpf_target_off(struct net_device, ifindex, 4,
3732 target_size));
37e82c2f
AS
3733 break;
3734
ba7591d8 3735 case offsetof(struct __sk_buff, hash):
6b8cc1d1 3736 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3737 bpf_target_off(struct sk_buff, hash, 4,
3738 target_size));
ba7591d8
DB
3739 break;
3740
9bac3d6d 3741 case offsetof(struct __sk_buff, mark):
d691f9e8 3742 if (type == BPF_WRITE)
6b8cc1d1 3743 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3744 bpf_target_off(struct sk_buff, mark, 4,
3745 target_size));
d691f9e8 3746 else
6b8cc1d1 3747 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3748 bpf_target_off(struct sk_buff, mark, 4,
3749 target_size));
d691f9e8 3750 break;
9bac3d6d
AS
3751
3752 case offsetof(struct __sk_buff, pkt_type):
f96da094
DB
3753 *target_size = 1;
3754 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
3755 PKT_TYPE_OFFSET());
3756 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
3757#ifdef __BIG_ENDIAN_BITFIELD
3758 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
3759#endif
3760 break;
9bac3d6d
AS
3761
3762 case offsetof(struct __sk_buff, queue_mapping):
f96da094
DB
3763 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3764 bpf_target_off(struct sk_buff, queue_mapping, 2,
3765 target_size));
3766 break;
c2497395 3767
c2497395 3768 case offsetof(struct __sk_buff, vlan_present):
c2497395 3769 case offsetof(struct __sk_buff, vlan_tci):
f96da094
DB
3770 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
3771
3772 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3773 bpf_target_off(struct sk_buff, vlan_tci, 2,
3774 target_size));
3775 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
3776 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
3777 ~VLAN_TAG_PRESENT);
3778 } else {
3779 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
3780 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
3781 }
3782 break;
d691f9e8
AS
3783
3784 case offsetof(struct __sk_buff, cb[0]) ...
f96da094 3785 offsetofend(struct __sk_buff, cb[4]) - 1:
d691f9e8 3786 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
62c7989b
DB
3787 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
3788 offsetof(struct qdisc_skb_cb, data)) %
3789 sizeof(__u64));
d691f9e8 3790
ff936a04 3791 prog->cb_access = 1;
6b8cc1d1
DB
3792 off = si->off;
3793 off -= offsetof(struct __sk_buff, cb[0]);
3794 off += offsetof(struct sk_buff, cb);
3795 off += offsetof(struct qdisc_skb_cb, data);
d691f9e8 3796 if (type == BPF_WRITE)
62c7989b 3797 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 3798 si->src_reg, off);
d691f9e8 3799 else
62c7989b 3800 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 3801 si->src_reg, off);
d691f9e8
AS
3802 break;
3803
045efa82 3804 case offsetof(struct __sk_buff, tc_classid):
6b8cc1d1
DB
3805 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
3806
3807 off = si->off;
3808 off -= offsetof(struct __sk_buff, tc_classid);
3809 off += offsetof(struct sk_buff, cb);
3810 off += offsetof(struct qdisc_skb_cb, tc_classid);
f96da094 3811 *target_size = 2;
09c37a2c 3812 if (type == BPF_WRITE)
6b8cc1d1
DB
3813 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
3814 si->src_reg, off);
09c37a2c 3815 else
6b8cc1d1
DB
3816 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
3817 si->src_reg, off);
045efa82
DB
3818 break;
3819
db58ba45 3820 case offsetof(struct __sk_buff, data):
f035a515 3821 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
6b8cc1d1 3822 si->dst_reg, si->src_reg,
db58ba45
AS
3823 offsetof(struct sk_buff, data));
3824 break;
3825
3826 case offsetof(struct __sk_buff, data_end):
6b8cc1d1
DB
3827 off = si->off;
3828 off -= offsetof(struct __sk_buff, data_end);
3829 off += offsetof(struct sk_buff, cb);
3830 off += offsetof(struct bpf_skb_data_end, data_end);
3831 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
3832 si->src_reg, off);
db58ba45
AS
3833 break;
3834
d691f9e8
AS
3835 case offsetof(struct __sk_buff, tc_index):
3836#ifdef CONFIG_NET_SCHED
d691f9e8 3837 if (type == BPF_WRITE)
6b8cc1d1 3838 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
3839 bpf_target_off(struct sk_buff, tc_index, 2,
3840 target_size));
d691f9e8 3841 else
6b8cc1d1 3842 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
3843 bpf_target_off(struct sk_buff, tc_index, 2,
3844 target_size));
d691f9e8 3845#else
2ed46ce4 3846 *target_size = 2;
d691f9e8 3847 if (type == BPF_WRITE)
6b8cc1d1 3848 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
d691f9e8 3849 else
6b8cc1d1 3850 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
b1d9fc41
DB
3851#endif
3852 break;
3853
3854 case offsetof(struct __sk_buff, napi_id):
3855#if defined(CONFIG_NET_RX_BUSY_POLL)
b1d9fc41 3856 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
3857 bpf_target_off(struct sk_buff, napi_id, 4,
3858 target_size));
b1d9fc41
DB
3859 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
3860 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3861#else
2ed46ce4 3862 *target_size = 4;
b1d9fc41 3863 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
d691f9e8 3864#endif
6b8cc1d1 3865 break;
8a31db56
JF
3866 case offsetof(struct __sk_buff, family):
3867 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
3868
3869 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3870 si->dst_reg, si->src_reg,
3871 offsetof(struct sk_buff, sk));
3872 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3873 bpf_target_off(struct sock_common,
3874 skc_family,
3875 2, target_size));
3876 break;
3877 case offsetof(struct __sk_buff, remote_ip4):
3878 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
3879
3880 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3881 si->dst_reg, si->src_reg,
3882 offsetof(struct sk_buff, sk));
3883 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3884 bpf_target_off(struct sock_common,
3885 skc_daddr,
3886 4, target_size));
3887 break;
3888 case offsetof(struct __sk_buff, local_ip4):
3889 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3890 skc_rcv_saddr) != 4);
3891
3892 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3893 si->dst_reg, si->src_reg,
3894 offsetof(struct sk_buff, sk));
3895 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3896 bpf_target_off(struct sock_common,
3897 skc_rcv_saddr,
3898 4, target_size));
3899 break;
3900 case offsetof(struct __sk_buff, remote_ip6[0]) ...
3901 offsetof(struct __sk_buff, remote_ip6[3]):
3902#if IS_ENABLED(CONFIG_IPV6)
3903 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3904 skc_v6_daddr.s6_addr32[0]) != 4);
3905
3906 off = si->off;
3907 off -= offsetof(struct __sk_buff, remote_ip6[0]);
3908
3909 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3910 si->dst_reg, si->src_reg,
3911 offsetof(struct sk_buff, sk));
3912 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3913 offsetof(struct sock_common,
3914 skc_v6_daddr.s6_addr32[0]) +
3915 off);
3916#else
3917 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3918#endif
3919 break;
3920 case offsetof(struct __sk_buff, local_ip6[0]) ...
3921 offsetof(struct __sk_buff, local_ip6[3]):
3922#if IS_ENABLED(CONFIG_IPV6)
3923 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3924 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
3925
3926 off = si->off;
3927 off -= offsetof(struct __sk_buff, local_ip6[0]);
3928
3929 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3930 si->dst_reg, si->src_reg,
3931 offsetof(struct sk_buff, sk));
3932 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3933 offsetof(struct sock_common,
3934 skc_v6_rcv_saddr.s6_addr32[0]) +
3935 off);
3936#else
3937 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3938#endif
3939 break;
3940
3941 case offsetof(struct __sk_buff, remote_port):
3942 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
3943
3944 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3945 si->dst_reg, si->src_reg,
3946 offsetof(struct sk_buff, sk));
3947 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3948 bpf_target_off(struct sock_common,
3949 skc_dport,
3950 2, target_size));
3951#ifndef __BIG_ENDIAN_BITFIELD
3952 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
3953#endif
3954 break;
3955
3956 case offsetof(struct __sk_buff, local_port):
3957 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
3958
3959 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
3960 si->dst_reg, si->src_reg,
3961 offsetof(struct sk_buff, sk));
3962 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3963 bpf_target_off(struct sock_common,
3964 skc_num, 2, target_size));
3965 break;
9bac3d6d
AS
3966 }
3967
3968 return insn - insn_buf;
89aa0758
AS
3969}
3970
61023658 3971static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
6b8cc1d1 3972 const struct bpf_insn *si,
61023658 3973 struct bpf_insn *insn_buf,
f96da094 3974 struct bpf_prog *prog, u32 *target_size)
61023658
DA
3975{
3976 struct bpf_insn *insn = insn_buf;
3977
6b8cc1d1 3978 switch (si->off) {
61023658
DA
3979 case offsetof(struct bpf_sock, bound_dev_if):
3980 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
3981
3982 if (type == BPF_WRITE)
6b8cc1d1 3983 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
3984 offsetof(struct sock, sk_bound_dev_if));
3985 else
6b8cc1d1 3986 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
3987 offsetof(struct sock, sk_bound_dev_if));
3988 break;
aa4c1037 3989
482dca93
DA
3990 case offsetof(struct bpf_sock, mark):
3991 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
3992
3993 if (type == BPF_WRITE)
3994 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3995 offsetof(struct sock, sk_mark));
3996 else
3997 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3998 offsetof(struct sock, sk_mark));
3999 break;
4000
4001 case offsetof(struct bpf_sock, priority):
4002 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
4003
4004 if (type == BPF_WRITE)
4005 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4006 offsetof(struct sock, sk_priority));
4007 else
4008 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4009 offsetof(struct sock, sk_priority));
4010 break;
4011
aa4c1037
DA
4012 case offsetof(struct bpf_sock, family):
4013 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
4014
6b8cc1d1 4015 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
aa4c1037
DA
4016 offsetof(struct sock, sk_family));
4017 break;
4018
4019 case offsetof(struct bpf_sock, type):
6b8cc1d1 4020 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
aa4c1037 4021 offsetof(struct sock, __sk_flags_offset));
6b8cc1d1
DB
4022 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
4023 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
aa4c1037
DA
4024 break;
4025
4026 case offsetof(struct bpf_sock, protocol):
6b8cc1d1 4027 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
aa4c1037 4028 offsetof(struct sock, __sk_flags_offset));
6b8cc1d1
DB
4029 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
4030 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
aa4c1037 4031 break;
61023658
DA
4032 }
4033
4034 return insn - insn_buf;
4035}
4036
6b8cc1d1
DB
4037static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
4038 const struct bpf_insn *si,
374fb54e 4039 struct bpf_insn *insn_buf,
f96da094 4040 struct bpf_prog *prog, u32 *target_size)
374fb54e
DB
4041{
4042 struct bpf_insn *insn = insn_buf;
4043
6b8cc1d1 4044 switch (si->off) {
374fb54e 4045 case offsetof(struct __sk_buff, ifindex):
374fb54e 4046 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 4047 si->dst_reg, si->src_reg,
374fb54e 4048 offsetof(struct sk_buff, dev));
6b8cc1d1 4049 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
4050 bpf_target_off(struct net_device, ifindex, 4,
4051 target_size));
374fb54e
DB
4052 break;
4053 default:
f96da094
DB
4054 return bpf_convert_ctx_access(type, si, insn_buf, prog,
4055 target_size);
374fb54e
DB
4056 }
4057
4058 return insn - insn_buf;
4059}
4060
6b8cc1d1
DB
4061static u32 xdp_convert_ctx_access(enum bpf_access_type type,
4062 const struct bpf_insn *si,
6a773a15 4063 struct bpf_insn *insn_buf,
f96da094 4064 struct bpf_prog *prog, u32 *target_size)
6a773a15
BB
4065{
4066 struct bpf_insn *insn = insn_buf;
4067
6b8cc1d1 4068 switch (si->off) {
6a773a15 4069 case offsetof(struct xdp_md, data):
f035a515 4070 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6b8cc1d1 4071 si->dst_reg, si->src_reg,
6a773a15
BB
4072 offsetof(struct xdp_buff, data));
4073 break;
4074 case offsetof(struct xdp_md, data_end):
f035a515 4075 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6b8cc1d1 4076 si->dst_reg, si->src_reg,
6a773a15
BB
4077 offsetof(struct xdp_buff, data_end));
4078 break;
4079 }
4080
4081 return insn - insn_buf;
4082}
4083
40304b2a
LB
4084static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
4085 const struct bpf_insn *si,
4086 struct bpf_insn *insn_buf,
f96da094
DB
4087 struct bpf_prog *prog,
4088 u32 *target_size)
40304b2a
LB
4089{
4090 struct bpf_insn *insn = insn_buf;
4091 int off;
4092
4093 switch (si->off) {
4094 case offsetof(struct bpf_sock_ops, op) ...
4095 offsetof(struct bpf_sock_ops, replylong[3]):
4096 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
4097 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
4098 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
4099 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
4100 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
4101 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
4102 off = si->off;
4103 off -= offsetof(struct bpf_sock_ops, op);
4104 off += offsetof(struct bpf_sock_ops_kern, op);
4105 if (type == BPF_WRITE)
4106 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
4107 off);
4108 else
4109 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
4110 off);
4111 break;
4112
4113 case offsetof(struct bpf_sock_ops, family):
4114 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
4115
4116 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4117 struct bpf_sock_ops_kern, sk),
4118 si->dst_reg, si->src_reg,
4119 offsetof(struct bpf_sock_ops_kern, sk));
4120 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4121 offsetof(struct sock_common, skc_family));
4122 break;
4123
4124 case offsetof(struct bpf_sock_ops, remote_ip4):
4125 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
4126
4127 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4128 struct bpf_sock_ops_kern, sk),
4129 si->dst_reg, si->src_reg,
4130 offsetof(struct bpf_sock_ops_kern, sk));
4131 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4132 offsetof(struct sock_common, skc_daddr));
4133 break;
4134
4135 case offsetof(struct bpf_sock_ops, local_ip4):
4136 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
4137
4138 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4139 struct bpf_sock_ops_kern, sk),
4140 si->dst_reg, si->src_reg,
4141 offsetof(struct bpf_sock_ops_kern, sk));
4142 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4143 offsetof(struct sock_common,
4144 skc_rcv_saddr));
4145 break;
4146
4147 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
4148 offsetof(struct bpf_sock_ops, remote_ip6[3]):
4149#if IS_ENABLED(CONFIG_IPV6)
4150 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4151 skc_v6_daddr.s6_addr32[0]) != 4);
4152
4153 off = si->off;
4154 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
4155 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4156 struct bpf_sock_ops_kern, sk),
4157 si->dst_reg, si->src_reg,
4158 offsetof(struct bpf_sock_ops_kern, sk));
4159 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4160 offsetof(struct sock_common,
4161 skc_v6_daddr.s6_addr32[0]) +
4162 off);
4163#else
4164 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4165#endif
4166 break;
4167
4168 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
4169 offsetof(struct bpf_sock_ops, local_ip6[3]):
4170#if IS_ENABLED(CONFIG_IPV6)
4171 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
4172 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
4173
4174 off = si->off;
4175 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
4176 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4177 struct bpf_sock_ops_kern, sk),
4178 si->dst_reg, si->src_reg,
4179 offsetof(struct bpf_sock_ops_kern, sk));
4180 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
4181 offsetof(struct sock_common,
4182 skc_v6_rcv_saddr.s6_addr32[0]) +
4183 off);
4184#else
4185 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
4186#endif
4187 break;
4188
4189 case offsetof(struct bpf_sock_ops, remote_port):
4190 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
4191
4192 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4193 struct bpf_sock_ops_kern, sk),
4194 si->dst_reg, si->src_reg,
4195 offsetof(struct bpf_sock_ops_kern, sk));
4196 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4197 offsetof(struct sock_common, skc_dport));
4198#ifndef __BIG_ENDIAN_BITFIELD
4199 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
4200#endif
4201 break;
4202
4203 case offsetof(struct bpf_sock_ops, local_port):
4204 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
4205
4206 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
4207 struct bpf_sock_ops_kern, sk),
4208 si->dst_reg, si->src_reg,
4209 offsetof(struct bpf_sock_ops_kern, sk));
4210 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
4211 offsetof(struct sock_common, skc_num));
4212 break;
4213 }
4214 return insn - insn_buf;
4215}
4216
be9370a7 4217const struct bpf_verifier_ops sk_filter_prog_ops = {
4936e352
DB
4218 .get_func_proto = sk_filter_func_proto,
4219 .is_valid_access = sk_filter_is_valid_access,
2492d3b8 4220 .convert_ctx_access = bpf_convert_ctx_access,
89aa0758
AS
4221};
4222
be9370a7 4223const struct bpf_verifier_ops tc_cls_act_prog_ops = {
4936e352
DB
4224 .get_func_proto = tc_cls_act_func_proto,
4225 .is_valid_access = tc_cls_act_is_valid_access,
374fb54e 4226 .convert_ctx_access = tc_cls_act_convert_ctx_access,
36bbef52 4227 .gen_prologue = tc_cls_act_prologue,
1cf1cae9 4228 .test_run = bpf_prog_test_run_skb,
608cd71a
AS
4229};
4230
be9370a7 4231const struct bpf_verifier_ops xdp_prog_ops = {
6a773a15
BB
4232 .get_func_proto = xdp_func_proto,
4233 .is_valid_access = xdp_is_valid_access,
4234 .convert_ctx_access = xdp_convert_ctx_access,
1cf1cae9 4235 .test_run = bpf_prog_test_run_xdp,
6a773a15
BB
4236};
4237
be9370a7 4238const struct bpf_verifier_ops cg_skb_prog_ops = {
966789fb 4239 .get_func_proto = sk_filter_func_proto,
0e33661d 4240 .is_valid_access = sk_filter_is_valid_access,
2492d3b8 4241 .convert_ctx_access = bpf_convert_ctx_access,
1cf1cae9 4242 .test_run = bpf_prog_test_run_skb,
0e33661d
DM
4243};
4244
be9370a7 4245const struct bpf_verifier_ops lwt_inout_prog_ops = {
3a0af8fd
TG
4246 .get_func_proto = lwt_inout_func_proto,
4247 .is_valid_access = lwt_is_valid_access,
2492d3b8 4248 .convert_ctx_access = bpf_convert_ctx_access,
1cf1cae9 4249 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
4250};
4251
be9370a7 4252const struct bpf_verifier_ops lwt_xmit_prog_ops = {
3a0af8fd
TG
4253 .get_func_proto = lwt_xmit_func_proto,
4254 .is_valid_access = lwt_is_valid_access,
2492d3b8 4255 .convert_ctx_access = bpf_convert_ctx_access,
3a0af8fd 4256 .gen_prologue = tc_cls_act_prologue,
1cf1cae9 4257 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
4258};
4259
be9370a7 4260const struct bpf_verifier_ops cg_sock_prog_ops = {
ae2cf1c4 4261 .get_func_proto = sock_filter_func_proto,
61023658
DA
4262 .is_valid_access = sock_filter_is_valid_access,
4263 .convert_ctx_access = sock_filter_convert_ctx_access,
4264};
4265
40304b2a 4266const struct bpf_verifier_ops sock_ops_prog_ops = {
8c4b4c7e 4267 .get_func_proto = sock_ops_func_proto,
40304b2a
LB
4268 .is_valid_access = sock_ops_is_valid_access,
4269 .convert_ctx_access = sock_ops_convert_ctx_access,
4270};
4271
b005fd18
JF
4272const struct bpf_verifier_ops sk_skb_prog_ops = {
4273 .get_func_proto = sk_skb_func_proto,
4274 .is_valid_access = sk_skb_is_valid_access,
4275 .convert_ctx_access = bpf_convert_ctx_access,
8a31db56 4276 .gen_prologue = sk_skb_prologue,
b005fd18
JF
4277};
4278
8ced425e 4279int sk_detach_filter(struct sock *sk)
55b33325
PE
4280{
4281 int ret = -ENOENT;
4282 struct sk_filter *filter;
4283
d59577b6
VB
4284 if (sock_flag(sk, SOCK_FILTER_LOCKED))
4285 return -EPERM;
4286
8ced425e
HFS
4287 filter = rcu_dereference_protected(sk->sk_filter,
4288 lockdep_sock_is_held(sk));
55b33325 4289 if (filter) {
a9b3cd7f 4290 RCU_INIT_POINTER(sk->sk_filter, NULL);
46bcf14f 4291 sk_filter_uncharge(sk, filter);
55b33325
PE
4292 ret = 0;
4293 }
a3ea269b 4294
55b33325
PE
4295 return ret;
4296}
8ced425e 4297EXPORT_SYMBOL_GPL(sk_detach_filter);
a8fc9277 4298
a3ea269b
DB
4299int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
4300 unsigned int len)
a8fc9277 4301{
a3ea269b 4302 struct sock_fprog_kern *fprog;
a8fc9277 4303 struct sk_filter *filter;
a3ea269b 4304 int ret = 0;
a8fc9277
PE
4305
4306 lock_sock(sk);
4307 filter = rcu_dereference_protected(sk->sk_filter,
8ced425e 4308 lockdep_sock_is_held(sk));
a8fc9277
PE
4309 if (!filter)
4310 goto out;
a3ea269b
DB
4311
4312 /* We're copying the filter that has been originally attached,
93d08b69
DB
4313 * so no conversion/decode needed anymore. eBPF programs that
4314 * have no original program cannot be dumped through this.
a3ea269b 4315 */
93d08b69 4316 ret = -EACCES;
7ae457c1 4317 fprog = filter->prog->orig_prog;
93d08b69
DB
4318 if (!fprog)
4319 goto out;
a3ea269b
DB
4320
4321 ret = fprog->len;
a8fc9277 4322 if (!len)
a3ea269b 4323 /* User space only enquires number of filter blocks. */
a8fc9277 4324 goto out;
a3ea269b 4325
a8fc9277 4326 ret = -EINVAL;
a3ea269b 4327 if (len < fprog->len)
a8fc9277
PE
4328 goto out;
4329
4330 ret = -EFAULT;
009937e7 4331 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
a3ea269b 4332 goto out;
a8fc9277 4333
a3ea269b
DB
4334 /* Instead of bytes, the API requests to return the number
4335 * of filter blocks.
4336 */
4337 ret = fprog->len;
a8fc9277
PE
4338out:
4339 release_sock(sk);
4340 return ret;
4341}