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