bpf: generalise tail call map compatibility check
[linux-2.6-block.git] / net / core / filter.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Linux Socket Filter - Kernel level socket filtering
4 *
bd4cf0ed
AS
5 * Based on the design of the Berkeley Packet Filter. The new
6 * internal format has been designed by PLUMgrid:
1da177e4 7 *
bd4cf0ed
AS
8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
9 *
10 * Authors:
11 *
12 * Jay Schulist <jschlst@samba.org>
13 * Alexei Starovoitov <ast@plumgrid.com>
14 * Daniel Borkmann <dborkman@redhat.com>
1da177e4 15 *
1da177e4 16 * Andi Kleen - Fix a few bad bugs and races.
4df95ff4 17 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
1da177e4
LT
18 */
19
201e2c1b 20#include <linux/atomic.h>
1da177e4
LT
21#include <linux/module.h>
22#include <linux/types.h>
1da177e4
LT
23#include <linux/mm.h>
24#include <linux/fcntl.h>
25#include <linux/socket.h>
91b8270f 26#include <linux/sock_diag.h>
1da177e4
LT
27#include <linux/in.h>
28#include <linux/inet.h>
29#include <linux/netdevice.h>
30#include <linux/if_packet.h>
c491680f 31#include <linux/if_arp.h>
5a0e3ad6 32#include <linux/gfp.h>
d74bad4e 33#include <net/inet_common.h>
1da177e4
LT
34#include <net/ip.h>
35#include <net/protocol.h>
4738c1db 36#include <net/netlink.h>
1da177e4 37#include <linux/skbuff.h>
604326b4 38#include <linux/skmsg.h>
1da177e4 39#include <net/sock.h>
10b89ee4 40#include <net/flow_dissector.h>
1da177e4
LT
41#include <linux/errno.h>
42#include <linux/timer.h>
7c0f6ba6 43#include <linux/uaccess.h>
40daafc8 44#include <asm/unaligned.h>
1da177e4 45#include <linux/filter.h>
86e4ca66 46#include <linux/ratelimit.h>
46b325c7 47#include <linux/seccomp.h>
f3335031 48#include <linux/if_vlan.h>
89aa0758 49#include <linux/bpf.h>
af7ec138 50#include <linux/btf.h>
d691f9e8 51#include <net/sch_generic.h>
8d20aabe 52#include <net/cls_cgroup.h>
d3aa45ce 53#include <net/dst_metadata.h>
c46646d0 54#include <net/dst.h>
538950a1 55#include <net/sock_reuseport.h>
b1d9fc41 56#include <net/busy_poll.h>
8c4b4c7e 57#include <net/tcp.h>
12bed760 58#include <net/xfrm.h>
6acc9b43 59#include <net/udp.h>
5acaee0a 60#include <linux/bpf_trace.h>
02671e23 61#include <net/xdp_sock.h>
87f5fc7e 62#include <linux/inetdevice.h>
6acc9b43
JS
63#include <net/inet_hashtables.h>
64#include <net/inet6_hashtables.h>
87f5fc7e 65#include <net/ip_fib.h>
5481d73f 66#include <net/nexthop.h>
87f5fc7e
DA
67#include <net/flow.h>
68#include <net/arp.h>
fe94cc29 69#include <net/ipv6.h>
6acc9b43 70#include <net/net_namespace.h>
fe94cc29
MX
71#include <linux/seg6_local.h>
72#include <net/seg6.h>
73#include <net/seg6_local.h>
52f27877 74#include <net/lwtunnel.h>
3616d08b 75#include <net/ipv6_stubs.h>
6ac99e8f 76#include <net/bpf_sk_storage.h>
478cfbdf 77#include <net/transp_v6.h>
c9a0f3b8 78#include <linux/btf_ids.h>
18ebe16d 79#include <net/tls.h>
7445cf31 80#include <net/xdp.h>
1da177e4 81
1df8f55a
MKL
82static const struct bpf_func_proto *
83bpf_sk_base_func_proto(enum bpf_func_id func_id);
84
b1ea9ff6 85int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
4d295e54
CH
86{
87 if (in_compat_syscall()) {
88 struct compat_sock_fprog f32;
89
90 if (len != sizeof(f32))
91 return -EINVAL;
b1ea9ff6 92 if (copy_from_sockptr(&f32, src, sizeof(f32)))
4d295e54
CH
93 return -EFAULT;
94 memset(dst, 0, sizeof(*dst));
95 dst->len = f32.len;
96 dst->filter = compat_ptr(f32.filter);
97 } else {
98 if (len != sizeof(*dst))
99 return -EINVAL;
b1ea9ff6 100 if (copy_from_sockptr(dst, src, sizeof(*dst)))
4d295e54
CH
101 return -EFAULT;
102 }
103
104 return 0;
105}
106EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user);
107
43db6d65 108/**
f4979fce 109 * sk_filter_trim_cap - run a packet through a socket filter
43db6d65
SH
110 * @sk: sock associated with &sk_buff
111 * @skb: buffer to filter
f4979fce 112 * @cap: limit on how short the eBPF program may trim the packet
43db6d65 113 *
ff936a04
AS
114 * Run the eBPF program and then cut skb->data to correct size returned by
115 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
43db6d65 116 * than pkt_len we keep whole skb->data. This is the socket level
fb7dd8bc 117 * wrapper to bpf_prog_run. It returns 0 if the packet should
43db6d65
SH
118 * be accepted or -EPERM if the packet should be tossed.
119 *
120 */
f4979fce 121int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
43db6d65
SH
122{
123 int err;
124 struct sk_filter *filter;
125
c93bdd0e
MG
126 /*
127 * If the skb was allocated from pfmemalloc reserves, only
128 * allow SOCK_MEMALLOC sockets to use it as this socket is
129 * helping free memory
130 */
8fe809a9
ED
131 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
132 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
c93bdd0e 133 return -ENOMEM;
8fe809a9 134 }
c11cd3a6
DM
135 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
136 if (err)
137 return err;
138
43db6d65
SH
139 err = security_sock_rcv_skb(sk, skb);
140 if (err)
141 return err;
142
80f8f102
ED
143 rcu_read_lock();
144 filter = rcu_dereference(sk->sk_filter);
43db6d65 145 if (filter) {
8f917bba
WB
146 struct sock *save_sk = skb->sk;
147 unsigned int pkt_len;
148
149 skb->sk = sk;
150 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
8f917bba 151 skb->sk = save_sk;
d1f496fd 152 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
43db6d65 153 }
80f8f102 154 rcu_read_unlock();
43db6d65
SH
155
156 return err;
157}
f4979fce 158EXPORT_SYMBOL(sk_filter_trim_cap);
43db6d65 159
b390134c 160BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
bd4cf0ed 161{
f3694e00 162 return skb_get_poff(skb);
bd4cf0ed
AS
163}
164
b390134c 165BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
bd4cf0ed 166{
bd4cf0ed
AS
167 struct nlattr *nla;
168
169 if (skb_is_nonlinear(skb))
170 return 0;
171
05ab8f26
MK
172 if (skb->len < sizeof(struct nlattr))
173 return 0;
174
30743837 175 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
176 return 0;
177
30743837 178 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
bd4cf0ed
AS
179 if (nla)
180 return (void *) nla - (void *) skb->data;
181
182 return 0;
183}
184
b390134c 185BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
bd4cf0ed 186{
bd4cf0ed
AS
187 struct nlattr *nla;
188
189 if (skb_is_nonlinear(skb))
190 return 0;
191
05ab8f26
MK
192 if (skb->len < sizeof(struct nlattr))
193 return 0;
194
30743837 195 if (a > skb->len - sizeof(struct nlattr))
bd4cf0ed
AS
196 return 0;
197
30743837
DB
198 nla = (struct nlattr *) &skb->data[a];
199 if (nla->nla_len > skb->len - a)
bd4cf0ed
AS
200 return 0;
201
30743837 202 nla = nla_find_nested(nla, x);
bd4cf0ed
AS
203 if (nla)
204 return (void *) nla - (void *) skb->data;
205
206 return 0;
207}
208
e0cea7ce
DB
209BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
210 data, int, headlen, int, offset)
211{
212 u8 tmp, *ptr;
213 const int len = sizeof(tmp);
214
215 if (offset >= 0) {
216 if (headlen - offset >= len)
217 return *(u8 *)(data + offset);
218 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
219 return tmp;
220 } else {
221 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
222 if (likely(ptr))
223 return *(u8 *)ptr;
224 }
225
226 return -EFAULT;
227}
228
229BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
230 int, offset)
231{
232 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
233 offset);
234}
235
236BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
237 data, int, headlen, int, offset)
238{
239 u16 tmp, *ptr;
240 const int len = sizeof(tmp);
241
242 if (offset >= 0) {
243 if (headlen - offset >= len)
244 return get_unaligned_be16(data + offset);
245 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
246 return be16_to_cpu(tmp);
247 } else {
248 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
249 if (likely(ptr))
250 return get_unaligned_be16(ptr);
251 }
252
253 return -EFAULT;
254}
255
256BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
257 int, offset)
258{
259 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
260 offset);
261}
262
263BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
264 data, int, headlen, int, offset)
265{
266 u32 tmp, *ptr;
267 const int len = sizeof(tmp);
268
269 if (likely(offset >= 0)) {
270 if (headlen - offset >= len)
271 return get_unaligned_be32(data + offset);
272 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
273 return be32_to_cpu(tmp);
274 } else {
275 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
276 if (likely(ptr))
277 return get_unaligned_be32(ptr);
278 }
279
280 return -EFAULT;
281}
282
283BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
284 int, offset)
285{
286 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
287 offset);
288}
289
9bac3d6d
AS
290static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
291 struct bpf_insn *insn_buf)
292{
293 struct bpf_insn *insn = insn_buf;
294
295 switch (skb_field) {
296 case SKF_AD_MARK:
c593642c 297 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
9bac3d6d
AS
298
299 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
300 offsetof(struct sk_buff, mark));
301 break;
302
303 case SKF_AD_PKTTYPE:
fba84957 304 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET);
9bac3d6d
AS
305 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
306#ifdef __BIG_ENDIAN_BITFIELD
307 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
308#endif
309 break;
310
311 case SKF_AD_QUEUE:
c593642c 312 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2);
9bac3d6d
AS
313
314 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
315 offsetof(struct sk_buff, queue_mapping));
316 break;
c2497395 317
c2497395 318 case SKF_AD_VLAN_TAG:
c593642c 319 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
c2497395
AS
320
321 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
322 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
323 offsetof(struct sk_buff, vlan_tci));
9c212255
MM
324 break;
325 case SKF_AD_VLAN_TAG_PRESENT:
fba84957 326 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET);
9c212255
MM
327 if (PKT_VLAN_PRESENT_BIT)
328 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT);
329 if (PKT_VLAN_PRESENT_BIT < 7)
c2497395 330 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
c2497395 331 break;
9bac3d6d
AS
332 }
333
334 return insn - insn_buf;
335}
336
bd4cf0ed 337static bool convert_bpf_extensions(struct sock_filter *fp,
2695fb55 338 struct bpf_insn **insnp)
bd4cf0ed 339{
2695fb55 340 struct bpf_insn *insn = *insnp;
9bac3d6d 341 u32 cnt;
bd4cf0ed
AS
342
343 switch (fp->k) {
344 case SKF_AD_OFF + SKF_AD_PROTOCOL:
c593642c 345 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2);
0b8c707d
DB
346
347 /* A = *(u16 *) (CTX + offsetof(protocol)) */
348 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
349 offsetof(struct sk_buff, protocol));
350 /* A = ntohs(A) [emitting a nop or swap16] */
351 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
bd4cf0ed
AS
352 break;
353
354 case SKF_AD_OFF + SKF_AD_PKTTYPE:
9bac3d6d
AS
355 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
356 insn += cnt - 1;
bd4cf0ed
AS
357 break;
358
359 case SKF_AD_OFF + SKF_AD_IFINDEX:
360 case SKF_AD_OFF + SKF_AD_HATYPE:
c593642c
PB
361 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4);
362 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2);
f8f6d679 363
f035a515 364 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
f8f6d679
DB
365 BPF_REG_TMP, BPF_REG_CTX,
366 offsetof(struct sk_buff, dev));
367 /* if (tmp != 0) goto pc + 1 */
368 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
369 *insn++ = BPF_EXIT_INSN();
370 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
371 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
372 offsetof(struct net_device, ifindex));
373 else
374 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
375 offsetof(struct net_device, type));
bd4cf0ed
AS
376 break;
377
378 case SKF_AD_OFF + SKF_AD_MARK:
9bac3d6d
AS
379 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
380 insn += cnt - 1;
bd4cf0ed
AS
381 break;
382
383 case SKF_AD_OFF + SKF_AD_RXHASH:
c593642c 384 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
bd4cf0ed 385
9739eef1
AS
386 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
387 offsetof(struct sk_buff, hash));
bd4cf0ed
AS
388 break;
389
390 case SKF_AD_OFF + SKF_AD_QUEUE:
9bac3d6d
AS
391 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
392 insn += cnt - 1;
bd4cf0ed
AS
393 break;
394
395 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
c2497395
AS
396 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
397 BPF_REG_A, BPF_REG_CTX, insn);
398 insn += cnt - 1;
399 break;
bd4cf0ed 400
c2497395
AS
401 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
402 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
403 BPF_REG_A, BPF_REG_CTX, insn);
404 insn += cnt - 1;
bd4cf0ed
AS
405 break;
406
27cd5452 407 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
c593642c 408 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2);
27cd5452
MS
409
410 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
411 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
412 offsetof(struct sk_buff, vlan_proto));
413 /* A = ntohs(A) [emitting a nop or swap16] */
414 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
415 break;
416
bd4cf0ed
AS
417 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
418 case SKF_AD_OFF + SKF_AD_NLATTR:
419 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
420 case SKF_AD_OFF + SKF_AD_CPU:
4cd3675e 421 case SKF_AD_OFF + SKF_AD_RANDOM:
e430f34e 422 /* arg1 = CTX */
f8f6d679 423 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
bd4cf0ed 424 /* arg2 = A */
f8f6d679 425 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
bd4cf0ed 426 /* arg3 = X */
f8f6d679 427 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
e430f34e 428 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
bd4cf0ed
AS
429 switch (fp->k) {
430 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
b390134c 431 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
bd4cf0ed
AS
432 break;
433 case SKF_AD_OFF + SKF_AD_NLATTR:
b390134c 434 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
bd4cf0ed
AS
435 break;
436 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
b390134c 437 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
bd4cf0ed
AS
438 break;
439 case SKF_AD_OFF + SKF_AD_CPU:
b390134c 440 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
bd4cf0ed 441 break;
4cd3675e 442 case SKF_AD_OFF + SKF_AD_RANDOM:
3ad00405
DB
443 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
444 bpf_user_rnd_init_once();
4cd3675e 445 break;
bd4cf0ed
AS
446 }
447 break;
448
449 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
9739eef1
AS
450 /* A ^= X */
451 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
452 break;
453
454 default:
455 /* This is just a dummy call to avoid letting the compiler
456 * evict __bpf_call_base() as an optimization. Placed here
457 * where no-one bothers.
458 */
459 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
460 return false;
461 }
462
463 *insnp = insn;
464 return true;
465}
466
e0cea7ce
DB
467static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
468{
469 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
470 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
471 bool endian = BPF_SIZE(fp->code) == BPF_H ||
472 BPF_SIZE(fp->code) == BPF_W;
473 bool indirect = BPF_MODE(fp->code) == BPF_IND;
474 const int ip_align = NET_IP_ALIGN;
475 struct bpf_insn *insn = *insnp;
476 int offset = fp->k;
477
478 if (!indirect &&
479 ((unaligned_ok && offset >= 0) ||
480 (!unaligned_ok && offset >= 0 &&
481 offset + ip_align >= 0 &&
482 offset + ip_align % size == 0))) {
59ee4129
DB
483 bool ldx_off_ok = offset <= S16_MAX;
484
e0cea7ce 485 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
d8f3e978
DM
486 if (offset)
487 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
59ee4129
DB
488 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
489 size, 2 + endian + (!ldx_off_ok * 2));
490 if (ldx_off_ok) {
491 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
492 BPF_REG_D, offset);
493 } else {
494 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
495 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
496 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
497 BPF_REG_TMP, 0);
498 }
e0cea7ce
DB
499 if (endian)
500 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
501 *insn++ = BPF_JMP_A(8);
502 }
503
504 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
505 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
506 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
507 if (!indirect) {
508 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
509 } else {
510 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
511 if (fp->k)
512 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
513 }
514
515 switch (BPF_SIZE(fp->code)) {
516 case BPF_B:
517 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
518 break;
519 case BPF_H:
520 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
521 break;
522 case BPF_W:
523 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
524 break;
525 default:
526 return false;
527 }
528
529 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
530 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
531 *insn = BPF_EXIT_INSN();
532
533 *insnp = insn;
534 return true;
535}
536
bd4cf0ed 537/**
8fb575ca 538 * bpf_convert_filter - convert filter program
bd4cf0ed
AS
539 * @prog: the user passed filter program
540 * @len: the length of the user passed filter program
50bbfed9 541 * @new_prog: allocated 'struct bpf_prog' or NULL
bd4cf0ed 542 * @new_len: pointer to store length of converted program
e0cea7ce 543 * @seen_ld_abs: bool whether we've seen ld_abs/ind
bd4cf0ed 544 *
1f504ec9
TK
545 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
546 * style extended BPF (eBPF).
bd4cf0ed
AS
547 * Conversion workflow:
548 *
549 * 1) First pass for calculating the new program length:
e0cea7ce 550 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
bd4cf0ed
AS
551 *
552 * 2) 2nd pass to remap in two passes: 1st pass finds new
553 * jump offsets, 2nd pass remapping:
e0cea7ce 554 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
bd4cf0ed 555 */
d9e12f42 556static int bpf_convert_filter(struct sock_filter *prog, int len,
e0cea7ce
DB
557 struct bpf_prog *new_prog, int *new_len,
558 bool *seen_ld_abs)
bd4cf0ed 559{
50bbfed9
AS
560 int new_flen = 0, pass = 0, target, i, stack_off;
561 struct bpf_insn *new_insn, *first_insn = NULL;
bd4cf0ed
AS
562 struct sock_filter *fp;
563 int *addrs = NULL;
564 u8 bpf_src;
565
566 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
30743837 567 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
bd4cf0ed 568
6f9a093b 569 if (len <= 0 || len > BPF_MAXINSNS)
bd4cf0ed
AS
570 return -EINVAL;
571
572 if (new_prog) {
50bbfed9 573 first_insn = new_prog->insnsi;
658da937
DB
574 addrs = kcalloc(len, sizeof(*addrs),
575 GFP_KERNEL | __GFP_NOWARN);
bd4cf0ed
AS
576 if (!addrs)
577 return -ENOMEM;
578 }
579
580do_pass:
50bbfed9 581 new_insn = first_insn;
bd4cf0ed
AS
582 fp = prog;
583
8b614aeb 584 /* Classic BPF related prologue emission. */
50bbfed9 585 if (new_prog) {
8b614aeb
DB
586 /* Classic BPF expects A and X to be reset first. These need
587 * to be guaranteed to be the first two instructions.
588 */
1d621674
DB
589 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
590 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
8b614aeb
DB
591
592 /* All programs must keep CTX in callee saved BPF_REG_CTX.
593 * In eBPF case it's done by the compiler, here we need to
594 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
595 */
596 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
e0cea7ce
DB
597 if (*seen_ld_abs) {
598 /* For packet access in classic BPF, cache skb->data
599 * in callee-saved BPF R8 and skb->len - skb->data_len
600 * (headlen) in BPF R9. Since classic BPF is read-only
601 * on CTX, we only need to cache it once.
602 */
603 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
604 BPF_REG_D, BPF_REG_CTX,
605 offsetof(struct sk_buff, data));
606 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
607 offsetof(struct sk_buff, len));
608 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
609 offsetof(struct sk_buff, data_len));
610 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
611 }
8b614aeb
DB
612 } else {
613 new_insn += 3;
614 }
bd4cf0ed
AS
615
616 for (i = 0; i < len; fp++, i++) {
e0cea7ce 617 struct bpf_insn tmp_insns[32] = { };
2695fb55 618 struct bpf_insn *insn = tmp_insns;
bd4cf0ed
AS
619
620 if (addrs)
50bbfed9 621 addrs[i] = new_insn - first_insn;
bd4cf0ed
AS
622
623 switch (fp->code) {
624 /* All arithmetic insns and skb loads map as-is. */
625 case BPF_ALU | BPF_ADD | BPF_X:
626 case BPF_ALU | BPF_ADD | BPF_K:
627 case BPF_ALU | BPF_SUB | BPF_X:
628 case BPF_ALU | BPF_SUB | BPF_K:
629 case BPF_ALU | BPF_AND | BPF_X:
630 case BPF_ALU | BPF_AND | BPF_K:
631 case BPF_ALU | BPF_OR | BPF_X:
632 case BPF_ALU | BPF_OR | BPF_K:
633 case BPF_ALU | BPF_LSH | BPF_X:
634 case BPF_ALU | BPF_LSH | BPF_K:
635 case BPF_ALU | BPF_RSH | BPF_X:
636 case BPF_ALU | BPF_RSH | BPF_K:
637 case BPF_ALU | BPF_XOR | BPF_X:
638 case BPF_ALU | BPF_XOR | BPF_K:
639 case BPF_ALU | BPF_MUL | BPF_X:
640 case BPF_ALU | BPF_MUL | BPF_K:
641 case BPF_ALU | BPF_DIV | BPF_X:
642 case BPF_ALU | BPF_DIV | BPF_K:
643 case BPF_ALU | BPF_MOD | BPF_X:
644 case BPF_ALU | BPF_MOD | BPF_K:
645 case BPF_ALU | BPF_NEG:
646 case BPF_LD | BPF_ABS | BPF_W:
647 case BPF_LD | BPF_ABS | BPF_H:
648 case BPF_LD | BPF_ABS | BPF_B:
649 case BPF_LD | BPF_IND | BPF_W:
650 case BPF_LD | BPF_IND | BPF_H:
651 case BPF_LD | BPF_IND | BPF_B:
652 /* Check for overloaded BPF extension and
653 * directly convert it if found, otherwise
654 * just move on with mapping.
655 */
656 if (BPF_CLASS(fp->code) == BPF_LD &&
657 BPF_MODE(fp->code) == BPF_ABS &&
658 convert_bpf_extensions(fp, &insn))
659 break;
e0cea7ce
DB
660 if (BPF_CLASS(fp->code) == BPF_LD &&
661 convert_bpf_ld_abs(fp, &insn)) {
662 *seen_ld_abs = true;
663 break;
664 }
bd4cf0ed 665
68fda450 666 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
f6b1b3bf 667 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
68fda450 668 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
f6b1b3bf
DB
669 /* Error with exception code on div/mod by 0.
670 * For cBPF programs, this was always return 0.
671 */
672 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
673 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
674 *insn++ = BPF_EXIT_INSN();
675 }
68fda450 676
f8f6d679 677 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
bd4cf0ed
AS
678 break;
679
f8f6d679
DB
680 /* Jump transformation cannot use BPF block macros
681 * everywhere as offset calculation and target updates
682 * require a bit more work than the rest, i.e. jump
683 * opcodes map as-is, but offsets need adjustment.
684 */
685
686#define BPF_EMIT_JMP \
bd4cf0ed 687 do { \
050fad7c
DB
688 const s32 off_min = S16_MIN, off_max = S16_MAX; \
689 s32 off; \
690 \
bd4cf0ed
AS
691 if (target >= len || target < 0) \
692 goto err; \
050fad7c 693 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
bd4cf0ed 694 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
050fad7c
DB
695 off -= insn - tmp_insns; \
696 /* Reject anything not fitting into insn->off. */ \
697 if (off < off_min || off > off_max) \
698 goto err; \
699 insn->off = off; \
bd4cf0ed
AS
700 } while (0)
701
f8f6d679
DB
702 case BPF_JMP | BPF_JA:
703 target = i + fp->k + 1;
704 insn->code = fp->code;
705 BPF_EMIT_JMP;
bd4cf0ed
AS
706 break;
707
708 case BPF_JMP | BPF_JEQ | BPF_K:
709 case BPF_JMP | BPF_JEQ | BPF_X:
710 case BPF_JMP | BPF_JSET | BPF_K:
711 case BPF_JMP | BPF_JSET | BPF_X:
712 case BPF_JMP | BPF_JGT | BPF_K:
713 case BPF_JMP | BPF_JGT | BPF_X:
714 case BPF_JMP | BPF_JGE | BPF_K:
715 case BPF_JMP | BPF_JGE | BPF_X:
716 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
717 /* BPF immediates are signed, zero extend
718 * immediate into tmp register and use it
719 * in compare insn.
720 */
f8f6d679 721 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
bd4cf0ed 722
e430f34e
AS
723 insn->dst_reg = BPF_REG_A;
724 insn->src_reg = BPF_REG_TMP;
bd4cf0ed
AS
725 bpf_src = BPF_X;
726 } else {
e430f34e 727 insn->dst_reg = BPF_REG_A;
bd4cf0ed
AS
728 insn->imm = fp->k;
729 bpf_src = BPF_SRC(fp->code);
19539ce7 730 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
1da177e4 731 }
bd4cf0ed
AS
732
733 /* Common case where 'jump_false' is next insn. */
734 if (fp->jf == 0) {
735 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
736 target = i + fp->jt + 1;
f8f6d679 737 BPF_EMIT_JMP;
bd4cf0ed 738 break;
1da177e4 739 }
bd4cf0ed 740
92b31a9a
DB
741 /* Convert some jumps when 'jump_true' is next insn. */
742 if (fp->jt == 0) {
743 switch (BPF_OP(fp->code)) {
744 case BPF_JEQ:
745 insn->code = BPF_JMP | BPF_JNE | bpf_src;
746 break;
747 case BPF_JGT:
748 insn->code = BPF_JMP | BPF_JLE | bpf_src;
749 break;
750 case BPF_JGE:
751 insn->code = BPF_JMP | BPF_JLT | bpf_src;
752 break;
753 default:
754 goto jmp_rest;
755 }
756
bd4cf0ed 757 target = i + fp->jf + 1;
f8f6d679 758 BPF_EMIT_JMP;
bd4cf0ed 759 break;
0b05b2a4 760 }
92b31a9a 761jmp_rest:
bd4cf0ed
AS
762 /* Other jumps are mapped into two insns: Jxx and JA. */
763 target = i + fp->jt + 1;
764 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
f8f6d679 765 BPF_EMIT_JMP;
bd4cf0ed
AS
766 insn++;
767
768 insn->code = BPF_JMP | BPF_JA;
769 target = i + fp->jf + 1;
f8f6d679 770 BPF_EMIT_JMP;
bd4cf0ed
AS
771 break;
772
773 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
e0cea7ce
DB
774 case BPF_LDX | BPF_MSH | BPF_B: {
775 struct sock_filter tmp = {
776 .code = BPF_LD | BPF_ABS | BPF_B,
777 .k = fp->k,
778 };
779
780 *seen_ld_abs = true;
781
782 /* X = A */
783 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
1268e253 784 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
e0cea7ce
DB
785 convert_bpf_ld_abs(&tmp, &insn);
786 insn++;
9739eef1 787 /* A &= 0xf */
f8f6d679 788 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
9739eef1 789 /* A <<= 2 */
f8f6d679 790 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
e0cea7ce
DB
791 /* tmp = X */
792 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
9739eef1 793 /* X = A */
f8f6d679 794 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
9739eef1 795 /* A = tmp */
f8f6d679 796 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
bd4cf0ed 797 break;
e0cea7ce 798 }
6205b9cf
DB
799 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
800 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
801 */
bd4cf0ed
AS
802 case BPF_RET | BPF_A:
803 case BPF_RET | BPF_K:
6205b9cf
DB
804 if (BPF_RVAL(fp->code) == BPF_K)
805 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
806 0, fp->k);
9739eef1 807 *insn = BPF_EXIT_INSN();
bd4cf0ed
AS
808 break;
809
810 /* Store to stack. */
811 case BPF_ST:
812 case BPF_STX:
50bbfed9 813 stack_off = fp->k * 4 + 4;
f8f6d679
DB
814 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
815 BPF_ST ? BPF_REG_A : BPF_REG_X,
50bbfed9
AS
816 -stack_off);
817 /* check_load_and_stores() verifies that classic BPF can
818 * load from stack only after write, so tracking
819 * stack_depth for ST|STX insns is enough
820 */
821 if (new_prog && new_prog->aux->stack_depth < stack_off)
822 new_prog->aux->stack_depth = stack_off;
bd4cf0ed
AS
823 break;
824
825 /* Load from stack. */
826 case BPF_LD | BPF_MEM:
827 case BPF_LDX | BPF_MEM:
50bbfed9 828 stack_off = fp->k * 4 + 4;
f8f6d679
DB
829 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
830 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
50bbfed9 831 -stack_off);
bd4cf0ed
AS
832 break;
833
834 /* A = K or X = K */
835 case BPF_LD | BPF_IMM:
836 case BPF_LDX | BPF_IMM:
f8f6d679
DB
837 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
838 BPF_REG_A : BPF_REG_X, fp->k);
bd4cf0ed
AS
839 break;
840
841 /* X = A */
842 case BPF_MISC | BPF_TAX:
f8f6d679 843 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
bd4cf0ed
AS
844 break;
845
846 /* A = X */
847 case BPF_MISC | BPF_TXA:
f8f6d679 848 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
bd4cf0ed
AS
849 break;
850
851 /* A = skb->len or X = skb->len */
852 case BPF_LD | BPF_W | BPF_LEN:
853 case BPF_LDX | BPF_W | BPF_LEN:
f8f6d679
DB
854 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
855 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
856 offsetof(struct sk_buff, len));
bd4cf0ed
AS
857 break;
858
f8f6d679 859 /* Access seccomp_data fields. */
bd4cf0ed 860 case BPF_LDX | BPF_ABS | BPF_W:
9739eef1
AS
861 /* A = *(u32 *) (ctx + K) */
862 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
bd4cf0ed
AS
863 break;
864
ca9f1fd2 865 /* Unknown instruction. */
1da177e4 866 default:
bd4cf0ed 867 goto err;
1da177e4 868 }
bd4cf0ed
AS
869
870 insn++;
871 if (new_prog)
872 memcpy(new_insn, tmp_insns,
873 sizeof(*insn) * (insn - tmp_insns));
bd4cf0ed 874 new_insn += insn - tmp_insns;
1da177e4
LT
875 }
876
bd4cf0ed
AS
877 if (!new_prog) {
878 /* Only calculating new length. */
50bbfed9 879 *new_len = new_insn - first_insn;
e0cea7ce
DB
880 if (*seen_ld_abs)
881 *new_len += 4; /* Prologue bits. */
bd4cf0ed
AS
882 return 0;
883 }
884
885 pass++;
50bbfed9
AS
886 if (new_flen != new_insn - first_insn) {
887 new_flen = new_insn - first_insn;
bd4cf0ed
AS
888 if (pass > 2)
889 goto err;
bd4cf0ed
AS
890 goto do_pass;
891 }
892
893 kfree(addrs);
894 BUG_ON(*new_len != new_flen);
1da177e4 895 return 0;
bd4cf0ed
AS
896err:
897 kfree(addrs);
898 return -EINVAL;
1da177e4
LT
899}
900
bd4cf0ed 901/* Security:
bd4cf0ed 902 *
2d5311e4 903 * As we dont want to clear mem[] array for each packet going through
8ea6e345 904 * __bpf_prog_run(), we check that filter loaded by user never try to read
2d5311e4 905 * a cell if not previously written, and we check all branches to be sure
25985edc 906 * a malicious user doesn't try to abuse us.
2d5311e4 907 */
ec31a05c 908static int check_load_and_stores(const struct sock_filter *filter, int flen)
2d5311e4 909{
34805931 910 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
2d5311e4
ED
911 int pc, ret = 0;
912
913 BUILD_BUG_ON(BPF_MEMWORDS > 16);
34805931 914
99e72a0f 915 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
2d5311e4
ED
916 if (!masks)
917 return -ENOMEM;
34805931 918
2d5311e4
ED
919 memset(masks, 0xff, flen * sizeof(*masks));
920
921 for (pc = 0; pc < flen; pc++) {
922 memvalid &= masks[pc];
923
924 switch (filter[pc].code) {
34805931
DB
925 case BPF_ST:
926 case BPF_STX:
2d5311e4
ED
927 memvalid |= (1 << filter[pc].k);
928 break;
34805931
DB
929 case BPF_LD | BPF_MEM:
930 case BPF_LDX | BPF_MEM:
2d5311e4
ED
931 if (!(memvalid & (1 << filter[pc].k))) {
932 ret = -EINVAL;
933 goto error;
934 }
935 break;
34805931
DB
936 case BPF_JMP | BPF_JA:
937 /* A jump must set masks on target */
2d5311e4
ED
938 masks[pc + 1 + filter[pc].k] &= memvalid;
939 memvalid = ~0;
940 break;
34805931
DB
941 case BPF_JMP | BPF_JEQ | BPF_K:
942 case BPF_JMP | BPF_JEQ | BPF_X:
943 case BPF_JMP | BPF_JGE | BPF_K:
944 case BPF_JMP | BPF_JGE | BPF_X:
945 case BPF_JMP | BPF_JGT | BPF_K:
946 case BPF_JMP | BPF_JGT | BPF_X:
947 case BPF_JMP | BPF_JSET | BPF_K:
948 case BPF_JMP | BPF_JSET | BPF_X:
949 /* A jump must set masks on targets */
2d5311e4
ED
950 masks[pc + 1 + filter[pc].jt] &= memvalid;
951 masks[pc + 1 + filter[pc].jf] &= memvalid;
952 memvalid = ~0;
953 break;
954 }
955 }
956error:
957 kfree(masks);
958 return ret;
959}
960
34805931
DB
961static bool chk_code_allowed(u16 code_to_probe)
962{
963 static const bool codes[] = {
964 /* 32 bit ALU operations */
965 [BPF_ALU | BPF_ADD | BPF_K] = true,
966 [BPF_ALU | BPF_ADD | BPF_X] = true,
967 [BPF_ALU | BPF_SUB | BPF_K] = true,
968 [BPF_ALU | BPF_SUB | BPF_X] = true,
969 [BPF_ALU | BPF_MUL | BPF_K] = true,
970 [BPF_ALU | BPF_MUL | BPF_X] = true,
971 [BPF_ALU | BPF_DIV | BPF_K] = true,
972 [BPF_ALU | BPF_DIV | BPF_X] = true,
973 [BPF_ALU | BPF_MOD | BPF_K] = true,
974 [BPF_ALU | BPF_MOD | BPF_X] = true,
975 [BPF_ALU | BPF_AND | BPF_K] = true,
976 [BPF_ALU | BPF_AND | BPF_X] = true,
977 [BPF_ALU | BPF_OR | BPF_K] = true,
978 [BPF_ALU | BPF_OR | BPF_X] = true,
979 [BPF_ALU | BPF_XOR | BPF_K] = true,
980 [BPF_ALU | BPF_XOR | BPF_X] = true,
981 [BPF_ALU | BPF_LSH | BPF_K] = true,
982 [BPF_ALU | BPF_LSH | BPF_X] = true,
983 [BPF_ALU | BPF_RSH | BPF_K] = true,
984 [BPF_ALU | BPF_RSH | BPF_X] = true,
985 [BPF_ALU | BPF_NEG] = true,
986 /* Load instructions */
987 [BPF_LD | BPF_W | BPF_ABS] = true,
988 [BPF_LD | BPF_H | BPF_ABS] = true,
989 [BPF_LD | BPF_B | BPF_ABS] = true,
990 [BPF_LD | BPF_W | BPF_LEN] = true,
991 [BPF_LD | BPF_W | BPF_IND] = true,
992 [BPF_LD | BPF_H | BPF_IND] = true,
993 [BPF_LD | BPF_B | BPF_IND] = true,
994 [BPF_LD | BPF_IMM] = true,
995 [BPF_LD | BPF_MEM] = true,
996 [BPF_LDX | BPF_W | BPF_LEN] = true,
997 [BPF_LDX | BPF_B | BPF_MSH] = true,
998 [BPF_LDX | BPF_IMM] = true,
999 [BPF_LDX | BPF_MEM] = true,
1000 /* Store instructions */
1001 [BPF_ST] = true,
1002 [BPF_STX] = true,
1003 /* Misc instructions */
1004 [BPF_MISC | BPF_TAX] = true,
1005 [BPF_MISC | BPF_TXA] = true,
1006 /* Return instructions */
1007 [BPF_RET | BPF_K] = true,
1008 [BPF_RET | BPF_A] = true,
1009 /* Jump instructions */
1010 [BPF_JMP | BPF_JA] = true,
1011 [BPF_JMP | BPF_JEQ | BPF_K] = true,
1012 [BPF_JMP | BPF_JEQ | BPF_X] = true,
1013 [BPF_JMP | BPF_JGE | BPF_K] = true,
1014 [BPF_JMP | BPF_JGE | BPF_X] = true,
1015 [BPF_JMP | BPF_JGT | BPF_K] = true,
1016 [BPF_JMP | BPF_JGT | BPF_X] = true,
1017 [BPF_JMP | BPF_JSET | BPF_K] = true,
1018 [BPF_JMP | BPF_JSET | BPF_X] = true,
1019 };
1020
1021 if (code_to_probe >= ARRAY_SIZE(codes))
1022 return false;
1023
1024 return codes[code_to_probe];
1025}
1026
f7bd9e36
DB
1027static bool bpf_check_basics_ok(const struct sock_filter *filter,
1028 unsigned int flen)
1029{
1030 if (filter == NULL)
1031 return false;
1032 if (flen == 0 || flen > BPF_MAXINSNS)
1033 return false;
1034
1035 return true;
1036}
1037
1da177e4 1038/**
4df95ff4 1039 * bpf_check_classic - verify socket filter code
1da177e4
LT
1040 * @filter: filter to verify
1041 * @flen: length of filter
1042 *
1043 * Check the user's filter code. If we let some ugly
1044 * filter code slip through kaboom! The filter must contain
93699863
KK
1045 * no references or jumps that are out of range, no illegal
1046 * instructions, and must end with a RET instruction.
1da177e4 1047 *
7b11f69f
KK
1048 * All jumps are forward as they are not signed.
1049 *
1050 * Returns 0 if the rule set is legal or -EINVAL if not.
1da177e4 1051 */
d9e12f42
NS
1052static int bpf_check_classic(const struct sock_filter *filter,
1053 unsigned int flen)
1da177e4 1054{
aa1113d9 1055 bool anc_found;
34805931 1056 int pc;
1da177e4 1057
34805931 1058 /* Check the filter code now */
1da177e4 1059 for (pc = 0; pc < flen; pc++) {
ec31a05c 1060 const struct sock_filter *ftest = &filter[pc];
93699863 1061
34805931
DB
1062 /* May we actually operate on this code? */
1063 if (!chk_code_allowed(ftest->code))
cba328fc 1064 return -EINVAL;
34805931 1065
93699863 1066 /* Some instructions need special checks */
34805931
DB
1067 switch (ftest->code) {
1068 case BPF_ALU | BPF_DIV | BPF_K:
1069 case BPF_ALU | BPF_MOD | BPF_K:
1070 /* Check for division by zero */
b6069a95
ED
1071 if (ftest->k == 0)
1072 return -EINVAL;
1073 break;
229394e8
RV
1074 case BPF_ALU | BPF_LSH | BPF_K:
1075 case BPF_ALU | BPF_RSH | BPF_K:
1076 if (ftest->k >= 32)
1077 return -EINVAL;
1078 break;
34805931
DB
1079 case BPF_LD | BPF_MEM:
1080 case BPF_LDX | BPF_MEM:
1081 case BPF_ST:
1082 case BPF_STX:
1083 /* Check for invalid memory addresses */
93699863
KK
1084 if (ftest->k >= BPF_MEMWORDS)
1085 return -EINVAL;
1086 break;
34805931
DB
1087 case BPF_JMP | BPF_JA:
1088 /* Note, the large ftest->k might cause loops.
93699863
KK
1089 * Compare this with conditional jumps below,
1090 * where offsets are limited. --ANK (981016)
1091 */
34805931 1092 if (ftest->k >= (unsigned int)(flen - pc - 1))
93699863 1093 return -EINVAL;
01f2f3f6 1094 break;
34805931
DB
1095 case BPF_JMP | BPF_JEQ | BPF_K:
1096 case BPF_JMP | BPF_JEQ | BPF_X:
1097 case BPF_JMP | BPF_JGE | BPF_K:
1098 case BPF_JMP | BPF_JGE | BPF_X:
1099 case BPF_JMP | BPF_JGT | BPF_K:
1100 case BPF_JMP | BPF_JGT | BPF_X:
1101 case BPF_JMP | BPF_JSET | BPF_K:
1102 case BPF_JMP | BPF_JSET | BPF_X:
1103 /* Both conditionals must be safe */
e35bedf3 1104 if (pc + ftest->jt + 1 >= flen ||
93699863
KK
1105 pc + ftest->jf + 1 >= flen)
1106 return -EINVAL;
cba328fc 1107 break;
34805931
DB
1108 case BPF_LD | BPF_W | BPF_ABS:
1109 case BPF_LD | BPF_H | BPF_ABS:
1110 case BPF_LD | BPF_B | BPF_ABS:
aa1113d9 1111 anc_found = false;
34805931
DB
1112 if (bpf_anc_helper(ftest) & BPF_ANC)
1113 anc_found = true;
1114 /* Ancillary operation unknown or unsupported */
aa1113d9
DB
1115 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1116 return -EINVAL;
01f2f3f6
HPP
1117 }
1118 }
93699863 1119
34805931 1120 /* Last instruction must be a RET code */
01f2f3f6 1121 switch (filter[flen - 1].code) {
34805931
DB
1122 case BPF_RET | BPF_K:
1123 case BPF_RET | BPF_A:
2d5311e4 1124 return check_load_and_stores(filter, flen);
cba328fc 1125 }
34805931 1126
cba328fc 1127 return -EINVAL;
1da177e4
LT
1128}
1129
7ae457c1
AS
1130static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1131 const struct sock_fprog *fprog)
a3ea269b 1132{
009937e7 1133 unsigned int fsize = bpf_classic_proglen(fprog);
a3ea269b
DB
1134 struct sock_fprog_kern *fkprog;
1135
1136 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1137 if (!fp->orig_prog)
1138 return -ENOMEM;
1139
1140 fkprog = fp->orig_prog;
1141 fkprog->len = fprog->len;
658da937
DB
1142
1143 fkprog->filter = kmemdup(fp->insns, fsize,
1144 GFP_KERNEL | __GFP_NOWARN);
a3ea269b
DB
1145 if (!fkprog->filter) {
1146 kfree(fp->orig_prog);
1147 return -ENOMEM;
1148 }
1149
1150 return 0;
1151}
1152
7ae457c1 1153static void bpf_release_orig_filter(struct bpf_prog *fp)
a3ea269b
DB
1154{
1155 struct sock_fprog_kern *fprog = fp->orig_prog;
1156
1157 if (fprog) {
1158 kfree(fprog->filter);
1159 kfree(fprog);
1160 }
1161}
1162
7ae457c1
AS
1163static void __bpf_prog_release(struct bpf_prog *prog)
1164{
24701ece 1165 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
89aa0758
AS
1166 bpf_prog_put(prog);
1167 } else {
1168 bpf_release_orig_filter(prog);
1169 bpf_prog_free(prog);
1170 }
7ae457c1
AS
1171}
1172
34c5bd66
PN
1173static void __sk_filter_release(struct sk_filter *fp)
1174{
7ae457c1
AS
1175 __bpf_prog_release(fp->prog);
1176 kfree(fp);
34c5bd66
PN
1177}
1178
47e958ea 1179/**
46bcf14f 1180 * sk_filter_release_rcu - Release a socket filter by rcu_head
47e958ea
PE
1181 * @rcu: rcu_head that contains the sk_filter to free
1182 */
fbc907f0 1183static void sk_filter_release_rcu(struct rcu_head *rcu)
47e958ea
PE
1184{
1185 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1186
34c5bd66 1187 __sk_filter_release(fp);
47e958ea 1188}
fbc907f0
DB
1189
1190/**
1191 * sk_filter_release - release a socket filter
1192 * @fp: filter to remove
1193 *
1194 * Remove a filter from a socket and release its resources.
1195 */
1196static void sk_filter_release(struct sk_filter *fp)
1197{
4c355cdf 1198 if (refcount_dec_and_test(&fp->refcnt))
fbc907f0
DB
1199 call_rcu(&fp->rcu, sk_filter_release_rcu);
1200}
1201
1202void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1203{
7ae457c1 1204 u32 filter_size = bpf_prog_size(fp->prog->len);
fbc907f0 1205
278571ba
AS
1206 atomic_sub(filter_size, &sk->sk_omem_alloc);
1207 sk_filter_release(fp);
fbc907f0 1208}
47e958ea 1209
278571ba
AS
1210/* try to charge the socket memory if there is space available
1211 * return true on success
1212 */
4c355cdf 1213static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
bd4cf0ed 1214{
7ae457c1 1215 u32 filter_size = bpf_prog_size(fp->prog->len);
278571ba
AS
1216
1217 /* same check as in sock_kmalloc() */
1218 if (filter_size <= sysctl_optmem_max &&
1219 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
278571ba
AS
1220 atomic_add(filter_size, &sk->sk_omem_alloc);
1221 return true;
bd4cf0ed 1222 }
278571ba 1223 return false;
bd4cf0ed
AS
1224}
1225
4c355cdf
RE
1226bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1227{
eefca20e
ED
1228 if (!refcount_inc_not_zero(&fp->refcnt))
1229 return false;
1230
1231 if (!__sk_filter_charge(sk, fp)) {
1232 sk_filter_release(fp);
1233 return false;
1234 }
1235 return true;
4c355cdf
RE
1236}
1237
7ae457c1 1238static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
bd4cf0ed
AS
1239{
1240 struct sock_filter *old_prog;
7ae457c1 1241 struct bpf_prog *old_fp;
34805931 1242 int err, new_len, old_len = fp->len;
e0cea7ce 1243 bool seen_ld_abs = false;
bd4cf0ed 1244
06edc59c
CH
1245 /* We are free to overwrite insns et al right here as it won't be used at
1246 * this point in time anymore internally after the migration to the eBPF
1247 * instruction representation.
bd4cf0ed
AS
1248 */
1249 BUILD_BUG_ON(sizeof(struct sock_filter) !=
2695fb55 1250 sizeof(struct bpf_insn));
bd4cf0ed 1251
bd4cf0ed
AS
1252 /* Conversion cannot happen on overlapping memory areas,
1253 * so we need to keep the user BPF around until the 2nd
1254 * pass. At this time, the user BPF is stored in fp->insns.
1255 */
1256 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
658da937 1257 GFP_KERNEL | __GFP_NOWARN);
bd4cf0ed
AS
1258 if (!old_prog) {
1259 err = -ENOMEM;
1260 goto out_err;
1261 }
1262
1263 /* 1st pass: calculate the new program length. */
e0cea7ce
DB
1264 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1265 &seen_ld_abs);
bd4cf0ed
AS
1266 if (err)
1267 goto out_err_free;
1268
1269 /* Expand fp for appending the new filter representation. */
1270 old_fp = fp;
60a3b225 1271 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
bd4cf0ed
AS
1272 if (!fp) {
1273 /* The old_fp is still around in case we couldn't
1274 * allocate new memory, so uncharge on that one.
1275 */
1276 fp = old_fp;
1277 err = -ENOMEM;
1278 goto out_err_free;
1279 }
1280
bd4cf0ed
AS
1281 fp->len = new_len;
1282
2695fb55 1283 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
e0cea7ce
DB
1284 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1285 &seen_ld_abs);
bd4cf0ed 1286 if (err)
8fb575ca 1287 /* 2nd bpf_convert_filter() can fail only if it fails
bd4cf0ed
AS
1288 * to allocate memory, remapping must succeed. Note,
1289 * that at this time old_fp has already been released
278571ba 1290 * by krealloc().
bd4cf0ed
AS
1291 */
1292 goto out_err_free;
1293
d1c55ab5 1294 fp = bpf_prog_select_runtime(fp, &err);
290af866
AS
1295 if (err)
1296 goto out_err_free;
5fe821a9 1297
bd4cf0ed
AS
1298 kfree(old_prog);
1299 return fp;
1300
1301out_err_free:
1302 kfree(old_prog);
1303out_err:
7ae457c1 1304 __bpf_prog_release(fp);
bd4cf0ed
AS
1305 return ERR_PTR(err);
1306}
1307
ac67eb2c
DB
1308static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1309 bpf_aux_classic_check_t trans)
302d6637
JP
1310{
1311 int err;
1312
bd4cf0ed 1313 fp->bpf_func = NULL;
a91263d5 1314 fp->jited = 0;
302d6637 1315
4df95ff4 1316 err = bpf_check_classic(fp->insns, fp->len);
418c96ac 1317 if (err) {
7ae457c1 1318 __bpf_prog_release(fp);
bd4cf0ed 1319 return ERR_PTR(err);
418c96ac 1320 }
302d6637 1321
4ae92bc7
NS
1322 /* There might be additional checks and transformations
1323 * needed on classic filters, f.e. in case of seccomp.
1324 */
1325 if (trans) {
1326 err = trans(fp->insns, fp->len);
1327 if (err) {
1328 __bpf_prog_release(fp);
1329 return ERR_PTR(err);
1330 }
1331 }
1332
bd4cf0ed
AS
1333 /* Probe if we can JIT compile the filter and if so, do
1334 * the compilation of the filter.
1335 */
302d6637 1336 bpf_jit_compile(fp);
bd4cf0ed 1337
06edc59c
CH
1338 /* JIT compiler couldn't process this filter, so do the eBPF translation
1339 * for the optimized interpreter.
bd4cf0ed 1340 */
5fe821a9 1341 if (!fp->jited)
7ae457c1 1342 fp = bpf_migrate_filter(fp);
bd4cf0ed
AS
1343
1344 return fp;
302d6637
JP
1345}
1346
1347/**
7ae457c1 1348 * bpf_prog_create - create an unattached filter
c6c4b97c 1349 * @pfp: the unattached filter that is created
677a9fd3 1350 * @fprog: the filter program
302d6637 1351 *
c6c4b97c 1352 * Create a filter independent of any socket. We first run some
302d6637
JP
1353 * sanity checks on it to make sure it does not explode on us later.
1354 * If an error occurs or there is insufficient memory for the filter
1355 * a negative errno code is returned. On success the return is zero.
1356 */
7ae457c1 1357int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
302d6637 1358{
009937e7 1359 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1 1360 struct bpf_prog *fp;
302d6637
JP
1361
1362 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1363 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
302d6637
JP
1364 return -EINVAL;
1365
60a3b225 1366 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
302d6637
JP
1367 if (!fp)
1368 return -ENOMEM;
a3ea269b 1369
302d6637
JP
1370 memcpy(fp->insns, fprog->filter, fsize);
1371
302d6637 1372 fp->len = fprog->len;
a3ea269b
DB
1373 /* Since unattached filters are not copied back to user
1374 * space through sk_get_filter(), we do not need to hold
1375 * a copy here, and can spare us the work.
1376 */
1377 fp->orig_prog = NULL;
302d6637 1378
7ae457c1 1379 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1380 * memory in case something goes wrong.
1381 */
4ae92bc7 1382 fp = bpf_prepare_filter(fp, NULL);
bd4cf0ed
AS
1383 if (IS_ERR(fp))
1384 return PTR_ERR(fp);
302d6637
JP
1385
1386 *pfp = fp;
1387 return 0;
302d6637 1388}
7ae457c1 1389EXPORT_SYMBOL_GPL(bpf_prog_create);
302d6637 1390
ac67eb2c
DB
1391/**
1392 * bpf_prog_create_from_user - create an unattached filter from user buffer
1393 * @pfp: the unattached filter that is created
1394 * @fprog: the filter program
1395 * @trans: post-classic verifier transformation handler
bab18991 1396 * @save_orig: save classic BPF program
ac67eb2c
DB
1397 *
1398 * This function effectively does the same as bpf_prog_create(), only
1399 * that it builds up its insns buffer from user space provided buffer.
1400 * It also allows for passing a bpf_aux_classic_check_t handler.
1401 */
1402int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
bab18991 1403 bpf_aux_classic_check_t trans, bool save_orig)
ac67eb2c
DB
1404{
1405 unsigned int fsize = bpf_classic_proglen(fprog);
1406 struct bpf_prog *fp;
bab18991 1407 int err;
ac67eb2c
DB
1408
1409 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1410 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
ac67eb2c
DB
1411 return -EINVAL;
1412
1413 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1414 if (!fp)
1415 return -ENOMEM;
1416
1417 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1418 __bpf_prog_free(fp);
1419 return -EFAULT;
1420 }
1421
1422 fp->len = fprog->len;
ac67eb2c
DB
1423 fp->orig_prog = NULL;
1424
bab18991
DB
1425 if (save_orig) {
1426 err = bpf_prog_store_orig_filter(fp, fprog);
1427 if (err) {
1428 __bpf_prog_free(fp);
1429 return -ENOMEM;
1430 }
1431 }
1432
ac67eb2c
DB
1433 /* bpf_prepare_filter() already takes care of freeing
1434 * memory in case something goes wrong.
1435 */
1436 fp = bpf_prepare_filter(fp, trans);
1437 if (IS_ERR(fp))
1438 return PTR_ERR(fp);
1439
1440 *pfp = fp;
1441 return 0;
1442}
2ea273d7 1443EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
ac67eb2c 1444
7ae457c1 1445void bpf_prog_destroy(struct bpf_prog *fp)
302d6637 1446{
7ae457c1 1447 __bpf_prog_release(fp);
302d6637 1448}
7ae457c1 1449EXPORT_SYMBOL_GPL(bpf_prog_destroy);
302d6637 1450
8ced425e 1451static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
49b31e57
DB
1452{
1453 struct sk_filter *fp, *old_fp;
1454
1455 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1456 if (!fp)
1457 return -ENOMEM;
1458
1459 fp->prog = prog;
49b31e57 1460
4c355cdf 1461 if (!__sk_filter_charge(sk, fp)) {
49b31e57
DB
1462 kfree(fp);
1463 return -ENOMEM;
1464 }
4c355cdf 1465 refcount_set(&fp->refcnt, 1);
49b31e57 1466
8ced425e
HFS
1467 old_fp = rcu_dereference_protected(sk->sk_filter,
1468 lockdep_sock_is_held(sk));
49b31e57 1469 rcu_assign_pointer(sk->sk_filter, fp);
8ced425e 1470
49b31e57
DB
1471 if (old_fp)
1472 sk_filter_uncharge(sk, old_fp);
1473
1474 return 0;
1475}
1476
538950a1
CG
1477static
1478struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1da177e4 1479{
009937e7 1480 unsigned int fsize = bpf_classic_proglen(fprog);
7ae457c1 1481 struct bpf_prog *prog;
1da177e4
LT
1482 int err;
1483
d59577b6 1484 if (sock_flag(sk, SOCK_FILTER_LOCKED))
538950a1 1485 return ERR_PTR(-EPERM);
d59577b6 1486
1da177e4 1487 /* Make sure new filter is there and in the right amounts. */
f7bd9e36 1488 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
538950a1 1489 return ERR_PTR(-EINVAL);
1da177e4 1490
f7bd9e36 1491 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
7ae457c1 1492 if (!prog)
538950a1 1493 return ERR_PTR(-ENOMEM);
a3ea269b 1494
7ae457c1 1495 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
c0d1379a 1496 __bpf_prog_free(prog);
538950a1 1497 return ERR_PTR(-EFAULT);
1da177e4
LT
1498 }
1499
7ae457c1 1500 prog->len = fprog->len;
1da177e4 1501
7ae457c1 1502 err = bpf_prog_store_orig_filter(prog, fprog);
a3ea269b 1503 if (err) {
c0d1379a 1504 __bpf_prog_free(prog);
538950a1 1505 return ERR_PTR(-ENOMEM);
a3ea269b
DB
1506 }
1507
7ae457c1 1508 /* bpf_prepare_filter() already takes care of freeing
bd4cf0ed
AS
1509 * memory in case something goes wrong.
1510 */
538950a1
CG
1511 return bpf_prepare_filter(prog, NULL);
1512}
1513
1514/**
1515 * sk_attach_filter - attach a socket filter
1516 * @fprog: the filter program
1517 * @sk: the socket to use
1518 *
1519 * Attach the user's filter code. We first run some sanity checks on
1520 * it to make sure it does not explode on us later. If an error
1521 * occurs or there is insufficient memory for the filter a negative
1522 * errno code is returned. On success the return is zero.
1523 */
8ced425e 1524int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
538950a1
CG
1525{
1526 struct bpf_prog *prog = __get_filter(fprog, sk);
1527 int err;
1528
7ae457c1
AS
1529 if (IS_ERR(prog))
1530 return PTR_ERR(prog);
1531
8ced425e 1532 err = __sk_attach_prog(prog, sk);
49b31e57 1533 if (err < 0) {
7ae457c1 1534 __bpf_prog_release(prog);
49b31e57 1535 return err;
278571ba
AS
1536 }
1537
d3904b73 1538 return 0;
1da177e4 1539}
8ced425e 1540EXPORT_SYMBOL_GPL(sk_attach_filter);
1da177e4 1541
538950a1 1542int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
89aa0758 1543{
538950a1 1544 struct bpf_prog *prog = __get_filter(fprog, sk);
49b31e57 1545 int err;
89aa0758 1546
538950a1
CG
1547 if (IS_ERR(prog))
1548 return PTR_ERR(prog);
1549
8217ca65
MKL
1550 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1551 err = -ENOMEM;
1552 else
1553 err = reuseport_attach_prog(sk, prog);
1554
1555 if (err)
538950a1 1556 __bpf_prog_release(prog);
538950a1 1557
8217ca65 1558 return err;
538950a1
CG
1559}
1560
1561static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1562{
89aa0758 1563 if (sock_flag(sk, SOCK_FILTER_LOCKED))
538950a1 1564 return ERR_PTR(-EPERM);
89aa0758 1565
113214be 1566 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
538950a1
CG
1567}
1568
1569int sk_attach_bpf(u32 ufd, struct sock *sk)
1570{
1571 struct bpf_prog *prog = __get_bpf(ufd, sk);
1572 int err;
1573
1574 if (IS_ERR(prog))
1575 return PTR_ERR(prog);
1576
8ced425e 1577 err = __sk_attach_prog(prog, sk);
49b31e57 1578 if (err < 0) {
89aa0758 1579 bpf_prog_put(prog);
49b31e57 1580 return err;
89aa0758
AS
1581 }
1582
89aa0758
AS
1583 return 0;
1584}
1585
538950a1
CG
1586int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1587{
8217ca65 1588 struct bpf_prog *prog;
538950a1
CG
1589 int err;
1590
8217ca65
MKL
1591 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1592 return -EPERM;
1593
1594 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
45586c70 1595 if (PTR_ERR(prog) == -EINVAL)
8217ca65 1596 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
538950a1
CG
1597 if (IS_ERR(prog))
1598 return PTR_ERR(prog);
1599
8217ca65
MKL
1600 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1601 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1602 * bpf prog (e.g. sockmap). It depends on the
1603 * limitation imposed by bpf_prog_load().
1604 * Hence, sysctl_optmem_max is not checked.
1605 */
1606 if ((sk->sk_type != SOCK_STREAM &&
1607 sk->sk_type != SOCK_DGRAM) ||
1608 (sk->sk_protocol != IPPROTO_UDP &&
1609 sk->sk_protocol != IPPROTO_TCP) ||
1610 (sk->sk_family != AF_INET &&
1611 sk->sk_family != AF_INET6)) {
1612 err = -ENOTSUPP;
1613 goto err_prog_put;
1614 }
1615 } else {
1616 /* BPF_PROG_TYPE_SOCKET_FILTER */
1617 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1618 err = -ENOMEM;
1619 goto err_prog_put;
1620 }
538950a1
CG
1621 }
1622
8217ca65
MKL
1623 err = reuseport_attach_prog(sk, prog);
1624err_prog_put:
1625 if (err)
1626 bpf_prog_put(prog);
1627
1628 return err;
1629}
1630
1631void sk_reuseport_prog_free(struct bpf_prog *prog)
1632{
1633 if (!prog)
1634 return;
1635
1636 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1637 bpf_prog_put(prog);
1638 else
1639 bpf_prog_destroy(prog);
538950a1
CG
1640}
1641
21cafc1d
DB
1642struct bpf_scratchpad {
1643 union {
1644 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1645 u8 buff[MAX_BPF_STACK];
1646 };
1647};
1648
1649static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
91bc4822 1650
5293efe6
DB
1651static inline int __bpf_try_make_writable(struct sk_buff *skb,
1652 unsigned int write_len)
1653{
1654 return skb_ensure_writable(skb, write_len);
1655}
1656
db58ba45
AS
1657static inline int bpf_try_make_writable(struct sk_buff *skb,
1658 unsigned int write_len)
1659{
5293efe6 1660 int err = __bpf_try_make_writable(skb, write_len);
db58ba45 1661
6aaae2b6 1662 bpf_compute_data_pointers(skb);
db58ba45
AS
1663 return err;
1664}
1665
36bbef52
DB
1666static int bpf_try_make_head_writable(struct sk_buff *skb)
1667{
1668 return bpf_try_make_writable(skb, skb_headlen(skb));
1669}
1670
a2bfe6bf
DB
1671static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1672{
1673 if (skb_at_tc_ingress(skb))
1674 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1675}
1676
8065694e
DB
1677static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1678{
1679 if (skb_at_tc_ingress(skb))
1680 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1681}
1682
f3694e00
DB
1683BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1684 const void *, from, u32, len, u64, flags)
608cd71a 1685{
608cd71a
AS
1686 void *ptr;
1687
8afd54c8 1688 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
781c53bc 1689 return -EINVAL;
0ed661d5 1690 if (unlikely(offset > 0xffff))
608cd71a 1691 return -EFAULT;
db58ba45 1692 if (unlikely(bpf_try_make_writable(skb, offset + len)))
608cd71a
AS
1693 return -EFAULT;
1694
0ed661d5 1695 ptr = skb->data + offset;
781c53bc 1696 if (flags & BPF_F_RECOMPUTE_CSUM)
479ffccc 1697 __skb_postpull_rcsum(skb, ptr, len, offset);
608cd71a
AS
1698
1699 memcpy(ptr, from, len);
1700
781c53bc 1701 if (flags & BPF_F_RECOMPUTE_CSUM)
479ffccc 1702 __skb_postpush_rcsum(skb, ptr, len, offset);
8afd54c8
DB
1703 if (flags & BPF_F_INVALIDATE_HASH)
1704 skb_clear_hash(skb);
f8ffad69 1705
608cd71a
AS
1706 return 0;
1707}
1708
577c50aa 1709static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
608cd71a
AS
1710 .func = bpf_skb_store_bytes,
1711 .gpl_only = false,
1712 .ret_type = RET_INTEGER,
1713 .arg1_type = ARG_PTR_TO_CTX,
1714 .arg2_type = ARG_ANYTHING,
216e3cd2 1715 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
39f19ebb 1716 .arg4_type = ARG_CONST_SIZE,
91bc4822
AS
1717 .arg5_type = ARG_ANYTHING,
1718};
1719
f3694e00
DB
1720BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1721 void *, to, u32, len)
05c74e5e 1722{
05c74e5e
DB
1723 void *ptr;
1724
0ed661d5 1725 if (unlikely(offset > 0xffff))
074f528e 1726 goto err_clear;
05c74e5e
DB
1727
1728 ptr = skb_header_pointer(skb, offset, len, to);
1729 if (unlikely(!ptr))
074f528e 1730 goto err_clear;
05c74e5e
DB
1731 if (ptr != to)
1732 memcpy(to, ptr, len);
1733
1734 return 0;
074f528e
DB
1735err_clear:
1736 memset(to, 0, len);
1737 return -EFAULT;
05c74e5e
DB
1738}
1739
577c50aa 1740static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
05c74e5e
DB
1741 .func = bpf_skb_load_bytes,
1742 .gpl_only = false,
1743 .ret_type = RET_INTEGER,
1744 .arg1_type = ARG_PTR_TO_CTX,
1745 .arg2_type = ARG_ANYTHING,
39f19ebb
AS
1746 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1747 .arg4_type = ARG_CONST_SIZE,
05c74e5e
DB
1748};
1749
089b19a9
SF
1750BPF_CALL_4(bpf_flow_dissector_load_bytes,
1751 const struct bpf_flow_dissector *, ctx, u32, offset,
1752 void *, to, u32, len)
1753{
1754 void *ptr;
1755
1756 if (unlikely(offset > 0xffff))
1757 goto err_clear;
1758
1759 if (unlikely(!ctx->skb))
1760 goto err_clear;
1761
1762 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1763 if (unlikely(!ptr))
1764 goto err_clear;
1765 if (ptr != to)
1766 memcpy(to, ptr, len);
1767
1768 return 0;
1769err_clear:
1770 memset(to, 0, len);
1771 return -EFAULT;
1772}
1773
1774static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1775 .func = bpf_flow_dissector_load_bytes,
1776 .gpl_only = false,
1777 .ret_type = RET_INTEGER,
1778 .arg1_type = ARG_PTR_TO_CTX,
1779 .arg2_type = ARG_ANYTHING,
1780 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1781 .arg4_type = ARG_CONST_SIZE,
1782};
1783
4e1ec56c
DB
1784BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1785 u32, offset, void *, to, u32, len, u32, start_header)
1786{
3eee1f75 1787 u8 *end = skb_tail_pointer(skb);
0f5d82f1 1788 u8 *start, *ptr;
4e1ec56c 1789
0f5d82f1 1790 if (unlikely(offset > 0xffff))
4e1ec56c
DB
1791 goto err_clear;
1792
1793 switch (start_header) {
1794 case BPF_HDR_START_MAC:
0f5d82f1
YZ
1795 if (unlikely(!skb_mac_header_was_set(skb)))
1796 goto err_clear;
1797 start = skb_mac_header(skb);
4e1ec56c
DB
1798 break;
1799 case BPF_HDR_START_NET:
0f5d82f1 1800 start = skb_network_header(skb);
4e1ec56c
DB
1801 break;
1802 default:
1803 goto err_clear;
1804 }
1805
0f5d82f1
YZ
1806 ptr = start + offset;
1807
1808 if (likely(ptr + len <= end)) {
4e1ec56c
DB
1809 memcpy(to, ptr, len);
1810 return 0;
1811 }
1812
1813err_clear:
1814 memset(to, 0, len);
1815 return -EFAULT;
1816}
1817
1818static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1819 .func = bpf_skb_load_bytes_relative,
1820 .gpl_only = false,
1821 .ret_type = RET_INTEGER,
1822 .arg1_type = ARG_PTR_TO_CTX,
1823 .arg2_type = ARG_ANYTHING,
1824 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1825 .arg4_type = ARG_CONST_SIZE,
1826 .arg5_type = ARG_ANYTHING,
1827};
1828
36bbef52
DB
1829BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1830{
1831 /* Idea is the following: should the needed direct read/write
1832 * test fail during runtime, we can pull in more data and redo
1833 * again, since implicitly, we invalidate previous checks here.
1834 *
1835 * Or, since we know how much we need to make read/writeable,
1836 * this can be done once at the program beginning for direct
1837 * access case. By this we overcome limitations of only current
1838 * headroom being accessible.
1839 */
1840 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1841}
1842
1843static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1844 .func = bpf_skb_pull_data,
1845 .gpl_only = false,
1846 .ret_type = RET_INTEGER,
1847 .arg1_type = ARG_PTR_TO_CTX,
1848 .arg2_type = ARG_ANYTHING,
1849};
1850
46f8bc92
MKL
1851BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1852{
46f8bc92
MKL
1853 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1854}
1855
1856static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1857 .func = bpf_sk_fullsock,
1858 .gpl_only = false,
1859 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1860 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1861};
1862
0ea488ff
JF
1863static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1864 unsigned int write_len)
1865{
16137b09 1866 return __bpf_try_make_writable(skb, write_len);
0ea488ff
JF
1867}
1868
1869BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1870{
1871 /* Idea is the following: should the needed direct read/write
1872 * test fail during runtime, we can pull in more data and redo
1873 * again, since implicitly, we invalidate previous checks here.
1874 *
1875 * Or, since we know how much we need to make read/writeable,
1876 * this can be done once at the program beginning for direct
1877 * access case. By this we overcome limitations of only current
1878 * headroom being accessible.
1879 */
1880 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1881}
1882
1883static const struct bpf_func_proto sk_skb_pull_data_proto = {
1884 .func = sk_skb_pull_data,
1885 .gpl_only = false,
1886 .ret_type = RET_INTEGER,
1887 .arg1_type = ARG_PTR_TO_CTX,
1888 .arg2_type = ARG_ANYTHING,
1889};
1890
f3694e00
DB
1891BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1892 u64, from, u64, to, u64, flags)
91bc4822 1893{
0ed661d5 1894 __sum16 *ptr;
91bc4822 1895
781c53bc
DB
1896 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1897 return -EINVAL;
0ed661d5 1898 if (unlikely(offset > 0xffff || offset & 1))
91bc4822 1899 return -EFAULT;
0ed661d5 1900 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
91bc4822
AS
1901 return -EFAULT;
1902
0ed661d5 1903 ptr = (__sum16 *)(skb->data + offset);
781c53bc 1904 switch (flags & BPF_F_HDR_FIELD_MASK) {
8050c0f0
DB
1905 case 0:
1906 if (unlikely(from != 0))
1907 return -EINVAL;
1908
1909 csum_replace_by_diff(ptr, to);
1910 break;
91bc4822
AS
1911 case 2:
1912 csum_replace2(ptr, from, to);
1913 break;
1914 case 4:
1915 csum_replace4(ptr, from, to);
1916 break;
1917 default:
1918 return -EINVAL;
1919 }
1920
91bc4822
AS
1921 return 0;
1922}
1923
577c50aa 1924static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
91bc4822
AS
1925 .func = bpf_l3_csum_replace,
1926 .gpl_only = false,
1927 .ret_type = RET_INTEGER,
1928 .arg1_type = ARG_PTR_TO_CTX,
1929 .arg2_type = ARG_ANYTHING,
1930 .arg3_type = ARG_ANYTHING,
1931 .arg4_type = ARG_ANYTHING,
1932 .arg5_type = ARG_ANYTHING,
1933};
1934
f3694e00
DB
1935BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1936 u64, from, u64, to, u64, flags)
91bc4822 1937{
781c53bc 1938 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
2f72959a 1939 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
d1b662ad 1940 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
0ed661d5 1941 __sum16 *ptr;
91bc4822 1942
d1b662ad
DB
1943 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1944 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
781c53bc 1945 return -EINVAL;
0ed661d5 1946 if (unlikely(offset > 0xffff || offset & 1))
91bc4822 1947 return -EFAULT;
0ed661d5 1948 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
91bc4822
AS
1949 return -EFAULT;
1950
0ed661d5 1951 ptr = (__sum16 *)(skb->data + offset);
d1b662ad 1952 if (is_mmzero && !do_mforce && !*ptr)
2f72959a 1953 return 0;
91bc4822 1954
781c53bc 1955 switch (flags & BPF_F_HDR_FIELD_MASK) {
7d672345
DB
1956 case 0:
1957 if (unlikely(from != 0))
1958 return -EINVAL;
1959
1960 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1961 break;
91bc4822
AS
1962 case 2:
1963 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1964 break;
1965 case 4:
1966 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1967 break;
1968 default:
1969 return -EINVAL;
1970 }
1971
2f72959a
DB
1972 if (is_mmzero && !*ptr)
1973 *ptr = CSUM_MANGLED_0;
91bc4822
AS
1974 return 0;
1975}
1976
577c50aa 1977static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
91bc4822
AS
1978 .func = bpf_l4_csum_replace,
1979 .gpl_only = false,
1980 .ret_type = RET_INTEGER,
1981 .arg1_type = ARG_PTR_TO_CTX,
1982 .arg2_type = ARG_ANYTHING,
1983 .arg3_type = ARG_ANYTHING,
1984 .arg4_type = ARG_ANYTHING,
1985 .arg5_type = ARG_ANYTHING,
608cd71a
AS
1986};
1987
f3694e00
DB
1988BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1989 __be32 *, to, u32, to_size, __wsum, seed)
7d672345 1990{
21cafc1d 1991 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
f3694e00 1992 u32 diff_size = from_size + to_size;
7d672345
DB
1993 int i, j = 0;
1994
1995 /* This is quite flexible, some examples:
1996 *
1997 * from_size == 0, to_size > 0, seed := csum --> pushing data
1998 * from_size > 0, to_size == 0, seed := csum --> pulling data
1999 * from_size > 0, to_size > 0, seed := 0 --> diffing data
2000 *
2001 * Even for diffing, from_size and to_size don't need to be equal.
2002 */
2003 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
2004 diff_size > sizeof(sp->diff)))
2005 return -EINVAL;
2006
2007 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
2008 sp->diff[j] = ~from[i];
2009 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
2010 sp->diff[j] = to[i];
2011
2012 return csum_partial(sp->diff, diff_size, seed);
2013}
2014
577c50aa 2015static const struct bpf_func_proto bpf_csum_diff_proto = {
7d672345
DB
2016 .func = bpf_csum_diff,
2017 .gpl_only = false,
36bbef52 2018 .pkt_access = true,
7d672345 2019 .ret_type = RET_INTEGER,
216e3cd2 2020 .arg1_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
39f19ebb 2021 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
216e3cd2 2022 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
39f19ebb 2023 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
7d672345
DB
2024 .arg5_type = ARG_ANYTHING,
2025};
2026
36bbef52
DB
2027BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2028{
2029 /* The interface is to be used in combination with bpf_csum_diff()
2030 * for direct packet writes. csum rotation for alignment as well
2031 * as emulating csum_sub() can be done from the eBPF program.
2032 */
2033 if (skb->ip_summed == CHECKSUM_COMPLETE)
2034 return (skb->csum = csum_add(skb->csum, csum));
2035
2036 return -ENOTSUPP;
2037}
2038
2039static const struct bpf_func_proto bpf_csum_update_proto = {
2040 .func = bpf_csum_update,
2041 .gpl_only = false,
2042 .ret_type = RET_INTEGER,
2043 .arg1_type = ARG_PTR_TO_CTX,
2044 .arg2_type = ARG_ANYTHING,
2045};
2046
7cdec54f
DB
2047BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2048{
2049 /* The interface is to be used in combination with bpf_skb_adjust_room()
2050 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2051 * is passed as flags, for example.
2052 */
2053 switch (level) {
2054 case BPF_CSUM_LEVEL_INC:
2055 __skb_incr_checksum_unnecessary(skb);
2056 break;
2057 case BPF_CSUM_LEVEL_DEC:
2058 __skb_decr_checksum_unnecessary(skb);
2059 break;
2060 case BPF_CSUM_LEVEL_RESET:
2061 __skb_reset_checksum_unnecessary(skb);
2062 break;
2063 case BPF_CSUM_LEVEL_QUERY:
2064 return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2065 skb->csum_level : -EACCES;
2066 default:
2067 return -EINVAL;
2068 }
2069
2070 return 0;
2071}
2072
2073static const struct bpf_func_proto bpf_csum_level_proto = {
2074 .func = bpf_csum_level,
2075 .gpl_only = false,
2076 .ret_type = RET_INTEGER,
2077 .arg1_type = ARG_PTR_TO_CTX,
2078 .arg2_type = ARG_ANYTHING,
2079};
2080
a70b506e
DB
2081static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2082{
5f7d5728 2083 return dev_forward_skb_nomtu(dev, skb);
a70b506e
DB
2084}
2085
4e3264d2
MKL
2086static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2087 struct sk_buff *skb)
2088{
5f7d5728 2089 int ret = ____dev_forward_skb(dev, skb, false);
4e3264d2
MKL
2090
2091 if (likely(!ret)) {
2092 skb->dev = dev;
2093 ret = netif_rx(skb);
2094 }
2095
2096 return ret;
2097}
2098
a70b506e
DB
2099static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2100{
2101 int ret;
2102
97cdcf37 2103 if (dev_xmit_recursion()) {
a70b506e
DB
2104 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2105 kfree_skb(skb);
2106 return -ENETDOWN;
2107 }
2108
2109 skb->dev = dev;
5133498f 2110 skb->tstamp = 0;
a70b506e 2111
97cdcf37 2112 dev_xmit_recursion_inc();
a70b506e 2113 ret = dev_queue_xmit(skb);
97cdcf37 2114 dev_xmit_recursion_dec();
a70b506e
DB
2115
2116 return ret;
2117}
2118
4e3264d2
MKL
2119static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2120 u32 flags)
2121{
e7c87bd6 2122 unsigned int mlen = skb_network_offset(skb);
4e3264d2 2123
e7c87bd6
WB
2124 if (mlen) {
2125 __skb_pull(skb, mlen);
4e3264d2 2126
e7c87bd6
WB
2127 /* At ingress, the mac header has already been pulled once.
2128 * At egress, skb_pospull_rcsum has to be done in case that
2129 * the skb is originated from ingress (i.e. a forwarded skb)
2130 * to ensure that rcsum starts at net header.
2131 */
2132 if (!skb_at_tc_ingress(skb))
2133 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2134 }
4e3264d2
MKL
2135 skb_pop_mac_header(skb);
2136 skb_reset_mac_len(skb);
2137 return flags & BPF_F_INGRESS ?
2138 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2139}
2140
2141static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2142 u32 flags)
2143{
3a0af8fd
TG
2144 /* Verify that a link layer header is carried */
2145 if (unlikely(skb->mac_header >= skb->network_header)) {
2146 kfree_skb(skb);
2147 return -ERANGE;
2148 }
2149
4e3264d2
MKL
2150 bpf_push_mac_rcsum(skb);
2151 return flags & BPF_F_INGRESS ?
2152 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2153}
2154
2155static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2156 u32 flags)
2157{
c491680f 2158 if (dev_is_mac_header_xmit(dev))
4e3264d2 2159 return __bpf_redirect_common(skb, dev, flags);
c491680f
DB
2160 else
2161 return __bpf_redirect_no_mac(skb, dev, flags);
4e3264d2
MKL
2162}
2163
b4ab3141 2164#if IS_ENABLED(CONFIG_IPV6)
ba452c9e
THJ
2165static int bpf_out_neigh_v6(struct net *net, struct sk_buff *skb,
2166 struct net_device *dev, struct bpf_nh_params *nh)
b4ab3141 2167{
b4ab3141
DB
2168 u32 hh_len = LL_RESERVED_SPACE(dev);
2169 const struct in6_addr *nexthop;
ba452c9e 2170 struct dst_entry *dst = NULL;
b4ab3141
DB
2171 struct neighbour *neigh;
2172
2173 if (dev_xmit_recursion()) {
2174 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2175 goto out_drop;
2176 }
2177
2178 skb->dev = dev;
2179 skb->tstamp = 0;
2180
2181 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
a1e975e1
VA
2182 skb = skb_expand_head(skb, hh_len);
2183 if (!skb)
b4ab3141 2184 return -ENOMEM;
b4ab3141
DB
2185 }
2186
2187 rcu_read_lock_bh();
ba452c9e
THJ
2188 if (!nh) {
2189 dst = skb_dst(skb);
2190 nexthop = rt6_nexthop(container_of(dst, struct rt6_info, dst),
2191 &ipv6_hdr(skb)->daddr);
2192 } else {
2193 nexthop = &nh->ipv6_nh;
2194 }
b4ab3141
DB
2195 neigh = ip_neigh_gw6(dev, nexthop);
2196 if (likely(!IS_ERR(neigh))) {
2197 int ret;
2198
2199 sock_confirm_neigh(skb, neigh);
2200 dev_xmit_recursion_inc();
2201 ret = neigh_output(neigh, skb, false);
2202 dev_xmit_recursion_dec();
2203 rcu_read_unlock_bh();
2204 return ret;
2205 }
2206 rcu_read_unlock_bh();
ba452c9e 2207 if (dst)
a1e975e1 2208 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
b4ab3141
DB
2209out_drop:
2210 kfree_skb(skb);
2211 return -ENETDOWN;
2212}
2213
ba452c9e
THJ
2214static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2215 struct bpf_nh_params *nh)
b4ab3141
DB
2216{
2217 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
2218 struct net *net = dev_net(dev);
2219 int err, ret = NET_XMIT_DROP;
b4ab3141 2220
ba452c9e
THJ
2221 if (!nh) {
2222 struct dst_entry *dst;
2223 struct flowi6 fl6 = {
2224 .flowi6_flags = FLOWI_FLAG_ANYSRC,
2225 .flowi6_mark = skb->mark,
2226 .flowlabel = ip6_flowinfo(ip6h),
2227 .flowi6_oif = dev->ifindex,
2228 .flowi6_proto = ip6h->nexthdr,
2229 .daddr = ip6h->daddr,
2230 .saddr = ip6h->saddr,
2231 };
2232
2233 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
2234 if (IS_ERR(dst))
2235 goto out_drop;
b4ab3141 2236
ba452c9e
THJ
2237 skb_dst_set(skb, dst);
2238 } else if (nh->nh_family != AF_INET6) {
2239 goto out_drop;
2240 }
b4ab3141 2241
ba452c9e 2242 err = bpf_out_neigh_v6(net, skb, dev, nh);
b4ab3141
DB
2243 if (unlikely(net_xmit_eval(err)))
2244 dev->stats.tx_errors++;
2245 else
2246 ret = NET_XMIT_SUCCESS;
2247 goto out_xmit;
2248out_drop:
2249 dev->stats.tx_errors++;
2250 kfree_skb(skb);
2251out_xmit:
2252 return ret;
2253}
2254#else
ba452c9e
THJ
2255static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2256 struct bpf_nh_params *nh)
b4ab3141
DB
2257{
2258 kfree_skb(skb);
2259 return NET_XMIT_DROP;
2260}
2261#endif /* CONFIG_IPV6 */
2262
2263#if IS_ENABLED(CONFIG_INET)
ba452c9e
THJ
2264static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
2265 struct net_device *dev, struct bpf_nh_params *nh)
b4ab3141 2266{
b4ab3141
DB
2267 u32 hh_len = LL_RESERVED_SPACE(dev);
2268 struct neighbour *neigh;
2269 bool is_v6gw = false;
2270
2271 if (dev_xmit_recursion()) {
2272 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2273 goto out_drop;
2274 }
2275
2276 skb->dev = dev;
2277 skb->tstamp = 0;
2278
2279 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
a1e975e1
VA
2280 skb = skb_expand_head(skb, hh_len);
2281 if (!skb)
b4ab3141 2282 return -ENOMEM;
b4ab3141
DB
2283 }
2284
2285 rcu_read_lock_bh();
ba452c9e
THJ
2286 if (!nh) {
2287 struct dst_entry *dst = skb_dst(skb);
2288 struct rtable *rt = container_of(dst, struct rtable, dst);
2289
2290 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
2291 } else if (nh->nh_family == AF_INET6) {
2292 neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
2293 is_v6gw = true;
2294 } else if (nh->nh_family == AF_INET) {
2295 neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
2296 } else {
2297 rcu_read_unlock_bh();
2298 goto out_drop;
2299 }
2300
b4ab3141
DB
2301 if (likely(!IS_ERR(neigh))) {
2302 int ret;
2303
2304 sock_confirm_neigh(skb, neigh);
2305 dev_xmit_recursion_inc();
2306 ret = neigh_output(neigh, skb, is_v6gw);
2307 dev_xmit_recursion_dec();
2308 rcu_read_unlock_bh();
2309 return ret;
2310 }
2311 rcu_read_unlock_bh();
2312out_drop:
2313 kfree_skb(skb);
2314 return -ENETDOWN;
2315}
2316
ba452c9e
THJ
2317static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2318 struct bpf_nh_params *nh)
b4ab3141
DB
2319{
2320 const struct iphdr *ip4h = ip_hdr(skb);
2321 struct net *net = dev_net(dev);
2322 int err, ret = NET_XMIT_DROP;
b4ab3141 2323
ba452c9e
THJ
2324 if (!nh) {
2325 struct flowi4 fl4 = {
2326 .flowi4_flags = FLOWI_FLAG_ANYSRC,
2327 .flowi4_mark = skb->mark,
2328 .flowi4_tos = RT_TOS(ip4h->tos),
2329 .flowi4_oif = dev->ifindex,
2330 .flowi4_proto = ip4h->protocol,
2331 .daddr = ip4h->daddr,
2332 .saddr = ip4h->saddr,
2333 };
2334 struct rtable *rt;
2335
2336 rt = ip_route_output_flow(net, &fl4, NULL);
2337 if (IS_ERR(rt))
2338 goto out_drop;
2339 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
2340 ip_rt_put(rt);
2341 goto out_drop;
2342 }
b4ab3141 2343
ba452c9e
THJ
2344 skb_dst_set(skb, &rt->dst);
2345 }
b4ab3141 2346
ba452c9e 2347 err = bpf_out_neigh_v4(net, skb, dev, nh);
b4ab3141
DB
2348 if (unlikely(net_xmit_eval(err)))
2349 dev->stats.tx_errors++;
2350 else
2351 ret = NET_XMIT_SUCCESS;
2352 goto out_xmit;
2353out_drop:
2354 dev->stats.tx_errors++;
2355 kfree_skb(skb);
2356out_xmit:
2357 return ret;
2358}
2359#else
ba452c9e
THJ
2360static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2361 struct bpf_nh_params *nh)
b4ab3141
DB
2362{
2363 kfree_skb(skb);
2364 return NET_XMIT_DROP;
2365}
2366#endif /* CONFIG_INET */
2367
ba452c9e
THJ
2368static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev,
2369 struct bpf_nh_params *nh)
b4ab3141
DB
2370{
2371 struct ethhdr *ethh = eth_hdr(skb);
2372
2373 if (unlikely(skb->mac_header >= skb->network_header))
2374 goto out;
2375 bpf_push_mac_rcsum(skb);
2376 if (is_multicast_ether_addr(ethh->h_dest))
2377 goto out;
2378
2379 skb_pull(skb, sizeof(*ethh));
2380 skb_unset_mac_header(skb);
2381 skb_reset_network_header(skb);
2382
2383 if (skb->protocol == htons(ETH_P_IP))
ba452c9e 2384 return __bpf_redirect_neigh_v4(skb, dev, nh);
b4ab3141 2385 else if (skb->protocol == htons(ETH_P_IPV6))
ba452c9e 2386 return __bpf_redirect_neigh_v6(skb, dev, nh);
b4ab3141
DB
2387out:
2388 kfree_skb(skb);
2389 return -ENOTSUPP;
2390}
2391
2392/* Internal, non-exposed redirect flags. */
2393enum {
9aa1206e
DB
2394 BPF_F_NEIGH = (1ULL << 1),
2395 BPF_F_PEER = (1ULL << 2),
ba452c9e
THJ
2396 BPF_F_NEXTHOP = (1ULL << 3),
2397#define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH | BPF_F_PEER | BPF_F_NEXTHOP)
b4ab3141
DB
2398};
2399
f3694e00 2400BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
3896d655 2401{
3896d655 2402 struct net_device *dev;
36bbef52
DB
2403 struct sk_buff *clone;
2404 int ret;
3896d655 2405
b4ab3141 2406 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
781c53bc
DB
2407 return -EINVAL;
2408
3896d655
AS
2409 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2410 if (unlikely(!dev))
2411 return -EINVAL;
2412
36bbef52
DB
2413 clone = skb_clone(skb, GFP_ATOMIC);
2414 if (unlikely(!clone))
3896d655
AS
2415 return -ENOMEM;
2416
36bbef52
DB
2417 /* For direct write, we need to keep the invariant that the skbs
2418 * we're dealing with need to be uncloned. Should uncloning fail
2419 * here, we need to free the just generated clone to unclone once
2420 * again.
2421 */
2422 ret = bpf_try_make_head_writable(skb);
2423 if (unlikely(ret)) {
2424 kfree_skb(clone);
2425 return -ENOMEM;
2426 }
2427
4e3264d2 2428 return __bpf_redirect(clone, dev, flags);
3896d655
AS
2429}
2430
577c50aa 2431static const struct bpf_func_proto bpf_clone_redirect_proto = {
3896d655
AS
2432 .func = bpf_clone_redirect,
2433 .gpl_only = false,
2434 .ret_type = RET_INTEGER,
2435 .arg1_type = ARG_PTR_TO_CTX,
2436 .arg2_type = ARG_ANYTHING,
2437 .arg3_type = ARG_ANYTHING,
2438};
2439
0b19cc0a
TM
2440DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2441EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
781c53bc 2442
27b29f63
AS
2443int skb_do_redirect(struct sk_buff *skb)
2444{
0b19cc0a 2445 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
9aa1206e 2446 struct net *net = dev_net(skb->dev);
27b29f63 2447 struct net_device *dev;
b4ab3141 2448 u32 flags = ri->flags;
27b29f63 2449
9aa1206e 2450 dev = dev_get_by_index_rcu(net, ri->tgt_index);
4b55cf29 2451 ri->tgt_index = 0;
9aa1206e
DB
2452 ri->flags = 0;
2453 if (unlikely(!dev))
2454 goto out_drop;
2455 if (flags & BPF_F_PEER) {
2456 const struct net_device_ops *ops = dev->netdev_ops;
2457
2458 if (unlikely(!ops->ndo_get_peer_dev ||
2459 !skb_at_tc_ingress(skb)))
2460 goto out_drop;
2461 dev = ops->ndo_get_peer_dev(dev);
2462 if (unlikely(!dev ||
5f7d5728 2463 !(dev->flags & IFF_UP) ||
9aa1206e
DB
2464 net_eq(net, dev_net(dev))))
2465 goto out_drop;
2466 skb->dev = dev;
2467 return -EAGAIN;
27b29f63 2468 }
b4ab3141 2469 return flags & BPF_F_NEIGH ?
ba452c9e
THJ
2470 __bpf_redirect_neigh(skb, dev, flags & BPF_F_NEXTHOP ?
2471 &ri->nh : NULL) :
b4ab3141 2472 __bpf_redirect(skb, dev, flags);
9aa1206e
DB
2473out_drop:
2474 kfree_skb(skb);
2475 return -EINVAL;
b4ab3141
DB
2476}
2477
2478BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2479{
2480 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2481
2482 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2483 return TC_ACT_SHOT;
2484
2485 ri->flags = flags;
2486 ri->tgt_index = ifindex;
2487
2488 return TC_ACT_REDIRECT;
27b29f63
AS
2489}
2490
577c50aa 2491static const struct bpf_func_proto bpf_redirect_proto = {
27b29f63
AS
2492 .func = bpf_redirect,
2493 .gpl_only = false,
2494 .ret_type = RET_INTEGER,
2495 .arg1_type = ARG_ANYTHING,
2496 .arg2_type = ARG_ANYTHING,
2497};
2498
9aa1206e
DB
2499BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags)
2500{
2501 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2502
2503 if (unlikely(flags))
2504 return TC_ACT_SHOT;
2505
2506 ri->flags = BPF_F_PEER;
2507 ri->tgt_index = ifindex;
2508
2509 return TC_ACT_REDIRECT;
2510}
2511
2512static const struct bpf_func_proto bpf_redirect_peer_proto = {
2513 .func = bpf_redirect_peer,
2514 .gpl_only = false,
2515 .ret_type = RET_INTEGER,
2516 .arg1_type = ARG_ANYTHING,
2517 .arg2_type = ARG_ANYTHING,
2518};
2519
ba452c9e
THJ
2520BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params,
2521 int, plen, u64, flags)
b4ab3141
DB
2522{
2523 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2524
ba452c9e 2525 if (unlikely((plen && plen < sizeof(*params)) || flags))
b4ab3141
DB
2526 return TC_ACT_SHOT;
2527
ba452c9e 2528 ri->flags = BPF_F_NEIGH | (plen ? BPF_F_NEXTHOP : 0);
b4ab3141
DB
2529 ri->tgt_index = ifindex;
2530
ba452c9e
THJ
2531 BUILD_BUG_ON(sizeof(struct bpf_redir_neigh) != sizeof(struct bpf_nh_params));
2532 if (plen)
2533 memcpy(&ri->nh, params, sizeof(ri->nh));
2534
b4ab3141
DB
2535 return TC_ACT_REDIRECT;
2536}
2537
2538static const struct bpf_func_proto bpf_redirect_neigh_proto = {
2539 .func = bpf_redirect_neigh,
2540 .gpl_only = false,
2541 .ret_type = RET_INTEGER,
2542 .arg1_type = ARG_ANYTHING,
216e3cd2 2543 .arg2_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
ba452c9e
THJ
2544 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
2545 .arg4_type = ARG_ANYTHING,
b4ab3141
DB
2546};
2547
604326b4 2548BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2a100317
JF
2549{
2550 msg->apply_bytes = bytes;
2551 return 0;
2552}
2553
2554static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2555 .func = bpf_msg_apply_bytes,
2556 .gpl_only = false,
2557 .ret_type = RET_INTEGER,
2558 .arg1_type = ARG_PTR_TO_CTX,
2559 .arg2_type = ARG_ANYTHING,
2560};
2561
604326b4 2562BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
91843d54
JF
2563{
2564 msg->cork_bytes = bytes;
2565 return 0;
2566}
2567
2568static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2569 .func = bpf_msg_cork_bytes,
2570 .gpl_only = false,
2571 .ret_type = RET_INTEGER,
2572 .arg1_type = ARG_PTR_TO_CTX,
2573 .arg2_type = ARG_ANYTHING,
2574};
2575
604326b4
DB
2576BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2577 u32, end, u64, flags)
015632bb 2578{
604326b4
DB
2579 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2580 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2581 struct scatterlist *sge;
2582 u8 *raw, *to, *from;
015632bb
JF
2583 struct page *page;
2584
2585 if (unlikely(flags || end <= start))
2586 return -EINVAL;
2587
2588 /* First find the starting scatterlist element */
604326b4 2589 i = msg->sg.start;
015632bb 2590 do {
6562e29c 2591 offset += len;
604326b4 2592 len = sk_msg_elem(msg, i)->length;
015632bb
JF
2593 if (start < offset + len)
2594 break;
604326b4
DB
2595 sk_msg_iter_var_next(i);
2596 } while (i != msg->sg.end);
015632bb
JF
2597
2598 if (unlikely(start >= offset + len))
2599 return -EINVAL;
2600
604326b4 2601 first_sge = i;
5b24109b
DB
2602 /* The start may point into the sg element so we need to also
2603 * account for the headroom.
2604 */
2605 bytes_sg_total = start - offset + bytes;
163ab96b 2606 if (!test_bit(i, &msg->sg.copy) && bytes_sg_total <= len)
015632bb 2607 goto out;
015632bb
JF
2608
2609 /* At this point we need to linearize multiple scatterlist
2610 * elements or a single shared page. Either way we need to
2611 * copy into a linear buffer exclusively owned by BPF. Then
2612 * place the buffer in the scatterlist and fixup the original
2613 * entries by removing the entries now in the linear buffer
2614 * and shifting the remaining entries. For now we do not try
2615 * to copy partial entries to avoid complexity of running out
2616 * of sg_entry slots. The downside is reading a single byte
2617 * will copy the entire sg entry.
2618 */
2619 do {
604326b4
DB
2620 copy += sk_msg_elem(msg, i)->length;
2621 sk_msg_iter_var_next(i);
5b24109b 2622 if (bytes_sg_total <= copy)
015632bb 2623 break;
604326b4
DB
2624 } while (i != msg->sg.end);
2625 last_sge = i;
015632bb 2626
5b24109b 2627 if (unlikely(bytes_sg_total > copy))
015632bb
JF
2628 return -EINVAL;
2629
4c3d795c
TD
2630 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2631 get_order(copy));
015632bb
JF
2632 if (unlikely(!page))
2633 return -ENOMEM;
015632bb 2634
604326b4
DB
2635 raw = page_address(page);
2636 i = first_sge;
015632bb 2637 do {
604326b4
DB
2638 sge = sk_msg_elem(msg, i);
2639 from = sg_virt(sge);
2640 len = sge->length;
2641 to = raw + poffset;
015632bb
JF
2642
2643 memcpy(to, from, len);
9db39f4d 2644 poffset += len;
604326b4
DB
2645 sge->length = 0;
2646 put_page(sg_page(sge));
015632bb 2647
604326b4
DB
2648 sk_msg_iter_var_next(i);
2649 } while (i != last_sge);
015632bb 2650
604326b4 2651 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
015632bb
JF
2652
2653 /* To repair sg ring we need to shift entries. If we only
2654 * had a single entry though we can just replace it and
2655 * be done. Otherwise walk the ring and shift the entries.
2656 */
604326b4
DB
2657 WARN_ON_ONCE(last_sge == first_sge);
2658 shift = last_sge > first_sge ?
2659 last_sge - first_sge - 1 :
031097d9 2660 NR_MSG_FRAG_IDS - first_sge + last_sge - 1;
015632bb
JF
2661 if (!shift)
2662 goto out;
2663
604326b4
DB
2664 i = first_sge;
2665 sk_msg_iter_var_next(i);
015632bb 2666 do {
604326b4 2667 u32 move_from;
015632bb 2668
031097d9
JK
2669 if (i + shift >= NR_MSG_FRAG_IDS)
2670 move_from = i + shift - NR_MSG_FRAG_IDS;
015632bb
JF
2671 else
2672 move_from = i + shift;
604326b4 2673 if (move_from == msg->sg.end)
015632bb
JF
2674 break;
2675
604326b4
DB
2676 msg->sg.data[i] = msg->sg.data[move_from];
2677 msg->sg.data[move_from].length = 0;
2678 msg->sg.data[move_from].page_link = 0;
2679 msg->sg.data[move_from].offset = 0;
2680 sk_msg_iter_var_next(i);
015632bb 2681 } while (1);
604326b4
DB
2682
2683 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
031097d9 2684 msg->sg.end - shift + NR_MSG_FRAG_IDS :
604326b4 2685 msg->sg.end - shift;
015632bb 2686out:
604326b4 2687 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
015632bb 2688 msg->data_end = msg->data + bytes;
015632bb
JF
2689 return 0;
2690}
2691
2692static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2693 .func = bpf_msg_pull_data,
2694 .gpl_only = false,
2695 .ret_type = RET_INTEGER,
2696 .arg1_type = ARG_PTR_TO_CTX,
2697 .arg2_type = ARG_ANYTHING,
2698 .arg3_type = ARG_ANYTHING,
2699 .arg4_type = ARG_ANYTHING,
2700};
2701
6fff607e
JF
2702BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2703 u32, len, u64, flags)
2704{
2705 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
6562e29c 2706 u32 new, i = 0, l = 0, space, copy = 0, offset = 0;
6fff607e
JF
2707 u8 *raw, *to, *from;
2708 struct page *page;
2709
2710 if (unlikely(flags))
2711 return -EINVAL;
2712
2713 /* First find the starting scatterlist element */
2714 i = msg->sg.start;
2715 do {
6562e29c 2716 offset += l;
6fff607e
JF
2717 l = sk_msg_elem(msg, i)->length;
2718
2719 if (start < offset + l)
2720 break;
6fff607e
JF
2721 sk_msg_iter_var_next(i);
2722 } while (i != msg->sg.end);
2723
2724 if (start >= offset + l)
2725 return -EINVAL;
2726
2727 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2728
2729 /* If no space available will fallback to copy, we need at
2730 * least one scatterlist elem available to push data into
2731 * when start aligns to the beginning of an element or two
2732 * when it falls inside an element. We handle the start equals
2733 * offset case because its the common case for inserting a
2734 * header.
2735 */
2736 if (!space || (space == 1 && start != offset))
2737 copy = msg->sg.data[i].length;
2738
2739 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2740 get_order(copy + len));
2741 if (unlikely(!page))
2742 return -ENOMEM;
2743
2744 if (copy) {
2745 int front, back;
2746
2747 raw = page_address(page);
2748
2749 psge = sk_msg_elem(msg, i);
2750 front = start - offset;
2751 back = psge->length - front;
2752 from = sg_virt(psge);
2753
2754 if (front)
2755 memcpy(raw, from, front);
2756
2757 if (back) {
2758 from += front;
2759 to = raw + front + len;
2760
2761 memcpy(to, from, back);
2762 }
2763
2764 put_page(sg_page(psge));
2765 } else if (start - offset) {
2766 psge = sk_msg_elem(msg, i);
2767 rsge = sk_msg_elem_cpy(msg, i);
2768
2769 psge->length = start - offset;
2770 rsge.length -= psge->length;
2771 rsge.offset += start;
2772
2773 sk_msg_iter_var_next(i);
2774 sg_unmark_end(psge);
cf21e9ba 2775 sg_unmark_end(&rsge);
6fff607e
JF
2776 sk_msg_iter_next(msg, end);
2777 }
2778
2779 /* Slot(s) to place newly allocated data */
2780 new = i;
2781
2782 /* Shift one or two slots as needed */
2783 if (!copy) {
2784 sge = sk_msg_elem_cpy(msg, i);
2785
2786 sk_msg_iter_var_next(i);
2787 sg_unmark_end(&sge);
2788 sk_msg_iter_next(msg, end);
2789
2790 nsge = sk_msg_elem_cpy(msg, i);
2791 if (rsge.length) {
2792 sk_msg_iter_var_next(i);
2793 nnsge = sk_msg_elem_cpy(msg, i);
2794 }
2795
2796 while (i != msg->sg.end) {
2797 msg->sg.data[i] = sge;
2798 sge = nsge;
2799 sk_msg_iter_var_next(i);
2800 if (rsge.length) {
2801 nsge = nnsge;
2802 nnsge = sk_msg_elem_cpy(msg, i);
2803 } else {
2804 nsge = sk_msg_elem_cpy(msg, i);
2805 }
2806 }
2807 }
2808
2809 /* Place newly allocated data buffer */
2810 sk_mem_charge(msg->sk, len);
2811 msg->sg.size += len;
163ab96b 2812 __clear_bit(new, &msg->sg.copy);
6fff607e
JF
2813 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2814 if (rsge.length) {
2815 get_page(sg_page(&rsge));
2816 sk_msg_iter_var_next(new);
2817 msg->sg.data[new] = rsge;
2818 }
2819
2820 sk_msg_compute_data_pointers(msg);
2821 return 0;
2822}
2823
2824static const struct bpf_func_proto bpf_msg_push_data_proto = {
2825 .func = bpf_msg_push_data,
2826 .gpl_only = false,
2827 .ret_type = RET_INTEGER,
2828 .arg1_type = ARG_PTR_TO_CTX,
2829 .arg2_type = ARG_ANYTHING,
2830 .arg3_type = ARG_ANYTHING,
2831 .arg4_type = ARG_ANYTHING,
2832};
2833
7246d8ed
JF
2834static void sk_msg_shift_left(struct sk_msg *msg, int i)
2835{
2836 int prev;
2837
2838 do {
2839 prev = i;
2840 sk_msg_iter_var_next(i);
2841 msg->sg.data[prev] = msg->sg.data[i];
2842 } while (i != msg->sg.end);
2843
2844 sk_msg_iter_prev(msg, end);
2845}
2846
2847static void sk_msg_shift_right(struct sk_msg *msg, int i)
2848{
2849 struct scatterlist tmp, sge;
2850
2851 sk_msg_iter_next(msg, end);
2852 sge = sk_msg_elem_cpy(msg, i);
2853 sk_msg_iter_var_next(i);
2854 tmp = sk_msg_elem_cpy(msg, i);
2855
2856 while (i != msg->sg.end) {
2857 msg->sg.data[i] = sge;
2858 sk_msg_iter_var_next(i);
2859 sge = tmp;
2860 tmp = sk_msg_elem_cpy(msg, i);
2861 }
2862}
2863
2864BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2865 u32, len, u64, flags)
2866{
6562e29c 2867 u32 i = 0, l = 0, space, offset = 0;
7246d8ed
JF
2868 u64 last = start + len;
2869 int pop;
2870
2871 if (unlikely(flags))
2872 return -EINVAL;
2873
2874 /* First find the starting scatterlist element */
2875 i = msg->sg.start;
2876 do {
6562e29c 2877 offset += l;
7246d8ed
JF
2878 l = sk_msg_elem(msg, i)->length;
2879
2880 if (start < offset + l)
2881 break;
7246d8ed
JF
2882 sk_msg_iter_var_next(i);
2883 } while (i != msg->sg.end);
2884
2885 /* Bounds checks: start and pop must be inside message */
2886 if (start >= offset + l || last >= msg->sg.size)
2887 return -EINVAL;
2888
2889 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2890
2891 pop = len;
2892 /* --------------| offset
2893 * -| start |-------- len -------|
2894 *
2895 * |----- a ----|-------- pop -------|----- b ----|
2896 * |______________________________________________| length
2897 *
2898 *
2899 * a: region at front of scatter element to save
2900 * b: region at back of scatter element to save when length > A + pop
2901 * pop: region to pop from element, same as input 'pop' here will be
2902 * decremented below per iteration.
2903 *
2904 * Two top-level cases to handle when start != offset, first B is non
2905 * zero and second B is zero corresponding to when a pop includes more
2906 * than one element.
2907 *
2908 * Then if B is non-zero AND there is no space allocate space and
2909 * compact A, B regions into page. If there is space shift ring to
2910 * the rigth free'ing the next element in ring to place B, leaving
2911 * A untouched except to reduce length.
2912 */
2913 if (start != offset) {
2914 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2915 int a = start;
2916 int b = sge->length - pop - a;
2917
2918 sk_msg_iter_var_next(i);
2919
2920 if (pop < sge->length - a) {
2921 if (space) {
2922 sge->length = a;
2923 sk_msg_shift_right(msg, i);
2924 nsge = sk_msg_elem(msg, i);
2925 get_page(sg_page(sge));
2926 sg_set_page(nsge,
2927 sg_page(sge),
2928 b, sge->offset + pop + a);
2929 } else {
2930 struct page *page, *orig;
2931 u8 *to, *from;
2932
2933 page = alloc_pages(__GFP_NOWARN |
2934 __GFP_COMP | GFP_ATOMIC,
2935 get_order(a + b));
2936 if (unlikely(!page))
2937 return -ENOMEM;
2938
2939 sge->length = a;
2940 orig = sg_page(sge);
2941 from = sg_virt(sge);
2942 to = page_address(page);
2943 memcpy(to, from, a);
2944 memcpy(to + a, from + a + pop, b);
2945 sg_set_page(sge, page, a + b, 0);
2946 put_page(orig);
2947 }
2948 pop = 0;
2949 } else if (pop >= sge->length - a) {
7246d8ed 2950 pop -= (sge->length - a);
3e104c23 2951 sge->length = a;
7246d8ed
JF
2952 }
2953 }
2954
2955 /* From above the current layout _must_ be as follows,
2956 *
2957 * -| offset
2958 * -| start
2959 *
2960 * |---- pop ---|---------------- b ------------|
2961 * |____________________________________________| length
2962 *
2963 * Offset and start of the current msg elem are equal because in the
2964 * previous case we handled offset != start and either consumed the
2965 * entire element and advanced to the next element OR pop == 0.
2966 *
2967 * Two cases to handle here are first pop is less than the length
2968 * leaving some remainder b above. Simply adjust the element's layout
2969 * in this case. Or pop >= length of the element so that b = 0. In this
2970 * case advance to next element decrementing pop.
2971 */
2972 while (pop) {
2973 struct scatterlist *sge = sk_msg_elem(msg, i);
2974
2975 if (pop < sge->length) {
2976 sge->length -= pop;
2977 sge->offset += pop;
2978 pop = 0;
2979 } else {
2980 pop -= sge->length;
2981 sk_msg_shift_left(msg, i);
2982 }
2983 sk_msg_iter_var_next(i);
2984 }
2985
2986 sk_mem_uncharge(msg->sk, len - pop);
2987 msg->sg.size -= (len - pop);
2988 sk_msg_compute_data_pointers(msg);
2989 return 0;
2990}
2991
2992static const struct bpf_func_proto bpf_msg_pop_data_proto = {
2993 .func = bpf_msg_pop_data,
2994 .gpl_only = false,
2995 .ret_type = RET_INTEGER,
2996 .arg1_type = ARG_PTR_TO_CTX,
2997 .arg2_type = ARG_ANYTHING,
2998 .arg3_type = ARG_ANYTHING,
2999 .arg4_type = ARG_ANYTHING,
3000};
3001
5a52ae4e
DB
3002#ifdef CONFIG_CGROUP_NET_CLASSID
3003BPF_CALL_0(bpf_get_cgroup_classid_curr)
3004{
3005 return __task_get_classid(current);
3006}
3007
3008static const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
3009 .func = bpf_get_cgroup_classid_curr,
3010 .gpl_only = false,
3011 .ret_type = RET_INTEGER,
3012};
b426ce83
DB
3013
3014BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb)
3015{
3016 struct sock *sk = skb_to_full_sk(skb);
3017
3018 if (!sk || !sk_fullsock(sk))
3019 return 0;
3020
3021 return sock_cgroup_classid(&sk->sk_cgrp_data);
3022}
3023
3024static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = {
3025 .func = bpf_skb_cgroup_classid,
3026 .gpl_only = false,
3027 .ret_type = RET_INTEGER,
3028 .arg1_type = ARG_PTR_TO_CTX,
3029};
5a52ae4e
DB
3030#endif
3031
f3694e00 3032BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
8d20aabe 3033{
f3694e00 3034 return task_get_classid(skb);
8d20aabe
DB
3035}
3036
3037static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
3038 .func = bpf_get_cgroup_classid,
3039 .gpl_only = false,
3040 .ret_type = RET_INTEGER,
3041 .arg1_type = ARG_PTR_TO_CTX,
3042};
3043
f3694e00 3044BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
c46646d0 3045{
f3694e00 3046 return dst_tclassid(skb);
c46646d0
DB
3047}
3048
3049static const struct bpf_func_proto bpf_get_route_realm_proto = {
3050 .func = bpf_get_route_realm,
3051 .gpl_only = false,
3052 .ret_type = RET_INTEGER,
3053 .arg1_type = ARG_PTR_TO_CTX,
3054};
3055
f3694e00 3056BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
13c5c240
DB
3057{
3058 /* If skb_clear_hash() was called due to mangling, we can
3059 * trigger SW recalculation here. Later access to hash
3060 * can then use the inline skb->hash via context directly
3061 * instead of calling this helper again.
3062 */
f3694e00 3063 return skb_get_hash(skb);
13c5c240
DB
3064}
3065
3066static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
3067 .func = bpf_get_hash_recalc,
3068 .gpl_only = false,
3069 .ret_type = RET_INTEGER,
3070 .arg1_type = ARG_PTR_TO_CTX,
3071};
3072
7a4b28c6
DB
3073BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
3074{
3075 /* After all direct packet write, this can be used once for
3076 * triggering a lazy recalc on next skb_get_hash() invocation.
3077 */
3078 skb_clear_hash(skb);
3079 return 0;
3080}
3081
3082static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
3083 .func = bpf_set_hash_invalid,
3084 .gpl_only = false,
3085 .ret_type = RET_INTEGER,
3086 .arg1_type = ARG_PTR_TO_CTX,
3087};
3088
ded092cd
DB
3089BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
3090{
3091 /* Set user specified hash as L4(+), so that it gets returned
3092 * on skb_get_hash() call unless BPF prog later on triggers a
3093 * skb_clear_hash().
3094 */
3095 __skb_set_sw_hash(skb, hash, true);
3096 return 0;
3097}
3098
3099static const struct bpf_func_proto bpf_set_hash_proto = {
3100 .func = bpf_set_hash,
3101 .gpl_only = false,
3102 .ret_type = RET_INTEGER,
3103 .arg1_type = ARG_PTR_TO_CTX,
3104 .arg2_type = ARG_ANYTHING,
3105};
3106
f3694e00
DB
3107BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
3108 u16, vlan_tci)
4e10df9a 3109{
db58ba45 3110 int ret;
4e10df9a
AS
3111
3112 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
3113 vlan_proto != htons(ETH_P_8021AD)))
3114 vlan_proto = htons(ETH_P_8021Q);
3115
8065694e 3116 bpf_push_mac_rcsum(skb);
db58ba45 3117 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
8065694e
DB
3118 bpf_pull_mac_rcsum(skb);
3119
6aaae2b6 3120 bpf_compute_data_pointers(skb);
db58ba45 3121 return ret;
4e10df9a
AS
3122}
3123
93731ef0 3124static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
4e10df9a
AS
3125 .func = bpf_skb_vlan_push,
3126 .gpl_only = false,
3127 .ret_type = RET_INTEGER,
3128 .arg1_type = ARG_PTR_TO_CTX,
3129 .arg2_type = ARG_ANYTHING,
3130 .arg3_type = ARG_ANYTHING,
3131};
3132
f3694e00 3133BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
4e10df9a 3134{
db58ba45 3135 int ret;
4e10df9a 3136
8065694e 3137 bpf_push_mac_rcsum(skb);
db58ba45 3138 ret = skb_vlan_pop(skb);
8065694e
DB
3139 bpf_pull_mac_rcsum(skb);
3140
6aaae2b6 3141 bpf_compute_data_pointers(skb);
db58ba45 3142 return ret;
4e10df9a
AS
3143}
3144
93731ef0 3145static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
4e10df9a
AS
3146 .func = bpf_skb_vlan_pop,
3147 .gpl_only = false,
3148 .ret_type = RET_INTEGER,
3149 .arg1_type = ARG_PTR_TO_CTX,
3150};
3151
6578171a
DB
3152static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
3153{
3154 /* Caller already did skb_cow() with len as headroom,
3155 * so no need to do it here.
3156 */
3157 skb_push(skb, len);
3158 memmove(skb->data, skb->data + len, off);
3159 memset(skb->data + off, 0, len);
3160
3161 /* No skb_postpush_rcsum(skb, skb->data + off, len)
3162 * needed here as it does not change the skb->csum
3163 * result for checksum complete when summing over
3164 * zeroed blocks.
3165 */
3166 return 0;
3167}
3168
3169static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
3170{
3171 /* skb_ensure_writable() is not needed here, as we're
3172 * already working on an uncloned skb.
3173 */
3174 if (unlikely(!pskb_may_pull(skb, off + len)))
3175 return -ENOMEM;
3176
3177 skb_postpull_rcsum(skb, skb->data + off, len);
3178 memmove(skb->data + len, skb->data, off);
3179 __skb_pull(skb, len);
3180
3181 return 0;
3182}
3183
3184static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
3185{
3186 bool trans_same = skb->transport_header == skb->network_header;
3187 int ret;
3188
3189 /* There's no need for __skb_push()/__skb_pull() pair to
3190 * get to the start of the mac header as we're guaranteed
3191 * to always start from here under eBPF.
3192 */
3193 ret = bpf_skb_generic_push(skb, off, len);
3194 if (likely(!ret)) {
3195 skb->mac_header -= len;
3196 skb->network_header -= len;
3197 if (trans_same)
3198 skb->transport_header = skb->network_header;
3199 }
3200
3201 return ret;
3202}
3203
3204static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
3205{
3206 bool trans_same = skb->transport_header == skb->network_header;
3207 int ret;
3208
3209 /* Same here, __skb_push()/__skb_pull() pair not needed. */
3210 ret = bpf_skb_generic_pop(skb, off, len);
3211 if (likely(!ret)) {
3212 skb->mac_header += len;
3213 skb->network_header += len;
3214 if (trans_same)
3215 skb->transport_header = skb->network_header;
3216 }
3217
3218 return ret;
3219}
3220
3221static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
3222{
3223 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
0daf4349 3224 u32 off = skb_mac_header_len(skb);
6578171a
DB
3225 int ret;
3226
3227 ret = skb_cow(skb, len_diff);
3228 if (unlikely(ret < 0))
3229 return ret;
3230
3231 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3232 if (unlikely(ret < 0))
3233 return ret;
3234
3235 if (skb_is_gso(skb)) {
d02f51cb
DA
3236 struct skb_shared_info *shinfo = skb_shinfo(skb);
3237
0bc919d3 3238 /* SKB_GSO_TCPV4 needs to be changed into SKB_GSO_TCPV6. */
d02f51cb
DA
3239 if (shinfo->gso_type & SKB_GSO_TCPV4) {
3240 shinfo->gso_type &= ~SKB_GSO_TCPV4;
3241 shinfo->gso_type |= SKB_GSO_TCPV6;
6578171a 3242 }
6578171a
DB
3243 }
3244
3245 skb->protocol = htons(ETH_P_IPV6);
3246 skb_clear_hash(skb);
3247
3248 return 0;
3249}
3250
3251static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
3252{
3253 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
0daf4349 3254 u32 off = skb_mac_header_len(skb);
6578171a
DB
3255 int ret;
3256
3257 ret = skb_unclone(skb, GFP_ATOMIC);
3258 if (unlikely(ret < 0))
3259 return ret;
3260
3261 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3262 if (unlikely(ret < 0))
3263 return ret;
3264
3265 if (skb_is_gso(skb)) {
d02f51cb
DA
3266 struct skb_shared_info *shinfo = skb_shinfo(skb);
3267
0bc919d3 3268 /* SKB_GSO_TCPV6 needs to be changed into SKB_GSO_TCPV4. */
d02f51cb
DA
3269 if (shinfo->gso_type & SKB_GSO_TCPV6) {
3270 shinfo->gso_type &= ~SKB_GSO_TCPV6;
3271 shinfo->gso_type |= SKB_GSO_TCPV4;
6578171a 3272 }
6578171a
DB
3273 }
3274
3275 skb->protocol = htons(ETH_P_IP);
3276 skb_clear_hash(skb);
3277
3278 return 0;
3279}
3280
3281static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
3282{
3283 __be16 from_proto = skb->protocol;
3284
3285 if (from_proto == htons(ETH_P_IP) &&
3286 to_proto == htons(ETH_P_IPV6))
3287 return bpf_skb_proto_4_to_6(skb);
3288
3289 if (from_proto == htons(ETH_P_IPV6) &&
3290 to_proto == htons(ETH_P_IP))
3291 return bpf_skb_proto_6_to_4(skb);
3292
3293 return -ENOTSUPP;
3294}
3295
f3694e00
DB
3296BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
3297 u64, flags)
6578171a 3298{
6578171a
DB
3299 int ret;
3300
3301 if (unlikely(flags))
3302 return -EINVAL;
3303
3304 /* General idea is that this helper does the basic groundwork
3305 * needed for changing the protocol, and eBPF program fills the
3306 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
3307 * and other helpers, rather than passing a raw buffer here.
3308 *
3309 * The rationale is to keep this minimal and without a need to
3310 * deal with raw packet data. F.e. even if we would pass buffers
3311 * here, the program still needs to call the bpf_lX_csum_replace()
3312 * helpers anyway. Plus, this way we keep also separation of
3313 * concerns, since f.e. bpf_skb_store_bytes() should only take
3314 * care of stores.
3315 *
3316 * Currently, additional options and extension header space are
3317 * not supported, but flags register is reserved so we can adapt
3318 * that. For offloads, we mark packet as dodgy, so that headers
3319 * need to be verified first.
3320 */
3321 ret = bpf_skb_proto_xlat(skb, proto);
6aaae2b6 3322 bpf_compute_data_pointers(skb);
6578171a
DB
3323 return ret;
3324}
3325
3326static const struct bpf_func_proto bpf_skb_change_proto_proto = {
3327 .func = bpf_skb_change_proto,
3328 .gpl_only = false,
3329 .ret_type = RET_INTEGER,
3330 .arg1_type = ARG_PTR_TO_CTX,
3331 .arg2_type = ARG_ANYTHING,
3332 .arg3_type = ARG_ANYTHING,
3333};
3334
f3694e00 3335BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
d2485c42 3336{
d2485c42 3337 /* We only allow a restricted subset to be changed for now. */
45c7fffa
DB
3338 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
3339 !skb_pkt_type_ok(pkt_type)))
d2485c42
DB
3340 return -EINVAL;
3341
3342 skb->pkt_type = pkt_type;
3343 return 0;
3344}
3345
3346static const struct bpf_func_proto bpf_skb_change_type_proto = {
3347 .func = bpf_skb_change_type,
3348 .gpl_only = false,
3349 .ret_type = RET_INTEGER,
3350 .arg1_type = ARG_PTR_TO_CTX,
3351 .arg2_type = ARG_ANYTHING,
3352};
3353
2be7e212
DB
3354static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
3355{
3356 switch (skb->protocol) {
3357 case htons(ETH_P_IP):
3358 return sizeof(struct iphdr);
3359 case htons(ETH_P_IPV6):
3360 return sizeof(struct ipv6hdr);
3361 default:
3362 return ~0U;
3363 }
3364}
3365
868d5235
WB
3366#define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3367 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3368
3369#define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3370 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3371 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
58dfc900 3372 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
d01b59c9 3373 BPF_F_ADJ_ROOM_ENCAP_L2_ETH | \
58dfc900
AM
3374 BPF_F_ADJ_ROOM_ENCAP_L2( \
3375 BPF_ADJ_ROOM_ENCAP_L2_MASK))
2278f6cc
WB
3376
3377static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3378 u64 flags)
2be7e212 3379{
58dfc900 3380 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
868d5235 3381 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
62b31b42 3382 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
868d5235 3383 unsigned int gso_type = SKB_GSO_DODGY;
2be7e212
DB
3384 int ret;
3385
2278f6cc
WB
3386 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3387 /* udp gso_size delineates datagrams, only allow if fixed */
3388 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3389 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3390 return -ENOTSUPP;
3391 }
d02f51cb 3392
908adce6 3393 ret = skb_cow_head(skb, len_diff);
2be7e212
DB
3394 if (unlikely(ret < 0))
3395 return ret;
3396
868d5235
WB
3397 if (encap) {
3398 if (skb->protocol != htons(ETH_P_IP) &&
3399 skb->protocol != htons(ETH_P_IPV6))
3400 return -ENOTSUPP;
3401
3402 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3403 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3404 return -EINVAL;
3405
3406 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3407 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3408 return -EINVAL;
3409
d01b59c9
XH
3410 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH &&
3411 inner_mac_len < ETH_HLEN)
3412 return -EINVAL;
3413
868d5235
WB
3414 if (skb->encapsulation)
3415 return -EALREADY;
3416
3417 mac_len = skb->network_header - skb->mac_header;
3418 inner_net = skb->network_header;
58dfc900
AM
3419 if (inner_mac_len > len_diff)
3420 return -EINVAL;
868d5235
WB
3421 inner_trans = skb->transport_header;
3422 }
3423
2be7e212
DB
3424 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3425 if (unlikely(ret < 0))
3426 return ret;
3427
868d5235 3428 if (encap) {
58dfc900 3429 skb->inner_mac_header = inner_net - inner_mac_len;
868d5235
WB
3430 skb->inner_network_header = inner_net;
3431 skb->inner_transport_header = inner_trans;
d01b59c9
XH
3432
3433 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH)
3434 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
3435 else
3436 skb_set_inner_protocol(skb, skb->protocol);
868d5235
WB
3437
3438 skb->encapsulation = 1;
3439 skb_set_network_header(skb, mac_len);
3440
3441 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3442 gso_type |= SKB_GSO_UDP_TUNNEL;
3443 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3444 gso_type |= SKB_GSO_GRE;
3445 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3446 gso_type |= SKB_GSO_IPXIP6;
58dfc900 3447 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
868d5235
WB
3448 gso_type |= SKB_GSO_IPXIP4;
3449
3450 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3451 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3452 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3453 sizeof(struct ipv6hdr) :
3454 sizeof(struct iphdr);
3455
3456 skb_set_transport_header(skb, mac_len + nh_len);
3457 }
1b00e0df
WB
3458
3459 /* Match skb->protocol to new outer l3 protocol */
3460 if (skb->protocol == htons(ETH_P_IP) &&
3461 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3462 skb->protocol = htons(ETH_P_IPV6);
3463 else if (skb->protocol == htons(ETH_P_IPV6) &&
3464 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3465 skb->protocol = htons(ETH_P_IP);
868d5235
WB
3466 }
3467
2be7e212 3468 if (skb_is_gso(skb)) {
d02f51cb
DA
3469 struct skb_shared_info *shinfo = skb_shinfo(skb);
3470
2be7e212 3471 /* Due to header grow, MSS needs to be downgraded. */
2278f6cc
WB
3472 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3473 skb_decrease_gso_size(shinfo, len_diff);
3474
2be7e212 3475 /* Header must be checked, and gso_segs recomputed. */
868d5235 3476 shinfo->gso_type |= gso_type;
d02f51cb 3477 shinfo->gso_segs = 0;
2be7e212
DB
3478 }
3479
3480 return 0;
3481}
3482
2278f6cc
WB
3483static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3484 u64 flags)
2be7e212 3485{
2be7e212
DB
3486 int ret;
3487
836e66c2
DB
3488 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3489 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
43537b8e
WB
3490 return -EINVAL;
3491
2278f6cc
WB
3492 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3493 /* udp gso_size delineates datagrams, only allow if fixed */
3494 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3495 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3496 return -ENOTSUPP;
3497 }
d02f51cb 3498
2be7e212
DB
3499 ret = skb_unclone(skb, GFP_ATOMIC);
3500 if (unlikely(ret < 0))
3501 return ret;
3502
3503 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3504 if (unlikely(ret < 0))
3505 return ret;
3506
3507 if (skb_is_gso(skb)) {
d02f51cb
DA
3508 struct skb_shared_info *shinfo = skb_shinfo(skb);
3509
2be7e212 3510 /* Due to header shrink, MSS can be upgraded. */
2278f6cc
WB
3511 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3512 skb_increase_gso_size(shinfo, len_diff);
3513
2be7e212 3514 /* Header must be checked, and gso_segs recomputed. */
d02f51cb
DA
3515 shinfo->gso_type |= SKB_GSO_DODGY;
3516 shinfo->gso_segs = 0;
2be7e212
DB
3517 }
3518
3519 return 0;
3520}
3521
6306c118 3522#define BPF_SKB_MAX_LEN SKB_MAX_ALLOC
2be7e212 3523
18ebe16d
JF
3524BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3525 u32, mode, u64, flags)
3526{
3527 u32 len_diff_abs = abs(len_diff);
3528 bool shrink = len_diff < 0;
3529 int ret = 0;
3530
3531 if (unlikely(flags || mode))
3532 return -EINVAL;
3533 if (unlikely(len_diff_abs > 0xfffU))
3534 return -EFAULT;
3535
3536 if (!shrink) {
3537 ret = skb_cow(skb, len_diff);
3538 if (unlikely(ret < 0))
3539 return ret;
3540 __skb_push(skb, len_diff_abs);
3541 memset(skb->data, 0, len_diff_abs);
3542 } else {
3543 if (unlikely(!pskb_may_pull(skb, len_diff_abs)))
3544 return -ENOMEM;
3545 __skb_pull(skb, len_diff_abs);
3546 }
18ebe16d
JF
3547 if (tls_sw_has_ctx_rx(skb->sk)) {
3548 struct strp_msg *rxm = strp_msg(skb);
3549
3550 rxm->full_len += len_diff;
3551 }
3552 return ret;
3553}
3554
3555static const struct bpf_func_proto sk_skb_adjust_room_proto = {
3556 .func = sk_skb_adjust_room,
3557 .gpl_only = false,
3558 .ret_type = RET_INTEGER,
3559 .arg1_type = ARG_PTR_TO_CTX,
3560 .arg2_type = ARG_ANYTHING,
3561 .arg3_type = ARG_ANYTHING,
3562 .arg4_type = ARG_ANYTHING,
3563};
3564
14aa3192
WB
3565BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3566 u32, mode, u64, flags)
2be7e212 3567{
2be7e212
DB
3568 u32 len_cur, len_diff_abs = abs(len_diff);
3569 u32 len_min = bpf_skb_net_base_len(skb);
6306c118 3570 u32 len_max = BPF_SKB_MAX_LEN;
2be7e212
DB
3571 __be16 proto = skb->protocol;
3572 bool shrink = len_diff < 0;
14aa3192 3573 u32 off;
2be7e212
DB
3574 int ret;
3575
836e66c2
DB
3576 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3577 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
14aa3192 3578 return -EINVAL;
2be7e212
DB
3579 if (unlikely(len_diff_abs > 0xfffU))
3580 return -EFAULT;
3581 if (unlikely(proto != htons(ETH_P_IP) &&
3582 proto != htons(ETH_P_IPV6)))
3583 return -ENOTSUPP;
3584
14aa3192
WB
3585 off = skb_mac_header_len(skb);
3586 switch (mode) {
3587 case BPF_ADJ_ROOM_NET:
3588 off += bpf_skb_net_base_len(skb);
3589 break;
3590 case BPF_ADJ_ROOM_MAC:
3591 break;
3592 default:
3593 return -ENOTSUPP;
3594 }
3595
2be7e212 3596 len_cur = skb->len - skb_network_offset(skb);
2be7e212
DB
3597 if ((shrink && (len_diff_abs >= len_cur ||
3598 len_cur - len_diff_abs < len_min)) ||
3599 (!shrink && (skb->len + len_diff_abs > len_max &&
3600 !skb_is_gso(skb))))
3601 return -ENOTSUPP;
3602
2278f6cc
WB
3603 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3604 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
836e66c2
DB
3605 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3606 __skb_reset_checksum_unnecessary(skb);
2be7e212 3607
6aaae2b6 3608 bpf_compute_data_pointers(skb);
e4a6a342 3609 return ret;
2be7e212
DB
3610}
3611
2be7e212
DB
3612static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3613 .func = bpf_skb_adjust_room,
3614 .gpl_only = false,
3615 .ret_type = RET_INTEGER,
3616 .arg1_type = ARG_PTR_TO_CTX,
3617 .arg2_type = ARG_ANYTHING,
3618 .arg3_type = ARG_ANYTHING,
3619 .arg4_type = ARG_ANYTHING,
3620};
3621
5293efe6
DB
3622static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3623{
3624 u32 min_len = skb_network_offset(skb);
3625
3626 if (skb_transport_header_was_set(skb))
3627 min_len = skb_transport_offset(skb);
3628 if (skb->ip_summed == CHECKSUM_PARTIAL)
3629 min_len = skb_checksum_start_offset(skb) +
3630 skb->csum_offset + sizeof(__sum16);
3631 return min_len;
3632}
3633
5293efe6
DB
3634static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3635{
3636 unsigned int old_len = skb->len;
3637 int ret;
3638
3639 ret = __skb_grow_rcsum(skb, new_len);
3640 if (!ret)
3641 memset(skb->data + old_len, 0, new_len - old_len);
3642 return ret;
3643}
3644
3645static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3646{
3647 return __skb_trim_rcsum(skb, new_len);
3648}
3649
0ea488ff
JF
3650static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3651 u64 flags)
5293efe6 3652{
6306c118 3653 u32 max_len = BPF_SKB_MAX_LEN;
5293efe6 3654 u32 min_len = __bpf_skb_min_len(skb);
5293efe6
DB
3655 int ret;
3656
3657 if (unlikely(flags || new_len > max_len || new_len < min_len))
3658 return -EINVAL;
3659 if (skb->encapsulation)
3660 return -ENOTSUPP;
3661
3662 /* The basic idea of this helper is that it's performing the
3663 * needed work to either grow or trim an skb, and eBPF program
3664 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3665 * bpf_lX_csum_replace() and others rather than passing a raw
3666 * buffer here. This one is a slow path helper and intended
3667 * for replies with control messages.
3668 *
3669 * Like in bpf_skb_change_proto(), we want to keep this rather
3670 * minimal and without protocol specifics so that we are able
3671 * to separate concerns as in bpf_skb_store_bytes() should only
3672 * be the one responsible for writing buffers.
3673 *
3674 * It's really expected to be a slow path operation here for
3675 * control message replies, so we're implicitly linearizing,
3676 * uncloning and drop offloads from the skb by this.
3677 */
3678 ret = __bpf_try_make_writable(skb, skb->len);
3679 if (!ret) {
3680 if (new_len > skb->len)
3681 ret = bpf_skb_grow_rcsum(skb, new_len);
3682 else if (new_len < skb->len)
3683 ret = bpf_skb_trim_rcsum(skb, new_len);
3684 if (!ret && skb_is_gso(skb))
3685 skb_gso_reset(skb);
3686 }
0ea488ff
JF
3687 return ret;
3688}
3689
3690BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3691 u64, flags)
3692{
3693 int ret = __bpf_skb_change_tail(skb, new_len, flags);
5293efe6 3694
6aaae2b6 3695 bpf_compute_data_pointers(skb);
5293efe6
DB
3696 return ret;
3697}
3698
3699static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3700 .func = bpf_skb_change_tail,
3701 .gpl_only = false,
3702 .ret_type = RET_INTEGER,
3703 .arg1_type = ARG_PTR_TO_CTX,
3704 .arg2_type = ARG_ANYTHING,
3705 .arg3_type = ARG_ANYTHING,
3706};
3707
0ea488ff 3708BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3a0af8fd 3709 u64, flags)
0ea488ff 3710{
16137b09 3711 return __bpf_skb_change_tail(skb, new_len, flags);
0ea488ff
JF
3712}
3713
3714static const struct bpf_func_proto sk_skb_change_tail_proto = {
3715 .func = sk_skb_change_tail,
3716 .gpl_only = false,
3717 .ret_type = RET_INTEGER,
3718 .arg1_type = ARG_PTR_TO_CTX,
3719 .arg2_type = ARG_ANYTHING,
3720 .arg3_type = ARG_ANYTHING,
3721};
3722
3723static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3724 u64 flags)
3a0af8fd 3725{
6306c118 3726 u32 max_len = BPF_SKB_MAX_LEN;
3a0af8fd
TG
3727 u32 new_len = skb->len + head_room;
3728 int ret;
3729
3730 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3731 new_len < skb->len))
3732 return -EINVAL;
3733
3734 ret = skb_cow(skb, head_room);
3735 if (likely(!ret)) {
3736 /* Idea for this helper is that we currently only
3737 * allow to expand on mac header. This means that
3738 * skb->protocol network header, etc, stay as is.
3739 * Compared to bpf_skb_change_tail(), we're more
3740 * flexible due to not needing to linearize or
3741 * reset GSO. Intention for this helper is to be
3742 * used by an L3 skb that needs to push mac header
3743 * for redirection into L2 device.
3744 */
3745 __skb_push(skb, head_room);
3746 memset(skb->data, 0, head_room);
3747 skb_reset_mac_header(skb);
84316ca4 3748 skb_reset_mac_len(skb);
3a0af8fd
TG
3749 }
3750
0ea488ff
JF
3751 return ret;
3752}
3753
3754BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3755 u64, flags)
3756{
3757 int ret = __bpf_skb_change_head(skb, head_room, flags);
3758
6aaae2b6 3759 bpf_compute_data_pointers(skb);
0ea488ff 3760 return ret;
3a0af8fd
TG
3761}
3762
3763static const struct bpf_func_proto bpf_skb_change_head_proto = {
3764 .func = bpf_skb_change_head,
3765 .gpl_only = false,
3766 .ret_type = RET_INTEGER,
3767 .arg1_type = ARG_PTR_TO_CTX,
3768 .arg2_type = ARG_ANYTHING,
3769 .arg3_type = ARG_ANYTHING,
3770};
3771
0ea488ff
JF
3772BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3773 u64, flags)
3774{
16137b09 3775 return __bpf_skb_change_head(skb, head_room, flags);
0ea488ff
JF
3776}
3777
3778static const struct bpf_func_proto sk_skb_change_head_proto = {
3779 .func = sk_skb_change_head,
3780 .gpl_only = false,
3781 .ret_type = RET_INTEGER,
3782 .arg1_type = ARG_PTR_TO_CTX,
3783 .arg2_type = ARG_ANYTHING,
3784 .arg3_type = ARG_ANYTHING,
3785};
0165cc81
LB
3786
3787BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp)
3788{
3789 return xdp_get_buff_len(xdp);
3790}
3791
3792static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = {
3793 .func = bpf_xdp_get_buff_len,
3794 .gpl_only = false,
3795 .ret_type = RET_INTEGER,
3796 .arg1_type = ARG_PTR_TO_CTX,
3797};
3798
d9917302
EC
3799BTF_ID_LIST_SINGLE(bpf_xdp_get_buff_len_bpf_ids, struct, xdp_buff)
3800
3801const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto = {
3802 .func = bpf_xdp_get_buff_len,
3803 .gpl_only = false,
3804 .arg1_type = ARG_PTR_TO_BTF_ID,
3805 .arg1_btf_id = &bpf_xdp_get_buff_len_bpf_ids[0],
3806};
3807
de8f3a83
DB
3808static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3809{
3810 return xdp_data_meta_unsupported(xdp) ? 0 :
3811 xdp->data - xdp->data_meta;
3812}
3813
17bedab2
MKL
3814BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3815{
6dfb970d 3816 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
de8f3a83 3817 unsigned long metalen = xdp_get_metalen(xdp);
97e19cce 3818 void *data_start = xdp_frame_end + metalen;
17bedab2
MKL
3819 void *data = xdp->data + offset;
3820
de8f3a83 3821 if (unlikely(data < data_start ||
17bedab2
MKL
3822 data > xdp->data_end - ETH_HLEN))
3823 return -EINVAL;
3824
de8f3a83
DB
3825 if (metalen)
3826 memmove(xdp->data_meta + offset,
3827 xdp->data_meta, metalen);
3828 xdp->data_meta += offset;
17bedab2
MKL
3829 xdp->data = data;
3830
3831 return 0;
3832}
3833
3834static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3835 .func = bpf_xdp_adjust_head,
3836 .gpl_only = false,
3837 .ret_type = RET_INTEGER,
3838 .arg1_type = ARG_PTR_TO_CTX,
3839 .arg2_type = ARG_ANYTHING,
3840};
3841
bf25146a
EC
3842static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
3843{
3844 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
3845 skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags - 1];
3846 struct xdp_rxq_info *rxq = xdp->rxq;
3847 unsigned int tailroom;
3848
3849 if (!rxq->frag_size || rxq->frag_size > xdp->frame_sz)
3850 return -EOPNOTSUPP;
3851
3852 tailroom = rxq->frag_size - skb_frag_size(frag) - skb_frag_off(frag);
3853 if (unlikely(offset > tailroom))
3854 return -EINVAL;
3855
3856 memset(skb_frag_address(frag) + skb_frag_size(frag), 0, offset);
3857 skb_frag_size_add(frag, offset);
3858 sinfo->xdp_frags_size += offset;
3859
3860 return 0;
3861}
3862
3863static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
3864{
3865 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
3866 int i, n_frags_free = 0, len_free = 0;
3867
3868 if (unlikely(offset > (int)xdp_get_buff_len(xdp) - ETH_HLEN))
3869 return -EINVAL;
3870
3871 for (i = sinfo->nr_frags - 1; i >= 0 && offset > 0; i--) {
3872 skb_frag_t *frag = &sinfo->frags[i];
3873 int shrink = min_t(int, offset, skb_frag_size(frag));
3874
3875 len_free += shrink;
3876 offset -= shrink;
3877
3878 if (skb_frag_size(frag) == shrink) {
3879 struct page *page = skb_frag_page(frag);
3880
3881 __xdp_return(page_address(page), &xdp->rxq->mem,
3882 false, NULL);
3883 n_frags_free++;
3884 } else {
3885 skb_frag_size_sub(frag, shrink);
3886 break;
3887 }
3888 }
3889 sinfo->nr_frags -= n_frags_free;
3890 sinfo->xdp_frags_size -= len_free;
3891
3892 if (unlikely(!sinfo->nr_frags)) {
3893 xdp_buff_clear_frags_flag(xdp);
3894 xdp->data_end -= offset;
3895 }
3896
3897 return 0;
3898}
3899
b32cc5b9
NS
3900BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3901{
c8741e2b 3902 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */
b32cc5b9
NS
3903 void *data_end = xdp->data_end + offset;
3904
bf25146a
EC
3905 if (unlikely(xdp_buff_has_frags(xdp))) { /* non-linear xdp buff */
3906 if (offset < 0)
3907 return bpf_xdp_frags_shrink_tail(xdp, -offset);
3908
3909 return bpf_xdp_frags_increase_tail(xdp, offset);
3910 }
3911
c8741e2b
JDB
3912 /* Notice that xdp_data_hard_end have reserved some tailroom */
3913 if (unlikely(data_end > data_hard_end))
b32cc5b9
NS
3914 return -EINVAL;
3915
c8741e2b
JDB
3916 /* ALL drivers MUST init xdp->frame_sz, chicken check below */
3917 if (unlikely(xdp->frame_sz > PAGE_SIZE)) {
3918 WARN_ONCE(1, "Too BIG xdp->frame_sz = %d\n", xdp->frame_sz);
3919 return -EINVAL;
3920 }
3921
b32cc5b9
NS
3922 if (unlikely(data_end < xdp->data + ETH_HLEN))
3923 return -EINVAL;
3924
ddb47d51
JDB
3925 /* Clear memory area on grow, can contain uninit kernel memory */
3926 if (offset > 0)
3927 memset(xdp->data_end, 0, offset);
3928
b32cc5b9
NS
3929 xdp->data_end = data_end;
3930
3931 return 0;
3932}
3933
3934static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3935 .func = bpf_xdp_adjust_tail,
3936 .gpl_only = false,
3937 .ret_type = RET_INTEGER,
3938 .arg1_type = ARG_PTR_TO_CTX,
3939 .arg2_type = ARG_ANYTHING,
3940};
3941
de8f3a83
DB
3942BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3943{
97e19cce 3944 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
de8f3a83
DB
3945 void *meta = xdp->data_meta + offset;
3946 unsigned long metalen = xdp->data - meta;
3947
3948 if (xdp_data_meta_unsupported(xdp))
3949 return -ENOTSUPP;
97e19cce 3950 if (unlikely(meta < xdp_frame_end ||
de8f3a83
DB
3951 meta > xdp->data))
3952 return -EINVAL;
7445cf31 3953 if (unlikely(xdp_metalen_invalid(metalen)))
de8f3a83
DB
3954 return -EACCES;
3955
3956 xdp->data_meta = meta;
3957
3958 return 0;
3959}
3960
3961static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3962 .func = bpf_xdp_adjust_meta,
3963 .gpl_only = false,
3964 .ret_type = RET_INTEGER,
3965 .arg1_type = ARG_PTR_TO_CTX,
3966 .arg2_type = ARG_ANYTHING,
3967};
3968
782347b6
THJ
3969/* XDP_REDIRECT works by a three-step process, implemented in the functions
3970 * below:
3971 *
3972 * 1. The bpf_redirect() and bpf_redirect_map() helpers will lookup the target
3973 * of the redirect and store it (along with some other metadata) in a per-CPU
3974 * struct bpf_redirect_info.
3975 *
3976 * 2. When the program returns the XDP_REDIRECT return code, the driver will
3977 * call xdp_do_redirect() which will use the information in struct
3978 * bpf_redirect_info to actually enqueue the frame into a map type-specific
3979 * bulk queue structure.
3980 *
3981 * 3. Before exiting its NAPI poll loop, the driver will call xdp_do_flush(),
3982 * which will flush all the different bulk queues, thus completing the
3983 * redirect.
3984 *
3985 * Pointers to the map entries will be kept around for this whole sequence of
3986 * steps, protected by RCU. However, there is no top-level rcu_read_lock() in
3987 * the core code; instead, the RCU protection relies on everything happening
3988 * inside a single NAPI poll sequence, which means it's between a pair of calls
3989 * to local_bh_disable()/local_bh_enable().
3990 *
3991 * The map entries are marked as __rcu and the map code makes sure to
3992 * dereference those pointers with rcu_dereference_check() in a way that works
3993 * for both sections that to hold an rcu_read_lock() and sections that are
3994 * called from NAPI without a separate rcu_read_lock(). The code below does not
3995 * use RCU annotations, but relies on those in the map code.
3996 */
1d233886 3997void xdp_do_flush(void)
11393cc9 3998{
1d233886 3999 __dev_flush();
332f22a6
BT
4000 __cpu_map_flush();
4001 __xsk_map_flush();
11393cc9 4002}
1d233886 4003EXPORT_SYMBOL_GPL(xdp_do_flush);
11393cc9 4004
e624d4ed
HL
4005void bpf_clear_redirect_map(struct bpf_map *map)
4006{
4007 struct bpf_redirect_info *ri;
4008 int cpu;
4009
4010 for_each_possible_cpu(cpu) {
4011 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
4012 /* Avoid polluting remote cacheline due to writes if
4013 * not needed. Once we pass this test, we need the
4014 * cmpxchg() to make sure it hasn't been changed in
4015 * the meantime by remote CPU.
4016 */
4017 if (unlikely(READ_ONCE(ri->map) == map))
4018 cmpxchg(&ri->map, map, NULL);
4019 }
4020}
4021
879af96f
JM
4022DEFINE_STATIC_KEY_FALSE(bpf_master_redirect_enabled_key);
4023EXPORT_SYMBOL_GPL(bpf_master_redirect_enabled_key);
4024
4025u32 xdp_master_redirect(struct xdp_buff *xdp)
4026{
4027 struct net_device *master, *slave;
4028 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4029
4030 master = netdev_master_upper_dev_get_rcu(xdp->rxq->dev);
4031 slave = master->netdev_ops->ndo_xdp_get_xmit_slave(master, xdp);
4032 if (slave && slave != xdp->rxq->dev) {
4033 /* The target device is different from the receiving device, so
4034 * redirect it to the new device.
4035 * Using XDP_REDIRECT gets the correct behaviour from XDP enabled
4036 * drivers to unmap the packet from their rx ring.
4037 */
4038 ri->tgt_index = slave->ifindex;
4039 ri->map_id = INT_MAX;
4040 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4041 return XDP_REDIRECT;
4042 }
4043 return XDP_TX;
4044}
4045EXPORT_SYMBOL_GPL(xdp_master_redirect);
4046
1372d34c
THJ
4047static inline int __xdp_do_redirect_xsk(struct bpf_redirect_info *ri,
4048 struct net_device *dev,
4049 struct xdp_buff *xdp,
4050 struct bpf_prog *xdp_prog)
97f91a7c 4051{
ee75aef2 4052 enum bpf_map_type map_type = ri->map_type;
43e74c02 4053 void *fwd = ri->tgt_value;
ee75aef2 4054 u32 map_id = ri->map_id;
4c03bdd7 4055 int err;
97f91a7c 4056
ee75aef2
BT
4057 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4058 ri->map_type = BPF_MAP_TYPE_UNSPEC;
97f91a7c 4059
1372d34c
THJ
4060 err = __xsk_map_redirect(fwd, xdp);
4061 if (unlikely(err))
4062 goto err;
4063
4064 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4065 return 0;
4066err:
4067 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4068 return err;
4069}
4070
4071static __always_inline int __xdp_do_redirect_frame(struct bpf_redirect_info *ri,
4072 struct net_device *dev,
4073 struct xdp_frame *xdpf,
4074 struct bpf_prog *xdp_prog)
4075{
4076 enum bpf_map_type map_type = ri->map_type;
4077 void *fwd = ri->tgt_value;
4078 u32 map_id = ri->map_id;
4079 struct bpf_map *map;
4080 int err;
4081
4082 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4083 ri->map_type = BPF_MAP_TYPE_UNSPEC;
d53ad5d8 4084
d53ad5d8
THJ
4085 if (unlikely(!xdpf)) {
4086 err = -EOVERFLOW;
4087 goto err;
4088 }
4089
ee75aef2
BT
4090 switch (map_type) {
4091 case BPF_MAP_TYPE_DEVMAP:
4092 fallthrough;
4093 case BPF_MAP_TYPE_DEVMAP_HASH:
e624d4ed
HL
4094 map = READ_ONCE(ri->map);
4095 if (unlikely(map)) {
4096 WRITE_ONCE(ri->map, NULL);
d53ad5d8 4097 err = dev_map_enqueue_multi(xdpf, dev, map,
e624d4ed
HL
4098 ri->flags & BPF_F_EXCLUDE_INGRESS);
4099 } else {
d53ad5d8 4100 err = dev_map_enqueue(fwd, xdpf, dev);
e624d4ed 4101 }
ee75aef2
BT
4102 break;
4103 case BPF_MAP_TYPE_CPUMAP:
d53ad5d8 4104 err = cpu_map_enqueue(fwd, xdpf, dev);
ee75aef2
BT
4105 break;
4106 case BPF_MAP_TYPE_UNSPEC:
4107 if (map_id == INT_MAX) {
4108 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4109 if (unlikely(!fwd)) {
4110 err = -EINVAL;
4111 break;
4112 }
d53ad5d8 4113 err = dev_xdp_enqueue(fwd, xdpf, dev);
ee75aef2 4114 break;
1d233886 4115 }
ee75aef2
BT
4116 fallthrough;
4117 default:
4118 err = -EBADRQC;
1d233886
THJ
4119 }
4120
f5836ca5
JDB
4121 if (unlikely(err))
4122 goto err;
4123
ee75aef2 4124 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
f5836ca5
JDB
4125 return 0;
4126err:
ee75aef2 4127 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
97f91a7c
JF
4128 return err;
4129}
1372d34c
THJ
4130
4131int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
4132 struct bpf_prog *xdp_prog)
4133{
4134 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4135 enum bpf_map_type map_type = ri->map_type;
4136
4137 if (map_type == BPF_MAP_TYPE_XSKMAP)
4138 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4139
4140 return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp),
4141 xdp_prog);
4142}
814abfab
JF
4143EXPORT_SYMBOL_GPL(xdp_do_redirect);
4144
1372d34c
THJ
4145int xdp_do_redirect_frame(struct net_device *dev, struct xdp_buff *xdp,
4146 struct xdp_frame *xdpf, struct bpf_prog *xdp_prog)
4147{
4148 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4149 enum bpf_map_type map_type = ri->map_type;
4150
4151 if (map_type == BPF_MAP_TYPE_XSKMAP)
4152 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4153
4154 return __xdp_do_redirect_frame(ri, dev, xdpf, xdp_prog);
4155}
4156EXPORT_SYMBOL_GPL(xdp_do_redirect_frame);
4157
c060bc61
XS
4158static int xdp_do_generic_redirect_map(struct net_device *dev,
4159 struct sk_buff *skb,
02671e23 4160 struct xdp_buff *xdp,
f6069b9a 4161 struct bpf_prog *xdp_prog,
ee75aef2
BT
4162 void *fwd,
4163 enum bpf_map_type map_type, u32 map_id)
6103aa96 4164{
0b19cc0a 4165 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
e624d4ed 4166 struct bpf_map *map;
ee75aef2 4167 int err;
6d5fc195 4168
ee75aef2
BT
4169 switch (map_type) {
4170 case BPF_MAP_TYPE_DEVMAP:
4171 fallthrough;
4172 case BPF_MAP_TYPE_DEVMAP_HASH:
e624d4ed
HL
4173 map = READ_ONCE(ri->map);
4174 if (unlikely(map)) {
4175 WRITE_ONCE(ri->map, NULL);
4176 err = dev_map_redirect_multi(dev, skb, xdp_prog, map,
4177 ri->flags & BPF_F_EXCLUDE_INGRESS);
4178 } else {
4179 err = dev_map_generic_redirect(fwd, skb, xdp_prog);
4180 }
6d5fc195 4181 if (unlikely(err))
9c270af3 4182 goto err;
ee75aef2
BT
4183 break;
4184 case BPF_MAP_TYPE_XSKMAP:
4185 err = xsk_generic_rcv(fwd, xdp);
02671e23
BT
4186 if (err)
4187 goto err;
4188 consume_skb(skb);
ee75aef2 4189 break;
11941f8a
KKD
4190 case BPF_MAP_TYPE_CPUMAP:
4191 err = cpu_map_generic_redirect(fwd, skb);
4192 if (unlikely(err))
4193 goto err;
4194 break;
ee75aef2 4195 default:
9c270af3 4196 err = -EBADRQC;
f5836ca5 4197 goto err;
2facaad6 4198 }
6103aa96 4199
ee75aef2 4200 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
9c270af3
JDB
4201 return 0;
4202err:
ee75aef2 4203 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
9c270af3
JDB
4204 return err;
4205}
4206
4207int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
02671e23 4208 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
9c270af3 4209{
0b19cc0a 4210 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
ee75aef2
BT
4211 enum bpf_map_type map_type = ri->map_type;
4212 void *fwd = ri->tgt_value;
4213 u32 map_id = ri->map_id;
4214 int err;
2facaad6 4215
ee75aef2
BT
4216 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4217 ri->map_type = BPF_MAP_TYPE_UNSPEC;
9c270af3 4218
ee75aef2
BT
4219 if (map_type == BPF_MAP_TYPE_UNSPEC && map_id == INT_MAX) {
4220 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4221 if (unlikely(!fwd)) {
4222 err = -EINVAL;
4223 goto err;
4224 }
4225
4226 err = xdp_ok_fwd_dev(fwd, skb->len);
4227 if (unlikely(err))
4228 goto err;
4229
4230 skb->dev = fwd;
4231 _trace_xdp_redirect(dev, xdp_prog, ri->tgt_index);
4232 generic_xdp_tx(skb, xdp_prog);
4233 return 0;
4234 }
4235
4236 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, fwd, map_type, map_id);
f5836ca5 4237err:
ee75aef2 4238 _trace_xdp_redirect_err(dev, xdp_prog, ri->tgt_index, err);
2facaad6 4239 return err;
6103aa96 4240}
6103aa96 4241
814abfab
JF
4242BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
4243{
0b19cc0a 4244 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
814abfab
JF
4245
4246 if (unlikely(flags))
4247 return XDP_ABORTED;
4248
ee75aef2
BT
4249 /* NB! Map type UNSPEC and map_id == INT_MAX (never generated
4250 * by map_idr) is used for ifindex based XDP redirect.
4251 */
4b55cf29 4252 ri->tgt_index = ifindex;
ee75aef2
BT
4253 ri->map_id = INT_MAX;
4254 ri->map_type = BPF_MAP_TYPE_UNSPEC;
e4a8e817 4255
814abfab
JF
4256 return XDP_REDIRECT;
4257}
4258
4259static const struct bpf_func_proto bpf_xdp_redirect_proto = {
4260 .func = bpf_xdp_redirect,
4261 .gpl_only = false,
4262 .ret_type = RET_INTEGER,
4263 .arg1_type = ARG_ANYTHING,
4264 .arg2_type = ARG_ANYTHING,
4265};
4266
f6069b9a
DB
4267BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
4268 u64, flags)
e4a8e817 4269{
e6a4750f 4270 return map->ops->map_redirect(map, ifindex, flags);
e4a8e817
DB
4271}
4272
4273static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
4274 .func = bpf_xdp_redirect_map,
4275 .gpl_only = false,
4276 .ret_type = RET_INTEGER,
4277 .arg1_type = ARG_CONST_MAP_PTR,
4278 .arg2_type = ARG_ANYTHING,
4279 .arg3_type = ARG_ANYTHING,
4280};
4281
555c8a86 4282static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
aa7145c1 4283 unsigned long off, unsigned long len)
555c8a86 4284{
aa7145c1 4285 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
555c8a86
DB
4286
4287 if (unlikely(!ptr))
4288 return len;
4289 if (ptr != dst_buff)
4290 memcpy(dst_buff, ptr, len);
4291
4292 return 0;
4293}
4294
f3694e00
DB
4295BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
4296 u64, flags, void *, meta, u64, meta_size)
555c8a86 4297{
555c8a86 4298 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
555c8a86
DB
4299
4300 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4301 return -EINVAL;
a7658e1a 4302 if (unlikely(!skb || skb_size > skb->len))
555c8a86
DB
4303 return -EFAULT;
4304
4305 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
4306 bpf_skb_copy);
4307}
4308
4309static const struct bpf_func_proto bpf_skb_event_output_proto = {
4310 .func = bpf_skb_event_output,
4311 .gpl_only = true,
4312 .ret_type = RET_INTEGER,
4313 .arg1_type = ARG_PTR_TO_CTX,
4314 .arg2_type = ARG_CONST_MAP_PTR,
4315 .arg3_type = ARG_ANYTHING,
216e3cd2 4316 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1728a4f2 4317 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
555c8a86
DB
4318};
4319
9436ef6e 4320BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff)
c9a0f3b8 4321
a7658e1a
AS
4322const struct bpf_func_proto bpf_skb_output_proto = {
4323 .func = bpf_skb_event_output,
4324 .gpl_only = true,
4325 .ret_type = RET_INTEGER,
4326 .arg1_type = ARG_PTR_TO_BTF_ID,
9436ef6e 4327 .arg1_btf_id = &bpf_skb_output_btf_ids[0],
a7658e1a
AS
4328 .arg2_type = ARG_CONST_MAP_PTR,
4329 .arg3_type = ARG_ANYTHING,
216e3cd2 4330 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
a7658e1a 4331 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
a7658e1a
AS
4332};
4333
c6c33454
DB
4334static unsigned short bpf_tunnel_key_af(u64 flags)
4335{
4336 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
4337}
4338
f3694e00
DB
4339BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
4340 u32, size, u64, flags)
d3aa45ce 4341{
c6c33454
DB
4342 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4343 u8 compat[sizeof(struct bpf_tunnel_key)];
074f528e
DB
4344 void *to_orig = to;
4345 int err;
d3aa45ce 4346
074f528e
DB
4347 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
4348 err = -EINVAL;
4349 goto err_clear;
4350 }
4351 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
4352 err = -EPROTO;
4353 goto err_clear;
4354 }
c6c33454 4355 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
074f528e 4356 err = -EINVAL;
c6c33454 4357 switch (size) {
4018ab18 4358 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 4359 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4018ab18 4360 goto set_compat;
c6c33454
DB
4361 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4362 /* Fixup deprecated structure layouts here, so we have
4363 * a common path later on.
4364 */
4365 if (ip_tunnel_info_af(info) != AF_INET)
074f528e 4366 goto err_clear;
4018ab18 4367set_compat:
c6c33454
DB
4368 to = (struct bpf_tunnel_key *)compat;
4369 break;
4370 default:
074f528e 4371 goto err_clear;
c6c33454
DB
4372 }
4373 }
d3aa45ce
AS
4374
4375 to->tunnel_id = be64_to_cpu(info->key.tun_id);
c6c33454
DB
4376 to->tunnel_tos = info->key.tos;
4377 to->tunnel_ttl = info->key.ttl;
1fbc2e0c 4378 to->tunnel_ext = 0;
c6c33454 4379
4018ab18 4380 if (flags & BPF_F_TUNINFO_IPV6) {
c6c33454
DB
4381 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
4382 sizeof(to->remote_ipv6));
4018ab18
DB
4383 to->tunnel_label = be32_to_cpu(info->key.label);
4384 } else {
c6c33454 4385 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
1fbc2e0c
DB
4386 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4387 to->tunnel_label = 0;
4018ab18 4388 }
c6c33454
DB
4389
4390 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
074f528e 4391 memcpy(to_orig, to, size);
d3aa45ce
AS
4392
4393 return 0;
074f528e
DB
4394err_clear:
4395 memset(to_orig, 0, size);
4396 return err;
d3aa45ce
AS
4397}
4398
577c50aa 4399static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
d3aa45ce
AS
4400 .func = bpf_skb_get_tunnel_key,
4401 .gpl_only = false,
4402 .ret_type = RET_INTEGER,
4403 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
4404 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4405 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
4406 .arg4_type = ARG_ANYTHING,
4407};
4408
f3694e00 4409BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
14ca0751 4410{
14ca0751 4411 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
074f528e 4412 int err;
14ca0751
DB
4413
4414 if (unlikely(!info ||
074f528e
DB
4415 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
4416 err = -ENOENT;
4417 goto err_clear;
4418 }
4419 if (unlikely(size < info->options_len)) {
4420 err = -ENOMEM;
4421 goto err_clear;
4422 }
14ca0751
DB
4423
4424 ip_tunnel_info_opts_get(to, info);
074f528e
DB
4425 if (size > info->options_len)
4426 memset(to + info->options_len, 0, size - info->options_len);
14ca0751
DB
4427
4428 return info->options_len;
074f528e
DB
4429err_clear:
4430 memset(to, 0, size);
4431 return err;
14ca0751
DB
4432}
4433
4434static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
4435 .func = bpf_skb_get_tunnel_opt,
4436 .gpl_only = false,
4437 .ret_type = RET_INTEGER,
4438 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
4439 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4440 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
4441};
4442
d3aa45ce
AS
4443static struct metadata_dst __percpu *md_dst;
4444
f3694e00
DB
4445BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
4446 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
d3aa45ce 4447{
d3aa45ce 4448 struct metadata_dst *md = this_cpu_ptr(md_dst);
c6c33454 4449 u8 compat[sizeof(struct bpf_tunnel_key)];
d3aa45ce
AS
4450 struct ip_tunnel_info *info;
4451
22080870 4452 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
77a5196a 4453 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
d3aa45ce 4454 return -EINVAL;
c6c33454
DB
4455 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4456 switch (size) {
4018ab18 4457 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 4458 case offsetof(struct bpf_tunnel_key, tunnel_ext):
c6c33454
DB
4459 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4460 /* Fixup deprecated structure layouts here, so we have
4461 * a common path later on.
4462 */
4463 memcpy(compat, from, size);
4464 memset(compat + size, 0, sizeof(compat) - size);
f3694e00 4465 from = (const struct bpf_tunnel_key *) compat;
c6c33454
DB
4466 break;
4467 default:
4468 return -EINVAL;
4469 }
4470 }
c0e760c9
DB
4471 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
4472 from->tunnel_ext))
4018ab18 4473 return -EINVAL;
d3aa45ce
AS
4474
4475 skb_dst_drop(skb);
4476 dst_hold((struct dst_entry *) md);
4477 skb_dst_set(skb, (struct dst_entry *) md);
4478
4479 info = &md->u.tun_info;
5540fbf4 4480 memset(info, 0, sizeof(*info));
d3aa45ce 4481 info->mode = IP_TUNNEL_INFO_TX;
c6c33454 4482
db3c6139 4483 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
22080870
DB
4484 if (flags & BPF_F_DONT_FRAGMENT)
4485 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
792f3dd6
WT
4486 if (flags & BPF_F_ZERO_CSUM_TX)
4487 info->key.tun_flags &= ~TUNNEL_CSUM;
77a5196a
WT
4488 if (flags & BPF_F_SEQ_NUMBER)
4489 info->key.tun_flags |= TUNNEL_SEQ;
22080870 4490
d3aa45ce 4491 info->key.tun_id = cpu_to_be64(from->tunnel_id);
c6c33454
DB
4492 info->key.tos = from->tunnel_tos;
4493 info->key.ttl = from->tunnel_ttl;
4494
4495 if (flags & BPF_F_TUNINFO_IPV6) {
4496 info->mode |= IP_TUNNEL_INFO_IPV6;
4497 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
4498 sizeof(from->remote_ipv6));
4018ab18
DB
4499 info->key.label = cpu_to_be32(from->tunnel_label) &
4500 IPV6_FLOWLABEL_MASK;
c6c33454
DB
4501 } else {
4502 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
4503 }
d3aa45ce
AS
4504
4505 return 0;
4506}
4507
577c50aa 4508static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
d3aa45ce
AS
4509 .func = bpf_skb_set_tunnel_key,
4510 .gpl_only = false,
4511 .ret_type = RET_INTEGER,
4512 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 4513 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
39f19ebb 4514 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
4515 .arg4_type = ARG_ANYTHING,
4516};
4517
f3694e00
DB
4518BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
4519 const u8 *, from, u32, size)
14ca0751 4520{
14ca0751
DB
4521 struct ip_tunnel_info *info = skb_tunnel_info(skb);
4522 const struct metadata_dst *md = this_cpu_ptr(md_dst);
4523
4524 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
4525 return -EINVAL;
fca5fdf6 4526 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
14ca0751
DB
4527 return -ENOMEM;
4528
256c87c1 4529 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
14ca0751
DB
4530
4531 return 0;
4532}
4533
4534static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
4535 .func = bpf_skb_set_tunnel_opt,
4536 .gpl_only = false,
4537 .ret_type = RET_INTEGER,
4538 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 4539 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
39f19ebb 4540 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
4541};
4542
4543static const struct bpf_func_proto *
4544bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
d3aa45ce
AS
4545{
4546 if (!md_dst) {
d66f2b91
JK
4547 struct metadata_dst __percpu *tmp;
4548
4549 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4550 METADATA_IP_TUNNEL,
4551 GFP_KERNEL);
4552 if (!tmp)
d3aa45ce 4553 return NULL;
d66f2b91
JK
4554 if (cmpxchg(&md_dst, NULL, tmp))
4555 metadata_dst_free_percpu(tmp);
d3aa45ce 4556 }
14ca0751
DB
4557
4558 switch (which) {
4559 case BPF_FUNC_skb_set_tunnel_key:
4560 return &bpf_skb_set_tunnel_key_proto;
4561 case BPF_FUNC_skb_set_tunnel_opt:
4562 return &bpf_skb_set_tunnel_opt_proto;
4563 default:
4564 return NULL;
4565 }
d3aa45ce
AS
4566}
4567
f3694e00
DB
4568BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4569 u32, idx)
4a482f34 4570{
4a482f34
MKL
4571 struct bpf_array *array = container_of(map, struct bpf_array, map);
4572 struct cgroup *cgrp;
4573 struct sock *sk;
4a482f34 4574
2d48c5f9 4575 sk = skb_to_full_sk(skb);
4a482f34
MKL
4576 if (!sk || !sk_fullsock(sk))
4577 return -ENOENT;
f3694e00 4578 if (unlikely(idx >= array->map.max_entries))
4a482f34
MKL
4579 return -E2BIG;
4580
f3694e00 4581 cgrp = READ_ONCE(array->ptrs[idx]);
4a482f34
MKL
4582 if (unlikely(!cgrp))
4583 return -EAGAIN;
4584
54fd9c2d 4585 return sk_under_cgroup_hierarchy(sk, cgrp);
4a482f34
MKL
4586}
4587
747ea55e
DB
4588static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4589 .func = bpf_skb_under_cgroup,
4a482f34
MKL
4590 .gpl_only = false,
4591 .ret_type = RET_INTEGER,
4592 .arg1_type = ARG_PTR_TO_CTX,
4593 .arg2_type = ARG_CONST_MAP_PTR,
4594 .arg3_type = ARG_ANYTHING,
4595};
4a482f34 4596
cb20b08e 4597#ifdef CONFIG_SOCK_CGROUP_DATA
f307fa2c
AI
4598static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
4599{
4600 struct cgroup *cgrp;
4601
a5fa25ad
MKL
4602 sk = sk_to_full_sk(sk);
4603 if (!sk || !sk_fullsock(sk))
4604 return 0;
4605
f307fa2c
AI
4606 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4607 return cgroup_id(cgrp);
4608}
4609
cb20b08e
DB
4610BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4611{
a5fa25ad 4612 return __bpf_sk_cgroup_id(skb->sk);
cb20b08e
DB
4613}
4614
4615static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4616 .func = bpf_skb_cgroup_id,
4617 .gpl_only = false,
4618 .ret_type = RET_INTEGER,
4619 .arg1_type = ARG_PTR_TO_CTX,
4620};
77236281 4621
f307fa2c
AI
4622static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
4623 int ancestor_level)
77236281 4624{
77236281
AI
4625 struct cgroup *ancestor;
4626 struct cgroup *cgrp;
4627
a5fa25ad
MKL
4628 sk = sk_to_full_sk(sk);
4629 if (!sk || !sk_fullsock(sk))
4630 return 0;
4631
77236281
AI
4632 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4633 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4634 if (!ancestor)
4635 return 0;
4636
74321038 4637 return cgroup_id(ancestor);
77236281
AI
4638}
4639
f307fa2c
AI
4640BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4641 ancestor_level)
4642{
a5fa25ad 4643 return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level);
f307fa2c
AI
4644}
4645
77236281
AI
4646static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4647 .func = bpf_skb_ancestor_cgroup_id,
4648 .gpl_only = false,
4649 .ret_type = RET_INTEGER,
4650 .arg1_type = ARG_PTR_TO_CTX,
4651 .arg2_type = ARG_ANYTHING,
4652};
f307fa2c
AI
4653
4654BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk)
4655{
4656 return __bpf_sk_cgroup_id(sk);
4657}
4658
4659static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
4660 .func = bpf_sk_cgroup_id,
4661 .gpl_only = false,
4662 .ret_type = RET_INTEGER,
a5fa25ad 4663 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
f307fa2c
AI
4664};
4665
4666BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
4667{
4668 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4669}
4670
4671static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
4672 .func = bpf_sk_ancestor_cgroup_id,
4673 .gpl_only = false,
4674 .ret_type = RET_INTEGER,
a5fa25ad 4675 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
f307fa2c
AI
4676 .arg2_type = ARG_ANYTHING,
4677};
cb20b08e
DB
4678#endif
4679
d9917302 4680static unsigned long bpf_xdp_copy(void *dst_buff, const void *ctx,
4de16969
DB
4681 unsigned long off, unsigned long len)
4682{
d9917302
EC
4683 struct xdp_buff *xdp = (struct xdp_buff *)ctx;
4684 unsigned long ptr_len, ptr_off = 0;
4685 skb_frag_t *next_frag, *end_frag;
4686 struct skb_shared_info *sinfo;
4687 u8 *ptr_buf;
4688
4689 if (likely(xdp->data_end - xdp->data >= off + len)) {
4690 memcpy(dst_buff, xdp->data + off, len);
4691 return 0;
4692 }
4693
4694 sinfo = xdp_get_shared_info_from_buff(xdp);
4695 end_frag = &sinfo->frags[sinfo->nr_frags];
4696 next_frag = &sinfo->frags[0];
4697
4698 ptr_len = xdp->data_end - xdp->data;
4699 ptr_buf = xdp->data;
4700
4701 while (true) {
4702 if (off < ptr_off + ptr_len) {
4703 unsigned long copy_off = off - ptr_off;
4704 unsigned long copy_len = min(len, ptr_len - copy_off);
4705
4706 memcpy(dst_buff, ptr_buf + copy_off, copy_len);
4707
4708 off += copy_len;
4709 len -= copy_len;
4710 dst_buff += copy_len;
4711 }
4712
4713 if (!len || next_frag == end_frag)
4714 break;
4715
4716 ptr_off += ptr_len;
4717 ptr_buf = skb_frag_address(next_frag);
4718 ptr_len = skb_frag_size(next_frag);
4719 next_frag++;
4720 }
4721
4de16969
DB
4722 return 0;
4723}
4724
f3694e00
DB
4725BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4726 u64, flags, void *, meta, u64, meta_size)
4de16969 4727{
4de16969 4728 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4de16969
DB
4729
4730 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4731 return -EINVAL;
d9917302
EC
4732
4733 if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp)))
4de16969
DB
4734 return -EFAULT;
4735
d9917302 4736 return bpf_event_output(map, flags, meta, meta_size, xdp,
9c471370 4737 xdp_size, bpf_xdp_copy);
4de16969
DB
4738}
4739
4740static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4741 .func = bpf_xdp_event_output,
4742 .gpl_only = true,
4743 .ret_type = RET_INTEGER,
4744 .arg1_type = ARG_PTR_TO_CTX,
4745 .arg2_type = ARG_CONST_MAP_PTR,
4746 .arg3_type = ARG_ANYTHING,
216e3cd2 4747 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1728a4f2 4748 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4de16969
DB
4749};
4750
9436ef6e 4751BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff)
c9a0f3b8 4752
d831ee84
EC
4753const struct bpf_func_proto bpf_xdp_output_proto = {
4754 .func = bpf_xdp_event_output,
4755 .gpl_only = true,
4756 .ret_type = RET_INTEGER,
4757 .arg1_type = ARG_PTR_TO_BTF_ID,
9436ef6e 4758 .arg1_btf_id = &bpf_xdp_output_btf_ids[0],
d831ee84
EC
4759 .arg2_type = ARG_CONST_MAP_PTR,
4760 .arg3_type = ARG_ANYTHING,
216e3cd2 4761 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
d831ee84 4762 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
d831ee84
EC
4763};
4764
91b8270f
CF
4765BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4766{
92acdc58 4767 return skb->sk ? __sock_gen_cookie(skb->sk) : 0;
91b8270f
CF
4768}
4769
4770static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4771 .func = bpf_get_socket_cookie,
4772 .gpl_only = false,
4773 .ret_type = RET_INTEGER,
4774 .arg1_type = ARG_PTR_TO_CTX,
4775};
4776
d692f113
AI
4777BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4778{
92acdc58 4779 return __sock_gen_cookie(ctx->sk);
d692f113
AI
4780}
4781
4782static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4783 .func = bpf_get_socket_cookie_sock_addr,
4784 .gpl_only = false,
4785 .ret_type = RET_INTEGER,
4786 .arg1_type = ARG_PTR_TO_CTX,
4787};
4788
0e53d9e5
DB
4789BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx)
4790{
92acdc58 4791 return __sock_gen_cookie(ctx);
0e53d9e5
DB
4792}
4793
4794static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
4795 .func = bpf_get_socket_cookie_sock,
4796 .gpl_only = false,
4797 .ret_type = RET_INTEGER,
4798 .arg1_type = ARG_PTR_TO_CTX,
4799};
4800
c5dbb89f
FR
4801BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk)
4802{
4803 return sk ? sock_gen_cookie(sk) : 0;
4804}
4805
4806const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
4807 .func = bpf_get_socket_ptr_cookie,
4808 .gpl_only = false,
4809 .ret_type = RET_INTEGER,
4810 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4811};
4812
d692f113
AI
4813BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4814{
92acdc58 4815 return __sock_gen_cookie(ctx->sk);
d692f113
AI
4816}
4817
4818static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4819 .func = bpf_get_socket_cookie_sock_ops,
4820 .gpl_only = false,
4821 .ret_type = RET_INTEGER,
4822 .arg1_type = ARG_PTR_TO_CTX,
4823};
4824
f318903c
DB
4825static u64 __bpf_get_netns_cookie(struct sock *sk)
4826{
3d368ab8
ED
4827 const struct net *net = sk ? sock_net(sk) : &init_net;
4828
4829 return net->net_cookie;
f318903c
DB
4830}
4831
4832BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
4833{
4834 return __bpf_get_netns_cookie(ctx);
4835}
4836
4837static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = {
4838 .func = bpf_get_netns_cookie_sock,
4839 .gpl_only = false,
4840 .ret_type = RET_INTEGER,
4841 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4842};
4843
4844BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4845{
4846 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4847}
4848
4849static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = {
4850 .func = bpf_get_netns_cookie_sock_addr,
4851 .gpl_only = false,
4852 .ret_type = RET_INTEGER,
4853 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4854};
4855
6cf1770d
XL
4856BPF_CALL_1(bpf_get_netns_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4857{
4858 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4859}
4860
4861static const struct bpf_func_proto bpf_get_netns_cookie_sock_ops_proto = {
4862 .func = bpf_get_netns_cookie_sock_ops,
4863 .gpl_only = false,
4864 .ret_type = RET_INTEGER,
4865 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4866};
4867
fab60e29
XL
4868BPF_CALL_1(bpf_get_netns_cookie_sk_msg, struct sk_msg *, ctx)
4869{
4870 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4871}
4872
4873static const struct bpf_func_proto bpf_get_netns_cookie_sk_msg_proto = {
4874 .func = bpf_get_netns_cookie_sk_msg,
4875 .gpl_only = false,
4876 .ret_type = RET_INTEGER,
4877 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4878};
4879
6acc5c29
CF
4880BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
4881{
4882 struct sock *sk = sk_to_full_sk(skb->sk);
4883 kuid_t kuid;
4884
4885 if (!sk || !sk_fullsock(sk))
4886 return overflowuid;
4887 kuid = sock_net_uid(sock_net(sk), sk);
4888 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
4889}
4890
4891static const struct bpf_func_proto bpf_get_socket_uid_proto = {
4892 .func = bpf_get_socket_uid,
4893 .gpl_only = false,
4894 .ret_type = RET_INTEGER,
4895 .arg1_type = ARG_PTR_TO_CTX,
4896};
4897
beecf11b 4898static int _bpf_setsockopt(struct sock *sk, int level, int optname,
5cdc744c 4899 char *optval, int optlen)
8c4b4c7e 4900{
70c58997 4901 char devname[IFNAMSIZ];
f9bcf968 4902 int val, valbool;
70c58997
FF
4903 struct net *net;
4904 int ifindex;
8c4b4c7e 4905 int ret = 0;
8c4b4c7e
LB
4906
4907 if (!sk_fullsock(sk))
4908 return -EINVAL;
4909
beecf11b
SF
4910 sock_owned_by_me(sk);
4911
8c4b4c7e 4912 if (level == SOL_SOCKET) {
70c58997 4913 if (optlen != sizeof(int) && optname != SO_BINDTODEVICE)
8c4b4c7e
LB
4914 return -EINVAL;
4915 val = *((int *)optval);
f9bcf968 4916 valbool = val ? 1 : 0;
8c4b4c7e
LB
4917
4918 /* Only some socketops are supported */
4919 switch (optname) {
4920 case SO_RCVBUF:
c9e45767 4921 val = min_t(u32, val, sysctl_rmem_max);
04c350b1 4922 val = min_t(int, val, INT_MAX / 2);
8c4b4c7e 4923 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
ebb3b78d
ED
4924 WRITE_ONCE(sk->sk_rcvbuf,
4925 max_t(int, val * 2, SOCK_MIN_RCVBUF));
8c4b4c7e
LB
4926 break;
4927 case SO_SNDBUF:
c9e45767 4928 val = min_t(u32, val, sysctl_wmem_max);
04c350b1 4929 val = min_t(int, val, INT_MAX / 2);
8c4b4c7e 4930 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
e292f05e
ED
4931 WRITE_ONCE(sk->sk_sndbuf,
4932 max_t(int, val * 2, SOCK_MIN_SNDBUF));
8c4b4c7e 4933 break;
76a9ebe8 4934 case SO_MAX_PACING_RATE: /* 32bit version */
e224c390
YC
4935 if (val != ~0U)
4936 cmpxchg(&sk->sk_pacing_status,
4937 SK_PACING_NONE,
4938 SK_PACING_NEEDED);
700465fd
KL
4939 sk->sk_max_pacing_rate = (val == ~0U) ?
4940 ~0UL : (unsigned int)val;
8c4b4c7e
LB
4941 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
4942 sk->sk_max_pacing_rate);
4943 break;
4944 case SO_PRIORITY:
4945 sk->sk_priority = val;
4946 break;
4947 case SO_RCVLOWAT:
4948 if (val < 0)
4949 val = INT_MAX;
eac66402 4950 WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
8c4b4c7e
LB
4951 break;
4952 case SO_MARK:
f4924f24
PO
4953 if (sk->sk_mark != val) {
4954 sk->sk_mark = val;
4955 sk_dst_reset(sk);
4956 }
8c4b4c7e 4957 break;
70c58997 4958 case SO_BINDTODEVICE:
70c58997
FF
4959 optlen = min_t(long, optlen, IFNAMSIZ - 1);
4960 strncpy(devname, optval, optlen);
4961 devname[optlen] = 0;
4962
4963 ifindex = 0;
4964 if (devname[0] != '\0') {
4965 struct net_device *dev;
4966
4967 ret = -ENODEV;
4968
4969 net = sock_net(sk);
4970 dev = dev_get_by_name(net, devname);
4971 if (!dev)
4972 break;
4973 ifindex = dev->ifindex;
4974 dev_put(dev);
4975 }
bcd6f4a8
DB
4976 fallthrough;
4977 case SO_BINDTOIFINDEX:
4978 if (optname == SO_BINDTOIFINDEX)
4979 ifindex = val;
70c58997 4980 ret = sock_bindtoindex(sk, ifindex, false);
70c58997 4981 break;
f9bcf968
DY
4982 case SO_KEEPALIVE:
4983 if (sk->sk_prot->keepalive)
4984 sk->sk_prot->keepalive(sk, valbool);
4985 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
4986 break;
6503b9f2
MB
4987 case SO_REUSEPORT:
4988 sk->sk_reuseport = valbool;
4989 break;
8c4b4c7e
LB
4990 default:
4991 ret = -EINVAL;
4992 }
a5192c52 4993#ifdef CONFIG_INET
6f5c39fa
NS
4994 } else if (level == SOL_IP) {
4995 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4996 return -EINVAL;
4997
4998 val = *((int *)optval);
4999 /* Only some options are supported */
5000 switch (optname) {
5001 case IP_TOS:
5002 if (val < -1 || val > 0xff) {
5003 ret = -EINVAL;
5004 } else {
5005 struct inet_sock *inet = inet_sk(sk);
5006
5007 if (val == -1)
5008 val = 0;
5009 inet->tos = val;
5010 }
5011 break;
5012 default:
5013 ret = -EINVAL;
5014 }
6f9bd3d7
LB
5015#if IS_ENABLED(CONFIG_IPV6)
5016 } else if (level == SOL_IPV6) {
5017 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
5018 return -EINVAL;
5019
5020 val = *((int *)optval);
5021 /* Only some options are supported */
5022 switch (optname) {
5023 case IPV6_TCLASS:
5024 if (val < -1 || val > 0xff) {
5025 ret = -EINVAL;
5026 } else {
5027 struct ipv6_pinfo *np = inet6_sk(sk);
5028
5029 if (val == -1)
5030 val = 0;
5031 np->tclass = val;
5032 }
5033 break;
5034 default:
5035 ret = -EINVAL;
5036 }
5037#endif
8c4b4c7e
LB
5038 } else if (level == SOL_TCP &&
5039 sk->sk_prot->setsockopt == tcp_setsockopt) {
91b5b21c
LB
5040 if (optname == TCP_CONGESTION) {
5041 char name[TCP_CA_NAME_MAX];
5042
5043 strncpy(name, optval, min_t(long, optlen,
5044 TCP_CA_NAME_MAX-1));
5045 name[TCP_CA_NAME_MAX-1] = 0;
29a94932 5046 ret = tcp_set_congestion_control(sk, name, false, true);
91b5b21c 5047 } else {
f9bcf968 5048 struct inet_connection_sock *icsk = inet_csk(sk);
fc747810 5049 struct tcp_sock *tp = tcp_sk(sk);
2b8ee4f0 5050 unsigned long timeout;
fc747810
LB
5051
5052 if (optlen != sizeof(int))
5053 return -EINVAL;
5054
5055 val = *((int *)optval);
5056 /* Only some options are supported */
5057 switch (optname) {
5058 case TCP_BPF_IW:
31aa6503 5059 if (val <= 0 || tp->data_segs_out > tp->syn_data)
fc747810
LB
5060 ret = -EINVAL;
5061 else
5062 tp->snd_cwnd = val;
5063 break;
13bf9641
LB
5064 case TCP_BPF_SNDCWND_CLAMP:
5065 if (val <= 0) {
5066 ret = -EINVAL;
5067 } else {
5068 tp->snd_cwnd_clamp = val;
5069 tp->snd_ssthresh = val;
5070 }
6d3f06a0 5071 break;
2b8ee4f0
MKL
5072 case TCP_BPF_DELACK_MAX:
5073 timeout = usecs_to_jiffies(val);
5074 if (timeout > TCP_DELACK_MAX ||
5075 timeout < TCP_TIMEOUT_MIN)
5076 return -EINVAL;
5077 inet_csk(sk)->icsk_delack_max = timeout;
5078 break;
ca584ba0
MKL
5079 case TCP_BPF_RTO_MIN:
5080 timeout = usecs_to_jiffies(val);
5081 if (timeout > TCP_RTO_MIN ||
5082 timeout < TCP_TIMEOUT_MIN)
5083 return -EINVAL;
5084 inet_csk(sk)->icsk_rto_min = timeout;
5085 break;
1e215300
NS
5086 case TCP_SAVE_SYN:
5087 if (val < 0 || val > 1)
5088 ret = -EINVAL;
5089 else
5090 tp->save_syn = val;
5091 break;
f9bcf968
DY
5092 case TCP_KEEPIDLE:
5093 ret = tcp_sock_set_keepidle_locked(sk, val);
5094 break;
5095 case TCP_KEEPINTVL:
5096 if (val < 1 || val > MAX_TCP_KEEPINTVL)
5097 ret = -EINVAL;
5098 else
5099 tp->keepalive_intvl = val * HZ;
5100 break;
5101 case TCP_KEEPCNT:
5102 if (val < 1 || val > MAX_TCP_KEEPCNT)
5103 ret = -EINVAL;
5104 else
5105 tp->keepalive_probes = val;
5106 break;
5107 case TCP_SYNCNT:
5108 if (val < 1 || val > MAX_TCP_SYNCNT)
5109 ret = -EINVAL;
5110 else
5111 icsk->icsk_syn_retries = val;
5112 break;
5113 case TCP_USER_TIMEOUT:
5114 if (val < 0)
5115 ret = -EINVAL;
5116 else
5117 icsk->icsk_user_timeout = val;
5118 break;
eca43ee6
NS
5119 case TCP_NOTSENT_LOWAT:
5120 tp->notsent_lowat = val;
5121 sk->sk_write_space(sk);
5122 break;
cb811109
P
5123 case TCP_WINDOW_CLAMP:
5124 ret = tcp_set_window_clamp(sk, val);
5125 break;
fc747810
LB
5126 default:
5127 ret = -EINVAL;
5128 }
91b5b21c 5129 }
91b5b21c 5130#endif
8c4b4c7e
LB
5131 } else {
5132 ret = -EINVAL;
5133 }
5134 return ret;
5135}
5136
beecf11b
SF
5137static int _bpf_getsockopt(struct sock *sk, int level, int optname,
5138 char *optval, int optlen)
cd86d1fd 5139{
cd86d1fd
LB
5140 if (!sk_fullsock(sk))
5141 goto err_clear;
beecf11b
SF
5142
5143 sock_owned_by_me(sk);
5144
bcd6f4a8
DB
5145 if (level == SOL_SOCKET) {
5146 if (optlen != sizeof(int))
5147 goto err_clear;
5148
5149 switch (optname) {
28479934
KI
5150 case SO_RCVBUF:
5151 *((int *)optval) = sk->sk_rcvbuf;
5152 break;
5153 case SO_SNDBUF:
5154 *((int *)optval) = sk->sk_sndbuf;
5155 break;
bcd6f4a8
DB
5156 case SO_MARK:
5157 *((int *)optval) = sk->sk_mark;
5158 break;
5159 case SO_PRIORITY:
5160 *((int *)optval) = sk->sk_priority;
5161 break;
5162 case SO_BINDTOIFINDEX:
5163 *((int *)optval) = sk->sk_bound_dev_if;
5164 break;
6503b9f2
MB
5165 case SO_REUSEPORT:
5166 *((int *)optval) = sk->sk_reuseport;
5167 break;
bcd6f4a8
DB
5168 default:
5169 goto err_clear;
5170 }
cd86d1fd 5171#ifdef CONFIG_INET
bcd6f4a8 5172 } else if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
1edb6e03
AR
5173 struct inet_connection_sock *icsk;
5174 struct tcp_sock *tp;
5175
1e215300
NS
5176 switch (optname) {
5177 case TCP_CONGESTION:
5178 icsk = inet_csk(sk);
cd86d1fd
LB
5179
5180 if (!icsk->icsk_ca_ops || optlen <= 1)
5181 goto err_clear;
5182 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
5183 optval[optlen - 1] = 0;
1e215300
NS
5184 break;
5185 case TCP_SAVED_SYN:
5186 tp = tcp_sk(sk);
5187
5188 if (optlen <= 0 || !tp->saved_syn ||
70a217f1 5189 optlen > tcp_saved_syn_len(tp->saved_syn))
1e215300 5190 goto err_clear;
70a217f1 5191 memcpy(optval, tp->saved_syn->data, optlen);
1e215300
NS
5192 break;
5193 default:
cd86d1fd
LB
5194 goto err_clear;
5195 }
6f5c39fa
NS
5196 } else if (level == SOL_IP) {
5197 struct inet_sock *inet = inet_sk(sk);
5198
5199 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
5200 goto err_clear;
5201
5202 /* Only some options are supported */
5203 switch (optname) {
5204 case IP_TOS:
5205 *((int *)optval) = (int)inet->tos;
5206 break;
5207 default:
5208 goto err_clear;
5209 }
6f9bd3d7
LB
5210#if IS_ENABLED(CONFIG_IPV6)
5211 } else if (level == SOL_IPV6) {
5212 struct ipv6_pinfo *np = inet6_sk(sk);
5213
5214 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
5215 goto err_clear;
5216
5217 /* Only some options are supported */
5218 switch (optname) {
5219 case IPV6_TCLASS:
5220 *((int *)optval) = (int)np->tclass;
5221 break;
5222 default:
5223 goto err_clear;
5224 }
bcd6f4a8 5225#endif
6f9bd3d7 5226#endif
cd86d1fd
LB
5227 } else {
5228 goto err_clear;
5229 }
aa2bc739 5230 return 0;
cd86d1fd
LB
5231err_clear:
5232 memset(optval, 0, optlen);
5233 return -EINVAL;
5234}
5235
3cee6fb8
MKL
5236BPF_CALL_5(bpf_sk_setsockopt, struct sock *, sk, int, level,
5237 int, optname, char *, optval, int, optlen)
5238{
eb18b49e
MKL
5239 if (level == SOL_TCP && optname == TCP_CONGESTION) {
5240 if (optlen >= sizeof("cdg") - 1 &&
5241 !strncmp("cdg", optval, optlen))
5242 return -ENOTSUPP;
5243 }
5244
3cee6fb8
MKL
5245 return _bpf_setsockopt(sk, level, optname, optval, optlen);
5246}
5247
5248const struct bpf_func_proto bpf_sk_setsockopt_proto = {
5249 .func = bpf_sk_setsockopt,
5250 .gpl_only = false,
5251 .ret_type = RET_INTEGER,
5252 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5253 .arg2_type = ARG_ANYTHING,
5254 .arg3_type = ARG_ANYTHING,
216e3cd2 5255 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
3cee6fb8
MKL
5256 .arg5_type = ARG_CONST_SIZE,
5257};
5258
5259BPF_CALL_5(bpf_sk_getsockopt, struct sock *, sk, int, level,
5260 int, optname, char *, optval, int, optlen)
5261{
5262 return _bpf_getsockopt(sk, level, optname, optval, optlen);
5263}
5264
5265const struct bpf_func_proto bpf_sk_getsockopt_proto = {
5266 .func = bpf_sk_getsockopt,
5267 .gpl_only = false,
5268 .ret_type = RET_INTEGER,
5269 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5270 .arg2_type = ARG_ANYTHING,
5271 .arg3_type = ARG_ANYTHING,
5272 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5273 .arg5_type = ARG_CONST_SIZE,
5274};
5275
beecf11b
SF
5276BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx,
5277 int, level, int, optname, char *, optval, int, optlen)
5278{
5cdc744c 5279 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen);
beecf11b
SF
5280}
5281
5282static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = {
5283 .func = bpf_sock_addr_setsockopt,
5284 .gpl_only = false,
5285 .ret_type = RET_INTEGER,
5286 .arg1_type = ARG_PTR_TO_CTX,
5287 .arg2_type = ARG_ANYTHING,
5288 .arg3_type = ARG_ANYTHING,
216e3cd2 5289 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
beecf11b
SF
5290 .arg5_type = ARG_CONST_SIZE,
5291};
5292
5293BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx,
5294 int, level, int, optname, char *, optval, int, optlen)
5295{
5296 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen);
5297}
5298
5299static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
5300 .func = bpf_sock_addr_getsockopt,
5301 .gpl_only = false,
5302 .ret_type = RET_INTEGER,
5303 .arg1_type = ARG_PTR_TO_CTX,
5304 .arg2_type = ARG_ANYTHING,
5305 .arg3_type = ARG_ANYTHING,
5306 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5307 .arg5_type = ARG_CONST_SIZE,
5308};
5309
5310BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5311 int, level, int, optname, char *, optval, int, optlen)
5312{
5cdc744c 5313 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
beecf11b
SF
5314}
5315
5316static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = {
5317 .func = bpf_sock_ops_setsockopt,
5318 .gpl_only = false,
5319 .ret_type = RET_INTEGER,
5320 .arg1_type = ARG_PTR_TO_CTX,
5321 .arg2_type = ARG_ANYTHING,
5322 .arg3_type = ARG_ANYTHING,
216e3cd2 5323 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
beecf11b
SF
5324 .arg5_type = ARG_CONST_SIZE,
5325};
5326
0813a841
MKL
5327static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock,
5328 int optname, const u8 **start)
5329{
5330 struct sk_buff *syn_skb = bpf_sock->syn_skb;
5331 const u8 *hdr_start;
5332 int ret;
5333
5334 if (syn_skb) {
5335 /* sk is a request_sock here */
5336
5337 if (optname == TCP_BPF_SYN) {
5338 hdr_start = syn_skb->data;
5339 ret = tcp_hdrlen(syn_skb);
267cf9fa 5340 } else if (optname == TCP_BPF_SYN_IP) {
0813a841
MKL
5341 hdr_start = skb_network_header(syn_skb);
5342 ret = skb_network_header_len(syn_skb) +
5343 tcp_hdrlen(syn_skb);
267cf9fa
MKL
5344 } else {
5345 /* optname == TCP_BPF_SYN_MAC */
5346 hdr_start = skb_mac_header(syn_skb);
5347 ret = skb_mac_header_len(syn_skb) +
5348 skb_network_header_len(syn_skb) +
5349 tcp_hdrlen(syn_skb);
0813a841
MKL
5350 }
5351 } else {
5352 struct sock *sk = bpf_sock->sk;
5353 struct saved_syn *saved_syn;
5354
5355 if (sk->sk_state == TCP_NEW_SYN_RECV)
5356 /* synack retransmit. bpf_sock->syn_skb will
5357 * not be available. It has to resort to
5358 * saved_syn (if it is saved).
5359 */
5360 saved_syn = inet_reqsk(sk)->saved_syn;
5361 else
5362 saved_syn = tcp_sk(sk)->saved_syn;
5363
5364 if (!saved_syn)
5365 return -ENOENT;
5366
5367 if (optname == TCP_BPF_SYN) {
5368 hdr_start = saved_syn->data +
267cf9fa 5369 saved_syn->mac_hdrlen +
0813a841
MKL
5370 saved_syn->network_hdrlen;
5371 ret = saved_syn->tcp_hdrlen;
267cf9fa
MKL
5372 } else if (optname == TCP_BPF_SYN_IP) {
5373 hdr_start = saved_syn->data +
5374 saved_syn->mac_hdrlen;
5375 ret = saved_syn->network_hdrlen +
5376 saved_syn->tcp_hdrlen;
0813a841 5377 } else {
267cf9fa
MKL
5378 /* optname == TCP_BPF_SYN_MAC */
5379
5380 /* TCP_SAVE_SYN may not have saved the mac hdr */
5381 if (!saved_syn->mac_hdrlen)
5382 return -ENOENT;
5383
0813a841 5384 hdr_start = saved_syn->data;
267cf9fa
MKL
5385 ret = saved_syn->mac_hdrlen +
5386 saved_syn->network_hdrlen +
0813a841
MKL
5387 saved_syn->tcp_hdrlen;
5388 }
5389 }
5390
5391 *start = hdr_start;
5392 return ret;
5393}
5394
beecf11b
SF
5395BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5396 int, level, int, optname, char *, optval, int, optlen)
5397{
0813a841 5398 if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP &&
267cf9fa 5399 optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) {
0813a841
MKL
5400 int ret, copy_len = 0;
5401 const u8 *start;
5402
5403 ret = bpf_sock_ops_get_syn(bpf_sock, optname, &start);
5404 if (ret > 0) {
5405 copy_len = ret;
5406 if (optlen < copy_len) {
5407 copy_len = optlen;
5408 ret = -ENOSPC;
5409 }
5410
5411 memcpy(optval, start, copy_len);
5412 }
5413
5414 /* Zero out unused buffer at the end */
5415 memset(optval + copy_len, 0, optlen - copy_len);
5416
5417 return ret;
5418 }
5419
beecf11b
SF
5420 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen);
5421}
5422
5423static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = {
5424 .func = bpf_sock_ops_getsockopt,
cd86d1fd
LB
5425 .gpl_only = false,
5426 .ret_type = RET_INTEGER,
5427 .arg1_type = ARG_PTR_TO_CTX,
5428 .arg2_type = ARG_ANYTHING,
5429 .arg3_type = ARG_ANYTHING,
5430 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5431 .arg5_type = ARG_CONST_SIZE,
5432};
5433
b13d8807
LB
5434BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
5435 int, argval)
5436{
5437 struct sock *sk = bpf_sock->sk;
5438 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
5439
a7dcdf6e 5440 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
b13d8807
LB
5441 return -EINVAL;
5442
725721a6 5443 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
b13d8807
LB
5444
5445 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
b13d8807
LB
5446}
5447
5448static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
5449 .func = bpf_sock_ops_cb_flags_set,
5450 .gpl_only = false,
5451 .ret_type = RET_INTEGER,
5452 .arg1_type = ARG_PTR_TO_CTX,
5453 .arg2_type = ARG_ANYTHING,
5454};
5455
d74bad4e
AI
5456const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
5457EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
5458
5459BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
5460 int, addr_len)
5461{
5462#ifdef CONFIG_INET
5463 struct sock *sk = ctx->sk;
8086fbaf 5464 u32 flags = BIND_FROM_BPF;
d74bad4e
AI
5465 int err;
5466
d74bad4e 5467 err = -EINVAL;
ba024f25
TH
5468 if (addr_len < offsetofend(struct sockaddr, sa_family))
5469 return err;
d74bad4e
AI
5470 if (addr->sa_family == AF_INET) {
5471 if (addr_len < sizeof(struct sockaddr_in))
5472 return err;
8086fbaf
SF
5473 if (((struct sockaddr_in *)addr)->sin_port == htons(0))
5474 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5475 return __inet_bind(sk, addr, addr_len, flags);
d74bad4e
AI
5476#if IS_ENABLED(CONFIG_IPV6)
5477 } else if (addr->sa_family == AF_INET6) {
5478 if (addr_len < SIN6_LEN_RFC2133)
5479 return err;
8086fbaf
SF
5480 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0))
5481 flags |= BIND_FORCE_ADDRESS_NO_PORT;
d74bad4e
AI
5482 /* ipv6_bpf_stub cannot be NULL, since it's called from
5483 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
5484 */
8086fbaf 5485 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags);
d74bad4e
AI
5486#endif /* CONFIG_IPV6 */
5487 }
5488#endif /* CONFIG_INET */
5489
5490 return -EAFNOSUPPORT;
5491}
5492
5493static const struct bpf_func_proto bpf_bind_proto = {
5494 .func = bpf_bind,
5495 .gpl_only = false,
5496 .ret_type = RET_INTEGER,
5497 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 5498 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
d74bad4e
AI
5499 .arg3_type = ARG_CONST_SIZE,
5500};
5501
12bed760
EB
5502#ifdef CONFIG_XFRM
5503BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
5504 struct bpf_xfrm_state *, to, u32, size, u64, flags)
5505{
5506 const struct sec_path *sp = skb_sec_path(skb);
5507 const struct xfrm_state *x;
5508
5509 if (!sp || unlikely(index >= sp->len || flags))
5510 goto err_clear;
5511
5512 x = sp->xvec[index];
5513
5514 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
5515 goto err_clear;
5516
5517 to->reqid = x->props.reqid;
5518 to->spi = x->id.spi;
5519 to->family = x->props.family;
1fbc2e0c
DB
5520 to->ext = 0;
5521
12bed760
EB
5522 if (to->family == AF_INET6) {
5523 memcpy(to->remote_ipv6, x->props.saddr.a6,
5524 sizeof(to->remote_ipv6));
5525 } else {
5526 to->remote_ipv4 = x->props.saddr.a4;
1fbc2e0c 5527 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
12bed760
EB
5528 }
5529
5530 return 0;
5531err_clear:
5532 memset(to, 0, size);
5533 return -EINVAL;
5534}
5535
5536static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
5537 .func = bpf_skb_get_xfrm_state,
5538 .gpl_only = false,
5539 .ret_type = RET_INTEGER,
5540 .arg1_type = ARG_PTR_TO_CTX,
5541 .arg2_type = ARG_ANYTHING,
5542 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
5543 .arg4_type = ARG_CONST_SIZE,
5544 .arg5_type = ARG_ANYTHING,
5545};
5546#endif
5547
87f5fc7e
DA
5548#if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
5549static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
5550 const struct neighbour *neigh,
e1850ea9 5551 const struct net_device *dev, u32 mtu)
87f5fc7e
DA
5552{
5553 memcpy(params->dmac, neigh->ha, ETH_ALEN);
5554 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
5555 params->h_vlan_TCI = 0;
5556 params->h_vlan_proto = 0;
e1850ea9
JDB
5557 if (mtu)
5558 params->mtu_result = mtu; /* union with tot_len */
87f5fc7e 5559
4c79579b 5560 return 0;
87f5fc7e
DA
5561}
5562#endif
5563
5564#if IS_ENABLED(CONFIG_INET)
5565static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4f74fede 5566 u32 flags, bool check_mtu)
87f5fc7e 5567{
eba618ab 5568 struct fib_nh_common *nhc;
87f5fc7e
DA
5569 struct in_device *in_dev;
5570 struct neighbour *neigh;
5571 struct net_device *dev;
5572 struct fib_result res;
87f5fc7e 5573 struct flowi4 fl4;
e1850ea9 5574 u32 mtu = 0;
87f5fc7e
DA
5575 int err;
5576
5577 dev = dev_get_by_index_rcu(net, params->ifindex);
5578 if (unlikely(!dev))
5579 return -ENODEV;
5580
5581 /* verify forwarding is enabled on this interface */
5582 in_dev = __in_dev_get_rcu(dev);
5583 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4c79579b 5584 return BPF_FIB_LKUP_RET_FWD_DISABLED;
87f5fc7e
DA
5585
5586 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5587 fl4.flowi4_iif = 1;
5588 fl4.flowi4_oif = params->ifindex;
5589 } else {
5590 fl4.flowi4_iif = params->ifindex;
5591 fl4.flowi4_oif = 0;
5592 }
5593 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
5594 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
5595 fl4.flowi4_flags = 0;
5596
5597 fl4.flowi4_proto = params->l4_protocol;
5598 fl4.daddr = params->ipv4_dst;
5599 fl4.saddr = params->ipv4_src;
5600 fl4.fl4_sport = params->sport;
5601 fl4.fl4_dport = params->dport;
1869e226 5602 fl4.flowi4_multipath_hash = 0;
87f5fc7e
DA
5603
5604 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5605 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5606 struct fib_table *tb;
5607
5608 tb = fib_get_table(net, tbid);
5609 if (unlikely(!tb))
4c79579b 5610 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
5611
5612 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
5613 } else {
5614 fl4.flowi4_mark = 0;
5615 fl4.flowi4_secid = 0;
5616 fl4.flowi4_tun_key.tun_id = 0;
5617 fl4.flowi4_uid = sock_net_uid(net, NULL);
5618
5619 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
5620 }
5621
4c79579b
DA
5622 if (err) {
5623 /* map fib lookup errors to RTN_ type */
5624 if (err == -EINVAL)
5625 return BPF_FIB_LKUP_RET_BLACKHOLE;
5626 if (err == -EHOSTUNREACH)
5627 return BPF_FIB_LKUP_RET_UNREACHABLE;
5628 if (err == -EACCES)
5629 return BPF_FIB_LKUP_RET_PROHIBIT;
5630
5631 return BPF_FIB_LKUP_RET_NOT_FWDED;
5632 }
5633
5634 if (res.type != RTN_UNICAST)
5635 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e 5636
5481d73f 5637 if (fib_info_num_path(res.fi) > 1)
87f5fc7e
DA
5638 fib_select_path(net, &res, &fl4, NULL);
5639
4f74fede
DA
5640 if (check_mtu) {
5641 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
e1850ea9
JDB
5642 if (params->tot_len > mtu) {
5643 params->mtu_result = mtu; /* union with tot_len */
4c79579b 5644 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
e1850ea9 5645 }
4f74fede
DA
5646 }
5647
eba618ab 5648 nhc = res.nhc;
87f5fc7e
DA
5649
5650 /* do not handle lwt encaps right now */
eba618ab 5651 if (nhc->nhc_lwtstate)
4c79579b 5652 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
87f5fc7e 5653
eba618ab 5654 dev = nhc->nhc_dev;
87f5fc7e
DA
5655
5656 params->rt_metric = res.fi->fib_priority;
d1c362e1 5657 params->ifindex = dev->ifindex;
87f5fc7e
DA
5658
5659 /* xdp and cls_bpf programs are run in RCU-bh so
5660 * rcu_read_lock_bh is not needed here
5661 */
6f5f68d0
DA
5662 if (likely(nhc->nhc_gw_family != AF_INET6)) {
5663 if (nhc->nhc_gw_family)
5664 params->ipv4_dst = nhc->nhc_gw.ipv4;
5665
5666 neigh = __ipv4_neigh_lookup_noref(dev,
5667 (__force u32)params->ipv4_dst);
5668 } else {
5669 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
5670
5671 params->family = AF_INET6;
5672 *dst = nhc->nhc_gw.ipv6;
5673 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5674 }
5675
4c79579b
DA
5676 if (!neigh)
5677 return BPF_FIB_LKUP_RET_NO_NEIGH;
87f5fc7e 5678
e1850ea9 5679 return bpf_fib_set_fwd_params(params, neigh, dev, mtu);
87f5fc7e
DA
5680}
5681#endif
5682
5683#if IS_ENABLED(CONFIG_IPV6)
5684static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4f74fede 5685 u32 flags, bool check_mtu)
87f5fc7e
DA
5686{
5687 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
5688 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
e55449e7 5689 struct fib6_result res = {};
87f5fc7e
DA
5690 struct neighbour *neigh;
5691 struct net_device *dev;
5692 struct inet6_dev *idev;
87f5fc7e
DA
5693 struct flowi6 fl6;
5694 int strict = 0;
effda4dd 5695 int oif, err;
e1850ea9 5696 u32 mtu = 0;
87f5fc7e
DA
5697
5698 /* link local addresses are never forwarded */
5699 if (rt6_need_strict(dst) || rt6_need_strict(src))
4c79579b 5700 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
5701
5702 dev = dev_get_by_index_rcu(net, params->ifindex);
5703 if (unlikely(!dev))
5704 return -ENODEV;
5705
5706 idev = __in6_dev_get_safely(dev);
56f0f84e 5707 if (unlikely(!idev || !idev->cnf.forwarding))
4c79579b 5708 return BPF_FIB_LKUP_RET_FWD_DISABLED;
87f5fc7e
DA
5709
5710 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5711 fl6.flowi6_iif = 1;
5712 oif = fl6.flowi6_oif = params->ifindex;
5713 } else {
5714 oif = fl6.flowi6_iif = params->ifindex;
5715 fl6.flowi6_oif = 0;
5716 strict = RT6_LOOKUP_F_HAS_SADDR;
5717 }
bd3a08aa 5718 fl6.flowlabel = params->flowinfo;
87f5fc7e
DA
5719 fl6.flowi6_scope = 0;
5720 fl6.flowi6_flags = 0;
5721 fl6.mp_hash = 0;
5722
5723 fl6.flowi6_proto = params->l4_protocol;
5724 fl6.daddr = *dst;
5725 fl6.saddr = *src;
5726 fl6.fl6_sport = params->sport;
5727 fl6.fl6_dport = params->dport;
5728
5729 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5730 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5731 struct fib6_table *tb;
5732
5733 tb = ipv6_stub->fib6_get_table(net, tbid);
5734 if (unlikely(!tb))
4c79579b 5735 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e 5736
effda4dd
DA
5737 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
5738 strict);
87f5fc7e
DA
5739 } else {
5740 fl6.flowi6_mark = 0;
5741 fl6.flowi6_secid = 0;
5742 fl6.flowi6_tun_key.tun_id = 0;
5743 fl6.flowi6_uid = sock_net_uid(net, NULL);
5744
effda4dd 5745 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
87f5fc7e
DA
5746 }
5747
effda4dd 5748 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
b1d40991 5749 res.f6i == net->ipv6.fib6_null_entry))
4c79579b
DA
5750 return BPF_FIB_LKUP_RET_NOT_FWDED;
5751
7d21fec9
DA
5752 switch (res.fib6_type) {
5753 /* only unicast is forwarded */
5754 case RTN_UNICAST:
5755 break;
5756 case RTN_BLACKHOLE:
5757 return BPF_FIB_LKUP_RET_BLACKHOLE;
5758 case RTN_UNREACHABLE:
5759 return BPF_FIB_LKUP_RET_UNREACHABLE;
5760 case RTN_PROHIBIT:
5761 return BPF_FIB_LKUP_RET_PROHIBIT;
5762 default:
4c79579b 5763 return BPF_FIB_LKUP_RET_NOT_FWDED;
7d21fec9 5764 }
87f5fc7e 5765
b1d40991
DA
5766 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
5767 fl6.flowi6_oif != 0, NULL, strict);
87f5fc7e 5768
4f74fede 5769 if (check_mtu) {
b748f260 5770 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
e1850ea9
JDB
5771 if (params->tot_len > mtu) {
5772 params->mtu_result = mtu; /* union with tot_len */
4c79579b 5773 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
e1850ea9 5774 }
4f74fede
DA
5775 }
5776
b1d40991 5777 if (res.nh->fib_nh_lws)
4c79579b 5778 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
87f5fc7e 5779
b1d40991
DA
5780 if (res.nh->fib_nh_gw_family)
5781 *dst = res.nh->fib_nh_gw6;
87f5fc7e 5782
b1d40991
DA
5783 dev = res.nh->fib_nh_dev;
5784 params->rt_metric = res.f6i->fib6_metric;
d1c362e1 5785 params->ifindex = dev->ifindex;
87f5fc7e
DA
5786
5787 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
71df5777 5788 * not needed here.
87f5fc7e 5789 */
71df5777 5790 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4c79579b
DA
5791 if (!neigh)
5792 return BPF_FIB_LKUP_RET_NO_NEIGH;
87f5fc7e 5793
e1850ea9 5794 return bpf_fib_set_fwd_params(params, neigh, dev, mtu);
87f5fc7e
DA
5795}
5796#endif
5797
5798BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
5799 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5800{
5801 if (plen < sizeof(*params))
5802 return -EINVAL;
5803
9ce64f19
DA
5804 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5805 return -EINVAL;
5806
87f5fc7e
DA
5807 switch (params->family) {
5808#if IS_ENABLED(CONFIG_INET)
5809 case AF_INET:
5810 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4f74fede 5811 flags, true);
87f5fc7e
DA
5812#endif
5813#if IS_ENABLED(CONFIG_IPV6)
5814 case AF_INET6:
5815 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4f74fede 5816 flags, true);
87f5fc7e
DA
5817#endif
5818 }
bcece5dc 5819 return -EAFNOSUPPORT;
87f5fc7e
DA
5820}
5821
5822static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
5823 .func = bpf_xdp_fib_lookup,
5824 .gpl_only = true,
5825 .ret_type = RET_INTEGER,
5826 .arg1_type = ARG_PTR_TO_CTX,
5827 .arg2_type = ARG_PTR_TO_MEM,
5828 .arg3_type = ARG_CONST_SIZE,
5829 .arg4_type = ARG_ANYTHING,
5830};
5831
5832BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
5833 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5834{
4f74fede 5835 struct net *net = dev_net(skb->dev);
4c79579b 5836 int rc = -EAFNOSUPPORT;
2c0a10af 5837 bool check_mtu = false;
4f74fede 5838
87f5fc7e
DA
5839 if (plen < sizeof(*params))
5840 return -EINVAL;
5841
9ce64f19
DA
5842 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5843 return -EINVAL;
5844
2c0a10af
JDB
5845 if (params->tot_len)
5846 check_mtu = true;
5847
87f5fc7e
DA
5848 switch (params->family) {
5849#if IS_ENABLED(CONFIG_INET)
5850 case AF_INET:
2c0a10af 5851 rc = bpf_ipv4_fib_lookup(net, params, flags, check_mtu);
4f74fede 5852 break;
87f5fc7e
DA
5853#endif
5854#if IS_ENABLED(CONFIG_IPV6)
5855 case AF_INET6:
2c0a10af 5856 rc = bpf_ipv6_fib_lookup(net, params, flags, check_mtu);
4f74fede 5857 break;
87f5fc7e
DA
5858#endif
5859 }
4f74fede 5860
2c0a10af 5861 if (rc == BPF_FIB_LKUP_RET_SUCCESS && !check_mtu) {
4f74fede
DA
5862 struct net_device *dev;
5863
2c0a10af
JDB
5864 /* When tot_len isn't provided by user, check skb
5865 * against MTU of FIB lookup resulting net_device
5866 */
4c79579b 5867 dev = dev_get_by_index_rcu(net, params->ifindex);
4f74fede 5868 if (!is_skb_forwardable(dev, skb))
4c79579b 5869 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
e1850ea9
JDB
5870
5871 params->mtu_result = dev->mtu; /* union with tot_len */
4f74fede
DA
5872 }
5873
4c79579b 5874 return rc;
87f5fc7e
DA
5875}
5876
5877static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
5878 .func = bpf_skb_fib_lookup,
5879 .gpl_only = true,
5880 .ret_type = RET_INTEGER,
5881 .arg1_type = ARG_PTR_TO_CTX,
5882 .arg2_type = ARG_PTR_TO_MEM,
5883 .arg3_type = ARG_CONST_SIZE,
5884 .arg4_type = ARG_ANYTHING,
5885};
5886
34b2021c
JDB
5887static struct net_device *__dev_via_ifindex(struct net_device *dev_curr,
5888 u32 ifindex)
5889{
5890 struct net *netns = dev_net(dev_curr);
5891
5892 /* Non-redirect use-cases can use ifindex=0 and save ifindex lookup */
5893 if (ifindex == 0)
5894 return dev_curr;
5895
5896 return dev_get_by_index_rcu(netns, ifindex);
5897}
5898
5899BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
5900 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
5901{
5902 int ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
5903 struct net_device *dev = skb->dev;
5904 int skb_len, dev_len;
5905 int mtu;
5906
5907 if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
5908 return -EINVAL;
5909
e5e35e75 5910 if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
34b2021c
JDB
5911 return -EINVAL;
5912
5913 dev = __dev_via_ifindex(dev, ifindex);
5914 if (unlikely(!dev))
5915 return -ENODEV;
5916
5917 mtu = READ_ONCE(dev->mtu);
5918
5919 dev_len = mtu + dev->hard_header_len;
e5e35e75
JDB
5920
5921 /* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
5922 skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len;
5923
5924 skb_len += len_diff; /* minus result pass check */
34b2021c
JDB
5925 if (skb_len <= dev_len) {
5926 ret = BPF_MTU_CHK_RET_SUCCESS;
5927 goto out;
5928 }
5929 /* At this point, skb->len exceed MTU, but as it include length of all
5930 * segments, it can still be below MTU. The SKB can possibly get
5931 * re-segmented in transmit path (see validate_xmit_skb). Thus, user
5932 * must choose if segs are to be MTU checked.
5933 */
5934 if (skb_is_gso(skb)) {
5935 ret = BPF_MTU_CHK_RET_SUCCESS;
5936
5937 if (flags & BPF_MTU_CHK_SEGS &&
5938 !skb_gso_validate_network_len(skb, mtu))
5939 ret = BPF_MTU_CHK_RET_SEGS_TOOBIG;
5940 }
5941out:
5942 /* BPF verifier guarantees valid pointer */
5943 *mtu_len = mtu;
5944
5945 return ret;
5946}
5947
5948BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
5949 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
5950{
5951 struct net_device *dev = xdp->rxq->dev;
5952 int xdp_len = xdp->data_end - xdp->data;
5953 int ret = BPF_MTU_CHK_RET_SUCCESS;
5954 int mtu, dev_len;
5955
5956 /* XDP variant doesn't support multi-buffer segment check (yet) */
5957 if (unlikely(flags))
5958 return -EINVAL;
5959
5960 dev = __dev_via_ifindex(dev, ifindex);
5961 if (unlikely(!dev))
5962 return -ENODEV;
5963
5964 mtu = READ_ONCE(dev->mtu);
5965
5966 /* Add L2-header as dev MTU is L3 size */
5967 dev_len = mtu + dev->hard_header_len;
5968
e5e35e75
JDB
5969 /* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
5970 if (*mtu_len)
5971 xdp_len = *mtu_len + dev->hard_header_len;
5972
34b2021c
JDB
5973 xdp_len += len_diff; /* minus result pass check */
5974 if (xdp_len > dev_len)
5975 ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
5976
5977 /* BPF verifier guarantees valid pointer */
5978 *mtu_len = mtu;
5979
5980 return ret;
5981}
5982
5983static const struct bpf_func_proto bpf_skb_check_mtu_proto = {
5984 .func = bpf_skb_check_mtu,
5985 .gpl_only = true,
5986 .ret_type = RET_INTEGER,
5987 .arg1_type = ARG_PTR_TO_CTX,
5988 .arg2_type = ARG_ANYTHING,
5989 .arg3_type = ARG_PTR_TO_INT,
5990 .arg4_type = ARG_ANYTHING,
5991 .arg5_type = ARG_ANYTHING,
5992};
5993
5994static const struct bpf_func_proto bpf_xdp_check_mtu_proto = {
5995 .func = bpf_xdp_check_mtu,
5996 .gpl_only = true,
5997 .ret_type = RET_INTEGER,
5998 .arg1_type = ARG_PTR_TO_CTX,
5999 .arg2_type = ARG_ANYTHING,
6000 .arg3_type = ARG_PTR_TO_INT,
6001 .arg4_type = ARG_ANYTHING,
6002 .arg5_type = ARG_ANYTHING,
6003};
6004
fe94cc29
MX
6005#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6006static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
6007{
6008 int err;
6009 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
6010
bb986a50 6011 if (!seg6_validate_srh(srh, len, false))
fe94cc29
MX
6012 return -EINVAL;
6013
6014 switch (type) {
6015 case BPF_LWT_ENCAP_SEG6_INLINE:
6016 if (skb->protocol != htons(ETH_P_IPV6))
6017 return -EBADMSG;
6018
6019 err = seg6_do_srh_inline(skb, srh);
6020 break;
6021 case BPF_LWT_ENCAP_SEG6:
6022 skb_reset_inner_headers(skb);
6023 skb->encapsulation = 1;
6024 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
6025 break;
6026 default:
6027 return -EINVAL;
6028 }
6029
6030 bpf_compute_data_pointers(skb);
6031 if (err)
6032 return err;
6033
6034 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
6035 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
6036
6037 return seg6_lookup_nexthop(skb, NULL, 0);
6038}
6039#endif /* CONFIG_IPV6_SEG6_BPF */
6040
3e0bd37c
PO
6041#if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6042static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
6043 bool ingress)
6044{
52f27877 6045 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
3e0bd37c
PO
6046}
6047#endif
6048
6049BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
fe94cc29
MX
6050 u32, len)
6051{
6052 switch (type) {
6053#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6054 case BPF_LWT_ENCAP_SEG6:
6055 case BPF_LWT_ENCAP_SEG6_INLINE:
6056 return bpf_push_seg6_encap(skb, type, hdr, len);
3e0bd37c
PO
6057#endif
6058#if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6059 case BPF_LWT_ENCAP_IP:
6060 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
fe94cc29
MX
6061#endif
6062 default:
6063 return -EINVAL;
6064 }
6065}
6066
3e0bd37c
PO
6067BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
6068 void *, hdr, u32, len)
6069{
6070 switch (type) {
6071#if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6072 case BPF_LWT_ENCAP_IP:
6073 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
fe94cc29
MX
6074#endif
6075 default:
6076 return -EINVAL;
6077 }
6078}
6079
3e0bd37c
PO
6080static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
6081 .func = bpf_lwt_in_push_encap,
6082 .gpl_only = false,
6083 .ret_type = RET_INTEGER,
6084 .arg1_type = ARG_PTR_TO_CTX,
6085 .arg2_type = ARG_ANYTHING,
216e3cd2 6086 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
3e0bd37c
PO
6087 .arg4_type = ARG_CONST_SIZE
6088};
6089
6090static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
6091 .func = bpf_lwt_xmit_push_encap,
fe94cc29
MX
6092 .gpl_only = false,
6093 .ret_type = RET_INTEGER,
6094 .arg1_type = ARG_PTR_TO_CTX,
6095 .arg2_type = ARG_ANYTHING,
216e3cd2 6096 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
fe94cc29
MX
6097 .arg4_type = ARG_CONST_SIZE
6098};
6099
61d76980 6100#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
fe94cc29
MX
6101BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
6102 const void *, from, u32, len)
6103{
fe94cc29
MX
6104 struct seg6_bpf_srh_state *srh_state =
6105 this_cpu_ptr(&seg6_bpf_srh_states);
486cdf21 6106 struct ipv6_sr_hdr *srh = srh_state->srh;
fe94cc29 6107 void *srh_tlvs, *srh_end, *ptr;
fe94cc29
MX
6108 int srhoff = 0;
6109
486cdf21 6110 if (srh == NULL)
fe94cc29
MX
6111 return -EINVAL;
6112
fe94cc29
MX
6113 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
6114 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
6115
6116 ptr = skb->data + offset;
6117 if (ptr >= srh_tlvs && ptr + len <= srh_end)
486cdf21 6118 srh_state->valid = false;
fe94cc29
MX
6119 else if (ptr < (void *)&srh->flags ||
6120 ptr + len > (void *)&srh->segments)
6121 return -EFAULT;
6122
6123 if (unlikely(bpf_try_make_writable(skb, offset + len)))
6124 return -EFAULT;
486cdf21
MX
6125 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6126 return -EINVAL;
6127 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
fe94cc29
MX
6128
6129 memcpy(skb->data + offset, from, len);
6130 return 0;
fe94cc29
MX
6131}
6132
6133static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
6134 .func = bpf_lwt_seg6_store_bytes,
6135 .gpl_only = false,
6136 .ret_type = RET_INTEGER,
6137 .arg1_type = ARG_PTR_TO_CTX,
6138 .arg2_type = ARG_ANYTHING,
216e3cd2 6139 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
fe94cc29
MX
6140 .arg4_type = ARG_CONST_SIZE
6141};
6142
486cdf21 6143static void bpf_update_srh_state(struct sk_buff *skb)
fe94cc29 6144{
fe94cc29
MX
6145 struct seg6_bpf_srh_state *srh_state =
6146 this_cpu_ptr(&seg6_bpf_srh_states);
fe94cc29 6147 int srhoff = 0;
fe94cc29 6148
486cdf21
MX
6149 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
6150 srh_state->srh = NULL;
6151 } else {
6152 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6153 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
6154 srh_state->valid = true;
fe94cc29 6155 }
486cdf21
MX
6156}
6157
6158BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
6159 u32, action, void *, param, u32, param_len)
6160{
6161 struct seg6_bpf_srh_state *srh_state =
6162 this_cpu_ptr(&seg6_bpf_srh_states);
6163 int hdroff = 0;
6164 int err;
fe94cc29
MX
6165
6166 switch (action) {
6167 case SEG6_LOCAL_ACTION_END_X:
486cdf21
MX
6168 if (!seg6_bpf_has_valid_srh(skb))
6169 return -EBADMSG;
fe94cc29
MX
6170 if (param_len != sizeof(struct in6_addr))
6171 return -EINVAL;
6172 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
6173 case SEG6_LOCAL_ACTION_END_T:
486cdf21
MX
6174 if (!seg6_bpf_has_valid_srh(skb))
6175 return -EBADMSG;
fe94cc29
MX
6176 if (param_len != sizeof(int))
6177 return -EINVAL;
6178 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
486cdf21
MX
6179 case SEG6_LOCAL_ACTION_END_DT6:
6180 if (!seg6_bpf_has_valid_srh(skb))
6181 return -EBADMSG;
fe94cc29
MX
6182 if (param_len != sizeof(int))
6183 return -EINVAL;
486cdf21
MX
6184
6185 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
6186 return -EBADMSG;
6187 if (!pskb_pull(skb, hdroff))
6188 return -EBADMSG;
6189
6190 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
6191 skb_reset_network_header(skb);
6192 skb_reset_transport_header(skb);
6193 skb->encapsulation = 0;
6194
6195 bpf_compute_data_pointers(skb);
6196 bpf_update_srh_state(skb);
fe94cc29
MX
6197 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6198 case SEG6_LOCAL_ACTION_END_B6:
486cdf21
MX
6199 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6200 return -EBADMSG;
fe94cc29
MX
6201 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
6202 param, param_len);
6203 if (!err)
486cdf21
MX
6204 bpf_update_srh_state(skb);
6205
fe94cc29
MX
6206 return err;
6207 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
486cdf21
MX
6208 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6209 return -EBADMSG;
fe94cc29
MX
6210 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
6211 param, param_len);
6212 if (!err)
486cdf21
MX
6213 bpf_update_srh_state(skb);
6214
fe94cc29
MX
6215 return err;
6216 default:
6217 return -EINVAL;
6218 }
fe94cc29
MX
6219}
6220
6221static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
6222 .func = bpf_lwt_seg6_action,
6223 .gpl_only = false,
6224 .ret_type = RET_INTEGER,
6225 .arg1_type = ARG_PTR_TO_CTX,
6226 .arg2_type = ARG_ANYTHING,
216e3cd2 6227 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
fe94cc29
MX
6228 .arg4_type = ARG_CONST_SIZE
6229};
6230
6231BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
6232 s32, len)
6233{
fe94cc29
MX
6234 struct seg6_bpf_srh_state *srh_state =
6235 this_cpu_ptr(&seg6_bpf_srh_states);
486cdf21 6236 struct ipv6_sr_hdr *srh = srh_state->srh;
fe94cc29 6237 void *srh_end, *srh_tlvs, *ptr;
fe94cc29
MX
6238 struct ipv6hdr *hdr;
6239 int srhoff = 0;
6240 int ret;
6241
486cdf21 6242 if (unlikely(srh == NULL))
fe94cc29 6243 return -EINVAL;
fe94cc29
MX
6244
6245 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
6246 ((srh->first_segment + 1) << 4));
6247 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
6248 srh_state->hdrlen);
6249 ptr = skb->data + offset;
6250
6251 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
6252 return -EFAULT;
6253 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
6254 return -EFAULT;
6255
6256 if (len > 0) {
6257 ret = skb_cow_head(skb, len);
6258 if (unlikely(ret < 0))
6259 return ret;
6260
6261 ret = bpf_skb_net_hdr_push(skb, offset, len);
6262 } else {
6263 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
6264 }
6265
6266 bpf_compute_data_pointers(skb);
6267 if (unlikely(ret < 0))
6268 return ret;
6269
6270 hdr = (struct ipv6hdr *)skb->data;
6271 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
6272
486cdf21
MX
6273 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6274 return -EINVAL;
6275 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
fe94cc29 6276 srh_state->hdrlen += len;
486cdf21 6277 srh_state->valid = false;
fe94cc29 6278 return 0;
fe94cc29
MX
6279}
6280
6281static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
6282 .func = bpf_lwt_seg6_adjust_srh,
6283 .gpl_only = false,
6284 .ret_type = RET_INTEGER,
6285 .arg1_type = ARG_PTR_TO_CTX,
6286 .arg2_type = ARG_ANYTHING,
6287 .arg3_type = ARG_ANYTHING,
6288};
61d76980 6289#endif /* CONFIG_IPV6_SEG6_BPF */
fe94cc29 6290
df3f94a0
AB
6291#ifdef CONFIG_INET
6292static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
c8123ead 6293 int dif, int sdif, u8 family, u8 proto)
6acc9b43 6294{
6acc9b43
JS
6295 bool refcounted = false;
6296 struct sock *sk = NULL;
6297
6298 if (family == AF_INET) {
6299 __be32 src4 = tuple->ipv4.saddr;
6300 __be32 dst4 = tuple->ipv4.daddr;
6acc9b43
JS
6301
6302 if (proto == IPPROTO_TCP)
c8123ead 6303 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
6acc9b43
JS
6304 src4, tuple->ipv4.sport,
6305 dst4, tuple->ipv4.dport,
6306 dif, sdif, &refcounted);
6307 else
6308 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
6309 dst4, tuple->ipv4.dport,
c8123ead 6310 dif, sdif, &udp_table, NULL);
8a615c6b 6311#if IS_ENABLED(CONFIG_IPV6)
6acc9b43
JS
6312 } else {
6313 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
6314 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
6acc9b43
JS
6315
6316 if (proto == IPPROTO_TCP)
c8123ead 6317 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
6acc9b43 6318 src6, tuple->ipv6.sport,
cac6cc2f 6319 dst6, ntohs(tuple->ipv6.dport),
6acc9b43 6320 dif, sdif, &refcounted);
8a615c6b
JS
6321 else if (likely(ipv6_bpf_stub))
6322 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
6323 src6, tuple->ipv6.sport,
cac6cc2f 6324 dst6, tuple->ipv6.dport,
8a615c6b 6325 dif, sdif,
c8123ead 6326 &udp_table, NULL);
6acc9b43
JS
6327#endif
6328 }
6329
6330 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
6331 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6332 sk = NULL;
6333 }
6334 return sk;
6335}
6336
edbf8c01 6337/* bpf_skc_lookup performs the core lookup for different types of sockets,
6acc9b43
JS
6338 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
6339 * Returns the socket as an 'unsigned long' to simplify the casting in the
6340 * callers to satisfy BPF_CALL declarations.
6341 */
edbf8c01
LB
6342static struct sock *
6343__bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6344 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6345 u64 flags)
6acc9b43 6346{
6acc9b43
JS
6347 struct sock *sk = NULL;
6348 u8 family = AF_UNSPEC;
6349 struct net *net;
c8123ead 6350 int sdif;
6acc9b43 6351
9b28ae24
LB
6352 if (len == sizeof(tuple->ipv4))
6353 family = AF_INET;
6354 else if (len == sizeof(tuple->ipv6))
6355 family = AF_INET6;
6356 else
6357 return NULL;
6358
f71c6143
JS
6359 if (unlikely(family == AF_UNSPEC || flags ||
6360 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
6acc9b43
JS
6361 goto out;
6362
c8123ead
NH
6363 if (family == AF_INET)
6364 sdif = inet_sdif(skb);
6acc9b43 6365 else
c8123ead
NH
6366 sdif = inet6_sdif(skb);
6367
f71c6143
JS
6368 if ((s32)netns_id < 0) {
6369 net = caller_net;
4cc1feeb 6370 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
f71c6143 6371 } else {
6acc9b43
JS
6372 net = get_net_ns_by_id(caller_net, netns_id);
6373 if (unlikely(!net))
6374 goto out;
c8123ead 6375 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6acc9b43 6376 put_net(net);
6acc9b43
JS
6377 }
6378
edbf8c01
LB
6379out:
6380 return sk;
6381}
6382
6383static struct sock *
6384__bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6385 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6386 u64 flags)
6387{
6388 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
6389 ifindex, proto, netns_id, flags);
6390
f7355a6c 6391 if (sk) {
6acc9b43 6392 sk = sk_to_full_sk(sk);
f7355a6c 6393 if (!sk_fullsock(sk)) {
2e012c74 6394 sock_gen_put(sk);
f7355a6c
MKL
6395 return NULL;
6396 }
6397 }
edbf8c01
LB
6398
6399 return sk;
6acc9b43
JS
6400}
6401
edbf8c01
LB
6402static struct sock *
6403bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6404 u8 proto, u64 netns_id, u64 flags)
c8123ead
NH
6405{
6406 struct net *caller_net;
6407 int ifindex;
6408
6409 if (skb->dev) {
6410 caller_net = dev_net(skb->dev);
6411 ifindex = skb->dev->ifindex;
6412 } else {
6413 caller_net = sock_net(skb->sk);
6414 ifindex = 0;
6415 }
6416
edbf8c01
LB
6417 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
6418 netns_id, flags);
c8123ead
NH
6419}
6420
edbf8c01
LB
6421static struct sock *
6422bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6423 u8 proto, u64 netns_id, u64 flags)
6424{
6425 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
6426 flags);
6427
f7355a6c 6428 if (sk) {
edbf8c01 6429 sk = sk_to_full_sk(sk);
f7355a6c 6430 if (!sk_fullsock(sk)) {
2e012c74 6431 sock_gen_put(sk);
f7355a6c
MKL
6432 return NULL;
6433 }
6434 }
edbf8c01
LB
6435
6436 return sk;
6437}
6438
6439BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
6440 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6441{
6442 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
6443 netns_id, flags);
6444}
6445
6446static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
6447 .func = bpf_skc_lookup_tcp,
6448 .gpl_only = false,
6449 .pkt_access = true,
6450 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6451 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 6452 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
edbf8c01
LB
6453 .arg3_type = ARG_CONST_SIZE,
6454 .arg4_type = ARG_ANYTHING,
6455 .arg5_type = ARG_ANYTHING,
6456};
6457
6acc9b43
JS
6458BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
6459 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6460{
edbf8c01
LB
6461 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
6462 netns_id, flags);
6acc9b43
JS
6463}
6464
6465static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
6466 .func = bpf_sk_lookup_tcp,
6467 .gpl_only = false,
6468 .pkt_access = true,
6469 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6470 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 6471 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6acc9b43
JS
6472 .arg3_type = ARG_CONST_SIZE,
6473 .arg4_type = ARG_ANYTHING,
6474 .arg5_type = ARG_ANYTHING,
6475};
6476
6477BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
6478 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6479{
edbf8c01
LB
6480 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
6481 netns_id, flags);
6acc9b43
JS
6482}
6483
6484static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
6485 .func = bpf_sk_lookup_udp,
6486 .gpl_only = false,
6487 .pkt_access = true,
6488 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6489 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 6490 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6acc9b43
JS
6491 .arg3_type = ARG_CONST_SIZE,
6492 .arg4_type = ARG_ANYTHING,
6493 .arg5_type = ARG_ANYTHING,
6494};
6495
6496BPF_CALL_1(bpf_sk_release, struct sock *, sk)
6497{
a5fa25ad 6498 if (sk && sk_is_refcounted(sk))
6acc9b43
JS
6499 sock_gen_put(sk);
6500 return 0;
6501}
6502
6503static const struct bpf_func_proto bpf_sk_release_proto = {
6504 .func = bpf_sk_release,
6505 .gpl_only = false,
6506 .ret_type = RET_INTEGER,
a5fa25ad 6507 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
6acc9b43 6508};
c8123ead
NH
6509
6510BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
6511 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6512{
6513 struct net *caller_net = dev_net(ctx->rxq->dev);
6514 int ifindex = ctx->rxq->dev->ifindex;
6515
edbf8c01
LB
6516 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6517 ifindex, IPPROTO_UDP, netns_id,
6518 flags);
c8123ead
NH
6519}
6520
6521static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
6522 .func = bpf_xdp_sk_lookup_udp,
6523 .gpl_only = false,
6524 .pkt_access = true,
6525 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6526 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 6527 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
c8123ead
NH
6528 .arg3_type = ARG_CONST_SIZE,
6529 .arg4_type = ARG_ANYTHING,
6530 .arg5_type = ARG_ANYTHING,
6531};
6532
edbf8c01
LB
6533BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
6534 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6535{
6536 struct net *caller_net = dev_net(ctx->rxq->dev);
6537 int ifindex = ctx->rxq->dev->ifindex;
6538
6539 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
6540 ifindex, IPPROTO_TCP, netns_id,
6541 flags);
6542}
6543
6544static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
6545 .func = bpf_xdp_skc_lookup_tcp,
6546 .gpl_only = false,
6547 .pkt_access = true,
6548 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6549 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 6550 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
edbf8c01
LB
6551 .arg3_type = ARG_CONST_SIZE,
6552 .arg4_type = ARG_ANYTHING,
6553 .arg5_type = ARG_ANYTHING,
6554};
6555
c8123ead
NH
6556BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
6557 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6558{
6559 struct net *caller_net = dev_net(ctx->rxq->dev);
6560 int ifindex = ctx->rxq->dev->ifindex;
6561
edbf8c01
LB
6562 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6563 ifindex, IPPROTO_TCP, netns_id,
6564 flags);
c8123ead
NH
6565}
6566
6567static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
6568 .func = bpf_xdp_sk_lookup_tcp,
6569 .gpl_only = false,
6570 .pkt_access = true,
6571 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6572 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 6573 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
c8123ead
NH
6574 .arg3_type = ARG_CONST_SIZE,
6575 .arg4_type = ARG_ANYTHING,
6576 .arg5_type = ARG_ANYTHING,
6577};
6c49e65e 6578
edbf8c01
LB
6579BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6580 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6581{
6582 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
6583 sock_net(ctx->sk), 0,
6584 IPPROTO_TCP, netns_id, flags);
6585}
6586
6587static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
6588 .func = bpf_sock_addr_skc_lookup_tcp,
6589 .gpl_only = false,
6590 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6591 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 6592 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
edbf8c01
LB
6593 .arg3_type = ARG_CONST_SIZE,
6594 .arg4_type = ARG_ANYTHING,
6595 .arg5_type = ARG_ANYTHING,
6596};
6597
6c49e65e
AI
6598BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6599 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6600{
edbf8c01
LB
6601 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6602 sock_net(ctx->sk), 0, IPPROTO_TCP,
6603 netns_id, flags);
6c49e65e
AI
6604}
6605
6606static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
6607 .func = bpf_sock_addr_sk_lookup_tcp,
6608 .gpl_only = false,
6609 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6610 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 6611 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6c49e65e
AI
6612 .arg3_type = ARG_CONST_SIZE,
6613 .arg4_type = ARG_ANYTHING,
6614 .arg5_type = ARG_ANYTHING,
6615};
6616
6617BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
6618 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6619{
edbf8c01
LB
6620 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6621 sock_net(ctx->sk), 0, IPPROTO_UDP,
6622 netns_id, flags);
6c49e65e
AI
6623}
6624
6625static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
6626 .func = bpf_sock_addr_sk_lookup_udp,
6627 .gpl_only = false,
6628 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6629 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 6630 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6c49e65e
AI
6631 .arg3_type = ARG_CONST_SIZE,
6632 .arg4_type = ARG_ANYTHING,
6633 .arg5_type = ARG_ANYTHING,
6634};
6635
655a51e5
MKL
6636bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6637 struct bpf_insn_access_aux *info)
6638{
c2cb5e82
SF
6639 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
6640 icsk_retransmits))
655a51e5
MKL
6641 return false;
6642
6643 if (off % size != 0)
6644 return false;
6645
6646 switch (off) {
6647 case offsetof(struct bpf_tcp_sock, bytes_received):
6648 case offsetof(struct bpf_tcp_sock, bytes_acked):
6649 return size == sizeof(__u64);
6650 default:
6651 return size == sizeof(__u32);
6652 }
6653}
6654
6655u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
6656 const struct bpf_insn *si,
6657 struct bpf_insn *insn_buf,
6658 struct bpf_prog *prog, u32 *target_size)
6659{
6660 struct bpf_insn *insn = insn_buf;
6661
6662#define BPF_TCP_SOCK_GET_COMMON(FIELD) \
6663 do { \
c593642c
PB
6664 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
6665 sizeof_field(struct bpf_tcp_sock, FIELD)); \
655a51e5
MKL
6666 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
6667 si->dst_reg, si->src_reg, \
6668 offsetof(struct tcp_sock, FIELD)); \
6669 } while (0)
6670
c2cb5e82
SF
6671#define BPF_INET_SOCK_GET_COMMON(FIELD) \
6672 do { \
c593642c 6673 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
c2cb5e82 6674 FIELD) > \
c593642c 6675 sizeof_field(struct bpf_tcp_sock, FIELD)); \
c2cb5e82
SF
6676 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6677 struct inet_connection_sock, \
6678 FIELD), \
6679 si->dst_reg, si->src_reg, \
6680 offsetof( \
6681 struct inet_connection_sock, \
6682 FIELD)); \
6683 } while (0)
6684
655a51e5
MKL
6685 if (insn > insn_buf)
6686 return insn - insn_buf;
6687
6688 switch (si->off) {
6689 case offsetof(struct bpf_tcp_sock, rtt_min):
c593642c 6690 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
655a51e5
MKL
6691 sizeof(struct minmax));
6692 BUILD_BUG_ON(sizeof(struct minmax) <
6693 sizeof(struct minmax_sample));
6694
6695 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6696 offsetof(struct tcp_sock, rtt_min) +
6697 offsetof(struct minmax_sample, v));
6698 break;
2377b81d
SF
6699 case offsetof(struct bpf_tcp_sock, snd_cwnd):
6700 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
6701 break;
6702 case offsetof(struct bpf_tcp_sock, srtt_us):
6703 BPF_TCP_SOCK_GET_COMMON(srtt_us);
6704 break;
6705 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
6706 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
6707 break;
6708 case offsetof(struct bpf_tcp_sock, rcv_nxt):
6709 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
6710 break;
6711 case offsetof(struct bpf_tcp_sock, snd_nxt):
6712 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
6713 break;
6714 case offsetof(struct bpf_tcp_sock, snd_una):
6715 BPF_TCP_SOCK_GET_COMMON(snd_una);
6716 break;
6717 case offsetof(struct bpf_tcp_sock, mss_cache):
6718 BPF_TCP_SOCK_GET_COMMON(mss_cache);
6719 break;
6720 case offsetof(struct bpf_tcp_sock, ecn_flags):
6721 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
6722 break;
6723 case offsetof(struct bpf_tcp_sock, rate_delivered):
6724 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
6725 break;
6726 case offsetof(struct bpf_tcp_sock, rate_interval_us):
6727 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
6728 break;
6729 case offsetof(struct bpf_tcp_sock, packets_out):
6730 BPF_TCP_SOCK_GET_COMMON(packets_out);
6731 break;
6732 case offsetof(struct bpf_tcp_sock, retrans_out):
6733 BPF_TCP_SOCK_GET_COMMON(retrans_out);
6734 break;
6735 case offsetof(struct bpf_tcp_sock, total_retrans):
6736 BPF_TCP_SOCK_GET_COMMON(total_retrans);
6737 break;
6738 case offsetof(struct bpf_tcp_sock, segs_in):
6739 BPF_TCP_SOCK_GET_COMMON(segs_in);
6740 break;
6741 case offsetof(struct bpf_tcp_sock, data_segs_in):
6742 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
6743 break;
6744 case offsetof(struct bpf_tcp_sock, segs_out):
6745 BPF_TCP_SOCK_GET_COMMON(segs_out);
6746 break;
6747 case offsetof(struct bpf_tcp_sock, data_segs_out):
6748 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
6749 break;
6750 case offsetof(struct bpf_tcp_sock, lost_out):
6751 BPF_TCP_SOCK_GET_COMMON(lost_out);
6752 break;
6753 case offsetof(struct bpf_tcp_sock, sacked_out):
6754 BPF_TCP_SOCK_GET_COMMON(sacked_out);
6755 break;
6756 case offsetof(struct bpf_tcp_sock, bytes_received):
6757 BPF_TCP_SOCK_GET_COMMON(bytes_received);
6758 break;
6759 case offsetof(struct bpf_tcp_sock, bytes_acked):
6760 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
6761 break;
0357746d
SF
6762 case offsetof(struct bpf_tcp_sock, dsack_dups):
6763 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
6764 break;
6765 case offsetof(struct bpf_tcp_sock, delivered):
6766 BPF_TCP_SOCK_GET_COMMON(delivered);
6767 break;
6768 case offsetof(struct bpf_tcp_sock, delivered_ce):
6769 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
6770 break;
c2cb5e82
SF
6771 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
6772 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
6773 break;
655a51e5
MKL
6774 }
6775
6776 return insn - insn_buf;
6777}
6778
6779BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
6780{
655a51e5
MKL
6781 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
6782 return (unsigned long)sk;
6783
6784 return (unsigned long)NULL;
6785}
6786
0d01da6a 6787const struct bpf_func_proto bpf_tcp_sock_proto = {
655a51e5
MKL
6788 .func = bpf_tcp_sock,
6789 .gpl_only = false,
6790 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
6791 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6792};
6793
dbafd7dd
MKL
6794BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
6795{
6796 sk = sk_to_full_sk(sk);
6797
6798 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
6799 return (unsigned long)sk;
6800
6801 return (unsigned long)NULL;
6802}
6803
6804static const struct bpf_func_proto bpf_get_listener_sock_proto = {
6805 .func = bpf_get_listener_sock,
6806 .gpl_only = false,
6807 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6808 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6809};
6810
f7c917ba 6811BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
6812{
6813 unsigned int iphdr_len;
6814
d7bf2ebe
THJ
6815 switch (skb_protocol(skb, true)) {
6816 case cpu_to_be16(ETH_P_IP):
f7c917ba 6817 iphdr_len = sizeof(struct iphdr);
d7bf2ebe
THJ
6818 break;
6819 case cpu_to_be16(ETH_P_IPV6):
f7c917ba 6820 iphdr_len = sizeof(struct ipv6hdr);
d7bf2ebe
THJ
6821 break;
6822 default:
f7c917ba 6823 return 0;
d7bf2ebe 6824 }
f7c917ba 6825
6826 if (skb_headlen(skb) < iphdr_len)
6827 return 0;
6828
6829 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
6830 return 0;
6831
6832 return INET_ECN_set_ce(skb);
6833}
6834
fada7fdc
JL
6835bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6836 struct bpf_insn_access_aux *info)
6837{
6838 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
6839 return false;
6840
6841 if (off % size != 0)
6842 return false;
6843
6844 switch (off) {
6845 default:
6846 return size == sizeof(__u32);
6847 }
6848}
6849
6850u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
6851 const struct bpf_insn *si,
6852 struct bpf_insn *insn_buf,
6853 struct bpf_prog *prog, u32 *target_size)
6854{
6855 struct bpf_insn *insn = insn_buf;
6856
6857#define BPF_XDP_SOCK_GET(FIELD) \
6858 do { \
c593642c
PB
6859 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
6860 sizeof_field(struct bpf_xdp_sock, FIELD)); \
fada7fdc
JL
6861 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
6862 si->dst_reg, si->src_reg, \
6863 offsetof(struct xdp_sock, FIELD)); \
6864 } while (0)
6865
6866 switch (si->off) {
6867 case offsetof(struct bpf_xdp_sock, queue_id):
6868 BPF_XDP_SOCK_GET(queue_id);
6869 break;
6870 }
6871
6872 return insn - insn_buf;
6873}
6874
f7c917ba 6875static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
6876 .func = bpf_skb_ecn_set_ce,
6877 .gpl_only = false,
6878 .ret_type = RET_INTEGER,
6879 .arg1_type = ARG_PTR_TO_CTX,
6880};
39904084
LB
6881
6882BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
6883 struct tcphdr *, th, u32, th_len)
6884{
6885#ifdef CONFIG_SYN_COOKIES
6886 u32 cookie;
6887 int ret;
6888
c0df236e 6889 if (unlikely(!sk || th_len < sizeof(*th)))
39904084
LB
6890 return -EINVAL;
6891
6892 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
6893 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
6894 return -EINVAL;
6895
6896 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
6897 return -EINVAL;
6898
6899 if (!th->ack || th->rst || th->syn)
6900 return -ENOENT;
6901
6902 if (tcp_synq_no_recent_overflow(sk))
6903 return -ENOENT;
6904
6905 cookie = ntohl(th->ack_seq) - 1;
6906
6907 switch (sk->sk_family) {
6908 case AF_INET:
6909 if (unlikely(iph_len < sizeof(struct iphdr)))
6910 return -EINVAL;
6911
6912 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
6913 break;
6914
6915#if IS_BUILTIN(CONFIG_IPV6)
6916 case AF_INET6:
6917 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
6918 return -EINVAL;
6919
6920 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
6921 break;
6922#endif /* CONFIG_IPV6 */
6923
6924 default:
6925 return -EPROTONOSUPPORT;
6926 }
6927
6928 if (ret > 0)
6929 return 0;
6930
6931 return -ENOENT;
6932#else
6933 return -ENOTSUPP;
6934#endif
6935}
6936
6937static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
6938 .func = bpf_tcp_check_syncookie,
6939 .gpl_only = true,
6940 .pkt_access = true,
6941 .ret_type = RET_INTEGER,
c0df236e 6942 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
216e3cd2 6943 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
39904084 6944 .arg3_type = ARG_CONST_SIZE,
216e3cd2 6945 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
39904084
LB
6946 .arg5_type = ARG_CONST_SIZE,
6947};
6948
70d66244
PP
6949BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
6950 struct tcphdr *, th, u32, th_len)
6951{
6952#ifdef CONFIG_SYN_COOKIES
6953 u32 cookie;
6954 u16 mss;
6955
c0df236e 6956 if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4))
70d66244
PP
6957 return -EINVAL;
6958
6959 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
6960 return -EINVAL;
6961
6962 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
6963 return -ENOENT;
6964
6965 if (!th->syn || th->ack || th->fin || th->rst)
6966 return -EINVAL;
6967
6968 if (unlikely(iph_len < sizeof(struct iphdr)))
6969 return -EINVAL;
6970
6971 /* Both struct iphdr and struct ipv6hdr have the version field at the
6972 * same offset so we can cast to the shorter header (struct iphdr).
6973 */
6974 switch (((struct iphdr *)iph)->version) {
6975 case 4:
6976 if (sk->sk_family == AF_INET6 && sk->sk_ipv6only)
6977 return -EINVAL;
6978
6979 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
6980 break;
6981
6982#if IS_BUILTIN(CONFIG_IPV6)
6983 case 6:
6984 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
6985 return -EINVAL;
6986
6987 if (sk->sk_family != AF_INET6)
6988 return -EINVAL;
6989
6990 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
6991 break;
6992#endif /* CONFIG_IPV6 */
6993
6994 default:
6995 return -EPROTONOSUPPORT;
6996 }
0741be35 6997 if (mss == 0)
70d66244
PP
6998 return -ENOENT;
6999
7000 return cookie | ((u64)mss << 32);
7001#else
7002 return -EOPNOTSUPP;
7003#endif /* CONFIG_SYN_COOKIES */
7004}
7005
7006static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
7007 .func = bpf_tcp_gen_syncookie,
7008 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
7009 .pkt_access = true,
7010 .ret_type = RET_INTEGER,
c0df236e 7011 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
216e3cd2 7012 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
70d66244 7013 .arg3_type = ARG_CONST_SIZE,
216e3cd2 7014 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
70d66244
PP
7015 .arg5_type = ARG_CONST_SIZE,
7016};
7017
cf7fbe66
JS
7018BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
7019{
27e5203b 7020 if (!sk || flags != 0)
cf7fbe66
JS
7021 return -EINVAL;
7022 if (!skb_at_tc_ingress(skb))
7023 return -EOPNOTSUPP;
7024 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
7025 return -ENETUNREACH;
8e368dc7 7026 if (unlikely(sk_fullsock(sk) && sk->sk_reuseport))
cf7fbe66 7027 return -ESOCKTNOSUPPORT;
7ae215d2
JS
7028 if (sk_is_refcounted(sk) &&
7029 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
cf7fbe66
JS
7030 return -ENOENT;
7031
7032 skb_orphan(skb);
7033 skb->sk = sk;
7034 skb->destructor = sock_pfree;
7035
7036 return 0;
7037}
7038
7039static const struct bpf_func_proto bpf_sk_assign_proto = {
7040 .func = bpf_sk_assign,
7041 .gpl_only = false,
7042 .ret_type = RET_INTEGER,
7043 .arg1_type = ARG_PTR_TO_CTX,
27e5203b 7044 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
cf7fbe66
JS
7045 .arg3_type = ARG_ANYTHING,
7046};
7047
0813a841
MKL
7048static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,
7049 u8 search_kind, const u8 *magic,
7050 u8 magic_len, bool *eol)
7051{
7052 u8 kind, kind_len;
7053
7054 *eol = false;
7055
7056 while (op < opend) {
7057 kind = op[0];
7058
7059 if (kind == TCPOPT_EOL) {
7060 *eol = true;
7061 return ERR_PTR(-ENOMSG);
7062 } else if (kind == TCPOPT_NOP) {
7063 op++;
7064 continue;
7065 }
7066
7067 if (opend - op < 2 || opend - op < op[1] || op[1] < 2)
7068 /* Something is wrong in the received header.
7069 * Follow the TCP stack's tcp_parse_options()
7070 * and just bail here.
7071 */
7072 return ERR_PTR(-EFAULT);
7073
7074 kind_len = op[1];
7075 if (search_kind == kind) {
7076 if (!magic_len)
7077 return op;
7078
7079 if (magic_len > kind_len - 2)
7080 return ERR_PTR(-ENOMSG);
7081
7082 if (!memcmp(&op[2], magic, magic_len))
7083 return op;
7084 }
7085
7086 op += kind_len;
7087 }
7088
7089 return ERR_PTR(-ENOMSG);
7090}
7091
7092BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7093 void *, search_res, u32, len, u64, flags)
7094{
7095 bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN;
7096 const u8 *op, *opend, *magic, *search = search_res;
7097 u8 search_kind, search_len, copy_len, magic_len;
7098 int ret;
7099
7100 /* 2 byte is the minimal option len except TCPOPT_NOP and
7101 * TCPOPT_EOL which are useless for the bpf prog to learn
7102 * and this helper disallow loading them also.
7103 */
7104 if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN)
7105 return -EINVAL;
7106
7107 search_kind = search[0];
7108 search_len = search[1];
7109
7110 if (search_len > len || search_kind == TCPOPT_NOP ||
7111 search_kind == TCPOPT_EOL)
7112 return -EINVAL;
7113
7114 if (search_kind == TCPOPT_EXP || search_kind == 253) {
7115 /* 16 or 32 bit magic. +2 for kind and kind length */
7116 if (search_len != 4 && search_len != 6)
7117 return -EINVAL;
7118 magic = &search[2];
7119 magic_len = search_len - 2;
7120 } else {
7121 if (search_len)
7122 return -EINVAL;
7123 magic = NULL;
7124 magic_len = 0;
7125 }
7126
7127 if (load_syn) {
7128 ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op);
7129 if (ret < 0)
7130 return ret;
7131
7132 opend = op + ret;
7133 op += sizeof(struct tcphdr);
7134 } else {
7135 if (!bpf_sock->skb ||
7136 bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7137 /* This bpf_sock->op cannot call this helper */
7138 return -EPERM;
7139
7140 opend = bpf_sock->skb_data_end;
7141 op = bpf_sock->skb->data + sizeof(struct tcphdr);
7142 }
7143
7144 op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len,
7145 &eol);
7146 if (IS_ERR(op))
7147 return PTR_ERR(op);
7148
7149 copy_len = op[1];
7150 ret = copy_len;
7151 if (copy_len > len) {
7152 ret = -ENOSPC;
7153 copy_len = len;
7154 }
7155
7156 memcpy(search_res, op, copy_len);
7157 return ret;
7158}
7159
7160static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
7161 .func = bpf_sock_ops_load_hdr_opt,
7162 .gpl_only = false,
7163 .ret_type = RET_INTEGER,
7164 .arg1_type = ARG_PTR_TO_CTX,
7165 .arg2_type = ARG_PTR_TO_MEM,
7166 .arg3_type = ARG_CONST_SIZE,
7167 .arg4_type = ARG_ANYTHING,
7168};
7169
7170BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7171 const void *, from, u32, len, u64, flags)
7172{
7173 u8 new_kind, new_kind_len, magic_len = 0, *opend;
7174 const u8 *op, *new_op, *magic = NULL;
7175 struct sk_buff *skb;
7176 bool eol;
7177
7178 if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
7179 return -EPERM;
7180
7181 if (len < 2 || flags)
7182 return -EINVAL;
7183
7184 new_op = from;
7185 new_kind = new_op[0];
7186 new_kind_len = new_op[1];
7187
7188 if (new_kind_len > len || new_kind == TCPOPT_NOP ||
7189 new_kind == TCPOPT_EOL)
7190 return -EINVAL;
7191
7192 if (new_kind_len > bpf_sock->remaining_opt_len)
7193 return -ENOSPC;
7194
7195 /* 253 is another experimental kind */
7196 if (new_kind == TCPOPT_EXP || new_kind == 253) {
7197 if (new_kind_len < 4)
7198 return -EINVAL;
7199 /* Match for the 2 byte magic also.
7200 * RFC 6994: the magic could be 2 or 4 bytes.
7201 * Hence, matching by 2 byte only is on the
7202 * conservative side but it is the right
7203 * thing to do for the 'search-for-duplication'
7204 * purpose.
7205 */
7206 magic = &new_op[2];
7207 magic_len = 2;
7208 }
7209
7210 /* Check for duplication */
7211 skb = bpf_sock->skb;
7212 op = skb->data + sizeof(struct tcphdr);
7213 opend = bpf_sock->skb_data_end;
7214
7215 op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len,
7216 &eol);
7217 if (!IS_ERR(op))
7218 return -EEXIST;
7219
7220 if (PTR_ERR(op) != -ENOMSG)
7221 return PTR_ERR(op);
7222
7223 if (eol)
7224 /* The option has been ended. Treat it as no more
7225 * header option can be written.
7226 */
7227 return -ENOSPC;
7228
7229 /* No duplication found. Store the header option. */
7230 memcpy(opend, from, new_kind_len);
7231
7232 bpf_sock->remaining_opt_len -= new_kind_len;
7233 bpf_sock->skb_data_end += new_kind_len;
7234
7235 return 0;
7236}
7237
7238static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {
7239 .func = bpf_sock_ops_store_hdr_opt,
7240 .gpl_only = false,
7241 .ret_type = RET_INTEGER,
7242 .arg1_type = ARG_PTR_TO_CTX,
216e3cd2 7243 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
0813a841
MKL
7244 .arg3_type = ARG_CONST_SIZE,
7245 .arg4_type = ARG_ANYTHING,
7246};
7247
7248BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7249 u32, len, u64, flags)
7250{
7251 if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7252 return -EPERM;
7253
7254 if (flags || len < 2)
7255 return -EINVAL;
7256
7257 if (len > bpf_sock->remaining_opt_len)
7258 return -ENOSPC;
7259
7260 bpf_sock->remaining_opt_len -= len;
7261
7262 return 0;
7263}
7264
7265static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = {
7266 .func = bpf_sock_ops_reserve_hdr_opt,
7267 .gpl_only = false,
7268 .ret_type = RET_INTEGER,
7269 .arg1_type = ARG_PTR_TO_CTX,
7270 .arg2_type = ARG_ANYTHING,
7271 .arg3_type = ARG_ANYTHING,
7272};
7273
df3f94a0 7274#endif /* CONFIG_INET */
6acc9b43 7275
fe94cc29
MX
7276bool bpf_helper_changes_pkt_data(void *func)
7277{
7278 if (func == bpf_skb_vlan_push ||
7279 func == bpf_skb_vlan_pop ||
7280 func == bpf_skb_store_bytes ||
7281 func == bpf_skb_change_proto ||
7282 func == bpf_skb_change_head ||
0ea488ff 7283 func == sk_skb_change_head ||
fe94cc29 7284 func == bpf_skb_change_tail ||
0ea488ff 7285 func == sk_skb_change_tail ||
fe94cc29 7286 func == bpf_skb_adjust_room ||
18ebe16d 7287 func == sk_skb_adjust_room ||
fe94cc29 7288 func == bpf_skb_pull_data ||
0ea488ff 7289 func == sk_skb_pull_data ||
fe94cc29
MX
7290 func == bpf_clone_redirect ||
7291 func == bpf_l3_csum_replace ||
7292 func == bpf_l4_csum_replace ||
7293 func == bpf_xdp_adjust_head ||
7294 func == bpf_xdp_adjust_meta ||
7295 func == bpf_msg_pull_data ||
6fff607e 7296 func == bpf_msg_push_data ||
7246d8ed 7297 func == bpf_msg_pop_data ||
fe94cc29 7298 func == bpf_xdp_adjust_tail ||
61d76980 7299#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
fe94cc29
MX
7300 func == bpf_lwt_seg6_store_bytes ||
7301 func == bpf_lwt_seg6_adjust_srh ||
61d76980 7302 func == bpf_lwt_seg6_action ||
0813a841
MKL
7303#endif
7304#ifdef CONFIG_INET
7305 func == bpf_sock_ops_store_hdr_opt ||
61d76980 7306#endif
3e0bd37c
PO
7307 func == bpf_lwt_in_push_encap ||
7308 func == bpf_lwt_xmit_push_encap)
fe94cc29
MX
7309 return true;
7310
7311 return false;
7312}
7313
6890896b 7314const struct bpf_func_proto bpf_event_output_data_proto __weak;
f7c6cb1d 7315const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak;
89aa0758 7316
ae2cf1c4 7317static const struct bpf_func_proto *
5e43f899 7318sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
ae2cf1c4
DA
7319{
7320 switch (func_id) {
7321 /* inet and inet6 sockets are created in a process
7322 * context so there is always a valid uid/gid
7323 */
7324 case BPF_FUNC_get_current_uid_gid:
7325 return &bpf_get_current_uid_gid_proto;
cd339431
RG
7326 case BPF_FUNC_get_local_storage:
7327 return &bpf_get_local_storage_proto;
0e53d9e5
DB
7328 case BPF_FUNC_get_socket_cookie:
7329 return &bpf_get_socket_cookie_sock_proto;
f318903c
DB
7330 case BPF_FUNC_get_netns_cookie:
7331 return &bpf_get_netns_cookie_sock_proto;
fcf752ea
DB
7332 case BPF_FUNC_perf_event_output:
7333 return &bpf_event_output_data_proto;
834ebca8
DB
7334 case BPF_FUNC_get_current_pid_tgid:
7335 return &bpf_get_current_pid_tgid_proto;
7336 case BPF_FUNC_get_current_comm:
7337 return &bpf_get_current_comm_proto;
0f09abd1
DB
7338#ifdef CONFIG_CGROUPS
7339 case BPF_FUNC_get_current_cgroup_id:
7340 return &bpf_get_current_cgroup_id_proto;
7341 case BPF_FUNC_get_current_ancestor_cgroup_id:
7342 return &bpf_get_current_ancestor_cgroup_id_proto;
7343#endif
5a52ae4e
DB
7344#ifdef CONFIG_CGROUP_NET_CLASSID
7345 case BPF_FUNC_get_cgroup_classid:
7346 return &bpf_get_cgroup_classid_curr_proto;
7347#endif
f7c6cb1d
SF
7348 case BPF_FUNC_sk_storage_get:
7349 return &bpf_sk_storage_get_cg_sock_proto;
5e0bc308
DB
7350 case BPF_FUNC_ktime_get_coarse_ns:
7351 return &bpf_ktime_get_coarse_ns_proto;
ae2cf1c4
DA
7352 default:
7353 return bpf_base_func_proto(func_id);
7354 }
7355}
7356
4fbac77d
AI
7357static const struct bpf_func_proto *
7358sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7359{
7360 switch (func_id) {
7361 /* inet and inet6 sockets are created in a process
7362 * context so there is always a valid uid/gid
7363 */
7364 case BPF_FUNC_get_current_uid_gid:
7365 return &bpf_get_current_uid_gid_proto;
d74bad4e
AI
7366 case BPF_FUNC_bind:
7367 switch (prog->expected_attach_type) {
7368 case BPF_CGROUP_INET4_CONNECT:
7369 case BPF_CGROUP_INET6_CONNECT:
7370 return &bpf_bind_proto;
7371 default:
7372 return NULL;
7373 }
d692f113
AI
7374 case BPF_FUNC_get_socket_cookie:
7375 return &bpf_get_socket_cookie_sock_addr_proto;
f318903c
DB
7376 case BPF_FUNC_get_netns_cookie:
7377 return &bpf_get_netns_cookie_sock_addr_proto;
cd339431
RG
7378 case BPF_FUNC_get_local_storage:
7379 return &bpf_get_local_storage_proto;
fcf752ea
DB
7380 case BPF_FUNC_perf_event_output:
7381 return &bpf_event_output_data_proto;
834ebca8
DB
7382 case BPF_FUNC_get_current_pid_tgid:
7383 return &bpf_get_current_pid_tgid_proto;
7384 case BPF_FUNC_get_current_comm:
7385 return &bpf_get_current_comm_proto;
0f09abd1
DB
7386#ifdef CONFIG_CGROUPS
7387 case BPF_FUNC_get_current_cgroup_id:
7388 return &bpf_get_current_cgroup_id_proto;
7389 case BPF_FUNC_get_current_ancestor_cgroup_id:
7390 return &bpf_get_current_ancestor_cgroup_id_proto;
7391#endif
5a52ae4e
DB
7392#ifdef CONFIG_CGROUP_NET_CLASSID
7393 case BPF_FUNC_get_cgroup_classid:
7394 return &bpf_get_cgroup_classid_curr_proto;
7395#endif
6c49e65e
AI
7396#ifdef CONFIG_INET
7397 case BPF_FUNC_sk_lookup_tcp:
7398 return &bpf_sock_addr_sk_lookup_tcp_proto;
7399 case BPF_FUNC_sk_lookup_udp:
7400 return &bpf_sock_addr_sk_lookup_udp_proto;
7401 case BPF_FUNC_sk_release:
7402 return &bpf_sk_release_proto;
edbf8c01
LB
7403 case BPF_FUNC_skc_lookup_tcp:
7404 return &bpf_sock_addr_skc_lookup_tcp_proto;
6c49e65e 7405#endif /* CONFIG_INET */
fb85c4a7
SF
7406 case BPF_FUNC_sk_storage_get:
7407 return &bpf_sk_storage_get_proto;
7408 case BPF_FUNC_sk_storage_delete:
7409 return &bpf_sk_storage_delete_proto;
beecf11b
SF
7410 case BPF_FUNC_setsockopt:
7411 switch (prog->expected_attach_type) {
427167c0
SF
7412 case BPF_CGROUP_INET4_BIND:
7413 case BPF_CGROUP_INET6_BIND:
beecf11b
SF
7414 case BPF_CGROUP_INET4_CONNECT:
7415 case BPF_CGROUP_INET6_CONNECT:
4c3384d7
SF
7416 case BPF_CGROUP_UDP4_RECVMSG:
7417 case BPF_CGROUP_UDP6_RECVMSG:
62476cc1
SF
7418 case BPF_CGROUP_UDP4_SENDMSG:
7419 case BPF_CGROUP_UDP6_SENDMSG:
073f4ec1
SF
7420 case BPF_CGROUP_INET4_GETPEERNAME:
7421 case BPF_CGROUP_INET6_GETPEERNAME:
7422 case BPF_CGROUP_INET4_GETSOCKNAME:
7423 case BPF_CGROUP_INET6_GETSOCKNAME:
beecf11b
SF
7424 return &bpf_sock_addr_setsockopt_proto;
7425 default:
7426 return NULL;
7427 }
7428 case BPF_FUNC_getsockopt:
7429 switch (prog->expected_attach_type) {
427167c0
SF
7430 case BPF_CGROUP_INET4_BIND:
7431 case BPF_CGROUP_INET6_BIND:
beecf11b
SF
7432 case BPF_CGROUP_INET4_CONNECT:
7433 case BPF_CGROUP_INET6_CONNECT:
4c3384d7
SF
7434 case BPF_CGROUP_UDP4_RECVMSG:
7435 case BPF_CGROUP_UDP6_RECVMSG:
62476cc1
SF
7436 case BPF_CGROUP_UDP4_SENDMSG:
7437 case BPF_CGROUP_UDP6_SENDMSG:
073f4ec1
SF
7438 case BPF_CGROUP_INET4_GETPEERNAME:
7439 case BPF_CGROUP_INET6_GETPEERNAME:
7440 case BPF_CGROUP_INET4_GETSOCKNAME:
7441 case BPF_CGROUP_INET6_GETSOCKNAME:
beecf11b
SF
7442 return &bpf_sock_addr_getsockopt_proto;
7443 default:
7444 return NULL;
7445 }
4fbac77d 7446 default:
1df8f55a 7447 return bpf_sk_base_func_proto(func_id);
4fbac77d
AI
7448 }
7449}
7450
2492d3b8 7451static const struct bpf_func_proto *
5e43f899 7452sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2492d3b8
DB
7453{
7454 switch (func_id) {
7455 case BPF_FUNC_skb_load_bytes:
7456 return &bpf_skb_load_bytes_proto;
4e1ec56c
DB
7457 case BPF_FUNC_skb_load_bytes_relative:
7458 return &bpf_skb_load_bytes_relative_proto;
91b8270f
CF
7459 case BPF_FUNC_get_socket_cookie:
7460 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
7461 case BPF_FUNC_get_socket_uid:
7462 return &bpf_get_socket_uid_proto;
7c4b90d7
AZ
7463 case BPF_FUNC_perf_event_output:
7464 return &bpf_skb_event_output_proto;
2492d3b8 7465 default:
1df8f55a 7466 return bpf_sk_base_func_proto(func_id);
2492d3b8
DB
7467 }
7468}
7469
6ac99e8f
MKL
7470const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
7471const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
7472
cd339431
RG
7473static const struct bpf_func_proto *
7474cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7475{
7476 switch (func_id) {
7477 case BPF_FUNC_get_local_storage:
7478 return &bpf_get_local_storage_proto;
46f8bc92
MKL
7479 case BPF_FUNC_sk_fullsock:
7480 return &bpf_sk_fullsock_proto;
6ac99e8f
MKL
7481 case BPF_FUNC_sk_storage_get:
7482 return &bpf_sk_storage_get_proto;
7483 case BPF_FUNC_sk_storage_delete:
7484 return &bpf_sk_storage_delete_proto;
7c4b90d7
AZ
7485 case BPF_FUNC_perf_event_output:
7486 return &bpf_skb_event_output_proto;
4ecabd55
RG
7487#ifdef CONFIG_SOCK_CGROUP_DATA
7488 case BPF_FUNC_skb_cgroup_id:
7489 return &bpf_skb_cgroup_id_proto;
06d3e4c9
AI
7490 case BPF_FUNC_skb_ancestor_cgroup_id:
7491 return &bpf_skb_ancestor_cgroup_id_proto;
f307fa2c
AI
7492 case BPF_FUNC_sk_cgroup_id:
7493 return &bpf_sk_cgroup_id_proto;
7494 case BPF_FUNC_sk_ancestor_cgroup_id:
7495 return &bpf_sk_ancestor_cgroup_id_proto;
4ecabd55 7496#endif
655a51e5 7497#ifdef CONFIG_INET
d56c2f95
AI
7498 case BPF_FUNC_sk_lookup_tcp:
7499 return &bpf_sk_lookup_tcp_proto;
7500 case BPF_FUNC_sk_lookup_udp:
7501 return &bpf_sk_lookup_udp_proto;
7502 case BPF_FUNC_sk_release:
7503 return &bpf_sk_release_proto;
7504 case BPF_FUNC_skc_lookup_tcp:
7505 return &bpf_skc_lookup_tcp_proto;
655a51e5
MKL
7506 case BPF_FUNC_tcp_sock:
7507 return &bpf_tcp_sock_proto;
dbafd7dd
MKL
7508 case BPF_FUNC_get_listener_sock:
7509 return &bpf_get_listener_sock_proto;
f7c917ba 7510 case BPF_FUNC_skb_ecn_set_ce:
7511 return &bpf_skb_ecn_set_ce_proto;
655a51e5 7512#endif
cd339431
RG
7513 default:
7514 return sk_filter_func_proto(func_id, prog);
7515 }
7516}
7517
608cd71a 7518static const struct bpf_func_proto *
5e43f899 7519tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
608cd71a
AS
7520{
7521 switch (func_id) {
7522 case BPF_FUNC_skb_store_bytes:
7523 return &bpf_skb_store_bytes_proto;
05c74e5e
DB
7524 case BPF_FUNC_skb_load_bytes:
7525 return &bpf_skb_load_bytes_proto;
4e1ec56c
DB
7526 case BPF_FUNC_skb_load_bytes_relative:
7527 return &bpf_skb_load_bytes_relative_proto;
36bbef52
DB
7528 case BPF_FUNC_skb_pull_data:
7529 return &bpf_skb_pull_data_proto;
7d672345
DB
7530 case BPF_FUNC_csum_diff:
7531 return &bpf_csum_diff_proto;
36bbef52
DB
7532 case BPF_FUNC_csum_update:
7533 return &bpf_csum_update_proto;
7cdec54f
DB
7534 case BPF_FUNC_csum_level:
7535 return &bpf_csum_level_proto;
91bc4822
AS
7536 case BPF_FUNC_l3_csum_replace:
7537 return &bpf_l3_csum_replace_proto;
7538 case BPF_FUNC_l4_csum_replace:
7539 return &bpf_l4_csum_replace_proto;
3896d655
AS
7540 case BPF_FUNC_clone_redirect:
7541 return &bpf_clone_redirect_proto;
8d20aabe
DB
7542 case BPF_FUNC_get_cgroup_classid:
7543 return &bpf_get_cgroup_classid_proto;
4e10df9a
AS
7544 case BPF_FUNC_skb_vlan_push:
7545 return &bpf_skb_vlan_push_proto;
7546 case BPF_FUNC_skb_vlan_pop:
7547 return &bpf_skb_vlan_pop_proto;
6578171a
DB
7548 case BPF_FUNC_skb_change_proto:
7549 return &bpf_skb_change_proto_proto;
d2485c42
DB
7550 case BPF_FUNC_skb_change_type:
7551 return &bpf_skb_change_type_proto;
2be7e212
DB
7552 case BPF_FUNC_skb_adjust_room:
7553 return &bpf_skb_adjust_room_proto;
5293efe6
DB
7554 case BPF_FUNC_skb_change_tail:
7555 return &bpf_skb_change_tail_proto;
6f3f65d8
LC
7556 case BPF_FUNC_skb_change_head:
7557 return &bpf_skb_change_head_proto;
d3aa45ce
AS
7558 case BPF_FUNC_skb_get_tunnel_key:
7559 return &bpf_skb_get_tunnel_key_proto;
7560 case BPF_FUNC_skb_set_tunnel_key:
14ca0751
DB
7561 return bpf_get_skb_set_tunnel_proto(func_id);
7562 case BPF_FUNC_skb_get_tunnel_opt:
7563 return &bpf_skb_get_tunnel_opt_proto;
7564 case BPF_FUNC_skb_set_tunnel_opt:
7565 return bpf_get_skb_set_tunnel_proto(func_id);
27b29f63
AS
7566 case BPF_FUNC_redirect:
7567 return &bpf_redirect_proto;
b4ab3141
DB
7568 case BPF_FUNC_redirect_neigh:
7569 return &bpf_redirect_neigh_proto;
9aa1206e
DB
7570 case BPF_FUNC_redirect_peer:
7571 return &bpf_redirect_peer_proto;
c46646d0
DB
7572 case BPF_FUNC_get_route_realm:
7573 return &bpf_get_route_realm_proto;
13c5c240
DB
7574 case BPF_FUNC_get_hash_recalc:
7575 return &bpf_get_hash_recalc_proto;
7a4b28c6
DB
7576 case BPF_FUNC_set_hash_invalid:
7577 return &bpf_set_hash_invalid_proto;
ded092cd
DB
7578 case BPF_FUNC_set_hash:
7579 return &bpf_set_hash_proto;
bd570ff9 7580 case BPF_FUNC_perf_event_output:
555c8a86 7581 return &bpf_skb_event_output_proto;
80b48c44
DB
7582 case BPF_FUNC_get_smp_processor_id:
7583 return &bpf_get_smp_processor_id_proto;
747ea55e
DB
7584 case BPF_FUNC_skb_under_cgroup:
7585 return &bpf_skb_under_cgroup_proto;
91b8270f
CF
7586 case BPF_FUNC_get_socket_cookie:
7587 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
7588 case BPF_FUNC_get_socket_uid:
7589 return &bpf_get_socket_uid_proto;
cb20b08e
DB
7590 case BPF_FUNC_fib_lookup:
7591 return &bpf_skb_fib_lookup_proto;
34b2021c
JDB
7592 case BPF_FUNC_check_mtu:
7593 return &bpf_skb_check_mtu_proto;
46f8bc92
MKL
7594 case BPF_FUNC_sk_fullsock:
7595 return &bpf_sk_fullsock_proto;
6ac99e8f
MKL
7596 case BPF_FUNC_sk_storage_get:
7597 return &bpf_sk_storage_get_proto;
7598 case BPF_FUNC_sk_storage_delete:
7599 return &bpf_sk_storage_delete_proto;
12bed760
EB
7600#ifdef CONFIG_XFRM
7601 case BPF_FUNC_skb_get_xfrm_state:
7602 return &bpf_skb_get_xfrm_state_proto;
7603#endif
b426ce83
DB
7604#ifdef CONFIG_CGROUP_NET_CLASSID
7605 case BPF_FUNC_skb_cgroup_classid:
7606 return &bpf_skb_cgroup_classid_proto;
7607#endif
cb20b08e
DB
7608#ifdef CONFIG_SOCK_CGROUP_DATA
7609 case BPF_FUNC_skb_cgroup_id:
7610 return &bpf_skb_cgroup_id_proto;
77236281
AI
7611 case BPF_FUNC_skb_ancestor_cgroup_id:
7612 return &bpf_skb_ancestor_cgroup_id_proto;
cb20b08e 7613#endif
df3f94a0 7614#ifdef CONFIG_INET
6acc9b43
JS
7615 case BPF_FUNC_sk_lookup_tcp:
7616 return &bpf_sk_lookup_tcp_proto;
7617 case BPF_FUNC_sk_lookup_udp:
7618 return &bpf_sk_lookup_udp_proto;
7619 case BPF_FUNC_sk_release:
7620 return &bpf_sk_release_proto;
655a51e5
MKL
7621 case BPF_FUNC_tcp_sock:
7622 return &bpf_tcp_sock_proto;
dbafd7dd
MKL
7623 case BPF_FUNC_get_listener_sock:
7624 return &bpf_get_listener_sock_proto;
edbf8c01
LB
7625 case BPF_FUNC_skc_lookup_tcp:
7626 return &bpf_skc_lookup_tcp_proto;
39904084
LB
7627 case BPF_FUNC_tcp_check_syncookie:
7628 return &bpf_tcp_check_syncookie_proto;
315a2029
PO
7629 case BPF_FUNC_skb_ecn_set_ce:
7630 return &bpf_skb_ecn_set_ce_proto;
70d66244
PP
7631 case BPF_FUNC_tcp_gen_syncookie:
7632 return &bpf_tcp_gen_syncookie_proto;
cf7fbe66
JS
7633 case BPF_FUNC_sk_assign:
7634 return &bpf_sk_assign_proto;
df3f94a0 7635#endif
608cd71a 7636 default:
1df8f55a 7637 return bpf_sk_base_func_proto(func_id);
608cd71a
AS
7638 }
7639}
7640
6a773a15 7641static const struct bpf_func_proto *
5e43f899 7642xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6a773a15 7643{
4de16969
DB
7644 switch (func_id) {
7645 case BPF_FUNC_perf_event_output:
7646 return &bpf_xdp_event_output_proto;
669dc4d7
DB
7647 case BPF_FUNC_get_smp_processor_id:
7648 return &bpf_get_smp_processor_id_proto;
205c3807
DB
7649 case BPF_FUNC_csum_diff:
7650 return &bpf_csum_diff_proto;
17bedab2
MKL
7651 case BPF_FUNC_xdp_adjust_head:
7652 return &bpf_xdp_adjust_head_proto;
de8f3a83
DB
7653 case BPF_FUNC_xdp_adjust_meta:
7654 return &bpf_xdp_adjust_meta_proto;
814abfab
JF
7655 case BPF_FUNC_redirect:
7656 return &bpf_xdp_redirect_proto;
97f91a7c 7657 case BPF_FUNC_redirect_map:
e4a8e817 7658 return &bpf_xdp_redirect_map_proto;
b32cc5b9
NS
7659 case BPF_FUNC_xdp_adjust_tail:
7660 return &bpf_xdp_adjust_tail_proto;
0165cc81
LB
7661 case BPF_FUNC_xdp_get_buff_len:
7662 return &bpf_xdp_get_buff_len_proto;
87f5fc7e
DA
7663 case BPF_FUNC_fib_lookup:
7664 return &bpf_xdp_fib_lookup_proto;
34b2021c
JDB
7665 case BPF_FUNC_check_mtu:
7666 return &bpf_xdp_check_mtu_proto;
c8123ead
NH
7667#ifdef CONFIG_INET
7668 case BPF_FUNC_sk_lookup_udp:
7669 return &bpf_xdp_sk_lookup_udp_proto;
7670 case BPF_FUNC_sk_lookup_tcp:
7671 return &bpf_xdp_sk_lookup_tcp_proto;
7672 case BPF_FUNC_sk_release:
7673 return &bpf_sk_release_proto;
edbf8c01
LB
7674 case BPF_FUNC_skc_lookup_tcp:
7675 return &bpf_xdp_skc_lookup_tcp_proto;
39904084
LB
7676 case BPF_FUNC_tcp_check_syncookie:
7677 return &bpf_tcp_check_syncookie_proto;
70d66244
PP
7678 case BPF_FUNC_tcp_gen_syncookie:
7679 return &bpf_tcp_gen_syncookie_proto;
c8123ead 7680#endif
4de16969 7681 default:
1df8f55a 7682 return bpf_sk_base_func_proto(func_id);
4de16969 7683 }
6a773a15
BB
7684}
7685
604326b4
DB
7686const struct bpf_func_proto bpf_sock_map_update_proto __weak;
7687const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
7688
8c4b4c7e 7689static const struct bpf_func_proto *
5e43f899 7690sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8c4b4c7e
LB
7691{
7692 switch (func_id) {
7693 case BPF_FUNC_setsockopt:
beecf11b 7694 return &bpf_sock_ops_setsockopt_proto;
cd86d1fd 7695 case BPF_FUNC_getsockopt:
beecf11b 7696 return &bpf_sock_ops_getsockopt_proto;
b13d8807
LB
7697 case BPF_FUNC_sock_ops_cb_flags_set:
7698 return &bpf_sock_ops_cb_flags_set_proto;
174a79ff
JF
7699 case BPF_FUNC_sock_map_update:
7700 return &bpf_sock_map_update_proto;
81110384
JF
7701 case BPF_FUNC_sock_hash_update:
7702 return &bpf_sock_hash_update_proto;
d692f113
AI
7703 case BPF_FUNC_get_socket_cookie:
7704 return &bpf_get_socket_cookie_sock_ops_proto;
cd339431
RG
7705 case BPF_FUNC_get_local_storage:
7706 return &bpf_get_local_storage_proto;
a5a3a828 7707 case BPF_FUNC_perf_event_output:
fcf752ea 7708 return &bpf_event_output_data_proto;
1314ef56
SF
7709 case BPF_FUNC_sk_storage_get:
7710 return &bpf_sk_storage_get_proto;
7711 case BPF_FUNC_sk_storage_delete:
7712 return &bpf_sk_storage_delete_proto;
6cf1770d
XL
7713 case BPF_FUNC_get_netns_cookie:
7714 return &bpf_get_netns_cookie_sock_ops_proto;
1314ef56 7715#ifdef CONFIG_INET
0813a841
MKL
7716 case BPF_FUNC_load_hdr_opt:
7717 return &bpf_sock_ops_load_hdr_opt_proto;
7718 case BPF_FUNC_store_hdr_opt:
7719 return &bpf_sock_ops_store_hdr_opt_proto;
7720 case BPF_FUNC_reserve_hdr_opt:
7721 return &bpf_sock_ops_reserve_hdr_opt_proto;
1314ef56
SF
7722 case BPF_FUNC_tcp_sock:
7723 return &bpf_tcp_sock_proto;
7724#endif /* CONFIG_INET */
8c4b4c7e 7725 default:
1df8f55a 7726 return bpf_sk_base_func_proto(func_id);
8c4b4c7e
LB
7727 }
7728}
7729
604326b4
DB
7730const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
7731const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
7732
5e43f899
AI
7733static const struct bpf_func_proto *
7734sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4f738adb
JF
7735{
7736 switch (func_id) {
7737 case BPF_FUNC_msg_redirect_map:
7738 return &bpf_msg_redirect_map_proto;
81110384
JF
7739 case BPF_FUNC_msg_redirect_hash:
7740 return &bpf_msg_redirect_hash_proto;
2a100317
JF
7741 case BPF_FUNC_msg_apply_bytes:
7742 return &bpf_msg_apply_bytes_proto;
91843d54
JF
7743 case BPF_FUNC_msg_cork_bytes:
7744 return &bpf_msg_cork_bytes_proto;
015632bb
JF
7745 case BPF_FUNC_msg_pull_data:
7746 return &bpf_msg_pull_data_proto;
6fff607e
JF
7747 case BPF_FUNC_msg_push_data:
7748 return &bpf_msg_push_data_proto;
7246d8ed
JF
7749 case BPF_FUNC_msg_pop_data:
7750 return &bpf_msg_pop_data_proto;
abe3cac8
JF
7751 case BPF_FUNC_perf_event_output:
7752 return &bpf_event_output_data_proto;
7753 case BPF_FUNC_get_current_uid_gid:
7754 return &bpf_get_current_uid_gid_proto;
7755 case BPF_FUNC_get_current_pid_tgid:
7756 return &bpf_get_current_pid_tgid_proto;
13d70f5a
JF
7757 case BPF_FUNC_sk_storage_get:
7758 return &bpf_sk_storage_get_proto;
7759 case BPF_FUNC_sk_storage_delete:
7760 return &bpf_sk_storage_delete_proto;
fab60e29
XL
7761 case BPF_FUNC_get_netns_cookie:
7762 return &bpf_get_netns_cookie_sk_msg_proto;
abe3cac8
JF
7763#ifdef CONFIG_CGROUPS
7764 case BPF_FUNC_get_current_cgroup_id:
7765 return &bpf_get_current_cgroup_id_proto;
7766 case BPF_FUNC_get_current_ancestor_cgroup_id:
7767 return &bpf_get_current_ancestor_cgroup_id_proto;
7768#endif
7769#ifdef CONFIG_CGROUP_NET_CLASSID
7770 case BPF_FUNC_get_cgroup_classid:
7771 return &bpf_get_cgroup_classid_curr_proto;
7772#endif
4f738adb 7773 default:
1df8f55a 7774 return bpf_sk_base_func_proto(func_id);
4f738adb
JF
7775 }
7776}
7777
604326b4
DB
7778const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
7779const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
7780
5e43f899
AI
7781static const struct bpf_func_proto *
7782sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
b005fd18
JF
7783{
7784 switch (func_id) {
8a31db56
JF
7785 case BPF_FUNC_skb_store_bytes:
7786 return &bpf_skb_store_bytes_proto;
b005fd18
JF
7787 case BPF_FUNC_skb_load_bytes:
7788 return &bpf_skb_load_bytes_proto;
8a31db56 7789 case BPF_FUNC_skb_pull_data:
0ea488ff 7790 return &sk_skb_pull_data_proto;
8a31db56 7791 case BPF_FUNC_skb_change_tail:
0ea488ff 7792 return &sk_skb_change_tail_proto;
8a31db56 7793 case BPF_FUNC_skb_change_head:
0ea488ff 7794 return &sk_skb_change_head_proto;
18ebe16d
JF
7795 case BPF_FUNC_skb_adjust_room:
7796 return &sk_skb_adjust_room_proto;
b005fd18
JF
7797 case BPF_FUNC_get_socket_cookie:
7798 return &bpf_get_socket_cookie_proto;
7799 case BPF_FUNC_get_socket_uid:
7800 return &bpf_get_socket_uid_proto;
174a79ff
JF
7801 case BPF_FUNC_sk_redirect_map:
7802 return &bpf_sk_redirect_map_proto;
81110384
JF
7803 case BPF_FUNC_sk_redirect_hash:
7804 return &bpf_sk_redirect_hash_proto;
7c4b90d7
AZ
7805 case BPF_FUNC_perf_event_output:
7806 return &bpf_skb_event_output_proto;
df3f94a0 7807#ifdef CONFIG_INET
6acc9b43
JS
7808 case BPF_FUNC_sk_lookup_tcp:
7809 return &bpf_sk_lookup_tcp_proto;
7810 case BPF_FUNC_sk_lookup_udp:
7811 return &bpf_sk_lookup_udp_proto;
7812 case BPF_FUNC_sk_release:
7813 return &bpf_sk_release_proto;
edbf8c01
LB
7814 case BPF_FUNC_skc_lookup_tcp:
7815 return &bpf_skc_lookup_tcp_proto;
df3f94a0 7816#endif
b005fd18 7817 default:
1df8f55a 7818 return bpf_sk_base_func_proto(func_id);
b005fd18
JF
7819 }
7820}
7821
d58e468b
PP
7822static const struct bpf_func_proto *
7823flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7824{
7825 switch (func_id) {
7826 case BPF_FUNC_skb_load_bytes:
089b19a9 7827 return &bpf_flow_dissector_load_bytes_proto;
d58e468b 7828 default:
1df8f55a 7829 return bpf_sk_base_func_proto(func_id);
d58e468b
PP
7830 }
7831}
7832
cd3092c7
MX
7833static const struct bpf_func_proto *
7834lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7835{
7836 switch (func_id) {
7837 case BPF_FUNC_skb_load_bytes:
7838 return &bpf_skb_load_bytes_proto;
7839 case BPF_FUNC_skb_pull_data:
7840 return &bpf_skb_pull_data_proto;
7841 case BPF_FUNC_csum_diff:
7842 return &bpf_csum_diff_proto;
7843 case BPF_FUNC_get_cgroup_classid:
7844 return &bpf_get_cgroup_classid_proto;
7845 case BPF_FUNC_get_route_realm:
7846 return &bpf_get_route_realm_proto;
7847 case BPF_FUNC_get_hash_recalc:
7848 return &bpf_get_hash_recalc_proto;
7849 case BPF_FUNC_perf_event_output:
7850 return &bpf_skb_event_output_proto;
7851 case BPF_FUNC_get_smp_processor_id:
7852 return &bpf_get_smp_processor_id_proto;
7853 case BPF_FUNC_skb_under_cgroup:
7854 return &bpf_skb_under_cgroup_proto;
7855 default:
1df8f55a 7856 return bpf_sk_base_func_proto(func_id);
cd3092c7
MX
7857 }
7858}
7859
7860static const struct bpf_func_proto *
7861lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7862{
7863 switch (func_id) {
7864 case BPF_FUNC_lwt_push_encap:
3e0bd37c 7865 return &bpf_lwt_in_push_encap_proto;
cd3092c7
MX
7866 default:
7867 return lwt_out_func_proto(func_id, prog);
7868 }
7869}
7870
3a0af8fd 7871static const struct bpf_func_proto *
5e43f899 7872lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
3a0af8fd
TG
7873{
7874 switch (func_id) {
7875 case BPF_FUNC_skb_get_tunnel_key:
7876 return &bpf_skb_get_tunnel_key_proto;
7877 case BPF_FUNC_skb_set_tunnel_key:
7878 return bpf_get_skb_set_tunnel_proto(func_id);
7879 case BPF_FUNC_skb_get_tunnel_opt:
7880 return &bpf_skb_get_tunnel_opt_proto;
7881 case BPF_FUNC_skb_set_tunnel_opt:
7882 return bpf_get_skb_set_tunnel_proto(func_id);
7883 case BPF_FUNC_redirect:
7884 return &bpf_redirect_proto;
7885 case BPF_FUNC_clone_redirect:
7886 return &bpf_clone_redirect_proto;
7887 case BPF_FUNC_skb_change_tail:
7888 return &bpf_skb_change_tail_proto;
7889 case BPF_FUNC_skb_change_head:
7890 return &bpf_skb_change_head_proto;
7891 case BPF_FUNC_skb_store_bytes:
7892 return &bpf_skb_store_bytes_proto;
7893 case BPF_FUNC_csum_update:
7894 return &bpf_csum_update_proto;
7cdec54f
DB
7895 case BPF_FUNC_csum_level:
7896 return &bpf_csum_level_proto;
3a0af8fd
TG
7897 case BPF_FUNC_l3_csum_replace:
7898 return &bpf_l3_csum_replace_proto;
7899 case BPF_FUNC_l4_csum_replace:
7900 return &bpf_l4_csum_replace_proto;
7901 case BPF_FUNC_set_hash_invalid:
7902 return &bpf_set_hash_invalid_proto;
3e0bd37c
PO
7903 case BPF_FUNC_lwt_push_encap:
7904 return &bpf_lwt_xmit_push_encap_proto;
3a0af8fd 7905 default:
cd3092c7 7906 return lwt_out_func_proto(func_id, prog);
3a0af8fd
TG
7907 }
7908}
7909
004d4b27
MX
7910static const struct bpf_func_proto *
7911lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7912{
7913 switch (func_id) {
61d76980 7914#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
004d4b27
MX
7915 case BPF_FUNC_lwt_seg6_store_bytes:
7916 return &bpf_lwt_seg6_store_bytes_proto;
7917 case BPF_FUNC_lwt_seg6_action:
7918 return &bpf_lwt_seg6_action_proto;
7919 case BPF_FUNC_lwt_seg6_adjust_srh:
7920 return &bpf_lwt_seg6_adjust_srh_proto;
61d76980 7921#endif
004d4b27
MX
7922 default:
7923 return lwt_out_func_proto(func_id, prog);
3a0af8fd
TG
7924 }
7925}
7926
f96da094 7927static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
5e43f899 7928 const struct bpf_prog *prog,
f96da094 7929 struct bpf_insn_access_aux *info)
23994631 7930{
f96da094 7931 const int size_default = sizeof(__u32);
23994631 7932
9bac3d6d
AS
7933 if (off < 0 || off >= sizeof(struct __sk_buff))
7934 return false;
62c7989b 7935
4936e352 7936 /* The verifier guarantees that size > 0. */
9bac3d6d
AS
7937 if (off % size != 0)
7938 return false;
62c7989b
DB
7939
7940 switch (off) {
f96da094
DB
7941 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7942 if (off + size > offsetofend(struct __sk_buff, cb[4]))
62c7989b
DB
7943 return false;
7944 break;
8a31db56
JF
7945 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
7946 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
7947 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
7948 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
f96da094 7949 case bpf_ctx_range(struct __sk_buff, data):
de8f3a83 7950 case bpf_ctx_range(struct __sk_buff, data_meta):
f96da094
DB
7951 case bpf_ctx_range(struct __sk_buff, data_end):
7952 if (size != size_default)
23994631 7953 return false;
31fd8581 7954 break;
b7df9ada 7955 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
089b19a9 7956 return false;
f64c4ace
VF
7957 case bpf_ctx_range(struct __sk_buff, hwtstamp):
7958 if (type == BPF_WRITE || size != sizeof(__u64))
7959 return false;
7960 break;
f11216b2
VD
7961 case bpf_ctx_range(struct __sk_buff, tstamp):
7962 if (size != sizeof(__u64))
7963 return false;
7964 break;
46f8bc92
MKL
7965 case offsetof(struct __sk_buff, sk):
7966 if (type == BPF_WRITE || size != sizeof(__u64))
7967 return false;
7968 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
7969 break;
f64c4ace
VF
7970 case offsetofend(struct __sk_buff, gso_size) ... offsetof(struct __sk_buff, hwtstamp) - 1:
7971 /* Explicitly prohibit access to padding in __sk_buff. */
7972 return false;
31fd8581 7973 default:
f96da094 7974 /* Only narrow read access allowed for now. */
31fd8581 7975 if (type == BPF_WRITE) {
f96da094 7976 if (size != size_default)
31fd8581
YS
7977 return false;
7978 } else {
f96da094
DB
7979 bpf_ctx_record_field_size(info, size_default);
7980 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
23994631 7981 return false;
31fd8581 7982 }
62c7989b 7983 }
9bac3d6d
AS
7984
7985 return true;
7986}
7987
d691f9e8 7988static bool sk_filter_is_valid_access(int off, int size,
19de99f7 7989 enum bpf_access_type type,
5e43f899 7990 const struct bpf_prog *prog,
23994631 7991 struct bpf_insn_access_aux *info)
d691f9e8 7992{
db58ba45 7993 switch (off) {
f96da094
DB
7994 case bpf_ctx_range(struct __sk_buff, tc_classid):
7995 case bpf_ctx_range(struct __sk_buff, data):
de8f3a83 7996 case bpf_ctx_range(struct __sk_buff, data_meta):
f96da094 7997 case bpf_ctx_range(struct __sk_buff, data_end):
8a31db56 7998 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
f11216b2 7999 case bpf_ctx_range(struct __sk_buff, tstamp):
e3da08d0 8000 case bpf_ctx_range(struct __sk_buff, wire_len):
f64c4ace 8001 case bpf_ctx_range(struct __sk_buff, hwtstamp):
045efa82 8002 return false;
db58ba45 8003 }
045efa82 8004
d691f9e8
AS
8005 if (type == BPF_WRITE) {
8006 switch (off) {
f96da094 8007 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
d691f9e8
AS
8008 break;
8009 default:
8010 return false;
8011 }
8012 }
8013
5e43f899 8014 return bpf_skb_is_valid_access(off, size, type, prog, info);
d691f9e8
AS
8015}
8016
b39b5f41
SL
8017static bool cg_skb_is_valid_access(int off, int size,
8018 enum bpf_access_type type,
8019 const struct bpf_prog *prog,
8020 struct bpf_insn_access_aux *info)
8021{
8022 switch (off) {
8023 case bpf_ctx_range(struct __sk_buff, tc_classid):
8024 case bpf_ctx_range(struct __sk_buff, data_meta):
e3da08d0 8025 case bpf_ctx_range(struct __sk_buff, wire_len):
b39b5f41 8026 return false;
ab21c1b5
DB
8027 case bpf_ctx_range(struct __sk_buff, data):
8028 case bpf_ctx_range(struct __sk_buff, data_end):
2c78ee89 8029 if (!bpf_capable())
ab21c1b5
DB
8030 return false;
8031 break;
b39b5f41 8032 }
ab21c1b5 8033
b39b5f41
SL
8034 if (type == BPF_WRITE) {
8035 switch (off) {
8036 case bpf_ctx_range(struct __sk_buff, mark):
8037 case bpf_ctx_range(struct __sk_buff, priority):
8038 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8039 break;
f11216b2 8040 case bpf_ctx_range(struct __sk_buff, tstamp):
2c78ee89 8041 if (!bpf_capable())
f11216b2
VD
8042 return false;
8043 break;
b39b5f41
SL
8044 default:
8045 return false;
8046 }
8047 }
8048
8049 switch (off) {
8050 case bpf_ctx_range(struct __sk_buff, data):
8051 info->reg_type = PTR_TO_PACKET;
8052 break;
8053 case bpf_ctx_range(struct __sk_buff, data_end):
8054 info->reg_type = PTR_TO_PACKET_END;
8055 break;
8056 }
8057
8058 return bpf_skb_is_valid_access(off, size, type, prog, info);
8059}
8060
3a0af8fd
TG
8061static bool lwt_is_valid_access(int off, int size,
8062 enum bpf_access_type type,
5e43f899 8063 const struct bpf_prog *prog,
23994631 8064 struct bpf_insn_access_aux *info)
3a0af8fd
TG
8065{
8066 switch (off) {
f96da094 8067 case bpf_ctx_range(struct __sk_buff, tc_classid):
8a31db56 8068 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
de8f3a83 8069 case bpf_ctx_range(struct __sk_buff, data_meta):
f11216b2 8070 case bpf_ctx_range(struct __sk_buff, tstamp):
e3da08d0 8071 case bpf_ctx_range(struct __sk_buff, wire_len):
f64c4ace 8072 case bpf_ctx_range(struct __sk_buff, hwtstamp):
3a0af8fd
TG
8073 return false;
8074 }
8075
8076 if (type == BPF_WRITE) {
8077 switch (off) {
f96da094
DB
8078 case bpf_ctx_range(struct __sk_buff, mark):
8079 case bpf_ctx_range(struct __sk_buff, priority):
8080 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3a0af8fd
TG
8081 break;
8082 default:
8083 return false;
8084 }
8085 }
8086
f96da094
DB
8087 switch (off) {
8088 case bpf_ctx_range(struct __sk_buff, data):
8089 info->reg_type = PTR_TO_PACKET;
8090 break;
8091 case bpf_ctx_range(struct __sk_buff, data_end):
8092 info->reg_type = PTR_TO_PACKET_END;
8093 break;
8094 }
8095
5e43f899 8096 return bpf_skb_is_valid_access(off, size, type, prog, info);
3a0af8fd
TG
8097}
8098
aac3fc32
AI
8099/* Attach type specific accesses */
8100static bool __sock_filter_check_attach_type(int off,
8101 enum bpf_access_type access_type,
8102 enum bpf_attach_type attach_type)
61023658 8103{
aac3fc32
AI
8104 switch (off) {
8105 case offsetof(struct bpf_sock, bound_dev_if):
8106 case offsetof(struct bpf_sock, mark):
8107 case offsetof(struct bpf_sock, priority):
8108 switch (attach_type) {
8109 case BPF_CGROUP_INET_SOCK_CREATE:
f5836749 8110 case BPF_CGROUP_INET_SOCK_RELEASE:
aac3fc32
AI
8111 goto full_access;
8112 default:
8113 return false;
8114 }
8115 case bpf_ctx_range(struct bpf_sock, src_ip4):
8116 switch (attach_type) {
8117 case BPF_CGROUP_INET4_POST_BIND:
8118 goto read_only;
8119 default:
8120 return false;
8121 }
8122 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8123 switch (attach_type) {
8124 case BPF_CGROUP_INET6_POST_BIND:
8125 goto read_only;
8126 default:
8127 return false;
8128 }
8129 case bpf_ctx_range(struct bpf_sock, src_port):
8130 switch (attach_type) {
8131 case BPF_CGROUP_INET4_POST_BIND:
8132 case BPF_CGROUP_INET6_POST_BIND:
8133 goto read_only;
61023658
DA
8134 default:
8135 return false;
8136 }
8137 }
aac3fc32
AI
8138read_only:
8139 return access_type == BPF_READ;
8140full_access:
8141 return true;
8142}
8143
46f8bc92
MKL
8144bool bpf_sock_common_is_valid_access(int off, int size,
8145 enum bpf_access_type type,
aac3fc32
AI
8146 struct bpf_insn_access_aux *info)
8147{
aac3fc32 8148 switch (off) {
46f8bc92
MKL
8149 case bpf_ctx_range_till(struct bpf_sock, type, priority):
8150 return false;
8151 default:
8152 return bpf_sock_is_valid_access(off, size, type, info);
aac3fc32 8153 }
aac3fc32
AI
8154}
8155
c64b7983
JS
8156bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
8157 struct bpf_insn_access_aux *info)
aac3fc32 8158{
aa65d696
MKL
8159 const int size_default = sizeof(__u32);
8160
aac3fc32 8161 if (off < 0 || off >= sizeof(struct bpf_sock))
61023658 8162 return false;
61023658
DA
8163 if (off % size != 0)
8164 return false;
aa65d696
MKL
8165
8166 switch (off) {
8167 case offsetof(struct bpf_sock, state):
8168 case offsetof(struct bpf_sock, family):
8169 case offsetof(struct bpf_sock, type):
8170 case offsetof(struct bpf_sock, protocol):
8171 case offsetof(struct bpf_sock, dst_port):
8172 case offsetof(struct bpf_sock, src_port):
c3c16f2e 8173 case offsetof(struct bpf_sock, rx_queue_mapping):
aa65d696
MKL
8174 case bpf_ctx_range(struct bpf_sock, src_ip4):
8175 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8176 case bpf_ctx_range(struct bpf_sock, dst_ip4):
8177 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
8178 bpf_ctx_record_field_size(info, size_default);
8179 return bpf_ctx_narrow_access_ok(off, size, size_default);
8180 }
8181
8182 return size == size_default;
61023658
DA
8183}
8184
c64b7983
JS
8185static bool sock_filter_is_valid_access(int off, int size,
8186 enum bpf_access_type type,
8187 const struct bpf_prog *prog,
8188 struct bpf_insn_access_aux *info)
8189{
8190 if (!bpf_sock_is_valid_access(off, size, type, info))
8191 return false;
8192 return __sock_filter_check_attach_type(off, type,
8193 prog->expected_attach_type);
8194}
8195
b09928b9
DB
8196static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
8197 const struct bpf_prog *prog)
8198{
8199 /* Neither direct read nor direct write requires any preliminary
8200 * action.
8201 */
8202 return 0;
8203}
8204
047b0ecd
DB
8205static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
8206 const struct bpf_prog *prog, int drop_verdict)
36bbef52
DB
8207{
8208 struct bpf_insn *insn = insn_buf;
8209
8210 if (!direct_write)
8211 return 0;
8212
8213 /* if (!skb->cloned)
8214 * goto start;
8215 *
8216 * (Fast-path, otherwise approximation that we might be
8217 * a clone, do the rest in helper.)
8218 */
fba84957 8219 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET);
36bbef52
DB
8220 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
8221 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
8222
8223 /* ret = bpf_skb_pull_data(skb, 0); */
8224 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
8225 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
8226 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
8227 BPF_FUNC_skb_pull_data);
8228 /* if (!ret)
8229 * goto restore;
8230 * return TC_ACT_SHOT;
8231 */
8232 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
047b0ecd 8233 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
36bbef52
DB
8234 *insn++ = BPF_EXIT_INSN();
8235
8236 /* restore: */
8237 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
8238 /* start: */
8239 *insn++ = prog->insnsi[0];
8240
8241 return insn - insn_buf;
8242}
8243
e0cea7ce
DB
8244static int bpf_gen_ld_abs(const struct bpf_insn *orig,
8245 struct bpf_insn *insn_buf)
8246{
8247 bool indirect = BPF_MODE(orig->code) == BPF_IND;
8248 struct bpf_insn *insn = insn_buf;
8249
e0cea7ce
DB
8250 if (!indirect) {
8251 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
8252 } else {
8253 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
8254 if (orig->imm)
8255 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
8256 }
e6a18d36
DB
8257 /* We're guaranteed here that CTX is in R6. */
8258 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
e0cea7ce
DB
8259
8260 switch (BPF_SIZE(orig->code)) {
8261 case BPF_B:
8262 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
8263 break;
8264 case BPF_H:
8265 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
8266 break;
8267 case BPF_W:
8268 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
8269 break;
8270 }
8271
8272 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
8273 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
8274 *insn++ = BPF_EXIT_INSN();
8275
8276 return insn - insn_buf;
8277}
8278
047b0ecd
DB
8279static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
8280 const struct bpf_prog *prog)
8281{
8282 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
8283}
8284
d691f9e8 8285static bool tc_cls_act_is_valid_access(int off, int size,
19de99f7 8286 enum bpf_access_type type,
5e43f899 8287 const struct bpf_prog *prog,
23994631 8288 struct bpf_insn_access_aux *info)
d691f9e8
AS
8289{
8290 if (type == BPF_WRITE) {
8291 switch (off) {
f96da094
DB
8292 case bpf_ctx_range(struct __sk_buff, mark):
8293 case bpf_ctx_range(struct __sk_buff, tc_index):
8294 case bpf_ctx_range(struct __sk_buff, priority):
8295 case bpf_ctx_range(struct __sk_buff, tc_classid):
8296 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
f11216b2 8297 case bpf_ctx_range(struct __sk_buff, tstamp):
74e31ca8 8298 case bpf_ctx_range(struct __sk_buff, queue_mapping):
d691f9e8
AS
8299 break;
8300 default:
8301 return false;
8302 }
8303 }
19de99f7 8304
f96da094
DB
8305 switch (off) {
8306 case bpf_ctx_range(struct __sk_buff, data):
8307 info->reg_type = PTR_TO_PACKET;
8308 break;
de8f3a83
DB
8309 case bpf_ctx_range(struct __sk_buff, data_meta):
8310 info->reg_type = PTR_TO_PACKET_META;
8311 break;
f96da094
DB
8312 case bpf_ctx_range(struct __sk_buff, data_end):
8313 info->reg_type = PTR_TO_PACKET_END;
8314 break;
8a31db56
JF
8315 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8316 return false;
f96da094
DB
8317 }
8318
5e43f899 8319 return bpf_skb_is_valid_access(off, size, type, prog, info);
d691f9e8
AS
8320}
8321
1afaf661 8322static bool __is_valid_xdp_access(int off, int size)
6a773a15
BB
8323{
8324 if (off < 0 || off >= sizeof(struct xdp_md))
8325 return false;
8326 if (off % size != 0)
8327 return false;
6088b582 8328 if (size != sizeof(__u32))
6a773a15
BB
8329 return false;
8330
8331 return true;
8332}
8333
8334static bool xdp_is_valid_access(int off, int size,
8335 enum bpf_access_type type,
5e43f899 8336 const struct bpf_prog *prog,
23994631 8337 struct bpf_insn_access_aux *info)
6a773a15 8338{
64b59025
DA
8339 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
8340 switch (off) {
8341 case offsetof(struct xdp_md, egress_ifindex):
8342 return false;
8343 }
8344 }
8345
0d830032
JK
8346 if (type == BPF_WRITE) {
8347 if (bpf_prog_is_dev_bound(prog->aux)) {
8348 switch (off) {
8349 case offsetof(struct xdp_md, rx_queue_index):
8350 return __is_valid_xdp_access(off, size);
8351 }
8352 }
6a773a15 8353 return false;
0d830032 8354 }
6a773a15
BB
8355
8356 switch (off) {
8357 case offsetof(struct xdp_md, data):
23994631 8358 info->reg_type = PTR_TO_PACKET;
6a773a15 8359 break;
de8f3a83
DB
8360 case offsetof(struct xdp_md, data_meta):
8361 info->reg_type = PTR_TO_PACKET_META;
8362 break;
6a773a15 8363 case offsetof(struct xdp_md, data_end):
23994631 8364 info->reg_type = PTR_TO_PACKET_END;
6a773a15
BB
8365 break;
8366 }
8367
1afaf661 8368 return __is_valid_xdp_access(off, size);
6a773a15
BB
8369}
8370
c8064e5b 8371void bpf_warn_invalid_xdp_action(struct net_device *dev, struct bpf_prog *prog, u32 act)
6a773a15 8372{
9beb8bed
DB
8373 const u32 act_max = XDP_REDIRECT;
8374
c8064e5b 8375 pr_warn_once("%s XDP return value %u on prog %s (id %d) dev %s, expect packet loss!\n",
2cbad989 8376 act > act_max ? "Illegal" : "Driver unsupported",
c8064e5b 8377 act, prog->aux->name, prog->aux->id, dev ? dev->name : "N/A");
6a773a15
BB
8378}
8379EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
8380
4fbac77d
AI
8381static bool sock_addr_is_valid_access(int off, int size,
8382 enum bpf_access_type type,
8383 const struct bpf_prog *prog,
8384 struct bpf_insn_access_aux *info)
8385{
8386 const int size_default = sizeof(__u32);
8387
8388 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
8389 return false;
8390 if (off % size != 0)
8391 return false;
8392
8393 /* Disallow access to IPv6 fields from IPv4 contex and vise
8394 * versa.
8395 */
8396 switch (off) {
8397 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8398 switch (prog->expected_attach_type) {
8399 case BPF_CGROUP_INET4_BIND:
d74bad4e 8400 case BPF_CGROUP_INET4_CONNECT:
1b66d253
DB
8401 case BPF_CGROUP_INET4_GETPEERNAME:
8402 case BPF_CGROUP_INET4_GETSOCKNAME:
1cedee13 8403 case BPF_CGROUP_UDP4_SENDMSG:
983695fa 8404 case BPF_CGROUP_UDP4_RECVMSG:
4fbac77d
AI
8405 break;
8406 default:
8407 return false;
8408 }
8409 break;
8410 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8411 switch (prog->expected_attach_type) {
8412 case BPF_CGROUP_INET6_BIND:
d74bad4e 8413 case BPF_CGROUP_INET6_CONNECT:
1b66d253
DB
8414 case BPF_CGROUP_INET6_GETPEERNAME:
8415 case BPF_CGROUP_INET6_GETSOCKNAME:
1cedee13 8416 case BPF_CGROUP_UDP6_SENDMSG:
983695fa 8417 case BPF_CGROUP_UDP6_RECVMSG:
1cedee13
AI
8418 break;
8419 default:
8420 return false;
8421 }
8422 break;
8423 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8424 switch (prog->expected_attach_type) {
8425 case BPF_CGROUP_UDP4_SENDMSG:
8426 break;
8427 default:
8428 return false;
8429 }
8430 break;
8431 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8432 msg_src_ip6[3]):
8433 switch (prog->expected_attach_type) {
8434 case BPF_CGROUP_UDP6_SENDMSG:
4fbac77d
AI
8435 break;
8436 default:
8437 return false;
8438 }
8439 break;
8440 }
8441
8442 switch (off) {
8443 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8444 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
1cedee13
AI
8445 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8446 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8447 msg_src_ip6[3]):
7aebfa1b 8448 case bpf_ctx_range(struct bpf_sock_addr, user_port):
4fbac77d
AI
8449 if (type == BPF_READ) {
8450 bpf_ctx_record_field_size(info, size_default);
d4ecfeb1
SF
8451
8452 if (bpf_ctx_wide_access_ok(off, size,
8453 struct bpf_sock_addr,
8454 user_ip6))
8455 return true;
8456
8457 if (bpf_ctx_wide_access_ok(off, size,
8458 struct bpf_sock_addr,
8459 msg_src_ip6))
8460 return true;
8461
4fbac77d
AI
8462 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8463 return false;
8464 } else {
b4399546
SF
8465 if (bpf_ctx_wide_access_ok(off, size,
8466 struct bpf_sock_addr,
8467 user_ip6))
600c70ba
SF
8468 return true;
8469
b4399546
SF
8470 if (bpf_ctx_wide_access_ok(off, size,
8471 struct bpf_sock_addr,
8472 msg_src_ip6))
600c70ba
SF
8473 return true;
8474
4fbac77d
AI
8475 if (size != size_default)
8476 return false;
8477 }
8478 break;
fb85c4a7
SF
8479 case offsetof(struct bpf_sock_addr, sk):
8480 if (type != BPF_READ)
8481 return false;
8482 if (size != sizeof(__u64))
8483 return false;
8484 info->reg_type = PTR_TO_SOCKET;
8485 break;
4fbac77d
AI
8486 default:
8487 if (type == BPF_READ) {
8488 if (size != size_default)
8489 return false;
8490 } else {
8491 return false;
8492 }
8493 }
8494
8495 return true;
8496}
8497
44f0e430
LB
8498static bool sock_ops_is_valid_access(int off, int size,
8499 enum bpf_access_type type,
5e43f899 8500 const struct bpf_prog *prog,
44f0e430 8501 struct bpf_insn_access_aux *info)
40304b2a 8502{
44f0e430
LB
8503 const int size_default = sizeof(__u32);
8504
40304b2a
LB
8505 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
8506 return false;
44f0e430 8507
40304b2a
LB
8508 /* The verifier guarantees that size > 0. */
8509 if (off % size != 0)
8510 return false;
40304b2a 8511
40304b2a
LB
8512 if (type == BPF_WRITE) {
8513 switch (off) {
2585cd62 8514 case offsetof(struct bpf_sock_ops, reply):
6f9bd3d7 8515 case offsetof(struct bpf_sock_ops, sk_txhash):
44f0e430
LB
8516 if (size != size_default)
8517 return false;
40304b2a
LB
8518 break;
8519 default:
8520 return false;
8521 }
44f0e430
LB
8522 } else {
8523 switch (off) {
8524 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
8525 bytes_acked):
8526 if (size != sizeof(__u64))
8527 return false;
8528 break;
1314ef56
SF
8529 case offsetof(struct bpf_sock_ops, sk):
8530 if (size != sizeof(__u64))
8531 return false;
8532 info->reg_type = PTR_TO_SOCKET_OR_NULL;
8533 break;
0813a841
MKL
8534 case offsetof(struct bpf_sock_ops, skb_data):
8535 if (size != sizeof(__u64))
8536 return false;
8537 info->reg_type = PTR_TO_PACKET;
8538 break;
8539 case offsetof(struct bpf_sock_ops, skb_data_end):
8540 if (size != sizeof(__u64))
8541 return false;
8542 info->reg_type = PTR_TO_PACKET_END;
8543 break;
8544 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
8545 bpf_ctx_record_field_size(info, size_default);
8546 return bpf_ctx_narrow_access_ok(off, size,
8547 size_default);
44f0e430
LB
8548 default:
8549 if (size != size_default)
8550 return false;
8551 break;
8552 }
40304b2a
LB
8553 }
8554
44f0e430 8555 return true;
40304b2a
LB
8556}
8557
8a31db56
JF
8558static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
8559 const struct bpf_prog *prog)
8560{
047b0ecd 8561 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
8a31db56
JF
8562}
8563
b005fd18
JF
8564static bool sk_skb_is_valid_access(int off, int size,
8565 enum bpf_access_type type,
5e43f899 8566 const struct bpf_prog *prog,
b005fd18
JF
8567 struct bpf_insn_access_aux *info)
8568{
de8f3a83
DB
8569 switch (off) {
8570 case bpf_ctx_range(struct __sk_buff, tc_classid):
8571 case bpf_ctx_range(struct __sk_buff, data_meta):
f11216b2 8572 case bpf_ctx_range(struct __sk_buff, tstamp):
e3da08d0 8573 case bpf_ctx_range(struct __sk_buff, wire_len):
f64c4ace 8574 case bpf_ctx_range(struct __sk_buff, hwtstamp):
de8f3a83
DB
8575 return false;
8576 }
8577
8a31db56
JF
8578 if (type == BPF_WRITE) {
8579 switch (off) {
8a31db56
JF
8580 case bpf_ctx_range(struct __sk_buff, tc_index):
8581 case bpf_ctx_range(struct __sk_buff, priority):
8582 break;
8583 default:
8584 return false;
8585 }
8586 }
8587
b005fd18 8588 switch (off) {
f7e9cb1e 8589 case bpf_ctx_range(struct __sk_buff, mark):
8a31db56 8590 return false;
b005fd18
JF
8591 case bpf_ctx_range(struct __sk_buff, data):
8592 info->reg_type = PTR_TO_PACKET;
8593 break;
8594 case bpf_ctx_range(struct __sk_buff, data_end):
8595 info->reg_type = PTR_TO_PACKET_END;
8596 break;
8597 }
8598
5e43f899 8599 return bpf_skb_is_valid_access(off, size, type, prog, info);
b005fd18
JF
8600}
8601
4f738adb
JF
8602static bool sk_msg_is_valid_access(int off, int size,
8603 enum bpf_access_type type,
5e43f899 8604 const struct bpf_prog *prog,
4f738adb
JF
8605 struct bpf_insn_access_aux *info)
8606{
8607 if (type == BPF_WRITE)
8608 return false;
8609
bc1b4f01
JF
8610 if (off % size != 0)
8611 return false;
8612
4f738adb
JF
8613 switch (off) {
8614 case offsetof(struct sk_msg_md, data):
8615 info->reg_type = PTR_TO_PACKET;
303def35
JF
8616 if (size != sizeof(__u64))
8617 return false;
4f738adb
JF
8618 break;
8619 case offsetof(struct sk_msg_md, data_end):
8620 info->reg_type = PTR_TO_PACKET_END;
303def35
JF
8621 if (size != sizeof(__u64))
8622 return false;
4f738adb 8623 break;
13d70f5a
JF
8624 case offsetof(struct sk_msg_md, sk):
8625 if (size != sizeof(__u64))
8626 return false;
8627 info->reg_type = PTR_TO_SOCKET;
8628 break;
bc1b4f01
JF
8629 case bpf_ctx_range(struct sk_msg_md, family):
8630 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
8631 case bpf_ctx_range(struct sk_msg_md, local_ip4):
8632 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
8633 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
8634 case bpf_ctx_range(struct sk_msg_md, remote_port):
8635 case bpf_ctx_range(struct sk_msg_md, local_port):
8636 case bpf_ctx_range(struct sk_msg_md, size):
303def35
JF
8637 if (size != sizeof(__u32))
8638 return false;
bc1b4f01
JF
8639 break;
8640 default:
4f738adb 8641 return false;
bc1b4f01 8642 }
4f738adb
JF
8643 return true;
8644}
8645
d58e468b
PP
8646static bool flow_dissector_is_valid_access(int off, int size,
8647 enum bpf_access_type type,
8648 const struct bpf_prog *prog,
8649 struct bpf_insn_access_aux *info)
8650{
089b19a9
SF
8651 const int size_default = sizeof(__u32);
8652
8653 if (off < 0 || off >= sizeof(struct __sk_buff))
8654 return false;
8655
2ee7fba0
SF
8656 if (type == BPF_WRITE)
8657 return false;
d58e468b
PP
8658
8659 switch (off) {
8660 case bpf_ctx_range(struct __sk_buff, data):
089b19a9
SF
8661 if (size != size_default)
8662 return false;
d58e468b 8663 info->reg_type = PTR_TO_PACKET;
089b19a9 8664 return true;
d58e468b 8665 case bpf_ctx_range(struct __sk_buff, data_end):
089b19a9
SF
8666 if (size != size_default)
8667 return false;
d58e468b 8668 info->reg_type = PTR_TO_PACKET_END;
089b19a9 8669 return true;
b7df9ada 8670 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
089b19a9
SF
8671 if (size != sizeof(__u64))
8672 return false;
d58e468b 8673 info->reg_type = PTR_TO_FLOW_KEYS;
089b19a9 8674 return true;
2ee7fba0 8675 default:
d58e468b
PP
8676 return false;
8677 }
089b19a9 8678}
d58e468b 8679
089b19a9
SF
8680static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
8681 const struct bpf_insn *si,
8682 struct bpf_insn *insn_buf,
8683 struct bpf_prog *prog,
8684 u32 *target_size)
8685
8686{
8687 struct bpf_insn *insn = insn_buf;
8688
8689 switch (si->off) {
8690 case offsetof(struct __sk_buff, data):
8691 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
8692 si->dst_reg, si->src_reg,
8693 offsetof(struct bpf_flow_dissector, data));
8694 break;
8695
8696 case offsetof(struct __sk_buff, data_end):
8697 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
8698 si->dst_reg, si->src_reg,
8699 offsetof(struct bpf_flow_dissector, data_end));
8700 break;
8701
8702 case offsetof(struct __sk_buff, flow_keys):
8703 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
8704 si->dst_reg, si->src_reg,
8705 offsetof(struct bpf_flow_dissector, flow_keys));
8706 break;
8707 }
8708
8709 return insn - insn_buf;
d58e468b
PP
8710}
8711
cf62089b
WB
8712static struct bpf_insn *bpf_convert_shinfo_access(const struct bpf_insn *si,
8713 struct bpf_insn *insn)
8714{
8715 /* si->dst_reg = skb_shinfo(SKB); */
8716#ifdef NET_SKBUFF_DATA_USES_OFFSET
8717 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
8718 BPF_REG_AX, si->src_reg,
8719 offsetof(struct sk_buff, end));
8720 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
8721 si->dst_reg, si->src_reg,
8722 offsetof(struct sk_buff, head));
8723 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
8724#else
8725 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
8726 si->dst_reg, si->src_reg,
8727 offsetof(struct sk_buff, end));
8728#endif
8729
8730 return insn;
8731}
8732
2492d3b8
DB
8733static u32 bpf_convert_ctx_access(enum bpf_access_type type,
8734 const struct bpf_insn *si,
8735 struct bpf_insn *insn_buf,
f96da094 8736 struct bpf_prog *prog, u32 *target_size)
9bac3d6d
AS
8737{
8738 struct bpf_insn *insn = insn_buf;
6b8cc1d1 8739 int off;
9bac3d6d 8740
6b8cc1d1 8741 switch (si->off) {
9bac3d6d 8742 case offsetof(struct __sk_buff, len):
6b8cc1d1 8743 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
8744 bpf_target_off(struct sk_buff, len, 4,
8745 target_size));
9bac3d6d
AS
8746 break;
8747
0b8c707d 8748 case offsetof(struct __sk_buff, protocol):
6b8cc1d1 8749 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
8750 bpf_target_off(struct sk_buff, protocol, 2,
8751 target_size));
0b8c707d
DB
8752 break;
8753
27cd5452 8754 case offsetof(struct __sk_buff, vlan_proto):
6b8cc1d1 8755 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
8756 bpf_target_off(struct sk_buff, vlan_proto, 2,
8757 target_size));
27cd5452
MS
8758 break;
8759
bcad5718 8760 case offsetof(struct __sk_buff, priority):
754f1e6a 8761 if (type == BPF_WRITE)
6b8cc1d1 8762 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
8763 bpf_target_off(struct sk_buff, priority, 4,
8764 target_size));
754f1e6a 8765 else
6b8cc1d1 8766 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
8767 bpf_target_off(struct sk_buff, priority, 4,
8768 target_size));
bcad5718
DB
8769 break;
8770
37e82c2f 8771 case offsetof(struct __sk_buff, ingress_ifindex):
6b8cc1d1 8772 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
8773 bpf_target_off(struct sk_buff, skb_iif, 4,
8774 target_size));
37e82c2f
AS
8775 break;
8776
8777 case offsetof(struct __sk_buff, ifindex):
f035a515 8778 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 8779 si->dst_reg, si->src_reg,
37e82c2f 8780 offsetof(struct sk_buff, dev));
6b8cc1d1
DB
8781 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
8782 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
8783 bpf_target_off(struct net_device, ifindex, 4,
8784 target_size));
37e82c2f
AS
8785 break;
8786
ba7591d8 8787 case offsetof(struct __sk_buff, hash):
6b8cc1d1 8788 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
8789 bpf_target_off(struct sk_buff, hash, 4,
8790 target_size));
ba7591d8
DB
8791 break;
8792
9bac3d6d 8793 case offsetof(struct __sk_buff, mark):
d691f9e8 8794 if (type == BPF_WRITE)
6b8cc1d1 8795 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
8796 bpf_target_off(struct sk_buff, mark, 4,
8797 target_size));
d691f9e8 8798 else
6b8cc1d1 8799 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
8800 bpf_target_off(struct sk_buff, mark, 4,
8801 target_size));
d691f9e8 8802 break;
9bac3d6d
AS
8803
8804 case offsetof(struct __sk_buff, pkt_type):
f96da094
DB
8805 *target_size = 1;
8806 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
fba84957 8807 PKT_TYPE_OFFSET);
f96da094
DB
8808 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
8809#ifdef __BIG_ENDIAN_BITFIELD
8810 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
8811#endif
8812 break;
9bac3d6d
AS
8813
8814 case offsetof(struct __sk_buff, queue_mapping):
74e31ca8
JDB
8815 if (type == BPF_WRITE) {
8816 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
8817 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
8818 bpf_target_off(struct sk_buff,
8819 queue_mapping,
8820 2, target_size));
8821 } else {
8822 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8823 bpf_target_off(struct sk_buff,
8824 queue_mapping,
8825 2, target_size));
8826 }
f96da094 8827 break;
c2497395 8828
c2497395 8829 case offsetof(struct __sk_buff, vlan_present):
9c212255
MM
8830 *target_size = 1;
8831 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
fba84957 8832 PKT_VLAN_PRESENT_OFFSET);
9c212255
MM
8833 if (PKT_VLAN_PRESENT_BIT)
8834 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
8835 if (PKT_VLAN_PRESENT_BIT < 7)
8836 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
8837 break;
f96da094 8838
9c212255 8839 case offsetof(struct __sk_buff, vlan_tci):
f96da094
DB
8840 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8841 bpf_target_off(struct sk_buff, vlan_tci, 2,
8842 target_size));
f96da094 8843 break;
d691f9e8
AS
8844
8845 case offsetof(struct __sk_buff, cb[0]) ...
f96da094 8846 offsetofend(struct __sk_buff, cb[4]) - 1:
c593642c 8847 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
62c7989b
DB
8848 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
8849 offsetof(struct qdisc_skb_cb, data)) %
8850 sizeof(__u64));
d691f9e8 8851
ff936a04 8852 prog->cb_access = 1;
6b8cc1d1
DB
8853 off = si->off;
8854 off -= offsetof(struct __sk_buff, cb[0]);
8855 off += offsetof(struct sk_buff, cb);
8856 off += offsetof(struct qdisc_skb_cb, data);
d691f9e8 8857 if (type == BPF_WRITE)
62c7989b 8858 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 8859 si->src_reg, off);
d691f9e8 8860 else
62c7989b 8861 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 8862 si->src_reg, off);
d691f9e8
AS
8863 break;
8864
045efa82 8865 case offsetof(struct __sk_buff, tc_classid):
c593642c 8866 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
6b8cc1d1
DB
8867
8868 off = si->off;
8869 off -= offsetof(struct __sk_buff, tc_classid);
8870 off += offsetof(struct sk_buff, cb);
8871 off += offsetof(struct qdisc_skb_cb, tc_classid);
f96da094 8872 *target_size = 2;
09c37a2c 8873 if (type == BPF_WRITE)
6b8cc1d1
DB
8874 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
8875 si->src_reg, off);
09c37a2c 8876 else
6b8cc1d1
DB
8877 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
8878 si->src_reg, off);
045efa82
DB
8879 break;
8880
db58ba45 8881 case offsetof(struct __sk_buff, data):
f035a515 8882 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
6b8cc1d1 8883 si->dst_reg, si->src_reg,
db58ba45
AS
8884 offsetof(struct sk_buff, data));
8885 break;
8886
de8f3a83
DB
8887 case offsetof(struct __sk_buff, data_meta):
8888 off = si->off;
8889 off -= offsetof(struct __sk_buff, data_meta);
8890 off += offsetof(struct sk_buff, cb);
8891 off += offsetof(struct bpf_skb_data_end, data_meta);
8892 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
8893 si->src_reg, off);
8894 break;
8895
db58ba45 8896 case offsetof(struct __sk_buff, data_end):
6b8cc1d1
DB
8897 off = si->off;
8898 off -= offsetof(struct __sk_buff, data_end);
8899 off += offsetof(struct sk_buff, cb);
8900 off += offsetof(struct bpf_skb_data_end, data_end);
8901 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
8902 si->src_reg, off);
db58ba45
AS
8903 break;
8904
d691f9e8
AS
8905 case offsetof(struct __sk_buff, tc_index):
8906#ifdef CONFIG_NET_SCHED
d691f9e8 8907 if (type == BPF_WRITE)
6b8cc1d1 8908 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
8909 bpf_target_off(struct sk_buff, tc_index, 2,
8910 target_size));
d691f9e8 8911 else
6b8cc1d1 8912 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
8913 bpf_target_off(struct sk_buff, tc_index, 2,
8914 target_size));
d691f9e8 8915#else
2ed46ce4 8916 *target_size = 2;
d691f9e8 8917 if (type == BPF_WRITE)
6b8cc1d1 8918 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
d691f9e8 8919 else
6b8cc1d1 8920 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
b1d9fc41
DB
8921#endif
8922 break;
8923
8924 case offsetof(struct __sk_buff, napi_id):
8925#if defined(CONFIG_NET_RX_BUSY_POLL)
b1d9fc41 8926 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
8927 bpf_target_off(struct sk_buff, napi_id, 4,
8928 target_size));
b1d9fc41
DB
8929 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
8930 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
8931#else
2ed46ce4 8932 *target_size = 4;
b1d9fc41 8933 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
d691f9e8 8934#endif
6b8cc1d1 8935 break;
8a31db56 8936 case offsetof(struct __sk_buff, family):
c593642c 8937 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
8a31db56
JF
8938
8939 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8940 si->dst_reg, si->src_reg,
8941 offsetof(struct sk_buff, sk));
8942 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8943 bpf_target_off(struct sock_common,
8944 skc_family,
8945 2, target_size));
8946 break;
8947 case offsetof(struct __sk_buff, remote_ip4):
c593642c 8948 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
8a31db56
JF
8949
8950 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8951 si->dst_reg, si->src_reg,
8952 offsetof(struct sk_buff, sk));
8953 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8954 bpf_target_off(struct sock_common,
8955 skc_daddr,
8956 4, target_size));
8957 break;
8958 case offsetof(struct __sk_buff, local_ip4):
c593642c 8959 BUILD_BUG_ON(sizeof_field(struct sock_common,
8a31db56
JF
8960 skc_rcv_saddr) != 4);
8961
8962 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8963 si->dst_reg, si->src_reg,
8964 offsetof(struct sk_buff, sk));
8965 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8966 bpf_target_off(struct sock_common,
8967 skc_rcv_saddr,
8968 4, target_size));
8969 break;
8970 case offsetof(struct __sk_buff, remote_ip6[0]) ...
8971 offsetof(struct __sk_buff, remote_ip6[3]):
8972#if IS_ENABLED(CONFIG_IPV6)
c593642c 8973 BUILD_BUG_ON(sizeof_field(struct sock_common,
8a31db56
JF
8974 skc_v6_daddr.s6_addr32[0]) != 4);
8975
8976 off = si->off;
8977 off -= offsetof(struct __sk_buff, remote_ip6[0]);
8978
8979 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8980 si->dst_reg, si->src_reg,
8981 offsetof(struct sk_buff, sk));
8982 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8983 offsetof(struct sock_common,
8984 skc_v6_daddr.s6_addr32[0]) +
8985 off);
8986#else
8987 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8988#endif
8989 break;
8990 case offsetof(struct __sk_buff, local_ip6[0]) ...
8991 offsetof(struct __sk_buff, local_ip6[3]):
8992#if IS_ENABLED(CONFIG_IPV6)
c593642c 8993 BUILD_BUG_ON(sizeof_field(struct sock_common,
8a31db56
JF
8994 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8995
8996 off = si->off;
8997 off -= offsetof(struct __sk_buff, local_ip6[0]);
8998
8999 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9000 si->dst_reg, si->src_reg,
9001 offsetof(struct sk_buff, sk));
9002 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9003 offsetof(struct sock_common,
9004 skc_v6_rcv_saddr.s6_addr32[0]) +
9005 off);
9006#else
9007 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9008#endif
9009 break;
9010
9011 case offsetof(struct __sk_buff, remote_port):
c593642c 9012 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
8a31db56
JF
9013
9014 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9015 si->dst_reg, si->src_reg,
9016 offsetof(struct sk_buff, sk));
9017 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9018 bpf_target_off(struct sock_common,
9019 skc_dport,
9020 2, target_size));
9021#ifndef __BIG_ENDIAN_BITFIELD
9022 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
9023#endif
9024 break;
9025
9026 case offsetof(struct __sk_buff, local_port):
c593642c 9027 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
8a31db56
JF
9028
9029 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9030 si->dst_reg, si->src_reg,
9031 offsetof(struct sk_buff, sk));
9032 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9033 bpf_target_off(struct sock_common,
9034 skc_num, 2, target_size));
9035 break;
d58e468b 9036
f11216b2 9037 case offsetof(struct __sk_buff, tstamp):
c593642c 9038 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
f11216b2
VD
9039
9040 if (type == BPF_WRITE)
9041 *insn++ = BPF_STX_MEM(BPF_DW,
9042 si->dst_reg, si->src_reg,
9043 bpf_target_off(struct sk_buff,
9044 tstamp, 8,
9045 target_size));
9046 else
9047 *insn++ = BPF_LDX_MEM(BPF_DW,
9048 si->dst_reg, si->src_reg,
9049 bpf_target_off(struct sk_buff,
9050 tstamp, 8,
9051 target_size));
e3da08d0
PP
9052 break;
9053
d9ff286a 9054 case offsetof(struct __sk_buff, gso_segs):
cf62089b 9055 insn = bpf_convert_shinfo_access(si, insn);
d9ff286a
ED
9056 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
9057 si->dst_reg, si->dst_reg,
9058 bpf_target_off(struct skb_shared_info,
9059 gso_segs, 2,
9060 target_size));
9061 break;
cf62089b
WB
9062 case offsetof(struct __sk_buff, gso_size):
9063 insn = bpf_convert_shinfo_access(si, insn);
9064 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
9065 si->dst_reg, si->dst_reg,
9066 bpf_target_off(struct skb_shared_info,
9067 gso_size, 2,
9068 target_size));
9069 break;
e3da08d0 9070 case offsetof(struct __sk_buff, wire_len):
c593642c 9071 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
e3da08d0
PP
9072
9073 off = si->off;
9074 off -= offsetof(struct __sk_buff, wire_len);
9075 off += offsetof(struct sk_buff, cb);
9076 off += offsetof(struct qdisc_skb_cb, pkt_len);
9077 *target_size = 4;
9078 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
46f8bc92
MKL
9079 break;
9080
9081 case offsetof(struct __sk_buff, sk):
9082 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9083 si->dst_reg, si->src_reg,
9084 offsetof(struct sk_buff, sk));
9085 break;
f64c4ace
VF
9086 case offsetof(struct __sk_buff, hwtstamp):
9087 BUILD_BUG_ON(sizeof_field(struct skb_shared_hwtstamps, hwtstamp) != 8);
9088 BUILD_BUG_ON(offsetof(struct skb_shared_hwtstamps, hwtstamp) != 0);
9089
9090 insn = bpf_convert_shinfo_access(si, insn);
9091 *insn++ = BPF_LDX_MEM(BPF_DW,
9092 si->dst_reg, si->dst_reg,
9093 bpf_target_off(struct skb_shared_info,
9094 hwtstamps, 8,
9095 target_size));
9096 break;
9bac3d6d
AS
9097 }
9098
9099 return insn - insn_buf;
89aa0758
AS
9100}
9101
c64b7983
JS
9102u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
9103 const struct bpf_insn *si,
9104 struct bpf_insn *insn_buf,
9105 struct bpf_prog *prog, u32 *target_size)
61023658
DA
9106{
9107 struct bpf_insn *insn = insn_buf;
aac3fc32 9108 int off;
61023658 9109
6b8cc1d1 9110 switch (si->off) {
61023658 9111 case offsetof(struct bpf_sock, bound_dev_if):
c593642c 9112 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
61023658
DA
9113
9114 if (type == BPF_WRITE)
6b8cc1d1 9115 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
9116 offsetof(struct sock, sk_bound_dev_if));
9117 else
6b8cc1d1 9118 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
9119 offsetof(struct sock, sk_bound_dev_if));
9120 break;
aa4c1037 9121
482dca93 9122 case offsetof(struct bpf_sock, mark):
c593642c 9123 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
482dca93
DA
9124
9125 if (type == BPF_WRITE)
9126 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9127 offsetof(struct sock, sk_mark));
9128 else
9129 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9130 offsetof(struct sock, sk_mark));
9131 break;
9132
9133 case offsetof(struct bpf_sock, priority):
c593642c 9134 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
482dca93
DA
9135
9136 if (type == BPF_WRITE)
9137 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9138 offsetof(struct sock, sk_priority));
9139 else
9140 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9141 offsetof(struct sock, sk_priority));
9142 break;
9143
aa4c1037 9144 case offsetof(struct bpf_sock, family):
aa65d696
MKL
9145 *insn++ = BPF_LDX_MEM(
9146 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
9147 si->dst_reg, si->src_reg,
9148 bpf_target_off(struct sock_common,
9149 skc_family,
c593642c 9150 sizeof_field(struct sock_common,
aa65d696
MKL
9151 skc_family),
9152 target_size));
aa4c1037
DA
9153 break;
9154
9155 case offsetof(struct bpf_sock, type):
bf976514
MM
9156 *insn++ = BPF_LDX_MEM(
9157 BPF_FIELD_SIZEOF(struct sock, sk_type),
9158 si->dst_reg, si->src_reg,
9159 bpf_target_off(struct sock, sk_type,
9160 sizeof_field(struct sock, sk_type),
9161 target_size));
aa4c1037
DA
9162 break;
9163
9164 case offsetof(struct bpf_sock, protocol):
bf976514
MM
9165 *insn++ = BPF_LDX_MEM(
9166 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
9167 si->dst_reg, si->src_reg,
9168 bpf_target_off(struct sock, sk_protocol,
9169 sizeof_field(struct sock, sk_protocol),
9170 target_size));
aa4c1037 9171 break;
aac3fc32
AI
9172
9173 case offsetof(struct bpf_sock, src_ip4):
9174 *insn++ = BPF_LDX_MEM(
9175 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9176 bpf_target_off(struct sock_common, skc_rcv_saddr,
c593642c 9177 sizeof_field(struct sock_common,
aac3fc32
AI
9178 skc_rcv_saddr),
9179 target_size));
9180 break;
9181
aa65d696
MKL
9182 case offsetof(struct bpf_sock, dst_ip4):
9183 *insn++ = BPF_LDX_MEM(
9184 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9185 bpf_target_off(struct sock_common, skc_daddr,
c593642c 9186 sizeof_field(struct sock_common,
aa65d696
MKL
9187 skc_daddr),
9188 target_size));
9189 break;
9190
aac3fc32
AI
9191 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
9192#if IS_ENABLED(CONFIG_IPV6)
9193 off = si->off;
9194 off -= offsetof(struct bpf_sock, src_ip6[0]);
9195 *insn++ = BPF_LDX_MEM(
9196 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9197 bpf_target_off(
9198 struct sock_common,
9199 skc_v6_rcv_saddr.s6_addr32[0],
c593642c 9200 sizeof_field(struct sock_common,
aac3fc32
AI
9201 skc_v6_rcv_saddr.s6_addr32[0]),
9202 target_size) + off);
9203#else
9204 (void)off;
9205 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9206#endif
9207 break;
9208
aa65d696
MKL
9209 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
9210#if IS_ENABLED(CONFIG_IPV6)
9211 off = si->off;
9212 off -= offsetof(struct bpf_sock, dst_ip6[0]);
9213 *insn++ = BPF_LDX_MEM(
9214 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9215 bpf_target_off(struct sock_common,
9216 skc_v6_daddr.s6_addr32[0],
c593642c 9217 sizeof_field(struct sock_common,
aa65d696
MKL
9218 skc_v6_daddr.s6_addr32[0]),
9219 target_size) + off);
9220#else
9221 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9222 *target_size = 4;
9223#endif
9224 break;
9225
aac3fc32
AI
9226 case offsetof(struct bpf_sock, src_port):
9227 *insn++ = BPF_LDX_MEM(
9228 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
9229 si->dst_reg, si->src_reg,
9230 bpf_target_off(struct sock_common, skc_num,
c593642c 9231 sizeof_field(struct sock_common,
aac3fc32
AI
9232 skc_num),
9233 target_size));
9234 break;
aa65d696
MKL
9235
9236 case offsetof(struct bpf_sock, dst_port):
9237 *insn++ = BPF_LDX_MEM(
9238 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
9239 si->dst_reg, si->src_reg,
9240 bpf_target_off(struct sock_common, skc_dport,
c593642c 9241 sizeof_field(struct sock_common,
aa65d696
MKL
9242 skc_dport),
9243 target_size));
9244 break;
9245
9246 case offsetof(struct bpf_sock, state):
9247 *insn++ = BPF_LDX_MEM(
9248 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
9249 si->dst_reg, si->src_reg,
9250 bpf_target_off(struct sock_common, skc_state,
c593642c 9251 sizeof_field(struct sock_common,
aa65d696
MKL
9252 skc_state),
9253 target_size));
9254 break;
c3c16f2e 9255 case offsetof(struct bpf_sock, rx_queue_mapping):
4e1beecc 9256#ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
c3c16f2e
AN
9257 *insn++ = BPF_LDX_MEM(
9258 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
9259 si->dst_reg, si->src_reg,
9260 bpf_target_off(struct sock, sk_rx_queue_mapping,
9261 sizeof_field(struct sock,
9262 sk_rx_queue_mapping),
9263 target_size));
9264 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
9265 1);
9266 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
9267#else
9268 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
9269 *target_size = 2;
9270#endif
9271 break;
61023658
DA
9272 }
9273
9274 return insn - insn_buf;
9275}
9276
6b8cc1d1
DB
9277static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
9278 const struct bpf_insn *si,
374fb54e 9279 struct bpf_insn *insn_buf,
f96da094 9280 struct bpf_prog *prog, u32 *target_size)
374fb54e
DB
9281{
9282 struct bpf_insn *insn = insn_buf;
9283
6b8cc1d1 9284 switch (si->off) {
374fb54e 9285 case offsetof(struct __sk_buff, ifindex):
374fb54e 9286 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 9287 si->dst_reg, si->src_reg,
374fb54e 9288 offsetof(struct sk_buff, dev));
6b8cc1d1 9289 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
9290 bpf_target_off(struct net_device, ifindex, 4,
9291 target_size));
374fb54e
DB
9292 break;
9293 default:
f96da094
DB
9294 return bpf_convert_ctx_access(type, si, insn_buf, prog,
9295 target_size);
374fb54e
DB
9296 }
9297
9298 return insn - insn_buf;
9299}
9300
6b8cc1d1
DB
9301static u32 xdp_convert_ctx_access(enum bpf_access_type type,
9302 const struct bpf_insn *si,
6a773a15 9303 struct bpf_insn *insn_buf,
f96da094 9304 struct bpf_prog *prog, u32 *target_size)
6a773a15
BB
9305{
9306 struct bpf_insn *insn = insn_buf;
9307
6b8cc1d1 9308 switch (si->off) {
6a773a15 9309 case offsetof(struct xdp_md, data):
f035a515 9310 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6b8cc1d1 9311 si->dst_reg, si->src_reg,
6a773a15
BB
9312 offsetof(struct xdp_buff, data));
9313 break;
de8f3a83
DB
9314 case offsetof(struct xdp_md, data_meta):
9315 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
9316 si->dst_reg, si->src_reg,
9317 offsetof(struct xdp_buff, data_meta));
9318 break;
6a773a15 9319 case offsetof(struct xdp_md, data_end):
f035a515 9320 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6b8cc1d1 9321 si->dst_reg, si->src_reg,
6a773a15
BB
9322 offsetof(struct xdp_buff, data_end));
9323 break;
02dd3291
JDB
9324 case offsetof(struct xdp_md, ingress_ifindex):
9325 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
9326 si->dst_reg, si->src_reg,
9327 offsetof(struct xdp_buff, rxq));
9328 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
9329 si->dst_reg, si->dst_reg,
9330 offsetof(struct xdp_rxq_info, dev));
9331 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
daaf24c6 9332 offsetof(struct net_device, ifindex));
02dd3291
JDB
9333 break;
9334 case offsetof(struct xdp_md, rx_queue_index):
9335 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
9336 si->dst_reg, si->src_reg,
9337 offsetof(struct xdp_buff, rxq));
9338 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
daaf24c6
JDB
9339 offsetof(struct xdp_rxq_info,
9340 queue_index));
02dd3291 9341 break;
64b59025
DA
9342 case offsetof(struct xdp_md, egress_ifindex):
9343 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
9344 si->dst_reg, si->src_reg,
9345 offsetof(struct xdp_buff, txq));
9346 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
9347 si->dst_reg, si->dst_reg,
9348 offsetof(struct xdp_txq_info, dev));
9349 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9350 offsetof(struct net_device, ifindex));
9351 break;
6a773a15
BB
9352 }
9353
9354 return insn - insn_buf;
9355}
9356
4fbac77d
AI
9357/* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
9358 * context Structure, F is Field in context structure that contains a pointer
9359 * to Nested Structure of type NS that has the field NF.
9360 *
9361 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
9362 * sure that SIZE is not greater than actual size of S.F.NF.
9363 *
9364 * If offset OFF is provided, the load happens from that offset relative to
9365 * offset of NF.
9366 */
9367#define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
9368 do { \
9369 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
9370 si->src_reg, offsetof(S, F)); \
9371 *insn++ = BPF_LDX_MEM( \
9372 SIZE, si->dst_reg, si->dst_reg, \
c593642c 9373 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
4fbac77d
AI
9374 target_size) \
9375 + OFF); \
9376 } while (0)
9377
9378#define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
9379 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
9380 BPF_FIELD_SIZEOF(NS, NF), 0)
9381
9382/* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
9383 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
9384 *
4fbac77d
AI
9385 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
9386 * "register" since two registers available in convert_ctx_access are not
9387 * enough: we can't override neither SRC, since it contains value to store, nor
9388 * DST since it contains pointer to context that may be used by later
9389 * instructions. But we need a temporary place to save pointer to nested
9390 * structure whose field we want to store to.
9391 */
600c70ba 9392#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
4fbac77d
AI
9393 do { \
9394 int tmp_reg = BPF_REG_9; \
9395 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9396 --tmp_reg; \
9397 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9398 --tmp_reg; \
9399 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
9400 offsetof(S, TF)); \
9401 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
9402 si->dst_reg, offsetof(S, F)); \
600c70ba 9403 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
c593642c 9404 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
4fbac77d
AI
9405 target_size) \
9406 + OFF); \
9407 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
9408 offsetof(S, TF)); \
9409 } while (0)
9410
9411#define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
9412 TF) \
9413 do { \
9414 if (type == BPF_WRITE) { \
600c70ba
SF
9415 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
9416 OFF, TF); \
4fbac77d
AI
9417 } else { \
9418 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
9419 S, NS, F, NF, SIZE, OFF); \
9420 } \
9421 } while (0)
9422
9423#define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
9424 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
9425 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
9426
9427static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
9428 const struct bpf_insn *si,
9429 struct bpf_insn *insn_buf,
9430 struct bpf_prog *prog, u32 *target_size)
9431{
7aebfa1b 9432 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
4fbac77d 9433 struct bpf_insn *insn = insn_buf;
4fbac77d
AI
9434
9435 switch (si->off) {
9436 case offsetof(struct bpf_sock_addr, user_family):
9437 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9438 struct sockaddr, uaddr, sa_family);
9439 break;
9440
9441 case offsetof(struct bpf_sock_addr, user_ip4):
9442 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9443 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
9444 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
9445 break;
9446
9447 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9448 off = si->off;
9449 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
9450 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9451 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9452 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
9453 tmp_reg);
9454 break;
9455
9456 case offsetof(struct bpf_sock_addr, user_port):
9457 /* To get port we need to know sa_family first and then treat
9458 * sockaddr as either sockaddr_in or sockaddr_in6.
9459 * Though we can simplify since port field has same offset and
9460 * size in both structures.
9461 * Here we check this invariant and use just one of the
9462 * structures if it's true.
9463 */
9464 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
9465 offsetof(struct sockaddr_in6, sin6_port));
c593642c
PB
9466 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
9467 sizeof_field(struct sockaddr_in6, sin6_port));
7aebfa1b
AI
9468 /* Account for sin6_port being smaller than user_port. */
9469 port_size = min(port_size, BPF_LDST_BYTES(si));
9470 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9471 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9472 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
4fbac77d
AI
9473 break;
9474
9475 case offsetof(struct bpf_sock_addr, family):
9476 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9477 struct sock, sk, sk_family);
9478 break;
9479
9480 case offsetof(struct bpf_sock_addr, type):
bf976514
MM
9481 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9482 struct sock, sk, sk_type);
4fbac77d
AI
9483 break;
9484
9485 case offsetof(struct bpf_sock_addr, protocol):
bf976514
MM
9486 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9487 struct sock, sk, sk_protocol);
4fbac77d 9488 break;
1cedee13
AI
9489
9490 case offsetof(struct bpf_sock_addr, msg_src_ip4):
9491 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
9492 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9493 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
9494 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
9495 break;
9496
9497 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9498 msg_src_ip6[3]):
9499 off = si->off;
9500 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
9501 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
9502 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9503 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
9504 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
9505 break;
fb85c4a7
SF
9506 case offsetof(struct bpf_sock_addr, sk):
9507 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
9508 si->dst_reg, si->src_reg,
9509 offsetof(struct bpf_sock_addr_kern, sk));
9510 break;
4fbac77d
AI
9511 }
9512
9513 return insn - insn_buf;
9514}
9515
40304b2a
LB
9516static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
9517 const struct bpf_insn *si,
9518 struct bpf_insn *insn_buf,
f96da094
DB
9519 struct bpf_prog *prog,
9520 u32 *target_size)
40304b2a
LB
9521{
9522 struct bpf_insn *insn = insn_buf;
9523 int off;
9524
9b1f3d6e
MKL
9525/* Helper macro for adding read access to tcp_sock or sock fields. */
9526#define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
9527 do { \
fd09af01 9528 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \
c593642c
PB
9529 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
9530 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
fd09af01
JF
9531 if (si->dst_reg == reg || si->src_reg == reg) \
9532 reg--; \
9533 if (si->dst_reg == reg || si->src_reg == reg) \
9534 reg--; \
9535 if (si->dst_reg == si->src_reg) { \
9536 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
9537 offsetof(struct bpf_sock_ops_kern, \
9538 temp)); \
9539 fullsock_reg = reg; \
9540 jmp += 2; \
9541 } \
9b1f3d6e
MKL
9542 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9543 struct bpf_sock_ops_kern, \
9544 is_fullsock), \
fd09af01 9545 fullsock_reg, si->src_reg, \
9b1f3d6e
MKL
9546 offsetof(struct bpf_sock_ops_kern, \
9547 is_fullsock)); \
fd09af01
JF
9548 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
9549 if (si->dst_reg == si->src_reg) \
9550 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9551 offsetof(struct bpf_sock_ops_kern, \
9552 temp)); \
9b1f3d6e
MKL
9553 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9554 struct bpf_sock_ops_kern, sk),\
9555 si->dst_reg, si->src_reg, \
9556 offsetof(struct bpf_sock_ops_kern, sk));\
9557 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
9558 OBJ_FIELD), \
9559 si->dst_reg, si->dst_reg, \
9560 offsetof(OBJ, OBJ_FIELD)); \
fd09af01
JF
9561 if (si->dst_reg == si->src_reg) { \
9562 *insn++ = BPF_JMP_A(1); \
9563 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9564 offsetof(struct bpf_sock_ops_kern, \
9565 temp)); \
9566 } \
9b1f3d6e
MKL
9567 } while (0)
9568
84f44df6
JF
9569#define SOCK_OPS_GET_SK() \
9570 do { \
9571 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \
9572 if (si->dst_reg == reg || si->src_reg == reg) \
9573 reg--; \
9574 if (si->dst_reg == reg || si->src_reg == reg) \
9575 reg--; \
9576 if (si->dst_reg == si->src_reg) { \
9577 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
9578 offsetof(struct bpf_sock_ops_kern, \
9579 temp)); \
9580 fullsock_reg = reg; \
9581 jmp += 2; \
9582 } \
9583 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9584 struct bpf_sock_ops_kern, \
9585 is_fullsock), \
9586 fullsock_reg, si->src_reg, \
9587 offsetof(struct bpf_sock_ops_kern, \
9588 is_fullsock)); \
9589 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
9590 if (si->dst_reg == si->src_reg) \
9591 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9592 offsetof(struct bpf_sock_ops_kern, \
9593 temp)); \
9594 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9595 struct bpf_sock_ops_kern, sk),\
9596 si->dst_reg, si->src_reg, \
9597 offsetof(struct bpf_sock_ops_kern, sk));\
9598 if (si->dst_reg == si->src_reg) { \
9599 *insn++ = BPF_JMP_A(1); \
9600 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9601 offsetof(struct bpf_sock_ops_kern, \
9602 temp)); \
9603 } \
9604 } while (0)
9605
9b1f3d6e
MKL
9606#define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
9607 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
9608
9609/* Helper macro for adding write access to tcp_sock or sock fields.
9610 * The macro is called with two registers, dst_reg which contains a pointer
9611 * to ctx (context) and src_reg which contains the value that should be
9612 * stored. However, we need an additional register since we cannot overwrite
9613 * dst_reg because it may be used later in the program.
9614 * Instead we "borrow" one of the other register. We first save its value
9615 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
9616 * it at the end of the macro.
9617 */
9618#define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
9619 do { \
9620 int reg = BPF_REG_9; \
c593642c
PB
9621 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
9622 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
9b1f3d6e
MKL
9623 if (si->dst_reg == reg || si->src_reg == reg) \
9624 reg--; \
9625 if (si->dst_reg == reg || si->src_reg == reg) \
9626 reg--; \
9627 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
9628 offsetof(struct bpf_sock_ops_kern, \
9629 temp)); \
9630 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9631 struct bpf_sock_ops_kern, \
9632 is_fullsock), \
9633 reg, si->dst_reg, \
9634 offsetof(struct bpf_sock_ops_kern, \
9635 is_fullsock)); \
9636 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
9637 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9638 struct bpf_sock_ops_kern, sk),\
9639 reg, si->dst_reg, \
9640 offsetof(struct bpf_sock_ops_kern, sk));\
9641 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
9642 reg, si->src_reg, \
9643 offsetof(OBJ, OBJ_FIELD)); \
9644 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
9645 offsetof(struct bpf_sock_ops_kern, \
9646 temp)); \
9647 } while (0)
9648
9649#define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
9650 do { \
9651 if (TYPE == BPF_WRITE) \
9652 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
9653 else \
9654 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
9655 } while (0)
9656
9b1f3d6e
MKL
9657 if (insn > insn_buf)
9658 return insn - insn_buf;
9659
40304b2a 9660 switch (si->off) {
c9985d09
MKL
9661 case offsetof(struct bpf_sock_ops, op):
9662 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9663 op),
9664 si->dst_reg, si->src_reg,
9665 offsetof(struct bpf_sock_ops_kern, op));
9666 break;
9667
9668 case offsetof(struct bpf_sock_ops, replylong[0]) ...
40304b2a 9669 offsetof(struct bpf_sock_ops, replylong[3]):
c593642c
PB
9670 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
9671 sizeof_field(struct bpf_sock_ops_kern, reply));
9672 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
9673 sizeof_field(struct bpf_sock_ops_kern, replylong));
40304b2a 9674 off = si->off;
c9985d09
MKL
9675 off -= offsetof(struct bpf_sock_ops, replylong[0]);
9676 off += offsetof(struct bpf_sock_ops_kern, replylong[0]);
40304b2a
LB
9677 if (type == BPF_WRITE)
9678 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9679 off);
9680 else
9681 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9682 off);
9683 break;
9684
9685 case offsetof(struct bpf_sock_ops, family):
c593642c 9686 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
40304b2a
LB
9687
9688 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9689 struct bpf_sock_ops_kern, sk),
9690 si->dst_reg, si->src_reg,
9691 offsetof(struct bpf_sock_ops_kern, sk));
9692 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9693 offsetof(struct sock_common, skc_family));
9694 break;
9695
9696 case offsetof(struct bpf_sock_ops, remote_ip4):
c593642c 9697 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
40304b2a
LB
9698
9699 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9700 struct bpf_sock_ops_kern, sk),
9701 si->dst_reg, si->src_reg,
9702 offsetof(struct bpf_sock_ops_kern, sk));
9703 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9704 offsetof(struct sock_common, skc_daddr));
9705 break;
9706
9707 case offsetof(struct bpf_sock_ops, local_ip4):
c593642c 9708 BUILD_BUG_ON(sizeof_field(struct sock_common,
303def35 9709 skc_rcv_saddr) != 4);
40304b2a
LB
9710
9711 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9712 struct bpf_sock_ops_kern, sk),
9713 si->dst_reg, si->src_reg,
9714 offsetof(struct bpf_sock_ops_kern, sk));
9715 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9716 offsetof(struct sock_common,
9717 skc_rcv_saddr));
9718 break;
9719
9720 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
9721 offsetof(struct bpf_sock_ops, remote_ip6[3]):
9722#if IS_ENABLED(CONFIG_IPV6)
c593642c 9723 BUILD_BUG_ON(sizeof_field(struct sock_common,
40304b2a
LB
9724 skc_v6_daddr.s6_addr32[0]) != 4);
9725
9726 off = si->off;
9727 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
9728 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9729 struct bpf_sock_ops_kern, sk),
9730 si->dst_reg, si->src_reg,
9731 offsetof(struct bpf_sock_ops_kern, sk));
9732 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9733 offsetof(struct sock_common,
9734 skc_v6_daddr.s6_addr32[0]) +
9735 off);
9736#else
9737 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9738#endif
9739 break;
9740
9741 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
9742 offsetof(struct bpf_sock_ops, local_ip6[3]):
9743#if IS_ENABLED(CONFIG_IPV6)
c593642c 9744 BUILD_BUG_ON(sizeof_field(struct sock_common,
40304b2a
LB
9745 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
9746
9747 off = si->off;
9748 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
9749 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9750 struct bpf_sock_ops_kern, sk),
9751 si->dst_reg, si->src_reg,
9752 offsetof(struct bpf_sock_ops_kern, sk));
9753 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9754 offsetof(struct sock_common,
9755 skc_v6_rcv_saddr.s6_addr32[0]) +
9756 off);
9757#else
9758 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9759#endif
9760 break;
9761
9762 case offsetof(struct bpf_sock_ops, remote_port):
c593642c 9763 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
40304b2a
LB
9764
9765 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9766 struct bpf_sock_ops_kern, sk),
9767 si->dst_reg, si->src_reg,
9768 offsetof(struct bpf_sock_ops_kern, sk));
9769 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9770 offsetof(struct sock_common, skc_dport));
9771#ifndef __BIG_ENDIAN_BITFIELD
9772 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
9773#endif
9774 break;
9775
9776 case offsetof(struct bpf_sock_ops, local_port):
c593642c 9777 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
40304b2a
LB
9778
9779 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9780 struct bpf_sock_ops_kern, sk),
9781 si->dst_reg, si->src_reg,
9782 offsetof(struct bpf_sock_ops_kern, sk));
9783 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9784 offsetof(struct sock_common, skc_num));
9785 break;
f19397a5
LB
9786
9787 case offsetof(struct bpf_sock_ops, is_fullsock):
9788 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9789 struct bpf_sock_ops_kern,
9790 is_fullsock),
9791 si->dst_reg, si->src_reg,
9792 offsetof(struct bpf_sock_ops_kern,
9793 is_fullsock));
9794 break;
9795
44f0e430 9796 case offsetof(struct bpf_sock_ops, state):
c593642c 9797 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
44f0e430
LB
9798
9799 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9800 struct bpf_sock_ops_kern, sk),
9801 si->dst_reg, si->src_reg,
9802 offsetof(struct bpf_sock_ops_kern, sk));
9803 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
9804 offsetof(struct sock_common, skc_state));
9805 break;
9806
9807 case offsetof(struct bpf_sock_ops, rtt_min):
c593642c 9808 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
44f0e430
LB
9809 sizeof(struct minmax));
9810 BUILD_BUG_ON(sizeof(struct minmax) <
9811 sizeof(struct minmax_sample));
9812
9813 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9814 struct bpf_sock_ops_kern, sk),
9815 si->dst_reg, si->src_reg,
9816 offsetof(struct bpf_sock_ops_kern, sk));
9817 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9818 offsetof(struct tcp_sock, rtt_min) +
c593642c 9819 sizeof_field(struct minmax_sample, t));
44f0e430
LB
9820 break;
9821
b13d8807
LB
9822 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
9823 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
9824 struct tcp_sock);
9825 break;
44f0e430 9826
44f0e430 9827 case offsetof(struct bpf_sock_ops, sk_txhash):
6f9bd3d7
LB
9828 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
9829 struct sock, type);
44f0e430 9830 break;
2377b81d
SF
9831 case offsetof(struct bpf_sock_ops, snd_cwnd):
9832 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
9833 break;
9834 case offsetof(struct bpf_sock_ops, srtt_us):
9835 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
9836 break;
9837 case offsetof(struct bpf_sock_ops, snd_ssthresh):
9838 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
9839 break;
9840 case offsetof(struct bpf_sock_ops, rcv_nxt):
9841 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
9842 break;
9843 case offsetof(struct bpf_sock_ops, snd_nxt):
9844 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
9845 break;
9846 case offsetof(struct bpf_sock_ops, snd_una):
9847 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
9848 break;
9849 case offsetof(struct bpf_sock_ops, mss_cache):
9850 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
9851 break;
9852 case offsetof(struct bpf_sock_ops, ecn_flags):
9853 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
9854 break;
9855 case offsetof(struct bpf_sock_ops, rate_delivered):
9856 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
9857 break;
9858 case offsetof(struct bpf_sock_ops, rate_interval_us):
9859 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
9860 break;
9861 case offsetof(struct bpf_sock_ops, packets_out):
9862 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
9863 break;
9864 case offsetof(struct bpf_sock_ops, retrans_out):
9865 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
9866 break;
9867 case offsetof(struct bpf_sock_ops, total_retrans):
9868 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
9869 break;
9870 case offsetof(struct bpf_sock_ops, segs_in):
9871 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
9872 break;
9873 case offsetof(struct bpf_sock_ops, data_segs_in):
9874 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
9875 break;
9876 case offsetof(struct bpf_sock_ops, segs_out):
9877 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
9878 break;
9879 case offsetof(struct bpf_sock_ops, data_segs_out):
9880 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
9881 break;
9882 case offsetof(struct bpf_sock_ops, lost_out):
9883 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
9884 break;
9885 case offsetof(struct bpf_sock_ops, sacked_out):
9886 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
9887 break;
9888 case offsetof(struct bpf_sock_ops, bytes_received):
9889 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
9890 break;
9891 case offsetof(struct bpf_sock_ops, bytes_acked):
9892 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
9893 break;
1314ef56 9894 case offsetof(struct bpf_sock_ops, sk):
84f44df6 9895 SOCK_OPS_GET_SK();
1314ef56 9896 break;
0813a841
MKL
9897 case offsetof(struct bpf_sock_ops, skb_data_end):
9898 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9899 skb_data_end),
9900 si->dst_reg, si->src_reg,
9901 offsetof(struct bpf_sock_ops_kern,
9902 skb_data_end));
9903 break;
9904 case offsetof(struct bpf_sock_ops, skb_data):
9905 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9906 skb),
9907 si->dst_reg, si->src_reg,
9908 offsetof(struct bpf_sock_ops_kern,
9909 skb));
9910 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9911 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9912 si->dst_reg, si->dst_reg,
9913 offsetof(struct sk_buff, data));
9914 break;
9915 case offsetof(struct bpf_sock_ops, skb_len):
9916 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9917 skb),
9918 si->dst_reg, si->src_reg,
9919 offsetof(struct bpf_sock_ops_kern,
9920 skb));
9921 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9922 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
9923 si->dst_reg, si->dst_reg,
9924 offsetof(struct sk_buff, len));
9925 break;
9926 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
9927 off = offsetof(struct sk_buff, cb);
9928 off += offsetof(struct tcp_skb_cb, tcp_flags);
9929 *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags);
9930 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9931 skb),
9932 si->dst_reg, si->src_reg,
9933 offsetof(struct bpf_sock_ops_kern,
9934 skb));
9935 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9936 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb,
9937 tcp_flags),
9938 si->dst_reg, si->dst_reg, off);
9939 break;
40304b2a
LB
9940 }
9941 return insn - insn_buf;
9942}
9943
16137b09
CW
9944/* data_end = skb->data + skb_headlen() */
9945static struct bpf_insn *bpf_convert_data_end_access(const struct bpf_insn *si,
9946 struct bpf_insn *insn)
9947{
b2c46181
JM
9948 int reg;
9949 int temp_reg_off = offsetof(struct sk_buff, cb) +
9950 offsetof(struct sk_skb_cb, temp_reg);
9951
9952 if (si->src_reg == si->dst_reg) {
9953 /* We need an extra register, choose and save a register. */
9954 reg = BPF_REG_9;
9955 if (si->src_reg == reg || si->dst_reg == reg)
9956 reg--;
9957 if (si->src_reg == reg || si->dst_reg == reg)
9958 reg--;
9959 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, temp_reg_off);
9960 } else {
9961 reg = si->dst_reg;
9962 }
9963
9964 /* reg = skb->data */
16137b09 9965 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
b2c46181 9966 reg, si->src_reg,
16137b09
CW
9967 offsetof(struct sk_buff, data));
9968 /* AX = skb->len */
9969 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
9970 BPF_REG_AX, si->src_reg,
9971 offsetof(struct sk_buff, len));
b2c46181
JM
9972 /* reg = skb->data + skb->len */
9973 *insn++ = BPF_ALU64_REG(BPF_ADD, reg, BPF_REG_AX);
16137b09
CW
9974 /* AX = skb->data_len */
9975 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data_len),
9976 BPF_REG_AX, si->src_reg,
9977 offsetof(struct sk_buff, data_len));
b2c46181
JM
9978
9979 /* reg = skb->data + skb->len - skb->data_len */
9980 *insn++ = BPF_ALU64_REG(BPF_SUB, reg, BPF_REG_AX);
9981
9982 if (si->src_reg == si->dst_reg) {
9983 /* Restore the saved register */
9984 *insn++ = BPF_MOV64_REG(BPF_REG_AX, si->src_reg);
9985 *insn++ = BPF_MOV64_REG(si->dst_reg, reg);
9986 *insn++ = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_AX, temp_reg_off);
9987 }
16137b09
CW
9988
9989 return insn;
9990}
9991
8108a775
JF
9992static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
9993 const struct bpf_insn *si,
9994 struct bpf_insn *insn_buf,
9995 struct bpf_prog *prog, u32 *target_size)
9996{
9997 struct bpf_insn *insn = insn_buf;
e0dc3b93 9998 int off;
8108a775
JF
9999
10000 switch (si->off) {
10001 case offsetof(struct __sk_buff, data_end):
16137b09 10002 insn = bpf_convert_data_end_access(si, insn);
8108a775 10003 break;
e0dc3b93
JF
10004 case offsetof(struct __sk_buff, cb[0]) ...
10005 offsetofend(struct __sk_buff, cb[4]) - 1:
10006 BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20);
10007 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
10008 offsetof(struct sk_skb_cb, data)) %
10009 sizeof(__u64));
10010
10011 prog->cb_access = 1;
10012 off = si->off;
10013 off -= offsetof(struct __sk_buff, cb[0]);
10014 off += offsetof(struct sk_buff, cb);
10015 off += offsetof(struct sk_skb_cb, data);
10016 if (type == BPF_WRITE)
10017 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
10018 si->src_reg, off);
10019 else
10020 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
10021 si->src_reg, off);
10022 break;
10023
10024
8108a775
JF
10025 default:
10026 return bpf_convert_ctx_access(type, si, insn_buf, prog,
10027 target_size);
10028 }
10029
10030 return insn - insn_buf;
10031}
10032
4f738adb
JF
10033static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
10034 const struct bpf_insn *si,
10035 struct bpf_insn *insn_buf,
10036 struct bpf_prog *prog, u32 *target_size)
10037{
10038 struct bpf_insn *insn = insn_buf;
720e7f38 10039#if IS_ENABLED(CONFIG_IPV6)
303def35 10040 int off;
720e7f38 10041#endif
4f738adb 10042
7a69c0f2
JF
10043 /* convert ctx uses the fact sg element is first in struct */
10044 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
10045
4f738adb
JF
10046 switch (si->off) {
10047 case offsetof(struct sk_msg_md, data):
604326b4 10048 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
4f738adb 10049 si->dst_reg, si->src_reg,
604326b4 10050 offsetof(struct sk_msg, data));
4f738adb
JF
10051 break;
10052 case offsetof(struct sk_msg_md, data_end):
604326b4 10053 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
4f738adb 10054 si->dst_reg, si->src_reg,
604326b4 10055 offsetof(struct sk_msg, data_end));
4f738adb 10056 break;
303def35 10057 case offsetof(struct sk_msg_md, family):
c593642c 10058 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
303def35
JF
10059
10060 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
604326b4 10061 struct sk_msg, sk),
303def35 10062 si->dst_reg, si->src_reg,
604326b4 10063 offsetof(struct sk_msg, sk));
303def35
JF
10064 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10065 offsetof(struct sock_common, skc_family));
10066 break;
10067
10068 case offsetof(struct sk_msg_md, remote_ip4):
c593642c 10069 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
303def35
JF
10070
10071 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
604326b4 10072 struct sk_msg, sk),
303def35 10073 si->dst_reg, si->src_reg,
604326b4 10074 offsetof(struct sk_msg, sk));
303def35
JF
10075 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10076 offsetof(struct sock_common, skc_daddr));
10077 break;
10078
10079 case offsetof(struct sk_msg_md, local_ip4):
c593642c 10080 BUILD_BUG_ON(sizeof_field(struct sock_common,
303def35
JF
10081 skc_rcv_saddr) != 4);
10082
10083 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
604326b4 10084 struct sk_msg, sk),
303def35 10085 si->dst_reg, si->src_reg,
604326b4 10086 offsetof(struct sk_msg, sk));
303def35
JF
10087 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10088 offsetof(struct sock_common,
10089 skc_rcv_saddr));
10090 break;
10091
10092 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
10093 offsetof(struct sk_msg_md, remote_ip6[3]):
10094#if IS_ENABLED(CONFIG_IPV6)
c593642c 10095 BUILD_BUG_ON(sizeof_field(struct sock_common,
303def35
JF
10096 skc_v6_daddr.s6_addr32[0]) != 4);
10097
10098 off = si->off;
10099 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
10100 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
604326b4 10101 struct sk_msg, sk),
303def35 10102 si->dst_reg, si->src_reg,
604326b4 10103 offsetof(struct sk_msg, sk));
303def35
JF
10104 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10105 offsetof(struct sock_common,
10106 skc_v6_daddr.s6_addr32[0]) +
10107 off);
10108#else
10109 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10110#endif
10111 break;
10112
10113 case offsetof(struct sk_msg_md, local_ip6[0]) ...
10114 offsetof(struct sk_msg_md, local_ip6[3]):
10115#if IS_ENABLED(CONFIG_IPV6)
c593642c 10116 BUILD_BUG_ON(sizeof_field(struct sock_common,
303def35
JF
10117 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10118
10119 off = si->off;
10120 off -= offsetof(struct sk_msg_md, local_ip6[0]);
10121 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
604326b4 10122 struct sk_msg, sk),
303def35 10123 si->dst_reg, si->src_reg,
604326b4 10124 offsetof(struct sk_msg, sk));
303def35
JF
10125 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10126 offsetof(struct sock_common,
10127 skc_v6_rcv_saddr.s6_addr32[0]) +
10128 off);
10129#else
10130 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10131#endif
10132 break;
10133
10134 case offsetof(struct sk_msg_md, remote_port):
c593642c 10135 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
303def35
JF
10136
10137 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
604326b4 10138 struct sk_msg, sk),
303def35 10139 si->dst_reg, si->src_reg,
604326b4 10140 offsetof(struct sk_msg, sk));
303def35
JF
10141 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10142 offsetof(struct sock_common, skc_dport));
10143#ifndef __BIG_ENDIAN_BITFIELD
10144 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10145#endif
10146 break;
10147
10148 case offsetof(struct sk_msg_md, local_port):
c593642c 10149 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
303def35
JF
10150
10151 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
604326b4 10152 struct sk_msg, sk),
303def35 10153 si->dst_reg, si->src_reg,
604326b4 10154 offsetof(struct sk_msg, sk));
303def35
JF
10155 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10156 offsetof(struct sock_common, skc_num));
10157 break;
3bdbd022
JF
10158
10159 case offsetof(struct sk_msg_md, size):
10160 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
10161 si->dst_reg, si->src_reg,
10162 offsetof(struct sk_msg_sg, size));
10163 break;
13d70f5a
JF
10164
10165 case offsetof(struct sk_msg_md, sk):
10166 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
10167 si->dst_reg, si->src_reg,
10168 offsetof(struct sk_msg, sk));
10169 break;
4f738adb
JF
10170 }
10171
10172 return insn - insn_buf;
10173}
10174
7de16e3a 10175const struct bpf_verifier_ops sk_filter_verifier_ops = {
4936e352
DB
10176 .get_func_proto = sk_filter_func_proto,
10177 .is_valid_access = sk_filter_is_valid_access,
2492d3b8 10178 .convert_ctx_access = bpf_convert_ctx_access,
e0cea7ce 10179 .gen_ld_abs = bpf_gen_ld_abs,
89aa0758
AS
10180};
10181
7de16e3a 10182const struct bpf_prog_ops sk_filter_prog_ops = {
61f3c964 10183 .test_run = bpf_prog_test_run_skb,
7de16e3a
JK
10184};
10185
10186const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
4936e352
DB
10187 .get_func_proto = tc_cls_act_func_proto,
10188 .is_valid_access = tc_cls_act_is_valid_access,
374fb54e 10189 .convert_ctx_access = tc_cls_act_convert_ctx_access,
36bbef52 10190 .gen_prologue = tc_cls_act_prologue,
e0cea7ce 10191 .gen_ld_abs = bpf_gen_ld_abs,
7de16e3a
JK
10192};
10193
10194const struct bpf_prog_ops tc_cls_act_prog_ops = {
1cf1cae9 10195 .test_run = bpf_prog_test_run_skb,
608cd71a
AS
10196};
10197
7de16e3a 10198const struct bpf_verifier_ops xdp_verifier_ops = {
6a773a15
BB
10199 .get_func_proto = xdp_func_proto,
10200 .is_valid_access = xdp_is_valid_access,
10201 .convert_ctx_access = xdp_convert_ctx_access,
b09928b9 10202 .gen_prologue = bpf_noop_prologue,
7de16e3a
JK
10203};
10204
10205const struct bpf_prog_ops xdp_prog_ops = {
1cf1cae9 10206 .test_run = bpf_prog_test_run_xdp,
6a773a15
BB
10207};
10208
7de16e3a 10209const struct bpf_verifier_ops cg_skb_verifier_ops = {
cd339431 10210 .get_func_proto = cg_skb_func_proto,
b39b5f41 10211 .is_valid_access = cg_skb_is_valid_access,
2492d3b8 10212 .convert_ctx_access = bpf_convert_ctx_access,
7de16e3a
JK
10213};
10214
10215const struct bpf_prog_ops cg_skb_prog_ops = {
1cf1cae9 10216 .test_run = bpf_prog_test_run_skb,
0e33661d
DM
10217};
10218
cd3092c7
MX
10219const struct bpf_verifier_ops lwt_in_verifier_ops = {
10220 .get_func_proto = lwt_in_func_proto,
3a0af8fd 10221 .is_valid_access = lwt_is_valid_access,
2492d3b8 10222 .convert_ctx_access = bpf_convert_ctx_access,
7de16e3a
JK
10223};
10224
cd3092c7
MX
10225const struct bpf_prog_ops lwt_in_prog_ops = {
10226 .test_run = bpf_prog_test_run_skb,
10227};
10228
10229const struct bpf_verifier_ops lwt_out_verifier_ops = {
10230 .get_func_proto = lwt_out_func_proto,
3a0af8fd 10231 .is_valid_access = lwt_is_valid_access,
2492d3b8 10232 .convert_ctx_access = bpf_convert_ctx_access,
7de16e3a
JK
10233};
10234
cd3092c7 10235const struct bpf_prog_ops lwt_out_prog_ops = {
1cf1cae9 10236 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
10237};
10238
7de16e3a 10239const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
3a0af8fd
TG
10240 .get_func_proto = lwt_xmit_func_proto,
10241 .is_valid_access = lwt_is_valid_access,
2492d3b8 10242 .convert_ctx_access = bpf_convert_ctx_access,
3a0af8fd 10243 .gen_prologue = tc_cls_act_prologue,
7de16e3a
JK
10244};
10245
10246const struct bpf_prog_ops lwt_xmit_prog_ops = {
1cf1cae9 10247 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
10248};
10249
004d4b27
MX
10250const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
10251 .get_func_proto = lwt_seg6local_func_proto,
10252 .is_valid_access = lwt_is_valid_access,
10253 .convert_ctx_access = bpf_convert_ctx_access,
10254};
10255
10256const struct bpf_prog_ops lwt_seg6local_prog_ops = {
10257 .test_run = bpf_prog_test_run_skb,
10258};
10259
7de16e3a 10260const struct bpf_verifier_ops cg_sock_verifier_ops = {
ae2cf1c4 10261 .get_func_proto = sock_filter_func_proto,
61023658 10262 .is_valid_access = sock_filter_is_valid_access,
c64b7983 10263 .convert_ctx_access = bpf_sock_convert_ctx_access,
61023658
DA
10264};
10265
7de16e3a
JK
10266const struct bpf_prog_ops cg_sock_prog_ops = {
10267};
10268
4fbac77d
AI
10269const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
10270 .get_func_proto = sock_addr_func_proto,
10271 .is_valid_access = sock_addr_is_valid_access,
10272 .convert_ctx_access = sock_addr_convert_ctx_access,
10273};
10274
10275const struct bpf_prog_ops cg_sock_addr_prog_ops = {
10276};
10277
7de16e3a 10278const struct bpf_verifier_ops sock_ops_verifier_ops = {
8c4b4c7e 10279 .get_func_proto = sock_ops_func_proto,
40304b2a
LB
10280 .is_valid_access = sock_ops_is_valid_access,
10281 .convert_ctx_access = sock_ops_convert_ctx_access,
10282};
10283
7de16e3a
JK
10284const struct bpf_prog_ops sock_ops_prog_ops = {
10285};
10286
10287const struct bpf_verifier_ops sk_skb_verifier_ops = {
b005fd18
JF
10288 .get_func_proto = sk_skb_func_proto,
10289 .is_valid_access = sk_skb_is_valid_access,
8108a775 10290 .convert_ctx_access = sk_skb_convert_ctx_access,
8a31db56 10291 .gen_prologue = sk_skb_prologue,
b005fd18
JF
10292};
10293
7de16e3a
JK
10294const struct bpf_prog_ops sk_skb_prog_ops = {
10295};
10296
4f738adb
JF
10297const struct bpf_verifier_ops sk_msg_verifier_ops = {
10298 .get_func_proto = sk_msg_func_proto,
10299 .is_valid_access = sk_msg_is_valid_access,
10300 .convert_ctx_access = sk_msg_convert_ctx_access,
b09928b9 10301 .gen_prologue = bpf_noop_prologue,
4f738adb
JF
10302};
10303
10304const struct bpf_prog_ops sk_msg_prog_ops = {
10305};
10306
d58e468b
PP
10307const struct bpf_verifier_ops flow_dissector_verifier_ops = {
10308 .get_func_proto = flow_dissector_func_proto,
10309 .is_valid_access = flow_dissector_is_valid_access,
089b19a9 10310 .convert_ctx_access = flow_dissector_convert_ctx_access,
d58e468b
PP
10311};
10312
10313const struct bpf_prog_ops flow_dissector_prog_ops = {
b7a1848e 10314 .test_run = bpf_prog_test_run_flow_dissector,
d58e468b
PP
10315};
10316
8ced425e 10317int sk_detach_filter(struct sock *sk)
55b33325
PE
10318{
10319 int ret = -ENOENT;
10320 struct sk_filter *filter;
10321
d59577b6
VB
10322 if (sock_flag(sk, SOCK_FILTER_LOCKED))
10323 return -EPERM;
10324
8ced425e
HFS
10325 filter = rcu_dereference_protected(sk->sk_filter,
10326 lockdep_sock_is_held(sk));
55b33325 10327 if (filter) {
a9b3cd7f 10328 RCU_INIT_POINTER(sk->sk_filter, NULL);
46bcf14f 10329 sk_filter_uncharge(sk, filter);
55b33325
PE
10330 ret = 0;
10331 }
a3ea269b 10332
55b33325
PE
10333 return ret;
10334}
8ced425e 10335EXPORT_SYMBOL_GPL(sk_detach_filter);
a8fc9277 10336
a3ea269b
DB
10337int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
10338 unsigned int len)
a8fc9277 10339{
a3ea269b 10340 struct sock_fprog_kern *fprog;
a8fc9277 10341 struct sk_filter *filter;
a3ea269b 10342 int ret = 0;
a8fc9277
PE
10343
10344 lock_sock(sk);
10345 filter = rcu_dereference_protected(sk->sk_filter,
8ced425e 10346 lockdep_sock_is_held(sk));
a8fc9277
PE
10347 if (!filter)
10348 goto out;
a3ea269b
DB
10349
10350 /* We're copying the filter that has been originally attached,
93d08b69
DB
10351 * so no conversion/decode needed anymore. eBPF programs that
10352 * have no original program cannot be dumped through this.
a3ea269b 10353 */
93d08b69 10354 ret = -EACCES;
7ae457c1 10355 fprog = filter->prog->orig_prog;
93d08b69
DB
10356 if (!fprog)
10357 goto out;
a3ea269b
DB
10358
10359 ret = fprog->len;
a8fc9277 10360 if (!len)
a3ea269b 10361 /* User space only enquires number of filter blocks. */
a8fc9277 10362 goto out;
a3ea269b 10363
a8fc9277 10364 ret = -EINVAL;
a3ea269b 10365 if (len < fprog->len)
a8fc9277
PE
10366 goto out;
10367
10368 ret = -EFAULT;
009937e7 10369 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
a3ea269b 10370 goto out;
a8fc9277 10371
a3ea269b
DB
10372 /* Instead of bytes, the API requests to return the number
10373 * of filter blocks.
10374 */
10375 ret = fprog->len;
a8fc9277
PE
10376out:
10377 release_sock(sk);
10378 return ret;
10379}
2dbb9b9e
MKL
10380
10381#ifdef CONFIG_INET
2dbb9b9e
MKL
10382static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
10383 struct sock_reuseport *reuse,
10384 struct sock *sk, struct sk_buff *skb,
d5e4ddae 10385 struct sock *migrating_sk,
2dbb9b9e
MKL
10386 u32 hash)
10387{
10388 reuse_kern->skb = skb;
10389 reuse_kern->sk = sk;
10390 reuse_kern->selected_sk = NULL;
d5e4ddae 10391 reuse_kern->migrating_sk = migrating_sk;
2dbb9b9e
MKL
10392 reuse_kern->data_end = skb->data + skb_headlen(skb);
10393 reuse_kern->hash = hash;
10394 reuse_kern->reuseport_id = reuse->reuseport_id;
10395 reuse_kern->bind_inany = reuse->bind_inany;
10396}
10397
10398struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
10399 struct bpf_prog *prog, struct sk_buff *skb,
d5e4ddae 10400 struct sock *migrating_sk,
2dbb9b9e
MKL
10401 u32 hash)
10402{
10403 struct sk_reuseport_kern reuse_kern;
10404 enum sk_action action;
10405
d5e4ddae 10406 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, migrating_sk, hash);
fb7dd8bc 10407 action = bpf_prog_run(prog, &reuse_kern);
2dbb9b9e
MKL
10408
10409 if (action == SK_PASS)
10410 return reuse_kern.selected_sk;
10411 else
10412 return ERR_PTR(-ECONNREFUSED);
10413}
10414
10415BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
10416 struct bpf_map *, map, void *, key, u32, flags)
10417{
9fed9000 10418 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
2dbb9b9e
MKL
10419 struct sock_reuseport *reuse;
10420 struct sock *selected_sk;
10421
10422 selected_sk = map->ops->map_lookup_elem(map, key);
10423 if (!selected_sk)
10424 return -ENOENT;
10425
10426 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
9fed9000 10427 if (!reuse) {
64d85290
JS
10428 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
10429 if (sk_is_refcounted(selected_sk))
10430 sock_put(selected_sk);
10431
9fed9000
JS
10432 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
10433 * The only (!reuse) case here is - the sk has already been
10434 * unhashed (e.g. by close()), so treat it as -ENOENT.
10435 *
10436 * Other maps (e.g. sock_map) do not provide this guarantee and
10437 * the sk may never be in the reuseport group to begin with.
2dbb9b9e 10438 */
9fed9000
JS
10439 return is_sockarray ? -ENOENT : -EINVAL;
10440 }
2dbb9b9e
MKL
10441
10442 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
035ff358 10443 struct sock *sk = reuse_kern->sk;
2dbb9b9e 10444
2dbb9b9e
MKL
10445 if (sk->sk_protocol != selected_sk->sk_protocol)
10446 return -EPROTOTYPE;
10447 else if (sk->sk_family != selected_sk->sk_family)
10448 return -EAFNOSUPPORT;
10449
10450 /* Catch all. Likely bound to a different sockaddr. */
10451 return -EBADFD;
10452 }
10453
10454 reuse_kern->selected_sk = selected_sk;
10455
10456 return 0;
10457}
10458
10459static const struct bpf_func_proto sk_select_reuseport_proto = {
10460 .func = sk_select_reuseport,
10461 .gpl_only = false,
10462 .ret_type = RET_INTEGER,
10463 .arg1_type = ARG_PTR_TO_CTX,
10464 .arg2_type = ARG_CONST_MAP_PTR,
10465 .arg3_type = ARG_PTR_TO_MAP_KEY,
10466 .arg4_type = ARG_ANYTHING,
10467};
10468
10469BPF_CALL_4(sk_reuseport_load_bytes,
10470 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10471 void *, to, u32, len)
10472{
10473 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
10474}
10475
10476static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
10477 .func = sk_reuseport_load_bytes,
10478 .gpl_only = false,
10479 .ret_type = RET_INTEGER,
10480 .arg1_type = ARG_PTR_TO_CTX,
10481 .arg2_type = ARG_ANYTHING,
10482 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10483 .arg4_type = ARG_CONST_SIZE,
10484};
10485
10486BPF_CALL_5(sk_reuseport_load_bytes_relative,
10487 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10488 void *, to, u32, len, u32, start_header)
10489{
10490 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
10491 len, start_header);
10492}
10493
10494static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
10495 .func = sk_reuseport_load_bytes_relative,
10496 .gpl_only = false,
10497 .ret_type = RET_INTEGER,
10498 .arg1_type = ARG_PTR_TO_CTX,
10499 .arg2_type = ARG_ANYTHING,
10500 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10501 .arg4_type = ARG_CONST_SIZE,
10502 .arg5_type = ARG_ANYTHING,
10503};
10504
10505static const struct bpf_func_proto *
10506sk_reuseport_func_proto(enum bpf_func_id func_id,
10507 const struct bpf_prog *prog)
10508{
10509 switch (func_id) {
10510 case BPF_FUNC_sk_select_reuseport:
10511 return &sk_select_reuseport_proto;
10512 case BPF_FUNC_skb_load_bytes:
10513 return &sk_reuseport_load_bytes_proto;
10514 case BPF_FUNC_skb_load_bytes_relative:
10515 return &sk_reuseport_load_bytes_relative_proto;
e0610476
KI
10516 case BPF_FUNC_get_socket_cookie:
10517 return &bpf_get_socket_ptr_cookie_proto;
5e0bc308
DB
10518 case BPF_FUNC_ktime_get_coarse_ns:
10519 return &bpf_ktime_get_coarse_ns_proto;
2dbb9b9e
MKL
10520 default:
10521 return bpf_base_func_proto(func_id);
10522 }
10523}
10524
10525static bool
10526sk_reuseport_is_valid_access(int off, int size,
10527 enum bpf_access_type type,
10528 const struct bpf_prog *prog,
10529 struct bpf_insn_access_aux *info)
10530{
10531 const u32 size_default = sizeof(__u32);
10532
10533 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
10534 off % size || type != BPF_READ)
10535 return false;
10536
10537 switch (off) {
10538 case offsetof(struct sk_reuseport_md, data):
10539 info->reg_type = PTR_TO_PACKET;
10540 return size == sizeof(__u64);
10541
10542 case offsetof(struct sk_reuseport_md, data_end):
10543 info->reg_type = PTR_TO_PACKET_END;
10544 return size == sizeof(__u64);
10545
10546 case offsetof(struct sk_reuseport_md, hash):
10547 return size == size_default;
10548
e0610476
KI
10549 case offsetof(struct sk_reuseport_md, sk):
10550 info->reg_type = PTR_TO_SOCKET;
10551 return size == sizeof(__u64);
10552
d5e4ddae
KI
10553 case offsetof(struct sk_reuseport_md, migrating_sk):
10554 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
10555 return size == sizeof(__u64);
10556
2dbb9b9e 10557 /* Fields that allow narrowing */
2c238177 10558 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
c593642c 10559 if (size < sizeof_field(struct sk_buff, protocol))
2dbb9b9e 10560 return false;
df561f66 10561 fallthrough;
2c238177
IL
10562 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
10563 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
10564 case bpf_ctx_range(struct sk_reuseport_md, len):
2dbb9b9e
MKL
10565 bpf_ctx_record_field_size(info, size_default);
10566 return bpf_ctx_narrow_access_ok(off, size, size_default);
10567
10568 default:
10569 return false;
10570 }
10571}
10572
10573#define SK_REUSEPORT_LOAD_FIELD(F) ({ \
10574 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
10575 si->dst_reg, si->src_reg, \
10576 bpf_target_off(struct sk_reuseport_kern, F, \
c593642c 10577 sizeof_field(struct sk_reuseport_kern, F), \
2dbb9b9e
MKL
10578 target_size)); \
10579 })
10580
10581#define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
10582 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
10583 struct sk_buff, \
10584 skb, \
10585 SKB_FIELD)
10586
bf976514
MM
10587#define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
10588 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
10589 struct sock, \
10590 sk, \
10591 SK_FIELD)
2dbb9b9e
MKL
10592
10593static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
10594 const struct bpf_insn *si,
10595 struct bpf_insn *insn_buf,
10596 struct bpf_prog *prog,
10597 u32 *target_size)
10598{
10599 struct bpf_insn *insn = insn_buf;
10600
10601 switch (si->off) {
10602 case offsetof(struct sk_reuseport_md, data):
10603 SK_REUSEPORT_LOAD_SKB_FIELD(data);
10604 break;
10605
10606 case offsetof(struct sk_reuseport_md, len):
10607 SK_REUSEPORT_LOAD_SKB_FIELD(len);
10608 break;
10609
10610 case offsetof(struct sk_reuseport_md, eth_protocol):
10611 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
10612 break;
10613
10614 case offsetof(struct sk_reuseport_md, ip_protocol):
bf976514 10615 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
2dbb9b9e
MKL
10616 break;
10617
10618 case offsetof(struct sk_reuseport_md, data_end):
10619 SK_REUSEPORT_LOAD_FIELD(data_end);
10620 break;
10621
10622 case offsetof(struct sk_reuseport_md, hash):
10623 SK_REUSEPORT_LOAD_FIELD(hash);
10624 break;
10625
10626 case offsetof(struct sk_reuseport_md, bind_inany):
10627 SK_REUSEPORT_LOAD_FIELD(bind_inany);
10628 break;
e0610476
KI
10629
10630 case offsetof(struct sk_reuseport_md, sk):
10631 SK_REUSEPORT_LOAD_FIELD(sk);
10632 break;
d5e4ddae
KI
10633
10634 case offsetof(struct sk_reuseport_md, migrating_sk):
10635 SK_REUSEPORT_LOAD_FIELD(migrating_sk);
10636 break;
2dbb9b9e
MKL
10637 }
10638
10639 return insn - insn_buf;
10640}
10641
10642const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
10643 .get_func_proto = sk_reuseport_func_proto,
10644 .is_valid_access = sk_reuseport_is_valid_access,
10645 .convert_ctx_access = sk_reuseport_convert_ctx_access,
10646};
10647
10648const struct bpf_prog_ops sk_reuseport_prog_ops = {
10649};
7e6897f9 10650
1559b4aa
JS
10651DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled);
10652EXPORT_SYMBOL(bpf_sk_lookup_enabled);
7e6897f9 10653
e9ddbb77
JS
10654BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx,
10655 struct sock *, sk, u64, flags)
7e6897f9 10656{
e9ddbb77
JS
10657 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE |
10658 BPF_SK_LOOKUP_F_NO_REUSEPORT)))
10659 return -EINVAL;
10660 if (unlikely(sk && sk_is_refcounted(sk)))
10661 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */
40a34121
JF
10662 if (unlikely(sk && sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN))
10663 return -ESOCKTNOSUPPORT; /* only accept TCP socket in LISTEN */
10664 if (unlikely(sk && sk_is_udp(sk) && sk->sk_state != TCP_CLOSE))
10665 return -ESOCKTNOSUPPORT; /* only accept UDP socket in CLOSE */
e9ddbb77
JS
10666
10667 /* Check if socket is suitable for packet L3/L4 protocol */
10668 if (sk && sk->sk_protocol != ctx->protocol)
10669 return -EPROTOTYPE;
10670 if (sk && sk->sk_family != ctx->family &&
10671 (sk->sk_family == AF_INET || ipv6_only_sock(sk)))
10672 return -EAFNOSUPPORT;
10673
10674 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE))
10675 return -EEXIST;
10676
10677 /* Select socket as lookup result */
10678 ctx->selected_sk = sk;
10679 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT;
10680 return 0;
7e6897f9 10681}
af7ec138 10682
e9ddbb77
JS
10683static const struct bpf_func_proto bpf_sk_lookup_assign_proto = {
10684 .func = bpf_sk_lookup_assign,
10685 .gpl_only = false,
10686 .ret_type = RET_INTEGER,
10687 .arg1_type = ARG_PTR_TO_CTX,
10688 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL,
10689 .arg3_type = ARG_ANYTHING,
af7ec138
YS
10690};
10691
e9ddbb77
JS
10692static const struct bpf_func_proto *
10693sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10694{
10695 switch (func_id) {
10696 case BPF_FUNC_perf_event_output:
10697 return &bpf_event_output_data_proto;
10698 case BPF_FUNC_sk_assign:
10699 return &bpf_sk_lookup_assign_proto;
10700 case BPF_FUNC_sk_release:
10701 return &bpf_sk_release_proto;
10702 default:
1df8f55a 10703 return bpf_sk_base_func_proto(func_id);
e9ddbb77
JS
10704 }
10705}
af7ec138 10706
e9ddbb77
JS
10707static bool sk_lookup_is_valid_access(int off, int size,
10708 enum bpf_access_type type,
10709 const struct bpf_prog *prog,
10710 struct bpf_insn_access_aux *info)
10711{
10712 if (off < 0 || off >= sizeof(struct bpf_sk_lookup))
10713 return false;
10714 if (off % size != 0)
10715 return false;
10716 if (type != BPF_READ)
10717 return false;
10718
10719 switch (off) {
10720 case offsetof(struct bpf_sk_lookup, sk):
10721 info->reg_type = PTR_TO_SOCKET_OR_NULL;
10722 return size == sizeof(__u64);
af7ec138 10723
e9ddbb77
JS
10724 case bpf_ctx_range(struct bpf_sk_lookup, family):
10725 case bpf_ctx_range(struct bpf_sk_lookup, protocol):
10726 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4):
10727 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
10728 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
10729 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
10730 case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
10731 case bpf_ctx_range(struct bpf_sk_lookup, local_port):
f8931565 10732 case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):
e9ddbb77
JS
10733 bpf_ctx_record_field_size(info, sizeof(__u32));
10734 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
10735
10736 default:
10737 return false;
10738 }
10739}
10740
10741static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
10742 const struct bpf_insn *si,
10743 struct bpf_insn *insn_buf,
10744 struct bpf_prog *prog,
10745 u32 *target_size)
af7ec138 10746{
e9ddbb77
JS
10747 struct bpf_insn *insn = insn_buf;
10748
10749 switch (si->off) {
10750 case offsetof(struct bpf_sk_lookup, sk):
10751 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
10752 offsetof(struct bpf_sk_lookup_kern, selected_sk));
10753 break;
af7ec138 10754
e9ddbb77
JS
10755 case offsetof(struct bpf_sk_lookup, family):
10756 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10757 bpf_target_off(struct bpf_sk_lookup_kern,
10758 family, 2, target_size));
10759 break;
10760
10761 case offsetof(struct bpf_sk_lookup, protocol):
10762 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10763 bpf_target_off(struct bpf_sk_lookup_kern,
10764 protocol, 2, target_size));
10765 break;
10766
10767 case offsetof(struct bpf_sk_lookup, remote_ip4):
10768 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10769 bpf_target_off(struct bpf_sk_lookup_kern,
10770 v4.saddr, 4, target_size));
10771 break;
10772
10773 case offsetof(struct bpf_sk_lookup, local_ip4):
10774 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10775 bpf_target_off(struct bpf_sk_lookup_kern,
10776 v4.daddr, 4, target_size));
10777 break;
10778
10779 case bpf_ctx_range_till(struct bpf_sk_lookup,
10780 remote_ip6[0], remote_ip6[3]): {
10781#if IS_ENABLED(CONFIG_IPV6)
10782 int off = si->off;
10783
10784 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]);
10785 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
10786 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
10787 offsetof(struct bpf_sk_lookup_kern, v6.saddr));
10788 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10789 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
10790#else
10791 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10792#endif
10793 break;
10794 }
10795 case bpf_ctx_range_till(struct bpf_sk_lookup,
10796 local_ip6[0], local_ip6[3]): {
10797#if IS_ENABLED(CONFIG_IPV6)
10798 int off = si->off;
10799
10800 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]);
10801 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
10802 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
10803 offsetof(struct bpf_sk_lookup_kern, v6.daddr));
10804 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10805 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
10806#else
10807 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10808#endif
10809 break;
af7ec138 10810 }
e9ddbb77
JS
10811 case offsetof(struct bpf_sk_lookup, remote_port):
10812 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10813 bpf_target_off(struct bpf_sk_lookup_kern,
10814 sport, 2, target_size));
10815 break;
10816
10817 case offsetof(struct bpf_sk_lookup, local_port):
10818 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10819 bpf_target_off(struct bpf_sk_lookup_kern,
10820 dport, 2, target_size));
10821 break;
f8931565
MP
10822
10823 case offsetof(struct bpf_sk_lookup, ingress_ifindex):
10824 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10825 bpf_target_off(struct bpf_sk_lookup_kern,
10826 ingress_ifindex, 4, target_size));
10827 break;
e9ddbb77
JS
10828 }
10829
10830 return insn - insn_buf;
af7ec138 10831}
e9ddbb77
JS
10832
10833const struct bpf_prog_ops sk_lookup_prog_ops = {
7c32e8f8 10834 .test_run = bpf_prog_test_run_sk_lookup,
e9ddbb77
JS
10835};
10836
10837const struct bpf_verifier_ops sk_lookup_verifier_ops = {
10838 .get_func_proto = sk_lookup_func_proto,
10839 .is_valid_access = sk_lookup_is_valid_access,
10840 .convert_ctx_access = sk_lookup_convert_ctx_access,
10841};
10842
2dbb9b9e 10843#endif /* CONFIG_INET */
7e6897f9 10844
6a64037d 10845DEFINE_BPF_DISPATCHER(xdp)
7e6897f9
BT
10846
10847void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
10848{
6a64037d 10849 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
7e6897f9 10850}
af7ec138 10851
9e2ad638 10852BTF_ID_LIST_GLOBAL(btf_sock_ids, MAX_BTF_SOCK_TYPE)
bc4f0548 10853#define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
af7ec138
YS
10854BTF_SOCK_TYPE_xxx
10855#undef BTF_SOCK_TYPE
af7ec138 10856
af7ec138
YS
10857BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
10858{
10859 /* tcp6_sock type is not generated in dwarf and hence btf,
10860 * trigger an explicit type generation here.
10861 */
10862 BTF_TYPE_EMIT(struct tcp6_sock);
8c33dadc 10863 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
af7ec138
YS
10864 sk->sk_family == AF_INET6)
10865 return (unsigned long)sk;
10866
10867 return (unsigned long)NULL;
10868}
10869
10870const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
10871 .func = bpf_skc_to_tcp6_sock,
10872 .gpl_only = false,
10873 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
1df8f55a 10874 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
af7ec138
YS
10875 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
10876};
478cfbdf
YS
10877
10878BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
10879{
8c33dadc 10880 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
478cfbdf
YS
10881 return (unsigned long)sk;
10882
10883 return (unsigned long)NULL;
10884}
10885
10886const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
10887 .func = bpf_skc_to_tcp_sock,
10888 .gpl_only = false,
10889 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
1df8f55a 10890 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
478cfbdf
YS
10891 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
10892};
10893
10894BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
10895{
d82a532a
YS
10896 /* BTF types for tcp_timewait_sock and inet_timewait_sock are not
10897 * generated if CONFIG_INET=n. Trigger an explicit generation here.
10898 */
10899 BTF_TYPE_EMIT(struct inet_timewait_sock);
10900 BTF_TYPE_EMIT(struct tcp_timewait_sock);
10901
6b207d66 10902#ifdef CONFIG_INET
8c33dadc 10903 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
478cfbdf 10904 return (unsigned long)sk;
6b207d66 10905#endif
478cfbdf
YS
10906
10907#if IS_BUILTIN(CONFIG_IPV6)
8c33dadc 10908 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
478cfbdf
YS
10909 return (unsigned long)sk;
10910#endif
10911
10912 return (unsigned long)NULL;
10913}
10914
10915const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
10916 .func = bpf_skc_to_tcp_timewait_sock,
10917 .gpl_only = false,
10918 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
1df8f55a 10919 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
478cfbdf
YS
10920 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
10921};
10922
10923BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
10924{
6b207d66 10925#ifdef CONFIG_INET
8c33dadc 10926 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
478cfbdf 10927 return (unsigned long)sk;
6b207d66 10928#endif
478cfbdf
YS
10929
10930#if IS_BUILTIN(CONFIG_IPV6)
8c33dadc 10931 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
478cfbdf
YS
10932 return (unsigned long)sk;
10933#endif
10934
10935 return (unsigned long)NULL;
10936}
10937
10938const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
10939 .func = bpf_skc_to_tcp_request_sock,
10940 .gpl_only = false,
10941 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
1df8f55a 10942 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
478cfbdf
YS
10943 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
10944};
0d4fad3e
YS
10945
10946BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
10947{
10948 /* udp6_sock type is not generated in dwarf and hence btf,
10949 * trigger an explicit type generation here.
10950 */
10951 BTF_TYPE_EMIT(struct udp6_sock);
8c33dadc 10952 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
0d4fad3e
YS
10953 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
10954 return (unsigned long)sk;
10955
10956 return (unsigned long)NULL;
10957}
10958
10959const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
10960 .func = bpf_skc_to_udp6_sock,
10961 .gpl_only = false,
10962 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
1df8f55a 10963 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
0d4fad3e
YS
10964 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
10965};
1df8f55a 10966
9eeb3aa3
HC
10967BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
10968{
10969 /* unix_sock type is not generated in dwarf and hence btf,
10970 * trigger an explicit type generation here.
10971 */
10972 BTF_TYPE_EMIT(struct unix_sock);
10973 if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX)
10974 return (unsigned long)sk;
10975
10976 return (unsigned long)NULL;
10977}
10978
10979const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
10980 .func = bpf_skc_to_unix_sock,
10981 .gpl_only = false,
10982 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10983 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10984 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
10985};
10986
b60da495
FR
10987BPF_CALL_1(bpf_sock_from_file, struct file *, file)
10988{
10989 return (unsigned long)sock_from_file(file);
10990}
10991
10992BTF_ID_LIST(bpf_sock_from_file_btf_ids)
10993BTF_ID(struct, socket)
10994BTF_ID(struct, file)
10995
10996const struct bpf_func_proto bpf_sock_from_file_proto = {
10997 .func = bpf_sock_from_file,
10998 .gpl_only = false,
10999 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11000 .ret_btf_id = &bpf_sock_from_file_btf_ids[0],
11001 .arg1_type = ARG_PTR_TO_BTF_ID,
11002 .arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
11003};
11004
1df8f55a
MKL
11005static const struct bpf_func_proto *
11006bpf_sk_base_func_proto(enum bpf_func_id func_id)
11007{
11008 const struct bpf_func_proto *func;
11009
11010 switch (func_id) {
11011 case BPF_FUNC_skc_to_tcp6_sock:
11012 func = &bpf_skc_to_tcp6_sock_proto;
11013 break;
11014 case BPF_FUNC_skc_to_tcp_sock:
11015 func = &bpf_skc_to_tcp_sock_proto;
11016 break;
11017 case BPF_FUNC_skc_to_tcp_timewait_sock:
11018 func = &bpf_skc_to_tcp_timewait_sock_proto;
11019 break;
11020 case BPF_FUNC_skc_to_tcp_request_sock:
11021 func = &bpf_skc_to_tcp_request_sock_proto;
11022 break;
11023 case BPF_FUNC_skc_to_udp6_sock:
11024 func = &bpf_skc_to_udp6_sock_proto;
11025 break;
9eeb3aa3
HC
11026 case BPF_FUNC_skc_to_unix_sock:
11027 func = &bpf_skc_to_unix_sock_proto;
11028 break;
5e0bc308
DB
11029 case BPF_FUNC_ktime_get_coarse_ns:
11030 return &bpf_ktime_get_coarse_ns_proto;
1df8f55a
MKL
11031 default:
11032 return bpf_base_func_proto(func_id);
11033 }
11034
11035 if (!perfmon_capable())
11036 return NULL;
11037
11038 return func;
11039}