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