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