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