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