]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blob - net/core/filter.c
bpf, sockmap: convert to generic sk_msg interface
[mirror_ubuntu-disco-kernel.git] / net / core / filter.c
1 /*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
6 *
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
8 *
9 * Authors:
10 *
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
22 */
23
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/mm.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
29 #include <linux/sock_diag.h>
30 #include <linux/in.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_packet.h>
34 #include <linux/if_arp.h>
35 #include <linux/gfp.h>
36 #include <net/inet_common.h>
37 #include <net/ip.h>
38 #include <net/protocol.h>
39 #include <net/netlink.h>
40 #include <linux/skbuff.h>
41 #include <linux/skmsg.h>
42 #include <net/sock.h>
43 #include <net/flow_dissector.h>
44 #include <linux/errno.h>
45 #include <linux/timer.h>
46 #include <linux/uaccess.h>
47 #include <asm/unaligned.h>
48 #include <asm/cmpxchg.h>
49 #include <linux/filter.h>
50 #include <linux/ratelimit.h>
51 #include <linux/seccomp.h>
52 #include <linux/if_vlan.h>
53 #include <linux/bpf.h>
54 #include <net/sch_generic.h>
55 #include <net/cls_cgroup.h>
56 #include <net/dst_metadata.h>
57 #include <net/dst.h>
58 #include <net/sock_reuseport.h>
59 #include <net/busy_poll.h>
60 #include <net/tcp.h>
61 #include <net/xfrm.h>
62 #include <net/udp.h>
63 #include <linux/bpf_trace.h>
64 #include <net/xdp_sock.h>
65 #include <linux/inetdevice.h>
66 #include <net/inet_hashtables.h>
67 #include <net/inet6_hashtables.h>
68 #include <net/ip_fib.h>
69 #include <net/flow.h>
70 #include <net/arp.h>
71 #include <net/ipv6.h>
72 #include <net/net_namespace.h>
73 #include <linux/seg6_local.h>
74 #include <net/seg6.h>
75 #include <net/seg6_local.h>
76
77 /**
78 * sk_filter_trim_cap - run a packet through a socket filter
79 * @sk: sock associated with &sk_buff
80 * @skb: buffer to filter
81 * @cap: limit on how short the eBPF program may trim the packet
82 *
83 * Run the eBPF program and then cut skb->data to correct size returned by
84 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
85 * than pkt_len we keep whole skb->data. This is the socket level
86 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
87 * be accepted or -EPERM if the packet should be tossed.
88 *
89 */
90 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
91 {
92 int err;
93 struct sk_filter *filter;
94
95 /*
96 * If the skb was allocated from pfmemalloc reserves, only
97 * allow SOCK_MEMALLOC sockets to use it as this socket is
98 * helping free memory
99 */
100 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
101 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
102 return -ENOMEM;
103 }
104 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
105 if (err)
106 return err;
107
108 err = security_sock_rcv_skb(sk, skb);
109 if (err)
110 return err;
111
112 rcu_read_lock();
113 filter = rcu_dereference(sk->sk_filter);
114 if (filter) {
115 struct sock *save_sk = skb->sk;
116 unsigned int pkt_len;
117
118 skb->sk = sk;
119 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
120 skb->sk = save_sk;
121 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
122 }
123 rcu_read_unlock();
124
125 return err;
126 }
127 EXPORT_SYMBOL(sk_filter_trim_cap);
128
129 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
130 {
131 return skb_get_poff(skb);
132 }
133
134 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
135 {
136 struct nlattr *nla;
137
138 if (skb_is_nonlinear(skb))
139 return 0;
140
141 if (skb->len < sizeof(struct nlattr))
142 return 0;
143
144 if (a > skb->len - sizeof(struct nlattr))
145 return 0;
146
147 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
148 if (nla)
149 return (void *) nla - (void *) skb->data;
150
151 return 0;
152 }
153
154 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
155 {
156 struct nlattr *nla;
157
158 if (skb_is_nonlinear(skb))
159 return 0;
160
161 if (skb->len < sizeof(struct nlattr))
162 return 0;
163
164 if (a > skb->len - sizeof(struct nlattr))
165 return 0;
166
167 nla = (struct nlattr *) &skb->data[a];
168 if (nla->nla_len > skb->len - a)
169 return 0;
170
171 nla = nla_find_nested(nla, x);
172 if (nla)
173 return (void *) nla - (void *) skb->data;
174
175 return 0;
176 }
177
178 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
179 data, int, headlen, int, offset)
180 {
181 u8 tmp, *ptr;
182 const int len = sizeof(tmp);
183
184 if (offset >= 0) {
185 if (headlen - offset >= len)
186 return *(u8 *)(data + offset);
187 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
188 return tmp;
189 } else {
190 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
191 if (likely(ptr))
192 return *(u8 *)ptr;
193 }
194
195 return -EFAULT;
196 }
197
198 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
199 int, offset)
200 {
201 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
202 offset);
203 }
204
205 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
206 data, int, headlen, int, offset)
207 {
208 u16 tmp, *ptr;
209 const int len = sizeof(tmp);
210
211 if (offset >= 0) {
212 if (headlen - offset >= len)
213 return get_unaligned_be16(data + offset);
214 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
215 return be16_to_cpu(tmp);
216 } else {
217 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
218 if (likely(ptr))
219 return get_unaligned_be16(ptr);
220 }
221
222 return -EFAULT;
223 }
224
225 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
226 int, offset)
227 {
228 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
229 offset);
230 }
231
232 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
233 data, int, headlen, int, offset)
234 {
235 u32 tmp, *ptr;
236 const int len = sizeof(tmp);
237
238 if (likely(offset >= 0)) {
239 if (headlen - offset >= len)
240 return get_unaligned_be32(data + offset);
241 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
242 return be32_to_cpu(tmp);
243 } else {
244 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
245 if (likely(ptr))
246 return get_unaligned_be32(ptr);
247 }
248
249 return -EFAULT;
250 }
251
252 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
253 int, offset)
254 {
255 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
256 offset);
257 }
258
259 BPF_CALL_0(bpf_get_raw_cpu_id)
260 {
261 return raw_smp_processor_id();
262 }
263
264 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
265 .func = bpf_get_raw_cpu_id,
266 .gpl_only = false,
267 .ret_type = RET_INTEGER,
268 };
269
270 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
271 struct bpf_insn *insn_buf)
272 {
273 struct bpf_insn *insn = insn_buf;
274
275 switch (skb_field) {
276 case SKF_AD_MARK:
277 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
278
279 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
280 offsetof(struct sk_buff, mark));
281 break;
282
283 case SKF_AD_PKTTYPE:
284 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
285 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
286 #ifdef __BIG_ENDIAN_BITFIELD
287 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
288 #endif
289 break;
290
291 case SKF_AD_QUEUE:
292 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
293
294 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
295 offsetof(struct sk_buff, queue_mapping));
296 break;
297
298 case SKF_AD_VLAN_TAG:
299 case SKF_AD_VLAN_TAG_PRESENT:
300 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
301 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
302
303 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
304 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
305 offsetof(struct sk_buff, vlan_tci));
306 if (skb_field == SKF_AD_VLAN_TAG) {
307 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
308 ~VLAN_TAG_PRESENT);
309 } else {
310 /* dst_reg >>= 12 */
311 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
312 /* dst_reg &= 1 */
313 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
314 }
315 break;
316 }
317
318 return insn - insn_buf;
319 }
320
321 static bool convert_bpf_extensions(struct sock_filter *fp,
322 struct bpf_insn **insnp)
323 {
324 struct bpf_insn *insn = *insnp;
325 u32 cnt;
326
327 switch (fp->k) {
328 case SKF_AD_OFF + SKF_AD_PROTOCOL:
329 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
330
331 /* A = *(u16 *) (CTX + offsetof(protocol)) */
332 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
333 offsetof(struct sk_buff, protocol));
334 /* A = ntohs(A) [emitting a nop or swap16] */
335 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
336 break;
337
338 case SKF_AD_OFF + SKF_AD_PKTTYPE:
339 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
340 insn += cnt - 1;
341 break;
342
343 case SKF_AD_OFF + SKF_AD_IFINDEX:
344 case SKF_AD_OFF + SKF_AD_HATYPE:
345 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
346 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
347
348 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
349 BPF_REG_TMP, BPF_REG_CTX,
350 offsetof(struct sk_buff, dev));
351 /* if (tmp != 0) goto pc + 1 */
352 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
353 *insn++ = BPF_EXIT_INSN();
354 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
355 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
356 offsetof(struct net_device, ifindex));
357 else
358 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
359 offsetof(struct net_device, type));
360 break;
361
362 case SKF_AD_OFF + SKF_AD_MARK:
363 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
364 insn += cnt - 1;
365 break;
366
367 case SKF_AD_OFF + SKF_AD_RXHASH:
368 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
369
370 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
371 offsetof(struct sk_buff, hash));
372 break;
373
374 case SKF_AD_OFF + SKF_AD_QUEUE:
375 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
376 insn += cnt - 1;
377 break;
378
379 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
380 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
381 BPF_REG_A, BPF_REG_CTX, insn);
382 insn += cnt - 1;
383 break;
384
385 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
386 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
387 BPF_REG_A, BPF_REG_CTX, insn);
388 insn += cnt - 1;
389 break;
390
391 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
392 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
393
394 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
395 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
396 offsetof(struct sk_buff, vlan_proto));
397 /* A = ntohs(A) [emitting a nop or swap16] */
398 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
399 break;
400
401 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
402 case SKF_AD_OFF + SKF_AD_NLATTR:
403 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
404 case SKF_AD_OFF + SKF_AD_CPU:
405 case SKF_AD_OFF + SKF_AD_RANDOM:
406 /* arg1 = CTX */
407 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
408 /* arg2 = A */
409 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
410 /* arg3 = X */
411 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
412 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
413 switch (fp->k) {
414 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
415 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
416 break;
417 case SKF_AD_OFF + SKF_AD_NLATTR:
418 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
419 break;
420 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
421 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
422 break;
423 case SKF_AD_OFF + SKF_AD_CPU:
424 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
425 break;
426 case SKF_AD_OFF + SKF_AD_RANDOM:
427 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
428 bpf_user_rnd_init_once();
429 break;
430 }
431 break;
432
433 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
434 /* A ^= X */
435 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
436 break;
437
438 default:
439 /* This is just a dummy call to avoid letting the compiler
440 * evict __bpf_call_base() as an optimization. Placed here
441 * where no-one bothers.
442 */
443 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
444 return false;
445 }
446
447 *insnp = insn;
448 return true;
449 }
450
451 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
452 {
453 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
454 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
455 bool endian = BPF_SIZE(fp->code) == BPF_H ||
456 BPF_SIZE(fp->code) == BPF_W;
457 bool indirect = BPF_MODE(fp->code) == BPF_IND;
458 const int ip_align = NET_IP_ALIGN;
459 struct bpf_insn *insn = *insnp;
460 int offset = fp->k;
461
462 if (!indirect &&
463 ((unaligned_ok && offset >= 0) ||
464 (!unaligned_ok && offset >= 0 &&
465 offset + ip_align >= 0 &&
466 offset + ip_align % size == 0))) {
467 bool ldx_off_ok = offset <= S16_MAX;
468
469 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
470 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
471 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
472 size, 2 + endian + (!ldx_off_ok * 2));
473 if (ldx_off_ok) {
474 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
475 BPF_REG_D, offset);
476 } else {
477 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
478 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
479 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
480 BPF_REG_TMP, 0);
481 }
482 if (endian)
483 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
484 *insn++ = BPF_JMP_A(8);
485 }
486
487 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
488 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
489 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
490 if (!indirect) {
491 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
492 } else {
493 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
494 if (fp->k)
495 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
496 }
497
498 switch (BPF_SIZE(fp->code)) {
499 case BPF_B:
500 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
501 break;
502 case BPF_H:
503 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
504 break;
505 case BPF_W:
506 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
507 break;
508 default:
509 return false;
510 }
511
512 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
513 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
514 *insn = BPF_EXIT_INSN();
515
516 *insnp = insn;
517 return true;
518 }
519
520 /**
521 * bpf_convert_filter - convert filter program
522 * @prog: the user passed filter program
523 * @len: the length of the user passed filter program
524 * @new_prog: allocated 'struct bpf_prog' or NULL
525 * @new_len: pointer to store length of converted program
526 * @seen_ld_abs: bool whether we've seen ld_abs/ind
527 *
528 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
529 * style extended BPF (eBPF).
530 * Conversion workflow:
531 *
532 * 1) First pass for calculating the new program length:
533 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
534 *
535 * 2) 2nd pass to remap in two passes: 1st pass finds new
536 * jump offsets, 2nd pass remapping:
537 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
538 */
539 static int bpf_convert_filter(struct sock_filter *prog, int len,
540 struct bpf_prog *new_prog, int *new_len,
541 bool *seen_ld_abs)
542 {
543 int new_flen = 0, pass = 0, target, i, stack_off;
544 struct bpf_insn *new_insn, *first_insn = NULL;
545 struct sock_filter *fp;
546 int *addrs = NULL;
547 u8 bpf_src;
548
549 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
550 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
551
552 if (len <= 0 || len > BPF_MAXINSNS)
553 return -EINVAL;
554
555 if (new_prog) {
556 first_insn = new_prog->insnsi;
557 addrs = kcalloc(len, sizeof(*addrs),
558 GFP_KERNEL | __GFP_NOWARN);
559 if (!addrs)
560 return -ENOMEM;
561 }
562
563 do_pass:
564 new_insn = first_insn;
565 fp = prog;
566
567 /* Classic BPF related prologue emission. */
568 if (new_prog) {
569 /* Classic BPF expects A and X to be reset first. These need
570 * to be guaranteed to be the first two instructions.
571 */
572 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
573 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
574
575 /* All programs must keep CTX in callee saved BPF_REG_CTX.
576 * In eBPF case it's done by the compiler, here we need to
577 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
578 */
579 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
580 if (*seen_ld_abs) {
581 /* For packet access in classic BPF, cache skb->data
582 * in callee-saved BPF R8 and skb->len - skb->data_len
583 * (headlen) in BPF R9. Since classic BPF is read-only
584 * on CTX, we only need to cache it once.
585 */
586 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
587 BPF_REG_D, BPF_REG_CTX,
588 offsetof(struct sk_buff, data));
589 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
590 offsetof(struct sk_buff, len));
591 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
592 offsetof(struct sk_buff, data_len));
593 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
594 }
595 } else {
596 new_insn += 3;
597 }
598
599 for (i = 0; i < len; fp++, i++) {
600 struct bpf_insn tmp_insns[32] = { };
601 struct bpf_insn *insn = tmp_insns;
602
603 if (addrs)
604 addrs[i] = new_insn - first_insn;
605
606 switch (fp->code) {
607 /* All arithmetic insns and skb loads map as-is. */
608 case BPF_ALU | BPF_ADD | BPF_X:
609 case BPF_ALU | BPF_ADD | BPF_K:
610 case BPF_ALU | BPF_SUB | BPF_X:
611 case BPF_ALU | BPF_SUB | BPF_K:
612 case BPF_ALU | BPF_AND | BPF_X:
613 case BPF_ALU | BPF_AND | BPF_K:
614 case BPF_ALU | BPF_OR | BPF_X:
615 case BPF_ALU | BPF_OR | BPF_K:
616 case BPF_ALU | BPF_LSH | BPF_X:
617 case BPF_ALU | BPF_LSH | BPF_K:
618 case BPF_ALU | BPF_RSH | BPF_X:
619 case BPF_ALU | BPF_RSH | BPF_K:
620 case BPF_ALU | BPF_XOR | BPF_X:
621 case BPF_ALU | BPF_XOR | BPF_K:
622 case BPF_ALU | BPF_MUL | BPF_X:
623 case BPF_ALU | BPF_MUL | BPF_K:
624 case BPF_ALU | BPF_DIV | BPF_X:
625 case BPF_ALU | BPF_DIV | BPF_K:
626 case BPF_ALU | BPF_MOD | BPF_X:
627 case BPF_ALU | BPF_MOD | BPF_K:
628 case BPF_ALU | BPF_NEG:
629 case BPF_LD | BPF_ABS | BPF_W:
630 case BPF_LD | BPF_ABS | BPF_H:
631 case BPF_LD | BPF_ABS | BPF_B:
632 case BPF_LD | BPF_IND | BPF_W:
633 case BPF_LD | BPF_IND | BPF_H:
634 case BPF_LD | BPF_IND | BPF_B:
635 /* Check for overloaded BPF extension and
636 * directly convert it if found, otherwise
637 * just move on with mapping.
638 */
639 if (BPF_CLASS(fp->code) == BPF_LD &&
640 BPF_MODE(fp->code) == BPF_ABS &&
641 convert_bpf_extensions(fp, &insn))
642 break;
643 if (BPF_CLASS(fp->code) == BPF_LD &&
644 convert_bpf_ld_abs(fp, &insn)) {
645 *seen_ld_abs = true;
646 break;
647 }
648
649 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
650 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
651 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
652 /* Error with exception code on div/mod by 0.
653 * For cBPF programs, this was always return 0.
654 */
655 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
656 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
657 *insn++ = BPF_EXIT_INSN();
658 }
659
660 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
661 break;
662
663 /* Jump transformation cannot use BPF block macros
664 * everywhere as offset calculation and target updates
665 * require a bit more work than the rest, i.e. jump
666 * opcodes map as-is, but offsets need adjustment.
667 */
668
669 #define BPF_EMIT_JMP \
670 do { \
671 const s32 off_min = S16_MIN, off_max = S16_MAX; \
672 s32 off; \
673 \
674 if (target >= len || target < 0) \
675 goto err; \
676 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
677 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
678 off -= insn - tmp_insns; \
679 /* Reject anything not fitting into insn->off. */ \
680 if (off < off_min || off > off_max) \
681 goto err; \
682 insn->off = off; \
683 } while (0)
684
685 case BPF_JMP | BPF_JA:
686 target = i + fp->k + 1;
687 insn->code = fp->code;
688 BPF_EMIT_JMP;
689 break;
690
691 case BPF_JMP | BPF_JEQ | BPF_K:
692 case BPF_JMP | BPF_JEQ | BPF_X:
693 case BPF_JMP | BPF_JSET | BPF_K:
694 case BPF_JMP | BPF_JSET | BPF_X:
695 case BPF_JMP | BPF_JGT | BPF_K:
696 case BPF_JMP | BPF_JGT | BPF_X:
697 case BPF_JMP | BPF_JGE | BPF_K:
698 case BPF_JMP | BPF_JGE | BPF_X:
699 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
700 /* BPF immediates are signed, zero extend
701 * immediate into tmp register and use it
702 * in compare insn.
703 */
704 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
705
706 insn->dst_reg = BPF_REG_A;
707 insn->src_reg = BPF_REG_TMP;
708 bpf_src = BPF_X;
709 } else {
710 insn->dst_reg = BPF_REG_A;
711 insn->imm = fp->k;
712 bpf_src = BPF_SRC(fp->code);
713 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
714 }
715
716 /* Common case where 'jump_false' is next insn. */
717 if (fp->jf == 0) {
718 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
719 target = i + fp->jt + 1;
720 BPF_EMIT_JMP;
721 break;
722 }
723
724 /* Convert some jumps when 'jump_true' is next insn. */
725 if (fp->jt == 0) {
726 switch (BPF_OP(fp->code)) {
727 case BPF_JEQ:
728 insn->code = BPF_JMP | BPF_JNE | bpf_src;
729 break;
730 case BPF_JGT:
731 insn->code = BPF_JMP | BPF_JLE | bpf_src;
732 break;
733 case BPF_JGE:
734 insn->code = BPF_JMP | BPF_JLT | bpf_src;
735 break;
736 default:
737 goto jmp_rest;
738 }
739
740 target = i + fp->jf + 1;
741 BPF_EMIT_JMP;
742 break;
743 }
744 jmp_rest:
745 /* Other jumps are mapped into two insns: Jxx and JA. */
746 target = i + fp->jt + 1;
747 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
748 BPF_EMIT_JMP;
749 insn++;
750
751 insn->code = BPF_JMP | BPF_JA;
752 target = i + fp->jf + 1;
753 BPF_EMIT_JMP;
754 break;
755
756 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
757 case BPF_LDX | BPF_MSH | BPF_B: {
758 struct sock_filter tmp = {
759 .code = BPF_LD | BPF_ABS | BPF_B,
760 .k = fp->k,
761 };
762
763 *seen_ld_abs = true;
764
765 /* X = A */
766 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
767 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
768 convert_bpf_ld_abs(&tmp, &insn);
769 insn++;
770 /* A &= 0xf */
771 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
772 /* A <<= 2 */
773 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
774 /* tmp = X */
775 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
776 /* X = A */
777 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
778 /* A = tmp */
779 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
780 break;
781 }
782 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
783 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
784 */
785 case BPF_RET | BPF_A:
786 case BPF_RET | BPF_K:
787 if (BPF_RVAL(fp->code) == BPF_K)
788 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
789 0, fp->k);
790 *insn = BPF_EXIT_INSN();
791 break;
792
793 /* Store to stack. */
794 case BPF_ST:
795 case BPF_STX:
796 stack_off = fp->k * 4 + 4;
797 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
798 BPF_ST ? BPF_REG_A : BPF_REG_X,
799 -stack_off);
800 /* check_load_and_stores() verifies that classic BPF can
801 * load from stack only after write, so tracking
802 * stack_depth for ST|STX insns is enough
803 */
804 if (new_prog && new_prog->aux->stack_depth < stack_off)
805 new_prog->aux->stack_depth = stack_off;
806 break;
807
808 /* Load from stack. */
809 case BPF_LD | BPF_MEM:
810 case BPF_LDX | BPF_MEM:
811 stack_off = fp->k * 4 + 4;
812 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
813 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
814 -stack_off);
815 break;
816
817 /* A = K or X = K */
818 case BPF_LD | BPF_IMM:
819 case BPF_LDX | BPF_IMM:
820 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
821 BPF_REG_A : BPF_REG_X, fp->k);
822 break;
823
824 /* X = A */
825 case BPF_MISC | BPF_TAX:
826 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
827 break;
828
829 /* A = X */
830 case BPF_MISC | BPF_TXA:
831 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
832 break;
833
834 /* A = skb->len or X = skb->len */
835 case BPF_LD | BPF_W | BPF_LEN:
836 case BPF_LDX | BPF_W | BPF_LEN:
837 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
838 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
839 offsetof(struct sk_buff, len));
840 break;
841
842 /* Access seccomp_data fields. */
843 case BPF_LDX | BPF_ABS | BPF_W:
844 /* A = *(u32 *) (ctx + K) */
845 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
846 break;
847
848 /* Unknown instruction. */
849 default:
850 goto err;
851 }
852
853 insn++;
854 if (new_prog)
855 memcpy(new_insn, tmp_insns,
856 sizeof(*insn) * (insn - tmp_insns));
857 new_insn += insn - tmp_insns;
858 }
859
860 if (!new_prog) {
861 /* Only calculating new length. */
862 *new_len = new_insn - first_insn;
863 if (*seen_ld_abs)
864 *new_len += 4; /* Prologue bits. */
865 return 0;
866 }
867
868 pass++;
869 if (new_flen != new_insn - first_insn) {
870 new_flen = new_insn - first_insn;
871 if (pass > 2)
872 goto err;
873 goto do_pass;
874 }
875
876 kfree(addrs);
877 BUG_ON(*new_len != new_flen);
878 return 0;
879 err:
880 kfree(addrs);
881 return -EINVAL;
882 }
883
884 /* Security:
885 *
886 * As we dont want to clear mem[] array for each packet going through
887 * __bpf_prog_run(), we check that filter loaded by user never try to read
888 * a cell if not previously written, and we check all branches to be sure
889 * a malicious user doesn't try to abuse us.
890 */
891 static int check_load_and_stores(const struct sock_filter *filter, int flen)
892 {
893 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
894 int pc, ret = 0;
895
896 BUILD_BUG_ON(BPF_MEMWORDS > 16);
897
898 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
899 if (!masks)
900 return -ENOMEM;
901
902 memset(masks, 0xff, flen * sizeof(*masks));
903
904 for (pc = 0; pc < flen; pc++) {
905 memvalid &= masks[pc];
906
907 switch (filter[pc].code) {
908 case BPF_ST:
909 case BPF_STX:
910 memvalid |= (1 << filter[pc].k);
911 break;
912 case BPF_LD | BPF_MEM:
913 case BPF_LDX | BPF_MEM:
914 if (!(memvalid & (1 << filter[pc].k))) {
915 ret = -EINVAL;
916 goto error;
917 }
918 break;
919 case BPF_JMP | BPF_JA:
920 /* A jump must set masks on target */
921 masks[pc + 1 + filter[pc].k] &= memvalid;
922 memvalid = ~0;
923 break;
924 case BPF_JMP | BPF_JEQ | BPF_K:
925 case BPF_JMP | BPF_JEQ | BPF_X:
926 case BPF_JMP | BPF_JGE | BPF_K:
927 case BPF_JMP | BPF_JGE | BPF_X:
928 case BPF_JMP | BPF_JGT | BPF_K:
929 case BPF_JMP | BPF_JGT | BPF_X:
930 case BPF_JMP | BPF_JSET | BPF_K:
931 case BPF_JMP | BPF_JSET | BPF_X:
932 /* A jump must set masks on targets */
933 masks[pc + 1 + filter[pc].jt] &= memvalid;
934 masks[pc + 1 + filter[pc].jf] &= memvalid;
935 memvalid = ~0;
936 break;
937 }
938 }
939 error:
940 kfree(masks);
941 return ret;
942 }
943
944 static bool chk_code_allowed(u16 code_to_probe)
945 {
946 static const bool codes[] = {
947 /* 32 bit ALU operations */
948 [BPF_ALU | BPF_ADD | BPF_K] = true,
949 [BPF_ALU | BPF_ADD | BPF_X] = true,
950 [BPF_ALU | BPF_SUB | BPF_K] = true,
951 [BPF_ALU | BPF_SUB | BPF_X] = true,
952 [BPF_ALU | BPF_MUL | BPF_K] = true,
953 [BPF_ALU | BPF_MUL | BPF_X] = true,
954 [BPF_ALU | BPF_DIV | BPF_K] = true,
955 [BPF_ALU | BPF_DIV | BPF_X] = true,
956 [BPF_ALU | BPF_MOD | BPF_K] = true,
957 [BPF_ALU | BPF_MOD | BPF_X] = true,
958 [BPF_ALU | BPF_AND | BPF_K] = true,
959 [BPF_ALU | BPF_AND | BPF_X] = true,
960 [BPF_ALU | BPF_OR | BPF_K] = true,
961 [BPF_ALU | BPF_OR | BPF_X] = true,
962 [BPF_ALU | BPF_XOR | BPF_K] = true,
963 [BPF_ALU | BPF_XOR | BPF_X] = true,
964 [BPF_ALU | BPF_LSH | BPF_K] = true,
965 [BPF_ALU | BPF_LSH | BPF_X] = true,
966 [BPF_ALU | BPF_RSH | BPF_K] = true,
967 [BPF_ALU | BPF_RSH | BPF_X] = true,
968 [BPF_ALU | BPF_NEG] = true,
969 /* Load instructions */
970 [BPF_LD | BPF_W | BPF_ABS] = true,
971 [BPF_LD | BPF_H | BPF_ABS] = true,
972 [BPF_LD | BPF_B | BPF_ABS] = true,
973 [BPF_LD | BPF_W | BPF_LEN] = true,
974 [BPF_LD | BPF_W | BPF_IND] = true,
975 [BPF_LD | BPF_H | BPF_IND] = true,
976 [BPF_LD | BPF_B | BPF_IND] = true,
977 [BPF_LD | BPF_IMM] = true,
978 [BPF_LD | BPF_MEM] = true,
979 [BPF_LDX | BPF_W | BPF_LEN] = true,
980 [BPF_LDX | BPF_B | BPF_MSH] = true,
981 [BPF_LDX | BPF_IMM] = true,
982 [BPF_LDX | BPF_MEM] = true,
983 /* Store instructions */
984 [BPF_ST] = true,
985 [BPF_STX] = true,
986 /* Misc instructions */
987 [BPF_MISC | BPF_TAX] = true,
988 [BPF_MISC | BPF_TXA] = true,
989 /* Return instructions */
990 [BPF_RET | BPF_K] = true,
991 [BPF_RET | BPF_A] = true,
992 /* Jump instructions */
993 [BPF_JMP | BPF_JA] = true,
994 [BPF_JMP | BPF_JEQ | BPF_K] = true,
995 [BPF_JMP | BPF_JEQ | BPF_X] = true,
996 [BPF_JMP | BPF_JGE | BPF_K] = true,
997 [BPF_JMP | BPF_JGE | BPF_X] = true,
998 [BPF_JMP | BPF_JGT | BPF_K] = true,
999 [BPF_JMP | BPF_JGT | BPF_X] = true,
1000 [BPF_JMP | BPF_JSET | BPF_K] = true,
1001 [BPF_JMP | BPF_JSET | BPF_X] = true,
1002 };
1003
1004 if (code_to_probe >= ARRAY_SIZE(codes))
1005 return false;
1006
1007 return codes[code_to_probe];
1008 }
1009
1010 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1011 unsigned int flen)
1012 {
1013 if (filter == NULL)
1014 return false;
1015 if (flen == 0 || flen > BPF_MAXINSNS)
1016 return false;
1017
1018 return true;
1019 }
1020
1021 /**
1022 * bpf_check_classic - verify socket filter code
1023 * @filter: filter to verify
1024 * @flen: length of filter
1025 *
1026 * Check the user's filter code. If we let some ugly
1027 * filter code slip through kaboom! The filter must contain
1028 * no references or jumps that are out of range, no illegal
1029 * instructions, and must end with a RET instruction.
1030 *
1031 * All jumps are forward as they are not signed.
1032 *
1033 * Returns 0 if the rule set is legal or -EINVAL if not.
1034 */
1035 static int bpf_check_classic(const struct sock_filter *filter,
1036 unsigned int flen)
1037 {
1038 bool anc_found;
1039 int pc;
1040
1041 /* Check the filter code now */
1042 for (pc = 0; pc < flen; pc++) {
1043 const struct sock_filter *ftest = &filter[pc];
1044
1045 /* May we actually operate on this code? */
1046 if (!chk_code_allowed(ftest->code))
1047 return -EINVAL;
1048
1049 /* Some instructions need special checks */
1050 switch (ftest->code) {
1051 case BPF_ALU | BPF_DIV | BPF_K:
1052 case BPF_ALU | BPF_MOD | BPF_K:
1053 /* Check for division by zero */
1054 if (ftest->k == 0)
1055 return -EINVAL;
1056 break;
1057 case BPF_ALU | BPF_LSH | BPF_K:
1058 case BPF_ALU | BPF_RSH | BPF_K:
1059 if (ftest->k >= 32)
1060 return -EINVAL;
1061 break;
1062 case BPF_LD | BPF_MEM:
1063 case BPF_LDX | BPF_MEM:
1064 case BPF_ST:
1065 case BPF_STX:
1066 /* Check for invalid memory addresses */
1067 if (ftest->k >= BPF_MEMWORDS)
1068 return -EINVAL;
1069 break;
1070 case BPF_JMP | BPF_JA:
1071 /* Note, the large ftest->k might cause loops.
1072 * Compare this with conditional jumps below,
1073 * where offsets are limited. --ANK (981016)
1074 */
1075 if (ftest->k >= (unsigned int)(flen - pc - 1))
1076 return -EINVAL;
1077 break;
1078 case BPF_JMP | BPF_JEQ | BPF_K:
1079 case BPF_JMP | BPF_JEQ | BPF_X:
1080 case BPF_JMP | BPF_JGE | BPF_K:
1081 case BPF_JMP | BPF_JGE | BPF_X:
1082 case BPF_JMP | BPF_JGT | BPF_K:
1083 case BPF_JMP | BPF_JGT | BPF_X:
1084 case BPF_JMP | BPF_JSET | BPF_K:
1085 case BPF_JMP | BPF_JSET | BPF_X:
1086 /* Both conditionals must be safe */
1087 if (pc + ftest->jt + 1 >= flen ||
1088 pc + ftest->jf + 1 >= flen)
1089 return -EINVAL;
1090 break;
1091 case BPF_LD | BPF_W | BPF_ABS:
1092 case BPF_LD | BPF_H | BPF_ABS:
1093 case BPF_LD | BPF_B | BPF_ABS:
1094 anc_found = false;
1095 if (bpf_anc_helper(ftest) & BPF_ANC)
1096 anc_found = true;
1097 /* Ancillary operation unknown or unsupported */
1098 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1099 return -EINVAL;
1100 }
1101 }
1102
1103 /* Last instruction must be a RET code */
1104 switch (filter[flen - 1].code) {
1105 case BPF_RET | BPF_K:
1106 case BPF_RET | BPF_A:
1107 return check_load_and_stores(filter, flen);
1108 }
1109
1110 return -EINVAL;
1111 }
1112
1113 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1114 const struct sock_fprog *fprog)
1115 {
1116 unsigned int fsize = bpf_classic_proglen(fprog);
1117 struct sock_fprog_kern *fkprog;
1118
1119 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1120 if (!fp->orig_prog)
1121 return -ENOMEM;
1122
1123 fkprog = fp->orig_prog;
1124 fkprog->len = fprog->len;
1125
1126 fkprog->filter = kmemdup(fp->insns, fsize,
1127 GFP_KERNEL | __GFP_NOWARN);
1128 if (!fkprog->filter) {
1129 kfree(fp->orig_prog);
1130 return -ENOMEM;
1131 }
1132
1133 return 0;
1134 }
1135
1136 static void bpf_release_orig_filter(struct bpf_prog *fp)
1137 {
1138 struct sock_fprog_kern *fprog = fp->orig_prog;
1139
1140 if (fprog) {
1141 kfree(fprog->filter);
1142 kfree(fprog);
1143 }
1144 }
1145
1146 static void __bpf_prog_release(struct bpf_prog *prog)
1147 {
1148 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1149 bpf_prog_put(prog);
1150 } else {
1151 bpf_release_orig_filter(prog);
1152 bpf_prog_free(prog);
1153 }
1154 }
1155
1156 static void __sk_filter_release(struct sk_filter *fp)
1157 {
1158 __bpf_prog_release(fp->prog);
1159 kfree(fp);
1160 }
1161
1162 /**
1163 * sk_filter_release_rcu - Release a socket filter by rcu_head
1164 * @rcu: rcu_head that contains the sk_filter to free
1165 */
1166 static void sk_filter_release_rcu(struct rcu_head *rcu)
1167 {
1168 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1169
1170 __sk_filter_release(fp);
1171 }
1172
1173 /**
1174 * sk_filter_release - release a socket filter
1175 * @fp: filter to remove
1176 *
1177 * Remove a filter from a socket and release its resources.
1178 */
1179 static void sk_filter_release(struct sk_filter *fp)
1180 {
1181 if (refcount_dec_and_test(&fp->refcnt))
1182 call_rcu(&fp->rcu, sk_filter_release_rcu);
1183 }
1184
1185 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1186 {
1187 u32 filter_size = bpf_prog_size(fp->prog->len);
1188
1189 atomic_sub(filter_size, &sk->sk_omem_alloc);
1190 sk_filter_release(fp);
1191 }
1192
1193 /* try to charge the socket memory if there is space available
1194 * return true on success
1195 */
1196 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1197 {
1198 u32 filter_size = bpf_prog_size(fp->prog->len);
1199
1200 /* same check as in sock_kmalloc() */
1201 if (filter_size <= sysctl_optmem_max &&
1202 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1203 atomic_add(filter_size, &sk->sk_omem_alloc);
1204 return true;
1205 }
1206 return false;
1207 }
1208
1209 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1210 {
1211 if (!refcount_inc_not_zero(&fp->refcnt))
1212 return false;
1213
1214 if (!__sk_filter_charge(sk, fp)) {
1215 sk_filter_release(fp);
1216 return false;
1217 }
1218 return true;
1219 }
1220
1221 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1222 {
1223 struct sock_filter *old_prog;
1224 struct bpf_prog *old_fp;
1225 int err, new_len, old_len = fp->len;
1226 bool seen_ld_abs = false;
1227
1228 /* We are free to overwrite insns et al right here as it
1229 * won't be used at this point in time anymore internally
1230 * after the migration to the internal BPF instruction
1231 * representation.
1232 */
1233 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1234 sizeof(struct bpf_insn));
1235
1236 /* Conversion cannot happen on overlapping memory areas,
1237 * so we need to keep the user BPF around until the 2nd
1238 * pass. At this time, the user BPF is stored in fp->insns.
1239 */
1240 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1241 GFP_KERNEL | __GFP_NOWARN);
1242 if (!old_prog) {
1243 err = -ENOMEM;
1244 goto out_err;
1245 }
1246
1247 /* 1st pass: calculate the new program length. */
1248 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1249 &seen_ld_abs);
1250 if (err)
1251 goto out_err_free;
1252
1253 /* Expand fp for appending the new filter representation. */
1254 old_fp = fp;
1255 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1256 if (!fp) {
1257 /* The old_fp is still around in case we couldn't
1258 * allocate new memory, so uncharge on that one.
1259 */
1260 fp = old_fp;
1261 err = -ENOMEM;
1262 goto out_err_free;
1263 }
1264
1265 fp->len = new_len;
1266
1267 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1268 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1269 &seen_ld_abs);
1270 if (err)
1271 /* 2nd bpf_convert_filter() can fail only if it fails
1272 * to allocate memory, remapping must succeed. Note,
1273 * that at this time old_fp has already been released
1274 * by krealloc().
1275 */
1276 goto out_err_free;
1277
1278 fp = bpf_prog_select_runtime(fp, &err);
1279 if (err)
1280 goto out_err_free;
1281
1282 kfree(old_prog);
1283 return fp;
1284
1285 out_err_free:
1286 kfree(old_prog);
1287 out_err:
1288 __bpf_prog_release(fp);
1289 return ERR_PTR(err);
1290 }
1291
1292 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1293 bpf_aux_classic_check_t trans)
1294 {
1295 int err;
1296
1297 fp->bpf_func = NULL;
1298 fp->jited = 0;
1299
1300 err = bpf_check_classic(fp->insns, fp->len);
1301 if (err) {
1302 __bpf_prog_release(fp);
1303 return ERR_PTR(err);
1304 }
1305
1306 /* There might be additional checks and transformations
1307 * needed on classic filters, f.e. in case of seccomp.
1308 */
1309 if (trans) {
1310 err = trans(fp->insns, fp->len);
1311 if (err) {
1312 __bpf_prog_release(fp);
1313 return ERR_PTR(err);
1314 }
1315 }
1316
1317 /* Probe if we can JIT compile the filter and if so, do
1318 * the compilation of the filter.
1319 */
1320 bpf_jit_compile(fp);
1321
1322 /* JIT compiler couldn't process this filter, so do the
1323 * internal BPF translation for the optimized interpreter.
1324 */
1325 if (!fp->jited)
1326 fp = bpf_migrate_filter(fp);
1327
1328 return fp;
1329 }
1330
1331 /**
1332 * bpf_prog_create - create an unattached filter
1333 * @pfp: the unattached filter that is created
1334 * @fprog: the filter program
1335 *
1336 * Create a filter independent of any socket. We first run some
1337 * sanity checks on it to make sure it does not explode on us later.
1338 * If an error occurs or there is insufficient memory for the filter
1339 * a negative errno code is returned. On success the return is zero.
1340 */
1341 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1342 {
1343 unsigned int fsize = bpf_classic_proglen(fprog);
1344 struct bpf_prog *fp;
1345
1346 /* Make sure new filter is there and in the right amounts. */
1347 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1348 return -EINVAL;
1349
1350 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1351 if (!fp)
1352 return -ENOMEM;
1353
1354 memcpy(fp->insns, fprog->filter, fsize);
1355
1356 fp->len = fprog->len;
1357 /* Since unattached filters are not copied back to user
1358 * space through sk_get_filter(), we do not need to hold
1359 * a copy here, and can spare us the work.
1360 */
1361 fp->orig_prog = NULL;
1362
1363 /* bpf_prepare_filter() already takes care of freeing
1364 * memory in case something goes wrong.
1365 */
1366 fp = bpf_prepare_filter(fp, NULL);
1367 if (IS_ERR(fp))
1368 return PTR_ERR(fp);
1369
1370 *pfp = fp;
1371 return 0;
1372 }
1373 EXPORT_SYMBOL_GPL(bpf_prog_create);
1374
1375 /**
1376 * bpf_prog_create_from_user - create an unattached filter from user buffer
1377 * @pfp: the unattached filter that is created
1378 * @fprog: the filter program
1379 * @trans: post-classic verifier transformation handler
1380 * @save_orig: save classic BPF program
1381 *
1382 * This function effectively does the same as bpf_prog_create(), only
1383 * that it builds up its insns buffer from user space provided buffer.
1384 * It also allows for passing a bpf_aux_classic_check_t handler.
1385 */
1386 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1387 bpf_aux_classic_check_t trans, bool save_orig)
1388 {
1389 unsigned int fsize = bpf_classic_proglen(fprog);
1390 struct bpf_prog *fp;
1391 int err;
1392
1393 /* Make sure new filter is there and in the right amounts. */
1394 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1395 return -EINVAL;
1396
1397 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1398 if (!fp)
1399 return -ENOMEM;
1400
1401 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1402 __bpf_prog_free(fp);
1403 return -EFAULT;
1404 }
1405
1406 fp->len = fprog->len;
1407 fp->orig_prog = NULL;
1408
1409 if (save_orig) {
1410 err = bpf_prog_store_orig_filter(fp, fprog);
1411 if (err) {
1412 __bpf_prog_free(fp);
1413 return -ENOMEM;
1414 }
1415 }
1416
1417 /* bpf_prepare_filter() already takes care of freeing
1418 * memory in case something goes wrong.
1419 */
1420 fp = bpf_prepare_filter(fp, trans);
1421 if (IS_ERR(fp))
1422 return PTR_ERR(fp);
1423
1424 *pfp = fp;
1425 return 0;
1426 }
1427 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1428
1429 void bpf_prog_destroy(struct bpf_prog *fp)
1430 {
1431 __bpf_prog_release(fp);
1432 }
1433 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1434
1435 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1436 {
1437 struct sk_filter *fp, *old_fp;
1438
1439 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1440 if (!fp)
1441 return -ENOMEM;
1442
1443 fp->prog = prog;
1444
1445 if (!__sk_filter_charge(sk, fp)) {
1446 kfree(fp);
1447 return -ENOMEM;
1448 }
1449 refcount_set(&fp->refcnt, 1);
1450
1451 old_fp = rcu_dereference_protected(sk->sk_filter,
1452 lockdep_sock_is_held(sk));
1453 rcu_assign_pointer(sk->sk_filter, fp);
1454
1455 if (old_fp)
1456 sk_filter_uncharge(sk, old_fp);
1457
1458 return 0;
1459 }
1460
1461 static
1462 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1463 {
1464 unsigned int fsize = bpf_classic_proglen(fprog);
1465 struct bpf_prog *prog;
1466 int err;
1467
1468 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1469 return ERR_PTR(-EPERM);
1470
1471 /* Make sure new filter is there and in the right amounts. */
1472 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1473 return ERR_PTR(-EINVAL);
1474
1475 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1476 if (!prog)
1477 return ERR_PTR(-ENOMEM);
1478
1479 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1480 __bpf_prog_free(prog);
1481 return ERR_PTR(-EFAULT);
1482 }
1483
1484 prog->len = fprog->len;
1485
1486 err = bpf_prog_store_orig_filter(prog, fprog);
1487 if (err) {
1488 __bpf_prog_free(prog);
1489 return ERR_PTR(-ENOMEM);
1490 }
1491
1492 /* bpf_prepare_filter() already takes care of freeing
1493 * memory in case something goes wrong.
1494 */
1495 return bpf_prepare_filter(prog, NULL);
1496 }
1497
1498 /**
1499 * sk_attach_filter - attach a socket filter
1500 * @fprog: the filter program
1501 * @sk: the socket to use
1502 *
1503 * Attach the user's filter code. We first run some sanity checks on
1504 * it to make sure it does not explode on us later. If an error
1505 * occurs or there is insufficient memory for the filter a negative
1506 * errno code is returned. On success the return is zero.
1507 */
1508 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1509 {
1510 struct bpf_prog *prog = __get_filter(fprog, sk);
1511 int err;
1512
1513 if (IS_ERR(prog))
1514 return PTR_ERR(prog);
1515
1516 err = __sk_attach_prog(prog, sk);
1517 if (err < 0) {
1518 __bpf_prog_release(prog);
1519 return err;
1520 }
1521
1522 return 0;
1523 }
1524 EXPORT_SYMBOL_GPL(sk_attach_filter);
1525
1526 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1527 {
1528 struct bpf_prog *prog = __get_filter(fprog, sk);
1529 int err;
1530
1531 if (IS_ERR(prog))
1532 return PTR_ERR(prog);
1533
1534 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1535 err = -ENOMEM;
1536 else
1537 err = reuseport_attach_prog(sk, prog);
1538
1539 if (err)
1540 __bpf_prog_release(prog);
1541
1542 return err;
1543 }
1544
1545 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1546 {
1547 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1548 return ERR_PTR(-EPERM);
1549
1550 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1551 }
1552
1553 int sk_attach_bpf(u32 ufd, struct sock *sk)
1554 {
1555 struct bpf_prog *prog = __get_bpf(ufd, sk);
1556 int err;
1557
1558 if (IS_ERR(prog))
1559 return PTR_ERR(prog);
1560
1561 err = __sk_attach_prog(prog, sk);
1562 if (err < 0) {
1563 bpf_prog_put(prog);
1564 return err;
1565 }
1566
1567 return 0;
1568 }
1569
1570 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1571 {
1572 struct bpf_prog *prog;
1573 int err;
1574
1575 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1576 return -EPERM;
1577
1578 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1579 if (IS_ERR(prog) && PTR_ERR(prog) == -EINVAL)
1580 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1581 if (IS_ERR(prog))
1582 return PTR_ERR(prog);
1583
1584 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1585 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1586 * bpf prog (e.g. sockmap). It depends on the
1587 * limitation imposed by bpf_prog_load().
1588 * Hence, sysctl_optmem_max is not checked.
1589 */
1590 if ((sk->sk_type != SOCK_STREAM &&
1591 sk->sk_type != SOCK_DGRAM) ||
1592 (sk->sk_protocol != IPPROTO_UDP &&
1593 sk->sk_protocol != IPPROTO_TCP) ||
1594 (sk->sk_family != AF_INET &&
1595 sk->sk_family != AF_INET6)) {
1596 err = -ENOTSUPP;
1597 goto err_prog_put;
1598 }
1599 } else {
1600 /* BPF_PROG_TYPE_SOCKET_FILTER */
1601 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1602 err = -ENOMEM;
1603 goto err_prog_put;
1604 }
1605 }
1606
1607 err = reuseport_attach_prog(sk, prog);
1608 err_prog_put:
1609 if (err)
1610 bpf_prog_put(prog);
1611
1612 return err;
1613 }
1614
1615 void sk_reuseport_prog_free(struct bpf_prog *prog)
1616 {
1617 if (!prog)
1618 return;
1619
1620 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1621 bpf_prog_put(prog);
1622 else
1623 bpf_prog_destroy(prog);
1624 }
1625
1626 struct bpf_scratchpad {
1627 union {
1628 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1629 u8 buff[MAX_BPF_STACK];
1630 };
1631 };
1632
1633 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1634
1635 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1636 unsigned int write_len)
1637 {
1638 return skb_ensure_writable(skb, write_len);
1639 }
1640
1641 static inline int bpf_try_make_writable(struct sk_buff *skb,
1642 unsigned int write_len)
1643 {
1644 int err = __bpf_try_make_writable(skb, write_len);
1645
1646 bpf_compute_data_pointers(skb);
1647 return err;
1648 }
1649
1650 static int bpf_try_make_head_writable(struct sk_buff *skb)
1651 {
1652 return bpf_try_make_writable(skb, skb_headlen(skb));
1653 }
1654
1655 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1656 {
1657 if (skb_at_tc_ingress(skb))
1658 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1659 }
1660
1661 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1662 {
1663 if (skb_at_tc_ingress(skb))
1664 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1665 }
1666
1667 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1668 const void *, from, u32, len, u64, flags)
1669 {
1670 void *ptr;
1671
1672 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1673 return -EINVAL;
1674 if (unlikely(offset > 0xffff))
1675 return -EFAULT;
1676 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1677 return -EFAULT;
1678
1679 ptr = skb->data + offset;
1680 if (flags & BPF_F_RECOMPUTE_CSUM)
1681 __skb_postpull_rcsum(skb, ptr, len, offset);
1682
1683 memcpy(ptr, from, len);
1684
1685 if (flags & BPF_F_RECOMPUTE_CSUM)
1686 __skb_postpush_rcsum(skb, ptr, len, offset);
1687 if (flags & BPF_F_INVALIDATE_HASH)
1688 skb_clear_hash(skb);
1689
1690 return 0;
1691 }
1692
1693 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1694 .func = bpf_skb_store_bytes,
1695 .gpl_only = false,
1696 .ret_type = RET_INTEGER,
1697 .arg1_type = ARG_PTR_TO_CTX,
1698 .arg2_type = ARG_ANYTHING,
1699 .arg3_type = ARG_PTR_TO_MEM,
1700 .arg4_type = ARG_CONST_SIZE,
1701 .arg5_type = ARG_ANYTHING,
1702 };
1703
1704 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1705 void *, to, u32, len)
1706 {
1707 void *ptr;
1708
1709 if (unlikely(offset > 0xffff))
1710 goto err_clear;
1711
1712 ptr = skb_header_pointer(skb, offset, len, to);
1713 if (unlikely(!ptr))
1714 goto err_clear;
1715 if (ptr != to)
1716 memcpy(to, ptr, len);
1717
1718 return 0;
1719 err_clear:
1720 memset(to, 0, len);
1721 return -EFAULT;
1722 }
1723
1724 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1725 .func = bpf_skb_load_bytes,
1726 .gpl_only = false,
1727 .ret_type = RET_INTEGER,
1728 .arg1_type = ARG_PTR_TO_CTX,
1729 .arg2_type = ARG_ANYTHING,
1730 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1731 .arg4_type = ARG_CONST_SIZE,
1732 };
1733
1734 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1735 u32, offset, void *, to, u32, len, u32, start_header)
1736 {
1737 u8 *end = skb_tail_pointer(skb);
1738 u8 *net = skb_network_header(skb);
1739 u8 *mac = skb_mac_header(skb);
1740 u8 *ptr;
1741
1742 if (unlikely(offset > 0xffff || len > (end - mac)))
1743 goto err_clear;
1744
1745 switch (start_header) {
1746 case BPF_HDR_START_MAC:
1747 ptr = mac + offset;
1748 break;
1749 case BPF_HDR_START_NET:
1750 ptr = net + offset;
1751 break;
1752 default:
1753 goto err_clear;
1754 }
1755
1756 if (likely(ptr >= mac && ptr + len <= end)) {
1757 memcpy(to, ptr, len);
1758 return 0;
1759 }
1760
1761 err_clear:
1762 memset(to, 0, len);
1763 return -EFAULT;
1764 }
1765
1766 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1767 .func = bpf_skb_load_bytes_relative,
1768 .gpl_only = false,
1769 .ret_type = RET_INTEGER,
1770 .arg1_type = ARG_PTR_TO_CTX,
1771 .arg2_type = ARG_ANYTHING,
1772 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1773 .arg4_type = ARG_CONST_SIZE,
1774 .arg5_type = ARG_ANYTHING,
1775 };
1776
1777 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1778 {
1779 /* Idea is the following: should the needed direct read/write
1780 * test fail during runtime, we can pull in more data and redo
1781 * again, since implicitly, we invalidate previous checks here.
1782 *
1783 * Or, since we know how much we need to make read/writeable,
1784 * this can be done once at the program beginning for direct
1785 * access case. By this we overcome limitations of only current
1786 * headroom being accessible.
1787 */
1788 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1789 }
1790
1791 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1792 .func = bpf_skb_pull_data,
1793 .gpl_only = false,
1794 .ret_type = RET_INTEGER,
1795 .arg1_type = ARG_PTR_TO_CTX,
1796 .arg2_type = ARG_ANYTHING,
1797 };
1798
1799 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1800 unsigned int write_len)
1801 {
1802 int err = __bpf_try_make_writable(skb, write_len);
1803
1804 bpf_compute_data_end_sk_skb(skb);
1805 return err;
1806 }
1807
1808 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1809 {
1810 /* Idea is the following: should the needed direct read/write
1811 * test fail during runtime, we can pull in more data and redo
1812 * again, since implicitly, we invalidate previous checks here.
1813 *
1814 * Or, since we know how much we need to make read/writeable,
1815 * this can be done once at the program beginning for direct
1816 * access case. By this we overcome limitations of only current
1817 * headroom being accessible.
1818 */
1819 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1820 }
1821
1822 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1823 .func = sk_skb_pull_data,
1824 .gpl_only = false,
1825 .ret_type = RET_INTEGER,
1826 .arg1_type = ARG_PTR_TO_CTX,
1827 .arg2_type = ARG_ANYTHING,
1828 };
1829
1830 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1831 u64, from, u64, to, u64, flags)
1832 {
1833 __sum16 *ptr;
1834
1835 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1836 return -EINVAL;
1837 if (unlikely(offset > 0xffff || offset & 1))
1838 return -EFAULT;
1839 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1840 return -EFAULT;
1841
1842 ptr = (__sum16 *)(skb->data + offset);
1843 switch (flags & BPF_F_HDR_FIELD_MASK) {
1844 case 0:
1845 if (unlikely(from != 0))
1846 return -EINVAL;
1847
1848 csum_replace_by_diff(ptr, to);
1849 break;
1850 case 2:
1851 csum_replace2(ptr, from, to);
1852 break;
1853 case 4:
1854 csum_replace4(ptr, from, to);
1855 break;
1856 default:
1857 return -EINVAL;
1858 }
1859
1860 return 0;
1861 }
1862
1863 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1864 .func = bpf_l3_csum_replace,
1865 .gpl_only = false,
1866 .ret_type = RET_INTEGER,
1867 .arg1_type = ARG_PTR_TO_CTX,
1868 .arg2_type = ARG_ANYTHING,
1869 .arg3_type = ARG_ANYTHING,
1870 .arg4_type = ARG_ANYTHING,
1871 .arg5_type = ARG_ANYTHING,
1872 };
1873
1874 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1875 u64, from, u64, to, u64, flags)
1876 {
1877 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1878 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1879 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1880 __sum16 *ptr;
1881
1882 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1883 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1884 return -EINVAL;
1885 if (unlikely(offset > 0xffff || offset & 1))
1886 return -EFAULT;
1887 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1888 return -EFAULT;
1889
1890 ptr = (__sum16 *)(skb->data + offset);
1891 if (is_mmzero && !do_mforce && !*ptr)
1892 return 0;
1893
1894 switch (flags & BPF_F_HDR_FIELD_MASK) {
1895 case 0:
1896 if (unlikely(from != 0))
1897 return -EINVAL;
1898
1899 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1900 break;
1901 case 2:
1902 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1903 break;
1904 case 4:
1905 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1906 break;
1907 default:
1908 return -EINVAL;
1909 }
1910
1911 if (is_mmzero && !*ptr)
1912 *ptr = CSUM_MANGLED_0;
1913 return 0;
1914 }
1915
1916 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1917 .func = bpf_l4_csum_replace,
1918 .gpl_only = false,
1919 .ret_type = RET_INTEGER,
1920 .arg1_type = ARG_PTR_TO_CTX,
1921 .arg2_type = ARG_ANYTHING,
1922 .arg3_type = ARG_ANYTHING,
1923 .arg4_type = ARG_ANYTHING,
1924 .arg5_type = ARG_ANYTHING,
1925 };
1926
1927 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1928 __be32 *, to, u32, to_size, __wsum, seed)
1929 {
1930 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1931 u32 diff_size = from_size + to_size;
1932 int i, j = 0;
1933
1934 /* This is quite flexible, some examples:
1935 *
1936 * from_size == 0, to_size > 0, seed := csum --> pushing data
1937 * from_size > 0, to_size == 0, seed := csum --> pulling data
1938 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1939 *
1940 * Even for diffing, from_size and to_size don't need to be equal.
1941 */
1942 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1943 diff_size > sizeof(sp->diff)))
1944 return -EINVAL;
1945
1946 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1947 sp->diff[j] = ~from[i];
1948 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1949 sp->diff[j] = to[i];
1950
1951 return csum_partial(sp->diff, diff_size, seed);
1952 }
1953
1954 static const struct bpf_func_proto bpf_csum_diff_proto = {
1955 .func = bpf_csum_diff,
1956 .gpl_only = false,
1957 .pkt_access = true,
1958 .ret_type = RET_INTEGER,
1959 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1960 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1961 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1962 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1963 .arg5_type = ARG_ANYTHING,
1964 };
1965
1966 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1967 {
1968 /* The interface is to be used in combination with bpf_csum_diff()
1969 * for direct packet writes. csum rotation for alignment as well
1970 * as emulating csum_sub() can be done from the eBPF program.
1971 */
1972 if (skb->ip_summed == CHECKSUM_COMPLETE)
1973 return (skb->csum = csum_add(skb->csum, csum));
1974
1975 return -ENOTSUPP;
1976 }
1977
1978 static const struct bpf_func_proto bpf_csum_update_proto = {
1979 .func = bpf_csum_update,
1980 .gpl_only = false,
1981 .ret_type = RET_INTEGER,
1982 .arg1_type = ARG_PTR_TO_CTX,
1983 .arg2_type = ARG_ANYTHING,
1984 };
1985
1986 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1987 {
1988 return dev_forward_skb(dev, skb);
1989 }
1990
1991 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1992 struct sk_buff *skb)
1993 {
1994 int ret = ____dev_forward_skb(dev, skb);
1995
1996 if (likely(!ret)) {
1997 skb->dev = dev;
1998 ret = netif_rx(skb);
1999 }
2000
2001 return ret;
2002 }
2003
2004 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2005 {
2006 int ret;
2007
2008 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
2009 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2010 kfree_skb(skb);
2011 return -ENETDOWN;
2012 }
2013
2014 skb->dev = dev;
2015
2016 __this_cpu_inc(xmit_recursion);
2017 ret = dev_queue_xmit(skb);
2018 __this_cpu_dec(xmit_recursion);
2019
2020 return ret;
2021 }
2022
2023 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2024 u32 flags)
2025 {
2026 /* skb->mac_len is not set on normal egress */
2027 unsigned int mlen = skb->network_header - skb->mac_header;
2028
2029 __skb_pull(skb, mlen);
2030
2031 /* At ingress, the mac header has already been pulled once.
2032 * At egress, skb_pospull_rcsum has to be done in case that
2033 * the skb is originated from ingress (i.e. a forwarded skb)
2034 * to ensure that rcsum starts at net header.
2035 */
2036 if (!skb_at_tc_ingress(skb))
2037 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2038 skb_pop_mac_header(skb);
2039 skb_reset_mac_len(skb);
2040 return flags & BPF_F_INGRESS ?
2041 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2042 }
2043
2044 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2045 u32 flags)
2046 {
2047 /* Verify that a link layer header is carried */
2048 if (unlikely(skb->mac_header >= skb->network_header)) {
2049 kfree_skb(skb);
2050 return -ERANGE;
2051 }
2052
2053 bpf_push_mac_rcsum(skb);
2054 return flags & BPF_F_INGRESS ?
2055 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2056 }
2057
2058 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2059 u32 flags)
2060 {
2061 if (dev_is_mac_header_xmit(dev))
2062 return __bpf_redirect_common(skb, dev, flags);
2063 else
2064 return __bpf_redirect_no_mac(skb, dev, flags);
2065 }
2066
2067 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2068 {
2069 struct net_device *dev;
2070 struct sk_buff *clone;
2071 int ret;
2072
2073 if (unlikely(flags & ~(BPF_F_INGRESS)))
2074 return -EINVAL;
2075
2076 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2077 if (unlikely(!dev))
2078 return -EINVAL;
2079
2080 clone = skb_clone(skb, GFP_ATOMIC);
2081 if (unlikely(!clone))
2082 return -ENOMEM;
2083
2084 /* For direct write, we need to keep the invariant that the skbs
2085 * we're dealing with need to be uncloned. Should uncloning fail
2086 * here, we need to free the just generated clone to unclone once
2087 * again.
2088 */
2089 ret = bpf_try_make_head_writable(skb);
2090 if (unlikely(ret)) {
2091 kfree_skb(clone);
2092 return -ENOMEM;
2093 }
2094
2095 return __bpf_redirect(clone, dev, flags);
2096 }
2097
2098 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2099 .func = bpf_clone_redirect,
2100 .gpl_only = false,
2101 .ret_type = RET_INTEGER,
2102 .arg1_type = ARG_PTR_TO_CTX,
2103 .arg2_type = ARG_ANYTHING,
2104 .arg3_type = ARG_ANYTHING,
2105 };
2106
2107 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2108 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2109
2110 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2111 {
2112 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2113
2114 if (unlikely(flags & ~(BPF_F_INGRESS)))
2115 return TC_ACT_SHOT;
2116
2117 ri->ifindex = ifindex;
2118 ri->flags = flags;
2119
2120 return TC_ACT_REDIRECT;
2121 }
2122
2123 int skb_do_redirect(struct sk_buff *skb)
2124 {
2125 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2126 struct net_device *dev;
2127
2128 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2129 ri->ifindex = 0;
2130 if (unlikely(!dev)) {
2131 kfree_skb(skb);
2132 return -EINVAL;
2133 }
2134
2135 return __bpf_redirect(skb, dev, ri->flags);
2136 }
2137
2138 static const struct bpf_func_proto bpf_redirect_proto = {
2139 .func = bpf_redirect,
2140 .gpl_only = false,
2141 .ret_type = RET_INTEGER,
2142 .arg1_type = ARG_ANYTHING,
2143 .arg2_type = ARG_ANYTHING,
2144 };
2145
2146 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2147 {
2148 msg->apply_bytes = bytes;
2149 return 0;
2150 }
2151
2152 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2153 .func = bpf_msg_apply_bytes,
2154 .gpl_only = false,
2155 .ret_type = RET_INTEGER,
2156 .arg1_type = ARG_PTR_TO_CTX,
2157 .arg2_type = ARG_ANYTHING,
2158 };
2159
2160 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2161 {
2162 msg->cork_bytes = bytes;
2163 return 0;
2164 }
2165
2166 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2167 .func = bpf_msg_cork_bytes,
2168 .gpl_only = false,
2169 .ret_type = RET_INTEGER,
2170 .arg1_type = ARG_PTR_TO_CTX,
2171 .arg2_type = ARG_ANYTHING,
2172 };
2173
2174 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2175 u32, end, u64, flags)
2176 {
2177 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2178 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2179 struct scatterlist *sge;
2180 u8 *raw, *to, *from;
2181 struct page *page;
2182
2183 if (unlikely(flags || end <= start))
2184 return -EINVAL;
2185
2186 /* First find the starting scatterlist element */
2187 i = msg->sg.start;
2188 do {
2189 len = sk_msg_elem(msg, i)->length;
2190 if (start < offset + len)
2191 break;
2192 offset += len;
2193 sk_msg_iter_var_next(i);
2194 } while (i != msg->sg.end);
2195
2196 if (unlikely(start >= offset + len))
2197 return -EINVAL;
2198
2199 first_sge = i;
2200 /* The start may point into the sg element so we need to also
2201 * account for the headroom.
2202 */
2203 bytes_sg_total = start - offset + bytes;
2204 if (!msg->sg.copy[i] && bytes_sg_total <= len)
2205 goto out;
2206
2207 /* At this point we need to linearize multiple scatterlist
2208 * elements or a single shared page. Either way we need to
2209 * copy into a linear buffer exclusively owned by BPF. Then
2210 * place the buffer in the scatterlist and fixup the original
2211 * entries by removing the entries now in the linear buffer
2212 * and shifting the remaining entries. For now we do not try
2213 * to copy partial entries to avoid complexity of running out
2214 * of sg_entry slots. The downside is reading a single byte
2215 * will copy the entire sg entry.
2216 */
2217 do {
2218 copy += sk_msg_elem(msg, i)->length;
2219 sk_msg_iter_var_next(i);
2220 if (bytes_sg_total <= copy)
2221 break;
2222 } while (i != msg->sg.end);
2223 last_sge = i;
2224
2225 if (unlikely(bytes_sg_total > copy))
2226 return -EINVAL;
2227
2228 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2229 get_order(copy));
2230 if (unlikely(!page))
2231 return -ENOMEM;
2232
2233 raw = page_address(page);
2234 i = first_sge;
2235 do {
2236 sge = sk_msg_elem(msg, i);
2237 from = sg_virt(sge);
2238 len = sge->length;
2239 to = raw + poffset;
2240
2241 memcpy(to, from, len);
2242 poffset += len;
2243 sge->length = 0;
2244 put_page(sg_page(sge));
2245
2246 sk_msg_iter_var_next(i);
2247 } while (i != last_sge);
2248
2249 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2250
2251 /* To repair sg ring we need to shift entries. If we only
2252 * had a single entry though we can just replace it and
2253 * be done. Otherwise walk the ring and shift the entries.
2254 */
2255 WARN_ON_ONCE(last_sge == first_sge);
2256 shift = last_sge > first_sge ?
2257 last_sge - first_sge - 1 :
2258 MAX_SKB_FRAGS - first_sge + last_sge - 1;
2259 if (!shift)
2260 goto out;
2261
2262 i = first_sge;
2263 sk_msg_iter_var_next(i);
2264 do {
2265 u32 move_from;
2266
2267 if (i + shift >= MAX_MSG_FRAGS)
2268 move_from = i + shift - MAX_MSG_FRAGS;
2269 else
2270 move_from = i + shift;
2271 if (move_from == msg->sg.end)
2272 break;
2273
2274 msg->sg.data[i] = msg->sg.data[move_from];
2275 msg->sg.data[move_from].length = 0;
2276 msg->sg.data[move_from].page_link = 0;
2277 msg->sg.data[move_from].offset = 0;
2278 sk_msg_iter_var_next(i);
2279 } while (1);
2280
2281 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2282 msg->sg.end - shift + MAX_MSG_FRAGS :
2283 msg->sg.end - shift;
2284 out:
2285 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2286 msg->data_end = msg->data + bytes;
2287 return 0;
2288 }
2289
2290 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2291 .func = bpf_msg_pull_data,
2292 .gpl_only = false,
2293 .ret_type = RET_INTEGER,
2294 .arg1_type = ARG_PTR_TO_CTX,
2295 .arg2_type = ARG_ANYTHING,
2296 .arg3_type = ARG_ANYTHING,
2297 .arg4_type = ARG_ANYTHING,
2298 };
2299
2300 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2301 {
2302 return task_get_classid(skb);
2303 }
2304
2305 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2306 .func = bpf_get_cgroup_classid,
2307 .gpl_only = false,
2308 .ret_type = RET_INTEGER,
2309 .arg1_type = ARG_PTR_TO_CTX,
2310 };
2311
2312 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2313 {
2314 return dst_tclassid(skb);
2315 }
2316
2317 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2318 .func = bpf_get_route_realm,
2319 .gpl_only = false,
2320 .ret_type = RET_INTEGER,
2321 .arg1_type = ARG_PTR_TO_CTX,
2322 };
2323
2324 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2325 {
2326 /* If skb_clear_hash() was called due to mangling, we can
2327 * trigger SW recalculation here. Later access to hash
2328 * can then use the inline skb->hash via context directly
2329 * instead of calling this helper again.
2330 */
2331 return skb_get_hash(skb);
2332 }
2333
2334 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2335 .func = bpf_get_hash_recalc,
2336 .gpl_only = false,
2337 .ret_type = RET_INTEGER,
2338 .arg1_type = ARG_PTR_TO_CTX,
2339 };
2340
2341 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2342 {
2343 /* After all direct packet write, this can be used once for
2344 * triggering a lazy recalc on next skb_get_hash() invocation.
2345 */
2346 skb_clear_hash(skb);
2347 return 0;
2348 }
2349
2350 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2351 .func = bpf_set_hash_invalid,
2352 .gpl_only = false,
2353 .ret_type = RET_INTEGER,
2354 .arg1_type = ARG_PTR_TO_CTX,
2355 };
2356
2357 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2358 {
2359 /* Set user specified hash as L4(+), so that it gets returned
2360 * on skb_get_hash() call unless BPF prog later on triggers a
2361 * skb_clear_hash().
2362 */
2363 __skb_set_sw_hash(skb, hash, true);
2364 return 0;
2365 }
2366
2367 static const struct bpf_func_proto bpf_set_hash_proto = {
2368 .func = bpf_set_hash,
2369 .gpl_only = false,
2370 .ret_type = RET_INTEGER,
2371 .arg1_type = ARG_PTR_TO_CTX,
2372 .arg2_type = ARG_ANYTHING,
2373 };
2374
2375 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2376 u16, vlan_tci)
2377 {
2378 int ret;
2379
2380 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2381 vlan_proto != htons(ETH_P_8021AD)))
2382 vlan_proto = htons(ETH_P_8021Q);
2383
2384 bpf_push_mac_rcsum(skb);
2385 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2386 bpf_pull_mac_rcsum(skb);
2387
2388 bpf_compute_data_pointers(skb);
2389 return ret;
2390 }
2391
2392 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2393 .func = bpf_skb_vlan_push,
2394 .gpl_only = false,
2395 .ret_type = RET_INTEGER,
2396 .arg1_type = ARG_PTR_TO_CTX,
2397 .arg2_type = ARG_ANYTHING,
2398 .arg3_type = ARG_ANYTHING,
2399 };
2400
2401 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2402 {
2403 int ret;
2404
2405 bpf_push_mac_rcsum(skb);
2406 ret = skb_vlan_pop(skb);
2407 bpf_pull_mac_rcsum(skb);
2408
2409 bpf_compute_data_pointers(skb);
2410 return ret;
2411 }
2412
2413 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2414 .func = bpf_skb_vlan_pop,
2415 .gpl_only = false,
2416 .ret_type = RET_INTEGER,
2417 .arg1_type = ARG_PTR_TO_CTX,
2418 };
2419
2420 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2421 {
2422 /* Caller already did skb_cow() with len as headroom,
2423 * so no need to do it here.
2424 */
2425 skb_push(skb, len);
2426 memmove(skb->data, skb->data + len, off);
2427 memset(skb->data + off, 0, len);
2428
2429 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2430 * needed here as it does not change the skb->csum
2431 * result for checksum complete when summing over
2432 * zeroed blocks.
2433 */
2434 return 0;
2435 }
2436
2437 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2438 {
2439 /* skb_ensure_writable() is not needed here, as we're
2440 * already working on an uncloned skb.
2441 */
2442 if (unlikely(!pskb_may_pull(skb, off + len)))
2443 return -ENOMEM;
2444
2445 skb_postpull_rcsum(skb, skb->data + off, len);
2446 memmove(skb->data + len, skb->data, off);
2447 __skb_pull(skb, len);
2448
2449 return 0;
2450 }
2451
2452 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2453 {
2454 bool trans_same = skb->transport_header == skb->network_header;
2455 int ret;
2456
2457 /* There's no need for __skb_push()/__skb_pull() pair to
2458 * get to the start of the mac header as we're guaranteed
2459 * to always start from here under eBPF.
2460 */
2461 ret = bpf_skb_generic_push(skb, off, len);
2462 if (likely(!ret)) {
2463 skb->mac_header -= len;
2464 skb->network_header -= len;
2465 if (trans_same)
2466 skb->transport_header = skb->network_header;
2467 }
2468
2469 return ret;
2470 }
2471
2472 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2473 {
2474 bool trans_same = skb->transport_header == skb->network_header;
2475 int ret;
2476
2477 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2478 ret = bpf_skb_generic_pop(skb, off, len);
2479 if (likely(!ret)) {
2480 skb->mac_header += len;
2481 skb->network_header += len;
2482 if (trans_same)
2483 skb->transport_header = skb->network_header;
2484 }
2485
2486 return ret;
2487 }
2488
2489 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2490 {
2491 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2492 u32 off = skb_mac_header_len(skb);
2493 int ret;
2494
2495 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2496 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2497 return -ENOTSUPP;
2498
2499 ret = skb_cow(skb, len_diff);
2500 if (unlikely(ret < 0))
2501 return ret;
2502
2503 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2504 if (unlikely(ret < 0))
2505 return ret;
2506
2507 if (skb_is_gso(skb)) {
2508 struct skb_shared_info *shinfo = skb_shinfo(skb);
2509
2510 /* SKB_GSO_TCPV4 needs to be changed into
2511 * SKB_GSO_TCPV6.
2512 */
2513 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2514 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2515 shinfo->gso_type |= SKB_GSO_TCPV6;
2516 }
2517
2518 /* Due to IPv6 header, MSS needs to be downgraded. */
2519 skb_decrease_gso_size(shinfo, len_diff);
2520 /* Header must be checked, and gso_segs recomputed. */
2521 shinfo->gso_type |= SKB_GSO_DODGY;
2522 shinfo->gso_segs = 0;
2523 }
2524
2525 skb->protocol = htons(ETH_P_IPV6);
2526 skb_clear_hash(skb);
2527
2528 return 0;
2529 }
2530
2531 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2532 {
2533 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2534 u32 off = skb_mac_header_len(skb);
2535 int ret;
2536
2537 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2538 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2539 return -ENOTSUPP;
2540
2541 ret = skb_unclone(skb, GFP_ATOMIC);
2542 if (unlikely(ret < 0))
2543 return ret;
2544
2545 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2546 if (unlikely(ret < 0))
2547 return ret;
2548
2549 if (skb_is_gso(skb)) {
2550 struct skb_shared_info *shinfo = skb_shinfo(skb);
2551
2552 /* SKB_GSO_TCPV6 needs to be changed into
2553 * SKB_GSO_TCPV4.
2554 */
2555 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2556 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2557 shinfo->gso_type |= SKB_GSO_TCPV4;
2558 }
2559
2560 /* Due to IPv4 header, MSS can be upgraded. */
2561 skb_increase_gso_size(shinfo, len_diff);
2562 /* Header must be checked, and gso_segs recomputed. */
2563 shinfo->gso_type |= SKB_GSO_DODGY;
2564 shinfo->gso_segs = 0;
2565 }
2566
2567 skb->protocol = htons(ETH_P_IP);
2568 skb_clear_hash(skb);
2569
2570 return 0;
2571 }
2572
2573 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2574 {
2575 __be16 from_proto = skb->protocol;
2576
2577 if (from_proto == htons(ETH_P_IP) &&
2578 to_proto == htons(ETH_P_IPV6))
2579 return bpf_skb_proto_4_to_6(skb);
2580
2581 if (from_proto == htons(ETH_P_IPV6) &&
2582 to_proto == htons(ETH_P_IP))
2583 return bpf_skb_proto_6_to_4(skb);
2584
2585 return -ENOTSUPP;
2586 }
2587
2588 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2589 u64, flags)
2590 {
2591 int ret;
2592
2593 if (unlikely(flags))
2594 return -EINVAL;
2595
2596 /* General idea is that this helper does the basic groundwork
2597 * needed for changing the protocol, and eBPF program fills the
2598 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2599 * and other helpers, rather than passing a raw buffer here.
2600 *
2601 * The rationale is to keep this minimal and without a need to
2602 * deal with raw packet data. F.e. even if we would pass buffers
2603 * here, the program still needs to call the bpf_lX_csum_replace()
2604 * helpers anyway. Plus, this way we keep also separation of
2605 * concerns, since f.e. bpf_skb_store_bytes() should only take
2606 * care of stores.
2607 *
2608 * Currently, additional options and extension header space are
2609 * not supported, but flags register is reserved so we can adapt
2610 * that. For offloads, we mark packet as dodgy, so that headers
2611 * need to be verified first.
2612 */
2613 ret = bpf_skb_proto_xlat(skb, proto);
2614 bpf_compute_data_pointers(skb);
2615 return ret;
2616 }
2617
2618 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2619 .func = bpf_skb_change_proto,
2620 .gpl_only = false,
2621 .ret_type = RET_INTEGER,
2622 .arg1_type = ARG_PTR_TO_CTX,
2623 .arg2_type = ARG_ANYTHING,
2624 .arg3_type = ARG_ANYTHING,
2625 };
2626
2627 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2628 {
2629 /* We only allow a restricted subset to be changed for now. */
2630 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2631 !skb_pkt_type_ok(pkt_type)))
2632 return -EINVAL;
2633
2634 skb->pkt_type = pkt_type;
2635 return 0;
2636 }
2637
2638 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2639 .func = bpf_skb_change_type,
2640 .gpl_only = false,
2641 .ret_type = RET_INTEGER,
2642 .arg1_type = ARG_PTR_TO_CTX,
2643 .arg2_type = ARG_ANYTHING,
2644 };
2645
2646 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2647 {
2648 switch (skb->protocol) {
2649 case htons(ETH_P_IP):
2650 return sizeof(struct iphdr);
2651 case htons(ETH_P_IPV6):
2652 return sizeof(struct ipv6hdr);
2653 default:
2654 return ~0U;
2655 }
2656 }
2657
2658 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2659 {
2660 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2661 int ret;
2662
2663 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2664 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2665 return -ENOTSUPP;
2666
2667 ret = skb_cow(skb, len_diff);
2668 if (unlikely(ret < 0))
2669 return ret;
2670
2671 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2672 if (unlikely(ret < 0))
2673 return ret;
2674
2675 if (skb_is_gso(skb)) {
2676 struct skb_shared_info *shinfo = skb_shinfo(skb);
2677
2678 /* Due to header grow, MSS needs to be downgraded. */
2679 skb_decrease_gso_size(shinfo, len_diff);
2680 /* Header must be checked, and gso_segs recomputed. */
2681 shinfo->gso_type |= SKB_GSO_DODGY;
2682 shinfo->gso_segs = 0;
2683 }
2684
2685 return 0;
2686 }
2687
2688 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2689 {
2690 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2691 int ret;
2692
2693 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2694 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2695 return -ENOTSUPP;
2696
2697 ret = skb_unclone(skb, GFP_ATOMIC);
2698 if (unlikely(ret < 0))
2699 return ret;
2700
2701 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2702 if (unlikely(ret < 0))
2703 return ret;
2704
2705 if (skb_is_gso(skb)) {
2706 struct skb_shared_info *shinfo = skb_shinfo(skb);
2707
2708 /* Due to header shrink, MSS can be upgraded. */
2709 skb_increase_gso_size(shinfo, len_diff);
2710 /* Header must be checked, and gso_segs recomputed. */
2711 shinfo->gso_type |= SKB_GSO_DODGY;
2712 shinfo->gso_segs = 0;
2713 }
2714
2715 return 0;
2716 }
2717
2718 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2719 {
2720 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
2721 SKB_MAX_ALLOC;
2722 }
2723
2724 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2725 {
2726 bool trans_same = skb->transport_header == skb->network_header;
2727 u32 len_cur, len_diff_abs = abs(len_diff);
2728 u32 len_min = bpf_skb_net_base_len(skb);
2729 u32 len_max = __bpf_skb_max_len(skb);
2730 __be16 proto = skb->protocol;
2731 bool shrink = len_diff < 0;
2732 int ret;
2733
2734 if (unlikely(len_diff_abs > 0xfffU))
2735 return -EFAULT;
2736 if (unlikely(proto != htons(ETH_P_IP) &&
2737 proto != htons(ETH_P_IPV6)))
2738 return -ENOTSUPP;
2739
2740 len_cur = skb->len - skb_network_offset(skb);
2741 if (skb_transport_header_was_set(skb) && !trans_same)
2742 len_cur = skb_network_header_len(skb);
2743 if ((shrink && (len_diff_abs >= len_cur ||
2744 len_cur - len_diff_abs < len_min)) ||
2745 (!shrink && (skb->len + len_diff_abs > len_max &&
2746 !skb_is_gso(skb))))
2747 return -ENOTSUPP;
2748
2749 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2750 bpf_skb_net_grow(skb, len_diff_abs);
2751
2752 bpf_compute_data_pointers(skb);
2753 return ret;
2754 }
2755
2756 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2757 u32, mode, u64, flags)
2758 {
2759 if (unlikely(flags))
2760 return -EINVAL;
2761 if (likely(mode == BPF_ADJ_ROOM_NET))
2762 return bpf_skb_adjust_net(skb, len_diff);
2763
2764 return -ENOTSUPP;
2765 }
2766
2767 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2768 .func = bpf_skb_adjust_room,
2769 .gpl_only = false,
2770 .ret_type = RET_INTEGER,
2771 .arg1_type = ARG_PTR_TO_CTX,
2772 .arg2_type = ARG_ANYTHING,
2773 .arg3_type = ARG_ANYTHING,
2774 .arg4_type = ARG_ANYTHING,
2775 };
2776
2777 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2778 {
2779 u32 min_len = skb_network_offset(skb);
2780
2781 if (skb_transport_header_was_set(skb))
2782 min_len = skb_transport_offset(skb);
2783 if (skb->ip_summed == CHECKSUM_PARTIAL)
2784 min_len = skb_checksum_start_offset(skb) +
2785 skb->csum_offset + sizeof(__sum16);
2786 return min_len;
2787 }
2788
2789 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2790 {
2791 unsigned int old_len = skb->len;
2792 int ret;
2793
2794 ret = __skb_grow_rcsum(skb, new_len);
2795 if (!ret)
2796 memset(skb->data + old_len, 0, new_len - old_len);
2797 return ret;
2798 }
2799
2800 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2801 {
2802 return __skb_trim_rcsum(skb, new_len);
2803 }
2804
2805 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
2806 u64 flags)
2807 {
2808 u32 max_len = __bpf_skb_max_len(skb);
2809 u32 min_len = __bpf_skb_min_len(skb);
2810 int ret;
2811
2812 if (unlikely(flags || new_len > max_len || new_len < min_len))
2813 return -EINVAL;
2814 if (skb->encapsulation)
2815 return -ENOTSUPP;
2816
2817 /* The basic idea of this helper is that it's performing the
2818 * needed work to either grow or trim an skb, and eBPF program
2819 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2820 * bpf_lX_csum_replace() and others rather than passing a raw
2821 * buffer here. This one is a slow path helper and intended
2822 * for replies with control messages.
2823 *
2824 * Like in bpf_skb_change_proto(), we want to keep this rather
2825 * minimal and without protocol specifics so that we are able
2826 * to separate concerns as in bpf_skb_store_bytes() should only
2827 * be the one responsible for writing buffers.
2828 *
2829 * It's really expected to be a slow path operation here for
2830 * control message replies, so we're implicitly linearizing,
2831 * uncloning and drop offloads from the skb by this.
2832 */
2833 ret = __bpf_try_make_writable(skb, skb->len);
2834 if (!ret) {
2835 if (new_len > skb->len)
2836 ret = bpf_skb_grow_rcsum(skb, new_len);
2837 else if (new_len < skb->len)
2838 ret = bpf_skb_trim_rcsum(skb, new_len);
2839 if (!ret && skb_is_gso(skb))
2840 skb_gso_reset(skb);
2841 }
2842 return ret;
2843 }
2844
2845 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2846 u64, flags)
2847 {
2848 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2849
2850 bpf_compute_data_pointers(skb);
2851 return ret;
2852 }
2853
2854 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2855 .func = bpf_skb_change_tail,
2856 .gpl_only = false,
2857 .ret_type = RET_INTEGER,
2858 .arg1_type = ARG_PTR_TO_CTX,
2859 .arg2_type = ARG_ANYTHING,
2860 .arg3_type = ARG_ANYTHING,
2861 };
2862
2863 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2864 u64, flags)
2865 {
2866 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2867
2868 bpf_compute_data_end_sk_skb(skb);
2869 return ret;
2870 }
2871
2872 static const struct bpf_func_proto sk_skb_change_tail_proto = {
2873 .func = sk_skb_change_tail,
2874 .gpl_only = false,
2875 .ret_type = RET_INTEGER,
2876 .arg1_type = ARG_PTR_TO_CTX,
2877 .arg2_type = ARG_ANYTHING,
2878 .arg3_type = ARG_ANYTHING,
2879 };
2880
2881 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
2882 u64 flags)
2883 {
2884 u32 max_len = __bpf_skb_max_len(skb);
2885 u32 new_len = skb->len + head_room;
2886 int ret;
2887
2888 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2889 new_len < skb->len))
2890 return -EINVAL;
2891
2892 ret = skb_cow(skb, head_room);
2893 if (likely(!ret)) {
2894 /* Idea for this helper is that we currently only
2895 * allow to expand on mac header. This means that
2896 * skb->protocol network header, etc, stay as is.
2897 * Compared to bpf_skb_change_tail(), we're more
2898 * flexible due to not needing to linearize or
2899 * reset GSO. Intention for this helper is to be
2900 * used by an L3 skb that needs to push mac header
2901 * for redirection into L2 device.
2902 */
2903 __skb_push(skb, head_room);
2904 memset(skb->data, 0, head_room);
2905 skb_reset_mac_header(skb);
2906 }
2907
2908 return ret;
2909 }
2910
2911 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2912 u64, flags)
2913 {
2914 int ret = __bpf_skb_change_head(skb, head_room, flags);
2915
2916 bpf_compute_data_pointers(skb);
2917 return ret;
2918 }
2919
2920 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2921 .func = bpf_skb_change_head,
2922 .gpl_only = false,
2923 .ret_type = RET_INTEGER,
2924 .arg1_type = ARG_PTR_TO_CTX,
2925 .arg2_type = ARG_ANYTHING,
2926 .arg3_type = ARG_ANYTHING,
2927 };
2928
2929 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
2930 u64, flags)
2931 {
2932 int ret = __bpf_skb_change_head(skb, head_room, flags);
2933
2934 bpf_compute_data_end_sk_skb(skb);
2935 return ret;
2936 }
2937
2938 static const struct bpf_func_proto sk_skb_change_head_proto = {
2939 .func = sk_skb_change_head,
2940 .gpl_only = false,
2941 .ret_type = RET_INTEGER,
2942 .arg1_type = ARG_PTR_TO_CTX,
2943 .arg2_type = ARG_ANYTHING,
2944 .arg3_type = ARG_ANYTHING,
2945 };
2946 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
2947 {
2948 return xdp_data_meta_unsupported(xdp) ? 0 :
2949 xdp->data - xdp->data_meta;
2950 }
2951
2952 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2953 {
2954 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
2955 unsigned long metalen = xdp_get_metalen(xdp);
2956 void *data_start = xdp_frame_end + metalen;
2957 void *data = xdp->data + offset;
2958
2959 if (unlikely(data < data_start ||
2960 data > xdp->data_end - ETH_HLEN))
2961 return -EINVAL;
2962
2963 if (metalen)
2964 memmove(xdp->data_meta + offset,
2965 xdp->data_meta, metalen);
2966 xdp->data_meta += offset;
2967 xdp->data = data;
2968
2969 return 0;
2970 }
2971
2972 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2973 .func = bpf_xdp_adjust_head,
2974 .gpl_only = false,
2975 .ret_type = RET_INTEGER,
2976 .arg1_type = ARG_PTR_TO_CTX,
2977 .arg2_type = ARG_ANYTHING,
2978 };
2979
2980 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
2981 {
2982 void *data_end = xdp->data_end + offset;
2983
2984 /* only shrinking is allowed for now. */
2985 if (unlikely(offset >= 0))
2986 return -EINVAL;
2987
2988 if (unlikely(data_end < xdp->data + ETH_HLEN))
2989 return -EINVAL;
2990
2991 xdp->data_end = data_end;
2992
2993 return 0;
2994 }
2995
2996 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
2997 .func = bpf_xdp_adjust_tail,
2998 .gpl_only = false,
2999 .ret_type = RET_INTEGER,
3000 .arg1_type = ARG_PTR_TO_CTX,
3001 .arg2_type = ARG_ANYTHING,
3002 };
3003
3004 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3005 {
3006 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3007 void *meta = xdp->data_meta + offset;
3008 unsigned long metalen = xdp->data - meta;
3009
3010 if (xdp_data_meta_unsupported(xdp))
3011 return -ENOTSUPP;
3012 if (unlikely(meta < xdp_frame_end ||
3013 meta > xdp->data))
3014 return -EINVAL;
3015 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3016 (metalen > 32)))
3017 return -EACCES;
3018
3019 xdp->data_meta = meta;
3020
3021 return 0;
3022 }
3023
3024 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3025 .func = bpf_xdp_adjust_meta,
3026 .gpl_only = false,
3027 .ret_type = RET_INTEGER,
3028 .arg1_type = ARG_PTR_TO_CTX,
3029 .arg2_type = ARG_ANYTHING,
3030 };
3031
3032 static int __bpf_tx_xdp(struct net_device *dev,
3033 struct bpf_map *map,
3034 struct xdp_buff *xdp,
3035 u32 index)
3036 {
3037 struct xdp_frame *xdpf;
3038 int err, sent;
3039
3040 if (!dev->netdev_ops->ndo_xdp_xmit) {
3041 return -EOPNOTSUPP;
3042 }
3043
3044 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3045 if (unlikely(err))
3046 return err;
3047
3048 xdpf = convert_to_xdp_frame(xdp);
3049 if (unlikely(!xdpf))
3050 return -EOVERFLOW;
3051
3052 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3053 if (sent <= 0)
3054 return sent;
3055 return 0;
3056 }
3057
3058 static noinline int
3059 xdp_do_redirect_slow(struct net_device *dev, struct xdp_buff *xdp,
3060 struct bpf_prog *xdp_prog, struct bpf_redirect_info *ri)
3061 {
3062 struct net_device *fwd;
3063 u32 index = ri->ifindex;
3064 int err;
3065
3066 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3067 ri->ifindex = 0;
3068 if (unlikely(!fwd)) {
3069 err = -EINVAL;
3070 goto err;
3071 }
3072
3073 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3074 if (unlikely(err))
3075 goto err;
3076
3077 _trace_xdp_redirect(dev, xdp_prog, index);
3078 return 0;
3079 err:
3080 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3081 return err;
3082 }
3083
3084 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3085 struct bpf_map *map,
3086 struct xdp_buff *xdp,
3087 u32 index)
3088 {
3089 int err;
3090
3091 switch (map->map_type) {
3092 case BPF_MAP_TYPE_DEVMAP: {
3093 struct bpf_dtab_netdev *dst = fwd;
3094
3095 err = dev_map_enqueue(dst, xdp, dev_rx);
3096 if (unlikely(err))
3097 return err;
3098 __dev_map_insert_ctx(map, index);
3099 break;
3100 }
3101 case BPF_MAP_TYPE_CPUMAP: {
3102 struct bpf_cpu_map_entry *rcpu = fwd;
3103
3104 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3105 if (unlikely(err))
3106 return err;
3107 __cpu_map_insert_ctx(map, index);
3108 break;
3109 }
3110 case BPF_MAP_TYPE_XSKMAP: {
3111 struct xdp_sock *xs = fwd;
3112
3113 err = __xsk_map_redirect(map, xdp, xs);
3114 return err;
3115 }
3116 default:
3117 break;
3118 }
3119 return 0;
3120 }
3121
3122 void xdp_do_flush_map(void)
3123 {
3124 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3125 struct bpf_map *map = ri->map_to_flush;
3126
3127 ri->map_to_flush = NULL;
3128 if (map) {
3129 switch (map->map_type) {
3130 case BPF_MAP_TYPE_DEVMAP:
3131 __dev_map_flush(map);
3132 break;
3133 case BPF_MAP_TYPE_CPUMAP:
3134 __cpu_map_flush(map);
3135 break;
3136 case BPF_MAP_TYPE_XSKMAP:
3137 __xsk_map_flush(map);
3138 break;
3139 default:
3140 break;
3141 }
3142 }
3143 }
3144 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3145
3146 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3147 {
3148 switch (map->map_type) {
3149 case BPF_MAP_TYPE_DEVMAP:
3150 return __dev_map_lookup_elem(map, index);
3151 case BPF_MAP_TYPE_CPUMAP:
3152 return __cpu_map_lookup_elem(map, index);
3153 case BPF_MAP_TYPE_XSKMAP:
3154 return __xsk_map_lookup_elem(map, index);
3155 default:
3156 return NULL;
3157 }
3158 }
3159
3160 void bpf_clear_redirect_map(struct bpf_map *map)
3161 {
3162 struct bpf_redirect_info *ri;
3163 int cpu;
3164
3165 for_each_possible_cpu(cpu) {
3166 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
3167 /* Avoid polluting remote cacheline due to writes if
3168 * not needed. Once we pass this test, we need the
3169 * cmpxchg() to make sure it hasn't been changed in
3170 * the meantime by remote CPU.
3171 */
3172 if (unlikely(READ_ONCE(ri->map) == map))
3173 cmpxchg(&ri->map, map, NULL);
3174 }
3175 }
3176
3177 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3178 struct bpf_prog *xdp_prog, struct bpf_map *map,
3179 struct bpf_redirect_info *ri)
3180 {
3181 u32 index = ri->ifindex;
3182 void *fwd = NULL;
3183 int err;
3184
3185 ri->ifindex = 0;
3186 WRITE_ONCE(ri->map, NULL);
3187
3188 fwd = __xdp_map_lookup_elem(map, index);
3189 if (unlikely(!fwd)) {
3190 err = -EINVAL;
3191 goto err;
3192 }
3193 if (ri->map_to_flush && unlikely(ri->map_to_flush != map))
3194 xdp_do_flush_map();
3195
3196 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3197 if (unlikely(err))
3198 goto err;
3199
3200 ri->map_to_flush = map;
3201 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3202 return 0;
3203 err:
3204 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3205 return err;
3206 }
3207
3208 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3209 struct bpf_prog *xdp_prog)
3210 {
3211 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3212 struct bpf_map *map = READ_ONCE(ri->map);
3213
3214 if (likely(map))
3215 return xdp_do_redirect_map(dev, xdp, xdp_prog, map, ri);
3216
3217 return xdp_do_redirect_slow(dev, xdp, xdp_prog, ri);
3218 }
3219 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3220
3221 static int xdp_do_generic_redirect_map(struct net_device *dev,
3222 struct sk_buff *skb,
3223 struct xdp_buff *xdp,
3224 struct bpf_prog *xdp_prog,
3225 struct bpf_map *map)
3226 {
3227 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3228 u32 index = ri->ifindex;
3229 void *fwd = NULL;
3230 int err = 0;
3231
3232 ri->ifindex = 0;
3233 WRITE_ONCE(ri->map, NULL);
3234
3235 fwd = __xdp_map_lookup_elem(map, index);
3236 if (unlikely(!fwd)) {
3237 err = -EINVAL;
3238 goto err;
3239 }
3240
3241 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3242 struct bpf_dtab_netdev *dst = fwd;
3243
3244 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3245 if (unlikely(err))
3246 goto err;
3247 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3248 struct xdp_sock *xs = fwd;
3249
3250 err = xsk_generic_rcv(xs, xdp);
3251 if (err)
3252 goto err;
3253 consume_skb(skb);
3254 } else {
3255 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3256 err = -EBADRQC;
3257 goto err;
3258 }
3259
3260 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3261 return 0;
3262 err:
3263 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3264 return err;
3265 }
3266
3267 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3268 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3269 {
3270 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3271 struct bpf_map *map = READ_ONCE(ri->map);
3272 u32 index = ri->ifindex;
3273 struct net_device *fwd;
3274 int err = 0;
3275
3276 if (map)
3277 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
3278 map);
3279 ri->ifindex = 0;
3280 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3281 if (unlikely(!fwd)) {
3282 err = -EINVAL;
3283 goto err;
3284 }
3285
3286 err = xdp_ok_fwd_dev(fwd, skb->len);
3287 if (unlikely(err))
3288 goto err;
3289
3290 skb->dev = fwd;
3291 _trace_xdp_redirect(dev, xdp_prog, index);
3292 generic_xdp_tx(skb, xdp_prog);
3293 return 0;
3294 err:
3295 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3296 return err;
3297 }
3298 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3299
3300 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3301 {
3302 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3303
3304 if (unlikely(flags))
3305 return XDP_ABORTED;
3306
3307 ri->ifindex = ifindex;
3308 ri->flags = flags;
3309 WRITE_ONCE(ri->map, NULL);
3310
3311 return XDP_REDIRECT;
3312 }
3313
3314 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3315 .func = bpf_xdp_redirect,
3316 .gpl_only = false,
3317 .ret_type = RET_INTEGER,
3318 .arg1_type = ARG_ANYTHING,
3319 .arg2_type = ARG_ANYTHING,
3320 };
3321
3322 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
3323 u64, flags)
3324 {
3325 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3326
3327 if (unlikely(flags))
3328 return XDP_ABORTED;
3329
3330 ri->ifindex = ifindex;
3331 ri->flags = flags;
3332 WRITE_ONCE(ri->map, map);
3333
3334 return XDP_REDIRECT;
3335 }
3336
3337 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3338 .func = bpf_xdp_redirect_map,
3339 .gpl_only = false,
3340 .ret_type = RET_INTEGER,
3341 .arg1_type = ARG_CONST_MAP_PTR,
3342 .arg2_type = ARG_ANYTHING,
3343 .arg3_type = ARG_ANYTHING,
3344 };
3345
3346 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3347 unsigned long off, unsigned long len)
3348 {
3349 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3350
3351 if (unlikely(!ptr))
3352 return len;
3353 if (ptr != dst_buff)
3354 memcpy(dst_buff, ptr, len);
3355
3356 return 0;
3357 }
3358
3359 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3360 u64, flags, void *, meta, u64, meta_size)
3361 {
3362 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3363
3364 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3365 return -EINVAL;
3366 if (unlikely(skb_size > skb->len))
3367 return -EFAULT;
3368
3369 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3370 bpf_skb_copy);
3371 }
3372
3373 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3374 .func = bpf_skb_event_output,
3375 .gpl_only = true,
3376 .ret_type = RET_INTEGER,
3377 .arg1_type = ARG_PTR_TO_CTX,
3378 .arg2_type = ARG_CONST_MAP_PTR,
3379 .arg3_type = ARG_ANYTHING,
3380 .arg4_type = ARG_PTR_TO_MEM,
3381 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3382 };
3383
3384 static unsigned short bpf_tunnel_key_af(u64 flags)
3385 {
3386 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3387 }
3388
3389 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3390 u32, size, u64, flags)
3391 {
3392 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3393 u8 compat[sizeof(struct bpf_tunnel_key)];
3394 void *to_orig = to;
3395 int err;
3396
3397 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3398 err = -EINVAL;
3399 goto err_clear;
3400 }
3401 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3402 err = -EPROTO;
3403 goto err_clear;
3404 }
3405 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3406 err = -EINVAL;
3407 switch (size) {
3408 case offsetof(struct bpf_tunnel_key, tunnel_label):
3409 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3410 goto set_compat;
3411 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3412 /* Fixup deprecated structure layouts here, so we have
3413 * a common path later on.
3414 */
3415 if (ip_tunnel_info_af(info) != AF_INET)
3416 goto err_clear;
3417 set_compat:
3418 to = (struct bpf_tunnel_key *)compat;
3419 break;
3420 default:
3421 goto err_clear;
3422 }
3423 }
3424
3425 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3426 to->tunnel_tos = info->key.tos;
3427 to->tunnel_ttl = info->key.ttl;
3428 to->tunnel_ext = 0;
3429
3430 if (flags & BPF_F_TUNINFO_IPV6) {
3431 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3432 sizeof(to->remote_ipv6));
3433 to->tunnel_label = be32_to_cpu(info->key.label);
3434 } else {
3435 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3436 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3437 to->tunnel_label = 0;
3438 }
3439
3440 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3441 memcpy(to_orig, to, size);
3442
3443 return 0;
3444 err_clear:
3445 memset(to_orig, 0, size);
3446 return err;
3447 }
3448
3449 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3450 .func = bpf_skb_get_tunnel_key,
3451 .gpl_only = false,
3452 .ret_type = RET_INTEGER,
3453 .arg1_type = ARG_PTR_TO_CTX,
3454 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3455 .arg3_type = ARG_CONST_SIZE,
3456 .arg4_type = ARG_ANYTHING,
3457 };
3458
3459 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3460 {
3461 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3462 int err;
3463
3464 if (unlikely(!info ||
3465 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3466 err = -ENOENT;
3467 goto err_clear;
3468 }
3469 if (unlikely(size < info->options_len)) {
3470 err = -ENOMEM;
3471 goto err_clear;
3472 }
3473
3474 ip_tunnel_info_opts_get(to, info);
3475 if (size > info->options_len)
3476 memset(to + info->options_len, 0, size - info->options_len);
3477
3478 return info->options_len;
3479 err_clear:
3480 memset(to, 0, size);
3481 return err;
3482 }
3483
3484 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3485 .func = bpf_skb_get_tunnel_opt,
3486 .gpl_only = false,
3487 .ret_type = RET_INTEGER,
3488 .arg1_type = ARG_PTR_TO_CTX,
3489 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3490 .arg3_type = ARG_CONST_SIZE,
3491 };
3492
3493 static struct metadata_dst __percpu *md_dst;
3494
3495 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3496 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3497 {
3498 struct metadata_dst *md = this_cpu_ptr(md_dst);
3499 u8 compat[sizeof(struct bpf_tunnel_key)];
3500 struct ip_tunnel_info *info;
3501
3502 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3503 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3504 return -EINVAL;
3505 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3506 switch (size) {
3507 case offsetof(struct bpf_tunnel_key, tunnel_label):
3508 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3509 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3510 /* Fixup deprecated structure layouts here, so we have
3511 * a common path later on.
3512 */
3513 memcpy(compat, from, size);
3514 memset(compat + size, 0, sizeof(compat) - size);
3515 from = (const struct bpf_tunnel_key *) compat;
3516 break;
3517 default:
3518 return -EINVAL;
3519 }
3520 }
3521 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3522 from->tunnel_ext))
3523 return -EINVAL;
3524
3525 skb_dst_drop(skb);
3526 dst_hold((struct dst_entry *) md);
3527 skb_dst_set(skb, (struct dst_entry *) md);
3528
3529 info = &md->u.tun_info;
3530 memset(info, 0, sizeof(*info));
3531 info->mode = IP_TUNNEL_INFO_TX;
3532
3533 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3534 if (flags & BPF_F_DONT_FRAGMENT)
3535 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3536 if (flags & BPF_F_ZERO_CSUM_TX)
3537 info->key.tun_flags &= ~TUNNEL_CSUM;
3538 if (flags & BPF_F_SEQ_NUMBER)
3539 info->key.tun_flags |= TUNNEL_SEQ;
3540
3541 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3542 info->key.tos = from->tunnel_tos;
3543 info->key.ttl = from->tunnel_ttl;
3544
3545 if (flags & BPF_F_TUNINFO_IPV6) {
3546 info->mode |= IP_TUNNEL_INFO_IPV6;
3547 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3548 sizeof(from->remote_ipv6));
3549 info->key.label = cpu_to_be32(from->tunnel_label) &
3550 IPV6_FLOWLABEL_MASK;
3551 } else {
3552 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3553 }
3554
3555 return 0;
3556 }
3557
3558 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3559 .func = bpf_skb_set_tunnel_key,
3560 .gpl_only = false,
3561 .ret_type = RET_INTEGER,
3562 .arg1_type = ARG_PTR_TO_CTX,
3563 .arg2_type = ARG_PTR_TO_MEM,
3564 .arg3_type = ARG_CONST_SIZE,
3565 .arg4_type = ARG_ANYTHING,
3566 };
3567
3568 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3569 const u8 *, from, u32, size)
3570 {
3571 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3572 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3573
3574 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3575 return -EINVAL;
3576 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3577 return -ENOMEM;
3578
3579 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
3580
3581 return 0;
3582 }
3583
3584 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3585 .func = bpf_skb_set_tunnel_opt,
3586 .gpl_only = false,
3587 .ret_type = RET_INTEGER,
3588 .arg1_type = ARG_PTR_TO_CTX,
3589 .arg2_type = ARG_PTR_TO_MEM,
3590 .arg3_type = ARG_CONST_SIZE,
3591 };
3592
3593 static const struct bpf_func_proto *
3594 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3595 {
3596 if (!md_dst) {
3597 struct metadata_dst __percpu *tmp;
3598
3599 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3600 METADATA_IP_TUNNEL,
3601 GFP_KERNEL);
3602 if (!tmp)
3603 return NULL;
3604 if (cmpxchg(&md_dst, NULL, tmp))
3605 metadata_dst_free_percpu(tmp);
3606 }
3607
3608 switch (which) {
3609 case BPF_FUNC_skb_set_tunnel_key:
3610 return &bpf_skb_set_tunnel_key_proto;
3611 case BPF_FUNC_skb_set_tunnel_opt:
3612 return &bpf_skb_set_tunnel_opt_proto;
3613 default:
3614 return NULL;
3615 }
3616 }
3617
3618 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3619 u32, idx)
3620 {
3621 struct bpf_array *array = container_of(map, struct bpf_array, map);
3622 struct cgroup *cgrp;
3623 struct sock *sk;
3624
3625 sk = skb_to_full_sk(skb);
3626 if (!sk || !sk_fullsock(sk))
3627 return -ENOENT;
3628 if (unlikely(idx >= array->map.max_entries))
3629 return -E2BIG;
3630
3631 cgrp = READ_ONCE(array->ptrs[idx]);
3632 if (unlikely(!cgrp))
3633 return -EAGAIN;
3634
3635 return sk_under_cgroup_hierarchy(sk, cgrp);
3636 }
3637
3638 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3639 .func = bpf_skb_under_cgroup,
3640 .gpl_only = false,
3641 .ret_type = RET_INTEGER,
3642 .arg1_type = ARG_PTR_TO_CTX,
3643 .arg2_type = ARG_CONST_MAP_PTR,
3644 .arg3_type = ARG_ANYTHING,
3645 };
3646
3647 #ifdef CONFIG_SOCK_CGROUP_DATA
3648 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
3649 {
3650 struct sock *sk = skb_to_full_sk(skb);
3651 struct cgroup *cgrp;
3652
3653 if (!sk || !sk_fullsock(sk))
3654 return 0;
3655
3656 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3657 return cgrp->kn->id.id;
3658 }
3659
3660 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
3661 .func = bpf_skb_cgroup_id,
3662 .gpl_only = false,
3663 .ret_type = RET_INTEGER,
3664 .arg1_type = ARG_PTR_TO_CTX,
3665 };
3666
3667 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
3668 ancestor_level)
3669 {
3670 struct sock *sk = skb_to_full_sk(skb);
3671 struct cgroup *ancestor;
3672 struct cgroup *cgrp;
3673
3674 if (!sk || !sk_fullsock(sk))
3675 return 0;
3676
3677 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3678 ancestor = cgroup_ancestor(cgrp, ancestor_level);
3679 if (!ancestor)
3680 return 0;
3681
3682 return ancestor->kn->id.id;
3683 }
3684
3685 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
3686 .func = bpf_skb_ancestor_cgroup_id,
3687 .gpl_only = false,
3688 .ret_type = RET_INTEGER,
3689 .arg1_type = ARG_PTR_TO_CTX,
3690 .arg2_type = ARG_ANYTHING,
3691 };
3692 #endif
3693
3694 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3695 unsigned long off, unsigned long len)
3696 {
3697 memcpy(dst_buff, src_buff + off, len);
3698 return 0;
3699 }
3700
3701 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3702 u64, flags, void *, meta, u64, meta_size)
3703 {
3704 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3705
3706 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3707 return -EINVAL;
3708 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3709 return -EFAULT;
3710
3711 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3712 xdp_size, bpf_xdp_copy);
3713 }
3714
3715 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3716 .func = bpf_xdp_event_output,
3717 .gpl_only = true,
3718 .ret_type = RET_INTEGER,
3719 .arg1_type = ARG_PTR_TO_CTX,
3720 .arg2_type = ARG_CONST_MAP_PTR,
3721 .arg3_type = ARG_ANYTHING,
3722 .arg4_type = ARG_PTR_TO_MEM,
3723 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3724 };
3725
3726 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3727 {
3728 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3729 }
3730
3731 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3732 .func = bpf_get_socket_cookie,
3733 .gpl_only = false,
3734 .ret_type = RET_INTEGER,
3735 .arg1_type = ARG_PTR_TO_CTX,
3736 };
3737
3738 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
3739 {
3740 return sock_gen_cookie(ctx->sk);
3741 }
3742
3743 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
3744 .func = bpf_get_socket_cookie_sock_addr,
3745 .gpl_only = false,
3746 .ret_type = RET_INTEGER,
3747 .arg1_type = ARG_PTR_TO_CTX,
3748 };
3749
3750 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
3751 {
3752 return sock_gen_cookie(ctx->sk);
3753 }
3754
3755 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
3756 .func = bpf_get_socket_cookie_sock_ops,
3757 .gpl_only = false,
3758 .ret_type = RET_INTEGER,
3759 .arg1_type = ARG_PTR_TO_CTX,
3760 };
3761
3762 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3763 {
3764 struct sock *sk = sk_to_full_sk(skb->sk);
3765 kuid_t kuid;
3766
3767 if (!sk || !sk_fullsock(sk))
3768 return overflowuid;
3769 kuid = sock_net_uid(sock_net(sk), sk);
3770 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3771 }
3772
3773 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3774 .func = bpf_get_socket_uid,
3775 .gpl_only = false,
3776 .ret_type = RET_INTEGER,
3777 .arg1_type = ARG_PTR_TO_CTX,
3778 };
3779
3780 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3781 int, level, int, optname, char *, optval, int, optlen)
3782 {
3783 struct sock *sk = bpf_sock->sk;
3784 int ret = 0;
3785 int val;
3786
3787 if (!sk_fullsock(sk))
3788 return -EINVAL;
3789
3790 if (level == SOL_SOCKET) {
3791 if (optlen != sizeof(int))
3792 return -EINVAL;
3793 val = *((int *)optval);
3794
3795 /* Only some socketops are supported */
3796 switch (optname) {
3797 case SO_RCVBUF:
3798 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3799 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3800 break;
3801 case SO_SNDBUF:
3802 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3803 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3804 break;
3805 case SO_MAX_PACING_RATE:
3806 sk->sk_max_pacing_rate = val;
3807 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3808 sk->sk_max_pacing_rate);
3809 break;
3810 case SO_PRIORITY:
3811 sk->sk_priority = val;
3812 break;
3813 case SO_RCVLOWAT:
3814 if (val < 0)
3815 val = INT_MAX;
3816 sk->sk_rcvlowat = val ? : 1;
3817 break;
3818 case SO_MARK:
3819 sk->sk_mark = val;
3820 break;
3821 default:
3822 ret = -EINVAL;
3823 }
3824 #ifdef CONFIG_INET
3825 } else if (level == SOL_IP) {
3826 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3827 return -EINVAL;
3828
3829 val = *((int *)optval);
3830 /* Only some options are supported */
3831 switch (optname) {
3832 case IP_TOS:
3833 if (val < -1 || val > 0xff) {
3834 ret = -EINVAL;
3835 } else {
3836 struct inet_sock *inet = inet_sk(sk);
3837
3838 if (val == -1)
3839 val = 0;
3840 inet->tos = val;
3841 }
3842 break;
3843 default:
3844 ret = -EINVAL;
3845 }
3846 #if IS_ENABLED(CONFIG_IPV6)
3847 } else if (level == SOL_IPV6) {
3848 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3849 return -EINVAL;
3850
3851 val = *((int *)optval);
3852 /* Only some options are supported */
3853 switch (optname) {
3854 case IPV6_TCLASS:
3855 if (val < -1 || val > 0xff) {
3856 ret = -EINVAL;
3857 } else {
3858 struct ipv6_pinfo *np = inet6_sk(sk);
3859
3860 if (val == -1)
3861 val = 0;
3862 np->tclass = val;
3863 }
3864 break;
3865 default:
3866 ret = -EINVAL;
3867 }
3868 #endif
3869 } else if (level == SOL_TCP &&
3870 sk->sk_prot->setsockopt == tcp_setsockopt) {
3871 if (optname == TCP_CONGESTION) {
3872 char name[TCP_CA_NAME_MAX];
3873 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3874
3875 strncpy(name, optval, min_t(long, optlen,
3876 TCP_CA_NAME_MAX-1));
3877 name[TCP_CA_NAME_MAX-1] = 0;
3878 ret = tcp_set_congestion_control(sk, name, false,
3879 reinit);
3880 } else {
3881 struct tcp_sock *tp = tcp_sk(sk);
3882
3883 if (optlen != sizeof(int))
3884 return -EINVAL;
3885
3886 val = *((int *)optval);
3887 /* Only some options are supported */
3888 switch (optname) {
3889 case TCP_BPF_IW:
3890 if (val <= 0 || tp->data_segs_out > 0)
3891 ret = -EINVAL;
3892 else
3893 tp->snd_cwnd = val;
3894 break;
3895 case TCP_BPF_SNDCWND_CLAMP:
3896 if (val <= 0) {
3897 ret = -EINVAL;
3898 } else {
3899 tp->snd_cwnd_clamp = val;
3900 tp->snd_ssthresh = val;
3901 }
3902 break;
3903 case TCP_SAVE_SYN:
3904 if (val < 0 || val > 1)
3905 ret = -EINVAL;
3906 else
3907 tp->save_syn = val;
3908 break;
3909 default:
3910 ret = -EINVAL;
3911 }
3912 }
3913 #endif
3914 } else {
3915 ret = -EINVAL;
3916 }
3917 return ret;
3918 }
3919
3920 static const struct bpf_func_proto bpf_setsockopt_proto = {
3921 .func = bpf_setsockopt,
3922 .gpl_only = false,
3923 .ret_type = RET_INTEGER,
3924 .arg1_type = ARG_PTR_TO_CTX,
3925 .arg2_type = ARG_ANYTHING,
3926 .arg3_type = ARG_ANYTHING,
3927 .arg4_type = ARG_PTR_TO_MEM,
3928 .arg5_type = ARG_CONST_SIZE,
3929 };
3930
3931 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3932 int, level, int, optname, char *, optval, int, optlen)
3933 {
3934 struct sock *sk = bpf_sock->sk;
3935
3936 if (!sk_fullsock(sk))
3937 goto err_clear;
3938 #ifdef CONFIG_INET
3939 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3940 struct inet_connection_sock *icsk;
3941 struct tcp_sock *tp;
3942
3943 switch (optname) {
3944 case TCP_CONGESTION:
3945 icsk = inet_csk(sk);
3946
3947 if (!icsk->icsk_ca_ops || optlen <= 1)
3948 goto err_clear;
3949 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3950 optval[optlen - 1] = 0;
3951 break;
3952 case TCP_SAVED_SYN:
3953 tp = tcp_sk(sk);
3954
3955 if (optlen <= 0 || !tp->saved_syn ||
3956 optlen > tp->saved_syn[0])
3957 goto err_clear;
3958 memcpy(optval, tp->saved_syn + 1, optlen);
3959 break;
3960 default:
3961 goto err_clear;
3962 }
3963 } else if (level == SOL_IP) {
3964 struct inet_sock *inet = inet_sk(sk);
3965
3966 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3967 goto err_clear;
3968
3969 /* Only some options are supported */
3970 switch (optname) {
3971 case IP_TOS:
3972 *((int *)optval) = (int)inet->tos;
3973 break;
3974 default:
3975 goto err_clear;
3976 }
3977 #if IS_ENABLED(CONFIG_IPV6)
3978 } else if (level == SOL_IPV6) {
3979 struct ipv6_pinfo *np = inet6_sk(sk);
3980
3981 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3982 goto err_clear;
3983
3984 /* Only some options are supported */
3985 switch (optname) {
3986 case IPV6_TCLASS:
3987 *((int *)optval) = (int)np->tclass;
3988 break;
3989 default:
3990 goto err_clear;
3991 }
3992 #endif
3993 } else {
3994 goto err_clear;
3995 }
3996 return 0;
3997 #endif
3998 err_clear:
3999 memset(optval, 0, optlen);
4000 return -EINVAL;
4001 }
4002
4003 static const struct bpf_func_proto bpf_getsockopt_proto = {
4004 .func = bpf_getsockopt,
4005 .gpl_only = false,
4006 .ret_type = RET_INTEGER,
4007 .arg1_type = ARG_PTR_TO_CTX,
4008 .arg2_type = ARG_ANYTHING,
4009 .arg3_type = ARG_ANYTHING,
4010 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4011 .arg5_type = ARG_CONST_SIZE,
4012 };
4013
4014 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4015 int, argval)
4016 {
4017 struct sock *sk = bpf_sock->sk;
4018 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4019
4020 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4021 return -EINVAL;
4022
4023 if (val)
4024 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4025
4026 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4027 }
4028
4029 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4030 .func = bpf_sock_ops_cb_flags_set,
4031 .gpl_only = false,
4032 .ret_type = RET_INTEGER,
4033 .arg1_type = ARG_PTR_TO_CTX,
4034 .arg2_type = ARG_ANYTHING,
4035 };
4036
4037 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4038 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4039
4040 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4041 int, addr_len)
4042 {
4043 #ifdef CONFIG_INET
4044 struct sock *sk = ctx->sk;
4045 int err;
4046
4047 /* Binding to port can be expensive so it's prohibited in the helper.
4048 * Only binding to IP is supported.
4049 */
4050 err = -EINVAL;
4051 if (addr->sa_family == AF_INET) {
4052 if (addr_len < sizeof(struct sockaddr_in))
4053 return err;
4054 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4055 return err;
4056 return __inet_bind(sk, addr, addr_len, true, false);
4057 #if IS_ENABLED(CONFIG_IPV6)
4058 } else if (addr->sa_family == AF_INET6) {
4059 if (addr_len < SIN6_LEN_RFC2133)
4060 return err;
4061 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4062 return err;
4063 /* ipv6_bpf_stub cannot be NULL, since it's called from
4064 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4065 */
4066 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4067 #endif /* CONFIG_IPV6 */
4068 }
4069 #endif /* CONFIG_INET */
4070
4071 return -EAFNOSUPPORT;
4072 }
4073
4074 static const struct bpf_func_proto bpf_bind_proto = {
4075 .func = bpf_bind,
4076 .gpl_only = false,
4077 .ret_type = RET_INTEGER,
4078 .arg1_type = ARG_PTR_TO_CTX,
4079 .arg2_type = ARG_PTR_TO_MEM,
4080 .arg3_type = ARG_CONST_SIZE,
4081 };
4082
4083 #ifdef CONFIG_XFRM
4084 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4085 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4086 {
4087 const struct sec_path *sp = skb_sec_path(skb);
4088 const struct xfrm_state *x;
4089
4090 if (!sp || unlikely(index >= sp->len || flags))
4091 goto err_clear;
4092
4093 x = sp->xvec[index];
4094
4095 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4096 goto err_clear;
4097
4098 to->reqid = x->props.reqid;
4099 to->spi = x->id.spi;
4100 to->family = x->props.family;
4101 to->ext = 0;
4102
4103 if (to->family == AF_INET6) {
4104 memcpy(to->remote_ipv6, x->props.saddr.a6,
4105 sizeof(to->remote_ipv6));
4106 } else {
4107 to->remote_ipv4 = x->props.saddr.a4;
4108 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4109 }
4110
4111 return 0;
4112 err_clear:
4113 memset(to, 0, size);
4114 return -EINVAL;
4115 }
4116
4117 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4118 .func = bpf_skb_get_xfrm_state,
4119 .gpl_only = false,
4120 .ret_type = RET_INTEGER,
4121 .arg1_type = ARG_PTR_TO_CTX,
4122 .arg2_type = ARG_ANYTHING,
4123 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4124 .arg4_type = ARG_CONST_SIZE,
4125 .arg5_type = ARG_ANYTHING,
4126 };
4127 #endif
4128
4129 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4130 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4131 const struct neighbour *neigh,
4132 const struct net_device *dev)
4133 {
4134 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4135 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4136 params->h_vlan_TCI = 0;
4137 params->h_vlan_proto = 0;
4138 params->ifindex = dev->ifindex;
4139
4140 return 0;
4141 }
4142 #endif
4143
4144 #if IS_ENABLED(CONFIG_INET)
4145 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4146 u32 flags, bool check_mtu)
4147 {
4148 struct in_device *in_dev;
4149 struct neighbour *neigh;
4150 struct net_device *dev;
4151 struct fib_result res;
4152 struct fib_nh *nh;
4153 struct flowi4 fl4;
4154 int err;
4155 u32 mtu;
4156
4157 dev = dev_get_by_index_rcu(net, params->ifindex);
4158 if (unlikely(!dev))
4159 return -ENODEV;
4160
4161 /* verify forwarding is enabled on this interface */
4162 in_dev = __in_dev_get_rcu(dev);
4163 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4164 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4165
4166 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4167 fl4.flowi4_iif = 1;
4168 fl4.flowi4_oif = params->ifindex;
4169 } else {
4170 fl4.flowi4_iif = params->ifindex;
4171 fl4.flowi4_oif = 0;
4172 }
4173 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4174 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4175 fl4.flowi4_flags = 0;
4176
4177 fl4.flowi4_proto = params->l4_protocol;
4178 fl4.daddr = params->ipv4_dst;
4179 fl4.saddr = params->ipv4_src;
4180 fl4.fl4_sport = params->sport;
4181 fl4.fl4_dport = params->dport;
4182
4183 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4184 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4185 struct fib_table *tb;
4186
4187 tb = fib_get_table(net, tbid);
4188 if (unlikely(!tb))
4189 return BPF_FIB_LKUP_RET_NOT_FWDED;
4190
4191 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4192 } else {
4193 fl4.flowi4_mark = 0;
4194 fl4.flowi4_secid = 0;
4195 fl4.flowi4_tun_key.tun_id = 0;
4196 fl4.flowi4_uid = sock_net_uid(net, NULL);
4197
4198 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4199 }
4200
4201 if (err) {
4202 /* map fib lookup errors to RTN_ type */
4203 if (err == -EINVAL)
4204 return BPF_FIB_LKUP_RET_BLACKHOLE;
4205 if (err == -EHOSTUNREACH)
4206 return BPF_FIB_LKUP_RET_UNREACHABLE;
4207 if (err == -EACCES)
4208 return BPF_FIB_LKUP_RET_PROHIBIT;
4209
4210 return BPF_FIB_LKUP_RET_NOT_FWDED;
4211 }
4212
4213 if (res.type != RTN_UNICAST)
4214 return BPF_FIB_LKUP_RET_NOT_FWDED;
4215
4216 if (res.fi->fib_nhs > 1)
4217 fib_select_path(net, &res, &fl4, NULL);
4218
4219 if (check_mtu) {
4220 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4221 if (params->tot_len > mtu)
4222 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4223 }
4224
4225 nh = &res.fi->fib_nh[res.nh_sel];
4226
4227 /* do not handle lwt encaps right now */
4228 if (nh->nh_lwtstate)
4229 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4230
4231 dev = nh->nh_dev;
4232 if (nh->nh_gw)
4233 params->ipv4_dst = nh->nh_gw;
4234
4235 params->rt_metric = res.fi->fib_priority;
4236
4237 /* xdp and cls_bpf programs are run in RCU-bh so
4238 * rcu_read_lock_bh is not needed here
4239 */
4240 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4241 if (!neigh)
4242 return BPF_FIB_LKUP_RET_NO_NEIGH;
4243
4244 return bpf_fib_set_fwd_params(params, neigh, dev);
4245 }
4246 #endif
4247
4248 #if IS_ENABLED(CONFIG_IPV6)
4249 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4250 u32 flags, bool check_mtu)
4251 {
4252 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4253 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4254 struct neighbour *neigh;
4255 struct net_device *dev;
4256 struct inet6_dev *idev;
4257 struct fib6_info *f6i;
4258 struct flowi6 fl6;
4259 int strict = 0;
4260 int oif;
4261 u32 mtu;
4262
4263 /* link local addresses are never forwarded */
4264 if (rt6_need_strict(dst) || rt6_need_strict(src))
4265 return BPF_FIB_LKUP_RET_NOT_FWDED;
4266
4267 dev = dev_get_by_index_rcu(net, params->ifindex);
4268 if (unlikely(!dev))
4269 return -ENODEV;
4270
4271 idev = __in6_dev_get_safely(dev);
4272 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4273 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4274
4275 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4276 fl6.flowi6_iif = 1;
4277 oif = fl6.flowi6_oif = params->ifindex;
4278 } else {
4279 oif = fl6.flowi6_iif = params->ifindex;
4280 fl6.flowi6_oif = 0;
4281 strict = RT6_LOOKUP_F_HAS_SADDR;
4282 }
4283 fl6.flowlabel = params->flowinfo;
4284 fl6.flowi6_scope = 0;
4285 fl6.flowi6_flags = 0;
4286 fl6.mp_hash = 0;
4287
4288 fl6.flowi6_proto = params->l4_protocol;
4289 fl6.daddr = *dst;
4290 fl6.saddr = *src;
4291 fl6.fl6_sport = params->sport;
4292 fl6.fl6_dport = params->dport;
4293
4294 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4295 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4296 struct fib6_table *tb;
4297
4298 tb = ipv6_stub->fib6_get_table(net, tbid);
4299 if (unlikely(!tb))
4300 return BPF_FIB_LKUP_RET_NOT_FWDED;
4301
4302 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4303 } else {
4304 fl6.flowi6_mark = 0;
4305 fl6.flowi6_secid = 0;
4306 fl6.flowi6_tun_key.tun_id = 0;
4307 fl6.flowi6_uid = sock_net_uid(net, NULL);
4308
4309 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4310 }
4311
4312 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4313 return BPF_FIB_LKUP_RET_NOT_FWDED;
4314
4315 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4316 switch (f6i->fib6_type) {
4317 case RTN_BLACKHOLE:
4318 return BPF_FIB_LKUP_RET_BLACKHOLE;
4319 case RTN_UNREACHABLE:
4320 return BPF_FIB_LKUP_RET_UNREACHABLE;
4321 case RTN_PROHIBIT:
4322 return BPF_FIB_LKUP_RET_PROHIBIT;
4323 default:
4324 return BPF_FIB_LKUP_RET_NOT_FWDED;
4325 }
4326 }
4327
4328 if (f6i->fib6_type != RTN_UNICAST)
4329 return BPF_FIB_LKUP_RET_NOT_FWDED;
4330
4331 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4332 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4333 fl6.flowi6_oif, NULL,
4334 strict);
4335
4336 if (check_mtu) {
4337 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4338 if (params->tot_len > mtu)
4339 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4340 }
4341
4342 if (f6i->fib6_nh.nh_lwtstate)
4343 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4344
4345 if (f6i->fib6_flags & RTF_GATEWAY)
4346 *dst = f6i->fib6_nh.nh_gw;
4347
4348 dev = f6i->fib6_nh.nh_dev;
4349 params->rt_metric = f6i->fib6_metric;
4350
4351 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4352 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4353 * because we need to get nd_tbl via the stub
4354 */
4355 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4356 ndisc_hashfn, dst, dev);
4357 if (!neigh)
4358 return BPF_FIB_LKUP_RET_NO_NEIGH;
4359
4360 return bpf_fib_set_fwd_params(params, neigh, dev);
4361 }
4362 #endif
4363
4364 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4365 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4366 {
4367 if (plen < sizeof(*params))
4368 return -EINVAL;
4369
4370 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4371 return -EINVAL;
4372
4373 switch (params->family) {
4374 #if IS_ENABLED(CONFIG_INET)
4375 case AF_INET:
4376 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4377 flags, true);
4378 #endif
4379 #if IS_ENABLED(CONFIG_IPV6)
4380 case AF_INET6:
4381 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4382 flags, true);
4383 #endif
4384 }
4385 return -EAFNOSUPPORT;
4386 }
4387
4388 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4389 .func = bpf_xdp_fib_lookup,
4390 .gpl_only = true,
4391 .ret_type = RET_INTEGER,
4392 .arg1_type = ARG_PTR_TO_CTX,
4393 .arg2_type = ARG_PTR_TO_MEM,
4394 .arg3_type = ARG_CONST_SIZE,
4395 .arg4_type = ARG_ANYTHING,
4396 };
4397
4398 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4399 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4400 {
4401 struct net *net = dev_net(skb->dev);
4402 int rc = -EAFNOSUPPORT;
4403
4404 if (plen < sizeof(*params))
4405 return -EINVAL;
4406
4407 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4408 return -EINVAL;
4409
4410 switch (params->family) {
4411 #if IS_ENABLED(CONFIG_INET)
4412 case AF_INET:
4413 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4414 break;
4415 #endif
4416 #if IS_ENABLED(CONFIG_IPV6)
4417 case AF_INET6:
4418 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4419 break;
4420 #endif
4421 }
4422
4423 if (!rc) {
4424 struct net_device *dev;
4425
4426 dev = dev_get_by_index_rcu(net, params->ifindex);
4427 if (!is_skb_forwardable(dev, skb))
4428 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4429 }
4430
4431 return rc;
4432 }
4433
4434 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4435 .func = bpf_skb_fib_lookup,
4436 .gpl_only = true,
4437 .ret_type = RET_INTEGER,
4438 .arg1_type = ARG_PTR_TO_CTX,
4439 .arg2_type = ARG_PTR_TO_MEM,
4440 .arg3_type = ARG_CONST_SIZE,
4441 .arg4_type = ARG_ANYTHING,
4442 };
4443
4444 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4445 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4446 {
4447 int err;
4448 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4449
4450 if (!seg6_validate_srh(srh, len))
4451 return -EINVAL;
4452
4453 switch (type) {
4454 case BPF_LWT_ENCAP_SEG6_INLINE:
4455 if (skb->protocol != htons(ETH_P_IPV6))
4456 return -EBADMSG;
4457
4458 err = seg6_do_srh_inline(skb, srh);
4459 break;
4460 case BPF_LWT_ENCAP_SEG6:
4461 skb_reset_inner_headers(skb);
4462 skb->encapsulation = 1;
4463 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4464 break;
4465 default:
4466 return -EINVAL;
4467 }
4468
4469 bpf_compute_data_pointers(skb);
4470 if (err)
4471 return err;
4472
4473 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4474 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4475
4476 return seg6_lookup_nexthop(skb, NULL, 0);
4477 }
4478 #endif /* CONFIG_IPV6_SEG6_BPF */
4479
4480 BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4481 u32, len)
4482 {
4483 switch (type) {
4484 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4485 case BPF_LWT_ENCAP_SEG6:
4486 case BPF_LWT_ENCAP_SEG6_INLINE:
4487 return bpf_push_seg6_encap(skb, type, hdr, len);
4488 #endif
4489 default:
4490 return -EINVAL;
4491 }
4492 }
4493
4494 static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4495 .func = bpf_lwt_push_encap,
4496 .gpl_only = false,
4497 .ret_type = RET_INTEGER,
4498 .arg1_type = ARG_PTR_TO_CTX,
4499 .arg2_type = ARG_ANYTHING,
4500 .arg3_type = ARG_PTR_TO_MEM,
4501 .arg4_type = ARG_CONST_SIZE
4502 };
4503
4504 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4505 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4506 const void *, from, u32, len)
4507 {
4508 struct seg6_bpf_srh_state *srh_state =
4509 this_cpu_ptr(&seg6_bpf_srh_states);
4510 struct ipv6_sr_hdr *srh = srh_state->srh;
4511 void *srh_tlvs, *srh_end, *ptr;
4512 int srhoff = 0;
4513
4514 if (srh == NULL)
4515 return -EINVAL;
4516
4517 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4518 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4519
4520 ptr = skb->data + offset;
4521 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4522 srh_state->valid = false;
4523 else if (ptr < (void *)&srh->flags ||
4524 ptr + len > (void *)&srh->segments)
4525 return -EFAULT;
4526
4527 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4528 return -EFAULT;
4529 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4530 return -EINVAL;
4531 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4532
4533 memcpy(skb->data + offset, from, len);
4534 return 0;
4535 }
4536
4537 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4538 .func = bpf_lwt_seg6_store_bytes,
4539 .gpl_only = false,
4540 .ret_type = RET_INTEGER,
4541 .arg1_type = ARG_PTR_TO_CTX,
4542 .arg2_type = ARG_ANYTHING,
4543 .arg3_type = ARG_PTR_TO_MEM,
4544 .arg4_type = ARG_CONST_SIZE
4545 };
4546
4547 static void bpf_update_srh_state(struct sk_buff *skb)
4548 {
4549 struct seg6_bpf_srh_state *srh_state =
4550 this_cpu_ptr(&seg6_bpf_srh_states);
4551 int srhoff = 0;
4552
4553 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
4554 srh_state->srh = NULL;
4555 } else {
4556 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4557 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
4558 srh_state->valid = true;
4559 }
4560 }
4561
4562 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4563 u32, action, void *, param, u32, param_len)
4564 {
4565 struct seg6_bpf_srh_state *srh_state =
4566 this_cpu_ptr(&seg6_bpf_srh_states);
4567 int hdroff = 0;
4568 int err;
4569
4570 switch (action) {
4571 case SEG6_LOCAL_ACTION_END_X:
4572 if (!seg6_bpf_has_valid_srh(skb))
4573 return -EBADMSG;
4574 if (param_len != sizeof(struct in6_addr))
4575 return -EINVAL;
4576 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4577 case SEG6_LOCAL_ACTION_END_T:
4578 if (!seg6_bpf_has_valid_srh(skb))
4579 return -EBADMSG;
4580 if (param_len != sizeof(int))
4581 return -EINVAL;
4582 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4583 case SEG6_LOCAL_ACTION_END_DT6:
4584 if (!seg6_bpf_has_valid_srh(skb))
4585 return -EBADMSG;
4586 if (param_len != sizeof(int))
4587 return -EINVAL;
4588
4589 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
4590 return -EBADMSG;
4591 if (!pskb_pull(skb, hdroff))
4592 return -EBADMSG;
4593
4594 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
4595 skb_reset_network_header(skb);
4596 skb_reset_transport_header(skb);
4597 skb->encapsulation = 0;
4598
4599 bpf_compute_data_pointers(skb);
4600 bpf_update_srh_state(skb);
4601 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4602 case SEG6_LOCAL_ACTION_END_B6:
4603 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4604 return -EBADMSG;
4605 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4606 param, param_len);
4607 if (!err)
4608 bpf_update_srh_state(skb);
4609
4610 return err;
4611 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
4612 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4613 return -EBADMSG;
4614 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4615 param, param_len);
4616 if (!err)
4617 bpf_update_srh_state(skb);
4618
4619 return err;
4620 default:
4621 return -EINVAL;
4622 }
4623 }
4624
4625 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4626 .func = bpf_lwt_seg6_action,
4627 .gpl_only = false,
4628 .ret_type = RET_INTEGER,
4629 .arg1_type = ARG_PTR_TO_CTX,
4630 .arg2_type = ARG_ANYTHING,
4631 .arg3_type = ARG_PTR_TO_MEM,
4632 .arg4_type = ARG_CONST_SIZE
4633 };
4634
4635 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4636 s32, len)
4637 {
4638 struct seg6_bpf_srh_state *srh_state =
4639 this_cpu_ptr(&seg6_bpf_srh_states);
4640 struct ipv6_sr_hdr *srh = srh_state->srh;
4641 void *srh_end, *srh_tlvs, *ptr;
4642 struct ipv6hdr *hdr;
4643 int srhoff = 0;
4644 int ret;
4645
4646 if (unlikely(srh == NULL))
4647 return -EINVAL;
4648
4649 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4650 ((srh->first_segment + 1) << 4));
4651 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4652 srh_state->hdrlen);
4653 ptr = skb->data + offset;
4654
4655 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4656 return -EFAULT;
4657 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4658 return -EFAULT;
4659
4660 if (len > 0) {
4661 ret = skb_cow_head(skb, len);
4662 if (unlikely(ret < 0))
4663 return ret;
4664
4665 ret = bpf_skb_net_hdr_push(skb, offset, len);
4666 } else {
4667 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4668 }
4669
4670 bpf_compute_data_pointers(skb);
4671 if (unlikely(ret < 0))
4672 return ret;
4673
4674 hdr = (struct ipv6hdr *)skb->data;
4675 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4676
4677 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4678 return -EINVAL;
4679 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4680 srh_state->hdrlen += len;
4681 srh_state->valid = false;
4682 return 0;
4683 }
4684
4685 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
4686 .func = bpf_lwt_seg6_adjust_srh,
4687 .gpl_only = false,
4688 .ret_type = RET_INTEGER,
4689 .arg1_type = ARG_PTR_TO_CTX,
4690 .arg2_type = ARG_ANYTHING,
4691 .arg3_type = ARG_ANYTHING,
4692 };
4693 #endif /* CONFIG_IPV6_SEG6_BPF */
4694
4695 #ifdef CONFIG_INET
4696 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
4697 struct sk_buff *skb, u8 family, u8 proto)
4698 {
4699 bool refcounted = false;
4700 struct sock *sk = NULL;
4701 int dif = 0;
4702
4703 if (skb->dev)
4704 dif = skb->dev->ifindex;
4705
4706 if (family == AF_INET) {
4707 __be32 src4 = tuple->ipv4.saddr;
4708 __be32 dst4 = tuple->ipv4.daddr;
4709 int sdif = inet_sdif(skb);
4710
4711 if (proto == IPPROTO_TCP)
4712 sk = __inet_lookup(net, &tcp_hashinfo, skb, 0,
4713 src4, tuple->ipv4.sport,
4714 dst4, tuple->ipv4.dport,
4715 dif, sdif, &refcounted);
4716 else
4717 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
4718 dst4, tuple->ipv4.dport,
4719 dif, sdif, &udp_table, skb);
4720 #if IS_REACHABLE(CONFIG_IPV6)
4721 } else {
4722 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
4723 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
4724 int sdif = inet6_sdif(skb);
4725
4726 if (proto == IPPROTO_TCP)
4727 sk = __inet6_lookup(net, &tcp_hashinfo, skb, 0,
4728 src6, tuple->ipv6.sport,
4729 dst6, tuple->ipv6.dport,
4730 dif, sdif, &refcounted);
4731 else
4732 sk = __udp6_lib_lookup(net, src6, tuple->ipv6.sport,
4733 dst6, tuple->ipv6.dport,
4734 dif, sdif, &udp_table, skb);
4735 #endif
4736 }
4737
4738 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
4739 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
4740 sk = NULL;
4741 }
4742 return sk;
4743 }
4744
4745 /* bpf_sk_lookup performs the core lookup for different types of sockets,
4746 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
4747 * Returns the socket as an 'unsigned long' to simplify the casting in the
4748 * callers to satisfy BPF_CALL declarations.
4749 */
4750 static unsigned long
4751 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
4752 u8 proto, u64 netns_id, u64 flags)
4753 {
4754 struct net *caller_net;
4755 struct sock *sk = NULL;
4756 u8 family = AF_UNSPEC;
4757 struct net *net;
4758
4759 family = len == sizeof(tuple->ipv4) ? AF_INET : AF_INET6;
4760 if (unlikely(family == AF_UNSPEC || netns_id > U32_MAX || flags))
4761 goto out;
4762
4763 if (skb->dev)
4764 caller_net = dev_net(skb->dev);
4765 else
4766 caller_net = sock_net(skb->sk);
4767 if (netns_id) {
4768 net = get_net_ns_by_id(caller_net, netns_id);
4769 if (unlikely(!net))
4770 goto out;
4771 sk = sk_lookup(net, tuple, skb, family, proto);
4772 put_net(net);
4773 } else {
4774 net = caller_net;
4775 sk = sk_lookup(net, tuple, skb, family, proto);
4776 }
4777
4778 if (sk)
4779 sk = sk_to_full_sk(sk);
4780 out:
4781 return (unsigned long) sk;
4782 }
4783
4784 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
4785 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
4786 {
4787 return bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, netns_id, flags);
4788 }
4789
4790 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
4791 .func = bpf_sk_lookup_tcp,
4792 .gpl_only = false,
4793 .pkt_access = true,
4794 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
4795 .arg1_type = ARG_PTR_TO_CTX,
4796 .arg2_type = ARG_PTR_TO_MEM,
4797 .arg3_type = ARG_CONST_SIZE,
4798 .arg4_type = ARG_ANYTHING,
4799 .arg5_type = ARG_ANYTHING,
4800 };
4801
4802 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
4803 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
4804 {
4805 return bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, netns_id, flags);
4806 }
4807
4808 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
4809 .func = bpf_sk_lookup_udp,
4810 .gpl_only = false,
4811 .pkt_access = true,
4812 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
4813 .arg1_type = ARG_PTR_TO_CTX,
4814 .arg2_type = ARG_PTR_TO_MEM,
4815 .arg3_type = ARG_CONST_SIZE,
4816 .arg4_type = ARG_ANYTHING,
4817 .arg5_type = ARG_ANYTHING,
4818 };
4819
4820 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
4821 {
4822 if (!sock_flag(sk, SOCK_RCU_FREE))
4823 sock_gen_put(sk);
4824 return 0;
4825 }
4826
4827 static const struct bpf_func_proto bpf_sk_release_proto = {
4828 .func = bpf_sk_release,
4829 .gpl_only = false,
4830 .ret_type = RET_INTEGER,
4831 .arg1_type = ARG_PTR_TO_SOCKET,
4832 };
4833 #endif /* CONFIG_INET */
4834
4835 bool bpf_helper_changes_pkt_data(void *func)
4836 {
4837 if (func == bpf_skb_vlan_push ||
4838 func == bpf_skb_vlan_pop ||
4839 func == bpf_skb_store_bytes ||
4840 func == bpf_skb_change_proto ||
4841 func == bpf_skb_change_head ||
4842 func == sk_skb_change_head ||
4843 func == bpf_skb_change_tail ||
4844 func == sk_skb_change_tail ||
4845 func == bpf_skb_adjust_room ||
4846 func == bpf_skb_pull_data ||
4847 func == sk_skb_pull_data ||
4848 func == bpf_clone_redirect ||
4849 func == bpf_l3_csum_replace ||
4850 func == bpf_l4_csum_replace ||
4851 func == bpf_xdp_adjust_head ||
4852 func == bpf_xdp_adjust_meta ||
4853 func == bpf_msg_pull_data ||
4854 func == bpf_xdp_adjust_tail ||
4855 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4856 func == bpf_lwt_seg6_store_bytes ||
4857 func == bpf_lwt_seg6_adjust_srh ||
4858 func == bpf_lwt_seg6_action ||
4859 #endif
4860 func == bpf_lwt_push_encap)
4861 return true;
4862
4863 return false;
4864 }
4865
4866 static const struct bpf_func_proto *
4867 bpf_base_func_proto(enum bpf_func_id func_id)
4868 {
4869 switch (func_id) {
4870 case BPF_FUNC_map_lookup_elem:
4871 return &bpf_map_lookup_elem_proto;
4872 case BPF_FUNC_map_update_elem:
4873 return &bpf_map_update_elem_proto;
4874 case BPF_FUNC_map_delete_elem:
4875 return &bpf_map_delete_elem_proto;
4876 case BPF_FUNC_get_prandom_u32:
4877 return &bpf_get_prandom_u32_proto;
4878 case BPF_FUNC_get_smp_processor_id:
4879 return &bpf_get_raw_smp_processor_id_proto;
4880 case BPF_FUNC_get_numa_node_id:
4881 return &bpf_get_numa_node_id_proto;
4882 case BPF_FUNC_tail_call:
4883 return &bpf_tail_call_proto;
4884 case BPF_FUNC_ktime_get_ns:
4885 return &bpf_ktime_get_ns_proto;
4886 case BPF_FUNC_trace_printk:
4887 if (capable(CAP_SYS_ADMIN))
4888 return bpf_get_trace_printk_proto();
4889 /* else: fall through */
4890 default:
4891 return NULL;
4892 }
4893 }
4894
4895 static const struct bpf_func_proto *
4896 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4897 {
4898 switch (func_id) {
4899 /* inet and inet6 sockets are created in a process
4900 * context so there is always a valid uid/gid
4901 */
4902 case BPF_FUNC_get_current_uid_gid:
4903 return &bpf_get_current_uid_gid_proto;
4904 case BPF_FUNC_get_local_storage:
4905 return &bpf_get_local_storage_proto;
4906 default:
4907 return bpf_base_func_proto(func_id);
4908 }
4909 }
4910
4911 static const struct bpf_func_proto *
4912 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4913 {
4914 switch (func_id) {
4915 /* inet and inet6 sockets are created in a process
4916 * context so there is always a valid uid/gid
4917 */
4918 case BPF_FUNC_get_current_uid_gid:
4919 return &bpf_get_current_uid_gid_proto;
4920 case BPF_FUNC_bind:
4921 switch (prog->expected_attach_type) {
4922 case BPF_CGROUP_INET4_CONNECT:
4923 case BPF_CGROUP_INET6_CONNECT:
4924 return &bpf_bind_proto;
4925 default:
4926 return NULL;
4927 }
4928 case BPF_FUNC_get_socket_cookie:
4929 return &bpf_get_socket_cookie_sock_addr_proto;
4930 case BPF_FUNC_get_local_storage:
4931 return &bpf_get_local_storage_proto;
4932 default:
4933 return bpf_base_func_proto(func_id);
4934 }
4935 }
4936
4937 static const struct bpf_func_proto *
4938 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4939 {
4940 switch (func_id) {
4941 case BPF_FUNC_skb_load_bytes:
4942 return &bpf_skb_load_bytes_proto;
4943 case BPF_FUNC_skb_load_bytes_relative:
4944 return &bpf_skb_load_bytes_relative_proto;
4945 case BPF_FUNC_get_socket_cookie:
4946 return &bpf_get_socket_cookie_proto;
4947 case BPF_FUNC_get_socket_uid:
4948 return &bpf_get_socket_uid_proto;
4949 default:
4950 return bpf_base_func_proto(func_id);
4951 }
4952 }
4953
4954 static const struct bpf_func_proto *
4955 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4956 {
4957 switch (func_id) {
4958 case BPF_FUNC_get_local_storage:
4959 return &bpf_get_local_storage_proto;
4960 default:
4961 return sk_filter_func_proto(func_id, prog);
4962 }
4963 }
4964
4965 static const struct bpf_func_proto *
4966 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4967 {
4968 switch (func_id) {
4969 case BPF_FUNC_skb_store_bytes:
4970 return &bpf_skb_store_bytes_proto;
4971 case BPF_FUNC_skb_load_bytes:
4972 return &bpf_skb_load_bytes_proto;
4973 case BPF_FUNC_skb_load_bytes_relative:
4974 return &bpf_skb_load_bytes_relative_proto;
4975 case BPF_FUNC_skb_pull_data:
4976 return &bpf_skb_pull_data_proto;
4977 case BPF_FUNC_csum_diff:
4978 return &bpf_csum_diff_proto;
4979 case BPF_FUNC_csum_update:
4980 return &bpf_csum_update_proto;
4981 case BPF_FUNC_l3_csum_replace:
4982 return &bpf_l3_csum_replace_proto;
4983 case BPF_FUNC_l4_csum_replace:
4984 return &bpf_l4_csum_replace_proto;
4985 case BPF_FUNC_clone_redirect:
4986 return &bpf_clone_redirect_proto;
4987 case BPF_FUNC_get_cgroup_classid:
4988 return &bpf_get_cgroup_classid_proto;
4989 case BPF_FUNC_skb_vlan_push:
4990 return &bpf_skb_vlan_push_proto;
4991 case BPF_FUNC_skb_vlan_pop:
4992 return &bpf_skb_vlan_pop_proto;
4993 case BPF_FUNC_skb_change_proto:
4994 return &bpf_skb_change_proto_proto;
4995 case BPF_FUNC_skb_change_type:
4996 return &bpf_skb_change_type_proto;
4997 case BPF_FUNC_skb_adjust_room:
4998 return &bpf_skb_adjust_room_proto;
4999 case BPF_FUNC_skb_change_tail:
5000 return &bpf_skb_change_tail_proto;
5001 case BPF_FUNC_skb_get_tunnel_key:
5002 return &bpf_skb_get_tunnel_key_proto;
5003 case BPF_FUNC_skb_set_tunnel_key:
5004 return bpf_get_skb_set_tunnel_proto(func_id);
5005 case BPF_FUNC_skb_get_tunnel_opt:
5006 return &bpf_skb_get_tunnel_opt_proto;
5007 case BPF_FUNC_skb_set_tunnel_opt:
5008 return bpf_get_skb_set_tunnel_proto(func_id);
5009 case BPF_FUNC_redirect:
5010 return &bpf_redirect_proto;
5011 case BPF_FUNC_get_route_realm:
5012 return &bpf_get_route_realm_proto;
5013 case BPF_FUNC_get_hash_recalc:
5014 return &bpf_get_hash_recalc_proto;
5015 case BPF_FUNC_set_hash_invalid:
5016 return &bpf_set_hash_invalid_proto;
5017 case BPF_FUNC_set_hash:
5018 return &bpf_set_hash_proto;
5019 case BPF_FUNC_perf_event_output:
5020 return &bpf_skb_event_output_proto;
5021 case BPF_FUNC_get_smp_processor_id:
5022 return &bpf_get_smp_processor_id_proto;
5023 case BPF_FUNC_skb_under_cgroup:
5024 return &bpf_skb_under_cgroup_proto;
5025 case BPF_FUNC_get_socket_cookie:
5026 return &bpf_get_socket_cookie_proto;
5027 case BPF_FUNC_get_socket_uid:
5028 return &bpf_get_socket_uid_proto;
5029 case BPF_FUNC_fib_lookup:
5030 return &bpf_skb_fib_lookup_proto;
5031 #ifdef CONFIG_XFRM
5032 case BPF_FUNC_skb_get_xfrm_state:
5033 return &bpf_skb_get_xfrm_state_proto;
5034 #endif
5035 #ifdef CONFIG_SOCK_CGROUP_DATA
5036 case BPF_FUNC_skb_cgroup_id:
5037 return &bpf_skb_cgroup_id_proto;
5038 case BPF_FUNC_skb_ancestor_cgroup_id:
5039 return &bpf_skb_ancestor_cgroup_id_proto;
5040 #endif
5041 #ifdef CONFIG_INET
5042 case BPF_FUNC_sk_lookup_tcp:
5043 return &bpf_sk_lookup_tcp_proto;
5044 case BPF_FUNC_sk_lookup_udp:
5045 return &bpf_sk_lookup_udp_proto;
5046 case BPF_FUNC_sk_release:
5047 return &bpf_sk_release_proto;
5048 #endif
5049 default:
5050 return bpf_base_func_proto(func_id);
5051 }
5052 }
5053
5054 static const struct bpf_func_proto *
5055 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5056 {
5057 switch (func_id) {
5058 case BPF_FUNC_perf_event_output:
5059 return &bpf_xdp_event_output_proto;
5060 case BPF_FUNC_get_smp_processor_id:
5061 return &bpf_get_smp_processor_id_proto;
5062 case BPF_FUNC_csum_diff:
5063 return &bpf_csum_diff_proto;
5064 case BPF_FUNC_xdp_adjust_head:
5065 return &bpf_xdp_adjust_head_proto;
5066 case BPF_FUNC_xdp_adjust_meta:
5067 return &bpf_xdp_adjust_meta_proto;
5068 case BPF_FUNC_redirect:
5069 return &bpf_xdp_redirect_proto;
5070 case BPF_FUNC_redirect_map:
5071 return &bpf_xdp_redirect_map_proto;
5072 case BPF_FUNC_xdp_adjust_tail:
5073 return &bpf_xdp_adjust_tail_proto;
5074 case BPF_FUNC_fib_lookup:
5075 return &bpf_xdp_fib_lookup_proto;
5076 default:
5077 return bpf_base_func_proto(func_id);
5078 }
5079 }
5080
5081 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
5082 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
5083
5084 static const struct bpf_func_proto *
5085 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5086 {
5087 switch (func_id) {
5088 case BPF_FUNC_setsockopt:
5089 return &bpf_setsockopt_proto;
5090 case BPF_FUNC_getsockopt:
5091 return &bpf_getsockopt_proto;
5092 case BPF_FUNC_sock_ops_cb_flags_set:
5093 return &bpf_sock_ops_cb_flags_set_proto;
5094 case BPF_FUNC_sock_map_update:
5095 return &bpf_sock_map_update_proto;
5096 case BPF_FUNC_sock_hash_update:
5097 return &bpf_sock_hash_update_proto;
5098 case BPF_FUNC_get_socket_cookie:
5099 return &bpf_get_socket_cookie_sock_ops_proto;
5100 case BPF_FUNC_get_local_storage:
5101 return &bpf_get_local_storage_proto;
5102 default:
5103 return bpf_base_func_proto(func_id);
5104 }
5105 }
5106
5107 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
5108 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
5109
5110 static const struct bpf_func_proto *
5111 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5112 {
5113 switch (func_id) {
5114 case BPF_FUNC_msg_redirect_map:
5115 return &bpf_msg_redirect_map_proto;
5116 case BPF_FUNC_msg_redirect_hash:
5117 return &bpf_msg_redirect_hash_proto;
5118 case BPF_FUNC_msg_apply_bytes:
5119 return &bpf_msg_apply_bytes_proto;
5120 case BPF_FUNC_msg_cork_bytes:
5121 return &bpf_msg_cork_bytes_proto;
5122 case BPF_FUNC_msg_pull_data:
5123 return &bpf_msg_pull_data_proto;
5124 case BPF_FUNC_get_local_storage:
5125 return &bpf_get_local_storage_proto;
5126 default:
5127 return bpf_base_func_proto(func_id);
5128 }
5129 }
5130
5131 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
5132 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
5133
5134 static const struct bpf_func_proto *
5135 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5136 {
5137 switch (func_id) {
5138 case BPF_FUNC_skb_store_bytes:
5139 return &bpf_skb_store_bytes_proto;
5140 case BPF_FUNC_skb_load_bytes:
5141 return &bpf_skb_load_bytes_proto;
5142 case BPF_FUNC_skb_pull_data:
5143 return &sk_skb_pull_data_proto;
5144 case BPF_FUNC_skb_change_tail:
5145 return &sk_skb_change_tail_proto;
5146 case BPF_FUNC_skb_change_head:
5147 return &sk_skb_change_head_proto;
5148 case BPF_FUNC_get_socket_cookie:
5149 return &bpf_get_socket_cookie_proto;
5150 case BPF_FUNC_get_socket_uid:
5151 return &bpf_get_socket_uid_proto;
5152 case BPF_FUNC_sk_redirect_map:
5153 return &bpf_sk_redirect_map_proto;
5154 case BPF_FUNC_sk_redirect_hash:
5155 return &bpf_sk_redirect_hash_proto;
5156 case BPF_FUNC_get_local_storage:
5157 return &bpf_get_local_storage_proto;
5158 #ifdef CONFIG_INET
5159 case BPF_FUNC_sk_lookup_tcp:
5160 return &bpf_sk_lookup_tcp_proto;
5161 case BPF_FUNC_sk_lookup_udp:
5162 return &bpf_sk_lookup_udp_proto;
5163 case BPF_FUNC_sk_release:
5164 return &bpf_sk_release_proto;
5165 #endif
5166 default:
5167 return bpf_base_func_proto(func_id);
5168 }
5169 }
5170
5171 static const struct bpf_func_proto *
5172 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5173 {
5174 switch (func_id) {
5175 case BPF_FUNC_skb_load_bytes:
5176 return &bpf_skb_load_bytes_proto;
5177 default:
5178 return bpf_base_func_proto(func_id);
5179 }
5180 }
5181
5182 static const struct bpf_func_proto *
5183 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5184 {
5185 switch (func_id) {
5186 case BPF_FUNC_skb_load_bytes:
5187 return &bpf_skb_load_bytes_proto;
5188 case BPF_FUNC_skb_pull_data:
5189 return &bpf_skb_pull_data_proto;
5190 case BPF_FUNC_csum_diff:
5191 return &bpf_csum_diff_proto;
5192 case BPF_FUNC_get_cgroup_classid:
5193 return &bpf_get_cgroup_classid_proto;
5194 case BPF_FUNC_get_route_realm:
5195 return &bpf_get_route_realm_proto;
5196 case BPF_FUNC_get_hash_recalc:
5197 return &bpf_get_hash_recalc_proto;
5198 case BPF_FUNC_perf_event_output:
5199 return &bpf_skb_event_output_proto;
5200 case BPF_FUNC_get_smp_processor_id:
5201 return &bpf_get_smp_processor_id_proto;
5202 case BPF_FUNC_skb_under_cgroup:
5203 return &bpf_skb_under_cgroup_proto;
5204 default:
5205 return bpf_base_func_proto(func_id);
5206 }
5207 }
5208
5209 static const struct bpf_func_proto *
5210 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5211 {
5212 switch (func_id) {
5213 case BPF_FUNC_lwt_push_encap:
5214 return &bpf_lwt_push_encap_proto;
5215 default:
5216 return lwt_out_func_proto(func_id, prog);
5217 }
5218 }
5219
5220 static const struct bpf_func_proto *
5221 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5222 {
5223 switch (func_id) {
5224 case BPF_FUNC_skb_get_tunnel_key:
5225 return &bpf_skb_get_tunnel_key_proto;
5226 case BPF_FUNC_skb_set_tunnel_key:
5227 return bpf_get_skb_set_tunnel_proto(func_id);
5228 case BPF_FUNC_skb_get_tunnel_opt:
5229 return &bpf_skb_get_tunnel_opt_proto;
5230 case BPF_FUNC_skb_set_tunnel_opt:
5231 return bpf_get_skb_set_tunnel_proto(func_id);
5232 case BPF_FUNC_redirect:
5233 return &bpf_redirect_proto;
5234 case BPF_FUNC_clone_redirect:
5235 return &bpf_clone_redirect_proto;
5236 case BPF_FUNC_skb_change_tail:
5237 return &bpf_skb_change_tail_proto;
5238 case BPF_FUNC_skb_change_head:
5239 return &bpf_skb_change_head_proto;
5240 case BPF_FUNC_skb_store_bytes:
5241 return &bpf_skb_store_bytes_proto;
5242 case BPF_FUNC_csum_update:
5243 return &bpf_csum_update_proto;
5244 case BPF_FUNC_l3_csum_replace:
5245 return &bpf_l3_csum_replace_proto;
5246 case BPF_FUNC_l4_csum_replace:
5247 return &bpf_l4_csum_replace_proto;
5248 case BPF_FUNC_set_hash_invalid:
5249 return &bpf_set_hash_invalid_proto;
5250 default:
5251 return lwt_out_func_proto(func_id, prog);
5252 }
5253 }
5254
5255 static const struct bpf_func_proto *
5256 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5257 {
5258 switch (func_id) {
5259 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5260 case BPF_FUNC_lwt_seg6_store_bytes:
5261 return &bpf_lwt_seg6_store_bytes_proto;
5262 case BPF_FUNC_lwt_seg6_action:
5263 return &bpf_lwt_seg6_action_proto;
5264 case BPF_FUNC_lwt_seg6_adjust_srh:
5265 return &bpf_lwt_seg6_adjust_srh_proto;
5266 #endif
5267 default:
5268 return lwt_out_func_proto(func_id, prog);
5269 }
5270 }
5271
5272 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
5273 const struct bpf_prog *prog,
5274 struct bpf_insn_access_aux *info)
5275 {
5276 const int size_default = sizeof(__u32);
5277
5278 if (off < 0 || off >= sizeof(struct __sk_buff))
5279 return false;
5280
5281 /* The verifier guarantees that size > 0. */
5282 if (off % size != 0)
5283 return false;
5284
5285 switch (off) {
5286 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5287 if (off + size > offsetofend(struct __sk_buff, cb[4]))
5288 return false;
5289 break;
5290 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
5291 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
5292 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
5293 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
5294 case bpf_ctx_range(struct __sk_buff, data):
5295 case bpf_ctx_range(struct __sk_buff, data_meta):
5296 case bpf_ctx_range(struct __sk_buff, data_end):
5297 if (size != size_default)
5298 return false;
5299 break;
5300 case bpf_ctx_range(struct __sk_buff, flow_keys):
5301 if (size != sizeof(struct bpf_flow_keys *))
5302 return false;
5303 break;
5304 default:
5305 /* Only narrow read access allowed for now. */
5306 if (type == BPF_WRITE) {
5307 if (size != size_default)
5308 return false;
5309 } else {
5310 bpf_ctx_record_field_size(info, size_default);
5311 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5312 return false;
5313 }
5314 }
5315
5316 return true;
5317 }
5318
5319 static bool sk_filter_is_valid_access(int off, int size,
5320 enum bpf_access_type type,
5321 const struct bpf_prog *prog,
5322 struct bpf_insn_access_aux *info)
5323 {
5324 switch (off) {
5325 case bpf_ctx_range(struct __sk_buff, tc_classid):
5326 case bpf_ctx_range(struct __sk_buff, data):
5327 case bpf_ctx_range(struct __sk_buff, data_meta):
5328 case bpf_ctx_range(struct __sk_buff, data_end):
5329 case bpf_ctx_range(struct __sk_buff, flow_keys):
5330 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5331 return false;
5332 }
5333
5334 if (type == BPF_WRITE) {
5335 switch (off) {
5336 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5337 break;
5338 default:
5339 return false;
5340 }
5341 }
5342
5343 return bpf_skb_is_valid_access(off, size, type, prog, info);
5344 }
5345
5346 static bool lwt_is_valid_access(int off, int size,
5347 enum bpf_access_type type,
5348 const struct bpf_prog *prog,
5349 struct bpf_insn_access_aux *info)
5350 {
5351 switch (off) {
5352 case bpf_ctx_range(struct __sk_buff, tc_classid):
5353 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5354 case bpf_ctx_range(struct __sk_buff, data_meta):
5355 case bpf_ctx_range(struct __sk_buff, flow_keys):
5356 return false;
5357 }
5358
5359 if (type == BPF_WRITE) {
5360 switch (off) {
5361 case bpf_ctx_range(struct __sk_buff, mark):
5362 case bpf_ctx_range(struct __sk_buff, priority):
5363 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5364 break;
5365 default:
5366 return false;
5367 }
5368 }
5369
5370 switch (off) {
5371 case bpf_ctx_range(struct __sk_buff, data):
5372 info->reg_type = PTR_TO_PACKET;
5373 break;
5374 case bpf_ctx_range(struct __sk_buff, data_end):
5375 info->reg_type = PTR_TO_PACKET_END;
5376 break;
5377 }
5378
5379 return bpf_skb_is_valid_access(off, size, type, prog, info);
5380 }
5381
5382 /* Attach type specific accesses */
5383 static bool __sock_filter_check_attach_type(int off,
5384 enum bpf_access_type access_type,
5385 enum bpf_attach_type attach_type)
5386 {
5387 switch (off) {
5388 case offsetof(struct bpf_sock, bound_dev_if):
5389 case offsetof(struct bpf_sock, mark):
5390 case offsetof(struct bpf_sock, priority):
5391 switch (attach_type) {
5392 case BPF_CGROUP_INET_SOCK_CREATE:
5393 goto full_access;
5394 default:
5395 return false;
5396 }
5397 case bpf_ctx_range(struct bpf_sock, src_ip4):
5398 switch (attach_type) {
5399 case BPF_CGROUP_INET4_POST_BIND:
5400 goto read_only;
5401 default:
5402 return false;
5403 }
5404 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5405 switch (attach_type) {
5406 case BPF_CGROUP_INET6_POST_BIND:
5407 goto read_only;
5408 default:
5409 return false;
5410 }
5411 case bpf_ctx_range(struct bpf_sock, src_port):
5412 switch (attach_type) {
5413 case BPF_CGROUP_INET4_POST_BIND:
5414 case BPF_CGROUP_INET6_POST_BIND:
5415 goto read_only;
5416 default:
5417 return false;
5418 }
5419 }
5420 read_only:
5421 return access_type == BPF_READ;
5422 full_access:
5423 return true;
5424 }
5425
5426 static bool __sock_filter_check_size(int off, int size,
5427 struct bpf_insn_access_aux *info)
5428 {
5429 const int size_default = sizeof(__u32);
5430
5431 switch (off) {
5432 case bpf_ctx_range(struct bpf_sock, src_ip4):
5433 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5434 bpf_ctx_record_field_size(info, size_default);
5435 return bpf_ctx_narrow_access_ok(off, size, size_default);
5436 }
5437
5438 return size == size_default;
5439 }
5440
5441 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5442 struct bpf_insn_access_aux *info)
5443 {
5444 if (off < 0 || off >= sizeof(struct bpf_sock))
5445 return false;
5446 if (off % size != 0)
5447 return false;
5448 if (!__sock_filter_check_size(off, size, info))
5449 return false;
5450 return true;
5451 }
5452
5453 static bool sock_filter_is_valid_access(int off, int size,
5454 enum bpf_access_type type,
5455 const struct bpf_prog *prog,
5456 struct bpf_insn_access_aux *info)
5457 {
5458 if (!bpf_sock_is_valid_access(off, size, type, info))
5459 return false;
5460 return __sock_filter_check_attach_type(off, type,
5461 prog->expected_attach_type);
5462 }
5463
5464 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5465 const struct bpf_prog *prog, int drop_verdict)
5466 {
5467 struct bpf_insn *insn = insn_buf;
5468
5469 if (!direct_write)
5470 return 0;
5471
5472 /* if (!skb->cloned)
5473 * goto start;
5474 *
5475 * (Fast-path, otherwise approximation that we might be
5476 * a clone, do the rest in helper.)
5477 */
5478 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5479 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5480 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5481
5482 /* ret = bpf_skb_pull_data(skb, 0); */
5483 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5484 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5485 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5486 BPF_FUNC_skb_pull_data);
5487 /* if (!ret)
5488 * goto restore;
5489 * return TC_ACT_SHOT;
5490 */
5491 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
5492 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
5493 *insn++ = BPF_EXIT_INSN();
5494
5495 /* restore: */
5496 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
5497 /* start: */
5498 *insn++ = prog->insnsi[0];
5499
5500 return insn - insn_buf;
5501 }
5502
5503 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
5504 struct bpf_insn *insn_buf)
5505 {
5506 bool indirect = BPF_MODE(orig->code) == BPF_IND;
5507 struct bpf_insn *insn = insn_buf;
5508
5509 /* We're guaranteed here that CTX is in R6. */
5510 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
5511 if (!indirect) {
5512 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
5513 } else {
5514 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
5515 if (orig->imm)
5516 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
5517 }
5518
5519 switch (BPF_SIZE(orig->code)) {
5520 case BPF_B:
5521 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
5522 break;
5523 case BPF_H:
5524 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
5525 break;
5526 case BPF_W:
5527 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
5528 break;
5529 }
5530
5531 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
5532 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
5533 *insn++ = BPF_EXIT_INSN();
5534
5535 return insn - insn_buf;
5536 }
5537
5538 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
5539 const struct bpf_prog *prog)
5540 {
5541 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
5542 }
5543
5544 static bool tc_cls_act_is_valid_access(int off, int size,
5545 enum bpf_access_type type,
5546 const struct bpf_prog *prog,
5547 struct bpf_insn_access_aux *info)
5548 {
5549 if (type == BPF_WRITE) {
5550 switch (off) {
5551 case bpf_ctx_range(struct __sk_buff, mark):
5552 case bpf_ctx_range(struct __sk_buff, tc_index):
5553 case bpf_ctx_range(struct __sk_buff, priority):
5554 case bpf_ctx_range(struct __sk_buff, tc_classid):
5555 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5556 break;
5557 default:
5558 return false;
5559 }
5560 }
5561
5562 switch (off) {
5563 case bpf_ctx_range(struct __sk_buff, data):
5564 info->reg_type = PTR_TO_PACKET;
5565 break;
5566 case bpf_ctx_range(struct __sk_buff, data_meta):
5567 info->reg_type = PTR_TO_PACKET_META;
5568 break;
5569 case bpf_ctx_range(struct __sk_buff, data_end):
5570 info->reg_type = PTR_TO_PACKET_END;
5571 break;
5572 case bpf_ctx_range(struct __sk_buff, flow_keys):
5573 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5574 return false;
5575 }
5576
5577 return bpf_skb_is_valid_access(off, size, type, prog, info);
5578 }
5579
5580 static bool __is_valid_xdp_access(int off, int size)
5581 {
5582 if (off < 0 || off >= sizeof(struct xdp_md))
5583 return false;
5584 if (off % size != 0)
5585 return false;
5586 if (size != sizeof(__u32))
5587 return false;
5588
5589 return true;
5590 }
5591
5592 static bool xdp_is_valid_access(int off, int size,
5593 enum bpf_access_type type,
5594 const struct bpf_prog *prog,
5595 struct bpf_insn_access_aux *info)
5596 {
5597 if (type == BPF_WRITE) {
5598 if (bpf_prog_is_dev_bound(prog->aux)) {
5599 switch (off) {
5600 case offsetof(struct xdp_md, rx_queue_index):
5601 return __is_valid_xdp_access(off, size);
5602 }
5603 }
5604 return false;
5605 }
5606
5607 switch (off) {
5608 case offsetof(struct xdp_md, data):
5609 info->reg_type = PTR_TO_PACKET;
5610 break;
5611 case offsetof(struct xdp_md, data_meta):
5612 info->reg_type = PTR_TO_PACKET_META;
5613 break;
5614 case offsetof(struct xdp_md, data_end):
5615 info->reg_type = PTR_TO_PACKET_END;
5616 break;
5617 }
5618
5619 return __is_valid_xdp_access(off, size);
5620 }
5621
5622 void bpf_warn_invalid_xdp_action(u32 act)
5623 {
5624 const u32 act_max = XDP_REDIRECT;
5625
5626 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
5627 act > act_max ? "Illegal" : "Driver unsupported",
5628 act);
5629 }
5630 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
5631
5632 static bool sock_addr_is_valid_access(int off, int size,
5633 enum bpf_access_type type,
5634 const struct bpf_prog *prog,
5635 struct bpf_insn_access_aux *info)
5636 {
5637 const int size_default = sizeof(__u32);
5638
5639 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
5640 return false;
5641 if (off % size != 0)
5642 return false;
5643
5644 /* Disallow access to IPv6 fields from IPv4 contex and vise
5645 * versa.
5646 */
5647 switch (off) {
5648 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5649 switch (prog->expected_attach_type) {
5650 case BPF_CGROUP_INET4_BIND:
5651 case BPF_CGROUP_INET4_CONNECT:
5652 case BPF_CGROUP_UDP4_SENDMSG:
5653 break;
5654 default:
5655 return false;
5656 }
5657 break;
5658 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5659 switch (prog->expected_attach_type) {
5660 case BPF_CGROUP_INET6_BIND:
5661 case BPF_CGROUP_INET6_CONNECT:
5662 case BPF_CGROUP_UDP6_SENDMSG:
5663 break;
5664 default:
5665 return false;
5666 }
5667 break;
5668 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5669 switch (prog->expected_attach_type) {
5670 case BPF_CGROUP_UDP4_SENDMSG:
5671 break;
5672 default:
5673 return false;
5674 }
5675 break;
5676 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5677 msg_src_ip6[3]):
5678 switch (prog->expected_attach_type) {
5679 case BPF_CGROUP_UDP6_SENDMSG:
5680 break;
5681 default:
5682 return false;
5683 }
5684 break;
5685 }
5686
5687 switch (off) {
5688 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5689 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5690 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5691 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5692 msg_src_ip6[3]):
5693 /* Only narrow read access allowed for now. */
5694 if (type == BPF_READ) {
5695 bpf_ctx_record_field_size(info, size_default);
5696 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5697 return false;
5698 } else {
5699 if (size != size_default)
5700 return false;
5701 }
5702 break;
5703 case bpf_ctx_range(struct bpf_sock_addr, user_port):
5704 if (size != size_default)
5705 return false;
5706 break;
5707 default:
5708 if (type == BPF_READ) {
5709 if (size != size_default)
5710 return false;
5711 } else {
5712 return false;
5713 }
5714 }
5715
5716 return true;
5717 }
5718
5719 static bool sock_ops_is_valid_access(int off, int size,
5720 enum bpf_access_type type,
5721 const struct bpf_prog *prog,
5722 struct bpf_insn_access_aux *info)
5723 {
5724 const int size_default = sizeof(__u32);
5725
5726 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5727 return false;
5728
5729 /* The verifier guarantees that size > 0. */
5730 if (off % size != 0)
5731 return false;
5732
5733 if (type == BPF_WRITE) {
5734 switch (off) {
5735 case offsetof(struct bpf_sock_ops, reply):
5736 case offsetof(struct bpf_sock_ops, sk_txhash):
5737 if (size != size_default)
5738 return false;
5739 break;
5740 default:
5741 return false;
5742 }
5743 } else {
5744 switch (off) {
5745 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5746 bytes_acked):
5747 if (size != sizeof(__u64))
5748 return false;
5749 break;
5750 default:
5751 if (size != size_default)
5752 return false;
5753 break;
5754 }
5755 }
5756
5757 return true;
5758 }
5759
5760 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5761 const struct bpf_prog *prog)
5762 {
5763 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
5764 }
5765
5766 static bool sk_skb_is_valid_access(int off, int size,
5767 enum bpf_access_type type,
5768 const struct bpf_prog *prog,
5769 struct bpf_insn_access_aux *info)
5770 {
5771 switch (off) {
5772 case bpf_ctx_range(struct __sk_buff, tc_classid):
5773 case bpf_ctx_range(struct __sk_buff, data_meta):
5774 case bpf_ctx_range(struct __sk_buff, flow_keys):
5775 return false;
5776 }
5777
5778 if (type == BPF_WRITE) {
5779 switch (off) {
5780 case bpf_ctx_range(struct __sk_buff, tc_index):
5781 case bpf_ctx_range(struct __sk_buff, priority):
5782 break;
5783 default:
5784 return false;
5785 }
5786 }
5787
5788 switch (off) {
5789 case bpf_ctx_range(struct __sk_buff, mark):
5790 return false;
5791 case bpf_ctx_range(struct __sk_buff, data):
5792 info->reg_type = PTR_TO_PACKET;
5793 break;
5794 case bpf_ctx_range(struct __sk_buff, data_end):
5795 info->reg_type = PTR_TO_PACKET_END;
5796 break;
5797 }
5798
5799 return bpf_skb_is_valid_access(off, size, type, prog, info);
5800 }
5801
5802 static bool sk_msg_is_valid_access(int off, int size,
5803 enum bpf_access_type type,
5804 const struct bpf_prog *prog,
5805 struct bpf_insn_access_aux *info)
5806 {
5807 if (type == BPF_WRITE)
5808 return false;
5809
5810 switch (off) {
5811 case offsetof(struct sk_msg_md, data):
5812 info->reg_type = PTR_TO_PACKET;
5813 if (size != sizeof(__u64))
5814 return false;
5815 break;
5816 case offsetof(struct sk_msg_md, data_end):
5817 info->reg_type = PTR_TO_PACKET_END;
5818 if (size != sizeof(__u64))
5819 return false;
5820 break;
5821 default:
5822 if (size != sizeof(__u32))
5823 return false;
5824 }
5825
5826 if (off < 0 || off >= sizeof(struct sk_msg_md))
5827 return false;
5828 if (off % size != 0)
5829 return false;
5830
5831 return true;
5832 }
5833
5834 static bool flow_dissector_is_valid_access(int off, int size,
5835 enum bpf_access_type type,
5836 const struct bpf_prog *prog,
5837 struct bpf_insn_access_aux *info)
5838 {
5839 if (type == BPF_WRITE) {
5840 switch (off) {
5841 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5842 break;
5843 default:
5844 return false;
5845 }
5846 }
5847
5848 switch (off) {
5849 case bpf_ctx_range(struct __sk_buff, data):
5850 info->reg_type = PTR_TO_PACKET;
5851 break;
5852 case bpf_ctx_range(struct __sk_buff, data_end):
5853 info->reg_type = PTR_TO_PACKET_END;
5854 break;
5855 case bpf_ctx_range(struct __sk_buff, flow_keys):
5856 info->reg_type = PTR_TO_FLOW_KEYS;
5857 break;
5858 case bpf_ctx_range(struct __sk_buff, tc_classid):
5859 case bpf_ctx_range(struct __sk_buff, data_meta):
5860 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5861 return false;
5862 }
5863
5864 return bpf_skb_is_valid_access(off, size, type, prog, info);
5865 }
5866
5867 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
5868 const struct bpf_insn *si,
5869 struct bpf_insn *insn_buf,
5870 struct bpf_prog *prog, u32 *target_size)
5871 {
5872 struct bpf_insn *insn = insn_buf;
5873 int off;
5874
5875 switch (si->off) {
5876 case offsetof(struct __sk_buff, len):
5877 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5878 bpf_target_off(struct sk_buff, len, 4,
5879 target_size));
5880 break;
5881
5882 case offsetof(struct __sk_buff, protocol):
5883 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5884 bpf_target_off(struct sk_buff, protocol, 2,
5885 target_size));
5886 break;
5887
5888 case offsetof(struct __sk_buff, vlan_proto):
5889 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5890 bpf_target_off(struct sk_buff, vlan_proto, 2,
5891 target_size));
5892 break;
5893
5894 case offsetof(struct __sk_buff, priority):
5895 if (type == BPF_WRITE)
5896 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5897 bpf_target_off(struct sk_buff, priority, 4,
5898 target_size));
5899 else
5900 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5901 bpf_target_off(struct sk_buff, priority, 4,
5902 target_size));
5903 break;
5904
5905 case offsetof(struct __sk_buff, ingress_ifindex):
5906 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5907 bpf_target_off(struct sk_buff, skb_iif, 4,
5908 target_size));
5909 break;
5910
5911 case offsetof(struct __sk_buff, ifindex):
5912 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
5913 si->dst_reg, si->src_reg,
5914 offsetof(struct sk_buff, dev));
5915 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
5916 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5917 bpf_target_off(struct net_device, ifindex, 4,
5918 target_size));
5919 break;
5920
5921 case offsetof(struct __sk_buff, hash):
5922 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5923 bpf_target_off(struct sk_buff, hash, 4,
5924 target_size));
5925 break;
5926
5927 case offsetof(struct __sk_buff, mark):
5928 if (type == BPF_WRITE)
5929 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5930 bpf_target_off(struct sk_buff, mark, 4,
5931 target_size));
5932 else
5933 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5934 bpf_target_off(struct sk_buff, mark, 4,
5935 target_size));
5936 break;
5937
5938 case offsetof(struct __sk_buff, pkt_type):
5939 *target_size = 1;
5940 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
5941 PKT_TYPE_OFFSET());
5942 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
5943 #ifdef __BIG_ENDIAN_BITFIELD
5944 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
5945 #endif
5946 break;
5947
5948 case offsetof(struct __sk_buff, queue_mapping):
5949 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5950 bpf_target_off(struct sk_buff, queue_mapping, 2,
5951 target_size));
5952 break;
5953
5954 case offsetof(struct __sk_buff, vlan_present):
5955 case offsetof(struct __sk_buff, vlan_tci):
5956 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
5957
5958 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5959 bpf_target_off(struct sk_buff, vlan_tci, 2,
5960 target_size));
5961 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
5962 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
5963 ~VLAN_TAG_PRESENT);
5964 } else {
5965 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
5966 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
5967 }
5968 break;
5969
5970 case offsetof(struct __sk_buff, cb[0]) ...
5971 offsetofend(struct __sk_buff, cb[4]) - 1:
5972 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
5973 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
5974 offsetof(struct qdisc_skb_cb, data)) %
5975 sizeof(__u64));
5976
5977 prog->cb_access = 1;
5978 off = si->off;
5979 off -= offsetof(struct __sk_buff, cb[0]);
5980 off += offsetof(struct sk_buff, cb);
5981 off += offsetof(struct qdisc_skb_cb, data);
5982 if (type == BPF_WRITE)
5983 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
5984 si->src_reg, off);
5985 else
5986 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
5987 si->src_reg, off);
5988 break;
5989
5990 case offsetof(struct __sk_buff, tc_classid):
5991 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
5992
5993 off = si->off;
5994 off -= offsetof(struct __sk_buff, tc_classid);
5995 off += offsetof(struct sk_buff, cb);
5996 off += offsetof(struct qdisc_skb_cb, tc_classid);
5997 *target_size = 2;
5998 if (type == BPF_WRITE)
5999 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
6000 si->src_reg, off);
6001 else
6002 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
6003 si->src_reg, off);
6004 break;
6005
6006 case offsetof(struct __sk_buff, data):
6007 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
6008 si->dst_reg, si->src_reg,
6009 offsetof(struct sk_buff, data));
6010 break;
6011
6012 case offsetof(struct __sk_buff, data_meta):
6013 off = si->off;
6014 off -= offsetof(struct __sk_buff, data_meta);
6015 off += offsetof(struct sk_buff, cb);
6016 off += offsetof(struct bpf_skb_data_end, data_meta);
6017 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6018 si->src_reg, off);
6019 break;
6020
6021 case offsetof(struct __sk_buff, data_end):
6022 off = si->off;
6023 off -= offsetof(struct __sk_buff, data_end);
6024 off += offsetof(struct sk_buff, cb);
6025 off += offsetof(struct bpf_skb_data_end, data_end);
6026 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6027 si->src_reg, off);
6028 break;
6029
6030 case offsetof(struct __sk_buff, tc_index):
6031 #ifdef CONFIG_NET_SCHED
6032 if (type == BPF_WRITE)
6033 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
6034 bpf_target_off(struct sk_buff, tc_index, 2,
6035 target_size));
6036 else
6037 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6038 bpf_target_off(struct sk_buff, tc_index, 2,
6039 target_size));
6040 #else
6041 *target_size = 2;
6042 if (type == BPF_WRITE)
6043 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
6044 else
6045 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6046 #endif
6047 break;
6048
6049 case offsetof(struct __sk_buff, napi_id):
6050 #if defined(CONFIG_NET_RX_BUSY_POLL)
6051 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6052 bpf_target_off(struct sk_buff, napi_id, 4,
6053 target_size));
6054 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
6055 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6056 #else
6057 *target_size = 4;
6058 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6059 #endif
6060 break;
6061 case offsetof(struct __sk_buff, family):
6062 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6063
6064 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6065 si->dst_reg, si->src_reg,
6066 offsetof(struct sk_buff, sk));
6067 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6068 bpf_target_off(struct sock_common,
6069 skc_family,
6070 2, target_size));
6071 break;
6072 case offsetof(struct __sk_buff, remote_ip4):
6073 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6074
6075 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6076 si->dst_reg, si->src_reg,
6077 offsetof(struct sk_buff, sk));
6078 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6079 bpf_target_off(struct sock_common,
6080 skc_daddr,
6081 4, target_size));
6082 break;
6083 case offsetof(struct __sk_buff, local_ip4):
6084 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6085 skc_rcv_saddr) != 4);
6086
6087 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6088 si->dst_reg, si->src_reg,
6089 offsetof(struct sk_buff, sk));
6090 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6091 bpf_target_off(struct sock_common,
6092 skc_rcv_saddr,
6093 4, target_size));
6094 break;
6095 case offsetof(struct __sk_buff, remote_ip6[0]) ...
6096 offsetof(struct __sk_buff, remote_ip6[3]):
6097 #if IS_ENABLED(CONFIG_IPV6)
6098 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6099 skc_v6_daddr.s6_addr32[0]) != 4);
6100
6101 off = si->off;
6102 off -= offsetof(struct __sk_buff, remote_ip6[0]);
6103
6104 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6105 si->dst_reg, si->src_reg,
6106 offsetof(struct sk_buff, sk));
6107 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6108 offsetof(struct sock_common,
6109 skc_v6_daddr.s6_addr32[0]) +
6110 off);
6111 #else
6112 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6113 #endif
6114 break;
6115 case offsetof(struct __sk_buff, local_ip6[0]) ...
6116 offsetof(struct __sk_buff, local_ip6[3]):
6117 #if IS_ENABLED(CONFIG_IPV6)
6118 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6119 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6120
6121 off = si->off;
6122 off -= offsetof(struct __sk_buff, local_ip6[0]);
6123
6124 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6125 si->dst_reg, si->src_reg,
6126 offsetof(struct sk_buff, sk));
6127 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6128 offsetof(struct sock_common,
6129 skc_v6_rcv_saddr.s6_addr32[0]) +
6130 off);
6131 #else
6132 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6133 #endif
6134 break;
6135
6136 case offsetof(struct __sk_buff, remote_port):
6137 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6138
6139 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6140 si->dst_reg, si->src_reg,
6141 offsetof(struct sk_buff, sk));
6142 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6143 bpf_target_off(struct sock_common,
6144 skc_dport,
6145 2, target_size));
6146 #ifndef __BIG_ENDIAN_BITFIELD
6147 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6148 #endif
6149 break;
6150
6151 case offsetof(struct __sk_buff, local_port):
6152 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6153
6154 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6155 si->dst_reg, si->src_reg,
6156 offsetof(struct sk_buff, sk));
6157 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6158 bpf_target_off(struct sock_common,
6159 skc_num, 2, target_size));
6160 break;
6161
6162 case offsetof(struct __sk_buff, flow_keys):
6163 off = si->off;
6164 off -= offsetof(struct __sk_buff, flow_keys);
6165 off += offsetof(struct sk_buff, cb);
6166 off += offsetof(struct qdisc_skb_cb, flow_keys);
6167 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6168 si->src_reg, off);
6169 break;
6170 }
6171
6172 return insn - insn_buf;
6173 }
6174
6175 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
6176 const struct bpf_insn *si,
6177 struct bpf_insn *insn_buf,
6178 struct bpf_prog *prog, u32 *target_size)
6179 {
6180 struct bpf_insn *insn = insn_buf;
6181 int off;
6182
6183 switch (si->off) {
6184 case offsetof(struct bpf_sock, bound_dev_if):
6185 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
6186
6187 if (type == BPF_WRITE)
6188 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6189 offsetof(struct sock, sk_bound_dev_if));
6190 else
6191 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6192 offsetof(struct sock, sk_bound_dev_if));
6193 break;
6194
6195 case offsetof(struct bpf_sock, mark):
6196 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
6197
6198 if (type == BPF_WRITE)
6199 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6200 offsetof(struct sock, sk_mark));
6201 else
6202 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6203 offsetof(struct sock, sk_mark));
6204 break;
6205
6206 case offsetof(struct bpf_sock, priority):
6207 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
6208
6209 if (type == BPF_WRITE)
6210 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6211 offsetof(struct sock, sk_priority));
6212 else
6213 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6214 offsetof(struct sock, sk_priority));
6215 break;
6216
6217 case offsetof(struct bpf_sock, family):
6218 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
6219
6220 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6221 offsetof(struct sock, sk_family));
6222 break;
6223
6224 case offsetof(struct bpf_sock, type):
6225 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6226 offsetof(struct sock, __sk_flags_offset));
6227 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6228 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6229 break;
6230
6231 case offsetof(struct bpf_sock, protocol):
6232 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6233 offsetof(struct sock, __sk_flags_offset));
6234 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6235 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
6236 break;
6237
6238 case offsetof(struct bpf_sock, src_ip4):
6239 *insn++ = BPF_LDX_MEM(
6240 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6241 bpf_target_off(struct sock_common, skc_rcv_saddr,
6242 FIELD_SIZEOF(struct sock_common,
6243 skc_rcv_saddr),
6244 target_size));
6245 break;
6246
6247 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6248 #if IS_ENABLED(CONFIG_IPV6)
6249 off = si->off;
6250 off -= offsetof(struct bpf_sock, src_ip6[0]);
6251 *insn++ = BPF_LDX_MEM(
6252 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6253 bpf_target_off(
6254 struct sock_common,
6255 skc_v6_rcv_saddr.s6_addr32[0],
6256 FIELD_SIZEOF(struct sock_common,
6257 skc_v6_rcv_saddr.s6_addr32[0]),
6258 target_size) + off);
6259 #else
6260 (void)off;
6261 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6262 #endif
6263 break;
6264
6265 case offsetof(struct bpf_sock, src_port):
6266 *insn++ = BPF_LDX_MEM(
6267 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
6268 si->dst_reg, si->src_reg,
6269 bpf_target_off(struct sock_common, skc_num,
6270 FIELD_SIZEOF(struct sock_common,
6271 skc_num),
6272 target_size));
6273 break;
6274 }
6275
6276 return insn - insn_buf;
6277 }
6278
6279 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
6280 const struct bpf_insn *si,
6281 struct bpf_insn *insn_buf,
6282 struct bpf_prog *prog, u32 *target_size)
6283 {
6284 struct bpf_insn *insn = insn_buf;
6285
6286 switch (si->off) {
6287 case offsetof(struct __sk_buff, ifindex):
6288 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6289 si->dst_reg, si->src_reg,
6290 offsetof(struct sk_buff, dev));
6291 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6292 bpf_target_off(struct net_device, ifindex, 4,
6293 target_size));
6294 break;
6295 default:
6296 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6297 target_size);
6298 }
6299
6300 return insn - insn_buf;
6301 }
6302
6303 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
6304 const struct bpf_insn *si,
6305 struct bpf_insn *insn_buf,
6306 struct bpf_prog *prog, u32 *target_size)
6307 {
6308 struct bpf_insn *insn = insn_buf;
6309
6310 switch (si->off) {
6311 case offsetof(struct xdp_md, data):
6312 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6313 si->dst_reg, si->src_reg,
6314 offsetof(struct xdp_buff, data));
6315 break;
6316 case offsetof(struct xdp_md, data_meta):
6317 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
6318 si->dst_reg, si->src_reg,
6319 offsetof(struct xdp_buff, data_meta));
6320 break;
6321 case offsetof(struct xdp_md, data_end):
6322 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6323 si->dst_reg, si->src_reg,
6324 offsetof(struct xdp_buff, data_end));
6325 break;
6326 case offsetof(struct xdp_md, ingress_ifindex):
6327 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6328 si->dst_reg, si->src_reg,
6329 offsetof(struct xdp_buff, rxq));
6330 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
6331 si->dst_reg, si->dst_reg,
6332 offsetof(struct xdp_rxq_info, dev));
6333 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6334 offsetof(struct net_device, ifindex));
6335 break;
6336 case offsetof(struct xdp_md, rx_queue_index):
6337 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6338 si->dst_reg, si->src_reg,
6339 offsetof(struct xdp_buff, rxq));
6340 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6341 offsetof(struct xdp_rxq_info,
6342 queue_index));
6343 break;
6344 }
6345
6346 return insn - insn_buf;
6347 }
6348
6349 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
6350 * context Structure, F is Field in context structure that contains a pointer
6351 * to Nested Structure of type NS that has the field NF.
6352 *
6353 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
6354 * sure that SIZE is not greater than actual size of S.F.NF.
6355 *
6356 * If offset OFF is provided, the load happens from that offset relative to
6357 * offset of NF.
6358 */
6359 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
6360 do { \
6361 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
6362 si->src_reg, offsetof(S, F)); \
6363 *insn++ = BPF_LDX_MEM( \
6364 SIZE, si->dst_reg, si->dst_reg, \
6365 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6366 target_size) \
6367 + OFF); \
6368 } while (0)
6369
6370 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
6371 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
6372 BPF_FIELD_SIZEOF(NS, NF), 0)
6373
6374 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
6375 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
6376 *
6377 * It doesn't support SIZE argument though since narrow stores are not
6378 * supported for now.
6379 *
6380 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
6381 * "register" since two registers available in convert_ctx_access are not
6382 * enough: we can't override neither SRC, since it contains value to store, nor
6383 * DST since it contains pointer to context that may be used by later
6384 * instructions. But we need a temporary place to save pointer to nested
6385 * structure whose field we want to store to.
6386 */
6387 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
6388 do { \
6389 int tmp_reg = BPF_REG_9; \
6390 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6391 --tmp_reg; \
6392 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6393 --tmp_reg; \
6394 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
6395 offsetof(S, TF)); \
6396 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
6397 si->dst_reg, offsetof(S, F)); \
6398 *insn++ = BPF_STX_MEM( \
6399 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
6400 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6401 target_size) \
6402 + OFF); \
6403 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
6404 offsetof(S, TF)); \
6405 } while (0)
6406
6407 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
6408 TF) \
6409 do { \
6410 if (type == BPF_WRITE) { \
6411 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6412 TF); \
6413 } else { \
6414 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6415 S, NS, F, NF, SIZE, OFF); \
6416 } \
6417 } while (0)
6418
6419 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6420 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6421 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6422
6423 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6424 const struct bpf_insn *si,
6425 struct bpf_insn *insn_buf,
6426 struct bpf_prog *prog, u32 *target_size)
6427 {
6428 struct bpf_insn *insn = insn_buf;
6429 int off;
6430
6431 switch (si->off) {
6432 case offsetof(struct bpf_sock_addr, user_family):
6433 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6434 struct sockaddr, uaddr, sa_family);
6435 break;
6436
6437 case offsetof(struct bpf_sock_addr, user_ip4):
6438 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6439 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6440 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6441 break;
6442
6443 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6444 off = si->off;
6445 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6446 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6447 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6448 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6449 tmp_reg);
6450 break;
6451
6452 case offsetof(struct bpf_sock_addr, user_port):
6453 /* To get port we need to know sa_family first and then treat
6454 * sockaddr as either sockaddr_in or sockaddr_in6.
6455 * Though we can simplify since port field has same offset and
6456 * size in both structures.
6457 * Here we check this invariant and use just one of the
6458 * structures if it's true.
6459 */
6460 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
6461 offsetof(struct sockaddr_in6, sin6_port));
6462 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
6463 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
6464 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
6465 struct sockaddr_in6, uaddr,
6466 sin6_port, tmp_reg);
6467 break;
6468
6469 case offsetof(struct bpf_sock_addr, family):
6470 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6471 struct sock, sk, sk_family);
6472 break;
6473
6474 case offsetof(struct bpf_sock_addr, type):
6475 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6476 struct bpf_sock_addr_kern, struct sock, sk,
6477 __sk_flags_offset, BPF_W, 0);
6478 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6479 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6480 break;
6481
6482 case offsetof(struct bpf_sock_addr, protocol):
6483 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6484 struct bpf_sock_addr_kern, struct sock, sk,
6485 __sk_flags_offset, BPF_W, 0);
6486 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6487 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
6488 SK_FL_PROTO_SHIFT);
6489 break;
6490
6491 case offsetof(struct bpf_sock_addr, msg_src_ip4):
6492 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
6493 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6494 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
6495 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
6496 break;
6497
6498 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6499 msg_src_ip6[3]):
6500 off = si->off;
6501 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
6502 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
6503 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6504 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
6505 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
6506 break;
6507 }
6508
6509 return insn - insn_buf;
6510 }
6511
6512 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
6513 const struct bpf_insn *si,
6514 struct bpf_insn *insn_buf,
6515 struct bpf_prog *prog,
6516 u32 *target_size)
6517 {
6518 struct bpf_insn *insn = insn_buf;
6519 int off;
6520
6521 switch (si->off) {
6522 case offsetof(struct bpf_sock_ops, op) ...
6523 offsetof(struct bpf_sock_ops, replylong[3]):
6524 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
6525 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
6526 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
6527 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
6528 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
6529 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
6530 off = si->off;
6531 off -= offsetof(struct bpf_sock_ops, op);
6532 off += offsetof(struct bpf_sock_ops_kern, op);
6533 if (type == BPF_WRITE)
6534 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6535 off);
6536 else
6537 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6538 off);
6539 break;
6540
6541 case offsetof(struct bpf_sock_ops, family):
6542 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6543
6544 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6545 struct bpf_sock_ops_kern, sk),
6546 si->dst_reg, si->src_reg,
6547 offsetof(struct bpf_sock_ops_kern, sk));
6548 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6549 offsetof(struct sock_common, skc_family));
6550 break;
6551
6552 case offsetof(struct bpf_sock_ops, remote_ip4):
6553 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6554
6555 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6556 struct bpf_sock_ops_kern, sk),
6557 si->dst_reg, si->src_reg,
6558 offsetof(struct bpf_sock_ops_kern, sk));
6559 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6560 offsetof(struct sock_common, skc_daddr));
6561 break;
6562
6563 case offsetof(struct bpf_sock_ops, local_ip4):
6564 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6565 skc_rcv_saddr) != 4);
6566
6567 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6568 struct bpf_sock_ops_kern, sk),
6569 si->dst_reg, si->src_reg,
6570 offsetof(struct bpf_sock_ops_kern, sk));
6571 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6572 offsetof(struct sock_common,
6573 skc_rcv_saddr));
6574 break;
6575
6576 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
6577 offsetof(struct bpf_sock_ops, remote_ip6[3]):
6578 #if IS_ENABLED(CONFIG_IPV6)
6579 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6580 skc_v6_daddr.s6_addr32[0]) != 4);
6581
6582 off = si->off;
6583 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
6584 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6585 struct bpf_sock_ops_kern, sk),
6586 si->dst_reg, si->src_reg,
6587 offsetof(struct bpf_sock_ops_kern, sk));
6588 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6589 offsetof(struct sock_common,
6590 skc_v6_daddr.s6_addr32[0]) +
6591 off);
6592 #else
6593 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6594 #endif
6595 break;
6596
6597 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
6598 offsetof(struct bpf_sock_ops, local_ip6[3]):
6599 #if IS_ENABLED(CONFIG_IPV6)
6600 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6601 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6602
6603 off = si->off;
6604 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
6605 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6606 struct bpf_sock_ops_kern, sk),
6607 si->dst_reg, si->src_reg,
6608 offsetof(struct bpf_sock_ops_kern, sk));
6609 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6610 offsetof(struct sock_common,
6611 skc_v6_rcv_saddr.s6_addr32[0]) +
6612 off);
6613 #else
6614 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6615 #endif
6616 break;
6617
6618 case offsetof(struct bpf_sock_ops, remote_port):
6619 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6620
6621 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6622 struct bpf_sock_ops_kern, sk),
6623 si->dst_reg, si->src_reg,
6624 offsetof(struct bpf_sock_ops_kern, sk));
6625 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6626 offsetof(struct sock_common, skc_dport));
6627 #ifndef __BIG_ENDIAN_BITFIELD
6628 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6629 #endif
6630 break;
6631
6632 case offsetof(struct bpf_sock_ops, local_port):
6633 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6634
6635 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6636 struct bpf_sock_ops_kern, sk),
6637 si->dst_reg, si->src_reg,
6638 offsetof(struct bpf_sock_ops_kern, sk));
6639 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6640 offsetof(struct sock_common, skc_num));
6641 break;
6642
6643 case offsetof(struct bpf_sock_ops, is_fullsock):
6644 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6645 struct bpf_sock_ops_kern,
6646 is_fullsock),
6647 si->dst_reg, si->src_reg,
6648 offsetof(struct bpf_sock_ops_kern,
6649 is_fullsock));
6650 break;
6651
6652 case offsetof(struct bpf_sock_ops, state):
6653 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
6654
6655 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6656 struct bpf_sock_ops_kern, sk),
6657 si->dst_reg, si->src_reg,
6658 offsetof(struct bpf_sock_ops_kern, sk));
6659 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
6660 offsetof(struct sock_common, skc_state));
6661 break;
6662
6663 case offsetof(struct bpf_sock_ops, rtt_min):
6664 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
6665 sizeof(struct minmax));
6666 BUILD_BUG_ON(sizeof(struct minmax) <
6667 sizeof(struct minmax_sample));
6668
6669 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6670 struct bpf_sock_ops_kern, sk),
6671 si->dst_reg, si->src_reg,
6672 offsetof(struct bpf_sock_ops_kern, sk));
6673 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6674 offsetof(struct tcp_sock, rtt_min) +
6675 FIELD_SIZEOF(struct minmax_sample, t));
6676 break;
6677
6678 /* Helper macro for adding read access to tcp_sock or sock fields. */
6679 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6680 do { \
6681 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6682 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6683 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6684 struct bpf_sock_ops_kern, \
6685 is_fullsock), \
6686 si->dst_reg, si->src_reg, \
6687 offsetof(struct bpf_sock_ops_kern, \
6688 is_fullsock)); \
6689 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
6690 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6691 struct bpf_sock_ops_kern, sk),\
6692 si->dst_reg, si->src_reg, \
6693 offsetof(struct bpf_sock_ops_kern, sk));\
6694 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
6695 OBJ_FIELD), \
6696 si->dst_reg, si->dst_reg, \
6697 offsetof(OBJ, OBJ_FIELD)); \
6698 } while (0)
6699
6700 /* Helper macro for adding write access to tcp_sock or sock fields.
6701 * The macro is called with two registers, dst_reg which contains a pointer
6702 * to ctx (context) and src_reg which contains the value that should be
6703 * stored. However, we need an additional register since we cannot overwrite
6704 * dst_reg because it may be used later in the program.
6705 * Instead we "borrow" one of the other register. We first save its value
6706 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
6707 * it at the end of the macro.
6708 */
6709 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6710 do { \
6711 int reg = BPF_REG_9; \
6712 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6713 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6714 if (si->dst_reg == reg || si->src_reg == reg) \
6715 reg--; \
6716 if (si->dst_reg == reg || si->src_reg == reg) \
6717 reg--; \
6718 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
6719 offsetof(struct bpf_sock_ops_kern, \
6720 temp)); \
6721 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6722 struct bpf_sock_ops_kern, \
6723 is_fullsock), \
6724 reg, si->dst_reg, \
6725 offsetof(struct bpf_sock_ops_kern, \
6726 is_fullsock)); \
6727 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
6728 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6729 struct bpf_sock_ops_kern, sk),\
6730 reg, si->dst_reg, \
6731 offsetof(struct bpf_sock_ops_kern, sk));\
6732 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
6733 reg, si->src_reg, \
6734 offsetof(OBJ, OBJ_FIELD)); \
6735 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
6736 offsetof(struct bpf_sock_ops_kern, \
6737 temp)); \
6738 } while (0)
6739
6740 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
6741 do { \
6742 if (TYPE == BPF_WRITE) \
6743 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6744 else \
6745 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6746 } while (0)
6747
6748 case offsetof(struct bpf_sock_ops, snd_cwnd):
6749 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
6750 break;
6751
6752 case offsetof(struct bpf_sock_ops, srtt_us):
6753 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
6754 break;
6755
6756 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
6757 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
6758 struct tcp_sock);
6759 break;
6760
6761 case offsetof(struct bpf_sock_ops, snd_ssthresh):
6762 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
6763 break;
6764
6765 case offsetof(struct bpf_sock_ops, rcv_nxt):
6766 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
6767 break;
6768
6769 case offsetof(struct bpf_sock_ops, snd_nxt):
6770 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
6771 break;
6772
6773 case offsetof(struct bpf_sock_ops, snd_una):
6774 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
6775 break;
6776
6777 case offsetof(struct bpf_sock_ops, mss_cache):
6778 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
6779 break;
6780
6781 case offsetof(struct bpf_sock_ops, ecn_flags):
6782 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6783 break;
6784
6785 case offsetof(struct bpf_sock_ops, rate_delivered):
6786 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6787 struct tcp_sock);
6788 break;
6789
6790 case offsetof(struct bpf_sock_ops, rate_interval_us):
6791 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6792 struct tcp_sock);
6793 break;
6794
6795 case offsetof(struct bpf_sock_ops, packets_out):
6796 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6797 break;
6798
6799 case offsetof(struct bpf_sock_ops, retrans_out):
6800 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6801 break;
6802
6803 case offsetof(struct bpf_sock_ops, total_retrans):
6804 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6805 struct tcp_sock);
6806 break;
6807
6808 case offsetof(struct bpf_sock_ops, segs_in):
6809 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
6810 break;
6811
6812 case offsetof(struct bpf_sock_ops, data_segs_in):
6813 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
6814 break;
6815
6816 case offsetof(struct bpf_sock_ops, segs_out):
6817 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
6818 break;
6819
6820 case offsetof(struct bpf_sock_ops, data_segs_out):
6821 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
6822 struct tcp_sock);
6823 break;
6824
6825 case offsetof(struct bpf_sock_ops, lost_out):
6826 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
6827 break;
6828
6829 case offsetof(struct bpf_sock_ops, sacked_out):
6830 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
6831 break;
6832
6833 case offsetof(struct bpf_sock_ops, sk_txhash):
6834 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
6835 struct sock, type);
6836 break;
6837
6838 case offsetof(struct bpf_sock_ops, bytes_received):
6839 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
6840 struct tcp_sock);
6841 break;
6842
6843 case offsetof(struct bpf_sock_ops, bytes_acked):
6844 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
6845 break;
6846
6847 }
6848 return insn - insn_buf;
6849 }
6850
6851 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
6852 const struct bpf_insn *si,
6853 struct bpf_insn *insn_buf,
6854 struct bpf_prog *prog, u32 *target_size)
6855 {
6856 struct bpf_insn *insn = insn_buf;
6857 int off;
6858
6859 switch (si->off) {
6860 case offsetof(struct __sk_buff, data_end):
6861 off = si->off;
6862 off -= offsetof(struct __sk_buff, data_end);
6863 off += offsetof(struct sk_buff, cb);
6864 off += offsetof(struct tcp_skb_cb, bpf.data_end);
6865 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6866 si->src_reg, off);
6867 break;
6868 default:
6869 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6870 target_size);
6871 }
6872
6873 return insn - insn_buf;
6874 }
6875
6876 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
6877 const struct bpf_insn *si,
6878 struct bpf_insn *insn_buf,
6879 struct bpf_prog *prog, u32 *target_size)
6880 {
6881 struct bpf_insn *insn = insn_buf;
6882 #if IS_ENABLED(CONFIG_IPV6)
6883 int off;
6884 #endif
6885
6886 switch (si->off) {
6887 case offsetof(struct sk_msg_md, data):
6888 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
6889 si->dst_reg, si->src_reg,
6890 offsetof(struct sk_msg, data));
6891 break;
6892 case offsetof(struct sk_msg_md, data_end):
6893 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
6894 si->dst_reg, si->src_reg,
6895 offsetof(struct sk_msg, data_end));
6896 break;
6897 case offsetof(struct sk_msg_md, family):
6898 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6899
6900 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6901 struct sk_msg, sk),
6902 si->dst_reg, si->src_reg,
6903 offsetof(struct sk_msg, sk));
6904 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6905 offsetof(struct sock_common, skc_family));
6906 break;
6907
6908 case offsetof(struct sk_msg_md, remote_ip4):
6909 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6910
6911 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6912 struct sk_msg, sk),
6913 si->dst_reg, si->src_reg,
6914 offsetof(struct sk_msg, sk));
6915 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6916 offsetof(struct sock_common, skc_daddr));
6917 break;
6918
6919 case offsetof(struct sk_msg_md, local_ip4):
6920 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6921 skc_rcv_saddr) != 4);
6922
6923 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6924 struct sk_msg, sk),
6925 si->dst_reg, si->src_reg,
6926 offsetof(struct sk_msg, sk));
6927 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6928 offsetof(struct sock_common,
6929 skc_rcv_saddr));
6930 break;
6931
6932 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
6933 offsetof(struct sk_msg_md, remote_ip6[3]):
6934 #if IS_ENABLED(CONFIG_IPV6)
6935 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6936 skc_v6_daddr.s6_addr32[0]) != 4);
6937
6938 off = si->off;
6939 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
6940 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6941 struct sk_msg, sk),
6942 si->dst_reg, si->src_reg,
6943 offsetof(struct sk_msg, sk));
6944 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6945 offsetof(struct sock_common,
6946 skc_v6_daddr.s6_addr32[0]) +
6947 off);
6948 #else
6949 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6950 #endif
6951 break;
6952
6953 case offsetof(struct sk_msg_md, local_ip6[0]) ...
6954 offsetof(struct sk_msg_md, local_ip6[3]):
6955 #if IS_ENABLED(CONFIG_IPV6)
6956 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6957 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6958
6959 off = si->off;
6960 off -= offsetof(struct sk_msg_md, local_ip6[0]);
6961 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6962 struct sk_msg, sk),
6963 si->dst_reg, si->src_reg,
6964 offsetof(struct sk_msg, sk));
6965 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6966 offsetof(struct sock_common,
6967 skc_v6_rcv_saddr.s6_addr32[0]) +
6968 off);
6969 #else
6970 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6971 #endif
6972 break;
6973
6974 case offsetof(struct sk_msg_md, remote_port):
6975 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6976
6977 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6978 struct sk_msg, sk),
6979 si->dst_reg, si->src_reg,
6980 offsetof(struct sk_msg, sk));
6981 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6982 offsetof(struct sock_common, skc_dport));
6983 #ifndef __BIG_ENDIAN_BITFIELD
6984 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6985 #endif
6986 break;
6987
6988 case offsetof(struct sk_msg_md, local_port):
6989 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6990
6991 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6992 struct sk_msg, sk),
6993 si->dst_reg, si->src_reg,
6994 offsetof(struct sk_msg, sk));
6995 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6996 offsetof(struct sock_common, skc_num));
6997 break;
6998 }
6999
7000 return insn - insn_buf;
7001 }
7002
7003 const struct bpf_verifier_ops sk_filter_verifier_ops = {
7004 .get_func_proto = sk_filter_func_proto,
7005 .is_valid_access = sk_filter_is_valid_access,
7006 .convert_ctx_access = bpf_convert_ctx_access,
7007 .gen_ld_abs = bpf_gen_ld_abs,
7008 };
7009
7010 const struct bpf_prog_ops sk_filter_prog_ops = {
7011 .test_run = bpf_prog_test_run_skb,
7012 };
7013
7014 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
7015 .get_func_proto = tc_cls_act_func_proto,
7016 .is_valid_access = tc_cls_act_is_valid_access,
7017 .convert_ctx_access = tc_cls_act_convert_ctx_access,
7018 .gen_prologue = tc_cls_act_prologue,
7019 .gen_ld_abs = bpf_gen_ld_abs,
7020 };
7021
7022 const struct bpf_prog_ops tc_cls_act_prog_ops = {
7023 .test_run = bpf_prog_test_run_skb,
7024 };
7025
7026 const struct bpf_verifier_ops xdp_verifier_ops = {
7027 .get_func_proto = xdp_func_proto,
7028 .is_valid_access = xdp_is_valid_access,
7029 .convert_ctx_access = xdp_convert_ctx_access,
7030 };
7031
7032 const struct bpf_prog_ops xdp_prog_ops = {
7033 .test_run = bpf_prog_test_run_xdp,
7034 };
7035
7036 const struct bpf_verifier_ops cg_skb_verifier_ops = {
7037 .get_func_proto = cg_skb_func_proto,
7038 .is_valid_access = sk_filter_is_valid_access,
7039 .convert_ctx_access = bpf_convert_ctx_access,
7040 };
7041
7042 const struct bpf_prog_ops cg_skb_prog_ops = {
7043 .test_run = bpf_prog_test_run_skb,
7044 };
7045
7046 const struct bpf_verifier_ops lwt_in_verifier_ops = {
7047 .get_func_proto = lwt_in_func_proto,
7048 .is_valid_access = lwt_is_valid_access,
7049 .convert_ctx_access = bpf_convert_ctx_access,
7050 };
7051
7052 const struct bpf_prog_ops lwt_in_prog_ops = {
7053 .test_run = bpf_prog_test_run_skb,
7054 };
7055
7056 const struct bpf_verifier_ops lwt_out_verifier_ops = {
7057 .get_func_proto = lwt_out_func_proto,
7058 .is_valid_access = lwt_is_valid_access,
7059 .convert_ctx_access = bpf_convert_ctx_access,
7060 };
7061
7062 const struct bpf_prog_ops lwt_out_prog_ops = {
7063 .test_run = bpf_prog_test_run_skb,
7064 };
7065
7066 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
7067 .get_func_proto = lwt_xmit_func_proto,
7068 .is_valid_access = lwt_is_valid_access,
7069 .convert_ctx_access = bpf_convert_ctx_access,
7070 .gen_prologue = tc_cls_act_prologue,
7071 };
7072
7073 const struct bpf_prog_ops lwt_xmit_prog_ops = {
7074 .test_run = bpf_prog_test_run_skb,
7075 };
7076
7077 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
7078 .get_func_proto = lwt_seg6local_func_proto,
7079 .is_valid_access = lwt_is_valid_access,
7080 .convert_ctx_access = bpf_convert_ctx_access,
7081 };
7082
7083 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
7084 .test_run = bpf_prog_test_run_skb,
7085 };
7086
7087 const struct bpf_verifier_ops cg_sock_verifier_ops = {
7088 .get_func_proto = sock_filter_func_proto,
7089 .is_valid_access = sock_filter_is_valid_access,
7090 .convert_ctx_access = bpf_sock_convert_ctx_access,
7091 };
7092
7093 const struct bpf_prog_ops cg_sock_prog_ops = {
7094 };
7095
7096 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
7097 .get_func_proto = sock_addr_func_proto,
7098 .is_valid_access = sock_addr_is_valid_access,
7099 .convert_ctx_access = sock_addr_convert_ctx_access,
7100 };
7101
7102 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
7103 };
7104
7105 const struct bpf_verifier_ops sock_ops_verifier_ops = {
7106 .get_func_proto = sock_ops_func_proto,
7107 .is_valid_access = sock_ops_is_valid_access,
7108 .convert_ctx_access = sock_ops_convert_ctx_access,
7109 };
7110
7111 const struct bpf_prog_ops sock_ops_prog_ops = {
7112 };
7113
7114 const struct bpf_verifier_ops sk_skb_verifier_ops = {
7115 .get_func_proto = sk_skb_func_proto,
7116 .is_valid_access = sk_skb_is_valid_access,
7117 .convert_ctx_access = sk_skb_convert_ctx_access,
7118 .gen_prologue = sk_skb_prologue,
7119 };
7120
7121 const struct bpf_prog_ops sk_skb_prog_ops = {
7122 };
7123
7124 const struct bpf_verifier_ops sk_msg_verifier_ops = {
7125 .get_func_proto = sk_msg_func_proto,
7126 .is_valid_access = sk_msg_is_valid_access,
7127 .convert_ctx_access = sk_msg_convert_ctx_access,
7128 };
7129
7130 const struct bpf_prog_ops sk_msg_prog_ops = {
7131 };
7132
7133 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
7134 .get_func_proto = flow_dissector_func_proto,
7135 .is_valid_access = flow_dissector_is_valid_access,
7136 .convert_ctx_access = bpf_convert_ctx_access,
7137 };
7138
7139 const struct bpf_prog_ops flow_dissector_prog_ops = {
7140 };
7141
7142 int sk_detach_filter(struct sock *sk)
7143 {
7144 int ret = -ENOENT;
7145 struct sk_filter *filter;
7146
7147 if (sock_flag(sk, SOCK_FILTER_LOCKED))
7148 return -EPERM;
7149
7150 filter = rcu_dereference_protected(sk->sk_filter,
7151 lockdep_sock_is_held(sk));
7152 if (filter) {
7153 RCU_INIT_POINTER(sk->sk_filter, NULL);
7154 sk_filter_uncharge(sk, filter);
7155 ret = 0;
7156 }
7157
7158 return ret;
7159 }
7160 EXPORT_SYMBOL_GPL(sk_detach_filter);
7161
7162 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
7163 unsigned int len)
7164 {
7165 struct sock_fprog_kern *fprog;
7166 struct sk_filter *filter;
7167 int ret = 0;
7168
7169 lock_sock(sk);
7170 filter = rcu_dereference_protected(sk->sk_filter,
7171 lockdep_sock_is_held(sk));
7172 if (!filter)
7173 goto out;
7174
7175 /* We're copying the filter that has been originally attached,
7176 * so no conversion/decode needed anymore. eBPF programs that
7177 * have no original program cannot be dumped through this.
7178 */
7179 ret = -EACCES;
7180 fprog = filter->prog->orig_prog;
7181 if (!fprog)
7182 goto out;
7183
7184 ret = fprog->len;
7185 if (!len)
7186 /* User space only enquires number of filter blocks. */
7187 goto out;
7188
7189 ret = -EINVAL;
7190 if (len < fprog->len)
7191 goto out;
7192
7193 ret = -EFAULT;
7194 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
7195 goto out;
7196
7197 /* Instead of bytes, the API requests to return the number
7198 * of filter blocks.
7199 */
7200 ret = fprog->len;
7201 out:
7202 release_sock(sk);
7203 return ret;
7204 }
7205
7206 #ifdef CONFIG_INET
7207 struct sk_reuseport_kern {
7208 struct sk_buff *skb;
7209 struct sock *sk;
7210 struct sock *selected_sk;
7211 void *data_end;
7212 u32 hash;
7213 u32 reuseport_id;
7214 bool bind_inany;
7215 };
7216
7217 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
7218 struct sock_reuseport *reuse,
7219 struct sock *sk, struct sk_buff *skb,
7220 u32 hash)
7221 {
7222 reuse_kern->skb = skb;
7223 reuse_kern->sk = sk;
7224 reuse_kern->selected_sk = NULL;
7225 reuse_kern->data_end = skb->data + skb_headlen(skb);
7226 reuse_kern->hash = hash;
7227 reuse_kern->reuseport_id = reuse->reuseport_id;
7228 reuse_kern->bind_inany = reuse->bind_inany;
7229 }
7230
7231 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
7232 struct bpf_prog *prog, struct sk_buff *skb,
7233 u32 hash)
7234 {
7235 struct sk_reuseport_kern reuse_kern;
7236 enum sk_action action;
7237
7238 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
7239 action = BPF_PROG_RUN(prog, &reuse_kern);
7240
7241 if (action == SK_PASS)
7242 return reuse_kern.selected_sk;
7243 else
7244 return ERR_PTR(-ECONNREFUSED);
7245 }
7246
7247 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
7248 struct bpf_map *, map, void *, key, u32, flags)
7249 {
7250 struct sock_reuseport *reuse;
7251 struct sock *selected_sk;
7252
7253 selected_sk = map->ops->map_lookup_elem(map, key);
7254 if (!selected_sk)
7255 return -ENOENT;
7256
7257 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
7258 if (!reuse)
7259 /* selected_sk is unhashed (e.g. by close()) after the
7260 * above map_lookup_elem(). Treat selected_sk has already
7261 * been removed from the map.
7262 */
7263 return -ENOENT;
7264
7265 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
7266 struct sock *sk;
7267
7268 if (unlikely(!reuse_kern->reuseport_id))
7269 /* There is a small race between adding the
7270 * sk to the map and setting the
7271 * reuse_kern->reuseport_id.
7272 * Treat it as the sk has not been added to
7273 * the bpf map yet.
7274 */
7275 return -ENOENT;
7276
7277 sk = reuse_kern->sk;
7278 if (sk->sk_protocol != selected_sk->sk_protocol)
7279 return -EPROTOTYPE;
7280 else if (sk->sk_family != selected_sk->sk_family)
7281 return -EAFNOSUPPORT;
7282
7283 /* Catch all. Likely bound to a different sockaddr. */
7284 return -EBADFD;
7285 }
7286
7287 reuse_kern->selected_sk = selected_sk;
7288
7289 return 0;
7290 }
7291
7292 static const struct bpf_func_proto sk_select_reuseport_proto = {
7293 .func = sk_select_reuseport,
7294 .gpl_only = false,
7295 .ret_type = RET_INTEGER,
7296 .arg1_type = ARG_PTR_TO_CTX,
7297 .arg2_type = ARG_CONST_MAP_PTR,
7298 .arg3_type = ARG_PTR_TO_MAP_KEY,
7299 .arg4_type = ARG_ANYTHING,
7300 };
7301
7302 BPF_CALL_4(sk_reuseport_load_bytes,
7303 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7304 void *, to, u32, len)
7305 {
7306 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
7307 }
7308
7309 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
7310 .func = sk_reuseport_load_bytes,
7311 .gpl_only = false,
7312 .ret_type = RET_INTEGER,
7313 .arg1_type = ARG_PTR_TO_CTX,
7314 .arg2_type = ARG_ANYTHING,
7315 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7316 .arg4_type = ARG_CONST_SIZE,
7317 };
7318
7319 BPF_CALL_5(sk_reuseport_load_bytes_relative,
7320 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7321 void *, to, u32, len, u32, start_header)
7322 {
7323 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
7324 len, start_header);
7325 }
7326
7327 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
7328 .func = sk_reuseport_load_bytes_relative,
7329 .gpl_only = false,
7330 .ret_type = RET_INTEGER,
7331 .arg1_type = ARG_PTR_TO_CTX,
7332 .arg2_type = ARG_ANYTHING,
7333 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7334 .arg4_type = ARG_CONST_SIZE,
7335 .arg5_type = ARG_ANYTHING,
7336 };
7337
7338 static const struct bpf_func_proto *
7339 sk_reuseport_func_proto(enum bpf_func_id func_id,
7340 const struct bpf_prog *prog)
7341 {
7342 switch (func_id) {
7343 case BPF_FUNC_sk_select_reuseport:
7344 return &sk_select_reuseport_proto;
7345 case BPF_FUNC_skb_load_bytes:
7346 return &sk_reuseport_load_bytes_proto;
7347 case BPF_FUNC_skb_load_bytes_relative:
7348 return &sk_reuseport_load_bytes_relative_proto;
7349 default:
7350 return bpf_base_func_proto(func_id);
7351 }
7352 }
7353
7354 static bool
7355 sk_reuseport_is_valid_access(int off, int size,
7356 enum bpf_access_type type,
7357 const struct bpf_prog *prog,
7358 struct bpf_insn_access_aux *info)
7359 {
7360 const u32 size_default = sizeof(__u32);
7361
7362 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
7363 off % size || type != BPF_READ)
7364 return false;
7365
7366 switch (off) {
7367 case offsetof(struct sk_reuseport_md, data):
7368 info->reg_type = PTR_TO_PACKET;
7369 return size == sizeof(__u64);
7370
7371 case offsetof(struct sk_reuseport_md, data_end):
7372 info->reg_type = PTR_TO_PACKET_END;
7373 return size == sizeof(__u64);
7374
7375 case offsetof(struct sk_reuseport_md, hash):
7376 return size == size_default;
7377
7378 /* Fields that allow narrowing */
7379 case offsetof(struct sk_reuseport_md, eth_protocol):
7380 if (size < FIELD_SIZEOF(struct sk_buff, protocol))
7381 return false;
7382 /* fall through */
7383 case offsetof(struct sk_reuseport_md, ip_protocol):
7384 case offsetof(struct sk_reuseport_md, bind_inany):
7385 case offsetof(struct sk_reuseport_md, len):
7386 bpf_ctx_record_field_size(info, size_default);
7387 return bpf_ctx_narrow_access_ok(off, size, size_default);
7388
7389 default:
7390 return false;
7391 }
7392 }
7393
7394 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
7395 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7396 si->dst_reg, si->src_reg, \
7397 bpf_target_off(struct sk_reuseport_kern, F, \
7398 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7399 target_size)); \
7400 })
7401
7402 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
7403 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
7404 struct sk_buff, \
7405 skb, \
7406 SKB_FIELD)
7407
7408 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
7409 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
7410 struct sock, \
7411 sk, \
7412 SK_FIELD, BPF_SIZE, EXTRA_OFF)
7413
7414 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
7415 const struct bpf_insn *si,
7416 struct bpf_insn *insn_buf,
7417 struct bpf_prog *prog,
7418 u32 *target_size)
7419 {
7420 struct bpf_insn *insn = insn_buf;
7421
7422 switch (si->off) {
7423 case offsetof(struct sk_reuseport_md, data):
7424 SK_REUSEPORT_LOAD_SKB_FIELD(data);
7425 break;
7426
7427 case offsetof(struct sk_reuseport_md, len):
7428 SK_REUSEPORT_LOAD_SKB_FIELD(len);
7429 break;
7430
7431 case offsetof(struct sk_reuseport_md, eth_protocol):
7432 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
7433 break;
7434
7435 case offsetof(struct sk_reuseport_md, ip_protocol):
7436 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
7437 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset,
7438 BPF_W, 0);
7439 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7440 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7441 SK_FL_PROTO_SHIFT);
7442 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
7443 * aware. No further narrowing or masking is needed.
7444 */
7445 *target_size = 1;
7446 break;
7447
7448 case offsetof(struct sk_reuseport_md, data_end):
7449 SK_REUSEPORT_LOAD_FIELD(data_end);
7450 break;
7451
7452 case offsetof(struct sk_reuseport_md, hash):
7453 SK_REUSEPORT_LOAD_FIELD(hash);
7454 break;
7455
7456 case offsetof(struct sk_reuseport_md, bind_inany):
7457 SK_REUSEPORT_LOAD_FIELD(bind_inany);
7458 break;
7459 }
7460
7461 return insn - insn_buf;
7462 }
7463
7464 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
7465 .get_func_proto = sk_reuseport_func_proto,
7466 .is_valid_access = sk_reuseport_is_valid_access,
7467 .convert_ctx_access = sk_reuseport_convert_ctx_access,
7468 };
7469
7470 const struct bpf_prog_ops sk_reuseport_prog_ops = {
7471 };
7472 #endif /* CONFIG_INET */