1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux Socket Filter - Kernel level socket filtering
4 *
5 * Based on the design of the Berkeley Packet Filter. The new
6 * internal format has been designed by PLUMgrid:
7 *
8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
9 *
10 * Authors:
11 *
12 * Jay Schulist <jschlst@samba.org>
13 * Alexei Starovoitov <ast@plumgrid.com>
14 * Daniel Borkmann <dborkman@redhat.com>
15 *
16 * Andi Kleen - Fix a few bad bugs and races.
17 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
18 */
19
20 #include <linux/atomic.h>
21 #include <linux/bpf_verifier.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/mm.h>
25 #include <linux/fcntl.h>
26 #include <linux/socket.h>
27 #include <linux/sock_diag.h>
28 #include <linux/in.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_packet.h>
32 #include <linux/if_arp.h>
33 #include <linux/gfp.h>
34 #include <net/inet_common.h>
35 #include <net/ip.h>
36 #include <net/protocol.h>
37 #include <net/netlink.h>
38 #include <linux/skbuff.h>
39 #include <linux/skmsg.h>
40 #include <net/sock.h>
41 #include <net/flow_dissector.h>
42 #include <linux/errno.h>
43 #include <linux/timer.h>
44 #include <linux/uaccess.h>
45 #include <linux/unaligned.h>
46 #include <linux/filter.h>
47 #include <linux/ratelimit.h>
48 #include <linux/seccomp.h>
49 #include <linux/if_vlan.h>
50 #include <linux/bpf.h>
51 #include <linux/btf.h>
52 #include <net/sch_generic.h>
53 #include <net/cls_cgroup.h>
54 #include <net/dst_metadata.h>
55 #include <net/dst.h>
56 #include <net/sock_reuseport.h>
57 #include <net/busy_poll.h>
58 #include <net/tcp.h>
59 #include <net/xfrm.h>
60 #include <net/udp.h>
61 #include <linux/bpf_trace.h>
62 #include <net/xdp_sock.h>
63 #include <linux/inetdevice.h>
64 #include <net/inet_hashtables.h>
65 #include <net/inet6_hashtables.h>
66 #include <net/ip_fib.h>
67 #include <net/nexthop.h>
68 #include <net/flow.h>
69 #include <net/arp.h>
70 #include <net/ipv6.h>
71 #include <net/net_namespace.h>
72 #include <linux/seg6_local.h>
73 #include <net/seg6.h>
74 #include <net/seg6_local.h>
75 #include <net/lwtunnel.h>
76 #include <net/ipv6_stubs.h>
77 #include <net/bpf_sk_storage.h>
78 #include <net/transp_v6.h>
79 #include <linux/btf_ids.h>
80 #include <net/tls.h>
81 #include <net/xdp.h>
82 #include <net/mptcp.h>
83 #include <net/netfilter/nf_conntrack_bpf.h>
84 #include <net/netkit.h>
85 #include <linux/un.h>
86 #include <net/xdp_sock_drv.h>
87 #include <net/inet_dscp.h>
88
89 #include "dev.h"
90
91 /* Keep the struct bpf_fib_lookup small so that it fits into a cacheline */
92 static_assert(sizeof(struct bpf_fib_lookup) == 64, "struct bpf_fib_lookup size check");
93
94 #include <trace/hooks/net.h>
95
96 static const struct bpf_func_proto *
97 bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
98
copy_bpf_fprog_from_user(struct sock_fprog * dst,sockptr_t src,int len)99 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
100 {
101 if (in_compat_syscall()) {
102 struct compat_sock_fprog f32;
103
104 if (len != sizeof(f32))
105 return -EINVAL;
106 if (copy_from_sockptr(&f32, src, sizeof(f32)))
107 return -EFAULT;
108 memset(dst, 0, sizeof(*dst));
109 dst->len = f32.len;
110 dst->filter = compat_ptr(f32.filter);
111 } else {
112 if (len != sizeof(*dst))
113 return -EINVAL;
114 if (copy_from_sockptr(dst, src, sizeof(*dst)))
115 return -EFAULT;
116 }
117
118 return 0;
119 }
120 EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user);
121
122 /**
123 * sk_filter_trim_cap - run a packet through a socket filter
124 * @sk: sock associated with &sk_buff
125 * @skb: buffer to filter
126 * @cap: limit on how short the eBPF program may trim the packet
127 *
128 * Run the eBPF program and then cut skb->data to correct size returned by
129 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
130 * than pkt_len we keep whole skb->data. This is the socket level
131 * wrapper to bpf_prog_run. It returns 0 if the packet should
132 * be accepted or -EPERM if the packet should be tossed.
133 *
134 */
sk_filter_trim_cap(struct sock * sk,struct sk_buff * skb,unsigned int cap)135 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
136 {
137 int err;
138 struct sk_filter *filter;
139
140 /*
141 * If the skb was allocated from pfmemalloc reserves, only
142 * allow SOCK_MEMALLOC sockets to use it as this socket is
143 * helping free memory
144 */
145 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
146 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
147 return -ENOMEM;
148 }
149 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
150 if (err)
151 return err;
152
153 err = security_sock_rcv_skb(sk, skb);
154 if (err)
155 return err;
156
157 rcu_read_lock();
158 filter = rcu_dereference(sk->sk_filter);
159 if (filter) {
160 struct sock *save_sk = skb->sk;
161 unsigned int pkt_len;
162
163 skb->sk = sk;
164 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
165 skb->sk = save_sk;
166 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
167 }
168 rcu_read_unlock();
169
170 return err;
171 }
172 EXPORT_SYMBOL(sk_filter_trim_cap);
173
BPF_CALL_1(bpf_skb_get_pay_offset,struct sk_buff *,skb)174 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
175 {
176 return skb_get_poff(skb);
177 }
178
BPF_CALL_3(bpf_skb_get_nlattr,struct sk_buff *,skb,u32,a,u32,x)179 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
180 {
181 struct nlattr *nla;
182
183 if (skb_is_nonlinear(skb))
184 return 0;
185
186 if (skb->len < sizeof(struct nlattr))
187 return 0;
188
189 if (a > skb->len - sizeof(struct nlattr))
190 return 0;
191
192 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
193 if (nla)
194 return (void *) nla - (void *) skb->data;
195
196 return 0;
197 }
198
BPF_CALL_3(bpf_skb_get_nlattr_nest,struct sk_buff *,skb,u32,a,u32,x)199 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
200 {
201 struct nlattr *nla;
202
203 if (skb_is_nonlinear(skb))
204 return 0;
205
206 if (skb->len < sizeof(struct nlattr))
207 return 0;
208
209 if (a > skb->len - sizeof(struct nlattr))
210 return 0;
211
212 nla = (struct nlattr *) &skb->data[a];
213 if (!nla_ok(nla, skb->len - a))
214 return 0;
215
216 nla = nla_find_nested(nla, x);
217 if (nla)
218 return (void *) nla - (void *) skb->data;
219
220 return 0;
221 }
222
bpf_skb_load_helper_convert_offset(const struct sk_buff * skb,int offset)223 static int bpf_skb_load_helper_convert_offset(const struct sk_buff *skb, int offset)
224 {
225 if (likely(offset >= 0))
226 return offset;
227
228 if (offset >= SKF_NET_OFF)
229 return offset - SKF_NET_OFF + skb_network_offset(skb);
230
231 if (offset >= SKF_LL_OFF && skb_mac_header_was_set(skb))
232 return offset - SKF_LL_OFF + skb_mac_offset(skb);
233
234 return INT_MIN;
235 }
236
BPF_CALL_4(bpf_skb_load_helper_8,const struct sk_buff *,skb,const void *,data,int,headlen,int,offset)237 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
238 data, int, headlen, int, offset)
239 {
240 u8 tmp;
241 const int len = sizeof(tmp);
242
243 offset = bpf_skb_load_helper_convert_offset(skb, offset);
244 if (offset == INT_MIN)
245 return -EFAULT;
246
247 if (headlen - offset >= len)
248 return *(u8 *)(data + offset);
249 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
250 return tmp;
251 else
252 return -EFAULT;
253 }
254
BPF_CALL_2(bpf_skb_load_helper_8_no_cache,const struct sk_buff *,skb,int,offset)255 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
256 int, offset)
257 {
258 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
259 offset);
260 }
261
BPF_CALL_4(bpf_skb_load_helper_16,const struct sk_buff *,skb,const void *,data,int,headlen,int,offset)262 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
263 data, int, headlen, int, offset)
264 {
265 __be16 tmp;
266 const int len = sizeof(tmp);
267
268 offset = bpf_skb_load_helper_convert_offset(skb, offset);
269 if (offset == INT_MIN)
270 return -EFAULT;
271
272 if (headlen - offset >= len)
273 return get_unaligned_be16(data + offset);
274 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
275 return be16_to_cpu(tmp);
276 else
277 return -EFAULT;
278 }
279
BPF_CALL_2(bpf_skb_load_helper_16_no_cache,const struct sk_buff *,skb,int,offset)280 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
281 int, offset)
282 {
283 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
284 offset);
285 }
286
BPF_CALL_4(bpf_skb_load_helper_32,const struct sk_buff *,skb,const void *,data,int,headlen,int,offset)287 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
288 data, int, headlen, int, offset)
289 {
290 __be32 tmp;
291 const int len = sizeof(tmp);
292
293 offset = bpf_skb_load_helper_convert_offset(skb, offset);
294 if (offset == INT_MIN)
295 return -EFAULT;
296
297 if (headlen - offset >= len)
298 return get_unaligned_be32(data + offset);
299 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
300 return be32_to_cpu(tmp);
301 else
302 return -EFAULT;
303 }
304
BPF_CALL_2(bpf_skb_load_helper_32_no_cache,const struct sk_buff *,skb,int,offset)305 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
306 int, offset)
307 {
308 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
309 offset);
310 }
311
convert_skb_access(int skb_field,int dst_reg,int src_reg,struct bpf_insn * insn_buf)312 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
313 struct bpf_insn *insn_buf)
314 {
315 struct bpf_insn *insn = insn_buf;
316
317 switch (skb_field) {
318 case SKF_AD_MARK:
319 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
320
321 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
322 offsetof(struct sk_buff, mark));
323 break;
324
325 case SKF_AD_PKTTYPE:
326 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET);
327 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
328 #ifdef __BIG_ENDIAN_BITFIELD
329 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
330 #endif
331 break;
332
333 case SKF_AD_QUEUE:
334 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2);
335
336 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
337 offsetof(struct sk_buff, queue_mapping));
338 break;
339
340 case SKF_AD_VLAN_TAG:
341 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
342
343 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
344 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
345 offsetof(struct sk_buff, vlan_tci));
346 break;
347 case SKF_AD_VLAN_TAG_PRESENT:
348 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_all) != 4);
349 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
350 offsetof(struct sk_buff, vlan_all));
351 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
352 *insn++ = BPF_ALU32_IMM(BPF_MOV, dst_reg, 1);
353 break;
354 }
355
356 return insn - insn_buf;
357 }
358
convert_bpf_extensions(struct sock_filter * fp,struct bpf_insn ** insnp)359 static bool convert_bpf_extensions(struct sock_filter *fp,
360 struct bpf_insn **insnp)
361 {
362 struct bpf_insn *insn = *insnp;
363 u32 cnt;
364
365 switch (fp->k) {
366 case SKF_AD_OFF + SKF_AD_PROTOCOL:
367 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2);
368
369 /* A = *(u16 *) (CTX + offsetof(protocol)) */
370 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
371 offsetof(struct sk_buff, protocol));
372 /* A = ntohs(A) [emitting a nop or swap16] */
373 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
374 break;
375
376 case SKF_AD_OFF + SKF_AD_PKTTYPE:
377 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
378 insn += cnt - 1;
379 break;
380
381 case SKF_AD_OFF + SKF_AD_IFINDEX:
382 case SKF_AD_OFF + SKF_AD_HATYPE:
383 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4);
384 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2);
385
386 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
387 BPF_REG_TMP, BPF_REG_CTX,
388 offsetof(struct sk_buff, dev));
389 /* if (tmp != 0) goto pc + 1 */
390 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
391 *insn++ = BPF_EXIT_INSN();
392 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
393 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
394 offsetof(struct net_device, ifindex));
395 else
396 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
397 offsetof(struct net_device, type));
398 break;
399
400 case SKF_AD_OFF + SKF_AD_MARK:
401 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
402 insn += cnt - 1;
403 break;
404
405 case SKF_AD_OFF + SKF_AD_RXHASH:
406 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
407
408 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
409 offsetof(struct sk_buff, hash));
410 break;
411
412 case SKF_AD_OFF + SKF_AD_QUEUE:
413 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
414 insn += cnt - 1;
415 break;
416
417 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
418 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
419 BPF_REG_A, BPF_REG_CTX, insn);
420 insn += cnt - 1;
421 break;
422
423 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
424 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
425 BPF_REG_A, BPF_REG_CTX, insn);
426 insn += cnt - 1;
427 break;
428
429 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
430 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2);
431
432 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
433 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
434 offsetof(struct sk_buff, vlan_proto));
435 /* A = ntohs(A) [emitting a nop or swap16] */
436 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
437 break;
438
439 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
440 case SKF_AD_OFF + SKF_AD_NLATTR:
441 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
442 case SKF_AD_OFF + SKF_AD_CPU:
443 case SKF_AD_OFF + SKF_AD_RANDOM:
444 /* arg1 = CTX */
445 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
446 /* arg2 = A */
447 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
448 /* arg3 = X */
449 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
450 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
451 switch (fp->k) {
452 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
453 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
454 break;
455 case SKF_AD_OFF + SKF_AD_NLATTR:
456 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
457 break;
458 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
459 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
460 break;
461 case SKF_AD_OFF + SKF_AD_CPU:
462 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
463 break;
464 case SKF_AD_OFF + SKF_AD_RANDOM:
465 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
466 bpf_user_rnd_init_once();
467 break;
468 }
469 break;
470
471 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
472 /* A ^= X */
473 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
474 break;
475
476 default:
477 /* This is just a dummy call to avoid letting the compiler
478 * evict __bpf_call_base() as an optimization. Placed here
479 * where no-one bothers.
480 */
481 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
482 return false;
483 }
484
485 *insnp = insn;
486 return true;
487 }
488
convert_bpf_ld_abs(struct sock_filter * fp,struct bpf_insn ** insnp)489 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
490 {
491 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
492 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
493 bool endian = BPF_SIZE(fp->code) == BPF_H ||
494 BPF_SIZE(fp->code) == BPF_W;
495 bool indirect = BPF_MODE(fp->code) == BPF_IND;
496 const int ip_align = NET_IP_ALIGN;
497 struct bpf_insn *insn = *insnp;
498 int offset = fp->k;
499
500 if (!indirect &&
501 ((unaligned_ok && offset >= 0) ||
502 (!unaligned_ok && offset >= 0 &&
503 offset + ip_align >= 0 &&
504 offset + ip_align % size == 0))) {
505 bool ldx_off_ok = offset <= S16_MAX;
506
507 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
508 if (offset)
509 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
510 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
511 size, 2 + endian + (!ldx_off_ok * 2));
512 if (ldx_off_ok) {
513 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
514 BPF_REG_D, offset);
515 } else {
516 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
517 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
518 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
519 BPF_REG_TMP, 0);
520 }
521 if (endian)
522 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
523 *insn++ = BPF_JMP_A(8);
524 }
525
526 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
527 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
528 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
529 if (!indirect) {
530 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
531 } else {
532 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
533 if (fp->k)
534 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
535 }
536
537 switch (BPF_SIZE(fp->code)) {
538 case BPF_B:
539 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
540 break;
541 case BPF_H:
542 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
543 break;
544 case BPF_W:
545 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
546 break;
547 default:
548 return false;
549 }
550
551 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
552 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
553 *insn = BPF_EXIT_INSN();
554
555 *insnp = insn;
556 return true;
557 }
558
559 /**
560 * bpf_convert_filter - convert filter program
561 * @prog: the user passed filter program
562 * @len: the length of the user passed filter program
563 * @new_prog: allocated 'struct bpf_prog' or NULL
564 * @new_len: pointer to store length of converted program
565 * @seen_ld_abs: bool whether we've seen ld_abs/ind
566 *
567 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
568 * style extended BPF (eBPF).
569 * Conversion workflow:
570 *
571 * 1) First pass for calculating the new program length:
572 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
573 *
574 * 2) 2nd pass to remap in two passes: 1st pass finds new
575 * jump offsets, 2nd pass remapping:
576 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
577 */
bpf_convert_filter(struct sock_filter * prog,int len,struct bpf_prog * new_prog,int * new_len,bool * seen_ld_abs)578 static int bpf_convert_filter(struct sock_filter *prog, int len,
579 struct bpf_prog *new_prog, int *new_len,
580 bool *seen_ld_abs)
581 {
582 int new_flen = 0, pass = 0, target, i, stack_off;
583 struct bpf_insn *new_insn, *first_insn = NULL;
584 struct sock_filter *fp;
585 int *addrs = NULL;
586 u8 bpf_src;
587
588 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
589 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
590
591 if (len <= 0 || len > BPF_MAXINSNS)
592 return -EINVAL;
593
594 if (new_prog) {
595 first_insn = new_prog->insnsi;
596 addrs = kcalloc(len, sizeof(*addrs),
597 GFP_KERNEL | __GFP_NOWARN);
598 if (!addrs)
599 return -ENOMEM;
600 }
601
602 do_pass:
603 new_insn = first_insn;
604 fp = prog;
605
606 /* Classic BPF related prologue emission. */
607 if (new_prog) {
608 /* Classic BPF expects A and X to be reset first. These need
609 * to be guaranteed to be the first two instructions.
610 */
611 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
612 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
613
614 /* All programs must keep CTX in callee saved BPF_REG_CTX.
615 * In eBPF case it's done by the compiler, here we need to
616 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
617 */
618 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
619 if (*seen_ld_abs) {
620 /* For packet access in classic BPF, cache skb->data
621 * in callee-saved BPF R8 and skb->len - skb->data_len
622 * (headlen) in BPF R9. Since classic BPF is read-only
623 * on CTX, we only need to cache it once.
624 */
625 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
626 BPF_REG_D, BPF_REG_CTX,
627 offsetof(struct sk_buff, data));
628 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
629 offsetof(struct sk_buff, len));
630 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
631 offsetof(struct sk_buff, data_len));
632 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
633 }
634 } else {
635 new_insn += 3;
636 }
637
638 for (i = 0; i < len; fp++, i++) {
639 struct bpf_insn tmp_insns[32] = { };
640 struct bpf_insn *insn = tmp_insns;
641
642 if (addrs)
643 addrs[i] = new_insn - first_insn;
644
645 switch (fp->code) {
646 /* All arithmetic insns and skb loads map as-is. */
647 case BPF_ALU | BPF_ADD | BPF_X:
648 case BPF_ALU | BPF_ADD | BPF_K:
649 case BPF_ALU | BPF_SUB | BPF_X:
650 case BPF_ALU | BPF_SUB | BPF_K:
651 case BPF_ALU | BPF_AND | BPF_X:
652 case BPF_ALU | BPF_AND | BPF_K:
653 case BPF_ALU | BPF_OR | BPF_X:
654 case BPF_ALU | BPF_OR | BPF_K:
655 case BPF_ALU | BPF_LSH | BPF_X:
656 case BPF_ALU | BPF_LSH | BPF_K:
657 case BPF_ALU | BPF_RSH | BPF_X:
658 case BPF_ALU | BPF_RSH | BPF_K:
659 case BPF_ALU | BPF_XOR | BPF_X:
660 case BPF_ALU | BPF_XOR | BPF_K:
661 case BPF_ALU | BPF_MUL | BPF_X:
662 case BPF_ALU | BPF_MUL | BPF_K:
663 case BPF_ALU | BPF_DIV | BPF_X:
664 case BPF_ALU | BPF_DIV | BPF_K:
665 case BPF_ALU | BPF_MOD | BPF_X:
666 case BPF_ALU | BPF_MOD | BPF_K:
667 case BPF_ALU | BPF_NEG:
668 case BPF_LD | BPF_ABS | BPF_W:
669 case BPF_LD | BPF_ABS | BPF_H:
670 case BPF_LD | BPF_ABS | BPF_B:
671 case BPF_LD | BPF_IND | BPF_W:
672 case BPF_LD | BPF_IND | BPF_H:
673 case BPF_LD | BPF_IND | BPF_B:
674 /* Check for overloaded BPF extension and
675 * directly convert it if found, otherwise
676 * just move on with mapping.
677 */
678 if (BPF_CLASS(fp->code) == BPF_LD &&
679 BPF_MODE(fp->code) == BPF_ABS &&
680 convert_bpf_extensions(fp, &insn))
681 break;
682 if (BPF_CLASS(fp->code) == BPF_LD &&
683 convert_bpf_ld_abs(fp, &insn)) {
684 *seen_ld_abs = true;
685 break;
686 }
687
688 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
689 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
690 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
691 /* Error with exception code on div/mod by 0.
692 * For cBPF programs, this was always return 0.
693 */
694 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
695 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
696 *insn++ = BPF_EXIT_INSN();
697 }
698
699 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
700 break;
701
702 /* Jump transformation cannot use BPF block macros
703 * everywhere as offset calculation and target updates
704 * require a bit more work than the rest, i.e. jump
705 * opcodes map as-is, but offsets need adjustment.
706 */
707
708 #define BPF_EMIT_JMP \
709 do { \
710 const s32 off_min = S16_MIN, off_max = S16_MAX; \
711 s32 off; \
712 \
713 if (target >= len || target < 0) \
714 goto err; \
715 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
716 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
717 off -= insn - tmp_insns; \
718 /* Reject anything not fitting into insn->off. */ \
719 if (off < off_min || off > off_max) \
720 goto err; \
721 insn->off = off; \
722 } while (0)
723
724 case BPF_JMP | BPF_JA:
725 target = i + fp->k + 1;
726 insn->code = fp->code;
727 BPF_EMIT_JMP;
728 break;
729
730 case BPF_JMP | BPF_JEQ | BPF_K:
731 case BPF_JMP | BPF_JEQ | BPF_X:
732 case BPF_JMP | BPF_JSET | BPF_K:
733 case BPF_JMP | BPF_JSET | BPF_X:
734 case BPF_JMP | BPF_JGT | BPF_K:
735 case BPF_JMP | BPF_JGT | BPF_X:
736 case BPF_JMP | BPF_JGE | BPF_K:
737 case BPF_JMP | BPF_JGE | BPF_X:
738 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
739 /* BPF immediates are signed, zero extend
740 * immediate into tmp register and use it
741 * in compare insn.
742 */
743 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
744
745 insn->dst_reg = BPF_REG_A;
746 insn->src_reg = BPF_REG_TMP;
747 bpf_src = BPF_X;
748 } else {
749 insn->dst_reg = BPF_REG_A;
750 insn->imm = fp->k;
751 bpf_src = BPF_SRC(fp->code);
752 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
753 }
754
755 /* Common case where 'jump_false' is next insn. */
756 if (fp->jf == 0) {
757 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
758 target = i + fp->jt + 1;
759 BPF_EMIT_JMP;
760 break;
761 }
762
763 /* Convert some jumps when 'jump_true' is next insn. */
764 if (fp->jt == 0) {
765 switch (BPF_OP(fp->code)) {
766 case BPF_JEQ:
767 insn->code = BPF_JMP | BPF_JNE | bpf_src;
768 break;
769 case BPF_JGT:
770 insn->code = BPF_JMP | BPF_JLE | bpf_src;
771 break;
772 case BPF_JGE:
773 insn->code = BPF_JMP | BPF_JLT | bpf_src;
774 break;
775 default:
776 goto jmp_rest;
777 }
778
779 target = i + fp->jf + 1;
780 BPF_EMIT_JMP;
781 break;
782 }
783 jmp_rest:
784 /* Other jumps are mapped into two insns: Jxx and JA. */
785 target = i + fp->jt + 1;
786 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
787 BPF_EMIT_JMP;
788 insn++;
789
790 insn->code = BPF_JMP | BPF_JA;
791 target = i + fp->jf + 1;
792 BPF_EMIT_JMP;
793 break;
794
795 /* ldxb 4 * ([14] & 0xf) is remapped into 6 insns. */
796 case BPF_LDX | BPF_MSH | BPF_B: {
797 struct sock_filter tmp = {
798 .code = BPF_LD | BPF_ABS | BPF_B,
799 .k = fp->k,
800 };
801
802 *seen_ld_abs = true;
803
804 /* X = A */
805 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
806 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
807 convert_bpf_ld_abs(&tmp, &insn);
808 insn++;
809 /* A &= 0xf */
810 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
811 /* A <<= 2 */
812 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
813 /* tmp = X */
814 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
815 /* X = A */
816 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
817 /* A = tmp */
818 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
819 break;
820 }
821 /* RET_K is remapped into 2 insns. RET_A case doesn't need an
822 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
823 */
824 case BPF_RET | BPF_A:
825 case BPF_RET | BPF_K:
826 if (BPF_RVAL(fp->code) == BPF_K)
827 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
828 0, fp->k);
829 *insn = BPF_EXIT_INSN();
830 break;
831
832 /* Store to stack. */
833 case BPF_ST:
834 case BPF_STX:
835 stack_off = fp->k * 4 + 4;
836 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
837 BPF_ST ? BPF_REG_A : BPF_REG_X,
838 -stack_off);
839 /* check_load_and_stores() verifies that classic BPF can
840 * load from stack only after write, so tracking
841 * stack_depth for ST|STX insns is enough
842 */
843 if (new_prog && new_prog->aux->stack_depth < stack_off)
844 new_prog->aux->stack_depth = stack_off;
845 break;
846
847 /* Load from stack. */
848 case BPF_LD | BPF_MEM:
849 case BPF_LDX | BPF_MEM:
850 stack_off = fp->k * 4 + 4;
851 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
852 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
853 -stack_off);
854 break;
855
856 /* A = K or X = K */
857 case BPF_LD | BPF_IMM:
858 case BPF_LDX | BPF_IMM:
859 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
860 BPF_REG_A : BPF_REG_X, fp->k);
861 break;
862
863 /* X = A */
864 case BPF_MISC | BPF_TAX:
865 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
866 break;
867
868 /* A = X */
869 case BPF_MISC | BPF_TXA:
870 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
871 break;
872
873 /* A = skb->len or X = skb->len */
874 case BPF_LD | BPF_W | BPF_LEN:
875 case BPF_LDX | BPF_W | BPF_LEN:
876 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
877 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
878 offsetof(struct sk_buff, len));
879 break;
880
881 /* Access seccomp_data fields. */
882 case BPF_LDX | BPF_ABS | BPF_W:
883 /* A = *(u32 *) (ctx + K) */
884 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
885 break;
886
887 /* Unknown instruction. */
888 default:
889 goto err;
890 }
891
892 insn++;
893 if (new_prog)
894 memcpy(new_insn, tmp_insns,
895 sizeof(*insn) * (insn - tmp_insns));
896 new_insn += insn - tmp_insns;
897 }
898
899 if (!new_prog) {
900 /* Only calculating new length. */
901 *new_len = new_insn - first_insn;
902 if (*seen_ld_abs)
903 *new_len += 4; /* Prologue bits. */
904 return 0;
905 }
906
907 pass++;
908 if (new_flen != new_insn - first_insn) {
909 new_flen = new_insn - first_insn;
910 if (pass > 2)
911 goto err;
912 goto do_pass;
913 }
914
915 kfree(addrs);
916 BUG_ON(*new_len != new_flen);
917 return 0;
918 err:
919 kfree(addrs);
920 return -EINVAL;
921 }
922
923 /* Security:
924 *
925 * As we dont want to clear mem[] array for each packet going through
926 * __bpf_prog_run(), we check that filter loaded by user never try to read
927 * a cell if not previously written, and we check all branches to be sure
928 * a malicious user doesn't try to abuse us.
929 */
check_load_and_stores(const struct sock_filter * filter,int flen)930 static int check_load_and_stores(const struct sock_filter *filter, int flen)
931 {
932 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
933 int pc, ret = 0;
934
935 BUILD_BUG_ON(BPF_MEMWORDS > 16);
936
937 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
938 if (!masks)
939 return -ENOMEM;
940
941 memset(masks, 0xff, flen * sizeof(*masks));
942
943 for (pc = 0; pc < flen; pc++) {
944 memvalid &= masks[pc];
945
946 switch (filter[pc].code) {
947 case BPF_ST:
948 case BPF_STX:
949 memvalid |= (1 << filter[pc].k);
950 break;
951 case BPF_LD | BPF_MEM:
952 case BPF_LDX | BPF_MEM:
953 if (!(memvalid & (1 << filter[pc].k))) {
954 ret = -EINVAL;
955 goto error;
956 }
957 break;
958 case BPF_JMP | BPF_JA:
959 /* A jump must set masks on target */
960 masks[pc + 1 + filter[pc].k] &= memvalid;
961 memvalid = ~0;
962 break;
963 case BPF_JMP | BPF_JEQ | BPF_K:
964 case BPF_JMP | BPF_JEQ | BPF_X:
965 case BPF_JMP | BPF_JGE | BPF_K:
966 case BPF_JMP | BPF_JGE | BPF_X:
967 case BPF_JMP | BPF_JGT | BPF_K:
968 case BPF_JMP | BPF_JGT | BPF_X:
969 case BPF_JMP | BPF_JSET | BPF_K:
970 case BPF_JMP | BPF_JSET | BPF_X:
971 /* A jump must set masks on targets */
972 masks[pc + 1 + filter[pc].jt] &= memvalid;
973 masks[pc + 1 + filter[pc].jf] &= memvalid;
974 memvalid = ~0;
975 break;
976 }
977 }
978 error:
979 kfree(masks);
980 return ret;
981 }
982
chk_code_allowed(u16 code_to_probe)983 static bool chk_code_allowed(u16 code_to_probe)
984 {
985 static const bool codes[] = {
986 /* 32 bit ALU operations */
987 [BPF_ALU | BPF_ADD | BPF_K] = true,
988 [BPF_ALU | BPF_ADD | BPF_X] = true,
989 [BPF_ALU | BPF_SUB | BPF_K] = true,
990 [BPF_ALU | BPF_SUB | BPF_X] = true,
991 [BPF_ALU | BPF_MUL | BPF_K] = true,
992 [BPF_ALU | BPF_MUL | BPF_X] = true,
993 [BPF_ALU | BPF_DIV | BPF_K] = true,
994 [BPF_ALU | BPF_DIV | BPF_X] = true,
995 [BPF_ALU | BPF_MOD | BPF_K] = true,
996 [BPF_ALU | BPF_MOD | BPF_X] = true,
997 [BPF_ALU | BPF_AND | BPF_K] = true,
998 [BPF_ALU | BPF_AND | BPF_X] = true,
999 [BPF_ALU | BPF_OR | BPF_K] = true,
1000 [BPF_ALU | BPF_OR | BPF_X] = true,
1001 [BPF_ALU | BPF_XOR | BPF_K] = true,
1002 [BPF_ALU | BPF_XOR | BPF_X] = true,
1003 [BPF_ALU | BPF_LSH | BPF_K] = true,
1004 [BPF_ALU | BPF_LSH | BPF_X] = true,
1005 [BPF_ALU | BPF_RSH | BPF_K] = true,
1006 [BPF_ALU | BPF_RSH | BPF_X] = true,
1007 [BPF_ALU | BPF_NEG] = true,
1008 /* Load instructions */
1009 [BPF_LD | BPF_W | BPF_ABS] = true,
1010 [BPF_LD | BPF_H | BPF_ABS] = true,
1011 [BPF_LD | BPF_B | BPF_ABS] = true,
1012 [BPF_LD | BPF_W | BPF_LEN] = true,
1013 [BPF_LD | BPF_W | BPF_IND] = true,
1014 [BPF_LD | BPF_H | BPF_IND] = true,
1015 [BPF_LD | BPF_B | BPF_IND] = true,
1016 [BPF_LD | BPF_IMM] = true,
1017 [BPF_LD | BPF_MEM] = true,
1018 [BPF_LDX | BPF_W | BPF_LEN] = true,
1019 [BPF_LDX | BPF_B | BPF_MSH] = true,
1020 [BPF_LDX | BPF_IMM] = true,
1021 [BPF_LDX | BPF_MEM] = true,
1022 /* Store instructions */
1023 [BPF_ST] = true,
1024 [BPF_STX] = true,
1025 /* Misc instructions */
1026 [BPF_MISC | BPF_TAX] = true,
1027 [BPF_MISC | BPF_TXA] = true,
1028 /* Return instructions */
1029 [BPF_RET | BPF_K] = true,
1030 [BPF_RET | BPF_A] = true,
1031 /* Jump instructions */
1032 [BPF_JMP | BPF_JA] = true,
1033 [BPF_JMP | BPF_JEQ | BPF_K] = true,
1034 [BPF_JMP | BPF_JEQ | BPF_X] = true,
1035 [BPF_JMP | BPF_JGE | BPF_K] = true,
1036 [BPF_JMP | BPF_JGE | BPF_X] = true,
1037 [BPF_JMP | BPF_JGT | BPF_K] = true,
1038 [BPF_JMP | BPF_JGT | BPF_X] = true,
1039 [BPF_JMP | BPF_JSET | BPF_K] = true,
1040 [BPF_JMP | BPF_JSET | BPF_X] = true,
1041 };
1042
1043 if (code_to_probe >= ARRAY_SIZE(codes))
1044 return false;
1045
1046 return codes[code_to_probe];
1047 }
1048
bpf_check_basics_ok(const struct sock_filter * filter,unsigned int flen)1049 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1050 unsigned int flen)
1051 {
1052 if (filter == NULL)
1053 return false;
1054 if (flen == 0 || flen > BPF_MAXINSNS)
1055 return false;
1056
1057 return true;
1058 }
1059
1060 /**
1061 * bpf_check_classic - verify socket filter code
1062 * @filter: filter to verify
1063 * @flen: length of filter
1064 *
1065 * Check the user's filter code. If we let some ugly
1066 * filter code slip through kaboom! The filter must contain
1067 * no references or jumps that are out of range, no illegal
1068 * instructions, and must end with a RET instruction.
1069 *
1070 * All jumps are forward as they are not signed.
1071 *
1072 * Returns 0 if the rule set is legal or -EINVAL if not.
1073 */
bpf_check_classic(const struct sock_filter * filter,unsigned int flen)1074 static int bpf_check_classic(const struct sock_filter *filter,
1075 unsigned int flen)
1076 {
1077 bool anc_found;
1078 int pc;
1079
1080 /* Check the filter code now */
1081 for (pc = 0; pc < flen; pc++) {
1082 const struct sock_filter *ftest = &filter[pc];
1083
1084 /* May we actually operate on this code? */
1085 if (!chk_code_allowed(ftest->code))
1086 return -EINVAL;
1087
1088 /* Some instructions need special checks */
1089 switch (ftest->code) {
1090 case BPF_ALU | BPF_DIV | BPF_K:
1091 case BPF_ALU | BPF_MOD | BPF_K:
1092 /* Check for division by zero */
1093 if (ftest->k == 0)
1094 return -EINVAL;
1095 break;
1096 case BPF_ALU | BPF_LSH | BPF_K:
1097 case BPF_ALU | BPF_RSH | BPF_K:
1098 if (ftest->k >= 32)
1099 return -EINVAL;
1100 break;
1101 case BPF_LD | BPF_MEM:
1102 case BPF_LDX | BPF_MEM:
1103 case BPF_ST:
1104 case BPF_STX:
1105 /* Check for invalid memory addresses */
1106 if (ftest->k >= BPF_MEMWORDS)
1107 return -EINVAL;
1108 break;
1109 case BPF_JMP | BPF_JA:
1110 /* Note, the large ftest->k might cause loops.
1111 * Compare this with conditional jumps below,
1112 * where offsets are limited. --ANK (981016)
1113 */
1114 if (ftest->k >= (unsigned int)(flen - pc - 1))
1115 return -EINVAL;
1116 break;
1117 case BPF_JMP | BPF_JEQ | BPF_K:
1118 case BPF_JMP | BPF_JEQ | BPF_X:
1119 case BPF_JMP | BPF_JGE | BPF_K:
1120 case BPF_JMP | BPF_JGE | BPF_X:
1121 case BPF_JMP | BPF_JGT | BPF_K:
1122 case BPF_JMP | BPF_JGT | BPF_X:
1123 case BPF_JMP | BPF_JSET | BPF_K:
1124 case BPF_JMP | BPF_JSET | BPF_X:
1125 /* Both conditionals must be safe */
1126 if (pc + ftest->jt + 1 >= flen ||
1127 pc + ftest->jf + 1 >= flen)
1128 return -EINVAL;
1129 break;
1130 case BPF_LD | BPF_W | BPF_ABS:
1131 case BPF_LD | BPF_H | BPF_ABS:
1132 case BPF_LD | BPF_B | BPF_ABS:
1133 anc_found = false;
1134 if (bpf_anc_helper(ftest) & BPF_ANC)
1135 anc_found = true;
1136 /* Ancillary operation unknown or unsupported */
1137 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1138 return -EINVAL;
1139 }
1140 }
1141
1142 /* Last instruction must be a RET code */
1143 switch (filter[flen - 1].code) {
1144 case BPF_RET | BPF_K:
1145 case BPF_RET | BPF_A:
1146 return check_load_and_stores(filter, flen);
1147 }
1148
1149 return -EINVAL;
1150 }
1151
bpf_prog_store_orig_filter(struct bpf_prog * fp,const struct sock_fprog * fprog)1152 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1153 const struct sock_fprog *fprog)
1154 {
1155 unsigned int fsize = bpf_classic_proglen(fprog);
1156 struct sock_fprog_kern *fkprog;
1157
1158 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1159 if (!fp->orig_prog)
1160 return -ENOMEM;
1161
1162 fkprog = fp->orig_prog;
1163 fkprog->len = fprog->len;
1164
1165 fkprog->filter = kmemdup(fp->insns, fsize,
1166 GFP_KERNEL | __GFP_NOWARN);
1167 if (!fkprog->filter) {
1168 kfree(fp->orig_prog);
1169 return -ENOMEM;
1170 }
1171
1172 return 0;
1173 }
1174
bpf_release_orig_filter(struct bpf_prog * fp)1175 static void bpf_release_orig_filter(struct bpf_prog *fp)
1176 {
1177 struct sock_fprog_kern *fprog = fp->orig_prog;
1178
1179 if (fprog) {
1180 kfree(fprog->filter);
1181 kfree(fprog);
1182 }
1183 }
1184
__bpf_prog_release(struct bpf_prog * prog)1185 static void __bpf_prog_release(struct bpf_prog *prog)
1186 {
1187 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1188 bpf_prog_put(prog);
1189 } else {
1190 bpf_release_orig_filter(prog);
1191 bpf_prog_free(prog);
1192 }
1193 }
1194
__sk_filter_release(struct sk_filter * fp)1195 static void __sk_filter_release(struct sk_filter *fp)
1196 {
1197 __bpf_prog_release(fp->prog);
1198 kfree(fp);
1199 }
1200
1201 /**
1202 * sk_filter_release_rcu - Release a socket filter by rcu_head
1203 * @rcu: rcu_head that contains the sk_filter to free
1204 */
sk_filter_release_rcu(struct rcu_head * rcu)1205 static void sk_filter_release_rcu(struct rcu_head *rcu)
1206 {
1207 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1208
1209 __sk_filter_release(fp);
1210 }
1211
1212 /**
1213 * sk_filter_release - release a socket filter
1214 * @fp: filter to remove
1215 *
1216 * Remove a filter from a socket and release its resources.
1217 */
sk_filter_release(struct sk_filter * fp)1218 static void sk_filter_release(struct sk_filter *fp)
1219 {
1220 if (refcount_dec_and_test(&fp->refcnt))
1221 call_rcu(&fp->rcu, sk_filter_release_rcu);
1222 }
1223
sk_filter_uncharge(struct sock * sk,struct sk_filter * fp)1224 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1225 {
1226 u32 filter_size = bpf_prog_size(fp->prog->len);
1227
1228 atomic_sub(filter_size, &sk->sk_omem_alloc);
1229 sk_filter_release(fp);
1230 }
1231
1232 /* try to charge the socket memory if there is space available
1233 * return true on success
1234 */
__sk_filter_charge(struct sock * sk,struct sk_filter * fp)1235 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1236 {
1237 int optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
1238 u32 filter_size = bpf_prog_size(fp->prog->len);
1239
1240 /* same check as in sock_kmalloc() */
1241 if (filter_size <= optmem_max &&
1242 atomic_read(&sk->sk_omem_alloc) + filter_size < optmem_max) {
1243 atomic_add(filter_size, &sk->sk_omem_alloc);
1244 return true;
1245 }
1246 return false;
1247 }
1248
sk_filter_charge(struct sock * sk,struct sk_filter * fp)1249 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1250 {
1251 if (!refcount_inc_not_zero(&fp->refcnt))
1252 return false;
1253
1254 if (!__sk_filter_charge(sk, fp)) {
1255 sk_filter_release(fp);
1256 return false;
1257 }
1258 return true;
1259 }
1260
bpf_migrate_filter(struct bpf_prog * fp)1261 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1262 {
1263 struct sock_filter *old_prog;
1264 struct bpf_prog *old_fp;
1265 int err, new_len, old_len = fp->len;
1266 bool seen_ld_abs = false;
1267
1268 /* We are free to overwrite insns et al right here as it won't be used at
1269 * this point in time anymore internally after the migration to the eBPF
1270 * instruction representation.
1271 */
1272 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1273 sizeof(struct bpf_insn));
1274
1275 /* Conversion cannot happen on overlapping memory areas,
1276 * so we need to keep the user BPF around until the 2nd
1277 * pass. At this time, the user BPF is stored in fp->insns.
1278 */
1279 old_prog = kmemdup_array(fp->insns, old_len, sizeof(struct sock_filter),
1280 GFP_KERNEL | __GFP_NOWARN);
1281 if (!old_prog) {
1282 err = -ENOMEM;
1283 goto out_err;
1284 }
1285
1286 /* 1st pass: calculate the new program length. */
1287 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1288 &seen_ld_abs);
1289 if (err)
1290 goto out_err_free;
1291
1292 /* Expand fp for appending the new filter representation. */
1293 old_fp = fp;
1294 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1295 if (!fp) {
1296 /* The old_fp is still around in case we couldn't
1297 * allocate new memory, so uncharge on that one.
1298 */
1299 fp = old_fp;
1300 err = -ENOMEM;
1301 goto out_err_free;
1302 }
1303
1304 fp->len = new_len;
1305
1306 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1307 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1308 &seen_ld_abs);
1309 if (err)
1310 /* 2nd bpf_convert_filter() can fail only if it fails
1311 * to allocate memory, remapping must succeed. Note,
1312 * that at this time old_fp has already been released
1313 * by krealloc().
1314 */
1315 goto out_err_free;
1316
1317 fp = bpf_prog_select_runtime(fp, &err);
1318 if (err)
1319 goto out_err_free;
1320
1321 kfree(old_prog);
1322 return fp;
1323
1324 out_err_free:
1325 kfree(old_prog);
1326 out_err:
1327 __bpf_prog_release(fp);
1328 return ERR_PTR(err);
1329 }
1330
bpf_prepare_filter(struct bpf_prog * fp,bpf_aux_classic_check_t trans)1331 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1332 bpf_aux_classic_check_t trans)
1333 {
1334 int err;
1335
1336 fp->bpf_func = NULL;
1337 fp->jited = 0;
1338
1339 err = bpf_check_classic(fp->insns, fp->len);
1340 if (err) {
1341 __bpf_prog_release(fp);
1342 return ERR_PTR(err);
1343 }
1344
1345 /* There might be additional checks and transformations
1346 * needed on classic filters, f.e. in case of seccomp.
1347 */
1348 if (trans) {
1349 err = trans(fp->insns, fp->len);
1350 if (err) {
1351 __bpf_prog_release(fp);
1352 return ERR_PTR(err);
1353 }
1354 }
1355
1356 /* Probe if we can JIT compile the filter and if so, do
1357 * the compilation of the filter.
1358 */
1359 bpf_jit_compile(fp);
1360
1361 /* JIT compiler couldn't process this filter, so do the eBPF translation
1362 * for the optimized interpreter.
1363 */
1364 if (!fp->jited)
1365 fp = bpf_migrate_filter(fp);
1366
1367 return fp;
1368 }
1369
1370 /**
1371 * bpf_prog_create - create an unattached filter
1372 * @pfp: the unattached filter that is created
1373 * @fprog: the filter program
1374 *
1375 * Create a filter independent of any socket. We first run some
1376 * sanity checks on it to make sure it does not explode on us later.
1377 * If an error occurs or there is insufficient memory for the filter
1378 * a negative errno code is returned. On success the return is zero.
1379 */
bpf_prog_create(struct bpf_prog ** pfp,struct sock_fprog_kern * fprog)1380 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1381 {
1382 unsigned int fsize = bpf_classic_proglen(fprog);
1383 struct bpf_prog *fp;
1384
1385 /* Make sure new filter is there and in the right amounts. */
1386 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1387 return -EINVAL;
1388
1389 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1390 if (!fp)
1391 return -ENOMEM;
1392
1393 memcpy(fp->insns, fprog->filter, fsize);
1394
1395 fp->len = fprog->len;
1396 /* Since unattached filters are not copied back to user
1397 * space through sk_get_filter(), we do not need to hold
1398 * a copy here, and can spare us the work.
1399 */
1400 fp->orig_prog = NULL;
1401
1402 /* bpf_prepare_filter() already takes care of freeing
1403 * memory in case something goes wrong.
1404 */
1405 fp = bpf_prepare_filter(fp, NULL);
1406 if (IS_ERR(fp))
1407 return PTR_ERR(fp);
1408
1409 *pfp = fp;
1410 return 0;
1411 }
1412 EXPORT_SYMBOL_GPL(bpf_prog_create);
1413
1414 /**
1415 * bpf_prog_create_from_user - create an unattached filter from user buffer
1416 * @pfp: the unattached filter that is created
1417 * @fprog: the filter program
1418 * @trans: post-classic verifier transformation handler
1419 * @save_orig: save classic BPF program
1420 *
1421 * This function effectively does the same as bpf_prog_create(), only
1422 * that it builds up its insns buffer from user space provided buffer.
1423 * It also allows for passing a bpf_aux_classic_check_t handler.
1424 */
bpf_prog_create_from_user(struct bpf_prog ** pfp,struct sock_fprog * fprog,bpf_aux_classic_check_t trans,bool save_orig)1425 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1426 bpf_aux_classic_check_t trans, bool save_orig)
1427 {
1428 unsigned int fsize = bpf_classic_proglen(fprog);
1429 struct bpf_prog *fp;
1430 int err;
1431
1432 /* Make sure new filter is there and in the right amounts. */
1433 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1434 return -EINVAL;
1435
1436 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1437 if (!fp)
1438 return -ENOMEM;
1439
1440 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1441 __bpf_prog_free(fp);
1442 return -EFAULT;
1443 }
1444
1445 fp->len = fprog->len;
1446 fp->orig_prog = NULL;
1447
1448 if (save_orig) {
1449 err = bpf_prog_store_orig_filter(fp, fprog);
1450 if (err) {
1451 __bpf_prog_free(fp);
1452 return -ENOMEM;
1453 }
1454 }
1455
1456 /* bpf_prepare_filter() already takes care of freeing
1457 * memory in case something goes wrong.
1458 */
1459 fp = bpf_prepare_filter(fp, trans);
1460 if (IS_ERR(fp))
1461 return PTR_ERR(fp);
1462
1463 *pfp = fp;
1464 return 0;
1465 }
1466 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1467
bpf_prog_destroy(struct bpf_prog * fp)1468 void bpf_prog_destroy(struct bpf_prog *fp)
1469 {
1470 __bpf_prog_release(fp);
1471 }
1472 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1473
__sk_attach_prog(struct bpf_prog * prog,struct sock * sk)1474 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1475 {
1476 struct sk_filter *fp, *old_fp;
1477
1478 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1479 if (!fp)
1480 return -ENOMEM;
1481
1482 fp->prog = prog;
1483
1484 if (!__sk_filter_charge(sk, fp)) {
1485 kfree(fp);
1486 return -ENOMEM;
1487 }
1488 refcount_set(&fp->refcnt, 1);
1489
1490 old_fp = rcu_dereference_protected(sk->sk_filter,
1491 lockdep_sock_is_held(sk));
1492 rcu_assign_pointer(sk->sk_filter, fp);
1493
1494 if (old_fp)
1495 sk_filter_uncharge(sk, old_fp);
1496
1497 return 0;
1498 }
1499
1500 static
__get_filter(struct sock_fprog * fprog,struct sock * sk)1501 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1502 {
1503 unsigned int fsize = bpf_classic_proglen(fprog);
1504 struct bpf_prog *prog;
1505 int err;
1506
1507 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1508 return ERR_PTR(-EPERM);
1509
1510 /* Make sure new filter is there and in the right amounts. */
1511 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1512 return ERR_PTR(-EINVAL);
1513
1514 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1515 if (!prog)
1516 return ERR_PTR(-ENOMEM);
1517
1518 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1519 __bpf_prog_free(prog);
1520 return ERR_PTR(-EFAULT);
1521 }
1522
1523 prog->len = fprog->len;
1524
1525 err = bpf_prog_store_orig_filter(prog, fprog);
1526 if (err) {
1527 __bpf_prog_free(prog);
1528 return ERR_PTR(-ENOMEM);
1529 }
1530
1531 /* bpf_prepare_filter() already takes care of freeing
1532 * memory in case something goes wrong.
1533 */
1534 return bpf_prepare_filter(prog, NULL);
1535 }
1536
1537 /**
1538 * sk_attach_filter - attach a socket filter
1539 * @fprog: the filter program
1540 * @sk: the socket to use
1541 *
1542 * Attach the user's filter code. We first run some sanity checks on
1543 * it to make sure it does not explode on us later. If an error
1544 * occurs or there is insufficient memory for the filter a negative
1545 * errno code is returned. On success the return is zero.
1546 */
sk_attach_filter(struct sock_fprog * fprog,struct sock * sk)1547 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1548 {
1549 struct bpf_prog *prog = __get_filter(fprog, sk);
1550 int err;
1551
1552 if (IS_ERR(prog))
1553 return PTR_ERR(prog);
1554
1555 err = __sk_attach_prog(prog, sk);
1556 if (err < 0) {
1557 __bpf_prog_release(prog);
1558 return err;
1559 }
1560
1561 return 0;
1562 }
1563 EXPORT_SYMBOL_GPL(sk_attach_filter);
1564
sk_reuseport_attach_filter(struct sock_fprog * fprog,struct sock * sk)1565 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1566 {
1567 struct bpf_prog *prog = __get_filter(fprog, sk);
1568 int err, optmem_max;
1569
1570 if (IS_ERR(prog))
1571 return PTR_ERR(prog);
1572
1573 optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
1574 if (bpf_prog_size(prog->len) > optmem_max)
1575 err = -ENOMEM;
1576 else
1577 err = reuseport_attach_prog(sk, prog);
1578
1579 if (err)
1580 __bpf_prog_release(prog);
1581
1582 return err;
1583 }
1584
__get_bpf(u32 ufd,struct sock * sk)1585 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1586 {
1587 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1588 return ERR_PTR(-EPERM);
1589
1590 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1591 }
1592
sk_attach_bpf(u32 ufd,struct sock * sk)1593 int sk_attach_bpf(u32 ufd, struct sock *sk)
1594 {
1595 struct bpf_prog *prog = __get_bpf(ufd, sk);
1596 int err;
1597
1598 if (IS_ERR(prog))
1599 return PTR_ERR(prog);
1600
1601 err = __sk_attach_prog(prog, sk);
1602 if (err < 0) {
1603 bpf_prog_put(prog);
1604 return err;
1605 }
1606
1607 return 0;
1608 }
1609
sk_reuseport_attach_bpf(u32 ufd,struct sock * sk)1610 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1611 {
1612 struct bpf_prog *prog;
1613 int err, optmem_max;
1614
1615 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1616 return -EPERM;
1617
1618 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1619 if (PTR_ERR(prog) == -EINVAL)
1620 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1621 if (IS_ERR(prog))
1622 return PTR_ERR(prog);
1623
1624 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1625 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1626 * bpf prog (e.g. sockmap). It depends on the
1627 * limitation imposed by bpf_prog_load().
1628 * Hence, sysctl_optmem_max is not checked.
1629 */
1630 if ((sk->sk_type != SOCK_STREAM &&
1631 sk->sk_type != SOCK_DGRAM) ||
1632 (sk->sk_protocol != IPPROTO_UDP &&
1633 sk->sk_protocol != IPPROTO_TCP) ||
1634 (sk->sk_family != AF_INET &&
1635 sk->sk_family != AF_INET6)) {
1636 err = -ENOTSUPP;
1637 goto err_prog_put;
1638 }
1639 } else {
1640 /* BPF_PROG_TYPE_SOCKET_FILTER */
1641 optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
1642 if (bpf_prog_size(prog->len) > optmem_max) {
1643 err = -ENOMEM;
1644 goto err_prog_put;
1645 }
1646 }
1647
1648 err = reuseport_attach_prog(sk, prog);
1649 err_prog_put:
1650 if (err)
1651 bpf_prog_put(prog);
1652
1653 return err;
1654 }
1655
sk_reuseport_prog_free(struct bpf_prog * prog)1656 void sk_reuseport_prog_free(struct bpf_prog *prog)
1657 {
1658 if (!prog)
1659 return;
1660
1661 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1662 bpf_prog_put(prog);
1663 else
1664 bpf_prog_destroy(prog);
1665 }
1666
1667 struct bpf_scratchpad {
1668 union {
1669 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1670 u8 buff[MAX_BPF_STACK];
1671 };
1672 local_lock_t bh_lock;
1673 };
1674
1675 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp) = {
1676 .bh_lock = INIT_LOCAL_LOCK(bh_lock),
1677 };
1678
__bpf_try_make_writable(struct sk_buff * skb,unsigned int write_len)1679 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1680 unsigned int write_len)
1681 {
1682 #ifdef CONFIG_DEBUG_NET
1683 /* Avoid a splat in pskb_may_pull_reason() */
1684 if (write_len > INT_MAX)
1685 return -EINVAL;
1686 #endif
1687 return skb_ensure_writable(skb, write_len);
1688 }
1689
bpf_try_make_writable(struct sk_buff * skb,unsigned int write_len)1690 static inline int bpf_try_make_writable(struct sk_buff *skb,
1691 unsigned int write_len)
1692 {
1693 int err = __bpf_try_make_writable(skb, write_len);
1694
1695 bpf_compute_data_pointers(skb);
1696 return err;
1697 }
1698
bpf_try_make_head_writable(struct sk_buff * skb)1699 static int bpf_try_make_head_writable(struct sk_buff *skb)
1700 {
1701 return bpf_try_make_writable(skb, skb_headlen(skb));
1702 }
1703
bpf_push_mac_rcsum(struct sk_buff * skb)1704 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1705 {
1706 if (skb_at_tc_ingress(skb))
1707 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1708 }
1709
bpf_pull_mac_rcsum(struct sk_buff * skb)1710 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1711 {
1712 if (skb_at_tc_ingress(skb))
1713 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1714 }
1715
BPF_CALL_5(bpf_skb_store_bytes,struct sk_buff *,skb,u32,offset,const void *,from,u32,len,u64,flags)1716 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1717 const void *, from, u32, len, u64, flags)
1718 {
1719 void *ptr;
1720
1721 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1722 return -EINVAL;
1723 if (unlikely(offset > INT_MAX))
1724 return -EFAULT;
1725 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1726 return -EFAULT;
1727
1728 ptr = skb->data + offset;
1729 if (flags & BPF_F_RECOMPUTE_CSUM)
1730 __skb_postpull_rcsum(skb, ptr, len, offset);
1731
1732 memcpy(ptr, from, len);
1733
1734 if (flags & BPF_F_RECOMPUTE_CSUM)
1735 __skb_postpush_rcsum(skb, ptr, len, offset);
1736 if (flags & BPF_F_INVALIDATE_HASH)
1737 skb_clear_hash(skb);
1738
1739 return 0;
1740 }
1741
1742 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1743 .func = bpf_skb_store_bytes,
1744 .gpl_only = false,
1745 .ret_type = RET_INTEGER,
1746 .arg1_type = ARG_PTR_TO_CTX,
1747 .arg2_type = ARG_ANYTHING,
1748 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1749 .arg4_type = ARG_CONST_SIZE,
1750 .arg5_type = ARG_ANYTHING,
1751 };
1752
__bpf_skb_store_bytes(struct sk_buff * skb,u32 offset,const void * from,u32 len,u64 flags)1753 int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
1754 u32 len, u64 flags)
1755 {
1756 return ____bpf_skb_store_bytes(skb, offset, from, len, flags);
1757 }
1758
BPF_CALL_4(bpf_skb_load_bytes,const struct sk_buff *,skb,u32,offset,void *,to,u32,len)1759 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1760 void *, to, u32, len)
1761 {
1762 void *ptr;
1763 int handled = 0;
1764 int err = 0;
1765
1766 trace_android_rvh_bpf_skb_load_bytes(skb, offset, to, len, &handled, &err);
1767 if (handled)
1768 return err;
1769
1770 if (unlikely(offset > INT_MAX))
1771 goto err_clear;
1772
1773 ptr = skb_header_pointer(skb, offset, len, to);
1774 if (unlikely(!ptr))
1775 goto err_clear;
1776 if (ptr != to)
1777 memcpy(to, ptr, len);
1778
1779 return 0;
1780 err_clear:
1781 memset(to, 0, len);
1782 return -EFAULT;
1783 }
1784
1785 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1786 .func = bpf_skb_load_bytes,
1787 .gpl_only = false,
1788 .ret_type = RET_INTEGER,
1789 .arg1_type = ARG_PTR_TO_CTX,
1790 .arg2_type = ARG_ANYTHING,
1791 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1792 .arg4_type = ARG_CONST_SIZE,
1793 };
1794
__bpf_skb_load_bytes(const struct sk_buff * skb,u32 offset,void * to,u32 len)1795 int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len)
1796 {
1797 return ____bpf_skb_load_bytes(skb, offset, to, len);
1798 }
1799
BPF_CALL_4(bpf_flow_dissector_load_bytes,const struct bpf_flow_dissector *,ctx,u32,offset,void *,to,u32,len)1800 BPF_CALL_4(bpf_flow_dissector_load_bytes,
1801 const struct bpf_flow_dissector *, ctx, u32, offset,
1802 void *, to, u32, len)
1803 {
1804 void *ptr;
1805
1806 if (unlikely(offset > 0xffff))
1807 goto err_clear;
1808
1809 if (unlikely(!ctx->skb))
1810 goto err_clear;
1811
1812 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1813 if (unlikely(!ptr))
1814 goto err_clear;
1815 if (ptr != to)
1816 memcpy(to, ptr, len);
1817
1818 return 0;
1819 err_clear:
1820 memset(to, 0, len);
1821 return -EFAULT;
1822 }
1823
1824 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1825 .func = bpf_flow_dissector_load_bytes,
1826 .gpl_only = false,
1827 .ret_type = RET_INTEGER,
1828 .arg1_type = ARG_PTR_TO_CTX,
1829 .arg2_type = ARG_ANYTHING,
1830 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1831 .arg4_type = ARG_CONST_SIZE,
1832 };
1833
BPF_CALL_5(bpf_skb_load_bytes_relative,const struct sk_buff *,skb,u32,offset,void *,to,u32,len,u32,start_header)1834 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1835 u32, offset, void *, to, u32, len, u32, start_header)
1836 {
1837 u8 *end = skb_tail_pointer(skb);
1838 u8 *start, *ptr;
1839
1840 if (unlikely(offset > 0xffff))
1841 goto err_clear;
1842
1843 switch (start_header) {
1844 case BPF_HDR_START_MAC:
1845 if (unlikely(!skb_mac_header_was_set(skb)))
1846 goto err_clear;
1847 start = skb_mac_header(skb);
1848 break;
1849 case BPF_HDR_START_NET:
1850 start = skb_network_header(skb);
1851 break;
1852 default:
1853 goto err_clear;
1854 }
1855
1856 ptr = start + offset;
1857
1858 if (likely(ptr + len <= end)) {
1859 memcpy(to, ptr, len);
1860 return 0;
1861 }
1862
1863 err_clear:
1864 memset(to, 0, len);
1865 return -EFAULT;
1866 }
1867
1868 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1869 .func = bpf_skb_load_bytes_relative,
1870 .gpl_only = false,
1871 .ret_type = RET_INTEGER,
1872 .arg1_type = ARG_PTR_TO_CTX,
1873 .arg2_type = ARG_ANYTHING,
1874 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1875 .arg4_type = ARG_CONST_SIZE,
1876 .arg5_type = ARG_ANYTHING,
1877 };
1878
BPF_CALL_2(bpf_skb_pull_data,struct sk_buff *,skb,u32,len)1879 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1880 {
1881 /* Idea is the following: should the needed direct read/write
1882 * test fail during runtime, we can pull in more data and redo
1883 * again, since implicitly, we invalidate previous checks here.
1884 *
1885 * Or, since we know how much we need to make read/writeable,
1886 * this can be done once at the program beginning for direct
1887 * access case. By this we overcome limitations of only current
1888 * headroom being accessible.
1889 */
1890 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1891 }
1892
1893 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1894 .func = bpf_skb_pull_data,
1895 .gpl_only = false,
1896 .ret_type = RET_INTEGER,
1897 .arg1_type = ARG_PTR_TO_CTX,
1898 .arg2_type = ARG_ANYTHING,
1899 };
1900
BPF_CALL_1(bpf_sk_fullsock,struct sock *,sk)1901 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1902 {
1903 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1904 }
1905
1906 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1907 .func = bpf_sk_fullsock,
1908 .gpl_only = false,
1909 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1910 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1911 };
1912
sk_skb_try_make_writable(struct sk_buff * skb,unsigned int write_len)1913 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1914 unsigned int write_len)
1915 {
1916 return __bpf_try_make_writable(skb, write_len);
1917 }
1918
BPF_CALL_2(sk_skb_pull_data,struct sk_buff *,skb,u32,len)1919 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1920 {
1921 /* Idea is the following: should the needed direct read/write
1922 * test fail during runtime, we can pull in more data and redo
1923 * again, since implicitly, we invalidate previous checks here.
1924 *
1925 * Or, since we know how much we need to make read/writeable,
1926 * this can be done once at the program beginning for direct
1927 * access case. By this we overcome limitations of only current
1928 * headroom being accessible.
1929 */
1930 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1931 }
1932
1933 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1934 .func = sk_skb_pull_data,
1935 .gpl_only = false,
1936 .ret_type = RET_INTEGER,
1937 .arg1_type = ARG_PTR_TO_CTX,
1938 .arg2_type = ARG_ANYTHING,
1939 };
1940
BPF_CALL_5(bpf_l3_csum_replace,struct sk_buff *,skb,u32,offset,u64,from,u64,to,u64,flags)1941 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1942 u64, from, u64, to, u64, flags)
1943 {
1944 __sum16 *ptr;
1945
1946 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1947 return -EINVAL;
1948 if (unlikely(offset > 0xffff || offset & 1))
1949 return -EFAULT;
1950 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1951 return -EFAULT;
1952
1953 ptr = (__sum16 *)(skb->data + offset);
1954 switch (flags & BPF_F_HDR_FIELD_MASK) {
1955 case 0:
1956 if (unlikely(from != 0))
1957 return -EINVAL;
1958
1959 csum_replace_by_diff(ptr, to);
1960 break;
1961 case 2:
1962 csum_replace2(ptr, from, to);
1963 break;
1964 case 4:
1965 csum_replace4(ptr, from, to);
1966 break;
1967 default:
1968 return -EINVAL;
1969 }
1970
1971 return 0;
1972 }
1973
1974 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1975 .func = bpf_l3_csum_replace,
1976 .gpl_only = false,
1977 .ret_type = RET_INTEGER,
1978 .arg1_type = ARG_PTR_TO_CTX,
1979 .arg2_type = ARG_ANYTHING,
1980 .arg3_type = ARG_ANYTHING,
1981 .arg4_type = ARG_ANYTHING,
1982 .arg5_type = ARG_ANYTHING,
1983 };
1984
BPF_CALL_5(bpf_l4_csum_replace,struct sk_buff *,skb,u32,offset,u64,from,u64,to,u64,flags)1985 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1986 u64, from, u64, to, u64, flags)
1987 {
1988 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1989 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1990 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1991 bool is_ipv6 = flags & BPF_F_IPV6;
1992 __sum16 *ptr;
1993
1994 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1995 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK | BPF_F_IPV6)))
1996 return -EINVAL;
1997 if (unlikely(offset > 0xffff || offset & 1))
1998 return -EFAULT;
1999 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
2000 return -EFAULT;
2001
2002 ptr = (__sum16 *)(skb->data + offset);
2003 if (is_mmzero && !do_mforce && !*ptr)
2004 return 0;
2005
2006 switch (flags & BPF_F_HDR_FIELD_MASK) {
2007 case 0:
2008 if (unlikely(from != 0))
2009 return -EINVAL;
2010
2011 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo, is_ipv6);
2012 break;
2013 case 2:
2014 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
2015 break;
2016 case 4:
2017 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
2018 break;
2019 default:
2020 return -EINVAL;
2021 }
2022
2023 if (is_mmzero && !*ptr)
2024 *ptr = CSUM_MANGLED_0;
2025 return 0;
2026 }
2027
2028 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
2029 .func = bpf_l4_csum_replace,
2030 .gpl_only = false,
2031 .ret_type = RET_INTEGER,
2032 .arg1_type = ARG_PTR_TO_CTX,
2033 .arg2_type = ARG_ANYTHING,
2034 .arg3_type = ARG_ANYTHING,
2035 .arg4_type = ARG_ANYTHING,
2036 .arg5_type = ARG_ANYTHING,
2037 };
2038
BPF_CALL_5(bpf_csum_diff,__be32 *,from,u32,from_size,__be32 *,to,u32,to_size,__wsum,seed)2039 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
2040 __be32 *, to, u32, to_size, __wsum, seed)
2041 {
2042 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
2043 u32 diff_size = from_size + to_size;
2044 int i, j = 0;
2045 __wsum ret;
2046
2047 /* This is quite flexible, some examples:
2048 *
2049 * from_size == 0, to_size > 0, seed := csum --> pushing data
2050 * from_size > 0, to_size == 0, seed := csum --> pulling data
2051 * from_size > 0, to_size > 0, seed := 0 --> diffing data
2052 *
2053 * Even for diffing, from_size and to_size don't need to be equal.
2054 */
2055 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
2056 diff_size > sizeof(sp->diff)))
2057 return -EINVAL;
2058
2059 local_lock_nested_bh(&bpf_sp.bh_lock);
2060 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
2061 sp->diff[j] = ~from[i];
2062 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
2063 sp->diff[j] = to[i];
2064
2065 ret = csum_partial(sp->diff, diff_size, seed);
2066 local_unlock_nested_bh(&bpf_sp.bh_lock);
2067 return ret;
2068 }
2069
2070 static const struct bpf_func_proto bpf_csum_diff_proto = {
2071 .func = bpf_csum_diff,
2072 .gpl_only = false,
2073 .pkt_access = true,
2074 .ret_type = RET_INTEGER,
2075 .arg1_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2076 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2077 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2078 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
2079 .arg5_type = ARG_ANYTHING,
2080 };
2081
BPF_CALL_2(bpf_csum_update,struct sk_buff *,skb,__wsum,csum)2082 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2083 {
2084 /* The interface is to be used in combination with bpf_csum_diff()
2085 * for direct packet writes. csum rotation for alignment as well
2086 * as emulating csum_sub() can be done from the eBPF program.
2087 */
2088 if (skb->ip_summed == CHECKSUM_COMPLETE)
2089 return (skb->csum = csum_add(skb->csum, csum));
2090
2091 return -ENOTSUPP;
2092 }
2093
2094 static const struct bpf_func_proto bpf_csum_update_proto = {
2095 .func = bpf_csum_update,
2096 .gpl_only = false,
2097 .ret_type = RET_INTEGER,
2098 .arg1_type = ARG_PTR_TO_CTX,
2099 .arg2_type = ARG_ANYTHING,
2100 };
2101
BPF_CALL_2(bpf_csum_level,struct sk_buff *,skb,u64,level)2102 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2103 {
2104 /* The interface is to be used in combination with bpf_skb_adjust_room()
2105 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2106 * is passed as flags, for example.
2107 */
2108 switch (level) {
2109 case BPF_CSUM_LEVEL_INC:
2110 __skb_incr_checksum_unnecessary(skb);
2111 break;
2112 case BPF_CSUM_LEVEL_DEC:
2113 __skb_decr_checksum_unnecessary(skb);
2114 break;
2115 case BPF_CSUM_LEVEL_RESET:
2116 __skb_reset_checksum_unnecessary(skb);
2117 break;
2118 case BPF_CSUM_LEVEL_QUERY:
2119 return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2120 skb->csum_level : -EACCES;
2121 default:
2122 return -EINVAL;
2123 }
2124
2125 return 0;
2126 }
2127
2128 static const struct bpf_func_proto bpf_csum_level_proto = {
2129 .func = bpf_csum_level,
2130 .gpl_only = false,
2131 .ret_type = RET_INTEGER,
2132 .arg1_type = ARG_PTR_TO_CTX,
2133 .arg2_type = ARG_ANYTHING,
2134 };
2135
__bpf_rx_skb(struct net_device * dev,struct sk_buff * skb)2136 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2137 {
2138 return dev_forward_skb_nomtu(dev, skb);
2139 }
2140
__bpf_rx_skb_no_mac(struct net_device * dev,struct sk_buff * skb)2141 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2142 struct sk_buff *skb)
2143 {
2144 int ret = ____dev_forward_skb(dev, skb, false);
2145
2146 if (likely(!ret)) {
2147 skb->dev = dev;
2148 ret = netif_rx(skb);
2149 }
2150
2151 return ret;
2152 }
2153
__bpf_tx_skb(struct net_device * dev,struct sk_buff * skb)2154 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2155 {
2156 int ret;
2157
2158 if (dev_xmit_recursion()) {
2159 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2160 kfree_skb(skb);
2161 return -ENETDOWN;
2162 }
2163
2164 skb->dev = dev;
2165 skb_set_redirected_noclear(skb, skb_at_tc_ingress(skb));
2166 skb_clear_tstamp(skb);
2167
2168 dev_xmit_recursion_inc();
2169 ret = dev_queue_xmit(skb);
2170 dev_xmit_recursion_dec();
2171
2172 return ret;
2173 }
2174
__bpf_redirect_no_mac(struct sk_buff * skb,struct net_device * dev,u32 flags)2175 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2176 u32 flags)
2177 {
2178 unsigned int mlen = skb_network_offset(skb);
2179
2180 if (unlikely(skb->len <= mlen)) {
2181 kfree_skb(skb);
2182 return -ERANGE;
2183 }
2184
2185 if (mlen) {
2186 __skb_pull(skb, mlen);
2187
2188 /* At ingress, the mac header has already been pulled once.
2189 * At egress, skb_pospull_rcsum has to be done in case that
2190 * the skb is originated from ingress (i.e. a forwarded skb)
2191 * to ensure that rcsum starts at net header.
2192 */
2193 if (!skb_at_tc_ingress(skb))
2194 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2195 }
2196 skb_pop_mac_header(skb);
2197 skb_reset_mac_len(skb);
2198 return flags & BPF_F_INGRESS ?
2199 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2200 }
2201
__bpf_redirect_common(struct sk_buff * skb,struct net_device * dev,u32 flags)2202 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2203 u32 flags)
2204 {
2205 /* Verify that a link layer header is carried */
2206 if (unlikely(skb->mac_header >= skb->network_header || skb->len == 0)) {
2207 kfree_skb(skb);
2208 return -ERANGE;
2209 }
2210
2211 bpf_push_mac_rcsum(skb);
2212 return flags & BPF_F_INGRESS ?
2213 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2214 }
2215
__bpf_redirect(struct sk_buff * skb,struct net_device * dev,u32 flags)2216 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2217 u32 flags)
2218 {
2219 if (dev_is_mac_header_xmit(dev))
2220 return __bpf_redirect_common(skb, dev, flags);
2221 else
2222 return __bpf_redirect_no_mac(skb, dev, flags);
2223 }
2224
2225 #if IS_ENABLED(CONFIG_IPV6)
bpf_out_neigh_v6(struct net * net,struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2226 static int bpf_out_neigh_v6(struct net *net, struct sk_buff *skb,
2227 struct net_device *dev, struct bpf_nh_params *nh)
2228 {
2229 u32 hh_len = LL_RESERVED_SPACE(dev);
2230 const struct in6_addr *nexthop;
2231 struct dst_entry *dst = NULL;
2232 struct neighbour *neigh;
2233
2234 if (dev_xmit_recursion()) {
2235 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2236 goto out_drop;
2237 }
2238
2239 skb->dev = dev;
2240 skb_clear_tstamp(skb);
2241
2242 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2243 skb = skb_expand_head(skb, hh_len);
2244 if (!skb)
2245 return -ENOMEM;
2246 }
2247
2248 rcu_read_lock();
2249 if (!nh) {
2250 dst = skb_dst(skb);
2251 nexthop = rt6_nexthop(dst_rt6_info(dst),
2252 &ipv6_hdr(skb)->daddr);
2253 } else {
2254 nexthop = &nh->ipv6_nh;
2255 }
2256 neigh = ip_neigh_gw6(dev, nexthop);
2257 if (likely(!IS_ERR(neigh))) {
2258 int ret;
2259
2260 sock_confirm_neigh(skb, neigh);
2261 local_bh_disable();
2262 dev_xmit_recursion_inc();
2263 ret = neigh_output(neigh, skb, false);
2264 dev_xmit_recursion_dec();
2265 local_bh_enable();
2266 rcu_read_unlock();
2267 return ret;
2268 }
2269 rcu_read_unlock();
2270 if (dst)
2271 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
2272 out_drop:
2273 kfree_skb(skb);
2274 return -ENETDOWN;
2275 }
2276
__bpf_redirect_neigh_v6(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2277 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2278 struct bpf_nh_params *nh)
2279 {
2280 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
2281 struct net *net = dev_net(dev);
2282 int err, ret = NET_XMIT_DROP;
2283
2284 if (!nh) {
2285 struct dst_entry *dst;
2286 struct flowi6 fl6 = {
2287 .flowi6_flags = FLOWI_FLAG_ANYSRC,
2288 .flowi6_mark = skb->mark,
2289 .flowlabel = ip6_flowinfo(ip6h),
2290 .flowi6_oif = dev->ifindex,
2291 .flowi6_proto = ip6h->nexthdr,
2292 .daddr = ip6h->daddr,
2293 .saddr = ip6h->saddr,
2294 };
2295
2296 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
2297 if (IS_ERR(dst))
2298 goto out_drop;
2299
2300 skb_dst_set(skb, dst);
2301 } else if (nh->nh_family != AF_INET6) {
2302 goto out_drop;
2303 }
2304
2305 err = bpf_out_neigh_v6(net, skb, dev, nh);
2306 if (unlikely(net_xmit_eval(err)))
2307 DEV_STATS_INC(dev, tx_errors);
2308 else
2309 ret = NET_XMIT_SUCCESS;
2310 goto out_xmit;
2311 out_drop:
2312 DEV_STATS_INC(dev, tx_errors);
2313 kfree_skb(skb);
2314 out_xmit:
2315 return ret;
2316 }
2317 #else
__bpf_redirect_neigh_v6(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2318 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2319 struct bpf_nh_params *nh)
2320 {
2321 kfree_skb(skb);
2322 return NET_XMIT_DROP;
2323 }
2324 #endif /* CONFIG_IPV6 */
2325
2326 #if IS_ENABLED(CONFIG_INET)
bpf_out_neigh_v4(struct net * net,struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2327 static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
2328 struct net_device *dev, struct bpf_nh_params *nh)
2329 {
2330 u32 hh_len = LL_RESERVED_SPACE(dev);
2331 struct neighbour *neigh;
2332 bool is_v6gw = false;
2333
2334 if (dev_xmit_recursion()) {
2335 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2336 goto out_drop;
2337 }
2338
2339 skb->dev = dev;
2340 skb_clear_tstamp(skb);
2341
2342 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2343 skb = skb_expand_head(skb, hh_len);
2344 if (!skb)
2345 return -ENOMEM;
2346 }
2347
2348 rcu_read_lock();
2349 if (!nh) {
2350 struct rtable *rt = skb_rtable(skb);
2351
2352 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
2353 } else if (nh->nh_family == AF_INET6) {
2354 neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
2355 is_v6gw = true;
2356 } else if (nh->nh_family == AF_INET) {
2357 neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
2358 } else {
2359 rcu_read_unlock();
2360 goto out_drop;
2361 }
2362
2363 if (likely(!IS_ERR(neigh))) {
2364 int ret;
2365
2366 sock_confirm_neigh(skb, neigh);
2367 local_bh_disable();
2368 dev_xmit_recursion_inc();
2369 ret = neigh_output(neigh, skb, is_v6gw);
2370 dev_xmit_recursion_dec();
2371 local_bh_enable();
2372 rcu_read_unlock();
2373 return ret;
2374 }
2375 rcu_read_unlock();
2376 out_drop:
2377 kfree_skb(skb);
2378 return -ENETDOWN;
2379 }
2380
__bpf_redirect_neigh_v4(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2381 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2382 struct bpf_nh_params *nh)
2383 {
2384 const struct iphdr *ip4h = ip_hdr(skb);
2385 struct net *net = dev_net(dev);
2386 int err, ret = NET_XMIT_DROP;
2387
2388 if (!nh) {
2389 struct flowi4 fl4 = {
2390 .flowi4_flags = FLOWI_FLAG_ANYSRC,
2391 .flowi4_mark = skb->mark,
2392 .flowi4_tos = ip4h->tos & INET_DSCP_MASK,
2393 .flowi4_oif = dev->ifindex,
2394 .flowi4_proto = ip4h->protocol,
2395 .daddr = ip4h->daddr,
2396 .saddr = ip4h->saddr,
2397 };
2398 struct rtable *rt;
2399
2400 rt = ip_route_output_flow(net, &fl4, NULL);
2401 if (IS_ERR(rt))
2402 goto out_drop;
2403 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
2404 ip_rt_put(rt);
2405 goto out_drop;
2406 }
2407
2408 skb_dst_set(skb, &rt->dst);
2409 }
2410
2411 err = bpf_out_neigh_v4(net, skb, dev, nh);
2412 if (unlikely(net_xmit_eval(err)))
2413 DEV_STATS_INC(dev, tx_errors);
2414 else
2415 ret = NET_XMIT_SUCCESS;
2416 goto out_xmit;
2417 out_drop:
2418 DEV_STATS_INC(dev, tx_errors);
2419 kfree_skb(skb);
2420 out_xmit:
2421 return ret;
2422 }
2423 #else
__bpf_redirect_neigh_v4(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2424 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2425 struct bpf_nh_params *nh)
2426 {
2427 kfree_skb(skb);
2428 return NET_XMIT_DROP;
2429 }
2430 #endif /* CONFIG_INET */
2431
__bpf_redirect_neigh(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2432 static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev,
2433 struct bpf_nh_params *nh)
2434 {
2435 struct ethhdr *ethh = eth_hdr(skb);
2436
2437 if (unlikely(skb->mac_header >= skb->network_header))
2438 goto out;
2439 bpf_push_mac_rcsum(skb);
2440 if (is_multicast_ether_addr(ethh->h_dest))
2441 goto out;
2442
2443 skb_pull(skb, sizeof(*ethh));
2444 skb_unset_mac_header(skb);
2445 skb_reset_network_header(skb);
2446
2447 if (skb->protocol == htons(ETH_P_IP))
2448 return __bpf_redirect_neigh_v4(skb, dev, nh);
2449 else if (skb->protocol == htons(ETH_P_IPV6))
2450 return __bpf_redirect_neigh_v6(skb, dev, nh);
2451 out:
2452 kfree_skb(skb);
2453 return -ENOTSUPP;
2454 }
2455
2456 /* Internal, non-exposed redirect flags. */
2457 enum {
2458 BPF_F_NEIGH = (1ULL << 16),
2459 BPF_F_PEER = (1ULL << 17),
2460 BPF_F_NEXTHOP = (1ULL << 18),
2461 #define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH | BPF_F_PEER | BPF_F_NEXTHOP)
2462 };
2463
BPF_CALL_3(bpf_clone_redirect,struct sk_buff *,skb,u32,ifindex,u64,flags)2464 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2465 {
2466 struct net_device *dev;
2467 struct sk_buff *clone;
2468 int ret;
2469
2470 BUILD_BUG_ON(BPF_F_REDIRECT_INTERNAL & BPF_F_REDIRECT_FLAGS);
2471
2472 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2473 return -EINVAL;
2474
2475 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2476 if (unlikely(!dev))
2477 return -EINVAL;
2478
2479 clone = skb_clone(skb, GFP_ATOMIC);
2480 if (unlikely(!clone))
2481 return -ENOMEM;
2482
2483 /* For direct write, we need to keep the invariant that the skbs
2484 * we're dealing with need to be uncloned. Should uncloning fail
2485 * here, we need to free the just generated clone to unclone once
2486 * again.
2487 */
2488 ret = bpf_try_make_head_writable(skb);
2489 if (unlikely(ret)) {
2490 kfree_skb(clone);
2491 return -ENOMEM;
2492 }
2493
2494 return __bpf_redirect(clone, dev, flags);
2495 }
2496
2497 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2498 .func = bpf_clone_redirect,
2499 .gpl_only = false,
2500 .ret_type = RET_INTEGER,
2501 .arg1_type = ARG_PTR_TO_CTX,
2502 .arg2_type = ARG_ANYTHING,
2503 .arg3_type = ARG_ANYTHING,
2504 };
2505
skb_get_peer_dev(struct net_device * dev)2506 static struct net_device *skb_get_peer_dev(struct net_device *dev)
2507 {
2508 const struct net_device_ops *ops = dev->netdev_ops;
2509
2510 if (likely(ops->ndo_get_peer_dev))
2511 return INDIRECT_CALL_1(ops->ndo_get_peer_dev,
2512 netkit_peer_dev, dev);
2513 return NULL;
2514 }
2515
skb_do_redirect(struct sk_buff * skb)2516 int skb_do_redirect(struct sk_buff *skb)
2517 {
2518 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
2519 struct net *net = dev_net(skb->dev);
2520 struct net_device *dev;
2521 u32 flags = ri->flags;
2522
2523 dev = dev_get_by_index_rcu(net, ri->tgt_index);
2524 ri->tgt_index = 0;
2525 ri->flags = 0;
2526 if (unlikely(!dev))
2527 goto out_drop;
2528 if (flags & BPF_F_PEER) {
2529 if (unlikely(!skb_at_tc_ingress(skb)))
2530 goto out_drop;
2531 dev = skb_get_peer_dev(dev);
2532 if (unlikely(!dev ||
2533 !(dev->flags & IFF_UP) ||
2534 net_eq(net, dev_net(dev))))
2535 goto out_drop;
2536 skb->dev = dev;
2537 dev_sw_netstats_rx_add(dev, skb->len);
2538 skb_scrub_packet(skb, false);
2539 return -EAGAIN;
2540 }
2541 return flags & BPF_F_NEIGH ?
2542 __bpf_redirect_neigh(skb, dev, flags & BPF_F_NEXTHOP ?
2543 &ri->nh : NULL) :
2544 __bpf_redirect(skb, dev, flags);
2545 out_drop:
2546 kfree_skb(skb);
2547 return -EINVAL;
2548 }
2549
BPF_CALL_2(bpf_redirect,u32,ifindex,u64,flags)2550 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2551 {
2552 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
2553
2554 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2555 return TC_ACT_SHOT;
2556
2557 ri->flags = flags;
2558 ri->tgt_index = ifindex;
2559
2560 return TC_ACT_REDIRECT;
2561 }
2562
2563 static const struct bpf_func_proto bpf_redirect_proto = {
2564 .func = bpf_redirect,
2565 .gpl_only = false,
2566 .ret_type = RET_INTEGER,
2567 .arg1_type = ARG_ANYTHING,
2568 .arg2_type = ARG_ANYTHING,
2569 };
2570
BPF_CALL_2(bpf_redirect_peer,u32,ifindex,u64,flags)2571 BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags)
2572 {
2573 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
2574
2575 if (unlikely(flags))
2576 return TC_ACT_SHOT;
2577
2578 ri->flags = BPF_F_PEER;
2579 ri->tgt_index = ifindex;
2580
2581 return TC_ACT_REDIRECT;
2582 }
2583
2584 static const struct bpf_func_proto bpf_redirect_peer_proto = {
2585 .func = bpf_redirect_peer,
2586 .gpl_only = false,
2587 .ret_type = RET_INTEGER,
2588 .arg1_type = ARG_ANYTHING,
2589 .arg2_type = ARG_ANYTHING,
2590 };
2591
BPF_CALL_4(bpf_redirect_neigh,u32,ifindex,struct bpf_redir_neigh *,params,int,plen,u64,flags)2592 BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params,
2593 int, plen, u64, flags)
2594 {
2595 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
2596
2597 if (unlikely((plen && plen < sizeof(*params)) || flags))
2598 return TC_ACT_SHOT;
2599
2600 ri->flags = BPF_F_NEIGH | (plen ? BPF_F_NEXTHOP : 0);
2601 ri->tgt_index = ifindex;
2602
2603 BUILD_BUG_ON(sizeof(struct bpf_redir_neigh) != sizeof(struct bpf_nh_params));
2604 if (plen)
2605 memcpy(&ri->nh, params, sizeof(ri->nh));
2606
2607 return TC_ACT_REDIRECT;
2608 }
2609
2610 static const struct bpf_func_proto bpf_redirect_neigh_proto = {
2611 .func = bpf_redirect_neigh,
2612 .gpl_only = false,
2613 .ret_type = RET_INTEGER,
2614 .arg1_type = ARG_ANYTHING,
2615 .arg2_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2616 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
2617 .arg4_type = ARG_ANYTHING,
2618 };
2619
BPF_CALL_2(bpf_msg_apply_bytes,struct sk_msg *,msg,u32,bytes)2620 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2621 {
2622 msg->apply_bytes = bytes;
2623 return 0;
2624 }
2625
2626 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2627 .func = bpf_msg_apply_bytes,
2628 .gpl_only = false,
2629 .ret_type = RET_INTEGER,
2630 .arg1_type = ARG_PTR_TO_CTX,
2631 .arg2_type = ARG_ANYTHING,
2632 };
2633
BPF_CALL_2(bpf_msg_cork_bytes,struct sk_msg *,msg,u32,bytes)2634 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2635 {
2636 msg->cork_bytes = bytes;
2637 return 0;
2638 }
2639
sk_msg_reset_curr(struct sk_msg * msg)2640 static void sk_msg_reset_curr(struct sk_msg *msg)
2641 {
2642 if (!msg->sg.size) {
2643 msg->sg.curr = msg->sg.start;
2644 msg->sg.copybreak = 0;
2645 } else {
2646 u32 i = msg->sg.end;
2647
2648 sk_msg_iter_var_prev(i);
2649 msg->sg.curr = i;
2650 msg->sg.copybreak = msg->sg.data[i].length;
2651 }
2652 }
2653
2654 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2655 .func = bpf_msg_cork_bytes,
2656 .gpl_only = false,
2657 .ret_type = RET_INTEGER,
2658 .arg1_type = ARG_PTR_TO_CTX,
2659 .arg2_type = ARG_ANYTHING,
2660 };
2661
BPF_CALL_4(bpf_msg_pull_data,struct sk_msg *,msg,u32,start,u32,end,u64,flags)2662 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2663 u32, end, u64, flags)
2664 {
2665 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2666 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2667 struct scatterlist *sge;
2668 u8 *raw, *to, *from;
2669 struct page *page;
2670
2671 if (unlikely(flags || end <= start))
2672 return -EINVAL;
2673
2674 /* First find the starting scatterlist element */
2675 i = msg->sg.start;
2676 do {
2677 offset += len;
2678 len = sk_msg_elem(msg, i)->length;
2679 if (start < offset + len)
2680 break;
2681 sk_msg_iter_var_next(i);
2682 } while (i != msg->sg.end);
2683
2684 if (unlikely(start >= offset + len))
2685 return -EINVAL;
2686
2687 first_sge = i;
2688 /* The start may point into the sg element so we need to also
2689 * account for the headroom.
2690 */
2691 bytes_sg_total = start - offset + bytes;
2692 if (!test_bit(i, msg->sg.copy) && bytes_sg_total <= len)
2693 goto out;
2694
2695 /* At this point we need to linearize multiple scatterlist
2696 * elements or a single shared page. Either way we need to
2697 * copy into a linear buffer exclusively owned by BPF. Then
2698 * place the buffer in the scatterlist and fixup the original
2699 * entries by removing the entries now in the linear buffer
2700 * and shifting the remaining entries. For now we do not try
2701 * to copy partial entries to avoid complexity of running out
2702 * of sg_entry slots. The downside is reading a single byte
2703 * will copy the entire sg entry.
2704 */
2705 do {
2706 copy += sk_msg_elem(msg, i)->length;
2707 sk_msg_iter_var_next(i);
2708 if (bytes_sg_total <= copy)
2709 break;
2710 } while (i != msg->sg.end);
2711 last_sge = i;
2712
2713 if (unlikely(bytes_sg_total > copy))
2714 return -EINVAL;
2715
2716 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2717 get_order(copy));
2718 if (unlikely(!page))
2719 return -ENOMEM;
2720
2721 raw = page_address(page);
2722 i = first_sge;
2723 do {
2724 sge = sk_msg_elem(msg, i);
2725 from = sg_virt(sge);
2726 len = sge->length;
2727 to = raw + poffset;
2728
2729 memcpy(to, from, len);
2730 poffset += len;
2731 sge->length = 0;
2732 put_page(sg_page(sge));
2733
2734 sk_msg_iter_var_next(i);
2735 } while (i != last_sge);
2736
2737 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2738
2739 /* To repair sg ring we need to shift entries. If we only
2740 * had a single entry though we can just replace it and
2741 * be done. Otherwise walk the ring and shift the entries.
2742 */
2743 WARN_ON_ONCE(last_sge == first_sge);
2744 shift = last_sge > first_sge ?
2745 last_sge - first_sge - 1 :
2746 NR_MSG_FRAG_IDS - first_sge + last_sge - 1;
2747 if (!shift)
2748 goto out;
2749
2750 i = first_sge;
2751 sk_msg_iter_var_next(i);
2752 do {
2753 u32 move_from;
2754
2755 if (i + shift >= NR_MSG_FRAG_IDS)
2756 move_from = i + shift - NR_MSG_FRAG_IDS;
2757 else
2758 move_from = i + shift;
2759 if (move_from == msg->sg.end)
2760 break;
2761
2762 msg->sg.data[i] = msg->sg.data[move_from];
2763 msg->sg.data[move_from].length = 0;
2764 msg->sg.data[move_from].page_link = 0;
2765 msg->sg.data[move_from].offset = 0;
2766 sk_msg_iter_var_next(i);
2767 } while (1);
2768
2769 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2770 msg->sg.end - shift + NR_MSG_FRAG_IDS :
2771 msg->sg.end - shift;
2772 out:
2773 sk_msg_reset_curr(msg);
2774 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2775 msg->data_end = msg->data + bytes;
2776 return 0;
2777 }
2778
2779 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2780 .func = bpf_msg_pull_data,
2781 .gpl_only = false,
2782 .ret_type = RET_INTEGER,
2783 .arg1_type = ARG_PTR_TO_CTX,
2784 .arg2_type = ARG_ANYTHING,
2785 .arg3_type = ARG_ANYTHING,
2786 .arg4_type = ARG_ANYTHING,
2787 };
2788
BPF_CALL_4(bpf_msg_push_data,struct sk_msg *,msg,u32,start,u32,len,u64,flags)2789 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2790 u32, len, u64, flags)
2791 {
2792 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2793 u32 new, i = 0, l = 0, space, copy = 0, offset = 0;
2794 u8 *raw, *to, *from;
2795 struct page *page;
2796
2797 if (unlikely(flags))
2798 return -EINVAL;
2799
2800 if (unlikely(len == 0))
2801 return 0;
2802
2803 /* First find the starting scatterlist element */
2804 i = msg->sg.start;
2805 do {
2806 offset += l;
2807 l = sk_msg_elem(msg, i)->length;
2808
2809 if (start < offset + l)
2810 break;
2811 sk_msg_iter_var_next(i);
2812 } while (i != msg->sg.end);
2813
2814 if (start > offset + l)
2815 return -EINVAL;
2816
2817 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2818
2819 /* If no space available will fallback to copy, we need at
2820 * least one scatterlist elem available to push data into
2821 * when start aligns to the beginning of an element or two
2822 * when it falls inside an element. We handle the start equals
2823 * offset case because its the common case for inserting a
2824 * header.
2825 */
2826 if (!space || (space == 1 && start != offset))
2827 copy = msg->sg.data[i].length;
2828
2829 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2830 get_order(copy + len));
2831 if (unlikely(!page))
2832 return -ENOMEM;
2833
2834 if (copy) {
2835 int front, back;
2836
2837 raw = page_address(page);
2838
2839 if (i == msg->sg.end)
2840 sk_msg_iter_var_prev(i);
2841 psge = sk_msg_elem(msg, i);
2842 front = start - offset;
2843 back = psge->length - front;
2844 from = sg_virt(psge);
2845
2846 if (front)
2847 memcpy(raw, from, front);
2848
2849 if (back) {
2850 from += front;
2851 to = raw + front + len;
2852
2853 memcpy(to, from, back);
2854 }
2855
2856 put_page(sg_page(psge));
2857 new = i;
2858 goto place_new;
2859 }
2860
2861 if (start - offset) {
2862 if (i == msg->sg.end)
2863 sk_msg_iter_var_prev(i);
2864 psge = sk_msg_elem(msg, i);
2865 rsge = sk_msg_elem_cpy(msg, i);
2866
2867 psge->length = start - offset;
2868 rsge.length -= psge->length;
2869 rsge.offset += start;
2870
2871 sk_msg_iter_var_next(i);
2872 sg_unmark_end(psge);
2873 sg_unmark_end(&rsge);
2874 }
2875
2876 /* Slot(s) to place newly allocated data */
2877 sk_msg_iter_next(msg, end);
2878 new = i;
2879 sk_msg_iter_var_next(i);
2880
2881 if (i == msg->sg.end) {
2882 if (!rsge.length)
2883 goto place_new;
2884 sk_msg_iter_next(msg, end);
2885 goto place_new;
2886 }
2887
2888 /* Shift one or two slots as needed */
2889 sge = sk_msg_elem_cpy(msg, new);
2890 sg_unmark_end(&sge);
2891
2892 nsge = sk_msg_elem_cpy(msg, i);
2893 if (rsge.length) {
2894 sk_msg_iter_var_next(i);
2895 nnsge = sk_msg_elem_cpy(msg, i);
2896 sk_msg_iter_next(msg, end);
2897 }
2898
2899 while (i != msg->sg.end) {
2900 msg->sg.data[i] = sge;
2901 sge = nsge;
2902 sk_msg_iter_var_next(i);
2903 if (rsge.length) {
2904 nsge = nnsge;
2905 nnsge = sk_msg_elem_cpy(msg, i);
2906 } else {
2907 nsge = sk_msg_elem_cpy(msg, i);
2908 }
2909 }
2910
2911 place_new:
2912 /* Place newly allocated data buffer */
2913 sk_mem_charge(msg->sk, len);
2914 msg->sg.size += len;
2915 __clear_bit(new, msg->sg.copy);
2916 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2917 if (rsge.length) {
2918 get_page(sg_page(&rsge));
2919 sk_msg_iter_var_next(new);
2920 msg->sg.data[new] = rsge;
2921 }
2922
2923 sk_msg_reset_curr(msg);
2924 sk_msg_compute_data_pointers(msg);
2925 return 0;
2926 }
2927
2928 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2929 .func = bpf_msg_push_data,
2930 .gpl_only = false,
2931 .ret_type = RET_INTEGER,
2932 .arg1_type = ARG_PTR_TO_CTX,
2933 .arg2_type = ARG_ANYTHING,
2934 .arg3_type = ARG_ANYTHING,
2935 .arg4_type = ARG_ANYTHING,
2936 };
2937
sk_msg_shift_left(struct sk_msg * msg,int i)2938 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2939 {
2940 struct scatterlist *sge = sk_msg_elem(msg, i);
2941 int prev;
2942
2943 put_page(sg_page(sge));
2944 do {
2945 prev = i;
2946 sk_msg_iter_var_next(i);
2947 msg->sg.data[prev] = msg->sg.data[i];
2948 } while (i != msg->sg.end);
2949
2950 sk_msg_iter_prev(msg, end);
2951 }
2952
sk_msg_shift_right(struct sk_msg * msg,int i)2953 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2954 {
2955 struct scatterlist tmp, sge;
2956
2957 sk_msg_iter_next(msg, end);
2958 sge = sk_msg_elem_cpy(msg, i);
2959 sk_msg_iter_var_next(i);
2960 tmp = sk_msg_elem_cpy(msg, i);
2961
2962 while (i != msg->sg.end) {
2963 msg->sg.data[i] = sge;
2964 sk_msg_iter_var_next(i);
2965 sge = tmp;
2966 tmp = sk_msg_elem_cpy(msg, i);
2967 }
2968 }
2969
BPF_CALL_4(bpf_msg_pop_data,struct sk_msg *,msg,u32,start,u32,len,u64,flags)2970 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2971 u32, len, u64, flags)
2972 {
2973 u32 i = 0, l = 0, space, offset = 0;
2974 u64 last = start + len;
2975 int pop;
2976
2977 if (unlikely(flags))
2978 return -EINVAL;
2979
2980 if (unlikely(len == 0))
2981 return 0;
2982
2983 /* First find the starting scatterlist element */
2984 i = msg->sg.start;
2985 do {
2986 offset += l;
2987 l = sk_msg_elem(msg, i)->length;
2988
2989 if (start < offset + l)
2990 break;
2991 sk_msg_iter_var_next(i);
2992 } while (i != msg->sg.end);
2993
2994 /* Bounds checks: start and pop must be inside message */
2995 if (start >= offset + l || last > msg->sg.size)
2996 return -EINVAL;
2997
2998 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2999
3000 pop = len;
3001 /* --------------| offset
3002 * -| start |-------- len -------|
3003 *
3004 * |----- a ----|-------- pop -------|----- b ----|
3005 * |______________________________________________| length
3006 *
3007 *
3008 * a: region at front of scatter element to save
3009 * b: region at back of scatter element to save when length > A + pop
3010 * pop: region to pop from element, same as input 'pop' here will be
3011 * decremented below per iteration.
3012 *
3013 * Two top-level cases to handle when start != offset, first B is non
3014 * zero and second B is zero corresponding to when a pop includes more
3015 * than one element.
3016 *
3017 * Then if B is non-zero AND there is no space allocate space and
3018 * compact A, B regions into page. If there is space shift ring to
3019 * the right free'ing the next element in ring to place B, leaving
3020 * A untouched except to reduce length.
3021 */
3022 if (start != offset) {
3023 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
3024 int a = start - offset;
3025 int b = sge->length - pop - a;
3026
3027 sk_msg_iter_var_next(i);
3028
3029 if (b > 0) {
3030 if (space) {
3031 sge->length = a;
3032 sk_msg_shift_right(msg, i);
3033 nsge = sk_msg_elem(msg, i);
3034 get_page(sg_page(sge));
3035 sg_set_page(nsge,
3036 sg_page(sge),
3037 b, sge->offset + pop + a);
3038 } else {
3039 struct page *page, *orig;
3040 u8 *to, *from;
3041
3042 page = alloc_pages(__GFP_NOWARN |
3043 __GFP_COMP | GFP_ATOMIC,
3044 get_order(a + b));
3045 if (unlikely(!page))
3046 return -ENOMEM;
3047
3048 orig = sg_page(sge);
3049 from = sg_virt(sge);
3050 to = page_address(page);
3051 memcpy(to, from, a);
3052 memcpy(to + a, from + a + pop, b);
3053 sg_set_page(sge, page, a + b, 0);
3054 put_page(orig);
3055 }
3056 pop = 0;
3057 } else {
3058 pop -= (sge->length - a);
3059 sge->length = a;
3060 }
3061 }
3062
3063 /* From above the current layout _must_ be as follows,
3064 *
3065 * -| offset
3066 * -| start
3067 *
3068 * |---- pop ---|---------------- b ------------|
3069 * |____________________________________________| length
3070 *
3071 * Offset and start of the current msg elem are equal because in the
3072 * previous case we handled offset != start and either consumed the
3073 * entire element and advanced to the next element OR pop == 0.
3074 *
3075 * Two cases to handle here are first pop is less than the length
3076 * leaving some remainder b above. Simply adjust the element's layout
3077 * in this case. Or pop >= length of the element so that b = 0. In this
3078 * case advance to next element decrementing pop.
3079 */
3080 while (pop) {
3081 struct scatterlist *sge = sk_msg_elem(msg, i);
3082
3083 if (pop < sge->length) {
3084 sge->length -= pop;
3085 sge->offset += pop;
3086 pop = 0;
3087 } else {
3088 pop -= sge->length;
3089 sk_msg_shift_left(msg, i);
3090 }
3091 }
3092
3093 sk_mem_uncharge(msg->sk, len - pop);
3094 msg->sg.size -= (len - pop);
3095 sk_msg_reset_curr(msg);
3096 sk_msg_compute_data_pointers(msg);
3097 return 0;
3098 }
3099
3100 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
3101 .func = bpf_msg_pop_data,
3102 .gpl_only = false,
3103 .ret_type = RET_INTEGER,
3104 .arg1_type = ARG_PTR_TO_CTX,
3105 .arg2_type = ARG_ANYTHING,
3106 .arg3_type = ARG_ANYTHING,
3107 .arg4_type = ARG_ANYTHING,
3108 };
3109
3110 #ifdef CONFIG_CGROUP_NET_CLASSID
BPF_CALL_0(bpf_get_cgroup_classid_curr)3111 BPF_CALL_0(bpf_get_cgroup_classid_curr)
3112 {
3113 return __task_get_classid(current);
3114 }
3115
3116 const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
3117 .func = bpf_get_cgroup_classid_curr,
3118 .gpl_only = false,
3119 .ret_type = RET_INTEGER,
3120 };
3121
BPF_CALL_1(bpf_skb_cgroup_classid,const struct sk_buff *,skb)3122 BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb)
3123 {
3124 struct sock *sk = skb_to_full_sk(skb);
3125
3126 if (!sk || !sk_fullsock(sk))
3127 return 0;
3128
3129 return sock_cgroup_classid(&sk->sk_cgrp_data);
3130 }
3131
3132 static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = {
3133 .func = bpf_skb_cgroup_classid,
3134 .gpl_only = false,
3135 .ret_type = RET_INTEGER,
3136 .arg1_type = ARG_PTR_TO_CTX,
3137 };
3138 #endif
3139
BPF_CALL_1(bpf_get_cgroup_classid,const struct sk_buff *,skb)3140 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
3141 {
3142 return task_get_classid(skb);
3143 }
3144
3145 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
3146 .func = bpf_get_cgroup_classid,
3147 .gpl_only = false,
3148 .ret_type = RET_INTEGER,
3149 .arg1_type = ARG_PTR_TO_CTX,
3150 };
3151
BPF_CALL_1(bpf_get_route_realm,const struct sk_buff *,skb)3152 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
3153 {
3154 return dst_tclassid(skb);
3155 }
3156
3157 static const struct bpf_func_proto bpf_get_route_realm_proto = {
3158 .func = bpf_get_route_realm,
3159 .gpl_only = false,
3160 .ret_type = RET_INTEGER,
3161 .arg1_type = ARG_PTR_TO_CTX,
3162 };
3163
BPF_CALL_1(bpf_get_hash_recalc,struct sk_buff *,skb)3164 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
3165 {
3166 /* If skb_clear_hash() was called due to mangling, we can
3167 * trigger SW recalculation here. Later access to hash
3168 * can then use the inline skb->hash via context directly
3169 * instead of calling this helper again.
3170 */
3171 return skb_get_hash(skb);
3172 }
3173
3174 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
3175 .func = bpf_get_hash_recalc,
3176 .gpl_only = false,
3177 .ret_type = RET_INTEGER,
3178 .arg1_type = ARG_PTR_TO_CTX,
3179 };
3180
BPF_CALL_1(bpf_set_hash_invalid,struct sk_buff *,skb)3181 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
3182 {
3183 /* After all direct packet write, this can be used once for
3184 * triggering a lazy recalc on next skb_get_hash() invocation.
3185 */
3186 skb_clear_hash(skb);
3187 return 0;
3188 }
3189
3190 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
3191 .func = bpf_set_hash_invalid,
3192 .gpl_only = false,
3193 .ret_type = RET_INTEGER,
3194 .arg1_type = ARG_PTR_TO_CTX,
3195 };
3196
BPF_CALL_2(bpf_set_hash,struct sk_buff *,skb,u32,hash)3197 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
3198 {
3199 /* Set user specified hash as L4(+), so that it gets returned
3200 * on skb_get_hash() call unless BPF prog later on triggers a
3201 * skb_clear_hash().
3202 */
3203 __skb_set_sw_hash(skb, hash, true);
3204 return 0;
3205 }
3206
3207 static const struct bpf_func_proto bpf_set_hash_proto = {
3208 .func = bpf_set_hash,
3209 .gpl_only = false,
3210 .ret_type = RET_INTEGER,
3211 .arg1_type = ARG_PTR_TO_CTX,
3212 .arg2_type = ARG_ANYTHING,
3213 };
3214
BPF_CALL_3(bpf_skb_vlan_push,struct sk_buff *,skb,__be16,vlan_proto,u16,vlan_tci)3215 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
3216 u16, vlan_tci)
3217 {
3218 int ret;
3219
3220 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
3221 vlan_proto != htons(ETH_P_8021AD)))
3222 vlan_proto = htons(ETH_P_8021Q);
3223
3224 bpf_push_mac_rcsum(skb);
3225 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
3226 bpf_pull_mac_rcsum(skb);
3227 skb_reset_mac_len(skb);
3228
3229 bpf_compute_data_pointers(skb);
3230 return ret;
3231 }
3232
3233 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
3234 .func = bpf_skb_vlan_push,
3235 .gpl_only = false,
3236 .ret_type = RET_INTEGER,
3237 .arg1_type = ARG_PTR_TO_CTX,
3238 .arg2_type = ARG_ANYTHING,
3239 .arg3_type = ARG_ANYTHING,
3240 };
3241
BPF_CALL_1(bpf_skb_vlan_pop,struct sk_buff *,skb)3242 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
3243 {
3244 int ret;
3245
3246 bpf_push_mac_rcsum(skb);
3247 ret = skb_vlan_pop(skb);
3248 bpf_pull_mac_rcsum(skb);
3249
3250 bpf_compute_data_pointers(skb);
3251 return ret;
3252 }
3253
3254 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
3255 .func = bpf_skb_vlan_pop,
3256 .gpl_only = false,
3257 .ret_type = RET_INTEGER,
3258 .arg1_type = ARG_PTR_TO_CTX,
3259 };
3260
bpf_skb_change_protocol(struct sk_buff * skb,u16 proto)3261 static void bpf_skb_change_protocol(struct sk_buff *skb, u16 proto)
3262 {
3263 skb->protocol = htons(proto);
3264 if (skb_valid_dst(skb))
3265 skb_dst_drop(skb);
3266 }
3267
bpf_skb_generic_push(struct sk_buff * skb,u32 off,u32 len)3268 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
3269 {
3270 /* Caller already did skb_cow() with len as headroom,
3271 * so no need to do it here.
3272 */
3273 skb_push(skb, len);
3274 memmove(skb->data, skb->data + len, off);
3275 memset(skb->data + off, 0, len);
3276
3277 /* No skb_postpush_rcsum(skb, skb->data + off, len)
3278 * needed here as it does not change the skb->csum
3279 * result for checksum complete when summing over
3280 * zeroed blocks.
3281 */
3282 return 0;
3283 }
3284
bpf_skb_generic_pop(struct sk_buff * skb,u32 off,u32 len)3285 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
3286 {
3287 void *old_data;
3288
3289 /* skb_ensure_writable() is not needed here, as we're
3290 * already working on an uncloned skb.
3291 */
3292 if (unlikely(!pskb_may_pull(skb, off + len)))
3293 return -ENOMEM;
3294
3295 old_data = skb->data;
3296 __skb_pull(skb, len);
3297 skb_postpull_rcsum(skb, old_data + off, len);
3298 memmove(skb->data, old_data, off);
3299
3300 return 0;
3301 }
3302
bpf_skb_net_hdr_push(struct sk_buff * skb,u32 off,u32 len)3303 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
3304 {
3305 bool trans_same = skb->transport_header == skb->network_header;
3306 int ret;
3307
3308 /* There's no need for __skb_push()/__skb_pull() pair to
3309 * get to the start of the mac header as we're guaranteed
3310 * to always start from here under eBPF.
3311 */
3312 ret = bpf_skb_generic_push(skb, off, len);
3313 if (likely(!ret)) {
3314 skb->mac_header -= len;
3315 skb->network_header -= len;
3316 if (trans_same)
3317 skb->transport_header = skb->network_header;
3318 }
3319
3320 return ret;
3321 }
3322
bpf_skb_net_hdr_pop(struct sk_buff * skb,u32 off,u32 len)3323 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
3324 {
3325 bool trans_same = skb->transport_header == skb->network_header;
3326 int ret;
3327
3328 /* Same here, __skb_push()/__skb_pull() pair not needed. */
3329 ret = bpf_skb_generic_pop(skb, off, len);
3330 if (likely(!ret)) {
3331 skb->mac_header += len;
3332 skb->network_header += len;
3333 if (trans_same)
3334 skb->transport_header = skb->network_header;
3335 }
3336
3337 return ret;
3338 }
3339
bpf_skb_proto_4_to_6(struct sk_buff * skb)3340 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
3341 {
3342 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3343 u32 off = skb_mac_header_len(skb);
3344 int ret;
3345
3346 ret = skb_cow(skb, len_diff);
3347 if (unlikely(ret < 0))
3348 return ret;
3349
3350 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3351 if (unlikely(ret < 0))
3352 return ret;
3353
3354 if (skb_is_gso(skb)) {
3355 struct skb_shared_info *shinfo = skb_shinfo(skb);
3356
3357 /* SKB_GSO_TCPV4 needs to be changed into SKB_GSO_TCPV6. */
3358 if (shinfo->gso_type & SKB_GSO_TCPV4) {
3359 shinfo->gso_type &= ~SKB_GSO_TCPV4;
3360 shinfo->gso_type |= SKB_GSO_TCPV6;
3361 }
3362 }
3363
3364 bpf_skb_change_protocol(skb, ETH_P_IPV6);
3365 skb_clear_hash(skb);
3366
3367 return 0;
3368 }
3369
bpf_skb_proto_6_to_4(struct sk_buff * skb)3370 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
3371 {
3372 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3373 u32 off = skb_mac_header_len(skb);
3374 int ret;
3375
3376 ret = skb_unclone(skb, GFP_ATOMIC);
3377 if (unlikely(ret < 0))
3378 return ret;
3379
3380 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3381 if (unlikely(ret < 0))
3382 return ret;
3383
3384 if (skb_is_gso(skb)) {
3385 struct skb_shared_info *shinfo = skb_shinfo(skb);
3386
3387 /* SKB_GSO_TCPV6 needs to be changed into SKB_GSO_TCPV4. */
3388 if (shinfo->gso_type & SKB_GSO_TCPV6) {
3389 shinfo->gso_type &= ~SKB_GSO_TCPV6;
3390 shinfo->gso_type |= SKB_GSO_TCPV4;
3391 }
3392 }
3393
3394 bpf_skb_change_protocol(skb, ETH_P_IP);
3395 skb_clear_hash(skb);
3396
3397 return 0;
3398 }
3399
bpf_skb_proto_xlat(struct sk_buff * skb,__be16 to_proto)3400 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
3401 {
3402 __be16 from_proto = skb->protocol;
3403
3404 if (from_proto == htons(ETH_P_IP) &&
3405 to_proto == htons(ETH_P_IPV6))
3406 return bpf_skb_proto_4_to_6(skb);
3407
3408 if (from_proto == htons(ETH_P_IPV6) &&
3409 to_proto == htons(ETH_P_IP))
3410 return bpf_skb_proto_6_to_4(skb);
3411
3412 return -ENOTSUPP;
3413 }
3414
BPF_CALL_3(bpf_skb_change_proto,struct sk_buff *,skb,__be16,proto,u64,flags)3415 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
3416 u64, flags)
3417 {
3418 int ret;
3419
3420 if (unlikely(flags))
3421 return -EINVAL;
3422
3423 /* General idea is that this helper does the basic groundwork
3424 * needed for changing the protocol, and eBPF program fills the
3425 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
3426 * and other helpers, rather than passing a raw buffer here.
3427 *
3428 * The rationale is to keep this minimal and without a need to
3429 * deal with raw packet data. F.e. even if we would pass buffers
3430 * here, the program still needs to call the bpf_lX_csum_replace()
3431 * helpers anyway. Plus, this way we keep also separation of
3432 * concerns, since f.e. bpf_skb_store_bytes() should only take
3433 * care of stores.
3434 *
3435 * Currently, additional options and extension header space are
3436 * not supported, but flags register is reserved so we can adapt
3437 * that. For offloads, we mark packet as dodgy, so that headers
3438 * need to be verified first.
3439 */
3440 ret = bpf_skb_proto_xlat(skb, proto);
3441 bpf_compute_data_pointers(skb);
3442 return ret;
3443 }
3444
3445 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
3446 .func = bpf_skb_change_proto,
3447 .gpl_only = false,
3448 .ret_type = RET_INTEGER,
3449 .arg1_type = ARG_PTR_TO_CTX,
3450 .arg2_type = ARG_ANYTHING,
3451 .arg3_type = ARG_ANYTHING,
3452 };
3453
BPF_CALL_2(bpf_skb_change_type,struct sk_buff *,skb,u32,pkt_type)3454 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
3455 {
3456 /* We only allow a restricted subset to be changed for now. */
3457 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
3458 !skb_pkt_type_ok(pkt_type)))
3459 return -EINVAL;
3460
3461 skb->pkt_type = pkt_type;
3462 return 0;
3463 }
3464
3465 static const struct bpf_func_proto bpf_skb_change_type_proto = {
3466 .func = bpf_skb_change_type,
3467 .gpl_only = false,
3468 .ret_type = RET_INTEGER,
3469 .arg1_type = ARG_PTR_TO_CTX,
3470 .arg2_type = ARG_ANYTHING,
3471 };
3472
bpf_skb_net_base_len(const struct sk_buff * skb)3473 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
3474 {
3475 switch (skb->protocol) {
3476 case htons(ETH_P_IP):
3477 return sizeof(struct iphdr);
3478 case htons(ETH_P_IPV6):
3479 return sizeof(struct ipv6hdr);
3480 default:
3481 return ~0U;
3482 }
3483 }
3484
3485 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3486 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3487
3488 #define BPF_F_ADJ_ROOM_DECAP_L3_MASK (BPF_F_ADJ_ROOM_DECAP_L3_IPV4 | \
3489 BPF_F_ADJ_ROOM_DECAP_L3_IPV6)
3490
3491 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3492 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3493 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3494 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3495 BPF_F_ADJ_ROOM_ENCAP_L2_ETH | \
3496 BPF_F_ADJ_ROOM_ENCAP_L2( \
3497 BPF_ADJ_ROOM_ENCAP_L2_MASK) | \
3498 BPF_F_ADJ_ROOM_DECAP_L3_MASK)
3499
bpf_skb_net_grow(struct sk_buff * skb,u32 off,u32 len_diff,u64 flags)3500 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3501 u64 flags)
3502 {
3503 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
3504 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
3505 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
3506 unsigned int gso_type = SKB_GSO_DODGY;
3507 int ret;
3508
3509 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3510 /* udp gso_size delineates datagrams, only allow if fixed */
3511 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3512 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3513 return -ENOTSUPP;
3514 }
3515
3516 ret = skb_cow_head(skb, len_diff);
3517 if (unlikely(ret < 0))
3518 return ret;
3519
3520 if (encap) {
3521 if (skb->protocol != htons(ETH_P_IP) &&
3522 skb->protocol != htons(ETH_P_IPV6))
3523 return -ENOTSUPP;
3524
3525 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3526 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3527 return -EINVAL;
3528
3529 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3530 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3531 return -EINVAL;
3532
3533 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH &&
3534 inner_mac_len < ETH_HLEN)
3535 return -EINVAL;
3536
3537 if (skb->encapsulation)
3538 return -EALREADY;
3539
3540 mac_len = skb->network_header - skb->mac_header;
3541 inner_net = skb->network_header;
3542 if (inner_mac_len > len_diff)
3543 return -EINVAL;
3544 inner_trans = skb->transport_header;
3545 }
3546
3547 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3548 if (unlikely(ret < 0))
3549 return ret;
3550
3551 if (encap) {
3552 skb->inner_mac_header = inner_net - inner_mac_len;
3553 skb->inner_network_header = inner_net;
3554 skb->inner_transport_header = inner_trans;
3555
3556 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH)
3557 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
3558 else
3559 skb_set_inner_protocol(skb, skb->protocol);
3560
3561 skb->encapsulation = 1;
3562 skb_set_network_header(skb, mac_len);
3563
3564 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3565 gso_type |= SKB_GSO_UDP_TUNNEL;
3566 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3567 gso_type |= SKB_GSO_GRE;
3568 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3569 gso_type |= SKB_GSO_IPXIP6;
3570 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3571 gso_type |= SKB_GSO_IPXIP4;
3572
3573 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3574 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3575 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3576 sizeof(struct ipv6hdr) :
3577 sizeof(struct iphdr);
3578
3579 skb_set_transport_header(skb, mac_len + nh_len);
3580 }
3581
3582 /* Match skb->protocol to new outer l3 protocol */
3583 if (skb->protocol == htons(ETH_P_IP) &&
3584 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3585 bpf_skb_change_protocol(skb, ETH_P_IPV6);
3586 else if (skb->protocol == htons(ETH_P_IPV6) &&
3587 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3588 bpf_skb_change_protocol(skb, ETH_P_IP);
3589 }
3590
3591 if (skb_is_gso(skb)) {
3592 struct skb_shared_info *shinfo = skb_shinfo(skb);
3593
3594 /* Header must be checked, and gso_segs recomputed. */
3595 shinfo->gso_type |= gso_type;
3596 shinfo->gso_segs = 0;
3597
3598 /* Due to header growth, MSS needs to be downgraded.
3599 * There is a BUG_ON() when segmenting the frag_list with
3600 * head_frag true, so linearize the skb after downgrading
3601 * the MSS.
3602 */
3603 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) {
3604 skb_decrease_gso_size(shinfo, len_diff);
3605 if (shinfo->frag_list)
3606 return skb_linearize(skb);
3607 }
3608 }
3609
3610 return 0;
3611 }
3612
bpf_skb_net_shrink(struct sk_buff * skb,u32 off,u32 len_diff,u64 flags)3613 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3614 u64 flags)
3615 {
3616 int ret;
3617
3618 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3619 BPF_F_ADJ_ROOM_DECAP_L3_MASK |
3620 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3621 return -EINVAL;
3622
3623 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3624 /* udp gso_size delineates datagrams, only allow if fixed */
3625 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3626 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3627 return -ENOTSUPP;
3628 }
3629
3630 ret = skb_unclone(skb, GFP_ATOMIC);
3631 if (unlikely(ret < 0))
3632 return ret;
3633
3634 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3635 if (unlikely(ret < 0))
3636 return ret;
3637
3638 /* Match skb->protocol to new outer l3 protocol */
3639 if (skb->protocol == htons(ETH_P_IP) &&
3640 flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV6)
3641 bpf_skb_change_protocol(skb, ETH_P_IPV6);
3642 else if (skb->protocol == htons(ETH_P_IPV6) &&
3643 flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV4)
3644 bpf_skb_change_protocol(skb, ETH_P_IP);
3645
3646 if (skb_is_gso(skb)) {
3647 struct skb_shared_info *shinfo = skb_shinfo(skb);
3648
3649 /* Due to header shrink, MSS can be upgraded. */
3650 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3651 skb_increase_gso_size(shinfo, len_diff);
3652
3653 /* Header must be checked, and gso_segs recomputed. */
3654 shinfo->gso_type |= SKB_GSO_DODGY;
3655 shinfo->gso_segs = 0;
3656 }
3657
3658 return 0;
3659 }
3660
3661 #define BPF_SKB_MAX_LEN SKB_MAX_ALLOC
3662
BPF_CALL_4(sk_skb_adjust_room,struct sk_buff *,skb,s32,len_diff,u32,mode,u64,flags)3663 BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3664 u32, mode, u64, flags)
3665 {
3666 u32 len_diff_abs = abs(len_diff);
3667 bool shrink = len_diff < 0;
3668 int ret = 0;
3669
3670 if (unlikely(flags || mode))
3671 return -EINVAL;
3672 if (unlikely(len_diff_abs > 0xfffU))
3673 return -EFAULT;
3674
3675 if (!shrink) {
3676 ret = skb_cow(skb, len_diff);
3677 if (unlikely(ret < 0))
3678 return ret;
3679 __skb_push(skb, len_diff_abs);
3680 memset(skb->data, 0, len_diff_abs);
3681 } else {
3682 if (unlikely(!pskb_may_pull(skb, len_diff_abs)))
3683 return -ENOMEM;
3684 __skb_pull(skb, len_diff_abs);
3685 }
3686 if (tls_sw_has_ctx_rx(skb->sk)) {
3687 struct strp_msg *rxm = strp_msg(skb);
3688
3689 rxm->full_len += len_diff;
3690 }
3691 return ret;
3692 }
3693
3694 static const struct bpf_func_proto sk_skb_adjust_room_proto = {
3695 .func = sk_skb_adjust_room,
3696 .gpl_only = false,
3697 .ret_type = RET_INTEGER,
3698 .arg1_type = ARG_PTR_TO_CTX,
3699 .arg2_type = ARG_ANYTHING,
3700 .arg3_type = ARG_ANYTHING,
3701 .arg4_type = ARG_ANYTHING,
3702 };
3703
BPF_CALL_4(bpf_skb_adjust_room,struct sk_buff *,skb,s32,len_diff,u32,mode,u64,flags)3704 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3705 u32, mode, u64, flags)
3706 {
3707 u32 len_cur, len_diff_abs = abs(len_diff);
3708 u32 len_min = bpf_skb_net_base_len(skb);
3709 u32 len_max = BPF_SKB_MAX_LEN;
3710 __be16 proto = skb->protocol;
3711 bool shrink = len_diff < 0;
3712 u32 off;
3713 int ret;
3714
3715 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3716 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3717 return -EINVAL;
3718 if (unlikely(len_diff_abs > 0xfffU))
3719 return -EFAULT;
3720 if (unlikely(proto != htons(ETH_P_IP) &&
3721 proto != htons(ETH_P_IPV6)))
3722 return -ENOTSUPP;
3723
3724 off = skb_mac_header_len(skb);
3725 switch (mode) {
3726 case BPF_ADJ_ROOM_NET:
3727 off += bpf_skb_net_base_len(skb);
3728 break;
3729 case BPF_ADJ_ROOM_MAC:
3730 break;
3731 default:
3732 return -ENOTSUPP;
3733 }
3734
3735 if (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) {
3736 if (!shrink)
3737 return -EINVAL;
3738
3739 switch (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) {
3740 case BPF_F_ADJ_ROOM_DECAP_L3_IPV4:
3741 len_min = sizeof(struct iphdr);
3742 break;
3743 case BPF_F_ADJ_ROOM_DECAP_L3_IPV6:
3744 len_min = sizeof(struct ipv6hdr);
3745 break;
3746 default:
3747 return -EINVAL;
3748 }
3749 }
3750
3751 len_cur = skb->len - skb_network_offset(skb);
3752 if ((shrink && (len_diff_abs >= len_cur ||
3753 len_cur - len_diff_abs < len_min)) ||
3754 (!shrink && (skb->len + len_diff_abs > len_max &&
3755 !skb_is_gso(skb))))
3756 return -ENOTSUPP;
3757
3758 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3759 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3760 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3761 __skb_reset_checksum_unnecessary(skb);
3762
3763 bpf_compute_data_pointers(skb);
3764 return ret;
3765 }
3766
3767 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3768 .func = bpf_skb_adjust_room,
3769 .gpl_only = false,
3770 .ret_type = RET_INTEGER,
3771 .arg1_type = ARG_PTR_TO_CTX,
3772 .arg2_type = ARG_ANYTHING,
3773 .arg3_type = ARG_ANYTHING,
3774 .arg4_type = ARG_ANYTHING,
3775 };
3776
__bpf_skb_min_len(const struct sk_buff * skb)3777 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3778 {
3779 int offset = skb_network_offset(skb);
3780 u32 min_len = 0;
3781
3782 if (offset > 0)
3783 min_len = offset;
3784 if (skb_transport_header_was_set(skb)) {
3785 offset = skb_transport_offset(skb);
3786 if (offset > 0)
3787 min_len = offset;
3788 }
3789 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3790 offset = skb_checksum_start_offset(skb) +
3791 skb->csum_offset + sizeof(__sum16);
3792 if (offset > 0)
3793 min_len = offset;
3794 }
3795 return min_len;
3796 }
3797
bpf_skb_grow_rcsum(struct sk_buff * skb,unsigned int new_len)3798 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3799 {
3800 unsigned int old_len = skb->len;
3801 int ret;
3802
3803 ret = __skb_grow_rcsum(skb, new_len);
3804 if (!ret)
3805 memset(skb->data + old_len, 0, new_len - old_len);
3806 return ret;
3807 }
3808
bpf_skb_trim_rcsum(struct sk_buff * skb,unsigned int new_len)3809 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3810 {
3811 return __skb_trim_rcsum(skb, new_len);
3812 }
3813
__bpf_skb_change_tail(struct sk_buff * skb,u32 new_len,u64 flags)3814 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3815 u64 flags)
3816 {
3817 u32 max_len = BPF_SKB_MAX_LEN;
3818 u32 min_len = __bpf_skb_min_len(skb);
3819 int ret;
3820
3821 if (unlikely(flags || new_len > max_len || new_len < min_len))
3822 return -EINVAL;
3823 if (skb->encapsulation)
3824 return -ENOTSUPP;
3825
3826 /* The basic idea of this helper is that it's performing the
3827 * needed work to either grow or trim an skb, and eBPF program
3828 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3829 * bpf_lX_csum_replace() and others rather than passing a raw
3830 * buffer here. This one is a slow path helper and intended
3831 * for replies with control messages.
3832 *
3833 * Like in bpf_skb_change_proto(), we want to keep this rather
3834 * minimal and without protocol specifics so that we are able
3835 * to separate concerns as in bpf_skb_store_bytes() should only
3836 * be the one responsible for writing buffers.
3837 *
3838 * It's really expected to be a slow path operation here for
3839 * control message replies, so we're implicitly linearizing,
3840 * uncloning and drop offloads from the skb by this.
3841 */
3842 ret = __bpf_try_make_writable(skb, skb->len);
3843 if (!ret) {
3844 if (new_len > skb->len)
3845 ret = bpf_skb_grow_rcsum(skb, new_len);
3846 else if (new_len < skb->len)
3847 ret = bpf_skb_trim_rcsum(skb, new_len);
3848 if (!ret && skb_is_gso(skb))
3849 skb_gso_reset(skb);
3850 }
3851 return ret;
3852 }
3853
BPF_CALL_3(bpf_skb_change_tail,struct sk_buff *,skb,u32,new_len,u64,flags)3854 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3855 u64, flags)
3856 {
3857 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3858
3859 bpf_compute_data_pointers(skb);
3860 return ret;
3861 }
3862
3863 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3864 .func = bpf_skb_change_tail,
3865 .gpl_only = false,
3866 .ret_type = RET_INTEGER,
3867 .arg1_type = ARG_PTR_TO_CTX,
3868 .arg2_type = ARG_ANYTHING,
3869 .arg3_type = ARG_ANYTHING,
3870 };
3871
BPF_CALL_3(sk_skb_change_tail,struct sk_buff *,skb,u32,new_len,u64,flags)3872 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3873 u64, flags)
3874 {
3875 return __bpf_skb_change_tail(skb, new_len, flags);
3876 }
3877
3878 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3879 .func = sk_skb_change_tail,
3880 .gpl_only = false,
3881 .ret_type = RET_INTEGER,
3882 .arg1_type = ARG_PTR_TO_CTX,
3883 .arg2_type = ARG_ANYTHING,
3884 .arg3_type = ARG_ANYTHING,
3885 };
3886
__bpf_skb_change_head(struct sk_buff * skb,u32 head_room,u64 flags)3887 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3888 u64 flags)
3889 {
3890 u32 max_len = BPF_SKB_MAX_LEN;
3891 u32 new_len = skb->len + head_room;
3892 int ret;
3893
3894 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3895 new_len < skb->len))
3896 return -EINVAL;
3897
3898 ret = skb_cow(skb, head_room);
3899 if (likely(!ret)) {
3900 /* Idea for this helper is that we currently only
3901 * allow to expand on mac header. This means that
3902 * skb->protocol network header, etc, stay as is.
3903 * Compared to bpf_skb_change_tail(), we're more
3904 * flexible due to not needing to linearize or
3905 * reset GSO. Intention for this helper is to be
3906 * used by an L3 skb that needs to push mac header
3907 * for redirection into L2 device.
3908 */
3909 __skb_push(skb, head_room);
3910 memset(skb->data, 0, head_room);
3911 skb_reset_mac_header(skb);
3912 skb_reset_mac_len(skb);
3913 }
3914
3915 return ret;
3916 }
3917
BPF_CALL_3(bpf_skb_change_head,struct sk_buff *,skb,u32,head_room,u64,flags)3918 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3919 u64, flags)
3920 {
3921 int ret = __bpf_skb_change_head(skb, head_room, flags);
3922
3923 bpf_compute_data_pointers(skb);
3924 return ret;
3925 }
3926
3927 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3928 .func = bpf_skb_change_head,
3929 .gpl_only = false,
3930 .ret_type = RET_INTEGER,
3931 .arg1_type = ARG_PTR_TO_CTX,
3932 .arg2_type = ARG_ANYTHING,
3933 .arg3_type = ARG_ANYTHING,
3934 };
3935
BPF_CALL_3(sk_skb_change_head,struct sk_buff *,skb,u32,head_room,u64,flags)3936 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3937 u64, flags)
3938 {
3939 return __bpf_skb_change_head(skb, head_room, flags);
3940 }
3941
3942 static const struct bpf_func_proto sk_skb_change_head_proto = {
3943 .func = sk_skb_change_head,
3944 .gpl_only = false,
3945 .ret_type = RET_INTEGER,
3946 .arg1_type = ARG_PTR_TO_CTX,
3947 .arg2_type = ARG_ANYTHING,
3948 .arg3_type = ARG_ANYTHING,
3949 };
3950
BPF_CALL_1(bpf_xdp_get_buff_len,struct xdp_buff *,xdp)3951 BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp)
3952 {
3953 return xdp_get_buff_len(xdp);
3954 }
3955
3956 static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = {
3957 .func = bpf_xdp_get_buff_len,
3958 .gpl_only = false,
3959 .ret_type = RET_INTEGER,
3960 .arg1_type = ARG_PTR_TO_CTX,
3961 };
3962
3963 BTF_ID_LIST_SINGLE(bpf_xdp_get_buff_len_bpf_ids, struct, xdp_buff)
3964
3965 const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto = {
3966 .func = bpf_xdp_get_buff_len,
3967 .gpl_only = false,
3968 .arg1_type = ARG_PTR_TO_BTF_ID,
3969 .arg1_btf_id = &bpf_xdp_get_buff_len_bpf_ids[0],
3970 };
3971
xdp_get_metalen(const struct xdp_buff * xdp)3972 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3973 {
3974 return xdp_data_meta_unsupported(xdp) ? 0 :
3975 xdp->data - xdp->data_meta;
3976 }
3977
BPF_CALL_2(bpf_xdp_adjust_head,struct xdp_buff *,xdp,int,offset)3978 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3979 {
3980 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3981 unsigned long metalen = xdp_get_metalen(xdp);
3982 void *data_start = xdp_frame_end + metalen;
3983 void *data = xdp->data + offset;
3984
3985 if (unlikely(data < data_start ||
3986 data > xdp->data_end - ETH_HLEN))
3987 return -EINVAL;
3988
3989 if (metalen)
3990 memmove(xdp->data_meta + offset,
3991 xdp->data_meta, metalen);
3992 xdp->data_meta += offset;
3993 xdp->data = data;
3994
3995 return 0;
3996 }
3997
3998 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3999 .func = bpf_xdp_adjust_head,
4000 .gpl_only = false,
4001 .ret_type = RET_INTEGER,
4002 .arg1_type = ARG_PTR_TO_CTX,
4003 .arg2_type = ARG_ANYTHING,
4004 };
4005
bpf_xdp_copy_buf(struct xdp_buff * xdp,unsigned long off,void * buf,unsigned long len,bool flush)4006 void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
4007 void *buf, unsigned long len, bool flush)
4008 {
4009 unsigned long ptr_len, ptr_off = 0;
4010 skb_frag_t *next_frag, *end_frag;
4011 struct skb_shared_info *sinfo;
4012 void *src, *dst;
4013 u8 *ptr_buf;
4014
4015 if (likely(xdp->data_end - xdp->data >= off + len)) {
4016 src = flush ? buf : xdp->data + off;
4017 dst = flush ? xdp->data + off : buf;
4018 memcpy(dst, src, len);
4019 return;
4020 }
4021
4022 sinfo = xdp_get_shared_info_from_buff(xdp);
4023 end_frag = &sinfo->frags[sinfo->nr_frags];
4024 next_frag = &sinfo->frags[0];
4025
4026 ptr_len = xdp->data_end - xdp->data;
4027 ptr_buf = xdp->data;
4028
4029 while (true) {
4030 if (off < ptr_off + ptr_len) {
4031 unsigned long copy_off = off - ptr_off;
4032 unsigned long copy_len = min(len, ptr_len - copy_off);
4033
4034 src = flush ? buf : ptr_buf + copy_off;
4035 dst = flush ? ptr_buf + copy_off : buf;
4036 memcpy(dst, src, copy_len);
4037
4038 off += copy_len;
4039 len -= copy_len;
4040 buf += copy_len;
4041 }
4042
4043 if (!len || next_frag == end_frag)
4044 break;
4045
4046 ptr_off += ptr_len;
4047 ptr_buf = skb_frag_address(next_frag);
4048 ptr_len = skb_frag_size(next_frag);
4049 next_frag++;
4050 }
4051 }
4052
bpf_xdp_pointer(struct xdp_buff * xdp,u32 offset,u32 len)4053 void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
4054 {
4055 u32 size = xdp->data_end - xdp->data;
4056 struct skb_shared_info *sinfo;
4057 void *addr = xdp->data;
4058 int i;
4059
4060 if (unlikely(offset > 0xffff || len > 0xffff))
4061 return ERR_PTR(-EFAULT);
4062
4063 if (unlikely(offset + len > xdp_get_buff_len(xdp)))
4064 return ERR_PTR(-EINVAL);
4065
4066 if (likely(offset < size)) /* linear area */
4067 goto out;
4068
4069 sinfo = xdp_get_shared_info_from_buff(xdp);
4070 offset -= size;
4071 for (i = 0; i < sinfo->nr_frags; i++) { /* paged area */
4072 u32 frag_size = skb_frag_size(&sinfo->frags[i]);
4073
4074 if (offset < frag_size) {
4075 addr = skb_frag_address(&sinfo->frags[i]);
4076 size = frag_size;
4077 break;
4078 }
4079 offset -= frag_size;
4080 }
4081 out:
4082 return offset + len <= size ? addr + offset : NULL;
4083 }
4084
BPF_CALL_4(bpf_xdp_load_bytes,struct xdp_buff *,xdp,u32,offset,void *,buf,u32,len)4085 BPF_CALL_4(bpf_xdp_load_bytes, struct xdp_buff *, xdp, u32, offset,
4086 void *, buf, u32, len)
4087 {
4088 void *ptr;
4089
4090 ptr = bpf_xdp_pointer(xdp, offset, len);
4091 if (IS_ERR(ptr))
4092 return PTR_ERR(ptr);
4093
4094 if (!ptr)
4095 bpf_xdp_copy_buf(xdp, offset, buf, len, false);
4096 else
4097 memcpy(buf, ptr, len);
4098
4099 return 0;
4100 }
4101
4102 static const struct bpf_func_proto bpf_xdp_load_bytes_proto = {
4103 .func = bpf_xdp_load_bytes,
4104 .gpl_only = false,
4105 .ret_type = RET_INTEGER,
4106 .arg1_type = ARG_PTR_TO_CTX,
4107 .arg2_type = ARG_ANYTHING,
4108 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4109 .arg4_type = ARG_CONST_SIZE,
4110 };
4111
__bpf_xdp_load_bytes(struct xdp_buff * xdp,u32 offset,void * buf,u32 len)4112 int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len)
4113 {
4114 return ____bpf_xdp_load_bytes(xdp, offset, buf, len);
4115 }
4116
BPF_CALL_4(bpf_xdp_store_bytes,struct xdp_buff *,xdp,u32,offset,void *,buf,u32,len)4117 BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset,
4118 void *, buf, u32, len)
4119 {
4120 void *ptr;
4121
4122 ptr = bpf_xdp_pointer(xdp, offset, len);
4123 if (IS_ERR(ptr))
4124 return PTR_ERR(ptr);
4125
4126 if (!ptr)
4127 bpf_xdp_copy_buf(xdp, offset, buf, len, true);
4128 else
4129 memcpy(ptr, buf, len);
4130
4131 return 0;
4132 }
4133
4134 static const struct bpf_func_proto bpf_xdp_store_bytes_proto = {
4135 .func = bpf_xdp_store_bytes,
4136 .gpl_only = false,
4137 .ret_type = RET_INTEGER,
4138 .arg1_type = ARG_PTR_TO_CTX,
4139 .arg2_type = ARG_ANYTHING,
4140 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4141 .arg4_type = ARG_CONST_SIZE,
4142 };
4143
__bpf_xdp_store_bytes(struct xdp_buff * xdp,u32 offset,void * buf,u32 len)4144 int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len)
4145 {
4146 return ____bpf_xdp_store_bytes(xdp, offset, buf, len);
4147 }
4148
bpf_xdp_frags_increase_tail(struct xdp_buff * xdp,int offset)4149 static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
4150 {
4151 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
4152 skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags - 1];
4153 struct xdp_rxq_info *rxq = xdp->rxq;
4154 unsigned int tailroom;
4155
4156 if (!rxq->frag_size || rxq->frag_size > xdp->frame_sz)
4157 return -EOPNOTSUPP;
4158
4159 tailroom = rxq->frag_size - skb_frag_size(frag) - skb_frag_off(frag);
4160 if (unlikely(offset > tailroom))
4161 return -EINVAL;
4162
4163 memset(skb_frag_address(frag) + skb_frag_size(frag), 0, offset);
4164 skb_frag_size_add(frag, offset);
4165 sinfo->xdp_frags_size += offset;
4166 if (rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
4167 xsk_buff_get_tail(xdp)->data_end += offset;
4168
4169 return 0;
4170 }
4171
bpf_xdp_shrink_data_zc(struct xdp_buff * xdp,int shrink,struct xdp_mem_info * mem_info,bool release)4172 static void bpf_xdp_shrink_data_zc(struct xdp_buff *xdp, int shrink,
4173 struct xdp_mem_info *mem_info, bool release)
4174 {
4175 struct xdp_buff *zc_frag = xsk_buff_get_tail(xdp);
4176
4177 if (release) {
4178 xsk_buff_del_tail(zc_frag);
4179 __xdp_return(NULL, mem_info, false, zc_frag);
4180 } else {
4181 zc_frag->data_end -= shrink;
4182 }
4183 }
4184
bpf_xdp_shrink_data(struct xdp_buff * xdp,skb_frag_t * frag,int shrink)4185 static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,
4186 int shrink)
4187 {
4188 struct xdp_mem_info *mem_info = &xdp->rxq->mem;
4189 bool release = skb_frag_size(frag) == shrink;
4190
4191 if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL) {
4192 bpf_xdp_shrink_data_zc(xdp, shrink, mem_info, release);
4193 goto out;
4194 }
4195
4196 if (release) {
4197 struct page *page = skb_frag_page(frag);
4198
4199 __xdp_return(page_address(page), mem_info, false, NULL);
4200 }
4201
4202 out:
4203 return release;
4204 }
4205
bpf_xdp_frags_shrink_tail(struct xdp_buff * xdp,int offset)4206 static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
4207 {
4208 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
4209 int i, n_frags_free = 0, len_free = 0;
4210
4211 if (unlikely(offset > (int)xdp_get_buff_len(xdp) - ETH_HLEN))
4212 return -EINVAL;
4213
4214 for (i = sinfo->nr_frags - 1; i >= 0 && offset > 0; i--) {
4215 skb_frag_t *frag = &sinfo->frags[i];
4216 int shrink = min_t(int, offset, skb_frag_size(frag));
4217
4218 len_free += shrink;
4219 offset -= shrink;
4220 if (bpf_xdp_shrink_data(xdp, frag, shrink)) {
4221 n_frags_free++;
4222 } else {
4223 skb_frag_size_sub(frag, shrink);
4224 break;
4225 }
4226 }
4227 sinfo->nr_frags -= n_frags_free;
4228 sinfo->xdp_frags_size -= len_free;
4229
4230 if (unlikely(!sinfo->nr_frags)) {
4231 xdp_buff_clear_frags_flag(xdp);
4232 xdp->data_end -= offset;
4233 }
4234
4235 return 0;
4236 }
4237
BPF_CALL_2(bpf_xdp_adjust_tail,struct xdp_buff *,xdp,int,offset)4238 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
4239 {
4240 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */
4241 void *data_end = xdp->data_end + offset;
4242
4243 if (unlikely(xdp_buff_has_frags(xdp))) { /* non-linear xdp buff */
4244 if (offset < 0)
4245 return bpf_xdp_frags_shrink_tail(xdp, -offset);
4246
4247 return bpf_xdp_frags_increase_tail(xdp, offset);
4248 }
4249
4250 /* Notice that xdp_data_hard_end have reserved some tailroom */
4251 if (unlikely(data_end > data_hard_end))
4252 return -EINVAL;
4253
4254 if (unlikely(data_end < xdp->data + ETH_HLEN))
4255 return -EINVAL;
4256
4257 /* Clear memory area on grow, can contain uninit kernel memory */
4258 if (offset > 0)
4259 memset(xdp->data_end, 0, offset);
4260
4261 xdp->data_end = data_end;
4262
4263 return 0;
4264 }
4265
4266 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
4267 .func = bpf_xdp_adjust_tail,
4268 .gpl_only = false,
4269 .ret_type = RET_INTEGER,
4270 .arg1_type = ARG_PTR_TO_CTX,
4271 .arg2_type = ARG_ANYTHING,
4272 };
4273
BPF_CALL_2(bpf_xdp_adjust_meta,struct xdp_buff *,xdp,int,offset)4274 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
4275 {
4276 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
4277 void *meta = xdp->data_meta + offset;
4278 unsigned long metalen = xdp->data - meta;
4279
4280 if (xdp_data_meta_unsupported(xdp))
4281 return -ENOTSUPP;
4282 if (unlikely(meta < xdp_frame_end ||
4283 meta > xdp->data))
4284 return -EINVAL;
4285 if (unlikely(xdp_metalen_invalid(metalen)))
4286 return -EACCES;
4287
4288 xdp->data_meta = meta;
4289
4290 return 0;
4291 }
4292
4293 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
4294 .func = bpf_xdp_adjust_meta,
4295 .gpl_only = false,
4296 .ret_type = RET_INTEGER,
4297 .arg1_type = ARG_PTR_TO_CTX,
4298 .arg2_type = ARG_ANYTHING,
4299 };
4300
4301 /**
4302 * DOC: xdp redirect
4303 *
4304 * XDP_REDIRECT works by a three-step process, implemented in the functions
4305 * below:
4306 *
4307 * 1. The bpf_redirect() and bpf_redirect_map() helpers will lookup the target
4308 * of the redirect and store it (along with some other metadata) in a per-CPU
4309 * struct bpf_redirect_info.
4310 *
4311 * 2. When the program returns the XDP_REDIRECT return code, the driver will
4312 * call xdp_do_redirect() which will use the information in struct
4313 * bpf_redirect_info to actually enqueue the frame into a map type-specific
4314 * bulk queue structure.
4315 *
4316 * 3. Before exiting its NAPI poll loop, the driver will call
4317 * xdp_do_flush(), which will flush all the different bulk queues,
4318 * thus completing the redirect. Note that xdp_do_flush() must be
4319 * called before napi_complete_done() in the driver, as the
4320 * XDP_REDIRECT logic relies on being inside a single NAPI instance
4321 * through to the xdp_do_flush() call for RCU protection of all
4322 * in-kernel data structures.
4323 */
4324 /*
4325 * Pointers to the map entries will be kept around for this whole sequence of
4326 * steps, protected by RCU. However, there is no top-level rcu_read_lock() in
4327 * the core code; instead, the RCU protection relies on everything happening
4328 * inside a single NAPI poll sequence, which means it's between a pair of calls
4329 * to local_bh_disable()/local_bh_enable().
4330 *
4331 * The map entries are marked as __rcu and the map code makes sure to
4332 * dereference those pointers with rcu_dereference_check() in a way that works
4333 * for both sections that to hold an rcu_read_lock() and sections that are
4334 * called from NAPI without a separate rcu_read_lock(). The code below does not
4335 * use RCU annotations, but relies on those in the map code.
4336 */
xdp_do_flush(void)4337 void xdp_do_flush(void)
4338 {
4339 struct list_head *lh_map, *lh_dev, *lh_xsk;
4340
4341 bpf_net_ctx_get_all_used_flush_lists(&lh_map, &lh_dev, &lh_xsk);
4342 if (lh_dev)
4343 __dev_flush(lh_dev);
4344 if (lh_map)
4345 __cpu_map_flush(lh_map);
4346 if (lh_xsk)
4347 __xsk_map_flush(lh_xsk);
4348 }
4349 EXPORT_SYMBOL_GPL(xdp_do_flush);
4350
4351 #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
xdp_do_check_flushed(struct napi_struct * napi)4352 void xdp_do_check_flushed(struct napi_struct *napi)
4353 {
4354 struct list_head *lh_map, *lh_dev, *lh_xsk;
4355 bool missed = false;
4356
4357 bpf_net_ctx_get_all_used_flush_lists(&lh_map, &lh_dev, &lh_xsk);
4358 if (lh_dev) {
4359 __dev_flush(lh_dev);
4360 missed = true;
4361 }
4362 if (lh_map) {
4363 __cpu_map_flush(lh_map);
4364 missed = true;
4365 }
4366 if (lh_xsk) {
4367 __xsk_map_flush(lh_xsk);
4368 missed = true;
4369 }
4370
4371 WARN_ONCE(missed, "Missing xdp_do_flush() invocation after NAPI by %ps\n",
4372 napi->poll);
4373 }
4374 #endif
4375
4376 DEFINE_STATIC_KEY_FALSE(bpf_master_redirect_enabled_key);
4377 EXPORT_SYMBOL_GPL(bpf_master_redirect_enabled_key);
4378
xdp_master_redirect(struct xdp_buff * xdp)4379 u32 xdp_master_redirect(struct xdp_buff *xdp)
4380 {
4381 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4382 struct net_device *master, *slave;
4383
4384 master = netdev_master_upper_dev_get_rcu(xdp->rxq->dev);
4385 slave = master->netdev_ops->ndo_xdp_get_xmit_slave(master, xdp);
4386 if (slave && slave != xdp->rxq->dev) {
4387 /* The target device is different from the receiving device, so
4388 * redirect it to the new device.
4389 * Using XDP_REDIRECT gets the correct behaviour from XDP enabled
4390 * drivers to unmap the packet from their rx ring.
4391 */
4392 ri->tgt_index = slave->ifindex;
4393 ri->map_id = INT_MAX;
4394 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4395 return XDP_REDIRECT;
4396 }
4397 return XDP_TX;
4398 }
4399 EXPORT_SYMBOL_GPL(xdp_master_redirect);
4400
__xdp_do_redirect_xsk(struct bpf_redirect_info * ri,struct net_device * dev,struct xdp_buff * xdp,struct bpf_prog * xdp_prog)4401 static inline int __xdp_do_redirect_xsk(struct bpf_redirect_info *ri,
4402 struct net_device *dev,
4403 struct xdp_buff *xdp,
4404 struct bpf_prog *xdp_prog)
4405 {
4406 enum bpf_map_type map_type = ri->map_type;
4407 void *fwd = ri->tgt_value;
4408 u32 map_id = ri->map_id;
4409 int err;
4410
4411 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4412 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4413
4414 err = __xsk_map_redirect(fwd, xdp);
4415 if (unlikely(err))
4416 goto err;
4417
4418 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4419 return 0;
4420 err:
4421 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4422 return err;
4423 }
4424
__xdp_do_redirect_frame(struct bpf_redirect_info * ri,struct net_device * dev,struct xdp_frame * xdpf,struct bpf_prog * xdp_prog)4425 static __always_inline int __xdp_do_redirect_frame(struct bpf_redirect_info *ri,
4426 struct net_device *dev,
4427 struct xdp_frame *xdpf,
4428 struct bpf_prog *xdp_prog)
4429 {
4430 enum bpf_map_type map_type = ri->map_type;
4431 void *fwd = ri->tgt_value;
4432 u32 map_id = ri->map_id;
4433 u32 flags = ri->flags;
4434 struct bpf_map *map;
4435 int err;
4436
4437 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4438 ri->flags = 0;
4439 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4440
4441 if (unlikely(!xdpf)) {
4442 err = -EOVERFLOW;
4443 goto err;
4444 }
4445
4446 switch (map_type) {
4447 case BPF_MAP_TYPE_DEVMAP:
4448 fallthrough;
4449 case BPF_MAP_TYPE_DEVMAP_HASH:
4450 if (unlikely(flags & BPF_F_BROADCAST)) {
4451 map = READ_ONCE(ri->map);
4452
4453 /* The map pointer is cleared when the map is being torn
4454 * down by dev_map_free()
4455 */
4456 if (unlikely(!map)) {
4457 err = -ENOENT;
4458 break;
4459 }
4460
4461 WRITE_ONCE(ri->map, NULL);
4462 err = dev_map_enqueue_multi(xdpf, dev, map,
4463 flags & BPF_F_EXCLUDE_INGRESS);
4464 } else {
4465 err = dev_map_enqueue(fwd, xdpf, dev);
4466 }
4467 break;
4468 case BPF_MAP_TYPE_CPUMAP:
4469 err = cpu_map_enqueue(fwd, xdpf, dev);
4470 break;
4471 case BPF_MAP_TYPE_UNSPEC:
4472 if (map_id == INT_MAX) {
4473 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4474 if (unlikely(!fwd)) {
4475 err = -EINVAL;
4476 break;
4477 }
4478 err = dev_xdp_enqueue(fwd, xdpf, dev);
4479 break;
4480 }
4481 fallthrough;
4482 default:
4483 err = -EBADRQC;
4484 }
4485
4486 if (unlikely(err))
4487 goto err;
4488
4489 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4490 return 0;
4491 err:
4492 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4493 return err;
4494 }
4495
xdp_do_redirect(struct net_device * dev,struct xdp_buff * xdp,struct bpf_prog * xdp_prog)4496 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
4497 struct bpf_prog *xdp_prog)
4498 {
4499 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4500 enum bpf_map_type map_type = ri->map_type;
4501
4502 if (map_type == BPF_MAP_TYPE_XSKMAP)
4503 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4504
4505 return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp),
4506 xdp_prog);
4507 }
4508 EXPORT_SYMBOL_GPL(xdp_do_redirect);
4509
xdp_do_redirect_frame(struct net_device * dev,struct xdp_buff * xdp,struct xdp_frame * xdpf,struct bpf_prog * xdp_prog)4510 int xdp_do_redirect_frame(struct net_device *dev, struct xdp_buff *xdp,
4511 struct xdp_frame *xdpf, struct bpf_prog *xdp_prog)
4512 {
4513 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4514 enum bpf_map_type map_type = ri->map_type;
4515
4516 if (map_type == BPF_MAP_TYPE_XSKMAP)
4517 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4518
4519 return __xdp_do_redirect_frame(ri, dev, xdpf, xdp_prog);
4520 }
4521 EXPORT_SYMBOL_GPL(xdp_do_redirect_frame);
4522
xdp_do_generic_redirect_map(struct net_device * dev,struct sk_buff * skb,struct xdp_buff * xdp,struct bpf_prog * xdp_prog,void * fwd,enum bpf_map_type map_type,u32 map_id,u32 flags)4523 static int xdp_do_generic_redirect_map(struct net_device *dev,
4524 struct sk_buff *skb,
4525 struct xdp_buff *xdp,
4526 struct bpf_prog *xdp_prog, void *fwd,
4527 enum bpf_map_type map_type, u32 map_id,
4528 u32 flags)
4529 {
4530 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4531 struct bpf_map *map;
4532 int err;
4533
4534 switch (map_type) {
4535 case BPF_MAP_TYPE_DEVMAP:
4536 fallthrough;
4537 case BPF_MAP_TYPE_DEVMAP_HASH:
4538 if (unlikely(flags & BPF_F_BROADCAST)) {
4539 map = READ_ONCE(ri->map);
4540
4541 /* The map pointer is cleared when the map is being torn
4542 * down by dev_map_free()
4543 */
4544 if (unlikely(!map)) {
4545 err = -ENOENT;
4546 break;
4547 }
4548
4549 WRITE_ONCE(ri->map, NULL);
4550 err = dev_map_redirect_multi(dev, skb, xdp_prog, map,
4551 flags & BPF_F_EXCLUDE_INGRESS);
4552 } else {
4553 err = dev_map_generic_redirect(fwd, skb, xdp_prog);
4554 }
4555 if (unlikely(err))
4556 goto err;
4557 break;
4558 case BPF_MAP_TYPE_XSKMAP:
4559 err = xsk_generic_rcv(fwd, xdp);
4560 if (err)
4561 goto err;
4562 consume_skb(skb);
4563 break;
4564 case BPF_MAP_TYPE_CPUMAP:
4565 err = cpu_map_generic_redirect(fwd, skb);
4566 if (unlikely(err))
4567 goto err;
4568 break;
4569 default:
4570 err = -EBADRQC;
4571 goto err;
4572 }
4573
4574 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4575 return 0;
4576 err:
4577 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4578 return err;
4579 }
4580
xdp_do_generic_redirect(struct net_device * dev,struct sk_buff * skb,struct xdp_buff * xdp,struct bpf_prog * xdp_prog)4581 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
4582 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
4583 {
4584 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4585 enum bpf_map_type map_type = ri->map_type;
4586 void *fwd = ri->tgt_value;
4587 u32 map_id = ri->map_id;
4588 u32 flags = ri->flags;
4589 int err;
4590
4591 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4592 ri->flags = 0;
4593 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4594
4595 if (map_type == BPF_MAP_TYPE_UNSPEC && map_id == INT_MAX) {
4596 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4597 if (unlikely(!fwd)) {
4598 err = -EINVAL;
4599 goto err;
4600 }
4601
4602 err = xdp_ok_fwd_dev(fwd, skb->len);
4603 if (unlikely(err))
4604 goto err;
4605
4606 skb->dev = fwd;
4607 _trace_xdp_redirect(dev, xdp_prog, ri->tgt_index);
4608 generic_xdp_tx(skb, xdp_prog);
4609 return 0;
4610 }
4611
4612 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, fwd, map_type, map_id, flags);
4613 err:
4614 _trace_xdp_redirect_err(dev, xdp_prog, ri->tgt_index, err);
4615 return err;
4616 }
4617
BPF_CALL_2(bpf_xdp_redirect,u32,ifindex,u64,flags)4618 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
4619 {
4620 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4621
4622 if (unlikely(flags))
4623 return XDP_ABORTED;
4624
4625 /* NB! Map type UNSPEC and map_id == INT_MAX (never generated
4626 * by map_idr) is used for ifindex based XDP redirect.
4627 */
4628 ri->tgt_index = ifindex;
4629 ri->map_id = INT_MAX;
4630 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4631
4632 return XDP_REDIRECT;
4633 }
4634
4635 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
4636 .func = bpf_xdp_redirect,
4637 .gpl_only = false,
4638 .ret_type = RET_INTEGER,
4639 .arg1_type = ARG_ANYTHING,
4640 .arg2_type = ARG_ANYTHING,
4641 };
4642
BPF_CALL_3(bpf_xdp_redirect_map,struct bpf_map *,map,u64,key,u64,flags)4643 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u64, key,
4644 u64, flags)
4645 {
4646 return map->ops->map_redirect(map, key, flags);
4647 }
4648
4649 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
4650 .func = bpf_xdp_redirect_map,
4651 .gpl_only = false,
4652 .ret_type = RET_INTEGER,
4653 .arg1_type = ARG_CONST_MAP_PTR,
4654 .arg2_type = ARG_ANYTHING,
4655 .arg3_type = ARG_ANYTHING,
4656 };
4657
bpf_skb_copy(void * dst_buff,const void * skb,unsigned long off,unsigned long len)4658 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
4659 unsigned long off, unsigned long len)
4660 {
4661 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
4662
4663 if (unlikely(!ptr))
4664 return len;
4665 if (ptr != dst_buff)
4666 memcpy(dst_buff, ptr, len);
4667
4668 return 0;
4669 }
4670
BPF_CALL_5(bpf_skb_event_output,struct sk_buff *,skb,struct bpf_map *,map,u64,flags,void *,meta,u64,meta_size)4671 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
4672 u64, flags, void *, meta, u64, meta_size)
4673 {
4674 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4675
4676 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4677 return -EINVAL;
4678 if (unlikely(!skb || skb_size > skb->len))
4679 return -EFAULT;
4680
4681 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
4682 bpf_skb_copy);
4683 }
4684
4685 static const struct bpf_func_proto bpf_skb_event_output_proto = {
4686 .func = bpf_skb_event_output,
4687 .gpl_only = true,
4688 .ret_type = RET_INTEGER,
4689 .arg1_type = ARG_PTR_TO_CTX,
4690 .arg2_type = ARG_CONST_MAP_PTR,
4691 .arg3_type = ARG_ANYTHING,
4692 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4693 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4694 };
4695
4696 BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff)
4697
4698 const struct bpf_func_proto bpf_skb_output_proto = {
4699 .func = bpf_skb_event_output,
4700 .gpl_only = true,
4701 .ret_type = RET_INTEGER,
4702 .arg1_type = ARG_PTR_TO_BTF_ID,
4703 .arg1_btf_id = &bpf_skb_output_btf_ids[0],
4704 .arg2_type = ARG_CONST_MAP_PTR,
4705 .arg3_type = ARG_ANYTHING,
4706 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4707 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4708 };
4709
bpf_tunnel_key_af(u64 flags)4710 static unsigned short bpf_tunnel_key_af(u64 flags)
4711 {
4712 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
4713 }
4714
BPF_CALL_4(bpf_skb_get_tunnel_key,struct sk_buff *,skb,struct bpf_tunnel_key *,to,u32,size,u64,flags)4715 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
4716 u32, size, u64, flags)
4717 {
4718 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4719 u8 compat[sizeof(struct bpf_tunnel_key)];
4720 void *to_orig = to;
4721 int err;
4722
4723 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6 |
4724 BPF_F_TUNINFO_FLAGS)))) {
4725 err = -EINVAL;
4726 goto err_clear;
4727 }
4728 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
4729 err = -EPROTO;
4730 goto err_clear;
4731 }
4732 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4733 err = -EINVAL;
4734 switch (size) {
4735 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4736 case offsetof(struct bpf_tunnel_key, tunnel_label):
4737 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4738 goto set_compat;
4739 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4740 /* Fixup deprecated structure layouts here, so we have
4741 * a common path later on.
4742 */
4743 if (ip_tunnel_info_af(info) != AF_INET)
4744 goto err_clear;
4745 set_compat:
4746 to = (struct bpf_tunnel_key *)compat;
4747 break;
4748 default:
4749 goto err_clear;
4750 }
4751 }
4752
4753 to->tunnel_id = be64_to_cpu(info->key.tun_id);
4754 to->tunnel_tos = info->key.tos;
4755 to->tunnel_ttl = info->key.ttl;
4756 if (flags & BPF_F_TUNINFO_FLAGS)
4757 to->tunnel_flags = ip_tunnel_flags_to_be16(info->key.tun_flags);
4758 else
4759 to->tunnel_ext = 0;
4760
4761 if (flags & BPF_F_TUNINFO_IPV6) {
4762 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
4763 sizeof(to->remote_ipv6));
4764 memcpy(to->local_ipv6, &info->key.u.ipv6.dst,
4765 sizeof(to->local_ipv6));
4766 to->tunnel_label = be32_to_cpu(info->key.label);
4767 } else {
4768 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
4769 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4770 to->local_ipv4 = be32_to_cpu(info->key.u.ipv4.dst);
4771 memset(&to->local_ipv6[1], 0, sizeof(__u32) * 3);
4772 to->tunnel_label = 0;
4773 }
4774
4775 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
4776 memcpy(to_orig, to, size);
4777
4778 return 0;
4779 err_clear:
4780 memset(to_orig, 0, size);
4781 return err;
4782 }
4783
4784 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
4785 .func = bpf_skb_get_tunnel_key,
4786 .gpl_only = false,
4787 .ret_type = RET_INTEGER,
4788 .arg1_type = ARG_PTR_TO_CTX,
4789 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4790 .arg3_type = ARG_CONST_SIZE,
4791 .arg4_type = ARG_ANYTHING,
4792 };
4793
BPF_CALL_3(bpf_skb_get_tunnel_opt,struct sk_buff *,skb,u8 *,to,u32,size)4794 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
4795 {
4796 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4797 int err;
4798
4799 if (unlikely(!info ||
4800 !ip_tunnel_is_options_present(info->key.tun_flags))) {
4801 err = -ENOENT;
4802 goto err_clear;
4803 }
4804 if (unlikely(size < info->options_len)) {
4805 err = -ENOMEM;
4806 goto err_clear;
4807 }
4808
4809 ip_tunnel_info_opts_get(to, info);
4810 if (size > info->options_len)
4811 memset(to + info->options_len, 0, size - info->options_len);
4812
4813 return info->options_len;
4814 err_clear:
4815 memset(to, 0, size);
4816 return err;
4817 }
4818
4819 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
4820 .func = bpf_skb_get_tunnel_opt,
4821 .gpl_only = false,
4822 .ret_type = RET_INTEGER,
4823 .arg1_type = ARG_PTR_TO_CTX,
4824 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4825 .arg3_type = ARG_CONST_SIZE,
4826 };
4827
4828 static struct metadata_dst __percpu *md_dst;
4829
BPF_CALL_4(bpf_skb_set_tunnel_key,struct sk_buff *,skb,const struct bpf_tunnel_key *,from,u32,size,u64,flags)4830 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
4831 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
4832 {
4833 struct metadata_dst *md = this_cpu_ptr(md_dst);
4834 u8 compat[sizeof(struct bpf_tunnel_key)];
4835 struct ip_tunnel_info *info;
4836
4837 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
4838 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER |
4839 BPF_F_NO_TUNNEL_KEY)))
4840 return -EINVAL;
4841 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4842 switch (size) {
4843 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4844 case offsetof(struct bpf_tunnel_key, tunnel_label):
4845 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4846 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4847 /* Fixup deprecated structure layouts here, so we have
4848 * a common path later on.
4849 */
4850 memcpy(compat, from, size);
4851 memset(compat + size, 0, sizeof(compat) - size);
4852 from = (const struct bpf_tunnel_key *) compat;
4853 break;
4854 default:
4855 return -EINVAL;
4856 }
4857 }
4858 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
4859 from->tunnel_ext))
4860 return -EINVAL;
4861
4862 skb_dst_drop(skb);
4863 dst_hold((struct dst_entry *) md);
4864 skb_dst_set(skb, (struct dst_entry *) md);
4865
4866 info = &md->u.tun_info;
4867 memset(info, 0, sizeof(*info));
4868 info->mode = IP_TUNNEL_INFO_TX;
4869
4870 __set_bit(IP_TUNNEL_NOCACHE_BIT, info->key.tun_flags);
4871 __assign_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, info->key.tun_flags,
4872 flags & BPF_F_DONT_FRAGMENT);
4873 __assign_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags,
4874 !(flags & BPF_F_ZERO_CSUM_TX));
4875 __assign_bit(IP_TUNNEL_SEQ_BIT, info->key.tun_flags,
4876 flags & BPF_F_SEQ_NUMBER);
4877 __assign_bit(IP_TUNNEL_KEY_BIT, info->key.tun_flags,
4878 !(flags & BPF_F_NO_TUNNEL_KEY));
4879
4880 info->key.tun_id = cpu_to_be64(from->tunnel_id);
4881 info->key.tos = from->tunnel_tos;
4882 info->key.ttl = from->tunnel_ttl;
4883
4884 if (flags & BPF_F_TUNINFO_IPV6) {
4885 info->mode |= IP_TUNNEL_INFO_IPV6;
4886 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
4887 sizeof(from->remote_ipv6));
4888 memcpy(&info->key.u.ipv6.src, from->local_ipv6,
4889 sizeof(from->local_ipv6));
4890 info->key.label = cpu_to_be32(from->tunnel_label) &
4891 IPV6_FLOWLABEL_MASK;
4892 } else {
4893 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
4894 info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4);
4895 info->key.flow_flags = FLOWI_FLAG_ANYSRC;
4896 }
4897
4898 return 0;
4899 }
4900
4901 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
4902 .func = bpf_skb_set_tunnel_key,
4903 .gpl_only = false,
4904 .ret_type = RET_INTEGER,
4905 .arg1_type = ARG_PTR_TO_CTX,
4906 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4907 .arg3_type = ARG_CONST_SIZE,
4908 .arg4_type = ARG_ANYTHING,
4909 };
4910
BPF_CALL_3(bpf_skb_set_tunnel_opt,struct sk_buff *,skb,const u8 *,from,u32,size)4911 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
4912 const u8 *, from, u32, size)
4913 {
4914 struct ip_tunnel_info *info = skb_tunnel_info(skb);
4915 const struct metadata_dst *md = this_cpu_ptr(md_dst);
4916 IP_TUNNEL_DECLARE_FLAGS(present) = { };
4917
4918 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
4919 return -EINVAL;
4920 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
4921 return -ENOMEM;
4922
4923 ip_tunnel_set_options_present(present);
4924 ip_tunnel_info_opts_set(info, from, size, present);
4925
4926 return 0;
4927 }
4928
4929 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
4930 .func = bpf_skb_set_tunnel_opt,
4931 .gpl_only = false,
4932 .ret_type = RET_INTEGER,
4933 .arg1_type = ARG_PTR_TO_CTX,
4934 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4935 .arg3_type = ARG_CONST_SIZE,
4936 };
4937
4938 static const struct bpf_func_proto *
bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)4939 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
4940 {
4941 if (!md_dst) {
4942 struct metadata_dst __percpu *tmp;
4943
4944 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4945 METADATA_IP_TUNNEL,
4946 GFP_KERNEL);
4947 if (!tmp)
4948 return NULL;
4949 if (cmpxchg(&md_dst, NULL, tmp))
4950 metadata_dst_free_percpu(tmp);
4951 }
4952
4953 switch (which) {
4954 case BPF_FUNC_skb_set_tunnel_key:
4955 return &bpf_skb_set_tunnel_key_proto;
4956 case BPF_FUNC_skb_set_tunnel_opt:
4957 return &bpf_skb_set_tunnel_opt_proto;
4958 default:
4959 return NULL;
4960 }
4961 }
4962
BPF_CALL_3(bpf_skb_under_cgroup,struct sk_buff *,skb,struct bpf_map *,map,u32,idx)4963 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4964 u32, idx)
4965 {
4966 struct bpf_array *array = container_of(map, struct bpf_array, map);
4967 struct cgroup *cgrp;
4968 struct sock *sk;
4969
4970 sk = skb_to_full_sk(skb);
4971 if (!sk || !sk_fullsock(sk))
4972 return -ENOENT;
4973 if (unlikely(idx >= array->map.max_entries))
4974 return -E2BIG;
4975
4976 cgrp = READ_ONCE(array->ptrs[idx]);
4977 if (unlikely(!cgrp))
4978 return -EAGAIN;
4979
4980 return sk_under_cgroup_hierarchy(sk, cgrp);
4981 }
4982
4983 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4984 .func = bpf_skb_under_cgroup,
4985 .gpl_only = false,
4986 .ret_type = RET_INTEGER,
4987 .arg1_type = ARG_PTR_TO_CTX,
4988 .arg2_type = ARG_CONST_MAP_PTR,
4989 .arg3_type = ARG_ANYTHING,
4990 };
4991
4992 #ifdef CONFIG_SOCK_CGROUP_DATA
__bpf_sk_cgroup_id(struct sock * sk)4993 static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
4994 {
4995 struct cgroup *cgrp;
4996
4997 sk = sk_to_full_sk(sk);
4998 if (!sk || !sk_fullsock(sk))
4999 return 0;
5000
5001 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
5002 return cgroup_id(cgrp);
5003 }
5004
BPF_CALL_1(bpf_skb_cgroup_id,const struct sk_buff *,skb)5005 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
5006 {
5007 return __bpf_sk_cgroup_id(skb->sk);
5008 }
5009
5010 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
5011 .func = bpf_skb_cgroup_id,
5012 .gpl_only = false,
5013 .ret_type = RET_INTEGER,
5014 .arg1_type = ARG_PTR_TO_CTX,
5015 };
5016
__bpf_sk_ancestor_cgroup_id(struct sock * sk,int ancestor_level)5017 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
5018 int ancestor_level)
5019 {
5020 struct cgroup *ancestor;
5021 struct cgroup *cgrp;
5022
5023 sk = sk_to_full_sk(sk);
5024 if (!sk || !sk_fullsock(sk))
5025 return 0;
5026
5027 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
5028 ancestor = cgroup_ancestor(cgrp, ancestor_level);
5029 if (!ancestor)
5030 return 0;
5031
5032 return cgroup_id(ancestor);
5033 }
5034
BPF_CALL_2(bpf_skb_ancestor_cgroup_id,const struct sk_buff *,skb,int,ancestor_level)5035 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
5036 ancestor_level)
5037 {
5038 return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level);
5039 }
5040
5041 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
5042 .func = bpf_skb_ancestor_cgroup_id,
5043 .gpl_only = false,
5044 .ret_type = RET_INTEGER,
5045 .arg1_type = ARG_PTR_TO_CTX,
5046 .arg2_type = ARG_ANYTHING,
5047 };
5048
BPF_CALL_1(bpf_sk_cgroup_id,struct sock *,sk)5049 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk)
5050 {
5051 return __bpf_sk_cgroup_id(sk);
5052 }
5053
5054 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
5055 .func = bpf_sk_cgroup_id,
5056 .gpl_only = false,
5057 .ret_type = RET_INTEGER,
5058 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5059 };
5060
BPF_CALL_2(bpf_sk_ancestor_cgroup_id,struct sock *,sk,int,ancestor_level)5061 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
5062 {
5063 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
5064 }
5065
5066 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
5067 .func = bpf_sk_ancestor_cgroup_id,
5068 .gpl_only = false,
5069 .ret_type = RET_INTEGER,
5070 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5071 .arg2_type = ARG_ANYTHING,
5072 };
5073 #endif
5074
bpf_xdp_copy(void * dst,const void * ctx,unsigned long off,unsigned long len)5075 static unsigned long bpf_xdp_copy(void *dst, const void *ctx,
5076 unsigned long off, unsigned long len)
5077 {
5078 struct xdp_buff *xdp = (struct xdp_buff *)ctx;
5079
5080 bpf_xdp_copy_buf(xdp, off, dst, len, false);
5081 return 0;
5082 }
5083
BPF_CALL_5(bpf_xdp_event_output,struct xdp_buff *,xdp,struct bpf_map *,map,u64,flags,void *,meta,u64,meta_size)5084 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
5085 u64, flags, void *, meta, u64, meta_size)
5086 {
5087 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
5088
5089 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
5090 return -EINVAL;
5091
5092 if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp)))
5093 return -EFAULT;
5094
5095 return bpf_event_output(map, flags, meta, meta_size, xdp,
5096 xdp_size, bpf_xdp_copy);
5097 }
5098
5099 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
5100 .func = bpf_xdp_event_output,
5101 .gpl_only = true,
5102 .ret_type = RET_INTEGER,
5103 .arg1_type = ARG_PTR_TO_CTX,
5104 .arg2_type = ARG_CONST_MAP_PTR,
5105 .arg3_type = ARG_ANYTHING,
5106 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5107 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
5108 };
5109
5110 BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff)
5111
5112 const struct bpf_func_proto bpf_xdp_output_proto = {
5113 .func = bpf_xdp_event_output,
5114 .gpl_only = true,
5115 .ret_type = RET_INTEGER,
5116 .arg1_type = ARG_PTR_TO_BTF_ID,
5117 .arg1_btf_id = &bpf_xdp_output_btf_ids[0],
5118 .arg2_type = ARG_CONST_MAP_PTR,
5119 .arg3_type = ARG_ANYTHING,
5120 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5121 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
5122 };
5123
BPF_CALL_1(bpf_get_socket_cookie,struct sk_buff *,skb)5124 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
5125 {
5126 return skb->sk ? __sock_gen_cookie(skb->sk) : 0;
5127 }
5128
5129 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
5130 .func = bpf_get_socket_cookie,
5131 .gpl_only = false,
5132 .ret_type = RET_INTEGER,
5133 .arg1_type = ARG_PTR_TO_CTX,
5134 };
5135
BPF_CALL_1(bpf_get_socket_cookie_sock_addr,struct bpf_sock_addr_kern *,ctx)5136 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
5137 {
5138 return __sock_gen_cookie(ctx->sk);
5139 }
5140
5141 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
5142 .func = bpf_get_socket_cookie_sock_addr,
5143 .gpl_only = false,
5144 .ret_type = RET_INTEGER,
5145 .arg1_type = ARG_PTR_TO_CTX,
5146 };
5147
BPF_CALL_1(bpf_get_socket_cookie_sock,struct sock *,ctx)5148 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx)
5149 {
5150 return __sock_gen_cookie(ctx);
5151 }
5152
5153 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
5154 .func = bpf_get_socket_cookie_sock,
5155 .gpl_only = false,
5156 .ret_type = RET_INTEGER,
5157 .arg1_type = ARG_PTR_TO_CTX,
5158 };
5159
BPF_CALL_1(bpf_get_socket_ptr_cookie,struct sock *,sk)5160 BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk)
5161 {
5162 return sk ? sock_gen_cookie(sk) : 0;
5163 }
5164
5165 const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
5166 .func = bpf_get_socket_ptr_cookie,
5167 .gpl_only = false,
5168 .ret_type = RET_INTEGER,
5169 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | PTR_MAYBE_NULL,
5170 };
5171
BPF_CALL_1(bpf_get_socket_cookie_sock_ops,struct bpf_sock_ops_kern *,ctx)5172 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
5173 {
5174 return __sock_gen_cookie(ctx->sk);
5175 }
5176
5177 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
5178 .func = bpf_get_socket_cookie_sock_ops,
5179 .gpl_only = false,
5180 .ret_type = RET_INTEGER,
5181 .arg1_type = ARG_PTR_TO_CTX,
5182 };
5183
__bpf_get_netns_cookie(struct sock * sk)5184 static u64 __bpf_get_netns_cookie(struct sock *sk)
5185 {
5186 const struct net *net = sk ? sock_net(sk) : &init_net;
5187
5188 return net->net_cookie;
5189 }
5190
BPF_CALL_1(bpf_get_netns_cookie_sock,struct sock *,ctx)5191 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
5192 {
5193 return __bpf_get_netns_cookie(ctx);
5194 }
5195
5196 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = {
5197 .func = bpf_get_netns_cookie_sock,
5198 .gpl_only = false,
5199 .ret_type = RET_INTEGER,
5200 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5201 };
5202
BPF_CALL_1(bpf_get_netns_cookie_sock_addr,struct bpf_sock_addr_kern *,ctx)5203 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
5204 {
5205 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5206 }
5207
5208 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = {
5209 .func = bpf_get_netns_cookie_sock_addr,
5210 .gpl_only = false,
5211 .ret_type = RET_INTEGER,
5212 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5213 };
5214
BPF_CALL_1(bpf_get_netns_cookie_sock_ops,struct bpf_sock_ops_kern *,ctx)5215 BPF_CALL_1(bpf_get_netns_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
5216 {
5217 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5218 }
5219
5220 static const struct bpf_func_proto bpf_get_netns_cookie_sock_ops_proto = {
5221 .func = bpf_get_netns_cookie_sock_ops,
5222 .gpl_only = false,
5223 .ret_type = RET_INTEGER,
5224 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5225 };
5226
BPF_CALL_1(bpf_get_netns_cookie_sk_msg,struct sk_msg *,ctx)5227 BPF_CALL_1(bpf_get_netns_cookie_sk_msg, struct sk_msg *, ctx)
5228 {
5229 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5230 }
5231
5232 static const struct bpf_func_proto bpf_get_netns_cookie_sk_msg_proto = {
5233 .func = bpf_get_netns_cookie_sk_msg,
5234 .gpl_only = false,
5235 .ret_type = RET_INTEGER,
5236 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5237 };
5238
BPF_CALL_1(bpf_get_socket_uid,struct sk_buff *,skb)5239 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
5240 {
5241 struct sock *sk = sk_to_full_sk(skb->sk);
5242 kuid_t kuid;
5243
5244 if (!sk || !sk_fullsock(sk))
5245 return overflowuid;
5246 kuid = sock_net_uid(sock_net(sk), sk);
5247 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
5248 }
5249
5250 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
5251 .func = bpf_get_socket_uid,
5252 .gpl_only = false,
5253 .ret_type = RET_INTEGER,
5254 .arg1_type = ARG_PTR_TO_CTX,
5255 };
5256
sol_socket_sockopt(struct sock * sk,int optname,char * optval,int * optlen,bool getopt)5257 static int sol_socket_sockopt(struct sock *sk, int optname,
5258 char *optval, int *optlen,
5259 bool getopt)
5260 {
5261 switch (optname) {
5262 case SO_REUSEADDR:
5263 case SO_SNDBUF:
5264 case SO_RCVBUF:
5265 case SO_KEEPALIVE:
5266 case SO_PRIORITY:
5267 case SO_REUSEPORT:
5268 case SO_RCVLOWAT:
5269 case SO_MARK:
5270 case SO_MAX_PACING_RATE:
5271 case SO_BINDTOIFINDEX:
5272 case SO_TXREHASH:
5273 if (*optlen != sizeof(int))
5274 return -EINVAL;
5275 break;
5276 case SO_BINDTODEVICE:
5277 break;
5278 default:
5279 return -EINVAL;
5280 }
5281
5282 if (getopt) {
5283 if (optname == SO_BINDTODEVICE)
5284 return -EINVAL;
5285 return sk_getsockopt(sk, SOL_SOCKET, optname,
5286 KERNEL_SOCKPTR(optval),
5287 KERNEL_SOCKPTR(optlen));
5288 }
5289
5290 return sk_setsockopt(sk, SOL_SOCKET, optname,
5291 KERNEL_SOCKPTR(optval), *optlen);
5292 }
5293
bpf_sol_tcp_setsockopt(struct sock * sk,int optname,char * optval,int optlen)5294 static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,
5295 char *optval, int optlen)
5296 {
5297 struct tcp_sock *tp = tcp_sk(sk);
5298 unsigned long timeout;
5299 int val;
5300
5301 if (optlen != sizeof(int))
5302 return -EINVAL;
5303
5304 val = *(int *)optval;
5305
5306 /* Only some options are supported */
5307 switch (optname) {
5308 case TCP_BPF_IW:
5309 if (val <= 0 || tp->data_segs_out > tp->syn_data)
5310 return -EINVAL;
5311 tcp_snd_cwnd_set(tp, val);
5312 break;
5313 case TCP_BPF_SNDCWND_CLAMP:
5314 if (val <= 0)
5315 return -EINVAL;
5316 tp->snd_cwnd_clamp = val;
5317 tp->snd_ssthresh = val;
5318 break;
5319 case TCP_BPF_DELACK_MAX:
5320 timeout = usecs_to_jiffies(val);
5321 if (timeout > TCP_DELACK_MAX ||
5322 timeout < TCP_TIMEOUT_MIN)
5323 return -EINVAL;
5324 inet_csk(sk)->icsk_delack_max = timeout;
5325 break;
5326 case TCP_BPF_RTO_MIN:
5327 timeout = usecs_to_jiffies(val);
5328 if (timeout > TCP_RTO_MIN ||
5329 timeout < TCP_TIMEOUT_MIN)
5330 return -EINVAL;
5331 inet_csk(sk)->icsk_rto_min = timeout;
5332 break;
5333 case TCP_BPF_SOCK_OPS_CB_FLAGS:
5334 if (val & ~(BPF_SOCK_OPS_ALL_CB_FLAGS))
5335 return -EINVAL;
5336 tp->bpf_sock_ops_cb_flags = val;
5337 break;
5338 default:
5339 return -EINVAL;
5340 }
5341
5342 return 0;
5343 }
5344
sol_tcp_sockopt_congestion(struct sock * sk,char * optval,int * optlen,bool getopt)5345 static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval,
5346 int *optlen, bool getopt)
5347 {
5348 struct tcp_sock *tp;
5349 int ret;
5350
5351 if (*optlen < 2)
5352 return -EINVAL;
5353
5354 if (getopt) {
5355 if (!inet_csk(sk)->icsk_ca_ops)
5356 return -EINVAL;
5357 /* BPF expects NULL-terminated tcp-cc string */
5358 optval[--(*optlen)] = '\0';
5359 return do_tcp_getsockopt(sk, SOL_TCP, TCP_CONGESTION,
5360 KERNEL_SOCKPTR(optval),
5361 KERNEL_SOCKPTR(optlen));
5362 }
5363
5364 /* "cdg" is the only cc that alloc a ptr
5365 * in inet_csk_ca area. The bpf-tcp-cc may
5366 * overwrite this ptr after switching to cdg.
5367 */
5368 if (*optlen >= sizeof("cdg") - 1 && !strncmp("cdg", optval, *optlen))
5369 return -ENOTSUPP;
5370
5371 /* It stops this looping
5372 *
5373 * .init => bpf_setsockopt(tcp_cc) => .init =>
5374 * bpf_setsockopt(tcp_cc)" => .init => ....
5375 *
5376 * The second bpf_setsockopt(tcp_cc) is not allowed
5377 * in order to break the loop when both .init
5378 * are the same bpf prog.
5379 *
5380 * This applies even the second bpf_setsockopt(tcp_cc)
5381 * does not cause a loop. This limits only the first
5382 * '.init' can call bpf_setsockopt(TCP_CONGESTION) to
5383 * pick a fallback cc (eg. peer does not support ECN)
5384 * and the second '.init' cannot fallback to
5385 * another.
5386 */
5387 tp = tcp_sk(sk);
5388 if (tp->bpf_chg_cc_inprogress)
5389 return -EBUSY;
5390
5391 tp->bpf_chg_cc_inprogress = 1;
5392 ret = do_tcp_setsockopt(sk, SOL_TCP, TCP_CONGESTION,
5393 KERNEL_SOCKPTR(optval), *optlen);
5394 tp->bpf_chg_cc_inprogress = 0;
5395 return ret;
5396 }
5397
sol_tcp_sockopt(struct sock * sk,int optname,char * optval,int * optlen,bool getopt)5398 static int sol_tcp_sockopt(struct sock *sk, int optname,
5399 char *optval, int *optlen,
5400 bool getopt)
5401 {
5402 if (sk->sk_protocol != IPPROTO_TCP)
5403 return -EINVAL;
5404
5405 switch (optname) {
5406 case TCP_NODELAY:
5407 case TCP_MAXSEG:
5408 case TCP_KEEPIDLE:
5409 case TCP_KEEPINTVL:
5410 case TCP_KEEPCNT:
5411 case TCP_SYNCNT:
5412 case TCP_WINDOW_CLAMP:
5413 case TCP_THIN_LINEAR_TIMEOUTS:
5414 case TCP_USER_TIMEOUT:
5415 case TCP_NOTSENT_LOWAT:
5416 case TCP_SAVE_SYN:
5417 if (*optlen != sizeof(int))
5418 return -EINVAL;
5419 break;
5420 case TCP_CONGESTION:
5421 return sol_tcp_sockopt_congestion(sk, optval, optlen, getopt);
5422 case TCP_SAVED_SYN:
5423 if (*optlen < 1)
5424 return -EINVAL;
5425 break;
5426 case TCP_BPF_SOCK_OPS_CB_FLAGS:
5427 if (*optlen != sizeof(int))
5428 return -EINVAL;
5429 if (getopt) {
5430 struct tcp_sock *tp = tcp_sk(sk);
5431 int cb_flags = tp->bpf_sock_ops_cb_flags;
5432
5433 memcpy(optval, &cb_flags, *optlen);
5434 return 0;
5435 }
5436 return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
5437 default:
5438 if (getopt)
5439 return -EINVAL;
5440 return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
5441 }
5442
5443 if (getopt) {
5444 if (optname == TCP_SAVED_SYN) {
5445 struct tcp_sock *tp = tcp_sk(sk);
5446
5447 if (!tp->saved_syn ||
5448 *optlen > tcp_saved_syn_len(tp->saved_syn))
5449 return -EINVAL;
5450 memcpy(optval, tp->saved_syn->data, *optlen);
5451 /* It cannot free tp->saved_syn here because it
5452 * does not know if the user space still needs it.
5453 */
5454 return 0;
5455 }
5456
5457 return do_tcp_getsockopt(sk, SOL_TCP, optname,
5458 KERNEL_SOCKPTR(optval),
5459 KERNEL_SOCKPTR(optlen));
5460 }
5461
5462 return do_tcp_setsockopt(sk, SOL_TCP, optname,
5463 KERNEL_SOCKPTR(optval), *optlen);
5464 }
5465
sol_ip_sockopt(struct sock * sk,int optname,char * optval,int * optlen,bool getopt)5466 static int sol_ip_sockopt(struct sock *sk, int optname,
5467 char *optval, int *optlen,
5468 bool getopt)
5469 {
5470 if (sk->sk_family != AF_INET)
5471 return -EINVAL;
5472
5473 switch (optname) {
5474 case IP_TOS:
5475 if (*optlen != sizeof(int))
5476 return -EINVAL;
5477 break;
5478 default:
5479 return -EINVAL;
5480 }
5481
5482 if (getopt)
5483 return do_ip_getsockopt(sk, SOL_IP, optname,
5484 KERNEL_SOCKPTR(optval),
5485 KERNEL_SOCKPTR(optlen));
5486
5487 return do_ip_setsockopt(sk, SOL_IP, optname,
5488 KERNEL_SOCKPTR(optval), *optlen);
5489 }
5490
sol_ipv6_sockopt(struct sock * sk,int optname,char * optval,int * optlen,bool getopt)5491 static int sol_ipv6_sockopt(struct sock *sk, int optname,
5492 char *optval, int *optlen,
5493 bool getopt)
5494 {
5495 if (sk->sk_family != AF_INET6)
5496 return -EINVAL;
5497
5498 switch (optname) {
5499 case IPV6_TCLASS:
5500 case IPV6_AUTOFLOWLABEL:
5501 if (*optlen != sizeof(int))
5502 return -EINVAL;
5503 break;
5504 default:
5505 return -EINVAL;
5506 }
5507
5508 if (getopt)
5509 return ipv6_bpf_stub->ipv6_getsockopt(sk, SOL_IPV6, optname,
5510 KERNEL_SOCKPTR(optval),
5511 KERNEL_SOCKPTR(optlen));
5512
5513 return ipv6_bpf_stub->ipv6_setsockopt(sk, SOL_IPV6, optname,
5514 KERNEL_SOCKPTR(optval), *optlen);
5515 }
5516
__bpf_setsockopt(struct sock * sk,int level,int optname,char * optval,int optlen)5517 static int __bpf_setsockopt(struct sock *sk, int level, int optname,
5518 char *optval, int optlen)
5519 {
5520 if (!sk_fullsock(sk))
5521 return -EINVAL;
5522
5523 if (level == SOL_SOCKET)
5524 return sol_socket_sockopt(sk, optname, optval, &optlen, false);
5525 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5526 return sol_ip_sockopt(sk, optname, optval, &optlen, false);
5527 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5528 return sol_ipv6_sockopt(sk, optname, optval, &optlen, false);
5529 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5530 return sol_tcp_sockopt(sk, optname, optval, &optlen, false);
5531
5532 return -EINVAL;
5533 }
5534
_bpf_setsockopt(struct sock * sk,int level,int optname,char * optval,int optlen)5535 static int _bpf_setsockopt(struct sock *sk, int level, int optname,
5536 char *optval, int optlen)
5537 {
5538 if (sk_fullsock(sk))
5539 sock_owned_by_me(sk);
5540 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5541 }
5542
__bpf_getsockopt(struct sock * sk,int level,int optname,char * optval,int optlen)5543 static int __bpf_getsockopt(struct sock *sk, int level, int optname,
5544 char *optval, int optlen)
5545 {
5546 int err, saved_optlen = optlen;
5547
5548 if (!sk_fullsock(sk)) {
5549 err = -EINVAL;
5550 goto done;
5551 }
5552
5553 if (level == SOL_SOCKET)
5554 err = sol_socket_sockopt(sk, optname, optval, &optlen, true);
5555 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5556 err = sol_tcp_sockopt(sk, optname, optval, &optlen, true);
5557 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5558 err = sol_ip_sockopt(sk, optname, optval, &optlen, true);
5559 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5560 err = sol_ipv6_sockopt(sk, optname, optval, &optlen, true);
5561 else
5562 err = -EINVAL;
5563
5564 done:
5565 if (err)
5566 optlen = 0;
5567 if (optlen < saved_optlen)
5568 memset(optval + optlen, 0, saved_optlen - optlen);
5569 return err;
5570 }
5571
_bpf_getsockopt(struct sock * sk,int level,int optname,char * optval,int optlen)5572 static int _bpf_getsockopt(struct sock *sk, int level, int optname,
5573 char *optval, int optlen)
5574 {
5575 if (sk_fullsock(sk))
5576 sock_owned_by_me(sk);
5577 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5578 }
5579
BPF_CALL_5(bpf_sk_setsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5580 BPF_CALL_5(bpf_sk_setsockopt, struct sock *, sk, int, level,
5581 int, optname, char *, optval, int, optlen)
5582 {
5583 return _bpf_setsockopt(sk, level, optname, optval, optlen);
5584 }
5585
5586 const struct bpf_func_proto bpf_sk_setsockopt_proto = {
5587 .func = bpf_sk_setsockopt,
5588 .gpl_only = false,
5589 .ret_type = RET_INTEGER,
5590 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5591 .arg2_type = ARG_ANYTHING,
5592 .arg3_type = ARG_ANYTHING,
5593 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5594 .arg5_type = ARG_CONST_SIZE,
5595 };
5596
BPF_CALL_5(bpf_sk_getsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5597 BPF_CALL_5(bpf_sk_getsockopt, struct sock *, sk, int, level,
5598 int, optname, char *, optval, int, optlen)
5599 {
5600 return _bpf_getsockopt(sk, level, optname, optval, optlen);
5601 }
5602
5603 const struct bpf_func_proto bpf_sk_getsockopt_proto = {
5604 .func = bpf_sk_getsockopt,
5605 .gpl_only = false,
5606 .ret_type = RET_INTEGER,
5607 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5608 .arg2_type = ARG_ANYTHING,
5609 .arg3_type = ARG_ANYTHING,
5610 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5611 .arg5_type = ARG_CONST_SIZE,
5612 };
5613
BPF_CALL_5(bpf_unlocked_sk_setsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5614 BPF_CALL_5(bpf_unlocked_sk_setsockopt, struct sock *, sk, int, level,
5615 int, optname, char *, optval, int, optlen)
5616 {
5617 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5618 }
5619
5620 const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto = {
5621 .func = bpf_unlocked_sk_setsockopt,
5622 .gpl_only = false,
5623 .ret_type = RET_INTEGER,
5624 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5625 .arg2_type = ARG_ANYTHING,
5626 .arg3_type = ARG_ANYTHING,
5627 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5628 .arg5_type = ARG_CONST_SIZE,
5629 };
5630
BPF_CALL_5(bpf_unlocked_sk_getsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5631 BPF_CALL_5(bpf_unlocked_sk_getsockopt, struct sock *, sk, int, level,
5632 int, optname, char *, optval, int, optlen)
5633 {
5634 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5635 }
5636
5637 const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto = {
5638 .func = bpf_unlocked_sk_getsockopt,
5639 .gpl_only = false,
5640 .ret_type = RET_INTEGER,
5641 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5642 .arg2_type = ARG_ANYTHING,
5643 .arg3_type = ARG_ANYTHING,
5644 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5645 .arg5_type = ARG_CONST_SIZE,
5646 };
5647
BPF_CALL_5(bpf_sock_addr_setsockopt,struct bpf_sock_addr_kern *,ctx,int,level,int,optname,char *,optval,int,optlen)5648 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx,
5649 int, level, int, optname, char *, optval, int, optlen)
5650 {
5651 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen);
5652 }
5653
5654 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = {
5655 .func = bpf_sock_addr_setsockopt,
5656 .gpl_only = false,
5657 .ret_type = RET_INTEGER,
5658 .arg1_type = ARG_PTR_TO_CTX,
5659 .arg2_type = ARG_ANYTHING,
5660 .arg3_type = ARG_ANYTHING,
5661 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5662 .arg5_type = ARG_CONST_SIZE,
5663 };
5664
BPF_CALL_5(bpf_sock_addr_getsockopt,struct bpf_sock_addr_kern *,ctx,int,level,int,optname,char *,optval,int,optlen)5665 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx,
5666 int, level, int, optname, char *, optval, int, optlen)
5667 {
5668 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen);
5669 }
5670
5671 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
5672 .func = bpf_sock_addr_getsockopt,
5673 .gpl_only = false,
5674 .ret_type = RET_INTEGER,
5675 .arg1_type = ARG_PTR_TO_CTX,
5676 .arg2_type = ARG_ANYTHING,
5677 .arg3_type = ARG_ANYTHING,
5678 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5679 .arg5_type = ARG_CONST_SIZE,
5680 };
5681
BPF_CALL_5(bpf_sock_ops_setsockopt,struct bpf_sock_ops_kern *,bpf_sock,int,level,int,optname,char *,optval,int,optlen)5682 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5683 int, level, int, optname, char *, optval, int, optlen)
5684 {
5685 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
5686 }
5687
5688 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = {
5689 .func = bpf_sock_ops_setsockopt,
5690 .gpl_only = false,
5691 .ret_type = RET_INTEGER,
5692 .arg1_type = ARG_PTR_TO_CTX,
5693 .arg2_type = ARG_ANYTHING,
5694 .arg3_type = ARG_ANYTHING,
5695 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5696 .arg5_type = ARG_CONST_SIZE,
5697 };
5698
bpf_sock_ops_get_syn(struct bpf_sock_ops_kern * bpf_sock,int optname,const u8 ** start)5699 static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock,
5700 int optname, const u8 **start)
5701 {
5702 struct sk_buff *syn_skb = bpf_sock->syn_skb;
5703 const u8 *hdr_start;
5704 int ret;
5705
5706 if (syn_skb) {
5707 /* sk is a request_sock here */
5708
5709 if (optname == TCP_BPF_SYN) {
5710 hdr_start = syn_skb->data;
5711 ret = tcp_hdrlen(syn_skb);
5712 } else if (optname == TCP_BPF_SYN_IP) {
5713 hdr_start = skb_network_header(syn_skb);
5714 ret = skb_network_header_len(syn_skb) +
5715 tcp_hdrlen(syn_skb);
5716 } else {
5717 /* optname == TCP_BPF_SYN_MAC */
5718 hdr_start = skb_mac_header(syn_skb);
5719 ret = skb_mac_header_len(syn_skb) +
5720 skb_network_header_len(syn_skb) +
5721 tcp_hdrlen(syn_skb);
5722 }
5723 } else {
5724 struct sock *sk = bpf_sock->sk;
5725 struct saved_syn *saved_syn;
5726
5727 if (sk->sk_state == TCP_NEW_SYN_RECV)
5728 /* synack retransmit. bpf_sock->syn_skb will
5729 * not be available. It has to resort to
5730 * saved_syn (if it is saved).
5731 */
5732 saved_syn = inet_reqsk(sk)->saved_syn;
5733 else
5734 saved_syn = tcp_sk(sk)->saved_syn;
5735
5736 if (!saved_syn)
5737 return -ENOENT;
5738
5739 if (optname == TCP_BPF_SYN) {
5740 hdr_start = saved_syn->data +
5741 saved_syn->mac_hdrlen +
5742 saved_syn->network_hdrlen;
5743 ret = saved_syn->tcp_hdrlen;
5744 } else if (optname == TCP_BPF_SYN_IP) {
5745 hdr_start = saved_syn->data +
5746 saved_syn->mac_hdrlen;
5747 ret = saved_syn->network_hdrlen +
5748 saved_syn->tcp_hdrlen;
5749 } else {
5750 /* optname == TCP_BPF_SYN_MAC */
5751
5752 /* TCP_SAVE_SYN may not have saved the mac hdr */
5753 if (!saved_syn->mac_hdrlen)
5754 return -ENOENT;
5755
5756 hdr_start = saved_syn->data;
5757 ret = saved_syn->mac_hdrlen +
5758 saved_syn->network_hdrlen +
5759 saved_syn->tcp_hdrlen;
5760 }
5761 }
5762
5763 *start = hdr_start;
5764 return ret;
5765 }
5766
BPF_CALL_5(bpf_sock_ops_getsockopt,struct bpf_sock_ops_kern *,bpf_sock,int,level,int,optname,char *,optval,int,optlen)5767 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5768 int, level, int, optname, char *, optval, int, optlen)
5769 {
5770 if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP &&
5771 optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) {
5772 int ret, copy_len = 0;
5773 const u8 *start;
5774
5775 ret = bpf_sock_ops_get_syn(bpf_sock, optname, &start);
5776 if (ret > 0) {
5777 copy_len = ret;
5778 if (optlen < copy_len) {
5779 copy_len = optlen;
5780 ret = -ENOSPC;
5781 }
5782
5783 memcpy(optval, start, copy_len);
5784 }
5785
5786 /* Zero out unused buffer at the end */
5787 memset(optval + copy_len, 0, optlen - copy_len);
5788
5789 return ret;
5790 }
5791
5792 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen);
5793 }
5794
5795 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = {
5796 .func = bpf_sock_ops_getsockopt,
5797 .gpl_only = false,
5798 .ret_type = RET_INTEGER,
5799 .arg1_type = ARG_PTR_TO_CTX,
5800 .arg2_type = ARG_ANYTHING,
5801 .arg3_type = ARG_ANYTHING,
5802 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5803 .arg5_type = ARG_CONST_SIZE,
5804 };
5805
BPF_CALL_2(bpf_sock_ops_cb_flags_set,struct bpf_sock_ops_kern *,bpf_sock,int,argval)5806 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
5807 int, argval)
5808 {
5809 struct sock *sk = bpf_sock->sk;
5810 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
5811
5812 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
5813 return -EINVAL;
5814
5815 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
5816
5817 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
5818 }
5819
5820 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
5821 .func = bpf_sock_ops_cb_flags_set,
5822 .gpl_only = false,
5823 .ret_type = RET_INTEGER,
5824 .arg1_type = ARG_PTR_TO_CTX,
5825 .arg2_type = ARG_ANYTHING,
5826 };
5827
5828 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
5829 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
5830
BPF_CALL_3(bpf_bind,struct bpf_sock_addr_kern *,ctx,struct sockaddr *,addr,int,addr_len)5831 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
5832 int, addr_len)
5833 {
5834 #ifdef CONFIG_INET
5835 struct sock *sk = ctx->sk;
5836 u32 flags = BIND_FROM_BPF;
5837 int err;
5838
5839 err = -EINVAL;
5840 if (addr_len < offsetofend(struct sockaddr, sa_family))
5841 return err;
5842 if (addr->sa_family == AF_INET) {
5843 if (addr_len < sizeof(struct sockaddr_in))
5844 return err;
5845 if (((struct sockaddr_in *)addr)->sin_port == htons(0))
5846 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5847 return __inet_bind(sk, addr, addr_len, flags);
5848 #if IS_ENABLED(CONFIG_IPV6)
5849 } else if (addr->sa_family == AF_INET6) {
5850 if (addr_len < SIN6_LEN_RFC2133)
5851 return err;
5852 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0))
5853 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5854 /* ipv6_bpf_stub cannot be NULL, since it's called from
5855 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
5856 */
5857 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags);
5858 #endif /* CONFIG_IPV6 */
5859 }
5860 #endif /* CONFIG_INET */
5861
5862 return -EAFNOSUPPORT;
5863 }
5864
5865 static const struct bpf_func_proto bpf_bind_proto = {
5866 .func = bpf_bind,
5867 .gpl_only = false,
5868 .ret_type = RET_INTEGER,
5869 .arg1_type = ARG_PTR_TO_CTX,
5870 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5871 .arg3_type = ARG_CONST_SIZE,
5872 };
5873
5874 #ifdef CONFIG_XFRM
5875
5876 #if (IS_BUILTIN(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
5877 (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
5878
5879 struct metadata_dst __percpu *xfrm_bpf_md_dst;
5880 EXPORT_SYMBOL_GPL(xfrm_bpf_md_dst);
5881
5882 #endif
5883
BPF_CALL_5(bpf_skb_get_xfrm_state,struct sk_buff *,skb,u32,index,struct bpf_xfrm_state *,to,u32,size,u64,flags)5884 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
5885 struct bpf_xfrm_state *, to, u32, size, u64, flags)
5886 {
5887 const struct sec_path *sp = skb_sec_path(skb);
5888 const struct xfrm_state *x;
5889
5890 if (!sp || unlikely(index >= sp->len || flags))
5891 goto err_clear;
5892
5893 x = sp->xvec[index];
5894
5895 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
5896 goto err_clear;
5897
5898 to->reqid = x->props.reqid;
5899 to->spi = x->id.spi;
5900 to->family = x->props.family;
5901 to->ext = 0;
5902
5903 if (to->family == AF_INET6) {
5904 memcpy(to->remote_ipv6, x->props.saddr.a6,
5905 sizeof(to->remote_ipv6));
5906 } else {
5907 to->remote_ipv4 = x->props.saddr.a4;
5908 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
5909 }
5910
5911 return 0;
5912 err_clear:
5913 memset(to, 0, size);
5914 return -EINVAL;
5915 }
5916
5917 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
5918 .func = bpf_skb_get_xfrm_state,
5919 .gpl_only = false,
5920 .ret_type = RET_INTEGER,
5921 .arg1_type = ARG_PTR_TO_CTX,
5922 .arg2_type = ARG_ANYTHING,
5923 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
5924 .arg4_type = ARG_CONST_SIZE,
5925 .arg5_type = ARG_ANYTHING,
5926 };
5927 #endif
5928
5929 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
bpf_fib_set_fwd_params(struct bpf_fib_lookup * params,u32 mtu)5930 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params, u32 mtu)
5931 {
5932 params->h_vlan_TCI = 0;
5933 params->h_vlan_proto = 0;
5934 if (mtu)
5935 params->mtu_result = mtu; /* union with tot_len */
5936
5937 return 0;
5938 }
5939 #endif
5940
5941 #if IS_ENABLED(CONFIG_INET)
bpf_ipv4_fib_lookup(struct net * net,struct bpf_fib_lookup * params,u32 flags,bool check_mtu)5942 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5943 u32 flags, bool check_mtu)
5944 {
5945 struct fib_nh_common *nhc;
5946 struct in_device *in_dev;
5947 struct neighbour *neigh;
5948 struct net_device *dev;
5949 struct fib_result res;
5950 struct flowi4 fl4;
5951 u32 mtu = 0;
5952 int err;
5953
5954 dev = dev_get_by_index_rcu(net, params->ifindex);
5955 if (unlikely(!dev))
5956 return -ENODEV;
5957
5958 /* verify forwarding is enabled on this interface */
5959 in_dev = __in_dev_get_rcu(dev);
5960 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
5961 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5962
5963 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5964 fl4.flowi4_iif = 1;
5965 fl4.flowi4_oif = params->ifindex;
5966 } else {
5967 fl4.flowi4_iif = params->ifindex;
5968 fl4.flowi4_oif = 0;
5969 }
5970 fl4.flowi4_tos = params->tos & INET_DSCP_MASK;
5971 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
5972 fl4.flowi4_flags = 0;
5973
5974 fl4.flowi4_proto = params->l4_protocol;
5975 fl4.daddr = params->ipv4_dst;
5976 fl4.saddr = params->ipv4_src;
5977 fl4.fl4_sport = params->sport;
5978 fl4.fl4_dport = params->dport;
5979 fl4.flowi4_multipath_hash = 0;
5980
5981 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5982 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5983 struct fib_table *tb;
5984
5985 if (flags & BPF_FIB_LOOKUP_TBID) {
5986 tbid = params->tbid;
5987 /* zero out for vlan output */
5988 params->tbid = 0;
5989 }
5990
5991 tb = fib_get_table(net, tbid);
5992 if (unlikely(!tb))
5993 return BPF_FIB_LKUP_RET_NOT_FWDED;
5994
5995 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
5996 } else {
5997 if (flags & BPF_FIB_LOOKUP_MARK)
5998 fl4.flowi4_mark = params->mark;
5999 else
6000 fl4.flowi4_mark = 0;
6001 fl4.flowi4_secid = 0;
6002 fl4.flowi4_tun_key.tun_id = 0;
6003 fl4.flowi4_uid = sock_net_uid(net, NULL);
6004
6005 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
6006 }
6007
6008 if (err) {
6009 /* map fib lookup errors to RTN_ type */
6010 if (err == -EINVAL)
6011 return BPF_FIB_LKUP_RET_BLACKHOLE;
6012 if (err == -EHOSTUNREACH)
6013 return BPF_FIB_LKUP_RET_UNREACHABLE;
6014 if (err == -EACCES)
6015 return BPF_FIB_LKUP_RET_PROHIBIT;
6016
6017 return BPF_FIB_LKUP_RET_NOT_FWDED;
6018 }
6019
6020 if (res.type != RTN_UNICAST)
6021 return BPF_FIB_LKUP_RET_NOT_FWDED;
6022
6023 if (fib_info_num_path(res.fi) > 1)
6024 fib_select_path(net, &res, &fl4, NULL);
6025
6026 if (check_mtu) {
6027 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
6028 if (params->tot_len > mtu) {
6029 params->mtu_result = mtu; /* union with tot_len */
6030 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
6031 }
6032 }
6033
6034 nhc = res.nhc;
6035
6036 /* do not handle lwt encaps right now */
6037 if (nhc->nhc_lwtstate)
6038 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
6039
6040 dev = nhc->nhc_dev;
6041
6042 params->rt_metric = res.fi->fib_priority;
6043 params->ifindex = dev->ifindex;
6044
6045 if (flags & BPF_FIB_LOOKUP_SRC)
6046 params->ipv4_src = fib_result_prefsrc(net, &res);
6047
6048 /* xdp and cls_bpf programs are run in RCU-bh so
6049 * rcu_read_lock_bh is not needed here
6050 */
6051 if (likely(nhc->nhc_gw_family != AF_INET6)) {
6052 if (nhc->nhc_gw_family)
6053 params->ipv4_dst = nhc->nhc_gw.ipv4;
6054 } else {
6055 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
6056
6057 params->family = AF_INET6;
6058 *dst = nhc->nhc_gw.ipv6;
6059 }
6060
6061 if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
6062 goto set_fwd_params;
6063
6064 if (likely(nhc->nhc_gw_family != AF_INET6))
6065 neigh = __ipv4_neigh_lookup_noref(dev,
6066 (__force u32)params->ipv4_dst);
6067 else
6068 neigh = __ipv6_neigh_lookup_noref_stub(dev, params->ipv6_dst);
6069
6070 if (!neigh || !(READ_ONCE(neigh->nud_state) & NUD_VALID))
6071 return BPF_FIB_LKUP_RET_NO_NEIGH;
6072 memcpy(params->dmac, neigh->ha, ETH_ALEN);
6073 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
6074
6075 set_fwd_params:
6076 return bpf_fib_set_fwd_params(params, mtu);
6077 }
6078 #endif
6079
6080 #if IS_ENABLED(CONFIG_IPV6)
bpf_ipv6_fib_lookup(struct net * net,struct bpf_fib_lookup * params,u32 flags,bool check_mtu)6081 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
6082 u32 flags, bool check_mtu)
6083 {
6084 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
6085 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
6086 struct fib6_result res = {};
6087 struct neighbour *neigh;
6088 struct net_device *dev;
6089 struct inet6_dev *idev;
6090 struct flowi6 fl6;
6091 int strict = 0;
6092 int oif, err;
6093 u32 mtu = 0;
6094
6095 /* link local addresses are never forwarded */
6096 if (rt6_need_strict(dst) || rt6_need_strict(src))
6097 return BPF_FIB_LKUP_RET_NOT_FWDED;
6098
6099 dev = dev_get_by_index_rcu(net, params->ifindex);
6100 if (unlikely(!dev))
6101 return -ENODEV;
6102
6103 idev = __in6_dev_get_safely(dev);
6104 if (unlikely(!idev || !READ_ONCE(idev->cnf.forwarding)))
6105 return BPF_FIB_LKUP_RET_FWD_DISABLED;
6106
6107 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
6108 fl6.flowi6_iif = 1;
6109 oif = fl6.flowi6_oif = params->ifindex;
6110 } else {
6111 oif = fl6.flowi6_iif = params->ifindex;
6112 fl6.flowi6_oif = 0;
6113 strict = RT6_LOOKUP_F_HAS_SADDR;
6114 }
6115 fl6.flowlabel = params->flowinfo;
6116 fl6.flowi6_scope = 0;
6117 fl6.flowi6_flags = 0;
6118 fl6.mp_hash = 0;
6119
6120 fl6.flowi6_proto = params->l4_protocol;
6121 fl6.daddr = *dst;
6122 fl6.saddr = *src;
6123 fl6.fl6_sport = params->sport;
6124 fl6.fl6_dport = params->dport;
6125
6126 if (flags & BPF_FIB_LOOKUP_DIRECT) {
6127 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
6128 struct fib6_table *tb;
6129
6130 if (flags & BPF_FIB_LOOKUP_TBID) {
6131 tbid = params->tbid;
6132 /* zero out for vlan output */
6133 params->tbid = 0;
6134 }
6135
6136 tb = ipv6_stub->fib6_get_table(net, tbid);
6137 if (unlikely(!tb))
6138 return BPF_FIB_LKUP_RET_NOT_FWDED;
6139
6140 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
6141 strict);
6142 } else {
6143 if (flags & BPF_FIB_LOOKUP_MARK)
6144 fl6.flowi6_mark = params->mark;
6145 else
6146 fl6.flowi6_mark = 0;
6147 fl6.flowi6_secid = 0;
6148 fl6.flowi6_tun_key.tun_id = 0;
6149 fl6.flowi6_uid = sock_net_uid(net, NULL);
6150
6151 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
6152 }
6153
6154 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
6155 res.f6i == net->ipv6.fib6_null_entry))
6156 return BPF_FIB_LKUP_RET_NOT_FWDED;
6157
6158 switch (res.fib6_type) {
6159 /* only unicast is forwarded */
6160 case RTN_UNICAST:
6161 break;
6162 case RTN_BLACKHOLE:
6163 return BPF_FIB_LKUP_RET_BLACKHOLE;
6164 case RTN_UNREACHABLE:
6165 return BPF_FIB_LKUP_RET_UNREACHABLE;
6166 case RTN_PROHIBIT:
6167 return BPF_FIB_LKUP_RET_PROHIBIT;
6168 default:
6169 return BPF_FIB_LKUP_RET_NOT_FWDED;
6170 }
6171
6172 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
6173 fl6.flowi6_oif != 0, NULL, strict);
6174
6175 if (check_mtu) {
6176 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
6177 if (params->tot_len > mtu) {
6178 params->mtu_result = mtu; /* union with tot_len */
6179 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
6180 }
6181 }
6182
6183 if (res.nh->fib_nh_lws)
6184 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
6185
6186 if (res.nh->fib_nh_gw_family)
6187 *dst = res.nh->fib_nh_gw6;
6188
6189 dev = res.nh->fib_nh_dev;
6190 params->rt_metric = res.f6i->fib6_metric;
6191 params->ifindex = dev->ifindex;
6192
6193 if (flags & BPF_FIB_LOOKUP_SRC) {
6194 if (res.f6i->fib6_prefsrc.plen) {
6195 *src = res.f6i->fib6_prefsrc.addr;
6196 } else {
6197 err = ipv6_bpf_stub->ipv6_dev_get_saddr(net, dev,
6198 &fl6.daddr, 0,
6199 src);
6200 if (err)
6201 return BPF_FIB_LKUP_RET_NO_SRC_ADDR;
6202 }
6203 }
6204
6205 if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
6206 goto set_fwd_params;
6207
6208 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
6209 * not needed here.
6210 */
6211 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
6212 if (!neigh || !(READ_ONCE(neigh->nud_state) & NUD_VALID))
6213 return BPF_FIB_LKUP_RET_NO_NEIGH;
6214 memcpy(params->dmac, neigh->ha, ETH_ALEN);
6215 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
6216
6217 set_fwd_params:
6218 return bpf_fib_set_fwd_params(params, mtu);
6219 }
6220 #endif
6221
6222 #define BPF_FIB_LOOKUP_MASK (BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT | \
6223 BPF_FIB_LOOKUP_SKIP_NEIGH | BPF_FIB_LOOKUP_TBID | \
6224 BPF_FIB_LOOKUP_SRC | BPF_FIB_LOOKUP_MARK)
6225
BPF_CALL_4(bpf_xdp_fib_lookup,struct xdp_buff *,ctx,struct bpf_fib_lookup *,params,int,plen,u32,flags)6226 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
6227 struct bpf_fib_lookup *, params, int, plen, u32, flags)
6228 {
6229 if (plen < sizeof(*params))
6230 return -EINVAL;
6231
6232 if (flags & ~BPF_FIB_LOOKUP_MASK)
6233 return -EINVAL;
6234
6235 switch (params->family) {
6236 #if IS_ENABLED(CONFIG_INET)
6237 case AF_INET:
6238 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
6239 flags, true);
6240 #endif
6241 #if IS_ENABLED(CONFIG_IPV6)
6242 case AF_INET6:
6243 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
6244 flags, true);
6245 #endif
6246 }
6247 return -EAFNOSUPPORT;
6248 }
6249
6250 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
6251 .func = bpf_xdp_fib_lookup,
6252 .gpl_only = true,
6253 .ret_type = RET_INTEGER,
6254 .arg1_type = ARG_PTR_TO_CTX,
6255 .arg2_type = ARG_PTR_TO_MEM,
6256 .arg3_type = ARG_CONST_SIZE,
6257 .arg4_type = ARG_ANYTHING,
6258 };
6259
BPF_CALL_4(bpf_skb_fib_lookup,struct sk_buff *,skb,struct bpf_fib_lookup *,params,int,plen,u32,flags)6260 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
6261 struct bpf_fib_lookup *, params, int, plen, u32, flags)
6262 {
6263 struct net *net = dev_net(skb->dev);
6264 int rc = -EAFNOSUPPORT;
6265 bool check_mtu = false;
6266
6267 if (plen < sizeof(*params))
6268 return -EINVAL;
6269
6270 if (flags & ~BPF_FIB_LOOKUP_MASK)
6271 return -EINVAL;
6272
6273 if (params->tot_len)
6274 check_mtu = true;
6275
6276 switch (params->family) {
6277 #if IS_ENABLED(CONFIG_INET)
6278 case AF_INET:
6279 rc = bpf_ipv4_fib_lookup(net, params, flags, check_mtu);
6280 break;
6281 #endif
6282 #if IS_ENABLED(CONFIG_IPV6)
6283 case AF_INET6:
6284 rc = bpf_ipv6_fib_lookup(net, params, flags, check_mtu);
6285 break;
6286 #endif
6287 }
6288
6289 if (rc == BPF_FIB_LKUP_RET_SUCCESS && !check_mtu) {
6290 struct net_device *dev;
6291
6292 /* When tot_len isn't provided by user, check skb
6293 * against MTU of FIB lookup resulting net_device
6294 */
6295 dev = dev_get_by_index_rcu(net, params->ifindex);
6296 if (!is_skb_forwardable(dev, skb))
6297 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
6298
6299 params->mtu_result = dev->mtu; /* union with tot_len */
6300 }
6301
6302 return rc;
6303 }
6304
6305 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
6306 .func = bpf_skb_fib_lookup,
6307 .gpl_only = true,
6308 .ret_type = RET_INTEGER,
6309 .arg1_type = ARG_PTR_TO_CTX,
6310 .arg2_type = ARG_PTR_TO_MEM,
6311 .arg3_type = ARG_CONST_SIZE,
6312 .arg4_type = ARG_ANYTHING,
6313 };
6314
__dev_via_ifindex(struct net_device * dev_curr,u32 ifindex)6315 static struct net_device *__dev_via_ifindex(struct net_device *dev_curr,
6316 u32 ifindex)
6317 {
6318 struct net *netns = dev_net(dev_curr);
6319
6320 /* Non-redirect use-cases can use ifindex=0 and save ifindex lookup */
6321 if (ifindex == 0)
6322 return dev_curr;
6323
6324 return dev_get_by_index_rcu(netns, ifindex);
6325 }
6326
BPF_CALL_5(bpf_skb_check_mtu,struct sk_buff *,skb,u32,ifindex,u32 *,mtu_len,s32,len_diff,u64,flags)6327 BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
6328 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6329 {
6330 int ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6331 struct net_device *dev = skb->dev;
6332 int mtu, dev_len, skb_len;
6333
6334 if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
6335 return -EINVAL;
6336 if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
6337 return -EINVAL;
6338
6339 dev = __dev_via_ifindex(dev, ifindex);
6340 if (unlikely(!dev))
6341 return -ENODEV;
6342
6343 mtu = READ_ONCE(dev->mtu);
6344 dev_len = mtu + dev->hard_header_len;
6345
6346 /* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6347 skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len;
6348
6349 skb_len += len_diff; /* minus result pass check */
6350 if (skb_len <= dev_len) {
6351 ret = BPF_MTU_CHK_RET_SUCCESS;
6352 goto out;
6353 }
6354 /* At this point, skb->len exceed MTU, but as it include length of all
6355 * segments, it can still be below MTU. The SKB can possibly get
6356 * re-segmented in transmit path (see validate_xmit_skb). Thus, user
6357 * must choose if segs are to be MTU checked.
6358 */
6359 if (skb_is_gso(skb)) {
6360 ret = BPF_MTU_CHK_RET_SUCCESS;
6361 if (flags & BPF_MTU_CHK_SEGS &&
6362 !skb_gso_validate_network_len(skb, mtu))
6363 ret = BPF_MTU_CHK_RET_SEGS_TOOBIG;
6364 }
6365 out:
6366 *mtu_len = mtu;
6367 return ret;
6368 }
6369
BPF_CALL_5(bpf_xdp_check_mtu,struct xdp_buff *,xdp,u32,ifindex,u32 *,mtu_len,s32,len_diff,u64,flags)6370 BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
6371 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6372 {
6373 struct net_device *dev = xdp->rxq->dev;
6374 int xdp_len = xdp->data_end - xdp->data;
6375 int ret = BPF_MTU_CHK_RET_SUCCESS;
6376 int mtu, dev_len;
6377
6378 /* XDP variant doesn't support multi-buffer segment check (yet) */
6379 if (unlikely(flags))
6380 return -EINVAL;
6381
6382 dev = __dev_via_ifindex(dev, ifindex);
6383 if (unlikely(!dev))
6384 return -ENODEV;
6385
6386 mtu = READ_ONCE(dev->mtu);
6387 dev_len = mtu + dev->hard_header_len;
6388
6389 /* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6390 if (*mtu_len)
6391 xdp_len = *mtu_len + dev->hard_header_len;
6392
6393 xdp_len += len_diff; /* minus result pass check */
6394 if (xdp_len > dev_len)
6395 ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6396
6397 *mtu_len = mtu;
6398 return ret;
6399 }
6400
6401 static const struct bpf_func_proto bpf_skb_check_mtu_proto = {
6402 .func = bpf_skb_check_mtu,
6403 .gpl_only = true,
6404 .ret_type = RET_INTEGER,
6405 .arg1_type = ARG_PTR_TO_CTX,
6406 .arg2_type = ARG_ANYTHING,
6407 .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_WRITE | MEM_ALIGNED,
6408 .arg3_size = sizeof(u32),
6409 .arg4_type = ARG_ANYTHING,
6410 .arg5_type = ARG_ANYTHING,
6411 };
6412
6413 static const struct bpf_func_proto bpf_xdp_check_mtu_proto = {
6414 .func = bpf_xdp_check_mtu,
6415 .gpl_only = true,
6416 .ret_type = RET_INTEGER,
6417 .arg1_type = ARG_PTR_TO_CTX,
6418 .arg2_type = ARG_ANYTHING,
6419 .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_WRITE | MEM_ALIGNED,
6420 .arg3_size = sizeof(u32),
6421 .arg4_type = ARG_ANYTHING,
6422 .arg5_type = ARG_ANYTHING,
6423 };
6424
6425 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
bpf_push_seg6_encap(struct sk_buff * skb,u32 type,void * hdr,u32 len)6426 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
6427 {
6428 int err;
6429 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
6430
6431 if (!seg6_validate_srh(srh, len, false))
6432 return -EINVAL;
6433
6434 switch (type) {
6435 case BPF_LWT_ENCAP_SEG6_INLINE:
6436 if (skb->protocol != htons(ETH_P_IPV6))
6437 return -EBADMSG;
6438
6439 err = seg6_do_srh_inline(skb, srh);
6440 break;
6441 case BPF_LWT_ENCAP_SEG6:
6442 skb_reset_inner_headers(skb);
6443 skb->encapsulation = 1;
6444 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
6445 break;
6446 default:
6447 return -EINVAL;
6448 }
6449
6450 bpf_compute_data_pointers(skb);
6451 if (err)
6452 return err;
6453
6454 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
6455
6456 return seg6_lookup_nexthop(skb, NULL, 0);
6457 }
6458 #endif /* CONFIG_IPV6_SEG6_BPF */
6459
6460 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
bpf_push_ip_encap(struct sk_buff * skb,void * hdr,u32 len,bool ingress)6461 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
6462 bool ingress)
6463 {
6464 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
6465 }
6466 #endif
6467
BPF_CALL_4(bpf_lwt_in_push_encap,struct sk_buff *,skb,u32,type,void *,hdr,u32,len)6468 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
6469 u32, len)
6470 {
6471 switch (type) {
6472 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6473 case BPF_LWT_ENCAP_SEG6:
6474 case BPF_LWT_ENCAP_SEG6_INLINE:
6475 return bpf_push_seg6_encap(skb, type, hdr, len);
6476 #endif
6477 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6478 case BPF_LWT_ENCAP_IP:
6479 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
6480 #endif
6481 default:
6482 return -EINVAL;
6483 }
6484 }
6485
BPF_CALL_4(bpf_lwt_xmit_push_encap,struct sk_buff *,skb,u32,type,void *,hdr,u32,len)6486 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
6487 void *, hdr, u32, len)
6488 {
6489 switch (type) {
6490 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6491 case BPF_LWT_ENCAP_IP:
6492 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
6493 #endif
6494 default:
6495 return -EINVAL;
6496 }
6497 }
6498
6499 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
6500 .func = bpf_lwt_in_push_encap,
6501 .gpl_only = false,
6502 .ret_type = RET_INTEGER,
6503 .arg1_type = ARG_PTR_TO_CTX,
6504 .arg2_type = ARG_ANYTHING,
6505 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6506 .arg4_type = ARG_CONST_SIZE
6507 };
6508
6509 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
6510 .func = bpf_lwt_xmit_push_encap,
6511 .gpl_only = false,
6512 .ret_type = RET_INTEGER,
6513 .arg1_type = ARG_PTR_TO_CTX,
6514 .arg2_type = ARG_ANYTHING,
6515 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6516 .arg4_type = ARG_CONST_SIZE
6517 };
6518
6519 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
BPF_CALL_4(bpf_lwt_seg6_store_bytes,struct sk_buff *,skb,u32,offset,const void *,from,u32,len)6520 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
6521 const void *, from, u32, len)
6522 {
6523 struct seg6_bpf_srh_state *srh_state =
6524 this_cpu_ptr(&seg6_bpf_srh_states);
6525 struct ipv6_sr_hdr *srh = srh_state->srh;
6526 void *srh_tlvs, *srh_end, *ptr;
6527 int srhoff = 0;
6528
6529 lockdep_assert_held(&srh_state->bh_lock);
6530 if (srh == NULL)
6531 return -EINVAL;
6532
6533 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
6534 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
6535
6536 ptr = skb->data + offset;
6537 if (ptr >= srh_tlvs && ptr + len <= srh_end)
6538 srh_state->valid = false;
6539 else if (ptr < (void *)&srh->flags ||
6540 ptr + len > (void *)&srh->segments)
6541 return -EFAULT;
6542
6543 if (unlikely(bpf_try_make_writable(skb, offset + len)))
6544 return -EFAULT;
6545 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6546 return -EINVAL;
6547 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6548
6549 memcpy(skb->data + offset, from, len);
6550 return 0;
6551 }
6552
6553 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
6554 .func = bpf_lwt_seg6_store_bytes,
6555 .gpl_only = false,
6556 .ret_type = RET_INTEGER,
6557 .arg1_type = ARG_PTR_TO_CTX,
6558 .arg2_type = ARG_ANYTHING,
6559 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6560 .arg4_type = ARG_CONST_SIZE
6561 };
6562
bpf_update_srh_state(struct sk_buff * skb)6563 static void bpf_update_srh_state(struct sk_buff *skb)
6564 {
6565 struct seg6_bpf_srh_state *srh_state =
6566 this_cpu_ptr(&seg6_bpf_srh_states);
6567 int srhoff = 0;
6568
6569 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
6570 srh_state->srh = NULL;
6571 } else {
6572 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6573 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
6574 srh_state->valid = true;
6575 }
6576 }
6577
BPF_CALL_4(bpf_lwt_seg6_action,struct sk_buff *,skb,u32,action,void *,param,u32,param_len)6578 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
6579 u32, action, void *, param, u32, param_len)
6580 {
6581 struct seg6_bpf_srh_state *srh_state =
6582 this_cpu_ptr(&seg6_bpf_srh_states);
6583 int hdroff = 0;
6584 int err;
6585
6586 lockdep_assert_held(&srh_state->bh_lock);
6587 switch (action) {
6588 case SEG6_LOCAL_ACTION_END_X:
6589 if (!seg6_bpf_has_valid_srh(skb))
6590 return -EBADMSG;
6591 if (param_len != sizeof(struct in6_addr))
6592 return -EINVAL;
6593 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
6594 case SEG6_LOCAL_ACTION_END_T:
6595 if (!seg6_bpf_has_valid_srh(skb))
6596 return -EBADMSG;
6597 if (param_len != sizeof(int))
6598 return -EINVAL;
6599 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6600 case SEG6_LOCAL_ACTION_END_DT6:
6601 if (!seg6_bpf_has_valid_srh(skb))
6602 return -EBADMSG;
6603 if (param_len != sizeof(int))
6604 return -EINVAL;
6605
6606 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
6607 return -EBADMSG;
6608 if (!pskb_pull(skb, hdroff))
6609 return -EBADMSG;
6610
6611 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
6612 skb_reset_network_header(skb);
6613 skb_reset_transport_header(skb);
6614 skb->encapsulation = 0;
6615
6616 bpf_compute_data_pointers(skb);
6617 bpf_update_srh_state(skb);
6618 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6619 case SEG6_LOCAL_ACTION_END_B6:
6620 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6621 return -EBADMSG;
6622 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
6623 param, param_len);
6624 if (!err)
6625 bpf_update_srh_state(skb);
6626
6627 return err;
6628 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
6629 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6630 return -EBADMSG;
6631 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
6632 param, param_len);
6633 if (!err)
6634 bpf_update_srh_state(skb);
6635
6636 return err;
6637 default:
6638 return -EINVAL;
6639 }
6640 }
6641
6642 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
6643 .func = bpf_lwt_seg6_action,
6644 .gpl_only = false,
6645 .ret_type = RET_INTEGER,
6646 .arg1_type = ARG_PTR_TO_CTX,
6647 .arg2_type = ARG_ANYTHING,
6648 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6649 .arg4_type = ARG_CONST_SIZE
6650 };
6651
BPF_CALL_3(bpf_lwt_seg6_adjust_srh,struct sk_buff *,skb,u32,offset,s32,len)6652 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
6653 s32, len)
6654 {
6655 struct seg6_bpf_srh_state *srh_state =
6656 this_cpu_ptr(&seg6_bpf_srh_states);
6657 struct ipv6_sr_hdr *srh = srh_state->srh;
6658 void *srh_end, *srh_tlvs, *ptr;
6659 struct ipv6hdr *hdr;
6660 int srhoff = 0;
6661 int ret;
6662
6663 lockdep_assert_held(&srh_state->bh_lock);
6664 if (unlikely(srh == NULL))
6665 return -EINVAL;
6666
6667 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
6668 ((srh->first_segment + 1) << 4));
6669 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
6670 srh_state->hdrlen);
6671 ptr = skb->data + offset;
6672
6673 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
6674 return -EFAULT;
6675 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
6676 return -EFAULT;
6677
6678 if (len > 0) {
6679 ret = skb_cow_head(skb, len);
6680 if (unlikely(ret < 0))
6681 return ret;
6682
6683 ret = bpf_skb_net_hdr_push(skb, offset, len);
6684 } else {
6685 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
6686 }
6687
6688 bpf_compute_data_pointers(skb);
6689 if (unlikely(ret < 0))
6690 return ret;
6691
6692 hdr = (struct ipv6hdr *)skb->data;
6693 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
6694
6695 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6696 return -EINVAL;
6697 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6698 srh_state->hdrlen += len;
6699 srh_state->valid = false;
6700 return 0;
6701 }
6702
6703 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
6704 .func = bpf_lwt_seg6_adjust_srh,
6705 .gpl_only = false,
6706 .ret_type = RET_INTEGER,
6707 .arg1_type = ARG_PTR_TO_CTX,
6708 .arg2_type = ARG_ANYTHING,
6709 .arg3_type = ARG_ANYTHING,
6710 };
6711 #endif /* CONFIG_IPV6_SEG6_BPF */
6712
6713 #ifdef CONFIG_INET
sk_lookup(struct net * net,struct bpf_sock_tuple * tuple,int dif,int sdif,u8 family,u8 proto)6714 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
6715 int dif, int sdif, u8 family, u8 proto)
6716 {
6717 struct inet_hashinfo *hinfo = net->ipv4.tcp_death_row.hashinfo;
6718 bool refcounted = false;
6719 struct sock *sk = NULL;
6720
6721 if (family == AF_INET) {
6722 __be32 src4 = tuple->ipv4.saddr;
6723 __be32 dst4 = tuple->ipv4.daddr;
6724
6725 if (proto == IPPROTO_TCP)
6726 sk = __inet_lookup(net, hinfo, NULL, 0,
6727 src4, tuple->ipv4.sport,
6728 dst4, tuple->ipv4.dport,
6729 dif, sdif, &refcounted);
6730 else
6731 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
6732 dst4, tuple->ipv4.dport,
6733 dif, sdif, net->ipv4.udp_table, NULL);
6734 #if IS_ENABLED(CONFIG_IPV6)
6735 } else {
6736 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
6737 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
6738
6739 if (proto == IPPROTO_TCP)
6740 sk = __inet6_lookup(net, hinfo, NULL, 0,
6741 src6, tuple->ipv6.sport,
6742 dst6, ntohs(tuple->ipv6.dport),
6743 dif, sdif, &refcounted);
6744 else if (likely(ipv6_bpf_stub))
6745 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
6746 src6, tuple->ipv6.sport,
6747 dst6, tuple->ipv6.dport,
6748 dif, sdif,
6749 net->ipv4.udp_table, NULL);
6750 #endif
6751 }
6752
6753 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
6754 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6755 sk = NULL;
6756 }
6757 return sk;
6758 }
6759
6760 /* bpf_skc_lookup performs the core lookup for different types of sockets,
6761 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
6762 */
6763 static struct sock *
__bpf_skc_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,struct net * caller_net,u32 ifindex,u8 proto,u64 netns_id,u64 flags,int sdif)6764 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6765 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6766 u64 flags, int sdif)
6767 {
6768 struct sock *sk = NULL;
6769 struct net *net;
6770 u8 family;
6771
6772 if (len == sizeof(tuple->ipv4))
6773 family = AF_INET;
6774 else if (len == sizeof(tuple->ipv6))
6775 family = AF_INET6;
6776 else
6777 return NULL;
6778
6779 if (unlikely(flags || !((s32)netns_id < 0 || netns_id <= S32_MAX)))
6780 goto out;
6781
6782 if (sdif < 0) {
6783 if (family == AF_INET)
6784 sdif = inet_sdif(skb);
6785 else
6786 sdif = inet6_sdif(skb);
6787 }
6788
6789 if ((s32)netns_id < 0) {
6790 net = caller_net;
6791 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6792 } else {
6793 net = get_net_ns_by_id(caller_net, netns_id);
6794 if (unlikely(!net))
6795 goto out;
6796 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6797 put_net(net);
6798 }
6799
6800 out:
6801 return sk;
6802 }
6803
6804 static struct sock *
__bpf_sk_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,struct net * caller_net,u32 ifindex,u8 proto,u64 netns_id,u64 flags,int sdif)6805 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6806 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6807 u64 flags, int sdif)
6808 {
6809 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
6810 ifindex, proto, netns_id, flags,
6811 sdif);
6812
6813 if (sk) {
6814 struct sock *sk2 = sk_to_full_sk(sk);
6815
6816 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6817 * sock refcnt is decremented to prevent a request_sock leak.
6818 */
6819 if (!sk_fullsock(sk2))
6820 sk2 = NULL;
6821 if (sk2 != sk) {
6822 sock_gen_put(sk);
6823 /* Ensure there is no need to bump sk2 refcnt */
6824 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6825 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6826 return NULL;
6827 }
6828 sk = sk2;
6829 }
6830 }
6831
6832 return sk;
6833 }
6834
6835 static struct sock *
bpf_skc_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,u8 proto,u64 netns_id,u64 flags)6836 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6837 u8 proto, u64 netns_id, u64 flags)
6838 {
6839 struct net *caller_net;
6840 int ifindex;
6841
6842 if (skb->dev) {
6843 caller_net = dev_net(skb->dev);
6844 ifindex = skb->dev->ifindex;
6845 } else {
6846 caller_net = sock_net(skb->sk);
6847 ifindex = 0;
6848 }
6849
6850 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
6851 netns_id, flags, -1);
6852 }
6853
6854 static struct sock *
bpf_sk_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,u8 proto,u64 netns_id,u64 flags)6855 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6856 u8 proto, u64 netns_id, u64 flags)
6857 {
6858 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
6859 flags);
6860
6861 if (sk) {
6862 struct sock *sk2 = sk_to_full_sk(sk);
6863
6864 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6865 * sock refcnt is decremented to prevent a request_sock leak.
6866 */
6867 if (!sk_fullsock(sk2))
6868 sk2 = NULL;
6869 if (sk2 != sk) {
6870 sock_gen_put(sk);
6871 /* Ensure there is no need to bump sk2 refcnt */
6872 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6873 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6874 return NULL;
6875 }
6876 sk = sk2;
6877 }
6878 }
6879
6880 return sk;
6881 }
6882
BPF_CALL_5(bpf_skc_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6883 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
6884 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6885 {
6886 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
6887 netns_id, flags);
6888 }
6889
6890 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
6891 .func = bpf_skc_lookup_tcp,
6892 .gpl_only = false,
6893 .pkt_access = true,
6894 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6895 .arg1_type = ARG_PTR_TO_CTX,
6896 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6897 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
6898 .arg4_type = ARG_ANYTHING,
6899 .arg5_type = ARG_ANYTHING,
6900 };
6901
BPF_CALL_5(bpf_sk_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6902 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
6903 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6904 {
6905 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
6906 netns_id, flags);
6907 }
6908
6909 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
6910 .func = bpf_sk_lookup_tcp,
6911 .gpl_only = false,
6912 .pkt_access = true,
6913 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6914 .arg1_type = ARG_PTR_TO_CTX,
6915 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6916 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
6917 .arg4_type = ARG_ANYTHING,
6918 .arg5_type = ARG_ANYTHING,
6919 };
6920
BPF_CALL_5(bpf_sk_lookup_udp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6921 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
6922 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6923 {
6924 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
6925 netns_id, flags);
6926 }
6927
6928 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
6929 .func = bpf_sk_lookup_udp,
6930 .gpl_only = false,
6931 .pkt_access = true,
6932 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6933 .arg1_type = ARG_PTR_TO_CTX,
6934 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6935 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
6936 .arg4_type = ARG_ANYTHING,
6937 .arg5_type = ARG_ANYTHING,
6938 };
6939
BPF_CALL_5(bpf_tc_skc_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6940 BPF_CALL_5(bpf_tc_skc_lookup_tcp, struct sk_buff *, skb,
6941 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6942 {
6943 struct net_device *dev = skb->dev;
6944 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6945 struct net *caller_net = dev_net(dev);
6946
6947 return (unsigned long)__bpf_skc_lookup(skb, tuple, len, caller_net,
6948 ifindex, IPPROTO_TCP, netns_id,
6949 flags, sdif);
6950 }
6951
6952 static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = {
6953 .func = bpf_tc_skc_lookup_tcp,
6954 .gpl_only = false,
6955 .pkt_access = true,
6956 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6957 .arg1_type = ARG_PTR_TO_CTX,
6958 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6959 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
6960 .arg4_type = ARG_ANYTHING,
6961 .arg5_type = ARG_ANYTHING,
6962 };
6963
BPF_CALL_5(bpf_tc_sk_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6964 BPF_CALL_5(bpf_tc_sk_lookup_tcp, struct sk_buff *, skb,
6965 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6966 {
6967 struct net_device *dev = skb->dev;
6968 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6969 struct net *caller_net = dev_net(dev);
6970
6971 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net,
6972 ifindex, IPPROTO_TCP, netns_id,
6973 flags, sdif);
6974 }
6975
6976 static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = {
6977 .func = bpf_tc_sk_lookup_tcp,
6978 .gpl_only = false,
6979 .pkt_access = true,
6980 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6981 .arg1_type = ARG_PTR_TO_CTX,
6982 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6983 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
6984 .arg4_type = ARG_ANYTHING,
6985 .arg5_type = ARG_ANYTHING,
6986 };
6987
BPF_CALL_5(bpf_tc_sk_lookup_udp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6988 BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb,
6989 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6990 {
6991 struct net_device *dev = skb->dev;
6992 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6993 struct net *caller_net = dev_net(dev);
6994
6995 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net,
6996 ifindex, IPPROTO_UDP, netns_id,
6997 flags, sdif);
6998 }
6999
7000 static const struct bpf_func_proto bpf_tc_sk_lookup_udp_proto = {
7001 .func = bpf_tc_sk_lookup_udp,
7002 .gpl_only = false,
7003 .pkt_access = true,
7004 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7005 .arg1_type = ARG_PTR_TO_CTX,
7006 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7007 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7008 .arg4_type = ARG_ANYTHING,
7009 .arg5_type = ARG_ANYTHING,
7010 };
7011
BPF_CALL_1(bpf_sk_release,struct sock *,sk)7012 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
7013 {
7014 if (sk && sk_is_refcounted(sk))
7015 sock_gen_put(sk);
7016 return 0;
7017 }
7018
7019 static const struct bpf_func_proto bpf_sk_release_proto = {
7020 .func = bpf_sk_release,
7021 .gpl_only = false,
7022 .ret_type = RET_INTEGER,
7023 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | OBJ_RELEASE,
7024 };
7025
BPF_CALL_5(bpf_xdp_sk_lookup_udp,struct xdp_buff *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u32,netns_id,u64,flags)7026 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
7027 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
7028 {
7029 struct net_device *dev = ctx->rxq->dev;
7030 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
7031 struct net *caller_net = dev_net(dev);
7032
7033 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
7034 ifindex, IPPROTO_UDP, netns_id,
7035 flags, sdif);
7036 }
7037
7038 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
7039 .func = bpf_xdp_sk_lookup_udp,
7040 .gpl_only = false,
7041 .pkt_access = true,
7042 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7043 .arg1_type = ARG_PTR_TO_CTX,
7044 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7045 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7046 .arg4_type = ARG_ANYTHING,
7047 .arg5_type = ARG_ANYTHING,
7048 };
7049
BPF_CALL_5(bpf_xdp_skc_lookup_tcp,struct xdp_buff *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u32,netns_id,u64,flags)7050 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
7051 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
7052 {
7053 struct net_device *dev = ctx->rxq->dev;
7054 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
7055 struct net *caller_net = dev_net(dev);
7056
7057 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
7058 ifindex, IPPROTO_TCP, netns_id,
7059 flags, sdif);
7060 }
7061
7062 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
7063 .func = bpf_xdp_skc_lookup_tcp,
7064 .gpl_only = false,
7065 .pkt_access = true,
7066 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
7067 .arg1_type = ARG_PTR_TO_CTX,
7068 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7069 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7070 .arg4_type = ARG_ANYTHING,
7071 .arg5_type = ARG_ANYTHING,
7072 };
7073
BPF_CALL_5(bpf_xdp_sk_lookup_tcp,struct xdp_buff *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u32,netns_id,u64,flags)7074 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
7075 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
7076 {
7077 struct net_device *dev = ctx->rxq->dev;
7078 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
7079 struct net *caller_net = dev_net(dev);
7080
7081 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
7082 ifindex, IPPROTO_TCP, netns_id,
7083 flags, sdif);
7084 }
7085
7086 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
7087 .func = bpf_xdp_sk_lookup_tcp,
7088 .gpl_only = false,
7089 .pkt_access = true,
7090 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7091 .arg1_type = ARG_PTR_TO_CTX,
7092 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7093 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7094 .arg4_type = ARG_ANYTHING,
7095 .arg5_type = ARG_ANYTHING,
7096 };
7097
BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp,struct bpf_sock_addr_kern *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7098 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
7099 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7100 {
7101 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
7102 sock_net(ctx->sk), 0,
7103 IPPROTO_TCP, netns_id, flags,
7104 -1);
7105 }
7106
7107 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
7108 .func = bpf_sock_addr_skc_lookup_tcp,
7109 .gpl_only = false,
7110 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
7111 .arg1_type = ARG_PTR_TO_CTX,
7112 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7113 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7114 .arg4_type = ARG_ANYTHING,
7115 .arg5_type = ARG_ANYTHING,
7116 };
7117
BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp,struct bpf_sock_addr_kern *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7118 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
7119 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7120 {
7121 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
7122 sock_net(ctx->sk), 0, IPPROTO_TCP,
7123 netns_id, flags, -1);
7124 }
7125
7126 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
7127 .func = bpf_sock_addr_sk_lookup_tcp,
7128 .gpl_only = false,
7129 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7130 .arg1_type = ARG_PTR_TO_CTX,
7131 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7132 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7133 .arg4_type = ARG_ANYTHING,
7134 .arg5_type = ARG_ANYTHING,
7135 };
7136
BPF_CALL_5(bpf_sock_addr_sk_lookup_udp,struct bpf_sock_addr_kern *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7137 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
7138 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7139 {
7140 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
7141 sock_net(ctx->sk), 0, IPPROTO_UDP,
7142 netns_id, flags, -1);
7143 }
7144
7145 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
7146 .func = bpf_sock_addr_sk_lookup_udp,
7147 .gpl_only = false,
7148 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7149 .arg1_type = ARG_PTR_TO_CTX,
7150 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7151 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7152 .arg4_type = ARG_ANYTHING,
7153 .arg5_type = ARG_ANYTHING,
7154 };
7155
bpf_tcp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)7156 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
7157 struct bpf_insn_access_aux *info)
7158 {
7159 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
7160 icsk_retransmits))
7161 return false;
7162
7163 if (off % size != 0)
7164 return false;
7165
7166 switch (off) {
7167 case offsetof(struct bpf_tcp_sock, bytes_received):
7168 case offsetof(struct bpf_tcp_sock, bytes_acked):
7169 return size == sizeof(__u64);
7170 default:
7171 return size == sizeof(__u32);
7172 }
7173 }
7174
bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)7175 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
7176 const struct bpf_insn *si,
7177 struct bpf_insn *insn_buf,
7178 struct bpf_prog *prog, u32 *target_size)
7179 {
7180 struct bpf_insn *insn = insn_buf;
7181
7182 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
7183 do { \
7184 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
7185 sizeof_field(struct bpf_tcp_sock, FIELD)); \
7186 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
7187 si->dst_reg, si->src_reg, \
7188 offsetof(struct tcp_sock, FIELD)); \
7189 } while (0)
7190
7191 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
7192 do { \
7193 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
7194 FIELD) > \
7195 sizeof_field(struct bpf_tcp_sock, FIELD)); \
7196 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7197 struct inet_connection_sock, \
7198 FIELD), \
7199 si->dst_reg, si->src_reg, \
7200 offsetof( \
7201 struct inet_connection_sock, \
7202 FIELD)); \
7203 } while (0)
7204
7205 BTF_TYPE_EMIT(struct bpf_tcp_sock);
7206
7207 switch (si->off) {
7208 case offsetof(struct bpf_tcp_sock, rtt_min):
7209 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
7210 sizeof(struct minmax));
7211 BUILD_BUG_ON(sizeof(struct minmax) <
7212 sizeof(struct minmax_sample));
7213
7214 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7215 offsetof(struct tcp_sock, rtt_min) +
7216 offsetof(struct minmax_sample, v));
7217 break;
7218 case offsetof(struct bpf_tcp_sock, snd_cwnd):
7219 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
7220 break;
7221 case offsetof(struct bpf_tcp_sock, srtt_us):
7222 BPF_TCP_SOCK_GET_COMMON(srtt_us);
7223 break;
7224 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
7225 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
7226 break;
7227 case offsetof(struct bpf_tcp_sock, rcv_nxt):
7228 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
7229 break;
7230 case offsetof(struct bpf_tcp_sock, snd_nxt):
7231 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
7232 break;
7233 case offsetof(struct bpf_tcp_sock, snd_una):
7234 BPF_TCP_SOCK_GET_COMMON(snd_una);
7235 break;
7236 case offsetof(struct bpf_tcp_sock, mss_cache):
7237 BPF_TCP_SOCK_GET_COMMON(mss_cache);
7238 break;
7239 case offsetof(struct bpf_tcp_sock, ecn_flags):
7240 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
7241 break;
7242 case offsetof(struct bpf_tcp_sock, rate_delivered):
7243 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
7244 break;
7245 case offsetof(struct bpf_tcp_sock, rate_interval_us):
7246 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
7247 break;
7248 case offsetof(struct bpf_tcp_sock, packets_out):
7249 BPF_TCP_SOCK_GET_COMMON(packets_out);
7250 break;
7251 case offsetof(struct bpf_tcp_sock, retrans_out):
7252 BPF_TCP_SOCK_GET_COMMON(retrans_out);
7253 break;
7254 case offsetof(struct bpf_tcp_sock, total_retrans):
7255 BPF_TCP_SOCK_GET_COMMON(total_retrans);
7256 break;
7257 case offsetof(struct bpf_tcp_sock, segs_in):
7258 BPF_TCP_SOCK_GET_COMMON(segs_in);
7259 break;
7260 case offsetof(struct bpf_tcp_sock, data_segs_in):
7261 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
7262 break;
7263 case offsetof(struct bpf_tcp_sock, segs_out):
7264 BPF_TCP_SOCK_GET_COMMON(segs_out);
7265 break;
7266 case offsetof(struct bpf_tcp_sock, data_segs_out):
7267 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
7268 break;
7269 case offsetof(struct bpf_tcp_sock, lost_out):
7270 BPF_TCP_SOCK_GET_COMMON(lost_out);
7271 break;
7272 case offsetof(struct bpf_tcp_sock, sacked_out):
7273 BPF_TCP_SOCK_GET_COMMON(sacked_out);
7274 break;
7275 case offsetof(struct bpf_tcp_sock, bytes_received):
7276 BPF_TCP_SOCK_GET_COMMON(bytes_received);
7277 break;
7278 case offsetof(struct bpf_tcp_sock, bytes_acked):
7279 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
7280 break;
7281 case offsetof(struct bpf_tcp_sock, dsack_dups):
7282 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
7283 break;
7284 case offsetof(struct bpf_tcp_sock, delivered):
7285 BPF_TCP_SOCK_GET_COMMON(delivered);
7286 break;
7287 case offsetof(struct bpf_tcp_sock, delivered_ce):
7288 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
7289 break;
7290 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
7291 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
7292 break;
7293 }
7294
7295 return insn - insn_buf;
7296 }
7297
BPF_CALL_1(bpf_tcp_sock,struct sock *,sk)7298 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
7299 {
7300 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
7301 return (unsigned long)sk;
7302
7303 return (unsigned long)NULL;
7304 }
7305
7306 const struct bpf_func_proto bpf_tcp_sock_proto = {
7307 .func = bpf_tcp_sock,
7308 .gpl_only = false,
7309 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
7310 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
7311 };
7312
BPF_CALL_1(bpf_get_listener_sock,struct sock *,sk)7313 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
7314 {
7315 sk = sk_to_full_sk(sk);
7316
7317 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
7318 return (unsigned long)sk;
7319
7320 return (unsigned long)NULL;
7321 }
7322
7323 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
7324 .func = bpf_get_listener_sock,
7325 .gpl_only = false,
7326 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7327 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
7328 };
7329
BPF_CALL_1(bpf_skb_ecn_set_ce,struct sk_buff *,skb)7330 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
7331 {
7332 unsigned int iphdr_len;
7333
7334 switch (skb_protocol(skb, true)) {
7335 case cpu_to_be16(ETH_P_IP):
7336 iphdr_len = sizeof(struct iphdr);
7337 break;
7338 case cpu_to_be16(ETH_P_IPV6):
7339 iphdr_len = sizeof(struct ipv6hdr);
7340 break;
7341 default:
7342 return 0;
7343 }
7344
7345 if (skb_headlen(skb) < iphdr_len)
7346 return 0;
7347
7348 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
7349 return 0;
7350
7351 return INET_ECN_set_ce(skb);
7352 }
7353
bpf_xdp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)7354 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
7355 struct bpf_insn_access_aux *info)
7356 {
7357 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
7358 return false;
7359
7360 if (off % size != 0)
7361 return false;
7362
7363 switch (off) {
7364 default:
7365 return size == sizeof(__u32);
7366 }
7367 }
7368
bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)7369 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
7370 const struct bpf_insn *si,
7371 struct bpf_insn *insn_buf,
7372 struct bpf_prog *prog, u32 *target_size)
7373 {
7374 struct bpf_insn *insn = insn_buf;
7375
7376 #define BPF_XDP_SOCK_GET(FIELD) \
7377 do { \
7378 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
7379 sizeof_field(struct bpf_xdp_sock, FIELD)); \
7380 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
7381 si->dst_reg, si->src_reg, \
7382 offsetof(struct xdp_sock, FIELD)); \
7383 } while (0)
7384
7385 switch (si->off) {
7386 case offsetof(struct bpf_xdp_sock, queue_id):
7387 BPF_XDP_SOCK_GET(queue_id);
7388 break;
7389 }
7390
7391 return insn - insn_buf;
7392 }
7393
7394 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
7395 .func = bpf_skb_ecn_set_ce,
7396 .gpl_only = false,
7397 .ret_type = RET_INTEGER,
7398 .arg1_type = ARG_PTR_TO_CTX,
7399 };
7400
BPF_CALL_5(bpf_tcp_check_syncookie,struct sock *,sk,void *,iph,u32,iph_len,struct tcphdr *,th,u32,th_len)7401 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7402 struct tcphdr *, th, u32, th_len)
7403 {
7404 #ifdef CONFIG_SYN_COOKIES
7405 int ret;
7406
7407 if (unlikely(!sk || th_len < sizeof(*th)))
7408 return -EINVAL;
7409
7410 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
7411 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7412 return -EINVAL;
7413
7414 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7415 return -EINVAL;
7416
7417 if (!th->ack || th->rst || th->syn)
7418 return -ENOENT;
7419
7420 if (unlikely(iph_len < sizeof(struct iphdr)))
7421 return -EINVAL;
7422
7423 if (tcp_synq_no_recent_overflow(sk))
7424 return -ENOENT;
7425
7426 /* Both struct iphdr and struct ipv6hdr have the version field at the
7427 * same offset so we can cast to the shorter header (struct iphdr).
7428 */
7429 switch (((struct iphdr *)iph)->version) {
7430 case 4:
7431 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7432 return -EINVAL;
7433
7434 ret = __cookie_v4_check((struct iphdr *)iph, th);
7435 break;
7436
7437 #if IS_BUILTIN(CONFIG_IPV6)
7438 case 6:
7439 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7440 return -EINVAL;
7441
7442 if (sk->sk_family != AF_INET6)
7443 return -EINVAL;
7444
7445 ret = __cookie_v6_check((struct ipv6hdr *)iph, th);
7446 break;
7447 #endif /* CONFIG_IPV6 */
7448
7449 default:
7450 return -EPROTONOSUPPORT;
7451 }
7452
7453 if (ret > 0)
7454 return 0;
7455
7456 return -ENOENT;
7457 #else
7458 return -ENOTSUPP;
7459 #endif
7460 }
7461
7462 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
7463 .func = bpf_tcp_check_syncookie,
7464 .gpl_only = true,
7465 .pkt_access = true,
7466 .ret_type = RET_INTEGER,
7467 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7468 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7469 .arg3_type = ARG_CONST_SIZE,
7470 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7471 .arg5_type = ARG_CONST_SIZE,
7472 };
7473
BPF_CALL_5(bpf_tcp_gen_syncookie,struct sock *,sk,void *,iph,u32,iph_len,struct tcphdr *,th,u32,th_len)7474 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7475 struct tcphdr *, th, u32, th_len)
7476 {
7477 #ifdef CONFIG_SYN_COOKIES
7478 u32 cookie;
7479 u16 mss;
7480
7481 if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4))
7482 return -EINVAL;
7483
7484 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7485 return -EINVAL;
7486
7487 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7488 return -ENOENT;
7489
7490 if (!th->syn || th->ack || th->fin || th->rst)
7491 return -EINVAL;
7492
7493 if (unlikely(iph_len < sizeof(struct iphdr)))
7494 return -EINVAL;
7495
7496 /* Both struct iphdr and struct ipv6hdr have the version field at the
7497 * same offset so we can cast to the shorter header (struct iphdr).
7498 */
7499 switch (((struct iphdr *)iph)->version) {
7500 case 4:
7501 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7502 return -EINVAL;
7503
7504 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
7505 break;
7506
7507 #if IS_BUILTIN(CONFIG_IPV6)
7508 case 6:
7509 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7510 return -EINVAL;
7511
7512 if (sk->sk_family != AF_INET6)
7513 return -EINVAL;
7514
7515 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
7516 break;
7517 #endif /* CONFIG_IPV6 */
7518
7519 default:
7520 return -EPROTONOSUPPORT;
7521 }
7522 if (mss == 0)
7523 return -ENOENT;
7524
7525 return cookie | ((u64)mss << 32);
7526 #else
7527 return -EOPNOTSUPP;
7528 #endif /* CONFIG_SYN_COOKIES */
7529 }
7530
7531 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
7532 .func = bpf_tcp_gen_syncookie,
7533 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
7534 .pkt_access = true,
7535 .ret_type = RET_INTEGER,
7536 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7537 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7538 .arg3_type = ARG_CONST_SIZE,
7539 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7540 .arg5_type = ARG_CONST_SIZE,
7541 };
7542
BPF_CALL_3(bpf_sk_assign,struct sk_buff *,skb,struct sock *,sk,u64,flags)7543 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
7544 {
7545 if (!sk || flags != 0)
7546 return -EINVAL;
7547 if (!skb_at_tc_ingress(skb))
7548 return -EOPNOTSUPP;
7549 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
7550 return -ENETUNREACH;
7551 if (sk_unhashed(sk))
7552 return -EOPNOTSUPP;
7553 if (sk_is_refcounted(sk) &&
7554 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
7555 return -ENOENT;
7556
7557 skb_orphan(skb);
7558 skb->sk = sk;
7559 skb->destructor = sock_pfree;
7560
7561 return 0;
7562 }
7563
7564 static const struct bpf_func_proto bpf_sk_assign_proto = {
7565 .func = bpf_sk_assign,
7566 .gpl_only = false,
7567 .ret_type = RET_INTEGER,
7568 .arg1_type = ARG_PTR_TO_CTX,
7569 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7570 .arg3_type = ARG_ANYTHING,
7571 };
7572
bpf_search_tcp_opt(const u8 * op,const u8 * opend,u8 search_kind,const u8 * magic,u8 magic_len,bool * eol)7573 static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,
7574 u8 search_kind, const u8 *magic,
7575 u8 magic_len, bool *eol)
7576 {
7577 u8 kind, kind_len;
7578
7579 *eol = false;
7580
7581 while (op < opend) {
7582 kind = op[0];
7583
7584 if (kind == TCPOPT_EOL) {
7585 *eol = true;
7586 return ERR_PTR(-ENOMSG);
7587 } else if (kind == TCPOPT_NOP) {
7588 op++;
7589 continue;
7590 }
7591
7592 if (opend - op < 2 || opend - op < op[1] || op[1] < 2)
7593 /* Something is wrong in the received header.
7594 * Follow the TCP stack's tcp_parse_options()
7595 * and just bail here.
7596 */
7597 return ERR_PTR(-EFAULT);
7598
7599 kind_len = op[1];
7600 if (search_kind == kind) {
7601 if (!magic_len)
7602 return op;
7603
7604 if (magic_len > kind_len - 2)
7605 return ERR_PTR(-ENOMSG);
7606
7607 if (!memcmp(&op[2], magic, magic_len))
7608 return op;
7609 }
7610
7611 op += kind_len;
7612 }
7613
7614 return ERR_PTR(-ENOMSG);
7615 }
7616
BPF_CALL_4(bpf_sock_ops_load_hdr_opt,struct bpf_sock_ops_kern *,bpf_sock,void *,search_res,u32,len,u64,flags)7617 BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7618 void *, search_res, u32, len, u64, flags)
7619 {
7620 bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN;
7621 const u8 *op, *opend, *magic, *search = search_res;
7622 u8 search_kind, search_len, copy_len, magic_len;
7623 int ret;
7624
7625 /* 2 byte is the minimal option len except TCPOPT_NOP and
7626 * TCPOPT_EOL which are useless for the bpf prog to learn
7627 * and this helper disallow loading them also.
7628 */
7629 if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN)
7630 return -EINVAL;
7631
7632 search_kind = search[0];
7633 search_len = search[1];
7634
7635 if (search_len > len || search_kind == TCPOPT_NOP ||
7636 search_kind == TCPOPT_EOL)
7637 return -EINVAL;
7638
7639 if (search_kind == TCPOPT_EXP || search_kind == 253) {
7640 /* 16 or 32 bit magic. +2 for kind and kind length */
7641 if (search_len != 4 && search_len != 6)
7642 return -EINVAL;
7643 magic = &search[2];
7644 magic_len = search_len - 2;
7645 } else {
7646 if (search_len)
7647 return -EINVAL;
7648 magic = NULL;
7649 magic_len = 0;
7650 }
7651
7652 if (load_syn) {
7653 ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op);
7654 if (ret < 0)
7655 return ret;
7656
7657 opend = op + ret;
7658 op += sizeof(struct tcphdr);
7659 } else {
7660 if (!bpf_sock->skb ||
7661 bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7662 /* This bpf_sock->op cannot call this helper */
7663 return -EPERM;
7664
7665 opend = bpf_sock->skb_data_end;
7666 op = bpf_sock->skb->data + sizeof(struct tcphdr);
7667 }
7668
7669 op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len,
7670 &eol);
7671 if (IS_ERR(op))
7672 return PTR_ERR(op);
7673
7674 copy_len = op[1];
7675 ret = copy_len;
7676 if (copy_len > len) {
7677 ret = -ENOSPC;
7678 copy_len = len;
7679 }
7680
7681 memcpy(search_res, op, copy_len);
7682 return ret;
7683 }
7684
7685 static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
7686 .func = bpf_sock_ops_load_hdr_opt,
7687 .gpl_only = false,
7688 .ret_type = RET_INTEGER,
7689 .arg1_type = ARG_PTR_TO_CTX,
7690 .arg2_type = ARG_PTR_TO_MEM | MEM_WRITE,
7691 .arg3_type = ARG_CONST_SIZE,
7692 .arg4_type = ARG_ANYTHING,
7693 };
7694
BPF_CALL_4(bpf_sock_ops_store_hdr_opt,struct bpf_sock_ops_kern *,bpf_sock,const void *,from,u32,len,u64,flags)7695 BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7696 const void *, from, u32, len, u64, flags)
7697 {
7698 u8 new_kind, new_kind_len, magic_len = 0, *opend;
7699 const u8 *op, *new_op, *magic = NULL;
7700 struct sk_buff *skb;
7701 bool eol;
7702
7703 if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
7704 return -EPERM;
7705
7706 if (len < 2 || flags)
7707 return -EINVAL;
7708
7709 new_op = from;
7710 new_kind = new_op[0];
7711 new_kind_len = new_op[1];
7712
7713 if (new_kind_len > len || new_kind == TCPOPT_NOP ||
7714 new_kind == TCPOPT_EOL)
7715 return -EINVAL;
7716
7717 if (new_kind_len > bpf_sock->remaining_opt_len)
7718 return -ENOSPC;
7719
7720 /* 253 is another experimental kind */
7721 if (new_kind == TCPOPT_EXP || new_kind == 253) {
7722 if (new_kind_len < 4)
7723 return -EINVAL;
7724 /* Match for the 2 byte magic also.
7725 * RFC 6994: the magic could be 2 or 4 bytes.
7726 * Hence, matching by 2 byte only is on the
7727 * conservative side but it is the right
7728 * thing to do for the 'search-for-duplication'
7729 * purpose.
7730 */
7731 magic = &new_op[2];
7732 magic_len = 2;
7733 }
7734
7735 /* Check for duplication */
7736 skb = bpf_sock->skb;
7737 op = skb->data + sizeof(struct tcphdr);
7738 opend = bpf_sock->skb_data_end;
7739
7740 op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len,
7741 &eol);
7742 if (!IS_ERR(op))
7743 return -EEXIST;
7744
7745 if (PTR_ERR(op) != -ENOMSG)
7746 return PTR_ERR(op);
7747
7748 if (eol)
7749 /* The option has been ended. Treat it as no more
7750 * header option can be written.
7751 */
7752 return -ENOSPC;
7753
7754 /* No duplication found. Store the header option. */
7755 memcpy(opend, from, new_kind_len);
7756
7757 bpf_sock->remaining_opt_len -= new_kind_len;
7758 bpf_sock->skb_data_end += new_kind_len;
7759
7760 return 0;
7761 }
7762
7763 static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {
7764 .func = bpf_sock_ops_store_hdr_opt,
7765 .gpl_only = false,
7766 .ret_type = RET_INTEGER,
7767 .arg1_type = ARG_PTR_TO_CTX,
7768 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7769 .arg3_type = ARG_CONST_SIZE,
7770 .arg4_type = ARG_ANYTHING,
7771 };
7772
BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt,struct bpf_sock_ops_kern *,bpf_sock,u32,len,u64,flags)7773 BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7774 u32, len, u64, flags)
7775 {
7776 if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7777 return -EPERM;
7778
7779 if (flags || len < 2)
7780 return -EINVAL;
7781
7782 if (len > bpf_sock->remaining_opt_len)
7783 return -ENOSPC;
7784
7785 bpf_sock->remaining_opt_len -= len;
7786
7787 return 0;
7788 }
7789
7790 static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = {
7791 .func = bpf_sock_ops_reserve_hdr_opt,
7792 .gpl_only = false,
7793 .ret_type = RET_INTEGER,
7794 .arg1_type = ARG_PTR_TO_CTX,
7795 .arg2_type = ARG_ANYTHING,
7796 .arg3_type = ARG_ANYTHING,
7797 };
7798
BPF_CALL_3(bpf_skb_set_tstamp,struct sk_buff *,skb,u64,tstamp,u32,tstamp_type)7799 BPF_CALL_3(bpf_skb_set_tstamp, struct sk_buff *, skb,
7800 u64, tstamp, u32, tstamp_type)
7801 {
7802 /* skb_clear_delivery_time() is done for inet protocol */
7803 if (skb->protocol != htons(ETH_P_IP) &&
7804 skb->protocol != htons(ETH_P_IPV6))
7805 return -EOPNOTSUPP;
7806
7807 switch (tstamp_type) {
7808 case BPF_SKB_CLOCK_REALTIME:
7809 skb->tstamp = tstamp;
7810 skb->tstamp_type = SKB_CLOCK_REALTIME;
7811 break;
7812 case BPF_SKB_CLOCK_MONOTONIC:
7813 if (!tstamp)
7814 return -EINVAL;
7815 skb->tstamp = tstamp;
7816 skb->tstamp_type = SKB_CLOCK_MONOTONIC;
7817 break;
7818 case BPF_SKB_CLOCK_TAI:
7819 if (!tstamp)
7820 return -EINVAL;
7821 skb->tstamp = tstamp;
7822 skb->tstamp_type = SKB_CLOCK_TAI;
7823 break;
7824 default:
7825 return -EINVAL;
7826 }
7827
7828 return 0;
7829 }
7830
7831 static const struct bpf_func_proto bpf_skb_set_tstamp_proto = {
7832 .func = bpf_skb_set_tstamp,
7833 .gpl_only = false,
7834 .ret_type = RET_INTEGER,
7835 .arg1_type = ARG_PTR_TO_CTX,
7836 .arg2_type = ARG_ANYTHING,
7837 .arg3_type = ARG_ANYTHING,
7838 };
7839
7840 #ifdef CONFIG_SYN_COOKIES
BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4,struct iphdr *,iph,struct tcphdr *,th,u32,th_len)7841 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4, struct iphdr *, iph,
7842 struct tcphdr *, th, u32, th_len)
7843 {
7844 u32 cookie;
7845 u16 mss;
7846
7847 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7848 return -EINVAL;
7849
7850 mss = tcp_parse_mss_option(th, 0) ?: TCP_MSS_DEFAULT;
7851 cookie = __cookie_v4_init_sequence(iph, th, &mss);
7852
7853 return cookie | ((u64)mss << 32);
7854 }
7855
7856 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv4_proto = {
7857 .func = bpf_tcp_raw_gen_syncookie_ipv4,
7858 .gpl_only = true, /* __cookie_v4_init_sequence() is GPL */
7859 .pkt_access = true,
7860 .ret_type = RET_INTEGER,
7861 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7862 .arg1_size = sizeof(struct iphdr),
7863 .arg2_type = ARG_PTR_TO_MEM,
7864 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7865 };
7866
BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6,struct ipv6hdr *,iph,struct tcphdr *,th,u32,th_len)7867 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6, struct ipv6hdr *, iph,
7868 struct tcphdr *, th, u32, th_len)
7869 {
7870 #if IS_BUILTIN(CONFIG_IPV6)
7871 const u16 mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) -
7872 sizeof(struct ipv6hdr);
7873 u32 cookie;
7874 u16 mss;
7875
7876 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7877 return -EINVAL;
7878
7879 mss = tcp_parse_mss_option(th, 0) ?: mss_clamp;
7880 cookie = __cookie_v6_init_sequence(iph, th, &mss);
7881
7882 return cookie | ((u64)mss << 32);
7883 #else
7884 return -EPROTONOSUPPORT;
7885 #endif
7886 }
7887
7888 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv6_proto = {
7889 .func = bpf_tcp_raw_gen_syncookie_ipv6,
7890 .gpl_only = true, /* __cookie_v6_init_sequence() is GPL */
7891 .pkt_access = true,
7892 .ret_type = RET_INTEGER,
7893 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7894 .arg1_size = sizeof(struct ipv6hdr),
7895 .arg2_type = ARG_PTR_TO_MEM,
7896 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7897 };
7898
BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4,struct iphdr *,iph,struct tcphdr *,th)7899 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4, struct iphdr *, iph,
7900 struct tcphdr *, th)
7901 {
7902 if (__cookie_v4_check(iph, th) > 0)
7903 return 0;
7904
7905 return -EACCES;
7906 }
7907
7908 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv4_proto = {
7909 .func = bpf_tcp_raw_check_syncookie_ipv4,
7910 .gpl_only = true, /* __cookie_v4_check is GPL */
7911 .pkt_access = true,
7912 .ret_type = RET_INTEGER,
7913 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7914 .arg1_size = sizeof(struct iphdr),
7915 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7916 .arg2_size = sizeof(struct tcphdr),
7917 };
7918
BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6,struct ipv6hdr *,iph,struct tcphdr *,th)7919 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6, struct ipv6hdr *, iph,
7920 struct tcphdr *, th)
7921 {
7922 #if IS_BUILTIN(CONFIG_IPV6)
7923 if (__cookie_v6_check(iph, th) > 0)
7924 return 0;
7925
7926 return -EACCES;
7927 #else
7928 return -EPROTONOSUPPORT;
7929 #endif
7930 }
7931
7932 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv6_proto = {
7933 .func = bpf_tcp_raw_check_syncookie_ipv6,
7934 .gpl_only = true, /* __cookie_v6_check is GPL */
7935 .pkt_access = true,
7936 .ret_type = RET_INTEGER,
7937 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7938 .arg1_size = sizeof(struct ipv6hdr),
7939 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7940 .arg2_size = sizeof(struct tcphdr),
7941 };
7942 #endif /* CONFIG_SYN_COOKIES */
7943
7944 #endif /* CONFIG_INET */
7945
bpf_helper_changes_pkt_data(enum bpf_func_id func_id)7946 bool bpf_helper_changes_pkt_data(enum bpf_func_id func_id)
7947 {
7948 switch (func_id) {
7949 case BPF_FUNC_clone_redirect:
7950 case BPF_FUNC_l3_csum_replace:
7951 case BPF_FUNC_l4_csum_replace:
7952 case BPF_FUNC_lwt_push_encap:
7953 case BPF_FUNC_lwt_seg6_action:
7954 case BPF_FUNC_lwt_seg6_adjust_srh:
7955 case BPF_FUNC_lwt_seg6_store_bytes:
7956 case BPF_FUNC_msg_pop_data:
7957 case BPF_FUNC_msg_pull_data:
7958 case BPF_FUNC_msg_push_data:
7959 case BPF_FUNC_skb_adjust_room:
7960 case BPF_FUNC_skb_change_head:
7961 case BPF_FUNC_skb_change_proto:
7962 case BPF_FUNC_skb_change_tail:
7963 case BPF_FUNC_skb_pull_data:
7964 case BPF_FUNC_skb_store_bytes:
7965 case BPF_FUNC_skb_vlan_pop:
7966 case BPF_FUNC_skb_vlan_push:
7967 case BPF_FUNC_store_hdr_opt:
7968 case BPF_FUNC_xdp_adjust_head:
7969 case BPF_FUNC_xdp_adjust_meta:
7970 case BPF_FUNC_xdp_adjust_tail:
7971 /* tail-called program could call any of the above */
7972 case BPF_FUNC_tail_call:
7973 return true;
7974 default:
7975 return false;
7976 }
7977 }
7978
7979 const struct bpf_func_proto bpf_event_output_data_proto __weak;
7980 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak;
7981
7982 static const struct bpf_func_proto *
sock_filter_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7983 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7984 {
7985 const struct bpf_func_proto *func_proto;
7986
7987 func_proto = cgroup_common_func_proto(func_id, prog);
7988 if (func_proto)
7989 return func_proto;
7990
7991 func_proto = cgroup_current_func_proto(func_id, prog);
7992 if (func_proto)
7993 return func_proto;
7994
7995 switch (func_id) {
7996 case BPF_FUNC_get_socket_cookie:
7997 return &bpf_get_socket_cookie_sock_proto;
7998 case BPF_FUNC_get_netns_cookie:
7999 return &bpf_get_netns_cookie_sock_proto;
8000 case BPF_FUNC_perf_event_output:
8001 return &bpf_event_output_data_proto;
8002 case BPF_FUNC_sk_storage_get:
8003 return &bpf_sk_storage_get_cg_sock_proto;
8004 case BPF_FUNC_ktime_get_coarse_ns:
8005 return &bpf_ktime_get_coarse_ns_proto;
8006 default:
8007 return bpf_base_func_proto(func_id, prog);
8008 }
8009 }
8010
8011 static const struct bpf_func_proto *
sock_addr_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8012 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8013 {
8014 const struct bpf_func_proto *func_proto;
8015
8016 func_proto = cgroup_common_func_proto(func_id, prog);
8017 if (func_proto)
8018 return func_proto;
8019
8020 func_proto = cgroup_current_func_proto(func_id, prog);
8021 if (func_proto)
8022 return func_proto;
8023
8024 switch (func_id) {
8025 case BPF_FUNC_bind:
8026 switch (prog->expected_attach_type) {
8027 case BPF_CGROUP_INET4_CONNECT:
8028 case BPF_CGROUP_INET6_CONNECT:
8029 return &bpf_bind_proto;
8030 default:
8031 return NULL;
8032 }
8033 case BPF_FUNC_get_socket_cookie:
8034 return &bpf_get_socket_cookie_sock_addr_proto;
8035 case BPF_FUNC_get_netns_cookie:
8036 return &bpf_get_netns_cookie_sock_addr_proto;
8037 case BPF_FUNC_perf_event_output:
8038 return &bpf_event_output_data_proto;
8039 #ifdef CONFIG_INET
8040 case BPF_FUNC_sk_lookup_tcp:
8041 return &bpf_sock_addr_sk_lookup_tcp_proto;
8042 case BPF_FUNC_sk_lookup_udp:
8043 return &bpf_sock_addr_sk_lookup_udp_proto;
8044 case BPF_FUNC_sk_release:
8045 return &bpf_sk_release_proto;
8046 case BPF_FUNC_skc_lookup_tcp:
8047 return &bpf_sock_addr_skc_lookup_tcp_proto;
8048 #endif /* CONFIG_INET */
8049 case BPF_FUNC_sk_storage_get:
8050 return &bpf_sk_storage_get_proto;
8051 case BPF_FUNC_sk_storage_delete:
8052 return &bpf_sk_storage_delete_proto;
8053 case BPF_FUNC_setsockopt:
8054 switch (prog->expected_attach_type) {
8055 case BPF_CGROUP_INET4_BIND:
8056 case BPF_CGROUP_INET6_BIND:
8057 case BPF_CGROUP_INET4_CONNECT:
8058 case BPF_CGROUP_INET6_CONNECT:
8059 case BPF_CGROUP_UNIX_CONNECT:
8060 case BPF_CGROUP_UDP4_RECVMSG:
8061 case BPF_CGROUP_UDP6_RECVMSG:
8062 case BPF_CGROUP_UNIX_RECVMSG:
8063 case BPF_CGROUP_UDP4_SENDMSG:
8064 case BPF_CGROUP_UDP6_SENDMSG:
8065 case BPF_CGROUP_UNIX_SENDMSG:
8066 case BPF_CGROUP_INET4_GETPEERNAME:
8067 case BPF_CGROUP_INET6_GETPEERNAME:
8068 case BPF_CGROUP_UNIX_GETPEERNAME:
8069 case BPF_CGROUP_INET4_GETSOCKNAME:
8070 case BPF_CGROUP_INET6_GETSOCKNAME:
8071 case BPF_CGROUP_UNIX_GETSOCKNAME:
8072 return &bpf_sock_addr_setsockopt_proto;
8073 default:
8074 return NULL;
8075 }
8076 case BPF_FUNC_getsockopt:
8077 switch (prog->expected_attach_type) {
8078 case BPF_CGROUP_INET4_BIND:
8079 case BPF_CGROUP_INET6_BIND:
8080 case BPF_CGROUP_INET4_CONNECT:
8081 case BPF_CGROUP_INET6_CONNECT:
8082 case BPF_CGROUP_UNIX_CONNECT:
8083 case BPF_CGROUP_UDP4_RECVMSG:
8084 case BPF_CGROUP_UDP6_RECVMSG:
8085 case BPF_CGROUP_UNIX_RECVMSG:
8086 case BPF_CGROUP_UDP4_SENDMSG:
8087 case BPF_CGROUP_UDP6_SENDMSG:
8088 case BPF_CGROUP_UNIX_SENDMSG:
8089 case BPF_CGROUP_INET4_GETPEERNAME:
8090 case BPF_CGROUP_INET6_GETPEERNAME:
8091 case BPF_CGROUP_UNIX_GETPEERNAME:
8092 case BPF_CGROUP_INET4_GETSOCKNAME:
8093 case BPF_CGROUP_INET6_GETSOCKNAME:
8094 case BPF_CGROUP_UNIX_GETSOCKNAME:
8095 return &bpf_sock_addr_getsockopt_proto;
8096 default:
8097 return NULL;
8098 }
8099 default:
8100 return bpf_sk_base_func_proto(func_id, prog);
8101 }
8102 }
8103
8104 static const struct bpf_func_proto *
sk_filter_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8105 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8106 {
8107 switch (func_id) {
8108 case BPF_FUNC_skb_load_bytes:
8109 return &bpf_skb_load_bytes_proto;
8110 case BPF_FUNC_skb_load_bytes_relative:
8111 return &bpf_skb_load_bytes_relative_proto;
8112 case BPF_FUNC_get_socket_cookie:
8113 return &bpf_get_socket_cookie_proto;
8114 case BPF_FUNC_get_socket_uid:
8115 return &bpf_get_socket_uid_proto;
8116 case BPF_FUNC_perf_event_output:
8117 return &bpf_skb_event_output_proto;
8118 default:
8119 return bpf_sk_base_func_proto(func_id, prog);
8120 }
8121 }
8122
8123 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
8124 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
8125
8126 static const struct bpf_func_proto *
cg_skb_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8127 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8128 {
8129 const struct bpf_func_proto *func_proto;
8130
8131 func_proto = cgroup_common_func_proto(func_id, prog);
8132 if (func_proto)
8133 return func_proto;
8134
8135 switch (func_id) {
8136 case BPF_FUNC_sk_fullsock:
8137 return &bpf_sk_fullsock_proto;
8138 case BPF_FUNC_sk_storage_get:
8139 return &bpf_sk_storage_get_proto;
8140 case BPF_FUNC_sk_storage_delete:
8141 return &bpf_sk_storage_delete_proto;
8142 case BPF_FUNC_perf_event_output:
8143 return &bpf_skb_event_output_proto;
8144 #ifdef CONFIG_SOCK_CGROUP_DATA
8145 case BPF_FUNC_skb_cgroup_id:
8146 return &bpf_skb_cgroup_id_proto;
8147 case BPF_FUNC_skb_ancestor_cgroup_id:
8148 return &bpf_skb_ancestor_cgroup_id_proto;
8149 case BPF_FUNC_sk_cgroup_id:
8150 return &bpf_sk_cgroup_id_proto;
8151 case BPF_FUNC_sk_ancestor_cgroup_id:
8152 return &bpf_sk_ancestor_cgroup_id_proto;
8153 #endif
8154 #ifdef CONFIG_INET
8155 case BPF_FUNC_sk_lookup_tcp:
8156 return &bpf_sk_lookup_tcp_proto;
8157 case BPF_FUNC_sk_lookup_udp:
8158 return &bpf_sk_lookup_udp_proto;
8159 case BPF_FUNC_sk_release:
8160 return &bpf_sk_release_proto;
8161 case BPF_FUNC_skc_lookup_tcp:
8162 return &bpf_skc_lookup_tcp_proto;
8163 case BPF_FUNC_tcp_sock:
8164 return &bpf_tcp_sock_proto;
8165 case BPF_FUNC_get_listener_sock:
8166 return &bpf_get_listener_sock_proto;
8167 case BPF_FUNC_skb_ecn_set_ce:
8168 return &bpf_skb_ecn_set_ce_proto;
8169 #endif
8170 default:
8171 return sk_filter_func_proto(func_id, prog);
8172 }
8173 }
8174
8175 static const struct bpf_func_proto *
tc_cls_act_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8176 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8177 {
8178 switch (func_id) {
8179 case BPF_FUNC_skb_store_bytes:
8180 return &bpf_skb_store_bytes_proto;
8181 case BPF_FUNC_skb_load_bytes:
8182 return &bpf_skb_load_bytes_proto;
8183 case BPF_FUNC_skb_load_bytes_relative:
8184 return &bpf_skb_load_bytes_relative_proto;
8185 case BPF_FUNC_skb_pull_data:
8186 return &bpf_skb_pull_data_proto;
8187 case BPF_FUNC_csum_diff:
8188 return &bpf_csum_diff_proto;
8189 case BPF_FUNC_csum_update:
8190 return &bpf_csum_update_proto;
8191 case BPF_FUNC_csum_level:
8192 return &bpf_csum_level_proto;
8193 case BPF_FUNC_l3_csum_replace:
8194 return &bpf_l3_csum_replace_proto;
8195 case BPF_FUNC_l4_csum_replace:
8196 return &bpf_l4_csum_replace_proto;
8197 case BPF_FUNC_clone_redirect:
8198 return &bpf_clone_redirect_proto;
8199 case BPF_FUNC_get_cgroup_classid:
8200 return &bpf_get_cgroup_classid_proto;
8201 case BPF_FUNC_skb_vlan_push:
8202 return &bpf_skb_vlan_push_proto;
8203 case BPF_FUNC_skb_vlan_pop:
8204 return &bpf_skb_vlan_pop_proto;
8205 case BPF_FUNC_skb_change_proto:
8206 return &bpf_skb_change_proto_proto;
8207 case BPF_FUNC_skb_change_type:
8208 return &bpf_skb_change_type_proto;
8209 case BPF_FUNC_skb_adjust_room:
8210 return &bpf_skb_adjust_room_proto;
8211 case BPF_FUNC_skb_change_tail:
8212 return &bpf_skb_change_tail_proto;
8213 case BPF_FUNC_skb_change_head:
8214 return &bpf_skb_change_head_proto;
8215 case BPF_FUNC_skb_get_tunnel_key:
8216 return &bpf_skb_get_tunnel_key_proto;
8217 case BPF_FUNC_skb_set_tunnel_key:
8218 return bpf_get_skb_set_tunnel_proto(func_id);
8219 case BPF_FUNC_skb_get_tunnel_opt:
8220 return &bpf_skb_get_tunnel_opt_proto;
8221 case BPF_FUNC_skb_set_tunnel_opt:
8222 return bpf_get_skb_set_tunnel_proto(func_id);
8223 case BPF_FUNC_redirect:
8224 return &bpf_redirect_proto;
8225 case BPF_FUNC_redirect_neigh:
8226 return &bpf_redirect_neigh_proto;
8227 case BPF_FUNC_redirect_peer:
8228 return &bpf_redirect_peer_proto;
8229 case BPF_FUNC_get_route_realm:
8230 return &bpf_get_route_realm_proto;
8231 case BPF_FUNC_get_hash_recalc:
8232 return &bpf_get_hash_recalc_proto;
8233 case BPF_FUNC_set_hash_invalid:
8234 return &bpf_set_hash_invalid_proto;
8235 case BPF_FUNC_set_hash:
8236 return &bpf_set_hash_proto;
8237 case BPF_FUNC_perf_event_output:
8238 return &bpf_skb_event_output_proto;
8239 case BPF_FUNC_get_smp_processor_id:
8240 return &bpf_get_smp_processor_id_proto;
8241 case BPF_FUNC_skb_under_cgroup:
8242 return &bpf_skb_under_cgroup_proto;
8243 case BPF_FUNC_get_socket_cookie:
8244 return &bpf_get_socket_cookie_proto;
8245 case BPF_FUNC_get_socket_uid:
8246 return &bpf_get_socket_uid_proto;
8247 case BPF_FUNC_fib_lookup:
8248 return &bpf_skb_fib_lookup_proto;
8249 case BPF_FUNC_check_mtu:
8250 return &bpf_skb_check_mtu_proto;
8251 case BPF_FUNC_sk_fullsock:
8252 return &bpf_sk_fullsock_proto;
8253 case BPF_FUNC_sk_storage_get:
8254 return &bpf_sk_storage_get_proto;
8255 case BPF_FUNC_sk_storage_delete:
8256 return &bpf_sk_storage_delete_proto;
8257 #ifdef CONFIG_XFRM
8258 case BPF_FUNC_skb_get_xfrm_state:
8259 return &bpf_skb_get_xfrm_state_proto;
8260 #endif
8261 #ifdef CONFIG_CGROUP_NET_CLASSID
8262 case BPF_FUNC_skb_cgroup_classid:
8263 return &bpf_skb_cgroup_classid_proto;
8264 #endif
8265 #ifdef CONFIG_SOCK_CGROUP_DATA
8266 case BPF_FUNC_skb_cgroup_id:
8267 return &bpf_skb_cgroup_id_proto;
8268 case BPF_FUNC_skb_ancestor_cgroup_id:
8269 return &bpf_skb_ancestor_cgroup_id_proto;
8270 #endif
8271 #ifdef CONFIG_INET
8272 case BPF_FUNC_sk_lookup_tcp:
8273 return &bpf_tc_sk_lookup_tcp_proto;
8274 case BPF_FUNC_sk_lookup_udp:
8275 return &bpf_tc_sk_lookup_udp_proto;
8276 case BPF_FUNC_sk_release:
8277 return &bpf_sk_release_proto;
8278 case BPF_FUNC_tcp_sock:
8279 return &bpf_tcp_sock_proto;
8280 case BPF_FUNC_get_listener_sock:
8281 return &bpf_get_listener_sock_proto;
8282 case BPF_FUNC_skc_lookup_tcp:
8283 return &bpf_tc_skc_lookup_tcp_proto;
8284 case BPF_FUNC_tcp_check_syncookie:
8285 return &bpf_tcp_check_syncookie_proto;
8286 case BPF_FUNC_skb_ecn_set_ce:
8287 return &bpf_skb_ecn_set_ce_proto;
8288 case BPF_FUNC_tcp_gen_syncookie:
8289 return &bpf_tcp_gen_syncookie_proto;
8290 case BPF_FUNC_sk_assign:
8291 return &bpf_sk_assign_proto;
8292 case BPF_FUNC_skb_set_tstamp:
8293 return &bpf_skb_set_tstamp_proto;
8294 #ifdef CONFIG_SYN_COOKIES
8295 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
8296 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
8297 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
8298 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
8299 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
8300 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
8301 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
8302 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
8303 #endif
8304 #endif
8305 default:
8306 return bpf_sk_base_func_proto(func_id, prog);
8307 }
8308 }
8309
8310 static const struct bpf_func_proto *
xdp_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8311 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8312 {
8313 switch (func_id) {
8314 case BPF_FUNC_perf_event_output:
8315 return &bpf_xdp_event_output_proto;
8316 case BPF_FUNC_get_smp_processor_id:
8317 return &bpf_get_smp_processor_id_proto;
8318 case BPF_FUNC_csum_diff:
8319 return &bpf_csum_diff_proto;
8320 case BPF_FUNC_xdp_adjust_head:
8321 return &bpf_xdp_adjust_head_proto;
8322 case BPF_FUNC_xdp_adjust_meta:
8323 return &bpf_xdp_adjust_meta_proto;
8324 case BPF_FUNC_redirect:
8325 return &bpf_xdp_redirect_proto;
8326 case BPF_FUNC_redirect_map:
8327 return &bpf_xdp_redirect_map_proto;
8328 case BPF_FUNC_xdp_adjust_tail:
8329 return &bpf_xdp_adjust_tail_proto;
8330 case BPF_FUNC_xdp_get_buff_len:
8331 return &bpf_xdp_get_buff_len_proto;
8332 case BPF_FUNC_xdp_load_bytes:
8333 return &bpf_xdp_load_bytes_proto;
8334 case BPF_FUNC_xdp_store_bytes:
8335 return &bpf_xdp_store_bytes_proto;
8336 case BPF_FUNC_fib_lookup:
8337 return &bpf_xdp_fib_lookup_proto;
8338 case BPF_FUNC_check_mtu:
8339 return &bpf_xdp_check_mtu_proto;
8340 #ifdef CONFIG_INET
8341 case BPF_FUNC_sk_lookup_udp:
8342 return &bpf_xdp_sk_lookup_udp_proto;
8343 case BPF_FUNC_sk_lookup_tcp:
8344 return &bpf_xdp_sk_lookup_tcp_proto;
8345 case BPF_FUNC_sk_release:
8346 return &bpf_sk_release_proto;
8347 case BPF_FUNC_skc_lookup_tcp:
8348 return &bpf_xdp_skc_lookup_tcp_proto;
8349 case BPF_FUNC_tcp_check_syncookie:
8350 return &bpf_tcp_check_syncookie_proto;
8351 case BPF_FUNC_tcp_gen_syncookie:
8352 return &bpf_tcp_gen_syncookie_proto;
8353 #ifdef CONFIG_SYN_COOKIES
8354 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
8355 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
8356 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
8357 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
8358 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
8359 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
8360 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
8361 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
8362 #endif
8363 #endif
8364 default:
8365 return bpf_sk_base_func_proto(func_id, prog);
8366 }
8367
8368 #if IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)
8369 /* The nf_conn___init type is used in the NF_CONNTRACK kfuncs. The
8370 * kfuncs are defined in two different modules, and we want to be able
8371 * to use them interchangeably with the same BTF type ID. Because modules
8372 * can't de-duplicate BTF IDs between each other, we need the type to be
8373 * referenced in the vmlinux BTF or the verifier will get confused about
8374 * the different types. So we add this dummy type reference which will
8375 * be included in vmlinux BTF, allowing both modules to refer to the
8376 * same type ID.
8377 */
8378 BTF_TYPE_EMIT(struct nf_conn___init);
8379 #endif
8380 }
8381
8382 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
8383 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
8384
8385 static const struct bpf_func_proto *
sock_ops_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8386 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8387 {
8388 const struct bpf_func_proto *func_proto;
8389
8390 func_proto = cgroup_common_func_proto(func_id, prog);
8391 if (func_proto)
8392 return func_proto;
8393
8394 switch (func_id) {
8395 case BPF_FUNC_setsockopt:
8396 return &bpf_sock_ops_setsockopt_proto;
8397 case BPF_FUNC_getsockopt:
8398 return &bpf_sock_ops_getsockopt_proto;
8399 case BPF_FUNC_sock_ops_cb_flags_set:
8400 return &bpf_sock_ops_cb_flags_set_proto;
8401 case BPF_FUNC_sock_map_update:
8402 return &bpf_sock_map_update_proto;
8403 case BPF_FUNC_sock_hash_update:
8404 return &bpf_sock_hash_update_proto;
8405 case BPF_FUNC_get_socket_cookie:
8406 return &bpf_get_socket_cookie_sock_ops_proto;
8407 case BPF_FUNC_perf_event_output:
8408 return &bpf_event_output_data_proto;
8409 case BPF_FUNC_sk_storage_get:
8410 return &bpf_sk_storage_get_proto;
8411 case BPF_FUNC_sk_storage_delete:
8412 return &bpf_sk_storage_delete_proto;
8413 case BPF_FUNC_get_netns_cookie:
8414 return &bpf_get_netns_cookie_sock_ops_proto;
8415 #ifdef CONFIG_INET
8416 case BPF_FUNC_load_hdr_opt:
8417 return &bpf_sock_ops_load_hdr_opt_proto;
8418 case BPF_FUNC_store_hdr_opt:
8419 return &bpf_sock_ops_store_hdr_opt_proto;
8420 case BPF_FUNC_reserve_hdr_opt:
8421 return &bpf_sock_ops_reserve_hdr_opt_proto;
8422 case BPF_FUNC_tcp_sock:
8423 return &bpf_tcp_sock_proto;
8424 #endif /* CONFIG_INET */
8425 default:
8426 return bpf_sk_base_func_proto(func_id, prog);
8427 }
8428 }
8429
8430 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
8431 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
8432
8433 static const struct bpf_func_proto *
sk_msg_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8434 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8435 {
8436 switch (func_id) {
8437 case BPF_FUNC_msg_redirect_map:
8438 return &bpf_msg_redirect_map_proto;
8439 case BPF_FUNC_msg_redirect_hash:
8440 return &bpf_msg_redirect_hash_proto;
8441 case BPF_FUNC_msg_apply_bytes:
8442 return &bpf_msg_apply_bytes_proto;
8443 case BPF_FUNC_msg_cork_bytes:
8444 return &bpf_msg_cork_bytes_proto;
8445 case BPF_FUNC_msg_pull_data:
8446 return &bpf_msg_pull_data_proto;
8447 case BPF_FUNC_msg_push_data:
8448 return &bpf_msg_push_data_proto;
8449 case BPF_FUNC_msg_pop_data:
8450 return &bpf_msg_pop_data_proto;
8451 case BPF_FUNC_perf_event_output:
8452 return &bpf_event_output_data_proto;
8453 case BPF_FUNC_get_current_uid_gid:
8454 return &bpf_get_current_uid_gid_proto;
8455 case BPF_FUNC_sk_storage_get:
8456 return &bpf_sk_storage_get_proto;
8457 case BPF_FUNC_sk_storage_delete:
8458 return &bpf_sk_storage_delete_proto;
8459 case BPF_FUNC_get_netns_cookie:
8460 return &bpf_get_netns_cookie_sk_msg_proto;
8461 #ifdef CONFIG_CGROUP_NET_CLASSID
8462 case BPF_FUNC_get_cgroup_classid:
8463 return &bpf_get_cgroup_classid_curr_proto;
8464 #endif
8465 default:
8466 return bpf_sk_base_func_proto(func_id, prog);
8467 }
8468 }
8469
8470 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
8471 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
8472
8473 static const struct bpf_func_proto *
sk_skb_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8474 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8475 {
8476 switch (func_id) {
8477 case BPF_FUNC_skb_store_bytes:
8478 return &bpf_skb_store_bytes_proto;
8479 case BPF_FUNC_skb_load_bytes:
8480 return &bpf_skb_load_bytes_proto;
8481 case BPF_FUNC_skb_pull_data:
8482 return &sk_skb_pull_data_proto;
8483 case BPF_FUNC_skb_change_tail:
8484 return &sk_skb_change_tail_proto;
8485 case BPF_FUNC_skb_change_head:
8486 return &sk_skb_change_head_proto;
8487 case BPF_FUNC_skb_adjust_room:
8488 return &sk_skb_adjust_room_proto;
8489 case BPF_FUNC_get_socket_cookie:
8490 return &bpf_get_socket_cookie_proto;
8491 case BPF_FUNC_get_socket_uid:
8492 return &bpf_get_socket_uid_proto;
8493 case BPF_FUNC_sk_redirect_map:
8494 return &bpf_sk_redirect_map_proto;
8495 case BPF_FUNC_sk_redirect_hash:
8496 return &bpf_sk_redirect_hash_proto;
8497 case BPF_FUNC_perf_event_output:
8498 return &bpf_skb_event_output_proto;
8499 #ifdef CONFIG_INET
8500 case BPF_FUNC_sk_lookup_tcp:
8501 return &bpf_sk_lookup_tcp_proto;
8502 case BPF_FUNC_sk_lookup_udp:
8503 return &bpf_sk_lookup_udp_proto;
8504 case BPF_FUNC_sk_release:
8505 return &bpf_sk_release_proto;
8506 case BPF_FUNC_skc_lookup_tcp:
8507 return &bpf_skc_lookup_tcp_proto;
8508 #endif
8509 default:
8510 return bpf_sk_base_func_proto(func_id, prog);
8511 }
8512 }
8513
8514 static const struct bpf_func_proto *
flow_dissector_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8515 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8516 {
8517 switch (func_id) {
8518 case BPF_FUNC_skb_load_bytes:
8519 return &bpf_flow_dissector_load_bytes_proto;
8520 default:
8521 return bpf_sk_base_func_proto(func_id, prog);
8522 }
8523 }
8524
8525 static const struct bpf_func_proto *
lwt_out_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8526 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8527 {
8528 switch (func_id) {
8529 case BPF_FUNC_skb_load_bytes:
8530 return &bpf_skb_load_bytes_proto;
8531 case BPF_FUNC_skb_pull_data:
8532 return &bpf_skb_pull_data_proto;
8533 case BPF_FUNC_csum_diff:
8534 return &bpf_csum_diff_proto;
8535 case BPF_FUNC_get_cgroup_classid:
8536 return &bpf_get_cgroup_classid_proto;
8537 case BPF_FUNC_get_route_realm:
8538 return &bpf_get_route_realm_proto;
8539 case BPF_FUNC_get_hash_recalc:
8540 return &bpf_get_hash_recalc_proto;
8541 case BPF_FUNC_perf_event_output:
8542 return &bpf_skb_event_output_proto;
8543 case BPF_FUNC_get_smp_processor_id:
8544 return &bpf_get_smp_processor_id_proto;
8545 case BPF_FUNC_skb_under_cgroup:
8546 return &bpf_skb_under_cgroup_proto;
8547 default:
8548 return bpf_sk_base_func_proto(func_id, prog);
8549 }
8550 }
8551
8552 static const struct bpf_func_proto *
lwt_in_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8553 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8554 {
8555 switch (func_id) {
8556 case BPF_FUNC_lwt_push_encap:
8557 return &bpf_lwt_in_push_encap_proto;
8558 default:
8559 return lwt_out_func_proto(func_id, prog);
8560 }
8561 }
8562
8563 static const struct bpf_func_proto *
lwt_xmit_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8564 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8565 {
8566 switch (func_id) {
8567 case BPF_FUNC_skb_get_tunnel_key:
8568 return &bpf_skb_get_tunnel_key_proto;
8569 case BPF_FUNC_skb_set_tunnel_key:
8570 return bpf_get_skb_set_tunnel_proto(func_id);
8571 case BPF_FUNC_skb_get_tunnel_opt:
8572 return &bpf_skb_get_tunnel_opt_proto;
8573 case BPF_FUNC_skb_set_tunnel_opt:
8574 return bpf_get_skb_set_tunnel_proto(func_id);
8575 case BPF_FUNC_redirect:
8576 return &bpf_redirect_proto;
8577 case BPF_FUNC_clone_redirect:
8578 return &bpf_clone_redirect_proto;
8579 case BPF_FUNC_skb_change_tail:
8580 return &bpf_skb_change_tail_proto;
8581 case BPF_FUNC_skb_change_head:
8582 return &bpf_skb_change_head_proto;
8583 case BPF_FUNC_skb_store_bytes:
8584 return &bpf_skb_store_bytes_proto;
8585 case BPF_FUNC_csum_update:
8586 return &bpf_csum_update_proto;
8587 case BPF_FUNC_csum_level:
8588 return &bpf_csum_level_proto;
8589 case BPF_FUNC_l3_csum_replace:
8590 return &bpf_l3_csum_replace_proto;
8591 case BPF_FUNC_l4_csum_replace:
8592 return &bpf_l4_csum_replace_proto;
8593 case BPF_FUNC_set_hash_invalid:
8594 return &bpf_set_hash_invalid_proto;
8595 case BPF_FUNC_lwt_push_encap:
8596 return &bpf_lwt_xmit_push_encap_proto;
8597 default:
8598 return lwt_out_func_proto(func_id, prog);
8599 }
8600 }
8601
8602 static const struct bpf_func_proto *
lwt_seg6local_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8603 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8604 {
8605 switch (func_id) {
8606 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
8607 case BPF_FUNC_lwt_seg6_store_bytes:
8608 return &bpf_lwt_seg6_store_bytes_proto;
8609 case BPF_FUNC_lwt_seg6_action:
8610 return &bpf_lwt_seg6_action_proto;
8611 case BPF_FUNC_lwt_seg6_adjust_srh:
8612 return &bpf_lwt_seg6_adjust_srh_proto;
8613 #endif
8614 default:
8615 return lwt_out_func_proto(func_id, prog);
8616 }
8617 }
8618
bpf_skb_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8619 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
8620 const struct bpf_prog *prog,
8621 struct bpf_insn_access_aux *info)
8622 {
8623 const int size_default = sizeof(__u32);
8624
8625 if (off < 0 || off >= sizeof(struct __sk_buff))
8626 return false;
8627
8628 /* The verifier guarantees that size > 0. */
8629 if (off % size != 0)
8630 return false;
8631
8632 switch (off) {
8633 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8634 if (off + size > offsetofend(struct __sk_buff, cb[4]))
8635 return false;
8636 break;
8637 case bpf_ctx_range(struct __sk_buff, data):
8638 case bpf_ctx_range(struct __sk_buff, data_meta):
8639 case bpf_ctx_range(struct __sk_buff, data_end):
8640 if (info->is_ldsx || size != size_default)
8641 return false;
8642 break;
8643 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
8644 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
8645 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
8646 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
8647 if (size != size_default)
8648 return false;
8649 break;
8650 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
8651 return false;
8652 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8653 if (type == BPF_WRITE || size != sizeof(__u64))
8654 return false;
8655 break;
8656 case bpf_ctx_range(struct __sk_buff, tstamp):
8657 if (size != sizeof(__u64))
8658 return false;
8659 break;
8660 case offsetof(struct __sk_buff, sk):
8661 if (type == BPF_WRITE || size != sizeof(__u64))
8662 return false;
8663 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
8664 break;
8665 case offsetof(struct __sk_buff, tstamp_type):
8666 return false;
8667 case offsetofend(struct __sk_buff, tstamp_type) ... offsetof(struct __sk_buff, hwtstamp) - 1:
8668 /* Explicitly prohibit access to padding in __sk_buff. */
8669 return false;
8670 default:
8671 /* Only narrow read access allowed for now. */
8672 if (type == BPF_WRITE) {
8673 if (size != size_default)
8674 return false;
8675 } else {
8676 bpf_ctx_record_field_size(info, size_default);
8677 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8678 return false;
8679 }
8680 }
8681
8682 return true;
8683 }
8684
sk_filter_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8685 static bool sk_filter_is_valid_access(int off, int size,
8686 enum bpf_access_type type,
8687 const struct bpf_prog *prog,
8688 struct bpf_insn_access_aux *info)
8689 {
8690 switch (off) {
8691 case bpf_ctx_range(struct __sk_buff, tc_classid):
8692 case bpf_ctx_range(struct __sk_buff, data):
8693 case bpf_ctx_range(struct __sk_buff, data_meta):
8694 case bpf_ctx_range(struct __sk_buff, data_end):
8695 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8696 case bpf_ctx_range(struct __sk_buff, tstamp):
8697 case bpf_ctx_range(struct __sk_buff, wire_len):
8698 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8699 return false;
8700 }
8701
8702 if (type == BPF_WRITE) {
8703 switch (off) {
8704 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8705 break;
8706 default:
8707 return false;
8708 }
8709 }
8710
8711 return bpf_skb_is_valid_access(off, size, type, prog, info);
8712 }
8713
cg_skb_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8714 static bool cg_skb_is_valid_access(int off, int size,
8715 enum bpf_access_type type,
8716 const struct bpf_prog *prog,
8717 struct bpf_insn_access_aux *info)
8718 {
8719 switch (off) {
8720 case bpf_ctx_range(struct __sk_buff, tc_classid):
8721 case bpf_ctx_range(struct __sk_buff, data_meta):
8722 case bpf_ctx_range(struct __sk_buff, wire_len):
8723 return false;
8724 case bpf_ctx_range(struct __sk_buff, data):
8725 case bpf_ctx_range(struct __sk_buff, data_end):
8726 if (!bpf_token_capable(prog->aux->token, CAP_BPF))
8727 return false;
8728 break;
8729 }
8730
8731 if (type == BPF_WRITE) {
8732 switch (off) {
8733 case bpf_ctx_range(struct __sk_buff, mark):
8734 case bpf_ctx_range(struct __sk_buff, priority):
8735 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8736 break;
8737 case bpf_ctx_range(struct __sk_buff, tstamp):
8738 if (!bpf_token_capable(prog->aux->token, CAP_BPF))
8739 return false;
8740 break;
8741 default:
8742 return false;
8743 }
8744 }
8745
8746 switch (off) {
8747 case bpf_ctx_range(struct __sk_buff, data):
8748 info->reg_type = PTR_TO_PACKET;
8749 break;
8750 case bpf_ctx_range(struct __sk_buff, data_end):
8751 info->reg_type = PTR_TO_PACKET_END;
8752 break;
8753 }
8754
8755 return bpf_skb_is_valid_access(off, size, type, prog, info);
8756 }
8757
lwt_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8758 static bool lwt_is_valid_access(int off, int size,
8759 enum bpf_access_type type,
8760 const struct bpf_prog *prog,
8761 struct bpf_insn_access_aux *info)
8762 {
8763 switch (off) {
8764 case bpf_ctx_range(struct __sk_buff, tc_classid):
8765 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8766 case bpf_ctx_range(struct __sk_buff, data_meta):
8767 case bpf_ctx_range(struct __sk_buff, tstamp):
8768 case bpf_ctx_range(struct __sk_buff, wire_len):
8769 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8770 return false;
8771 }
8772
8773 if (type == BPF_WRITE) {
8774 switch (off) {
8775 case bpf_ctx_range(struct __sk_buff, mark):
8776 case bpf_ctx_range(struct __sk_buff, priority):
8777 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8778 break;
8779 default:
8780 return false;
8781 }
8782 }
8783
8784 switch (off) {
8785 case bpf_ctx_range(struct __sk_buff, data):
8786 info->reg_type = PTR_TO_PACKET;
8787 break;
8788 case bpf_ctx_range(struct __sk_buff, data_end):
8789 info->reg_type = PTR_TO_PACKET_END;
8790 break;
8791 }
8792
8793 return bpf_skb_is_valid_access(off, size, type, prog, info);
8794 }
8795
8796 /* Attach type specific accesses */
__sock_filter_check_attach_type(int off,enum bpf_access_type access_type,enum bpf_attach_type attach_type)8797 static bool __sock_filter_check_attach_type(int off,
8798 enum bpf_access_type access_type,
8799 enum bpf_attach_type attach_type)
8800 {
8801 switch (off) {
8802 case offsetof(struct bpf_sock, bound_dev_if):
8803 case offsetof(struct bpf_sock, mark):
8804 case offsetof(struct bpf_sock, priority):
8805 switch (attach_type) {
8806 case BPF_CGROUP_INET_SOCK_CREATE:
8807 case BPF_CGROUP_INET_SOCK_RELEASE:
8808 goto full_access;
8809 default:
8810 return false;
8811 }
8812 case bpf_ctx_range(struct bpf_sock, src_ip4):
8813 switch (attach_type) {
8814 case BPF_CGROUP_INET4_POST_BIND:
8815 goto read_only;
8816 default:
8817 return false;
8818 }
8819 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8820 switch (attach_type) {
8821 case BPF_CGROUP_INET6_POST_BIND:
8822 goto read_only;
8823 default:
8824 return false;
8825 }
8826 case bpf_ctx_range(struct bpf_sock, src_port):
8827 switch (attach_type) {
8828 case BPF_CGROUP_INET4_POST_BIND:
8829 case BPF_CGROUP_INET6_POST_BIND:
8830 goto read_only;
8831 default:
8832 return false;
8833 }
8834 }
8835 read_only:
8836 return access_type == BPF_READ;
8837 full_access:
8838 return true;
8839 }
8840
bpf_sock_common_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)8841 bool bpf_sock_common_is_valid_access(int off, int size,
8842 enum bpf_access_type type,
8843 struct bpf_insn_access_aux *info)
8844 {
8845 switch (off) {
8846 case bpf_ctx_range_till(struct bpf_sock, type, priority):
8847 return false;
8848 default:
8849 return bpf_sock_is_valid_access(off, size, type, info);
8850 }
8851 }
8852
bpf_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)8853 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
8854 struct bpf_insn_access_aux *info)
8855 {
8856 const int size_default = sizeof(__u32);
8857 int field_size;
8858
8859 if (off < 0 || off >= sizeof(struct bpf_sock))
8860 return false;
8861 if (off % size != 0)
8862 return false;
8863
8864 switch (off) {
8865 case offsetof(struct bpf_sock, state):
8866 case offsetof(struct bpf_sock, family):
8867 case offsetof(struct bpf_sock, type):
8868 case offsetof(struct bpf_sock, protocol):
8869 case offsetof(struct bpf_sock, src_port):
8870 case offsetof(struct bpf_sock, rx_queue_mapping):
8871 case bpf_ctx_range(struct bpf_sock, src_ip4):
8872 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8873 case bpf_ctx_range(struct bpf_sock, dst_ip4):
8874 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
8875 bpf_ctx_record_field_size(info, size_default);
8876 return bpf_ctx_narrow_access_ok(off, size, size_default);
8877 case bpf_ctx_range(struct bpf_sock, dst_port):
8878 field_size = size == size_default ?
8879 size_default : sizeof_field(struct bpf_sock, dst_port);
8880 bpf_ctx_record_field_size(info, field_size);
8881 return bpf_ctx_narrow_access_ok(off, size, field_size);
8882 case offsetofend(struct bpf_sock, dst_port) ...
8883 offsetof(struct bpf_sock, dst_ip4) - 1:
8884 return false;
8885 }
8886
8887 return size == size_default;
8888 }
8889
sock_filter_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8890 static bool sock_filter_is_valid_access(int off, int size,
8891 enum bpf_access_type type,
8892 const struct bpf_prog *prog,
8893 struct bpf_insn_access_aux *info)
8894 {
8895 if (!bpf_sock_is_valid_access(off, size, type, info))
8896 return false;
8897 return __sock_filter_check_attach_type(off, type,
8898 prog->expected_attach_type);
8899 }
8900
bpf_noop_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)8901 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
8902 const struct bpf_prog *prog)
8903 {
8904 /* Neither direct read nor direct write requires any preliminary
8905 * action.
8906 */
8907 return 0;
8908 }
8909
bpf_unclone_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog,int drop_verdict)8910 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
8911 const struct bpf_prog *prog, int drop_verdict)
8912 {
8913 struct bpf_insn *insn = insn_buf;
8914
8915 if (!direct_write)
8916 return 0;
8917
8918 /* if (!skb->cloned)
8919 * goto start;
8920 *
8921 * (Fast-path, otherwise approximation that we might be
8922 * a clone, do the rest in helper.)
8923 */
8924 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET);
8925 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
8926 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
8927
8928 /* ret = bpf_skb_pull_data(skb, 0); */
8929 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
8930 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
8931 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
8932 BPF_FUNC_skb_pull_data);
8933 /* if (!ret)
8934 * goto restore;
8935 * return TC_ACT_SHOT;
8936 */
8937 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
8938 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
8939 *insn++ = BPF_EXIT_INSN();
8940
8941 /* restore: */
8942 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
8943 /* start: */
8944 *insn++ = prog->insnsi[0];
8945
8946 return insn - insn_buf;
8947 }
8948
bpf_gen_ld_abs(const struct bpf_insn * orig,struct bpf_insn * insn_buf)8949 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
8950 struct bpf_insn *insn_buf)
8951 {
8952 bool indirect = BPF_MODE(orig->code) == BPF_IND;
8953 struct bpf_insn *insn = insn_buf;
8954
8955 if (!indirect) {
8956 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
8957 } else {
8958 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
8959 if (orig->imm)
8960 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
8961 }
8962 /* We're guaranteed here that CTX is in R6. */
8963 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
8964
8965 switch (BPF_SIZE(orig->code)) {
8966 case BPF_B:
8967 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
8968 break;
8969 case BPF_H:
8970 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
8971 break;
8972 case BPF_W:
8973 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
8974 break;
8975 }
8976
8977 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
8978 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
8979 *insn++ = BPF_EXIT_INSN();
8980
8981 return insn - insn_buf;
8982 }
8983
tc_cls_act_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)8984 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
8985 const struct bpf_prog *prog)
8986 {
8987 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
8988 }
8989
tc_cls_act_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8990 static bool tc_cls_act_is_valid_access(int off, int size,
8991 enum bpf_access_type type,
8992 const struct bpf_prog *prog,
8993 struct bpf_insn_access_aux *info)
8994 {
8995 if (type == BPF_WRITE) {
8996 switch (off) {
8997 case bpf_ctx_range(struct __sk_buff, mark):
8998 case bpf_ctx_range(struct __sk_buff, tc_index):
8999 case bpf_ctx_range(struct __sk_buff, priority):
9000 case bpf_ctx_range(struct __sk_buff, tc_classid):
9001 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
9002 case bpf_ctx_range(struct __sk_buff, tstamp):
9003 case bpf_ctx_range(struct __sk_buff, queue_mapping):
9004 break;
9005 default:
9006 return false;
9007 }
9008 }
9009
9010 switch (off) {
9011 case bpf_ctx_range(struct __sk_buff, data):
9012 info->reg_type = PTR_TO_PACKET;
9013 break;
9014 case bpf_ctx_range(struct __sk_buff, data_meta):
9015 info->reg_type = PTR_TO_PACKET_META;
9016 break;
9017 case bpf_ctx_range(struct __sk_buff, data_end):
9018 info->reg_type = PTR_TO_PACKET_END;
9019 break;
9020 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
9021 return false;
9022 case offsetof(struct __sk_buff, tstamp_type):
9023 /* The convert_ctx_access() on reading and writing
9024 * __sk_buff->tstamp depends on whether the bpf prog
9025 * has used __sk_buff->tstamp_type or not.
9026 * Thus, we need to set prog->tstamp_type_access
9027 * earlier during is_valid_access() here.
9028 */
9029 ((struct bpf_prog *)prog)->tstamp_type_access = 1;
9030 return size == sizeof(__u8);
9031 }
9032
9033 return bpf_skb_is_valid_access(off, size, type, prog, info);
9034 }
9035
9036 DEFINE_MUTEX(nf_conn_btf_access_lock);
9037 EXPORT_SYMBOL_GPL(nf_conn_btf_access_lock);
9038
9039 int (*nfct_btf_struct_access)(struct bpf_verifier_log *log,
9040 const struct bpf_reg_state *reg,
9041 int off, int size);
9042 EXPORT_SYMBOL_GPL(nfct_btf_struct_access);
9043
tc_cls_act_btf_struct_access(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,int off,int size)9044 static int tc_cls_act_btf_struct_access(struct bpf_verifier_log *log,
9045 const struct bpf_reg_state *reg,
9046 int off, int size)
9047 {
9048 int ret = -EACCES;
9049
9050 mutex_lock(&nf_conn_btf_access_lock);
9051 if (nfct_btf_struct_access)
9052 ret = nfct_btf_struct_access(log, reg, off, size);
9053 mutex_unlock(&nf_conn_btf_access_lock);
9054
9055 return ret;
9056 }
9057
__is_valid_xdp_access(int off,int size)9058 static bool __is_valid_xdp_access(int off, int size)
9059 {
9060 if (off < 0 || off >= sizeof(struct xdp_md))
9061 return false;
9062 if (off % size != 0)
9063 return false;
9064 if (size != sizeof(__u32))
9065 return false;
9066
9067 return true;
9068 }
9069
xdp_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9070 static bool xdp_is_valid_access(int off, int size,
9071 enum bpf_access_type type,
9072 const struct bpf_prog *prog,
9073 struct bpf_insn_access_aux *info)
9074 {
9075 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
9076 switch (off) {
9077 case offsetof(struct xdp_md, egress_ifindex):
9078 return false;
9079 }
9080 }
9081
9082 if (type == BPF_WRITE) {
9083 if (bpf_prog_is_offloaded(prog->aux)) {
9084 switch (off) {
9085 case offsetof(struct xdp_md, rx_queue_index):
9086 return __is_valid_xdp_access(off, size);
9087 }
9088 }
9089 return false;
9090 } else {
9091 switch (off) {
9092 case offsetof(struct xdp_md, data_meta):
9093 case offsetof(struct xdp_md, data):
9094 case offsetof(struct xdp_md, data_end):
9095 if (info->is_ldsx)
9096 return false;
9097 }
9098 }
9099
9100 switch (off) {
9101 case offsetof(struct xdp_md, data):
9102 info->reg_type = PTR_TO_PACKET;
9103 break;
9104 case offsetof(struct xdp_md, data_meta):
9105 info->reg_type = PTR_TO_PACKET_META;
9106 break;
9107 case offsetof(struct xdp_md, data_end):
9108 info->reg_type = PTR_TO_PACKET_END;
9109 break;
9110 }
9111
9112 return __is_valid_xdp_access(off, size);
9113 }
9114
bpf_warn_invalid_xdp_action(struct net_device * dev,struct bpf_prog * prog,u32 act)9115 void bpf_warn_invalid_xdp_action(struct net_device *dev, struct bpf_prog *prog, u32 act)
9116 {
9117 const u32 act_max = XDP_REDIRECT;
9118
9119 pr_warn_once("%s XDP return value %u on prog %s (id %d) dev %s, expect packet loss!\n",
9120 act > act_max ? "Illegal" : "Driver unsupported",
9121 act, prog->aux->name, prog->aux->id, dev ? dev->name : "N/A");
9122 }
9123 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
9124
xdp_btf_struct_access(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,int off,int size)9125 static int xdp_btf_struct_access(struct bpf_verifier_log *log,
9126 const struct bpf_reg_state *reg,
9127 int off, int size)
9128 {
9129 int ret = -EACCES;
9130
9131 mutex_lock(&nf_conn_btf_access_lock);
9132 if (nfct_btf_struct_access)
9133 ret = nfct_btf_struct_access(log, reg, off, size);
9134 mutex_unlock(&nf_conn_btf_access_lock);
9135
9136 return ret;
9137 }
9138
sock_addr_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9139 static bool sock_addr_is_valid_access(int off, int size,
9140 enum bpf_access_type type,
9141 const struct bpf_prog *prog,
9142 struct bpf_insn_access_aux *info)
9143 {
9144 const int size_default = sizeof(__u32);
9145
9146 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
9147 return false;
9148 if (off % size != 0)
9149 return false;
9150
9151 /* Disallow access to fields not belonging to the attach type's address
9152 * family.
9153 */
9154 switch (off) {
9155 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
9156 switch (prog->expected_attach_type) {
9157 case BPF_CGROUP_INET4_BIND:
9158 case BPF_CGROUP_INET4_CONNECT:
9159 case BPF_CGROUP_INET4_GETPEERNAME:
9160 case BPF_CGROUP_INET4_GETSOCKNAME:
9161 case BPF_CGROUP_UDP4_SENDMSG:
9162 case BPF_CGROUP_UDP4_RECVMSG:
9163 break;
9164 default:
9165 return false;
9166 }
9167 break;
9168 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9169 switch (prog->expected_attach_type) {
9170 case BPF_CGROUP_INET6_BIND:
9171 case BPF_CGROUP_INET6_CONNECT:
9172 case BPF_CGROUP_INET6_GETPEERNAME:
9173 case BPF_CGROUP_INET6_GETSOCKNAME:
9174 case BPF_CGROUP_UDP6_SENDMSG:
9175 case BPF_CGROUP_UDP6_RECVMSG:
9176 break;
9177 default:
9178 return false;
9179 }
9180 break;
9181 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
9182 switch (prog->expected_attach_type) {
9183 case BPF_CGROUP_UDP4_SENDMSG:
9184 break;
9185 default:
9186 return false;
9187 }
9188 break;
9189 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9190 msg_src_ip6[3]):
9191 switch (prog->expected_attach_type) {
9192 case BPF_CGROUP_UDP6_SENDMSG:
9193 break;
9194 default:
9195 return false;
9196 }
9197 break;
9198 }
9199
9200 switch (off) {
9201 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
9202 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9203 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
9204 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9205 msg_src_ip6[3]):
9206 case bpf_ctx_range(struct bpf_sock_addr, user_port):
9207 if (type == BPF_READ) {
9208 bpf_ctx_record_field_size(info, size_default);
9209
9210 if (bpf_ctx_wide_access_ok(off, size,
9211 struct bpf_sock_addr,
9212 user_ip6))
9213 return true;
9214
9215 if (bpf_ctx_wide_access_ok(off, size,
9216 struct bpf_sock_addr,
9217 msg_src_ip6))
9218 return true;
9219
9220 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
9221 return false;
9222 } else {
9223 if (bpf_ctx_wide_access_ok(off, size,
9224 struct bpf_sock_addr,
9225 user_ip6))
9226 return true;
9227
9228 if (bpf_ctx_wide_access_ok(off, size,
9229 struct bpf_sock_addr,
9230 msg_src_ip6))
9231 return true;
9232
9233 if (size != size_default)
9234 return false;
9235 }
9236 break;
9237 case offsetof(struct bpf_sock_addr, sk):
9238 if (type != BPF_READ)
9239 return false;
9240 if (size != sizeof(__u64))
9241 return false;
9242 info->reg_type = PTR_TO_SOCKET;
9243 break;
9244 default:
9245 if (type == BPF_READ) {
9246 if (size != size_default)
9247 return false;
9248 } else {
9249 return false;
9250 }
9251 }
9252
9253 return true;
9254 }
9255
sock_ops_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9256 static bool sock_ops_is_valid_access(int off, int size,
9257 enum bpf_access_type type,
9258 const struct bpf_prog *prog,
9259 struct bpf_insn_access_aux *info)
9260 {
9261 const int size_default = sizeof(__u32);
9262
9263 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
9264 return false;
9265
9266 /* The verifier guarantees that size > 0. */
9267 if (off % size != 0)
9268 return false;
9269
9270 if (type == BPF_WRITE) {
9271 switch (off) {
9272 case offsetof(struct bpf_sock_ops, reply):
9273 case offsetof(struct bpf_sock_ops, sk_txhash):
9274 if (size != size_default)
9275 return false;
9276 break;
9277 default:
9278 return false;
9279 }
9280 } else {
9281 switch (off) {
9282 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
9283 bytes_acked):
9284 if (size != sizeof(__u64))
9285 return false;
9286 break;
9287 case offsetof(struct bpf_sock_ops, sk):
9288 if (size != sizeof(__u64))
9289 return false;
9290 info->reg_type = PTR_TO_SOCKET_OR_NULL;
9291 break;
9292 case offsetof(struct bpf_sock_ops, skb_data):
9293 if (size != sizeof(__u64))
9294 return false;
9295 info->reg_type = PTR_TO_PACKET;
9296 break;
9297 case offsetof(struct bpf_sock_ops, skb_data_end):
9298 if (size != sizeof(__u64))
9299 return false;
9300 info->reg_type = PTR_TO_PACKET_END;
9301 break;
9302 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
9303 bpf_ctx_record_field_size(info, size_default);
9304 return bpf_ctx_narrow_access_ok(off, size,
9305 size_default);
9306 case offsetof(struct bpf_sock_ops, skb_hwtstamp):
9307 if (size != sizeof(__u64))
9308 return false;
9309 break;
9310 default:
9311 if (size != size_default)
9312 return false;
9313 break;
9314 }
9315 }
9316
9317 return true;
9318 }
9319
sk_skb_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)9320 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
9321 const struct bpf_prog *prog)
9322 {
9323 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
9324 }
9325
sk_skb_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9326 static bool sk_skb_is_valid_access(int off, int size,
9327 enum bpf_access_type type,
9328 const struct bpf_prog *prog,
9329 struct bpf_insn_access_aux *info)
9330 {
9331 switch (off) {
9332 case bpf_ctx_range(struct __sk_buff, tc_classid):
9333 case bpf_ctx_range(struct __sk_buff, data_meta):
9334 case bpf_ctx_range(struct __sk_buff, tstamp):
9335 case bpf_ctx_range(struct __sk_buff, wire_len):
9336 case bpf_ctx_range(struct __sk_buff, hwtstamp):
9337 return false;
9338 }
9339
9340 if (type == BPF_WRITE) {
9341 switch (off) {
9342 case bpf_ctx_range(struct __sk_buff, tc_index):
9343 case bpf_ctx_range(struct __sk_buff, priority):
9344 break;
9345 default:
9346 return false;
9347 }
9348 }
9349
9350 switch (off) {
9351 case bpf_ctx_range(struct __sk_buff, mark):
9352 return false;
9353 case bpf_ctx_range(struct __sk_buff, data):
9354 info->reg_type = PTR_TO_PACKET;
9355 break;
9356 case bpf_ctx_range(struct __sk_buff, data_end):
9357 info->reg_type = PTR_TO_PACKET_END;
9358 break;
9359 }
9360
9361 return bpf_skb_is_valid_access(off, size, type, prog, info);
9362 }
9363
sk_msg_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9364 static bool sk_msg_is_valid_access(int off, int size,
9365 enum bpf_access_type type,
9366 const struct bpf_prog *prog,
9367 struct bpf_insn_access_aux *info)
9368 {
9369 if (type == BPF_WRITE)
9370 return false;
9371
9372 if (off % size != 0)
9373 return false;
9374
9375 switch (off) {
9376 case offsetof(struct sk_msg_md, data):
9377 info->reg_type = PTR_TO_PACKET;
9378 if (size != sizeof(__u64))
9379 return false;
9380 break;
9381 case offsetof(struct sk_msg_md, data_end):
9382 info->reg_type = PTR_TO_PACKET_END;
9383 if (size != sizeof(__u64))
9384 return false;
9385 break;
9386 case offsetof(struct sk_msg_md, sk):
9387 if (size != sizeof(__u64))
9388 return false;
9389 info->reg_type = PTR_TO_SOCKET;
9390 break;
9391 case bpf_ctx_range(struct sk_msg_md, family):
9392 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
9393 case bpf_ctx_range(struct sk_msg_md, local_ip4):
9394 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
9395 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
9396 case bpf_ctx_range(struct sk_msg_md, remote_port):
9397 case bpf_ctx_range(struct sk_msg_md, local_port):
9398 case bpf_ctx_range(struct sk_msg_md, size):
9399 if (size != sizeof(__u32))
9400 return false;
9401 break;
9402 default:
9403 return false;
9404 }
9405 return true;
9406 }
9407
flow_dissector_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9408 static bool flow_dissector_is_valid_access(int off, int size,
9409 enum bpf_access_type type,
9410 const struct bpf_prog *prog,
9411 struct bpf_insn_access_aux *info)
9412 {
9413 const int size_default = sizeof(__u32);
9414
9415 if (off < 0 || off >= sizeof(struct __sk_buff))
9416 return false;
9417
9418 if (off % size != 0)
9419 return false;
9420
9421 if (type == BPF_WRITE)
9422 return false;
9423
9424 switch (off) {
9425 case bpf_ctx_range(struct __sk_buff, data):
9426 if (info->is_ldsx || size != size_default)
9427 return false;
9428 info->reg_type = PTR_TO_PACKET;
9429 return true;
9430 case bpf_ctx_range(struct __sk_buff, data_end):
9431 if (info->is_ldsx || size != size_default)
9432 return false;
9433 info->reg_type = PTR_TO_PACKET_END;
9434 return true;
9435 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
9436 if (size != sizeof(__u64))
9437 return false;
9438 info->reg_type = PTR_TO_FLOW_KEYS;
9439 return true;
9440 default:
9441 return false;
9442 }
9443 }
9444
flow_dissector_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)9445 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
9446 const struct bpf_insn *si,
9447 struct bpf_insn *insn_buf,
9448 struct bpf_prog *prog,
9449 u32 *target_size)
9450
9451 {
9452 struct bpf_insn *insn = insn_buf;
9453
9454 switch (si->off) {
9455 case offsetof(struct __sk_buff, data):
9456 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
9457 si->dst_reg, si->src_reg,
9458 offsetof(struct bpf_flow_dissector, data));
9459 break;
9460
9461 case offsetof(struct __sk_buff, data_end):
9462 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
9463 si->dst_reg, si->src_reg,
9464 offsetof(struct bpf_flow_dissector, data_end));
9465 break;
9466
9467 case offsetof(struct __sk_buff, flow_keys):
9468 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
9469 si->dst_reg, si->src_reg,
9470 offsetof(struct bpf_flow_dissector, flow_keys));
9471 break;
9472 }
9473
9474 return insn - insn_buf;
9475 }
9476
bpf_convert_tstamp_type_read(const struct bpf_insn * si,struct bpf_insn * insn)9477 static struct bpf_insn *bpf_convert_tstamp_type_read(const struct bpf_insn *si,
9478 struct bpf_insn *insn)
9479 {
9480 __u8 value_reg = si->dst_reg;
9481 __u8 skb_reg = si->src_reg;
9482 BUILD_BUG_ON(__SKB_CLOCK_MAX != (int)BPF_SKB_CLOCK_TAI);
9483 BUILD_BUG_ON(SKB_CLOCK_REALTIME != (int)BPF_SKB_CLOCK_REALTIME);
9484 BUILD_BUG_ON(SKB_CLOCK_MONOTONIC != (int)BPF_SKB_CLOCK_MONOTONIC);
9485 BUILD_BUG_ON(SKB_CLOCK_TAI != (int)BPF_SKB_CLOCK_TAI);
9486 *insn++ = BPF_LDX_MEM(BPF_B, value_reg, skb_reg, SKB_BF_MONO_TC_OFFSET);
9487 *insn++ = BPF_ALU32_IMM(BPF_AND, value_reg, SKB_TSTAMP_TYPE_MASK);
9488 #ifdef __BIG_ENDIAN_BITFIELD
9489 *insn++ = BPF_ALU32_IMM(BPF_RSH, value_reg, SKB_TSTAMP_TYPE_RSHIFT);
9490 #else
9491 BUILD_BUG_ON(!(SKB_TSTAMP_TYPE_MASK & 0x1));
9492 #endif
9493
9494 return insn;
9495 }
9496
bpf_convert_shinfo_access(__u8 dst_reg,__u8 skb_reg,struct bpf_insn * insn)9497 static struct bpf_insn *bpf_convert_shinfo_access(__u8 dst_reg, __u8 skb_reg,
9498 struct bpf_insn *insn)
9499 {
9500 /* si->dst_reg = skb_shinfo(SKB); */
9501 #ifdef NET_SKBUFF_DATA_USES_OFFSET
9502 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9503 BPF_REG_AX, skb_reg,
9504 offsetof(struct sk_buff, end));
9505 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
9506 dst_reg, skb_reg,
9507 offsetof(struct sk_buff, head));
9508 *insn++ = BPF_ALU64_REG(BPF_ADD, dst_reg, BPF_REG_AX);
9509 #else
9510 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9511 dst_reg, skb_reg,
9512 offsetof(struct sk_buff, end));
9513 #endif
9514
9515 return insn;
9516 }
9517
bpf_convert_tstamp_read(const struct bpf_prog * prog,const struct bpf_insn * si,struct bpf_insn * insn)9518 static struct bpf_insn *bpf_convert_tstamp_read(const struct bpf_prog *prog,
9519 const struct bpf_insn *si,
9520 struct bpf_insn *insn)
9521 {
9522 __u8 value_reg = si->dst_reg;
9523 __u8 skb_reg = si->src_reg;
9524
9525 #ifdef CONFIG_NET_XGRESS
9526 /* If the tstamp_type is read,
9527 * the bpf prog is aware the tstamp could have delivery time.
9528 * Thus, read skb->tstamp as is if tstamp_type_access is true.
9529 */
9530 if (!prog->tstamp_type_access) {
9531 /* AX is needed because src_reg and dst_reg could be the same */
9532 __u8 tmp_reg = BPF_REG_AX;
9533
9534 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, SKB_BF_MONO_TC_OFFSET);
9535 /* check if ingress mask bits is set */
9536 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, TC_AT_INGRESS_MASK, 1);
9537 *insn++ = BPF_JMP_A(4);
9538 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, SKB_TSTAMP_TYPE_MASK, 1);
9539 *insn++ = BPF_JMP_A(2);
9540 /* skb->tc_at_ingress && skb->tstamp_type,
9541 * read 0 as the (rcv) timestamp.
9542 */
9543 *insn++ = BPF_MOV64_IMM(value_reg, 0);
9544 *insn++ = BPF_JMP_A(1);
9545 }
9546 #endif
9547
9548 *insn++ = BPF_LDX_MEM(BPF_DW, value_reg, skb_reg,
9549 offsetof(struct sk_buff, tstamp));
9550 return insn;
9551 }
9552
bpf_convert_tstamp_write(const struct bpf_prog * prog,const struct bpf_insn * si,struct bpf_insn * insn)9553 static struct bpf_insn *bpf_convert_tstamp_write(const struct bpf_prog *prog,
9554 const struct bpf_insn *si,
9555 struct bpf_insn *insn)
9556 {
9557 __u8 value_reg = si->src_reg;
9558 __u8 skb_reg = si->dst_reg;
9559
9560 #ifdef CONFIG_NET_XGRESS
9561 /* If the tstamp_type is read,
9562 * the bpf prog is aware the tstamp could have delivery time.
9563 * Thus, write skb->tstamp as is if tstamp_type_access is true.
9564 * Otherwise, writing at ingress will have to clear the
9565 * skb->tstamp_type bit also.
9566 */
9567 if (!prog->tstamp_type_access) {
9568 __u8 tmp_reg = BPF_REG_AX;
9569
9570 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, SKB_BF_MONO_TC_OFFSET);
9571 /* Writing __sk_buff->tstamp as ingress, goto <clear> */
9572 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, TC_AT_INGRESS_MASK, 1);
9573 /* goto <store> */
9574 *insn++ = BPF_JMP_A(2);
9575 /* <clear>: skb->tstamp_type */
9576 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg, ~SKB_TSTAMP_TYPE_MASK);
9577 *insn++ = BPF_STX_MEM(BPF_B, skb_reg, tmp_reg, SKB_BF_MONO_TC_OFFSET);
9578 }
9579 #endif
9580
9581 /* <store>: skb->tstamp = tstamp */
9582 *insn++ = BPF_RAW_INSN(BPF_CLASS(si->code) | BPF_DW | BPF_MEM,
9583 skb_reg, value_reg, offsetof(struct sk_buff, tstamp), si->imm);
9584 return insn;
9585 }
9586
9587 #define BPF_EMIT_STORE(size, si, off) \
9588 BPF_RAW_INSN(BPF_CLASS((si)->code) | (size) | BPF_MEM, \
9589 (si)->dst_reg, (si)->src_reg, (off), (si)->imm)
9590
bpf_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)9591 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
9592 const struct bpf_insn *si,
9593 struct bpf_insn *insn_buf,
9594 struct bpf_prog *prog, u32 *target_size)
9595 {
9596 struct bpf_insn *insn = insn_buf;
9597 int off;
9598
9599 switch (si->off) {
9600 case offsetof(struct __sk_buff, len):
9601 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9602 bpf_target_off(struct sk_buff, len, 4,
9603 target_size));
9604 break;
9605
9606 case offsetof(struct __sk_buff, protocol):
9607 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9608 bpf_target_off(struct sk_buff, protocol, 2,
9609 target_size));
9610 break;
9611
9612 case offsetof(struct __sk_buff, vlan_proto):
9613 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9614 bpf_target_off(struct sk_buff, vlan_proto, 2,
9615 target_size));
9616 break;
9617
9618 case offsetof(struct __sk_buff, priority):
9619 if (type == BPF_WRITE)
9620 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9621 bpf_target_off(struct sk_buff, priority, 4,
9622 target_size));
9623 else
9624 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9625 bpf_target_off(struct sk_buff, priority, 4,
9626 target_size));
9627 break;
9628
9629 case offsetof(struct __sk_buff, ingress_ifindex):
9630 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9631 bpf_target_off(struct sk_buff, skb_iif, 4,
9632 target_size));
9633 break;
9634
9635 case offsetof(struct __sk_buff, ifindex):
9636 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
9637 si->dst_reg, si->src_reg,
9638 offsetof(struct sk_buff, dev));
9639 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9640 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9641 bpf_target_off(struct net_device, ifindex, 4,
9642 target_size));
9643 break;
9644
9645 case offsetof(struct __sk_buff, hash):
9646 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9647 bpf_target_off(struct sk_buff, hash, 4,
9648 target_size));
9649 break;
9650
9651 case offsetof(struct __sk_buff, mark):
9652 if (type == BPF_WRITE)
9653 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9654 bpf_target_off(struct sk_buff, mark, 4,
9655 target_size));
9656 else
9657 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9658 bpf_target_off(struct sk_buff, mark, 4,
9659 target_size));
9660 break;
9661
9662 case offsetof(struct __sk_buff, pkt_type):
9663 *target_size = 1;
9664 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
9665 PKT_TYPE_OFFSET);
9666 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
9667 #ifdef __BIG_ENDIAN_BITFIELD
9668 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
9669 #endif
9670 break;
9671
9672 case offsetof(struct __sk_buff, queue_mapping):
9673 if (type == BPF_WRITE) {
9674 u32 off = bpf_target_off(struct sk_buff, queue_mapping, 2, target_size);
9675
9676 if (BPF_CLASS(si->code) == BPF_ST && si->imm >= NO_QUEUE_MAPPING) {
9677 *insn++ = BPF_JMP_A(0); /* noop */
9678 break;
9679 }
9680
9681 if (BPF_CLASS(si->code) == BPF_STX)
9682 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
9683 *insn++ = BPF_EMIT_STORE(BPF_H, si, off);
9684 } else {
9685 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9686 bpf_target_off(struct sk_buff,
9687 queue_mapping,
9688 2, target_size));
9689 }
9690 break;
9691
9692 case offsetof(struct __sk_buff, vlan_present):
9693 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9694 bpf_target_off(struct sk_buff,
9695 vlan_all, 4, target_size));
9696 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9697 *insn++ = BPF_ALU32_IMM(BPF_MOV, si->dst_reg, 1);
9698 break;
9699
9700 case offsetof(struct __sk_buff, vlan_tci):
9701 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9702 bpf_target_off(struct sk_buff, vlan_tci, 2,
9703 target_size));
9704 break;
9705
9706 case offsetof(struct __sk_buff, cb[0]) ...
9707 offsetofend(struct __sk_buff, cb[4]) - 1:
9708 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
9709 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
9710 offsetof(struct qdisc_skb_cb, data)) %
9711 sizeof(__u64));
9712
9713 prog->cb_access = 1;
9714 off = si->off;
9715 off -= offsetof(struct __sk_buff, cb[0]);
9716 off += offsetof(struct sk_buff, cb);
9717 off += offsetof(struct qdisc_skb_cb, data);
9718 if (type == BPF_WRITE)
9719 *insn++ = BPF_EMIT_STORE(BPF_SIZE(si->code), si, off);
9720 else
9721 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
9722 si->src_reg, off);
9723 break;
9724
9725 case offsetof(struct __sk_buff, tc_classid):
9726 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
9727
9728 off = si->off;
9729 off -= offsetof(struct __sk_buff, tc_classid);
9730 off += offsetof(struct sk_buff, cb);
9731 off += offsetof(struct qdisc_skb_cb, tc_classid);
9732 *target_size = 2;
9733 if (type == BPF_WRITE)
9734 *insn++ = BPF_EMIT_STORE(BPF_H, si, off);
9735 else
9736 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
9737 si->src_reg, off);
9738 break;
9739
9740 case offsetof(struct __sk_buff, data):
9741 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9742 si->dst_reg, si->src_reg,
9743 offsetof(struct sk_buff, data));
9744 break;
9745
9746 case offsetof(struct __sk_buff, data_meta):
9747 off = si->off;
9748 off -= offsetof(struct __sk_buff, data_meta);
9749 off += offsetof(struct sk_buff, cb);
9750 off += offsetof(struct bpf_skb_data_end, data_meta);
9751 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9752 si->src_reg, off);
9753 break;
9754
9755 case offsetof(struct __sk_buff, data_end):
9756 off = si->off;
9757 off -= offsetof(struct __sk_buff, data_end);
9758 off += offsetof(struct sk_buff, cb);
9759 off += offsetof(struct bpf_skb_data_end, data_end);
9760 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9761 si->src_reg, off);
9762 break;
9763
9764 case offsetof(struct __sk_buff, tc_index):
9765 #ifdef CONFIG_NET_SCHED
9766 if (type == BPF_WRITE)
9767 *insn++ = BPF_EMIT_STORE(BPF_H, si,
9768 bpf_target_off(struct sk_buff, tc_index, 2,
9769 target_size));
9770 else
9771 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9772 bpf_target_off(struct sk_buff, tc_index, 2,
9773 target_size));
9774 #else
9775 *target_size = 2;
9776 if (type == BPF_WRITE)
9777 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
9778 else
9779 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9780 #endif
9781 break;
9782
9783 case offsetof(struct __sk_buff, napi_id):
9784 #if defined(CONFIG_NET_RX_BUSY_POLL)
9785 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9786 bpf_target_off(struct sk_buff, napi_id, 4,
9787 target_size));
9788 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
9789 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9790 #else
9791 *target_size = 4;
9792 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9793 #endif
9794 break;
9795 case offsetof(struct __sk_buff, family):
9796 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
9797
9798 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9799 si->dst_reg, si->src_reg,
9800 offsetof(struct sk_buff, sk));
9801 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9802 bpf_target_off(struct sock_common,
9803 skc_family,
9804 2, target_size));
9805 break;
9806 case offsetof(struct __sk_buff, remote_ip4):
9807 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
9808
9809 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9810 si->dst_reg, si->src_reg,
9811 offsetof(struct sk_buff, sk));
9812 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9813 bpf_target_off(struct sock_common,
9814 skc_daddr,
9815 4, target_size));
9816 break;
9817 case offsetof(struct __sk_buff, local_ip4):
9818 BUILD_BUG_ON(sizeof_field(struct sock_common,
9819 skc_rcv_saddr) != 4);
9820
9821 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9822 si->dst_reg, si->src_reg,
9823 offsetof(struct sk_buff, sk));
9824 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9825 bpf_target_off(struct sock_common,
9826 skc_rcv_saddr,
9827 4, target_size));
9828 break;
9829 case offsetof(struct __sk_buff, remote_ip6[0]) ...
9830 offsetof(struct __sk_buff, remote_ip6[3]):
9831 #if IS_ENABLED(CONFIG_IPV6)
9832 BUILD_BUG_ON(sizeof_field(struct sock_common,
9833 skc_v6_daddr.s6_addr32[0]) != 4);
9834
9835 off = si->off;
9836 off -= offsetof(struct __sk_buff, remote_ip6[0]);
9837
9838 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9839 si->dst_reg, si->src_reg,
9840 offsetof(struct sk_buff, sk));
9841 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9842 offsetof(struct sock_common,
9843 skc_v6_daddr.s6_addr32[0]) +
9844 off);
9845 #else
9846 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9847 #endif
9848 break;
9849 case offsetof(struct __sk_buff, local_ip6[0]) ...
9850 offsetof(struct __sk_buff, local_ip6[3]):
9851 #if IS_ENABLED(CONFIG_IPV6)
9852 BUILD_BUG_ON(sizeof_field(struct sock_common,
9853 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
9854
9855 off = si->off;
9856 off -= offsetof(struct __sk_buff, local_ip6[0]);
9857
9858 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9859 si->dst_reg, si->src_reg,
9860 offsetof(struct sk_buff, sk));
9861 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9862 offsetof(struct sock_common,
9863 skc_v6_rcv_saddr.s6_addr32[0]) +
9864 off);
9865 #else
9866 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9867 #endif
9868 break;
9869
9870 case offsetof(struct __sk_buff, remote_port):
9871 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
9872
9873 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9874 si->dst_reg, si->src_reg,
9875 offsetof(struct sk_buff, sk));
9876 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9877 bpf_target_off(struct sock_common,
9878 skc_dport,
9879 2, target_size));
9880 #ifndef __BIG_ENDIAN_BITFIELD
9881 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
9882 #endif
9883 break;
9884
9885 case offsetof(struct __sk_buff, local_port):
9886 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
9887
9888 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9889 si->dst_reg, si->src_reg,
9890 offsetof(struct sk_buff, sk));
9891 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9892 bpf_target_off(struct sock_common,
9893 skc_num, 2, target_size));
9894 break;
9895
9896 case offsetof(struct __sk_buff, tstamp):
9897 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
9898
9899 if (type == BPF_WRITE)
9900 insn = bpf_convert_tstamp_write(prog, si, insn);
9901 else
9902 insn = bpf_convert_tstamp_read(prog, si, insn);
9903 break;
9904
9905 case offsetof(struct __sk_buff, tstamp_type):
9906 insn = bpf_convert_tstamp_type_read(si, insn);
9907 break;
9908
9909 case offsetof(struct __sk_buff, gso_segs):
9910 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9911 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
9912 si->dst_reg, si->dst_reg,
9913 bpf_target_off(struct skb_shared_info,
9914 gso_segs, 2,
9915 target_size));
9916 break;
9917 case offsetof(struct __sk_buff, gso_size):
9918 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9919 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
9920 si->dst_reg, si->dst_reg,
9921 bpf_target_off(struct skb_shared_info,
9922 gso_size, 2,
9923 target_size));
9924 break;
9925 case offsetof(struct __sk_buff, wire_len):
9926 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
9927
9928 off = si->off;
9929 off -= offsetof(struct __sk_buff, wire_len);
9930 off += offsetof(struct sk_buff, cb);
9931 off += offsetof(struct qdisc_skb_cb, pkt_len);
9932 *target_size = 4;
9933 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
9934 break;
9935
9936 case offsetof(struct __sk_buff, sk):
9937 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9938 si->dst_reg, si->src_reg,
9939 offsetof(struct sk_buff, sk));
9940 break;
9941 case offsetof(struct __sk_buff, hwtstamp):
9942 BUILD_BUG_ON(sizeof_field(struct skb_shared_hwtstamps, hwtstamp) != 8);
9943 BUILD_BUG_ON(offsetof(struct skb_shared_hwtstamps, hwtstamp) != 0);
9944
9945 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9946 *insn++ = BPF_LDX_MEM(BPF_DW,
9947 si->dst_reg, si->dst_reg,
9948 bpf_target_off(struct skb_shared_info,
9949 hwtstamps, 8,
9950 target_size));
9951 break;
9952 }
9953
9954 return insn - insn_buf;
9955 }
9956
bpf_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)9957 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
9958 const struct bpf_insn *si,
9959 struct bpf_insn *insn_buf,
9960 struct bpf_prog *prog, u32 *target_size)
9961 {
9962 struct bpf_insn *insn = insn_buf;
9963 int off;
9964
9965 switch (si->off) {
9966 case offsetof(struct bpf_sock, bound_dev_if):
9967 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
9968
9969 if (type == BPF_WRITE)
9970 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9971 offsetof(struct sock, sk_bound_dev_if));
9972 else
9973 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9974 offsetof(struct sock, sk_bound_dev_if));
9975 break;
9976
9977 case offsetof(struct bpf_sock, mark):
9978 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
9979
9980 if (type == BPF_WRITE)
9981 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9982 offsetof(struct sock, sk_mark));
9983 else
9984 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9985 offsetof(struct sock, sk_mark));
9986 break;
9987
9988 case offsetof(struct bpf_sock, priority):
9989 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
9990
9991 if (type == BPF_WRITE)
9992 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9993 offsetof(struct sock, sk_priority));
9994 else
9995 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9996 offsetof(struct sock, sk_priority));
9997 break;
9998
9999 case offsetof(struct bpf_sock, family):
10000 *insn++ = BPF_LDX_MEM(
10001 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
10002 si->dst_reg, si->src_reg,
10003 bpf_target_off(struct sock_common,
10004 skc_family,
10005 sizeof_field(struct sock_common,
10006 skc_family),
10007 target_size));
10008 break;
10009
10010 case offsetof(struct bpf_sock, type):
10011 *insn++ = BPF_LDX_MEM(
10012 BPF_FIELD_SIZEOF(struct sock, sk_type),
10013 si->dst_reg, si->src_reg,
10014 bpf_target_off(struct sock, sk_type,
10015 sizeof_field(struct sock, sk_type),
10016 target_size));
10017 break;
10018
10019 case offsetof(struct bpf_sock, protocol):
10020 *insn++ = BPF_LDX_MEM(
10021 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
10022 si->dst_reg, si->src_reg,
10023 bpf_target_off(struct sock, sk_protocol,
10024 sizeof_field(struct sock, sk_protocol),
10025 target_size));
10026 break;
10027
10028 case offsetof(struct bpf_sock, src_ip4):
10029 *insn++ = BPF_LDX_MEM(
10030 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
10031 bpf_target_off(struct sock_common, skc_rcv_saddr,
10032 sizeof_field(struct sock_common,
10033 skc_rcv_saddr),
10034 target_size));
10035 break;
10036
10037 case offsetof(struct bpf_sock, dst_ip4):
10038 *insn++ = BPF_LDX_MEM(
10039 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
10040 bpf_target_off(struct sock_common, skc_daddr,
10041 sizeof_field(struct sock_common,
10042 skc_daddr),
10043 target_size));
10044 break;
10045
10046 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
10047 #if IS_ENABLED(CONFIG_IPV6)
10048 off = si->off;
10049 off -= offsetof(struct bpf_sock, src_ip6[0]);
10050 *insn++ = BPF_LDX_MEM(
10051 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
10052 bpf_target_off(
10053 struct sock_common,
10054 skc_v6_rcv_saddr.s6_addr32[0],
10055 sizeof_field(struct sock_common,
10056 skc_v6_rcv_saddr.s6_addr32[0]),
10057 target_size) + off);
10058 #else
10059 (void)off;
10060 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10061 #endif
10062 break;
10063
10064 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
10065 #if IS_ENABLED(CONFIG_IPV6)
10066 off = si->off;
10067 off -= offsetof(struct bpf_sock, dst_ip6[0]);
10068 *insn++ = BPF_LDX_MEM(
10069 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
10070 bpf_target_off(struct sock_common,
10071 skc_v6_daddr.s6_addr32[0],
10072 sizeof_field(struct sock_common,
10073 skc_v6_daddr.s6_addr32[0]),
10074 target_size) + off);
10075 #else
10076 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10077 *target_size = 4;
10078 #endif
10079 break;
10080
10081 case offsetof(struct bpf_sock, src_port):
10082 *insn++ = BPF_LDX_MEM(
10083 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
10084 si->dst_reg, si->src_reg,
10085 bpf_target_off(struct sock_common, skc_num,
10086 sizeof_field(struct sock_common,
10087 skc_num),
10088 target_size));
10089 break;
10090
10091 case offsetof(struct bpf_sock, dst_port):
10092 *insn++ = BPF_LDX_MEM(
10093 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
10094 si->dst_reg, si->src_reg,
10095 bpf_target_off(struct sock_common, skc_dport,
10096 sizeof_field(struct sock_common,
10097 skc_dport),
10098 target_size));
10099 break;
10100
10101 case offsetof(struct bpf_sock, state):
10102 *insn++ = BPF_LDX_MEM(
10103 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
10104 si->dst_reg, si->src_reg,
10105 bpf_target_off(struct sock_common, skc_state,
10106 sizeof_field(struct sock_common,
10107 skc_state),
10108 target_size));
10109 break;
10110 case offsetof(struct bpf_sock, rx_queue_mapping):
10111 #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
10112 *insn++ = BPF_LDX_MEM(
10113 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
10114 si->dst_reg, si->src_reg,
10115 bpf_target_off(struct sock, sk_rx_queue_mapping,
10116 sizeof_field(struct sock,
10117 sk_rx_queue_mapping),
10118 target_size));
10119 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
10120 1);
10121 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
10122 #else
10123 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
10124 *target_size = 2;
10125 #endif
10126 break;
10127 }
10128
10129 return insn - insn_buf;
10130 }
10131
tc_cls_act_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10132 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
10133 const struct bpf_insn *si,
10134 struct bpf_insn *insn_buf,
10135 struct bpf_prog *prog, u32 *target_size)
10136 {
10137 struct bpf_insn *insn = insn_buf;
10138
10139 switch (si->off) {
10140 case offsetof(struct __sk_buff, ifindex):
10141 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
10142 si->dst_reg, si->src_reg,
10143 offsetof(struct sk_buff, dev));
10144 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10145 bpf_target_off(struct net_device, ifindex, 4,
10146 target_size));
10147 break;
10148 default:
10149 return bpf_convert_ctx_access(type, si, insn_buf, prog,
10150 target_size);
10151 }
10152
10153 return insn - insn_buf;
10154 }
10155
xdp_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10156 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
10157 const struct bpf_insn *si,
10158 struct bpf_insn *insn_buf,
10159 struct bpf_prog *prog, u32 *target_size)
10160 {
10161 struct bpf_insn *insn = insn_buf;
10162
10163 switch (si->off) {
10164 case offsetof(struct xdp_md, data):
10165 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
10166 si->dst_reg, si->src_reg,
10167 offsetof(struct xdp_buff, data));
10168 break;
10169 case offsetof(struct xdp_md, data_meta):
10170 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
10171 si->dst_reg, si->src_reg,
10172 offsetof(struct xdp_buff, data_meta));
10173 break;
10174 case offsetof(struct xdp_md, data_end):
10175 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
10176 si->dst_reg, si->src_reg,
10177 offsetof(struct xdp_buff, data_end));
10178 break;
10179 case offsetof(struct xdp_md, ingress_ifindex):
10180 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
10181 si->dst_reg, si->src_reg,
10182 offsetof(struct xdp_buff, rxq));
10183 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
10184 si->dst_reg, si->dst_reg,
10185 offsetof(struct xdp_rxq_info, dev));
10186 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10187 offsetof(struct net_device, ifindex));
10188 break;
10189 case offsetof(struct xdp_md, rx_queue_index):
10190 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
10191 si->dst_reg, si->src_reg,
10192 offsetof(struct xdp_buff, rxq));
10193 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10194 offsetof(struct xdp_rxq_info,
10195 queue_index));
10196 break;
10197 case offsetof(struct xdp_md, egress_ifindex):
10198 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
10199 si->dst_reg, si->src_reg,
10200 offsetof(struct xdp_buff, txq));
10201 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
10202 si->dst_reg, si->dst_reg,
10203 offsetof(struct xdp_txq_info, dev));
10204 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10205 offsetof(struct net_device, ifindex));
10206 break;
10207 }
10208
10209 return insn - insn_buf;
10210 }
10211
10212 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
10213 * context Structure, F is Field in context structure that contains a pointer
10214 * to Nested Structure of type NS that has the field NF.
10215 *
10216 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
10217 * sure that SIZE is not greater than actual size of S.F.NF.
10218 *
10219 * If offset OFF is provided, the load happens from that offset relative to
10220 * offset of NF.
10221 */
10222 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
10223 do { \
10224 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
10225 si->src_reg, offsetof(S, F)); \
10226 *insn++ = BPF_LDX_MEM( \
10227 SIZE, si->dst_reg, si->dst_reg, \
10228 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
10229 target_size) \
10230 + OFF); \
10231 } while (0)
10232
10233 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
10234 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
10235 BPF_FIELD_SIZEOF(NS, NF), 0)
10236
10237 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
10238 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
10239 *
10240 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
10241 * "register" since two registers available in convert_ctx_access are not
10242 * enough: we can't override neither SRC, since it contains value to store, nor
10243 * DST since it contains pointer to context that may be used by later
10244 * instructions. But we need a temporary place to save pointer to nested
10245 * structure whose field we want to store to.
10246 */
10247 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
10248 do { \
10249 int tmp_reg = BPF_REG_9; \
10250 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
10251 --tmp_reg; \
10252 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
10253 --tmp_reg; \
10254 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
10255 offsetof(S, TF)); \
10256 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
10257 si->dst_reg, offsetof(S, F)); \
10258 *insn++ = BPF_RAW_INSN(SIZE | BPF_MEM | BPF_CLASS(si->code), \
10259 tmp_reg, si->src_reg, \
10260 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
10261 target_size) \
10262 + OFF, \
10263 si->imm); \
10264 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
10265 offsetof(S, TF)); \
10266 } while (0)
10267
10268 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
10269 TF) \
10270 do { \
10271 if (type == BPF_WRITE) { \
10272 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
10273 OFF, TF); \
10274 } else { \
10275 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
10276 S, NS, F, NF, SIZE, OFF); \
10277 } \
10278 } while (0)
10279
10280 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
10281 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
10282 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
10283
sock_addr_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10284 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
10285 const struct bpf_insn *si,
10286 struct bpf_insn *insn_buf,
10287 struct bpf_prog *prog, u32 *target_size)
10288 {
10289 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
10290 struct bpf_insn *insn = insn_buf;
10291
10292 switch (si->off) {
10293 case offsetof(struct bpf_sock_addr, user_family):
10294 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10295 struct sockaddr, uaddr, sa_family);
10296 break;
10297
10298 case offsetof(struct bpf_sock_addr, user_ip4):
10299 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10300 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
10301 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
10302 break;
10303
10304 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
10305 off = si->off;
10306 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
10307 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10308 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
10309 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
10310 tmp_reg);
10311 break;
10312
10313 case offsetof(struct bpf_sock_addr, user_port):
10314 /* To get port we need to know sa_family first and then treat
10315 * sockaddr as either sockaddr_in or sockaddr_in6.
10316 * Though we can simplify since port field has same offset and
10317 * size in both structures.
10318 * Here we check this invariant and use just one of the
10319 * structures if it's true.
10320 */
10321 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
10322 offsetof(struct sockaddr_in6, sin6_port));
10323 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
10324 sizeof_field(struct sockaddr_in6, sin6_port));
10325 /* Account for sin6_port being smaller than user_port. */
10326 port_size = min(port_size, BPF_LDST_BYTES(si));
10327 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10328 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
10329 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
10330 break;
10331
10332 case offsetof(struct bpf_sock_addr, family):
10333 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10334 struct sock, sk, sk_family);
10335 break;
10336
10337 case offsetof(struct bpf_sock_addr, type):
10338 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10339 struct sock, sk, sk_type);
10340 break;
10341
10342 case offsetof(struct bpf_sock_addr, protocol):
10343 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10344 struct sock, sk, sk_protocol);
10345 break;
10346
10347 case offsetof(struct bpf_sock_addr, msg_src_ip4):
10348 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
10349 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10350 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
10351 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
10352 break;
10353
10354 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
10355 msg_src_ip6[3]):
10356 off = si->off;
10357 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
10358 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
10359 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10360 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
10361 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
10362 break;
10363 case offsetof(struct bpf_sock_addr, sk):
10364 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
10365 si->dst_reg, si->src_reg,
10366 offsetof(struct bpf_sock_addr_kern, sk));
10367 break;
10368 }
10369
10370 return insn - insn_buf;
10371 }
10372
sock_ops_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10373 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
10374 const struct bpf_insn *si,
10375 struct bpf_insn *insn_buf,
10376 struct bpf_prog *prog,
10377 u32 *target_size)
10378 {
10379 struct bpf_insn *insn = insn_buf;
10380 int off;
10381
10382 /* Helper macro for adding read access to tcp_sock or sock fields. */
10383 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
10384 do { \
10385 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \
10386 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
10387 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
10388 if (si->dst_reg == reg || si->src_reg == reg) \
10389 reg--; \
10390 if (si->dst_reg == reg || si->src_reg == reg) \
10391 reg--; \
10392 if (si->dst_reg == si->src_reg) { \
10393 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10394 offsetof(struct bpf_sock_ops_kern, \
10395 temp)); \
10396 fullsock_reg = reg; \
10397 jmp += 2; \
10398 } \
10399 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10400 struct bpf_sock_ops_kern, \
10401 is_fullsock), \
10402 fullsock_reg, si->src_reg, \
10403 offsetof(struct bpf_sock_ops_kern, \
10404 is_fullsock)); \
10405 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10406 if (si->dst_reg == si->src_reg) \
10407 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10408 offsetof(struct bpf_sock_ops_kern, \
10409 temp)); \
10410 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10411 struct bpf_sock_ops_kern, sk),\
10412 si->dst_reg, si->src_reg, \
10413 offsetof(struct bpf_sock_ops_kern, sk));\
10414 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
10415 OBJ_FIELD), \
10416 si->dst_reg, si->dst_reg, \
10417 offsetof(OBJ, OBJ_FIELD)); \
10418 if (si->dst_reg == si->src_reg) { \
10419 *insn++ = BPF_JMP_A(1); \
10420 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10421 offsetof(struct bpf_sock_ops_kern, \
10422 temp)); \
10423 } \
10424 } while (0)
10425
10426 #define SOCK_OPS_GET_SK() \
10427 do { \
10428 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \
10429 if (si->dst_reg == reg || si->src_reg == reg) \
10430 reg--; \
10431 if (si->dst_reg == reg || si->src_reg == reg) \
10432 reg--; \
10433 if (si->dst_reg == si->src_reg) { \
10434 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10435 offsetof(struct bpf_sock_ops_kern, \
10436 temp)); \
10437 fullsock_reg = reg; \
10438 jmp += 2; \
10439 } \
10440 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10441 struct bpf_sock_ops_kern, \
10442 is_fullsock), \
10443 fullsock_reg, si->src_reg, \
10444 offsetof(struct bpf_sock_ops_kern, \
10445 is_fullsock)); \
10446 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10447 if (si->dst_reg == si->src_reg) \
10448 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10449 offsetof(struct bpf_sock_ops_kern, \
10450 temp)); \
10451 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10452 struct bpf_sock_ops_kern, sk),\
10453 si->dst_reg, si->src_reg, \
10454 offsetof(struct bpf_sock_ops_kern, sk));\
10455 if (si->dst_reg == si->src_reg) { \
10456 *insn++ = BPF_JMP_A(1); \
10457 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10458 offsetof(struct bpf_sock_ops_kern, \
10459 temp)); \
10460 } \
10461 } while (0)
10462
10463 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
10464 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
10465
10466 /* Helper macro for adding write access to tcp_sock or sock fields.
10467 * The macro is called with two registers, dst_reg which contains a pointer
10468 * to ctx (context) and src_reg which contains the value that should be
10469 * stored. However, we need an additional register since we cannot overwrite
10470 * dst_reg because it may be used later in the program.
10471 * Instead we "borrow" one of the other register. We first save its value
10472 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
10473 * it at the end of the macro.
10474 */
10475 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
10476 do { \
10477 int reg = BPF_REG_9; \
10478 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
10479 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
10480 if (si->dst_reg == reg || si->src_reg == reg) \
10481 reg--; \
10482 if (si->dst_reg == reg || si->src_reg == reg) \
10483 reg--; \
10484 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
10485 offsetof(struct bpf_sock_ops_kern, \
10486 temp)); \
10487 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10488 struct bpf_sock_ops_kern, \
10489 is_fullsock), \
10490 reg, si->dst_reg, \
10491 offsetof(struct bpf_sock_ops_kern, \
10492 is_fullsock)); \
10493 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
10494 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10495 struct bpf_sock_ops_kern, sk),\
10496 reg, si->dst_reg, \
10497 offsetof(struct bpf_sock_ops_kern, sk));\
10498 *insn++ = BPF_RAW_INSN(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD) | \
10499 BPF_MEM | BPF_CLASS(si->code), \
10500 reg, si->src_reg, \
10501 offsetof(OBJ, OBJ_FIELD), \
10502 si->imm); \
10503 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
10504 offsetof(struct bpf_sock_ops_kern, \
10505 temp)); \
10506 } while (0)
10507
10508 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
10509 do { \
10510 if (TYPE == BPF_WRITE) \
10511 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10512 else \
10513 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10514 } while (0)
10515
10516 switch (si->off) {
10517 case offsetof(struct bpf_sock_ops, op):
10518 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10519 op),
10520 si->dst_reg, si->src_reg,
10521 offsetof(struct bpf_sock_ops_kern, op));
10522 break;
10523
10524 case offsetof(struct bpf_sock_ops, replylong[0]) ...
10525 offsetof(struct bpf_sock_ops, replylong[3]):
10526 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
10527 sizeof_field(struct bpf_sock_ops_kern, reply));
10528 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
10529 sizeof_field(struct bpf_sock_ops_kern, replylong));
10530 off = si->off;
10531 off -= offsetof(struct bpf_sock_ops, replylong[0]);
10532 off += offsetof(struct bpf_sock_ops_kern, replylong[0]);
10533 if (type == BPF_WRITE)
10534 *insn++ = BPF_EMIT_STORE(BPF_W, si, off);
10535 else
10536 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10537 off);
10538 break;
10539
10540 case offsetof(struct bpf_sock_ops, family):
10541 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
10542
10543 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10544 struct bpf_sock_ops_kern, sk),
10545 si->dst_reg, si->src_reg,
10546 offsetof(struct bpf_sock_ops_kern, sk));
10547 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10548 offsetof(struct sock_common, skc_family));
10549 break;
10550
10551 case offsetof(struct bpf_sock_ops, remote_ip4):
10552 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10553
10554 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10555 struct bpf_sock_ops_kern, sk),
10556 si->dst_reg, si->src_reg,
10557 offsetof(struct bpf_sock_ops_kern, sk));
10558 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10559 offsetof(struct sock_common, skc_daddr));
10560 break;
10561
10562 case offsetof(struct bpf_sock_ops, local_ip4):
10563 BUILD_BUG_ON(sizeof_field(struct sock_common,
10564 skc_rcv_saddr) != 4);
10565
10566 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10567 struct bpf_sock_ops_kern, sk),
10568 si->dst_reg, si->src_reg,
10569 offsetof(struct bpf_sock_ops_kern, sk));
10570 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10571 offsetof(struct sock_common,
10572 skc_rcv_saddr));
10573 break;
10574
10575 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
10576 offsetof(struct bpf_sock_ops, remote_ip6[3]):
10577 #if IS_ENABLED(CONFIG_IPV6)
10578 BUILD_BUG_ON(sizeof_field(struct sock_common,
10579 skc_v6_daddr.s6_addr32[0]) != 4);
10580
10581 off = si->off;
10582 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
10583 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10584 struct bpf_sock_ops_kern, sk),
10585 si->dst_reg, si->src_reg,
10586 offsetof(struct bpf_sock_ops_kern, sk));
10587 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10588 offsetof(struct sock_common,
10589 skc_v6_daddr.s6_addr32[0]) +
10590 off);
10591 #else
10592 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10593 #endif
10594 break;
10595
10596 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
10597 offsetof(struct bpf_sock_ops, local_ip6[3]):
10598 #if IS_ENABLED(CONFIG_IPV6)
10599 BUILD_BUG_ON(sizeof_field(struct sock_common,
10600 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10601
10602 off = si->off;
10603 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
10604 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10605 struct bpf_sock_ops_kern, sk),
10606 si->dst_reg, si->src_reg,
10607 offsetof(struct bpf_sock_ops_kern, sk));
10608 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10609 offsetof(struct sock_common,
10610 skc_v6_rcv_saddr.s6_addr32[0]) +
10611 off);
10612 #else
10613 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10614 #endif
10615 break;
10616
10617 case offsetof(struct bpf_sock_ops, remote_port):
10618 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10619
10620 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10621 struct bpf_sock_ops_kern, sk),
10622 si->dst_reg, si->src_reg,
10623 offsetof(struct bpf_sock_ops_kern, sk));
10624 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10625 offsetof(struct sock_common, skc_dport));
10626 #ifndef __BIG_ENDIAN_BITFIELD
10627 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10628 #endif
10629 break;
10630
10631 case offsetof(struct bpf_sock_ops, local_port):
10632 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10633
10634 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10635 struct bpf_sock_ops_kern, sk),
10636 si->dst_reg, si->src_reg,
10637 offsetof(struct bpf_sock_ops_kern, sk));
10638 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10639 offsetof(struct sock_common, skc_num));
10640 break;
10641
10642 case offsetof(struct bpf_sock_ops, is_fullsock):
10643 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10644 struct bpf_sock_ops_kern,
10645 is_fullsock),
10646 si->dst_reg, si->src_reg,
10647 offsetof(struct bpf_sock_ops_kern,
10648 is_fullsock));
10649 break;
10650
10651 case offsetof(struct bpf_sock_ops, state):
10652 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
10653
10654 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10655 struct bpf_sock_ops_kern, sk),
10656 si->dst_reg, si->src_reg,
10657 offsetof(struct bpf_sock_ops_kern, sk));
10658 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
10659 offsetof(struct sock_common, skc_state));
10660 break;
10661
10662 case offsetof(struct bpf_sock_ops, rtt_min):
10663 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
10664 sizeof(struct minmax));
10665 BUILD_BUG_ON(sizeof(struct minmax) <
10666 sizeof(struct minmax_sample));
10667
10668 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10669 struct bpf_sock_ops_kern, sk),
10670 si->dst_reg, si->src_reg,
10671 offsetof(struct bpf_sock_ops_kern, sk));
10672 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10673 offsetof(struct tcp_sock, rtt_min) +
10674 sizeof_field(struct minmax_sample, t));
10675 break;
10676
10677 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
10678 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
10679 struct tcp_sock);
10680 break;
10681
10682 case offsetof(struct bpf_sock_ops, sk_txhash):
10683 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
10684 struct sock, type);
10685 break;
10686 case offsetof(struct bpf_sock_ops, snd_cwnd):
10687 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
10688 break;
10689 case offsetof(struct bpf_sock_ops, srtt_us):
10690 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
10691 break;
10692 case offsetof(struct bpf_sock_ops, snd_ssthresh):
10693 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
10694 break;
10695 case offsetof(struct bpf_sock_ops, rcv_nxt):
10696 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
10697 break;
10698 case offsetof(struct bpf_sock_ops, snd_nxt):
10699 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
10700 break;
10701 case offsetof(struct bpf_sock_ops, snd_una):
10702 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
10703 break;
10704 case offsetof(struct bpf_sock_ops, mss_cache):
10705 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
10706 break;
10707 case offsetof(struct bpf_sock_ops, ecn_flags):
10708 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
10709 break;
10710 case offsetof(struct bpf_sock_ops, rate_delivered):
10711 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
10712 break;
10713 case offsetof(struct bpf_sock_ops, rate_interval_us):
10714 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
10715 break;
10716 case offsetof(struct bpf_sock_ops, packets_out):
10717 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
10718 break;
10719 case offsetof(struct bpf_sock_ops, retrans_out):
10720 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
10721 break;
10722 case offsetof(struct bpf_sock_ops, total_retrans):
10723 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
10724 break;
10725 case offsetof(struct bpf_sock_ops, segs_in):
10726 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
10727 break;
10728 case offsetof(struct bpf_sock_ops, data_segs_in):
10729 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
10730 break;
10731 case offsetof(struct bpf_sock_ops, segs_out):
10732 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
10733 break;
10734 case offsetof(struct bpf_sock_ops, data_segs_out):
10735 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
10736 break;
10737 case offsetof(struct bpf_sock_ops, lost_out):
10738 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
10739 break;
10740 case offsetof(struct bpf_sock_ops, sacked_out):
10741 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
10742 break;
10743 case offsetof(struct bpf_sock_ops, bytes_received):
10744 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
10745 break;
10746 case offsetof(struct bpf_sock_ops, bytes_acked):
10747 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
10748 break;
10749 case offsetof(struct bpf_sock_ops, sk):
10750 SOCK_OPS_GET_SK();
10751 break;
10752 case offsetof(struct bpf_sock_ops, skb_data_end):
10753 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10754 skb_data_end),
10755 si->dst_reg, si->src_reg,
10756 offsetof(struct bpf_sock_ops_kern,
10757 skb_data_end));
10758 break;
10759 case offsetof(struct bpf_sock_ops, skb_data):
10760 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10761 skb),
10762 si->dst_reg, si->src_reg,
10763 offsetof(struct bpf_sock_ops_kern,
10764 skb));
10765 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10766 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10767 si->dst_reg, si->dst_reg,
10768 offsetof(struct sk_buff, data));
10769 break;
10770 case offsetof(struct bpf_sock_ops, skb_len):
10771 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10772 skb),
10773 si->dst_reg, si->src_reg,
10774 offsetof(struct bpf_sock_ops_kern,
10775 skb));
10776 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10777 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
10778 si->dst_reg, si->dst_reg,
10779 offsetof(struct sk_buff, len));
10780 break;
10781 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
10782 off = offsetof(struct sk_buff, cb);
10783 off += offsetof(struct tcp_skb_cb, tcp_flags);
10784 *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags);
10785 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10786 skb),
10787 si->dst_reg, si->src_reg,
10788 offsetof(struct bpf_sock_ops_kern,
10789 skb));
10790 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10791 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb,
10792 tcp_flags),
10793 si->dst_reg, si->dst_reg, off);
10794 break;
10795 case offsetof(struct bpf_sock_ops, skb_hwtstamp): {
10796 struct bpf_insn *jmp_on_null_skb;
10797
10798 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10799 skb),
10800 si->dst_reg, si->src_reg,
10801 offsetof(struct bpf_sock_ops_kern,
10802 skb));
10803 /* Reserve one insn to test skb == NULL */
10804 jmp_on_null_skb = insn++;
10805 insn = bpf_convert_shinfo_access(si->dst_reg, si->dst_reg, insn);
10806 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
10807 bpf_target_off(struct skb_shared_info,
10808 hwtstamps, 8,
10809 target_size));
10810 *jmp_on_null_skb = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0,
10811 insn - jmp_on_null_skb - 1);
10812 break;
10813 }
10814 }
10815 return insn - insn_buf;
10816 }
10817
10818 /* data_end = skb->data + skb_headlen() */
bpf_convert_data_end_access(const struct bpf_insn * si,struct bpf_insn * insn)10819 static struct bpf_insn *bpf_convert_data_end_access(const struct bpf_insn *si,
10820 struct bpf_insn *insn)
10821 {
10822 int reg;
10823 int temp_reg_off = offsetof(struct sk_buff, cb) +
10824 offsetof(struct sk_skb_cb, temp_reg);
10825
10826 if (si->src_reg == si->dst_reg) {
10827 /* We need an extra register, choose and save a register. */
10828 reg = BPF_REG_9;
10829 if (si->src_reg == reg || si->dst_reg == reg)
10830 reg--;
10831 if (si->src_reg == reg || si->dst_reg == reg)
10832 reg--;
10833 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, temp_reg_off);
10834 } else {
10835 reg = si->dst_reg;
10836 }
10837
10838 /* reg = skb->data */
10839 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10840 reg, si->src_reg,
10841 offsetof(struct sk_buff, data));
10842 /* AX = skb->len */
10843 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
10844 BPF_REG_AX, si->src_reg,
10845 offsetof(struct sk_buff, len));
10846 /* reg = skb->data + skb->len */
10847 *insn++ = BPF_ALU64_REG(BPF_ADD, reg, BPF_REG_AX);
10848 /* AX = skb->data_len */
10849 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data_len),
10850 BPF_REG_AX, si->src_reg,
10851 offsetof(struct sk_buff, data_len));
10852
10853 /* reg = skb->data + skb->len - skb->data_len */
10854 *insn++ = BPF_ALU64_REG(BPF_SUB, reg, BPF_REG_AX);
10855
10856 if (si->src_reg == si->dst_reg) {
10857 /* Restore the saved register */
10858 *insn++ = BPF_MOV64_REG(BPF_REG_AX, si->src_reg);
10859 *insn++ = BPF_MOV64_REG(si->dst_reg, reg);
10860 *insn++ = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_AX, temp_reg_off);
10861 }
10862
10863 return insn;
10864 }
10865
sk_skb_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10866 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
10867 const struct bpf_insn *si,
10868 struct bpf_insn *insn_buf,
10869 struct bpf_prog *prog, u32 *target_size)
10870 {
10871 struct bpf_insn *insn = insn_buf;
10872 int off;
10873
10874 switch (si->off) {
10875 case offsetof(struct __sk_buff, data_end):
10876 insn = bpf_convert_data_end_access(si, insn);
10877 break;
10878 case offsetof(struct __sk_buff, cb[0]) ...
10879 offsetofend(struct __sk_buff, cb[4]) - 1:
10880 BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20);
10881 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
10882 offsetof(struct sk_skb_cb, data)) %
10883 sizeof(__u64));
10884
10885 prog->cb_access = 1;
10886 off = si->off;
10887 off -= offsetof(struct __sk_buff, cb[0]);
10888 off += offsetof(struct sk_buff, cb);
10889 off += offsetof(struct sk_skb_cb, data);
10890 if (type == BPF_WRITE)
10891 *insn++ = BPF_EMIT_STORE(BPF_SIZE(si->code), si, off);
10892 else
10893 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
10894 si->src_reg, off);
10895 break;
10896
10897
10898 default:
10899 return bpf_convert_ctx_access(type, si, insn_buf, prog,
10900 target_size);
10901 }
10902
10903 return insn - insn_buf;
10904 }
10905
sk_msg_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10906 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
10907 const struct bpf_insn *si,
10908 struct bpf_insn *insn_buf,
10909 struct bpf_prog *prog, u32 *target_size)
10910 {
10911 struct bpf_insn *insn = insn_buf;
10912 #if IS_ENABLED(CONFIG_IPV6)
10913 int off;
10914 #endif
10915
10916 /* convert ctx uses the fact sg element is first in struct */
10917 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
10918
10919 switch (si->off) {
10920 case offsetof(struct sk_msg_md, data):
10921 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
10922 si->dst_reg, si->src_reg,
10923 offsetof(struct sk_msg, data));
10924 break;
10925 case offsetof(struct sk_msg_md, data_end):
10926 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
10927 si->dst_reg, si->src_reg,
10928 offsetof(struct sk_msg, data_end));
10929 break;
10930 case offsetof(struct sk_msg_md, family):
10931 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
10932
10933 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10934 struct sk_msg, sk),
10935 si->dst_reg, si->src_reg,
10936 offsetof(struct sk_msg, sk));
10937 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10938 offsetof(struct sock_common, skc_family));
10939 break;
10940
10941 case offsetof(struct sk_msg_md, remote_ip4):
10942 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10943
10944 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10945 struct sk_msg, sk),
10946 si->dst_reg, si->src_reg,
10947 offsetof(struct sk_msg, sk));
10948 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10949 offsetof(struct sock_common, skc_daddr));
10950 break;
10951
10952 case offsetof(struct sk_msg_md, local_ip4):
10953 BUILD_BUG_ON(sizeof_field(struct sock_common,
10954 skc_rcv_saddr) != 4);
10955
10956 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10957 struct sk_msg, sk),
10958 si->dst_reg, si->src_reg,
10959 offsetof(struct sk_msg, sk));
10960 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10961 offsetof(struct sock_common,
10962 skc_rcv_saddr));
10963 break;
10964
10965 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
10966 offsetof(struct sk_msg_md, remote_ip6[3]):
10967 #if IS_ENABLED(CONFIG_IPV6)
10968 BUILD_BUG_ON(sizeof_field(struct sock_common,
10969 skc_v6_daddr.s6_addr32[0]) != 4);
10970
10971 off = si->off;
10972 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
10973 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10974 struct sk_msg, sk),
10975 si->dst_reg, si->src_reg,
10976 offsetof(struct sk_msg, sk));
10977 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10978 offsetof(struct sock_common,
10979 skc_v6_daddr.s6_addr32[0]) +
10980 off);
10981 #else
10982 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10983 #endif
10984 break;
10985
10986 case offsetof(struct sk_msg_md, local_ip6[0]) ...
10987 offsetof(struct sk_msg_md, local_ip6[3]):
10988 #if IS_ENABLED(CONFIG_IPV6)
10989 BUILD_BUG_ON(sizeof_field(struct sock_common,
10990 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10991
10992 off = si->off;
10993 off -= offsetof(struct sk_msg_md, local_ip6[0]);
10994 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10995 struct sk_msg, sk),
10996 si->dst_reg, si->src_reg,
10997 offsetof(struct sk_msg, sk));
10998 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10999 offsetof(struct sock_common,
11000 skc_v6_rcv_saddr.s6_addr32[0]) +
11001 off);
11002 #else
11003 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11004 #endif
11005 break;
11006
11007 case offsetof(struct sk_msg_md, remote_port):
11008 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
11009
11010 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
11011 struct sk_msg, sk),
11012 si->dst_reg, si->src_reg,
11013 offsetof(struct sk_msg, sk));
11014 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
11015 offsetof(struct sock_common, skc_dport));
11016 #ifndef __BIG_ENDIAN_BITFIELD
11017 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
11018 #endif
11019 break;
11020
11021 case offsetof(struct sk_msg_md, local_port):
11022 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
11023
11024 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
11025 struct sk_msg, sk),
11026 si->dst_reg, si->src_reg,
11027 offsetof(struct sk_msg, sk));
11028 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
11029 offsetof(struct sock_common, skc_num));
11030 break;
11031
11032 case offsetof(struct sk_msg_md, size):
11033 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
11034 si->dst_reg, si->src_reg,
11035 offsetof(struct sk_msg_sg, size));
11036 break;
11037
11038 case offsetof(struct sk_msg_md, sk):
11039 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
11040 si->dst_reg, si->src_reg,
11041 offsetof(struct sk_msg, sk));
11042 break;
11043 }
11044
11045 return insn - insn_buf;
11046 }
11047
11048 const struct bpf_verifier_ops sk_filter_verifier_ops = {
11049 .get_func_proto = sk_filter_func_proto,
11050 .is_valid_access = sk_filter_is_valid_access,
11051 .convert_ctx_access = bpf_convert_ctx_access,
11052 .gen_ld_abs = bpf_gen_ld_abs,
11053 };
11054
11055 const struct bpf_prog_ops sk_filter_prog_ops = {
11056 .test_run = bpf_prog_test_run_skb,
11057 };
11058
11059 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
11060 .get_func_proto = tc_cls_act_func_proto,
11061 .is_valid_access = tc_cls_act_is_valid_access,
11062 .convert_ctx_access = tc_cls_act_convert_ctx_access,
11063 .gen_prologue = tc_cls_act_prologue,
11064 .gen_ld_abs = bpf_gen_ld_abs,
11065 .btf_struct_access = tc_cls_act_btf_struct_access,
11066 };
11067
11068 const struct bpf_prog_ops tc_cls_act_prog_ops = {
11069 .test_run = bpf_prog_test_run_skb,
11070 };
11071
11072 const struct bpf_verifier_ops xdp_verifier_ops = {
11073 .get_func_proto = xdp_func_proto,
11074 .is_valid_access = xdp_is_valid_access,
11075 .convert_ctx_access = xdp_convert_ctx_access,
11076 .gen_prologue = bpf_noop_prologue,
11077 .btf_struct_access = xdp_btf_struct_access,
11078 };
11079
11080 const struct bpf_prog_ops xdp_prog_ops = {
11081 .test_run = bpf_prog_test_run_xdp,
11082 };
11083
11084 const struct bpf_verifier_ops cg_skb_verifier_ops = {
11085 .get_func_proto = cg_skb_func_proto,
11086 .is_valid_access = cg_skb_is_valid_access,
11087 .convert_ctx_access = bpf_convert_ctx_access,
11088 };
11089
11090 const struct bpf_prog_ops cg_skb_prog_ops = {
11091 .test_run = bpf_prog_test_run_skb,
11092 };
11093
11094 const struct bpf_verifier_ops lwt_in_verifier_ops = {
11095 .get_func_proto = lwt_in_func_proto,
11096 .is_valid_access = lwt_is_valid_access,
11097 .convert_ctx_access = bpf_convert_ctx_access,
11098 };
11099
11100 const struct bpf_prog_ops lwt_in_prog_ops = {
11101 .test_run = bpf_prog_test_run_skb,
11102 };
11103
11104 const struct bpf_verifier_ops lwt_out_verifier_ops = {
11105 .get_func_proto = lwt_out_func_proto,
11106 .is_valid_access = lwt_is_valid_access,
11107 .convert_ctx_access = bpf_convert_ctx_access,
11108 };
11109
11110 const struct bpf_prog_ops lwt_out_prog_ops = {
11111 .test_run = bpf_prog_test_run_skb,
11112 };
11113
11114 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
11115 .get_func_proto = lwt_xmit_func_proto,
11116 .is_valid_access = lwt_is_valid_access,
11117 .convert_ctx_access = bpf_convert_ctx_access,
11118 .gen_prologue = tc_cls_act_prologue,
11119 };
11120
11121 const struct bpf_prog_ops lwt_xmit_prog_ops = {
11122 .test_run = bpf_prog_test_run_skb,
11123 };
11124
11125 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
11126 .get_func_proto = lwt_seg6local_func_proto,
11127 .is_valid_access = lwt_is_valid_access,
11128 .convert_ctx_access = bpf_convert_ctx_access,
11129 };
11130
11131 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
11132 };
11133
11134 const struct bpf_verifier_ops cg_sock_verifier_ops = {
11135 .get_func_proto = sock_filter_func_proto,
11136 .is_valid_access = sock_filter_is_valid_access,
11137 .convert_ctx_access = bpf_sock_convert_ctx_access,
11138 };
11139
11140 const struct bpf_prog_ops cg_sock_prog_ops = {
11141 };
11142
11143 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
11144 .get_func_proto = sock_addr_func_proto,
11145 .is_valid_access = sock_addr_is_valid_access,
11146 .convert_ctx_access = sock_addr_convert_ctx_access,
11147 };
11148
11149 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
11150 };
11151
11152 const struct bpf_verifier_ops sock_ops_verifier_ops = {
11153 .get_func_proto = sock_ops_func_proto,
11154 .is_valid_access = sock_ops_is_valid_access,
11155 .convert_ctx_access = sock_ops_convert_ctx_access,
11156 };
11157
11158 const struct bpf_prog_ops sock_ops_prog_ops = {
11159 };
11160
11161 const struct bpf_verifier_ops sk_skb_verifier_ops = {
11162 .get_func_proto = sk_skb_func_proto,
11163 .is_valid_access = sk_skb_is_valid_access,
11164 .convert_ctx_access = sk_skb_convert_ctx_access,
11165 .gen_prologue = sk_skb_prologue,
11166 };
11167
11168 const struct bpf_prog_ops sk_skb_prog_ops = {
11169 };
11170
11171 const struct bpf_verifier_ops sk_msg_verifier_ops = {
11172 .get_func_proto = sk_msg_func_proto,
11173 .is_valid_access = sk_msg_is_valid_access,
11174 .convert_ctx_access = sk_msg_convert_ctx_access,
11175 .gen_prologue = bpf_noop_prologue,
11176 };
11177
11178 const struct bpf_prog_ops sk_msg_prog_ops = {
11179 };
11180
11181 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
11182 .get_func_proto = flow_dissector_func_proto,
11183 .is_valid_access = flow_dissector_is_valid_access,
11184 .convert_ctx_access = flow_dissector_convert_ctx_access,
11185 };
11186
11187 const struct bpf_prog_ops flow_dissector_prog_ops = {
11188 .test_run = bpf_prog_test_run_flow_dissector,
11189 };
11190
sk_detach_filter(struct sock * sk)11191 int sk_detach_filter(struct sock *sk)
11192 {
11193 int ret = -ENOENT;
11194 struct sk_filter *filter;
11195
11196 if (sock_flag(sk, SOCK_FILTER_LOCKED))
11197 return -EPERM;
11198
11199 filter = rcu_dereference_protected(sk->sk_filter,
11200 lockdep_sock_is_held(sk));
11201 if (filter) {
11202 RCU_INIT_POINTER(sk->sk_filter, NULL);
11203 sk_filter_uncharge(sk, filter);
11204 ret = 0;
11205 }
11206
11207 return ret;
11208 }
11209 EXPORT_SYMBOL_GPL(sk_detach_filter);
11210
sk_get_filter(struct sock * sk,sockptr_t optval,unsigned int len)11211 int sk_get_filter(struct sock *sk, sockptr_t optval, unsigned int len)
11212 {
11213 struct sock_fprog_kern *fprog;
11214 struct sk_filter *filter;
11215 int ret = 0;
11216
11217 sockopt_lock_sock(sk);
11218 filter = rcu_dereference_protected(sk->sk_filter,
11219 lockdep_sock_is_held(sk));
11220 if (!filter)
11221 goto out;
11222
11223 /* We're copying the filter that has been originally attached,
11224 * so no conversion/decode needed anymore. eBPF programs that
11225 * have no original program cannot be dumped through this.
11226 */
11227 ret = -EACCES;
11228 fprog = filter->prog->orig_prog;
11229 if (!fprog)
11230 goto out;
11231
11232 ret = fprog->len;
11233 if (!len)
11234 /* User space only enquires number of filter blocks. */
11235 goto out;
11236
11237 ret = -EINVAL;
11238 if (len < fprog->len)
11239 goto out;
11240
11241 ret = -EFAULT;
11242 if (copy_to_sockptr(optval, fprog->filter, bpf_classic_proglen(fprog)))
11243 goto out;
11244
11245 /* Instead of bytes, the API requests to return the number
11246 * of filter blocks.
11247 */
11248 ret = fprog->len;
11249 out:
11250 sockopt_release_sock(sk);
11251 return ret;
11252 }
11253
11254 #ifdef CONFIG_INET
bpf_init_reuseport_kern(struct sk_reuseport_kern * reuse_kern,struct sock_reuseport * reuse,struct sock * sk,struct sk_buff * skb,struct sock * migrating_sk,u32 hash)11255 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
11256 struct sock_reuseport *reuse,
11257 struct sock *sk, struct sk_buff *skb,
11258 struct sock *migrating_sk,
11259 u32 hash)
11260 {
11261 reuse_kern->skb = skb;
11262 reuse_kern->sk = sk;
11263 reuse_kern->selected_sk = NULL;
11264 reuse_kern->migrating_sk = migrating_sk;
11265 reuse_kern->data_end = skb->data + skb_headlen(skb);
11266 reuse_kern->hash = hash;
11267 reuse_kern->reuseport_id = reuse->reuseport_id;
11268 reuse_kern->bind_inany = reuse->bind_inany;
11269 }
11270
bpf_run_sk_reuseport(struct sock_reuseport * reuse,struct sock * sk,struct bpf_prog * prog,struct sk_buff * skb,struct sock * migrating_sk,u32 hash)11271 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
11272 struct bpf_prog *prog, struct sk_buff *skb,
11273 struct sock *migrating_sk,
11274 u32 hash)
11275 {
11276 struct sk_reuseport_kern reuse_kern;
11277 enum sk_action action;
11278
11279 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, migrating_sk, hash);
11280 action = bpf_prog_run(prog, &reuse_kern);
11281
11282 if (action == SK_PASS)
11283 return reuse_kern.selected_sk;
11284 else
11285 return ERR_PTR(-ECONNREFUSED);
11286 }
11287
BPF_CALL_4(sk_select_reuseport,struct sk_reuseport_kern *,reuse_kern,struct bpf_map *,map,void *,key,u32,flags)11288 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
11289 struct bpf_map *, map, void *, key, u32, flags)
11290 {
11291 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
11292 struct sock_reuseport *reuse;
11293 struct sock *selected_sk;
11294 int err;
11295
11296 selected_sk = map->ops->map_lookup_elem(map, key);
11297 if (!selected_sk)
11298 return -ENOENT;
11299
11300 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
11301 if (!reuse) {
11302 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
11303 * The only (!reuse) case here is - the sk has already been
11304 * unhashed (e.g. by close()), so treat it as -ENOENT.
11305 *
11306 * Other maps (e.g. sock_map) do not provide this guarantee and
11307 * the sk may never be in the reuseport group to begin with.
11308 */
11309 err = is_sockarray ? -ENOENT : -EINVAL;
11310 goto error;
11311 }
11312
11313 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
11314 struct sock *sk = reuse_kern->sk;
11315
11316 if (sk->sk_protocol != selected_sk->sk_protocol) {
11317 err = -EPROTOTYPE;
11318 } else if (sk->sk_family != selected_sk->sk_family) {
11319 err = -EAFNOSUPPORT;
11320 } else {
11321 /* Catch all. Likely bound to a different sockaddr. */
11322 err = -EBADFD;
11323 }
11324 goto error;
11325 }
11326
11327 reuse_kern->selected_sk = selected_sk;
11328
11329 return 0;
11330 error:
11331 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
11332 if (sk_is_refcounted(selected_sk))
11333 sock_put(selected_sk);
11334
11335 return err;
11336 }
11337
11338 static const struct bpf_func_proto sk_select_reuseport_proto = {
11339 .func = sk_select_reuseport,
11340 .gpl_only = false,
11341 .ret_type = RET_INTEGER,
11342 .arg1_type = ARG_PTR_TO_CTX,
11343 .arg2_type = ARG_CONST_MAP_PTR,
11344 .arg3_type = ARG_PTR_TO_MAP_KEY,
11345 .arg4_type = ARG_ANYTHING,
11346 };
11347
BPF_CALL_4(sk_reuseport_load_bytes,const struct sk_reuseport_kern *,reuse_kern,u32,offset,void *,to,u32,len)11348 BPF_CALL_4(sk_reuseport_load_bytes,
11349 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
11350 void *, to, u32, len)
11351 {
11352 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
11353 }
11354
11355 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
11356 .func = sk_reuseport_load_bytes,
11357 .gpl_only = false,
11358 .ret_type = RET_INTEGER,
11359 .arg1_type = ARG_PTR_TO_CTX,
11360 .arg2_type = ARG_ANYTHING,
11361 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
11362 .arg4_type = ARG_CONST_SIZE,
11363 };
11364
BPF_CALL_5(sk_reuseport_load_bytes_relative,const struct sk_reuseport_kern *,reuse_kern,u32,offset,void *,to,u32,len,u32,start_header)11365 BPF_CALL_5(sk_reuseport_load_bytes_relative,
11366 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
11367 void *, to, u32, len, u32, start_header)
11368 {
11369 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
11370 len, start_header);
11371 }
11372
11373 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
11374 .func = sk_reuseport_load_bytes_relative,
11375 .gpl_only = false,
11376 .ret_type = RET_INTEGER,
11377 .arg1_type = ARG_PTR_TO_CTX,
11378 .arg2_type = ARG_ANYTHING,
11379 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
11380 .arg4_type = ARG_CONST_SIZE,
11381 .arg5_type = ARG_ANYTHING,
11382 };
11383
11384 static const struct bpf_func_proto *
sk_reuseport_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)11385 sk_reuseport_func_proto(enum bpf_func_id func_id,
11386 const struct bpf_prog *prog)
11387 {
11388 switch (func_id) {
11389 case BPF_FUNC_sk_select_reuseport:
11390 return &sk_select_reuseport_proto;
11391 case BPF_FUNC_skb_load_bytes:
11392 return &sk_reuseport_load_bytes_proto;
11393 case BPF_FUNC_skb_load_bytes_relative:
11394 return &sk_reuseport_load_bytes_relative_proto;
11395 case BPF_FUNC_get_socket_cookie:
11396 return &bpf_get_socket_ptr_cookie_proto;
11397 case BPF_FUNC_ktime_get_coarse_ns:
11398 return &bpf_ktime_get_coarse_ns_proto;
11399 default:
11400 return bpf_base_func_proto(func_id, prog);
11401 }
11402 }
11403
11404 static bool
sk_reuseport_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)11405 sk_reuseport_is_valid_access(int off, int size,
11406 enum bpf_access_type type,
11407 const struct bpf_prog *prog,
11408 struct bpf_insn_access_aux *info)
11409 {
11410 const u32 size_default = sizeof(__u32);
11411
11412 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
11413 off % size || type != BPF_READ)
11414 return false;
11415
11416 switch (off) {
11417 case offsetof(struct sk_reuseport_md, data):
11418 info->reg_type = PTR_TO_PACKET;
11419 return size == sizeof(__u64);
11420
11421 case offsetof(struct sk_reuseport_md, data_end):
11422 info->reg_type = PTR_TO_PACKET_END;
11423 return size == sizeof(__u64);
11424
11425 case offsetof(struct sk_reuseport_md, hash):
11426 return size == size_default;
11427
11428 case offsetof(struct sk_reuseport_md, sk):
11429 info->reg_type = PTR_TO_SOCKET;
11430 return size == sizeof(__u64);
11431
11432 case offsetof(struct sk_reuseport_md, migrating_sk):
11433 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
11434 return size == sizeof(__u64);
11435
11436 /* Fields that allow narrowing */
11437 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
11438 if (size < sizeof_field(struct sk_buff, protocol))
11439 return false;
11440 fallthrough;
11441 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
11442 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
11443 case bpf_ctx_range(struct sk_reuseport_md, len):
11444 bpf_ctx_record_field_size(info, size_default);
11445 return bpf_ctx_narrow_access_ok(off, size, size_default);
11446
11447 default:
11448 return false;
11449 }
11450 }
11451
11452 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
11453 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
11454 si->dst_reg, si->src_reg, \
11455 bpf_target_off(struct sk_reuseport_kern, F, \
11456 sizeof_field(struct sk_reuseport_kern, F), \
11457 target_size)); \
11458 })
11459
11460 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
11461 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11462 struct sk_buff, \
11463 skb, \
11464 SKB_FIELD)
11465
11466 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
11467 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11468 struct sock, \
11469 sk, \
11470 SK_FIELD)
11471
sk_reuseport_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)11472 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
11473 const struct bpf_insn *si,
11474 struct bpf_insn *insn_buf,
11475 struct bpf_prog *prog,
11476 u32 *target_size)
11477 {
11478 struct bpf_insn *insn = insn_buf;
11479
11480 switch (si->off) {
11481 case offsetof(struct sk_reuseport_md, data):
11482 SK_REUSEPORT_LOAD_SKB_FIELD(data);
11483 break;
11484
11485 case offsetof(struct sk_reuseport_md, len):
11486 SK_REUSEPORT_LOAD_SKB_FIELD(len);
11487 break;
11488
11489 case offsetof(struct sk_reuseport_md, eth_protocol):
11490 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
11491 break;
11492
11493 case offsetof(struct sk_reuseport_md, ip_protocol):
11494 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
11495 break;
11496
11497 case offsetof(struct sk_reuseport_md, data_end):
11498 SK_REUSEPORT_LOAD_FIELD(data_end);
11499 break;
11500
11501 case offsetof(struct sk_reuseport_md, hash):
11502 SK_REUSEPORT_LOAD_FIELD(hash);
11503 break;
11504
11505 case offsetof(struct sk_reuseport_md, bind_inany):
11506 SK_REUSEPORT_LOAD_FIELD(bind_inany);
11507 break;
11508
11509 case offsetof(struct sk_reuseport_md, sk):
11510 SK_REUSEPORT_LOAD_FIELD(sk);
11511 break;
11512
11513 case offsetof(struct sk_reuseport_md, migrating_sk):
11514 SK_REUSEPORT_LOAD_FIELD(migrating_sk);
11515 break;
11516 }
11517
11518 return insn - insn_buf;
11519 }
11520
11521 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
11522 .get_func_proto = sk_reuseport_func_proto,
11523 .is_valid_access = sk_reuseport_is_valid_access,
11524 .convert_ctx_access = sk_reuseport_convert_ctx_access,
11525 };
11526
11527 const struct bpf_prog_ops sk_reuseport_prog_ops = {
11528 };
11529
11530 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled);
11531 EXPORT_SYMBOL(bpf_sk_lookup_enabled);
11532
BPF_CALL_3(bpf_sk_lookup_assign,struct bpf_sk_lookup_kern *,ctx,struct sock *,sk,u64,flags)11533 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx,
11534 struct sock *, sk, u64, flags)
11535 {
11536 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE |
11537 BPF_SK_LOOKUP_F_NO_REUSEPORT)))
11538 return -EINVAL;
11539 if (unlikely(sk && sk_is_refcounted(sk)))
11540 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */
11541 if (unlikely(sk && sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN))
11542 return -ESOCKTNOSUPPORT; /* only accept TCP socket in LISTEN */
11543 if (unlikely(sk && sk_is_udp(sk) && sk->sk_state != TCP_CLOSE))
11544 return -ESOCKTNOSUPPORT; /* only accept UDP socket in CLOSE */
11545
11546 /* Check if socket is suitable for packet L3/L4 protocol */
11547 if (sk && sk->sk_protocol != ctx->protocol)
11548 return -EPROTOTYPE;
11549 if (sk && sk->sk_family != ctx->family &&
11550 (sk->sk_family == AF_INET || ipv6_only_sock(sk)))
11551 return -EAFNOSUPPORT;
11552
11553 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE))
11554 return -EEXIST;
11555
11556 /* Select socket as lookup result */
11557 ctx->selected_sk = sk;
11558 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT;
11559 return 0;
11560 }
11561
11562 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = {
11563 .func = bpf_sk_lookup_assign,
11564 .gpl_only = false,
11565 .ret_type = RET_INTEGER,
11566 .arg1_type = ARG_PTR_TO_CTX,
11567 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL,
11568 .arg3_type = ARG_ANYTHING,
11569 };
11570
11571 static const struct bpf_func_proto *
sk_lookup_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)11572 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
11573 {
11574 switch (func_id) {
11575 case BPF_FUNC_perf_event_output:
11576 return &bpf_event_output_data_proto;
11577 case BPF_FUNC_sk_assign:
11578 return &bpf_sk_lookup_assign_proto;
11579 case BPF_FUNC_sk_release:
11580 return &bpf_sk_release_proto;
11581 default:
11582 return bpf_sk_base_func_proto(func_id, prog);
11583 }
11584 }
11585
sk_lookup_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)11586 static bool sk_lookup_is_valid_access(int off, int size,
11587 enum bpf_access_type type,
11588 const struct bpf_prog *prog,
11589 struct bpf_insn_access_aux *info)
11590 {
11591 if (off < 0 || off >= sizeof(struct bpf_sk_lookup))
11592 return false;
11593 if (off % size != 0)
11594 return false;
11595 if (type != BPF_READ)
11596 return false;
11597
11598 switch (off) {
11599 case offsetof(struct bpf_sk_lookup, sk):
11600 info->reg_type = PTR_TO_SOCKET_OR_NULL;
11601 return size == sizeof(__u64);
11602
11603 case bpf_ctx_range(struct bpf_sk_lookup, family):
11604 case bpf_ctx_range(struct bpf_sk_lookup, protocol):
11605 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4):
11606 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
11607 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
11608 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
11609 case bpf_ctx_range(struct bpf_sk_lookup, local_port):
11610 case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):
11611 bpf_ctx_record_field_size(info, sizeof(__u32));
11612 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
11613
11614 case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
11615 /* Allow 4-byte access to 2-byte field for backward compatibility */
11616 if (size == sizeof(__u32))
11617 return true;
11618 bpf_ctx_record_field_size(info, sizeof(__be16));
11619 return bpf_ctx_narrow_access_ok(off, size, sizeof(__be16));
11620
11621 case offsetofend(struct bpf_sk_lookup, remote_port) ...
11622 offsetof(struct bpf_sk_lookup, local_ip4) - 1:
11623 /* Allow access to zero padding for backward compatibility */
11624 bpf_ctx_record_field_size(info, sizeof(__u16));
11625 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u16));
11626
11627 default:
11628 return false;
11629 }
11630 }
11631
sk_lookup_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)11632 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
11633 const struct bpf_insn *si,
11634 struct bpf_insn *insn_buf,
11635 struct bpf_prog *prog,
11636 u32 *target_size)
11637 {
11638 struct bpf_insn *insn = insn_buf;
11639
11640 switch (si->off) {
11641 case offsetof(struct bpf_sk_lookup, sk):
11642 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11643 offsetof(struct bpf_sk_lookup_kern, selected_sk));
11644 break;
11645
11646 case offsetof(struct bpf_sk_lookup, family):
11647 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11648 bpf_target_off(struct bpf_sk_lookup_kern,
11649 family, 2, target_size));
11650 break;
11651
11652 case offsetof(struct bpf_sk_lookup, protocol):
11653 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11654 bpf_target_off(struct bpf_sk_lookup_kern,
11655 protocol, 2, target_size));
11656 break;
11657
11658 case offsetof(struct bpf_sk_lookup, remote_ip4):
11659 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11660 bpf_target_off(struct bpf_sk_lookup_kern,
11661 v4.saddr, 4, target_size));
11662 break;
11663
11664 case offsetof(struct bpf_sk_lookup, local_ip4):
11665 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11666 bpf_target_off(struct bpf_sk_lookup_kern,
11667 v4.daddr, 4, target_size));
11668 break;
11669
11670 case bpf_ctx_range_till(struct bpf_sk_lookup,
11671 remote_ip6[0], remote_ip6[3]): {
11672 #if IS_ENABLED(CONFIG_IPV6)
11673 int off = si->off;
11674
11675 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]);
11676 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11677 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11678 offsetof(struct bpf_sk_lookup_kern, v6.saddr));
11679 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11680 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11681 #else
11682 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11683 #endif
11684 break;
11685 }
11686 case bpf_ctx_range_till(struct bpf_sk_lookup,
11687 local_ip6[0], local_ip6[3]): {
11688 #if IS_ENABLED(CONFIG_IPV6)
11689 int off = si->off;
11690
11691 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]);
11692 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11693 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11694 offsetof(struct bpf_sk_lookup_kern, v6.daddr));
11695 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11696 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11697 #else
11698 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11699 #endif
11700 break;
11701 }
11702 case offsetof(struct bpf_sk_lookup, remote_port):
11703 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11704 bpf_target_off(struct bpf_sk_lookup_kern,
11705 sport, 2, target_size));
11706 break;
11707
11708 case offsetofend(struct bpf_sk_lookup, remote_port):
11709 *target_size = 2;
11710 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11711 break;
11712
11713 case offsetof(struct bpf_sk_lookup, local_port):
11714 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11715 bpf_target_off(struct bpf_sk_lookup_kern,
11716 dport, 2, target_size));
11717 break;
11718
11719 case offsetof(struct bpf_sk_lookup, ingress_ifindex):
11720 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11721 bpf_target_off(struct bpf_sk_lookup_kern,
11722 ingress_ifindex, 4, target_size));
11723 break;
11724 }
11725
11726 return insn - insn_buf;
11727 }
11728
11729 const struct bpf_prog_ops sk_lookup_prog_ops = {
11730 .test_run = bpf_prog_test_run_sk_lookup,
11731 };
11732
11733 const struct bpf_verifier_ops sk_lookup_verifier_ops = {
11734 .get_func_proto = sk_lookup_func_proto,
11735 .is_valid_access = sk_lookup_is_valid_access,
11736 .convert_ctx_access = sk_lookup_convert_ctx_access,
11737 };
11738
11739 #endif /* CONFIG_INET */
11740
DEFINE_BPF_DISPATCHER(xdp)11741 DEFINE_BPF_DISPATCHER(xdp)
11742
11743 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
11744 {
11745 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
11746 }
11747
BTF_ID_LIST_GLOBAL(btf_sock_ids,MAX_BTF_SOCK_TYPE)11748 BTF_ID_LIST_GLOBAL(btf_sock_ids, MAX_BTF_SOCK_TYPE)
11749 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
11750 BTF_SOCK_TYPE_xxx
11751 #undef BTF_SOCK_TYPE
11752
11753 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
11754 {
11755 /* tcp6_sock type is not generated in dwarf and hence btf,
11756 * trigger an explicit type generation here.
11757 */
11758 BTF_TYPE_EMIT(struct tcp6_sock);
11759 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
11760 sk->sk_family == AF_INET6)
11761 return (unsigned long)sk;
11762
11763 return (unsigned long)NULL;
11764 }
11765
11766 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
11767 .func = bpf_skc_to_tcp6_sock,
11768 .gpl_only = false,
11769 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11770 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11771 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
11772 };
11773
BPF_CALL_1(bpf_skc_to_tcp_sock,struct sock *,sk)11774 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
11775 {
11776 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
11777 return (unsigned long)sk;
11778
11779 return (unsigned long)NULL;
11780 }
11781
11782 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
11783 .func = bpf_skc_to_tcp_sock,
11784 .gpl_only = false,
11785 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11786 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11787 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
11788 };
11789
BPF_CALL_1(bpf_skc_to_tcp_timewait_sock,struct sock *,sk)11790 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
11791 {
11792 /* BTF types for tcp_timewait_sock and inet_timewait_sock are not
11793 * generated if CONFIG_INET=n. Trigger an explicit generation here.
11794 */
11795 BTF_TYPE_EMIT(struct inet_timewait_sock);
11796 BTF_TYPE_EMIT(struct tcp_timewait_sock);
11797
11798 #ifdef CONFIG_INET
11799 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
11800 return (unsigned long)sk;
11801 #endif
11802
11803 #if IS_BUILTIN(CONFIG_IPV6)
11804 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
11805 return (unsigned long)sk;
11806 #endif
11807
11808 return (unsigned long)NULL;
11809 }
11810
11811 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
11812 .func = bpf_skc_to_tcp_timewait_sock,
11813 .gpl_only = false,
11814 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11815 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11816 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
11817 };
11818
BPF_CALL_1(bpf_skc_to_tcp_request_sock,struct sock *,sk)11819 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
11820 {
11821 #ifdef CONFIG_INET
11822 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11823 return (unsigned long)sk;
11824 #endif
11825
11826 #if IS_BUILTIN(CONFIG_IPV6)
11827 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11828 return (unsigned long)sk;
11829 #endif
11830
11831 return (unsigned long)NULL;
11832 }
11833
11834 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
11835 .func = bpf_skc_to_tcp_request_sock,
11836 .gpl_only = false,
11837 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11838 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11839 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
11840 };
11841
BPF_CALL_1(bpf_skc_to_udp6_sock,struct sock *,sk)11842 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
11843 {
11844 /* udp6_sock type is not generated in dwarf and hence btf,
11845 * trigger an explicit type generation here.
11846 */
11847 BTF_TYPE_EMIT(struct udp6_sock);
11848 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
11849 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
11850 return (unsigned long)sk;
11851
11852 return (unsigned long)NULL;
11853 }
11854
11855 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
11856 .func = bpf_skc_to_udp6_sock,
11857 .gpl_only = false,
11858 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11859 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11860 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
11861 };
11862
BPF_CALL_1(bpf_skc_to_unix_sock,struct sock *,sk)11863 BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
11864 {
11865 /* unix_sock type is not generated in dwarf and hence btf,
11866 * trigger an explicit type generation here.
11867 */
11868 BTF_TYPE_EMIT(struct unix_sock);
11869 if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX)
11870 return (unsigned long)sk;
11871
11872 return (unsigned long)NULL;
11873 }
11874
11875 const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
11876 .func = bpf_skc_to_unix_sock,
11877 .gpl_only = false,
11878 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11879 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11880 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
11881 };
11882
BPF_CALL_1(bpf_skc_to_mptcp_sock,struct sock *,sk)11883 BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
11884 {
11885 BTF_TYPE_EMIT(struct mptcp_sock);
11886 return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
11887 }
11888
11889 const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
11890 .func = bpf_skc_to_mptcp_sock,
11891 .gpl_only = false,
11892 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11893 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
11894 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_MPTCP],
11895 };
11896
BPF_CALL_1(bpf_sock_from_file,struct file *,file)11897 BPF_CALL_1(bpf_sock_from_file, struct file *, file)
11898 {
11899 return (unsigned long)sock_from_file(file);
11900 }
11901
11902 BTF_ID_LIST(bpf_sock_from_file_btf_ids)
11903 BTF_ID(struct, socket)
11904 BTF_ID(struct, file)
11905
11906 const struct bpf_func_proto bpf_sock_from_file_proto = {
11907 .func = bpf_sock_from_file,
11908 .gpl_only = false,
11909 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11910 .ret_btf_id = &bpf_sock_from_file_btf_ids[0],
11911 .arg1_type = ARG_PTR_TO_BTF_ID,
11912 .arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
11913 };
11914
11915 static const struct bpf_func_proto *
bpf_sk_base_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)11916 bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
11917 {
11918 const struct bpf_func_proto *func;
11919
11920 switch (func_id) {
11921 case BPF_FUNC_skc_to_tcp6_sock:
11922 func = &bpf_skc_to_tcp6_sock_proto;
11923 break;
11924 case BPF_FUNC_skc_to_tcp_sock:
11925 func = &bpf_skc_to_tcp_sock_proto;
11926 break;
11927 case BPF_FUNC_skc_to_tcp_timewait_sock:
11928 func = &bpf_skc_to_tcp_timewait_sock_proto;
11929 break;
11930 case BPF_FUNC_skc_to_tcp_request_sock:
11931 func = &bpf_skc_to_tcp_request_sock_proto;
11932 break;
11933 case BPF_FUNC_skc_to_udp6_sock:
11934 func = &bpf_skc_to_udp6_sock_proto;
11935 break;
11936 case BPF_FUNC_skc_to_unix_sock:
11937 func = &bpf_skc_to_unix_sock_proto;
11938 break;
11939 case BPF_FUNC_skc_to_mptcp_sock:
11940 func = &bpf_skc_to_mptcp_sock_proto;
11941 break;
11942 case BPF_FUNC_ktime_get_coarse_ns:
11943 return &bpf_ktime_get_coarse_ns_proto;
11944 default:
11945 return bpf_base_func_proto(func_id, prog);
11946 }
11947
11948 if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
11949 return NULL;
11950
11951 return func;
11952 }
11953
11954 __bpf_kfunc_start_defs();
bpf_dynptr_from_skb(struct __sk_buff * s,u64 flags,struct bpf_dynptr * ptr__uninit)11955 __bpf_kfunc int bpf_dynptr_from_skb(struct __sk_buff *s, u64 flags,
11956 struct bpf_dynptr *ptr__uninit)
11957 {
11958 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)ptr__uninit;
11959 struct sk_buff *skb = (struct sk_buff *)s;
11960
11961 if (flags) {
11962 bpf_dynptr_set_null(ptr);
11963 return -EINVAL;
11964 }
11965
11966 bpf_dynptr_init(ptr, skb, BPF_DYNPTR_TYPE_SKB, 0, skb->len);
11967
11968 return 0;
11969 }
11970
bpf_dynptr_from_xdp(struct xdp_md * x,u64 flags,struct bpf_dynptr * ptr__uninit)11971 __bpf_kfunc int bpf_dynptr_from_xdp(struct xdp_md *x, u64 flags,
11972 struct bpf_dynptr *ptr__uninit)
11973 {
11974 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)ptr__uninit;
11975 struct xdp_buff *xdp = (struct xdp_buff *)x;
11976
11977 if (flags) {
11978 bpf_dynptr_set_null(ptr);
11979 return -EINVAL;
11980 }
11981
11982 bpf_dynptr_init(ptr, xdp, BPF_DYNPTR_TYPE_XDP, 0, xdp_get_buff_len(xdp));
11983
11984 return 0;
11985 }
11986
bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern * sa_kern,const u8 * sun_path,u32 sun_path__sz)11987 __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,
11988 const u8 *sun_path, u32 sun_path__sz)
11989 {
11990 struct sockaddr_un *un;
11991
11992 if (sa_kern->sk->sk_family != AF_UNIX)
11993 return -EINVAL;
11994
11995 /* We do not allow changing the address to unnamed or larger than the
11996 * maximum allowed address size for a unix sockaddr.
11997 */
11998 if (sun_path__sz == 0 || sun_path__sz > UNIX_PATH_MAX)
11999 return -EINVAL;
12000
12001 un = (struct sockaddr_un *)sa_kern->uaddr;
12002 memcpy(un->sun_path, sun_path, sun_path__sz);
12003 sa_kern->uaddrlen = offsetof(struct sockaddr_un, sun_path) + sun_path__sz;
12004
12005 return 0;
12006 }
12007
bpf_sk_assign_tcp_reqsk(struct __sk_buff * s,struct sock * sk,struct bpf_tcp_req_attrs * attrs,int attrs__sz)12008 __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk,
12009 struct bpf_tcp_req_attrs *attrs, int attrs__sz)
12010 {
12011 #if IS_ENABLED(CONFIG_SYN_COOKIES)
12012 struct sk_buff *skb = (struct sk_buff *)s;
12013 const struct request_sock_ops *ops;
12014 struct inet_request_sock *ireq;
12015 struct tcp_request_sock *treq;
12016 struct request_sock *req;
12017 struct net *net;
12018 __u16 min_mss;
12019 u32 tsoff = 0;
12020
12021 if (attrs__sz != sizeof(*attrs) ||
12022 attrs->reserved[0] || attrs->reserved[1] || attrs->reserved[2])
12023 return -EINVAL;
12024
12025 if (!skb_at_tc_ingress(skb))
12026 return -EINVAL;
12027
12028 net = dev_net(skb->dev);
12029 if (net != sock_net(sk))
12030 return -ENETUNREACH;
12031
12032 switch (skb->protocol) {
12033 case htons(ETH_P_IP):
12034 ops = &tcp_request_sock_ops;
12035 min_mss = 536;
12036 break;
12037 #if IS_BUILTIN(CONFIG_IPV6)
12038 case htons(ETH_P_IPV6):
12039 ops = &tcp6_request_sock_ops;
12040 min_mss = IPV6_MIN_MTU - 60;
12041 break;
12042 #endif
12043 default:
12044 return -EINVAL;
12045 }
12046
12047 if (sk->sk_type != SOCK_STREAM || sk->sk_state != TCP_LISTEN ||
12048 sk_is_mptcp(sk))
12049 return -EINVAL;
12050
12051 if (attrs->mss < min_mss)
12052 return -EINVAL;
12053
12054 if (attrs->wscale_ok) {
12055 if (!READ_ONCE(net->ipv4.sysctl_tcp_window_scaling))
12056 return -EINVAL;
12057
12058 if (attrs->snd_wscale > TCP_MAX_WSCALE ||
12059 attrs->rcv_wscale > TCP_MAX_WSCALE)
12060 return -EINVAL;
12061 }
12062
12063 if (attrs->sack_ok && !READ_ONCE(net->ipv4.sysctl_tcp_sack))
12064 return -EINVAL;
12065
12066 if (attrs->tstamp_ok) {
12067 if (!READ_ONCE(net->ipv4.sysctl_tcp_timestamps))
12068 return -EINVAL;
12069
12070 tsoff = attrs->rcv_tsecr - tcp_ns_to_ts(attrs->usec_ts_ok, tcp_clock_ns());
12071 }
12072
12073 req = inet_reqsk_alloc(ops, sk, false);
12074 if (!req)
12075 return -ENOMEM;
12076
12077 ireq = inet_rsk(req);
12078 treq = tcp_rsk(req);
12079
12080 req->rsk_listener = sk;
12081 req->syncookie = 1;
12082 req->mss = attrs->mss;
12083 req->ts_recent = attrs->rcv_tsval;
12084
12085 ireq->snd_wscale = attrs->snd_wscale;
12086 ireq->rcv_wscale = attrs->rcv_wscale;
12087 ireq->tstamp_ok = !!attrs->tstamp_ok;
12088 ireq->sack_ok = !!attrs->sack_ok;
12089 ireq->wscale_ok = !!attrs->wscale_ok;
12090 ireq->ecn_ok = !!attrs->ecn_ok;
12091
12092 treq->req_usec_ts = !!attrs->usec_ts_ok;
12093 treq->ts_off = tsoff;
12094
12095 skb_orphan(skb);
12096 skb->sk = req_to_sk(req);
12097 skb->destructor = sock_pfree;
12098
12099 return 0;
12100 #else
12101 return -EOPNOTSUPP;
12102 #endif
12103 }
12104
12105 __bpf_kfunc_end_defs();
12106
bpf_dynptr_from_skb_rdonly(struct __sk_buff * skb,u64 flags,struct bpf_dynptr * ptr__uninit)12107 int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,
12108 struct bpf_dynptr *ptr__uninit)
12109 {
12110 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)ptr__uninit;
12111 int err;
12112
12113 err = bpf_dynptr_from_skb(skb, flags, ptr__uninit);
12114 if (err)
12115 return err;
12116
12117 bpf_dynptr_set_rdonly(ptr);
12118
12119 return 0;
12120 }
12121
12122 BTF_KFUNCS_START(bpf_kfunc_check_set_skb)
12123 BTF_ID_FLAGS(func, bpf_dynptr_from_skb, KF_TRUSTED_ARGS)
12124 BTF_KFUNCS_END(bpf_kfunc_check_set_skb)
12125
12126 BTF_KFUNCS_START(bpf_kfunc_check_set_xdp)
12127 BTF_ID_FLAGS(func, bpf_dynptr_from_xdp)
12128 BTF_KFUNCS_END(bpf_kfunc_check_set_xdp)
12129
12130 BTF_KFUNCS_START(bpf_kfunc_check_set_sock_addr)
12131 BTF_ID_FLAGS(func, bpf_sock_addr_set_sun_path)
12132 BTF_KFUNCS_END(bpf_kfunc_check_set_sock_addr)
12133
12134 BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk)
12135 BTF_ID_FLAGS(func, bpf_sk_assign_tcp_reqsk, KF_TRUSTED_ARGS)
12136 BTF_KFUNCS_END(bpf_kfunc_check_set_tcp_reqsk)
12137
12138 static const struct btf_kfunc_id_set bpf_kfunc_set_skb = {
12139 .owner = THIS_MODULE,
12140 .set = &bpf_kfunc_check_set_skb,
12141 };
12142
12143 static const struct btf_kfunc_id_set bpf_kfunc_set_xdp = {
12144 .owner = THIS_MODULE,
12145 .set = &bpf_kfunc_check_set_xdp,
12146 };
12147
12148 static const struct btf_kfunc_id_set bpf_kfunc_set_sock_addr = {
12149 .owner = THIS_MODULE,
12150 .set = &bpf_kfunc_check_set_sock_addr,
12151 };
12152
12153 static const struct btf_kfunc_id_set bpf_kfunc_set_tcp_reqsk = {
12154 .owner = THIS_MODULE,
12155 .set = &bpf_kfunc_check_set_tcp_reqsk,
12156 };
12157
bpf_kfunc_init(void)12158 static int __init bpf_kfunc_init(void)
12159 {
12160 int ret;
12161
12162 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_skb);
12163 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, &bpf_kfunc_set_skb);
12164 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SK_SKB, &bpf_kfunc_set_skb);
12165 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SOCKET_FILTER, &bpf_kfunc_set_skb);
12166 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &bpf_kfunc_set_skb);
12167 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_OUT, &bpf_kfunc_set_skb);
12168 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_IN, &bpf_kfunc_set_skb);
12169 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT, &bpf_kfunc_set_skb);
12170 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, &bpf_kfunc_set_skb);
12171 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb);
12172 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_kfunc_set_skb);
12173 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &bpf_kfunc_set_xdp);
12174 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
12175 &bpf_kfunc_set_sock_addr);
12176 return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_tcp_reqsk);
12177 }
12178 late_initcall(bpf_kfunc_init);
12179
12180 __bpf_kfunc_start_defs();
12181
12182 /* bpf_sock_destroy: Destroy the given socket with ECONNABORTED error code.
12183 *
12184 * The function expects a non-NULL pointer to a socket, and invokes the
12185 * protocol specific socket destroy handlers.
12186 *
12187 * The helper can only be called from BPF contexts that have acquired the socket
12188 * locks.
12189 *
12190 * Parameters:
12191 * @sock: Pointer to socket to be destroyed
12192 *
12193 * Return:
12194 * On error, may return EPROTONOSUPPORT, EINVAL.
12195 * EPROTONOSUPPORT if protocol specific destroy handler is not supported.
12196 * 0 otherwise
12197 */
bpf_sock_destroy(struct sock_common * sock)12198 __bpf_kfunc int bpf_sock_destroy(struct sock_common *sock)
12199 {
12200 struct sock *sk = (struct sock *)sock;
12201
12202 /* The locking semantics that allow for synchronous execution of the
12203 * destroy handlers are only supported for TCP and UDP.
12204 * Supporting protocols will need to acquire sock lock in the BPF context
12205 * prior to invoking this kfunc.
12206 */
12207 if (!sk->sk_prot->diag_destroy || (sk->sk_protocol != IPPROTO_TCP &&
12208 sk->sk_protocol != IPPROTO_UDP))
12209 return -EOPNOTSUPP;
12210
12211 return sk->sk_prot->diag_destroy(sk, ECONNABORTED);
12212 }
12213
12214 __bpf_kfunc_end_defs();
12215
12216 BTF_KFUNCS_START(bpf_sk_iter_kfunc_ids)
BTF_ID_FLAGS(func,bpf_sock_destroy,KF_TRUSTED_ARGS)12217 BTF_ID_FLAGS(func, bpf_sock_destroy, KF_TRUSTED_ARGS)
12218 BTF_KFUNCS_END(bpf_sk_iter_kfunc_ids)
12219
12220 static int tracing_iter_filter(const struct bpf_prog *prog, u32 kfunc_id)
12221 {
12222 if (btf_id_set8_contains(&bpf_sk_iter_kfunc_ids, kfunc_id) &&
12223 prog->expected_attach_type != BPF_TRACE_ITER)
12224 return -EACCES;
12225 return 0;
12226 }
12227
12228 static const struct btf_kfunc_id_set bpf_sk_iter_kfunc_set = {
12229 .owner = THIS_MODULE,
12230 .set = &bpf_sk_iter_kfunc_ids,
12231 .filter = tracing_iter_filter,
12232 };
12233
init_subsystem(void)12234 static int init_subsystem(void)
12235 {
12236 return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_sk_iter_kfunc_set);
12237 }
12238 late_initcall(init_subsystem);
12239