]> git.ipfire.org Git - thirdparty/linux.git/blame - net/core/filter.c
veth: Add ndo_xdp_xmit
[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
27b29f63
AS
2085struct redirect_info {
2086 u32 ifindex;
2087 u32 flags;
97f91a7c 2088 struct bpf_map *map;
11393cc9 2089 struct bpf_map *map_to_flush;
7c300131 2090 unsigned long map_owner;
27b29f63
AS
2091};
2092
2093static DEFINE_PER_CPU(struct redirect_info, redirect_info);
781c53bc 2094
f3694e00 2095BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
27b29f63
AS
2096{
2097 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2098
781c53bc
DB
2099 if (unlikely(flags & ~(BPF_F_INGRESS)))
2100 return TC_ACT_SHOT;
2101
27b29f63
AS
2102 ri->ifindex = ifindex;
2103 ri->flags = flags;
781c53bc 2104
27b29f63
AS
2105 return TC_ACT_REDIRECT;
2106}
2107
2108int skb_do_redirect(struct sk_buff *skb)
2109{
2110 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2111 struct net_device *dev;
2112
2113 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2114 ri->ifindex = 0;
2115 if (unlikely(!dev)) {
2116 kfree_skb(skb);
2117 return -EINVAL;
2118 }
2119
4e3264d2 2120 return __bpf_redirect(skb, dev, ri->flags);
27b29f63
AS
2121}
2122
577c50aa 2123static const struct bpf_func_proto bpf_redirect_proto = {
27b29f63
AS
2124 .func = bpf_redirect,
2125 .gpl_only = false,
2126 .ret_type = RET_INTEGER,
2127 .arg1_type = ARG_ANYTHING,
2128 .arg2_type = ARG_ANYTHING,
2129};
2130
81110384
JF
2131BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
2132 struct bpf_map *, map, void *, key, u64, flags)
2133{
2134 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2135
2136 /* If user passes invalid input drop the packet. */
2137 if (unlikely(flags & ~(BPF_F_INGRESS)))
2138 return SK_DROP;
2139
2140 tcb->bpf.flags = flags;
2141 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
2142 if (!tcb->bpf.sk_redir)
2143 return SK_DROP;
2144
2145 return SK_PASS;
2146}
2147
2148static const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
2149 .func = bpf_sk_redirect_hash,
2150 .gpl_only = false,
2151 .ret_type = RET_INTEGER,
2152 .arg1_type = ARG_PTR_TO_CTX,
2153 .arg2_type = ARG_CONST_MAP_PTR,
2154 .arg3_type = ARG_PTR_TO_MAP_KEY,
2155 .arg4_type = ARG_ANYTHING,
2156};
2157
34f79502
JF
2158BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
2159 struct bpf_map *, map, u32, key, u64, flags)
174a79ff 2160{
34f79502 2161 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
174a79ff 2162
bfa64075 2163 /* If user passes invalid input drop the packet. */
fa246693 2164 if (unlikely(flags & ~(BPF_F_INGRESS)))
bfa64075 2165 return SK_DROP;
174a79ff 2166
34f79502 2167 tcb->bpf.flags = flags;
e5cd3abc
JF
2168 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
2169 if (!tcb->bpf.sk_redir)
2170 return SK_DROP;
174a79ff 2171
bfa64075 2172 return SK_PASS;
174a79ff
JF
2173}
2174
34f79502 2175struct sock *do_sk_redirect_map(struct sk_buff *skb)
174a79ff 2176{
34f79502 2177 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
174a79ff 2178
e5cd3abc 2179 return tcb->bpf.sk_redir;
174a79ff
JF
2180}
2181
2182static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
2183 .func = bpf_sk_redirect_map,
2184 .gpl_only = false,
2185 .ret_type = RET_INTEGER,
34f79502
JF
2186 .arg1_type = ARG_PTR_TO_CTX,
2187 .arg2_type = ARG_CONST_MAP_PTR,
174a79ff 2188 .arg3_type = ARG_ANYTHING,
34f79502 2189 .arg4_type = ARG_ANYTHING,
174a79ff
JF
2190};
2191
81110384
JF
2192BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg_buff *, msg,
2193 struct bpf_map *, map, void *, key, u64, flags)
2194{
2195 /* If user passes invalid input drop the packet. */
2196 if (unlikely(flags & ~(BPF_F_INGRESS)))
2197 return SK_DROP;
2198
2199 msg->flags = flags;
2200 msg->sk_redir = __sock_hash_lookup_elem(map, key);
2201 if (!msg->sk_redir)
2202 return SK_DROP;
2203
2204 return SK_PASS;
2205}
2206
2207static const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
2208 .func = bpf_msg_redirect_hash,
2209 .gpl_only = false,
2210 .ret_type = RET_INTEGER,
2211 .arg1_type = ARG_PTR_TO_CTX,
2212 .arg2_type = ARG_CONST_MAP_PTR,
2213 .arg3_type = ARG_PTR_TO_MAP_KEY,
2214 .arg4_type = ARG_ANYTHING,
2215};
2216
4f738adb
JF
2217BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg_buff *, msg,
2218 struct bpf_map *, map, u32, key, u64, flags)
2219{
2220 /* If user passes invalid input drop the packet. */
8934ce2f 2221 if (unlikely(flags & ~(BPF_F_INGRESS)))
4f738adb
JF
2222 return SK_DROP;
2223
4f738adb 2224 msg->flags = flags;
e5cd3abc
JF
2225 msg->sk_redir = __sock_map_lookup_elem(map, key);
2226 if (!msg->sk_redir)
2227 return SK_DROP;
4f738adb
JF
2228
2229 return SK_PASS;
2230}
2231
2232struct sock *do_msg_redirect_map(struct sk_msg_buff *msg)
2233{
e5cd3abc 2234 return msg->sk_redir;
4f738adb
JF
2235}
2236
2237static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
2238 .func = bpf_msg_redirect_map,
2239 .gpl_only = false,
2240 .ret_type = RET_INTEGER,
2241 .arg1_type = ARG_PTR_TO_CTX,
2242 .arg2_type = ARG_CONST_MAP_PTR,
2243 .arg3_type = ARG_ANYTHING,
2244 .arg4_type = ARG_ANYTHING,
2245};
2246
2a100317
JF
2247BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg_buff *, msg, u32, bytes)
2248{
2249 msg->apply_bytes = bytes;
2250 return 0;
2251}
2252
2253static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2254 .func = bpf_msg_apply_bytes,
2255 .gpl_only = false,
2256 .ret_type = RET_INTEGER,
2257 .arg1_type = ARG_PTR_TO_CTX,
2258 .arg2_type = ARG_ANYTHING,
2259};
2260
91843d54
JF
2261BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg_buff *, msg, u32, bytes)
2262{
2263 msg->cork_bytes = bytes;
2264 return 0;
2265}
2266
2267static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2268 .func = bpf_msg_cork_bytes,
2269 .gpl_only = false,
2270 .ret_type = RET_INTEGER,
2271 .arg1_type = ARG_PTR_TO_CTX,
2272 .arg2_type = ARG_ANYTHING,
2273};
2274
015632bb
JF
2275BPF_CALL_4(bpf_msg_pull_data,
2276 struct sk_msg_buff *, msg, u32, start, u32, end, u64, flags)
2277{
2278 unsigned int len = 0, offset = 0, copy = 0;
2279 struct scatterlist *sg = msg->sg_data;
2280 int first_sg, last_sg, i, shift;
2281 unsigned char *p, *to, *from;
2282 int bytes = end - start;
2283 struct page *page;
2284
2285 if (unlikely(flags || end <= start))
2286 return -EINVAL;
2287
2288 /* First find the starting scatterlist element */
2289 i = msg->sg_start;
2290 do {
2291 len = sg[i].length;
2292 offset += len;
2293 if (start < offset + len)
2294 break;
2295 i++;
2296 if (i == MAX_SKB_FRAGS)
2297 i = 0;
2298 } while (i != msg->sg_end);
2299
2300 if (unlikely(start >= offset + len))
2301 return -EINVAL;
2302
2303 if (!msg->sg_copy[i] && bytes <= len)
2304 goto out;
2305
2306 first_sg = i;
2307
2308 /* At this point we need to linearize multiple scatterlist
2309 * elements or a single shared page. Either way we need to
2310 * copy into a linear buffer exclusively owned by BPF. Then
2311 * place the buffer in the scatterlist and fixup the original
2312 * entries by removing the entries now in the linear buffer
2313 * and shifting the remaining entries. For now we do not try
2314 * to copy partial entries to avoid complexity of running out
2315 * of sg_entry slots. The downside is reading a single byte
2316 * will copy the entire sg entry.
2317 */
2318 do {
2319 copy += sg[i].length;
2320 i++;
2321 if (i == MAX_SKB_FRAGS)
2322 i = 0;
2323 if (bytes < copy)
2324 break;
2325 } while (i != msg->sg_end);
2326 last_sg = i;
2327
2328 if (unlikely(copy < end - start))
2329 return -EINVAL;
2330
2331 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC, get_order(copy));
2332 if (unlikely(!page))
2333 return -ENOMEM;
2334 p = page_address(page);
2335 offset = 0;
2336
2337 i = first_sg;
2338 do {
2339 from = sg_virt(&sg[i]);
2340 len = sg[i].length;
2341 to = p + offset;
2342
2343 memcpy(to, from, len);
2344 offset += len;
2345 sg[i].length = 0;
2346 put_page(sg_page(&sg[i]));
2347
2348 i++;
2349 if (i == MAX_SKB_FRAGS)
2350 i = 0;
2351 } while (i != last_sg);
2352
2353 sg[first_sg].length = copy;
2354 sg_set_page(&sg[first_sg], page, copy, 0);
2355
2356 /* To repair sg ring we need to shift entries. If we only
2357 * had a single entry though we can just replace it and
2358 * be done. Otherwise walk the ring and shift the entries.
2359 */
2360 shift = last_sg - first_sg - 1;
2361 if (!shift)
2362 goto out;
2363
2364 i = first_sg + 1;
2365 do {
2366 int move_from;
2367
2368 if (i + shift >= MAX_SKB_FRAGS)
2369 move_from = i + shift - MAX_SKB_FRAGS;
2370 else
2371 move_from = i + shift;
2372
2373 if (move_from == msg->sg_end)
2374 break;
2375
2376 sg[i] = sg[move_from];
2377 sg[move_from].length = 0;
2378 sg[move_from].page_link = 0;
2379 sg[move_from].offset = 0;
2380
2381 i++;
2382 if (i == MAX_SKB_FRAGS)
2383 i = 0;
2384 } while (1);
2385 msg->sg_end -= shift;
2386 if (msg->sg_end < 0)
2387 msg->sg_end += MAX_SKB_FRAGS;
2388out:
2389 msg->data = sg_virt(&sg[i]) + start - offset;
2390 msg->data_end = msg->data + bytes;
2391
2392 return 0;
2393}
2394
2395static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2396 .func = bpf_msg_pull_data,
2397 .gpl_only = false,
2398 .ret_type = RET_INTEGER,
2399 .arg1_type = ARG_PTR_TO_CTX,
2400 .arg2_type = ARG_ANYTHING,
2401 .arg3_type = ARG_ANYTHING,
2402 .arg4_type = ARG_ANYTHING,
2403};
2404
f3694e00 2405BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
8d20aabe 2406{
f3694e00 2407 return task_get_classid(skb);
8d20aabe
DB
2408}
2409
2410static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2411 .func = bpf_get_cgroup_classid,
2412 .gpl_only = false,
2413 .ret_type = RET_INTEGER,
2414 .arg1_type = ARG_PTR_TO_CTX,
2415};
2416
f3694e00 2417BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
c46646d0 2418{
f3694e00 2419 return dst_tclassid(skb);
c46646d0
DB
2420}
2421
2422static const struct bpf_func_proto bpf_get_route_realm_proto = {
2423 .func = bpf_get_route_realm,
2424 .gpl_only = false,
2425 .ret_type = RET_INTEGER,
2426 .arg1_type = ARG_PTR_TO_CTX,
2427};
2428
f3694e00 2429BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
13c5c240
DB
2430{
2431 /* If skb_clear_hash() was called due to mangling, we can
2432 * trigger SW recalculation here. Later access to hash
2433 * can then use the inline skb->hash via context directly
2434 * instead of calling this helper again.
2435 */
f3694e00 2436 return skb_get_hash(skb);
13c5c240
DB
2437}
2438
2439static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2440 .func = bpf_get_hash_recalc,
2441 .gpl_only = false,
2442 .ret_type = RET_INTEGER,
2443 .arg1_type = ARG_PTR_TO_CTX,
2444};
2445
7a4b28c6
DB
2446BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2447{
2448 /* After all direct packet write, this can be used once for
2449 * triggering a lazy recalc on next skb_get_hash() invocation.
2450 */
2451 skb_clear_hash(skb);
2452 return 0;
2453}
2454
2455static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2456 .func = bpf_set_hash_invalid,
2457 .gpl_only = false,
2458 .ret_type = RET_INTEGER,
2459 .arg1_type = ARG_PTR_TO_CTX,
2460};
2461
ded092cd
DB
2462BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2463{
2464 /* Set user specified hash as L4(+), so that it gets returned
2465 * on skb_get_hash() call unless BPF prog later on triggers a
2466 * skb_clear_hash().
2467 */
2468 __skb_set_sw_hash(skb, hash, true);
2469 return 0;
2470}
2471
2472static const struct bpf_func_proto bpf_set_hash_proto = {
2473 .func = bpf_set_hash,
2474 .gpl_only = false,
2475 .ret_type = RET_INTEGER,
2476 .arg1_type = ARG_PTR_TO_CTX,
2477 .arg2_type = ARG_ANYTHING,
2478};
2479
f3694e00
DB
2480BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2481 u16, vlan_tci)
4e10df9a 2482{
db58ba45 2483 int ret;
4e10df9a
AS
2484
2485 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2486 vlan_proto != htons(ETH_P_8021AD)))
2487 vlan_proto = htons(ETH_P_8021Q);
2488
8065694e 2489 bpf_push_mac_rcsum(skb);
db58ba45 2490 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
8065694e
DB
2491 bpf_pull_mac_rcsum(skb);
2492
6aaae2b6 2493 bpf_compute_data_pointers(skb);
db58ba45 2494 return ret;
4e10df9a
AS
2495}
2496
93731ef0 2497static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
4e10df9a
AS
2498 .func = bpf_skb_vlan_push,
2499 .gpl_only = false,
2500 .ret_type = RET_INTEGER,
2501 .arg1_type = ARG_PTR_TO_CTX,
2502 .arg2_type = ARG_ANYTHING,
2503 .arg3_type = ARG_ANYTHING,
2504};
2505
f3694e00 2506BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
4e10df9a 2507{
db58ba45 2508 int ret;
4e10df9a 2509
8065694e 2510 bpf_push_mac_rcsum(skb);
db58ba45 2511 ret = skb_vlan_pop(skb);
8065694e
DB
2512 bpf_pull_mac_rcsum(skb);
2513
6aaae2b6 2514 bpf_compute_data_pointers(skb);
db58ba45 2515 return ret;
4e10df9a
AS
2516}
2517
93731ef0 2518static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
4e10df9a
AS
2519 .func = bpf_skb_vlan_pop,
2520 .gpl_only = false,
2521 .ret_type = RET_INTEGER,
2522 .arg1_type = ARG_PTR_TO_CTX,
2523};
2524
6578171a
DB
2525static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2526{
2527 /* Caller already did skb_cow() with len as headroom,
2528 * so no need to do it here.
2529 */
2530 skb_push(skb, len);
2531 memmove(skb->data, skb->data + len, off);
2532 memset(skb->data + off, 0, len);
2533
2534 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2535 * needed here as it does not change the skb->csum
2536 * result for checksum complete when summing over
2537 * zeroed blocks.
2538 */
2539 return 0;
2540}
2541
2542static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2543{
2544 /* skb_ensure_writable() is not needed here, as we're
2545 * already working on an uncloned skb.
2546 */
2547 if (unlikely(!pskb_may_pull(skb, off + len)))
2548 return -ENOMEM;
2549
2550 skb_postpull_rcsum(skb, skb->data + off, len);
2551 memmove(skb->data + len, skb->data, off);
2552 __skb_pull(skb, len);
2553
2554 return 0;
2555}
2556
2557static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2558{
2559 bool trans_same = skb->transport_header == skb->network_header;
2560 int ret;
2561
2562 /* There's no need for __skb_push()/__skb_pull() pair to
2563 * get to the start of the mac header as we're guaranteed
2564 * to always start from here under eBPF.
2565 */
2566 ret = bpf_skb_generic_push(skb, off, len);
2567 if (likely(!ret)) {
2568 skb->mac_header -= len;
2569 skb->network_header -= len;
2570 if (trans_same)
2571 skb->transport_header = skb->network_header;
2572 }
2573
2574 return ret;
2575}
2576
2577static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2578{
2579 bool trans_same = skb->transport_header == skb->network_header;
2580 int ret;
2581
2582 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2583 ret = bpf_skb_generic_pop(skb, off, len);
2584 if (likely(!ret)) {
2585 skb->mac_header += len;
2586 skb->network_header += len;
2587 if (trans_same)
2588 skb->transport_header = skb->network_header;
2589 }
2590
2591 return ret;
2592}
2593
2594static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2595{
2596 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
0daf4349 2597 u32 off = skb_mac_header_len(skb);
6578171a
DB
2598 int ret;
2599
d02f51cb
DA
2600 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2601 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2602 return -ENOTSUPP;
2603
6578171a
DB
2604 ret = skb_cow(skb, len_diff);
2605 if (unlikely(ret < 0))
2606 return ret;
2607
2608 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2609 if (unlikely(ret < 0))
2610 return ret;
2611
2612 if (skb_is_gso(skb)) {
d02f51cb
DA
2613 struct skb_shared_info *shinfo = skb_shinfo(skb);
2614
880388aa
DM
2615 /* SKB_GSO_TCPV4 needs to be changed into
2616 * SKB_GSO_TCPV6.
6578171a 2617 */
d02f51cb
DA
2618 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2619 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2620 shinfo->gso_type |= SKB_GSO_TCPV6;
6578171a
DB
2621 }
2622
2623 /* Due to IPv6 header, MSS needs to be downgraded. */
d02f51cb 2624 skb_decrease_gso_size(shinfo, len_diff);
6578171a 2625 /* Header must be checked, and gso_segs recomputed. */
d02f51cb
DA
2626 shinfo->gso_type |= SKB_GSO_DODGY;
2627 shinfo->gso_segs = 0;
6578171a
DB
2628 }
2629
2630 skb->protocol = htons(ETH_P_IPV6);
2631 skb_clear_hash(skb);
2632
2633 return 0;
2634}
2635
2636static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2637{
2638 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
0daf4349 2639 u32 off = skb_mac_header_len(skb);
6578171a
DB
2640 int ret;
2641
d02f51cb
DA
2642 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2643 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2644 return -ENOTSUPP;
2645
6578171a
DB
2646 ret = skb_unclone(skb, GFP_ATOMIC);
2647 if (unlikely(ret < 0))
2648 return ret;
2649
2650 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2651 if (unlikely(ret < 0))
2652 return ret;
2653
2654 if (skb_is_gso(skb)) {
d02f51cb
DA
2655 struct skb_shared_info *shinfo = skb_shinfo(skb);
2656
880388aa
DM
2657 /* SKB_GSO_TCPV6 needs to be changed into
2658 * SKB_GSO_TCPV4.
6578171a 2659 */
d02f51cb
DA
2660 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2661 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2662 shinfo->gso_type |= SKB_GSO_TCPV4;
6578171a
DB
2663 }
2664
2665 /* Due to IPv4 header, MSS can be upgraded. */
d02f51cb 2666 skb_increase_gso_size(shinfo, len_diff);
6578171a 2667 /* Header must be checked, and gso_segs recomputed. */
d02f51cb
DA
2668 shinfo->gso_type |= SKB_GSO_DODGY;
2669 shinfo->gso_segs = 0;
6578171a
DB
2670 }
2671
2672 skb->protocol = htons(ETH_P_IP);
2673 skb_clear_hash(skb);
2674
2675 return 0;
2676}
2677
2678static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2679{
2680 __be16 from_proto = skb->protocol;
2681
2682 if (from_proto == htons(ETH_P_IP) &&
2683 to_proto == htons(ETH_P_IPV6))
2684 return bpf_skb_proto_4_to_6(skb);
2685
2686 if (from_proto == htons(ETH_P_IPV6) &&
2687 to_proto == htons(ETH_P_IP))
2688 return bpf_skb_proto_6_to_4(skb);
2689
2690 return -ENOTSUPP;
2691}
2692
f3694e00
DB
2693BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2694 u64, flags)
6578171a 2695{
6578171a
DB
2696 int ret;
2697
2698 if (unlikely(flags))
2699 return -EINVAL;
2700
2701 /* General idea is that this helper does the basic groundwork
2702 * needed for changing the protocol, and eBPF program fills the
2703 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2704 * and other helpers, rather than passing a raw buffer here.
2705 *
2706 * The rationale is to keep this minimal and without a need to
2707 * deal with raw packet data. F.e. even if we would pass buffers
2708 * here, the program still needs to call the bpf_lX_csum_replace()
2709 * helpers anyway. Plus, this way we keep also separation of
2710 * concerns, since f.e. bpf_skb_store_bytes() should only take
2711 * care of stores.
2712 *
2713 * Currently, additional options and extension header space are
2714 * not supported, but flags register is reserved so we can adapt
2715 * that. For offloads, we mark packet as dodgy, so that headers
2716 * need to be verified first.
2717 */
2718 ret = bpf_skb_proto_xlat(skb, proto);
6aaae2b6 2719 bpf_compute_data_pointers(skb);
6578171a
DB
2720 return ret;
2721}
2722
2723static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2724 .func = bpf_skb_change_proto,
2725 .gpl_only = false,
2726 .ret_type = RET_INTEGER,
2727 .arg1_type = ARG_PTR_TO_CTX,
2728 .arg2_type = ARG_ANYTHING,
2729 .arg3_type = ARG_ANYTHING,
2730};
2731
f3694e00 2732BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
d2485c42 2733{
d2485c42 2734 /* We only allow a restricted subset to be changed for now. */
45c7fffa
DB
2735 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2736 !skb_pkt_type_ok(pkt_type)))
d2485c42
DB
2737 return -EINVAL;
2738
2739 skb->pkt_type = pkt_type;
2740 return 0;
2741}
2742
2743static const struct bpf_func_proto bpf_skb_change_type_proto = {
2744 .func = bpf_skb_change_type,
2745 .gpl_only = false,
2746 .ret_type = RET_INTEGER,
2747 .arg1_type = ARG_PTR_TO_CTX,
2748 .arg2_type = ARG_ANYTHING,
2749};
2750
2be7e212
DB
2751static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2752{
2753 switch (skb->protocol) {
2754 case htons(ETH_P_IP):
2755 return sizeof(struct iphdr);
2756 case htons(ETH_P_IPV6):
2757 return sizeof(struct ipv6hdr);
2758 default:
2759 return ~0U;
2760 }
2761}
2762
2763static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2764{
2765 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2766 int ret;
2767
d02f51cb
DA
2768 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2769 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2770 return -ENOTSUPP;
2771
2be7e212
DB
2772 ret = skb_cow(skb, len_diff);
2773 if (unlikely(ret < 0))
2774 return ret;
2775
2776 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2777 if (unlikely(ret < 0))
2778 return ret;
2779
2780 if (skb_is_gso(skb)) {
d02f51cb
DA
2781 struct skb_shared_info *shinfo = skb_shinfo(skb);
2782
2be7e212 2783 /* Due to header grow, MSS needs to be downgraded. */
d02f51cb 2784 skb_decrease_gso_size(shinfo, len_diff);
2be7e212 2785 /* Header must be checked, and gso_segs recomputed. */
d02f51cb
DA
2786 shinfo->gso_type |= SKB_GSO_DODGY;
2787 shinfo->gso_segs = 0;
2be7e212
DB
2788 }
2789
2790 return 0;
2791}
2792
2793static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2794{
2795 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2796 int ret;
2797
d02f51cb
DA
2798 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2799 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2800 return -ENOTSUPP;
2801
2be7e212
DB
2802 ret = skb_unclone(skb, GFP_ATOMIC);
2803 if (unlikely(ret < 0))
2804 return ret;
2805
2806 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2807 if (unlikely(ret < 0))
2808 return ret;
2809
2810 if (skb_is_gso(skb)) {
d02f51cb
DA
2811 struct skb_shared_info *shinfo = skb_shinfo(skb);
2812
2be7e212 2813 /* Due to header shrink, MSS can be upgraded. */
d02f51cb 2814 skb_increase_gso_size(shinfo, len_diff);
2be7e212 2815 /* Header must be checked, and gso_segs recomputed. */
d02f51cb
DA
2816 shinfo->gso_type |= SKB_GSO_DODGY;
2817 shinfo->gso_segs = 0;
2be7e212
DB
2818 }
2819
2820 return 0;
2821}
2822
2823static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2824{
0c6bc6e5
JF
2825 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
2826 SKB_MAX_ALLOC;
2be7e212
DB
2827}
2828
2829static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2830{
2831 bool trans_same = skb->transport_header == skb->network_header;
2832 u32 len_cur, len_diff_abs = abs(len_diff);
2833 u32 len_min = bpf_skb_net_base_len(skb);
2834 u32 len_max = __bpf_skb_max_len(skb);
2835 __be16 proto = skb->protocol;
2836 bool shrink = len_diff < 0;
2837 int ret;
2838
2839 if (unlikely(len_diff_abs > 0xfffU))
2840 return -EFAULT;
2841 if (unlikely(proto != htons(ETH_P_IP) &&
2842 proto != htons(ETH_P_IPV6)))
2843 return -ENOTSUPP;
2844
2845 len_cur = skb->len - skb_network_offset(skb);
2846 if (skb_transport_header_was_set(skb) && !trans_same)
2847 len_cur = skb_network_header_len(skb);
2848 if ((shrink && (len_diff_abs >= len_cur ||
2849 len_cur - len_diff_abs < len_min)) ||
2850 (!shrink && (skb->len + len_diff_abs > len_max &&
2851 !skb_is_gso(skb))))
2852 return -ENOTSUPP;
2853
2854 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2855 bpf_skb_net_grow(skb, len_diff_abs);
2856
6aaae2b6 2857 bpf_compute_data_pointers(skb);
e4a6a342 2858 return ret;
2be7e212
DB
2859}
2860
2861BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2862 u32, mode, u64, flags)
2863{
2864 if (unlikely(flags))
2865 return -EINVAL;
2866 if (likely(mode == BPF_ADJ_ROOM_NET))
2867 return bpf_skb_adjust_net(skb, len_diff);
2868
2869 return -ENOTSUPP;
2870}
2871
2872static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2873 .func = bpf_skb_adjust_room,
2874 .gpl_only = false,
2875 .ret_type = RET_INTEGER,
2876 .arg1_type = ARG_PTR_TO_CTX,
2877 .arg2_type = ARG_ANYTHING,
2878 .arg3_type = ARG_ANYTHING,
2879 .arg4_type = ARG_ANYTHING,
2880};
2881
5293efe6
DB
2882static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2883{
2884 u32 min_len = skb_network_offset(skb);
2885
2886 if (skb_transport_header_was_set(skb))
2887 min_len = skb_transport_offset(skb);
2888 if (skb->ip_summed == CHECKSUM_PARTIAL)
2889 min_len = skb_checksum_start_offset(skb) +
2890 skb->csum_offset + sizeof(__sum16);
2891 return min_len;
2892}
2893
5293efe6
DB
2894static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2895{
2896 unsigned int old_len = skb->len;
2897 int ret;
2898
2899 ret = __skb_grow_rcsum(skb, new_len);
2900 if (!ret)
2901 memset(skb->data + old_len, 0, new_len - old_len);
2902 return ret;
2903}
2904
2905static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2906{
2907 return __skb_trim_rcsum(skb, new_len);
2908}
2909
0ea488ff
JF
2910static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
2911 u64 flags)
5293efe6 2912{
5293efe6
DB
2913 u32 max_len = __bpf_skb_max_len(skb);
2914 u32 min_len = __bpf_skb_min_len(skb);
5293efe6
DB
2915 int ret;
2916
2917 if (unlikely(flags || new_len > max_len || new_len < min_len))
2918 return -EINVAL;
2919 if (skb->encapsulation)
2920 return -ENOTSUPP;
2921
2922 /* The basic idea of this helper is that it's performing the
2923 * needed work to either grow or trim an skb, and eBPF program
2924 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2925 * bpf_lX_csum_replace() and others rather than passing a raw
2926 * buffer here. This one is a slow path helper and intended
2927 * for replies with control messages.
2928 *
2929 * Like in bpf_skb_change_proto(), we want to keep this rather
2930 * minimal and without protocol specifics so that we are able
2931 * to separate concerns as in bpf_skb_store_bytes() should only
2932 * be the one responsible for writing buffers.
2933 *
2934 * It's really expected to be a slow path operation here for
2935 * control message replies, so we're implicitly linearizing,
2936 * uncloning and drop offloads from the skb by this.
2937 */
2938 ret = __bpf_try_make_writable(skb, skb->len);
2939 if (!ret) {
2940 if (new_len > skb->len)
2941 ret = bpf_skb_grow_rcsum(skb, new_len);
2942 else if (new_len < skb->len)
2943 ret = bpf_skb_trim_rcsum(skb, new_len);
2944 if (!ret && skb_is_gso(skb))
2945 skb_gso_reset(skb);
2946 }
0ea488ff
JF
2947 return ret;
2948}
2949
2950BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2951 u64, flags)
2952{
2953 int ret = __bpf_skb_change_tail(skb, new_len, flags);
5293efe6 2954
6aaae2b6 2955 bpf_compute_data_pointers(skb);
5293efe6
DB
2956 return ret;
2957}
2958
2959static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2960 .func = bpf_skb_change_tail,
2961 .gpl_only = false,
2962 .ret_type = RET_INTEGER,
2963 .arg1_type = ARG_PTR_TO_CTX,
2964 .arg2_type = ARG_ANYTHING,
2965 .arg3_type = ARG_ANYTHING,
2966};
2967
0ea488ff 2968BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3a0af8fd 2969 u64, flags)
0ea488ff
JF
2970{
2971 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2972
2973 bpf_compute_data_end_sk_skb(skb);
2974 return ret;
2975}
2976
2977static const struct bpf_func_proto sk_skb_change_tail_proto = {
2978 .func = sk_skb_change_tail,
2979 .gpl_only = false,
2980 .ret_type = RET_INTEGER,
2981 .arg1_type = ARG_PTR_TO_CTX,
2982 .arg2_type = ARG_ANYTHING,
2983 .arg3_type = ARG_ANYTHING,
2984};
2985
2986static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
2987 u64 flags)
3a0af8fd
TG
2988{
2989 u32 max_len = __bpf_skb_max_len(skb);
2990 u32 new_len = skb->len + head_room;
2991 int ret;
2992
2993 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2994 new_len < skb->len))
2995 return -EINVAL;
2996
2997 ret = skb_cow(skb, head_room);
2998 if (likely(!ret)) {
2999 /* Idea for this helper is that we currently only
3000 * allow to expand on mac header. This means that
3001 * skb->protocol network header, etc, stay as is.
3002 * Compared to bpf_skb_change_tail(), we're more
3003 * flexible due to not needing to linearize or
3004 * reset GSO. Intention for this helper is to be
3005 * used by an L3 skb that needs to push mac header
3006 * for redirection into L2 device.
3007 */
3008 __skb_push(skb, head_room);
3009 memset(skb->data, 0, head_room);
3010 skb_reset_mac_header(skb);
3011 }
3012
0ea488ff
JF
3013 return ret;
3014}
3015
3016BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3017 u64, flags)
3018{
3019 int ret = __bpf_skb_change_head(skb, head_room, flags);
3020
6aaae2b6 3021 bpf_compute_data_pointers(skb);
0ea488ff 3022 return ret;
3a0af8fd
TG
3023}
3024
3025static const struct bpf_func_proto bpf_skb_change_head_proto = {
3026 .func = bpf_skb_change_head,
3027 .gpl_only = false,
3028 .ret_type = RET_INTEGER,
3029 .arg1_type = ARG_PTR_TO_CTX,
3030 .arg2_type = ARG_ANYTHING,
3031 .arg3_type = ARG_ANYTHING,
3032};
3033
0ea488ff
JF
3034BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3035 u64, flags)
3036{
3037 int ret = __bpf_skb_change_head(skb, head_room, flags);
3038
3039 bpf_compute_data_end_sk_skb(skb);
3040 return ret;
3041}
3042
3043static const struct bpf_func_proto sk_skb_change_head_proto = {
3044 .func = sk_skb_change_head,
3045 .gpl_only = false,
3046 .ret_type = RET_INTEGER,
3047 .arg1_type = ARG_PTR_TO_CTX,
3048 .arg2_type = ARG_ANYTHING,
3049 .arg3_type = ARG_ANYTHING,
3050};
de8f3a83
DB
3051static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3052{
3053 return xdp_data_meta_unsupported(xdp) ? 0 :
3054 xdp->data - xdp->data_meta;
3055}
3056
17bedab2
MKL
3057BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3058{
6dfb970d 3059 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
de8f3a83 3060 unsigned long metalen = xdp_get_metalen(xdp);
97e19cce 3061 void *data_start = xdp_frame_end + metalen;
17bedab2
MKL
3062 void *data = xdp->data + offset;
3063
de8f3a83 3064 if (unlikely(data < data_start ||
17bedab2
MKL
3065 data > xdp->data_end - ETH_HLEN))
3066 return -EINVAL;
3067
de8f3a83
DB
3068 if (metalen)
3069 memmove(xdp->data_meta + offset,
3070 xdp->data_meta, metalen);
3071 xdp->data_meta += offset;
17bedab2
MKL
3072 xdp->data = data;
3073
3074 return 0;
3075}
3076
3077static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3078 .func = bpf_xdp_adjust_head,
3079 .gpl_only = false,
3080 .ret_type = RET_INTEGER,
3081 .arg1_type = ARG_PTR_TO_CTX,
3082 .arg2_type = ARG_ANYTHING,
3083};
3084
b32cc5b9
NS
3085BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3086{
3087 void *data_end = xdp->data_end + offset;
3088
3089 /* only shrinking is allowed for now. */
3090 if (unlikely(offset >= 0))
3091 return -EINVAL;
3092
3093 if (unlikely(data_end < xdp->data + ETH_HLEN))
3094 return -EINVAL;
3095
3096 xdp->data_end = data_end;
3097
3098 return 0;
3099}
3100
3101static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3102 .func = bpf_xdp_adjust_tail,
3103 .gpl_only = false,
3104 .ret_type = RET_INTEGER,
3105 .arg1_type = ARG_PTR_TO_CTX,
3106 .arg2_type = ARG_ANYTHING,
3107};
3108
de8f3a83
DB
3109BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3110{
97e19cce 3111 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
de8f3a83
DB
3112 void *meta = xdp->data_meta + offset;
3113 unsigned long metalen = xdp->data - meta;
3114
3115 if (xdp_data_meta_unsupported(xdp))
3116 return -ENOTSUPP;
97e19cce 3117 if (unlikely(meta < xdp_frame_end ||
de8f3a83
DB
3118 meta > xdp->data))
3119 return -EINVAL;
3120 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3121 (metalen > 32)))
3122 return -EACCES;
3123
3124 xdp->data_meta = meta;
3125
3126 return 0;
3127}
3128
3129static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3130 .func = bpf_xdp_adjust_meta,
3131 .gpl_only = false,
3132 .ret_type = RET_INTEGER,
3133 .arg1_type = ARG_PTR_TO_CTX,
3134 .arg2_type = ARG_ANYTHING,
3135};
3136
11393cc9
JF
3137static int __bpf_tx_xdp(struct net_device *dev,
3138 struct bpf_map *map,
3139 struct xdp_buff *xdp,
3140 u32 index)
814abfab 3141{
44fa2dbd 3142 struct xdp_frame *xdpf;
d8d7218a 3143 int err, sent;
11393cc9
JF
3144
3145 if (!dev->netdev_ops->ndo_xdp_xmit) {
11393cc9 3146 return -EOPNOTSUPP;
814abfab 3147 }
11393cc9 3148
d8d7218a
TM
3149 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3150 if (unlikely(err))
3151 return err;
3152
44fa2dbd
JDB
3153 xdpf = convert_to_xdp_frame(xdp);
3154 if (unlikely(!xdpf))
3155 return -EOVERFLOW;
3156
1e67575a 3157 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
735fc405
JDB
3158 if (sent <= 0)
3159 return sent;
9c270af3
JDB
3160 return 0;
3161}
3162
3163static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3164 struct bpf_map *map,
3165 struct xdp_buff *xdp,
3166 u32 index)
3167{
3168 int err;
3169
1b1a251c
BT
3170 switch (map->map_type) {
3171 case BPF_MAP_TYPE_DEVMAP: {
67f29e07 3172 struct bpf_dtab_netdev *dst = fwd;
9c270af3 3173
38edddb8 3174 err = dev_map_enqueue(dst, xdp, dev_rx);
9c270af3
JDB
3175 if (err)
3176 return err;
11393cc9 3177 __dev_map_insert_ctx(map, index);
1b1a251c
BT
3178 break;
3179 }
3180 case BPF_MAP_TYPE_CPUMAP: {
9c270af3
JDB
3181 struct bpf_cpu_map_entry *rcpu = fwd;
3182
3183 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3184 if (err)
3185 return err;
3186 __cpu_map_insert_ctx(map, index);
1b1a251c
BT
3187 break;
3188 }
3189 case BPF_MAP_TYPE_XSKMAP: {
3190 struct xdp_sock *xs = fwd;
3191
3192 err = __xsk_map_redirect(map, xdp, xs);
3193 return err;
3194 }
3195 default:
3196 break;
9c270af3 3197 }
e4a8e817 3198 return 0;
814abfab
JF
3199}
3200
11393cc9
JF
3201void xdp_do_flush_map(void)
3202{
3203 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3204 struct bpf_map *map = ri->map_to_flush;
3205
11393cc9 3206 ri->map_to_flush = NULL;
9c270af3
JDB
3207 if (map) {
3208 switch (map->map_type) {
3209 case BPF_MAP_TYPE_DEVMAP:
3210 __dev_map_flush(map);
3211 break;
3212 case BPF_MAP_TYPE_CPUMAP:
3213 __cpu_map_flush(map);
3214 break;
1b1a251c
BT
3215 case BPF_MAP_TYPE_XSKMAP:
3216 __xsk_map_flush(map);
3217 break;
9c270af3
JDB
3218 default:
3219 break;
3220 }
3221 }
11393cc9
JF
3222}
3223EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3224
9c270af3
JDB
3225static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3226{
3227 switch (map->map_type) {
3228 case BPF_MAP_TYPE_DEVMAP:
3229 return __dev_map_lookup_elem(map, index);
3230 case BPF_MAP_TYPE_CPUMAP:
3231 return __cpu_map_lookup_elem(map, index);
1b1a251c
BT
3232 case BPF_MAP_TYPE_XSKMAP:
3233 return __xsk_map_lookup_elem(map, index);
9c270af3
JDB
3234 default:
3235 return NULL;
3236 }
3237}
3238
7c300131
DB
3239static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
3240 unsigned long aux)
3241{
3242 return (unsigned long)xdp_prog->aux != aux;
3243}
3244
e4a8e817
DB
3245static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3246 struct bpf_prog *xdp_prog)
97f91a7c
JF
3247{
3248 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
7c300131 3249 unsigned long map_owner = ri->map_owner;
97f91a7c 3250 struct bpf_map *map = ri->map;
11393cc9 3251 u32 index = ri->ifindex;
9c270af3 3252 void *fwd = NULL;
4c03bdd7 3253 int err;
97f91a7c
JF
3254
3255 ri->ifindex = 0;
3256 ri->map = NULL;
7c300131 3257 ri->map_owner = 0;
109980b8 3258
7c300131 3259 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
96c5508e
JDB
3260 err = -EFAULT;
3261 map = NULL;
3262 goto err;
3263 }
97f91a7c 3264
9c270af3 3265 fwd = __xdp_map_lookup_elem(map, index);
4c03bdd7
JDB
3266 if (!fwd) {
3267 err = -EINVAL;
f5836ca5 3268 goto err;
4c03bdd7 3269 }
e4a8e817 3270 if (ri->map_to_flush && ri->map_to_flush != map)
11393cc9
JF
3271 xdp_do_flush_map();
3272
9c270af3 3273 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
f5836ca5
JDB
3274 if (unlikely(err))
3275 goto err;
3276
3277 ri->map_to_flush = map;
59a30896 3278 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
f5836ca5
JDB
3279 return 0;
3280err:
59a30896 3281 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
97f91a7c
JF
3282 return err;
3283}
3284
5acaee0a
JF
3285int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3286 struct bpf_prog *xdp_prog)
814abfab
JF
3287{
3288 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
5acaee0a 3289 struct net_device *fwd;
eb48d682 3290 u32 index = ri->ifindex;
4c03bdd7 3291 int err;
814abfab 3292
97f91a7c
JF
3293 if (ri->map)
3294 return xdp_do_redirect_map(dev, xdp, xdp_prog);
3295
eb48d682 3296 fwd = dev_get_by_index_rcu(dev_net(dev), index);
814abfab 3297 ri->ifindex = 0;
5acaee0a 3298 if (unlikely(!fwd)) {
4c03bdd7 3299 err = -EINVAL;
f5836ca5 3300 goto err;
814abfab
JF
3301 }
3302
4c03bdd7 3303 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
f5836ca5
JDB
3304 if (unlikely(err))
3305 goto err;
3306
3307 _trace_xdp_redirect(dev, xdp_prog, index);
3308 return 0;
3309err:
3310 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
4c03bdd7 3311 return err;
814abfab
JF
3312}
3313EXPORT_SYMBOL_GPL(xdp_do_redirect);
3314
c060bc61
XS
3315static int xdp_do_generic_redirect_map(struct net_device *dev,
3316 struct sk_buff *skb,
02671e23 3317 struct xdp_buff *xdp,
c060bc61 3318 struct bpf_prog *xdp_prog)
6103aa96
JF
3319{
3320 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
7c300131 3321 unsigned long map_owner = ri->map_owner;
96c5508e 3322 struct bpf_map *map = ri->map;
eb48d682 3323 u32 index = ri->ifindex;
02671e23 3324 void *fwd = NULL;
2facaad6 3325 int err = 0;
6103aa96 3326
6103aa96 3327 ri->ifindex = 0;
96c5508e 3328 ri->map = NULL;
7c300131 3329 ri->map_owner = 0;
96c5508e 3330
9c270af3
JDB
3331 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3332 err = -EFAULT;
3333 map = NULL;
3334 goto err;
96c5508e 3335 }
9c270af3 3336 fwd = __xdp_map_lookup_elem(map, index);
2facaad6
JDB
3337 if (unlikely(!fwd)) {
3338 err = -EINVAL;
f5836ca5 3339 goto err;
6103aa96
JF
3340 }
3341
9c270af3 3342 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
6d5fc195
TM
3343 struct bpf_dtab_netdev *dst = fwd;
3344
3345 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3346 if (unlikely(err))
9c270af3 3347 goto err;
02671e23
BT
3348 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3349 struct xdp_sock *xs = fwd;
3350
3351 err = xsk_generic_rcv(xs, xdp);
3352 if (err)
3353 goto err;
3354 consume_skb(skb);
9c270af3
JDB
3355 } else {
3356 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3357 err = -EBADRQC;
f5836ca5 3358 goto err;
2facaad6 3359 }
6103aa96 3360
9c270af3
JDB
3361 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3362 return 0;
3363err:
3364 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3365 return err;
3366}
3367
3368int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
02671e23 3369 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
9c270af3
JDB
3370{
3371 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3372 u32 index = ri->ifindex;
3373 struct net_device *fwd;
3374 int err = 0;
3375
3376 if (ri->map)
02671e23 3377 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog);
9c270af3
JDB
3378
3379 ri->ifindex = 0;
3380 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3381 if (unlikely(!fwd)) {
3382 err = -EINVAL;
f5836ca5 3383 goto err;
2facaad6
JDB
3384 }
3385
d8d7218a
TM
3386 err = xdp_ok_fwd_dev(fwd, skb->len);
3387 if (unlikely(err))
9c270af3
JDB
3388 goto err;
3389
2facaad6 3390 skb->dev = fwd;
9c270af3 3391 _trace_xdp_redirect(dev, xdp_prog, index);
02671e23 3392 generic_xdp_tx(skb, xdp_prog);
f5836ca5
JDB
3393 return 0;
3394err:
9c270af3 3395 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
2facaad6 3396 return err;
6103aa96
JF
3397}
3398EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3399
814abfab
JF
3400BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3401{
3402 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3403
3404 if (unlikely(flags))
3405 return XDP_ABORTED;
3406
3407 ri->ifindex = ifindex;
3408 ri->flags = flags;
109980b8 3409 ri->map = NULL;
7c300131 3410 ri->map_owner = 0;
e4a8e817 3411
814abfab
JF
3412 return XDP_REDIRECT;
3413}
3414
3415static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3416 .func = bpf_xdp_redirect,
3417 .gpl_only = false,
3418 .ret_type = RET_INTEGER,
3419 .arg1_type = ARG_ANYTHING,
3420 .arg2_type = ARG_ANYTHING,
3421};
3422
109980b8 3423BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
7c300131 3424 unsigned long, map_owner)
e4a8e817
DB
3425{
3426 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3427
3428 if (unlikely(flags))
3429 return XDP_ABORTED;
3430
3431 ri->ifindex = ifindex;
3432 ri->flags = flags;
3433 ri->map = map;
109980b8 3434 ri->map_owner = map_owner;
e4a8e817
DB
3435
3436 return XDP_REDIRECT;
3437}
3438
109980b8
DB
3439/* Note, arg4 is hidden from users and populated by the verifier
3440 * with the right pointer.
3441 */
e4a8e817
DB
3442static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3443 .func = bpf_xdp_redirect_map,
3444 .gpl_only = false,
3445 .ret_type = RET_INTEGER,
3446 .arg1_type = ARG_CONST_MAP_PTR,
3447 .arg2_type = ARG_ANYTHING,
3448 .arg3_type = ARG_ANYTHING,
3449};
3450
555c8a86 3451static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
aa7145c1 3452 unsigned long off, unsigned long len)
555c8a86 3453{
aa7145c1 3454 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
555c8a86
DB
3455
3456 if (unlikely(!ptr))
3457 return len;
3458 if (ptr != dst_buff)
3459 memcpy(dst_buff, ptr, len);
3460
3461 return 0;
3462}
3463
f3694e00
DB
3464BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3465 u64, flags, void *, meta, u64, meta_size)
555c8a86 3466{
555c8a86 3467 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
555c8a86
DB
3468
3469 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3470 return -EINVAL;
3471 if (unlikely(skb_size > skb->len))
3472 return -EFAULT;
3473
3474 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3475 bpf_skb_copy);
3476}
3477
3478static const struct bpf_func_proto bpf_skb_event_output_proto = {
3479 .func = bpf_skb_event_output,
3480 .gpl_only = true,
3481 .ret_type = RET_INTEGER,
3482 .arg1_type = ARG_PTR_TO_CTX,
3483 .arg2_type = ARG_CONST_MAP_PTR,
3484 .arg3_type = ARG_ANYTHING,
39f19ebb 3485 .arg4_type = ARG_PTR_TO_MEM,
1728a4f2 3486 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
555c8a86
DB
3487};
3488
c6c33454
DB
3489static unsigned short bpf_tunnel_key_af(u64 flags)
3490{
3491 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3492}
3493
f3694e00
DB
3494BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3495 u32, size, u64, flags)
d3aa45ce 3496{
c6c33454
DB
3497 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3498 u8 compat[sizeof(struct bpf_tunnel_key)];
074f528e
DB
3499 void *to_orig = to;
3500 int err;
d3aa45ce 3501
074f528e
DB
3502 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3503 err = -EINVAL;
3504 goto err_clear;
3505 }
3506 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3507 err = -EPROTO;
3508 goto err_clear;
3509 }
c6c33454 3510 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
074f528e 3511 err = -EINVAL;
c6c33454 3512 switch (size) {
4018ab18 3513 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 3514 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4018ab18 3515 goto set_compat;
c6c33454
DB
3516 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3517 /* Fixup deprecated structure layouts here, so we have
3518 * a common path later on.
3519 */
3520 if (ip_tunnel_info_af(info) != AF_INET)
074f528e 3521 goto err_clear;
4018ab18 3522set_compat:
c6c33454
DB
3523 to = (struct bpf_tunnel_key *)compat;
3524 break;
3525 default:
074f528e 3526 goto err_clear;
c6c33454
DB
3527 }
3528 }
d3aa45ce
AS
3529
3530 to->tunnel_id = be64_to_cpu(info->key.tun_id);
c6c33454
DB
3531 to->tunnel_tos = info->key.tos;
3532 to->tunnel_ttl = info->key.ttl;
1fbc2e0c 3533 to->tunnel_ext = 0;
c6c33454 3534
4018ab18 3535 if (flags & BPF_F_TUNINFO_IPV6) {
c6c33454
DB
3536 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3537 sizeof(to->remote_ipv6));
4018ab18
DB
3538 to->tunnel_label = be32_to_cpu(info->key.label);
3539 } else {
c6c33454 3540 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
1fbc2e0c
DB
3541 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3542 to->tunnel_label = 0;
4018ab18 3543 }
c6c33454
DB
3544
3545 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
074f528e 3546 memcpy(to_orig, to, size);
d3aa45ce
AS
3547
3548 return 0;
074f528e
DB
3549err_clear:
3550 memset(to_orig, 0, size);
3551 return err;
d3aa45ce
AS
3552}
3553
577c50aa 3554static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
d3aa45ce
AS
3555 .func = bpf_skb_get_tunnel_key,
3556 .gpl_only = false,
3557 .ret_type = RET_INTEGER,
3558 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
3559 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3560 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
3561 .arg4_type = ARG_ANYTHING,
3562};
3563
f3694e00 3564BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
14ca0751 3565{
14ca0751 3566 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
074f528e 3567 int err;
14ca0751
DB
3568
3569 if (unlikely(!info ||
074f528e
DB
3570 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3571 err = -ENOENT;
3572 goto err_clear;
3573 }
3574 if (unlikely(size < info->options_len)) {
3575 err = -ENOMEM;
3576 goto err_clear;
3577 }
14ca0751
DB
3578
3579 ip_tunnel_info_opts_get(to, info);
074f528e
DB
3580 if (size > info->options_len)
3581 memset(to + info->options_len, 0, size - info->options_len);
14ca0751
DB
3582
3583 return info->options_len;
074f528e
DB
3584err_clear:
3585 memset(to, 0, size);
3586 return err;
14ca0751
DB
3587}
3588
3589static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3590 .func = bpf_skb_get_tunnel_opt,
3591 .gpl_only = false,
3592 .ret_type = RET_INTEGER,
3593 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
3594 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3595 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
3596};
3597
d3aa45ce
AS
3598static struct metadata_dst __percpu *md_dst;
3599
f3694e00
DB
3600BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3601 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
d3aa45ce 3602{
d3aa45ce 3603 struct metadata_dst *md = this_cpu_ptr(md_dst);
c6c33454 3604 u8 compat[sizeof(struct bpf_tunnel_key)];
d3aa45ce
AS
3605 struct ip_tunnel_info *info;
3606
22080870 3607 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
77a5196a 3608 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
d3aa45ce 3609 return -EINVAL;
c6c33454
DB
3610 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3611 switch (size) {
4018ab18 3612 case offsetof(struct bpf_tunnel_key, tunnel_label):
c0e760c9 3613 case offsetof(struct bpf_tunnel_key, tunnel_ext):
c6c33454
DB
3614 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3615 /* Fixup deprecated structure layouts here, so we have
3616 * a common path later on.
3617 */
3618 memcpy(compat, from, size);
3619 memset(compat + size, 0, sizeof(compat) - size);
f3694e00 3620 from = (const struct bpf_tunnel_key *) compat;
c6c33454
DB
3621 break;
3622 default:
3623 return -EINVAL;
3624 }
3625 }
c0e760c9
DB
3626 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3627 from->tunnel_ext))
4018ab18 3628 return -EINVAL;
d3aa45ce
AS
3629
3630 skb_dst_drop(skb);
3631 dst_hold((struct dst_entry *) md);
3632 skb_dst_set(skb, (struct dst_entry *) md);
3633
3634 info = &md->u.tun_info;
5540fbf4 3635 memset(info, 0, sizeof(*info));
d3aa45ce 3636 info->mode = IP_TUNNEL_INFO_TX;
c6c33454 3637
db3c6139 3638 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
22080870
DB
3639 if (flags & BPF_F_DONT_FRAGMENT)
3640 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
792f3dd6
WT
3641 if (flags & BPF_F_ZERO_CSUM_TX)
3642 info->key.tun_flags &= ~TUNNEL_CSUM;
77a5196a
WT
3643 if (flags & BPF_F_SEQ_NUMBER)
3644 info->key.tun_flags |= TUNNEL_SEQ;
22080870 3645
d3aa45ce 3646 info->key.tun_id = cpu_to_be64(from->tunnel_id);
c6c33454
DB
3647 info->key.tos = from->tunnel_tos;
3648 info->key.ttl = from->tunnel_ttl;
3649
3650 if (flags & BPF_F_TUNINFO_IPV6) {
3651 info->mode |= IP_TUNNEL_INFO_IPV6;
3652 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3653 sizeof(from->remote_ipv6));
4018ab18
DB
3654 info->key.label = cpu_to_be32(from->tunnel_label) &
3655 IPV6_FLOWLABEL_MASK;
c6c33454
DB
3656 } else {
3657 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3658 }
d3aa45ce
AS
3659
3660 return 0;
3661}
3662
577c50aa 3663static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
d3aa45ce
AS
3664 .func = bpf_skb_set_tunnel_key,
3665 .gpl_only = false,
3666 .ret_type = RET_INTEGER,
3667 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
3668 .arg2_type = ARG_PTR_TO_MEM,
3669 .arg3_type = ARG_CONST_SIZE,
d3aa45ce
AS
3670 .arg4_type = ARG_ANYTHING,
3671};
3672
f3694e00
DB
3673BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3674 const u8 *, from, u32, size)
14ca0751 3675{
14ca0751
DB
3676 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3677 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3678
3679 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3680 return -EINVAL;
fca5fdf6 3681 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
14ca0751
DB
3682 return -ENOMEM;
3683
256c87c1 3684 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
14ca0751
DB
3685
3686 return 0;
3687}
3688
3689static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3690 .func = bpf_skb_set_tunnel_opt,
3691 .gpl_only = false,
3692 .ret_type = RET_INTEGER,
3693 .arg1_type = ARG_PTR_TO_CTX,
39f19ebb
AS
3694 .arg2_type = ARG_PTR_TO_MEM,
3695 .arg3_type = ARG_CONST_SIZE,
14ca0751
DB
3696};
3697
3698static const struct bpf_func_proto *
3699bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
d3aa45ce
AS
3700{
3701 if (!md_dst) {
d66f2b91
JK
3702 struct metadata_dst __percpu *tmp;
3703
3704 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3705 METADATA_IP_TUNNEL,
3706 GFP_KERNEL);
3707 if (!tmp)
d3aa45ce 3708 return NULL;
d66f2b91
JK
3709 if (cmpxchg(&md_dst, NULL, tmp))
3710 metadata_dst_free_percpu(tmp);
d3aa45ce 3711 }
14ca0751
DB
3712
3713 switch (which) {
3714 case BPF_FUNC_skb_set_tunnel_key:
3715 return &bpf_skb_set_tunnel_key_proto;
3716 case BPF_FUNC_skb_set_tunnel_opt:
3717 return &bpf_skb_set_tunnel_opt_proto;
3718 default:
3719 return NULL;
3720 }
d3aa45ce
AS
3721}
3722
f3694e00
DB
3723BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3724 u32, idx)
4a482f34 3725{
4a482f34
MKL
3726 struct bpf_array *array = container_of(map, struct bpf_array, map);
3727 struct cgroup *cgrp;
3728 struct sock *sk;
4a482f34 3729
2d48c5f9 3730 sk = skb_to_full_sk(skb);
4a482f34
MKL
3731 if (!sk || !sk_fullsock(sk))
3732 return -ENOENT;
f3694e00 3733 if (unlikely(idx >= array->map.max_entries))
4a482f34
MKL
3734 return -E2BIG;
3735
f3694e00 3736 cgrp = READ_ONCE(array->ptrs[idx]);
4a482f34
MKL
3737 if (unlikely(!cgrp))
3738 return -EAGAIN;
3739
54fd9c2d 3740 return sk_under_cgroup_hierarchy(sk, cgrp);
4a482f34
MKL
3741}
3742
747ea55e
DB
3743static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3744 .func = bpf_skb_under_cgroup,
4a482f34
MKL
3745 .gpl_only = false,
3746 .ret_type = RET_INTEGER,
3747 .arg1_type = ARG_PTR_TO_CTX,
3748 .arg2_type = ARG_CONST_MAP_PTR,
3749 .arg3_type = ARG_ANYTHING,
3750};
4a482f34 3751
cb20b08e
DB
3752#ifdef CONFIG_SOCK_CGROUP_DATA
3753BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
3754{
3755 struct sock *sk = skb_to_full_sk(skb);
3756 struct cgroup *cgrp;
3757
3758 if (!sk || !sk_fullsock(sk))
3759 return 0;
3760
3761 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3762 return cgrp->kn->id.id;
3763}
3764
3765static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
3766 .func = bpf_skb_cgroup_id,
3767 .gpl_only = false,
3768 .ret_type = RET_INTEGER,
3769 .arg1_type = ARG_PTR_TO_CTX,
3770};
3771#endif
3772
4de16969
DB
3773static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3774 unsigned long off, unsigned long len)
3775{
3776 memcpy(dst_buff, src_buff + off, len);
3777 return 0;
3778}
3779
f3694e00
DB
3780BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3781 u64, flags, void *, meta, u64, meta_size)
4de16969 3782{
4de16969 3783 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4de16969
DB
3784
3785 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3786 return -EINVAL;
3787 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3788 return -EFAULT;
3789
9c471370
MKL
3790 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3791 xdp_size, bpf_xdp_copy);
4de16969
DB
3792}
3793
3794static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3795 .func = bpf_xdp_event_output,
3796 .gpl_only = true,
3797 .ret_type = RET_INTEGER,
3798 .arg1_type = ARG_PTR_TO_CTX,
3799 .arg2_type = ARG_CONST_MAP_PTR,
3800 .arg3_type = ARG_ANYTHING,
39f19ebb 3801 .arg4_type = ARG_PTR_TO_MEM,
1728a4f2 3802 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4de16969
DB
3803};
3804
91b8270f
CF
3805BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3806{
3807 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3808}
3809
3810static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3811 .func = bpf_get_socket_cookie,
3812 .gpl_only = false,
3813 .ret_type = RET_INTEGER,
3814 .arg1_type = ARG_PTR_TO_CTX,
3815};
3816
d692f113
AI
3817BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
3818{
3819 return sock_gen_cookie(ctx->sk);
3820}
3821
3822static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
3823 .func = bpf_get_socket_cookie_sock_addr,
3824 .gpl_only = false,
3825 .ret_type = RET_INTEGER,
3826 .arg1_type = ARG_PTR_TO_CTX,
3827};
3828
3829BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
3830{
3831 return sock_gen_cookie(ctx->sk);
3832}
3833
3834static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
3835 .func = bpf_get_socket_cookie_sock_ops,
3836 .gpl_only = false,
3837 .ret_type = RET_INTEGER,
3838 .arg1_type = ARG_PTR_TO_CTX,
3839};
3840
6acc5c29
CF
3841BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3842{
3843 struct sock *sk = sk_to_full_sk(skb->sk);
3844 kuid_t kuid;
3845
3846 if (!sk || !sk_fullsock(sk))
3847 return overflowuid;
3848 kuid = sock_net_uid(sock_net(sk), sk);
3849 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3850}
3851
3852static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3853 .func = bpf_get_socket_uid,
3854 .gpl_only = false,
3855 .ret_type = RET_INTEGER,
3856 .arg1_type = ARG_PTR_TO_CTX,
3857};
3858
8c4b4c7e
LB
3859BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3860 int, level, int, optname, char *, optval, int, optlen)
3861{
3862 struct sock *sk = bpf_sock->sk;
3863 int ret = 0;
3864 int val;
3865
3866 if (!sk_fullsock(sk))
3867 return -EINVAL;
3868
3869 if (level == SOL_SOCKET) {
3870 if (optlen != sizeof(int))
3871 return -EINVAL;
3872 val = *((int *)optval);
3873
3874 /* Only some socketops are supported */
3875 switch (optname) {
3876 case SO_RCVBUF:
3877 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3878 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3879 break;
3880 case SO_SNDBUF:
3881 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3882 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3883 break;
3884 case SO_MAX_PACING_RATE:
3885 sk->sk_max_pacing_rate = val;
3886 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3887 sk->sk_max_pacing_rate);
3888 break;
3889 case SO_PRIORITY:
3890 sk->sk_priority = val;
3891 break;
3892 case SO_RCVLOWAT:
3893 if (val < 0)
3894 val = INT_MAX;
3895 sk->sk_rcvlowat = val ? : 1;
3896 break;
3897 case SO_MARK:
3898 sk->sk_mark = val;
3899 break;
3900 default:
3901 ret = -EINVAL;
3902 }
a5192c52 3903#ifdef CONFIG_INET
6f5c39fa
NS
3904 } else if (level == SOL_IP) {
3905 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3906 return -EINVAL;
3907
3908 val = *((int *)optval);
3909 /* Only some options are supported */
3910 switch (optname) {
3911 case IP_TOS:
3912 if (val < -1 || val > 0xff) {
3913 ret = -EINVAL;
3914 } else {
3915 struct inet_sock *inet = inet_sk(sk);
3916
3917 if (val == -1)
3918 val = 0;
3919 inet->tos = val;
3920 }
3921 break;
3922 default:
3923 ret = -EINVAL;
3924 }
6f9bd3d7
LB
3925#if IS_ENABLED(CONFIG_IPV6)
3926 } else if (level == SOL_IPV6) {
3927 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3928 return -EINVAL;
3929
3930 val = *((int *)optval);
3931 /* Only some options are supported */
3932 switch (optname) {
3933 case IPV6_TCLASS:
3934 if (val < -1 || val > 0xff) {
3935 ret = -EINVAL;
3936 } else {
3937 struct ipv6_pinfo *np = inet6_sk(sk);
3938
3939 if (val == -1)
3940 val = 0;
3941 np->tclass = val;
3942 }
3943 break;
3944 default:
3945 ret = -EINVAL;
3946 }
3947#endif
8c4b4c7e
LB
3948 } else if (level == SOL_TCP &&
3949 sk->sk_prot->setsockopt == tcp_setsockopt) {
91b5b21c
LB
3950 if (optname == TCP_CONGESTION) {
3951 char name[TCP_CA_NAME_MAX];
ebfa00c5 3952 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
91b5b21c
LB
3953
3954 strncpy(name, optval, min_t(long, optlen,
3955 TCP_CA_NAME_MAX-1));
3956 name[TCP_CA_NAME_MAX-1] = 0;
6f9bd3d7
LB
3957 ret = tcp_set_congestion_control(sk, name, false,
3958 reinit);
91b5b21c 3959 } else {
fc747810
LB
3960 struct tcp_sock *tp = tcp_sk(sk);
3961
3962 if (optlen != sizeof(int))
3963 return -EINVAL;
3964
3965 val = *((int *)optval);
3966 /* Only some options are supported */
3967 switch (optname) {
3968 case TCP_BPF_IW:
3969 if (val <= 0 || tp->data_segs_out > 0)
3970 ret = -EINVAL;
3971 else
3972 tp->snd_cwnd = val;
3973 break;
13bf9641
LB
3974 case TCP_BPF_SNDCWND_CLAMP:
3975 if (val <= 0) {
3976 ret = -EINVAL;
3977 } else {
3978 tp->snd_cwnd_clamp = val;
3979 tp->snd_ssthresh = val;
3980 }
6d3f06a0 3981 break;
fc747810
LB
3982 default:
3983 ret = -EINVAL;
3984 }
91b5b21c 3985 }
91b5b21c 3986#endif
8c4b4c7e
LB
3987 } else {
3988 ret = -EINVAL;
3989 }
3990 return ret;
3991}
3992
3993static const struct bpf_func_proto bpf_setsockopt_proto = {
3994 .func = bpf_setsockopt,
cd86d1fd 3995 .gpl_only = false,
8c4b4c7e
LB
3996 .ret_type = RET_INTEGER,
3997 .arg1_type = ARG_PTR_TO_CTX,
3998 .arg2_type = ARG_ANYTHING,
3999 .arg3_type = ARG_ANYTHING,
4000 .arg4_type = ARG_PTR_TO_MEM,
4001 .arg5_type = ARG_CONST_SIZE,
4002};
4003
cd86d1fd
LB
4004BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4005 int, level, int, optname, char *, optval, int, optlen)
4006{
4007 struct sock *sk = bpf_sock->sk;
cd86d1fd
LB
4008
4009 if (!sk_fullsock(sk))
4010 goto err_clear;
4011
4012#ifdef CONFIG_INET
4013 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4014 if (optname == TCP_CONGESTION) {
4015 struct inet_connection_sock *icsk = inet_csk(sk);
4016
4017 if (!icsk->icsk_ca_ops || optlen <= 1)
4018 goto err_clear;
4019 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4020 optval[optlen - 1] = 0;
4021 } else {
4022 goto err_clear;
4023 }
6f5c39fa
NS
4024 } else if (level == SOL_IP) {
4025 struct inet_sock *inet = inet_sk(sk);
4026
4027 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4028 goto err_clear;
4029
4030 /* Only some options are supported */
4031 switch (optname) {
4032 case IP_TOS:
4033 *((int *)optval) = (int)inet->tos;
4034 break;
4035 default:
4036 goto err_clear;
4037 }
6f9bd3d7
LB
4038#if IS_ENABLED(CONFIG_IPV6)
4039 } else if (level == SOL_IPV6) {
4040 struct ipv6_pinfo *np = inet6_sk(sk);
4041
4042 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4043 goto err_clear;
4044
4045 /* Only some options are supported */
4046 switch (optname) {
4047 case IPV6_TCLASS:
4048 *((int *)optval) = (int)np->tclass;
4049 break;
4050 default:
4051 goto err_clear;
4052 }
4053#endif
cd86d1fd
LB
4054 } else {
4055 goto err_clear;
4056 }
aa2bc739 4057 return 0;
cd86d1fd
LB
4058#endif
4059err_clear:
4060 memset(optval, 0, optlen);
4061 return -EINVAL;
4062}
4063
4064static const struct bpf_func_proto bpf_getsockopt_proto = {
4065 .func = bpf_getsockopt,
4066 .gpl_only = false,
4067 .ret_type = RET_INTEGER,
4068 .arg1_type = ARG_PTR_TO_CTX,
4069 .arg2_type = ARG_ANYTHING,
4070 .arg3_type = ARG_ANYTHING,
4071 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4072 .arg5_type = ARG_CONST_SIZE,
4073};
4074
b13d8807
LB
4075BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4076 int, argval)
4077{
4078 struct sock *sk = bpf_sock->sk;
4079 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4080
a7dcdf6e 4081 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
b13d8807
LB
4082 return -EINVAL;
4083
b13d8807
LB
4084 if (val)
4085 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4086
4087 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
b13d8807
LB
4088}
4089
4090static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4091 .func = bpf_sock_ops_cb_flags_set,
4092 .gpl_only = false,
4093 .ret_type = RET_INTEGER,
4094 .arg1_type = ARG_PTR_TO_CTX,
4095 .arg2_type = ARG_ANYTHING,
4096};
4097
d74bad4e
AI
4098const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4099EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4100
4101BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4102 int, addr_len)
4103{
4104#ifdef CONFIG_INET
4105 struct sock *sk = ctx->sk;
4106 int err;
4107
4108 /* Binding to port can be expensive so it's prohibited in the helper.
4109 * Only binding to IP is supported.
4110 */
4111 err = -EINVAL;
4112 if (addr->sa_family == AF_INET) {
4113 if (addr_len < sizeof(struct sockaddr_in))
4114 return err;
4115 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4116 return err;
4117 return __inet_bind(sk, addr, addr_len, true, false);
4118#if IS_ENABLED(CONFIG_IPV6)
4119 } else if (addr->sa_family == AF_INET6) {
4120 if (addr_len < SIN6_LEN_RFC2133)
4121 return err;
4122 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4123 return err;
4124 /* ipv6_bpf_stub cannot be NULL, since it's called from
4125 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4126 */
4127 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4128#endif /* CONFIG_IPV6 */
4129 }
4130#endif /* CONFIG_INET */
4131
4132 return -EAFNOSUPPORT;
4133}
4134
4135static const struct bpf_func_proto bpf_bind_proto = {
4136 .func = bpf_bind,
4137 .gpl_only = false,
4138 .ret_type = RET_INTEGER,
4139 .arg1_type = ARG_PTR_TO_CTX,
4140 .arg2_type = ARG_PTR_TO_MEM,
4141 .arg3_type = ARG_CONST_SIZE,
4142};
4143
12bed760
EB
4144#ifdef CONFIG_XFRM
4145BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4146 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4147{
4148 const struct sec_path *sp = skb_sec_path(skb);
4149 const struct xfrm_state *x;
4150
4151 if (!sp || unlikely(index >= sp->len || flags))
4152 goto err_clear;
4153
4154 x = sp->xvec[index];
4155
4156 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4157 goto err_clear;
4158
4159 to->reqid = x->props.reqid;
4160 to->spi = x->id.spi;
4161 to->family = x->props.family;
1fbc2e0c
DB
4162 to->ext = 0;
4163
12bed760
EB
4164 if (to->family == AF_INET6) {
4165 memcpy(to->remote_ipv6, x->props.saddr.a6,
4166 sizeof(to->remote_ipv6));
4167 } else {
4168 to->remote_ipv4 = x->props.saddr.a4;
1fbc2e0c 4169 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
12bed760
EB
4170 }
4171
4172 return 0;
4173err_clear:
4174 memset(to, 0, size);
4175 return -EINVAL;
4176}
4177
4178static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4179 .func = bpf_skb_get_xfrm_state,
4180 .gpl_only = false,
4181 .ret_type = RET_INTEGER,
4182 .arg1_type = ARG_PTR_TO_CTX,
4183 .arg2_type = ARG_ANYTHING,
4184 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4185 .arg4_type = ARG_CONST_SIZE,
4186 .arg5_type = ARG_ANYTHING,
4187};
4188#endif
4189
87f5fc7e
DA
4190#if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4191static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4192 const struct neighbour *neigh,
4193 const struct net_device *dev)
4194{
4195 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4196 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4197 params->h_vlan_TCI = 0;
4198 params->h_vlan_proto = 0;
4c79579b 4199 params->ifindex = dev->ifindex;
87f5fc7e 4200
4c79579b 4201 return 0;
87f5fc7e
DA
4202}
4203#endif
4204
4205#if IS_ENABLED(CONFIG_INET)
4206static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4f74fede 4207 u32 flags, bool check_mtu)
87f5fc7e
DA
4208{
4209 struct in_device *in_dev;
4210 struct neighbour *neigh;
4211 struct net_device *dev;
4212 struct fib_result res;
4213 struct fib_nh *nh;
4214 struct flowi4 fl4;
4215 int err;
4f74fede 4216 u32 mtu;
87f5fc7e
DA
4217
4218 dev = dev_get_by_index_rcu(net, params->ifindex);
4219 if (unlikely(!dev))
4220 return -ENODEV;
4221
4222 /* verify forwarding is enabled on this interface */
4223 in_dev = __in_dev_get_rcu(dev);
4224 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4c79579b 4225 return BPF_FIB_LKUP_RET_FWD_DISABLED;
87f5fc7e
DA
4226
4227 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4228 fl4.flowi4_iif = 1;
4229 fl4.flowi4_oif = params->ifindex;
4230 } else {
4231 fl4.flowi4_iif = params->ifindex;
4232 fl4.flowi4_oif = 0;
4233 }
4234 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4235 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4236 fl4.flowi4_flags = 0;
4237
4238 fl4.flowi4_proto = params->l4_protocol;
4239 fl4.daddr = params->ipv4_dst;
4240 fl4.saddr = params->ipv4_src;
4241 fl4.fl4_sport = params->sport;
4242 fl4.fl4_dport = params->dport;
4243
4244 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4245 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4246 struct fib_table *tb;
4247
4248 tb = fib_get_table(net, tbid);
4249 if (unlikely(!tb))
4c79579b 4250 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4251
4252 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4253 } else {
4254 fl4.flowi4_mark = 0;
4255 fl4.flowi4_secid = 0;
4256 fl4.flowi4_tun_key.tun_id = 0;
4257 fl4.flowi4_uid = sock_net_uid(net, NULL);
4258
4259 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4260 }
4261
4c79579b
DA
4262 if (err) {
4263 /* map fib lookup errors to RTN_ type */
4264 if (err == -EINVAL)
4265 return BPF_FIB_LKUP_RET_BLACKHOLE;
4266 if (err == -EHOSTUNREACH)
4267 return BPF_FIB_LKUP_RET_UNREACHABLE;
4268 if (err == -EACCES)
4269 return BPF_FIB_LKUP_RET_PROHIBIT;
4270
4271 return BPF_FIB_LKUP_RET_NOT_FWDED;
4272 }
4273
4274 if (res.type != RTN_UNICAST)
4275 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4276
4277 if (res.fi->fib_nhs > 1)
4278 fib_select_path(net, &res, &fl4, NULL);
4279
4f74fede
DA
4280 if (check_mtu) {
4281 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4282 if (params->tot_len > mtu)
4c79579b 4283 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4f74fede
DA
4284 }
4285
87f5fc7e
DA
4286 nh = &res.fi->fib_nh[res.nh_sel];
4287
4288 /* do not handle lwt encaps right now */
4289 if (nh->nh_lwtstate)
4c79579b 4290 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
87f5fc7e
DA
4291
4292 dev = nh->nh_dev;
87f5fc7e
DA
4293 if (nh->nh_gw)
4294 params->ipv4_dst = nh->nh_gw;
4295
4296 params->rt_metric = res.fi->fib_priority;
4297
4298 /* xdp and cls_bpf programs are run in RCU-bh so
4299 * rcu_read_lock_bh is not needed here
4300 */
4301 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4c79579b
DA
4302 if (!neigh)
4303 return BPF_FIB_LKUP_RET_NO_NEIGH;
87f5fc7e 4304
4c79579b 4305 return bpf_fib_set_fwd_params(params, neigh, dev);
87f5fc7e
DA
4306}
4307#endif
4308
4309#if IS_ENABLED(CONFIG_IPV6)
4310static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4f74fede 4311 u32 flags, bool check_mtu)
87f5fc7e
DA
4312{
4313 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4314 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4315 struct neighbour *neigh;
4316 struct net_device *dev;
4317 struct inet6_dev *idev;
4318 struct fib6_info *f6i;
4319 struct flowi6 fl6;
4320 int strict = 0;
4321 int oif;
4f74fede 4322 u32 mtu;
87f5fc7e
DA
4323
4324 /* link local addresses are never forwarded */
4325 if (rt6_need_strict(dst) || rt6_need_strict(src))
4c79579b 4326 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4327
4328 dev = dev_get_by_index_rcu(net, params->ifindex);
4329 if (unlikely(!dev))
4330 return -ENODEV;
4331
4332 idev = __in6_dev_get_safely(dev);
4333 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4c79579b 4334 return BPF_FIB_LKUP_RET_FWD_DISABLED;
87f5fc7e
DA
4335
4336 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4337 fl6.flowi6_iif = 1;
4338 oif = fl6.flowi6_oif = params->ifindex;
4339 } else {
4340 oif = fl6.flowi6_iif = params->ifindex;
4341 fl6.flowi6_oif = 0;
4342 strict = RT6_LOOKUP_F_HAS_SADDR;
4343 }
bd3a08aa 4344 fl6.flowlabel = params->flowinfo;
87f5fc7e
DA
4345 fl6.flowi6_scope = 0;
4346 fl6.flowi6_flags = 0;
4347 fl6.mp_hash = 0;
4348
4349 fl6.flowi6_proto = params->l4_protocol;
4350 fl6.daddr = *dst;
4351 fl6.saddr = *src;
4352 fl6.fl6_sport = params->sport;
4353 fl6.fl6_dport = params->dport;
4354
4355 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4356 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4357 struct fib6_table *tb;
4358
4359 tb = ipv6_stub->fib6_get_table(net, tbid);
4360 if (unlikely(!tb))
4c79579b 4361 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4362
4363 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4364 } else {
4365 fl6.flowi6_mark = 0;
4366 fl6.flowi6_secid = 0;
4367 fl6.flowi6_tun_key.tun_id = 0;
4368 fl6.flowi6_uid = sock_net_uid(net, NULL);
4369
4370 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4371 }
4372
4373 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4c79579b
DA
4374 return BPF_FIB_LKUP_RET_NOT_FWDED;
4375
4376 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4377 switch (f6i->fib6_type) {
4378 case RTN_BLACKHOLE:
4379 return BPF_FIB_LKUP_RET_BLACKHOLE;
4380 case RTN_UNREACHABLE:
4381 return BPF_FIB_LKUP_RET_UNREACHABLE;
4382 case RTN_PROHIBIT:
4383 return BPF_FIB_LKUP_RET_PROHIBIT;
4384 default:
4385 return BPF_FIB_LKUP_RET_NOT_FWDED;
4386 }
4387 }
87f5fc7e 4388
4c79579b
DA
4389 if (f6i->fib6_type != RTN_UNICAST)
4390 return BPF_FIB_LKUP_RET_NOT_FWDED;
87f5fc7e
DA
4391
4392 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4393 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4394 fl6.flowi6_oif, NULL,
4395 strict);
4396
4f74fede
DA
4397 if (check_mtu) {
4398 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4399 if (params->tot_len > mtu)
4c79579b 4400 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4f74fede
DA
4401 }
4402
87f5fc7e 4403 if (f6i->fib6_nh.nh_lwtstate)
4c79579b 4404 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
87f5fc7e
DA
4405
4406 if (f6i->fib6_flags & RTF_GATEWAY)
4407 *dst = f6i->fib6_nh.nh_gw;
4408
4409 dev = f6i->fib6_nh.nh_dev;
4410 params->rt_metric = f6i->fib6_metric;
4411
4412 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4413 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4414 * because we need to get nd_tbl via the stub
4415 */
4416 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4417 ndisc_hashfn, dst, dev);
4c79579b
DA
4418 if (!neigh)
4419 return BPF_FIB_LKUP_RET_NO_NEIGH;
87f5fc7e 4420
4c79579b 4421 return bpf_fib_set_fwd_params(params, neigh, dev);
87f5fc7e
DA
4422}
4423#endif
4424
4425BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4426 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4427{
4428 if (plen < sizeof(*params))
4429 return -EINVAL;
4430
9ce64f19
DA
4431 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4432 return -EINVAL;
4433
87f5fc7e
DA
4434 switch (params->family) {
4435#if IS_ENABLED(CONFIG_INET)
4436 case AF_INET:
4437 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4f74fede 4438 flags, true);
87f5fc7e
DA
4439#endif
4440#if IS_ENABLED(CONFIG_IPV6)
4441 case AF_INET6:
4442 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4f74fede 4443 flags, true);
87f5fc7e
DA
4444#endif
4445 }
bcece5dc 4446 return -EAFNOSUPPORT;
87f5fc7e
DA
4447}
4448
4449static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4450 .func = bpf_xdp_fib_lookup,
4451 .gpl_only = true,
4452 .ret_type = RET_INTEGER,
4453 .arg1_type = ARG_PTR_TO_CTX,
4454 .arg2_type = ARG_PTR_TO_MEM,
4455 .arg3_type = ARG_CONST_SIZE,
4456 .arg4_type = ARG_ANYTHING,
4457};
4458
4459BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4460 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4461{
4f74fede 4462 struct net *net = dev_net(skb->dev);
4c79579b 4463 int rc = -EAFNOSUPPORT;
4f74fede 4464
87f5fc7e
DA
4465 if (plen < sizeof(*params))
4466 return -EINVAL;
4467
9ce64f19
DA
4468 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4469 return -EINVAL;
4470
87f5fc7e
DA
4471 switch (params->family) {
4472#if IS_ENABLED(CONFIG_INET)
4473 case AF_INET:
4c79579b 4474 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4f74fede 4475 break;
87f5fc7e
DA
4476#endif
4477#if IS_ENABLED(CONFIG_IPV6)
4478 case AF_INET6:
4c79579b 4479 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4f74fede 4480 break;
87f5fc7e
DA
4481#endif
4482 }
4f74fede 4483
4c79579b 4484 if (!rc) {
4f74fede
DA
4485 struct net_device *dev;
4486
4c79579b 4487 dev = dev_get_by_index_rcu(net, params->ifindex);
4f74fede 4488 if (!is_skb_forwardable(dev, skb))
4c79579b 4489 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4f74fede
DA
4490 }
4491
4c79579b 4492 return rc;
87f5fc7e
DA
4493}
4494
4495static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4496 .func = bpf_skb_fib_lookup,
4497 .gpl_only = true,
4498 .ret_type = RET_INTEGER,
4499 .arg1_type = ARG_PTR_TO_CTX,
4500 .arg2_type = ARG_PTR_TO_MEM,
4501 .arg3_type = ARG_CONST_SIZE,
4502 .arg4_type = ARG_ANYTHING,
4503};
4504
fe94cc29
MX
4505#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4506static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4507{
4508 int err;
4509 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4510
4511 if (!seg6_validate_srh(srh, len))
4512 return -EINVAL;
4513
4514 switch (type) {
4515 case BPF_LWT_ENCAP_SEG6_INLINE:
4516 if (skb->protocol != htons(ETH_P_IPV6))
4517 return -EBADMSG;
4518
4519 err = seg6_do_srh_inline(skb, srh);
4520 break;
4521 case BPF_LWT_ENCAP_SEG6:
4522 skb_reset_inner_headers(skb);
4523 skb->encapsulation = 1;
4524 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4525 break;
4526 default:
4527 return -EINVAL;
4528 }
4529
4530 bpf_compute_data_pointers(skb);
4531 if (err)
4532 return err;
4533
4534 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4535 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4536
4537 return seg6_lookup_nexthop(skb, NULL, 0);
4538}
4539#endif /* CONFIG_IPV6_SEG6_BPF */
4540
4541BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4542 u32, len)
4543{
4544 switch (type) {
4545#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4546 case BPF_LWT_ENCAP_SEG6:
4547 case BPF_LWT_ENCAP_SEG6_INLINE:
4548 return bpf_push_seg6_encap(skb, type, hdr, len);
4549#endif
4550 default:
4551 return -EINVAL;
4552 }
4553}
4554
4555static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4556 .func = bpf_lwt_push_encap,
4557 .gpl_only = false,
4558 .ret_type = RET_INTEGER,
4559 .arg1_type = ARG_PTR_TO_CTX,
4560 .arg2_type = ARG_ANYTHING,
4561 .arg3_type = ARG_PTR_TO_MEM,
4562 .arg4_type = ARG_CONST_SIZE
4563};
4564
61d76980 4565#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
fe94cc29
MX
4566BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4567 const void *, from, u32, len)
4568{
fe94cc29
MX
4569 struct seg6_bpf_srh_state *srh_state =
4570 this_cpu_ptr(&seg6_bpf_srh_states);
486cdf21 4571 struct ipv6_sr_hdr *srh = srh_state->srh;
fe94cc29 4572 void *srh_tlvs, *srh_end, *ptr;
fe94cc29
MX
4573 int srhoff = 0;
4574
486cdf21 4575 if (srh == NULL)
fe94cc29
MX
4576 return -EINVAL;
4577
fe94cc29
MX
4578 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4579 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4580
4581 ptr = skb->data + offset;
4582 if (ptr >= srh_tlvs && ptr + len <= srh_end)
486cdf21 4583 srh_state->valid = false;
fe94cc29
MX
4584 else if (ptr < (void *)&srh->flags ||
4585 ptr + len > (void *)&srh->segments)
4586 return -EFAULT;
4587
4588 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4589 return -EFAULT;
486cdf21
MX
4590 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4591 return -EINVAL;
4592 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
fe94cc29
MX
4593
4594 memcpy(skb->data + offset, from, len);
4595 return 0;
fe94cc29
MX
4596}
4597
4598static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4599 .func = bpf_lwt_seg6_store_bytes,
4600 .gpl_only = false,
4601 .ret_type = RET_INTEGER,
4602 .arg1_type = ARG_PTR_TO_CTX,
4603 .arg2_type = ARG_ANYTHING,
4604 .arg3_type = ARG_PTR_TO_MEM,
4605 .arg4_type = ARG_CONST_SIZE
4606};
4607
486cdf21 4608static void bpf_update_srh_state(struct sk_buff *skb)
fe94cc29 4609{
fe94cc29
MX
4610 struct seg6_bpf_srh_state *srh_state =
4611 this_cpu_ptr(&seg6_bpf_srh_states);
fe94cc29 4612 int srhoff = 0;
fe94cc29 4613
486cdf21
MX
4614 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
4615 srh_state->srh = NULL;
4616 } else {
4617 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4618 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
4619 srh_state->valid = true;
fe94cc29 4620 }
486cdf21
MX
4621}
4622
4623BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4624 u32, action, void *, param, u32, param_len)
4625{
4626 struct seg6_bpf_srh_state *srh_state =
4627 this_cpu_ptr(&seg6_bpf_srh_states);
4628 int hdroff = 0;
4629 int err;
fe94cc29
MX
4630
4631 switch (action) {
4632 case SEG6_LOCAL_ACTION_END_X:
486cdf21
MX
4633 if (!seg6_bpf_has_valid_srh(skb))
4634 return -EBADMSG;
fe94cc29
MX
4635 if (param_len != sizeof(struct in6_addr))
4636 return -EINVAL;
4637 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4638 case SEG6_LOCAL_ACTION_END_T:
486cdf21
MX
4639 if (!seg6_bpf_has_valid_srh(skb))
4640 return -EBADMSG;
fe94cc29
MX
4641 if (param_len != sizeof(int))
4642 return -EINVAL;
4643 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
486cdf21
MX
4644 case SEG6_LOCAL_ACTION_END_DT6:
4645 if (!seg6_bpf_has_valid_srh(skb))
4646 return -EBADMSG;
fe94cc29
MX
4647 if (param_len != sizeof(int))
4648 return -EINVAL;
486cdf21
MX
4649
4650 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
4651 return -EBADMSG;
4652 if (!pskb_pull(skb, hdroff))
4653 return -EBADMSG;
4654
4655 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
4656 skb_reset_network_header(skb);
4657 skb_reset_transport_header(skb);
4658 skb->encapsulation = 0;
4659
4660 bpf_compute_data_pointers(skb);
4661 bpf_update_srh_state(skb);
fe94cc29
MX
4662 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4663 case SEG6_LOCAL_ACTION_END_B6:
486cdf21
MX
4664 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4665 return -EBADMSG;
fe94cc29
MX
4666 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4667 param, param_len);
4668 if (!err)
486cdf21
MX
4669 bpf_update_srh_state(skb);
4670
fe94cc29
MX
4671 return err;
4672 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
486cdf21
MX
4673 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4674 return -EBADMSG;
fe94cc29
MX
4675 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4676 param, param_len);
4677 if (!err)
486cdf21
MX
4678 bpf_update_srh_state(skb);
4679
fe94cc29
MX
4680 return err;
4681 default:
4682 return -EINVAL;
4683 }
fe94cc29
MX
4684}
4685
4686static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4687 .func = bpf_lwt_seg6_action,
4688 .gpl_only = false,
4689 .ret_type = RET_INTEGER,
4690 .arg1_type = ARG_PTR_TO_CTX,
4691 .arg2_type = ARG_ANYTHING,
4692 .arg3_type = ARG_PTR_TO_MEM,
4693 .arg4_type = ARG_CONST_SIZE
4694};
4695
4696BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4697 s32, len)
4698{
fe94cc29
MX
4699 struct seg6_bpf_srh_state *srh_state =
4700 this_cpu_ptr(&seg6_bpf_srh_states);
486cdf21 4701 struct ipv6_sr_hdr *srh = srh_state->srh;
fe94cc29 4702 void *srh_end, *srh_tlvs, *ptr;
fe94cc29
MX
4703 struct ipv6hdr *hdr;
4704 int srhoff = 0;
4705 int ret;
4706
486cdf21 4707 if (unlikely(srh == NULL))
fe94cc29 4708 return -EINVAL;
fe94cc29
MX
4709
4710 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4711 ((srh->first_segment + 1) << 4));
4712 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4713 srh_state->hdrlen);
4714 ptr = skb->data + offset;
4715
4716 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4717 return -EFAULT;
4718 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4719 return -EFAULT;
4720
4721 if (len > 0) {
4722 ret = skb_cow_head(skb, len);
4723 if (unlikely(ret < 0))
4724 return ret;
4725
4726 ret = bpf_skb_net_hdr_push(skb, offset, len);
4727 } else {
4728 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4729 }
4730
4731 bpf_compute_data_pointers(skb);
4732 if (unlikely(ret < 0))
4733 return ret;
4734
4735 hdr = (struct ipv6hdr *)skb->data;
4736 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4737
486cdf21
MX
4738 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4739 return -EINVAL;
4740 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
fe94cc29 4741 srh_state->hdrlen += len;
486cdf21 4742 srh_state->valid = false;
fe94cc29 4743 return 0;
fe94cc29
MX
4744}
4745
4746static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
4747 .func = bpf_lwt_seg6_adjust_srh,
4748 .gpl_only = false,
4749 .ret_type = RET_INTEGER,
4750 .arg1_type = ARG_PTR_TO_CTX,
4751 .arg2_type = ARG_ANYTHING,
4752 .arg3_type = ARG_ANYTHING,
4753};
61d76980 4754#endif /* CONFIG_IPV6_SEG6_BPF */
fe94cc29
MX
4755
4756bool bpf_helper_changes_pkt_data(void *func)
4757{
4758 if (func == bpf_skb_vlan_push ||
4759 func == bpf_skb_vlan_pop ||
4760 func == bpf_skb_store_bytes ||
4761 func == bpf_skb_change_proto ||
4762 func == bpf_skb_change_head ||
0ea488ff 4763 func == sk_skb_change_head ||
fe94cc29 4764 func == bpf_skb_change_tail ||
0ea488ff 4765 func == sk_skb_change_tail ||
fe94cc29
MX
4766 func == bpf_skb_adjust_room ||
4767 func == bpf_skb_pull_data ||
0ea488ff 4768 func == sk_skb_pull_data ||
fe94cc29
MX
4769 func == bpf_clone_redirect ||
4770 func == bpf_l3_csum_replace ||
4771 func == bpf_l4_csum_replace ||
4772 func == bpf_xdp_adjust_head ||
4773 func == bpf_xdp_adjust_meta ||
4774 func == bpf_msg_pull_data ||
4775 func == bpf_xdp_adjust_tail ||
61d76980 4776#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
fe94cc29
MX
4777 func == bpf_lwt_seg6_store_bytes ||
4778 func == bpf_lwt_seg6_adjust_srh ||
61d76980
MX
4779 func == bpf_lwt_seg6_action ||
4780#endif
4781 func == bpf_lwt_push_encap)
fe94cc29
MX
4782 return true;
4783
4784 return false;
4785}
4786
d4052c4a 4787static const struct bpf_func_proto *
2492d3b8 4788bpf_base_func_proto(enum bpf_func_id func_id)
89aa0758
AS
4789{
4790 switch (func_id) {
4791 case BPF_FUNC_map_lookup_elem:
4792 return &bpf_map_lookup_elem_proto;
4793 case BPF_FUNC_map_update_elem:
4794 return &bpf_map_update_elem_proto;
4795 case BPF_FUNC_map_delete_elem:
4796 return &bpf_map_delete_elem_proto;
03e69b50
DB
4797 case BPF_FUNC_get_prandom_u32:
4798 return &bpf_get_prandom_u32_proto;
c04167ce 4799 case BPF_FUNC_get_smp_processor_id:
80b48c44 4800 return &bpf_get_raw_smp_processor_id_proto;
2d0e30c3
DB
4801 case BPF_FUNC_get_numa_node_id:
4802 return &bpf_get_numa_node_id_proto;
04fd61ab
AS
4803 case BPF_FUNC_tail_call:
4804 return &bpf_tail_call_proto;
17ca8cbf
DB
4805 case BPF_FUNC_ktime_get_ns:
4806 return &bpf_ktime_get_ns_proto;
0756ea3e 4807 case BPF_FUNC_trace_printk:
1be7f75d
AS
4808 if (capable(CAP_SYS_ADMIN))
4809 return bpf_get_trace_printk_proto();
2cc0608e 4810 /* else: fall through */
89aa0758
AS
4811 default:
4812 return NULL;
4813 }
4814}
4815
ae2cf1c4 4816static const struct bpf_func_proto *
5e43f899 4817sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
ae2cf1c4
DA
4818{
4819 switch (func_id) {
4820 /* inet and inet6 sockets are created in a process
4821 * context so there is always a valid uid/gid
4822 */
4823 case BPF_FUNC_get_current_uid_gid:
4824 return &bpf_get_current_uid_gid_proto;
cd339431
RG
4825 case BPF_FUNC_get_local_storage:
4826 return &bpf_get_local_storage_proto;
ae2cf1c4
DA
4827 default:
4828 return bpf_base_func_proto(func_id);
4829 }
4830}
4831
4fbac77d
AI
4832static const struct bpf_func_proto *
4833sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4834{
4835 switch (func_id) {
4836 /* inet and inet6 sockets are created in a process
4837 * context so there is always a valid uid/gid
4838 */
4839 case BPF_FUNC_get_current_uid_gid:
4840 return &bpf_get_current_uid_gid_proto;
d74bad4e
AI
4841 case BPF_FUNC_bind:
4842 switch (prog->expected_attach_type) {
4843 case BPF_CGROUP_INET4_CONNECT:
4844 case BPF_CGROUP_INET6_CONNECT:
4845 return &bpf_bind_proto;
4846 default:
4847 return NULL;
4848 }
d692f113
AI
4849 case BPF_FUNC_get_socket_cookie:
4850 return &bpf_get_socket_cookie_sock_addr_proto;
cd339431
RG
4851 case BPF_FUNC_get_local_storage:
4852 return &bpf_get_local_storage_proto;
4fbac77d
AI
4853 default:
4854 return bpf_base_func_proto(func_id);
4855 }
4856}
4857
2492d3b8 4858static const struct bpf_func_proto *
5e43f899 4859sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2492d3b8
DB
4860{
4861 switch (func_id) {
4862 case BPF_FUNC_skb_load_bytes:
4863 return &bpf_skb_load_bytes_proto;
4e1ec56c
DB
4864 case BPF_FUNC_skb_load_bytes_relative:
4865 return &bpf_skb_load_bytes_relative_proto;
91b8270f
CF
4866 case BPF_FUNC_get_socket_cookie:
4867 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
4868 case BPF_FUNC_get_socket_uid:
4869 return &bpf_get_socket_uid_proto;
2492d3b8
DB
4870 default:
4871 return bpf_base_func_proto(func_id);
4872 }
4873}
4874
cd339431
RG
4875static const struct bpf_func_proto *
4876cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4877{
4878 switch (func_id) {
4879 case BPF_FUNC_get_local_storage:
4880 return &bpf_get_local_storage_proto;
4881 default:
4882 return sk_filter_func_proto(func_id, prog);
4883 }
4884}
4885
608cd71a 4886static const struct bpf_func_proto *
5e43f899 4887tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
608cd71a
AS
4888{
4889 switch (func_id) {
4890 case BPF_FUNC_skb_store_bytes:
4891 return &bpf_skb_store_bytes_proto;
05c74e5e
DB
4892 case BPF_FUNC_skb_load_bytes:
4893 return &bpf_skb_load_bytes_proto;
4e1ec56c
DB
4894 case BPF_FUNC_skb_load_bytes_relative:
4895 return &bpf_skb_load_bytes_relative_proto;
36bbef52
DB
4896 case BPF_FUNC_skb_pull_data:
4897 return &bpf_skb_pull_data_proto;
7d672345
DB
4898 case BPF_FUNC_csum_diff:
4899 return &bpf_csum_diff_proto;
36bbef52
DB
4900 case BPF_FUNC_csum_update:
4901 return &bpf_csum_update_proto;
91bc4822
AS
4902 case BPF_FUNC_l3_csum_replace:
4903 return &bpf_l3_csum_replace_proto;
4904 case BPF_FUNC_l4_csum_replace:
4905 return &bpf_l4_csum_replace_proto;
3896d655
AS
4906 case BPF_FUNC_clone_redirect:
4907 return &bpf_clone_redirect_proto;
8d20aabe
DB
4908 case BPF_FUNC_get_cgroup_classid:
4909 return &bpf_get_cgroup_classid_proto;
4e10df9a
AS
4910 case BPF_FUNC_skb_vlan_push:
4911 return &bpf_skb_vlan_push_proto;
4912 case BPF_FUNC_skb_vlan_pop:
4913 return &bpf_skb_vlan_pop_proto;
6578171a
DB
4914 case BPF_FUNC_skb_change_proto:
4915 return &bpf_skb_change_proto_proto;
d2485c42
DB
4916 case BPF_FUNC_skb_change_type:
4917 return &bpf_skb_change_type_proto;
2be7e212
DB
4918 case BPF_FUNC_skb_adjust_room:
4919 return &bpf_skb_adjust_room_proto;
5293efe6
DB
4920 case BPF_FUNC_skb_change_tail:
4921 return &bpf_skb_change_tail_proto;
d3aa45ce
AS
4922 case BPF_FUNC_skb_get_tunnel_key:
4923 return &bpf_skb_get_tunnel_key_proto;
4924 case BPF_FUNC_skb_set_tunnel_key:
14ca0751
DB
4925 return bpf_get_skb_set_tunnel_proto(func_id);
4926 case BPF_FUNC_skb_get_tunnel_opt:
4927 return &bpf_skb_get_tunnel_opt_proto;
4928 case BPF_FUNC_skb_set_tunnel_opt:
4929 return bpf_get_skb_set_tunnel_proto(func_id);
27b29f63
AS
4930 case BPF_FUNC_redirect:
4931 return &bpf_redirect_proto;
c46646d0
DB
4932 case BPF_FUNC_get_route_realm:
4933 return &bpf_get_route_realm_proto;
13c5c240
DB
4934 case BPF_FUNC_get_hash_recalc:
4935 return &bpf_get_hash_recalc_proto;
7a4b28c6
DB
4936 case BPF_FUNC_set_hash_invalid:
4937 return &bpf_set_hash_invalid_proto;
ded092cd
DB
4938 case BPF_FUNC_set_hash:
4939 return &bpf_set_hash_proto;
bd570ff9 4940 case BPF_FUNC_perf_event_output:
555c8a86 4941 return &bpf_skb_event_output_proto;
80b48c44
DB
4942 case BPF_FUNC_get_smp_processor_id:
4943 return &bpf_get_smp_processor_id_proto;
747ea55e
DB
4944 case BPF_FUNC_skb_under_cgroup:
4945 return &bpf_skb_under_cgroup_proto;
91b8270f
CF
4946 case BPF_FUNC_get_socket_cookie:
4947 return &bpf_get_socket_cookie_proto;
6acc5c29
CF
4948 case BPF_FUNC_get_socket_uid:
4949 return &bpf_get_socket_uid_proto;
cb20b08e
DB
4950 case BPF_FUNC_fib_lookup:
4951 return &bpf_skb_fib_lookup_proto;
12bed760
EB
4952#ifdef CONFIG_XFRM
4953 case BPF_FUNC_skb_get_xfrm_state:
4954 return &bpf_skb_get_xfrm_state_proto;
4955#endif
cb20b08e
DB
4956#ifdef CONFIG_SOCK_CGROUP_DATA
4957 case BPF_FUNC_skb_cgroup_id:
4958 return &bpf_skb_cgroup_id_proto;
4959#endif
608cd71a 4960 default:
2492d3b8 4961 return bpf_base_func_proto(func_id);
608cd71a
AS
4962 }
4963}
4964
6a773a15 4965static const struct bpf_func_proto *
5e43f899 4966xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6a773a15 4967{
4de16969
DB
4968 switch (func_id) {
4969 case BPF_FUNC_perf_event_output:
4970 return &bpf_xdp_event_output_proto;
669dc4d7
DB
4971 case BPF_FUNC_get_smp_processor_id:
4972 return &bpf_get_smp_processor_id_proto;
205c3807
DB
4973 case BPF_FUNC_csum_diff:
4974 return &bpf_csum_diff_proto;
17bedab2
MKL
4975 case BPF_FUNC_xdp_adjust_head:
4976 return &bpf_xdp_adjust_head_proto;
de8f3a83
DB
4977 case BPF_FUNC_xdp_adjust_meta:
4978 return &bpf_xdp_adjust_meta_proto;
814abfab
JF
4979 case BPF_FUNC_redirect:
4980 return &bpf_xdp_redirect_proto;
97f91a7c 4981 case BPF_FUNC_redirect_map:
e4a8e817 4982 return &bpf_xdp_redirect_map_proto;
b32cc5b9
NS
4983 case BPF_FUNC_xdp_adjust_tail:
4984 return &bpf_xdp_adjust_tail_proto;
87f5fc7e
DA
4985 case BPF_FUNC_fib_lookup:
4986 return &bpf_xdp_fib_lookup_proto;
4de16969 4987 default:
2492d3b8 4988 return bpf_base_func_proto(func_id);
4de16969 4989 }
6a773a15
BB
4990}
4991
8c4b4c7e 4992static const struct bpf_func_proto *
5e43f899 4993sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8c4b4c7e
LB
4994{
4995 switch (func_id) {
4996 case BPF_FUNC_setsockopt:
4997 return &bpf_setsockopt_proto;
cd86d1fd
LB
4998 case BPF_FUNC_getsockopt:
4999 return &bpf_getsockopt_proto;
b13d8807
LB
5000 case BPF_FUNC_sock_ops_cb_flags_set:
5001 return &bpf_sock_ops_cb_flags_set_proto;
174a79ff
JF
5002 case BPF_FUNC_sock_map_update:
5003 return &bpf_sock_map_update_proto;
81110384
JF
5004 case BPF_FUNC_sock_hash_update:
5005 return &bpf_sock_hash_update_proto;
d692f113
AI
5006 case BPF_FUNC_get_socket_cookie:
5007 return &bpf_get_socket_cookie_sock_ops_proto;
cd339431
RG
5008 case BPF_FUNC_get_local_storage:
5009 return &bpf_get_local_storage_proto;
8c4b4c7e
LB
5010 default:
5011 return bpf_base_func_proto(func_id);
5012 }
5013}
5014
5e43f899
AI
5015static const struct bpf_func_proto *
5016sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4f738adb
JF
5017{
5018 switch (func_id) {
5019 case BPF_FUNC_msg_redirect_map:
5020 return &bpf_msg_redirect_map_proto;
81110384
JF
5021 case BPF_FUNC_msg_redirect_hash:
5022 return &bpf_msg_redirect_hash_proto;
2a100317
JF
5023 case BPF_FUNC_msg_apply_bytes:
5024 return &bpf_msg_apply_bytes_proto;
91843d54
JF
5025 case BPF_FUNC_msg_cork_bytes:
5026 return &bpf_msg_cork_bytes_proto;
015632bb
JF
5027 case BPF_FUNC_msg_pull_data:
5028 return &bpf_msg_pull_data_proto;
cd339431
RG
5029 case BPF_FUNC_get_local_storage:
5030 return &bpf_get_local_storage_proto;
4f738adb
JF
5031 default:
5032 return bpf_base_func_proto(func_id);
5033 }
5034}
5035
5e43f899
AI
5036static const struct bpf_func_proto *
5037sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
b005fd18
JF
5038{
5039 switch (func_id) {
8a31db56
JF
5040 case BPF_FUNC_skb_store_bytes:
5041 return &bpf_skb_store_bytes_proto;
b005fd18
JF
5042 case BPF_FUNC_skb_load_bytes:
5043 return &bpf_skb_load_bytes_proto;
8a31db56 5044 case BPF_FUNC_skb_pull_data:
0ea488ff 5045 return &sk_skb_pull_data_proto;
8a31db56 5046 case BPF_FUNC_skb_change_tail:
0ea488ff 5047 return &sk_skb_change_tail_proto;
8a31db56 5048 case BPF_FUNC_skb_change_head:
0ea488ff 5049 return &sk_skb_change_head_proto;
b005fd18
JF
5050 case BPF_FUNC_get_socket_cookie:
5051 return &bpf_get_socket_cookie_proto;
5052 case BPF_FUNC_get_socket_uid:
5053 return &bpf_get_socket_uid_proto;
174a79ff
JF
5054 case BPF_FUNC_sk_redirect_map:
5055 return &bpf_sk_redirect_map_proto;
81110384
JF
5056 case BPF_FUNC_sk_redirect_hash:
5057 return &bpf_sk_redirect_hash_proto;
cd339431
RG
5058 case BPF_FUNC_get_local_storage:
5059 return &bpf_get_local_storage_proto;
b005fd18
JF
5060 default:
5061 return bpf_base_func_proto(func_id);
5062 }
5063}
5064
cd3092c7
MX
5065static const struct bpf_func_proto *
5066lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5067{
5068 switch (func_id) {
5069 case BPF_FUNC_skb_load_bytes:
5070 return &bpf_skb_load_bytes_proto;
5071 case BPF_FUNC_skb_pull_data:
5072 return &bpf_skb_pull_data_proto;
5073 case BPF_FUNC_csum_diff:
5074 return &bpf_csum_diff_proto;
5075 case BPF_FUNC_get_cgroup_classid:
5076 return &bpf_get_cgroup_classid_proto;
5077 case BPF_FUNC_get_route_realm:
5078 return &bpf_get_route_realm_proto;
5079 case BPF_FUNC_get_hash_recalc:
5080 return &bpf_get_hash_recalc_proto;
5081 case BPF_FUNC_perf_event_output:
5082 return &bpf_skb_event_output_proto;
5083 case BPF_FUNC_get_smp_processor_id:
5084 return &bpf_get_smp_processor_id_proto;
5085 case BPF_FUNC_skb_under_cgroup:
5086 return &bpf_skb_under_cgroup_proto;
5087 default:
5088 return bpf_base_func_proto(func_id);
5089 }
5090}
5091
5092static const struct bpf_func_proto *
5093lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5094{
5095 switch (func_id) {
5096 case BPF_FUNC_lwt_push_encap:
5097 return &bpf_lwt_push_encap_proto;
5098 default:
5099 return lwt_out_func_proto(func_id, prog);
5100 }
5101}
5102
3a0af8fd 5103static const struct bpf_func_proto *
5e43f899 5104lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
3a0af8fd
TG
5105{
5106 switch (func_id) {
5107 case BPF_FUNC_skb_get_tunnel_key:
5108 return &bpf_skb_get_tunnel_key_proto;
5109 case BPF_FUNC_skb_set_tunnel_key:
5110 return bpf_get_skb_set_tunnel_proto(func_id);
5111 case BPF_FUNC_skb_get_tunnel_opt:
5112 return &bpf_skb_get_tunnel_opt_proto;
5113 case BPF_FUNC_skb_set_tunnel_opt:
5114 return bpf_get_skb_set_tunnel_proto(func_id);
5115 case BPF_FUNC_redirect:
5116 return &bpf_redirect_proto;
5117 case BPF_FUNC_clone_redirect:
5118 return &bpf_clone_redirect_proto;
5119 case BPF_FUNC_skb_change_tail:
5120 return &bpf_skb_change_tail_proto;
5121 case BPF_FUNC_skb_change_head:
5122 return &bpf_skb_change_head_proto;
5123 case BPF_FUNC_skb_store_bytes:
5124 return &bpf_skb_store_bytes_proto;
5125 case BPF_FUNC_csum_update:
5126 return &bpf_csum_update_proto;
5127 case BPF_FUNC_l3_csum_replace:
5128 return &bpf_l3_csum_replace_proto;
5129 case BPF_FUNC_l4_csum_replace:
5130 return &bpf_l4_csum_replace_proto;
5131 case BPF_FUNC_set_hash_invalid:
5132 return &bpf_set_hash_invalid_proto;
5133 default:
cd3092c7 5134 return lwt_out_func_proto(func_id, prog);
3a0af8fd
TG
5135 }
5136}
5137
004d4b27
MX
5138static const struct bpf_func_proto *
5139lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5140{
5141 switch (func_id) {
61d76980 5142#if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
004d4b27
MX
5143 case BPF_FUNC_lwt_seg6_store_bytes:
5144 return &bpf_lwt_seg6_store_bytes_proto;
5145 case BPF_FUNC_lwt_seg6_action:
5146 return &bpf_lwt_seg6_action_proto;
5147 case BPF_FUNC_lwt_seg6_adjust_srh:
5148 return &bpf_lwt_seg6_adjust_srh_proto;
61d76980 5149#endif
004d4b27
MX
5150 default:
5151 return lwt_out_func_proto(func_id, prog);
3a0af8fd
TG
5152 }
5153}
5154
f96da094 5155static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
5e43f899 5156 const struct bpf_prog *prog,
f96da094 5157 struct bpf_insn_access_aux *info)
23994631 5158{
f96da094 5159 const int size_default = sizeof(__u32);
23994631 5160
9bac3d6d
AS
5161 if (off < 0 || off >= sizeof(struct __sk_buff))
5162 return false;
62c7989b 5163
4936e352 5164 /* The verifier guarantees that size > 0. */
9bac3d6d
AS
5165 if (off % size != 0)
5166 return false;
62c7989b
DB
5167
5168 switch (off) {
f96da094
DB
5169 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5170 if (off + size > offsetofend(struct __sk_buff, cb[4]))
62c7989b
DB
5171 return false;
5172 break;
8a31db56
JF
5173 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
5174 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
5175 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
5176 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
f96da094 5177 case bpf_ctx_range(struct __sk_buff, data):
de8f3a83 5178 case bpf_ctx_range(struct __sk_buff, data_meta):
f96da094
DB
5179 case bpf_ctx_range(struct __sk_buff, data_end):
5180 if (size != size_default)
23994631 5181 return false;
31fd8581
YS
5182 break;
5183 default:
f96da094 5184 /* Only narrow read access allowed for now. */
31fd8581 5185 if (type == BPF_WRITE) {
f96da094 5186 if (size != size_default)
31fd8581
YS
5187 return false;
5188 } else {
f96da094
DB
5189 bpf_ctx_record_field_size(info, size_default);
5190 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
23994631 5191 return false;
31fd8581 5192 }
62c7989b 5193 }
9bac3d6d
AS
5194
5195 return true;
5196}
5197
d691f9e8 5198static bool sk_filter_is_valid_access(int off, int size,
19de99f7 5199 enum bpf_access_type type,
5e43f899 5200 const struct bpf_prog *prog,
23994631 5201 struct bpf_insn_access_aux *info)
d691f9e8 5202{
db58ba45 5203 switch (off) {
f96da094
DB
5204 case bpf_ctx_range(struct __sk_buff, tc_classid):
5205 case bpf_ctx_range(struct __sk_buff, data):
de8f3a83 5206 case bpf_ctx_range(struct __sk_buff, data_meta):
f96da094 5207 case bpf_ctx_range(struct __sk_buff, data_end):
8a31db56 5208 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
045efa82 5209 return false;
db58ba45 5210 }
045efa82 5211
d691f9e8
AS
5212 if (type == BPF_WRITE) {
5213 switch (off) {
f96da094 5214 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
d691f9e8
AS
5215 break;
5216 default:
5217 return false;
5218 }
5219 }
5220
5e43f899 5221 return bpf_skb_is_valid_access(off, size, type, prog, info);
d691f9e8
AS
5222}
5223
3a0af8fd
TG
5224static bool lwt_is_valid_access(int off, int size,
5225 enum bpf_access_type type,
5e43f899 5226 const struct bpf_prog *prog,
23994631 5227 struct bpf_insn_access_aux *info)
3a0af8fd
TG
5228{
5229 switch (off) {
f96da094 5230 case bpf_ctx_range(struct __sk_buff, tc_classid):
8a31db56 5231 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
de8f3a83 5232 case bpf_ctx_range(struct __sk_buff, data_meta):
3a0af8fd
TG
5233 return false;
5234 }
5235
5236 if (type == BPF_WRITE) {
5237 switch (off) {
f96da094
DB
5238 case bpf_ctx_range(struct __sk_buff, mark):
5239 case bpf_ctx_range(struct __sk_buff, priority):
5240 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3a0af8fd
TG
5241 break;
5242 default:
5243 return false;
5244 }
5245 }
5246
f96da094
DB
5247 switch (off) {
5248 case bpf_ctx_range(struct __sk_buff, data):
5249 info->reg_type = PTR_TO_PACKET;
5250 break;
5251 case bpf_ctx_range(struct __sk_buff, data_end):
5252 info->reg_type = PTR_TO_PACKET_END;
5253 break;
5254 }
5255
5e43f899 5256 return bpf_skb_is_valid_access(off, size, type, prog, info);
3a0af8fd
TG
5257}
5258
aac3fc32
AI
5259/* Attach type specific accesses */
5260static bool __sock_filter_check_attach_type(int off,
5261 enum bpf_access_type access_type,
5262 enum bpf_attach_type attach_type)
61023658 5263{
aac3fc32
AI
5264 switch (off) {
5265 case offsetof(struct bpf_sock, bound_dev_if):
5266 case offsetof(struct bpf_sock, mark):
5267 case offsetof(struct bpf_sock, priority):
5268 switch (attach_type) {
5269 case BPF_CGROUP_INET_SOCK_CREATE:
5270 goto full_access;
5271 default:
5272 return false;
5273 }
5274 case bpf_ctx_range(struct bpf_sock, src_ip4):
5275 switch (attach_type) {
5276 case BPF_CGROUP_INET4_POST_BIND:
5277 goto read_only;
5278 default:
5279 return false;
5280 }
5281 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5282 switch (attach_type) {
5283 case BPF_CGROUP_INET6_POST_BIND:
5284 goto read_only;
5285 default:
5286 return false;
5287 }
5288 case bpf_ctx_range(struct bpf_sock, src_port):
5289 switch (attach_type) {
5290 case BPF_CGROUP_INET4_POST_BIND:
5291 case BPF_CGROUP_INET6_POST_BIND:
5292 goto read_only;
61023658
DA
5293 default:
5294 return false;
5295 }
5296 }
aac3fc32
AI
5297read_only:
5298 return access_type == BPF_READ;
5299full_access:
5300 return true;
5301}
5302
5303static bool __sock_filter_check_size(int off, int size,
5304 struct bpf_insn_access_aux *info)
5305{
5306 const int size_default = sizeof(__u32);
61023658 5307
aac3fc32
AI
5308 switch (off) {
5309 case bpf_ctx_range(struct bpf_sock, src_ip4):
5310 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5311 bpf_ctx_record_field_size(info, size_default);
5312 return bpf_ctx_narrow_access_ok(off, size, size_default);
5313 }
5314
5315 return size == size_default;
5316}
5317
5318static bool sock_filter_is_valid_access(int off, int size,
5319 enum bpf_access_type type,
5320 const struct bpf_prog *prog,
5321 struct bpf_insn_access_aux *info)
5322{
5323 if (off < 0 || off >= sizeof(struct bpf_sock))
61023658 5324 return false;
61023658
DA
5325 if (off % size != 0)
5326 return false;
aac3fc32
AI
5327 if (!__sock_filter_check_attach_type(off, type,
5328 prog->expected_attach_type))
5329 return false;
5330 if (!__sock_filter_check_size(off, size, info))
61023658 5331 return false;
61023658
DA
5332 return true;
5333}
5334
047b0ecd
DB
5335static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5336 const struct bpf_prog *prog, int drop_verdict)
36bbef52
DB
5337{
5338 struct bpf_insn *insn = insn_buf;
5339
5340 if (!direct_write)
5341 return 0;
5342
5343 /* if (!skb->cloned)
5344 * goto start;
5345 *
5346 * (Fast-path, otherwise approximation that we might be
5347 * a clone, do the rest in helper.)
5348 */
5349 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5350 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5351 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5352
5353 /* ret = bpf_skb_pull_data(skb, 0); */
5354 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5355 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5356 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5357 BPF_FUNC_skb_pull_data);
5358 /* if (!ret)
5359 * goto restore;
5360 * return TC_ACT_SHOT;
5361 */
5362 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
047b0ecd 5363 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
36bbef52
DB
5364 *insn++ = BPF_EXIT_INSN();
5365
5366 /* restore: */
5367 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
5368 /* start: */
5369 *insn++ = prog->insnsi[0];
5370
5371 return insn - insn_buf;
5372}
5373
e0cea7ce
DB
5374static int bpf_gen_ld_abs(const struct bpf_insn *orig,
5375 struct bpf_insn *insn_buf)
5376{
5377 bool indirect = BPF_MODE(orig->code) == BPF_IND;
5378 struct bpf_insn *insn = insn_buf;
5379
5380 /* We're guaranteed here that CTX is in R6. */
5381 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
5382 if (!indirect) {
5383 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
5384 } else {
5385 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
5386 if (orig->imm)
5387 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
5388 }
5389
5390 switch (BPF_SIZE(orig->code)) {
5391 case BPF_B:
5392 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
5393 break;
5394 case BPF_H:
5395 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
5396 break;
5397 case BPF_W:
5398 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
5399 break;
5400 }
5401
5402 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
5403 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
5404 *insn++ = BPF_EXIT_INSN();
5405
5406 return insn - insn_buf;
5407}
5408
047b0ecd
DB
5409static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
5410 const struct bpf_prog *prog)
5411{
5412 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
5413}
5414
d691f9e8 5415static bool tc_cls_act_is_valid_access(int off, int size,
19de99f7 5416 enum bpf_access_type type,
5e43f899 5417 const struct bpf_prog *prog,
23994631 5418 struct bpf_insn_access_aux *info)
d691f9e8
AS
5419{
5420 if (type == BPF_WRITE) {
5421 switch (off) {
f96da094
DB
5422 case bpf_ctx_range(struct __sk_buff, mark):
5423 case bpf_ctx_range(struct __sk_buff, tc_index):
5424 case bpf_ctx_range(struct __sk_buff, priority):
5425 case bpf_ctx_range(struct __sk_buff, tc_classid):
5426 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
d691f9e8
AS
5427 break;
5428 default:
5429 return false;
5430 }
5431 }
19de99f7 5432
f96da094
DB
5433 switch (off) {
5434 case bpf_ctx_range(struct __sk_buff, data):
5435 info->reg_type = PTR_TO_PACKET;
5436 break;
de8f3a83
DB
5437 case bpf_ctx_range(struct __sk_buff, data_meta):
5438 info->reg_type = PTR_TO_PACKET_META;
5439 break;
f96da094
DB
5440 case bpf_ctx_range(struct __sk_buff, data_end):
5441 info->reg_type = PTR_TO_PACKET_END;
5442 break;
8a31db56
JF
5443 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5444 return false;
f96da094
DB
5445 }
5446
5e43f899 5447 return bpf_skb_is_valid_access(off, size, type, prog, info);
d691f9e8
AS
5448}
5449
1afaf661 5450static bool __is_valid_xdp_access(int off, int size)
6a773a15
BB
5451{
5452 if (off < 0 || off >= sizeof(struct xdp_md))
5453 return false;
5454 if (off % size != 0)
5455 return false;
6088b582 5456 if (size != sizeof(__u32))
6a773a15
BB
5457 return false;
5458
5459 return true;
5460}
5461
5462static bool xdp_is_valid_access(int off, int size,
5463 enum bpf_access_type type,
5e43f899 5464 const struct bpf_prog *prog,
23994631 5465 struct bpf_insn_access_aux *info)
6a773a15 5466{
0d830032
JK
5467 if (type == BPF_WRITE) {
5468 if (bpf_prog_is_dev_bound(prog->aux)) {
5469 switch (off) {
5470 case offsetof(struct xdp_md, rx_queue_index):
5471 return __is_valid_xdp_access(off, size);
5472 }
5473 }
6a773a15 5474 return false;
0d830032 5475 }
6a773a15
BB
5476
5477 switch (off) {
5478 case offsetof(struct xdp_md, data):
23994631 5479 info->reg_type = PTR_TO_PACKET;
6a773a15 5480 break;
de8f3a83
DB
5481 case offsetof(struct xdp_md, data_meta):
5482 info->reg_type = PTR_TO_PACKET_META;
5483 break;
6a773a15 5484 case offsetof(struct xdp_md, data_end):
23994631 5485 info->reg_type = PTR_TO_PACKET_END;
6a773a15
BB
5486 break;
5487 }
5488
1afaf661 5489 return __is_valid_xdp_access(off, size);
6a773a15
BB
5490}
5491
5492void bpf_warn_invalid_xdp_action(u32 act)
5493{
9beb8bed
DB
5494 const u32 act_max = XDP_REDIRECT;
5495
5496 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
5497 act > act_max ? "Illegal" : "Driver unsupported",
5498 act);
6a773a15
BB
5499}
5500EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
5501
4fbac77d
AI
5502static bool sock_addr_is_valid_access(int off, int size,
5503 enum bpf_access_type type,
5504 const struct bpf_prog *prog,
5505 struct bpf_insn_access_aux *info)
5506{
5507 const int size_default = sizeof(__u32);
5508
5509 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
5510 return false;
5511 if (off % size != 0)
5512 return false;
5513
5514 /* Disallow access to IPv6 fields from IPv4 contex and vise
5515 * versa.
5516 */
5517 switch (off) {
5518 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5519 switch (prog->expected_attach_type) {
5520 case BPF_CGROUP_INET4_BIND:
d74bad4e 5521 case BPF_CGROUP_INET4_CONNECT:
1cedee13 5522 case BPF_CGROUP_UDP4_SENDMSG:
4fbac77d
AI
5523 break;
5524 default:
5525 return false;
5526 }
5527 break;
5528 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5529 switch (prog->expected_attach_type) {
5530 case BPF_CGROUP_INET6_BIND:
d74bad4e 5531 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
5532 case BPF_CGROUP_UDP6_SENDMSG:
5533 break;
5534 default:
5535 return false;
5536 }
5537 break;
5538 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5539 switch (prog->expected_attach_type) {
5540 case BPF_CGROUP_UDP4_SENDMSG:
5541 break;
5542 default:
5543 return false;
5544 }
5545 break;
5546 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5547 msg_src_ip6[3]):
5548 switch (prog->expected_attach_type) {
5549 case BPF_CGROUP_UDP6_SENDMSG:
4fbac77d
AI
5550 break;
5551 default:
5552 return false;
5553 }
5554 break;
5555 }
5556
5557 switch (off) {
5558 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5559 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
1cedee13
AI
5560 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5561 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5562 msg_src_ip6[3]):
4fbac77d
AI
5563 /* Only narrow read access allowed for now. */
5564 if (type == BPF_READ) {
5565 bpf_ctx_record_field_size(info, size_default);
5566 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5567 return false;
5568 } else {
5569 if (size != size_default)
5570 return false;
5571 }
5572 break;
5573 case bpf_ctx_range(struct bpf_sock_addr, user_port):
5574 if (size != size_default)
5575 return false;
5576 break;
5577 default:
5578 if (type == BPF_READ) {
5579 if (size != size_default)
5580 return false;
5581 } else {
5582 return false;
5583 }
5584 }
5585
5586 return true;
5587}
5588
44f0e430
LB
5589static bool sock_ops_is_valid_access(int off, int size,
5590 enum bpf_access_type type,
5e43f899 5591 const struct bpf_prog *prog,
44f0e430 5592 struct bpf_insn_access_aux *info)
40304b2a 5593{
44f0e430
LB
5594 const int size_default = sizeof(__u32);
5595
40304b2a
LB
5596 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5597 return false;
44f0e430 5598
40304b2a
LB
5599 /* The verifier guarantees that size > 0. */
5600 if (off % size != 0)
5601 return false;
40304b2a 5602
40304b2a
LB
5603 if (type == BPF_WRITE) {
5604 switch (off) {
2585cd62 5605 case offsetof(struct bpf_sock_ops, reply):
6f9bd3d7 5606 case offsetof(struct bpf_sock_ops, sk_txhash):
44f0e430
LB
5607 if (size != size_default)
5608 return false;
40304b2a
LB
5609 break;
5610 default:
5611 return false;
5612 }
44f0e430
LB
5613 } else {
5614 switch (off) {
5615 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5616 bytes_acked):
5617 if (size != sizeof(__u64))
5618 return false;
5619 break;
5620 default:
5621 if (size != size_default)
5622 return false;
5623 break;
5624 }
40304b2a
LB
5625 }
5626
44f0e430 5627 return true;
40304b2a
LB
5628}
5629
8a31db56
JF
5630static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5631 const struct bpf_prog *prog)
5632{
047b0ecd 5633 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
8a31db56
JF
5634}
5635
b005fd18
JF
5636static bool sk_skb_is_valid_access(int off, int size,
5637 enum bpf_access_type type,
5e43f899 5638 const struct bpf_prog *prog,
b005fd18
JF
5639 struct bpf_insn_access_aux *info)
5640{
de8f3a83
DB
5641 switch (off) {
5642 case bpf_ctx_range(struct __sk_buff, tc_classid):
5643 case bpf_ctx_range(struct __sk_buff, data_meta):
5644 return false;
5645 }
5646
8a31db56
JF
5647 if (type == BPF_WRITE) {
5648 switch (off) {
8a31db56
JF
5649 case bpf_ctx_range(struct __sk_buff, tc_index):
5650 case bpf_ctx_range(struct __sk_buff, priority):
5651 break;
5652 default:
5653 return false;
5654 }
5655 }
5656
b005fd18 5657 switch (off) {
f7e9cb1e 5658 case bpf_ctx_range(struct __sk_buff, mark):
8a31db56 5659 return false;
b005fd18
JF
5660 case bpf_ctx_range(struct __sk_buff, data):
5661 info->reg_type = PTR_TO_PACKET;
5662 break;
5663 case bpf_ctx_range(struct __sk_buff, data_end):
5664 info->reg_type = PTR_TO_PACKET_END;
5665 break;
5666 }
5667
5e43f899 5668 return bpf_skb_is_valid_access(off, size, type, prog, info);
b005fd18
JF
5669}
5670
4f738adb
JF
5671static bool sk_msg_is_valid_access(int off, int size,
5672 enum bpf_access_type type,
5e43f899 5673 const struct bpf_prog *prog,
4f738adb
JF
5674 struct bpf_insn_access_aux *info)
5675{
5676 if (type == BPF_WRITE)
5677 return false;
5678
5679 switch (off) {
5680 case offsetof(struct sk_msg_md, data):
5681 info->reg_type = PTR_TO_PACKET;
303def35
JF
5682 if (size != sizeof(__u64))
5683 return false;
4f738adb
JF
5684 break;
5685 case offsetof(struct sk_msg_md, data_end):
5686 info->reg_type = PTR_TO_PACKET_END;
303def35
JF
5687 if (size != sizeof(__u64))
5688 return false;
4f738adb 5689 break;
303def35
JF
5690 default:
5691 if (size != sizeof(__u32))
5692 return false;
4f738adb
JF
5693 }
5694
5695 if (off < 0 || off >= sizeof(struct sk_msg_md))
5696 return false;
5697 if (off % size != 0)
5698 return false;
4f738adb
JF
5699
5700 return true;
5701}
5702
2492d3b8
DB
5703static u32 bpf_convert_ctx_access(enum bpf_access_type type,
5704 const struct bpf_insn *si,
5705 struct bpf_insn *insn_buf,
f96da094 5706 struct bpf_prog *prog, u32 *target_size)
9bac3d6d
AS
5707{
5708 struct bpf_insn *insn = insn_buf;
6b8cc1d1 5709 int off;
9bac3d6d 5710
6b8cc1d1 5711 switch (si->off) {
9bac3d6d 5712 case offsetof(struct __sk_buff, len):
6b8cc1d1 5713 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5714 bpf_target_off(struct sk_buff, len, 4,
5715 target_size));
9bac3d6d
AS
5716 break;
5717
0b8c707d 5718 case offsetof(struct __sk_buff, protocol):
6b8cc1d1 5719 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
5720 bpf_target_off(struct sk_buff, protocol, 2,
5721 target_size));
0b8c707d
DB
5722 break;
5723
27cd5452 5724 case offsetof(struct __sk_buff, vlan_proto):
6b8cc1d1 5725 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
5726 bpf_target_off(struct sk_buff, vlan_proto, 2,
5727 target_size));
27cd5452
MS
5728 break;
5729
bcad5718 5730 case offsetof(struct __sk_buff, priority):
754f1e6a 5731 if (type == BPF_WRITE)
6b8cc1d1 5732 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5733 bpf_target_off(struct sk_buff, priority, 4,
5734 target_size));
754f1e6a 5735 else
6b8cc1d1 5736 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5737 bpf_target_off(struct sk_buff, priority, 4,
5738 target_size));
bcad5718
DB
5739 break;
5740
37e82c2f 5741 case offsetof(struct __sk_buff, ingress_ifindex):
6b8cc1d1 5742 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5743 bpf_target_off(struct sk_buff, skb_iif, 4,
5744 target_size));
37e82c2f
AS
5745 break;
5746
5747 case offsetof(struct __sk_buff, ifindex):
f035a515 5748 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 5749 si->dst_reg, si->src_reg,
37e82c2f 5750 offsetof(struct sk_buff, dev));
6b8cc1d1
DB
5751 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
5752 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
5753 bpf_target_off(struct net_device, ifindex, 4,
5754 target_size));
37e82c2f
AS
5755 break;
5756
ba7591d8 5757 case offsetof(struct __sk_buff, hash):
6b8cc1d1 5758 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5759 bpf_target_off(struct sk_buff, hash, 4,
5760 target_size));
ba7591d8
DB
5761 break;
5762
9bac3d6d 5763 case offsetof(struct __sk_buff, mark):
d691f9e8 5764 if (type == BPF_WRITE)
6b8cc1d1 5765 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5766 bpf_target_off(struct sk_buff, mark, 4,
5767 target_size));
d691f9e8 5768 else
6b8cc1d1 5769 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5770 bpf_target_off(struct sk_buff, mark, 4,
5771 target_size));
d691f9e8 5772 break;
9bac3d6d
AS
5773
5774 case offsetof(struct __sk_buff, pkt_type):
f96da094
DB
5775 *target_size = 1;
5776 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
5777 PKT_TYPE_OFFSET());
5778 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
5779#ifdef __BIG_ENDIAN_BITFIELD
5780 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
5781#endif
5782 break;
9bac3d6d
AS
5783
5784 case offsetof(struct __sk_buff, queue_mapping):
f96da094
DB
5785 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5786 bpf_target_off(struct sk_buff, queue_mapping, 2,
5787 target_size));
5788 break;
c2497395 5789
c2497395 5790 case offsetof(struct __sk_buff, vlan_present):
c2497395 5791 case offsetof(struct __sk_buff, vlan_tci):
f96da094
DB
5792 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
5793
5794 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5795 bpf_target_off(struct sk_buff, vlan_tci, 2,
5796 target_size));
5797 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
5798 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
5799 ~VLAN_TAG_PRESENT);
5800 } else {
5801 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
5802 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
5803 }
5804 break;
d691f9e8
AS
5805
5806 case offsetof(struct __sk_buff, cb[0]) ...
f96da094 5807 offsetofend(struct __sk_buff, cb[4]) - 1:
d691f9e8 5808 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
62c7989b
DB
5809 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
5810 offsetof(struct qdisc_skb_cb, data)) %
5811 sizeof(__u64));
d691f9e8 5812
ff936a04 5813 prog->cb_access = 1;
6b8cc1d1
DB
5814 off = si->off;
5815 off -= offsetof(struct __sk_buff, cb[0]);
5816 off += offsetof(struct sk_buff, cb);
5817 off += offsetof(struct qdisc_skb_cb, data);
d691f9e8 5818 if (type == BPF_WRITE)
62c7989b 5819 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 5820 si->src_reg, off);
d691f9e8 5821 else
62c7989b 5822 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
6b8cc1d1 5823 si->src_reg, off);
d691f9e8
AS
5824 break;
5825
045efa82 5826 case offsetof(struct __sk_buff, tc_classid):
6b8cc1d1
DB
5827 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
5828
5829 off = si->off;
5830 off -= offsetof(struct __sk_buff, tc_classid);
5831 off += offsetof(struct sk_buff, cb);
5832 off += offsetof(struct qdisc_skb_cb, tc_classid);
f96da094 5833 *target_size = 2;
09c37a2c 5834 if (type == BPF_WRITE)
6b8cc1d1
DB
5835 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
5836 si->src_reg, off);
09c37a2c 5837 else
6b8cc1d1
DB
5838 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
5839 si->src_reg, off);
045efa82
DB
5840 break;
5841
db58ba45 5842 case offsetof(struct __sk_buff, data):
f035a515 5843 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
6b8cc1d1 5844 si->dst_reg, si->src_reg,
db58ba45
AS
5845 offsetof(struct sk_buff, data));
5846 break;
5847
de8f3a83
DB
5848 case offsetof(struct __sk_buff, data_meta):
5849 off = si->off;
5850 off -= offsetof(struct __sk_buff, data_meta);
5851 off += offsetof(struct sk_buff, cb);
5852 off += offsetof(struct bpf_skb_data_end, data_meta);
5853 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5854 si->src_reg, off);
5855 break;
5856
db58ba45 5857 case offsetof(struct __sk_buff, data_end):
6b8cc1d1
DB
5858 off = si->off;
5859 off -= offsetof(struct __sk_buff, data_end);
5860 off += offsetof(struct sk_buff, cb);
5861 off += offsetof(struct bpf_skb_data_end, data_end);
5862 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5863 si->src_reg, off);
db58ba45
AS
5864 break;
5865
d691f9e8
AS
5866 case offsetof(struct __sk_buff, tc_index):
5867#ifdef CONFIG_NET_SCHED
d691f9e8 5868 if (type == BPF_WRITE)
6b8cc1d1 5869 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
5870 bpf_target_off(struct sk_buff, tc_index, 2,
5871 target_size));
d691f9e8 5872 else
6b8cc1d1 5873 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
f96da094
DB
5874 bpf_target_off(struct sk_buff, tc_index, 2,
5875 target_size));
d691f9e8 5876#else
2ed46ce4 5877 *target_size = 2;
d691f9e8 5878 if (type == BPF_WRITE)
6b8cc1d1 5879 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
d691f9e8 5880 else
6b8cc1d1 5881 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
b1d9fc41
DB
5882#endif
5883 break;
5884
5885 case offsetof(struct __sk_buff, napi_id):
5886#if defined(CONFIG_NET_RX_BUSY_POLL)
b1d9fc41 5887 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
f96da094
DB
5888 bpf_target_off(struct sk_buff, napi_id, 4,
5889 target_size));
b1d9fc41
DB
5890 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
5891 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5892#else
2ed46ce4 5893 *target_size = 4;
b1d9fc41 5894 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
d691f9e8 5895#endif
6b8cc1d1 5896 break;
8a31db56
JF
5897 case offsetof(struct __sk_buff, family):
5898 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
5899
5900 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5901 si->dst_reg, si->src_reg,
5902 offsetof(struct sk_buff, sk));
5903 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5904 bpf_target_off(struct sock_common,
5905 skc_family,
5906 2, target_size));
5907 break;
5908 case offsetof(struct __sk_buff, remote_ip4):
5909 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
5910
5911 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5912 si->dst_reg, si->src_reg,
5913 offsetof(struct sk_buff, sk));
5914 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5915 bpf_target_off(struct sock_common,
5916 skc_daddr,
5917 4, target_size));
5918 break;
5919 case offsetof(struct __sk_buff, local_ip4):
5920 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5921 skc_rcv_saddr) != 4);
5922
5923 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5924 si->dst_reg, si->src_reg,
5925 offsetof(struct sk_buff, sk));
5926 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5927 bpf_target_off(struct sock_common,
5928 skc_rcv_saddr,
5929 4, target_size));
5930 break;
5931 case offsetof(struct __sk_buff, remote_ip6[0]) ...
5932 offsetof(struct __sk_buff, remote_ip6[3]):
5933#if IS_ENABLED(CONFIG_IPV6)
5934 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5935 skc_v6_daddr.s6_addr32[0]) != 4);
5936
5937 off = si->off;
5938 off -= offsetof(struct __sk_buff, remote_ip6[0]);
5939
5940 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5941 si->dst_reg, si->src_reg,
5942 offsetof(struct sk_buff, sk));
5943 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5944 offsetof(struct sock_common,
5945 skc_v6_daddr.s6_addr32[0]) +
5946 off);
5947#else
5948 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5949#endif
5950 break;
5951 case offsetof(struct __sk_buff, local_ip6[0]) ...
5952 offsetof(struct __sk_buff, local_ip6[3]):
5953#if IS_ENABLED(CONFIG_IPV6)
5954 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5955 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
5956
5957 off = si->off;
5958 off -= offsetof(struct __sk_buff, local_ip6[0]);
5959
5960 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5961 si->dst_reg, si->src_reg,
5962 offsetof(struct sk_buff, sk));
5963 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5964 offsetof(struct sock_common,
5965 skc_v6_rcv_saddr.s6_addr32[0]) +
5966 off);
5967#else
5968 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5969#endif
5970 break;
5971
5972 case offsetof(struct __sk_buff, remote_port):
5973 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
5974
5975 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5976 si->dst_reg, si->src_reg,
5977 offsetof(struct sk_buff, sk));
5978 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5979 bpf_target_off(struct sock_common,
5980 skc_dport,
5981 2, target_size));
5982#ifndef __BIG_ENDIAN_BITFIELD
5983 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
5984#endif
5985 break;
5986
5987 case offsetof(struct __sk_buff, local_port):
5988 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
5989
5990 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5991 si->dst_reg, si->src_reg,
5992 offsetof(struct sk_buff, sk));
5993 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5994 bpf_target_off(struct sock_common,
5995 skc_num, 2, target_size));
5996 break;
9bac3d6d
AS
5997 }
5998
5999 return insn - insn_buf;
89aa0758
AS
6000}
6001
61023658 6002static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
6b8cc1d1 6003 const struct bpf_insn *si,
61023658 6004 struct bpf_insn *insn_buf,
f96da094 6005 struct bpf_prog *prog, u32 *target_size)
61023658
DA
6006{
6007 struct bpf_insn *insn = insn_buf;
aac3fc32 6008 int off;
61023658 6009
6b8cc1d1 6010 switch (si->off) {
61023658
DA
6011 case offsetof(struct bpf_sock, bound_dev_if):
6012 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
6013
6014 if (type == BPF_WRITE)
6b8cc1d1 6015 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
6016 offsetof(struct sock, sk_bound_dev_if));
6017 else
6b8cc1d1 6018 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
61023658
DA
6019 offsetof(struct sock, sk_bound_dev_if));
6020 break;
aa4c1037 6021
482dca93
DA
6022 case offsetof(struct bpf_sock, mark):
6023 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
6024
6025 if (type == BPF_WRITE)
6026 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6027 offsetof(struct sock, sk_mark));
6028 else
6029 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6030 offsetof(struct sock, sk_mark));
6031 break;
6032
6033 case offsetof(struct bpf_sock, priority):
6034 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
6035
6036 if (type == BPF_WRITE)
6037 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6038 offsetof(struct sock, sk_priority));
6039 else
6040 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6041 offsetof(struct sock, sk_priority));
6042 break;
6043
aa4c1037
DA
6044 case offsetof(struct bpf_sock, family):
6045 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
6046
6b8cc1d1 6047 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
aa4c1037
DA
6048 offsetof(struct sock, sk_family));
6049 break;
6050
6051 case offsetof(struct bpf_sock, type):
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_TYPE_MASK);
6055 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
aa4c1037
DA
6056 break;
6057
6058 case offsetof(struct bpf_sock, protocol):
6b8cc1d1 6059 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
aa4c1037 6060 offsetof(struct sock, __sk_flags_offset));
6b8cc1d1
DB
6061 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6062 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
aa4c1037 6063 break;
aac3fc32
AI
6064
6065 case offsetof(struct bpf_sock, src_ip4):
6066 *insn++ = BPF_LDX_MEM(
6067 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6068 bpf_target_off(struct sock_common, skc_rcv_saddr,
6069 FIELD_SIZEOF(struct sock_common,
6070 skc_rcv_saddr),
6071 target_size));
6072 break;
6073
6074 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6075#if IS_ENABLED(CONFIG_IPV6)
6076 off = si->off;
6077 off -= offsetof(struct bpf_sock, src_ip6[0]);
6078 *insn++ = BPF_LDX_MEM(
6079 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6080 bpf_target_off(
6081 struct sock_common,
6082 skc_v6_rcv_saddr.s6_addr32[0],
6083 FIELD_SIZEOF(struct sock_common,
6084 skc_v6_rcv_saddr.s6_addr32[0]),
6085 target_size) + off);
6086#else
6087 (void)off;
6088 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6089#endif
6090 break;
6091
6092 case offsetof(struct bpf_sock, src_port):
6093 *insn++ = BPF_LDX_MEM(
6094 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
6095 si->dst_reg, si->src_reg,
6096 bpf_target_off(struct sock_common, skc_num,
6097 FIELD_SIZEOF(struct sock_common,
6098 skc_num),
6099 target_size));
6100 break;
61023658
DA
6101 }
6102
6103 return insn - insn_buf;
6104}
6105
6b8cc1d1
DB
6106static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
6107 const struct bpf_insn *si,
374fb54e 6108 struct bpf_insn *insn_buf,
f96da094 6109 struct bpf_prog *prog, u32 *target_size)
374fb54e
DB
6110{
6111 struct bpf_insn *insn = insn_buf;
6112
6b8cc1d1 6113 switch (si->off) {
374fb54e 6114 case offsetof(struct __sk_buff, ifindex):
374fb54e 6115 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6b8cc1d1 6116 si->dst_reg, si->src_reg,
374fb54e 6117 offsetof(struct sk_buff, dev));
6b8cc1d1 6118 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
f96da094
DB
6119 bpf_target_off(struct net_device, ifindex, 4,
6120 target_size));
374fb54e
DB
6121 break;
6122 default:
f96da094
DB
6123 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6124 target_size);
374fb54e
DB
6125 }
6126
6127 return insn - insn_buf;
6128}
6129
6b8cc1d1
DB
6130static u32 xdp_convert_ctx_access(enum bpf_access_type type,
6131 const struct bpf_insn *si,
6a773a15 6132 struct bpf_insn *insn_buf,
f96da094 6133 struct bpf_prog *prog, u32 *target_size)
6a773a15
BB
6134{
6135 struct bpf_insn *insn = insn_buf;
6136
6b8cc1d1 6137 switch (si->off) {
6a773a15 6138 case offsetof(struct xdp_md, data):
f035a515 6139 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6b8cc1d1 6140 si->dst_reg, si->src_reg,
6a773a15
BB
6141 offsetof(struct xdp_buff, data));
6142 break;
de8f3a83
DB
6143 case offsetof(struct xdp_md, data_meta):
6144 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
6145 si->dst_reg, si->src_reg,
6146 offsetof(struct xdp_buff, data_meta));
6147 break;
6a773a15 6148 case offsetof(struct xdp_md, data_end):
f035a515 6149 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6b8cc1d1 6150 si->dst_reg, si->src_reg,
6a773a15
BB
6151 offsetof(struct xdp_buff, data_end));
6152 break;
02dd3291
JDB
6153 case offsetof(struct xdp_md, ingress_ifindex):
6154 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6155 si->dst_reg, si->src_reg,
6156 offsetof(struct xdp_buff, rxq));
6157 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
6158 si->dst_reg, si->dst_reg,
6159 offsetof(struct xdp_rxq_info, dev));
6160 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
daaf24c6 6161 offsetof(struct net_device, ifindex));
02dd3291
JDB
6162 break;
6163 case offsetof(struct xdp_md, rx_queue_index):
6164 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6165 si->dst_reg, si->src_reg,
6166 offsetof(struct xdp_buff, rxq));
6167 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
daaf24c6
JDB
6168 offsetof(struct xdp_rxq_info,
6169 queue_index));
02dd3291 6170 break;
6a773a15
BB
6171 }
6172
6173 return insn - insn_buf;
6174}
6175
4fbac77d
AI
6176/* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
6177 * context Structure, F is Field in context structure that contains a pointer
6178 * to Nested Structure of type NS that has the field NF.
6179 *
6180 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
6181 * sure that SIZE is not greater than actual size of S.F.NF.
6182 *
6183 * If offset OFF is provided, the load happens from that offset relative to
6184 * offset of NF.
6185 */
6186#define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
6187 do { \
6188 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
6189 si->src_reg, offsetof(S, F)); \
6190 *insn++ = BPF_LDX_MEM( \
6191 SIZE, si->dst_reg, si->dst_reg, \
6192 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6193 target_size) \
6194 + OFF); \
6195 } while (0)
6196
6197#define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
6198 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
6199 BPF_FIELD_SIZEOF(NS, NF), 0)
6200
6201/* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
6202 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
6203 *
6204 * It doesn't support SIZE argument though since narrow stores are not
6205 * supported for now.
6206 *
6207 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
6208 * "register" since two registers available in convert_ctx_access are not
6209 * enough: we can't override neither SRC, since it contains value to store, nor
6210 * DST since it contains pointer to context that may be used by later
6211 * instructions. But we need a temporary place to save pointer to nested
6212 * structure whose field we want to store to.
6213 */
6214#define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
6215 do { \
6216 int tmp_reg = BPF_REG_9; \
6217 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6218 --tmp_reg; \
6219 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6220 --tmp_reg; \
6221 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
6222 offsetof(S, TF)); \
6223 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
6224 si->dst_reg, offsetof(S, F)); \
6225 *insn++ = BPF_STX_MEM( \
6226 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
6227 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6228 target_size) \
6229 + OFF); \
6230 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
6231 offsetof(S, TF)); \
6232 } while (0)
6233
6234#define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
6235 TF) \
6236 do { \
6237 if (type == BPF_WRITE) { \
6238 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6239 TF); \
6240 } else { \
6241 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6242 S, NS, F, NF, SIZE, OFF); \
6243 } \
6244 } while (0)
6245
6246#define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6247 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6248 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6249
6250static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6251 const struct bpf_insn *si,
6252 struct bpf_insn *insn_buf,
6253 struct bpf_prog *prog, u32 *target_size)
6254{
6255 struct bpf_insn *insn = insn_buf;
6256 int off;
6257
6258 switch (si->off) {
6259 case offsetof(struct bpf_sock_addr, user_family):
6260 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6261 struct sockaddr, uaddr, sa_family);
6262 break;
6263
6264 case offsetof(struct bpf_sock_addr, user_ip4):
6265 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6266 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6267 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6268 break;
6269
6270 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6271 off = si->off;
6272 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6273 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6274 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6275 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6276 tmp_reg);
6277 break;
6278
6279 case offsetof(struct bpf_sock_addr, user_port):
6280 /* To get port we need to know sa_family first and then treat
6281 * sockaddr as either sockaddr_in or sockaddr_in6.
6282 * Though we can simplify since port field has same offset and
6283 * size in both structures.
6284 * Here we check this invariant and use just one of the
6285 * structures if it's true.
6286 */
6287 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
6288 offsetof(struct sockaddr_in6, sin6_port));
6289 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
6290 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
6291 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
6292 struct sockaddr_in6, uaddr,
6293 sin6_port, tmp_reg);
6294 break;
6295
6296 case offsetof(struct bpf_sock_addr, family):
6297 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6298 struct sock, sk, sk_family);
6299 break;
6300
6301 case offsetof(struct bpf_sock_addr, type):
6302 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6303 struct bpf_sock_addr_kern, struct sock, sk,
6304 __sk_flags_offset, BPF_W, 0);
6305 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6306 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6307 break;
6308
6309 case offsetof(struct bpf_sock_addr, protocol):
6310 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6311 struct bpf_sock_addr_kern, struct sock, sk,
6312 __sk_flags_offset, BPF_W, 0);
6313 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6314 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
6315 SK_FL_PROTO_SHIFT);
6316 break;
1cedee13
AI
6317
6318 case offsetof(struct bpf_sock_addr, msg_src_ip4):
6319 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
6320 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6321 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
6322 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
6323 break;
6324
6325 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6326 msg_src_ip6[3]):
6327 off = si->off;
6328 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
6329 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
6330 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6331 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
6332 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
6333 break;
4fbac77d
AI
6334 }
6335
6336 return insn - insn_buf;
6337}
6338
40304b2a
LB
6339static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
6340 const struct bpf_insn *si,
6341 struct bpf_insn *insn_buf,
f96da094
DB
6342 struct bpf_prog *prog,
6343 u32 *target_size)
40304b2a
LB
6344{
6345 struct bpf_insn *insn = insn_buf;
6346 int off;
6347
6348 switch (si->off) {
6349 case offsetof(struct bpf_sock_ops, op) ...
6350 offsetof(struct bpf_sock_ops, replylong[3]):
6351 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
6352 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
6353 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
6354 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
6355 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
6356 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
6357 off = si->off;
6358 off -= offsetof(struct bpf_sock_ops, op);
6359 off += offsetof(struct bpf_sock_ops_kern, op);
6360 if (type == BPF_WRITE)
6361 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6362 off);
6363 else
6364 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6365 off);
6366 break;
6367
6368 case offsetof(struct bpf_sock_ops, family):
6369 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6370
6371 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6372 struct bpf_sock_ops_kern, sk),
6373 si->dst_reg, si->src_reg,
6374 offsetof(struct bpf_sock_ops_kern, sk));
6375 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6376 offsetof(struct sock_common, skc_family));
6377 break;
6378
6379 case offsetof(struct bpf_sock_ops, remote_ip4):
6380 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6381
6382 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6383 struct bpf_sock_ops_kern, sk),
6384 si->dst_reg, si->src_reg,
6385 offsetof(struct bpf_sock_ops_kern, sk));
6386 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6387 offsetof(struct sock_common, skc_daddr));
6388 break;
6389
6390 case offsetof(struct bpf_sock_ops, local_ip4):
303def35
JF
6391 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6392 skc_rcv_saddr) != 4);
40304b2a
LB
6393
6394 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6395 struct bpf_sock_ops_kern, sk),
6396 si->dst_reg, si->src_reg,
6397 offsetof(struct bpf_sock_ops_kern, sk));
6398 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6399 offsetof(struct sock_common,
6400 skc_rcv_saddr));
6401 break;
6402
6403 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
6404 offsetof(struct bpf_sock_ops, remote_ip6[3]):
6405#if IS_ENABLED(CONFIG_IPV6)
6406 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6407 skc_v6_daddr.s6_addr32[0]) != 4);
6408
6409 off = si->off;
6410 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
6411 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6412 struct bpf_sock_ops_kern, sk),
6413 si->dst_reg, si->src_reg,
6414 offsetof(struct bpf_sock_ops_kern, sk));
6415 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6416 offsetof(struct sock_common,
6417 skc_v6_daddr.s6_addr32[0]) +
6418 off);
6419#else
6420 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6421#endif
6422 break;
6423
6424 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
6425 offsetof(struct bpf_sock_ops, local_ip6[3]):
6426#if IS_ENABLED(CONFIG_IPV6)
6427 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6428 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6429
6430 off = si->off;
6431 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
6432 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6433 struct bpf_sock_ops_kern, sk),
6434 si->dst_reg, si->src_reg,
6435 offsetof(struct bpf_sock_ops_kern, sk));
6436 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6437 offsetof(struct sock_common,
6438 skc_v6_rcv_saddr.s6_addr32[0]) +
6439 off);
6440#else
6441 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6442#endif
6443 break;
6444
6445 case offsetof(struct bpf_sock_ops, remote_port):
6446 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6447
6448 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6449 struct bpf_sock_ops_kern, sk),
6450 si->dst_reg, si->src_reg,
6451 offsetof(struct bpf_sock_ops_kern, sk));
6452 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6453 offsetof(struct sock_common, skc_dport));
6454#ifndef __BIG_ENDIAN_BITFIELD
6455 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6456#endif
6457 break;
6458
6459 case offsetof(struct bpf_sock_ops, local_port):
6460 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6461
6462 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6463 struct bpf_sock_ops_kern, sk),
6464 si->dst_reg, si->src_reg,
6465 offsetof(struct bpf_sock_ops_kern, sk));
6466 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6467 offsetof(struct sock_common, skc_num));
6468 break;
f19397a5
LB
6469
6470 case offsetof(struct bpf_sock_ops, is_fullsock):
6471 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6472 struct bpf_sock_ops_kern,
6473 is_fullsock),
6474 si->dst_reg, si->src_reg,
6475 offsetof(struct bpf_sock_ops_kern,
6476 is_fullsock));
6477 break;
6478
44f0e430
LB
6479 case offsetof(struct bpf_sock_ops, state):
6480 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
6481
6482 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6483 struct bpf_sock_ops_kern, sk),
6484 si->dst_reg, si->src_reg,
6485 offsetof(struct bpf_sock_ops_kern, sk));
6486 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
6487 offsetof(struct sock_common, skc_state));
6488 break;
6489
6490 case offsetof(struct bpf_sock_ops, rtt_min):
6491 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
6492 sizeof(struct minmax));
6493 BUILD_BUG_ON(sizeof(struct minmax) <
6494 sizeof(struct minmax_sample));
6495
6496 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6497 struct bpf_sock_ops_kern, sk),
6498 si->dst_reg, si->src_reg,
6499 offsetof(struct bpf_sock_ops_kern, sk));
6500 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6501 offsetof(struct tcp_sock, rtt_min) +
6502 FIELD_SIZEOF(struct minmax_sample, t));
6503 break;
6504
34d367c5
LB
6505/* Helper macro for adding read access to tcp_sock or sock fields. */
6506#define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
f19397a5 6507 do { \
34d367c5
LB
6508 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6509 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
f19397a5
LB
6510 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6511 struct bpf_sock_ops_kern, \
6512 is_fullsock), \
6513 si->dst_reg, si->src_reg, \
6514 offsetof(struct bpf_sock_ops_kern, \
6515 is_fullsock)); \
6516 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
6517 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6518 struct bpf_sock_ops_kern, sk),\
6519 si->dst_reg, si->src_reg, \
6520 offsetof(struct bpf_sock_ops_kern, sk));\
34d367c5
LB
6521 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
6522 OBJ_FIELD), \
6523 si->dst_reg, si->dst_reg, \
6524 offsetof(OBJ, OBJ_FIELD)); \
f19397a5
LB
6525 } while (0)
6526
b73042b8
LB
6527/* Helper macro for adding write access to tcp_sock or sock fields.
6528 * The macro is called with two registers, dst_reg which contains a pointer
6529 * to ctx (context) and src_reg which contains the value that should be
6530 * stored. However, we need an additional register since we cannot overwrite
6531 * dst_reg because it may be used later in the program.
6532 * Instead we "borrow" one of the other register. We first save its value
6533 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
6534 * it at the end of the macro.
6535 */
6536#define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6537 do { \
6538 int reg = BPF_REG_9; \
6539 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6540 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6541 if (si->dst_reg == reg || si->src_reg == reg) \
6542 reg--; \
6543 if (si->dst_reg == reg || si->src_reg == reg) \
6544 reg--; \
6545 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
6546 offsetof(struct bpf_sock_ops_kern, \
6547 temp)); \
6548 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6549 struct bpf_sock_ops_kern, \
6550 is_fullsock), \
6551 reg, si->dst_reg, \
6552 offsetof(struct bpf_sock_ops_kern, \
6553 is_fullsock)); \
6554 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
6555 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6556 struct bpf_sock_ops_kern, sk),\
6557 reg, si->dst_reg, \
6558 offsetof(struct bpf_sock_ops_kern, sk));\
6559 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
6560 reg, si->src_reg, \
6561 offsetof(OBJ, OBJ_FIELD)); \
6562 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
6563 offsetof(struct bpf_sock_ops_kern, \
6564 temp)); \
6565 } while (0)
6566
6567#define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
6568 do { \
6569 if (TYPE == BPF_WRITE) \
6570 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6571 else \
6572 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6573 } while (0)
6574
f19397a5 6575 case offsetof(struct bpf_sock_ops, snd_cwnd):
34d367c5 6576 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
f19397a5
LB
6577 break;
6578
6579 case offsetof(struct bpf_sock_ops, srtt_us):
34d367c5 6580 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
f19397a5 6581 break;
b13d8807
LB
6582
6583 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
6584 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
6585 struct tcp_sock);
6586 break;
44f0e430
LB
6587
6588 case offsetof(struct bpf_sock_ops, snd_ssthresh):
6589 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
6590 break;
6591
6592 case offsetof(struct bpf_sock_ops, rcv_nxt):
6593 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
6594 break;
6595
6596 case offsetof(struct bpf_sock_ops, snd_nxt):
6597 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
6598 break;
6599
6600 case offsetof(struct bpf_sock_ops, snd_una):
6601 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
6602 break;
6603
6604 case offsetof(struct bpf_sock_ops, mss_cache):
6605 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
6606 break;
6607
6608 case offsetof(struct bpf_sock_ops, ecn_flags):
6609 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6610 break;
6611
6612 case offsetof(struct bpf_sock_ops, rate_delivered):
6613 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6614 struct tcp_sock);
6615 break;
6616
6617 case offsetof(struct bpf_sock_ops, rate_interval_us):
6618 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6619 struct tcp_sock);
6620 break;
6621
6622 case offsetof(struct bpf_sock_ops, packets_out):
6623 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6624 break;
6625
6626 case offsetof(struct bpf_sock_ops, retrans_out):
6627 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6628 break;
6629
6630 case offsetof(struct bpf_sock_ops, total_retrans):
6631 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6632 struct tcp_sock);
6633 break;
6634
6635 case offsetof(struct bpf_sock_ops, segs_in):
6636 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
6637 break;
6638
6639 case offsetof(struct bpf_sock_ops, data_segs_in):
6640 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
6641 break;
6642
6643 case offsetof(struct bpf_sock_ops, segs_out):
6644 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
6645 break;
6646
6647 case offsetof(struct bpf_sock_ops, data_segs_out):
6648 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
6649 struct tcp_sock);
6650 break;
6651
6652 case offsetof(struct bpf_sock_ops, lost_out):
6653 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
6654 break;
6655
6656 case offsetof(struct bpf_sock_ops, sacked_out):
6657 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
6658 break;
6659
6660 case offsetof(struct bpf_sock_ops, sk_txhash):
6f9bd3d7
LB
6661 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
6662 struct sock, type);
44f0e430
LB
6663 break;
6664
6665 case offsetof(struct bpf_sock_ops, bytes_received):
6666 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
6667 struct tcp_sock);
6668 break;
6669
6670 case offsetof(struct bpf_sock_ops, bytes_acked):
6671 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
6672 break;
6f9bd3d7 6673
40304b2a
LB
6674 }
6675 return insn - insn_buf;
6676}
6677
8108a775
JF
6678static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
6679 const struct bpf_insn *si,
6680 struct bpf_insn *insn_buf,
6681 struct bpf_prog *prog, u32 *target_size)
6682{
6683 struct bpf_insn *insn = insn_buf;
6684 int off;
6685
6686 switch (si->off) {
6687 case offsetof(struct __sk_buff, data_end):
6688 off = si->off;
6689 off -= offsetof(struct __sk_buff, data_end);
6690 off += offsetof(struct sk_buff, cb);
6691 off += offsetof(struct tcp_skb_cb, bpf.data_end);
6692 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6693 si->src_reg, off);
6694 break;
6695 default:
6696 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6697 target_size);
6698 }
6699
6700 return insn - insn_buf;
6701}
6702
4f738adb
JF
6703static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
6704 const struct bpf_insn *si,
6705 struct bpf_insn *insn_buf,
6706 struct bpf_prog *prog, u32 *target_size)
6707{
6708 struct bpf_insn *insn = insn_buf;
720e7f38 6709#if IS_ENABLED(CONFIG_IPV6)
303def35 6710 int off;
720e7f38 6711#endif
4f738adb
JF
6712
6713 switch (si->off) {
6714 case offsetof(struct sk_msg_md, data):
6715 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data),
6716 si->dst_reg, si->src_reg,
6717 offsetof(struct sk_msg_buff, data));
6718 break;
6719 case offsetof(struct sk_msg_md, data_end):
6720 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data_end),
6721 si->dst_reg, si->src_reg,
6722 offsetof(struct sk_msg_buff, data_end));
6723 break;
303def35
JF
6724 case offsetof(struct sk_msg_md, family):
6725 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6726
6727 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6728 struct sk_msg_buff, sk),
6729 si->dst_reg, si->src_reg,
6730 offsetof(struct sk_msg_buff, sk));
6731 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6732 offsetof(struct sock_common, skc_family));
6733 break;
6734
6735 case offsetof(struct sk_msg_md, remote_ip4):
6736 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6737
6738 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6739 struct sk_msg_buff, sk),
6740 si->dst_reg, si->src_reg,
6741 offsetof(struct sk_msg_buff, sk));
6742 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6743 offsetof(struct sock_common, skc_daddr));
6744 break;
6745
6746 case offsetof(struct sk_msg_md, local_ip4):
6747 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6748 skc_rcv_saddr) != 4);
6749
6750 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6751 struct sk_msg_buff, sk),
6752 si->dst_reg, si->src_reg,
6753 offsetof(struct sk_msg_buff, sk));
6754 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6755 offsetof(struct sock_common,
6756 skc_rcv_saddr));
6757 break;
6758
6759 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
6760 offsetof(struct sk_msg_md, remote_ip6[3]):
6761#if IS_ENABLED(CONFIG_IPV6)
6762 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6763 skc_v6_daddr.s6_addr32[0]) != 4);
6764
6765 off = si->off;
6766 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
6767 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6768 struct sk_msg_buff, sk),
6769 si->dst_reg, si->src_reg,
6770 offsetof(struct sk_msg_buff, sk));
6771 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6772 offsetof(struct sock_common,
6773 skc_v6_daddr.s6_addr32[0]) +
6774 off);
6775#else
6776 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6777#endif
6778 break;
6779
6780 case offsetof(struct sk_msg_md, local_ip6[0]) ...
6781 offsetof(struct sk_msg_md, local_ip6[3]):
6782#if IS_ENABLED(CONFIG_IPV6)
6783 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6784 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6785
6786 off = si->off;
6787 off -= offsetof(struct sk_msg_md, local_ip6[0]);
6788 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6789 struct sk_msg_buff, sk),
6790 si->dst_reg, si->src_reg,
6791 offsetof(struct sk_msg_buff, sk));
6792 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6793 offsetof(struct sock_common,
6794 skc_v6_rcv_saddr.s6_addr32[0]) +
6795 off);
6796#else
6797 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6798#endif
6799 break;
6800
6801 case offsetof(struct sk_msg_md, remote_port):
6802 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6803
6804 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6805 struct sk_msg_buff, sk),
6806 si->dst_reg, si->src_reg,
6807 offsetof(struct sk_msg_buff, sk));
6808 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6809 offsetof(struct sock_common, skc_dport));
6810#ifndef __BIG_ENDIAN_BITFIELD
6811 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6812#endif
6813 break;
6814
6815 case offsetof(struct sk_msg_md, local_port):
6816 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6817
6818 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6819 struct sk_msg_buff, sk),
6820 si->dst_reg, si->src_reg,
6821 offsetof(struct sk_msg_buff, sk));
6822 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6823 offsetof(struct sock_common, skc_num));
6824 break;
4f738adb
JF
6825 }
6826
6827 return insn - insn_buf;
6828}
6829
7de16e3a 6830const struct bpf_verifier_ops sk_filter_verifier_ops = {
4936e352
DB
6831 .get_func_proto = sk_filter_func_proto,
6832 .is_valid_access = sk_filter_is_valid_access,
2492d3b8 6833 .convert_ctx_access = bpf_convert_ctx_access,
e0cea7ce 6834 .gen_ld_abs = bpf_gen_ld_abs,
89aa0758
AS
6835};
6836
7de16e3a 6837const struct bpf_prog_ops sk_filter_prog_ops = {
61f3c964 6838 .test_run = bpf_prog_test_run_skb,
7de16e3a
JK
6839};
6840
6841const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
4936e352
DB
6842 .get_func_proto = tc_cls_act_func_proto,
6843 .is_valid_access = tc_cls_act_is_valid_access,
374fb54e 6844 .convert_ctx_access = tc_cls_act_convert_ctx_access,
36bbef52 6845 .gen_prologue = tc_cls_act_prologue,
e0cea7ce 6846 .gen_ld_abs = bpf_gen_ld_abs,
7de16e3a
JK
6847};
6848
6849const struct bpf_prog_ops tc_cls_act_prog_ops = {
1cf1cae9 6850 .test_run = bpf_prog_test_run_skb,
608cd71a
AS
6851};
6852
7de16e3a 6853const struct bpf_verifier_ops xdp_verifier_ops = {
6a773a15
BB
6854 .get_func_proto = xdp_func_proto,
6855 .is_valid_access = xdp_is_valid_access,
6856 .convert_ctx_access = xdp_convert_ctx_access,
7de16e3a
JK
6857};
6858
6859const struct bpf_prog_ops xdp_prog_ops = {
1cf1cae9 6860 .test_run = bpf_prog_test_run_xdp,
6a773a15
BB
6861};
6862
7de16e3a 6863const struct bpf_verifier_ops cg_skb_verifier_ops = {
cd339431 6864 .get_func_proto = cg_skb_func_proto,
0e33661d 6865 .is_valid_access = sk_filter_is_valid_access,
2492d3b8 6866 .convert_ctx_access = bpf_convert_ctx_access,
7de16e3a
JK
6867};
6868
6869const struct bpf_prog_ops cg_skb_prog_ops = {
1cf1cae9 6870 .test_run = bpf_prog_test_run_skb,
0e33661d
DM
6871};
6872
cd3092c7
MX
6873const struct bpf_verifier_ops lwt_in_verifier_ops = {
6874 .get_func_proto = lwt_in_func_proto,
3a0af8fd 6875 .is_valid_access = lwt_is_valid_access,
2492d3b8 6876 .convert_ctx_access = bpf_convert_ctx_access,
7de16e3a
JK
6877};
6878
cd3092c7
MX
6879const struct bpf_prog_ops lwt_in_prog_ops = {
6880 .test_run = bpf_prog_test_run_skb,
6881};
6882
6883const struct bpf_verifier_ops lwt_out_verifier_ops = {
6884 .get_func_proto = lwt_out_func_proto,
3a0af8fd 6885 .is_valid_access = lwt_is_valid_access,
2492d3b8 6886 .convert_ctx_access = bpf_convert_ctx_access,
7de16e3a
JK
6887};
6888
cd3092c7 6889const struct bpf_prog_ops lwt_out_prog_ops = {
1cf1cae9 6890 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
6891};
6892
7de16e3a 6893const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
3a0af8fd
TG
6894 .get_func_proto = lwt_xmit_func_proto,
6895 .is_valid_access = lwt_is_valid_access,
2492d3b8 6896 .convert_ctx_access = bpf_convert_ctx_access,
3a0af8fd 6897 .gen_prologue = tc_cls_act_prologue,
7de16e3a
JK
6898};
6899
6900const struct bpf_prog_ops lwt_xmit_prog_ops = {
1cf1cae9 6901 .test_run = bpf_prog_test_run_skb,
3a0af8fd
TG
6902};
6903
004d4b27
MX
6904const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
6905 .get_func_proto = lwt_seg6local_func_proto,
6906 .is_valid_access = lwt_is_valid_access,
6907 .convert_ctx_access = bpf_convert_ctx_access,
6908};
6909
6910const struct bpf_prog_ops lwt_seg6local_prog_ops = {
6911 .test_run = bpf_prog_test_run_skb,
6912};
6913
7de16e3a 6914const struct bpf_verifier_ops cg_sock_verifier_ops = {
ae2cf1c4 6915 .get_func_proto = sock_filter_func_proto,
61023658
DA
6916 .is_valid_access = sock_filter_is_valid_access,
6917 .convert_ctx_access = sock_filter_convert_ctx_access,
6918};
6919
7de16e3a
JK
6920const struct bpf_prog_ops cg_sock_prog_ops = {
6921};
6922
4fbac77d
AI
6923const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
6924 .get_func_proto = sock_addr_func_proto,
6925 .is_valid_access = sock_addr_is_valid_access,
6926 .convert_ctx_access = sock_addr_convert_ctx_access,
6927};
6928
6929const struct bpf_prog_ops cg_sock_addr_prog_ops = {
6930};
6931
7de16e3a 6932const struct bpf_verifier_ops sock_ops_verifier_ops = {
8c4b4c7e 6933 .get_func_proto = sock_ops_func_proto,
40304b2a
LB
6934 .is_valid_access = sock_ops_is_valid_access,
6935 .convert_ctx_access = sock_ops_convert_ctx_access,
6936};
6937
7de16e3a
JK
6938const struct bpf_prog_ops sock_ops_prog_ops = {
6939};
6940
6941const struct bpf_verifier_ops sk_skb_verifier_ops = {
b005fd18
JF
6942 .get_func_proto = sk_skb_func_proto,
6943 .is_valid_access = sk_skb_is_valid_access,
8108a775 6944 .convert_ctx_access = sk_skb_convert_ctx_access,
8a31db56 6945 .gen_prologue = sk_skb_prologue,
b005fd18
JF
6946};
6947
7de16e3a
JK
6948const struct bpf_prog_ops sk_skb_prog_ops = {
6949};
6950
4f738adb
JF
6951const struct bpf_verifier_ops sk_msg_verifier_ops = {
6952 .get_func_proto = sk_msg_func_proto,
6953 .is_valid_access = sk_msg_is_valid_access,
6954 .convert_ctx_access = sk_msg_convert_ctx_access,
6955};
6956
6957const struct bpf_prog_ops sk_msg_prog_ops = {
6958};
6959
8ced425e 6960int sk_detach_filter(struct sock *sk)
55b33325
PE
6961{
6962 int ret = -ENOENT;
6963 struct sk_filter *filter;
6964
d59577b6
VB
6965 if (sock_flag(sk, SOCK_FILTER_LOCKED))
6966 return -EPERM;
6967
8ced425e
HFS
6968 filter = rcu_dereference_protected(sk->sk_filter,
6969 lockdep_sock_is_held(sk));
55b33325 6970 if (filter) {
a9b3cd7f 6971 RCU_INIT_POINTER(sk->sk_filter, NULL);
46bcf14f 6972 sk_filter_uncharge(sk, filter);
55b33325
PE
6973 ret = 0;
6974 }
a3ea269b 6975
55b33325
PE
6976 return ret;
6977}
8ced425e 6978EXPORT_SYMBOL_GPL(sk_detach_filter);
a8fc9277 6979
a3ea269b
DB
6980int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
6981 unsigned int len)
a8fc9277 6982{
a3ea269b 6983 struct sock_fprog_kern *fprog;
a8fc9277 6984 struct sk_filter *filter;
a3ea269b 6985 int ret = 0;
a8fc9277
PE
6986
6987 lock_sock(sk);
6988 filter = rcu_dereference_protected(sk->sk_filter,
8ced425e 6989 lockdep_sock_is_held(sk));
a8fc9277
PE
6990 if (!filter)
6991 goto out;
a3ea269b
DB
6992
6993 /* We're copying the filter that has been originally attached,
93d08b69
DB
6994 * so no conversion/decode needed anymore. eBPF programs that
6995 * have no original program cannot be dumped through this.
a3ea269b 6996 */
93d08b69 6997 ret = -EACCES;
7ae457c1 6998 fprog = filter->prog->orig_prog;
93d08b69
DB
6999 if (!fprog)
7000 goto out;
a3ea269b
DB
7001
7002 ret = fprog->len;
a8fc9277 7003 if (!len)
a3ea269b 7004 /* User space only enquires number of filter blocks. */
a8fc9277 7005 goto out;
a3ea269b 7006
a8fc9277 7007 ret = -EINVAL;
a3ea269b 7008 if (len < fprog->len)
a8fc9277
PE
7009 goto out;
7010
7011 ret = -EFAULT;
009937e7 7012 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
a3ea269b 7013 goto out;
a8fc9277 7014
a3ea269b
DB
7015 /* Instead of bytes, the API requests to return the number
7016 * of filter blocks.
7017 */
7018 ret = fprog->len;
a8fc9277
PE
7019out:
7020 release_sock(sk);
7021 return ret;
7022}