]> git.ipfire.org Git - thirdparty/linux.git/blame - net/core/filter.c
bpf: fix build error in libbpf with EXTRA_CFLAGS="-Wp, -D_FORTIFY_SOURCE=2 -O2"
[thirdparty/linux.git] / net / core / filter.c
CommitLineData
1da177e4
LT
1/*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
bd4cf0ed
AS
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
1da177e4 6 *
bd4cf0ed
AS
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
8 *
9 * Authors:
10 *
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
1da177e4
LT
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 * Andi Kleen - Fix a few bad bugs and races.
4df95ff4 21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
1da177e4
LT
22 */
23
24#include <linux/module.h>
25#include <linux/types.h>
1da177e4
LT
26#include <linux/mm.h>
27#include <linux/fcntl.h>
28#include <linux/socket.h>
91b8270f 29#include <linux/sock_diag.h>
1da177e4
LT
30#include <linux/in.h>
31#include <linux/inet.h>
32#include <linux/netdevice.h>
33#include <linux/if_packet.h>
c491680f 34#include <linux/if_arp.h>
5a0e3ad6 35#include <linux/gfp.h>
d74bad4e 36#include <net/inet_common.h>
1da177e4
LT
37#include <net/ip.h>
38#include <net/protocol.h>
4738c1db 39#include <net/netlink.h>
1da177e4
LT
40#include <linux/skbuff.h>
41#include <net/sock.h>
10b89ee4 42#include <net/flow_dissector.h>
1da177e4
LT
43#include <linux/errno.h>
44#include <linux/timer.h>
7c0f6ba6 45#include <linux/uaccess.h>
40daafc8 46#include <asm/unaligned.h>
d66f2b91 47#include <asm/cmpxchg.h>
1da177e4 48#include <linux/filter.h>
86e4ca66 49#include <linux/ratelimit.h>
46b325c7 50#include <linux/seccomp.h>
f3335031 51#include <linux/if_vlan.h>
89aa0758 52#include <linux/bpf.h>
d691f9e8 53#include <net/sch_generic.h>
8d20aabe 54#include <net/cls_cgroup.h>
d3aa45ce 55#include <net/dst_metadata.h>
c46646d0 56#include <net/dst.h>
538950a1 57#include <net/sock_reuseport.h>
b1d9fc41 58#include <net/busy_poll.h>
8c4b4c7e 59#include <net/tcp.h>
12bed760 60#include <net/xfrm.h>
5acaee0a 61#include <linux/bpf_trace.h>
02671e23 62#include <net/xdp_sock.h>
87f5fc7e
DA
63#include <linux/inetdevice.h>
64#include <net/ip_fib.h>
65#include <net/flow.h>
66#include <net/arp.h>
fe94cc29
MX
67#include <net/ipv6.h>
68#include <linux/seg6_local.h>
69#include <net/seg6.h>
70#include <net/seg6_local.h>
1da177e4 71
43db6d65 72/**
f4979fce 73 * sk_filter_trim_cap - run a packet through a socket filter
43db6d65
SH
74 * @sk: sock associated with &sk_buff
75 * @skb: buffer to filter
f4979fce 76 * @cap: limit on how short the eBPF program may trim the packet
43db6d65 77 *
ff936a04
AS
78 * Run the eBPF program and then cut skb->data to correct size returned by
79 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
43db6d65 80 * than pkt_len we keep whole skb->data. This is the socket level
ff936a04 81 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
43db6d65
SH
82 * be accepted or -EPERM if the packet should be tossed.
83 *
84 */
f4979fce 85int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
43db6d65
SH
86{
87 int err;
88 struct sk_filter *filter;
89
c93bdd0e
MG
90 /*
91 * If the skb was allocated from pfmemalloc reserves, only
92 * allow SOCK_MEMALLOC sockets to use it as this socket is
93 * helping free memory
94 */
8fe809a9
ED
95 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
96 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
c93bdd0e 97 return -ENOMEM;
8fe809a9 98 }
c11cd3a6
DM
99 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
100 if (err)
101 return err;
102
43db6d65
SH
103 err = security_sock_rcv_skb(sk, skb);
104 if (err)
105 return err;
106
80f8f102
ED
107 rcu_read_lock();
108 filter = rcu_dereference(sk->sk_filter);
43db6d65 109 if (filter) {
8f917bba
WB
110 struct sock *save_sk = skb->sk;
111 unsigned int pkt_len;
112
113 skb->sk = sk;
114 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
8f917bba 115 skb->sk = save_sk;
d1f496fd 116 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
43db6d65 117 }
80f8f102 118 rcu_read_unlock();
43db6d65
SH
119
120 return err;
121}
f4979fce 122EXPORT_SYMBOL(sk_filter_trim_cap);
43db6d65 123
b390134c 124BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
bd4cf0ed 125{
f3694e00 126 return skb_get_poff(skb);
bd4cf0ed
AS
127}
128
b390134c 129BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
bd4cf0ed 130{
bd4cf0ed
AS
131 struct nlattr *nla;
132
133 if (skb_is_nonlinear(skb))
134 return 0;
135
05ab8f26
MK
136 if (skb->len < sizeof(struct nlattr))
137 return 0;
138
30743837 139 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
140 return 0;
141
30743837 142 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
bd4cf0ed
AS
143 if (nla)
144 return (void *) nla - (void *) skb->data;
145
146 return 0;
147}
148
b390134c 149BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
bd4cf0ed 150{
bd4cf0ed
AS
151 struct nlattr *nla;
152
153 if (skb_is_nonlinear(skb))
154 return 0;
155
05ab8f26
MK
156 if (skb->len < sizeof(struct nlattr))
157 return 0;
158
30743837 159 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
160 return 0;
161
30743837
DB
162 nla = (struct nlattr *) &skb->data[a];
163 if (nla->nla_len > skb->len - a)
bd4cf0ed
AS
164 return 0;
165
30743837 166 nla = nla_find_nested(nla, x);
bd4cf0ed
AS
167 if (nla)
168 return (void *) nla - (void *) skb->data;
169
170 return 0;
171}
172
e0cea7ce
DB
173BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
174 data, int, headlen, int, offset)
175{
176 u8 tmp, *ptr;
177 const int len = sizeof(tmp);
178
179 if (offset >= 0) {
180 if (headlen - offset >= len)
181 return *(u8 *)(data + offset);
182 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
183 return tmp;
184 } else {
185 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
186 if (likely(ptr))
187 return *(u8 *)ptr;
188 }
189
190 return -EFAULT;
191}
192
193BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
194 int, offset)
195{
196 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
197 offset);
198}
199
200BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
201 data, int, headlen, int, offset)
202{
203 u16 tmp, *ptr;
204 const int len = sizeof(tmp);
205
206 if (offset >= 0) {
207 if (headlen - offset >= len)
208 return get_unaligned_be16(data + offset);
209 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
210 return be16_to_cpu(tmp);
211 } else {
212 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
213 if (likely(ptr))
214 return get_unaligned_be16(ptr);
215 }
216
217 return -EFAULT;
218}
219
220BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
221 int, offset)
222{
223 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
224 offset);
225}
226
227BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
228 data, int, headlen, int, offset)
229{
230 u32 tmp, *ptr;
231 const int len = sizeof(tmp);
232
233 if (likely(offset >= 0)) {
234 if (headlen - offset >= len)
235 return get_unaligned_be32(data + offset);
236 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
237 return be32_to_cpu(tmp);
238 } else {
239 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
240 if (likely(ptr))
241 return get_unaligned_be32(ptr);
242 }
243
244 return -EFAULT;
245}
246
247BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
248 int, offset)
249{
250 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
251 offset);
252}
253
b390134c 254BPF_CALL_0(bpf_get_raw_cpu_id)
bd4cf0ed
AS
255{
256 return raw_smp_processor_id();
257}
258
80b48c44 259static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
b390134c 260 .func = bpf_get_raw_cpu_id,
80b48c44
DB
261 .gpl_only = false,
262 .ret_type = RET_INTEGER,
263};
264
9bac3d6d
AS
265static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
266 struct bpf_insn *insn_buf)
267{
268 struct bpf_insn *insn = insn_buf;
269
270 switch (skb_field) {
271 case SKF_AD_MARK:
272 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
273
274 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
275 offsetof(struct sk_buff, mark));
276 break;
277
278 case SKF_AD_PKTTYPE:
279 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
280 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
281#ifdef __BIG_ENDIAN_BITFIELD
282 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
283#endif
284 break;
285
286 case SKF_AD_QUEUE:
287 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
288
289 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
290 offsetof(struct sk_buff, queue_mapping));
291 break;
c2497395 292
c2497395
AS
293 case SKF_AD_VLAN_TAG:
294 case SKF_AD_VLAN_TAG_PRESENT:
295 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
296 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
297
298 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
299 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
300 offsetof(struct sk_buff, vlan_tci));
301 if (skb_field == SKF_AD_VLAN_TAG) {
302 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
303 ~VLAN_TAG_PRESENT);
304 } else {
305 /* dst_reg >>= 12 */
306 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
307 /* dst_reg &= 1 */
308 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
309 }
310 break;
9bac3d6d
AS
311 }
312
313 return insn - insn_buf;
314}
315
bd4cf0ed 316static bool convert_bpf_extensions(struct sock_filter *fp,
2695fb55 317 struct bpf_insn **insnp)
bd4cf0ed 318{
2695fb55 319 struct bpf_insn *insn = *insnp;
9bac3d6d 320 u32 cnt;
bd4cf0ed
AS
321
322 switch (fp->k) {
323 case SKF_AD_OFF + SKF_AD_PROTOCOL:
0b8c707d
DB
324 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
325
326 /* A = *(u16 *) (CTX + offsetof(protocol)) */
327 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
328 offsetof(struct sk_buff, protocol));
329 /* A = ntohs(A) [emitting a nop or swap16] */
330 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
bd4cf0ed
AS
331 break;
332
333 case SKF_AD_OFF + SKF_AD_PKTTYPE:
9bac3d6d
AS
334 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
335 insn += cnt - 1;
bd4cf0ed
AS
336 break;
337
338 case SKF_AD_OFF + SKF_AD_IFINDEX:
339 case SKF_AD_OFF + SKF_AD_HATYPE:
bd4cf0ed
AS
340 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
341 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
f8f6d679 342
f035a515 343 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
f8f6d679
DB
344 BPF_REG_TMP, BPF_REG_CTX,
345 offsetof(struct sk_buff, dev));
346 /* if (tmp != 0) goto pc + 1 */
347 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
348 *insn++ = BPF_EXIT_INSN();
349 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
350 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
351 offsetof(struct net_device, ifindex));
352 else
353 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
354 offsetof(struct net_device, type));
bd4cf0ed
AS
355 break;
356
357 case SKF_AD_OFF + SKF_AD_MARK:
9bac3d6d
AS
358 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
359 insn += cnt - 1;
bd4cf0ed
AS
360 break;
361
362 case SKF_AD_OFF + SKF_AD_RXHASH:
363 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
364
9739eef1
AS
365 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
366 offsetof(struct sk_buff, hash));
bd4cf0ed
AS
367 break;
368
369 case SKF_AD_OFF + SKF_AD_QUEUE:
9bac3d6d
AS
370 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
371 insn += cnt - 1;
bd4cf0ed
AS
372 break;
373
374 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
c2497395
AS
375 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
376 BPF_REG_A, BPF_REG_CTX, insn);
377 insn += cnt - 1;
378 break;
bd4cf0ed 379
c2497395
AS
380 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
381 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
382 BPF_REG_A, BPF_REG_CTX, insn);
383 insn += cnt - 1;
bd4cf0ed
AS
384 break;
385
27cd5452
MS
386 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
387 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
388
389 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
390 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
391 offsetof(struct sk_buff, vlan_proto));
392 /* A = ntohs(A) [emitting a nop or swap16] */
393 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
394 break;
395
bd4cf0ed
AS
396 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
397 case SKF_AD_OFF + SKF_AD_NLATTR:
398 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
399 case SKF_AD_OFF + SKF_AD_CPU:
4cd3675e 400 case SKF_AD_OFF + SKF_AD_RANDOM:
e430f34e 401 /* arg1 = CTX */
f8f6d679 402 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
bd4cf0ed 403 /* arg2 = A */
f8f6d679 404 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
bd4cf0ed 405 /* arg3 = X */
f8f6d679 406 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
e430f34e 407 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
bd4cf0ed
AS
408 switch (fp->k) {
409 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
b390134c 410 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
bd4cf0ed
AS
411 break;
412 case SKF_AD_OFF + SKF_AD_NLATTR:
b390134c 413 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
bd4cf0ed
AS
414 break;
415 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
b390134c 416 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
bd4cf0ed
AS
417 break;
418 case SKF_AD_OFF + SKF_AD_CPU:
b390134c 419 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
bd4cf0ed 420 break;
4cd3675e 421 case SKF_AD_OFF + SKF_AD_RANDOM:
3ad00405
DB
422 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
423 bpf_user_rnd_init_once();
4cd3675e 424 break;
bd4cf0ed
AS
425 }
426 break;
427
428 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
9739eef1
AS
429 /* A ^= X */
430 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
431 break;
432
433 default:
434 /* This is just a dummy call to avoid letting the compiler
435 * evict __bpf_call_base() as an optimization. Placed here
436 * where no-one bothers.
437 */
438 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
439 return false;
440 }
441
442 *insnp = insn;
443 return true;
444}
445
e0cea7ce
DB
446static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
447{
448 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
449 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
450 bool endian = BPF_SIZE(fp->code) == BPF_H ||
451 BPF_SIZE(fp->code) == BPF_W;
452 bool indirect = BPF_MODE(fp->code) == BPF_IND;
453 const int ip_align = NET_IP_ALIGN;
454 struct bpf_insn *insn = *insnp;
455 int offset = fp->k;
456
457 if (!indirect &&
458 ((unaligned_ok && offset >= 0) ||
459 (!unaligned_ok && offset >= 0 &&
460 offset + ip_align >= 0 &&
461 offset + ip_align % size == 0))) {
59ee4129
DB
462 bool ldx_off_ok = offset <= S16_MAX;
463
e0cea7ce
DB
464 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
465 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
59ee4129
DB
466 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
467 size, 2 + endian + (!ldx_off_ok * 2));
468 if (ldx_off_ok) {
469 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
470 BPF_REG_D, offset);
471 } else {
472 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
473 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
474 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
475 BPF_REG_TMP, 0);
476 }
e0cea7ce
DB
477 if (endian)
478 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
479 *insn++ = BPF_JMP_A(8);
480 }
481
482 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
483 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
484 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
485 if (!indirect) {
486 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
487 } else {
488 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
489 if (fp->k)
490 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
491 }
492
493 switch (BPF_SIZE(fp->code)) {
494 case BPF_B:
495 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
496 break;
497 case BPF_H:
498 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
499 break;
500 case BPF_W:
501 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
502 break;
503 default:
504 return false;
505 }
506
507 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
508 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
509 *insn = BPF_EXIT_INSN();
510
511 *insnp = insn;
512 return true;
513}
514
bd4cf0ed 515/**
8fb575ca 516 * bpf_convert_filter - convert filter program
bd4cf0ed
AS
517 * @prog: the user passed filter program
518 * @len: the length of the user passed filter program
50bbfed9 519 * @new_prog: allocated 'struct bpf_prog' or NULL
bd4cf0ed 520 * @new_len: pointer to store length of converted program
e0cea7ce 521 * @seen_ld_abs: bool whether we've seen ld_abs/ind
bd4cf0ed 522 *
1f504ec9
TK
523 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
524 * style extended BPF (eBPF).
bd4cf0ed
AS
525 * Conversion workflow:
526 *
527 * 1) First pass for calculating the new program length:
e0cea7ce 528 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
bd4cf0ed
AS
529 *
530 * 2) 2nd pass to remap in two passes: 1st pass finds new
531 * jump offsets, 2nd pass remapping:
e0cea7ce 532 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
bd4cf0ed 533 */
d9e12f42 534static int bpf_convert_filter(struct sock_filter *prog, int len,
e0cea7ce
DB
535 struct bpf_prog *new_prog, int *new_len,
536 bool *seen_ld_abs)
bd4cf0ed 537{
50bbfed9
AS
538 int new_flen = 0, pass = 0, target, i, stack_off;
539 struct bpf_insn *new_insn, *first_insn = NULL;
bd4cf0ed
AS
540 struct sock_filter *fp;
541 int *addrs = NULL;
542 u8 bpf_src;
543
544 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
30743837 545 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
bd4cf0ed 546
6f9a093b 547 if (len <= 0 || len > BPF_MAXINSNS)
bd4cf0ed
AS
548 return -EINVAL;
549
550 if (new_prog) {
50bbfed9 551 first_insn = new_prog->insnsi;
658da937
DB
552 addrs = kcalloc(len, sizeof(*addrs),
553 GFP_KERNEL | __GFP_NOWARN);
bd4cf0ed
AS
554 if (!addrs)
555 return -ENOMEM;
556 }
557
558do_pass:
50bbfed9 559 new_insn = first_insn;
bd4cf0ed
AS
560 fp = prog;
561
8b614aeb 562 /* Classic BPF related prologue emission. */
50bbfed9 563 if (new_prog) {
8b614aeb
DB
564 /* Classic BPF expects A and X to be reset first. These need
565 * to be guaranteed to be the first two instructions.
566 */
1d621674
DB
567 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
568 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
8b614aeb
DB
569
570 /* All programs must keep CTX in callee saved BPF_REG_CTX.
571 * In eBPF case it's done by the compiler, here we need to
572 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
573 */
574 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
e0cea7ce
DB
575 if (*seen_ld_abs) {
576 /* For packet access in classic BPF, cache skb->data
577 * in callee-saved BPF R8 and skb->len - skb->data_len
578 * (headlen) in BPF R9. Since classic BPF is read-only
579 * on CTX, we only need to cache it once.
580 */
581 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
582 BPF_REG_D, BPF_REG_CTX,
583 offsetof(struct sk_buff, data));
584 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
585 offsetof(struct sk_buff, len));
586 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
587 offsetof(struct sk_buff, data_len));
588 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
589 }
8b614aeb
DB
590 } else {
591 new_insn += 3;
592 }
bd4cf0ed
AS
593
594 for (i = 0; i < len; fp++, i++) {
e0cea7ce 595 struct bpf_insn tmp_insns[32] = { };
2695fb55 596 struct bpf_insn *insn = tmp_insns;
bd4cf0ed
AS
597
598 if (addrs)
50bbfed9 599 addrs[i] = new_insn - first_insn;
bd4cf0ed
AS
600
601 switch (fp->code) {
602 /* All arithmetic insns and skb loads map as-is. */
603 case BPF_ALU | BPF_ADD | BPF_X:
604 case BPF_ALU | BPF_ADD | BPF_K:
605 case BPF_ALU | BPF_SUB | BPF_X:
606 case BPF_ALU | BPF_SUB | BPF_K:
607 case BPF_ALU | BPF_AND | BPF_X:
608 case BPF_ALU | BPF_AND | BPF_K:
609 case BPF_ALU | BPF_OR | BPF_X:
610 case BPF_ALU | BPF_OR | BPF_K:
611 case BPF_ALU | BPF_LSH | BPF_X:
612 case BPF_ALU | BPF_LSH | BPF_K:
613 case BPF_ALU | BPF_RSH | BPF_X:
614 case BPF_ALU | BPF_RSH | BPF_K:
615 case BPF_ALU | BPF_XOR | BPF_X:
616 case BPF_ALU | BPF_XOR | BPF_K:
617 case BPF_ALU | BPF_MUL | BPF_X:
618 case BPF_ALU | BPF_MUL | BPF_K:
619 case BPF_ALU | BPF_DIV | BPF_X:
620 case BPF_ALU | BPF_DIV | BPF_K:
621 case BPF_ALU | BPF_MOD | BPF_X:
622 case BPF_ALU | BPF_MOD | BPF_K:
623 case BPF_ALU | BPF_NEG:
624 case BPF_LD | BPF_ABS | BPF_W:
625 case BPF_LD | BPF_ABS | BPF_H:
626 case BPF_LD | BPF_ABS | BPF_B:
627 case BPF_LD | BPF_IND | BPF_W:
628 case BPF_LD | BPF_IND | BPF_H:
629 case BPF_LD | BPF_IND | BPF_B:
630 /* Check for overloaded BPF extension and
631 * directly convert it if found, otherwise
632 * just move on with mapping.
633 */
634 if (BPF_CLASS(fp->code) == BPF_LD &&
635 BPF_MODE(fp->code) == BPF_ABS &&
636 convert_bpf_extensions(fp, &insn))
637 break;
e0cea7ce
DB
638 if (BPF_CLASS(fp->code) == BPF_LD &&
639 convert_bpf_ld_abs(fp, &insn)) {
640 *seen_ld_abs = true;
641 break;
642 }
bd4cf0ed 643
68fda450 644 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
f6b1b3bf 645 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
68fda450 646 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
f6b1b3bf
DB
647 /* Error with exception code on div/mod by 0.
648 * For cBPF programs, this was always return 0.
649 */
650 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
651 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
652 *insn++ = BPF_EXIT_INSN();
653 }
68fda450 654
f8f6d679 655 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
bd4cf0ed
AS
656 break;
657
f8f6d679
DB
658 /* Jump transformation cannot use BPF block macros
659 * everywhere as offset calculation and target updates
660 * require a bit more work than the rest, i.e. jump
661 * opcodes map as-is, but offsets need adjustment.
662 */
663
664#define BPF_EMIT_JMP \
bd4cf0ed 665 do { \
050fad7c
DB
666 const s32 off_min = S16_MIN, off_max = S16_MAX; \
667 s32 off; \
668 \
bd4cf0ed
AS
669 if (target >= len || target < 0) \
670 goto err; \
050fad7c 671 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
bd4cf0ed 672 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
050fad7c
DB
673 off -= insn - tmp_insns; \
674 /* Reject anything not fitting into insn->off. */ \
675 if (off < off_min || off > off_max) \
676 goto err; \
677 insn->off = off; \
bd4cf0ed
AS
678 } while (0)
679
f8f6d679
DB
680 case BPF_JMP | BPF_JA:
681 target = i + fp->k + 1;
682 insn->code = fp->code;
683 BPF_EMIT_JMP;
bd4cf0ed
AS
684 break;
685
686 case BPF_JMP | BPF_JEQ | BPF_K:
687 case BPF_JMP | BPF_JEQ | BPF_X:
688 case BPF_JMP | BPF_JSET | BPF_K:
689 case BPF_JMP | BPF_JSET | BPF_X:
690 case BPF_JMP | BPF_JGT | BPF_K:
691 case BPF_JMP | BPF_JGT | BPF_X:
692 case BPF_JMP | BPF_JGE | BPF_K:
693 case BPF_JMP | BPF_JGE | BPF_X:
694 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
695 /* BPF immediates are signed, zero extend
696 * immediate into tmp register and use it
697 * in compare insn.
698 */
f8f6d679 699 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
bd4cf0ed 700
e430f34e
AS
701 insn->dst_reg = BPF_REG_A;
702 insn->src_reg = BPF_REG_TMP;
bd4cf0ed
AS
703 bpf_src = BPF_X;
704 } else {
e430f34e 705 insn->dst_reg = BPF_REG_A;
bd4cf0ed
AS
706 insn->imm = fp->k;
707 bpf_src = BPF_SRC(fp->code);
19539ce7 708 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
1da177e4 709 }
bd4cf0ed
AS
710
711 /* Common case where 'jump_false' is next insn. */
712 if (fp->jf == 0) {
713 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
714 target = i + fp->jt + 1;
f8f6d679 715 BPF_EMIT_JMP;
bd4cf0ed 716 break;
1da177e4 717 }
bd4cf0ed 718
92b31a9a
DB
719 /* Convert some jumps when 'jump_true' is next insn. */
720 if (fp->jt == 0) {
721 switch (BPF_OP(fp->code)) {
722 case BPF_JEQ:
723 insn->code = BPF_JMP | BPF_JNE | bpf_src;
724 break;
725 case BPF_JGT:
726 insn->code = BPF_JMP | BPF_JLE | bpf_src;
727 break;
728 case BPF_JGE:
729 insn->code = BPF_JMP | BPF_JLT | bpf_src;
730 break;
731 default:
732 goto jmp_rest;
733 }
734
bd4cf0ed 735 target = i + fp->jf + 1;
f8f6d679 736 BPF_EMIT_JMP;
bd4cf0ed 737 break;
0b05b2a4 738 }
92b31a9a 739jmp_rest:
bd4cf0ed
AS
740 /* Other jumps are mapped into two insns: Jxx and JA. */
741 target = i + fp->jt + 1;
742 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
f8f6d679 743 BPF_EMIT_JMP;
bd4cf0ed
AS
744 insn++;
745
746 insn->code = BPF_JMP | BPF_JA;
747 target = i + fp->jf + 1;
f8f6d679 748 BPF_EMIT_JMP;
bd4cf0ed
AS
749 break;
750
751 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
e0cea7ce
DB
752 case BPF_LDX | BPF_MSH | BPF_B: {
753 struct sock_filter tmp = {
754 .code = BPF_LD | BPF_ABS | BPF_B,
755 .k = fp->k,
756 };
757
758 *seen_ld_abs = true;
759
760 /* X = A */
761 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
1268e253 762 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
e0cea7ce
DB
763 convert_bpf_ld_abs(&tmp, &insn);
764 insn++;
9739eef1 765 /* A &= 0xf */
f8f6d679 766 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
9739eef1 767 /* A <<= 2 */
f8f6d679 768 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
e0cea7ce
DB
769 /* tmp = X */
770 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
9739eef1 771 /* X = A */
f8f6d679 772 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
9739eef1 773 /* A = tmp */
f8f6d679 774 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
bd4cf0ed 775 break;
e0cea7ce 776 }
6205b9cf
DB
777 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
778 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
779 */
bd4cf0ed
AS
780 case BPF_RET | BPF_A:
781 case BPF_RET | BPF_K:
6205b9cf
DB
782 if (BPF_RVAL(fp->code) == BPF_K)
783 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
784 0, fp->k);
9739eef1 785 *insn = BPF_EXIT_INSN();
bd4cf0ed
AS
786 break;
787
788 /* Store to stack. */
789 case BPF_ST:
790 case BPF_STX:
50bbfed9 791 stack_off = fp->k * 4 + 4;
f8f6d679
DB
792 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
793 BPF_ST ? BPF_REG_A : BPF_REG_X,
50bbfed9
AS
794 -stack_off);
795 /* check_load_and_stores() verifies that classic BPF can
796 * load from stack only after write, so tracking
797 * stack_depth for ST|STX insns is enough
798 */
799 if (new_prog && new_prog->aux->stack_depth < stack_off)
800 new_prog->aux->stack_depth = stack_off;
bd4cf0ed
AS
801 break;
802
803 /* Load from stack. */
804 case BPF_LD | BPF_MEM:
805 case BPF_LDX | BPF_MEM:
50bbfed9 806 stack_off = fp->k * 4 + 4;
f8f6d679
DB
807 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
808 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
50bbfed9 809 -stack_off);
bd4cf0ed
AS
810 break;
811
812 /* A = K or X = K */
813 case BPF_LD | BPF_IMM:
814 case BPF_LDX | BPF_IMM:
f8f6d679
DB
815 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
816 BPF_REG_A : BPF_REG_X, fp->k);
bd4cf0ed
AS
817 break;
818
819 /* X = A */
820 case BPF_MISC | BPF_TAX:
f8f6d679 821 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
bd4cf0ed
AS
822 break;
823
824 /* A = X */
825 case BPF_MISC | BPF_TXA:
f8f6d679 826 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
827 break;
828
829 /* A = skb->len or X = skb->len */
830 case BPF_LD | BPF_W | BPF_LEN:
831 case BPF_LDX | BPF_W | BPF_LEN:
f8f6d679
DB
832 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
833 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
834 offsetof(struct sk_buff, len));
bd4cf0ed
AS
835 break;
836
f8f6d679 837 /* Access seccomp_data fields. */
bd4cf0ed 838 case BPF_LDX | BPF_ABS | BPF_W:
9739eef1
AS
839 /* A = *(u32 *) (ctx + K) */
840 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
bd4cf0ed
AS
841 break;
842
ca9f1fd2 843 /* Unknown instruction. */
1da177e4 844 default:
bd4cf0ed 845 goto err;
1da177e4 846 }
bd4cf0ed
AS
847
848 insn++;
849 if (new_prog)
850 memcpy(new_insn, tmp_insns,
851 sizeof(*insn) * (insn - tmp_insns));
bd4cf0ed 852 new_insn += insn - tmp_insns;
1da177e4
LT
853 }
854
bd4cf0ed
AS
855 if (!new_prog) {
856 /* Only calculating new length. */
50bbfed9 857 *new_len = new_insn - first_insn;
e0cea7ce
DB
858 if (*seen_ld_abs)
859 *new_len += 4; /* Prologue bits. */
bd4cf0ed
AS
860 return 0;
861 }
862
863 pass++;
50bbfed9
AS
864 if (new_flen != new_insn - first_insn) {
865 new_flen = new_insn - first_insn;
bd4cf0ed
AS
866 if (pass > 2)
867 goto err;
bd4cf0ed
AS
868 goto do_pass;
869 }
870
871 kfree(addrs);
872 BUG_ON(*new_len != new_flen);
1da177e4 873 return 0;
bd4cf0ed
AS
874err:
875 kfree(addrs);
876 return -EINVAL;
1da177e4
LT
877}
878
bd4cf0ed 879/* Security:
bd4cf0ed 880 *
2d5311e4 881 * As we dont want to clear mem[] array for each packet going through
8ea6e345 882 * __bpf_prog_run(), we check that filter loaded by user never try to read
2d5311e4 883 * a cell if not previously written, and we check all branches to be sure
25985edc 884 * a malicious user doesn't try to abuse us.
2d5311e4 885 */
ec31a05c 886static int check_load_and_stores(const struct sock_filter *filter, int flen)
2d5311e4 887{
34805931 888 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
2d5311e4
ED
889 int pc, ret = 0;
890
891 BUILD_BUG_ON(BPF_MEMWORDS > 16);
34805931 892
99e72a0f 893 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
2d5311e4
ED
894 if (!masks)
895 return -ENOMEM;
34805931 896
2d5311e4
ED
897 memset(masks, 0xff, flen * sizeof(*masks));
898
899 for (pc = 0; pc < flen; pc++) {
900 memvalid &= masks[pc];
901
902 switch (filter[pc].code) {
34805931
DB
903 case BPF_ST:
904 case BPF_STX:
2d5311e4
ED
905 memvalid |= (1 << filter[pc].k);
906 break;
34805931
DB
907 case BPF_LD | BPF_MEM:
908 case BPF_LDX | BPF_MEM:
2d5311e4
ED
909 if (!(memvalid & (1 << filter[pc].k))) {
910 ret = -EINVAL;
911 goto error;
912 }
913 break;
34805931
DB
914 case BPF_JMP | BPF_JA:
915 /* A jump must set masks on target */
2d5311e4
ED
916 masks[pc + 1 + filter[pc].k] &= memvalid;
917 memvalid = ~0;
918 break;
34805931
DB
919 case BPF_JMP | BPF_JEQ | BPF_K:
920 case BPF_JMP | BPF_JEQ | BPF_X:
921 case BPF_JMP | BPF_JGE | BPF_K:
922 case BPF_JMP | BPF_JGE | BPF_X:
923 case BPF_JMP | BPF_JGT | BPF_K:
924 case BPF_JMP | BPF_JGT | BPF_X:
925 case BPF_JMP | BPF_JSET | BPF_K:
926 case BPF_JMP | BPF_JSET | BPF_X:
927 /* A jump must set masks on targets */
2d5311e4
ED
928 masks[pc + 1 + filter[pc].jt] &= memvalid;
929 masks[pc + 1 + filter[pc].jf] &= memvalid;
930 memvalid = ~0;
931 break;
932 }
933 }
934error:
935 kfree(masks);
936 return ret;
937}
938
34805931
DB
939static bool chk_code_allowed(u16 code_to_probe)
940{
941 static const bool codes[] = {
942 /* 32 bit ALU operations */
943 [BPF_ALU | BPF_ADD | BPF_K] = true,
944 [BPF_ALU | BPF_ADD | BPF_X] = true,
945 [BPF_ALU | BPF_SUB | BPF_K] = true,
946 [BPF_ALU | BPF_SUB | BPF_X] = true,
947 [BPF_ALU | BPF_MUL | BPF_K] = true,
948 [BPF_ALU | BPF_MUL | BPF_X] = true,
949 [BPF_ALU | BPF_DIV | BPF_K] = true,
950 [BPF_ALU | BPF_DIV | BPF_X] = true,
951 [BPF_ALU | BPF_MOD | BPF_K] = true,
952 [BPF_ALU | BPF_MOD | BPF_X] = true,
953 [BPF_ALU | BPF_AND | BPF_K] = true,
954 [BPF_ALU | BPF_AND | BPF_X] = true,
955 [BPF_ALU | BPF_OR | BPF_K] = true,
956 [BPF_ALU | BPF_OR | BPF_X] = true,
957 [BPF_ALU | BPF_XOR | BPF_K] = true,
958 [BPF_ALU | BPF_XOR | BPF_X] = true,
959 [BPF_ALU | BPF_LSH | BPF_K] = true,
960 [BPF_ALU | BPF_LSH | BPF_X] = true,
961 [BPF_ALU | BPF_RSH | BPF_K] = true,
962 [BPF_ALU | BPF_RSH | BPF_X] = true,
963 [BPF_ALU | BPF_NEG] = true,
964 /* Load instructions */
965 [BPF_LD | BPF_W | BPF_ABS] = true,
966 [BPF_LD | BPF_H | BPF_ABS] = true,
967 [BPF_LD | BPF_B | BPF_ABS] = true,
968 [BPF_LD | BPF_W | BPF_LEN] = true,
969 [BPF_LD | BPF_W | BPF_IND] = true,
970 [BPF_LD | BPF_H | BPF_IND] = true,
971 [BPF_LD | BPF_B | BPF_IND] = true,
972 [BPF_LD | BPF_IMM] = true,
973 [BPF_LD | BPF_MEM] = true,
974 [BPF_LDX | BPF_W | BPF_LEN] = true,
975 [BPF_LDX | BPF_B | BPF_MSH] = true,
976 [BPF_LDX | BPF_IMM] = true,
977 [BPF_LDX | BPF_MEM] = true,
978 /* Store instructions */
979 [BPF_ST] = true,
980 [BPF_STX] = true,
981 /* Misc instructions */
982 [BPF_MISC | BPF_TAX] = true,
983 [BPF_MISC | BPF_TXA] = true,
984 /* Return instructions */
985 [BPF_RET | BPF_K] = true,
986 [BPF_RET | BPF_A] = true,
987 /* Jump instructions */
988 [BPF_JMP | BPF_JA] = true,
989 [BPF_JMP | BPF_JEQ | BPF_K] = true,
990 [BPF_JMP | BPF_JEQ | BPF_X] = true,
991 [BPF_JMP | BPF_JGE | BPF_K] = true,
992 [BPF_JMP | BPF_JGE | BPF_X] = true,
993 [BPF_JMP | BPF_JGT | BPF_K] = true,
994 [BPF_JMP | BPF_JGT | BPF_X] = true,
995 [BPF_JMP | BPF_JSET | BPF_K] = true,
996 [BPF_JMP | BPF_JSET | BPF_X] = true,
997 };
998
999 if (code_to_probe >= ARRAY_SIZE(codes))
1000 return false;
1001
1002 return codes[code_to_probe];
1003}
1004
f7bd9e36
DB
1005static bool bpf_check_basics_ok(const struct sock_filter *filter,
1006 unsigned int flen)
1007{
1008 if (filter == NULL)
1009 return false;
1010 if (flen == 0 || flen > BPF_MAXINSNS)
1011 return false;
1012
1013 return true;
1014}
1015
1da177e4 1016/**
4df95ff4 1017 * bpf_check_classic - verify socket filter code
1da177e4
LT
1018 * @filter: filter to verify
1019 * @flen: length of filter
1020 *
1021 * Check the user's filter code. If we let some ugly
1022 * filter code slip through kaboom! The filter must contain
93699863
KK
1023 * no references or jumps that are out of range, no illegal
1024 * instructions, and must end with a RET instruction.
1da177e4 1025 *
7b11f69f
KK
1026 * All jumps are forward as they are not signed.
1027 *
1028 * Returns 0 if the rule set is legal or -EINVAL if not.
1da177e4 1029 */
d9e12f42
NS
1030static int bpf_check_classic(const struct sock_filter *filter,
1031 unsigned int flen)
1da177e4 1032{
aa1113d9 1033 bool anc_found;
34805931 1034 int pc;
1da177e4 1035
34805931 1036 /* Check the filter code now */
1da177e4 1037 for (pc = 0; pc < flen; pc++) {
ec31a05c 1038 const struct sock_filter *ftest = &filter[pc];
93699863 1039
34805931
DB
1040 /* May we actually operate on this code? */
1041 if (!chk_code_allowed(ftest->code))
cba328fc 1042 return -EINVAL;
34805931 1043
93699863 1044 /* Some instructions need special checks */
34805931
DB
1045 switch (ftest->code) {
1046 case BPF_ALU | BPF_DIV | BPF_K:
1047 case BPF_ALU | BPF_MOD | BPF_K:
1048 /* Check for division by zero */
b6069a95
ED
1049 if (ftest->k == 0)
1050 return -EINVAL;
1051 break;
229394e8
RV
1052 case BPF_ALU | BPF_LSH | BPF_K:
1053 case BPF_ALU | BPF_RSH | BPF_K:
1054 if (ftest->k >= 32)
1055 return -EINVAL;
1056 break;
34805931
DB
1057 case BPF_LD | BPF_MEM:
1058 case BPF_LDX | BPF_MEM:
1059 case BPF_ST:
1060 case BPF_STX:
1061 /* Check for invalid memory addresses */
93699863
KK
1062 if (ftest->k >= BPF_MEMWORDS)
1063 return -EINVAL;
1064 break;
34805931
DB
1065 case BPF_JMP | BPF_JA:
1066 /* Note, the large ftest->k might cause loops.
93699863
KK
1067 * Compare this with conditional jumps below,
1068 * where offsets are limited. --ANK (981016)
1069 */
34805931 1070 if (ftest->k >= (unsigned int)(flen - pc - 1))
93699863 1071 return -EINVAL;
01f2f3f6 1072 break;
34805931
DB
1073 case BPF_JMP | BPF_JEQ | BPF_K:
1074 case BPF_JMP | BPF_JEQ | BPF_X:
1075 case BPF_JMP | BPF_JGE | BPF_K:
1076 case BPF_JMP | BPF_JGE | BPF_X:
1077 case BPF_JMP | BPF_JGT | BPF_K:
1078 case BPF_JMP | BPF_JGT | BPF_X:
1079 case BPF_JMP | BPF_JSET | BPF_K:
1080 case BPF_JMP | BPF_JSET | BPF_X:
1081 /* Both conditionals must be safe */
e35bedf3 1082 if (pc + ftest->jt + 1 >= flen ||
93699863
KK
1083 pc + ftest->jf + 1 >= flen)
1084 return -EINVAL;
cba328fc 1085 break;
34805931
DB
1086 case BPF_LD | BPF_W | BPF_ABS:
1087 case BPF_LD | BPF_H | BPF_ABS:
1088 case BPF_LD | BPF_B | BPF_ABS:
aa1113d9 1089 anc_found = false;
34805931
DB
1090 if (bpf_anc_helper(ftest) & BPF_ANC)
1091 anc_found = true;
1092 /* Ancillary operation unknown or unsupported */
aa1113d9
DB
1093 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1094 return -EINVAL;
01f2f3f6
HPP
1095 }
1096 }
93699863 1097
34805931 1098 /* Last instruction must be a RET code */
01f2f3f6 1099 switch (filter[flen - 1].code) {
34805931
DB
1100 case BPF_RET | BPF_K:
1101 case BPF_RET | BPF_A:
2d5311e4 1102 return check_load_and_stores(filter, flen);
cba328fc 1103 }
34805931 1104
cba328fc 1105 return -EINVAL;
1da177e4
LT
1106}
1107
7ae457c1
AS
1108static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1109 const struct sock_fprog *fprog)
a3ea269b 1110{
009937e7 1111 unsigned int fsize = bpf_classic_proglen(fprog);
a3ea269b
DB
1112 struct sock_fprog_kern *fkprog;
1113
1114 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1115 if (!fp->orig_prog)
1116 return -ENOMEM;
1117
1118 fkprog = fp->orig_prog;
1119 fkprog->len = fprog->len;
658da937
DB
1120
1121 fkprog->filter = kmemdup(fp->insns, fsize,
1122 GFP_KERNEL | __GFP_NOWARN);
a3ea269b
DB
1123 if (!fkprog->filter) {
1124 kfree(fp->orig_prog);
1125 return -ENOMEM;
1126 }
1127
1128 return 0;
1129}
1130
7ae457c1 1131static void bpf_release_orig_filter(struct bpf_prog *fp)
a3ea269b
DB
1132{
1133 struct sock_fprog_kern *fprog = fp->orig_prog;
1134
1135 if (fprog) {
1136 kfree(fprog->filter);
1137 kfree(fprog);
1138 }
1139}
1140
7ae457c1
AS
1141static void __bpf_prog_release(struct bpf_prog *prog)
1142{
24701ece 1143 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
89aa0758
AS
1144 bpf_prog_put(prog);
1145 } else {
1146 bpf_release_orig_filter(prog);
1147 bpf_prog_free(prog);
1148 }
7ae457c1
AS
1149}
1150
34c5bd66
PN
1151static void __sk_filter_release(struct sk_filter *fp)
1152{
7ae457c1
AS
1153 __bpf_prog_release(fp->prog);
1154 kfree(fp);
34c5bd66
PN
1155}
1156
47e958ea 1157/**
46bcf14f 1158 * sk_filter_release_rcu - Release a socket filter by rcu_head
47e958ea
PE
1159 * @rcu: rcu_head that contains the sk_filter to free
1160 */
fbc907f0 1161static void sk_filter_release_rcu(struct rcu_head *rcu)
47e958ea
PE
1162{
1163 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1164
34c5bd66 1165 __sk_filter_release(fp);
47e958ea 1166}
fbc907f0
DB
1167
1168/**
1169 * sk_filter_release - release a socket filter
1170 * @fp: filter to remove
1171 *
1172 * Remove a filter from a socket and release its resources.
1173 */
1174static void sk_filter_release(struct sk_filter *fp)
1175{
4c355cdf 1176 if (refcount_dec_and_test(&fp->refcnt))
fbc907f0
DB
1177 call_rcu(&fp->rcu, sk_filter_release_rcu);
1178}
1179
1180void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1181{
7ae457c1 1182 u32 filter_size = bpf_prog_size(fp->prog->len);
fbc907f0 1183
278571ba
AS
1184 atomic_sub(filter_size, &sk->sk_omem_alloc);
1185 sk_filter_release(fp);
fbc907f0 1186}
47e958ea 1187
278571ba
AS
1188/* try to charge the socket memory if there is space available
1189 * return true on success
1190 */
4c355cdf 1191static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
bd4cf0ed 1192{
7ae457c1 1193 u32 filter_size = bpf_prog_size(fp->prog->len);
278571ba
AS
1194
1195 /* same check as in sock_kmalloc() */
1196 if (filter_size <= sysctl_optmem_max &&
1197 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
278571ba
AS
1198 atomic_add(filter_size, &sk->sk_omem_alloc);
1199 return true;
bd4cf0ed 1200 }
278571ba 1201 return false;
bd4cf0ed
AS
1202}
1203
4c355cdf
RE
1204bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1205{
eefca20e
ED
1206 if (!refcount_inc_not_zero(&fp->refcnt))
1207 return false;
1208
1209 if (!__sk_filter_charge(sk, fp)) {
1210 sk_filter_release(fp);
1211 return false;
1212 }
1213 return true;
4c355cdf
RE
1214}
1215
7ae457c1 1216static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
bd4cf0ed
AS
1217{
1218 struct sock_filter *old_prog;
7ae457c1 1219 struct bpf_prog *old_fp;
34805931 1220 int err, new_len, old_len = fp->len;
e0cea7ce 1221 bool seen_ld_abs = false;
bd4cf0ed
AS
1222
1223 /* We are free to overwrite insns et al right here as it
1224 * won't be used at this point in time anymore internally
1225 * after the migration to the internal BPF instruction
1226 * representation.
1227 */
1228 BUILD_BUG_ON(sizeof(struct sock_filter) !=
2695fb55 1229 sizeof(struct bpf_insn));
bd4cf0ed 1230
bd4cf0ed
AS
1231 /* Conversion cannot happen on overlapping memory areas,
1232 * so we need to keep the user BPF around until the 2nd
1233 * pass. At this time, the user BPF is stored in fp->insns.
1234 */
1235 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
658da937 1236 GFP_KERNEL | __GFP_NOWARN);
bd4cf0ed
AS
1237 if (!old_prog) {
1238 err = -ENOMEM;
1239 goto out_err;
1240 }
1241
1242 /* 1st pass: calculate the new program length. */
e0cea7ce
DB
1243 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1244 &seen_ld_abs);
bd4cf0ed
AS
1245 if (err)
1246 goto out_err_free;
1247
1248 /* Expand fp for appending the new filter representation. */
1249 old_fp = fp;
60a3b225 1250 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
bd4cf0ed
AS
1251 if (!fp) {
1252 /* The old_fp is still around in case we couldn't
1253 * allocate new memory, so uncharge on that one.
1254 */
1255 fp = old_fp;
1256 err = -ENOMEM;
1257 goto out_err_free;
1258 }
1259
bd4cf0ed
AS
1260 fp->len = new_len;
1261
2695fb55 1262 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
e0cea7ce
DB
1263 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1264 &seen_ld_abs);
bd4cf0ed 1265 if (err)
8fb575ca 1266 /* 2nd bpf_convert_filter() can fail only if it fails
bd4cf0ed
AS
1267 * to allocate memory, remapping must succeed. Note,
1268 * that at this time old_fp has already been released
278571ba 1269 * by krealloc().
bd4cf0ed
AS
1270 */
1271 goto out_err_free;
1272
d1c55ab5 1273 fp = bpf_prog_select_runtime(fp, &err);
290af866
AS
1274 if (err)
1275 goto out_err_free;
5fe821a9 1276
bd4cf0ed
AS
1277 kfree(old_prog);
1278 return fp;
1279
1280out_err_free:
1281 kfree(old_prog);
1282out_err:
7ae457c1 1283 __bpf_prog_release(fp);
bd4cf0ed
AS
1284 return ERR_PTR(err);
1285}
1286
ac67eb2c
DB
1287static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1288 bpf_aux_classic_check_t trans)
302d6637
JP
1289{
1290 int err;
1291
bd4cf0ed 1292 fp->bpf_func = NULL;
a91263d5 1293 fp->jited = 0;
302d6637 1294
4df95ff4 1295 err = bpf_check_classic(fp->insns, fp->len);
418c96ac 1296 if (err) {
7ae457c1 1297 __bpf_prog_release(fp);
bd4cf0ed 1298 return ERR_PTR(err);
418c96ac 1299 }
302d6637 1300
4ae92bc7
NS
1301 /* There might be additional checks and transformations
1302 * needed on classic filters, f.e. in case of seccomp.
1303 */
1304 if (trans) {
1305 err = trans(fp->insns, fp->len);
1306 if (err) {
1307 __bpf_prog_release(fp);
1308 return ERR_PTR(err);
1309 }
1310 }
1311
bd4cf0ed
AS
1312 /* Probe if we can JIT compile the filter and if so, do
1313 * the compilation of the filter.
1314 */
302d6637 1315 bpf_jit_compile(fp);
bd4cf0ed
AS
1316
1317 /* JIT compiler couldn't process this filter, so do the
1318 * internal BPF translation for the optimized interpreter.
1319 */
5fe821a9 1320 if (!fp->jited)
7ae457c1 1321 fp = bpf_migrate_filter(fp);
bd4cf0ed
AS
1322
1323 return fp;
302d6637
JP
1324}
1325
1326/**
7ae457c1 1327 * bpf_prog_create - create an unattached filter
c6c4b97c 1328 * @pfp: the unattached filter that is created
677a9fd3 1329 * @fprog: the filter program
302d6637 1330 *
c6c4b97c 1331 * Create a filter independent of any socket. We first run some
302d6637
JP
1332 * sanity checks on it to make sure it does not explode on us later.
1333 * If an error occurs or there is insufficient memory for the filter
1334 * a negative errno code is returned. On success the return is zero.
1335 */
7ae457c1 1336int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
302d6637 1337{
009937e7 1338 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1 1339 struct bpf_prog *fp;
302d6637
JP
1340
1341 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1342 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
302d6637
JP
1343 return -EINVAL;
1344
60a3b225 1345 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
302d6637
JP
1346 if (!fp)
1347 return -ENOMEM;
a3ea269b 1348
302d6637
JP
1349 memcpy(fp->insns, fprog->filter, fsize);
1350
302d6637 1351 fp->len = fprog->len;
a3ea269b
DB
1352 /* Since unattached filters are not copied back to user
1353 * space through sk_get_filter(), we do not need to hold
1354 * a copy here, and can spare us the work.
1355 */
1356 fp->orig_prog = NULL;
302d6637 1357
7ae457c1 1358 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1359 * memory in case something goes wrong.
1360 */
4ae92bc7 1361 fp = bpf_prepare_filter(fp, NULL);
bd4cf0ed
AS
1362 if (IS_ERR(fp))
1363 return PTR_ERR(fp);
302d6637
JP
1364
1365 *pfp = fp;
1366 return 0;
302d6637 1367}
7ae457c1 1368EXPORT_SYMBOL_GPL(bpf_prog_create);
302d6637 1369
ac67eb2c
DB
1370/**
1371 * bpf_prog_create_from_user - create an unattached filter from user buffer
1372 * @pfp: the unattached filter that is created
1373 * @fprog: the filter program
1374 * @trans: post-classic verifier transformation handler
bab18991 1375 * @save_orig: save classic BPF program
ac67eb2c
DB
1376 *
1377 * This function effectively does the same as bpf_prog_create(), only
1378 * that it builds up its insns buffer from user space provided buffer.
1379 * It also allows for passing a bpf_aux_classic_check_t handler.
1380 */
1381int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
bab18991 1382 bpf_aux_classic_check_t trans, bool save_orig)
ac67eb2c
DB
1383{
1384 unsigned int fsize = bpf_classic_proglen(fprog);
1385 struct bpf_prog *fp;
bab18991 1386 int err;
ac67eb2c
DB
1387
1388 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1389 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
ac67eb2c
DB
1390 return -EINVAL;
1391
1392 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1393 if (!fp)
1394 return -ENOMEM;
1395
1396 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1397 __bpf_prog_free(fp);
1398 return -EFAULT;
1399 }
1400
1401 fp->len = fprog->len;
ac67eb2c
DB
1402 fp->orig_prog = NULL;
1403
bab18991
DB
1404 if (save_orig) {
1405 err = bpf_prog_store_orig_filter(fp, fprog);
1406 if (err) {
1407 __bpf_prog_free(fp);
1408 return -ENOMEM;
1409 }
1410 }
1411
ac67eb2c
DB
1412 /* bpf_prepare_filter() already takes care of freeing
1413 * memory in case something goes wrong.
1414 */
1415 fp = bpf_prepare_filter(fp, trans);
1416 if (IS_ERR(fp))
1417 return PTR_ERR(fp);
1418
1419 *pfp = fp;
1420 return 0;
1421}
2ea273d7 1422EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
ac67eb2c 1423
7ae457c1 1424void bpf_prog_destroy(struct bpf_prog *fp)
302d6637 1425{
7ae457c1 1426 __bpf_prog_release(fp);
302d6637 1427}
7ae457c1 1428EXPORT_SYMBOL_GPL(bpf_prog_destroy);
302d6637 1429
8ced425e 1430static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
49b31e57
DB
1431{
1432 struct sk_filter *fp, *old_fp;
1433
1434 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1435 if (!fp)
1436 return -ENOMEM;
1437
1438 fp->prog = prog;
49b31e57 1439
4c355cdf 1440 if (!__sk_filter_charge(sk, fp)) {
49b31e57
DB
1441 kfree(fp);
1442 return -ENOMEM;
1443 }
4c355cdf 1444 refcount_set(&fp->refcnt, 1);
49b31e57 1445
8ced425e
HFS
1446 old_fp = rcu_dereference_protected(sk->sk_filter,
1447 lockdep_sock_is_held(sk));
49b31e57 1448 rcu_assign_pointer(sk->sk_filter, fp);
8ced425e 1449
49b31e57
DB
1450 if (old_fp)
1451 sk_filter_uncharge(sk, old_fp);
1452
1453 return 0;
1454}
1455
538950a1
CG
1456static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1457{
1458 struct bpf_prog *old_prog;
1459 int err;
1460
1461 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1462 return -ENOMEM;
1463
fa463497 1464 if (sk_unhashed(sk) && sk->sk_reuseport) {
538950a1
CG
1465 err = reuseport_alloc(sk);
1466 if (err)
1467 return err;
1468 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1469 /* The socket wasn't bound with SO_REUSEPORT */
1470 return -EINVAL;
1471 }
1472
1473 old_prog = reuseport_attach_prog(sk, prog);
1474 if (old_prog)
1475 bpf_prog_destroy(old_prog);
1476
1477 return 0;
1478}
1479
1480static
1481struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1da177e4 1482{
009937e7 1483 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1 1484 struct bpf_prog *prog;
1da177e4
LT
1485 int err;
1486
d59577b6 1487 if (sock_flag(sk, SOCK_FILTER_LOCKED))
538950a1 1488 return ERR_PTR(-EPERM);
d59577b6 1489
1da177e4 1490 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1491 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
538950a1 1492 return ERR_PTR(-EINVAL);
1da177e4 1493
f7bd9e36 1494 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
7ae457c1 1495 if (!prog)
538950a1 1496 return ERR_PTR(-ENOMEM);
a3ea269b 1497
7ae457c1 1498 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
c0d1379a 1499 __bpf_prog_free(prog);
538950a1 1500 return ERR_PTR(-EFAULT);
1da177e4
LT
1501 }
1502
7ae457c1 1503 prog->len = fprog->len;
1da177e4 1504
7ae457c1 1505 err = bpf_prog_store_orig_filter(prog, fprog);
a3ea269b 1506 if (err) {
c0d1379a 1507 __bpf_prog_free(prog);
538950a1 1508 return ERR_PTR(-ENOMEM);
a3ea269b
DB
1509 }
1510
7ae457c1 1511 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1512 * memory in case something goes wrong.
1513 */
538950a1
CG
1514 return bpf_prepare_filter(prog, NULL);
1515}
1516
1517/**
1518 * sk_attach_filter - attach a socket filter
1519 * @fprog: the filter program
1520 * @sk: the socket to use
1521 *
1522 * Attach the user's filter code. We first run some sanity checks on
1523 * it to make sure it does not explode on us later. If an error
1524 * occurs or there is insufficient memory for the filter a negative
1525 * errno code is returned. On success the return is zero.
1526 */
8ced425e 1527int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
538950a1
CG
1528{
1529 struct bpf_prog *prog = __get_filter(fprog, sk);
1530 int err;
1531
7ae457c1
AS
1532 if (IS_ERR(prog))
1533 return PTR_ERR(prog);
1534
8ced425e 1535 err = __sk_attach_prog(prog, sk);
49b31e57 1536 if (err < 0) {
7ae457c1 1537 __bpf_prog_release(prog);
49b31e57 1538 return err;
278571ba
AS
1539 }
1540
d3904b73 1541 return 0;
1da177e4 1542}
8ced425e 1543EXPORT_SYMBOL_GPL(sk_attach_filter);
1da177e4 1544
538950a1 1545int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
89aa0758 1546{
538950a1 1547 struct bpf_prog *prog = __get_filter(fprog, sk);
49b31e57 1548 int err;
89aa0758 1549
538950a1
CG
1550 if (IS_ERR(prog))
1551 return PTR_ERR(prog);
1552
1553 err = __reuseport_attach_prog(prog, sk);
1554 if (err < 0) {
1555 __bpf_prog_release(prog);
1556 return err;
1557 }
1558
1559 return 0;
1560}
1561
1562static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1563{
89aa0758 1564 if (sock_flag(sk, SOCK_FILTER_LOCKED))
538950a1 1565 return ERR_PTR(-EPERM);
89aa0758 1566
113214be 1567 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
538950a1
CG
1568}
1569
1570int sk_attach_bpf(u32 ufd, struct sock *sk)
1571{
1572 struct bpf_prog *prog = __get_bpf(ufd, sk);
1573 int err;
1574
1575 if (IS_ERR(prog))
1576 return PTR_ERR(prog);
1577
8ced425e 1578 err = __sk_attach_prog(prog, sk);
49b31e57 1579 if (err < 0) {
89aa0758 1580 bpf_prog_put(prog);
49b31e57 1581 return err;
89aa0758
AS
1582 }
1583
89aa0758
AS
1584 return 0;
1585}
1586
538950a1
CG
1587int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1588{
1589 struct bpf_prog *prog = __get_bpf(ufd, sk);
1590 int err;
1591
1592 if (IS_ERR(prog))
1593 return PTR_ERR(prog);
1594
1595 err = __reuseport_attach_prog(prog, sk);
1596 if (err < 0) {
1597 bpf_prog_put(prog);
1598 return err;
1599 }
1600
1601 return 0;
1602}
1603
21cafc1d
DB
1604struct bpf_scratchpad {
1605 union {
1606 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1607 u8 buff[MAX_BPF_STACK];
1608 };
1609};
1610
1611static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
91bc4822 1612
5293efe6
DB
1613static inline int __bpf_try_make_writable(struct sk_buff *skb,
1614 unsigned int write_len)
1615{
1616 return skb_ensure_writable(skb, write_len);
1617}
1618
db58ba45
AS
1619static inline int bpf_try_make_writable(struct sk_buff *skb,
1620 unsigned int write_len)
1621{
5293efe6 1622 int err = __bpf_try_make_writable(skb, write_len);
db58ba45 1623
6aaae2b6 1624 bpf_compute_data_pointers(skb);
db58ba45
AS
1625 return err;
1626}
1627
36bbef52
DB
1628static int bpf_try_make_head_writable(struct sk_buff *skb)
1629{
1630 return bpf_try_make_writable(skb, skb_headlen(skb));
1631}
1632
a2bfe6bf
DB
1633static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1634{
1635 if (skb_at_tc_ingress(skb))
1636 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1637}
1638
8065694e
DB
1639static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1640{
1641 if (skb_at_tc_ingress(skb))
1642 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1643}
1644
f3694e00
DB
1645BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1646 const void *, from, u32, len, u64, flags)
608cd71a 1647{
608cd71a
AS
1648 void *ptr;
1649
8afd54c8 1650 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
781c53bc 1651 return -EINVAL;
0ed661d5 1652 if (unlikely(offset > 0xffff))
608cd71a 1653 return -EFAULT;
db58ba45 1654 if (unlikely(bpf_try_make_writable(skb, offset + len)))
608cd71a
AS
1655 return -EFAULT;
1656
0ed661d5 1657 ptr = skb->data + offset;
781c53bc 1658 if (flags & BPF_F_RECOMPUTE_CSUM)
479ffccc 1659 __skb_postpull_rcsum(skb, ptr, len, offset);
608cd71a
AS
1660
1661 memcpy(ptr, from, len);
1662
781c53bc 1663 if (flags & BPF_F_RECOMPUTE_CSUM)
479ffccc 1664 __skb_postpush_rcsum(skb, ptr, len, offset);
8afd54c8
DB
1665 if (flags & BPF_F_INVALIDATE_HASH)
1666 skb_clear_hash(skb);
f8ffad69 1667
608cd71a
AS
1668 return 0;
1669}
1670
577c50aa 1671static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
608cd71a
AS
1672 .func = bpf_skb_store_bytes,
1673 .gpl_only = false,
1674 .ret_type = RET_INTEGER,
1675 .arg1_type = ARG_PTR_TO_CTX,
1676 .arg2_type = ARG_ANYTHING,
39f19ebb
AS
1677 .arg3_type = ARG_PTR_TO_MEM,
1678 .arg4_type = ARG_CONST_SIZE,
91bc4822
AS
1679 .arg5_type = ARG_ANYTHING,
1680};
1681
f3694e00
DB
1682BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1683 void *, to, u32, len)
05c74e5e 1684{
05c74e5e
DB
1685 void *ptr;
1686
0ed661d5 1687 if (unlikely(offset > 0xffff))
074f528e 1688 goto err_clear;
05c74e5e
DB
1689
1690 ptr = skb_header_pointer(skb, offset, len, to);
1691 if (unlikely(!ptr))
074f528e 1692 goto err_clear;
05c74e5e
DB
1693 if (ptr != to)
1694 memcpy(to, ptr, len);
1695
1696 return 0;
074f528e
DB
1697err_clear:
1698 memset(to, 0, len);
1699 return -EFAULT;
05c74e5e
DB
1700}
1701
577c50aa 1702static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
05c74e5e
DB
1703 .func = bpf_skb_load_bytes,
1704 .gpl_only = false,
1705 .ret_type = RET_INTEGER,
1706 .arg1_type = ARG_PTR_TO_CTX,
1707 .arg2_type = ARG_ANYTHING,
39f19ebb
AS
1708 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1709 .arg4_type = ARG_CONST_SIZE,
05c74e5e
DB
1710};
1711
4e1ec56c
DB
1712BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1713 u32, offset, void *, to, u32, len, u32, start_header)
1714{
1715 u8 *ptr;
1716
1717 if (unlikely(offset > 0xffff || len > skb_headlen(skb)))
1718 goto err_clear;
1719
1720 switch (start_header) {
1721 case BPF_HDR_START_MAC:
1722 ptr = skb_mac_header(skb) + offset;
1723 break;
1724 case BPF_HDR_START_NET:
1725 ptr = skb_network_header(skb) + offset;
1726 break;
1727 default:
1728 goto err_clear;
1729 }
1730
1731 if (likely(ptr >= skb_mac_header(skb) &&
1732 ptr + len <= skb_tail_pointer(skb))) {
1733 memcpy(to, ptr, len);
1734 return 0;
1735 }
1736
1737err_clear:
1738 memset(to, 0, len);
1739 return -EFAULT;
1740}
1741
1742static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1743 .func = bpf_skb_load_bytes_relative,
1744 .gpl_only = false,
1745 .ret_type = RET_INTEGER,
1746 .arg1_type = ARG_PTR_TO_CTX,
1747 .arg2_type = ARG_ANYTHING,
1748 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1749 .arg4_type = ARG_CONST_SIZE,
1750 .arg5_type = ARG_ANYTHING,
1751};
1752
36bbef52
DB
1753BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1754{
1755 /* Idea is the following: should the needed direct read/write
1756 * test fail during runtime, we can pull in more data and redo
1757 * again, since implicitly, we invalidate previous checks here.
1758 *
1759 * Or, since we know how much we need to make read/writeable,
1760 * this can be done once at the program beginning for direct
1761 * access case. By this we overcome limitations of only current
1762 * headroom being accessible.
1763 */
1764 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1765}
1766
1767static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1768 .func = bpf_skb_pull_data,
1769 .gpl_only = false,
1770 .ret_type = RET_INTEGER,
1771 .arg1_type = ARG_PTR_TO_CTX,
1772 .arg2_type = ARG_ANYTHING,
1773};
1774
0ea488ff
JF
1775static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1776 unsigned int write_len)
1777{
1778 int err = __bpf_try_make_writable(skb, write_len);
1779
1780 bpf_compute_data_end_sk_skb(skb);
1781 return err;
1782}
1783
1784BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1785{
1786 /* Idea is the following: should the needed direct read/write
1787 * test fail during runtime, we can pull in more data and redo
1788 * again, since implicitly, we invalidate previous checks here.
1789 *
1790 * Or, since we know how much we need to make read/writeable,
1791 * this can be done once at the program beginning for direct
1792 * access case. By this we overcome limitations of only current
1793 * headroom being accessible.
1794 */
1795 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1796}
1797
1798static const struct bpf_func_proto sk_skb_pull_data_proto = {
1799 .func = sk_skb_pull_data,
1800 .gpl_only = false,
1801 .ret_type = RET_INTEGER,
1802 .arg1_type = ARG_PTR_TO_CTX,
1803 .arg2_type = ARG_ANYTHING,
1804};
1805
f3694e00
DB
1806BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1807 u64, from, u64, to, u64, flags)
91bc4822 1808{
0ed661d5 1809 __sum16 *ptr;
91bc4822 1810
781c53bc
DB
1811 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1812 return -EINVAL;
0ed661d5 1813 if (unlikely(offset > 0xffff || offset & 1))
91bc4822 1814 return -EFAULT;
0ed661d5 1815 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
91bc4822
AS
1816 return -EFAULT;
1817
0ed661d5 1818 ptr = (__sum16 *)(skb->data + offset);
781c53bc 1819 switch (flags & BPF_F_HDR_FIELD_MASK) {
8050c0f0
DB
1820 case 0:
1821 if (unlikely(from != 0))
1822 return -EINVAL;
1823
1824 csum_replace_by_diff(ptr, to);
1825 break;
91bc4822
AS
1826 case 2:
1827 csum_replace2(ptr, from, to);
1828 break;
1829 case 4:
1830 csum_replace4(ptr, from, to);
1831 break;
1832 default:
1833 return -EINVAL;
1834 }
1835
91bc4822
AS
1836 return 0;
1837}
1838
577c50aa 1839static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
91bc4822
AS
1840 .func = bpf_l3_csum_replace,
1841 .gpl_only = false,
1842 .ret_type = RET_INTEGER,
1843 .arg1_type = ARG_PTR_TO_CTX,
1844 .arg2_type = ARG_ANYTHING,
1845 .arg3_type = ARG_ANYTHING,
1846 .arg4_type = ARG_ANYTHING,
1847 .arg5_type = ARG_ANYTHING,
1848};
1849
f3694e00
DB
1850BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1851 u64, from, u64, to, u64, flags)
91bc4822 1852{
781c53bc 1853 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
2f72959a 1854 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
d1b662ad 1855 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
0ed661d5 1856 __sum16 *ptr;
91bc4822 1857
d1b662ad
DB
1858 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1859 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
781c53bc 1860 return -EINVAL;
0ed661d5 1861 if (unlikely(offset > 0xffff || offset & 1))
91bc4822 1862 return -EFAULT;
0ed661d5 1863 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
91bc4822
AS
1864 return -EFAULT;
1865
0ed661d5 1866 ptr = (__sum16 *)(skb->data + offset);
d1b662ad 1867 if (is_mmzero && !do_mforce && !*ptr)
2f72959a 1868 return 0;
91bc4822 1869
781c53bc 1870 switch (flags & BPF_F_HDR_FIELD_MASK) {
7d672345
DB
1871 case 0:
1872 if (unlikely(from != 0))
1873 return -EINVAL;
1874
1875 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1876 break;
91bc4822
AS
1877 case 2:
1878 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1879 break;
1880 case 4:
1881 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1882 break;
1883 default:
1884 return -EINVAL;
1885 }
1886
2f72959a
DB
1887 if (is_mmzero && !*ptr)
1888 *ptr = CSUM_MANGLED_0;
91bc4822
AS
1889 return 0;
1890}
1891
577c50aa 1892static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
91bc4822
AS
1893 .func = bpf_l4_csum_replace,
1894 .gpl_only = false,
1895 .ret_type = RET_INTEGER,
1896 .arg1_type = ARG_PTR_TO_CTX,
1897 .arg2_type = ARG_ANYTHING,
1898 .arg3_type = ARG_ANYTHING,
1899 .arg4_type = ARG_ANYTHING,
1900 .arg5_type = ARG_ANYTHING,
608cd71a
AS
1901};
1902
f3694e00
DB
1903BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1904 __be32 *, to, u32, to_size, __wsum, seed)
7d672345 1905{
21cafc1d 1906 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
f3694e00 1907 u32 diff_size = from_size + to_size;
7d672345
DB
1908 int i, j = 0;
1909
1910 /* This is quite flexible, some examples:
1911 *
1912 * from_size == 0, to_size > 0, seed := csum --> pushing data
1913 * from_size > 0, to_size == 0, seed := csum --> pulling data
1914 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1915 *
1916 * Even for diffing, from_size and to_size don't need to be equal.
1917 */
1918 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1919 diff_size > sizeof(sp->diff)))
1920 return -EINVAL;
1921
1922 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1923 sp->diff[j] = ~from[i];
1924 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1925 sp->diff[j] = to[i];
1926
1927 return csum_partial(sp->diff, diff_size, seed);
1928}
1929
577c50aa 1930static const struct bpf_func_proto bpf_csum_diff_proto = {
7d672345
DB
1931 .func = bpf_csum_diff,
1932 .gpl_only = false,
36bbef52 1933 .pkt_access = true,
7d672345 1934 .ret_type = RET_INTEGER,
db1ac496 1935 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
39f19ebb 1936 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
db1ac496 1937 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
39f19ebb 1938 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
7d672345
DB
1939 .arg5_type = ARG_ANYTHING,
1940};
1941
36bbef52
DB
1942BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1943{
1944 /* The interface is to be used in combination with bpf_csum_diff()
1945 * for direct packet writes. csum rotation for alignment as well
1946 * as emulating csum_sub() can be done from the eBPF program.
1947 */
1948 if (skb->ip_summed == CHECKSUM_COMPLETE)
1949 return (skb->csum = csum_add(skb->csum, csum));
1950
1951 return -ENOTSUPP;
1952}
1953
1954static const struct bpf_func_proto bpf_csum_update_proto = {
1955 .func = bpf_csum_update,
1956 .gpl_only = false,
1957 .ret_type = RET_INTEGER,
1958 .arg1_type = ARG_PTR_TO_CTX,
1959 .arg2_type = ARG_ANYTHING,
1960};
1961
a70b506e
DB
1962static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1963{
a70b506e
DB
1964 return dev_forward_skb(dev, skb);
1965}
1966
4e3264d2
MKL
1967static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1968 struct sk_buff *skb)
1969{
1970 int ret = ____dev_forward_skb(dev, skb);
1971
1972 if (likely(!ret)) {
1973 skb->dev = dev;
1974 ret = netif_rx(skb);
1975 }
1976
1977 return ret;
1978}
1979
a70b506e
DB
1980static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1981{
1982 int ret;
1983
1984 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1985 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1986 kfree_skb(skb);
1987 return -ENETDOWN;
1988 }
1989
1990 skb->dev = dev;
1991
1992 __this_cpu_inc(xmit_recursion);
1993 ret = dev_queue_xmit(skb);
1994 __this_cpu_dec(xmit_recursion);
1995
1996 return ret;
1997}
1998
4e3264d2
MKL
1999static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2000 u32 flags)
2001{
2002 /* skb->mac_len is not set on normal egress */
2003 unsigned int mlen = skb->network_header - skb->mac_header;
2004
2005 __skb_pull(skb, mlen);
2006
2007 /* At ingress, the mac header has already been pulled once.
2008 * At egress, skb_pospull_rcsum has to be done in case that
2009 * the skb is originated from ingress (i.e. a forwarded skb)
2010 * to ensure that rcsum starts at net header.
2011 */
2012 if (!skb_at_tc_ingress(skb))
2013 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2014 skb_pop_mac_header(skb);
2015 skb_reset_mac_len(skb);
2016 return flags & BPF_F_INGRESS ?
2017 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2018}
2019
2020static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2021 u32 flags)
2022{
3a0af8fd
TG
2023 /* Verify that a link layer header is carried */
2024 if (unlikely(skb->mac_header >= skb->network_header)) {
2025 kfree_skb(skb);
2026 return -ERANGE;
2027 }
2028
4e3264d2
MKL
2029 bpf_push_mac_rcsum(skb);
2030 return flags & BPF_F_INGRESS ?
2031 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2032}
2033
2034static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2035 u32 flags)
2036{
c491680f 2037 if (dev_is_mac_header_xmit(dev))
4e3264d2 2038 return __bpf_redirect_common(skb, dev, flags);
c491680f
DB
2039 else
2040 return __bpf_redirect_no_mac(skb, dev, flags);
4e3264d2
MKL
2041}
2042
f3694e00 2043BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
3896d655 2044{
3896d655 2045 struct net_device *dev;
36bbef52
DB
2046 struct sk_buff *clone;
2047 int ret;
3896d655 2048
781c53bc
DB
2049 if (unlikely(flags & ~(BPF_F_INGRESS)))
2050 return -EINVAL;
2051
3896d655
AS
2052 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2053 if (unlikely(!dev))
2054 return -EINVAL;
2055
36bbef52
DB
2056 clone = skb_clone(skb, GFP_ATOMIC);
2057 if (unlikely(!clone))
3896d655
AS
2058 return -ENOMEM;
2059
36bbef52
DB
2060 /* For direct write, we need to keep the invariant that the skbs
2061 * we're dealing with need to be uncloned. Should uncloning fail
2062 * here, we need to free the just generated clone to unclone once
2063 * again.
2064 */
2065 ret = bpf_try_make_head_writable(skb);
2066 if (unlikely(ret)) {
2067 kfree_skb(clone);
2068 return -ENOMEM;
2069 }
2070
4e3264d2 2071 return __bpf_redirect(clone, dev, flags);
3896d655
AS
2072}
2073
577c50aa 2074static const struct bpf_func_proto bpf_clone_redirect_proto = {
3896d655
AS
2075 .func = bpf_clone_redirect,
2076 .gpl_only = false,
2077 .ret_type = RET_INTEGER,
2078 .arg1_type = ARG_PTR_TO_CTX,
2079 .arg2_type = ARG_ANYTHING,
2080 .arg3_type = ARG_ANYTHING,
2081};
2082
27b29f63
AS
2083struct redirect_info {
2084 u32 ifindex;
2085 u32 flags;
97f91a7c 2086 struct bpf_map *map;
11393cc9 2087 struct bpf_map *map_to_flush;
7c300131 2088 unsigned long map_owner;
27b29f63
AS
2089};
2090
2091static DEFINE_PER_CPU(struct redirect_info, redirect_info);
781c53bc 2092
f3694e00 2093BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
27b29f63
AS
2094{
2095 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2096
781c53bc
DB
2097 if (unlikely(flags & ~(BPF_F_INGRESS)))
2098 return TC_ACT_SHOT;
2099
27b29f63
AS
2100 ri->ifindex = ifindex;
2101 ri->flags = flags;
781c53bc 2102
27b29f63
AS
2103 return TC_ACT_REDIRECT;
2104}
2105
2106int skb_do_redirect(struct sk_buff *skb)
2107{
2108 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2109 struct net_device *dev;
2110
2111 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2112 ri->ifindex = 0;
2113 if (unlikely(!dev)) {
2114 kfree_skb(skb);
2115 return -EINVAL;
2116 }
2117
4e3264d2 2118 return __bpf_redirect(skb, dev, ri->flags);
27b29f63
AS
2119}
2120
577c50aa 2121static const struct bpf_func_proto bpf_redirect_proto = {
27b29f63
AS
2122 .func = bpf_redirect,
2123 .gpl_only = false,
2124 .ret_type = RET_INTEGER,
2125 .arg1_type = ARG_ANYTHING,
2126 .arg2_type = ARG_ANYTHING,
2127};
2128
81110384
JF
2129BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
2130 struct bpf_map *, map, void *, key, u64, flags)
2131{
2132 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2133
2134 /* If user passes invalid input drop the packet. */
2135 if (unlikely(flags & ~(BPF_F_INGRESS)))
2136 return SK_DROP;
2137
2138 tcb->bpf.flags = flags;
2139 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
2140 if (!tcb->bpf.sk_redir)
2141 return SK_DROP;
2142
2143 return SK_PASS;
2144}
2145
2146static const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
2147 .func = bpf_sk_redirect_hash,
2148 .gpl_only = false,
2149 .ret_type = RET_INTEGER,
2150 .arg1_type = ARG_PTR_TO_CTX,
2151 .arg2_type = ARG_CONST_MAP_PTR,
2152 .arg3_type = ARG_PTR_TO_MAP_KEY,
2153 .arg4_type = ARG_ANYTHING,
2154};
2155
34f79502
JF
2156BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
2157 struct bpf_map *, map, u32, key, u64, flags)
174a79ff 2158{
34f79502 2159 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
174a79ff 2160
bfa64075 2161 /* If user passes invalid input drop the packet. */
fa246693 2162 if (unlikely(flags & ~(BPF_F_INGRESS)))
bfa64075 2163 return SK_DROP;
174a79ff 2164
34f79502 2165 tcb->bpf.flags = flags;
e5cd3abc
JF
2166 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
2167 if (!tcb->bpf.sk_redir)
2168 return SK_DROP;
174a79ff 2169
bfa64075 2170 return SK_PASS;
174a79ff
JF
2171}
2172
34f79502 2173struct sock *do_sk_redirect_map(struct sk_buff *skb)
174a79ff 2174{
34f79502 2175 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
174a79ff 2176
e5cd3abc 2177 return tcb->bpf.sk_redir;
174a79ff
JF
2178}
2179
2180static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
2181 .func = bpf_sk_redirect_map,
2182 .gpl_only = false,
2183 .ret_type = RET_INTEGER,
34f79502
JF
2184 .arg1_type = ARG_PTR_TO_CTX,
2185 .arg2_type = ARG_CONST_MAP_PTR,
174a79ff 2186 .arg3_type = ARG_ANYTHING,
34f79502 2187 .arg4_type = ARG_ANYTHING,
174a79ff
JF
2188};
2189
81110384
JF
2190BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg_buff *, msg,
2191 struct bpf_map *, map, void *, key, u64, flags)
2192{
2193 /* If user passes invalid input drop the packet. */
2194 if (unlikely(flags & ~(BPF_F_INGRESS)))
2195 return SK_DROP;
2196
2197 msg->flags = flags;
2198 msg->sk_redir = __sock_hash_lookup_elem(map, key);
2199 if (!msg->sk_redir)
2200 return SK_DROP;
2201
2202 return SK_PASS;
2203}
2204
2205static const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
2206 .func = bpf_msg_redirect_hash,
2207 .gpl_only = false,
2208 .ret_type = RET_INTEGER,
2209 .arg1_type = ARG_PTR_TO_CTX,
2210 .arg2_type = ARG_CONST_MAP_PTR,
2211 .arg3_type = ARG_PTR_TO_MAP_KEY,
2212 .arg4_type = ARG_ANYTHING,
2213};
2214
4f738adb
JF
2215BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg_buff *, msg,
2216 struct bpf_map *, map, u32, key, u64, flags)
2217{
2218 /* If user passes invalid input drop the packet. */
8934ce2f 2219 if (unlikely(flags & ~(BPF_F_INGRESS)))
4f738adb
JF
2220 return SK_DROP;
2221
4f738adb 2222 msg->flags = flags;
e5cd3abc
JF
2223 msg->sk_redir = __sock_map_lookup_elem(map, key);
2224 if (!msg->sk_redir)
2225 return SK_DROP;
4f738adb
JF
2226
2227 return SK_PASS;
2228}
2229
2230struct sock *do_msg_redirect_map(struct sk_msg_buff *msg)
2231{
e5cd3abc 2232 return msg->sk_redir;
4f738adb
JF
2233}
2234
2235static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
2236 .func = bpf_msg_redirect_map,
2237 .gpl_only = false,
2238 .ret_type = RET_INTEGER,
2239 .arg1_type = ARG_PTR_TO_CTX,
2240 .arg2_type = ARG_CONST_MAP_PTR,
2241 .arg3_type = ARG_ANYTHING,
2242 .arg4_type = ARG_ANYTHING,
2243};
2244
2a100317
JF
2245BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg_buff *, msg, u32, bytes)
2246{
2247 msg->apply_bytes = bytes;
2248 return 0;
2249}
2250
2251static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2252 .func = bpf_msg_apply_bytes,
2253 .gpl_only = false,
2254 .ret_type = RET_INTEGER,
2255 .arg1_type = ARG_PTR_TO_CTX,
2256 .arg2_type = ARG_ANYTHING,
2257};
2258
91843d54
JF
2259BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg_buff *, msg, u32, bytes)
2260{
2261 msg->cork_bytes = bytes;
2262 return 0;
2263}
2264
2265static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2266 .func = bpf_msg_cork_bytes,
2267 .gpl_only = false,
2268 .ret_type = RET_INTEGER,
2269 .arg1_type = ARG_PTR_TO_CTX,
2270 .arg2_type = ARG_ANYTHING,
2271};
2272
015632bb
JF
2273BPF_CALL_4(bpf_msg_pull_data,
2274 struct sk_msg_buff *, msg, u32, start, u32, end, u64, flags)
2275{
2276 unsigned int len = 0, offset = 0, copy = 0;
2277 struct scatterlist *sg = msg->sg_data;
2278 int first_sg, last_sg, i, shift;
2279 unsigned char *p, *to, *from;
2280 int bytes = end - start;
2281 struct page *page;
2282
2283 if (unlikely(flags || end <= start))
2284 return -EINVAL;
2285
2286 /* First find the starting scatterlist element */
2287 i = msg->sg_start;
2288 do {
2289 len = sg[i].length;
2290 offset += len;
2291 if (start < offset + len)
2292 break;
2293 i++;
2294 if (i == MAX_SKB_FRAGS)
2295 i = 0;
2296 } while (i != msg->sg_end);
2297
2298 if (unlikely(start >= offset + len))
2299 return -EINVAL;
2300
2301 if (!msg->sg_copy[i] && bytes <= len)
2302 goto out;
2303
2304 first_sg = i;
2305
2306 /* At this point we need to linearize multiple scatterlist
2307 * elements or a single shared page. Either way we need to
2308 * copy into a linear buffer exclusively owned by BPF. Then
2309 * place the buffer in the scatterlist and fixup the original
2310 * entries by removing the entries now in the linear buffer
2311 * and shifting the remaining entries. For now we do not try
2312 * to copy partial entries to avoid complexity of running out
2313 * of sg_entry slots. The downside is reading a single byte
2314 * will copy the entire sg entry.
2315 */
2316 do {
2317 copy += sg[i].length;
2318 i++;
2319 if (i == MAX_SKB_FRAGS)
2320 i = 0;
2321 if (bytes < copy)
2322 break;
2323 } while (i != msg->sg_end);
2324 last_sg = i;
2325
2326 if (unlikely(copy < end - start))
2327 return -EINVAL;
2328
2329 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC, get_order(copy));
2330 if (unlikely(!page))
2331 return -ENOMEM;
2332 p = page_address(page);
2333 offset = 0;
2334
2335 i = first_sg;
2336 do {
2337 from = sg_virt(&sg[i]);
2338 len = sg[i].length;
2339 to = p + offset;
2340
2341 memcpy(to, from, len);
2342 offset += len;
2343 sg[i].length = 0;
2344 put_page(sg_page(&sg[i]));
2345
2346 i++;
2347 if (i == MAX_SKB_FRAGS)
2348 i = 0;
2349 } while (i != last_sg);
2350
2351 sg[first_sg].length = copy;
2352 sg_set_page(&sg[first_sg], page, copy, 0);
2353
2354 /* To repair sg ring we need to shift entries. If we only
2355 * had a single entry though we can just replace it and
2356 * be done. Otherwise walk the ring and shift the entries.
2357 */
2358 shift = last_sg - first_sg - 1;
2359 if (!shift)
2360 goto out;
2361
2362 i = first_sg + 1;
2363 do {
2364 int move_from;
2365
2366 if (i + shift >= MAX_SKB_FRAGS)
2367 move_from = i + shift - MAX_SKB_FRAGS;
2368 else
2369 move_from = i + shift;
2370
2371 if (move_from == msg->sg_end)
2372 break;
2373
2374 sg[i] = sg[move_from];
2375 sg[move_from].length = 0;
2376 sg[move_from].page_link = 0;
2377 sg[move_from].offset = 0;
2378
2379 i++;
2380 if (i == MAX_SKB_FRAGS)
2381 i = 0;
2382 } while (1);
2383 msg->sg_end -= shift;
2384 if (msg->sg_end < 0)
2385 msg->sg_end += MAX_SKB_FRAGS;
2386out:
2387 msg->data = sg_virt(&sg[i]) + start - offset;
2388 msg->data_end = msg->data + bytes;
2389
2390 return 0;
2391}
2392
2393static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2394 .func = bpf_msg_pull_data,
2395 .gpl_only = false,
2396 .ret_type = RET_INTEGER,
2397 .arg1_type = ARG_PTR_TO_CTX,
2398 .arg2_type = ARG_ANYTHING,
2399 .arg3_type = ARG_ANYTHING,
2400 .arg4_type = ARG_ANYTHING,
2401};
2402
f3694e00 2403BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
8d20aabe 2404{
f3694e00 2405 return task_get_classid(skb);
8d20aabe
DB
2406}
2407
2408static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2409 .func = bpf_get_cgroup_classid,
2410 .gpl_only = false,
2411 .ret_type = RET_INTEGER,
2412 .arg1_type = ARG_PTR_TO_CTX,
2413};
2414
f3694e00 2415BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
c46646d0 2416{
f3694e00 2417 return dst_tclassid(skb);
c46646d0
DB
2418}
2419
2420static const struct bpf_func_proto bpf_get_route_realm_proto = {
2421 .func = bpf_get_route_realm,
2422 .gpl_only = false,
2423 .ret_type = RET_INTEGER,
2424 .arg1_type = ARG_PTR_TO_CTX,
2425};
2426
f3694e00 2427BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
13c5c240
DB
2428{
2429 /* If skb_clear_hash() was called due to mangling, we can
2430 * trigger SW recalculation here. Later access to hash
2431 * can then use the inline skb->hash via context directly
2432 * instead of calling this helper again.
2433 */
f3694e00 2434 return skb_get_hash(skb);
13c5c240
DB
2435}
2436
2437static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2438 .func = bpf_get_hash_recalc,
2439 .gpl_only = false,
2440 .ret_type = RET_INTEGER,
2441 .arg1_type = ARG_PTR_TO_CTX,
2442};
2443
7a4b28c6
DB
2444BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2445{
2446 /* After all direct packet write, this can be used once for
2447 * triggering a lazy recalc on next skb_get_hash() invocation.
2448 */
2449 skb_clear_hash(skb);
2450 return 0;
2451}
2452
2453static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2454 .func = bpf_set_hash_invalid,
2455 .gpl_only = false,
2456 .ret_type = RET_INTEGER,
2457 .arg1_type = ARG_PTR_TO_CTX,
2458};
2459
ded092cd
DB
2460BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2461{
2462 /* Set user specified hash as L4(+), so that it gets returned
2463 * on skb_get_hash() call unless BPF prog later on triggers a
2464 * skb_clear_hash().
2465 */
2466 __skb_set_sw_hash(skb, hash, true);
2467 return 0;
2468}
2469
2470static const struct bpf_func_proto bpf_set_hash_proto = {
2471 .func = bpf_set_hash,
2472 .gpl_only = false,
2473 .ret_type = RET_INTEGER,
2474 .arg1_type = ARG_PTR_TO_CTX,
2475 .arg2_type = ARG_ANYTHING,
2476};
2477
f3694e00
DB
2478BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2479 u16, vlan_tci)
4e10df9a 2480{
db58ba45 2481 int ret;
4e10df9a
AS
2482
2483 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2484 vlan_proto != htons(ETH_P_8021AD)))
2485 vlan_proto = htons(ETH_P_8021Q);
2486
8065694e 2487 bpf_push_mac_rcsum(skb);
db58ba45 2488 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
8065694e
DB
2489 bpf_pull_mac_rcsum(skb);
2490
6aaae2b6 2491 bpf_compute_data_pointers(skb);
db58ba45 2492 return ret;
4e10df9a
AS
2493}
2494
93731ef0 2495static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
4e10df9a
AS
2496 .func = bpf_skb_vlan_push,
2497 .gpl_only = false,
2498 .ret_type = RET_INTEGER,
2499 .arg1_type = ARG_PTR_TO_CTX,
2500 .arg2_type = ARG_ANYTHING,
2501 .arg3_type = ARG_ANYTHING,
2502};
2503
f3694e00 2504BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
4e10df9a 2505{
db58ba45 2506 int ret;
4e10df9a 2507
8065694e 2508 bpf_push_mac_rcsum(skb);
db58ba45 2509 ret = skb_vlan_pop(skb);
8065694e
DB
2510 bpf_pull_mac_rcsum(skb);
2511
6aaae2b6 2512 bpf_compute_data_pointers(skb);
db58ba45 2513 return ret;
4e10df9a
AS
2514}
2515
93731ef0 2516static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
4e10df9a
AS
2517 .func = bpf_skb_vlan_pop,
2518 .gpl_only = false,
2519 .ret_type = RET_INTEGER,
2520 .arg1_type = ARG_PTR_TO_CTX,
2521};
2522
6578171a
DB
2523static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2524{
2525 /* Caller already did skb_cow() with len as headroom,
2526 * so no need to do it here.
2527 */
2528 skb_push(skb, len);
2529 memmove(skb->data, skb->data + len, off);
2530 memset(skb->data + off, 0, len);
2531
2532 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2533 * needed here as it does not change the skb->csum
2534 * result for checksum complete when summing over
2535 * zeroed blocks.
2536 */
2537 return 0;
2538}
2539
2540static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2541{
2542 /* skb_ensure_writable() is not needed here, as we're
2543 * already working on an uncloned skb.
2544 */
2545 if (unlikely(!pskb_may_pull(skb, off + len)))
2546 return -ENOMEM;
2547
2548 skb_postpull_rcsum(skb, skb->data + off, len);
2549 memmove(skb->data + len, skb->data, off);
2550 __skb_pull(skb, len);
2551
2552 return 0;
2553}
2554
2555static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2556{
2557 bool trans_same = skb->transport_header == skb->network_header;
2558 int ret;
2559
2560 /* There's no need for __skb_push()/__skb_pull() pair to
2561 * get to the start of the mac header as we're guaranteed
2562 * to always start from here under eBPF.
2563 */
2564 ret = bpf_skb_generic_push(skb, off, len);
2565 if (likely(!ret)) {
2566 skb->mac_header -= len;
2567 skb->network_header -= len;
2568 if (trans_same)
2569 skb->transport_header = skb->network_header;
2570 }
2571
2572 return ret;
2573}
2574
2575static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2576{
2577 bool trans_same = skb->transport_header == skb->network_header;
2578 int ret;
2579
2580 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2581 ret = bpf_skb_generic_pop(skb, off, len);
2582 if (likely(!ret)) {
2583 skb->mac_header += len;
2584 skb->network_header += len;
2585 if (trans_same)
2586 skb->transport_header = skb->network_header;
2587 }
2588
2589 return ret;
2590}
2591
2592static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2593{
2594 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
0daf4349 2595 u32 off = skb_mac_header_len(skb);
6578171a
DB
2596 int ret;
2597
d02f51cb
DA
2598 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2599 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2600 return -ENOTSUPP;
2601
6578171a
DB
2602 ret = skb_cow(skb, len_diff);
2603 if (unlikely(ret < 0))
2604 return ret;
2605
2606 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2607 if (unlikely(ret < 0))
2608 return ret;
2609
2610 if (skb_is_gso(skb)) {
d02f51cb
DA
2611 struct skb_shared_info *shinfo = skb_shinfo(skb);
2612
880388aa
DM
2613 /* SKB_GSO_TCPV4 needs to be changed into
2614 * SKB_GSO_TCPV6.
6578171a 2615 */
d02f51cb
DA
2616 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2617 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2618 shinfo->gso_type |= SKB_GSO_TCPV6;
6578171a
DB
2619 }
2620
2621 /* Due to IPv6 header, MSS needs to be downgraded. */
d02f51cb 2622 skb_decrease_gso_size(shinfo, len_diff);
6578171a 2623 /* Header must be checked, and gso_segs recomputed. */
d02f51cb
DA
2624 shinfo->gso_type |= SKB_GSO_DODGY;
2625 shinfo->gso_segs = 0;
6578171a
DB
2626 }
2627
2628 skb->protocol = htons(ETH_P_IPV6);
2629 skb_clear_hash(skb);
2630
2631 return 0;
2632}
2633
2634static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2635{
2636 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
0daf4349 2637 u32 off = skb_mac_header_len(skb);
6578171a
DB
2638 int ret;
2639
d02f51cb
DA
2640 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2641 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2642 return -ENOTSUPP;
2643
6578171a
DB
2644 ret = skb_unclone(skb, GFP_ATOMIC);
2645 if (unlikely(ret < 0))
2646 return ret;
2647
2648 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2649 if (unlikely(ret < 0))
2650 return ret;
2651
2652 if (skb_is_gso(skb)) {
d02f51cb
DA
2653 struct skb_shared_info *shinfo = skb_shinfo(skb);
2654
880388aa
DM
2655 /* SKB_GSO_TCPV6 needs to be changed into
2656 * SKB_GSO_TCPV4.
6578171a 2657 */
d02f51cb
DA
2658 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2659 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2660 shinfo->gso_type |= SKB_GSO_TCPV4;
6578171a
DB
2661 }
2662
2663 /* Due to IPv4 header, MSS can be upgraded. */
d02f51cb 2664 skb_increase_gso_size(shinfo, len_diff);
6578171a 2665 /* Header must be checked, and gso_segs recomputed. */
d02f51cb
DA
2666 shinfo->gso_type |= SKB_GSO_DODGY;
2667 shinfo->gso_segs = 0;
6578171a
DB
2668 }
2669
2670 skb->protocol = htons(ETH_P_IP);
2671 skb_clear_hash(skb);
2672
2673 return 0;
2674}
2675
2676static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2677{
2678 __be16 from_proto = skb->protocol;
2679
2680 if (from_proto == htons(ETH_P_IP) &&
2681 to_proto == htons(ETH_P_IPV6))
2682 return bpf_skb_proto_4_to_6(skb);
2683
2684 if (from_proto == htons(ETH_P_IPV6) &&
2685 to_proto == htons(ETH_P_IP))
2686 return bpf_skb_proto_6_to_4(skb);
2687
2688 return -ENOTSUPP;
2689}
2690
f3694e00
DB
2691BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2692 u64, flags)
6578171a 2693{
6578171a
DB
2694 int ret;
2695
2696 if (unlikely(flags))
2697 return -EINVAL;
2698
2699 /* General idea is that this helper does the basic groundwork
2700 * needed for changing the protocol, and eBPF program fills the
2701 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2702 * and other helpers, rather than passing a raw buffer here.
2703 *
2704 * The rationale is to keep this minimal and without a need to
2705 * deal with raw packet data. F.e. even if we would pass buffers
2706 * here, the program still needs to call the bpf_lX_csum_replace()
2707 * helpers anyway. Plus, this way we keep also separation of
2708 * concerns, since f.e. bpf_skb_store_bytes() should only take
2709 * care of stores.
2710 *
2711 * Currently, additional options and extension header space are
2712 * not supported, but flags register is reserved so we can adapt
2713 * that. For offloads, we mark packet as dodgy, so that headers
2714 * need to be verified first.
2715 */
2716 ret = bpf_skb_proto_xlat(skb, proto);
6aaae2b6 2717 bpf_compute_data_pointers(skb);
6578171a
DB
2718 return ret;
2719}
2720
2721static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2722 .func = bpf_skb_change_proto,
2723 .gpl_only = false,
2724 .ret_type = RET_INTEGER,
2725 .arg1_type = ARG_PTR_TO_CTX,
2726 .arg2_type = ARG_ANYTHING,
2727 .arg3_type = ARG_ANYTHING,
2728};
2729
f3694e00 2730BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
d2485c42 2731{
d2485c42 2732 /* We only allow a restricted subset to be changed for now. */
45c7fffa
DB
2733 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2734 !skb_pkt_type_ok(pkt_type)))
d2485c42
DB
2735 return -EINVAL;
2736
2737 skb->pkt_type = pkt_type;
2738 return 0;
2739}
2740
2741static const struct bpf_func_proto bpf_skb_change_type_proto = {
2742 .func = bpf_skb_change_type,
2743 .gpl_only = false,
2744 .ret_type = RET_INTEGER,
2745 .arg1_type = ARG_PTR_TO_CTX,
2746 .arg2_type = ARG_ANYTHING,
2747};
2748
2be7e212
DB
2749static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2750{
2751 switch (skb->protocol) {
2752 case htons(ETH_P_IP):
2753 return sizeof(struct iphdr);
2754 case htons(ETH_P_IPV6):
2755 return sizeof(struct ipv6hdr);
2756 default:
2757 return ~0U;
2758 }
2759}
2760
2761static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2762{
2763 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2764 int ret;
2765
d02f51cb
DA
2766 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2767 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2768 return -ENOTSUPP;
2769
2be7e212
DB
2770 ret = skb_cow(skb, len_diff);
2771 if (unlikely(ret < 0))
2772 return ret;
2773
2774 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2775 if (unlikely(ret < 0))
2776 return ret;
2777
2778 if (skb_is_gso(skb)) {
d02f51cb
DA
2779 struct skb_shared_info *shinfo = skb_shinfo(skb);
2780
2be7e212 2781 /* Due to header grow, MSS needs to be downgraded. */
d02f51cb 2782 skb_decrease_gso_size(shinfo, len_diff);
2be7e212 2783 /* Header must be checked, and gso_segs recomputed. */
d02f51cb
DA
2784 shinfo->gso_type |= SKB_GSO_DODGY;
2785 shinfo->gso_segs = 0;
2be7e212
DB
2786 }
2787
2788 return 0;
2789}
2790
2791static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2792{
2793 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2794 int ret;
2795
d02f51cb
DA
2796 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2797 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2798 return -ENOTSUPP;
2799
2be7e212
DB
2800 ret = skb_unclone(skb, GFP_ATOMIC);
2801 if (unlikely(ret < 0))
2802 return ret;
2803
2804 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2805 if (unlikely(ret < 0))
2806 return ret;
2807
2808 if (skb_is_gso(skb)) {
d02f51cb
DA
2809 struct skb_shared_info *shinfo = skb_shinfo(skb);
2810
2be7e212 2811 /* Due to header shrink, MSS can be upgraded. */
d02f51cb 2812 skb_increase_gso_size(shinfo, len_diff);
2be7e212 2813 /* Header must be checked, and gso_segs recomputed. */
d02f51cb
DA
2814 shinfo->gso_type |= SKB_GSO_DODGY;
2815 shinfo->gso_segs = 0;
2be7e212
DB
2816 }
2817
2818 return 0;
2819}
2820
2821static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2822{
0c6bc6e5
JF
2823 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
2824 SKB_MAX_ALLOC;
2be7e212
DB
2825}
2826
2827static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2828{
2829 bool trans_same = skb->transport_header == skb->network_header;
2830 u32 len_cur, len_diff_abs = abs(len_diff);
2831 u32 len_min = bpf_skb_net_base_len(skb);
2832 u32 len_max = __bpf_skb_max_len(skb);
2833 __be16 proto = skb->protocol;
2834 bool shrink = len_diff < 0;
2835 int ret;
2836
2837 if (unlikely(len_diff_abs > 0xfffU))
2838 return -EFAULT;
2839 if (unlikely(proto != htons(ETH_P_IP) &&
2840 proto != htons(ETH_P_IPV6)))
2841 return -ENOTSUPP;
2842
2843 len_cur = skb->len - skb_network_offset(skb);
2844 if (skb_transport_header_was_set(skb) && !trans_same)
2845 len_cur = skb_network_header_len(skb);
2846 if ((shrink && (len_diff_abs >= len_cur ||
2847 len_cur - len_diff_abs < len_min)) ||
2848 (!shrink && (skb->len + len_diff_abs > len_max &&
2849 !skb_is_gso(skb))))
2850 return -ENOTSUPP;
2851
2852 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2853 bpf_skb_net_grow(skb, len_diff_abs);
2854
6aaae2b6 2855 bpf_compute_data_pointers(skb);
e4a6a342 2856 return ret;
2be7e212
DB
2857}
2858
2859BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2860 u32, mode, u64, flags)
2861{
2862 if (unlikely(flags))
2863 return -EINVAL;
2864 if (likely(mode == BPF_ADJ_ROOM_NET))
2865 return bpf_skb_adjust_net(skb, len_diff);
2866
2867 return -ENOTSUPP;
2868}
2869
2870static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2871 .func = bpf_skb_adjust_room,
2872 .gpl_only = false,
2873 .ret_type = RET_INTEGER,
2874 .arg1_type = ARG_PTR_TO_CTX,
2875 .arg2_type = ARG_ANYTHING,
2876 .arg3_type = ARG_ANYTHING,
2877 .arg4_type = ARG_ANYTHING,
2878};
2879
5293efe6
DB
2880static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2881{
2882 u32 min_len = skb_network_offset(skb);
2883
2884 if (skb_transport_header_was_set(skb))
2885 min_len = skb_transport_offset(skb);
2886 if (skb->ip_summed == CHECKSUM_PARTIAL)
2887 min_len = skb_checksum_start_offset(skb) +
2888 skb->csum_offset + sizeof(__sum16);
2889 return min_len;
2890}
2891
5293efe6
DB
2892static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2893{
2894 unsigned int old_len = skb->len;
2895 int ret;
2896
2897 ret = __skb_grow_rcsum(skb, new_len);
2898 if (!ret)
2899 memset(skb->data + old_len, 0, new_len - old_len);
2900 return ret;
2901}
2902
2903static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2904{
2905 return __skb_trim_rcsum(skb, new_len);
2906}
2907
0ea488ff
JF
2908static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
2909 u64 flags)
5293efe6 2910{
5293efe6
DB
2911 u32 max_len = __bpf_skb_max_len(skb);
2912 u32 min_len = __bpf_skb_min_len(skb);
5293efe6
DB
2913 int ret;
2914
2915 if (unlikely(flags || new_len > max_len || new_len < min_len))
2916 return -EINVAL;
2917 if (skb->encapsulation)
2918 return -ENOTSUPP;
2919
2920 /* The basic idea of this helper is that it's performing the
2921 * needed work to either grow or trim an skb, and eBPF program
2922 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2923 * bpf_lX_csum_replace() and others rather than passing a raw
2924 * buffer here. This one is a slow path helper and intended
2925 * for replies with control messages.
2926 *
2927 * Like in bpf_skb_change_proto(), we want to keep this rather
2928 * minimal and without protocol specifics so that we are able
2929 * to separate concerns as in bpf_skb_store_bytes() should only
2930 * be the one responsible for writing buffers.
2931 *
2932 * It's really expected to be a slow path operation here for
2933 * control message replies, so we're implicitly linearizing,
2934 * uncloning and drop offloads from the skb by this.
2935 */
2936 ret = __bpf_try_make_writable(skb, skb->len);
2937 if (!ret) {
2938 if (new_len > skb->len)
2939 ret = bpf_skb_grow_rcsum(skb, new_len);
2940 else if (new_len < skb->len)
2941 ret = bpf_skb_trim_rcsum(skb, new_len);
2942 if (!ret && skb_is_gso(skb))
2943 skb_gso_reset(skb);
2944 }
0ea488ff
JF
2945 return ret;
2946}
2947
2948BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2949 u64, flags)
2950{
2951 int ret = __bpf_skb_change_tail(skb, new_len, flags);
5293efe6 2952
6aaae2b6 2953 bpf_compute_data_pointers(skb);
5293efe6
DB
2954 return ret;
2955}
2956
2957static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2958 .func = bpf_skb_change_tail,
2959 .gpl_only = false,
2960 .ret_type = RET_INTEGER,
2961 .arg1_type = ARG_PTR_TO_CTX,
2962 .arg2_type = ARG_ANYTHING,
2963 .arg3_type = ARG_ANYTHING,
2964};
2965
0ea488ff 2966BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3a0af8fd 2967 u64, flags)
0ea488ff
JF
2968{
2969 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2970
2971 bpf_compute_data_end_sk_skb(skb);
2972 return ret;
2973}
2974
2975static const struct bpf_func_proto sk_skb_change_tail_proto = {
2976 .func = sk_skb_change_tail,
2977 .gpl_only = false,
2978 .ret_type = RET_INTEGER,
2979 .arg1_type = ARG_PTR_TO_CTX,
2980 .arg2_type = ARG_ANYTHING,
2981 .arg3_type = ARG_ANYTHING,
2982};
2983
2984static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
2985 u64 flags)
3a0af8fd
TG
2986{
2987 u32 max_len = __bpf_skb_max_len(skb);
2988 u32 new_len = skb->len + head_room;
2989 int ret;
2990
2991 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2992 new_len < skb->len))
2993 return -EINVAL;
2994
2995 ret = skb_cow(skb, head_room);
2996 if (likely(!ret)) {
2997 /* Idea for this helper is that we currently only
2998 * allow to expand on mac header. This means that
2999 * skb->protocol network header, etc, stay as is.
3000 * Compared to bpf_skb_change_tail(), we're more
3001 * flexible due to not needing to linearize or
3002 * reset GSO. Intention for this helper is to be
3003 * used by an L3 skb that needs to push mac header
3004 * for redirection into L2 device.
3005 */
3006 __skb_push(skb, head_room);
3007 memset(skb->data, 0, head_room);
3008 skb_reset_mac_header(skb);
3009 }
3010
0ea488ff
JF
3011 return ret;
3012}
3013
3014BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3015 u64, flags)
3016{
3017 int ret = __bpf_skb_change_head(skb, head_room, flags);
3018
6aaae2b6 3019 bpf_compute_data_pointers(skb);
0ea488ff 3020 return ret;
3a0af8fd
TG
3021}
3022
3023static const struct bpf_func_proto bpf_skb_change_head_proto = {
3024 .func = bpf_skb_change_head,
3025 .gpl_only = false,
3026 .ret_type = RET_INTEGER,
3027 .arg1_type = ARG_PTR_TO_CTX,
3028 .arg2_type = ARG_ANYTHING,
3029 .arg3_type = ARG_ANYTHING,
3030};
3031
0ea488ff
JF
3032BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3033 u64, flags)
3034{
3035 int ret = __bpf_skb_change_head(skb, head_room, flags);
3036
3037 bpf_compute_data_end_sk_skb(skb);
3038 return ret;
3039}
3040
3041static const struct bpf_func_proto sk_skb_change_head_proto = {
3042 .func = sk_skb_change_head,
3043 .gpl_only = false,
3044 .ret_type = RET_INTEGER,
3045 .arg1_type = ARG_PTR_TO_CTX,
3046 .arg2_type = ARG_ANYTHING,
3047 .arg3_type = ARG_ANYTHING,
3048};
de8f3a83
DB
3049static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3050{
3051 return xdp_data_meta_unsupported(xdp) ? 0 :
3052 xdp->data - xdp->data_meta;
3053}
3054
17bedab2
MKL
3055BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3056{
6dfb970d 3057 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
de8f3a83 3058 unsigned long metalen = xdp_get_metalen(xdp);
97e19cce 3059 void *data_start = xdp_frame_end + metalen;
17bedab2
MKL
3060 void *data = xdp->data + offset;
3061
de8f3a83 3062 if (unlikely(data < data_start ||
17bedab2
MKL
3063 data > xdp->data_end - ETH_HLEN))
3064 return -EINVAL;
3065
de8f3a83
DB
3066 if (metalen)
3067 memmove(xdp->data_meta + offset,
3068 xdp->data_meta, metalen);
3069 xdp->data_meta += offset;
17bedab2
MKL
3070 xdp->data = data;
3071
3072 return 0;
3073}
3074
3075static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3076 .func = bpf_xdp_adjust_head,
3077 .gpl_only = false,
3078 .ret_type = RET_INTEGER,
3079 .arg1_type = ARG_PTR_TO_CTX,
3080 .arg2_type = ARG_ANYTHING,
3081};
3082
b32cc5b9
NS
3083BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3084{
3085 void *data_end = xdp->data_end + offset;
3086
3087 /* only shrinking is allowed for now. */
3088 if (unlikely(offset >= 0))
3089 return -EINVAL;
3090
3091 if (unlikely(data_end < xdp->data + ETH_HLEN))
3092 return -EINVAL;
3093
3094 xdp->data_end = data_end;
3095
3096 return 0;
3097}
3098
3099static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3100 .func = bpf_xdp_adjust_tail,
3101 .gpl_only = false,
3102 .ret_type = RET_INTEGER,
3103 .arg1_type = ARG_PTR_TO_CTX,
3104 .arg2_type = ARG_ANYTHING,
3105};
3106
de8f3a83
DB
3107BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3108{
97e19cce 3109 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
de8f3a83
DB
3110 void *meta = xdp->data_meta + offset;
3111 unsigned long metalen = xdp->data - meta;
3112
3113 if (xdp_data_meta_unsupported(xdp))
3114 return -ENOTSUPP;
97e19cce 3115 if (unlikely(meta < xdp_frame_end ||
de8f3a83
DB
3116 meta > xdp->data))
3117 return -EINVAL;
3118 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3119 (metalen > 32)))
3120 return -EACCES;
3121
3122 xdp->data_meta = meta;
3123
3124 return 0;
3125}
3126
3127static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3128 .func = bpf_xdp_adjust_meta,
3129 .gpl_only = false,
3130 .ret_type = RET_INTEGER,
3131 .arg1_type = ARG_PTR_TO_CTX,
3132 .arg2_type = ARG_ANYTHING,
3133};
3134
11393cc9
JF
3135static int __bpf_tx_xdp(struct net_device *dev,
3136 struct bpf_map *map,
3137 struct xdp_buff *xdp,
3138 u32 index)
814abfab 3139{
44fa2dbd 3140 struct xdp_frame *xdpf;
d8d7218a 3141 int err, sent;
11393cc9
JF
3142
3143 if (!dev->netdev_ops->ndo_xdp_xmit) {
11393cc9 3144 return -EOPNOTSUPP;
814abfab 3145 }
11393cc9 3146
d8d7218a
TM
3147 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3148 if (unlikely(err))
3149 return err;
3150
44fa2dbd
JDB
3151 xdpf = convert_to_xdp_frame(xdp);
3152 if (unlikely(!xdpf))
3153 return -EOVERFLOW;
3154
1e67575a 3155 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
735fc405
JDB
3156 if (sent <= 0)
3157 return sent;
9c270af3
JDB
3158 return 0;
3159}
3160
3161static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3162 struct bpf_map *map,
3163 struct xdp_buff *xdp,
3164 u32 index)
3165{
3166 int err;
3167
1b1a251c
BT
3168 switch (map->map_type) {
3169 case BPF_MAP_TYPE_DEVMAP: {
67f29e07 3170 struct bpf_dtab_netdev *dst = fwd;
9c270af3 3171
38edddb8 3172 err = dev_map_enqueue(dst, xdp, dev_rx);
9c270af3
JDB
3173 if (err)
3174 return err;
11393cc9 3175 __dev_map_insert_ctx(map, index);
1b1a251c
BT
3176 break;
3177 }
3178 case BPF_MAP_TYPE_CPUMAP: {
9c270af3
JDB
3179 struct bpf_cpu_map_entry *rcpu = fwd;
3180
3181 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3182 if (err)
3183 return err;
3184 __cpu_map_insert_ctx(map, index);
1b1a251c
BT
3185 break;
3186 }
3187 case BPF_MAP_TYPE_XSKMAP: {
3188 struct xdp_sock *xs = fwd;
3189
3190 err = __xsk_map_redirect(map, xdp, xs);
3191 return err;
3192 }
3193 default:
3194 break;
9c270af3 3195 }
e4a8e817 3196 return 0;
814abfab
JF
3197}
3198
11393cc9
JF
3199void xdp_do_flush_map(void)
3200{
3201 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3202 struct bpf_map *map = ri->map_to_flush;
3203
11393cc9 3204 ri->map_to_flush = NULL;
9c270af3
JDB
3205 if (map) {
3206 switch (map->map_type) {
3207 case BPF_MAP_TYPE_DEVMAP:
3208 __dev_map_flush(map);
3209 break;
3210 case BPF_MAP_TYPE_CPUMAP:
3211 __cpu_map_flush(map);
3212 break;
1b1a251c
BT
3213 case BPF_MAP_TYPE_XSKMAP:
3214 __xsk_map_flush(map);
3215 break;
9c270af3
JDB
3216 default:
3217 break;
3218 }
3219 }
11393cc9
JF
3220}
3221EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3222
9c270af3
JDB
3223static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3224{
3225 switch (map->map_type) {
3226 case BPF_MAP_TYPE_DEVMAP:
3227 return __dev_map_lookup_elem(map, index);
3228 case BPF_MAP_TYPE_CPUMAP:
3229 return __cpu_map_lookup_elem(map, index);
1b1a251c
BT
3230 case BPF_MAP_TYPE_XSKMAP:
3231 return __xsk_map_lookup_elem(map, index);
9c270af3
JDB
3232 default:
3233 return NULL;
3234 }
3235}
3236
7c300131
DB
3237static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
3238 unsigned long aux)
3239{
3240 return (unsigned long)xdp_prog->aux != aux;
3241}
3242
e4a8e817
DB
3243static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3244 struct bpf_prog *xdp_prog)
97f91a7c
JF
3245{
3246 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
7c300131 3247 unsigned long map_owner = ri->map_owner;
97f91a7c 3248 struct bpf_map *map = ri->map;
11393cc9 3249 u32 index = ri->ifindex;
9c270af3 3250 void *fwd = NULL;
4c03bdd7 3251 int err;
97f91a7c
JF
3252
3253 ri->ifindex = 0;
3254 ri->map = NULL;
7c300131 3255 ri->map_owner = 0;
109980b8 3256
7c300131 3257 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
96c5508e
JDB
3258 err = -EFAULT;
3259 map = NULL;
3260 goto err;
3261 }
97f91a7c 3262
9c270af3 3263 fwd = __xdp_map_lookup_elem(map, index);
4c03bdd7
JDB
3264 if (!fwd) {
3265 err = -EINVAL;
f5836ca5 3266 goto err;
4c03bdd7 3267 }
e4a8e817 3268 if (ri->map_to_flush && ri->map_to_flush != map)
11393cc9
JF
3269 xdp_do_flush_map();
3270
9c270af3 3271 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
f5836ca5
JDB
3272 if (unlikely(err))
3273 goto err;
3274
3275 ri->map_to_flush = map;
59a30896 3276 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
f5836ca5
JDB
3277 return 0;
3278err:
59a30896 3279 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
97f91a7c
JF
3280 return err;
3281}
3282
5acaee0a
JF
3283int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3284 struct bpf_prog *xdp_prog)
814abfab
JF
3285{
3286 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
5acaee0a 3287 struct net_device *fwd;
eb48d682 3288 u32 index = ri->ifindex;
4c03bdd7 3289 int err;
814abfab 3290
97f91a7c
JF
3291 if (ri->map)
3292 return xdp_do_redirect_map(dev, xdp, xdp_prog);
3293
eb48d682 3294 fwd = dev_get_by_index_rcu(dev_net(dev), index);
814abfab 3295 ri->ifindex = 0;
5acaee0a 3296 if (unlikely(!fwd)) {
4c03bdd7 3297 err = -EINVAL;
f5836ca5 3298 goto err;
814abfab
JF
3299 }
3300
4c03bdd7 3301 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
f5836ca5
JDB
3302 if (unlikely(err))
3303 goto err;
3304
3305 _trace_xdp_redirect(dev, xdp_prog, index);
3306 return 0;
3307err:
3308 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
4c03bdd7 3309 return err;
814abfab
JF
3310}
3311EXPORT_SYMBOL_GPL(xdp_do_redirect);
3312
c060bc61
XS
3313static int xdp_do_generic_redirect_map(struct net_device *dev,
3314 struct sk_buff *skb,
02671e23 3315 struct xdp_buff *xdp,
c060bc61 3316 struct bpf_prog *xdp_prog)
6103aa96
JF
3317{
3318 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
7c300131 3319 unsigned long map_owner = ri->map_owner;
96c5508e 3320 struct bpf_map *map = ri->map;
eb48d682 3321 u32 index = ri->ifindex;
02671e23 3322 void *fwd = NULL;
2facaad6 3323 int err = 0;
6103aa96 3324
6103aa96 3325 ri->ifindex = 0;
96c5508e 3326 ri->map = NULL;
7c300131 3327 ri->map_owner = 0;
96c5508e 3328
9c270af3
JDB
3329 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3330 err = -EFAULT;
3331 map = NULL;
3332 goto err;
96c5508e 3333 }
9c270af3 3334 fwd = __xdp_map_lookup_elem(map, index);
2facaad6
JDB
3335 if (unlikely(!fwd)) {
3336 err = -EINVAL;
f5836ca5 3337 goto err;
6103aa96
JF
3338 }
3339
9c270af3 3340 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
6d5fc195
TM
3341 struct bpf_dtab_netdev *dst = fwd;
3342
3343 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3344 if (unlikely(err))
9c270af3 3345 goto err;
02671e23
BT
3346 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3347 struct xdp_sock *xs = fwd;
3348
3349 err = xsk_generic_rcv(xs, xdp);
3350 if (err)
3351 goto err;
3352 consume_skb(skb);
9c270af3
JDB
3353 } else {
3354 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3355 err = -EBADRQC;
f5836ca5 3356 goto err;
2facaad6 3357 }
6103aa96 3358
9c270af3
JDB
3359 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3360 return 0;
3361err:
3362 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3363 return err;
3364}
3365
3366int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
02671e23 3367 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
9c270af3
JDB
3368{
3369 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3370 u32 index = ri->ifindex;
3371 struct net_device *fwd;
3372 int err = 0;
3373
3374 if (ri->map)
02671e23 3375 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog);
9c270af3
JDB
3376
3377 ri->ifindex = 0;
3378 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3379 if (unlikely(!fwd)) {
3380 err = -EINVAL;
f5836ca5 3381 goto err;
2facaad6
JDB
3382 }
3383
d8d7218a
TM
3384 err = xdp_ok_fwd_dev(fwd, skb->len);
3385 if (unlikely(err))
9c270af3
JDB
3386 goto err;
3387
2facaad6 3388 skb->dev = fwd;
9c270af3 3389 _trace_xdp_redirect(dev, xdp_prog, index);
02671e23 3390 generic_xdp_tx(skb, xdp_prog);
f5836ca5
JDB
3391 return 0;
3392err:
9c270af3 3393 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2facaad6 3394 return err;
6103aa96
JF
3395}
3396EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3397
814abfab
JF
3398BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3399{
3400 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3401
3402 if (unlikely(flags))
3403 return XDP_ABORTED;
3404
3405 ri->ifindex = ifindex;
3406 ri->flags = flags;
109980b8 3407 ri->map = NULL;
7c300131 3408 ri->map_owner = 0;
e4a8e817 3409
814abfab
JF
3410 return XDP_REDIRECT;
3411}
3412
3413static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3414 .func = bpf_xdp_redirect,
3415 .gpl_only = false,
3416 .ret_type = RET_INTEGER,
3417 .arg1_type = ARG_ANYTHING,
3418 .arg2_type = ARG_ANYTHING,
3419};
3420
109980b8 3421BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
7c300131 3422 unsigned long, map_owner)
e4a8e817
DB
3423{
3424 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3425
3426 if (unlikely(flags))
3427 return XDP_ABORTED;
3428
3429 ri->ifindex = ifindex;
3430 ri->flags = flags;
3431 ri->map = map;
109980b8 3432 ri->map_owner = map_owner;
e4a8e817
DB
3433
3434 return XDP_REDIRECT;
3435}
3436
109980b8
DB
3437/* Note, arg4 is hidden from users and populated by the verifier
3438 * with the right pointer.
3439 */
e4a8e817
DB
3440static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3441 .func = bpf_xdp_redirect_map,
3442 .gpl_only = false,
3443 .ret_type = RET_INTEGER,
3444 .arg1_type = ARG_CONST_MAP_PTR,
3445 .arg2_type = ARG_ANYTHING,
3446 .arg3_type = ARG_ANYTHING,
3447};
3448
555c8a86 3449static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
aa7145c1 3450 unsigned long off, unsigned long len)
555c8a86 3451{
aa7145c1 3452 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
555c8a86
DB
3453
3454 if (unlikely(!ptr))
3455 return len;
3456 if (ptr != dst_buff)
3457 memcpy(dst_buff, ptr, len);
3458
3459 return 0;
3460}
3461
f3694e00
DB
3462BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3463 u64, flags, void *, meta, u64, meta_size)
555c8a86 3464{
555c8a86 3465 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
555c8a86
DB
3466
3467 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3468 return -EINVAL;
3469 if (unlikely(skb_size > skb->len))
3470 return -EFAULT;
3471
3472 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3473 bpf_skb_copy);
3474}
3475
3476static const struct bpf_func_proto bpf_skb_event_output_proto = {
3477 .func = bpf_skb_event_output,
3478 .gpl_only = true,
3479 .ret_type = RET_INTEGER,
3480 .arg1_type = ARG_PTR_TO_CTX,
3481 .arg2_type = ARG_CONST_MAP_PTR,
3482 .arg3_type = ARG_ANYTHING,
39f19ebb 3483 .arg4_type = ARG_PTR_TO_MEM,
1728a4f2 3484 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
555c8a86
DB
3485};
3486
c6c33454
DB
3487static unsigned short bpf_tunnel_key_af(u64 flags)
3488{
3489 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3490}
3491
f3694e00
DB
3492BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3493 u32, size, u64, flags)
d3aa45ce 3494{
c6c33454
DB
3495 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3496 u8 compat[sizeof(struct bpf_tunnel_key)];
074f528e
DB
3497 void *to_orig = to;
3498 int err;
d3aa45ce 3499
074f528e
DB
3500 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3501 err = -EINVAL;
3502 goto err_clear;
3503 }
3504 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3505 err = -EPROTO;
3506 goto err_clear;
3507 }
c6c33454 3508 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
074f528e 3509 err = -EINVAL;
c6c33454 3510 switch (size) {
4018ab18 3511 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 3512 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4018ab18 3513 goto set_compat;
c6c33454
DB
3514 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3515 /* Fixup deprecated structure layouts here, so we have
3516 * a common path later on.
3517 */
3518 if (ip_tunnel_info_af(info) != AF_INET)
074f528e 3519 goto err_clear;
4018ab18 3520set_compat:
c6c33454
DB
3521 to = (struct bpf_tunnel_key *)compat;
3522 break;
3523 default:
074f528e 3524 goto err_clear;
c6c33454
DB
3525 }
3526 }
d3aa45ce
AS
3527
3528 to->tunnel_id = be64_to_cpu(info->key.tun_id);
c6c33454
DB
3529 to->tunnel_tos = info->key.tos;
3530 to->tunnel_ttl = info->key.ttl;
1fbc2e0c 3531 to->tunnel_ext = 0;
c6c33454 3532
4018ab18 3533 if (flags & BPF_F_TUNINFO_IPV6) {
c6c33454
DB
3534 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3535 sizeof(to->remote_ipv6));
4018ab18
DB
3536 to->tunnel_label = be32_to_cpu(info->key.label);
3537 } else {
c6c33454 3538 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
1fbc2e0c
DB
3539 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3540 to->tunnel_label = 0;
4018ab18 3541 }
c6c33454
DB
3542
3543 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
074f528e 3544 memcpy(to_orig, to, size);
d3aa45ce
AS
3545
3546 return 0;
074f528e
DB
3547err_clear:
3548 memset(to_orig, 0, size);
3549 return err;
d3aa45ce
AS
3550}
3551
577c50aa 3552static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
d3aa45ce
AS
3553 .func = bpf_skb_get_tunnel_key,
3554 .gpl_only = false,
3555 .ret_type = RET_INTEGER,
3556 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
3557 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3558 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
3559 .arg4_type = ARG_ANYTHING,
3560};
3561
f3694e00 3562BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
14ca0751 3563{
14ca0751 3564 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
074f528e 3565 int err;
14ca0751
DB
3566
3567 if (unlikely(!info ||
074f528e
DB
3568 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3569 err = -ENOENT;
3570 goto err_clear;
3571 }
3572 if (unlikely(size < info->options_len)) {
3573 err = -ENOMEM;
3574 goto err_clear;
3575 }
14ca0751
DB
3576
3577 ip_tunnel_info_opts_get(to, info);
074f528e
DB
3578 if (size > info->options_len)
3579 memset(to + info->options_len, 0, size - info->options_len);
14ca0751
DB
3580
3581 return info->options_len;
074f528e
DB
3582err_clear:
3583 memset(to, 0, size);
3584 return err;
14ca0751
DB
3585}
3586
3587static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3588 .func = bpf_skb_get_tunnel_opt,
3589 .gpl_only = false,
3590 .ret_type = RET_INTEGER,
3591 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
3592 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3593 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
3594};
3595
d3aa45ce
AS
3596static struct metadata_dst __percpu *md_dst;
3597
f3694e00
DB
3598BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3599 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
d3aa45ce 3600{
d3aa45ce 3601 struct metadata_dst *md = this_cpu_ptr(md_dst);
c6c33454 3602 u8 compat[sizeof(struct bpf_tunnel_key)];
d3aa45ce
AS
3603 struct ip_tunnel_info *info;
3604
22080870 3605 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
77a5196a 3606 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
d3aa45ce 3607 return -EINVAL;
c6c33454
DB
3608 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3609 switch (size) {
4018ab18 3610 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 3611 case offsetof(struct bpf_tunnel_key, tunnel_ext):
c6c33454
DB
3612 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3613 /* Fixup deprecated structure layouts here, so we have
3614 * a common path later on.
3615 */
3616 memcpy(compat, from, size);
3617 memset(compat + size, 0, sizeof(compat) - size);
f3694e00 3618 from = (const struct bpf_tunnel_key *) compat;
c6c33454
DB
3619 break;
3620 default:
3621 return -EINVAL;
3622 }
3623 }
c0e760c9
DB
3624 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3625 from->tunnel_ext))
4018ab18 3626 return -EINVAL;
d3aa45ce
AS
3627
3628 skb_dst_drop(skb);
3629 dst_hold((struct dst_entry *) md);
3630 skb_dst_set(skb, (struct dst_entry *) md);
3631
3632 info = &md->u.tun_info;
5540fbf4 3633 memset(info, 0, sizeof(*info));
d3aa45ce 3634 info->mode = IP_TUNNEL_INFO_TX;
c6c33454 3635
db3c6139 3636 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
22080870
DB
3637 if (flags & BPF_F_DONT_FRAGMENT)
3638 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
792f3dd6
WT
3639 if (flags & BPF_F_ZERO_CSUM_TX)
3640 info->key.tun_flags &= ~TUNNEL_CSUM;
77a5196a
WT
3641 if (flags & BPF_F_SEQ_NUMBER)
3642 info->key.tun_flags |= TUNNEL_SEQ;
22080870 3643
d3aa45ce 3644 info->key.tun_id = cpu_to_be64(from->tunnel_id);
c6c33454
DB
3645 info->key.tos = from->tunnel_tos;
3646 info->key.ttl = from->tunnel_ttl;
3647
3648 if (flags & BPF_F_TUNINFO_IPV6) {
3649 info->mode |= IP_TUNNEL_INFO_IPV6;
3650 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3651 sizeof(from->remote_ipv6));
4018ab18
DB
3652 info->key.label = cpu_to_be32(from->tunnel_label) &
3653 IPV6_FLOWLABEL_MASK;
c6c33454
DB
3654 } else {
3655 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3656 }
d3aa45ce
AS
3657
3658 return 0;
3659}
3660
577c50aa 3661static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
d3aa45ce
AS
3662 .func = bpf_skb_set_tunnel_key,
3663 .gpl_only = false,
3664 .ret_type = RET_INTEGER,
3665 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
3666 .arg2_type = ARG_PTR_TO_MEM,
3667 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
3668 .arg4_type = ARG_ANYTHING,
3669};
3670
f3694e00
DB
3671BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3672 const u8 *, from, u32, size)
14ca0751 3673{
14ca0751
DB
3674 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3675 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3676
3677 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3678 return -EINVAL;
fca5fdf6 3679 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
14ca0751
DB
3680 return -ENOMEM;
3681
256c87c1 3682 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
14ca0751
DB
3683
3684 return 0;
3685}
3686
3687static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3688 .func = bpf_skb_set_tunnel_opt,
3689 .gpl_only = false,
3690 .ret_type = RET_INTEGER,
3691 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
3692 .arg2_type = ARG_PTR_TO_MEM,
3693 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
3694};
3695
3696static const struct bpf_func_proto *
3697bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
d3aa45ce
AS
3698{
3699 if (!md_dst) {
d66f2b91
JK
3700 struct metadata_dst __percpu *tmp;
3701
3702 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3703 METADATA_IP_TUNNEL,
3704 GFP_KERNEL);
3705 if (!tmp)
d3aa45ce 3706 return NULL;
d66f2b91
JK
3707 if (cmpxchg(&md_dst, NULL, tmp))
3708 metadata_dst_free_percpu(tmp);
d3aa45ce 3709 }
14ca0751
DB
3710
3711 switch (which) {
3712 case BPF_FUNC_skb_set_tunnel_key:
3713 return &bpf_skb_set_tunnel_key_proto;
3714 case BPF_FUNC_skb_set_tunnel_opt:
3715 return &bpf_skb_set_tunnel_opt_proto;
3716 default:
3717 return NULL;
3718 }
d3aa45ce
AS
3719}
3720
f3694e00
DB
3721BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3722 u32, idx)
4a482f34 3723{
4a482f34
MKL
3724 struct bpf_array *array = container_of(map, struct bpf_array, map);
3725 struct cgroup *cgrp;
3726 struct sock *sk;
4a482f34 3727
2d48c5f9 3728 sk = skb_to_full_sk(skb);
4a482f34
MKL
3729 if (!sk || !sk_fullsock(sk))
3730 return -ENOENT;
f3694e00 3731 if (unlikely(idx >= array->map.max_entries))
4a482f34
MKL
3732 return -E2BIG;
3733
f3694e00 3734 cgrp = READ_ONCE(array->ptrs[idx]);
4a482f34
MKL
3735 if (unlikely(!cgrp))
3736 return -EAGAIN;
3737
54fd9c2d 3738 return sk_under_cgroup_hierarchy(sk, cgrp);
4a482f34
MKL
3739}
3740
747ea55e
DB
3741static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3742 .func = bpf_skb_under_cgroup,
4a482f34
MKL
3743 .gpl_only = false,
3744 .ret_type = RET_INTEGER,
3745 .arg1_type = ARG_PTR_TO_CTX,
3746 .arg2_type = ARG_CONST_MAP_PTR,
3747 .arg3_type = ARG_ANYTHING,
3748};
4a482f34 3749
cb20b08e
DB
3750#ifdef CONFIG_SOCK_CGROUP_DATA
3751BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
3752{
3753 struct sock *sk = skb_to_full_sk(skb);
3754 struct cgroup *cgrp;
3755
3756 if (!sk || !sk_fullsock(sk))
3757 return 0;
3758
3759 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3760 return cgrp->kn->id.id;
3761}
3762
3763static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
3764 .func = bpf_skb_cgroup_id,
3765 .gpl_only = false,
3766 .ret_type = RET_INTEGER,
3767 .arg1_type = ARG_PTR_TO_CTX,
3768};
3769#endif
3770
4de16969
DB
3771static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3772 unsigned long off, unsigned long len)
3773{
3774 memcpy(dst_buff, src_buff + off, len);
3775 return 0;
3776}
3777
f3694e00
DB
3778BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3779 u64, flags, void *, meta, u64, meta_size)
4de16969 3780{
4de16969 3781 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4de16969
DB
3782
3783 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3784 return -EINVAL;
3785 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3786 return -EFAULT;
3787
9c471370
MKL
3788 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3789 xdp_size, bpf_xdp_copy);
4de16969
DB
3790}
3791
3792static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3793 .func = bpf_xdp_event_output,
3794 .gpl_only = true,
3795 .ret_type = RET_INTEGER,
3796 .arg1_type = ARG_PTR_TO_CTX,
3797 .arg2_type = ARG_CONST_MAP_PTR,
3798 .arg3_type = ARG_ANYTHING,
39f19ebb 3799 .arg4_type = ARG_PTR_TO_MEM,
1728a4f2 3800 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4de16969
DB
3801};
3802
91b8270f
CF
3803BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3804{
3805 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3806}
3807
3808static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3809 .func = bpf_get_socket_cookie,
3810 .gpl_only = false,
3811 .ret_type = RET_INTEGER,
3812 .arg1_type = ARG_PTR_TO_CTX,
3813};
3814
6acc5c29
CF
3815BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3816{
3817 struct sock *sk = sk_to_full_sk(skb->sk);
3818 kuid_t kuid;
3819
3820 if (!sk || !sk_fullsock(sk))
3821 return overflowuid;
3822 kuid = sock_net_uid(sock_net(sk), sk);
3823 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3824}
3825
3826static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3827 .func = bpf_get_socket_uid,
3828 .gpl_only = false,
3829 .ret_type = RET_INTEGER,
3830 .arg1_type = ARG_PTR_TO_CTX,
3831};
3832
8c4b4c7e
LB
3833BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3834 int, level, int, optname, char *, optval, int, optlen)
3835{
3836 struct sock *sk = bpf_sock->sk;
3837 int ret = 0;
3838 int val;
3839
3840 if (!sk_fullsock(sk))
3841 return -EINVAL;
3842
3843 if (level == SOL_SOCKET) {
3844 if (optlen != sizeof(int))
3845 return -EINVAL;
3846 val = *((int *)optval);
3847
3848 /* Only some socketops are supported */
3849 switch (optname) {
3850 case SO_RCVBUF:
3851 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3852 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3853 break;
3854 case SO_SNDBUF:
3855 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3856 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3857 break;
3858 case SO_MAX_PACING_RATE:
3859 sk->sk_max_pacing_rate = val;
3860 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3861 sk->sk_max_pacing_rate);
3862 break;
3863 case SO_PRIORITY:
3864 sk->sk_priority = val;
3865 break;
3866 case SO_RCVLOWAT:
3867 if (val < 0)
3868 val = INT_MAX;
3869 sk->sk_rcvlowat = val ? : 1;
3870 break;
3871 case SO_MARK:
3872 sk->sk_mark = val;
3873 break;
3874 default:
3875 ret = -EINVAL;
3876 }
a5192c52 3877#ifdef CONFIG_INET
6f5c39fa
NS
3878 } else if (level == SOL_IP) {
3879 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3880 return -EINVAL;
3881
3882 val = *((int *)optval);
3883 /* Only some options are supported */
3884 switch (optname) {
3885 case IP_TOS:
3886 if (val < -1 || val > 0xff) {
3887 ret = -EINVAL;
3888 } else {
3889 struct inet_sock *inet = inet_sk(sk);
3890
3891 if (val == -1)
3892 val = 0;
3893 inet->tos = val;
3894 }
3895 break;
3896 default:
3897 ret = -EINVAL;
3898 }
6f9bd3d7
LB
3899#if IS_ENABLED(CONFIG_IPV6)
3900 } else if (level == SOL_IPV6) {
3901 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3902 return -EINVAL;
3903
3904 val = *((int *)optval);
3905 /* Only some options are supported */
3906 switch (optname) {
3907 case IPV6_TCLASS:
3908 if (val < -1 || val > 0xff) {
3909 ret = -EINVAL;
3910 } else {
3911 struct ipv6_pinfo *np = inet6_sk(sk);
3912
3913 if (val == -1)
3914 val = 0;
3915 np->tclass = val;
3916 }
3917 break;
3918 default:
3919 ret = -EINVAL;
3920 }
3921#endif
8c4b4c7e
LB
3922 } else if (level == SOL_TCP &&
3923 sk->sk_prot->setsockopt == tcp_setsockopt) {
91b5b21c
LB
3924 if (optname == TCP_CONGESTION) {
3925 char name[TCP_CA_NAME_MAX];
ebfa00c5 3926 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
91b5b21c
LB
3927
3928 strncpy(name, optval, min_t(long, optlen,
3929 TCP_CA_NAME_MAX-1));
3930 name[TCP_CA_NAME_MAX-1] = 0;
6f9bd3d7
LB
3931 ret = tcp_set_congestion_control(sk, name, false,
3932 reinit);
91b5b21c 3933 } else {
fc747810
LB
3934 struct tcp_sock *tp = tcp_sk(sk);
3935
3936 if (optlen != sizeof(int))
3937 return -EINVAL;
3938
3939 val = *((int *)optval);
3940 /* Only some options are supported */
3941 switch (optname) {
3942 case TCP_BPF_IW:
3943 if (val <= 0 || tp->data_segs_out > 0)
3944 ret = -EINVAL;
3945 else
3946 tp->snd_cwnd = val;
3947 break;
13bf9641
LB
3948 case TCP_BPF_SNDCWND_CLAMP:
3949 if (val <= 0) {
3950 ret = -EINVAL;
3951 } else {
3952 tp->snd_cwnd_clamp = val;
3953 tp->snd_ssthresh = val;
3954 }
6d3f06a0 3955 break;
fc747810
LB
3956 default:
3957 ret = -EINVAL;
3958 }
91b5b21c 3959 }
91b5b21c 3960#endif
8c4b4c7e
LB
3961 } else {
3962 ret = -EINVAL;
3963 }
3964 return ret;
3965}
3966
3967static const struct bpf_func_proto bpf_setsockopt_proto = {
3968 .func = bpf_setsockopt,
cd86d1fd 3969 .gpl_only = false,
8c4b4c7e
LB
3970 .ret_type = RET_INTEGER,
3971 .arg1_type = ARG_PTR_TO_CTX,
3972 .arg2_type = ARG_ANYTHING,
3973 .arg3_type = ARG_ANYTHING,
3974 .arg4_type = ARG_PTR_TO_MEM,
3975 .arg5_type = ARG_CONST_SIZE,
3976};
3977
cd86d1fd
LB
3978BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3979 int, level, int, optname, char *, optval, int, optlen)
3980{
3981 struct sock *sk = bpf_sock->sk;
cd86d1fd
LB
3982
3983 if (!sk_fullsock(sk))
3984 goto err_clear;
3985
3986#ifdef CONFIG_INET
3987 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3988 if (optname == TCP_CONGESTION) {
3989 struct inet_connection_sock *icsk = inet_csk(sk);
3990
3991 if (!icsk->icsk_ca_ops || optlen <= 1)
3992 goto err_clear;
3993 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3994 optval[optlen - 1] = 0;
3995 } else {
3996 goto err_clear;
3997 }
6f5c39fa
NS
3998 } else if (level == SOL_IP) {
3999 struct inet_sock *inet = inet_sk(sk);
4000
4001 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4002 goto err_clear;
4003
4004 /* Only some options are supported */
4005 switch (optname) {
4006 case IP_TOS:
4007 *((int *)optval) = (int)inet->tos;
4008 break;
4009 default:
4010 goto err_clear;
4011 }
6f9bd3d7
LB
4012#if IS_ENABLED(CONFIG_IPV6)
4013 } else if (level == SOL_IPV6) {
4014 struct ipv6_pinfo *np = inet6_sk(sk);
4015
4016 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4017 goto err_clear;
4018
4019 /* Only some options are supported */
4020 switch (optname) {
4021 case IPV6_TCLASS:
4022 *((int *)optval) = (int)np->tclass;
4023 break;
4024 default:
4025 goto err_clear;
4026 }
4027#endif
cd86d1fd
LB
4028 } else {
4029 goto err_clear;
4030 }
aa2bc739 4031 return 0;
cd86d1fd
LB
4032#endif
4033err_clear:
4034 memset(optval, 0, optlen);
4035 return -EINVAL;
4036}
4037
4038static const struct bpf_func_proto bpf_getsockopt_proto = {
4039 .func = bpf_getsockopt,
4040 .gpl_only = false,
4041 .ret_type = RET_INTEGER,
4042 .arg1_type = ARG_PTR_TO_CTX,
4043 .arg2_type = ARG_ANYTHING,
4044 .arg3_type = ARG_ANYTHING,
4045 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4046 .arg5_type = ARG_CONST_SIZE,
4047};
4048
b13d8807
LB
4049BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4050 int, argval)
4051{
4052 struct sock *sk = bpf_sock->sk;
4053 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4054
a7dcdf6e 4055 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
b13d8807
LB
4056 return -EINVAL;
4057
b13d8807
LB
4058 if (val)
4059 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4060
4061 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
b13d8807
LB
4062}
4063
4064static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4065 .func = bpf_sock_ops_cb_flags_set,
4066 .gpl_only = false,
4067 .ret_type = RET_INTEGER,
4068 .arg1_type = ARG_PTR_TO_CTX,
4069 .arg2_type = ARG_ANYTHING,
4070};
4071
d74bad4e
AI
4072const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4073EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4074
4075BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4076 int, addr_len)
4077{
4078#ifdef CONFIG_INET
4079 struct sock *sk = ctx->sk;
4080 int err;
4081
4082 /* Binding to port can be expensive so it's prohibited in the helper.
4083 * Only binding to IP is supported.
4084 */
4085 err = -EINVAL;
4086 if (addr->sa_family == AF_INET) {
4087 if (addr_len < sizeof(struct sockaddr_in))
4088 return err;
4089 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4090 return err;
4091 return __inet_bind(sk, addr, addr_len, true, false);
4092#if IS_ENABLED(CONFIG_IPV6)
4093 } else if (addr->sa_family == AF_INET6) {
4094 if (addr_len < SIN6_LEN_RFC2133)
4095 return err;
4096 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4097 return err;
4098 /* ipv6_bpf_stub cannot be NULL, since it's called from
4099 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4100 */
4101 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4102#endif /* CONFIG_IPV6 */
4103 }
4104#endif /* CONFIG_INET */
4105
4106 return -EAFNOSUPPORT;
4107}
4108
4109static const struct bpf_func_proto bpf_bind_proto = {
4110 .func = bpf_bind,
4111 .gpl_only = false,
4112 .ret_type = RET_INTEGER,
4113 .arg1_type = ARG_PTR_TO_CTX,
4114 .arg2_type = ARG_PTR_TO_MEM,
4115 .arg3_type = ARG_CONST_SIZE,
4116};
4117
12bed760
EB
4118#ifdef CONFIG_XFRM
4119BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4120 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4121{
4122 const struct sec_path *sp = skb_sec_path(skb);
4123 const struct xfrm_state *x;
4124
4125 if (!sp || unlikely(index >= sp->len || flags))
4126 goto err_clear;
4127
4128 x = sp->xvec[index];
4129
4130 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4131 goto err_clear;
4132
4133 to->reqid = x->props.reqid;
4134 to->spi = x->id.spi;
4135 to->family = x->props.family;
1fbc2e0c
DB
4136 to->ext = 0;
4137
12bed760
EB
4138 if (to->family == AF_INET6) {
4139 memcpy(to->remote_ipv6, x->props.saddr.a6,
4140 sizeof(to->remote_ipv6));
4141 } else {
4142 to->remote_ipv4 = x->props.saddr.a4;
1fbc2e0c 4143 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
12bed760
EB
4144 }
4145
4146 return 0;
4147err_clear:
4148 memset(to, 0, size);
4149 return -EINVAL;
4150}
4151
4152static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4153 .func = bpf_skb_get_xfrm_state,
4154 .gpl_only = false,
4155 .ret_type = RET_INTEGER,
4156 .arg1_type = ARG_PTR_TO_CTX,
4157 .arg2_type = ARG_ANYTHING,
4158 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4159 .arg4_type = ARG_CONST_SIZE,
4160 .arg5_type = ARG_ANYTHING,
4161};
4162#endif
4163
87f5fc7e
DA
4164#if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4165static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4166 const struct neighbour *neigh,
4167 const struct net_device *dev)
4168{
4169 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4170 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4171 params->h_vlan_TCI = 0;
4172 params->h_vlan_proto = 0;
4c79579b 4173 params->ifindex = dev->ifindex;
87f5fc7e 4174
4c79579b 4175 return 0;
87f5fc7e
DA
4176}
4177#endif
4178
4179#if IS_ENABLED(CONFIG_INET)
4180static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4f74fede 4181 u32 flags, bool check_mtu)
87f5fc7e
DA
4182{
4183 struct in_device *in_dev;
4184 struct neighbour *neigh;
4185 struct net_device *dev;
4186 struct fib_result res;
4187 struct fib_nh *nh;
4188 struct flowi4 fl4;
4189 int err;
4f74fede 4190 u32 mtu;
87f5fc7e
DA
4191
4192 dev = dev_get_by_index_rcu(net, params->ifindex);
4193 if (unlikely(!dev))
4194 return -ENODEV;
4195
4196 /* verify forwarding is enabled on this interface */
4197 in_dev = __in_dev_get_rcu(dev);
4198 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4c79579b 4199 return BPF_FIB_LKUP_RET_FWD_DISABLED;
87f5fc7e
DA
4200
4201 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4202 fl4.flowi4_iif = 1;
4203 fl4.flowi4_oif = params->ifindex;
4204 } else {
4205 fl4.flowi4_iif = params->ifindex;
4206 fl4.flowi4_oif = 0;
4207 }
4208 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4209 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4210 fl4.flowi4_flags = 0;
4211
4212 fl4.flowi4_proto = params->l4_protocol;
4213 fl4.daddr = params->ipv4_dst;
4214 fl4.saddr = params->ipv4_src;
4215 fl4.fl4_sport = params->sport;
4216 fl4.fl4_dport = params->dport;
4217
4218 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4219 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4220 struct fib_table *tb;
4221
4222 tb = fib_get_table(net, tbid);
4223 if (unlikely(!tb))
4c79579b 4224 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4225
4226 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4227 } else {
4228 fl4.flowi4_mark = 0;
4229 fl4.flowi4_secid = 0;
4230 fl4.flowi4_tun_key.tun_id = 0;
4231 fl4.flowi4_uid = sock_net_uid(net, NULL);
4232
4233 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4234 }
4235
4c79579b
DA
4236 if (err) {
4237 /* map fib lookup errors to RTN_ type */
4238 if (err == -EINVAL)
4239 return BPF_FIB_LKUP_RET_BLACKHOLE;
4240 if (err == -EHOSTUNREACH)
4241 return BPF_FIB_LKUP_RET_UNREACHABLE;
4242 if (err == -EACCES)
4243 return BPF_FIB_LKUP_RET_PROHIBIT;
4244
4245 return BPF_FIB_LKUP_RET_NOT_FWDED;
4246 }
4247
4248 if (res.type != RTN_UNICAST)
4249 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4250
4251 if (res.fi->fib_nhs > 1)
4252 fib_select_path(net, &res, &fl4, NULL);
4253
4f74fede
DA
4254 if (check_mtu) {
4255 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4256 if (params->tot_len > mtu)
4c79579b 4257 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4f74fede
DA
4258 }
4259
87f5fc7e
DA
4260 nh = &res.fi->fib_nh[res.nh_sel];
4261
4262 /* do not handle lwt encaps right now */
4263 if (nh->nh_lwtstate)
4c79579b 4264 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
87f5fc7e
DA
4265
4266 dev = nh->nh_dev;
87f5fc7e
DA
4267 if (nh->nh_gw)
4268 params->ipv4_dst = nh->nh_gw;
4269
4270 params->rt_metric = res.fi->fib_priority;
4271
4272 /* xdp and cls_bpf programs are run in RCU-bh so
4273 * rcu_read_lock_bh is not needed here
4274 */
4275 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4c79579b
DA
4276 if (!neigh)
4277 return BPF_FIB_LKUP_RET_NO_NEIGH;
87f5fc7e 4278
4c79579b 4279 return bpf_fib_set_fwd_params(params, neigh, dev);
87f5fc7e
DA
4280}
4281#endif
4282
4283#if IS_ENABLED(CONFIG_IPV6)
4284static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4f74fede 4285 u32 flags, bool check_mtu)
87f5fc7e
DA
4286{
4287 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4288 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4289 struct neighbour *neigh;
4290 struct net_device *dev;
4291 struct inet6_dev *idev;
4292 struct fib6_info *f6i;
4293 struct flowi6 fl6;
4294 int strict = 0;
4295 int oif;
4f74fede 4296 u32 mtu;
87f5fc7e
DA
4297
4298 /* link local addresses are never forwarded */
4299 if (rt6_need_strict(dst) || rt6_need_strict(src))
4c79579b 4300 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4301
4302 dev = dev_get_by_index_rcu(net, params->ifindex);
4303 if (unlikely(!dev))
4304 return -ENODEV;
4305
4306 idev = __in6_dev_get_safely(dev);
4307 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4c79579b 4308 return BPF_FIB_LKUP_RET_FWD_DISABLED;
87f5fc7e
DA
4309
4310 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4311 fl6.flowi6_iif = 1;
4312 oif = fl6.flowi6_oif = params->ifindex;
4313 } else {
4314 oif = fl6.flowi6_iif = params->ifindex;
4315 fl6.flowi6_oif = 0;
4316 strict = RT6_LOOKUP_F_HAS_SADDR;
4317 }
bd3a08aa 4318 fl6.flowlabel = params->flowinfo;
87f5fc7e
DA
4319 fl6.flowi6_scope = 0;
4320 fl6.flowi6_flags = 0;
4321 fl6.mp_hash = 0;
4322
4323 fl6.flowi6_proto = params->l4_protocol;
4324 fl6.daddr = *dst;
4325 fl6.saddr = *src;
4326 fl6.fl6_sport = params->sport;
4327 fl6.fl6_dport = params->dport;
4328
4329 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4330 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4331 struct fib6_table *tb;
4332
4333 tb = ipv6_stub->fib6_get_table(net, tbid);
4334 if (unlikely(!tb))
4c79579b 4335 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4336
4337 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4338 } else {
4339 fl6.flowi6_mark = 0;
4340 fl6.flowi6_secid = 0;
4341 fl6.flowi6_tun_key.tun_id = 0;
4342 fl6.flowi6_uid = sock_net_uid(net, NULL);
4343
4344 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4345 }
4346
4347 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4c79579b
DA
4348 return BPF_FIB_LKUP_RET_NOT_FWDED;
4349
4350 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4351 switch (f6i->fib6_type) {
4352 case RTN_BLACKHOLE:
4353 return BPF_FIB_LKUP_RET_BLACKHOLE;
4354 case RTN_UNREACHABLE:
4355 return BPF_FIB_LKUP_RET_UNREACHABLE;
4356 case RTN_PROHIBIT:
4357 return BPF_FIB_LKUP_RET_PROHIBIT;
4358 default:
4359 return BPF_FIB_LKUP_RET_NOT_FWDED;
4360 }
4361 }
87f5fc7e 4362
4c79579b
DA
4363 if (f6i->fib6_type != RTN_UNICAST)
4364 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4365
4366 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4367 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4368 fl6.flowi6_oif, NULL,
4369 strict);
4370
4f74fede
DA
4371 if (check_mtu) {
4372 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4373 if (params->tot_len > mtu)
4c79579b 4374 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4f74fede
DA
4375 }
4376
87f5fc7e 4377 if (f6i->fib6_nh.nh_lwtstate)
4c79579b 4378 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
87f5fc7e
DA
4379
4380 if (f6i->fib6_flags & RTF_GATEWAY)
4381 *dst = f6i->fib6_nh.nh_gw;
4382
4383 dev = f6i->fib6_nh.nh_dev;
4384 params->rt_metric = f6i->fib6_metric;
4385
4386 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4387 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4388 * because we need to get nd_tbl via the stub
4389 */
4390 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4391 ndisc_hashfn, dst, dev);
4c79579b
DA
4392 if (!neigh)
4393 return BPF_FIB_LKUP_RET_NO_NEIGH;
87f5fc7e 4394
4c79579b 4395 return bpf_fib_set_fwd_params(params, neigh, dev);
87f5fc7e
DA
4396}
4397#endif
4398
4399BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4400 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4401{
4402 if (plen < sizeof(*params))
4403 return -EINVAL;
4404
9ce64f19
DA
4405 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4406 return -EINVAL;
4407
87f5fc7e
DA
4408 switch (params->family) {
4409#if IS_ENABLED(CONFIG_INET)
4410 case AF_INET:
4411 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4f74fede 4412 flags, true);
87f5fc7e
DA
4413#endif
4414#if IS_ENABLED(CONFIG_IPV6)
4415 case AF_INET6:
4416 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4f74fede 4417 flags, true);
87f5fc7e
DA
4418#endif
4419 }
bcece5dc 4420 return -EAFNOSUPPORT;
87f5fc7e
DA
4421}
4422
4423static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4424 .func = bpf_xdp_fib_lookup,
4425 .gpl_only = true,
4426 .ret_type = RET_INTEGER,
4427 .arg1_type = ARG_PTR_TO_CTX,
4428 .arg2_type = ARG_PTR_TO_MEM,
4429 .arg3_type = ARG_CONST_SIZE,
4430 .arg4_type = ARG_ANYTHING,
4431};
4432
4433BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4434 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4435{
4f74fede 4436 struct net *net = dev_net(skb->dev);
4c79579b 4437 int rc = -EAFNOSUPPORT;
4f74fede 4438
87f5fc7e
DA
4439 if (plen < sizeof(*params))
4440 return -EINVAL;
4441
9ce64f19
DA
4442 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4443 return -EINVAL;
4444
87f5fc7e
DA
4445 switch (params->family) {
4446#if IS_ENABLED(CONFIG_INET)
4447 case AF_INET:
4c79579b 4448 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4f74fede 4449 break;
87f5fc7e
DA
4450#endif
4451#if IS_ENABLED(CONFIG_IPV6)
4452 case AF_INET6:
4c79579b 4453 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4f74fede 4454 break;
87f5fc7e
DA
4455#endif
4456 }
4f74fede 4457
4c79579b 4458 if (!rc) {
4f74fede
DA
4459 struct net_device *dev;
4460
4c79579b 4461 dev = dev_get_by_index_rcu(net, params->ifindex);
4f74fede 4462 if (!is_skb_forwardable(dev, skb))
4c79579b 4463 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4f74fede
DA
4464 }
4465
4c79579b 4466 return rc;
87f5fc7e
DA
4467}
4468
4469static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4470 .func = bpf_skb_fib_lookup,
4471 .gpl_only = true,
4472 .ret_type = RET_INTEGER,
4473 .arg1_type = ARG_PTR_TO_CTX,
4474 .arg2_type = ARG_PTR_TO_MEM,
4475 .arg3_type = ARG_CONST_SIZE,
4476 .arg4_type = ARG_ANYTHING,
4477};
4478
fe94cc29
MX
4479#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4480static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4481{
4482 int err;
4483 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4484
4485 if (!seg6_validate_srh(srh, len))
4486 return -EINVAL;
4487
4488 switch (type) {
4489 case BPF_LWT_ENCAP_SEG6_INLINE:
4490 if (skb->protocol != htons(ETH_P_IPV6))
4491 return -EBADMSG;
4492
4493 err = seg6_do_srh_inline(skb, srh);
4494 break;
4495 case BPF_LWT_ENCAP_SEG6:
4496 skb_reset_inner_headers(skb);
4497 skb->encapsulation = 1;
4498 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4499 break;
4500 default:
4501 return -EINVAL;
4502 }
4503
4504 bpf_compute_data_pointers(skb);
4505 if (err)
4506 return err;
4507
4508 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4509 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4510
4511 return seg6_lookup_nexthop(skb, NULL, 0);
4512}
4513#endif /* CONFIG_IPV6_SEG6_BPF */
4514
4515BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4516 u32, len)
4517{
4518 switch (type) {
4519#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4520 case BPF_LWT_ENCAP_SEG6:
4521 case BPF_LWT_ENCAP_SEG6_INLINE:
4522 return bpf_push_seg6_encap(skb, type, hdr, len);
4523#endif
4524 default:
4525 return -EINVAL;
4526 }
4527}
4528
4529static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4530 .func = bpf_lwt_push_encap,
4531 .gpl_only = false,
4532 .ret_type = RET_INTEGER,
4533 .arg1_type = ARG_PTR_TO_CTX,
4534 .arg2_type = ARG_ANYTHING,
4535 .arg3_type = ARG_PTR_TO_MEM,
4536 .arg4_type = ARG_CONST_SIZE
4537};
4538
61d76980 4539#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
fe94cc29
MX
4540BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4541 const void *, from, u32, len)
4542{
fe94cc29
MX
4543 struct seg6_bpf_srh_state *srh_state =
4544 this_cpu_ptr(&seg6_bpf_srh_states);
4545 void *srh_tlvs, *srh_end, *ptr;
4546 struct ipv6_sr_hdr *srh;
4547 int srhoff = 0;
4548
4549 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4550 return -EINVAL;
4551
4552 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4553 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4554 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4555
4556 ptr = skb->data + offset;
4557 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4558 srh_state->valid = 0;
4559 else if (ptr < (void *)&srh->flags ||
4560 ptr + len > (void *)&srh->segments)
4561 return -EFAULT;
4562
4563 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4564 return -EFAULT;
4565
4566 memcpy(skb->data + offset, from, len);
4567 return 0;
fe94cc29
MX
4568}
4569
4570static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4571 .func = bpf_lwt_seg6_store_bytes,
4572 .gpl_only = false,
4573 .ret_type = RET_INTEGER,
4574 .arg1_type = ARG_PTR_TO_CTX,
4575 .arg2_type = ARG_ANYTHING,
4576 .arg3_type = ARG_PTR_TO_MEM,
4577 .arg4_type = ARG_CONST_SIZE
4578};
4579
4580BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4581 u32, action, void *, param, u32, param_len)
4582{
fe94cc29
MX
4583 struct seg6_bpf_srh_state *srh_state =
4584 this_cpu_ptr(&seg6_bpf_srh_states);
4585 struct ipv6_sr_hdr *srh;
4586 int srhoff = 0;
4587 int err;
4588
4589 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4590 return -EINVAL;
4591 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4592
4593 if (!srh_state->valid) {
4594 if (unlikely((srh_state->hdrlen & 7) != 0))
4595 return -EBADMSG;
4596
4597 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
4598 if (unlikely(!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3)))
4599 return -EBADMSG;
4600
4601 srh_state->valid = 1;
4602 }
4603
4604 switch (action) {
4605 case SEG6_LOCAL_ACTION_END_X:
4606 if (param_len != sizeof(struct in6_addr))
4607 return -EINVAL;
4608 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4609 case SEG6_LOCAL_ACTION_END_T:
4610 if (param_len != sizeof(int))
4611 return -EINVAL;
4612 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4613 case SEG6_LOCAL_ACTION_END_B6:
4614 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4615 param, param_len);
4616 if (!err)
4617 srh_state->hdrlen =
4618 ((struct ipv6_sr_hdr *)param)->hdrlen << 3;
4619 return err;
4620 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
4621 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4622 param, param_len);
4623 if (!err)
4624 srh_state->hdrlen =
4625 ((struct ipv6_sr_hdr *)param)->hdrlen << 3;
4626 return err;
4627 default:
4628 return -EINVAL;
4629 }
fe94cc29
MX
4630}
4631
4632static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4633 .func = bpf_lwt_seg6_action,
4634 .gpl_only = false,
4635 .ret_type = RET_INTEGER,
4636 .arg1_type = ARG_PTR_TO_CTX,
4637 .arg2_type = ARG_ANYTHING,
4638 .arg3_type = ARG_PTR_TO_MEM,
4639 .arg4_type = ARG_CONST_SIZE
4640};
4641
4642BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4643 s32, len)
4644{
fe94cc29
MX
4645 struct seg6_bpf_srh_state *srh_state =
4646 this_cpu_ptr(&seg6_bpf_srh_states);
4647 void *srh_end, *srh_tlvs, *ptr;
4648 struct ipv6_sr_hdr *srh;
4649 struct ipv6hdr *hdr;
4650 int srhoff = 0;
4651 int ret;
4652
4653 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4654 return -EINVAL;
4655 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4656
4657 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4658 ((srh->first_segment + 1) << 4));
4659 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4660 srh_state->hdrlen);
4661 ptr = skb->data + offset;
4662
4663 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4664 return -EFAULT;
4665 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4666 return -EFAULT;
4667
4668 if (len > 0) {
4669 ret = skb_cow_head(skb, len);
4670 if (unlikely(ret < 0))
4671 return ret;
4672
4673 ret = bpf_skb_net_hdr_push(skb, offset, len);
4674 } else {
4675 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4676 }
4677
4678 bpf_compute_data_pointers(skb);
4679 if (unlikely(ret < 0))
4680 return ret;
4681
4682 hdr = (struct ipv6hdr *)skb->data;
4683 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4684
4685 srh_state->hdrlen += len;
4686 srh_state->valid = 0;
4687 return 0;
fe94cc29
MX
4688}
4689
4690static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
4691 .func = bpf_lwt_seg6_adjust_srh,
4692 .gpl_only = false,
4693 .ret_type = RET_INTEGER,
4694 .arg1_type = ARG_PTR_TO_CTX,
4695 .arg2_type = ARG_ANYTHING,
4696 .arg3_type = ARG_ANYTHING,
4697};
61d76980 4698#endif /* CONFIG_IPV6_SEG6_BPF */
fe94cc29
MX
4699
4700bool bpf_helper_changes_pkt_data(void *func)
4701{
4702 if (func == bpf_skb_vlan_push ||
4703 func == bpf_skb_vlan_pop ||
4704 func == bpf_skb_store_bytes ||
4705 func == bpf_skb_change_proto ||
4706 func == bpf_skb_change_head ||
0ea488ff 4707 func == sk_skb_change_head ||
fe94cc29 4708 func == bpf_skb_change_tail ||
0ea488ff 4709 func == sk_skb_change_tail ||
fe94cc29
MX
4710 func == bpf_skb_adjust_room ||
4711 func == bpf_skb_pull_data ||
0ea488ff 4712 func == sk_skb_pull_data ||
fe94cc29
MX
4713 func == bpf_clone_redirect ||
4714 func == bpf_l3_csum_replace ||
4715 func == bpf_l4_csum_replace ||
4716 func == bpf_xdp_adjust_head ||
4717 func == bpf_xdp_adjust_meta ||
4718 func == bpf_msg_pull_data ||
4719 func == bpf_xdp_adjust_tail ||
61d76980 4720#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
fe94cc29
MX
4721 func == bpf_lwt_seg6_store_bytes ||
4722 func == bpf_lwt_seg6_adjust_srh ||
61d76980
MX
4723 func == bpf_lwt_seg6_action ||
4724#endif
4725 func == bpf_lwt_push_encap)
fe94cc29
MX
4726 return true;
4727
4728 return false;
4729}
4730
d4052c4a 4731static const struct bpf_func_proto *
2492d3b8 4732bpf_base_func_proto(enum bpf_func_id func_id)
89aa0758
AS
4733{
4734 switch (func_id) {
4735 case BPF_FUNC_map_lookup_elem:
4736 return &bpf_map_lookup_elem_proto;
4737 case BPF_FUNC_map_update_elem:
4738 return &bpf_map_update_elem_proto;
4739 case BPF_FUNC_map_delete_elem:
4740 return &bpf_map_delete_elem_proto;
03e69b50
DB
4741 case BPF_FUNC_get_prandom_u32:
4742 return &bpf_get_prandom_u32_proto;
c04167ce 4743 case BPF_FUNC_get_smp_processor_id:
80b48c44 4744 return &bpf_get_raw_smp_processor_id_proto;
2d0e30c3
DB
4745 case BPF_FUNC_get_numa_node_id:
4746 return &bpf_get_numa_node_id_proto;
04fd61ab
AS
4747 case BPF_FUNC_tail_call:
4748 return &bpf_tail_call_proto;
17ca8cbf
DB
4749 case BPF_FUNC_ktime_get_ns:
4750 return &bpf_ktime_get_ns_proto;
0756ea3e 4751 case BPF_FUNC_trace_printk:
1be7f75d
AS
4752 if (capable(CAP_SYS_ADMIN))
4753 return bpf_get_trace_printk_proto();
2cc0608e 4754 /* else: fall through */
89aa0758
AS
4755 default:
4756 return NULL;
4757 }
4758}
4759
ae2cf1c4 4760static const struct bpf_func_proto *
5e43f899 4761sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
ae2cf1c4
DA
4762{
4763 switch (func_id) {
4764 /* inet and inet6 sockets are created in a process
4765 * context so there is always a valid uid/gid
4766 */
4767 case BPF_FUNC_get_current_uid_gid:
4768 return &bpf_get_current_uid_gid_proto;
4769 default:
4770 return bpf_base_func_proto(func_id);
4771 }
4772}
4773
4fbac77d
AI
4774static const struct bpf_func_proto *
4775sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4776{
4777 switch (func_id) {
4778 /* inet and inet6 sockets are created in a process
4779 * context so there is always a valid uid/gid
4780 */
4781 case BPF_FUNC_get_current_uid_gid:
4782 return &bpf_get_current_uid_gid_proto;
d74bad4e
AI
4783 case BPF_FUNC_bind:
4784 switch (prog->expected_attach_type) {
4785 case BPF_CGROUP_INET4_CONNECT:
4786 case BPF_CGROUP_INET6_CONNECT:
4787 return &bpf_bind_proto;
4788 default:
4789 return NULL;
4790 }
4fbac77d
AI
4791 default:
4792 return bpf_base_func_proto(func_id);
4793 }
4794}
4795
2492d3b8 4796static const struct bpf_func_proto *
5e43f899 4797sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2492d3b8
DB
4798{
4799 switch (func_id) {
4800 case BPF_FUNC_skb_load_bytes:
4801 return &bpf_skb_load_bytes_proto;
4e1ec56c
DB
4802 case BPF_FUNC_skb_load_bytes_relative:
4803 return &bpf_skb_load_bytes_relative_proto;
91b8270f
CF
4804 case BPF_FUNC_get_socket_cookie:
4805 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
4806 case BPF_FUNC_get_socket_uid:
4807 return &bpf_get_socket_uid_proto;
2492d3b8
DB
4808 default:
4809 return bpf_base_func_proto(func_id);
4810 }
4811}
4812
608cd71a 4813static const struct bpf_func_proto *
5e43f899 4814tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
608cd71a
AS
4815{
4816 switch (func_id) {
4817 case BPF_FUNC_skb_store_bytes:
4818 return &bpf_skb_store_bytes_proto;
05c74e5e
DB
4819 case BPF_FUNC_skb_load_bytes:
4820 return &bpf_skb_load_bytes_proto;
4e1ec56c
DB
4821 case BPF_FUNC_skb_load_bytes_relative:
4822 return &bpf_skb_load_bytes_relative_proto;
36bbef52
DB
4823 case BPF_FUNC_skb_pull_data:
4824 return &bpf_skb_pull_data_proto;
7d672345
DB
4825 case BPF_FUNC_csum_diff:
4826 return &bpf_csum_diff_proto;
36bbef52
DB
4827 case BPF_FUNC_csum_update:
4828 return &bpf_csum_update_proto;
91bc4822
AS
4829 case BPF_FUNC_l3_csum_replace:
4830 return &bpf_l3_csum_replace_proto;
4831 case BPF_FUNC_l4_csum_replace:
4832 return &bpf_l4_csum_replace_proto;
3896d655
AS
4833 case BPF_FUNC_clone_redirect:
4834 return &bpf_clone_redirect_proto;
8d20aabe
DB
4835 case BPF_FUNC_get_cgroup_classid:
4836 return &bpf_get_cgroup_classid_proto;
4e10df9a
AS
4837 case BPF_FUNC_skb_vlan_push:
4838 return &bpf_skb_vlan_push_proto;
4839 case BPF_FUNC_skb_vlan_pop:
4840 return &bpf_skb_vlan_pop_proto;
6578171a
DB
4841 case BPF_FUNC_skb_change_proto:
4842 return &bpf_skb_change_proto_proto;
d2485c42
DB
4843 case BPF_FUNC_skb_change_type:
4844 return &bpf_skb_change_type_proto;
2be7e212
DB
4845 case BPF_FUNC_skb_adjust_room:
4846 return &bpf_skb_adjust_room_proto;
5293efe6
DB
4847 case BPF_FUNC_skb_change_tail:
4848 return &bpf_skb_change_tail_proto;
d3aa45ce
AS
4849 case BPF_FUNC_skb_get_tunnel_key:
4850 return &bpf_skb_get_tunnel_key_proto;
4851 case BPF_FUNC_skb_set_tunnel_key:
14ca0751
DB
4852 return bpf_get_skb_set_tunnel_proto(func_id);
4853 case BPF_FUNC_skb_get_tunnel_opt:
4854 return &bpf_skb_get_tunnel_opt_proto;
4855 case BPF_FUNC_skb_set_tunnel_opt:
4856 return bpf_get_skb_set_tunnel_proto(func_id);
27b29f63
AS
4857 case BPF_FUNC_redirect:
4858 return &bpf_redirect_proto;
c46646d0
DB
4859 case BPF_FUNC_get_route_realm:
4860 return &bpf_get_route_realm_proto;
13c5c240
DB
4861 case BPF_FUNC_get_hash_recalc:
4862 return &bpf_get_hash_recalc_proto;
7a4b28c6
DB
4863 case BPF_FUNC_set_hash_invalid:
4864 return &bpf_set_hash_invalid_proto;
ded092cd
DB
4865 case BPF_FUNC_set_hash:
4866 return &bpf_set_hash_proto;
bd570ff9 4867 case BPF_FUNC_perf_event_output:
555c8a86 4868 return &bpf_skb_event_output_proto;
80b48c44
DB
4869 case BPF_FUNC_get_smp_processor_id:
4870 return &bpf_get_smp_processor_id_proto;
747ea55e
DB
4871 case BPF_FUNC_skb_under_cgroup:
4872 return &bpf_skb_under_cgroup_proto;
91b8270f
CF
4873 case BPF_FUNC_get_socket_cookie:
4874 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
4875 case BPF_FUNC_get_socket_uid:
4876 return &bpf_get_socket_uid_proto;
cb20b08e
DB
4877 case BPF_FUNC_fib_lookup:
4878 return &bpf_skb_fib_lookup_proto;
12bed760
EB
4879#ifdef CONFIG_XFRM
4880 case BPF_FUNC_skb_get_xfrm_state:
4881 return &bpf_skb_get_xfrm_state_proto;
4882#endif
cb20b08e
DB
4883#ifdef CONFIG_SOCK_CGROUP_DATA
4884 case BPF_FUNC_skb_cgroup_id:
4885 return &bpf_skb_cgroup_id_proto;
4886#endif
608cd71a 4887 default:
2492d3b8 4888 return bpf_base_func_proto(func_id);
608cd71a
AS
4889 }
4890}
4891
6a773a15 4892static const struct bpf_func_proto *
5e43f899 4893xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6a773a15 4894{
4de16969
DB
4895 switch (func_id) {
4896 case BPF_FUNC_perf_event_output:
4897 return &bpf_xdp_event_output_proto;
669dc4d7
DB
4898 case BPF_FUNC_get_smp_processor_id:
4899 return &bpf_get_smp_processor_id_proto;
205c3807
DB
4900 case BPF_FUNC_csum_diff:
4901 return &bpf_csum_diff_proto;
17bedab2
MKL
4902 case BPF_FUNC_xdp_adjust_head:
4903 return &bpf_xdp_adjust_head_proto;
de8f3a83
DB
4904 case BPF_FUNC_xdp_adjust_meta:
4905 return &bpf_xdp_adjust_meta_proto;
814abfab
JF
4906 case BPF_FUNC_redirect:
4907 return &bpf_xdp_redirect_proto;
97f91a7c 4908 case BPF_FUNC_redirect_map:
e4a8e817 4909 return &bpf_xdp_redirect_map_proto;
b32cc5b9
NS
4910 case BPF_FUNC_xdp_adjust_tail:
4911 return &bpf_xdp_adjust_tail_proto;
87f5fc7e
DA
4912 case BPF_FUNC_fib_lookup:
4913 return &bpf_xdp_fib_lookup_proto;
4de16969 4914 default:
2492d3b8 4915 return bpf_base_func_proto(func_id);
4de16969 4916 }
6a773a15
BB
4917}
4918
8c4b4c7e 4919static const struct bpf_func_proto *
5e43f899 4920sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8c4b4c7e
LB
4921{
4922 switch (func_id) {
4923 case BPF_FUNC_setsockopt:
4924 return &bpf_setsockopt_proto;
cd86d1fd
LB
4925 case BPF_FUNC_getsockopt:
4926 return &bpf_getsockopt_proto;
b13d8807
LB
4927 case BPF_FUNC_sock_ops_cb_flags_set:
4928 return &bpf_sock_ops_cb_flags_set_proto;
174a79ff
JF
4929 case BPF_FUNC_sock_map_update:
4930 return &bpf_sock_map_update_proto;
81110384
JF
4931 case BPF_FUNC_sock_hash_update:
4932 return &bpf_sock_hash_update_proto;
8c4b4c7e
LB
4933 default:
4934 return bpf_base_func_proto(func_id);
4935 }
4936}
4937
5e43f899
AI
4938static const struct bpf_func_proto *
4939sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4f738adb
JF
4940{
4941 switch (func_id) {
4942 case BPF_FUNC_msg_redirect_map:
4943 return &bpf_msg_redirect_map_proto;
81110384
JF
4944 case BPF_FUNC_msg_redirect_hash:
4945 return &bpf_msg_redirect_hash_proto;
2a100317
JF
4946 case BPF_FUNC_msg_apply_bytes:
4947 return &bpf_msg_apply_bytes_proto;
91843d54
JF
4948 case BPF_FUNC_msg_cork_bytes:
4949 return &bpf_msg_cork_bytes_proto;
015632bb
JF
4950 case BPF_FUNC_msg_pull_data:
4951 return &bpf_msg_pull_data_proto;
4f738adb
JF
4952 default:
4953 return bpf_base_func_proto(func_id);
4954 }
4955}
4956
5e43f899
AI
4957static const struct bpf_func_proto *
4958sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
b005fd18
JF
4959{
4960 switch (func_id) {
8a31db56
JF
4961 case BPF_FUNC_skb_store_bytes:
4962 return &bpf_skb_store_bytes_proto;
b005fd18
JF
4963 case BPF_FUNC_skb_load_bytes:
4964 return &bpf_skb_load_bytes_proto;
8a31db56 4965 case BPF_FUNC_skb_pull_data:
0ea488ff 4966 return &sk_skb_pull_data_proto;
8a31db56 4967 case BPF_FUNC_skb_change_tail:
0ea488ff 4968 return &sk_skb_change_tail_proto;
8a31db56 4969 case BPF_FUNC_skb_change_head:
0ea488ff 4970 return &sk_skb_change_head_proto;
b005fd18
JF
4971 case BPF_FUNC_get_socket_cookie:
4972 return &bpf_get_socket_cookie_proto;
4973 case BPF_FUNC_get_socket_uid:
4974 return &bpf_get_socket_uid_proto;
174a79ff
JF
4975 case BPF_FUNC_sk_redirect_map:
4976 return &bpf_sk_redirect_map_proto;
81110384
JF
4977 case BPF_FUNC_sk_redirect_hash:
4978 return &bpf_sk_redirect_hash_proto;
b005fd18
JF
4979 default:
4980 return bpf_base_func_proto(func_id);
4981 }
4982}
4983
cd3092c7
MX
4984static const struct bpf_func_proto *
4985lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4986{
4987 switch (func_id) {
4988 case BPF_FUNC_skb_load_bytes:
4989 return &bpf_skb_load_bytes_proto;
4990 case BPF_FUNC_skb_pull_data:
4991 return &bpf_skb_pull_data_proto;
4992 case BPF_FUNC_csum_diff:
4993 return &bpf_csum_diff_proto;
4994 case BPF_FUNC_get_cgroup_classid:
4995 return &bpf_get_cgroup_classid_proto;
4996 case BPF_FUNC_get_route_realm:
4997 return &bpf_get_route_realm_proto;
4998 case BPF_FUNC_get_hash_recalc:
4999 return &bpf_get_hash_recalc_proto;
5000 case BPF_FUNC_perf_event_output:
5001 return &bpf_skb_event_output_proto;
5002 case BPF_FUNC_get_smp_processor_id:
5003 return &bpf_get_smp_processor_id_proto;
5004 case BPF_FUNC_skb_under_cgroup:
5005 return &bpf_skb_under_cgroup_proto;
5006 default:
5007 return bpf_base_func_proto(func_id);
5008 }
5009}
5010
5011static const struct bpf_func_proto *
5012lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5013{
5014 switch (func_id) {
5015 case BPF_FUNC_lwt_push_encap:
5016 return &bpf_lwt_push_encap_proto;
5017 default:
5018 return lwt_out_func_proto(func_id, prog);
5019 }
5020}
5021
3a0af8fd 5022static const struct bpf_func_proto *
5e43f899 5023lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
3a0af8fd
TG
5024{
5025 switch (func_id) {
5026 case BPF_FUNC_skb_get_tunnel_key:
5027 return &bpf_skb_get_tunnel_key_proto;
5028 case BPF_FUNC_skb_set_tunnel_key:
5029 return bpf_get_skb_set_tunnel_proto(func_id);
5030 case BPF_FUNC_skb_get_tunnel_opt:
5031 return &bpf_skb_get_tunnel_opt_proto;
5032 case BPF_FUNC_skb_set_tunnel_opt:
5033 return bpf_get_skb_set_tunnel_proto(func_id);
5034 case BPF_FUNC_redirect:
5035 return &bpf_redirect_proto;
5036 case BPF_FUNC_clone_redirect:
5037 return &bpf_clone_redirect_proto;
5038 case BPF_FUNC_skb_change_tail:
5039 return &bpf_skb_change_tail_proto;
5040 case BPF_FUNC_skb_change_head:
5041 return &bpf_skb_change_head_proto;
5042 case BPF_FUNC_skb_store_bytes:
5043 return &bpf_skb_store_bytes_proto;
5044 case BPF_FUNC_csum_update:
5045 return &bpf_csum_update_proto;
5046 case BPF_FUNC_l3_csum_replace:
5047 return &bpf_l3_csum_replace_proto;
5048 case BPF_FUNC_l4_csum_replace:
5049 return &bpf_l4_csum_replace_proto;
5050 case BPF_FUNC_set_hash_invalid:
5051 return &bpf_set_hash_invalid_proto;
5052 default:
cd3092c7 5053 return lwt_out_func_proto(func_id, prog);
3a0af8fd
TG
5054 }
5055}
5056
004d4b27
MX
5057static const struct bpf_func_proto *
5058lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5059{
5060 switch (func_id) {
61d76980 5061#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
004d4b27
MX
5062 case BPF_FUNC_lwt_seg6_store_bytes:
5063 return &bpf_lwt_seg6_store_bytes_proto;
5064 case BPF_FUNC_lwt_seg6_action:
5065 return &bpf_lwt_seg6_action_proto;
5066 case BPF_FUNC_lwt_seg6_adjust_srh:
5067 return &bpf_lwt_seg6_adjust_srh_proto;
61d76980 5068#endif
004d4b27
MX
5069 default:
5070 return lwt_out_func_proto(func_id, prog);
3a0af8fd
TG
5071 }
5072}
5073
f96da094 5074static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
5e43f899 5075 const struct bpf_prog *prog,
f96da094 5076 struct bpf_insn_access_aux *info)
23994631 5077{
f96da094 5078 const int size_default = sizeof(__u32);
23994631 5079
9bac3d6d
AS
5080 if (off < 0 || off >= sizeof(struct __sk_buff))
5081 return false;
62c7989b 5082
4936e352 5083 /* The verifier guarantees that size > 0. */
9bac3d6d
AS
5084 if (off % size != 0)
5085 return false;
62c7989b
DB
5086
5087 switch (off) {
f96da094
DB
5088 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5089 if (off + size > offsetofend(struct __sk_buff, cb[4]))
62c7989b
DB
5090 return false;
5091 break;
8a31db56
JF
5092 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
5093 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
5094 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
5095 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
f96da094 5096 case bpf_ctx_range(struct __sk_buff, data):
de8f3a83 5097 case bpf_ctx_range(struct __sk_buff, data_meta):
f96da094
DB
5098 case bpf_ctx_range(struct __sk_buff, data_end):
5099 if (size != size_default)
23994631 5100 return false;
31fd8581
YS
5101 break;
5102 default:
f96da094 5103 /* Only narrow read access allowed for now. */
31fd8581 5104 if (type == BPF_WRITE) {
f96da094 5105 if (size != size_default)
31fd8581
YS
5106 return false;
5107 } else {
f96da094
DB
5108 bpf_ctx_record_field_size(info, size_default);
5109 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
23994631 5110 return false;
31fd8581 5111 }
62c7989b 5112 }
9bac3d6d
AS
5113
5114 return true;
5115}
5116
d691f9e8 5117static bool sk_filter_is_valid_access(int off, int size,
19de99f7 5118 enum bpf_access_type type,
5e43f899 5119 const struct bpf_prog *prog,
23994631 5120 struct bpf_insn_access_aux *info)
d691f9e8 5121{
db58ba45 5122 switch (off) {
f96da094
DB
5123 case bpf_ctx_range(struct __sk_buff, tc_classid):
5124 case bpf_ctx_range(struct __sk_buff, data):
de8f3a83 5125 case bpf_ctx_range(struct __sk_buff, data_meta):
f96da094 5126 case bpf_ctx_range(struct __sk_buff, data_end):
8a31db56 5127 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
045efa82 5128 return false;
db58ba45 5129 }
045efa82 5130
d691f9e8
AS
5131 if (type == BPF_WRITE) {
5132 switch (off) {
f96da094 5133 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
d691f9e8
AS
5134 break;
5135 default:
5136 return false;
5137 }
5138 }
5139
5e43f899 5140 return bpf_skb_is_valid_access(off, size, type, prog, info);
d691f9e8
AS
5141}
5142
3a0af8fd
TG
5143static bool lwt_is_valid_access(int off, int size,
5144 enum bpf_access_type type,
5e43f899 5145 const struct bpf_prog *prog,
23994631 5146 struct bpf_insn_access_aux *info)
3a0af8fd
TG
5147{
5148 switch (off) {
f96da094 5149 case bpf_ctx_range(struct __sk_buff, tc_classid):
8a31db56 5150 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
de8f3a83 5151 case bpf_ctx_range(struct __sk_buff, data_meta):
3a0af8fd
TG
5152 return false;
5153 }
5154
5155 if (type == BPF_WRITE) {
5156 switch (off) {
f96da094
DB
5157 case bpf_ctx_range(struct __sk_buff, mark):
5158 case bpf_ctx_range(struct __sk_buff, priority):
5159 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3a0af8fd
TG
5160 break;
5161 default:
5162 return false;
5163 }
5164 }
5165
f96da094
DB
5166 switch (off) {
5167 case bpf_ctx_range(struct __sk_buff, data):
5168 info->reg_type = PTR_TO_PACKET;
5169 break;
5170 case bpf_ctx_range(struct __sk_buff, data_end):
5171 info->reg_type = PTR_TO_PACKET_END;
5172 break;
5173 }
5174
5e43f899 5175 return bpf_skb_is_valid_access(off, size, type, prog, info);
3a0af8fd
TG
5176}
5177
aac3fc32
AI
5178/* Attach type specific accesses */
5179static bool __sock_filter_check_attach_type(int off,
5180 enum bpf_access_type access_type,
5181 enum bpf_attach_type attach_type)
61023658 5182{
aac3fc32
AI
5183 switch (off) {
5184 case offsetof(struct bpf_sock, bound_dev_if):
5185 case offsetof(struct bpf_sock, mark):
5186 case offsetof(struct bpf_sock, priority):
5187 switch (attach_type) {
5188 case BPF_CGROUP_INET_SOCK_CREATE:
5189 goto full_access;
5190 default:
5191 return false;
5192 }
5193 case bpf_ctx_range(struct bpf_sock, src_ip4):
5194 switch (attach_type) {
5195 case BPF_CGROUP_INET4_POST_BIND:
5196 goto read_only;
5197 default:
5198 return false;
5199 }
5200 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5201 switch (attach_type) {
5202 case BPF_CGROUP_INET6_POST_BIND:
5203 goto read_only;
5204 default:
5205 return false;
5206 }
5207 case bpf_ctx_range(struct bpf_sock, src_port):
5208 switch (attach_type) {
5209 case BPF_CGROUP_INET4_POST_BIND:
5210 case BPF_CGROUP_INET6_POST_BIND:
5211 goto read_only;
61023658
DA
5212 default:
5213 return false;
5214 }
5215 }
aac3fc32
AI
5216read_only:
5217 return access_type == BPF_READ;
5218full_access:
5219 return true;
5220}
5221
5222static bool __sock_filter_check_size(int off, int size,
5223 struct bpf_insn_access_aux *info)
5224{
5225 const int size_default = sizeof(__u32);
61023658 5226
aac3fc32
AI
5227 switch (off) {
5228 case bpf_ctx_range(struct bpf_sock, src_ip4):
5229 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5230 bpf_ctx_record_field_size(info, size_default);
5231 return bpf_ctx_narrow_access_ok(off, size, size_default);
5232 }
5233
5234 return size == size_default;
5235}
5236
5237static bool sock_filter_is_valid_access(int off, int size,
5238 enum bpf_access_type type,
5239 const struct bpf_prog *prog,
5240 struct bpf_insn_access_aux *info)
5241{
5242 if (off < 0 || off >= sizeof(struct bpf_sock))
61023658 5243 return false;
61023658
DA
5244 if (off % size != 0)
5245 return false;
aac3fc32
AI
5246 if (!__sock_filter_check_attach_type(off, type,
5247 prog->expected_attach_type))
5248 return false;
5249 if (!__sock_filter_check_size(off, size, info))
61023658 5250 return false;
61023658
DA
5251 return true;
5252}
5253
047b0ecd
DB
5254static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5255 const struct bpf_prog *prog, int drop_verdict)
36bbef52
DB
5256{
5257 struct bpf_insn *insn = insn_buf;
5258
5259 if (!direct_write)
5260 return 0;
5261
5262 /* if (!skb->cloned)
5263 * goto start;
5264 *
5265 * (Fast-path, otherwise approximation that we might be
5266 * a clone, do the rest in helper.)
5267 */
5268 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5269 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5270 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5271
5272 /* ret = bpf_skb_pull_data(skb, 0); */
5273 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5274 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5275 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5276 BPF_FUNC_skb_pull_data);
5277 /* if (!ret)
5278 * goto restore;
5279 * return TC_ACT_SHOT;
5280 */
5281 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
047b0ecd 5282 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
36bbef52
DB
5283 *insn++ = BPF_EXIT_INSN();
5284
5285 /* restore: */
5286 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
5287 /* start: */
5288 *insn++ = prog->insnsi[0];
5289
5290 return insn - insn_buf;
5291}
5292
e0cea7ce
DB
5293static int bpf_gen_ld_abs(const struct bpf_insn *orig,
5294 struct bpf_insn *insn_buf)
5295{
5296 bool indirect = BPF_MODE(orig->code) == BPF_IND;
5297 struct bpf_insn *insn = insn_buf;
5298
5299 /* We're guaranteed here that CTX is in R6. */
5300 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
5301 if (!indirect) {
5302 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
5303 } else {
5304 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
5305 if (orig->imm)
5306 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
5307 }
5308
5309 switch (BPF_SIZE(orig->code)) {
5310 case BPF_B:
5311 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
5312 break;
5313 case BPF_H:
5314 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
5315 break;
5316 case BPF_W:
5317 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
5318 break;
5319 }
5320
5321 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
5322 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
5323 *insn++ = BPF_EXIT_INSN();
5324
5325 return insn - insn_buf;
5326}
5327
047b0ecd
DB
5328static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
5329 const struct bpf_prog *prog)
5330{
5331 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
5332}
5333
d691f9e8 5334static bool tc_cls_act_is_valid_access(int off, int size,
19de99f7 5335 enum bpf_access_type type,
5e43f899 5336 const struct bpf_prog *prog,
23994631 5337 struct bpf_insn_access_aux *info)
d691f9e8
AS
5338{
5339 if (type == BPF_WRITE) {
5340 switch (off) {
f96da094
DB
5341 case bpf_ctx_range(struct __sk_buff, mark):
5342 case bpf_ctx_range(struct __sk_buff, tc_index):
5343 case bpf_ctx_range(struct __sk_buff, priority):
5344 case bpf_ctx_range(struct __sk_buff, tc_classid):
5345 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
d691f9e8
AS
5346 break;
5347 default:
5348 return false;
5349 }
5350 }
19de99f7 5351
f96da094
DB
5352 switch (off) {
5353 case bpf_ctx_range(struct __sk_buff, data):
5354 info->reg_type = PTR_TO_PACKET;
5355 break;
de8f3a83
DB
5356 case bpf_ctx_range(struct __sk_buff, data_meta):
5357 info->reg_type = PTR_TO_PACKET_META;
5358 break;
f96da094
DB
5359 case bpf_ctx_range(struct __sk_buff, data_end):
5360 info->reg_type = PTR_TO_PACKET_END;
5361 break;
8a31db56
JF
5362 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5363 return false;
f96da094
DB
5364 }
5365
5e43f899 5366 return bpf_skb_is_valid_access(off, size, type, prog, info);
d691f9e8
AS
5367}
5368
1afaf661 5369static bool __is_valid_xdp_access(int off, int size)
6a773a15
BB
5370{
5371 if (off < 0 || off >= sizeof(struct xdp_md))
5372 return false;
5373 if (off % size != 0)
5374 return false;
6088b582 5375 if (size != sizeof(__u32))
6a773a15
BB
5376 return false;
5377
5378 return true;
5379}
5380
5381static bool xdp_is_valid_access(int off, int size,
5382 enum bpf_access_type type,
5e43f899 5383 const struct bpf_prog *prog,
23994631 5384 struct bpf_insn_access_aux *info)
6a773a15 5385{
0d830032
JK
5386 if (type == BPF_WRITE) {
5387 if (bpf_prog_is_dev_bound(prog->aux)) {
5388 switch (off) {
5389 case offsetof(struct xdp_md, rx_queue_index):
5390 return __is_valid_xdp_access(off, size);
5391 }
5392 }
6a773a15 5393 return false;
0d830032 5394 }
6a773a15
BB
5395
5396 switch (off) {
5397 case offsetof(struct xdp_md, data):
23994631 5398 info->reg_type = PTR_TO_PACKET;
6a773a15 5399 break;
de8f3a83
DB
5400 case offsetof(struct xdp_md, data_meta):
5401 info->reg_type = PTR_TO_PACKET_META;
5402 break;
6a773a15 5403 case offsetof(struct xdp_md, data_end):
23994631 5404 info->reg_type = PTR_TO_PACKET_END;
6a773a15
BB
5405 break;
5406 }
5407
1afaf661 5408 return __is_valid_xdp_access(off, size);
6a773a15
BB
5409}
5410
5411void bpf_warn_invalid_xdp_action(u32 act)
5412{
9beb8bed
DB
5413 const u32 act_max = XDP_REDIRECT;
5414
5415 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
5416 act > act_max ? "Illegal" : "Driver unsupported",
5417 act);
6a773a15
BB
5418}
5419EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
5420
4fbac77d
AI
5421static bool sock_addr_is_valid_access(int off, int size,
5422 enum bpf_access_type type,
5423 const struct bpf_prog *prog,
5424 struct bpf_insn_access_aux *info)
5425{
5426 const int size_default = sizeof(__u32);
5427
5428 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
5429 return false;
5430 if (off % size != 0)
5431 return false;
5432
5433 /* Disallow access to IPv6 fields from IPv4 contex and vise
5434 * versa.
5435 */
5436 switch (off) {
5437 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5438 switch (prog->expected_attach_type) {
5439 case BPF_CGROUP_INET4_BIND:
d74bad4e 5440 case BPF_CGROUP_INET4_CONNECT:
1cedee13 5441 case BPF_CGROUP_UDP4_SENDMSG:
4fbac77d
AI
5442 break;
5443 default:
5444 return false;
5445 }
5446 break;
5447 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5448 switch (prog->expected_attach_type) {
5449 case BPF_CGROUP_INET6_BIND:
d74bad4e 5450 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
5451 case BPF_CGROUP_UDP6_SENDMSG:
5452 break;
5453 default:
5454 return false;
5455 }
5456 break;
5457 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5458 switch (prog->expected_attach_type) {
5459 case BPF_CGROUP_UDP4_SENDMSG:
5460 break;
5461 default:
5462 return false;
5463 }
5464 break;
5465 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5466 msg_src_ip6[3]):
5467 switch (prog->expected_attach_type) {
5468 case BPF_CGROUP_UDP6_SENDMSG:
4fbac77d
AI
5469 break;
5470 default:
5471 return false;
5472 }
5473 break;
5474 }
5475
5476 switch (off) {
5477 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5478 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
1cedee13
AI
5479 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5480 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5481 msg_src_ip6[3]):
4fbac77d
AI
5482 /* Only narrow read access allowed for now. */
5483 if (type == BPF_READ) {
5484 bpf_ctx_record_field_size(info, size_default);
5485 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5486 return false;
5487 } else {
5488 if (size != size_default)
5489 return false;
5490 }
5491 break;
5492 case bpf_ctx_range(struct bpf_sock_addr, user_port):
5493 if (size != size_default)
5494 return false;
5495 break;
5496 default:
5497 if (type == BPF_READ) {
5498 if (size != size_default)
5499 return false;
5500 } else {
5501 return false;
5502 }
5503 }
5504
5505 return true;
5506}
5507
44f0e430
LB
5508static bool sock_ops_is_valid_access(int off, int size,
5509 enum bpf_access_type type,
5e43f899 5510 const struct bpf_prog *prog,
44f0e430 5511 struct bpf_insn_access_aux *info)
40304b2a 5512{
44f0e430
LB
5513 const int size_default = sizeof(__u32);
5514
40304b2a
LB
5515 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5516 return false;
44f0e430 5517
40304b2a
LB
5518 /* The verifier guarantees that size > 0. */
5519 if (off % size != 0)
5520 return false;
40304b2a 5521
40304b2a
LB
5522 if (type == BPF_WRITE) {
5523 switch (off) {
2585cd62 5524 case offsetof(struct bpf_sock_ops, reply):
6f9bd3d7 5525 case offsetof(struct bpf_sock_ops, sk_txhash):
44f0e430
LB
5526 if (size != size_default)
5527 return false;
40304b2a
LB
5528 break;
5529 default:
5530 return false;
5531 }
44f0e430
LB
5532 } else {
5533 switch (off) {
5534 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5535 bytes_acked):
5536 if (size != sizeof(__u64))
5537 return false;
5538 break;
5539 default:
5540 if (size != size_default)
5541 return false;
5542 break;
5543 }
40304b2a
LB
5544 }
5545
44f0e430 5546 return true;
40304b2a
LB
5547}
5548
8a31db56
JF
5549static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5550 const struct bpf_prog *prog)
5551{
047b0ecd 5552 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
8a31db56
JF
5553}
5554
b005fd18
JF
5555static bool sk_skb_is_valid_access(int off, int size,
5556 enum bpf_access_type type,
5e43f899 5557 const struct bpf_prog *prog,
b005fd18
JF
5558 struct bpf_insn_access_aux *info)
5559{
de8f3a83
DB
5560 switch (off) {
5561 case bpf_ctx_range(struct __sk_buff, tc_classid):
5562 case bpf_ctx_range(struct __sk_buff, data_meta):
5563 return false;
5564 }
5565
8a31db56
JF
5566 if (type == BPF_WRITE) {
5567 switch (off) {
8a31db56
JF
5568 case bpf_ctx_range(struct __sk_buff, tc_index):
5569 case bpf_ctx_range(struct __sk_buff, priority):
5570 break;
5571 default:
5572 return false;
5573 }
5574 }
5575
b005fd18 5576 switch (off) {
f7e9cb1e 5577 case bpf_ctx_range(struct __sk_buff, mark):
8a31db56 5578 return false;
b005fd18
JF
5579 case bpf_ctx_range(struct __sk_buff, data):
5580 info->reg_type = PTR_TO_PACKET;
5581 break;
5582 case bpf_ctx_range(struct __sk_buff, data_end):
5583 info->reg_type = PTR_TO_PACKET_END;
5584 break;
5585 }
5586
5e43f899 5587 return bpf_skb_is_valid_access(off, size, type, prog, info);
b005fd18
JF
5588}
5589
4f738adb
JF
5590static bool sk_msg_is_valid_access(int off, int size,
5591 enum bpf_access_type type,
5e43f899 5592 const struct bpf_prog *prog,
4f738adb
JF
5593 struct bpf_insn_access_aux *info)
5594{
5595 if (type == BPF_WRITE)
5596 return false;
5597
5598 switch (off) {
5599 case offsetof(struct sk_msg_md, data):
5600 info->reg_type = PTR_TO_PACKET;
303def35
JF
5601 if (size != sizeof(__u64))
5602 return false;
4f738adb
JF
5603 break;
5604 case offsetof(struct sk_msg_md, data_end):
5605 info->reg_type = PTR_TO_PACKET_END;
303def35
JF
5606 if (size != sizeof(__u64))
5607 return false;
4f738adb 5608 break;
303def35
JF
5609 default:
5610 if (size != sizeof(__u32))
5611 return false;
4f738adb
JF
5612 }
5613
5614 if (off < 0 || off >= sizeof(struct sk_msg_md))
5615 return false;
5616 if (off % size != 0)
5617 return false;
4f738adb
JF
5618
5619 return true;
5620}
5621
2492d3b8
DB
5622static u32 bpf_convert_ctx_access(enum bpf_access_type type,
5623 const struct bpf_insn *si,
5624 struct bpf_insn *insn_buf,
f96da094 5625 struct bpf_prog *prog, u32 *target_size)
9bac3d6d
AS
5626{
5627 struct bpf_insn *insn = insn_buf;
6b8cc1d1 5628 int off;
9bac3d6d 5629
6b8cc1d1 5630 switch (si->off) {
9bac3d6d 5631 case offsetof(struct __sk_buff, len):
6b8cc1d1 5632 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5633 bpf_target_off(struct sk_buff, len, 4,
5634 target_size));
9bac3d6d
AS
5635 break;
5636
0b8c707d 5637 case offsetof(struct __sk_buff, protocol):
6b8cc1d1 5638 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
5639 bpf_target_off(struct sk_buff, protocol, 2,
5640 target_size));
0b8c707d
DB
5641 break;
5642
27cd5452 5643 case offsetof(struct __sk_buff, vlan_proto):
6b8cc1d1 5644 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
5645 bpf_target_off(struct sk_buff, vlan_proto, 2,
5646 target_size));
27cd5452
MS
5647 break;
5648
bcad5718 5649 case offsetof(struct __sk_buff, priority):
754f1e6a 5650 if (type == BPF_WRITE)
6b8cc1d1 5651 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5652 bpf_target_off(struct sk_buff, priority, 4,
5653 target_size));
754f1e6a 5654 else
6b8cc1d1 5655 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5656 bpf_target_off(struct sk_buff, priority, 4,
5657 target_size));
bcad5718
DB
5658 break;
5659
37e82c2f 5660 case offsetof(struct __sk_buff, ingress_ifindex):
6b8cc1d1 5661 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5662 bpf_target_off(struct sk_buff, skb_iif, 4,
5663 target_size));
37e82c2f
AS
5664 break;
5665
5666 case offsetof(struct __sk_buff, ifindex):
f035a515 5667 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 5668 si->dst_reg, si->src_reg,
37e82c2f 5669 offsetof(struct sk_buff, dev));
6b8cc1d1
DB
5670 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
5671 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
5672 bpf_target_off(struct net_device, ifindex, 4,
5673 target_size));
37e82c2f
AS
5674 break;
5675
ba7591d8 5676 case offsetof(struct __sk_buff, hash):
6b8cc1d1 5677 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5678 bpf_target_off(struct sk_buff, hash, 4,
5679 target_size));
ba7591d8
DB
5680 break;
5681
9bac3d6d 5682 case offsetof(struct __sk_buff, mark):
d691f9e8 5683 if (type == BPF_WRITE)
6b8cc1d1 5684 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5685 bpf_target_off(struct sk_buff, mark, 4,
5686 target_size));
d691f9e8 5687 else
6b8cc1d1 5688 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5689 bpf_target_off(struct sk_buff, mark, 4,
5690 target_size));
d691f9e8 5691 break;
9bac3d6d
AS
5692
5693 case offsetof(struct __sk_buff, pkt_type):
f96da094
DB
5694 *target_size = 1;
5695 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
5696 PKT_TYPE_OFFSET());
5697 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
5698#ifdef __BIG_ENDIAN_BITFIELD
5699 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
5700#endif
5701 break;
9bac3d6d
AS
5702
5703 case offsetof(struct __sk_buff, queue_mapping):
f96da094
DB
5704 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5705 bpf_target_off(struct sk_buff, queue_mapping, 2,
5706 target_size));
5707 break;
c2497395 5708
c2497395 5709 case offsetof(struct __sk_buff, vlan_present):
c2497395 5710 case offsetof(struct __sk_buff, vlan_tci):
f96da094
DB
5711 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
5712
5713 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5714 bpf_target_off(struct sk_buff, vlan_tci, 2,
5715 target_size));
5716 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
5717 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
5718 ~VLAN_TAG_PRESENT);
5719 } else {
5720 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
5721 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
5722 }
5723 break;
d691f9e8
AS
5724
5725 case offsetof(struct __sk_buff, cb[0]) ...
f96da094 5726 offsetofend(struct __sk_buff, cb[4]) - 1:
d691f9e8 5727 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
62c7989b
DB
5728 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
5729 offsetof(struct qdisc_skb_cb, data)) %
5730 sizeof(__u64));
d691f9e8 5731
ff936a04 5732 prog->cb_access = 1;
6b8cc1d1
DB
5733 off = si->off;
5734 off -= offsetof(struct __sk_buff, cb[0]);
5735 off += offsetof(struct sk_buff, cb);
5736 off += offsetof(struct qdisc_skb_cb, data);
d691f9e8 5737 if (type == BPF_WRITE)
62c7989b 5738 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 5739 si->src_reg, off);
d691f9e8 5740 else
62c7989b 5741 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 5742 si->src_reg, off);
d691f9e8
AS
5743 break;
5744
045efa82 5745 case offsetof(struct __sk_buff, tc_classid):
6b8cc1d1
DB
5746 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
5747
5748 off = si->off;
5749 off -= offsetof(struct __sk_buff, tc_classid);
5750 off += offsetof(struct sk_buff, cb);
5751 off += offsetof(struct qdisc_skb_cb, tc_classid);
f96da094 5752 *target_size = 2;
09c37a2c 5753 if (type == BPF_WRITE)
6b8cc1d1
DB
5754 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
5755 si->src_reg, off);
09c37a2c 5756 else
6b8cc1d1
DB
5757 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
5758 si->src_reg, off);
045efa82
DB
5759 break;
5760
db58ba45 5761 case offsetof(struct __sk_buff, data):
f035a515 5762 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
6b8cc1d1 5763 si->dst_reg, si->src_reg,
db58ba45
AS
5764 offsetof(struct sk_buff, data));
5765 break;
5766
de8f3a83
DB
5767 case offsetof(struct __sk_buff, data_meta):
5768 off = si->off;
5769 off -= offsetof(struct __sk_buff, data_meta);
5770 off += offsetof(struct sk_buff, cb);
5771 off += offsetof(struct bpf_skb_data_end, data_meta);
5772 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5773 si->src_reg, off);
5774 break;
5775
db58ba45 5776 case offsetof(struct __sk_buff, data_end):
6b8cc1d1
DB
5777 off = si->off;
5778 off -= offsetof(struct __sk_buff, data_end);
5779 off += offsetof(struct sk_buff, cb);
5780 off += offsetof(struct bpf_skb_data_end, data_end);
5781 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5782 si->src_reg, off);
db58ba45
AS
5783 break;
5784
d691f9e8
AS
5785 case offsetof(struct __sk_buff, tc_index):
5786#ifdef CONFIG_NET_SCHED
d691f9e8 5787 if (type == BPF_WRITE)
6b8cc1d1 5788 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
5789 bpf_target_off(struct sk_buff, tc_index, 2,
5790 target_size));
d691f9e8 5791 else
6b8cc1d1 5792 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
5793 bpf_target_off(struct sk_buff, tc_index, 2,
5794 target_size));
d691f9e8 5795#else
2ed46ce4 5796 *target_size = 2;
d691f9e8 5797 if (type == BPF_WRITE)
6b8cc1d1 5798 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
d691f9e8 5799 else
6b8cc1d1 5800 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
b1d9fc41
DB
5801#endif
5802 break;
5803
5804 case offsetof(struct __sk_buff, napi_id):
5805#if defined(CONFIG_NET_RX_BUSY_POLL)
b1d9fc41 5806 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5807 bpf_target_off(struct sk_buff, napi_id, 4,
5808 target_size));
b1d9fc41
DB
5809 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
5810 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5811#else
2ed46ce4 5812 *target_size = 4;
b1d9fc41 5813 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
d691f9e8 5814#endif
6b8cc1d1 5815 break;
8a31db56
JF
5816 case offsetof(struct __sk_buff, family):
5817 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
5818
5819 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5820 si->dst_reg, si->src_reg,
5821 offsetof(struct sk_buff, sk));
5822 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5823 bpf_target_off(struct sock_common,
5824 skc_family,
5825 2, target_size));
5826 break;
5827 case offsetof(struct __sk_buff, remote_ip4):
5828 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
5829
5830 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5831 si->dst_reg, si->src_reg,
5832 offsetof(struct sk_buff, sk));
5833 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5834 bpf_target_off(struct sock_common,
5835 skc_daddr,
5836 4, target_size));
5837 break;
5838 case offsetof(struct __sk_buff, local_ip4):
5839 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5840 skc_rcv_saddr) != 4);
5841
5842 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5843 si->dst_reg, si->src_reg,
5844 offsetof(struct sk_buff, sk));
5845 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5846 bpf_target_off(struct sock_common,
5847 skc_rcv_saddr,
5848 4, target_size));
5849 break;
5850 case offsetof(struct __sk_buff, remote_ip6[0]) ...
5851 offsetof(struct __sk_buff, remote_ip6[3]):
5852#if IS_ENABLED(CONFIG_IPV6)
5853 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5854 skc_v6_daddr.s6_addr32[0]) != 4);
5855
5856 off = si->off;
5857 off -= offsetof(struct __sk_buff, remote_ip6[0]);
5858
5859 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5860 si->dst_reg, si->src_reg,
5861 offsetof(struct sk_buff, sk));
5862 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5863 offsetof(struct sock_common,
5864 skc_v6_daddr.s6_addr32[0]) +
5865 off);
5866#else
5867 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5868#endif
5869 break;
5870 case offsetof(struct __sk_buff, local_ip6[0]) ...
5871 offsetof(struct __sk_buff, local_ip6[3]):
5872#if IS_ENABLED(CONFIG_IPV6)
5873 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5874 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
5875
5876 off = si->off;
5877 off -= offsetof(struct __sk_buff, local_ip6[0]);
5878
5879 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5880 si->dst_reg, si->src_reg,
5881 offsetof(struct sk_buff, sk));
5882 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5883 offsetof(struct sock_common,
5884 skc_v6_rcv_saddr.s6_addr32[0]) +
5885 off);
5886#else
5887 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5888#endif
5889 break;
5890
5891 case offsetof(struct __sk_buff, remote_port):
5892 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
5893
5894 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5895 si->dst_reg, si->src_reg,
5896 offsetof(struct sk_buff, sk));
5897 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5898 bpf_target_off(struct sock_common,
5899 skc_dport,
5900 2, target_size));
5901#ifndef __BIG_ENDIAN_BITFIELD
5902 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
5903#endif
5904 break;
5905
5906 case offsetof(struct __sk_buff, local_port):
5907 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
5908
5909 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5910 si->dst_reg, si->src_reg,
5911 offsetof(struct sk_buff, sk));
5912 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5913 bpf_target_off(struct sock_common,
5914 skc_num, 2, target_size));
5915 break;
9bac3d6d
AS
5916 }
5917
5918 return insn - insn_buf;
89aa0758
AS
5919}
5920
61023658 5921static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
6b8cc1d1 5922 const struct bpf_insn *si,
61023658 5923 struct bpf_insn *insn_buf,
f96da094 5924 struct bpf_prog *prog, u32 *target_size)
61023658
DA
5925{
5926 struct bpf_insn *insn = insn_buf;
aac3fc32 5927 int off;
61023658 5928
6b8cc1d1 5929 switch (si->off) {
61023658
DA
5930 case offsetof(struct bpf_sock, bound_dev_if):
5931 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
5932
5933 if (type == BPF_WRITE)
6b8cc1d1 5934 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
5935 offsetof(struct sock, sk_bound_dev_if));
5936 else
6b8cc1d1 5937 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
5938 offsetof(struct sock, sk_bound_dev_if));
5939 break;
aa4c1037 5940
482dca93
DA
5941 case offsetof(struct bpf_sock, mark):
5942 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
5943
5944 if (type == BPF_WRITE)
5945 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5946 offsetof(struct sock, sk_mark));
5947 else
5948 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5949 offsetof(struct sock, sk_mark));
5950 break;
5951
5952 case offsetof(struct bpf_sock, priority):
5953 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
5954
5955 if (type == BPF_WRITE)
5956 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5957 offsetof(struct sock, sk_priority));
5958 else
5959 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5960 offsetof(struct sock, sk_priority));
5961 break;
5962
aa4c1037
DA
5963 case offsetof(struct bpf_sock, family):
5964 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
5965
6b8cc1d1 5966 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
aa4c1037
DA
5967 offsetof(struct sock, sk_family));
5968 break;
5969
5970 case offsetof(struct bpf_sock, type):
6b8cc1d1 5971 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
aa4c1037 5972 offsetof(struct sock, __sk_flags_offset));
6b8cc1d1
DB
5973 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
5974 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
aa4c1037
DA
5975 break;
5976
5977 case offsetof(struct bpf_sock, protocol):
6b8cc1d1 5978 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
aa4c1037 5979 offsetof(struct sock, __sk_flags_offset));
6b8cc1d1
DB
5980 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
5981 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
aa4c1037 5982 break;
aac3fc32
AI
5983
5984 case offsetof(struct bpf_sock, src_ip4):
5985 *insn++ = BPF_LDX_MEM(
5986 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
5987 bpf_target_off(struct sock_common, skc_rcv_saddr,
5988 FIELD_SIZEOF(struct sock_common,
5989 skc_rcv_saddr),
5990 target_size));
5991 break;
5992
5993 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5994#if IS_ENABLED(CONFIG_IPV6)
5995 off = si->off;
5996 off -= offsetof(struct bpf_sock, src_ip6[0]);
5997 *insn++ = BPF_LDX_MEM(
5998 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
5999 bpf_target_off(
6000 struct sock_common,
6001 skc_v6_rcv_saddr.s6_addr32[0],
6002 FIELD_SIZEOF(struct sock_common,
6003 skc_v6_rcv_saddr.s6_addr32[0]),
6004 target_size) + off);
6005#else
6006 (void)off;
6007 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6008#endif
6009 break;
6010
6011 case offsetof(struct bpf_sock, src_port):
6012 *insn++ = BPF_LDX_MEM(
6013 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
6014 si->dst_reg, si->src_reg,
6015 bpf_target_off(struct sock_common, skc_num,
6016 FIELD_SIZEOF(struct sock_common,
6017 skc_num),
6018 target_size));
6019 break;
61023658
DA
6020 }
6021
6022 return insn - insn_buf;
6023}
6024
6b8cc1d1
DB
6025static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
6026 const struct bpf_insn *si,
374fb54e 6027 struct bpf_insn *insn_buf,
f96da094 6028 struct bpf_prog *prog, u32 *target_size)
374fb54e
DB
6029{
6030 struct bpf_insn *insn = insn_buf;
6031
6b8cc1d1 6032 switch (si->off) {
374fb54e 6033 case offsetof(struct __sk_buff, ifindex):
374fb54e 6034 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 6035 si->dst_reg, si->src_reg,
374fb54e 6036 offsetof(struct sk_buff, dev));
6b8cc1d1 6037 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
6038 bpf_target_off(struct net_device, ifindex, 4,
6039 target_size));
374fb54e
DB
6040 break;
6041 default:
f96da094
DB
6042 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6043 target_size);
374fb54e
DB
6044 }
6045
6046 return insn - insn_buf;
6047}
6048
6b8cc1d1
DB
6049static u32 xdp_convert_ctx_access(enum bpf_access_type type,
6050 const struct bpf_insn *si,
6a773a15 6051 struct bpf_insn *insn_buf,
f96da094 6052 struct bpf_prog *prog, u32 *target_size)
6a773a15
BB
6053{
6054 struct bpf_insn *insn = insn_buf;
6055
6b8cc1d1 6056 switch (si->off) {
6a773a15 6057 case offsetof(struct xdp_md, data):
f035a515 6058 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6b8cc1d1 6059 si->dst_reg, si->src_reg,
6a773a15
BB
6060 offsetof(struct xdp_buff, data));
6061 break;
de8f3a83
DB
6062 case offsetof(struct xdp_md, data_meta):
6063 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
6064 si->dst_reg, si->src_reg,
6065 offsetof(struct xdp_buff, data_meta));
6066 break;
6a773a15 6067 case offsetof(struct xdp_md, data_end):
f035a515 6068 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6b8cc1d1 6069 si->dst_reg, si->src_reg,
6a773a15
BB
6070 offsetof(struct xdp_buff, data_end));
6071 break;
02dd3291
JDB
6072 case offsetof(struct xdp_md, ingress_ifindex):
6073 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6074 si->dst_reg, si->src_reg,
6075 offsetof(struct xdp_buff, rxq));
6076 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
6077 si->dst_reg, si->dst_reg,
6078 offsetof(struct xdp_rxq_info, dev));
6079 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
daaf24c6 6080 offsetof(struct net_device, ifindex));
02dd3291
JDB
6081 break;
6082 case offsetof(struct xdp_md, rx_queue_index):
6083 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6084 si->dst_reg, si->src_reg,
6085 offsetof(struct xdp_buff, rxq));
6086 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
daaf24c6
JDB
6087 offsetof(struct xdp_rxq_info,
6088 queue_index));
02dd3291 6089 break;
6a773a15
BB
6090 }
6091
6092 return insn - insn_buf;
6093}
6094
4fbac77d
AI
6095/* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
6096 * context Structure, F is Field in context structure that contains a pointer
6097 * to Nested Structure of type NS that has the field NF.
6098 *
6099 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
6100 * sure that SIZE is not greater than actual size of S.F.NF.
6101 *
6102 * If offset OFF is provided, the load happens from that offset relative to
6103 * offset of NF.
6104 */
6105#define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
6106 do { \
6107 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
6108 si->src_reg, offsetof(S, F)); \
6109 *insn++ = BPF_LDX_MEM( \
6110 SIZE, si->dst_reg, si->dst_reg, \
6111 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6112 target_size) \
6113 + OFF); \
6114 } while (0)
6115
6116#define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
6117 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
6118 BPF_FIELD_SIZEOF(NS, NF), 0)
6119
6120/* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
6121 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
6122 *
6123 * It doesn't support SIZE argument though since narrow stores are not
6124 * supported for now.
6125 *
6126 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
6127 * "register" since two registers available in convert_ctx_access are not
6128 * enough: we can't override neither SRC, since it contains value to store, nor
6129 * DST since it contains pointer to context that may be used by later
6130 * instructions. But we need a temporary place to save pointer to nested
6131 * structure whose field we want to store to.
6132 */
6133#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
6134 do { \
6135 int tmp_reg = BPF_REG_9; \
6136 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6137 --tmp_reg; \
6138 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6139 --tmp_reg; \
6140 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
6141 offsetof(S, TF)); \
6142 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
6143 si->dst_reg, offsetof(S, F)); \
6144 *insn++ = BPF_STX_MEM( \
6145 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
6146 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6147 target_size) \
6148 + OFF); \
6149 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
6150 offsetof(S, TF)); \
6151 } while (0)
6152
6153#define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
6154 TF) \
6155 do { \
6156 if (type == BPF_WRITE) { \
6157 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6158 TF); \
6159 } else { \
6160 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6161 S, NS, F, NF, SIZE, OFF); \
6162 } \
6163 } while (0)
6164
6165#define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6166 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6167 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6168
6169static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6170 const struct bpf_insn *si,
6171 struct bpf_insn *insn_buf,
6172 struct bpf_prog *prog, u32 *target_size)
6173{
6174 struct bpf_insn *insn = insn_buf;
6175 int off;
6176
6177 switch (si->off) {
6178 case offsetof(struct bpf_sock_addr, user_family):
6179 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6180 struct sockaddr, uaddr, sa_family);
6181 break;
6182
6183 case offsetof(struct bpf_sock_addr, user_ip4):
6184 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6185 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6186 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6187 break;
6188
6189 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6190 off = si->off;
6191 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6192 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6193 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6194 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6195 tmp_reg);
6196 break;
6197
6198 case offsetof(struct bpf_sock_addr, user_port):
6199 /* To get port we need to know sa_family first and then treat
6200 * sockaddr as either sockaddr_in or sockaddr_in6.
6201 * Though we can simplify since port field has same offset and
6202 * size in both structures.
6203 * Here we check this invariant and use just one of the
6204 * structures if it's true.
6205 */
6206 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
6207 offsetof(struct sockaddr_in6, sin6_port));
6208 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
6209 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
6210 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
6211 struct sockaddr_in6, uaddr,
6212 sin6_port, tmp_reg);
6213 break;
6214
6215 case offsetof(struct bpf_sock_addr, family):
6216 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6217 struct sock, sk, sk_family);
6218 break;
6219
6220 case offsetof(struct bpf_sock_addr, type):
6221 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6222 struct bpf_sock_addr_kern, struct sock, sk,
6223 __sk_flags_offset, BPF_W, 0);
6224 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6225 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6226 break;
6227
6228 case offsetof(struct bpf_sock_addr, protocol):
6229 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6230 struct bpf_sock_addr_kern, struct sock, sk,
6231 __sk_flags_offset, BPF_W, 0);
6232 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6233 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
6234 SK_FL_PROTO_SHIFT);
6235 break;
1cedee13
AI
6236
6237 case offsetof(struct bpf_sock_addr, msg_src_ip4):
6238 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
6239 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6240 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
6241 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
6242 break;
6243
6244 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6245 msg_src_ip6[3]):
6246 off = si->off;
6247 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
6248 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
6249 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6250 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
6251 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
6252 break;
4fbac77d
AI
6253 }
6254
6255 return insn - insn_buf;
6256}
6257
40304b2a
LB
6258static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
6259 const struct bpf_insn *si,
6260 struct bpf_insn *insn_buf,
f96da094
DB
6261 struct bpf_prog *prog,
6262 u32 *target_size)
40304b2a
LB
6263{
6264 struct bpf_insn *insn = insn_buf;
6265 int off;
6266
6267 switch (si->off) {
6268 case offsetof(struct bpf_sock_ops, op) ...
6269 offsetof(struct bpf_sock_ops, replylong[3]):
6270 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
6271 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
6272 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
6273 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
6274 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
6275 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
6276 off = si->off;
6277 off -= offsetof(struct bpf_sock_ops, op);
6278 off += offsetof(struct bpf_sock_ops_kern, op);
6279 if (type == BPF_WRITE)
6280 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6281 off);
6282 else
6283 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6284 off);
6285 break;
6286
6287 case offsetof(struct bpf_sock_ops, family):
6288 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6289
6290 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6291 struct bpf_sock_ops_kern, sk),
6292 si->dst_reg, si->src_reg,
6293 offsetof(struct bpf_sock_ops_kern, sk));
6294 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6295 offsetof(struct sock_common, skc_family));
6296 break;
6297
6298 case offsetof(struct bpf_sock_ops, remote_ip4):
6299 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6300
6301 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6302 struct bpf_sock_ops_kern, sk),
6303 si->dst_reg, si->src_reg,
6304 offsetof(struct bpf_sock_ops_kern, sk));
6305 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6306 offsetof(struct sock_common, skc_daddr));
6307 break;
6308
6309 case offsetof(struct bpf_sock_ops, local_ip4):
303def35
JF
6310 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6311 skc_rcv_saddr) != 4);
40304b2a
LB
6312
6313 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6314 struct bpf_sock_ops_kern, sk),
6315 si->dst_reg, si->src_reg,
6316 offsetof(struct bpf_sock_ops_kern, sk));
6317 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6318 offsetof(struct sock_common,
6319 skc_rcv_saddr));
6320 break;
6321
6322 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
6323 offsetof(struct bpf_sock_ops, remote_ip6[3]):
6324#if IS_ENABLED(CONFIG_IPV6)
6325 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6326 skc_v6_daddr.s6_addr32[0]) != 4);
6327
6328 off = si->off;
6329 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
6330 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6331 struct bpf_sock_ops_kern, sk),
6332 si->dst_reg, si->src_reg,
6333 offsetof(struct bpf_sock_ops_kern, sk));
6334 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6335 offsetof(struct sock_common,
6336 skc_v6_daddr.s6_addr32[0]) +
6337 off);
6338#else
6339 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6340#endif
6341 break;
6342
6343 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
6344 offsetof(struct bpf_sock_ops, local_ip6[3]):
6345#if IS_ENABLED(CONFIG_IPV6)
6346 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6347 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6348
6349 off = si->off;
6350 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
6351 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6352 struct bpf_sock_ops_kern, sk),
6353 si->dst_reg, si->src_reg,
6354 offsetof(struct bpf_sock_ops_kern, sk));
6355 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6356 offsetof(struct sock_common,
6357 skc_v6_rcv_saddr.s6_addr32[0]) +
6358 off);
6359#else
6360 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6361#endif
6362 break;
6363
6364 case offsetof(struct bpf_sock_ops, remote_port):
6365 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6366
6367 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6368 struct bpf_sock_ops_kern, sk),
6369 si->dst_reg, si->src_reg,
6370 offsetof(struct bpf_sock_ops_kern, sk));
6371 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6372 offsetof(struct sock_common, skc_dport));
6373#ifndef __BIG_ENDIAN_BITFIELD
6374 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6375#endif
6376 break;
6377
6378 case offsetof(struct bpf_sock_ops, local_port):
6379 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6380
6381 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6382 struct bpf_sock_ops_kern, sk),
6383 si->dst_reg, si->src_reg,
6384 offsetof(struct bpf_sock_ops_kern, sk));
6385 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6386 offsetof(struct sock_common, skc_num));
6387 break;
f19397a5
LB
6388
6389 case offsetof(struct bpf_sock_ops, is_fullsock):
6390 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6391 struct bpf_sock_ops_kern,
6392 is_fullsock),
6393 si->dst_reg, si->src_reg,
6394 offsetof(struct bpf_sock_ops_kern,
6395 is_fullsock));
6396 break;
6397
44f0e430
LB
6398 case offsetof(struct bpf_sock_ops, state):
6399 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
6400
6401 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6402 struct bpf_sock_ops_kern, sk),
6403 si->dst_reg, si->src_reg,
6404 offsetof(struct bpf_sock_ops_kern, sk));
6405 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
6406 offsetof(struct sock_common, skc_state));
6407 break;
6408
6409 case offsetof(struct bpf_sock_ops, rtt_min):
6410 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
6411 sizeof(struct minmax));
6412 BUILD_BUG_ON(sizeof(struct minmax) <
6413 sizeof(struct minmax_sample));
6414
6415 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6416 struct bpf_sock_ops_kern, sk),
6417 si->dst_reg, si->src_reg,
6418 offsetof(struct bpf_sock_ops_kern, sk));
6419 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6420 offsetof(struct tcp_sock, rtt_min) +
6421 FIELD_SIZEOF(struct minmax_sample, t));
6422 break;
6423
34d367c5
LB
6424/* Helper macro for adding read access to tcp_sock or sock fields. */
6425#define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
f19397a5 6426 do { \
34d367c5
LB
6427 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6428 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
f19397a5
LB
6429 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6430 struct bpf_sock_ops_kern, \
6431 is_fullsock), \
6432 si->dst_reg, si->src_reg, \
6433 offsetof(struct bpf_sock_ops_kern, \
6434 is_fullsock)); \
6435 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
6436 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6437 struct bpf_sock_ops_kern, sk),\
6438 si->dst_reg, si->src_reg, \
6439 offsetof(struct bpf_sock_ops_kern, sk));\
34d367c5
LB
6440 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
6441 OBJ_FIELD), \
6442 si->dst_reg, si->dst_reg, \
6443 offsetof(OBJ, OBJ_FIELD)); \
f19397a5
LB
6444 } while (0)
6445
b73042b8
LB
6446/* Helper macro for adding write access to tcp_sock or sock fields.
6447 * The macro is called with two registers, dst_reg which contains a pointer
6448 * to ctx (context) and src_reg which contains the value that should be
6449 * stored. However, we need an additional register since we cannot overwrite
6450 * dst_reg because it may be used later in the program.
6451 * Instead we "borrow" one of the other register. We first save its value
6452 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
6453 * it at the end of the macro.
6454 */
6455#define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6456 do { \
6457 int reg = BPF_REG_9; \
6458 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6459 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6460 if (si->dst_reg == reg || si->src_reg == reg) \
6461 reg--; \
6462 if (si->dst_reg == reg || si->src_reg == reg) \
6463 reg--; \
6464 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
6465 offsetof(struct bpf_sock_ops_kern, \
6466 temp)); \
6467 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6468 struct bpf_sock_ops_kern, \
6469 is_fullsock), \
6470 reg, si->dst_reg, \
6471 offsetof(struct bpf_sock_ops_kern, \
6472 is_fullsock)); \
6473 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
6474 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6475 struct bpf_sock_ops_kern, sk),\
6476 reg, si->dst_reg, \
6477 offsetof(struct bpf_sock_ops_kern, sk));\
6478 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
6479 reg, si->src_reg, \
6480 offsetof(OBJ, OBJ_FIELD)); \
6481 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
6482 offsetof(struct bpf_sock_ops_kern, \
6483 temp)); \
6484 } while (0)
6485
6486#define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
6487 do { \
6488 if (TYPE == BPF_WRITE) \
6489 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6490 else \
6491 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6492 } while (0)
6493
f19397a5 6494 case offsetof(struct bpf_sock_ops, snd_cwnd):
34d367c5 6495 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
f19397a5
LB
6496 break;
6497
6498 case offsetof(struct bpf_sock_ops, srtt_us):
34d367c5 6499 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
f19397a5 6500 break;
b13d8807
LB
6501
6502 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
6503 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
6504 struct tcp_sock);
6505 break;
44f0e430
LB
6506
6507 case offsetof(struct bpf_sock_ops, snd_ssthresh):
6508 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
6509 break;
6510
6511 case offsetof(struct bpf_sock_ops, rcv_nxt):
6512 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
6513 break;
6514
6515 case offsetof(struct bpf_sock_ops, snd_nxt):
6516 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
6517 break;
6518
6519 case offsetof(struct bpf_sock_ops, snd_una):
6520 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
6521 break;
6522
6523 case offsetof(struct bpf_sock_ops, mss_cache):
6524 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
6525 break;
6526
6527 case offsetof(struct bpf_sock_ops, ecn_flags):
6528 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6529 break;
6530
6531 case offsetof(struct bpf_sock_ops, rate_delivered):
6532 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6533 struct tcp_sock);
6534 break;
6535
6536 case offsetof(struct bpf_sock_ops, rate_interval_us):
6537 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6538 struct tcp_sock);
6539 break;
6540
6541 case offsetof(struct bpf_sock_ops, packets_out):
6542 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6543 break;
6544
6545 case offsetof(struct bpf_sock_ops, retrans_out):
6546 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6547 break;
6548
6549 case offsetof(struct bpf_sock_ops, total_retrans):
6550 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6551 struct tcp_sock);
6552 break;
6553
6554 case offsetof(struct bpf_sock_ops, segs_in):
6555 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
6556 break;
6557
6558 case offsetof(struct bpf_sock_ops, data_segs_in):
6559 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
6560 break;
6561
6562 case offsetof(struct bpf_sock_ops, segs_out):
6563 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
6564 break;
6565
6566 case offsetof(struct bpf_sock_ops, data_segs_out):
6567 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
6568 struct tcp_sock);
6569 break;
6570
6571 case offsetof(struct bpf_sock_ops, lost_out):
6572 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
6573 break;
6574
6575 case offsetof(struct bpf_sock_ops, sacked_out):
6576 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
6577 break;
6578
6579 case offsetof(struct bpf_sock_ops, sk_txhash):
6f9bd3d7
LB
6580 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
6581 struct sock, type);
44f0e430
LB
6582 break;
6583
6584 case offsetof(struct bpf_sock_ops, bytes_received):
6585 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
6586 struct tcp_sock);
6587 break;
6588
6589 case offsetof(struct bpf_sock_ops, bytes_acked):
6590 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
6591 break;
6f9bd3d7 6592
40304b2a
LB
6593 }
6594 return insn - insn_buf;
6595}
6596
8108a775
JF
6597static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
6598 const struct bpf_insn *si,
6599 struct bpf_insn *insn_buf,
6600 struct bpf_prog *prog, u32 *target_size)
6601{
6602 struct bpf_insn *insn = insn_buf;
6603 int off;
6604
6605 switch (si->off) {
6606 case offsetof(struct __sk_buff, data_end):
6607 off = si->off;
6608 off -= offsetof(struct __sk_buff, data_end);
6609 off += offsetof(struct sk_buff, cb);
6610 off += offsetof(struct tcp_skb_cb, bpf.data_end);
6611 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6612 si->src_reg, off);
6613 break;
6614 default:
6615 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6616 target_size);
6617 }
6618
6619 return insn - insn_buf;
6620}
6621
4f738adb
JF
6622static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
6623 const struct bpf_insn *si,
6624 struct bpf_insn *insn_buf,
6625 struct bpf_prog *prog, u32 *target_size)
6626{
6627 struct bpf_insn *insn = insn_buf;
720e7f38 6628#if IS_ENABLED(CONFIG_IPV6)
303def35 6629 int off;
720e7f38 6630#endif
4f738adb
JF
6631
6632 switch (si->off) {
6633 case offsetof(struct sk_msg_md, data):
6634 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data),
6635 si->dst_reg, si->src_reg,
6636 offsetof(struct sk_msg_buff, data));
6637 break;
6638 case offsetof(struct sk_msg_md, data_end):
6639 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data_end),
6640 si->dst_reg, si->src_reg,
6641 offsetof(struct sk_msg_buff, data_end));
6642 break;
303def35
JF
6643 case offsetof(struct sk_msg_md, family):
6644 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6645
6646 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6647 struct sk_msg_buff, sk),
6648 si->dst_reg, si->src_reg,
6649 offsetof(struct sk_msg_buff, sk));
6650 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6651 offsetof(struct sock_common, skc_family));
6652 break;
6653
6654 case offsetof(struct sk_msg_md, remote_ip4):
6655 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6656
6657 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6658 struct sk_msg_buff, sk),
6659 si->dst_reg, si->src_reg,
6660 offsetof(struct sk_msg_buff, sk));
6661 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6662 offsetof(struct sock_common, skc_daddr));
6663 break;
6664
6665 case offsetof(struct sk_msg_md, local_ip4):
6666 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6667 skc_rcv_saddr) != 4);
6668
6669 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6670 struct sk_msg_buff, sk),
6671 si->dst_reg, si->src_reg,
6672 offsetof(struct sk_msg_buff, sk));
6673 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6674 offsetof(struct sock_common,
6675 skc_rcv_saddr));
6676 break;
6677
6678 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
6679 offsetof(struct sk_msg_md, remote_ip6[3]):
6680#if IS_ENABLED(CONFIG_IPV6)
6681 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6682 skc_v6_daddr.s6_addr32[0]) != 4);
6683
6684 off = si->off;
6685 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
6686 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6687 struct sk_msg_buff, sk),
6688 si->dst_reg, si->src_reg,
6689 offsetof(struct sk_msg_buff, sk));
6690 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6691 offsetof(struct sock_common,
6692 skc_v6_daddr.s6_addr32[0]) +
6693 off);
6694#else
6695 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6696#endif
6697 break;
6698
6699 case offsetof(struct sk_msg_md, local_ip6[0]) ...
6700 offsetof(struct sk_msg_md, local_ip6[3]):
6701#if IS_ENABLED(CONFIG_IPV6)
6702 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6703 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6704
6705 off = si->off;
6706 off -= offsetof(struct sk_msg_md, local_ip6[0]);
6707 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6708 struct sk_msg_buff, sk),
6709 si->dst_reg, si->src_reg,
6710 offsetof(struct sk_msg_buff, sk));
6711 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6712 offsetof(struct sock_common,
6713 skc_v6_rcv_saddr.s6_addr32[0]) +
6714 off);
6715#else
6716 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6717#endif
6718 break;
6719
6720 case offsetof(struct sk_msg_md, remote_port):
6721 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6722
6723 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6724 struct sk_msg_buff, sk),
6725 si->dst_reg, si->src_reg,
6726 offsetof(struct sk_msg_buff, sk));
6727 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6728 offsetof(struct sock_common, skc_dport));
6729#ifndef __BIG_ENDIAN_BITFIELD
6730 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6731#endif
6732 break;
6733
6734 case offsetof(struct sk_msg_md, local_port):
6735 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6736
6737 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6738 struct sk_msg_buff, sk),
6739 si->dst_reg, si->src_reg,
6740 offsetof(struct sk_msg_buff, sk));
6741 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6742 offsetof(struct sock_common, skc_num));
6743 break;
4f738adb
JF
6744 }
6745
6746 return insn - insn_buf;
6747}
6748
7de16e3a 6749const struct bpf_verifier_ops sk_filter_verifier_ops = {
4936e352
DB
6750 .get_func_proto = sk_filter_func_proto,
6751 .is_valid_access = sk_filter_is_valid_access,
2492d3b8 6752 .convert_ctx_access = bpf_convert_ctx_access,
e0cea7ce 6753 .gen_ld_abs = bpf_gen_ld_abs,
89aa0758
AS
6754};
6755
7de16e3a 6756const struct bpf_prog_ops sk_filter_prog_ops = {
61f3c964 6757 .test_run = bpf_prog_test_run_skb,
7de16e3a
JK
6758};
6759
6760const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
4936e352
DB
6761 .get_func_proto = tc_cls_act_func_proto,
6762 .is_valid_access = tc_cls_act_is_valid_access,
374fb54e 6763 .convert_ctx_access = tc_cls_act_convert_ctx_access,
36bbef52 6764 .gen_prologue = tc_cls_act_prologue,
e0cea7ce 6765 .gen_ld_abs = bpf_gen_ld_abs,
7de16e3a
JK
6766};
6767
6768const struct bpf_prog_ops tc_cls_act_prog_ops = {
1cf1cae9 6769 .test_run = bpf_prog_test_run_skb,
608cd71a
AS
6770};
6771
7de16e3a 6772const struct bpf_verifier_ops xdp_verifier_ops = {
6a773a15
BB
6773 .get_func_proto = xdp_func_proto,
6774 .is_valid_access = xdp_is_valid_access,
6775 .convert_ctx_access = xdp_convert_ctx_access,
7de16e3a
JK
6776};
6777
6778const struct bpf_prog_ops xdp_prog_ops = {
1cf1cae9 6779 .test_run = bpf_prog_test_run_xdp,
6a773a15
BB
6780};
6781
7de16e3a 6782const struct bpf_verifier_ops cg_skb_verifier_ops = {
966789fb 6783 .get_func_proto = sk_filter_func_proto,
0e33661d 6784 .is_valid_access = sk_filter_is_valid_access,
2492d3b8 6785 .convert_ctx_access = bpf_convert_ctx_access,
7de16e3a
JK
6786};
6787
6788const struct bpf_prog_ops cg_skb_prog_ops = {
1cf1cae9 6789 .test_run = bpf_prog_test_run_skb,
0e33661d
DM
6790};
6791
cd3092c7
MX
6792const struct bpf_verifier_ops lwt_in_verifier_ops = {
6793 .get_func_proto = lwt_in_func_proto,
3a0af8fd 6794 .is_valid_access = lwt_is_valid_access,
2492d3b8 6795 .convert_ctx_access = bpf_convert_ctx_access,
7de16e3a
JK
6796};
6797
cd3092c7
MX
6798const struct bpf_prog_ops lwt_in_prog_ops = {
6799 .test_run = bpf_prog_test_run_skb,
6800};
6801
6802const struct bpf_verifier_ops lwt_out_verifier_ops = {
6803 .get_func_proto = lwt_out_func_proto,
3a0af8fd 6804 .is_valid_access = lwt_is_valid_access,
2492d3b8 6805 .convert_ctx_access = bpf_convert_ctx_access,
7de16e3a
JK
6806};
6807
cd3092c7 6808const struct bpf_prog_ops lwt_out_prog_ops = {
1cf1cae9 6809 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
6810};
6811
7de16e3a 6812const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
3a0af8fd
TG
6813 .get_func_proto = lwt_xmit_func_proto,
6814 .is_valid_access = lwt_is_valid_access,
2492d3b8 6815 .convert_ctx_access = bpf_convert_ctx_access,
3a0af8fd 6816 .gen_prologue = tc_cls_act_prologue,
7de16e3a
JK
6817};
6818
6819const struct bpf_prog_ops lwt_xmit_prog_ops = {
1cf1cae9 6820 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
6821};
6822
004d4b27
MX
6823const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
6824 .get_func_proto = lwt_seg6local_func_proto,
6825 .is_valid_access = lwt_is_valid_access,
6826 .convert_ctx_access = bpf_convert_ctx_access,
6827};
6828
6829const struct bpf_prog_ops lwt_seg6local_prog_ops = {
6830 .test_run = bpf_prog_test_run_skb,
6831};
6832
7de16e3a 6833const struct bpf_verifier_ops cg_sock_verifier_ops = {
ae2cf1c4 6834 .get_func_proto = sock_filter_func_proto,
61023658
DA
6835 .is_valid_access = sock_filter_is_valid_access,
6836 .convert_ctx_access = sock_filter_convert_ctx_access,
6837};
6838
7de16e3a
JK
6839const struct bpf_prog_ops cg_sock_prog_ops = {
6840};
6841
4fbac77d
AI
6842const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
6843 .get_func_proto = sock_addr_func_proto,
6844 .is_valid_access = sock_addr_is_valid_access,
6845 .convert_ctx_access = sock_addr_convert_ctx_access,
6846};
6847
6848const struct bpf_prog_ops cg_sock_addr_prog_ops = {
6849};
6850
7de16e3a 6851const struct bpf_verifier_ops sock_ops_verifier_ops = {
8c4b4c7e 6852 .get_func_proto = sock_ops_func_proto,
40304b2a
LB
6853 .is_valid_access = sock_ops_is_valid_access,
6854 .convert_ctx_access = sock_ops_convert_ctx_access,
6855};
6856
7de16e3a
JK
6857const struct bpf_prog_ops sock_ops_prog_ops = {
6858};
6859
6860const struct bpf_verifier_ops sk_skb_verifier_ops = {
b005fd18
JF
6861 .get_func_proto = sk_skb_func_proto,
6862 .is_valid_access = sk_skb_is_valid_access,
8108a775 6863 .convert_ctx_access = sk_skb_convert_ctx_access,
8a31db56 6864 .gen_prologue = sk_skb_prologue,
b005fd18
JF
6865};
6866
7de16e3a
JK
6867const struct bpf_prog_ops sk_skb_prog_ops = {
6868};
6869
4f738adb
JF
6870const struct bpf_verifier_ops sk_msg_verifier_ops = {
6871 .get_func_proto = sk_msg_func_proto,
6872 .is_valid_access = sk_msg_is_valid_access,
6873 .convert_ctx_access = sk_msg_convert_ctx_access,
6874};
6875
6876const struct bpf_prog_ops sk_msg_prog_ops = {
6877};
6878
8ced425e 6879int sk_detach_filter(struct sock *sk)
55b33325
PE
6880{
6881 int ret = -ENOENT;
6882 struct sk_filter *filter;
6883
d59577b6
VB
6884 if (sock_flag(sk, SOCK_FILTER_LOCKED))
6885 return -EPERM;
6886
8ced425e
HFS
6887 filter = rcu_dereference_protected(sk->sk_filter,
6888 lockdep_sock_is_held(sk));
55b33325 6889 if (filter) {
a9b3cd7f 6890 RCU_INIT_POINTER(sk->sk_filter, NULL);
46bcf14f 6891 sk_filter_uncharge(sk, filter);
55b33325
PE
6892 ret = 0;
6893 }
a3ea269b 6894
55b33325
PE
6895 return ret;
6896}
8ced425e 6897EXPORT_SYMBOL_GPL(sk_detach_filter);
a8fc9277 6898
a3ea269b
DB
6899int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
6900 unsigned int len)
a8fc9277 6901{
a3ea269b 6902 struct sock_fprog_kern *fprog;
a8fc9277 6903 struct sk_filter *filter;
a3ea269b 6904 int ret = 0;
a8fc9277
PE
6905
6906 lock_sock(sk);
6907 filter = rcu_dereference_protected(sk->sk_filter,
8ced425e 6908 lockdep_sock_is_held(sk));
a8fc9277
PE
6909 if (!filter)
6910 goto out;
a3ea269b
DB
6911
6912 /* We're copying the filter that has been originally attached,
93d08b69
DB
6913 * so no conversion/decode needed anymore. eBPF programs that
6914 * have no original program cannot be dumped through this.
a3ea269b 6915 */
93d08b69 6916 ret = -EACCES;
7ae457c1 6917 fprog = filter->prog->orig_prog;
93d08b69
DB
6918 if (!fprog)
6919 goto out;
a3ea269b
DB
6920
6921 ret = fprog->len;
a8fc9277 6922 if (!len)
a3ea269b 6923 /* User space only enquires number of filter blocks. */
a8fc9277 6924 goto out;
a3ea269b 6925
a8fc9277 6926 ret = -EINVAL;
a3ea269b 6927 if (len < fprog->len)
a8fc9277
PE
6928 goto out;
6929
6930 ret = -EFAULT;
009937e7 6931 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
a3ea269b 6932 goto out;
a8fc9277 6933
a3ea269b
DB
6934 /* Instead of bytes, the API requests to return the number
6935 * of filter blocks.
6936 */
6937 ret = fprog->len;
a8fc9277
PE
6938out:
6939 release_sock(sk);
6940 return ret;
6941}