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