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