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