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