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