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