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,const struct neighbour * neigh,const struct net_device * dev,u32 mtu)5395 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
5396 const struct neighbour *neigh,
5397 const struct net_device *dev, u32 mtu)
5398 {
5399 memcpy(params->dmac, neigh->ha, ETH_ALEN);
5400 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
5401 params->h_vlan_TCI = 0;
5402 params->h_vlan_proto = 0;
5403 if (mtu)
5404 params->mtu_result = mtu; /* union with tot_len */
5405
5406 return 0;
5407 }
5408 #endif
5409
5410 #if IS_ENABLED(CONFIG_INET)
bpf_ipv4_fib_lookup(struct net * net,struct bpf_fib_lookup * params,u32 flags,bool check_mtu)5411 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5412 u32 flags, bool check_mtu)
5413 {
5414 struct fib_nh_common *nhc;
5415 struct in_device *in_dev;
5416 struct neighbour *neigh;
5417 struct net_device *dev;
5418 struct fib_result res;
5419 struct flowi4 fl4;
5420 u32 mtu = 0;
5421 int err;
5422
5423 dev = dev_get_by_index_rcu(net, params->ifindex);
5424 if (unlikely(!dev))
5425 return -ENODEV;
5426
5427 /* verify forwarding is enabled on this interface */
5428 in_dev = __in_dev_get_rcu(dev);
5429 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
5430 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5431
5432 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5433 fl4.flowi4_iif = 1;
5434 fl4.flowi4_oif = params->ifindex;
5435 } else {
5436 fl4.flowi4_iif = params->ifindex;
5437 fl4.flowi4_oif = 0;
5438 }
5439 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
5440 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
5441 fl4.flowi4_flags = 0;
5442
5443 fl4.flowi4_proto = params->l4_protocol;
5444 fl4.daddr = params->ipv4_dst;
5445 fl4.saddr = params->ipv4_src;
5446 fl4.fl4_sport = params->sport;
5447 fl4.fl4_dport = params->dport;
5448 fl4.flowi4_multipath_hash = 0;
5449
5450 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5451 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5452 struct fib_table *tb;
5453
5454 tb = fib_get_table(net, tbid);
5455 if (unlikely(!tb))
5456 return BPF_FIB_LKUP_RET_NOT_FWDED;
5457
5458 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
5459 } else {
5460 fl4.flowi4_mark = 0;
5461 fl4.flowi4_secid = 0;
5462 fl4.flowi4_tun_key.tun_id = 0;
5463 fl4.flowi4_uid = sock_net_uid(net, NULL);
5464
5465 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
5466 }
5467
5468 if (err) {
5469 /* map fib lookup errors to RTN_ type */
5470 if (err == -EINVAL)
5471 return BPF_FIB_LKUP_RET_BLACKHOLE;
5472 if (err == -EHOSTUNREACH)
5473 return BPF_FIB_LKUP_RET_UNREACHABLE;
5474 if (err == -EACCES)
5475 return BPF_FIB_LKUP_RET_PROHIBIT;
5476
5477 return BPF_FIB_LKUP_RET_NOT_FWDED;
5478 }
5479
5480 if (res.type != RTN_UNICAST)
5481 return BPF_FIB_LKUP_RET_NOT_FWDED;
5482
5483 if (fib_info_num_path(res.fi) > 1)
5484 fib_select_path(net, &res, &fl4, NULL);
5485
5486 if (check_mtu) {
5487 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
5488 if (params->tot_len > mtu) {
5489 params->mtu_result = mtu; /* union with tot_len */
5490 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5491 }
5492 }
5493
5494 nhc = res.nhc;
5495
5496 /* do not handle lwt encaps right now */
5497 if (nhc->nhc_lwtstate)
5498 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5499
5500 dev = nhc->nhc_dev;
5501
5502 params->rt_metric = res.fi->fib_priority;
5503 params->ifindex = dev->ifindex;
5504
5505 /* xdp and cls_bpf programs are run in RCU-bh so
5506 * rcu_read_lock_bh is not needed here
5507 */
5508 if (likely(nhc->nhc_gw_family != AF_INET6)) {
5509 if (nhc->nhc_gw_family)
5510 params->ipv4_dst = nhc->nhc_gw.ipv4;
5511
5512 neigh = __ipv4_neigh_lookup_noref(dev,
5513 (__force u32)params->ipv4_dst);
5514 } else {
5515 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
5516
5517 params->family = AF_INET6;
5518 *dst = nhc->nhc_gw.ipv6;
5519 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5520 }
5521
5522 if (!neigh || !(neigh->nud_state & NUD_VALID))
5523 return BPF_FIB_LKUP_RET_NO_NEIGH;
5524
5525 return bpf_fib_set_fwd_params(params, neigh, dev, mtu);
5526 }
5527 #endif
5528
5529 #if IS_ENABLED(CONFIG_IPV6)
bpf_ipv6_fib_lookup(struct net * net,struct bpf_fib_lookup * params,u32 flags,bool check_mtu)5530 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5531 u32 flags, bool check_mtu)
5532 {
5533 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
5534 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
5535 struct fib6_result res = {};
5536 struct neighbour *neigh;
5537 struct net_device *dev;
5538 struct inet6_dev *idev;
5539 struct flowi6 fl6;
5540 int strict = 0;
5541 int oif, err;
5542 u32 mtu = 0;
5543
5544 /* link local addresses are never forwarded */
5545 if (rt6_need_strict(dst) || rt6_need_strict(src))
5546 return BPF_FIB_LKUP_RET_NOT_FWDED;
5547
5548 dev = dev_get_by_index_rcu(net, params->ifindex);
5549 if (unlikely(!dev))
5550 return -ENODEV;
5551
5552 idev = __in6_dev_get_safely(dev);
5553 if (unlikely(!idev || !idev->cnf.forwarding))
5554 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5555
5556 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5557 fl6.flowi6_iif = 1;
5558 oif = fl6.flowi6_oif = params->ifindex;
5559 } else {
5560 oif = fl6.flowi6_iif = params->ifindex;
5561 fl6.flowi6_oif = 0;
5562 strict = RT6_LOOKUP_F_HAS_SADDR;
5563 }
5564 fl6.flowlabel = params->flowinfo;
5565 fl6.flowi6_scope = 0;
5566 fl6.flowi6_flags = 0;
5567 fl6.mp_hash = 0;
5568
5569 fl6.flowi6_proto = params->l4_protocol;
5570 fl6.daddr = *dst;
5571 fl6.saddr = *src;
5572 fl6.fl6_sport = params->sport;
5573 fl6.fl6_dport = params->dport;
5574
5575 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5576 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5577 struct fib6_table *tb;
5578
5579 tb = ipv6_stub->fib6_get_table(net, tbid);
5580 if (unlikely(!tb))
5581 return BPF_FIB_LKUP_RET_NOT_FWDED;
5582
5583 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
5584 strict);
5585 } else {
5586 fl6.flowi6_mark = 0;
5587 fl6.flowi6_secid = 0;
5588 fl6.flowi6_tun_key.tun_id = 0;
5589 fl6.flowi6_uid = sock_net_uid(net, NULL);
5590
5591 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
5592 }
5593
5594 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
5595 res.f6i == net->ipv6.fib6_null_entry))
5596 return BPF_FIB_LKUP_RET_NOT_FWDED;
5597
5598 switch (res.fib6_type) {
5599 /* only unicast is forwarded */
5600 case RTN_UNICAST:
5601 break;
5602 case RTN_BLACKHOLE:
5603 return BPF_FIB_LKUP_RET_BLACKHOLE;
5604 case RTN_UNREACHABLE:
5605 return BPF_FIB_LKUP_RET_UNREACHABLE;
5606 case RTN_PROHIBIT:
5607 return BPF_FIB_LKUP_RET_PROHIBIT;
5608 default:
5609 return BPF_FIB_LKUP_RET_NOT_FWDED;
5610 }
5611
5612 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
5613 fl6.flowi6_oif != 0, NULL, strict);
5614
5615 if (check_mtu) {
5616 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
5617 if (params->tot_len > mtu) {
5618 params->mtu_result = mtu; /* union with tot_len */
5619 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5620 }
5621 }
5622
5623 if (res.nh->fib_nh_lws)
5624 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5625
5626 if (res.nh->fib_nh_gw_family)
5627 *dst = res.nh->fib_nh_gw6;
5628
5629 dev = res.nh->fib_nh_dev;
5630 params->rt_metric = res.f6i->fib6_metric;
5631 params->ifindex = dev->ifindex;
5632
5633 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
5634 * not needed here.
5635 */
5636 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5637 if (!neigh || !(neigh->nud_state & NUD_VALID))
5638 return BPF_FIB_LKUP_RET_NO_NEIGH;
5639
5640 return bpf_fib_set_fwd_params(params, neigh, dev, mtu);
5641 }
5642 #endif
5643
BPF_CALL_4(bpf_xdp_fib_lookup,struct xdp_buff *,ctx,struct bpf_fib_lookup *,params,int,plen,u32,flags)5644 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
5645 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5646 {
5647 if (plen < sizeof(*params))
5648 return -EINVAL;
5649
5650 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5651 return -EINVAL;
5652
5653 switch (params->family) {
5654 #if IS_ENABLED(CONFIG_INET)
5655 case AF_INET:
5656 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
5657 flags, true);
5658 #endif
5659 #if IS_ENABLED(CONFIG_IPV6)
5660 case AF_INET6:
5661 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
5662 flags, true);
5663 #endif
5664 }
5665 return -EAFNOSUPPORT;
5666 }
5667
5668 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
5669 .func = bpf_xdp_fib_lookup,
5670 .gpl_only = true,
5671 .ret_type = RET_INTEGER,
5672 .arg1_type = ARG_PTR_TO_CTX,
5673 .arg2_type = ARG_PTR_TO_MEM,
5674 .arg3_type = ARG_CONST_SIZE,
5675 .arg4_type = ARG_ANYTHING,
5676 };
5677
BPF_CALL_4(bpf_skb_fib_lookup,struct sk_buff *,skb,struct bpf_fib_lookup *,params,int,plen,u32,flags)5678 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
5679 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5680 {
5681 struct net *net = dev_net(skb->dev);
5682 int rc = -EAFNOSUPPORT;
5683 bool check_mtu = false;
5684
5685 if (plen < sizeof(*params))
5686 return -EINVAL;
5687
5688 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5689 return -EINVAL;
5690
5691 if (params->tot_len)
5692 check_mtu = true;
5693
5694 switch (params->family) {
5695 #if IS_ENABLED(CONFIG_INET)
5696 case AF_INET:
5697 rc = bpf_ipv4_fib_lookup(net, params, flags, check_mtu);
5698 break;
5699 #endif
5700 #if IS_ENABLED(CONFIG_IPV6)
5701 case AF_INET6:
5702 rc = bpf_ipv6_fib_lookup(net, params, flags, check_mtu);
5703 break;
5704 #endif
5705 }
5706
5707 if (rc == BPF_FIB_LKUP_RET_SUCCESS && !check_mtu) {
5708 struct net_device *dev;
5709
5710 /* When tot_len isn't provided by user, check skb
5711 * against MTU of FIB lookup resulting net_device
5712 */
5713 dev = dev_get_by_index_rcu(net, params->ifindex);
5714 if (!is_skb_forwardable(dev, skb))
5715 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
5716
5717 params->mtu_result = dev->mtu; /* union with tot_len */
5718 }
5719
5720 return rc;
5721 }
5722
5723 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
5724 .func = bpf_skb_fib_lookup,
5725 .gpl_only = true,
5726 .ret_type = RET_INTEGER,
5727 .arg1_type = ARG_PTR_TO_CTX,
5728 .arg2_type = ARG_PTR_TO_MEM,
5729 .arg3_type = ARG_CONST_SIZE,
5730 .arg4_type = ARG_ANYTHING,
5731 };
5732
__dev_via_ifindex(struct net_device * dev_curr,u32 ifindex)5733 static struct net_device *__dev_via_ifindex(struct net_device *dev_curr,
5734 u32 ifindex)
5735 {
5736 struct net *netns = dev_net(dev_curr);
5737
5738 /* Non-redirect use-cases can use ifindex=0 and save ifindex lookup */
5739 if (ifindex == 0)
5740 return dev_curr;
5741
5742 return dev_get_by_index_rcu(netns, ifindex);
5743 }
5744
BPF_CALL_5(bpf_skb_check_mtu,struct sk_buff *,skb,u32,ifindex,u32 *,mtu_len,s32,len_diff,u64,flags)5745 BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
5746 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
5747 {
5748 int ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
5749 struct net_device *dev = skb->dev;
5750 int skb_len, dev_len;
5751 int mtu;
5752
5753 if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
5754 return -EINVAL;
5755
5756 if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
5757 return -EINVAL;
5758
5759 dev = __dev_via_ifindex(dev, ifindex);
5760 if (unlikely(!dev))
5761 return -ENODEV;
5762
5763 mtu = READ_ONCE(dev->mtu);
5764
5765 dev_len = mtu + dev->hard_header_len;
5766
5767 /* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
5768 skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len;
5769
5770 skb_len += len_diff; /* minus result pass check */
5771 if (skb_len <= dev_len) {
5772 ret = BPF_MTU_CHK_RET_SUCCESS;
5773 goto out;
5774 }
5775 /* At this point, skb->len exceed MTU, but as it include length of all
5776 * segments, it can still be below MTU. The SKB can possibly get
5777 * re-segmented in transmit path (see validate_xmit_skb). Thus, user
5778 * must choose if segs are to be MTU checked.
5779 */
5780 if (skb_is_gso(skb)) {
5781 ret = BPF_MTU_CHK_RET_SUCCESS;
5782
5783 if (flags & BPF_MTU_CHK_SEGS &&
5784 !skb_gso_validate_network_len(skb, mtu))
5785 ret = BPF_MTU_CHK_RET_SEGS_TOOBIG;
5786 }
5787 out:
5788 /* BPF verifier guarantees valid pointer */
5789 *mtu_len = mtu;
5790
5791 return ret;
5792 }
5793
BPF_CALL_5(bpf_xdp_check_mtu,struct xdp_buff *,xdp,u32,ifindex,u32 *,mtu_len,s32,len_diff,u64,flags)5794 BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
5795 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
5796 {
5797 struct net_device *dev = xdp->rxq->dev;
5798 int xdp_len = xdp->data_end - xdp->data;
5799 int ret = BPF_MTU_CHK_RET_SUCCESS;
5800 int mtu, dev_len;
5801
5802 /* XDP variant doesn't support multi-buffer segment check (yet) */
5803 if (unlikely(flags))
5804 return -EINVAL;
5805
5806 dev = __dev_via_ifindex(dev, ifindex);
5807 if (unlikely(!dev))
5808 return -ENODEV;
5809
5810 mtu = READ_ONCE(dev->mtu);
5811
5812 /* Add L2-header as dev MTU is L3 size */
5813 dev_len = mtu + dev->hard_header_len;
5814
5815 /* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
5816 if (*mtu_len)
5817 xdp_len = *mtu_len + dev->hard_header_len;
5818
5819 xdp_len += len_diff; /* minus result pass check */
5820 if (xdp_len > dev_len)
5821 ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
5822
5823 /* BPF verifier guarantees valid pointer */
5824 *mtu_len = mtu;
5825
5826 return ret;
5827 }
5828
5829 static const struct bpf_func_proto bpf_skb_check_mtu_proto = {
5830 .func = bpf_skb_check_mtu,
5831 .gpl_only = true,
5832 .ret_type = RET_INTEGER,
5833 .arg1_type = ARG_PTR_TO_CTX,
5834 .arg2_type = ARG_ANYTHING,
5835 .arg3_type = ARG_PTR_TO_INT,
5836 .arg4_type = ARG_ANYTHING,
5837 .arg5_type = ARG_ANYTHING,
5838 };
5839
5840 static const struct bpf_func_proto bpf_xdp_check_mtu_proto = {
5841 .func = bpf_xdp_check_mtu,
5842 .gpl_only = true,
5843 .ret_type = RET_INTEGER,
5844 .arg1_type = ARG_PTR_TO_CTX,
5845 .arg2_type = ARG_ANYTHING,
5846 .arg3_type = ARG_PTR_TO_INT,
5847 .arg4_type = ARG_ANYTHING,
5848 .arg5_type = ARG_ANYTHING,
5849 };
5850
5851 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
bpf_push_seg6_encap(struct sk_buff * skb,u32 type,void * hdr,u32 len)5852 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
5853 {
5854 int err;
5855 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
5856
5857 if (!seg6_validate_srh(srh, len, false))
5858 return -EINVAL;
5859
5860 switch (type) {
5861 case BPF_LWT_ENCAP_SEG6_INLINE:
5862 if (skb->protocol != htons(ETH_P_IPV6))
5863 return -EBADMSG;
5864
5865 err = seg6_do_srh_inline(skb, srh);
5866 break;
5867 case BPF_LWT_ENCAP_SEG6:
5868 skb_reset_inner_headers(skb);
5869 skb->encapsulation = 1;
5870 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
5871 break;
5872 default:
5873 return -EINVAL;
5874 }
5875
5876 bpf_compute_data_pointers(skb);
5877 if (err)
5878 return err;
5879
5880 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
5881
5882 return seg6_lookup_nexthop(skb, NULL, 0);
5883 }
5884 #endif /* CONFIG_IPV6_SEG6_BPF */
5885
5886 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
bpf_push_ip_encap(struct sk_buff * skb,void * hdr,u32 len,bool ingress)5887 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
5888 bool ingress)
5889 {
5890 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
5891 }
5892 #endif
5893
BPF_CALL_4(bpf_lwt_in_push_encap,struct sk_buff *,skb,u32,type,void *,hdr,u32,len)5894 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
5895 u32, len)
5896 {
5897 switch (type) {
5898 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5899 case BPF_LWT_ENCAP_SEG6:
5900 case BPF_LWT_ENCAP_SEG6_INLINE:
5901 return bpf_push_seg6_encap(skb, type, hdr, len);
5902 #endif
5903 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5904 case BPF_LWT_ENCAP_IP:
5905 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
5906 #endif
5907 default:
5908 return -EINVAL;
5909 }
5910 }
5911
BPF_CALL_4(bpf_lwt_xmit_push_encap,struct sk_buff *,skb,u32,type,void *,hdr,u32,len)5912 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
5913 void *, hdr, u32, len)
5914 {
5915 switch (type) {
5916 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5917 case BPF_LWT_ENCAP_IP:
5918 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
5919 #endif
5920 default:
5921 return -EINVAL;
5922 }
5923 }
5924
5925 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
5926 .func = bpf_lwt_in_push_encap,
5927 .gpl_only = false,
5928 .ret_type = RET_INTEGER,
5929 .arg1_type = ARG_PTR_TO_CTX,
5930 .arg2_type = ARG_ANYTHING,
5931 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5932 .arg4_type = ARG_CONST_SIZE
5933 };
5934
5935 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
5936 .func = bpf_lwt_xmit_push_encap,
5937 .gpl_only = false,
5938 .ret_type = RET_INTEGER,
5939 .arg1_type = ARG_PTR_TO_CTX,
5940 .arg2_type = ARG_ANYTHING,
5941 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5942 .arg4_type = ARG_CONST_SIZE
5943 };
5944
5945 #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)5946 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
5947 const void *, from, u32, len)
5948 {
5949 struct seg6_bpf_srh_state *srh_state =
5950 this_cpu_ptr(&seg6_bpf_srh_states);
5951 struct ipv6_sr_hdr *srh = srh_state->srh;
5952 void *srh_tlvs, *srh_end, *ptr;
5953 int srhoff = 0;
5954
5955 if (srh == NULL)
5956 return -EINVAL;
5957
5958 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
5959 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
5960
5961 ptr = skb->data + offset;
5962 if (ptr >= srh_tlvs && ptr + len <= srh_end)
5963 srh_state->valid = false;
5964 else if (ptr < (void *)&srh->flags ||
5965 ptr + len > (void *)&srh->segments)
5966 return -EFAULT;
5967
5968 if (unlikely(bpf_try_make_writable(skb, offset + len)))
5969 return -EFAULT;
5970 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5971 return -EINVAL;
5972 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5973
5974 memcpy(skb->data + offset, from, len);
5975 return 0;
5976 }
5977
5978 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
5979 .func = bpf_lwt_seg6_store_bytes,
5980 .gpl_only = false,
5981 .ret_type = RET_INTEGER,
5982 .arg1_type = ARG_PTR_TO_CTX,
5983 .arg2_type = ARG_ANYTHING,
5984 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5985 .arg4_type = ARG_CONST_SIZE
5986 };
5987
bpf_update_srh_state(struct sk_buff * skb)5988 static void bpf_update_srh_state(struct sk_buff *skb)
5989 {
5990 struct seg6_bpf_srh_state *srh_state =
5991 this_cpu_ptr(&seg6_bpf_srh_states);
5992 int srhoff = 0;
5993
5994 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
5995 srh_state->srh = NULL;
5996 } else {
5997 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5998 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
5999 srh_state->valid = true;
6000 }
6001 }
6002
BPF_CALL_4(bpf_lwt_seg6_action,struct sk_buff *,skb,u32,action,void *,param,u32,param_len)6003 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
6004 u32, action, void *, param, u32, param_len)
6005 {
6006 struct seg6_bpf_srh_state *srh_state =
6007 this_cpu_ptr(&seg6_bpf_srh_states);
6008 int hdroff = 0;
6009 int err;
6010
6011 switch (action) {
6012 case SEG6_LOCAL_ACTION_END_X:
6013 if (!seg6_bpf_has_valid_srh(skb))
6014 return -EBADMSG;
6015 if (param_len != sizeof(struct in6_addr))
6016 return -EINVAL;
6017 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
6018 case SEG6_LOCAL_ACTION_END_T:
6019 if (!seg6_bpf_has_valid_srh(skb))
6020 return -EBADMSG;
6021 if (param_len != sizeof(int))
6022 return -EINVAL;
6023 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6024 case SEG6_LOCAL_ACTION_END_DT6:
6025 if (!seg6_bpf_has_valid_srh(skb))
6026 return -EBADMSG;
6027 if (param_len != sizeof(int))
6028 return -EINVAL;
6029
6030 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
6031 return -EBADMSG;
6032 if (!pskb_pull(skb, hdroff))
6033 return -EBADMSG;
6034
6035 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
6036 skb_reset_network_header(skb);
6037 skb_reset_transport_header(skb);
6038 skb->encapsulation = 0;
6039
6040 bpf_compute_data_pointers(skb);
6041 bpf_update_srh_state(skb);
6042 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6043 case SEG6_LOCAL_ACTION_END_B6:
6044 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6045 return -EBADMSG;
6046 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
6047 param, param_len);
6048 if (!err)
6049 bpf_update_srh_state(skb);
6050
6051 return err;
6052 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
6053 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6054 return -EBADMSG;
6055 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
6056 param, param_len);
6057 if (!err)
6058 bpf_update_srh_state(skb);
6059
6060 return err;
6061 default:
6062 return -EINVAL;
6063 }
6064 }
6065
6066 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
6067 .func = bpf_lwt_seg6_action,
6068 .gpl_only = false,
6069 .ret_type = RET_INTEGER,
6070 .arg1_type = ARG_PTR_TO_CTX,
6071 .arg2_type = ARG_ANYTHING,
6072 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6073 .arg4_type = ARG_CONST_SIZE
6074 };
6075
BPF_CALL_3(bpf_lwt_seg6_adjust_srh,struct sk_buff *,skb,u32,offset,s32,len)6076 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
6077 s32, len)
6078 {
6079 struct seg6_bpf_srh_state *srh_state =
6080 this_cpu_ptr(&seg6_bpf_srh_states);
6081 struct ipv6_sr_hdr *srh = srh_state->srh;
6082 void *srh_end, *srh_tlvs, *ptr;
6083 struct ipv6hdr *hdr;
6084 int srhoff = 0;
6085 int ret;
6086
6087 if (unlikely(srh == NULL))
6088 return -EINVAL;
6089
6090 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
6091 ((srh->first_segment + 1) << 4));
6092 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
6093 srh_state->hdrlen);
6094 ptr = skb->data + offset;
6095
6096 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
6097 return -EFAULT;
6098 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
6099 return -EFAULT;
6100
6101 if (len > 0) {
6102 ret = skb_cow_head(skb, len);
6103 if (unlikely(ret < 0))
6104 return ret;
6105
6106 ret = bpf_skb_net_hdr_push(skb, offset, len);
6107 } else {
6108 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
6109 }
6110
6111 bpf_compute_data_pointers(skb);
6112 if (unlikely(ret < 0))
6113 return ret;
6114
6115 hdr = (struct ipv6hdr *)skb->data;
6116 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
6117
6118 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6119 return -EINVAL;
6120 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6121 srh_state->hdrlen += len;
6122 srh_state->valid = false;
6123 return 0;
6124 }
6125
6126 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
6127 .func = bpf_lwt_seg6_adjust_srh,
6128 .gpl_only = false,
6129 .ret_type = RET_INTEGER,
6130 .arg1_type = ARG_PTR_TO_CTX,
6131 .arg2_type = ARG_ANYTHING,
6132 .arg3_type = ARG_ANYTHING,
6133 };
6134 #endif /* CONFIG_IPV6_SEG6_BPF */
6135
6136 #ifdef CONFIG_INET
sk_lookup(struct net * net,struct bpf_sock_tuple * tuple,int dif,int sdif,u8 family,u8 proto)6137 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
6138 int dif, int sdif, u8 family, u8 proto)
6139 {
6140 bool refcounted = false;
6141 struct sock *sk = NULL;
6142
6143 if (family == AF_INET) {
6144 __be32 src4 = tuple->ipv4.saddr;
6145 __be32 dst4 = tuple->ipv4.daddr;
6146
6147 if (proto == IPPROTO_TCP)
6148 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
6149 src4, tuple->ipv4.sport,
6150 dst4, tuple->ipv4.dport,
6151 dif, sdif, &refcounted);
6152 else
6153 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
6154 dst4, tuple->ipv4.dport,
6155 dif, sdif, &udp_table, NULL);
6156 #if IS_ENABLED(CONFIG_IPV6)
6157 } else {
6158 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
6159 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
6160
6161 if (proto == IPPROTO_TCP)
6162 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
6163 src6, tuple->ipv6.sport,
6164 dst6, ntohs(tuple->ipv6.dport),
6165 dif, sdif, &refcounted);
6166 else if (likely(ipv6_bpf_stub))
6167 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
6168 src6, tuple->ipv6.sport,
6169 dst6, tuple->ipv6.dport,
6170 dif, sdif,
6171 &udp_table, NULL);
6172 #endif
6173 }
6174
6175 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
6176 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6177 sk = NULL;
6178 }
6179 return sk;
6180 }
6181
6182 /* bpf_skc_lookup performs the core lookup for different types of sockets,
6183 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
6184 * Returns the socket as an 'unsigned long' to simplify the casting in the
6185 * callers to satisfy BPF_CALL declarations.
6186 */
6187 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)6188 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6189 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6190 u64 flags, int sdif)
6191 {
6192 struct sock *sk = NULL;
6193 struct net *net;
6194 u8 family;
6195
6196 if (len == sizeof(tuple->ipv4))
6197 family = AF_INET;
6198 else if (len == sizeof(tuple->ipv6))
6199 family = AF_INET6;
6200 else
6201 return NULL;
6202
6203 if (unlikely(flags || !((s32)netns_id < 0 || netns_id <= S32_MAX)))
6204 goto out;
6205
6206 if (sdif < 0) {
6207 if (family == AF_INET)
6208 sdif = inet_sdif(skb);
6209 else
6210 sdif = inet6_sdif(skb);
6211 }
6212
6213 if ((s32)netns_id < 0) {
6214 net = caller_net;
6215 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6216 } else {
6217 net = get_net_ns_by_id(caller_net, netns_id);
6218 if (unlikely(!net))
6219 goto out;
6220 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6221 put_net(net);
6222 }
6223
6224 out:
6225 return sk;
6226 }
6227
6228 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)6229 __bpf_sk_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 = __bpf_skc_lookup(skb, tuple, len, caller_net,
6234 ifindex, proto, netns_id, flags,
6235 sdif);
6236
6237 if (sk) {
6238 struct sock *sk2 = sk_to_full_sk(sk);
6239
6240 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6241 * sock refcnt is decremented to prevent a request_sock leak.
6242 */
6243 if (!sk_fullsock(sk2))
6244 sk2 = NULL;
6245 if (sk2 != sk) {
6246 sock_gen_put(sk);
6247 /* Ensure there is no need to bump sk2 refcnt */
6248 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6249 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6250 return NULL;
6251 }
6252 sk = sk2;
6253 }
6254 }
6255
6256 return sk;
6257 }
6258
6259 static struct sock *
bpf_skc_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,u8 proto,u64 netns_id,u64 flags)6260 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6261 u8 proto, u64 netns_id, u64 flags)
6262 {
6263 struct net *caller_net;
6264 int ifindex;
6265
6266 if (skb->dev) {
6267 caller_net = dev_net(skb->dev);
6268 ifindex = skb->dev->ifindex;
6269 } else {
6270 caller_net = sock_net(skb->sk);
6271 ifindex = 0;
6272 }
6273
6274 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
6275 netns_id, flags, -1);
6276 }
6277
6278 static struct sock *
bpf_sk_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,u8 proto,u64 netns_id,u64 flags)6279 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6280 u8 proto, u64 netns_id, u64 flags)
6281 {
6282 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
6283 flags);
6284
6285 if (sk) {
6286 struct sock *sk2 = sk_to_full_sk(sk);
6287
6288 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6289 * sock refcnt is decremented to prevent a request_sock leak.
6290 */
6291 if (!sk_fullsock(sk2))
6292 sk2 = NULL;
6293 if (sk2 != sk) {
6294 sock_gen_put(sk);
6295 /* Ensure there is no need to bump sk2 refcnt */
6296 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6297 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6298 return NULL;
6299 }
6300 sk = sk2;
6301 }
6302 }
6303
6304 return sk;
6305 }
6306
BPF_CALL_5(bpf_skc_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6307 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
6308 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6309 {
6310 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
6311 netns_id, flags);
6312 }
6313
6314 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
6315 .func = bpf_skc_lookup_tcp,
6316 .gpl_only = false,
6317 .pkt_access = true,
6318 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6319 .arg1_type = ARG_PTR_TO_CTX,
6320 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6321 .arg3_type = ARG_CONST_SIZE,
6322 .arg4_type = ARG_ANYTHING,
6323 .arg5_type = ARG_ANYTHING,
6324 };
6325
BPF_CALL_5(bpf_sk_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6326 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
6327 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6328 {
6329 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
6330 netns_id, flags);
6331 }
6332
6333 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
6334 .func = bpf_sk_lookup_tcp,
6335 .gpl_only = false,
6336 .pkt_access = true,
6337 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6338 .arg1_type = ARG_PTR_TO_CTX,
6339 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6340 .arg3_type = ARG_CONST_SIZE,
6341 .arg4_type = ARG_ANYTHING,
6342 .arg5_type = ARG_ANYTHING,
6343 };
6344
BPF_CALL_5(bpf_sk_lookup_udp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6345 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
6346 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6347 {
6348 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
6349 netns_id, flags);
6350 }
6351
6352 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
6353 .func = bpf_sk_lookup_udp,
6354 .gpl_only = false,
6355 .pkt_access = true,
6356 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6357 .arg1_type = ARG_PTR_TO_CTX,
6358 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6359 .arg3_type = ARG_CONST_SIZE,
6360 .arg4_type = ARG_ANYTHING,
6361 .arg5_type = ARG_ANYTHING,
6362 };
6363
BPF_CALL_5(bpf_tc_skc_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6364 BPF_CALL_5(bpf_tc_skc_lookup_tcp, struct sk_buff *, skb,
6365 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6366 {
6367 struct net_device *dev = skb->dev;
6368 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6369 struct net *caller_net = dev_net(dev);
6370
6371 return (unsigned long)__bpf_skc_lookup(skb, tuple, len, caller_net,
6372 ifindex, IPPROTO_TCP, netns_id,
6373 flags, sdif);
6374 }
6375
6376 static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = {
6377 .func = bpf_tc_skc_lookup_tcp,
6378 .gpl_only = false,
6379 .pkt_access = true,
6380 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6381 .arg1_type = ARG_PTR_TO_CTX,
6382 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6383 .arg3_type = ARG_CONST_SIZE,
6384 .arg4_type = ARG_ANYTHING,
6385 .arg5_type = ARG_ANYTHING,
6386 };
6387
BPF_CALL_5(bpf_tc_sk_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6388 BPF_CALL_5(bpf_tc_sk_lookup_tcp, struct sk_buff *, skb,
6389 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6390 {
6391 struct net_device *dev = skb->dev;
6392 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6393 struct net *caller_net = dev_net(dev);
6394
6395 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net,
6396 ifindex, IPPROTO_TCP, netns_id,
6397 flags, sdif);
6398 }
6399
6400 static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = {
6401 .func = bpf_tc_sk_lookup_tcp,
6402 .gpl_only = false,
6403 .pkt_access = true,
6404 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6405 .arg1_type = ARG_PTR_TO_CTX,
6406 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6407 .arg3_type = ARG_CONST_SIZE,
6408 .arg4_type = ARG_ANYTHING,
6409 .arg5_type = ARG_ANYTHING,
6410 };
6411
BPF_CALL_5(bpf_tc_sk_lookup_udp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)6412 BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb,
6413 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6414 {
6415 struct net_device *dev = skb->dev;
6416 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6417 struct net *caller_net = dev_net(dev);
6418
6419 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net,
6420 ifindex, IPPROTO_UDP, netns_id,
6421 flags, sdif);
6422 }
6423
6424 static const struct bpf_func_proto bpf_tc_sk_lookup_udp_proto = {
6425 .func = bpf_tc_sk_lookup_udp,
6426 .gpl_only = false,
6427 .pkt_access = true,
6428 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6429 .arg1_type = ARG_PTR_TO_CTX,
6430 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6431 .arg3_type = ARG_CONST_SIZE,
6432 .arg4_type = ARG_ANYTHING,
6433 .arg5_type = ARG_ANYTHING,
6434 };
6435
BPF_CALL_1(bpf_sk_release,struct sock *,sk)6436 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
6437 {
6438 if (sk && sk_is_refcounted(sk))
6439 sock_gen_put(sk);
6440 return 0;
6441 }
6442
6443 static const struct bpf_func_proto bpf_sk_release_proto = {
6444 .func = bpf_sk_release,
6445 .gpl_only = false,
6446 .ret_type = RET_INTEGER,
6447 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
6448 };
6449
BPF_CALL_5(bpf_xdp_sk_lookup_udp,struct xdp_buff *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u32,netns_id,u64,flags)6450 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
6451 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6452 {
6453 struct net_device *dev = ctx->rxq->dev;
6454 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6455 struct net *caller_net = dev_net(dev);
6456
6457 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6458 ifindex, IPPROTO_UDP, netns_id,
6459 flags, sdif);
6460 }
6461
6462 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
6463 .func = bpf_xdp_sk_lookup_udp,
6464 .gpl_only = false,
6465 .pkt_access = true,
6466 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6467 .arg1_type = ARG_PTR_TO_CTX,
6468 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6469 .arg3_type = ARG_CONST_SIZE,
6470 .arg4_type = ARG_ANYTHING,
6471 .arg5_type = ARG_ANYTHING,
6472 };
6473
BPF_CALL_5(bpf_xdp_skc_lookup_tcp,struct xdp_buff *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u32,netns_id,u64,flags)6474 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
6475 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6476 {
6477 struct net_device *dev = ctx->rxq->dev;
6478 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6479 struct net *caller_net = dev_net(dev);
6480
6481 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
6482 ifindex, IPPROTO_TCP, netns_id,
6483 flags, sdif);
6484 }
6485
6486 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
6487 .func = bpf_xdp_skc_lookup_tcp,
6488 .gpl_only = false,
6489 .pkt_access = true,
6490 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6491 .arg1_type = ARG_PTR_TO_CTX,
6492 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6493 .arg3_type = ARG_CONST_SIZE,
6494 .arg4_type = ARG_ANYTHING,
6495 .arg5_type = ARG_ANYTHING,
6496 };
6497
BPF_CALL_5(bpf_xdp_sk_lookup_tcp,struct xdp_buff *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u32,netns_id,u64,flags)6498 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
6499 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6500 {
6501 struct net_device *dev = ctx->rxq->dev;
6502 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6503 struct net *caller_net = dev_net(dev);
6504
6505 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6506 ifindex, IPPROTO_TCP, netns_id,
6507 flags, sdif);
6508 }
6509
6510 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
6511 .func = bpf_xdp_sk_lookup_tcp,
6512 .gpl_only = false,
6513 .pkt_access = true,
6514 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6515 .arg1_type = ARG_PTR_TO_CTX,
6516 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6517 .arg3_type = ARG_CONST_SIZE,
6518 .arg4_type = ARG_ANYTHING,
6519 .arg5_type = ARG_ANYTHING,
6520 };
6521
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)6522 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6523 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6524 {
6525 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
6526 sock_net(ctx->sk), 0,
6527 IPPROTO_TCP, netns_id, flags,
6528 -1);
6529 }
6530
6531 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
6532 .func = bpf_sock_addr_skc_lookup_tcp,
6533 .gpl_only = false,
6534 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6535 .arg1_type = ARG_PTR_TO_CTX,
6536 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6537 .arg3_type = ARG_CONST_SIZE,
6538 .arg4_type = ARG_ANYTHING,
6539 .arg5_type = ARG_ANYTHING,
6540 };
6541
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)6542 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6543 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6544 {
6545 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6546 sock_net(ctx->sk), 0, IPPROTO_TCP,
6547 netns_id, flags, -1);
6548 }
6549
6550 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
6551 .func = bpf_sock_addr_sk_lookup_tcp,
6552 .gpl_only = false,
6553 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6554 .arg1_type = ARG_PTR_TO_CTX,
6555 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6556 .arg3_type = ARG_CONST_SIZE,
6557 .arg4_type = ARG_ANYTHING,
6558 .arg5_type = ARG_ANYTHING,
6559 };
6560
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)6561 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
6562 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6563 {
6564 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6565 sock_net(ctx->sk), 0, IPPROTO_UDP,
6566 netns_id, flags, -1);
6567 }
6568
6569 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
6570 .func = bpf_sock_addr_sk_lookup_udp,
6571 .gpl_only = false,
6572 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6573 .arg1_type = ARG_PTR_TO_CTX,
6574 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6575 .arg3_type = ARG_CONST_SIZE,
6576 .arg4_type = ARG_ANYTHING,
6577 .arg5_type = ARG_ANYTHING,
6578 };
6579
bpf_tcp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)6580 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6581 struct bpf_insn_access_aux *info)
6582 {
6583 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
6584 icsk_retransmits))
6585 return false;
6586
6587 if (off % size != 0)
6588 return false;
6589
6590 switch (off) {
6591 case offsetof(struct bpf_tcp_sock, bytes_received):
6592 case offsetof(struct bpf_tcp_sock, bytes_acked):
6593 return size == sizeof(__u64);
6594 default:
6595 return size == sizeof(__u32);
6596 }
6597 }
6598
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)6599 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
6600 const struct bpf_insn *si,
6601 struct bpf_insn *insn_buf,
6602 struct bpf_prog *prog, u32 *target_size)
6603 {
6604 struct bpf_insn *insn = insn_buf;
6605
6606 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
6607 do { \
6608 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
6609 sizeof_field(struct bpf_tcp_sock, FIELD)); \
6610 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
6611 si->dst_reg, si->src_reg, \
6612 offsetof(struct tcp_sock, FIELD)); \
6613 } while (0)
6614
6615 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
6616 do { \
6617 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
6618 FIELD) > \
6619 sizeof_field(struct bpf_tcp_sock, FIELD)); \
6620 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6621 struct inet_connection_sock, \
6622 FIELD), \
6623 si->dst_reg, si->src_reg, \
6624 offsetof( \
6625 struct inet_connection_sock, \
6626 FIELD)); \
6627 } while (0)
6628
6629 if (insn > insn_buf)
6630 return insn - insn_buf;
6631
6632 switch (si->off) {
6633 case offsetof(struct bpf_tcp_sock, rtt_min):
6634 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
6635 sizeof(struct minmax));
6636 BUILD_BUG_ON(sizeof(struct minmax) <
6637 sizeof(struct minmax_sample));
6638
6639 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6640 offsetof(struct tcp_sock, rtt_min) +
6641 offsetof(struct minmax_sample, v));
6642 break;
6643 case offsetof(struct bpf_tcp_sock, snd_cwnd):
6644 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
6645 break;
6646 case offsetof(struct bpf_tcp_sock, srtt_us):
6647 BPF_TCP_SOCK_GET_COMMON(srtt_us);
6648 break;
6649 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
6650 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
6651 break;
6652 case offsetof(struct bpf_tcp_sock, rcv_nxt):
6653 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
6654 break;
6655 case offsetof(struct bpf_tcp_sock, snd_nxt):
6656 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
6657 break;
6658 case offsetof(struct bpf_tcp_sock, snd_una):
6659 BPF_TCP_SOCK_GET_COMMON(snd_una);
6660 break;
6661 case offsetof(struct bpf_tcp_sock, mss_cache):
6662 BPF_TCP_SOCK_GET_COMMON(mss_cache);
6663 break;
6664 case offsetof(struct bpf_tcp_sock, ecn_flags):
6665 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
6666 break;
6667 case offsetof(struct bpf_tcp_sock, rate_delivered):
6668 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
6669 break;
6670 case offsetof(struct bpf_tcp_sock, rate_interval_us):
6671 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
6672 break;
6673 case offsetof(struct bpf_tcp_sock, packets_out):
6674 BPF_TCP_SOCK_GET_COMMON(packets_out);
6675 break;
6676 case offsetof(struct bpf_tcp_sock, retrans_out):
6677 BPF_TCP_SOCK_GET_COMMON(retrans_out);
6678 break;
6679 case offsetof(struct bpf_tcp_sock, total_retrans):
6680 BPF_TCP_SOCK_GET_COMMON(total_retrans);
6681 break;
6682 case offsetof(struct bpf_tcp_sock, segs_in):
6683 BPF_TCP_SOCK_GET_COMMON(segs_in);
6684 break;
6685 case offsetof(struct bpf_tcp_sock, data_segs_in):
6686 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
6687 break;
6688 case offsetof(struct bpf_tcp_sock, segs_out):
6689 BPF_TCP_SOCK_GET_COMMON(segs_out);
6690 break;
6691 case offsetof(struct bpf_tcp_sock, data_segs_out):
6692 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
6693 break;
6694 case offsetof(struct bpf_tcp_sock, lost_out):
6695 BPF_TCP_SOCK_GET_COMMON(lost_out);
6696 break;
6697 case offsetof(struct bpf_tcp_sock, sacked_out):
6698 BPF_TCP_SOCK_GET_COMMON(sacked_out);
6699 break;
6700 case offsetof(struct bpf_tcp_sock, bytes_received):
6701 BPF_TCP_SOCK_GET_COMMON(bytes_received);
6702 break;
6703 case offsetof(struct bpf_tcp_sock, bytes_acked):
6704 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
6705 break;
6706 case offsetof(struct bpf_tcp_sock, dsack_dups):
6707 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
6708 break;
6709 case offsetof(struct bpf_tcp_sock, delivered):
6710 BPF_TCP_SOCK_GET_COMMON(delivered);
6711 break;
6712 case offsetof(struct bpf_tcp_sock, delivered_ce):
6713 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
6714 break;
6715 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
6716 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
6717 break;
6718 }
6719
6720 return insn - insn_buf;
6721 }
6722
BPF_CALL_1(bpf_tcp_sock,struct sock *,sk)6723 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
6724 {
6725 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
6726 return (unsigned long)sk;
6727
6728 return (unsigned long)NULL;
6729 }
6730
6731 const struct bpf_func_proto bpf_tcp_sock_proto = {
6732 .func = bpf_tcp_sock,
6733 .gpl_only = false,
6734 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
6735 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6736 };
6737
BPF_CALL_1(bpf_get_listener_sock,struct sock *,sk)6738 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
6739 {
6740 sk = sk_to_full_sk(sk);
6741
6742 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
6743 return (unsigned long)sk;
6744
6745 return (unsigned long)NULL;
6746 }
6747
6748 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
6749 .func = bpf_get_listener_sock,
6750 .gpl_only = false,
6751 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6752 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6753 };
6754
BPF_CALL_1(bpf_skb_ecn_set_ce,struct sk_buff *,skb)6755 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
6756 {
6757 unsigned int iphdr_len;
6758
6759 switch (skb_protocol(skb, true)) {
6760 case cpu_to_be16(ETH_P_IP):
6761 iphdr_len = sizeof(struct iphdr);
6762 break;
6763 case cpu_to_be16(ETH_P_IPV6):
6764 iphdr_len = sizeof(struct ipv6hdr);
6765 break;
6766 default:
6767 return 0;
6768 }
6769
6770 if (skb_headlen(skb) < iphdr_len)
6771 return 0;
6772
6773 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
6774 return 0;
6775
6776 return INET_ECN_set_ce(skb);
6777 }
6778
bpf_xdp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)6779 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6780 struct bpf_insn_access_aux *info)
6781 {
6782 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
6783 return false;
6784
6785 if (off % size != 0)
6786 return false;
6787
6788 switch (off) {
6789 default:
6790 return size == sizeof(__u32);
6791 }
6792 }
6793
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)6794 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
6795 const struct bpf_insn *si,
6796 struct bpf_insn *insn_buf,
6797 struct bpf_prog *prog, u32 *target_size)
6798 {
6799 struct bpf_insn *insn = insn_buf;
6800
6801 #define BPF_XDP_SOCK_GET(FIELD) \
6802 do { \
6803 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
6804 sizeof_field(struct bpf_xdp_sock, FIELD)); \
6805 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
6806 si->dst_reg, si->src_reg, \
6807 offsetof(struct xdp_sock, FIELD)); \
6808 } while (0)
6809
6810 switch (si->off) {
6811 case offsetof(struct bpf_xdp_sock, queue_id):
6812 BPF_XDP_SOCK_GET(queue_id);
6813 break;
6814 }
6815
6816 return insn - insn_buf;
6817 }
6818
6819 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
6820 .func = bpf_skb_ecn_set_ce,
6821 .gpl_only = false,
6822 .ret_type = RET_INTEGER,
6823 .arg1_type = ARG_PTR_TO_CTX,
6824 };
6825
BPF_CALL_5(bpf_tcp_check_syncookie,struct sock *,sk,void *,iph,u32,iph_len,struct tcphdr *,th,u32,th_len)6826 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
6827 struct tcphdr *, th, u32, th_len)
6828 {
6829 #ifdef CONFIG_SYN_COOKIES
6830 u32 cookie;
6831 int ret;
6832
6833 if (unlikely(!sk || th_len < sizeof(*th)))
6834 return -EINVAL;
6835
6836 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
6837 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
6838 return -EINVAL;
6839
6840 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
6841 return -EINVAL;
6842
6843 if (!th->ack || th->rst || th->syn)
6844 return -ENOENT;
6845
6846 if (unlikely(iph_len < sizeof(struct iphdr)))
6847 return -EINVAL;
6848
6849 if (tcp_synq_no_recent_overflow(sk))
6850 return -ENOENT;
6851
6852 cookie = ntohl(th->ack_seq) - 1;
6853
6854 /* Both struct iphdr and struct ipv6hdr have the version field at the
6855 * same offset so we can cast to the shorter header (struct iphdr).
6856 */
6857 switch (((struct iphdr *)iph)->version) {
6858 case 4:
6859 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
6860 return -EINVAL;
6861
6862 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
6863 break;
6864
6865 #if IS_BUILTIN(CONFIG_IPV6)
6866 case 6:
6867 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
6868 return -EINVAL;
6869
6870 if (sk->sk_family != AF_INET6)
6871 return -EINVAL;
6872
6873 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
6874 break;
6875 #endif /* CONFIG_IPV6 */
6876
6877 default:
6878 return -EPROTONOSUPPORT;
6879 }
6880
6881 if (ret > 0)
6882 return 0;
6883
6884 return -ENOENT;
6885 #else
6886 return -ENOTSUPP;
6887 #endif
6888 }
6889
6890 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
6891 .func = bpf_tcp_check_syncookie,
6892 .gpl_only = true,
6893 .pkt_access = true,
6894 .ret_type = RET_INTEGER,
6895 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
6896 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6897 .arg3_type = ARG_CONST_SIZE,
6898 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6899 .arg5_type = ARG_CONST_SIZE,
6900 };
6901
BPF_CALL_5(bpf_tcp_gen_syncookie,struct sock *,sk,void *,iph,u32,iph_len,struct tcphdr *,th,u32,th_len)6902 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
6903 struct tcphdr *, th, u32, th_len)
6904 {
6905 #ifdef CONFIG_SYN_COOKIES
6906 u32 cookie;
6907 u16 mss;
6908
6909 if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4))
6910 return -EINVAL;
6911
6912 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
6913 return -EINVAL;
6914
6915 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
6916 return -ENOENT;
6917
6918 if (!th->syn || th->ack || th->fin || th->rst)
6919 return -EINVAL;
6920
6921 if (unlikely(iph_len < sizeof(struct iphdr)))
6922 return -EINVAL;
6923
6924 /* Both struct iphdr and struct ipv6hdr have the version field at the
6925 * same offset so we can cast to the shorter header (struct iphdr).
6926 */
6927 switch (((struct iphdr *)iph)->version) {
6928 case 4:
6929 if (sk->sk_family == AF_INET6 && sk->sk_ipv6only)
6930 return -EINVAL;
6931
6932 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
6933 break;
6934
6935 #if IS_BUILTIN(CONFIG_IPV6)
6936 case 6:
6937 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
6938 return -EINVAL;
6939
6940 if (sk->sk_family != AF_INET6)
6941 return -EINVAL;
6942
6943 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
6944 break;
6945 #endif /* CONFIG_IPV6 */
6946
6947 default:
6948 return -EPROTONOSUPPORT;
6949 }
6950 if (mss == 0)
6951 return -ENOENT;
6952
6953 return cookie | ((u64)mss << 32);
6954 #else
6955 return -EOPNOTSUPP;
6956 #endif /* CONFIG_SYN_COOKIES */
6957 }
6958
6959 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
6960 .func = bpf_tcp_gen_syncookie,
6961 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
6962 .pkt_access = true,
6963 .ret_type = RET_INTEGER,
6964 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
6965 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6966 .arg3_type = ARG_CONST_SIZE,
6967 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6968 .arg5_type = ARG_CONST_SIZE,
6969 };
6970
BPF_CALL_3(bpf_sk_assign,struct sk_buff *,skb,struct sock *,sk,u64,flags)6971 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
6972 {
6973 if (!sk || flags != 0)
6974 return -EINVAL;
6975 if (!skb_at_tc_ingress(skb))
6976 return -EOPNOTSUPP;
6977 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
6978 return -ENETUNREACH;
6979 if (unlikely(sk_fullsock(sk) && sk->sk_reuseport))
6980 return -ESOCKTNOSUPPORT;
6981 if (sk_unhashed(sk))
6982 return -EOPNOTSUPP;
6983 if (sk_is_refcounted(sk) &&
6984 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
6985 return -ENOENT;
6986
6987 skb_orphan(skb);
6988 skb->sk = sk;
6989 skb->destructor = sock_pfree;
6990
6991 return 0;
6992 }
6993
6994 static const struct bpf_func_proto bpf_sk_assign_proto = {
6995 .func = bpf_sk_assign,
6996 .gpl_only = false,
6997 .ret_type = RET_INTEGER,
6998 .arg1_type = ARG_PTR_TO_CTX,
6999 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7000 .arg3_type = ARG_ANYTHING,
7001 };
7002
bpf_search_tcp_opt(const u8 * op,const u8 * opend,u8 search_kind,const u8 * magic,u8 magic_len,bool * eol)7003 static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,
7004 u8 search_kind, const u8 *magic,
7005 u8 magic_len, bool *eol)
7006 {
7007 u8 kind, kind_len;
7008
7009 *eol = false;
7010
7011 while (op < opend) {
7012 kind = op[0];
7013
7014 if (kind == TCPOPT_EOL) {
7015 *eol = true;
7016 return ERR_PTR(-ENOMSG);
7017 } else if (kind == TCPOPT_NOP) {
7018 op++;
7019 continue;
7020 }
7021
7022 if (opend - op < 2 || opend - op < op[1] || op[1] < 2)
7023 /* Something is wrong in the received header.
7024 * Follow the TCP stack's tcp_parse_options()
7025 * and just bail here.
7026 */
7027 return ERR_PTR(-EFAULT);
7028
7029 kind_len = op[1];
7030 if (search_kind == kind) {
7031 if (!magic_len)
7032 return op;
7033
7034 if (magic_len > kind_len - 2)
7035 return ERR_PTR(-ENOMSG);
7036
7037 if (!memcmp(&op[2], magic, magic_len))
7038 return op;
7039 }
7040
7041 op += kind_len;
7042 }
7043
7044 return ERR_PTR(-ENOMSG);
7045 }
7046
BPF_CALL_4(bpf_sock_ops_load_hdr_opt,struct bpf_sock_ops_kern *,bpf_sock,void *,search_res,u32,len,u64,flags)7047 BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7048 void *, search_res, u32, len, u64, flags)
7049 {
7050 bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN;
7051 const u8 *op, *opend, *magic, *search = search_res;
7052 u8 search_kind, search_len, copy_len, magic_len;
7053 int ret;
7054
7055 /* 2 byte is the minimal option len except TCPOPT_NOP and
7056 * TCPOPT_EOL which are useless for the bpf prog to learn
7057 * and this helper disallow loading them also.
7058 */
7059 if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN)
7060 return -EINVAL;
7061
7062 search_kind = search[0];
7063 search_len = search[1];
7064
7065 if (search_len > len || search_kind == TCPOPT_NOP ||
7066 search_kind == TCPOPT_EOL)
7067 return -EINVAL;
7068
7069 if (search_kind == TCPOPT_EXP || search_kind == 253) {
7070 /* 16 or 32 bit magic. +2 for kind and kind length */
7071 if (search_len != 4 && search_len != 6)
7072 return -EINVAL;
7073 magic = &search[2];
7074 magic_len = search_len - 2;
7075 } else {
7076 if (search_len)
7077 return -EINVAL;
7078 magic = NULL;
7079 magic_len = 0;
7080 }
7081
7082 if (load_syn) {
7083 ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op);
7084 if (ret < 0)
7085 return ret;
7086
7087 opend = op + ret;
7088 op += sizeof(struct tcphdr);
7089 } else {
7090 if (!bpf_sock->skb ||
7091 bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7092 /* This bpf_sock->op cannot call this helper */
7093 return -EPERM;
7094
7095 opend = bpf_sock->skb_data_end;
7096 op = bpf_sock->skb->data + sizeof(struct tcphdr);
7097 }
7098
7099 op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len,
7100 &eol);
7101 if (IS_ERR(op))
7102 return PTR_ERR(op);
7103
7104 copy_len = op[1];
7105 ret = copy_len;
7106 if (copy_len > len) {
7107 ret = -ENOSPC;
7108 copy_len = len;
7109 }
7110
7111 memcpy(search_res, op, copy_len);
7112 return ret;
7113 }
7114
7115 static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
7116 .func = bpf_sock_ops_load_hdr_opt,
7117 .gpl_only = false,
7118 .ret_type = RET_INTEGER,
7119 .arg1_type = ARG_PTR_TO_CTX,
7120 .arg2_type = ARG_PTR_TO_MEM,
7121 .arg3_type = ARG_CONST_SIZE,
7122 .arg4_type = ARG_ANYTHING,
7123 };
7124
BPF_CALL_4(bpf_sock_ops_store_hdr_opt,struct bpf_sock_ops_kern *,bpf_sock,const void *,from,u32,len,u64,flags)7125 BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7126 const void *, from, u32, len, u64, flags)
7127 {
7128 u8 new_kind, new_kind_len, magic_len = 0, *opend;
7129 const u8 *op, *new_op, *magic = NULL;
7130 struct sk_buff *skb;
7131 bool eol;
7132
7133 if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
7134 return -EPERM;
7135
7136 if (len < 2 || flags)
7137 return -EINVAL;
7138
7139 new_op = from;
7140 new_kind = new_op[0];
7141 new_kind_len = new_op[1];
7142
7143 if (new_kind_len > len || new_kind == TCPOPT_NOP ||
7144 new_kind == TCPOPT_EOL)
7145 return -EINVAL;
7146
7147 if (new_kind_len > bpf_sock->remaining_opt_len)
7148 return -ENOSPC;
7149
7150 /* 253 is another experimental kind */
7151 if (new_kind == TCPOPT_EXP || new_kind == 253) {
7152 if (new_kind_len < 4)
7153 return -EINVAL;
7154 /* Match for the 2 byte magic also.
7155 * RFC 6994: the magic could be 2 or 4 bytes.
7156 * Hence, matching by 2 byte only is on the
7157 * conservative side but it is the right
7158 * thing to do for the 'search-for-duplication'
7159 * purpose.
7160 */
7161 magic = &new_op[2];
7162 magic_len = 2;
7163 }
7164
7165 /* Check for duplication */
7166 skb = bpf_sock->skb;
7167 op = skb->data + sizeof(struct tcphdr);
7168 opend = bpf_sock->skb_data_end;
7169
7170 op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len,
7171 &eol);
7172 if (!IS_ERR(op))
7173 return -EEXIST;
7174
7175 if (PTR_ERR(op) != -ENOMSG)
7176 return PTR_ERR(op);
7177
7178 if (eol)
7179 /* The option has been ended. Treat it as no more
7180 * header option can be written.
7181 */
7182 return -ENOSPC;
7183
7184 /* No duplication found. Store the header option. */
7185 memcpy(opend, from, new_kind_len);
7186
7187 bpf_sock->remaining_opt_len -= new_kind_len;
7188 bpf_sock->skb_data_end += new_kind_len;
7189
7190 return 0;
7191 }
7192
7193 static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {
7194 .func = bpf_sock_ops_store_hdr_opt,
7195 .gpl_only = false,
7196 .ret_type = RET_INTEGER,
7197 .arg1_type = ARG_PTR_TO_CTX,
7198 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7199 .arg3_type = ARG_CONST_SIZE,
7200 .arg4_type = ARG_ANYTHING,
7201 };
7202
BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt,struct bpf_sock_ops_kern *,bpf_sock,u32,len,u64,flags)7203 BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7204 u32, len, u64, flags)
7205 {
7206 if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7207 return -EPERM;
7208
7209 if (flags || len < 2)
7210 return -EINVAL;
7211
7212 if (len > bpf_sock->remaining_opt_len)
7213 return -ENOSPC;
7214
7215 bpf_sock->remaining_opt_len -= len;
7216
7217 return 0;
7218 }
7219
7220 static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = {
7221 .func = bpf_sock_ops_reserve_hdr_opt,
7222 .gpl_only = false,
7223 .ret_type = RET_INTEGER,
7224 .arg1_type = ARG_PTR_TO_CTX,
7225 .arg2_type = ARG_ANYTHING,
7226 .arg3_type = ARG_ANYTHING,
7227 };
7228
7229 #endif /* CONFIG_INET */
7230
bpf_helper_changes_pkt_data(void * func)7231 bool bpf_helper_changes_pkt_data(void *func)
7232 {
7233 if (func == bpf_skb_vlan_push ||
7234 func == bpf_skb_vlan_pop ||
7235 func == bpf_skb_store_bytes ||
7236 func == bpf_skb_change_proto ||
7237 func == bpf_skb_change_head ||
7238 func == sk_skb_change_head ||
7239 func == bpf_skb_change_tail ||
7240 func == sk_skb_change_tail ||
7241 func == bpf_skb_adjust_room ||
7242 func == sk_skb_adjust_room ||
7243 func == bpf_skb_pull_data ||
7244 func == sk_skb_pull_data ||
7245 func == bpf_clone_redirect ||
7246 func == bpf_l3_csum_replace ||
7247 func == bpf_l4_csum_replace ||
7248 func == bpf_xdp_adjust_head ||
7249 func == bpf_xdp_adjust_meta ||
7250 func == bpf_msg_pull_data ||
7251 func == bpf_msg_push_data ||
7252 func == bpf_msg_pop_data ||
7253 func == bpf_xdp_adjust_tail ||
7254 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
7255 func == bpf_lwt_seg6_store_bytes ||
7256 func == bpf_lwt_seg6_adjust_srh ||
7257 func == bpf_lwt_seg6_action ||
7258 #endif
7259 #ifdef CONFIG_INET
7260 func == bpf_sock_ops_store_hdr_opt ||
7261 #endif
7262 func == bpf_lwt_in_push_encap ||
7263 func == bpf_lwt_xmit_push_encap)
7264 return true;
7265
7266 return false;
7267 }
7268
7269 const struct bpf_func_proto bpf_event_output_data_proto __weak;
7270 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak;
7271
7272 static const struct bpf_func_proto *
sock_filter_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7273 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7274 {
7275 switch (func_id) {
7276 /* inet and inet6 sockets are created in a process
7277 * context so there is always a valid uid/gid
7278 */
7279 case BPF_FUNC_get_current_uid_gid:
7280 return &bpf_get_current_uid_gid_proto;
7281 case BPF_FUNC_get_local_storage:
7282 return &bpf_get_local_storage_proto;
7283 case BPF_FUNC_get_socket_cookie:
7284 return &bpf_get_socket_cookie_sock_proto;
7285 case BPF_FUNC_get_netns_cookie:
7286 return &bpf_get_netns_cookie_sock_proto;
7287 case BPF_FUNC_perf_event_output:
7288 return &bpf_event_output_data_proto;
7289 case BPF_FUNC_get_current_pid_tgid:
7290 return &bpf_get_current_pid_tgid_proto;
7291 case BPF_FUNC_get_current_comm:
7292 return &bpf_get_current_comm_proto;
7293 #ifdef CONFIG_CGROUPS
7294 case BPF_FUNC_get_current_cgroup_id:
7295 return &bpf_get_current_cgroup_id_proto;
7296 case BPF_FUNC_get_current_ancestor_cgroup_id:
7297 return &bpf_get_current_ancestor_cgroup_id_proto;
7298 #endif
7299 #ifdef CONFIG_CGROUP_NET_CLASSID
7300 case BPF_FUNC_get_cgroup_classid:
7301 return &bpf_get_cgroup_classid_curr_proto;
7302 #endif
7303 case BPF_FUNC_sk_storage_get:
7304 return &bpf_sk_storage_get_cg_sock_proto;
7305 case BPF_FUNC_ktime_get_coarse_ns:
7306 return &bpf_ktime_get_coarse_ns_proto;
7307 default:
7308 return bpf_base_func_proto(func_id);
7309 }
7310 }
7311
7312 static const struct bpf_func_proto *
sock_addr_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7313 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7314 {
7315 switch (func_id) {
7316 /* inet and inet6 sockets are created in a process
7317 * context so there is always a valid uid/gid
7318 */
7319 case BPF_FUNC_get_current_uid_gid:
7320 return &bpf_get_current_uid_gid_proto;
7321 case BPF_FUNC_bind:
7322 switch (prog->expected_attach_type) {
7323 case BPF_CGROUP_INET4_CONNECT:
7324 case BPF_CGROUP_INET6_CONNECT:
7325 return &bpf_bind_proto;
7326 default:
7327 return NULL;
7328 }
7329 case BPF_FUNC_get_socket_cookie:
7330 return &bpf_get_socket_cookie_sock_addr_proto;
7331 case BPF_FUNC_get_netns_cookie:
7332 return &bpf_get_netns_cookie_sock_addr_proto;
7333 case BPF_FUNC_get_local_storage:
7334 return &bpf_get_local_storage_proto;
7335 case BPF_FUNC_perf_event_output:
7336 return &bpf_event_output_data_proto;
7337 case BPF_FUNC_get_current_pid_tgid:
7338 return &bpf_get_current_pid_tgid_proto;
7339 case BPF_FUNC_get_current_comm:
7340 return &bpf_get_current_comm_proto;
7341 #ifdef CONFIG_CGROUPS
7342 case BPF_FUNC_get_current_cgroup_id:
7343 return &bpf_get_current_cgroup_id_proto;
7344 case BPF_FUNC_get_current_ancestor_cgroup_id:
7345 return &bpf_get_current_ancestor_cgroup_id_proto;
7346 #endif
7347 #ifdef CONFIG_CGROUP_NET_CLASSID
7348 case BPF_FUNC_get_cgroup_classid:
7349 return &bpf_get_cgroup_classid_curr_proto;
7350 #endif
7351 #ifdef CONFIG_INET
7352 case BPF_FUNC_sk_lookup_tcp:
7353 return &bpf_sock_addr_sk_lookup_tcp_proto;
7354 case BPF_FUNC_sk_lookup_udp:
7355 return &bpf_sock_addr_sk_lookup_udp_proto;
7356 case BPF_FUNC_sk_release:
7357 return &bpf_sk_release_proto;
7358 case BPF_FUNC_skc_lookup_tcp:
7359 return &bpf_sock_addr_skc_lookup_tcp_proto;
7360 #endif /* CONFIG_INET */
7361 case BPF_FUNC_sk_storage_get:
7362 return &bpf_sk_storage_get_proto;
7363 case BPF_FUNC_sk_storage_delete:
7364 return &bpf_sk_storage_delete_proto;
7365 case BPF_FUNC_setsockopt:
7366 switch (prog->expected_attach_type) {
7367 case BPF_CGROUP_INET4_BIND:
7368 case BPF_CGROUP_INET6_BIND:
7369 case BPF_CGROUP_INET4_CONNECT:
7370 case BPF_CGROUP_INET6_CONNECT:
7371 case BPF_CGROUP_UDP4_RECVMSG:
7372 case BPF_CGROUP_UDP6_RECVMSG:
7373 case BPF_CGROUP_UDP4_SENDMSG:
7374 case BPF_CGROUP_UDP6_SENDMSG:
7375 case BPF_CGROUP_INET4_GETPEERNAME:
7376 case BPF_CGROUP_INET6_GETPEERNAME:
7377 case BPF_CGROUP_INET4_GETSOCKNAME:
7378 case BPF_CGROUP_INET6_GETSOCKNAME:
7379 return &bpf_sock_addr_setsockopt_proto;
7380 default:
7381 return NULL;
7382 }
7383 case BPF_FUNC_getsockopt:
7384 switch (prog->expected_attach_type) {
7385 case BPF_CGROUP_INET4_BIND:
7386 case BPF_CGROUP_INET6_BIND:
7387 case BPF_CGROUP_INET4_CONNECT:
7388 case BPF_CGROUP_INET6_CONNECT:
7389 case BPF_CGROUP_UDP4_RECVMSG:
7390 case BPF_CGROUP_UDP6_RECVMSG:
7391 case BPF_CGROUP_UDP4_SENDMSG:
7392 case BPF_CGROUP_UDP6_SENDMSG:
7393 case BPF_CGROUP_INET4_GETPEERNAME:
7394 case BPF_CGROUP_INET6_GETPEERNAME:
7395 case BPF_CGROUP_INET4_GETSOCKNAME:
7396 case BPF_CGROUP_INET6_GETSOCKNAME:
7397 return &bpf_sock_addr_getsockopt_proto;
7398 default:
7399 return NULL;
7400 }
7401 default:
7402 return bpf_sk_base_func_proto(func_id);
7403 }
7404 }
7405
7406 static const struct bpf_func_proto *
sk_filter_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7407 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7408 {
7409 switch (func_id) {
7410 case BPF_FUNC_skb_load_bytes:
7411 return &bpf_skb_load_bytes_proto;
7412 case BPF_FUNC_skb_load_bytes_relative:
7413 return &bpf_skb_load_bytes_relative_proto;
7414 case BPF_FUNC_get_socket_cookie:
7415 return &bpf_get_socket_cookie_proto;
7416 case BPF_FUNC_get_socket_uid:
7417 return &bpf_get_socket_uid_proto;
7418 case BPF_FUNC_perf_event_output:
7419 return &bpf_skb_event_output_proto;
7420 default:
7421 return bpf_sk_base_func_proto(func_id);
7422 }
7423 }
7424
7425 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
7426 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
7427
7428 static const struct bpf_func_proto *
cg_skb_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7429 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7430 {
7431 switch (func_id) {
7432 case BPF_FUNC_get_local_storage:
7433 return &bpf_get_local_storage_proto;
7434 case BPF_FUNC_sk_fullsock:
7435 return &bpf_sk_fullsock_proto;
7436 case BPF_FUNC_sk_storage_get:
7437 return &bpf_sk_storage_get_proto;
7438 case BPF_FUNC_sk_storage_delete:
7439 return &bpf_sk_storage_delete_proto;
7440 case BPF_FUNC_perf_event_output:
7441 return &bpf_skb_event_output_proto;
7442 #ifdef CONFIG_SOCK_CGROUP_DATA
7443 case BPF_FUNC_skb_cgroup_id:
7444 return &bpf_skb_cgroup_id_proto;
7445 case BPF_FUNC_skb_ancestor_cgroup_id:
7446 return &bpf_skb_ancestor_cgroup_id_proto;
7447 case BPF_FUNC_sk_cgroup_id:
7448 return &bpf_sk_cgroup_id_proto;
7449 case BPF_FUNC_sk_ancestor_cgroup_id:
7450 return &bpf_sk_ancestor_cgroup_id_proto;
7451 #endif
7452 #ifdef CONFIG_INET
7453 case BPF_FUNC_sk_lookup_tcp:
7454 return &bpf_sk_lookup_tcp_proto;
7455 case BPF_FUNC_sk_lookup_udp:
7456 return &bpf_sk_lookup_udp_proto;
7457 case BPF_FUNC_sk_release:
7458 return &bpf_sk_release_proto;
7459 case BPF_FUNC_skc_lookup_tcp:
7460 return &bpf_skc_lookup_tcp_proto;
7461 case BPF_FUNC_tcp_sock:
7462 return &bpf_tcp_sock_proto;
7463 case BPF_FUNC_get_listener_sock:
7464 return &bpf_get_listener_sock_proto;
7465 case BPF_FUNC_skb_ecn_set_ce:
7466 return &bpf_skb_ecn_set_ce_proto;
7467 #endif
7468 default:
7469 return sk_filter_func_proto(func_id, prog);
7470 }
7471 }
7472
7473 static const struct bpf_func_proto *
tc_cls_act_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7474 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7475 {
7476 switch (func_id) {
7477 case BPF_FUNC_skb_store_bytes:
7478 return &bpf_skb_store_bytes_proto;
7479 case BPF_FUNC_skb_load_bytes:
7480 return &bpf_skb_load_bytes_proto;
7481 case BPF_FUNC_skb_load_bytes_relative:
7482 return &bpf_skb_load_bytes_relative_proto;
7483 case BPF_FUNC_skb_pull_data:
7484 return &bpf_skb_pull_data_proto;
7485 case BPF_FUNC_csum_diff:
7486 return &bpf_csum_diff_proto;
7487 case BPF_FUNC_csum_update:
7488 return &bpf_csum_update_proto;
7489 case BPF_FUNC_csum_level:
7490 return &bpf_csum_level_proto;
7491 case BPF_FUNC_l3_csum_replace:
7492 return &bpf_l3_csum_replace_proto;
7493 case BPF_FUNC_l4_csum_replace:
7494 return &bpf_l4_csum_replace_proto;
7495 case BPF_FUNC_clone_redirect:
7496 return &bpf_clone_redirect_proto;
7497 case BPF_FUNC_get_cgroup_classid:
7498 return &bpf_get_cgroup_classid_proto;
7499 case BPF_FUNC_skb_vlan_push:
7500 return &bpf_skb_vlan_push_proto;
7501 case BPF_FUNC_skb_vlan_pop:
7502 return &bpf_skb_vlan_pop_proto;
7503 case BPF_FUNC_skb_change_proto:
7504 return &bpf_skb_change_proto_proto;
7505 case BPF_FUNC_skb_change_type:
7506 return &bpf_skb_change_type_proto;
7507 case BPF_FUNC_skb_adjust_room:
7508 return &bpf_skb_adjust_room_proto;
7509 case BPF_FUNC_skb_change_tail:
7510 return &bpf_skb_change_tail_proto;
7511 case BPF_FUNC_skb_change_head:
7512 return &bpf_skb_change_head_proto;
7513 case BPF_FUNC_skb_get_tunnel_key:
7514 return &bpf_skb_get_tunnel_key_proto;
7515 case BPF_FUNC_skb_set_tunnel_key:
7516 return bpf_get_skb_set_tunnel_proto(func_id);
7517 case BPF_FUNC_skb_get_tunnel_opt:
7518 return &bpf_skb_get_tunnel_opt_proto;
7519 case BPF_FUNC_skb_set_tunnel_opt:
7520 return bpf_get_skb_set_tunnel_proto(func_id);
7521 case BPF_FUNC_redirect:
7522 return &bpf_redirect_proto;
7523 case BPF_FUNC_redirect_neigh:
7524 return &bpf_redirect_neigh_proto;
7525 case BPF_FUNC_redirect_peer:
7526 return &bpf_redirect_peer_proto;
7527 case BPF_FUNC_get_route_realm:
7528 return &bpf_get_route_realm_proto;
7529 case BPF_FUNC_get_hash_recalc:
7530 return &bpf_get_hash_recalc_proto;
7531 case BPF_FUNC_set_hash_invalid:
7532 return &bpf_set_hash_invalid_proto;
7533 case BPF_FUNC_set_hash:
7534 return &bpf_set_hash_proto;
7535 case BPF_FUNC_perf_event_output:
7536 return &bpf_skb_event_output_proto;
7537 case BPF_FUNC_get_smp_processor_id:
7538 return &bpf_get_smp_processor_id_proto;
7539 case BPF_FUNC_skb_under_cgroup:
7540 return &bpf_skb_under_cgroup_proto;
7541 case BPF_FUNC_get_socket_cookie:
7542 return &bpf_get_socket_cookie_proto;
7543 case BPF_FUNC_get_socket_uid:
7544 return &bpf_get_socket_uid_proto;
7545 case BPF_FUNC_fib_lookup:
7546 return &bpf_skb_fib_lookup_proto;
7547 case BPF_FUNC_check_mtu:
7548 return &bpf_skb_check_mtu_proto;
7549 case BPF_FUNC_sk_fullsock:
7550 return &bpf_sk_fullsock_proto;
7551 case BPF_FUNC_sk_storage_get:
7552 return &bpf_sk_storage_get_proto;
7553 case BPF_FUNC_sk_storage_delete:
7554 return &bpf_sk_storage_delete_proto;
7555 #ifdef CONFIG_XFRM
7556 case BPF_FUNC_skb_get_xfrm_state:
7557 return &bpf_skb_get_xfrm_state_proto;
7558 #endif
7559 #ifdef CONFIG_CGROUP_NET_CLASSID
7560 case BPF_FUNC_skb_cgroup_classid:
7561 return &bpf_skb_cgroup_classid_proto;
7562 #endif
7563 #ifdef CONFIG_SOCK_CGROUP_DATA
7564 case BPF_FUNC_skb_cgroup_id:
7565 return &bpf_skb_cgroup_id_proto;
7566 case BPF_FUNC_skb_ancestor_cgroup_id:
7567 return &bpf_skb_ancestor_cgroup_id_proto;
7568 #endif
7569 #ifdef CONFIG_INET
7570 case BPF_FUNC_sk_lookup_tcp:
7571 return &bpf_tc_sk_lookup_tcp_proto;
7572 case BPF_FUNC_sk_lookup_udp:
7573 return &bpf_tc_sk_lookup_udp_proto;
7574 case BPF_FUNC_sk_release:
7575 return &bpf_sk_release_proto;
7576 case BPF_FUNC_tcp_sock:
7577 return &bpf_tcp_sock_proto;
7578 case BPF_FUNC_get_listener_sock:
7579 return &bpf_get_listener_sock_proto;
7580 case BPF_FUNC_skc_lookup_tcp:
7581 return &bpf_tc_skc_lookup_tcp_proto;
7582 case BPF_FUNC_tcp_check_syncookie:
7583 return &bpf_tcp_check_syncookie_proto;
7584 case BPF_FUNC_skb_ecn_set_ce:
7585 return &bpf_skb_ecn_set_ce_proto;
7586 case BPF_FUNC_tcp_gen_syncookie:
7587 return &bpf_tcp_gen_syncookie_proto;
7588 case BPF_FUNC_sk_assign:
7589 return &bpf_sk_assign_proto;
7590 #endif
7591 default:
7592 return bpf_sk_base_func_proto(func_id);
7593 }
7594 }
7595
7596 static const struct bpf_func_proto *
xdp_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7597 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7598 {
7599 switch (func_id) {
7600 case BPF_FUNC_perf_event_output:
7601 return &bpf_xdp_event_output_proto;
7602 case BPF_FUNC_get_smp_processor_id:
7603 return &bpf_get_smp_processor_id_proto;
7604 case BPF_FUNC_csum_diff:
7605 return &bpf_csum_diff_proto;
7606 case BPF_FUNC_xdp_adjust_head:
7607 return &bpf_xdp_adjust_head_proto;
7608 case BPF_FUNC_xdp_adjust_meta:
7609 return &bpf_xdp_adjust_meta_proto;
7610 case BPF_FUNC_redirect:
7611 return &bpf_xdp_redirect_proto;
7612 case BPF_FUNC_redirect_map:
7613 return &bpf_xdp_redirect_map_proto;
7614 case BPF_FUNC_xdp_adjust_tail:
7615 return &bpf_xdp_adjust_tail_proto;
7616 case BPF_FUNC_fib_lookup:
7617 return &bpf_xdp_fib_lookup_proto;
7618 case BPF_FUNC_check_mtu:
7619 return &bpf_xdp_check_mtu_proto;
7620 #ifdef CONFIG_INET
7621 case BPF_FUNC_sk_lookup_udp:
7622 return &bpf_xdp_sk_lookup_udp_proto;
7623 case BPF_FUNC_sk_lookup_tcp:
7624 return &bpf_xdp_sk_lookup_tcp_proto;
7625 case BPF_FUNC_sk_release:
7626 return &bpf_sk_release_proto;
7627 case BPF_FUNC_skc_lookup_tcp:
7628 return &bpf_xdp_skc_lookup_tcp_proto;
7629 case BPF_FUNC_tcp_check_syncookie:
7630 return &bpf_tcp_check_syncookie_proto;
7631 case BPF_FUNC_tcp_gen_syncookie:
7632 return &bpf_tcp_gen_syncookie_proto;
7633 #endif
7634 default:
7635 return bpf_sk_base_func_proto(func_id);
7636 }
7637 }
7638
7639 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
7640 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
7641
7642 static const struct bpf_func_proto *
sock_ops_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7643 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7644 {
7645 switch (func_id) {
7646 case BPF_FUNC_setsockopt:
7647 return &bpf_sock_ops_setsockopt_proto;
7648 case BPF_FUNC_getsockopt:
7649 return &bpf_sock_ops_getsockopt_proto;
7650 case BPF_FUNC_sock_ops_cb_flags_set:
7651 return &bpf_sock_ops_cb_flags_set_proto;
7652 case BPF_FUNC_sock_map_update:
7653 return &bpf_sock_map_update_proto;
7654 case BPF_FUNC_sock_hash_update:
7655 return &bpf_sock_hash_update_proto;
7656 case BPF_FUNC_get_socket_cookie:
7657 return &bpf_get_socket_cookie_sock_ops_proto;
7658 case BPF_FUNC_get_local_storage:
7659 return &bpf_get_local_storage_proto;
7660 case BPF_FUNC_perf_event_output:
7661 return &bpf_event_output_data_proto;
7662 case BPF_FUNC_sk_storage_get:
7663 return &bpf_sk_storage_get_proto;
7664 case BPF_FUNC_sk_storage_delete:
7665 return &bpf_sk_storage_delete_proto;
7666 case BPF_FUNC_get_netns_cookie:
7667 return &bpf_get_netns_cookie_sock_ops_proto;
7668 #ifdef CONFIG_INET
7669 case BPF_FUNC_load_hdr_opt:
7670 return &bpf_sock_ops_load_hdr_opt_proto;
7671 case BPF_FUNC_store_hdr_opt:
7672 return &bpf_sock_ops_store_hdr_opt_proto;
7673 case BPF_FUNC_reserve_hdr_opt:
7674 return &bpf_sock_ops_reserve_hdr_opt_proto;
7675 case BPF_FUNC_tcp_sock:
7676 return &bpf_tcp_sock_proto;
7677 #endif /* CONFIG_INET */
7678 default:
7679 return bpf_sk_base_func_proto(func_id);
7680 }
7681 }
7682
7683 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
7684 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
7685
7686 static const struct bpf_func_proto *
sk_msg_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7687 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7688 {
7689 switch (func_id) {
7690 case BPF_FUNC_msg_redirect_map:
7691 return &bpf_msg_redirect_map_proto;
7692 case BPF_FUNC_msg_redirect_hash:
7693 return &bpf_msg_redirect_hash_proto;
7694 case BPF_FUNC_msg_apply_bytes:
7695 return &bpf_msg_apply_bytes_proto;
7696 case BPF_FUNC_msg_cork_bytes:
7697 return &bpf_msg_cork_bytes_proto;
7698 case BPF_FUNC_msg_pull_data:
7699 return &bpf_msg_pull_data_proto;
7700 case BPF_FUNC_msg_push_data:
7701 return &bpf_msg_push_data_proto;
7702 case BPF_FUNC_msg_pop_data:
7703 return &bpf_msg_pop_data_proto;
7704 case BPF_FUNC_perf_event_output:
7705 return &bpf_event_output_data_proto;
7706 case BPF_FUNC_get_current_uid_gid:
7707 return &bpf_get_current_uid_gid_proto;
7708 case BPF_FUNC_get_current_pid_tgid:
7709 return &bpf_get_current_pid_tgid_proto;
7710 case BPF_FUNC_sk_storage_get:
7711 return &bpf_sk_storage_get_proto;
7712 case BPF_FUNC_sk_storage_delete:
7713 return &bpf_sk_storage_delete_proto;
7714 case BPF_FUNC_get_netns_cookie:
7715 return &bpf_get_netns_cookie_sk_msg_proto;
7716 #ifdef CONFIG_CGROUPS
7717 case BPF_FUNC_get_current_cgroup_id:
7718 return &bpf_get_current_cgroup_id_proto;
7719 case BPF_FUNC_get_current_ancestor_cgroup_id:
7720 return &bpf_get_current_ancestor_cgroup_id_proto;
7721 #endif
7722 #ifdef CONFIG_CGROUP_NET_CLASSID
7723 case BPF_FUNC_get_cgroup_classid:
7724 return &bpf_get_cgroup_classid_curr_proto;
7725 #endif
7726 default:
7727 return bpf_sk_base_func_proto(func_id);
7728 }
7729 }
7730
7731 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
7732 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
7733
7734 static const struct bpf_func_proto *
sk_skb_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7735 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7736 {
7737 switch (func_id) {
7738 case BPF_FUNC_skb_store_bytes:
7739 return &bpf_skb_store_bytes_proto;
7740 case BPF_FUNC_skb_load_bytes:
7741 return &bpf_skb_load_bytes_proto;
7742 case BPF_FUNC_skb_pull_data:
7743 return &sk_skb_pull_data_proto;
7744 case BPF_FUNC_skb_change_tail:
7745 return &sk_skb_change_tail_proto;
7746 case BPF_FUNC_skb_change_head:
7747 return &sk_skb_change_head_proto;
7748 case BPF_FUNC_skb_adjust_room:
7749 return &sk_skb_adjust_room_proto;
7750 case BPF_FUNC_get_socket_cookie:
7751 return &bpf_get_socket_cookie_proto;
7752 case BPF_FUNC_get_socket_uid:
7753 return &bpf_get_socket_uid_proto;
7754 case BPF_FUNC_sk_redirect_map:
7755 return &bpf_sk_redirect_map_proto;
7756 case BPF_FUNC_sk_redirect_hash:
7757 return &bpf_sk_redirect_hash_proto;
7758 case BPF_FUNC_perf_event_output:
7759 return &bpf_skb_event_output_proto;
7760 #ifdef CONFIG_INET
7761 case BPF_FUNC_sk_lookup_tcp:
7762 return &bpf_sk_lookup_tcp_proto;
7763 case BPF_FUNC_sk_lookup_udp:
7764 return &bpf_sk_lookup_udp_proto;
7765 case BPF_FUNC_sk_release:
7766 return &bpf_sk_release_proto;
7767 case BPF_FUNC_skc_lookup_tcp:
7768 return &bpf_skc_lookup_tcp_proto;
7769 #endif
7770 default:
7771 return bpf_sk_base_func_proto(func_id);
7772 }
7773 }
7774
7775 static const struct bpf_func_proto *
flow_dissector_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7776 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7777 {
7778 switch (func_id) {
7779 case BPF_FUNC_skb_load_bytes:
7780 return &bpf_flow_dissector_load_bytes_proto;
7781 default:
7782 return bpf_sk_base_func_proto(func_id);
7783 }
7784 }
7785
7786 static const struct bpf_func_proto *
lwt_out_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7787 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7788 {
7789 switch (func_id) {
7790 case BPF_FUNC_skb_load_bytes:
7791 return &bpf_skb_load_bytes_proto;
7792 case BPF_FUNC_skb_pull_data:
7793 return &bpf_skb_pull_data_proto;
7794 case BPF_FUNC_csum_diff:
7795 return &bpf_csum_diff_proto;
7796 case BPF_FUNC_get_cgroup_classid:
7797 return &bpf_get_cgroup_classid_proto;
7798 case BPF_FUNC_get_route_realm:
7799 return &bpf_get_route_realm_proto;
7800 case BPF_FUNC_get_hash_recalc:
7801 return &bpf_get_hash_recalc_proto;
7802 case BPF_FUNC_perf_event_output:
7803 return &bpf_skb_event_output_proto;
7804 case BPF_FUNC_get_smp_processor_id:
7805 return &bpf_get_smp_processor_id_proto;
7806 case BPF_FUNC_skb_under_cgroup:
7807 return &bpf_skb_under_cgroup_proto;
7808 default:
7809 return bpf_sk_base_func_proto(func_id);
7810 }
7811 }
7812
7813 static const struct bpf_func_proto *
lwt_in_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7814 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7815 {
7816 switch (func_id) {
7817 case BPF_FUNC_lwt_push_encap:
7818 return &bpf_lwt_in_push_encap_proto;
7819 default:
7820 return lwt_out_func_proto(func_id, prog);
7821 }
7822 }
7823
7824 static const struct bpf_func_proto *
lwt_xmit_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7825 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7826 {
7827 switch (func_id) {
7828 case BPF_FUNC_skb_get_tunnel_key:
7829 return &bpf_skb_get_tunnel_key_proto;
7830 case BPF_FUNC_skb_set_tunnel_key:
7831 return bpf_get_skb_set_tunnel_proto(func_id);
7832 case BPF_FUNC_skb_get_tunnel_opt:
7833 return &bpf_skb_get_tunnel_opt_proto;
7834 case BPF_FUNC_skb_set_tunnel_opt:
7835 return bpf_get_skb_set_tunnel_proto(func_id);
7836 case BPF_FUNC_redirect:
7837 return &bpf_redirect_proto;
7838 case BPF_FUNC_clone_redirect:
7839 return &bpf_clone_redirect_proto;
7840 case BPF_FUNC_skb_change_tail:
7841 return &bpf_skb_change_tail_proto;
7842 case BPF_FUNC_skb_change_head:
7843 return &bpf_skb_change_head_proto;
7844 case BPF_FUNC_skb_store_bytes:
7845 return &bpf_skb_store_bytes_proto;
7846 case BPF_FUNC_csum_update:
7847 return &bpf_csum_update_proto;
7848 case BPF_FUNC_csum_level:
7849 return &bpf_csum_level_proto;
7850 case BPF_FUNC_l3_csum_replace:
7851 return &bpf_l3_csum_replace_proto;
7852 case BPF_FUNC_l4_csum_replace:
7853 return &bpf_l4_csum_replace_proto;
7854 case BPF_FUNC_set_hash_invalid:
7855 return &bpf_set_hash_invalid_proto;
7856 case BPF_FUNC_lwt_push_encap:
7857 return &bpf_lwt_xmit_push_encap_proto;
7858 default:
7859 return lwt_out_func_proto(func_id, prog);
7860 }
7861 }
7862
7863 static const struct bpf_func_proto *
lwt_seg6local_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)7864 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7865 {
7866 switch (func_id) {
7867 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
7868 case BPF_FUNC_lwt_seg6_store_bytes:
7869 return &bpf_lwt_seg6_store_bytes_proto;
7870 case BPF_FUNC_lwt_seg6_action:
7871 return &bpf_lwt_seg6_action_proto;
7872 case BPF_FUNC_lwt_seg6_adjust_srh:
7873 return &bpf_lwt_seg6_adjust_srh_proto;
7874 #endif
7875 default:
7876 return lwt_out_func_proto(func_id, prog);
7877 }
7878 }
7879
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)7880 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
7881 const struct bpf_prog *prog,
7882 struct bpf_insn_access_aux *info)
7883 {
7884 const int size_default = sizeof(__u32);
7885
7886 if (off < 0 || off >= sizeof(struct __sk_buff))
7887 return false;
7888
7889 /* The verifier guarantees that size > 0. */
7890 if (off % size != 0)
7891 return false;
7892
7893 switch (off) {
7894 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7895 if (off + size > offsetofend(struct __sk_buff, cb[4]))
7896 return false;
7897 break;
7898 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
7899 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
7900 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
7901 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
7902 case bpf_ctx_range(struct __sk_buff, data):
7903 case bpf_ctx_range(struct __sk_buff, data_meta):
7904 case bpf_ctx_range(struct __sk_buff, data_end):
7905 if (size != size_default)
7906 return false;
7907 break;
7908 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
7909 return false;
7910 case bpf_ctx_range(struct __sk_buff, tstamp):
7911 if (size != sizeof(__u64))
7912 return false;
7913 break;
7914 case offsetof(struct __sk_buff, sk):
7915 if (type == BPF_WRITE || size != sizeof(__u64))
7916 return false;
7917 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
7918 break;
7919 default:
7920 /* Only narrow read access allowed for now. */
7921 if (type == BPF_WRITE) {
7922 if (size != size_default)
7923 return false;
7924 } else {
7925 bpf_ctx_record_field_size(info, size_default);
7926 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
7927 return false;
7928 }
7929 }
7930
7931 return true;
7932 }
7933
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)7934 static bool sk_filter_is_valid_access(int off, int size,
7935 enum bpf_access_type type,
7936 const struct bpf_prog *prog,
7937 struct bpf_insn_access_aux *info)
7938 {
7939 switch (off) {
7940 case bpf_ctx_range(struct __sk_buff, tc_classid):
7941 case bpf_ctx_range(struct __sk_buff, data):
7942 case bpf_ctx_range(struct __sk_buff, data_meta):
7943 case bpf_ctx_range(struct __sk_buff, data_end):
7944 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
7945 case bpf_ctx_range(struct __sk_buff, tstamp):
7946 case bpf_ctx_range(struct __sk_buff, wire_len):
7947 return false;
7948 }
7949
7950 if (type == BPF_WRITE) {
7951 switch (off) {
7952 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7953 break;
7954 default:
7955 return false;
7956 }
7957 }
7958
7959 return bpf_skb_is_valid_access(off, size, type, prog, info);
7960 }
7961
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)7962 static bool cg_skb_is_valid_access(int off, int size,
7963 enum bpf_access_type type,
7964 const struct bpf_prog *prog,
7965 struct bpf_insn_access_aux *info)
7966 {
7967 switch (off) {
7968 case bpf_ctx_range(struct __sk_buff, tc_classid):
7969 case bpf_ctx_range(struct __sk_buff, data_meta):
7970 case bpf_ctx_range(struct __sk_buff, wire_len):
7971 return false;
7972 case bpf_ctx_range(struct __sk_buff, data):
7973 case bpf_ctx_range(struct __sk_buff, data_end):
7974 if (!bpf_capable())
7975 return false;
7976 break;
7977 }
7978
7979 if (type == BPF_WRITE) {
7980 switch (off) {
7981 case bpf_ctx_range(struct __sk_buff, mark):
7982 case bpf_ctx_range(struct __sk_buff, priority):
7983 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7984 break;
7985 case bpf_ctx_range(struct __sk_buff, tstamp):
7986 if (!bpf_capable())
7987 return false;
7988 break;
7989 default:
7990 return false;
7991 }
7992 }
7993
7994 switch (off) {
7995 case bpf_ctx_range(struct __sk_buff, data):
7996 info->reg_type = PTR_TO_PACKET;
7997 break;
7998 case bpf_ctx_range(struct __sk_buff, data_end):
7999 info->reg_type = PTR_TO_PACKET_END;
8000 break;
8001 }
8002
8003 return bpf_skb_is_valid_access(off, size, type, prog, info);
8004 }
8005
lwt_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8006 static bool lwt_is_valid_access(int off, int size,
8007 enum bpf_access_type type,
8008 const struct bpf_prog *prog,
8009 struct bpf_insn_access_aux *info)
8010 {
8011 switch (off) {
8012 case bpf_ctx_range(struct __sk_buff, tc_classid):
8013 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8014 case bpf_ctx_range(struct __sk_buff, data_meta):
8015 case bpf_ctx_range(struct __sk_buff, tstamp):
8016 case bpf_ctx_range(struct __sk_buff, wire_len):
8017 return false;
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 default:
8027 return false;
8028 }
8029 }
8030
8031 switch (off) {
8032 case bpf_ctx_range(struct __sk_buff, data):
8033 info->reg_type = PTR_TO_PACKET;
8034 break;
8035 case bpf_ctx_range(struct __sk_buff, data_end):
8036 info->reg_type = PTR_TO_PACKET_END;
8037 break;
8038 }
8039
8040 return bpf_skb_is_valid_access(off, size, type, prog, info);
8041 }
8042
8043 /* Attach type specific accesses */
__sock_filter_check_attach_type(int off,enum bpf_access_type access_type,enum bpf_attach_type attach_type)8044 static bool __sock_filter_check_attach_type(int off,
8045 enum bpf_access_type access_type,
8046 enum bpf_attach_type attach_type)
8047 {
8048 switch (off) {
8049 case offsetof(struct bpf_sock, bound_dev_if):
8050 case offsetof(struct bpf_sock, mark):
8051 case offsetof(struct bpf_sock, priority):
8052 switch (attach_type) {
8053 case BPF_CGROUP_INET_SOCK_CREATE:
8054 case BPF_CGROUP_INET_SOCK_RELEASE:
8055 goto full_access;
8056 default:
8057 return false;
8058 }
8059 case bpf_ctx_range(struct bpf_sock, src_ip4):
8060 switch (attach_type) {
8061 case BPF_CGROUP_INET4_POST_BIND:
8062 goto read_only;
8063 default:
8064 return false;
8065 }
8066 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8067 switch (attach_type) {
8068 case BPF_CGROUP_INET6_POST_BIND:
8069 goto read_only;
8070 default:
8071 return false;
8072 }
8073 case bpf_ctx_range(struct bpf_sock, src_port):
8074 switch (attach_type) {
8075 case BPF_CGROUP_INET4_POST_BIND:
8076 case BPF_CGROUP_INET6_POST_BIND:
8077 goto read_only;
8078 default:
8079 return false;
8080 }
8081 }
8082 read_only:
8083 return access_type == BPF_READ;
8084 full_access:
8085 return true;
8086 }
8087
bpf_sock_common_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)8088 bool bpf_sock_common_is_valid_access(int off, int size,
8089 enum bpf_access_type type,
8090 struct bpf_insn_access_aux *info)
8091 {
8092 switch (off) {
8093 case bpf_ctx_range_till(struct bpf_sock, type, priority):
8094 return false;
8095 default:
8096 return bpf_sock_is_valid_access(off, size, type, info);
8097 }
8098 }
8099
bpf_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)8100 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
8101 struct bpf_insn_access_aux *info)
8102 {
8103 const int size_default = sizeof(__u32);
8104 int field_size;
8105
8106 if (off < 0 || off >= sizeof(struct bpf_sock))
8107 return false;
8108 if (off % size != 0)
8109 return false;
8110
8111 switch (off) {
8112 case offsetof(struct bpf_sock, state):
8113 case offsetof(struct bpf_sock, family):
8114 case offsetof(struct bpf_sock, type):
8115 case offsetof(struct bpf_sock, protocol):
8116 case offsetof(struct bpf_sock, src_port):
8117 case offsetof(struct bpf_sock, rx_queue_mapping):
8118 case bpf_ctx_range(struct bpf_sock, src_ip4):
8119 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8120 case bpf_ctx_range(struct bpf_sock, dst_ip4):
8121 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
8122 bpf_ctx_record_field_size(info, size_default);
8123 return bpf_ctx_narrow_access_ok(off, size, size_default);
8124 case bpf_ctx_range(struct bpf_sock, dst_port):
8125 field_size = size == size_default ?
8126 size_default : sizeof_field(struct bpf_sock, dst_port);
8127 bpf_ctx_record_field_size(info, field_size);
8128 return bpf_ctx_narrow_access_ok(off, size, field_size);
8129 case offsetofend(struct bpf_sock, dst_port) ...
8130 offsetof(struct bpf_sock, dst_ip4) - 1:
8131 return false;
8132 }
8133
8134 return size == size_default;
8135 }
8136
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)8137 static bool sock_filter_is_valid_access(int off, int size,
8138 enum bpf_access_type type,
8139 const struct bpf_prog *prog,
8140 struct bpf_insn_access_aux *info)
8141 {
8142 if (!bpf_sock_is_valid_access(off, size, type, info))
8143 return false;
8144 return __sock_filter_check_attach_type(off, type,
8145 prog->expected_attach_type);
8146 }
8147
bpf_noop_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)8148 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
8149 const struct bpf_prog *prog)
8150 {
8151 /* Neither direct read nor direct write requires any preliminary
8152 * action.
8153 */
8154 return 0;
8155 }
8156
bpf_unclone_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog,int drop_verdict)8157 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
8158 const struct bpf_prog *prog, int drop_verdict)
8159 {
8160 struct bpf_insn *insn = insn_buf;
8161
8162 if (!direct_write)
8163 return 0;
8164
8165 /* if (!skb->cloned)
8166 * goto start;
8167 *
8168 * (Fast-path, otherwise approximation that we might be
8169 * a clone, do the rest in helper.)
8170 */
8171 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
8172 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
8173 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
8174
8175 /* ret = bpf_skb_pull_data(skb, 0); */
8176 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
8177 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
8178 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
8179 BPF_FUNC_skb_pull_data);
8180 /* if (!ret)
8181 * goto restore;
8182 * return TC_ACT_SHOT;
8183 */
8184 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
8185 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
8186 *insn++ = BPF_EXIT_INSN();
8187
8188 /* restore: */
8189 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
8190 /* start: */
8191 *insn++ = prog->insnsi[0];
8192
8193 return insn - insn_buf;
8194 }
8195
bpf_gen_ld_abs(const struct bpf_insn * orig,struct bpf_insn * insn_buf)8196 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
8197 struct bpf_insn *insn_buf)
8198 {
8199 bool indirect = BPF_MODE(orig->code) == BPF_IND;
8200 struct bpf_insn *insn = insn_buf;
8201
8202 if (!indirect) {
8203 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
8204 } else {
8205 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
8206 if (orig->imm)
8207 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
8208 }
8209 /* We're guaranteed here that CTX is in R6. */
8210 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
8211
8212 switch (BPF_SIZE(orig->code)) {
8213 case BPF_B:
8214 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
8215 break;
8216 case BPF_H:
8217 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
8218 break;
8219 case BPF_W:
8220 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
8221 break;
8222 }
8223
8224 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
8225 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
8226 *insn++ = BPF_EXIT_INSN();
8227
8228 return insn - insn_buf;
8229 }
8230
tc_cls_act_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)8231 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
8232 const struct bpf_prog *prog)
8233 {
8234 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
8235 }
8236
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)8237 static bool tc_cls_act_is_valid_access(int off, int size,
8238 enum bpf_access_type type,
8239 const struct bpf_prog *prog,
8240 struct bpf_insn_access_aux *info)
8241 {
8242 if (type == BPF_WRITE) {
8243 switch (off) {
8244 case bpf_ctx_range(struct __sk_buff, mark):
8245 case bpf_ctx_range(struct __sk_buff, tc_index):
8246 case bpf_ctx_range(struct __sk_buff, priority):
8247 case bpf_ctx_range(struct __sk_buff, tc_classid):
8248 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8249 case bpf_ctx_range(struct __sk_buff, tstamp):
8250 case bpf_ctx_range(struct __sk_buff, queue_mapping):
8251 break;
8252 default:
8253 return false;
8254 }
8255 }
8256
8257 switch (off) {
8258 case bpf_ctx_range(struct __sk_buff, data):
8259 info->reg_type = PTR_TO_PACKET;
8260 break;
8261 case bpf_ctx_range(struct __sk_buff, data_meta):
8262 info->reg_type = PTR_TO_PACKET_META;
8263 break;
8264 case bpf_ctx_range(struct __sk_buff, data_end):
8265 info->reg_type = PTR_TO_PACKET_END;
8266 break;
8267 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8268 return false;
8269 }
8270
8271 return bpf_skb_is_valid_access(off, size, type, prog, info);
8272 }
8273
__is_valid_xdp_access(int off,int size)8274 static bool __is_valid_xdp_access(int off, int size)
8275 {
8276 if (off < 0 || off >= sizeof(struct xdp_md))
8277 return false;
8278 if (off % size != 0)
8279 return false;
8280 if (size != sizeof(__u32))
8281 return false;
8282
8283 return true;
8284 }
8285
xdp_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8286 static bool xdp_is_valid_access(int off, int size,
8287 enum bpf_access_type type,
8288 const struct bpf_prog *prog,
8289 struct bpf_insn_access_aux *info)
8290 {
8291 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
8292 switch (off) {
8293 case offsetof(struct xdp_md, egress_ifindex):
8294 return false;
8295 }
8296 }
8297
8298 if (type == BPF_WRITE) {
8299 if (bpf_prog_is_dev_bound(prog->aux)) {
8300 switch (off) {
8301 case offsetof(struct xdp_md, rx_queue_index):
8302 return __is_valid_xdp_access(off, size);
8303 }
8304 }
8305 return false;
8306 }
8307
8308 switch (off) {
8309 case offsetof(struct xdp_md, data):
8310 info->reg_type = PTR_TO_PACKET;
8311 break;
8312 case offsetof(struct xdp_md, data_meta):
8313 info->reg_type = PTR_TO_PACKET_META;
8314 break;
8315 case offsetof(struct xdp_md, data_end):
8316 info->reg_type = PTR_TO_PACKET_END;
8317 break;
8318 }
8319
8320 return __is_valid_xdp_access(off, size);
8321 }
8322
bpf_warn_invalid_xdp_action(u32 act)8323 void bpf_warn_invalid_xdp_action(u32 act)
8324 {
8325 const u32 act_max = XDP_REDIRECT;
8326
8327 pr_warn_once("%s XDP return value %u, expect packet loss!\n",
8328 act > act_max ? "Illegal" : "Driver unsupported",
8329 act);
8330 }
8331 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
8332
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)8333 static bool sock_addr_is_valid_access(int off, int size,
8334 enum bpf_access_type type,
8335 const struct bpf_prog *prog,
8336 struct bpf_insn_access_aux *info)
8337 {
8338 const int size_default = sizeof(__u32);
8339
8340 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
8341 return false;
8342 if (off % size != 0)
8343 return false;
8344
8345 /* Disallow access to IPv6 fields from IPv4 contex and vise
8346 * versa.
8347 */
8348 switch (off) {
8349 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8350 switch (prog->expected_attach_type) {
8351 case BPF_CGROUP_INET4_BIND:
8352 case BPF_CGROUP_INET4_CONNECT:
8353 case BPF_CGROUP_INET4_GETPEERNAME:
8354 case BPF_CGROUP_INET4_GETSOCKNAME:
8355 case BPF_CGROUP_UDP4_SENDMSG:
8356 case BPF_CGROUP_UDP4_RECVMSG:
8357 break;
8358 default:
8359 return false;
8360 }
8361 break;
8362 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8363 switch (prog->expected_attach_type) {
8364 case BPF_CGROUP_INET6_BIND:
8365 case BPF_CGROUP_INET6_CONNECT:
8366 case BPF_CGROUP_INET6_GETPEERNAME:
8367 case BPF_CGROUP_INET6_GETSOCKNAME:
8368 case BPF_CGROUP_UDP6_SENDMSG:
8369 case BPF_CGROUP_UDP6_RECVMSG:
8370 break;
8371 default:
8372 return false;
8373 }
8374 break;
8375 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8376 switch (prog->expected_attach_type) {
8377 case BPF_CGROUP_UDP4_SENDMSG:
8378 break;
8379 default:
8380 return false;
8381 }
8382 break;
8383 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8384 msg_src_ip6[3]):
8385 switch (prog->expected_attach_type) {
8386 case BPF_CGROUP_UDP6_SENDMSG:
8387 break;
8388 default:
8389 return false;
8390 }
8391 break;
8392 }
8393
8394 switch (off) {
8395 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8396 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8397 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8398 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8399 msg_src_ip6[3]):
8400 case bpf_ctx_range(struct bpf_sock_addr, user_port):
8401 if (type == BPF_READ) {
8402 bpf_ctx_record_field_size(info, size_default);
8403
8404 if (bpf_ctx_wide_access_ok(off, size,
8405 struct bpf_sock_addr,
8406 user_ip6))
8407 return true;
8408
8409 if (bpf_ctx_wide_access_ok(off, size,
8410 struct bpf_sock_addr,
8411 msg_src_ip6))
8412 return true;
8413
8414 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8415 return false;
8416 } else {
8417 if (bpf_ctx_wide_access_ok(off, size,
8418 struct bpf_sock_addr,
8419 user_ip6))
8420 return true;
8421
8422 if (bpf_ctx_wide_access_ok(off, size,
8423 struct bpf_sock_addr,
8424 msg_src_ip6))
8425 return true;
8426
8427 if (size != size_default)
8428 return false;
8429 }
8430 break;
8431 case offsetof(struct bpf_sock_addr, sk):
8432 if (type != BPF_READ)
8433 return false;
8434 if (size != sizeof(__u64))
8435 return false;
8436 info->reg_type = PTR_TO_SOCKET;
8437 break;
8438 default:
8439 if (type == BPF_READ) {
8440 if (size != size_default)
8441 return false;
8442 } else {
8443 return false;
8444 }
8445 }
8446
8447 return true;
8448 }
8449
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)8450 static bool sock_ops_is_valid_access(int off, int size,
8451 enum bpf_access_type type,
8452 const struct bpf_prog *prog,
8453 struct bpf_insn_access_aux *info)
8454 {
8455 const int size_default = sizeof(__u32);
8456
8457 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
8458 return false;
8459
8460 /* The verifier guarantees that size > 0. */
8461 if (off % size != 0)
8462 return false;
8463
8464 if (type == BPF_WRITE) {
8465 switch (off) {
8466 case offsetof(struct bpf_sock_ops, reply):
8467 case offsetof(struct bpf_sock_ops, sk_txhash):
8468 if (size != size_default)
8469 return false;
8470 break;
8471 default:
8472 return false;
8473 }
8474 } else {
8475 switch (off) {
8476 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
8477 bytes_acked):
8478 if (size != sizeof(__u64))
8479 return false;
8480 break;
8481 case offsetof(struct bpf_sock_ops, sk):
8482 if (size != sizeof(__u64))
8483 return false;
8484 info->reg_type = PTR_TO_SOCKET_OR_NULL;
8485 break;
8486 case offsetof(struct bpf_sock_ops, skb_data):
8487 if (size != sizeof(__u64))
8488 return false;
8489 info->reg_type = PTR_TO_PACKET;
8490 break;
8491 case offsetof(struct bpf_sock_ops, skb_data_end):
8492 if (size != sizeof(__u64))
8493 return false;
8494 info->reg_type = PTR_TO_PACKET_END;
8495 break;
8496 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
8497 bpf_ctx_record_field_size(info, size_default);
8498 return bpf_ctx_narrow_access_ok(off, size,
8499 size_default);
8500 default:
8501 if (size != size_default)
8502 return false;
8503 break;
8504 }
8505 }
8506
8507 return true;
8508 }
8509
sk_skb_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)8510 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
8511 const struct bpf_prog *prog)
8512 {
8513 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
8514 }
8515
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)8516 static bool sk_skb_is_valid_access(int off, int size,
8517 enum bpf_access_type type,
8518 const struct bpf_prog *prog,
8519 struct bpf_insn_access_aux *info)
8520 {
8521 switch (off) {
8522 case bpf_ctx_range(struct __sk_buff, tc_classid):
8523 case bpf_ctx_range(struct __sk_buff, data_meta):
8524 case bpf_ctx_range(struct __sk_buff, tstamp):
8525 case bpf_ctx_range(struct __sk_buff, wire_len):
8526 return false;
8527 }
8528
8529 if (type == BPF_WRITE) {
8530 switch (off) {
8531 case bpf_ctx_range(struct __sk_buff, tc_index):
8532 case bpf_ctx_range(struct __sk_buff, priority):
8533 break;
8534 default:
8535 return false;
8536 }
8537 }
8538
8539 switch (off) {
8540 case bpf_ctx_range(struct __sk_buff, mark):
8541 return false;
8542 case bpf_ctx_range(struct __sk_buff, data):
8543 info->reg_type = PTR_TO_PACKET;
8544 break;
8545 case bpf_ctx_range(struct __sk_buff, data_end):
8546 info->reg_type = PTR_TO_PACKET_END;
8547 break;
8548 }
8549
8550 return bpf_skb_is_valid_access(off, size, type, prog, info);
8551 }
8552
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)8553 static bool sk_msg_is_valid_access(int off, int size,
8554 enum bpf_access_type type,
8555 const struct bpf_prog *prog,
8556 struct bpf_insn_access_aux *info)
8557 {
8558 if (type == BPF_WRITE)
8559 return false;
8560
8561 if (off % size != 0)
8562 return false;
8563
8564 switch (off) {
8565 case offsetof(struct sk_msg_md, data):
8566 info->reg_type = PTR_TO_PACKET;
8567 if (size != sizeof(__u64))
8568 return false;
8569 break;
8570 case offsetof(struct sk_msg_md, data_end):
8571 info->reg_type = PTR_TO_PACKET_END;
8572 if (size != sizeof(__u64))
8573 return false;
8574 break;
8575 case offsetof(struct sk_msg_md, sk):
8576 if (size != sizeof(__u64))
8577 return false;
8578 info->reg_type = PTR_TO_SOCKET;
8579 break;
8580 case bpf_ctx_range(struct sk_msg_md, family):
8581 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
8582 case bpf_ctx_range(struct sk_msg_md, local_ip4):
8583 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
8584 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
8585 case bpf_ctx_range(struct sk_msg_md, remote_port):
8586 case bpf_ctx_range(struct sk_msg_md, local_port):
8587 case bpf_ctx_range(struct sk_msg_md, size):
8588 if (size != sizeof(__u32))
8589 return false;
8590 break;
8591 default:
8592 return false;
8593 }
8594 return true;
8595 }
8596
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)8597 static bool flow_dissector_is_valid_access(int off, int size,
8598 enum bpf_access_type type,
8599 const struct bpf_prog *prog,
8600 struct bpf_insn_access_aux *info)
8601 {
8602 const int size_default = sizeof(__u32);
8603
8604 if (off < 0 || off >= sizeof(struct __sk_buff))
8605 return false;
8606
8607 if (type == BPF_WRITE)
8608 return false;
8609
8610 switch (off) {
8611 case bpf_ctx_range(struct __sk_buff, data):
8612 if (size != size_default)
8613 return false;
8614 info->reg_type = PTR_TO_PACKET;
8615 return true;
8616 case bpf_ctx_range(struct __sk_buff, data_end):
8617 if (size != size_default)
8618 return false;
8619 info->reg_type = PTR_TO_PACKET_END;
8620 return true;
8621 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
8622 if (size != sizeof(__u64))
8623 return false;
8624 info->reg_type = PTR_TO_FLOW_KEYS;
8625 return true;
8626 default:
8627 return false;
8628 }
8629 }
8630
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)8631 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
8632 const struct bpf_insn *si,
8633 struct bpf_insn *insn_buf,
8634 struct bpf_prog *prog,
8635 u32 *target_size)
8636
8637 {
8638 struct bpf_insn *insn = insn_buf;
8639
8640 switch (si->off) {
8641 case offsetof(struct __sk_buff, data):
8642 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
8643 si->dst_reg, si->src_reg,
8644 offsetof(struct bpf_flow_dissector, data));
8645 break;
8646
8647 case offsetof(struct __sk_buff, data_end):
8648 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
8649 si->dst_reg, si->src_reg,
8650 offsetof(struct bpf_flow_dissector, data_end));
8651 break;
8652
8653 case offsetof(struct __sk_buff, flow_keys):
8654 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
8655 si->dst_reg, si->src_reg,
8656 offsetof(struct bpf_flow_dissector, flow_keys));
8657 break;
8658 }
8659
8660 return insn - insn_buf;
8661 }
8662
bpf_convert_shinfo_access(const struct bpf_insn * si,struct bpf_insn * insn)8663 static struct bpf_insn *bpf_convert_shinfo_access(const struct bpf_insn *si,
8664 struct bpf_insn *insn)
8665 {
8666 /* si->dst_reg = skb_shinfo(SKB); */
8667 #ifdef NET_SKBUFF_DATA_USES_OFFSET
8668 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
8669 BPF_REG_AX, si->src_reg,
8670 offsetof(struct sk_buff, end));
8671 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
8672 si->dst_reg, si->src_reg,
8673 offsetof(struct sk_buff, head));
8674 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
8675 #else
8676 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
8677 si->dst_reg, si->src_reg,
8678 offsetof(struct sk_buff, end));
8679 #endif
8680
8681 return insn;
8682 }
8683
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)8684 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
8685 const struct bpf_insn *si,
8686 struct bpf_insn *insn_buf,
8687 struct bpf_prog *prog, u32 *target_size)
8688 {
8689 struct bpf_insn *insn = insn_buf;
8690 int off;
8691
8692 switch (si->off) {
8693 case offsetof(struct __sk_buff, len):
8694 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8695 bpf_target_off(struct sk_buff, len, 4,
8696 target_size));
8697 break;
8698
8699 case offsetof(struct __sk_buff, protocol):
8700 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8701 bpf_target_off(struct sk_buff, protocol, 2,
8702 target_size));
8703 break;
8704
8705 case offsetof(struct __sk_buff, vlan_proto):
8706 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8707 bpf_target_off(struct sk_buff, vlan_proto, 2,
8708 target_size));
8709 break;
8710
8711 case offsetof(struct __sk_buff, priority):
8712 if (type == BPF_WRITE)
8713 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8714 bpf_target_off(struct sk_buff, priority, 4,
8715 target_size));
8716 else
8717 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8718 bpf_target_off(struct sk_buff, priority, 4,
8719 target_size));
8720 break;
8721
8722 case offsetof(struct __sk_buff, ingress_ifindex):
8723 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8724 bpf_target_off(struct sk_buff, skb_iif, 4,
8725 target_size));
8726 break;
8727
8728 case offsetof(struct __sk_buff, ifindex):
8729 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
8730 si->dst_reg, si->src_reg,
8731 offsetof(struct sk_buff, dev));
8732 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
8733 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8734 bpf_target_off(struct net_device, ifindex, 4,
8735 target_size));
8736 break;
8737
8738 case offsetof(struct __sk_buff, hash):
8739 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8740 bpf_target_off(struct sk_buff, hash, 4,
8741 target_size));
8742 break;
8743
8744 case offsetof(struct __sk_buff, mark):
8745 if (type == BPF_WRITE)
8746 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8747 bpf_target_off(struct sk_buff, mark, 4,
8748 target_size));
8749 else
8750 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8751 bpf_target_off(struct sk_buff, mark, 4,
8752 target_size));
8753 break;
8754
8755 case offsetof(struct __sk_buff, pkt_type):
8756 *target_size = 1;
8757 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
8758 PKT_TYPE_OFFSET());
8759 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
8760 #ifdef __BIG_ENDIAN_BITFIELD
8761 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
8762 #endif
8763 break;
8764
8765 case offsetof(struct __sk_buff, queue_mapping):
8766 if (type == BPF_WRITE) {
8767 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
8768 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
8769 bpf_target_off(struct sk_buff,
8770 queue_mapping,
8771 2, target_size));
8772 } else {
8773 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8774 bpf_target_off(struct sk_buff,
8775 queue_mapping,
8776 2, target_size));
8777 }
8778 break;
8779
8780 case offsetof(struct __sk_buff, vlan_present):
8781 *target_size = 1;
8782 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
8783 PKT_VLAN_PRESENT_OFFSET());
8784 if (PKT_VLAN_PRESENT_BIT)
8785 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
8786 if (PKT_VLAN_PRESENT_BIT < 7)
8787 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
8788 break;
8789
8790 case offsetof(struct __sk_buff, vlan_tci):
8791 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8792 bpf_target_off(struct sk_buff, vlan_tci, 2,
8793 target_size));
8794 break;
8795
8796 case offsetof(struct __sk_buff, cb[0]) ...
8797 offsetofend(struct __sk_buff, cb[4]) - 1:
8798 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
8799 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
8800 offsetof(struct qdisc_skb_cb, data)) %
8801 sizeof(__u64));
8802
8803 prog->cb_access = 1;
8804 off = si->off;
8805 off -= offsetof(struct __sk_buff, cb[0]);
8806 off += offsetof(struct sk_buff, cb);
8807 off += offsetof(struct qdisc_skb_cb, data);
8808 if (type == BPF_WRITE)
8809 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
8810 si->src_reg, off);
8811 else
8812 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
8813 si->src_reg, off);
8814 break;
8815
8816 case offsetof(struct __sk_buff, tc_classid):
8817 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
8818
8819 off = si->off;
8820 off -= offsetof(struct __sk_buff, tc_classid);
8821 off += offsetof(struct sk_buff, cb);
8822 off += offsetof(struct qdisc_skb_cb, tc_classid);
8823 *target_size = 2;
8824 if (type == BPF_WRITE)
8825 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
8826 si->src_reg, off);
8827 else
8828 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
8829 si->src_reg, off);
8830 break;
8831
8832 case offsetof(struct __sk_buff, data):
8833 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
8834 si->dst_reg, si->src_reg,
8835 offsetof(struct sk_buff, data));
8836 break;
8837
8838 case offsetof(struct __sk_buff, data_meta):
8839 off = si->off;
8840 off -= offsetof(struct __sk_buff, data_meta);
8841 off += offsetof(struct sk_buff, cb);
8842 off += offsetof(struct bpf_skb_data_end, data_meta);
8843 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
8844 si->src_reg, off);
8845 break;
8846
8847 case offsetof(struct __sk_buff, data_end):
8848 off = si->off;
8849 off -= offsetof(struct __sk_buff, data_end);
8850 off += offsetof(struct sk_buff, cb);
8851 off += offsetof(struct bpf_skb_data_end, data_end);
8852 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
8853 si->src_reg, off);
8854 break;
8855
8856 case offsetof(struct __sk_buff, tc_index):
8857 #ifdef CONFIG_NET_SCHED
8858 if (type == BPF_WRITE)
8859 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
8860 bpf_target_off(struct sk_buff, tc_index, 2,
8861 target_size));
8862 else
8863 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8864 bpf_target_off(struct sk_buff, tc_index, 2,
8865 target_size));
8866 #else
8867 *target_size = 2;
8868 if (type == BPF_WRITE)
8869 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
8870 else
8871 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
8872 #endif
8873 break;
8874
8875 case offsetof(struct __sk_buff, napi_id):
8876 #if defined(CONFIG_NET_RX_BUSY_POLL)
8877 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8878 bpf_target_off(struct sk_buff, napi_id, 4,
8879 target_size));
8880 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
8881 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
8882 #else
8883 *target_size = 4;
8884 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
8885 #endif
8886 break;
8887 case offsetof(struct __sk_buff, family):
8888 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
8889
8890 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8891 si->dst_reg, si->src_reg,
8892 offsetof(struct sk_buff, sk));
8893 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8894 bpf_target_off(struct sock_common,
8895 skc_family,
8896 2, target_size));
8897 break;
8898 case offsetof(struct __sk_buff, remote_ip4):
8899 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
8900
8901 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8902 si->dst_reg, si->src_reg,
8903 offsetof(struct sk_buff, sk));
8904 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8905 bpf_target_off(struct sock_common,
8906 skc_daddr,
8907 4, target_size));
8908 break;
8909 case offsetof(struct __sk_buff, local_ip4):
8910 BUILD_BUG_ON(sizeof_field(struct sock_common,
8911 skc_rcv_saddr) != 4);
8912
8913 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8914 si->dst_reg, si->src_reg,
8915 offsetof(struct sk_buff, sk));
8916 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8917 bpf_target_off(struct sock_common,
8918 skc_rcv_saddr,
8919 4, target_size));
8920 break;
8921 case offsetof(struct __sk_buff, remote_ip6[0]) ...
8922 offsetof(struct __sk_buff, remote_ip6[3]):
8923 #if IS_ENABLED(CONFIG_IPV6)
8924 BUILD_BUG_ON(sizeof_field(struct sock_common,
8925 skc_v6_daddr.s6_addr32[0]) != 4);
8926
8927 off = si->off;
8928 off -= offsetof(struct __sk_buff, remote_ip6[0]);
8929
8930 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8931 si->dst_reg, si->src_reg,
8932 offsetof(struct sk_buff, sk));
8933 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8934 offsetof(struct sock_common,
8935 skc_v6_daddr.s6_addr32[0]) +
8936 off);
8937 #else
8938 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8939 #endif
8940 break;
8941 case offsetof(struct __sk_buff, local_ip6[0]) ...
8942 offsetof(struct __sk_buff, local_ip6[3]):
8943 #if IS_ENABLED(CONFIG_IPV6)
8944 BUILD_BUG_ON(sizeof_field(struct sock_common,
8945 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8946
8947 off = si->off;
8948 off -= offsetof(struct __sk_buff, local_ip6[0]);
8949
8950 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8951 si->dst_reg, si->src_reg,
8952 offsetof(struct sk_buff, sk));
8953 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8954 offsetof(struct sock_common,
8955 skc_v6_rcv_saddr.s6_addr32[0]) +
8956 off);
8957 #else
8958 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8959 #endif
8960 break;
8961
8962 case offsetof(struct __sk_buff, remote_port):
8963 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
8964
8965 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8966 si->dst_reg, si->src_reg,
8967 offsetof(struct sk_buff, sk));
8968 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8969 bpf_target_off(struct sock_common,
8970 skc_dport,
8971 2, target_size));
8972 #ifndef __BIG_ENDIAN_BITFIELD
8973 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8974 #endif
8975 break;
8976
8977 case offsetof(struct __sk_buff, local_port):
8978 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
8979
8980 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8981 si->dst_reg, si->src_reg,
8982 offsetof(struct sk_buff, sk));
8983 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8984 bpf_target_off(struct sock_common,
8985 skc_num, 2, target_size));
8986 break;
8987
8988 case offsetof(struct __sk_buff, tstamp):
8989 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
8990
8991 if (type == BPF_WRITE)
8992 *insn++ = BPF_STX_MEM(BPF_DW,
8993 si->dst_reg, si->src_reg,
8994 bpf_target_off(struct sk_buff,
8995 tstamp, 8,
8996 target_size));
8997 else
8998 *insn++ = BPF_LDX_MEM(BPF_DW,
8999 si->dst_reg, si->src_reg,
9000 bpf_target_off(struct sk_buff,
9001 tstamp, 8,
9002 target_size));
9003 break;
9004
9005 case offsetof(struct __sk_buff, gso_segs):
9006 insn = bpf_convert_shinfo_access(si, insn);
9007 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
9008 si->dst_reg, si->dst_reg,
9009 bpf_target_off(struct skb_shared_info,
9010 gso_segs, 2,
9011 target_size));
9012 break;
9013 case offsetof(struct __sk_buff, gso_size):
9014 insn = bpf_convert_shinfo_access(si, insn);
9015 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
9016 si->dst_reg, si->dst_reg,
9017 bpf_target_off(struct skb_shared_info,
9018 gso_size, 2,
9019 target_size));
9020 break;
9021 case offsetof(struct __sk_buff, wire_len):
9022 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
9023
9024 off = si->off;
9025 off -= offsetof(struct __sk_buff, wire_len);
9026 off += offsetof(struct sk_buff, cb);
9027 off += offsetof(struct qdisc_skb_cb, pkt_len);
9028 *target_size = 4;
9029 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
9030 break;
9031
9032 case offsetof(struct __sk_buff, sk):
9033 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9034 si->dst_reg, si->src_reg,
9035 offsetof(struct sk_buff, sk));
9036 break;
9037 }
9038
9039 return insn - insn_buf;
9040 }
9041
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)9042 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
9043 const struct bpf_insn *si,
9044 struct bpf_insn *insn_buf,
9045 struct bpf_prog *prog, u32 *target_size)
9046 {
9047 struct bpf_insn *insn = insn_buf;
9048 int off;
9049
9050 switch (si->off) {
9051 case offsetof(struct bpf_sock, bound_dev_if):
9052 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
9053
9054 if (type == BPF_WRITE)
9055 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9056 offsetof(struct sock, sk_bound_dev_if));
9057 else
9058 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9059 offsetof(struct sock, sk_bound_dev_if));
9060 break;
9061
9062 case offsetof(struct bpf_sock, mark):
9063 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
9064
9065 if (type == BPF_WRITE)
9066 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9067 offsetof(struct sock, sk_mark));
9068 else
9069 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9070 offsetof(struct sock, sk_mark));
9071 break;
9072
9073 case offsetof(struct bpf_sock, priority):
9074 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
9075
9076 if (type == BPF_WRITE)
9077 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9078 offsetof(struct sock, sk_priority));
9079 else
9080 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9081 offsetof(struct sock, sk_priority));
9082 break;
9083
9084 case offsetof(struct bpf_sock, family):
9085 *insn++ = BPF_LDX_MEM(
9086 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
9087 si->dst_reg, si->src_reg,
9088 bpf_target_off(struct sock_common,
9089 skc_family,
9090 sizeof_field(struct sock_common,
9091 skc_family),
9092 target_size));
9093 break;
9094
9095 case offsetof(struct bpf_sock, type):
9096 *insn++ = BPF_LDX_MEM(
9097 BPF_FIELD_SIZEOF(struct sock, sk_type),
9098 si->dst_reg, si->src_reg,
9099 bpf_target_off(struct sock, sk_type,
9100 sizeof_field(struct sock, sk_type),
9101 target_size));
9102 break;
9103
9104 case offsetof(struct bpf_sock, protocol):
9105 *insn++ = BPF_LDX_MEM(
9106 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
9107 si->dst_reg, si->src_reg,
9108 bpf_target_off(struct sock, sk_protocol,
9109 sizeof_field(struct sock, sk_protocol),
9110 target_size));
9111 break;
9112
9113 case offsetof(struct bpf_sock, src_ip4):
9114 *insn++ = BPF_LDX_MEM(
9115 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9116 bpf_target_off(struct sock_common, skc_rcv_saddr,
9117 sizeof_field(struct sock_common,
9118 skc_rcv_saddr),
9119 target_size));
9120 break;
9121
9122 case offsetof(struct bpf_sock, dst_ip4):
9123 *insn++ = BPF_LDX_MEM(
9124 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9125 bpf_target_off(struct sock_common, skc_daddr,
9126 sizeof_field(struct sock_common,
9127 skc_daddr),
9128 target_size));
9129 break;
9130
9131 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
9132 #if IS_ENABLED(CONFIG_IPV6)
9133 off = si->off;
9134 off -= offsetof(struct bpf_sock, src_ip6[0]);
9135 *insn++ = BPF_LDX_MEM(
9136 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9137 bpf_target_off(
9138 struct sock_common,
9139 skc_v6_rcv_saddr.s6_addr32[0],
9140 sizeof_field(struct sock_common,
9141 skc_v6_rcv_saddr.s6_addr32[0]),
9142 target_size) + off);
9143 #else
9144 (void)off;
9145 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9146 #endif
9147 break;
9148
9149 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
9150 #if IS_ENABLED(CONFIG_IPV6)
9151 off = si->off;
9152 off -= offsetof(struct bpf_sock, dst_ip6[0]);
9153 *insn++ = BPF_LDX_MEM(
9154 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9155 bpf_target_off(struct sock_common,
9156 skc_v6_daddr.s6_addr32[0],
9157 sizeof_field(struct sock_common,
9158 skc_v6_daddr.s6_addr32[0]),
9159 target_size) + off);
9160 #else
9161 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9162 *target_size = 4;
9163 #endif
9164 break;
9165
9166 case offsetof(struct bpf_sock, src_port):
9167 *insn++ = BPF_LDX_MEM(
9168 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
9169 si->dst_reg, si->src_reg,
9170 bpf_target_off(struct sock_common, skc_num,
9171 sizeof_field(struct sock_common,
9172 skc_num),
9173 target_size));
9174 break;
9175
9176 case offsetof(struct bpf_sock, dst_port):
9177 *insn++ = BPF_LDX_MEM(
9178 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
9179 si->dst_reg, si->src_reg,
9180 bpf_target_off(struct sock_common, skc_dport,
9181 sizeof_field(struct sock_common,
9182 skc_dport),
9183 target_size));
9184 break;
9185
9186 case offsetof(struct bpf_sock, state):
9187 *insn++ = BPF_LDX_MEM(
9188 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
9189 si->dst_reg, si->src_reg,
9190 bpf_target_off(struct sock_common, skc_state,
9191 sizeof_field(struct sock_common,
9192 skc_state),
9193 target_size));
9194 break;
9195 case offsetof(struct bpf_sock, rx_queue_mapping):
9196 #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
9197 *insn++ = BPF_LDX_MEM(
9198 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
9199 si->dst_reg, si->src_reg,
9200 bpf_target_off(struct sock, sk_rx_queue_mapping,
9201 sizeof_field(struct sock,
9202 sk_rx_queue_mapping),
9203 target_size));
9204 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
9205 1);
9206 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
9207 #else
9208 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
9209 *target_size = 2;
9210 #endif
9211 break;
9212 }
9213
9214 return insn - insn_buf;
9215 }
9216
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)9217 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
9218 const struct bpf_insn *si,
9219 struct bpf_insn *insn_buf,
9220 struct bpf_prog *prog, u32 *target_size)
9221 {
9222 struct bpf_insn *insn = insn_buf;
9223
9224 switch (si->off) {
9225 case offsetof(struct __sk_buff, ifindex):
9226 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
9227 si->dst_reg, si->src_reg,
9228 offsetof(struct sk_buff, dev));
9229 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9230 bpf_target_off(struct net_device, ifindex, 4,
9231 target_size));
9232 break;
9233 default:
9234 return bpf_convert_ctx_access(type, si, insn_buf, prog,
9235 target_size);
9236 }
9237
9238 return insn - insn_buf;
9239 }
9240
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)9241 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
9242 const struct bpf_insn *si,
9243 struct bpf_insn *insn_buf,
9244 struct bpf_prog *prog, u32 *target_size)
9245 {
9246 struct bpf_insn *insn = insn_buf;
9247
9248 switch (si->off) {
9249 case offsetof(struct xdp_md, data):
9250 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
9251 si->dst_reg, si->src_reg,
9252 offsetof(struct xdp_buff, data));
9253 break;
9254 case offsetof(struct xdp_md, data_meta):
9255 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
9256 si->dst_reg, si->src_reg,
9257 offsetof(struct xdp_buff, data_meta));
9258 break;
9259 case offsetof(struct xdp_md, data_end):
9260 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
9261 si->dst_reg, si->src_reg,
9262 offsetof(struct xdp_buff, data_end));
9263 break;
9264 case offsetof(struct xdp_md, ingress_ifindex):
9265 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
9266 si->dst_reg, si->src_reg,
9267 offsetof(struct xdp_buff, rxq));
9268 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
9269 si->dst_reg, si->dst_reg,
9270 offsetof(struct xdp_rxq_info, dev));
9271 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9272 offsetof(struct net_device, ifindex));
9273 break;
9274 case offsetof(struct xdp_md, rx_queue_index):
9275 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
9276 si->dst_reg, si->src_reg,
9277 offsetof(struct xdp_buff, rxq));
9278 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9279 offsetof(struct xdp_rxq_info,
9280 queue_index));
9281 break;
9282 case offsetof(struct xdp_md, egress_ifindex):
9283 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
9284 si->dst_reg, si->src_reg,
9285 offsetof(struct xdp_buff, txq));
9286 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
9287 si->dst_reg, si->dst_reg,
9288 offsetof(struct xdp_txq_info, dev));
9289 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9290 offsetof(struct net_device, ifindex));
9291 break;
9292 }
9293
9294 return insn - insn_buf;
9295 }
9296
9297 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
9298 * context Structure, F is Field in context structure that contains a pointer
9299 * to Nested Structure of type NS that has the field NF.
9300 *
9301 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
9302 * sure that SIZE is not greater than actual size of S.F.NF.
9303 *
9304 * If offset OFF is provided, the load happens from that offset relative to
9305 * offset of NF.
9306 */
9307 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
9308 do { \
9309 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
9310 si->src_reg, offsetof(S, F)); \
9311 *insn++ = BPF_LDX_MEM( \
9312 SIZE, si->dst_reg, si->dst_reg, \
9313 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
9314 target_size) \
9315 + OFF); \
9316 } while (0)
9317
9318 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
9319 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
9320 BPF_FIELD_SIZEOF(NS, NF), 0)
9321
9322 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
9323 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
9324 *
9325 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
9326 * "register" since two registers available in convert_ctx_access are not
9327 * enough: we can't override neither SRC, since it contains value to store, nor
9328 * DST since it contains pointer to context that may be used by later
9329 * instructions. But we need a temporary place to save pointer to nested
9330 * structure whose field we want to store to.
9331 */
9332 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
9333 do { \
9334 int tmp_reg = BPF_REG_9; \
9335 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9336 --tmp_reg; \
9337 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9338 --tmp_reg; \
9339 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
9340 offsetof(S, TF)); \
9341 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
9342 si->dst_reg, offsetof(S, F)); \
9343 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
9344 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
9345 target_size) \
9346 + OFF); \
9347 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
9348 offsetof(S, TF)); \
9349 } while (0)
9350
9351 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
9352 TF) \
9353 do { \
9354 if (type == BPF_WRITE) { \
9355 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
9356 OFF, TF); \
9357 } else { \
9358 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
9359 S, NS, F, NF, SIZE, OFF); \
9360 } \
9361 } while (0)
9362
9363 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
9364 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
9365 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
9366
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)9367 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
9368 const struct bpf_insn *si,
9369 struct bpf_insn *insn_buf,
9370 struct bpf_prog *prog, u32 *target_size)
9371 {
9372 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
9373 struct bpf_insn *insn = insn_buf;
9374
9375 switch (si->off) {
9376 case offsetof(struct bpf_sock_addr, user_family):
9377 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9378 struct sockaddr, uaddr, sa_family);
9379 break;
9380
9381 case offsetof(struct bpf_sock_addr, user_ip4):
9382 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9383 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
9384 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
9385 break;
9386
9387 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9388 off = si->off;
9389 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
9390 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9391 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9392 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
9393 tmp_reg);
9394 break;
9395
9396 case offsetof(struct bpf_sock_addr, user_port):
9397 /* To get port we need to know sa_family first and then treat
9398 * sockaddr as either sockaddr_in or sockaddr_in6.
9399 * Though we can simplify since port field has same offset and
9400 * size in both structures.
9401 * Here we check this invariant and use just one of the
9402 * structures if it's true.
9403 */
9404 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
9405 offsetof(struct sockaddr_in6, sin6_port));
9406 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
9407 sizeof_field(struct sockaddr_in6, sin6_port));
9408 /* Account for sin6_port being smaller than user_port. */
9409 port_size = min(port_size, BPF_LDST_BYTES(si));
9410 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9411 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9412 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
9413 break;
9414
9415 case offsetof(struct bpf_sock_addr, family):
9416 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9417 struct sock, sk, sk_family);
9418 break;
9419
9420 case offsetof(struct bpf_sock_addr, type):
9421 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9422 struct sock, sk, sk_type);
9423 break;
9424
9425 case offsetof(struct bpf_sock_addr, protocol):
9426 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9427 struct sock, sk, sk_protocol);
9428 break;
9429
9430 case offsetof(struct bpf_sock_addr, msg_src_ip4):
9431 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
9432 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9433 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
9434 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
9435 break;
9436
9437 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9438 msg_src_ip6[3]):
9439 off = si->off;
9440 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
9441 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
9442 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9443 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
9444 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
9445 break;
9446 case offsetof(struct bpf_sock_addr, sk):
9447 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
9448 si->dst_reg, si->src_reg,
9449 offsetof(struct bpf_sock_addr_kern, sk));
9450 break;
9451 }
9452
9453 return insn - insn_buf;
9454 }
9455
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)9456 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
9457 const struct bpf_insn *si,
9458 struct bpf_insn *insn_buf,
9459 struct bpf_prog *prog,
9460 u32 *target_size)
9461 {
9462 struct bpf_insn *insn = insn_buf;
9463 int off;
9464
9465 /* Helper macro for adding read access to tcp_sock or sock fields. */
9466 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
9467 do { \
9468 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \
9469 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
9470 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
9471 if (si->dst_reg == reg || si->src_reg == reg) \
9472 reg--; \
9473 if (si->dst_reg == reg || si->src_reg == reg) \
9474 reg--; \
9475 if (si->dst_reg == si->src_reg) { \
9476 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
9477 offsetof(struct bpf_sock_ops_kern, \
9478 temp)); \
9479 fullsock_reg = reg; \
9480 jmp += 2; \
9481 } \
9482 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9483 struct bpf_sock_ops_kern, \
9484 is_fullsock), \
9485 fullsock_reg, si->src_reg, \
9486 offsetof(struct bpf_sock_ops_kern, \
9487 is_fullsock)); \
9488 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
9489 if (si->dst_reg == si->src_reg) \
9490 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9491 offsetof(struct bpf_sock_ops_kern, \
9492 temp)); \
9493 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9494 struct bpf_sock_ops_kern, sk),\
9495 si->dst_reg, si->src_reg, \
9496 offsetof(struct bpf_sock_ops_kern, sk));\
9497 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
9498 OBJ_FIELD), \
9499 si->dst_reg, si->dst_reg, \
9500 offsetof(OBJ, OBJ_FIELD)); \
9501 if (si->dst_reg == si->src_reg) { \
9502 *insn++ = BPF_JMP_A(1); \
9503 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9504 offsetof(struct bpf_sock_ops_kern, \
9505 temp)); \
9506 } \
9507 } while (0)
9508
9509 #define SOCK_OPS_GET_SK() \
9510 do { \
9511 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \
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 if (si->dst_reg == si->src_reg) { \
9539 *insn++ = BPF_JMP_A(1); \
9540 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9541 offsetof(struct bpf_sock_ops_kern, \
9542 temp)); \
9543 } \
9544 } while (0)
9545
9546 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
9547 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
9548
9549 /* Helper macro for adding write access to tcp_sock or sock fields.
9550 * The macro is called with two registers, dst_reg which contains a pointer
9551 * to ctx (context) and src_reg which contains the value that should be
9552 * stored. However, we need an additional register since we cannot overwrite
9553 * dst_reg because it may be used later in the program.
9554 * Instead we "borrow" one of the other register. We first save its value
9555 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
9556 * it at the end of the macro.
9557 */
9558 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
9559 do { \
9560 int reg = BPF_REG_9; \
9561 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
9562 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
9563 if (si->dst_reg == reg || si->src_reg == reg) \
9564 reg--; \
9565 if (si->dst_reg == reg || si->src_reg == reg) \
9566 reg--; \
9567 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
9568 offsetof(struct bpf_sock_ops_kern, \
9569 temp)); \
9570 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9571 struct bpf_sock_ops_kern, \
9572 is_fullsock), \
9573 reg, si->dst_reg, \
9574 offsetof(struct bpf_sock_ops_kern, \
9575 is_fullsock)); \
9576 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
9577 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9578 struct bpf_sock_ops_kern, sk),\
9579 reg, si->dst_reg, \
9580 offsetof(struct bpf_sock_ops_kern, sk));\
9581 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
9582 reg, si->src_reg, \
9583 offsetof(OBJ, OBJ_FIELD)); \
9584 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
9585 offsetof(struct bpf_sock_ops_kern, \
9586 temp)); \
9587 } while (0)
9588
9589 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
9590 do { \
9591 if (TYPE == BPF_WRITE) \
9592 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
9593 else \
9594 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
9595 } while (0)
9596
9597 if (insn > insn_buf)
9598 return insn - insn_buf;
9599
9600 switch (si->off) {
9601 case offsetof(struct bpf_sock_ops, op):
9602 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9603 op),
9604 si->dst_reg, si->src_reg,
9605 offsetof(struct bpf_sock_ops_kern, op));
9606 break;
9607
9608 case offsetof(struct bpf_sock_ops, replylong[0]) ...
9609 offsetof(struct bpf_sock_ops, replylong[3]):
9610 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
9611 sizeof_field(struct bpf_sock_ops_kern, reply));
9612 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
9613 sizeof_field(struct bpf_sock_ops_kern, replylong));
9614 off = si->off;
9615 off -= offsetof(struct bpf_sock_ops, replylong[0]);
9616 off += offsetof(struct bpf_sock_ops_kern, replylong[0]);
9617 if (type == BPF_WRITE)
9618 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9619 off);
9620 else
9621 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9622 off);
9623 break;
9624
9625 case offsetof(struct bpf_sock_ops, family):
9626 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
9627
9628 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9629 struct bpf_sock_ops_kern, sk),
9630 si->dst_reg, si->src_reg,
9631 offsetof(struct bpf_sock_ops_kern, sk));
9632 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9633 offsetof(struct sock_common, skc_family));
9634 break;
9635
9636 case offsetof(struct bpf_sock_ops, remote_ip4):
9637 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
9638
9639 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9640 struct bpf_sock_ops_kern, sk),
9641 si->dst_reg, si->src_reg,
9642 offsetof(struct bpf_sock_ops_kern, sk));
9643 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9644 offsetof(struct sock_common, skc_daddr));
9645 break;
9646
9647 case offsetof(struct bpf_sock_ops, local_ip4):
9648 BUILD_BUG_ON(sizeof_field(struct sock_common,
9649 skc_rcv_saddr) != 4);
9650
9651 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9652 struct bpf_sock_ops_kern, sk),
9653 si->dst_reg, si->src_reg,
9654 offsetof(struct bpf_sock_ops_kern, sk));
9655 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9656 offsetof(struct sock_common,
9657 skc_rcv_saddr));
9658 break;
9659
9660 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
9661 offsetof(struct bpf_sock_ops, remote_ip6[3]):
9662 #if IS_ENABLED(CONFIG_IPV6)
9663 BUILD_BUG_ON(sizeof_field(struct sock_common,
9664 skc_v6_daddr.s6_addr32[0]) != 4);
9665
9666 off = si->off;
9667 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
9668 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9669 struct bpf_sock_ops_kern, sk),
9670 si->dst_reg, si->src_reg,
9671 offsetof(struct bpf_sock_ops_kern, sk));
9672 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9673 offsetof(struct sock_common,
9674 skc_v6_daddr.s6_addr32[0]) +
9675 off);
9676 #else
9677 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9678 #endif
9679 break;
9680
9681 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
9682 offsetof(struct bpf_sock_ops, local_ip6[3]):
9683 #if IS_ENABLED(CONFIG_IPV6)
9684 BUILD_BUG_ON(sizeof_field(struct sock_common,
9685 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
9686
9687 off = si->off;
9688 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
9689 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9690 struct bpf_sock_ops_kern, sk),
9691 si->dst_reg, si->src_reg,
9692 offsetof(struct bpf_sock_ops_kern, sk));
9693 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9694 offsetof(struct sock_common,
9695 skc_v6_rcv_saddr.s6_addr32[0]) +
9696 off);
9697 #else
9698 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9699 #endif
9700 break;
9701
9702 case offsetof(struct bpf_sock_ops, remote_port):
9703 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
9704
9705 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9706 struct bpf_sock_ops_kern, sk),
9707 si->dst_reg, si->src_reg,
9708 offsetof(struct bpf_sock_ops_kern, sk));
9709 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9710 offsetof(struct sock_common, skc_dport));
9711 #ifndef __BIG_ENDIAN_BITFIELD
9712 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
9713 #endif
9714 break;
9715
9716 case offsetof(struct bpf_sock_ops, local_port):
9717 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
9718
9719 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9720 struct bpf_sock_ops_kern, sk),
9721 si->dst_reg, si->src_reg,
9722 offsetof(struct bpf_sock_ops_kern, sk));
9723 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9724 offsetof(struct sock_common, skc_num));
9725 break;
9726
9727 case offsetof(struct bpf_sock_ops, is_fullsock):
9728 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9729 struct bpf_sock_ops_kern,
9730 is_fullsock),
9731 si->dst_reg, si->src_reg,
9732 offsetof(struct bpf_sock_ops_kern,
9733 is_fullsock));
9734 break;
9735
9736 case offsetof(struct bpf_sock_ops, state):
9737 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
9738
9739 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9740 struct bpf_sock_ops_kern, sk),
9741 si->dst_reg, si->src_reg,
9742 offsetof(struct bpf_sock_ops_kern, sk));
9743 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
9744 offsetof(struct sock_common, skc_state));
9745 break;
9746
9747 case offsetof(struct bpf_sock_ops, rtt_min):
9748 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
9749 sizeof(struct minmax));
9750 BUILD_BUG_ON(sizeof(struct minmax) <
9751 sizeof(struct minmax_sample));
9752
9753 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9754 struct bpf_sock_ops_kern, sk),
9755 si->dst_reg, si->src_reg,
9756 offsetof(struct bpf_sock_ops_kern, sk));
9757 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9758 offsetof(struct tcp_sock, rtt_min) +
9759 sizeof_field(struct minmax_sample, t));
9760 break;
9761
9762 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
9763 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
9764 struct tcp_sock);
9765 break;
9766
9767 case offsetof(struct bpf_sock_ops, sk_txhash):
9768 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
9769 struct sock, type);
9770 break;
9771 case offsetof(struct bpf_sock_ops, snd_cwnd):
9772 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
9773 break;
9774 case offsetof(struct bpf_sock_ops, srtt_us):
9775 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
9776 break;
9777 case offsetof(struct bpf_sock_ops, snd_ssthresh):
9778 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
9779 break;
9780 case offsetof(struct bpf_sock_ops, rcv_nxt):
9781 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
9782 break;
9783 case offsetof(struct bpf_sock_ops, snd_nxt):
9784 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
9785 break;
9786 case offsetof(struct bpf_sock_ops, snd_una):
9787 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
9788 break;
9789 case offsetof(struct bpf_sock_ops, mss_cache):
9790 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
9791 break;
9792 case offsetof(struct bpf_sock_ops, ecn_flags):
9793 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
9794 break;
9795 case offsetof(struct bpf_sock_ops, rate_delivered):
9796 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
9797 break;
9798 case offsetof(struct bpf_sock_ops, rate_interval_us):
9799 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
9800 break;
9801 case offsetof(struct bpf_sock_ops, packets_out):
9802 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
9803 break;
9804 case offsetof(struct bpf_sock_ops, retrans_out):
9805 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
9806 break;
9807 case offsetof(struct bpf_sock_ops, total_retrans):
9808 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
9809 break;
9810 case offsetof(struct bpf_sock_ops, segs_in):
9811 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
9812 break;
9813 case offsetof(struct bpf_sock_ops, data_segs_in):
9814 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
9815 break;
9816 case offsetof(struct bpf_sock_ops, segs_out):
9817 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
9818 break;
9819 case offsetof(struct bpf_sock_ops, data_segs_out):
9820 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
9821 break;
9822 case offsetof(struct bpf_sock_ops, lost_out):
9823 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
9824 break;
9825 case offsetof(struct bpf_sock_ops, sacked_out):
9826 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
9827 break;
9828 case offsetof(struct bpf_sock_ops, bytes_received):
9829 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
9830 break;
9831 case offsetof(struct bpf_sock_ops, bytes_acked):
9832 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
9833 break;
9834 case offsetof(struct bpf_sock_ops, sk):
9835 SOCK_OPS_GET_SK();
9836 break;
9837 case offsetof(struct bpf_sock_ops, skb_data_end):
9838 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9839 skb_data_end),
9840 si->dst_reg, si->src_reg,
9841 offsetof(struct bpf_sock_ops_kern,
9842 skb_data_end));
9843 break;
9844 case offsetof(struct bpf_sock_ops, skb_data):
9845 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9846 skb),
9847 si->dst_reg, si->src_reg,
9848 offsetof(struct bpf_sock_ops_kern,
9849 skb));
9850 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9851 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9852 si->dst_reg, si->dst_reg,
9853 offsetof(struct sk_buff, data));
9854 break;
9855 case offsetof(struct bpf_sock_ops, skb_len):
9856 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9857 skb),
9858 si->dst_reg, si->src_reg,
9859 offsetof(struct bpf_sock_ops_kern,
9860 skb));
9861 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9862 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
9863 si->dst_reg, si->dst_reg,
9864 offsetof(struct sk_buff, len));
9865 break;
9866 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
9867 off = offsetof(struct sk_buff, cb);
9868 off += offsetof(struct tcp_skb_cb, tcp_flags);
9869 *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags);
9870 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9871 skb),
9872 si->dst_reg, si->src_reg,
9873 offsetof(struct bpf_sock_ops_kern,
9874 skb));
9875 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9876 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb,
9877 tcp_flags),
9878 si->dst_reg, si->dst_reg, off);
9879 break;
9880 }
9881 return insn - insn_buf;
9882 }
9883
9884 /* data_end = skb->data + skb_headlen() */
bpf_convert_data_end_access(const struct bpf_insn * si,struct bpf_insn * insn)9885 static struct bpf_insn *bpf_convert_data_end_access(const struct bpf_insn *si,
9886 struct bpf_insn *insn)
9887 {
9888 int reg;
9889 int temp_reg_off = offsetof(struct sk_buff, cb) +
9890 offsetof(struct sk_skb_cb, temp_reg);
9891
9892 if (si->src_reg == si->dst_reg) {
9893 /* We need an extra register, choose and save a register. */
9894 reg = BPF_REG_9;
9895 if (si->src_reg == reg || si->dst_reg == reg)
9896 reg--;
9897 if (si->src_reg == reg || si->dst_reg == reg)
9898 reg--;
9899 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, temp_reg_off);
9900 } else {
9901 reg = si->dst_reg;
9902 }
9903
9904 /* reg = skb->data */
9905 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9906 reg, si->src_reg,
9907 offsetof(struct sk_buff, data));
9908 /* AX = skb->len */
9909 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
9910 BPF_REG_AX, si->src_reg,
9911 offsetof(struct sk_buff, len));
9912 /* reg = skb->data + skb->len */
9913 *insn++ = BPF_ALU64_REG(BPF_ADD, reg, BPF_REG_AX);
9914 /* AX = skb->data_len */
9915 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data_len),
9916 BPF_REG_AX, si->src_reg,
9917 offsetof(struct sk_buff, data_len));
9918
9919 /* reg = skb->data + skb->len - skb->data_len */
9920 *insn++ = BPF_ALU64_REG(BPF_SUB, reg, BPF_REG_AX);
9921
9922 if (si->src_reg == si->dst_reg) {
9923 /* Restore the saved register */
9924 *insn++ = BPF_MOV64_REG(BPF_REG_AX, si->src_reg);
9925 *insn++ = BPF_MOV64_REG(si->dst_reg, reg);
9926 *insn++ = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_AX, temp_reg_off);
9927 }
9928
9929 return insn;
9930 }
9931
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)9932 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
9933 const struct bpf_insn *si,
9934 struct bpf_insn *insn_buf,
9935 struct bpf_prog *prog, u32 *target_size)
9936 {
9937 struct bpf_insn *insn = insn_buf;
9938 int off;
9939
9940 switch (si->off) {
9941 case offsetof(struct __sk_buff, data_end):
9942 insn = bpf_convert_data_end_access(si, insn);
9943 break;
9944 case offsetof(struct __sk_buff, cb[0]) ...
9945 offsetofend(struct __sk_buff, cb[4]) - 1:
9946 BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20);
9947 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
9948 offsetof(struct sk_skb_cb, data)) %
9949 sizeof(__u64));
9950
9951 prog->cb_access = 1;
9952 off = si->off;
9953 off -= offsetof(struct __sk_buff, cb[0]);
9954 off += offsetof(struct sk_buff, cb);
9955 off += offsetof(struct sk_skb_cb, data);
9956 if (type == BPF_WRITE)
9957 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
9958 si->src_reg, off);
9959 else
9960 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
9961 si->src_reg, off);
9962 break;
9963
9964
9965 default:
9966 return bpf_convert_ctx_access(type, si, insn_buf, prog,
9967 target_size);
9968 }
9969
9970 return insn - insn_buf;
9971 }
9972
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)9973 static u32 sk_msg_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 #if IS_ENABLED(CONFIG_IPV6)
9980 int off;
9981 #endif
9982
9983 /* convert ctx uses the fact sg element is first in struct */
9984 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
9985
9986 switch (si->off) {
9987 case offsetof(struct sk_msg_md, data):
9988 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
9989 si->dst_reg, si->src_reg,
9990 offsetof(struct sk_msg, data));
9991 break;
9992 case offsetof(struct sk_msg_md, data_end):
9993 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
9994 si->dst_reg, si->src_reg,
9995 offsetof(struct sk_msg, data_end));
9996 break;
9997 case offsetof(struct sk_msg_md, family):
9998 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
9999
10000 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10001 struct sk_msg, sk),
10002 si->dst_reg, si->src_reg,
10003 offsetof(struct sk_msg, sk));
10004 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10005 offsetof(struct sock_common, skc_family));
10006 break;
10007
10008 case offsetof(struct sk_msg_md, remote_ip4):
10009 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10010
10011 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10012 struct sk_msg, sk),
10013 si->dst_reg, si->src_reg,
10014 offsetof(struct sk_msg, sk));
10015 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10016 offsetof(struct sock_common, skc_daddr));
10017 break;
10018
10019 case offsetof(struct sk_msg_md, local_ip4):
10020 BUILD_BUG_ON(sizeof_field(struct sock_common,
10021 skc_rcv_saddr) != 4);
10022
10023 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10024 struct sk_msg, sk),
10025 si->dst_reg, si->src_reg,
10026 offsetof(struct sk_msg, sk));
10027 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10028 offsetof(struct sock_common,
10029 skc_rcv_saddr));
10030 break;
10031
10032 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
10033 offsetof(struct sk_msg_md, remote_ip6[3]):
10034 #if IS_ENABLED(CONFIG_IPV6)
10035 BUILD_BUG_ON(sizeof_field(struct sock_common,
10036 skc_v6_daddr.s6_addr32[0]) != 4);
10037
10038 off = si->off;
10039 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
10040 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10041 struct sk_msg, sk),
10042 si->dst_reg, si->src_reg,
10043 offsetof(struct sk_msg, sk));
10044 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10045 offsetof(struct sock_common,
10046 skc_v6_daddr.s6_addr32[0]) +
10047 off);
10048 #else
10049 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10050 #endif
10051 break;
10052
10053 case offsetof(struct sk_msg_md, local_ip6[0]) ...
10054 offsetof(struct sk_msg_md, local_ip6[3]):
10055 #if IS_ENABLED(CONFIG_IPV6)
10056 BUILD_BUG_ON(sizeof_field(struct sock_common,
10057 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10058
10059 off = si->off;
10060 off -= offsetof(struct sk_msg_md, local_ip6[0]);
10061 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10062 struct sk_msg, sk),
10063 si->dst_reg, si->src_reg,
10064 offsetof(struct sk_msg, sk));
10065 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10066 offsetof(struct sock_common,
10067 skc_v6_rcv_saddr.s6_addr32[0]) +
10068 off);
10069 #else
10070 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10071 #endif
10072 break;
10073
10074 case offsetof(struct sk_msg_md, remote_port):
10075 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10076
10077 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10078 struct sk_msg, sk),
10079 si->dst_reg, si->src_reg,
10080 offsetof(struct sk_msg, sk));
10081 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10082 offsetof(struct sock_common, skc_dport));
10083 #ifndef __BIG_ENDIAN_BITFIELD
10084 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10085 #endif
10086 break;
10087
10088 case offsetof(struct sk_msg_md, local_port):
10089 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10090
10091 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10092 struct sk_msg, sk),
10093 si->dst_reg, si->src_reg,
10094 offsetof(struct sk_msg, sk));
10095 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10096 offsetof(struct sock_common, skc_num));
10097 break;
10098
10099 case offsetof(struct sk_msg_md, size):
10100 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
10101 si->dst_reg, si->src_reg,
10102 offsetof(struct sk_msg_sg, size));
10103 break;
10104
10105 case offsetof(struct sk_msg_md, sk):
10106 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
10107 si->dst_reg, si->src_reg,
10108 offsetof(struct sk_msg, sk));
10109 break;
10110 }
10111
10112 return insn - insn_buf;
10113 }
10114
10115 const struct bpf_verifier_ops sk_filter_verifier_ops = {
10116 .get_func_proto = sk_filter_func_proto,
10117 .is_valid_access = sk_filter_is_valid_access,
10118 .convert_ctx_access = bpf_convert_ctx_access,
10119 .gen_ld_abs = bpf_gen_ld_abs,
10120 };
10121
10122 const struct bpf_prog_ops sk_filter_prog_ops = {
10123 .test_run = bpf_prog_test_run_skb,
10124 };
10125
10126 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
10127 .get_func_proto = tc_cls_act_func_proto,
10128 .is_valid_access = tc_cls_act_is_valid_access,
10129 .convert_ctx_access = tc_cls_act_convert_ctx_access,
10130 .gen_prologue = tc_cls_act_prologue,
10131 .gen_ld_abs = bpf_gen_ld_abs,
10132 .check_kfunc_call = bpf_prog_test_check_kfunc_call,
10133 };
10134
10135 const struct bpf_prog_ops tc_cls_act_prog_ops = {
10136 .test_run = bpf_prog_test_run_skb,
10137 };
10138
10139 const struct bpf_verifier_ops xdp_verifier_ops = {
10140 .get_func_proto = xdp_func_proto,
10141 .is_valid_access = xdp_is_valid_access,
10142 .convert_ctx_access = xdp_convert_ctx_access,
10143 .gen_prologue = bpf_noop_prologue,
10144 };
10145
10146 const struct bpf_prog_ops xdp_prog_ops = {
10147 .test_run = bpf_prog_test_run_xdp,
10148 };
10149
10150 const struct bpf_verifier_ops cg_skb_verifier_ops = {
10151 .get_func_proto = cg_skb_func_proto,
10152 .is_valid_access = cg_skb_is_valid_access,
10153 .convert_ctx_access = bpf_convert_ctx_access,
10154 };
10155
10156 const struct bpf_prog_ops cg_skb_prog_ops = {
10157 .test_run = bpf_prog_test_run_skb,
10158 };
10159
10160 const struct bpf_verifier_ops lwt_in_verifier_ops = {
10161 .get_func_proto = lwt_in_func_proto,
10162 .is_valid_access = lwt_is_valid_access,
10163 .convert_ctx_access = bpf_convert_ctx_access,
10164 };
10165
10166 const struct bpf_prog_ops lwt_in_prog_ops = {
10167 .test_run = bpf_prog_test_run_skb,
10168 };
10169
10170 const struct bpf_verifier_ops lwt_out_verifier_ops = {
10171 .get_func_proto = lwt_out_func_proto,
10172 .is_valid_access = lwt_is_valid_access,
10173 .convert_ctx_access = bpf_convert_ctx_access,
10174 };
10175
10176 const struct bpf_prog_ops lwt_out_prog_ops = {
10177 .test_run = bpf_prog_test_run_skb,
10178 };
10179
10180 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
10181 .get_func_proto = lwt_xmit_func_proto,
10182 .is_valid_access = lwt_is_valid_access,
10183 .convert_ctx_access = bpf_convert_ctx_access,
10184 .gen_prologue = tc_cls_act_prologue,
10185 };
10186
10187 const struct bpf_prog_ops lwt_xmit_prog_ops = {
10188 .test_run = bpf_prog_test_run_skb,
10189 };
10190
10191 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
10192 .get_func_proto = lwt_seg6local_func_proto,
10193 .is_valid_access = lwt_is_valid_access,
10194 .convert_ctx_access = bpf_convert_ctx_access,
10195 };
10196
10197 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
10198 .test_run = bpf_prog_test_run_skb,
10199 };
10200
10201 const struct bpf_verifier_ops cg_sock_verifier_ops = {
10202 .get_func_proto = sock_filter_func_proto,
10203 .is_valid_access = sock_filter_is_valid_access,
10204 .convert_ctx_access = bpf_sock_convert_ctx_access,
10205 };
10206
10207 const struct bpf_prog_ops cg_sock_prog_ops = {
10208 };
10209
10210 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
10211 .get_func_proto = sock_addr_func_proto,
10212 .is_valid_access = sock_addr_is_valid_access,
10213 .convert_ctx_access = sock_addr_convert_ctx_access,
10214 };
10215
10216 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
10217 };
10218
10219 const struct bpf_verifier_ops sock_ops_verifier_ops = {
10220 .get_func_proto = sock_ops_func_proto,
10221 .is_valid_access = sock_ops_is_valid_access,
10222 .convert_ctx_access = sock_ops_convert_ctx_access,
10223 };
10224
10225 const struct bpf_prog_ops sock_ops_prog_ops = {
10226 };
10227
10228 const struct bpf_verifier_ops sk_skb_verifier_ops = {
10229 .get_func_proto = sk_skb_func_proto,
10230 .is_valid_access = sk_skb_is_valid_access,
10231 .convert_ctx_access = sk_skb_convert_ctx_access,
10232 .gen_prologue = sk_skb_prologue,
10233 };
10234
10235 const struct bpf_prog_ops sk_skb_prog_ops = {
10236 };
10237
10238 const struct bpf_verifier_ops sk_msg_verifier_ops = {
10239 .get_func_proto = sk_msg_func_proto,
10240 .is_valid_access = sk_msg_is_valid_access,
10241 .convert_ctx_access = sk_msg_convert_ctx_access,
10242 .gen_prologue = bpf_noop_prologue,
10243 };
10244
10245 const struct bpf_prog_ops sk_msg_prog_ops = {
10246 };
10247
10248 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
10249 .get_func_proto = flow_dissector_func_proto,
10250 .is_valid_access = flow_dissector_is_valid_access,
10251 .convert_ctx_access = flow_dissector_convert_ctx_access,
10252 };
10253
10254 const struct bpf_prog_ops flow_dissector_prog_ops = {
10255 .test_run = bpf_prog_test_run_flow_dissector,
10256 };
10257
sk_detach_filter(struct sock * sk)10258 int sk_detach_filter(struct sock *sk)
10259 {
10260 int ret = -ENOENT;
10261 struct sk_filter *filter;
10262
10263 if (sock_flag(sk, SOCK_FILTER_LOCKED))
10264 return -EPERM;
10265
10266 filter = rcu_dereference_protected(sk->sk_filter,
10267 lockdep_sock_is_held(sk));
10268 if (filter) {
10269 RCU_INIT_POINTER(sk->sk_filter, NULL);
10270 sk_filter_uncharge(sk, filter);
10271 ret = 0;
10272 }
10273
10274 return ret;
10275 }
10276 EXPORT_SYMBOL_GPL(sk_detach_filter);
10277
sk_get_filter(struct sock * sk,struct sock_filter __user * ubuf,unsigned int len)10278 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
10279 unsigned int len)
10280 {
10281 struct sock_fprog_kern *fprog;
10282 struct sk_filter *filter;
10283 int ret = 0;
10284
10285 lock_sock(sk);
10286 filter = rcu_dereference_protected(sk->sk_filter,
10287 lockdep_sock_is_held(sk));
10288 if (!filter)
10289 goto out;
10290
10291 /* We're copying the filter that has been originally attached,
10292 * so no conversion/decode needed anymore. eBPF programs that
10293 * have no original program cannot be dumped through this.
10294 */
10295 ret = -EACCES;
10296 fprog = filter->prog->orig_prog;
10297 if (!fprog)
10298 goto out;
10299
10300 ret = fprog->len;
10301 if (!len)
10302 /* User space only enquires number of filter blocks. */
10303 goto out;
10304
10305 ret = -EINVAL;
10306 if (len < fprog->len)
10307 goto out;
10308
10309 ret = -EFAULT;
10310 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
10311 goto out;
10312
10313 /* Instead of bytes, the API requests to return the number
10314 * of filter blocks.
10315 */
10316 ret = fprog->len;
10317 out:
10318 release_sock(sk);
10319 return ret;
10320 }
10321
10322 #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)10323 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
10324 struct sock_reuseport *reuse,
10325 struct sock *sk, struct sk_buff *skb,
10326 struct sock *migrating_sk,
10327 u32 hash)
10328 {
10329 reuse_kern->skb = skb;
10330 reuse_kern->sk = sk;
10331 reuse_kern->selected_sk = NULL;
10332 reuse_kern->migrating_sk = migrating_sk;
10333 reuse_kern->data_end = skb->data + skb_headlen(skb);
10334 reuse_kern->hash = hash;
10335 reuse_kern->reuseport_id = reuse->reuseport_id;
10336 reuse_kern->bind_inany = reuse->bind_inany;
10337 }
10338
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)10339 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
10340 struct bpf_prog *prog, struct sk_buff *skb,
10341 struct sock *migrating_sk,
10342 u32 hash)
10343 {
10344 struct sk_reuseport_kern reuse_kern;
10345 enum sk_action action;
10346
10347 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, migrating_sk, hash);
10348 action = bpf_prog_run(prog, &reuse_kern);
10349
10350 if (action == SK_PASS)
10351 return reuse_kern.selected_sk;
10352 else
10353 return ERR_PTR(-ECONNREFUSED);
10354 }
10355
BPF_CALL_4(sk_select_reuseport,struct sk_reuseport_kern *,reuse_kern,struct bpf_map *,map,void *,key,u32,flags)10356 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
10357 struct bpf_map *, map, void *, key, u32, flags)
10358 {
10359 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
10360 struct sock_reuseport *reuse;
10361 struct sock *selected_sk;
10362
10363 selected_sk = map->ops->map_lookup_elem(map, key);
10364 if (!selected_sk)
10365 return -ENOENT;
10366
10367 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
10368 if (!reuse) {
10369 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
10370 if (sk_is_refcounted(selected_sk))
10371 sock_put(selected_sk);
10372
10373 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
10374 * The only (!reuse) case here is - the sk has already been
10375 * unhashed (e.g. by close()), so treat it as -ENOENT.
10376 *
10377 * Other maps (e.g. sock_map) do not provide this guarantee and
10378 * the sk may never be in the reuseport group to begin with.
10379 */
10380 return is_sockarray ? -ENOENT : -EINVAL;
10381 }
10382
10383 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
10384 struct sock *sk = reuse_kern->sk;
10385
10386 if (sk->sk_protocol != selected_sk->sk_protocol)
10387 return -EPROTOTYPE;
10388 else if (sk->sk_family != selected_sk->sk_family)
10389 return -EAFNOSUPPORT;
10390
10391 /* Catch all. Likely bound to a different sockaddr. */
10392 return -EBADFD;
10393 }
10394
10395 reuse_kern->selected_sk = selected_sk;
10396
10397 return 0;
10398 }
10399
10400 static const struct bpf_func_proto sk_select_reuseport_proto = {
10401 .func = sk_select_reuseport,
10402 .gpl_only = false,
10403 .ret_type = RET_INTEGER,
10404 .arg1_type = ARG_PTR_TO_CTX,
10405 .arg2_type = ARG_CONST_MAP_PTR,
10406 .arg3_type = ARG_PTR_TO_MAP_KEY,
10407 .arg4_type = ARG_ANYTHING,
10408 };
10409
BPF_CALL_4(sk_reuseport_load_bytes,const struct sk_reuseport_kern *,reuse_kern,u32,offset,void *,to,u32,len)10410 BPF_CALL_4(sk_reuseport_load_bytes,
10411 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10412 void *, to, u32, len)
10413 {
10414 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
10415 }
10416
10417 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
10418 .func = sk_reuseport_load_bytes,
10419 .gpl_only = false,
10420 .ret_type = RET_INTEGER,
10421 .arg1_type = ARG_PTR_TO_CTX,
10422 .arg2_type = ARG_ANYTHING,
10423 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10424 .arg4_type = ARG_CONST_SIZE,
10425 };
10426
BPF_CALL_5(sk_reuseport_load_bytes_relative,const struct sk_reuseport_kern *,reuse_kern,u32,offset,void *,to,u32,len,u32,start_header)10427 BPF_CALL_5(sk_reuseport_load_bytes_relative,
10428 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10429 void *, to, u32, len, u32, start_header)
10430 {
10431 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
10432 len, start_header);
10433 }
10434
10435 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
10436 .func = sk_reuseport_load_bytes_relative,
10437 .gpl_only = false,
10438 .ret_type = RET_INTEGER,
10439 .arg1_type = ARG_PTR_TO_CTX,
10440 .arg2_type = ARG_ANYTHING,
10441 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10442 .arg4_type = ARG_CONST_SIZE,
10443 .arg5_type = ARG_ANYTHING,
10444 };
10445
10446 static const struct bpf_func_proto *
sk_reuseport_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)10447 sk_reuseport_func_proto(enum bpf_func_id func_id,
10448 const struct bpf_prog *prog)
10449 {
10450 switch (func_id) {
10451 case BPF_FUNC_sk_select_reuseport:
10452 return &sk_select_reuseport_proto;
10453 case BPF_FUNC_skb_load_bytes:
10454 return &sk_reuseport_load_bytes_proto;
10455 case BPF_FUNC_skb_load_bytes_relative:
10456 return &sk_reuseport_load_bytes_relative_proto;
10457 case BPF_FUNC_get_socket_cookie:
10458 return &bpf_get_socket_ptr_cookie_proto;
10459 case BPF_FUNC_ktime_get_coarse_ns:
10460 return &bpf_ktime_get_coarse_ns_proto;
10461 default:
10462 return bpf_base_func_proto(func_id);
10463 }
10464 }
10465
10466 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)10467 sk_reuseport_is_valid_access(int off, int size,
10468 enum bpf_access_type type,
10469 const struct bpf_prog *prog,
10470 struct bpf_insn_access_aux *info)
10471 {
10472 const u32 size_default = sizeof(__u32);
10473
10474 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
10475 off % size || type != BPF_READ)
10476 return false;
10477
10478 switch (off) {
10479 case offsetof(struct sk_reuseport_md, data):
10480 info->reg_type = PTR_TO_PACKET;
10481 return size == sizeof(__u64);
10482
10483 case offsetof(struct sk_reuseport_md, data_end):
10484 info->reg_type = PTR_TO_PACKET_END;
10485 return size == sizeof(__u64);
10486
10487 case offsetof(struct sk_reuseport_md, hash):
10488 return size == size_default;
10489
10490 case offsetof(struct sk_reuseport_md, sk):
10491 info->reg_type = PTR_TO_SOCKET;
10492 return size == sizeof(__u64);
10493
10494 case offsetof(struct sk_reuseport_md, migrating_sk):
10495 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
10496 return size == sizeof(__u64);
10497
10498 /* Fields that allow narrowing */
10499 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
10500 if (size < sizeof_field(struct sk_buff, protocol))
10501 return false;
10502 fallthrough;
10503 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
10504 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
10505 case bpf_ctx_range(struct sk_reuseport_md, len):
10506 bpf_ctx_record_field_size(info, size_default);
10507 return bpf_ctx_narrow_access_ok(off, size, size_default);
10508
10509 default:
10510 return false;
10511 }
10512 }
10513
10514 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
10515 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
10516 si->dst_reg, si->src_reg, \
10517 bpf_target_off(struct sk_reuseport_kern, F, \
10518 sizeof_field(struct sk_reuseport_kern, F), \
10519 target_size)); \
10520 })
10521
10522 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
10523 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
10524 struct sk_buff, \
10525 skb, \
10526 SKB_FIELD)
10527
10528 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
10529 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
10530 struct sock, \
10531 sk, \
10532 SK_FIELD)
10533
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)10534 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
10535 const struct bpf_insn *si,
10536 struct bpf_insn *insn_buf,
10537 struct bpf_prog *prog,
10538 u32 *target_size)
10539 {
10540 struct bpf_insn *insn = insn_buf;
10541
10542 switch (si->off) {
10543 case offsetof(struct sk_reuseport_md, data):
10544 SK_REUSEPORT_LOAD_SKB_FIELD(data);
10545 break;
10546
10547 case offsetof(struct sk_reuseport_md, len):
10548 SK_REUSEPORT_LOAD_SKB_FIELD(len);
10549 break;
10550
10551 case offsetof(struct sk_reuseport_md, eth_protocol):
10552 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
10553 break;
10554
10555 case offsetof(struct sk_reuseport_md, ip_protocol):
10556 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
10557 break;
10558
10559 case offsetof(struct sk_reuseport_md, data_end):
10560 SK_REUSEPORT_LOAD_FIELD(data_end);
10561 break;
10562
10563 case offsetof(struct sk_reuseport_md, hash):
10564 SK_REUSEPORT_LOAD_FIELD(hash);
10565 break;
10566
10567 case offsetof(struct sk_reuseport_md, bind_inany):
10568 SK_REUSEPORT_LOAD_FIELD(bind_inany);
10569 break;
10570
10571 case offsetof(struct sk_reuseport_md, sk):
10572 SK_REUSEPORT_LOAD_FIELD(sk);
10573 break;
10574
10575 case offsetof(struct sk_reuseport_md, migrating_sk):
10576 SK_REUSEPORT_LOAD_FIELD(migrating_sk);
10577 break;
10578 }
10579
10580 return insn - insn_buf;
10581 }
10582
10583 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
10584 .get_func_proto = sk_reuseport_func_proto,
10585 .is_valid_access = sk_reuseport_is_valid_access,
10586 .convert_ctx_access = sk_reuseport_convert_ctx_access,
10587 };
10588
10589 const struct bpf_prog_ops sk_reuseport_prog_ops = {
10590 };
10591
10592 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled);
10593 EXPORT_SYMBOL(bpf_sk_lookup_enabled);
10594
BPF_CALL_3(bpf_sk_lookup_assign,struct bpf_sk_lookup_kern *,ctx,struct sock *,sk,u64,flags)10595 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx,
10596 struct sock *, sk, u64, flags)
10597 {
10598 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE |
10599 BPF_SK_LOOKUP_F_NO_REUSEPORT)))
10600 return -EINVAL;
10601 if (unlikely(sk && sk_is_refcounted(sk)))
10602 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */
10603 if (unlikely(sk && sk->sk_state == TCP_ESTABLISHED))
10604 return -ESOCKTNOSUPPORT; /* reject connected sockets */
10605
10606 /* Check if socket is suitable for packet L3/L4 protocol */
10607 if (sk && sk->sk_protocol != ctx->protocol)
10608 return -EPROTOTYPE;
10609 if (sk && sk->sk_family != ctx->family &&
10610 (sk->sk_family == AF_INET || ipv6_only_sock(sk)))
10611 return -EAFNOSUPPORT;
10612
10613 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE))
10614 return -EEXIST;
10615
10616 /* Select socket as lookup result */
10617 ctx->selected_sk = sk;
10618 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT;
10619 return 0;
10620 }
10621
10622 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = {
10623 .func = bpf_sk_lookup_assign,
10624 .gpl_only = false,
10625 .ret_type = RET_INTEGER,
10626 .arg1_type = ARG_PTR_TO_CTX,
10627 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL,
10628 .arg3_type = ARG_ANYTHING,
10629 };
10630
10631 static const struct bpf_func_proto *
sk_lookup_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)10632 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10633 {
10634 switch (func_id) {
10635 case BPF_FUNC_perf_event_output:
10636 return &bpf_event_output_data_proto;
10637 case BPF_FUNC_sk_assign:
10638 return &bpf_sk_lookup_assign_proto;
10639 case BPF_FUNC_sk_release:
10640 return &bpf_sk_release_proto;
10641 default:
10642 return bpf_sk_base_func_proto(func_id);
10643 }
10644 }
10645
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)10646 static bool sk_lookup_is_valid_access(int off, int size,
10647 enum bpf_access_type type,
10648 const struct bpf_prog *prog,
10649 struct bpf_insn_access_aux *info)
10650 {
10651 if (off < 0 || off >= sizeof(struct bpf_sk_lookup))
10652 return false;
10653 if (off % size != 0)
10654 return false;
10655 if (type != BPF_READ)
10656 return false;
10657
10658 switch (off) {
10659 case offsetof(struct bpf_sk_lookup, sk):
10660 info->reg_type = PTR_TO_SOCKET_OR_NULL;
10661 return size == sizeof(__u64);
10662
10663 case bpf_ctx_range(struct bpf_sk_lookup, family):
10664 case bpf_ctx_range(struct bpf_sk_lookup, protocol):
10665 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4):
10666 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
10667 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
10668 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
10669 case offsetof(struct bpf_sk_lookup, remote_port) ...
10670 offsetof(struct bpf_sk_lookup, local_ip4) - 1:
10671 case bpf_ctx_range(struct bpf_sk_lookup, local_port):
10672 bpf_ctx_record_field_size(info, sizeof(__u32));
10673 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
10674
10675 default:
10676 return false;
10677 }
10678 }
10679
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)10680 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
10681 const struct bpf_insn *si,
10682 struct bpf_insn *insn_buf,
10683 struct bpf_prog *prog,
10684 u32 *target_size)
10685 {
10686 struct bpf_insn *insn = insn_buf;
10687
10688 switch (si->off) {
10689 case offsetof(struct bpf_sk_lookup, sk):
10690 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
10691 offsetof(struct bpf_sk_lookup_kern, selected_sk));
10692 break;
10693
10694 case offsetof(struct bpf_sk_lookup, family):
10695 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10696 bpf_target_off(struct bpf_sk_lookup_kern,
10697 family, 2, target_size));
10698 break;
10699
10700 case offsetof(struct bpf_sk_lookup, protocol):
10701 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10702 bpf_target_off(struct bpf_sk_lookup_kern,
10703 protocol, 2, target_size));
10704 break;
10705
10706 case offsetof(struct bpf_sk_lookup, remote_ip4):
10707 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10708 bpf_target_off(struct bpf_sk_lookup_kern,
10709 v4.saddr, 4, target_size));
10710 break;
10711
10712 case offsetof(struct bpf_sk_lookup, local_ip4):
10713 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10714 bpf_target_off(struct bpf_sk_lookup_kern,
10715 v4.daddr, 4, target_size));
10716 break;
10717
10718 case bpf_ctx_range_till(struct bpf_sk_lookup,
10719 remote_ip6[0], remote_ip6[3]): {
10720 #if IS_ENABLED(CONFIG_IPV6)
10721 int off = si->off;
10722
10723 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]);
10724 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
10725 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
10726 offsetof(struct bpf_sk_lookup_kern, v6.saddr));
10727 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10728 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
10729 #else
10730 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10731 #endif
10732 break;
10733 }
10734 case bpf_ctx_range_till(struct bpf_sk_lookup,
10735 local_ip6[0], local_ip6[3]): {
10736 #if IS_ENABLED(CONFIG_IPV6)
10737 int off = si->off;
10738
10739 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]);
10740 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
10741 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
10742 offsetof(struct bpf_sk_lookup_kern, v6.daddr));
10743 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10744 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
10745 #else
10746 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10747 #endif
10748 break;
10749 }
10750 case offsetof(struct bpf_sk_lookup, remote_port):
10751 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10752 bpf_target_off(struct bpf_sk_lookup_kern,
10753 sport, 2, target_size));
10754 break;
10755
10756 case offsetof(struct bpf_sk_lookup, local_port):
10757 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10758 bpf_target_off(struct bpf_sk_lookup_kern,
10759 dport, 2, target_size));
10760 break;
10761 }
10762
10763 return insn - insn_buf;
10764 }
10765
10766 const struct bpf_prog_ops sk_lookup_prog_ops = {
10767 .test_run = bpf_prog_test_run_sk_lookup,
10768 };
10769
10770 const struct bpf_verifier_ops sk_lookup_verifier_ops = {
10771 .get_func_proto = sk_lookup_func_proto,
10772 .is_valid_access = sk_lookup_is_valid_access,
10773 .convert_ctx_access = sk_lookup_convert_ctx_access,
10774 };
10775
10776 #endif /* CONFIG_INET */
10777
DEFINE_BPF_DISPATCHER(xdp)10778 DEFINE_BPF_DISPATCHER(xdp)
10779
10780 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
10781 {
10782 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
10783 }
10784
10785 #ifdef CONFIG_DEBUG_INFO_BTF
BTF_ID_LIST_GLOBAL(btf_sock_ids)10786 BTF_ID_LIST_GLOBAL(btf_sock_ids)
10787 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
10788 BTF_SOCK_TYPE_xxx
10789 #undef BTF_SOCK_TYPE
10790 #else
10791 u32 btf_sock_ids[MAX_BTF_SOCK_TYPE];
10792 #endif
10793
10794 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
10795 {
10796 /* tcp6_sock type is not generated in dwarf and hence btf,
10797 * trigger an explicit type generation here.
10798 */
10799 BTF_TYPE_EMIT(struct tcp6_sock);
10800 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
10801 sk->sk_family == AF_INET6)
10802 return (unsigned long)sk;
10803
10804 return (unsigned long)NULL;
10805 }
10806
10807 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
10808 .func = bpf_skc_to_tcp6_sock,
10809 .gpl_only = false,
10810 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10811 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10812 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
10813 };
10814
BPF_CALL_1(bpf_skc_to_tcp_sock,struct sock *,sk)10815 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
10816 {
10817 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
10818 return (unsigned long)sk;
10819
10820 return (unsigned long)NULL;
10821 }
10822
10823 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
10824 .func = bpf_skc_to_tcp_sock,
10825 .gpl_only = false,
10826 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10827 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10828 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
10829 };
10830
BPF_CALL_1(bpf_skc_to_tcp_timewait_sock,struct sock *,sk)10831 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
10832 {
10833 /* BTF types for tcp_timewait_sock and inet_timewait_sock are not
10834 * generated if CONFIG_INET=n. Trigger an explicit generation here.
10835 */
10836 BTF_TYPE_EMIT(struct inet_timewait_sock);
10837 BTF_TYPE_EMIT(struct tcp_timewait_sock);
10838
10839 #ifdef CONFIG_INET
10840 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
10841 return (unsigned long)sk;
10842 #endif
10843
10844 #if IS_BUILTIN(CONFIG_IPV6)
10845 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
10846 return (unsigned long)sk;
10847 #endif
10848
10849 return (unsigned long)NULL;
10850 }
10851
10852 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
10853 .func = bpf_skc_to_tcp_timewait_sock,
10854 .gpl_only = false,
10855 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10856 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10857 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
10858 };
10859
BPF_CALL_1(bpf_skc_to_tcp_request_sock,struct sock *,sk)10860 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
10861 {
10862 #ifdef CONFIG_INET
10863 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
10864 return (unsigned long)sk;
10865 #endif
10866
10867 #if IS_BUILTIN(CONFIG_IPV6)
10868 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
10869 return (unsigned long)sk;
10870 #endif
10871
10872 return (unsigned long)NULL;
10873 }
10874
10875 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
10876 .func = bpf_skc_to_tcp_request_sock,
10877 .gpl_only = false,
10878 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10879 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10880 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
10881 };
10882
BPF_CALL_1(bpf_skc_to_udp6_sock,struct sock *,sk)10883 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
10884 {
10885 /* udp6_sock type is not generated in dwarf and hence btf,
10886 * trigger an explicit type generation here.
10887 */
10888 BTF_TYPE_EMIT(struct udp6_sock);
10889 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
10890 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
10891 return (unsigned long)sk;
10892
10893 return (unsigned long)NULL;
10894 }
10895
10896 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
10897 .func = bpf_skc_to_udp6_sock,
10898 .gpl_only = false,
10899 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10900 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10901 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
10902 };
10903
BPF_CALL_1(bpf_sock_from_file,struct file *,file)10904 BPF_CALL_1(bpf_sock_from_file, struct file *, file)
10905 {
10906 return (unsigned long)sock_from_file(file);
10907 }
10908
10909 BTF_ID_LIST(bpf_sock_from_file_btf_ids)
10910 BTF_ID(struct, socket)
10911 BTF_ID(struct, file)
10912
10913 const struct bpf_func_proto bpf_sock_from_file_proto = {
10914 .func = bpf_sock_from_file,
10915 .gpl_only = false,
10916 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10917 .ret_btf_id = &bpf_sock_from_file_btf_ids[0],
10918 .arg1_type = ARG_PTR_TO_BTF_ID,
10919 .arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
10920 };
10921
10922 static const struct bpf_func_proto *
bpf_sk_base_func_proto(enum bpf_func_id func_id)10923 bpf_sk_base_func_proto(enum bpf_func_id func_id)
10924 {
10925 const struct bpf_func_proto *func;
10926
10927 switch (func_id) {
10928 case BPF_FUNC_skc_to_tcp6_sock:
10929 func = &bpf_skc_to_tcp6_sock_proto;
10930 break;
10931 case BPF_FUNC_skc_to_tcp_sock:
10932 func = &bpf_skc_to_tcp_sock_proto;
10933 break;
10934 case BPF_FUNC_skc_to_tcp_timewait_sock:
10935 func = &bpf_skc_to_tcp_timewait_sock_proto;
10936 break;
10937 case BPF_FUNC_skc_to_tcp_request_sock:
10938 func = &bpf_skc_to_tcp_request_sock_proto;
10939 break;
10940 case BPF_FUNC_skc_to_udp6_sock:
10941 func = &bpf_skc_to_udp6_sock_proto;
10942 break;
10943 case BPF_FUNC_ktime_get_coarse_ns:
10944 return &bpf_ktime_get_coarse_ns_proto;
10945 default:
10946 return bpf_base_func_proto(func_id);
10947 }
10948
10949 if (!perfmon_capable())
10950 return NULL;
10951
10952 return func;
10953 }
10954