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