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