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