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