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