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