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