]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/core/filter.c
Merge tag 'for-linus-20170825' of git://git.infradead.org/linux-mtd
[mirror_ubuntu-artful-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/ip.h>
37 #include <net/protocol.h>
38 #include <net/netlink.h>
39 #include <linux/skbuff.h>
40 #include <net/sock.h>
41 #include <net/flow_dissector.h>
42 #include <linux/errno.h>
43 #include <linux/timer.h>
44 #include <linux/uaccess.h>
45 #include <asm/unaligned.h>
46 #include <linux/filter.h>
47 #include <linux/ratelimit.h>
48 #include <linux/seccomp.h>
49 #include <linux/if_vlan.h>
50 #include <linux/bpf.h>
51 #include <net/sch_generic.h>
52 #include <net/cls_cgroup.h>
53 #include <net/dst_metadata.h>
54 #include <net/dst.h>
55 #include <net/sock_reuseport.h>
56 #include <net/busy_poll.h>
57 #include <net/tcp.h>
58
59 /**
60 * sk_filter_trim_cap - run a packet through a socket filter
61 * @sk: sock associated with &sk_buff
62 * @skb: buffer to filter
63 * @cap: limit on how short the eBPF program may trim the packet
64 *
65 * Run the eBPF program and then cut skb->data to correct size returned by
66 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
67 * than pkt_len we keep whole skb->data. This is the socket level
68 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
69 * be accepted or -EPERM if the packet should be tossed.
70 *
71 */
72 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
73 {
74 int err;
75 struct sk_filter *filter;
76
77 /*
78 * If the skb was allocated from pfmemalloc reserves, only
79 * allow SOCK_MEMALLOC sockets to use it as this socket is
80 * helping free memory
81 */
82 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
83 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
84 return -ENOMEM;
85 }
86 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
87 if (err)
88 return err;
89
90 err = security_sock_rcv_skb(sk, skb);
91 if (err)
92 return err;
93
94 rcu_read_lock();
95 filter = rcu_dereference(sk->sk_filter);
96 if (filter) {
97 struct sock *save_sk = skb->sk;
98 unsigned int pkt_len;
99
100 skb->sk = sk;
101 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
102 skb->sk = save_sk;
103 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
104 }
105 rcu_read_unlock();
106
107 return err;
108 }
109 EXPORT_SYMBOL(sk_filter_trim_cap);
110
111 BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
112 {
113 return skb_get_poff(skb);
114 }
115
116 BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
117 {
118 struct nlattr *nla;
119
120 if (skb_is_nonlinear(skb))
121 return 0;
122
123 if (skb->len < sizeof(struct nlattr))
124 return 0;
125
126 if (a > skb->len - sizeof(struct nlattr))
127 return 0;
128
129 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
130 if (nla)
131 return (void *) nla - (void *) skb->data;
132
133 return 0;
134 }
135
136 BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
137 {
138 struct nlattr *nla;
139
140 if (skb_is_nonlinear(skb))
141 return 0;
142
143 if (skb->len < sizeof(struct nlattr))
144 return 0;
145
146 if (a > skb->len - sizeof(struct nlattr))
147 return 0;
148
149 nla = (struct nlattr *) &skb->data[a];
150 if (nla->nla_len > skb->len - a)
151 return 0;
152
153 nla = nla_find_nested(nla, x);
154 if (nla)
155 return (void *) nla - (void *) skb->data;
156
157 return 0;
158 }
159
160 BPF_CALL_0(__get_raw_cpu_id)
161 {
162 return raw_smp_processor_id();
163 }
164
165 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
166 .func = __get_raw_cpu_id,
167 .gpl_only = false,
168 .ret_type = RET_INTEGER,
169 };
170
171 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
172 struct bpf_insn *insn_buf)
173 {
174 struct bpf_insn *insn = insn_buf;
175
176 switch (skb_field) {
177 case SKF_AD_MARK:
178 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
179
180 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
181 offsetof(struct sk_buff, mark));
182 break;
183
184 case SKF_AD_PKTTYPE:
185 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
186 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
187 #ifdef __BIG_ENDIAN_BITFIELD
188 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
189 #endif
190 break;
191
192 case SKF_AD_QUEUE:
193 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
194
195 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
196 offsetof(struct sk_buff, queue_mapping));
197 break;
198
199 case SKF_AD_VLAN_TAG:
200 case SKF_AD_VLAN_TAG_PRESENT:
201 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
202 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
203
204 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
205 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
206 offsetof(struct sk_buff, vlan_tci));
207 if (skb_field == SKF_AD_VLAN_TAG) {
208 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
209 ~VLAN_TAG_PRESENT);
210 } else {
211 /* dst_reg >>= 12 */
212 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
213 /* dst_reg &= 1 */
214 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
215 }
216 break;
217 }
218
219 return insn - insn_buf;
220 }
221
222 static bool convert_bpf_extensions(struct sock_filter *fp,
223 struct bpf_insn **insnp)
224 {
225 struct bpf_insn *insn = *insnp;
226 u32 cnt;
227
228 switch (fp->k) {
229 case SKF_AD_OFF + SKF_AD_PROTOCOL:
230 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
231
232 /* A = *(u16 *) (CTX + offsetof(protocol)) */
233 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
234 offsetof(struct sk_buff, protocol));
235 /* A = ntohs(A) [emitting a nop or swap16] */
236 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
237 break;
238
239 case SKF_AD_OFF + SKF_AD_PKTTYPE:
240 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
241 insn += cnt - 1;
242 break;
243
244 case SKF_AD_OFF + SKF_AD_IFINDEX:
245 case SKF_AD_OFF + SKF_AD_HATYPE:
246 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
247 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
248
249 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
250 BPF_REG_TMP, BPF_REG_CTX,
251 offsetof(struct sk_buff, dev));
252 /* if (tmp != 0) goto pc + 1 */
253 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
254 *insn++ = BPF_EXIT_INSN();
255 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
256 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
257 offsetof(struct net_device, ifindex));
258 else
259 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
260 offsetof(struct net_device, type));
261 break;
262
263 case SKF_AD_OFF + SKF_AD_MARK:
264 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
265 insn += cnt - 1;
266 break;
267
268 case SKF_AD_OFF + SKF_AD_RXHASH:
269 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
270
271 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
272 offsetof(struct sk_buff, hash));
273 break;
274
275 case SKF_AD_OFF + SKF_AD_QUEUE:
276 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
277 insn += cnt - 1;
278 break;
279
280 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
281 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
282 BPF_REG_A, BPF_REG_CTX, insn);
283 insn += cnt - 1;
284 break;
285
286 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
287 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
288 BPF_REG_A, BPF_REG_CTX, insn);
289 insn += cnt - 1;
290 break;
291
292 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
293 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
294
295 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
296 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
297 offsetof(struct sk_buff, vlan_proto));
298 /* A = ntohs(A) [emitting a nop or swap16] */
299 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
300 break;
301
302 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
303 case SKF_AD_OFF + SKF_AD_NLATTR:
304 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
305 case SKF_AD_OFF + SKF_AD_CPU:
306 case SKF_AD_OFF + SKF_AD_RANDOM:
307 /* arg1 = CTX */
308 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
309 /* arg2 = A */
310 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
311 /* arg3 = X */
312 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
313 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
314 switch (fp->k) {
315 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
316 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
317 break;
318 case SKF_AD_OFF + SKF_AD_NLATTR:
319 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
320 break;
321 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
322 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
323 break;
324 case SKF_AD_OFF + SKF_AD_CPU:
325 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
326 break;
327 case SKF_AD_OFF + SKF_AD_RANDOM:
328 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
329 bpf_user_rnd_init_once();
330 break;
331 }
332 break;
333
334 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
335 /* A ^= X */
336 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
337 break;
338
339 default:
340 /* This is just a dummy call to avoid letting the compiler
341 * evict __bpf_call_base() as an optimization. Placed here
342 * where no-one bothers.
343 */
344 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
345 return false;
346 }
347
348 *insnp = insn;
349 return true;
350 }
351
352 /**
353 * bpf_convert_filter - convert filter program
354 * @prog: the user passed filter program
355 * @len: the length of the user passed filter program
356 * @new_prog: allocated 'struct bpf_prog' or NULL
357 * @new_len: pointer to store length of converted program
358 *
359 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
360 * style extended BPF (eBPF).
361 * Conversion workflow:
362 *
363 * 1) First pass for calculating the new program length:
364 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
365 *
366 * 2) 2nd pass to remap in two passes: 1st pass finds new
367 * jump offsets, 2nd pass remapping:
368 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
369 */
370 static int bpf_convert_filter(struct sock_filter *prog, int len,
371 struct bpf_prog *new_prog, int *new_len)
372 {
373 int new_flen = 0, pass = 0, target, i, stack_off;
374 struct bpf_insn *new_insn, *first_insn = NULL;
375 struct sock_filter *fp;
376 int *addrs = NULL;
377 u8 bpf_src;
378
379 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
380 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
381
382 if (len <= 0 || len > BPF_MAXINSNS)
383 return -EINVAL;
384
385 if (new_prog) {
386 first_insn = new_prog->insnsi;
387 addrs = kcalloc(len, sizeof(*addrs),
388 GFP_KERNEL | __GFP_NOWARN);
389 if (!addrs)
390 return -ENOMEM;
391 }
392
393 do_pass:
394 new_insn = first_insn;
395 fp = prog;
396
397 /* Classic BPF related prologue emission. */
398 if (new_prog) {
399 /* Classic BPF expects A and X to be reset first. These need
400 * to be guaranteed to be the first two instructions.
401 */
402 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
403 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
404
405 /* All programs must keep CTX in callee saved BPF_REG_CTX.
406 * In eBPF case it's done by the compiler, here we need to
407 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
408 */
409 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
410 } else {
411 new_insn += 3;
412 }
413
414 for (i = 0; i < len; fp++, i++) {
415 struct bpf_insn tmp_insns[6] = { };
416 struct bpf_insn *insn = tmp_insns;
417
418 if (addrs)
419 addrs[i] = new_insn - first_insn;
420
421 switch (fp->code) {
422 /* All arithmetic insns and skb loads map as-is. */
423 case BPF_ALU | BPF_ADD | BPF_X:
424 case BPF_ALU | BPF_ADD | BPF_K:
425 case BPF_ALU | BPF_SUB | BPF_X:
426 case BPF_ALU | BPF_SUB | BPF_K:
427 case BPF_ALU | BPF_AND | BPF_X:
428 case BPF_ALU | BPF_AND | BPF_K:
429 case BPF_ALU | BPF_OR | BPF_X:
430 case BPF_ALU | BPF_OR | BPF_K:
431 case BPF_ALU | BPF_LSH | BPF_X:
432 case BPF_ALU | BPF_LSH | BPF_K:
433 case BPF_ALU | BPF_RSH | BPF_X:
434 case BPF_ALU | BPF_RSH | BPF_K:
435 case BPF_ALU | BPF_XOR | BPF_X:
436 case BPF_ALU | BPF_XOR | BPF_K:
437 case BPF_ALU | BPF_MUL | BPF_X:
438 case BPF_ALU | BPF_MUL | BPF_K:
439 case BPF_ALU | BPF_DIV | BPF_X:
440 case BPF_ALU | BPF_DIV | BPF_K:
441 case BPF_ALU | BPF_MOD | BPF_X:
442 case BPF_ALU | BPF_MOD | BPF_K:
443 case BPF_ALU | BPF_NEG:
444 case BPF_LD | BPF_ABS | BPF_W:
445 case BPF_LD | BPF_ABS | BPF_H:
446 case BPF_LD | BPF_ABS | BPF_B:
447 case BPF_LD | BPF_IND | BPF_W:
448 case BPF_LD | BPF_IND | BPF_H:
449 case BPF_LD | BPF_IND | BPF_B:
450 /* Check for overloaded BPF extension and
451 * directly convert it if found, otherwise
452 * just move on with mapping.
453 */
454 if (BPF_CLASS(fp->code) == BPF_LD &&
455 BPF_MODE(fp->code) == BPF_ABS &&
456 convert_bpf_extensions(fp, &insn))
457 break;
458
459 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
460 break;
461
462 /* Jump transformation cannot use BPF block macros
463 * everywhere as offset calculation and target updates
464 * require a bit more work than the rest, i.e. jump
465 * opcodes map as-is, but offsets need adjustment.
466 */
467
468 #define BPF_EMIT_JMP \
469 do { \
470 if (target >= len || target < 0) \
471 goto err; \
472 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
473 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
474 insn->off -= insn - tmp_insns; \
475 } while (0)
476
477 case BPF_JMP | BPF_JA:
478 target = i + fp->k + 1;
479 insn->code = fp->code;
480 BPF_EMIT_JMP;
481 break;
482
483 case BPF_JMP | BPF_JEQ | BPF_K:
484 case BPF_JMP | BPF_JEQ | BPF_X:
485 case BPF_JMP | BPF_JSET | BPF_K:
486 case BPF_JMP | BPF_JSET | BPF_X:
487 case BPF_JMP | BPF_JGT | BPF_K:
488 case BPF_JMP | BPF_JGT | BPF_X:
489 case BPF_JMP | BPF_JGE | BPF_K:
490 case BPF_JMP | BPF_JGE | BPF_X:
491 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
492 /* BPF immediates are signed, zero extend
493 * immediate into tmp register and use it
494 * in compare insn.
495 */
496 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
497
498 insn->dst_reg = BPF_REG_A;
499 insn->src_reg = BPF_REG_TMP;
500 bpf_src = BPF_X;
501 } else {
502 insn->dst_reg = BPF_REG_A;
503 insn->imm = fp->k;
504 bpf_src = BPF_SRC(fp->code);
505 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
506 }
507
508 /* Common case where 'jump_false' is next insn. */
509 if (fp->jf == 0) {
510 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
511 target = i + fp->jt + 1;
512 BPF_EMIT_JMP;
513 break;
514 }
515
516 /* Convert JEQ into JNE when 'jump_true' is next insn. */
517 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
518 insn->code = BPF_JMP | BPF_JNE | bpf_src;
519 target = i + fp->jf + 1;
520 BPF_EMIT_JMP;
521 break;
522 }
523
524 /* Other jumps are mapped into two insns: Jxx and JA. */
525 target = i + fp->jt + 1;
526 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
527 BPF_EMIT_JMP;
528 insn++;
529
530 insn->code = BPF_JMP | BPF_JA;
531 target = i + fp->jf + 1;
532 BPF_EMIT_JMP;
533 break;
534
535 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
536 case BPF_LDX | BPF_MSH | BPF_B:
537 /* tmp = A */
538 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
539 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
540 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
541 /* A &= 0xf */
542 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
543 /* A <<= 2 */
544 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
545 /* X = A */
546 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
547 /* A = tmp */
548 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
549 break;
550
551 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
552 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
553 */
554 case BPF_RET | BPF_A:
555 case BPF_RET | BPF_K:
556 if (BPF_RVAL(fp->code) == BPF_K)
557 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
558 0, fp->k);
559 *insn = BPF_EXIT_INSN();
560 break;
561
562 /* Store to stack. */
563 case BPF_ST:
564 case BPF_STX:
565 stack_off = fp->k * 4 + 4;
566 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
567 BPF_ST ? BPF_REG_A : BPF_REG_X,
568 -stack_off);
569 /* check_load_and_stores() verifies that classic BPF can
570 * load from stack only after write, so tracking
571 * stack_depth for ST|STX insns is enough
572 */
573 if (new_prog && new_prog->aux->stack_depth < stack_off)
574 new_prog->aux->stack_depth = stack_off;
575 break;
576
577 /* Load from stack. */
578 case BPF_LD | BPF_MEM:
579 case BPF_LDX | BPF_MEM:
580 stack_off = fp->k * 4 + 4;
581 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
582 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
583 -stack_off);
584 break;
585
586 /* A = K or X = K */
587 case BPF_LD | BPF_IMM:
588 case BPF_LDX | BPF_IMM:
589 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
590 BPF_REG_A : BPF_REG_X, fp->k);
591 break;
592
593 /* X = A */
594 case BPF_MISC | BPF_TAX:
595 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
596 break;
597
598 /* A = X */
599 case BPF_MISC | BPF_TXA:
600 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
601 break;
602
603 /* A = skb->len or X = skb->len */
604 case BPF_LD | BPF_W | BPF_LEN:
605 case BPF_LDX | BPF_W | BPF_LEN:
606 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
607 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
608 offsetof(struct sk_buff, len));
609 break;
610
611 /* Access seccomp_data fields. */
612 case BPF_LDX | BPF_ABS | BPF_W:
613 /* A = *(u32 *) (ctx + K) */
614 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
615 break;
616
617 /* Unknown instruction. */
618 default:
619 goto err;
620 }
621
622 insn++;
623 if (new_prog)
624 memcpy(new_insn, tmp_insns,
625 sizeof(*insn) * (insn - tmp_insns));
626 new_insn += insn - tmp_insns;
627 }
628
629 if (!new_prog) {
630 /* Only calculating new length. */
631 *new_len = new_insn - first_insn;
632 return 0;
633 }
634
635 pass++;
636 if (new_flen != new_insn - first_insn) {
637 new_flen = new_insn - first_insn;
638 if (pass > 2)
639 goto err;
640 goto do_pass;
641 }
642
643 kfree(addrs);
644 BUG_ON(*new_len != new_flen);
645 return 0;
646 err:
647 kfree(addrs);
648 return -EINVAL;
649 }
650
651 /* Security:
652 *
653 * As we dont want to clear mem[] array for each packet going through
654 * __bpf_prog_run(), we check that filter loaded by user never try to read
655 * a cell if not previously written, and we check all branches to be sure
656 * a malicious user doesn't try to abuse us.
657 */
658 static int check_load_and_stores(const struct sock_filter *filter, int flen)
659 {
660 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
661 int pc, ret = 0;
662
663 BUILD_BUG_ON(BPF_MEMWORDS > 16);
664
665 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
666 if (!masks)
667 return -ENOMEM;
668
669 memset(masks, 0xff, flen * sizeof(*masks));
670
671 for (pc = 0; pc < flen; pc++) {
672 memvalid &= masks[pc];
673
674 switch (filter[pc].code) {
675 case BPF_ST:
676 case BPF_STX:
677 memvalid |= (1 << filter[pc].k);
678 break;
679 case BPF_LD | BPF_MEM:
680 case BPF_LDX | BPF_MEM:
681 if (!(memvalid & (1 << filter[pc].k))) {
682 ret = -EINVAL;
683 goto error;
684 }
685 break;
686 case BPF_JMP | BPF_JA:
687 /* A jump must set masks on target */
688 masks[pc + 1 + filter[pc].k] &= memvalid;
689 memvalid = ~0;
690 break;
691 case BPF_JMP | BPF_JEQ | BPF_K:
692 case BPF_JMP | BPF_JEQ | BPF_X:
693 case BPF_JMP | BPF_JGE | BPF_K:
694 case BPF_JMP | BPF_JGE | BPF_X:
695 case BPF_JMP | BPF_JGT | BPF_K:
696 case BPF_JMP | BPF_JGT | BPF_X:
697 case BPF_JMP | BPF_JSET | BPF_K:
698 case BPF_JMP | BPF_JSET | BPF_X:
699 /* A jump must set masks on targets */
700 masks[pc + 1 + filter[pc].jt] &= memvalid;
701 masks[pc + 1 + filter[pc].jf] &= memvalid;
702 memvalid = ~0;
703 break;
704 }
705 }
706 error:
707 kfree(masks);
708 return ret;
709 }
710
711 static bool chk_code_allowed(u16 code_to_probe)
712 {
713 static const bool codes[] = {
714 /* 32 bit ALU operations */
715 [BPF_ALU | BPF_ADD | BPF_K] = true,
716 [BPF_ALU | BPF_ADD | BPF_X] = true,
717 [BPF_ALU | BPF_SUB | BPF_K] = true,
718 [BPF_ALU | BPF_SUB | BPF_X] = true,
719 [BPF_ALU | BPF_MUL | BPF_K] = true,
720 [BPF_ALU | BPF_MUL | BPF_X] = true,
721 [BPF_ALU | BPF_DIV | BPF_K] = true,
722 [BPF_ALU | BPF_DIV | BPF_X] = true,
723 [BPF_ALU | BPF_MOD | BPF_K] = true,
724 [BPF_ALU | BPF_MOD | BPF_X] = true,
725 [BPF_ALU | BPF_AND | BPF_K] = true,
726 [BPF_ALU | BPF_AND | BPF_X] = true,
727 [BPF_ALU | BPF_OR | BPF_K] = true,
728 [BPF_ALU | BPF_OR | BPF_X] = true,
729 [BPF_ALU | BPF_XOR | BPF_K] = true,
730 [BPF_ALU | BPF_XOR | BPF_X] = true,
731 [BPF_ALU | BPF_LSH | BPF_K] = true,
732 [BPF_ALU | BPF_LSH | BPF_X] = true,
733 [BPF_ALU | BPF_RSH | BPF_K] = true,
734 [BPF_ALU | BPF_RSH | BPF_X] = true,
735 [BPF_ALU | BPF_NEG] = true,
736 /* Load instructions */
737 [BPF_LD | BPF_W | BPF_ABS] = true,
738 [BPF_LD | BPF_H | BPF_ABS] = true,
739 [BPF_LD | BPF_B | BPF_ABS] = true,
740 [BPF_LD | BPF_W | BPF_LEN] = true,
741 [BPF_LD | BPF_W | BPF_IND] = true,
742 [BPF_LD | BPF_H | BPF_IND] = true,
743 [BPF_LD | BPF_B | BPF_IND] = true,
744 [BPF_LD | BPF_IMM] = true,
745 [BPF_LD | BPF_MEM] = true,
746 [BPF_LDX | BPF_W | BPF_LEN] = true,
747 [BPF_LDX | BPF_B | BPF_MSH] = true,
748 [BPF_LDX | BPF_IMM] = true,
749 [BPF_LDX | BPF_MEM] = true,
750 /* Store instructions */
751 [BPF_ST] = true,
752 [BPF_STX] = true,
753 /* Misc instructions */
754 [BPF_MISC | BPF_TAX] = true,
755 [BPF_MISC | BPF_TXA] = true,
756 /* Return instructions */
757 [BPF_RET | BPF_K] = true,
758 [BPF_RET | BPF_A] = true,
759 /* Jump instructions */
760 [BPF_JMP | BPF_JA] = true,
761 [BPF_JMP | BPF_JEQ | BPF_K] = true,
762 [BPF_JMP | BPF_JEQ | BPF_X] = true,
763 [BPF_JMP | BPF_JGE | BPF_K] = true,
764 [BPF_JMP | BPF_JGE | BPF_X] = true,
765 [BPF_JMP | BPF_JGT | BPF_K] = true,
766 [BPF_JMP | BPF_JGT | BPF_X] = true,
767 [BPF_JMP | BPF_JSET | BPF_K] = true,
768 [BPF_JMP | BPF_JSET | BPF_X] = true,
769 };
770
771 if (code_to_probe >= ARRAY_SIZE(codes))
772 return false;
773
774 return codes[code_to_probe];
775 }
776
777 static bool bpf_check_basics_ok(const struct sock_filter *filter,
778 unsigned int flen)
779 {
780 if (filter == NULL)
781 return false;
782 if (flen == 0 || flen > BPF_MAXINSNS)
783 return false;
784
785 return true;
786 }
787
788 /**
789 * bpf_check_classic - verify socket filter code
790 * @filter: filter to verify
791 * @flen: length of filter
792 *
793 * Check the user's filter code. If we let some ugly
794 * filter code slip through kaboom! The filter must contain
795 * no references or jumps that are out of range, no illegal
796 * instructions, and must end with a RET instruction.
797 *
798 * All jumps are forward as they are not signed.
799 *
800 * Returns 0 if the rule set is legal or -EINVAL if not.
801 */
802 static int bpf_check_classic(const struct sock_filter *filter,
803 unsigned int flen)
804 {
805 bool anc_found;
806 int pc;
807
808 /* Check the filter code now */
809 for (pc = 0; pc < flen; pc++) {
810 const struct sock_filter *ftest = &filter[pc];
811
812 /* May we actually operate on this code? */
813 if (!chk_code_allowed(ftest->code))
814 return -EINVAL;
815
816 /* Some instructions need special checks */
817 switch (ftest->code) {
818 case BPF_ALU | BPF_DIV | BPF_K:
819 case BPF_ALU | BPF_MOD | BPF_K:
820 /* Check for division by zero */
821 if (ftest->k == 0)
822 return -EINVAL;
823 break;
824 case BPF_ALU | BPF_LSH | BPF_K:
825 case BPF_ALU | BPF_RSH | BPF_K:
826 if (ftest->k >= 32)
827 return -EINVAL;
828 break;
829 case BPF_LD | BPF_MEM:
830 case BPF_LDX | BPF_MEM:
831 case BPF_ST:
832 case BPF_STX:
833 /* Check for invalid memory addresses */
834 if (ftest->k >= BPF_MEMWORDS)
835 return -EINVAL;
836 break;
837 case BPF_JMP | BPF_JA:
838 /* Note, the large ftest->k might cause loops.
839 * Compare this with conditional jumps below,
840 * where offsets are limited. --ANK (981016)
841 */
842 if (ftest->k >= (unsigned int)(flen - pc - 1))
843 return -EINVAL;
844 break;
845 case BPF_JMP | BPF_JEQ | BPF_K:
846 case BPF_JMP | BPF_JEQ | BPF_X:
847 case BPF_JMP | BPF_JGE | BPF_K:
848 case BPF_JMP | BPF_JGE | BPF_X:
849 case BPF_JMP | BPF_JGT | BPF_K:
850 case BPF_JMP | BPF_JGT | BPF_X:
851 case BPF_JMP | BPF_JSET | BPF_K:
852 case BPF_JMP | BPF_JSET | BPF_X:
853 /* Both conditionals must be safe */
854 if (pc + ftest->jt + 1 >= flen ||
855 pc + ftest->jf + 1 >= flen)
856 return -EINVAL;
857 break;
858 case BPF_LD | BPF_W | BPF_ABS:
859 case BPF_LD | BPF_H | BPF_ABS:
860 case BPF_LD | BPF_B | BPF_ABS:
861 anc_found = false;
862 if (bpf_anc_helper(ftest) & BPF_ANC)
863 anc_found = true;
864 /* Ancillary operation unknown or unsupported */
865 if (anc_found == false && ftest->k >= SKF_AD_OFF)
866 return -EINVAL;
867 }
868 }
869
870 /* Last instruction must be a RET code */
871 switch (filter[flen - 1].code) {
872 case BPF_RET | BPF_K:
873 case BPF_RET | BPF_A:
874 return check_load_and_stores(filter, flen);
875 }
876
877 return -EINVAL;
878 }
879
880 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
881 const struct sock_fprog *fprog)
882 {
883 unsigned int fsize = bpf_classic_proglen(fprog);
884 struct sock_fprog_kern *fkprog;
885
886 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
887 if (!fp->orig_prog)
888 return -ENOMEM;
889
890 fkprog = fp->orig_prog;
891 fkprog->len = fprog->len;
892
893 fkprog->filter = kmemdup(fp->insns, fsize,
894 GFP_KERNEL | __GFP_NOWARN);
895 if (!fkprog->filter) {
896 kfree(fp->orig_prog);
897 return -ENOMEM;
898 }
899
900 return 0;
901 }
902
903 static void bpf_release_orig_filter(struct bpf_prog *fp)
904 {
905 struct sock_fprog_kern *fprog = fp->orig_prog;
906
907 if (fprog) {
908 kfree(fprog->filter);
909 kfree(fprog);
910 }
911 }
912
913 static void __bpf_prog_release(struct bpf_prog *prog)
914 {
915 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
916 bpf_prog_put(prog);
917 } else {
918 bpf_release_orig_filter(prog);
919 bpf_prog_free(prog);
920 }
921 }
922
923 static void __sk_filter_release(struct sk_filter *fp)
924 {
925 __bpf_prog_release(fp->prog);
926 kfree(fp);
927 }
928
929 /**
930 * sk_filter_release_rcu - Release a socket filter by rcu_head
931 * @rcu: rcu_head that contains the sk_filter to free
932 */
933 static void sk_filter_release_rcu(struct rcu_head *rcu)
934 {
935 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
936
937 __sk_filter_release(fp);
938 }
939
940 /**
941 * sk_filter_release - release a socket filter
942 * @fp: filter to remove
943 *
944 * Remove a filter from a socket and release its resources.
945 */
946 static void sk_filter_release(struct sk_filter *fp)
947 {
948 if (refcount_dec_and_test(&fp->refcnt))
949 call_rcu(&fp->rcu, sk_filter_release_rcu);
950 }
951
952 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
953 {
954 u32 filter_size = bpf_prog_size(fp->prog->len);
955
956 atomic_sub(filter_size, &sk->sk_omem_alloc);
957 sk_filter_release(fp);
958 }
959
960 /* try to charge the socket memory if there is space available
961 * return true on success
962 */
963 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
964 {
965 u32 filter_size = bpf_prog_size(fp->prog->len);
966
967 /* same check as in sock_kmalloc() */
968 if (filter_size <= sysctl_optmem_max &&
969 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
970 atomic_add(filter_size, &sk->sk_omem_alloc);
971 return true;
972 }
973 return false;
974 }
975
976 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
977 {
978 bool ret = __sk_filter_charge(sk, fp);
979 if (ret)
980 refcount_inc(&fp->refcnt);
981 return ret;
982 }
983
984 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
985 {
986 struct sock_filter *old_prog;
987 struct bpf_prog *old_fp;
988 int err, new_len, old_len = fp->len;
989
990 /* We are free to overwrite insns et al right here as it
991 * won't be used at this point in time anymore internally
992 * after the migration to the internal BPF instruction
993 * representation.
994 */
995 BUILD_BUG_ON(sizeof(struct sock_filter) !=
996 sizeof(struct bpf_insn));
997
998 /* Conversion cannot happen on overlapping memory areas,
999 * so we need to keep the user BPF around until the 2nd
1000 * pass. At this time, the user BPF is stored in fp->insns.
1001 */
1002 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1003 GFP_KERNEL | __GFP_NOWARN);
1004 if (!old_prog) {
1005 err = -ENOMEM;
1006 goto out_err;
1007 }
1008
1009 /* 1st pass: calculate the new program length. */
1010 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
1011 if (err)
1012 goto out_err_free;
1013
1014 /* Expand fp for appending the new filter representation. */
1015 old_fp = fp;
1016 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1017 if (!fp) {
1018 /* The old_fp is still around in case we couldn't
1019 * allocate new memory, so uncharge on that one.
1020 */
1021 fp = old_fp;
1022 err = -ENOMEM;
1023 goto out_err_free;
1024 }
1025
1026 fp->len = new_len;
1027
1028 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1029 err = bpf_convert_filter(old_prog, old_len, fp, &new_len);
1030 if (err)
1031 /* 2nd bpf_convert_filter() can fail only if it fails
1032 * to allocate memory, remapping must succeed. Note,
1033 * that at this time old_fp has already been released
1034 * by krealloc().
1035 */
1036 goto out_err_free;
1037
1038 /* We are guaranteed to never error here with cBPF to eBPF
1039 * transitions, since there's no issue with type compatibility
1040 * checks on program arrays.
1041 */
1042 fp = bpf_prog_select_runtime(fp, &err);
1043
1044 kfree(old_prog);
1045 return fp;
1046
1047 out_err_free:
1048 kfree(old_prog);
1049 out_err:
1050 __bpf_prog_release(fp);
1051 return ERR_PTR(err);
1052 }
1053
1054 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1055 bpf_aux_classic_check_t trans)
1056 {
1057 int err;
1058
1059 fp->bpf_func = NULL;
1060 fp->jited = 0;
1061
1062 err = bpf_check_classic(fp->insns, fp->len);
1063 if (err) {
1064 __bpf_prog_release(fp);
1065 return ERR_PTR(err);
1066 }
1067
1068 /* There might be additional checks and transformations
1069 * needed on classic filters, f.e. in case of seccomp.
1070 */
1071 if (trans) {
1072 err = trans(fp->insns, fp->len);
1073 if (err) {
1074 __bpf_prog_release(fp);
1075 return ERR_PTR(err);
1076 }
1077 }
1078
1079 /* Probe if we can JIT compile the filter and if so, do
1080 * the compilation of the filter.
1081 */
1082 bpf_jit_compile(fp);
1083
1084 /* JIT compiler couldn't process this filter, so do the
1085 * internal BPF translation for the optimized interpreter.
1086 */
1087 if (!fp->jited)
1088 fp = bpf_migrate_filter(fp);
1089
1090 return fp;
1091 }
1092
1093 /**
1094 * bpf_prog_create - create an unattached filter
1095 * @pfp: the unattached filter that is created
1096 * @fprog: the filter program
1097 *
1098 * Create a filter independent of any socket. We first run some
1099 * sanity checks on it to make sure it does not explode on us later.
1100 * If an error occurs or there is insufficient memory for the filter
1101 * a negative errno code is returned. On success the return is zero.
1102 */
1103 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1104 {
1105 unsigned int fsize = bpf_classic_proglen(fprog);
1106 struct bpf_prog *fp;
1107
1108 /* Make sure new filter is there and in the right amounts. */
1109 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1110 return -EINVAL;
1111
1112 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1113 if (!fp)
1114 return -ENOMEM;
1115
1116 memcpy(fp->insns, fprog->filter, fsize);
1117
1118 fp->len = fprog->len;
1119 /* Since unattached filters are not copied back to user
1120 * space through sk_get_filter(), we do not need to hold
1121 * a copy here, and can spare us the work.
1122 */
1123 fp->orig_prog = NULL;
1124
1125 /* bpf_prepare_filter() already takes care of freeing
1126 * memory in case something goes wrong.
1127 */
1128 fp = bpf_prepare_filter(fp, NULL);
1129 if (IS_ERR(fp))
1130 return PTR_ERR(fp);
1131
1132 *pfp = fp;
1133 return 0;
1134 }
1135 EXPORT_SYMBOL_GPL(bpf_prog_create);
1136
1137 /**
1138 * bpf_prog_create_from_user - create an unattached filter from user buffer
1139 * @pfp: the unattached filter that is created
1140 * @fprog: the filter program
1141 * @trans: post-classic verifier transformation handler
1142 * @save_orig: save classic BPF program
1143 *
1144 * This function effectively does the same as bpf_prog_create(), only
1145 * that it builds up its insns buffer from user space provided buffer.
1146 * It also allows for passing a bpf_aux_classic_check_t handler.
1147 */
1148 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1149 bpf_aux_classic_check_t trans, bool save_orig)
1150 {
1151 unsigned int fsize = bpf_classic_proglen(fprog);
1152 struct bpf_prog *fp;
1153 int err;
1154
1155 /* Make sure new filter is there and in the right amounts. */
1156 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1157 return -EINVAL;
1158
1159 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1160 if (!fp)
1161 return -ENOMEM;
1162
1163 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1164 __bpf_prog_free(fp);
1165 return -EFAULT;
1166 }
1167
1168 fp->len = fprog->len;
1169 fp->orig_prog = NULL;
1170
1171 if (save_orig) {
1172 err = bpf_prog_store_orig_filter(fp, fprog);
1173 if (err) {
1174 __bpf_prog_free(fp);
1175 return -ENOMEM;
1176 }
1177 }
1178
1179 /* bpf_prepare_filter() already takes care of freeing
1180 * memory in case something goes wrong.
1181 */
1182 fp = bpf_prepare_filter(fp, trans);
1183 if (IS_ERR(fp))
1184 return PTR_ERR(fp);
1185
1186 *pfp = fp;
1187 return 0;
1188 }
1189 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1190
1191 void bpf_prog_destroy(struct bpf_prog *fp)
1192 {
1193 __bpf_prog_release(fp);
1194 }
1195 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1196
1197 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1198 {
1199 struct sk_filter *fp, *old_fp;
1200
1201 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1202 if (!fp)
1203 return -ENOMEM;
1204
1205 fp->prog = prog;
1206
1207 if (!__sk_filter_charge(sk, fp)) {
1208 kfree(fp);
1209 return -ENOMEM;
1210 }
1211 refcount_set(&fp->refcnt, 1);
1212
1213 old_fp = rcu_dereference_protected(sk->sk_filter,
1214 lockdep_sock_is_held(sk));
1215 rcu_assign_pointer(sk->sk_filter, fp);
1216
1217 if (old_fp)
1218 sk_filter_uncharge(sk, old_fp);
1219
1220 return 0;
1221 }
1222
1223 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1224 {
1225 struct bpf_prog *old_prog;
1226 int err;
1227
1228 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1229 return -ENOMEM;
1230
1231 if (sk_unhashed(sk) && sk->sk_reuseport) {
1232 err = reuseport_alloc(sk);
1233 if (err)
1234 return err;
1235 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1236 /* The socket wasn't bound with SO_REUSEPORT */
1237 return -EINVAL;
1238 }
1239
1240 old_prog = reuseport_attach_prog(sk, prog);
1241 if (old_prog)
1242 bpf_prog_destroy(old_prog);
1243
1244 return 0;
1245 }
1246
1247 static
1248 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1249 {
1250 unsigned int fsize = bpf_classic_proglen(fprog);
1251 struct bpf_prog *prog;
1252 int err;
1253
1254 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1255 return ERR_PTR(-EPERM);
1256
1257 /* Make sure new filter is there and in the right amounts. */
1258 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1259 return ERR_PTR(-EINVAL);
1260
1261 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1262 if (!prog)
1263 return ERR_PTR(-ENOMEM);
1264
1265 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1266 __bpf_prog_free(prog);
1267 return ERR_PTR(-EFAULT);
1268 }
1269
1270 prog->len = fprog->len;
1271
1272 err = bpf_prog_store_orig_filter(prog, fprog);
1273 if (err) {
1274 __bpf_prog_free(prog);
1275 return ERR_PTR(-ENOMEM);
1276 }
1277
1278 /* bpf_prepare_filter() already takes care of freeing
1279 * memory in case something goes wrong.
1280 */
1281 return bpf_prepare_filter(prog, NULL);
1282 }
1283
1284 /**
1285 * sk_attach_filter - attach a socket filter
1286 * @fprog: the filter program
1287 * @sk: the socket to use
1288 *
1289 * Attach the user's filter code. We first run some sanity checks on
1290 * it to make sure it does not explode on us later. If an error
1291 * occurs or there is insufficient memory for the filter a negative
1292 * errno code is returned. On success the return is zero.
1293 */
1294 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1295 {
1296 struct bpf_prog *prog = __get_filter(fprog, sk);
1297 int err;
1298
1299 if (IS_ERR(prog))
1300 return PTR_ERR(prog);
1301
1302 err = __sk_attach_prog(prog, sk);
1303 if (err < 0) {
1304 __bpf_prog_release(prog);
1305 return err;
1306 }
1307
1308 return 0;
1309 }
1310 EXPORT_SYMBOL_GPL(sk_attach_filter);
1311
1312 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1313 {
1314 struct bpf_prog *prog = __get_filter(fprog, sk);
1315 int err;
1316
1317 if (IS_ERR(prog))
1318 return PTR_ERR(prog);
1319
1320 err = __reuseport_attach_prog(prog, sk);
1321 if (err < 0) {
1322 __bpf_prog_release(prog);
1323 return err;
1324 }
1325
1326 return 0;
1327 }
1328
1329 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1330 {
1331 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1332 return ERR_PTR(-EPERM);
1333
1334 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1335 }
1336
1337 int sk_attach_bpf(u32 ufd, struct sock *sk)
1338 {
1339 struct bpf_prog *prog = __get_bpf(ufd, sk);
1340 int err;
1341
1342 if (IS_ERR(prog))
1343 return PTR_ERR(prog);
1344
1345 err = __sk_attach_prog(prog, sk);
1346 if (err < 0) {
1347 bpf_prog_put(prog);
1348 return err;
1349 }
1350
1351 return 0;
1352 }
1353
1354 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1355 {
1356 struct bpf_prog *prog = __get_bpf(ufd, sk);
1357 int err;
1358
1359 if (IS_ERR(prog))
1360 return PTR_ERR(prog);
1361
1362 err = __reuseport_attach_prog(prog, sk);
1363 if (err < 0) {
1364 bpf_prog_put(prog);
1365 return err;
1366 }
1367
1368 return 0;
1369 }
1370
1371 struct bpf_scratchpad {
1372 union {
1373 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1374 u8 buff[MAX_BPF_STACK];
1375 };
1376 };
1377
1378 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1379
1380 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1381 unsigned int write_len)
1382 {
1383 return skb_ensure_writable(skb, write_len);
1384 }
1385
1386 static inline int bpf_try_make_writable(struct sk_buff *skb,
1387 unsigned int write_len)
1388 {
1389 int err = __bpf_try_make_writable(skb, write_len);
1390
1391 bpf_compute_data_end(skb);
1392 return err;
1393 }
1394
1395 static int bpf_try_make_head_writable(struct sk_buff *skb)
1396 {
1397 return bpf_try_make_writable(skb, skb_headlen(skb));
1398 }
1399
1400 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1401 {
1402 if (skb_at_tc_ingress(skb))
1403 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1404 }
1405
1406 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1407 {
1408 if (skb_at_tc_ingress(skb))
1409 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1410 }
1411
1412 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1413 const void *, from, u32, len, u64, flags)
1414 {
1415 void *ptr;
1416
1417 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1418 return -EINVAL;
1419 if (unlikely(offset > 0xffff))
1420 return -EFAULT;
1421 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1422 return -EFAULT;
1423
1424 ptr = skb->data + offset;
1425 if (flags & BPF_F_RECOMPUTE_CSUM)
1426 __skb_postpull_rcsum(skb, ptr, len, offset);
1427
1428 memcpy(ptr, from, len);
1429
1430 if (flags & BPF_F_RECOMPUTE_CSUM)
1431 __skb_postpush_rcsum(skb, ptr, len, offset);
1432 if (flags & BPF_F_INVALIDATE_HASH)
1433 skb_clear_hash(skb);
1434
1435 return 0;
1436 }
1437
1438 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1439 .func = bpf_skb_store_bytes,
1440 .gpl_only = false,
1441 .ret_type = RET_INTEGER,
1442 .arg1_type = ARG_PTR_TO_CTX,
1443 .arg2_type = ARG_ANYTHING,
1444 .arg3_type = ARG_PTR_TO_MEM,
1445 .arg4_type = ARG_CONST_SIZE,
1446 .arg5_type = ARG_ANYTHING,
1447 };
1448
1449 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1450 void *, to, u32, len)
1451 {
1452 void *ptr;
1453
1454 if (unlikely(offset > 0xffff))
1455 goto err_clear;
1456
1457 ptr = skb_header_pointer(skb, offset, len, to);
1458 if (unlikely(!ptr))
1459 goto err_clear;
1460 if (ptr != to)
1461 memcpy(to, ptr, len);
1462
1463 return 0;
1464 err_clear:
1465 memset(to, 0, len);
1466 return -EFAULT;
1467 }
1468
1469 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1470 .func = bpf_skb_load_bytes,
1471 .gpl_only = false,
1472 .ret_type = RET_INTEGER,
1473 .arg1_type = ARG_PTR_TO_CTX,
1474 .arg2_type = ARG_ANYTHING,
1475 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1476 .arg4_type = ARG_CONST_SIZE,
1477 };
1478
1479 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1480 {
1481 /* Idea is the following: should the needed direct read/write
1482 * test fail during runtime, we can pull in more data and redo
1483 * again, since implicitly, we invalidate previous checks here.
1484 *
1485 * Or, since we know how much we need to make read/writeable,
1486 * this can be done once at the program beginning for direct
1487 * access case. By this we overcome limitations of only current
1488 * headroom being accessible.
1489 */
1490 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1491 }
1492
1493 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1494 .func = bpf_skb_pull_data,
1495 .gpl_only = false,
1496 .ret_type = RET_INTEGER,
1497 .arg1_type = ARG_PTR_TO_CTX,
1498 .arg2_type = ARG_ANYTHING,
1499 };
1500
1501 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1502 u64, from, u64, to, u64, flags)
1503 {
1504 __sum16 *ptr;
1505
1506 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1507 return -EINVAL;
1508 if (unlikely(offset > 0xffff || offset & 1))
1509 return -EFAULT;
1510 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1511 return -EFAULT;
1512
1513 ptr = (__sum16 *)(skb->data + offset);
1514 switch (flags & BPF_F_HDR_FIELD_MASK) {
1515 case 0:
1516 if (unlikely(from != 0))
1517 return -EINVAL;
1518
1519 csum_replace_by_diff(ptr, to);
1520 break;
1521 case 2:
1522 csum_replace2(ptr, from, to);
1523 break;
1524 case 4:
1525 csum_replace4(ptr, from, to);
1526 break;
1527 default:
1528 return -EINVAL;
1529 }
1530
1531 return 0;
1532 }
1533
1534 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1535 .func = bpf_l3_csum_replace,
1536 .gpl_only = false,
1537 .ret_type = RET_INTEGER,
1538 .arg1_type = ARG_PTR_TO_CTX,
1539 .arg2_type = ARG_ANYTHING,
1540 .arg3_type = ARG_ANYTHING,
1541 .arg4_type = ARG_ANYTHING,
1542 .arg5_type = ARG_ANYTHING,
1543 };
1544
1545 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1546 u64, from, u64, to, u64, flags)
1547 {
1548 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1549 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1550 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1551 __sum16 *ptr;
1552
1553 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1554 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1555 return -EINVAL;
1556 if (unlikely(offset > 0xffff || offset & 1))
1557 return -EFAULT;
1558 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1559 return -EFAULT;
1560
1561 ptr = (__sum16 *)(skb->data + offset);
1562 if (is_mmzero && !do_mforce && !*ptr)
1563 return 0;
1564
1565 switch (flags & BPF_F_HDR_FIELD_MASK) {
1566 case 0:
1567 if (unlikely(from != 0))
1568 return -EINVAL;
1569
1570 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1571 break;
1572 case 2:
1573 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1574 break;
1575 case 4:
1576 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1577 break;
1578 default:
1579 return -EINVAL;
1580 }
1581
1582 if (is_mmzero && !*ptr)
1583 *ptr = CSUM_MANGLED_0;
1584 return 0;
1585 }
1586
1587 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1588 .func = bpf_l4_csum_replace,
1589 .gpl_only = false,
1590 .ret_type = RET_INTEGER,
1591 .arg1_type = ARG_PTR_TO_CTX,
1592 .arg2_type = ARG_ANYTHING,
1593 .arg3_type = ARG_ANYTHING,
1594 .arg4_type = ARG_ANYTHING,
1595 .arg5_type = ARG_ANYTHING,
1596 };
1597
1598 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1599 __be32 *, to, u32, to_size, __wsum, seed)
1600 {
1601 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1602 u32 diff_size = from_size + to_size;
1603 int i, j = 0;
1604
1605 /* This is quite flexible, some examples:
1606 *
1607 * from_size == 0, to_size > 0, seed := csum --> pushing data
1608 * from_size > 0, to_size == 0, seed := csum --> pulling data
1609 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1610 *
1611 * Even for diffing, from_size and to_size don't need to be equal.
1612 */
1613 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1614 diff_size > sizeof(sp->diff)))
1615 return -EINVAL;
1616
1617 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1618 sp->diff[j] = ~from[i];
1619 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1620 sp->diff[j] = to[i];
1621
1622 return csum_partial(sp->diff, diff_size, seed);
1623 }
1624
1625 static const struct bpf_func_proto bpf_csum_diff_proto = {
1626 .func = bpf_csum_diff,
1627 .gpl_only = false,
1628 .pkt_access = true,
1629 .ret_type = RET_INTEGER,
1630 .arg1_type = ARG_PTR_TO_MEM,
1631 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1632 .arg3_type = ARG_PTR_TO_MEM,
1633 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1634 .arg5_type = ARG_ANYTHING,
1635 };
1636
1637 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1638 {
1639 /* The interface is to be used in combination with bpf_csum_diff()
1640 * for direct packet writes. csum rotation for alignment as well
1641 * as emulating csum_sub() can be done from the eBPF program.
1642 */
1643 if (skb->ip_summed == CHECKSUM_COMPLETE)
1644 return (skb->csum = csum_add(skb->csum, csum));
1645
1646 return -ENOTSUPP;
1647 }
1648
1649 static const struct bpf_func_proto bpf_csum_update_proto = {
1650 .func = bpf_csum_update,
1651 .gpl_only = false,
1652 .ret_type = RET_INTEGER,
1653 .arg1_type = ARG_PTR_TO_CTX,
1654 .arg2_type = ARG_ANYTHING,
1655 };
1656
1657 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1658 {
1659 return dev_forward_skb(dev, skb);
1660 }
1661
1662 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1663 struct sk_buff *skb)
1664 {
1665 int ret = ____dev_forward_skb(dev, skb);
1666
1667 if (likely(!ret)) {
1668 skb->dev = dev;
1669 ret = netif_rx(skb);
1670 }
1671
1672 return ret;
1673 }
1674
1675 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1676 {
1677 int ret;
1678
1679 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1680 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1681 kfree_skb(skb);
1682 return -ENETDOWN;
1683 }
1684
1685 skb->dev = dev;
1686
1687 __this_cpu_inc(xmit_recursion);
1688 ret = dev_queue_xmit(skb);
1689 __this_cpu_dec(xmit_recursion);
1690
1691 return ret;
1692 }
1693
1694 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1695 u32 flags)
1696 {
1697 /* skb->mac_len is not set on normal egress */
1698 unsigned int mlen = skb->network_header - skb->mac_header;
1699
1700 __skb_pull(skb, mlen);
1701
1702 /* At ingress, the mac header has already been pulled once.
1703 * At egress, skb_pospull_rcsum has to be done in case that
1704 * the skb is originated from ingress (i.e. a forwarded skb)
1705 * to ensure that rcsum starts at net header.
1706 */
1707 if (!skb_at_tc_ingress(skb))
1708 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1709 skb_pop_mac_header(skb);
1710 skb_reset_mac_len(skb);
1711 return flags & BPF_F_INGRESS ?
1712 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1713 }
1714
1715 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1716 u32 flags)
1717 {
1718 /* Verify that a link layer header is carried */
1719 if (unlikely(skb->mac_header >= skb->network_header)) {
1720 kfree_skb(skb);
1721 return -ERANGE;
1722 }
1723
1724 bpf_push_mac_rcsum(skb);
1725 return flags & BPF_F_INGRESS ?
1726 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1727 }
1728
1729 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1730 u32 flags)
1731 {
1732 if (dev_is_mac_header_xmit(dev))
1733 return __bpf_redirect_common(skb, dev, flags);
1734 else
1735 return __bpf_redirect_no_mac(skb, dev, flags);
1736 }
1737
1738 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
1739 {
1740 struct net_device *dev;
1741 struct sk_buff *clone;
1742 int ret;
1743
1744 if (unlikely(flags & ~(BPF_F_INGRESS)))
1745 return -EINVAL;
1746
1747 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1748 if (unlikely(!dev))
1749 return -EINVAL;
1750
1751 clone = skb_clone(skb, GFP_ATOMIC);
1752 if (unlikely(!clone))
1753 return -ENOMEM;
1754
1755 /* For direct write, we need to keep the invariant that the skbs
1756 * we're dealing with need to be uncloned. Should uncloning fail
1757 * here, we need to free the just generated clone to unclone once
1758 * again.
1759 */
1760 ret = bpf_try_make_head_writable(skb);
1761 if (unlikely(ret)) {
1762 kfree_skb(clone);
1763 return -ENOMEM;
1764 }
1765
1766 return __bpf_redirect(clone, dev, flags);
1767 }
1768
1769 static const struct bpf_func_proto bpf_clone_redirect_proto = {
1770 .func = bpf_clone_redirect,
1771 .gpl_only = false,
1772 .ret_type = RET_INTEGER,
1773 .arg1_type = ARG_PTR_TO_CTX,
1774 .arg2_type = ARG_ANYTHING,
1775 .arg3_type = ARG_ANYTHING,
1776 };
1777
1778 struct redirect_info {
1779 u32 ifindex;
1780 u32 flags;
1781 };
1782
1783 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
1784
1785 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
1786 {
1787 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1788
1789 if (unlikely(flags & ~(BPF_F_INGRESS)))
1790 return TC_ACT_SHOT;
1791
1792 ri->ifindex = ifindex;
1793 ri->flags = flags;
1794
1795 return TC_ACT_REDIRECT;
1796 }
1797
1798 int skb_do_redirect(struct sk_buff *skb)
1799 {
1800 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1801 struct net_device *dev;
1802
1803 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1804 ri->ifindex = 0;
1805 if (unlikely(!dev)) {
1806 kfree_skb(skb);
1807 return -EINVAL;
1808 }
1809
1810 return __bpf_redirect(skb, dev, ri->flags);
1811 }
1812
1813 static const struct bpf_func_proto bpf_redirect_proto = {
1814 .func = bpf_redirect,
1815 .gpl_only = false,
1816 .ret_type = RET_INTEGER,
1817 .arg1_type = ARG_ANYTHING,
1818 .arg2_type = ARG_ANYTHING,
1819 };
1820
1821 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
1822 {
1823 return task_get_classid(skb);
1824 }
1825
1826 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1827 .func = bpf_get_cgroup_classid,
1828 .gpl_only = false,
1829 .ret_type = RET_INTEGER,
1830 .arg1_type = ARG_PTR_TO_CTX,
1831 };
1832
1833 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
1834 {
1835 return dst_tclassid(skb);
1836 }
1837
1838 static const struct bpf_func_proto bpf_get_route_realm_proto = {
1839 .func = bpf_get_route_realm,
1840 .gpl_only = false,
1841 .ret_type = RET_INTEGER,
1842 .arg1_type = ARG_PTR_TO_CTX,
1843 };
1844
1845 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
1846 {
1847 /* If skb_clear_hash() was called due to mangling, we can
1848 * trigger SW recalculation here. Later access to hash
1849 * can then use the inline skb->hash via context directly
1850 * instead of calling this helper again.
1851 */
1852 return skb_get_hash(skb);
1853 }
1854
1855 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1856 .func = bpf_get_hash_recalc,
1857 .gpl_only = false,
1858 .ret_type = RET_INTEGER,
1859 .arg1_type = ARG_PTR_TO_CTX,
1860 };
1861
1862 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1863 {
1864 /* After all direct packet write, this can be used once for
1865 * triggering a lazy recalc on next skb_get_hash() invocation.
1866 */
1867 skb_clear_hash(skb);
1868 return 0;
1869 }
1870
1871 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1872 .func = bpf_set_hash_invalid,
1873 .gpl_only = false,
1874 .ret_type = RET_INTEGER,
1875 .arg1_type = ARG_PTR_TO_CTX,
1876 };
1877
1878 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
1879 {
1880 /* Set user specified hash as L4(+), so that it gets returned
1881 * on skb_get_hash() call unless BPF prog later on triggers a
1882 * skb_clear_hash().
1883 */
1884 __skb_set_sw_hash(skb, hash, true);
1885 return 0;
1886 }
1887
1888 static const struct bpf_func_proto bpf_set_hash_proto = {
1889 .func = bpf_set_hash,
1890 .gpl_only = false,
1891 .ret_type = RET_INTEGER,
1892 .arg1_type = ARG_PTR_TO_CTX,
1893 .arg2_type = ARG_ANYTHING,
1894 };
1895
1896 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1897 u16, vlan_tci)
1898 {
1899 int ret;
1900
1901 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1902 vlan_proto != htons(ETH_P_8021AD)))
1903 vlan_proto = htons(ETH_P_8021Q);
1904
1905 bpf_push_mac_rcsum(skb);
1906 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1907 bpf_pull_mac_rcsum(skb);
1908
1909 bpf_compute_data_end(skb);
1910 return ret;
1911 }
1912
1913 const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1914 .func = bpf_skb_vlan_push,
1915 .gpl_only = false,
1916 .ret_type = RET_INTEGER,
1917 .arg1_type = ARG_PTR_TO_CTX,
1918 .arg2_type = ARG_ANYTHING,
1919 .arg3_type = ARG_ANYTHING,
1920 };
1921 EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
1922
1923 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
1924 {
1925 int ret;
1926
1927 bpf_push_mac_rcsum(skb);
1928 ret = skb_vlan_pop(skb);
1929 bpf_pull_mac_rcsum(skb);
1930
1931 bpf_compute_data_end(skb);
1932 return ret;
1933 }
1934
1935 const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1936 .func = bpf_skb_vlan_pop,
1937 .gpl_only = false,
1938 .ret_type = RET_INTEGER,
1939 .arg1_type = ARG_PTR_TO_CTX,
1940 };
1941 EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
1942
1943 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1944 {
1945 /* Caller already did skb_cow() with len as headroom,
1946 * so no need to do it here.
1947 */
1948 skb_push(skb, len);
1949 memmove(skb->data, skb->data + len, off);
1950 memset(skb->data + off, 0, len);
1951
1952 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1953 * needed here as it does not change the skb->csum
1954 * result for checksum complete when summing over
1955 * zeroed blocks.
1956 */
1957 return 0;
1958 }
1959
1960 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1961 {
1962 /* skb_ensure_writable() is not needed here, as we're
1963 * already working on an uncloned skb.
1964 */
1965 if (unlikely(!pskb_may_pull(skb, off + len)))
1966 return -ENOMEM;
1967
1968 skb_postpull_rcsum(skb, skb->data + off, len);
1969 memmove(skb->data + len, skb->data, off);
1970 __skb_pull(skb, len);
1971
1972 return 0;
1973 }
1974
1975 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1976 {
1977 bool trans_same = skb->transport_header == skb->network_header;
1978 int ret;
1979
1980 /* There's no need for __skb_push()/__skb_pull() pair to
1981 * get to the start of the mac header as we're guaranteed
1982 * to always start from here under eBPF.
1983 */
1984 ret = bpf_skb_generic_push(skb, off, len);
1985 if (likely(!ret)) {
1986 skb->mac_header -= len;
1987 skb->network_header -= len;
1988 if (trans_same)
1989 skb->transport_header = skb->network_header;
1990 }
1991
1992 return ret;
1993 }
1994
1995 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1996 {
1997 bool trans_same = skb->transport_header == skb->network_header;
1998 int ret;
1999
2000 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2001 ret = bpf_skb_generic_pop(skb, off, len);
2002 if (likely(!ret)) {
2003 skb->mac_header += len;
2004 skb->network_header += len;
2005 if (trans_same)
2006 skb->transport_header = skb->network_header;
2007 }
2008
2009 return ret;
2010 }
2011
2012 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2013 {
2014 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2015 u32 off = skb_mac_header_len(skb);
2016 int ret;
2017
2018 ret = skb_cow(skb, len_diff);
2019 if (unlikely(ret < 0))
2020 return ret;
2021
2022 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2023 if (unlikely(ret < 0))
2024 return ret;
2025
2026 if (skb_is_gso(skb)) {
2027 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
2028 * be changed into SKB_GSO_TCPV6.
2029 */
2030 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2031 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
2032 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
2033 }
2034
2035 /* Due to IPv6 header, MSS needs to be downgraded. */
2036 skb_shinfo(skb)->gso_size -= len_diff;
2037 /* Header must be checked, and gso_segs recomputed. */
2038 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2039 skb_shinfo(skb)->gso_segs = 0;
2040 }
2041
2042 skb->protocol = htons(ETH_P_IPV6);
2043 skb_clear_hash(skb);
2044
2045 return 0;
2046 }
2047
2048 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2049 {
2050 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2051 u32 off = skb_mac_header_len(skb);
2052 int ret;
2053
2054 ret = skb_unclone(skb, GFP_ATOMIC);
2055 if (unlikely(ret < 0))
2056 return ret;
2057
2058 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2059 if (unlikely(ret < 0))
2060 return ret;
2061
2062 if (skb_is_gso(skb)) {
2063 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
2064 * be changed into SKB_GSO_TCPV4.
2065 */
2066 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2067 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2068 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2069 }
2070
2071 /* Due to IPv4 header, MSS can be upgraded. */
2072 skb_shinfo(skb)->gso_size += len_diff;
2073 /* Header must be checked, and gso_segs recomputed. */
2074 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2075 skb_shinfo(skb)->gso_segs = 0;
2076 }
2077
2078 skb->protocol = htons(ETH_P_IP);
2079 skb_clear_hash(skb);
2080
2081 return 0;
2082 }
2083
2084 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2085 {
2086 __be16 from_proto = skb->protocol;
2087
2088 if (from_proto == htons(ETH_P_IP) &&
2089 to_proto == htons(ETH_P_IPV6))
2090 return bpf_skb_proto_4_to_6(skb);
2091
2092 if (from_proto == htons(ETH_P_IPV6) &&
2093 to_proto == htons(ETH_P_IP))
2094 return bpf_skb_proto_6_to_4(skb);
2095
2096 return -ENOTSUPP;
2097 }
2098
2099 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2100 u64, flags)
2101 {
2102 int ret;
2103
2104 if (unlikely(flags))
2105 return -EINVAL;
2106
2107 /* General idea is that this helper does the basic groundwork
2108 * needed for changing the protocol, and eBPF program fills the
2109 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2110 * and other helpers, rather than passing a raw buffer here.
2111 *
2112 * The rationale is to keep this minimal and without a need to
2113 * deal with raw packet data. F.e. even if we would pass buffers
2114 * here, the program still needs to call the bpf_lX_csum_replace()
2115 * helpers anyway. Plus, this way we keep also separation of
2116 * concerns, since f.e. bpf_skb_store_bytes() should only take
2117 * care of stores.
2118 *
2119 * Currently, additional options and extension header space are
2120 * not supported, but flags register is reserved so we can adapt
2121 * that. For offloads, we mark packet as dodgy, so that headers
2122 * need to be verified first.
2123 */
2124 ret = bpf_skb_proto_xlat(skb, proto);
2125 bpf_compute_data_end(skb);
2126 return ret;
2127 }
2128
2129 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2130 .func = bpf_skb_change_proto,
2131 .gpl_only = false,
2132 .ret_type = RET_INTEGER,
2133 .arg1_type = ARG_PTR_TO_CTX,
2134 .arg2_type = ARG_ANYTHING,
2135 .arg3_type = ARG_ANYTHING,
2136 };
2137
2138 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2139 {
2140 /* We only allow a restricted subset to be changed for now. */
2141 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2142 !skb_pkt_type_ok(pkt_type)))
2143 return -EINVAL;
2144
2145 skb->pkt_type = pkt_type;
2146 return 0;
2147 }
2148
2149 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2150 .func = bpf_skb_change_type,
2151 .gpl_only = false,
2152 .ret_type = RET_INTEGER,
2153 .arg1_type = ARG_PTR_TO_CTX,
2154 .arg2_type = ARG_ANYTHING,
2155 };
2156
2157 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2158 {
2159 switch (skb->protocol) {
2160 case htons(ETH_P_IP):
2161 return sizeof(struct iphdr);
2162 case htons(ETH_P_IPV6):
2163 return sizeof(struct ipv6hdr);
2164 default:
2165 return ~0U;
2166 }
2167 }
2168
2169 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2170 {
2171 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2172 int ret;
2173
2174 ret = skb_cow(skb, len_diff);
2175 if (unlikely(ret < 0))
2176 return ret;
2177
2178 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2179 if (unlikely(ret < 0))
2180 return ret;
2181
2182 if (skb_is_gso(skb)) {
2183 /* Due to header grow, MSS needs to be downgraded. */
2184 skb_shinfo(skb)->gso_size -= len_diff;
2185 /* Header must be checked, and gso_segs recomputed. */
2186 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2187 skb_shinfo(skb)->gso_segs = 0;
2188 }
2189
2190 return 0;
2191 }
2192
2193 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2194 {
2195 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2196 int ret;
2197
2198 ret = skb_unclone(skb, GFP_ATOMIC);
2199 if (unlikely(ret < 0))
2200 return ret;
2201
2202 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2203 if (unlikely(ret < 0))
2204 return ret;
2205
2206 if (skb_is_gso(skb)) {
2207 /* Due to header shrink, MSS can be upgraded. */
2208 skb_shinfo(skb)->gso_size += len_diff;
2209 /* Header must be checked, and gso_segs recomputed. */
2210 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2211 skb_shinfo(skb)->gso_segs = 0;
2212 }
2213
2214 return 0;
2215 }
2216
2217 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2218 {
2219 return skb->dev->mtu + skb->dev->hard_header_len;
2220 }
2221
2222 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2223 {
2224 bool trans_same = skb->transport_header == skb->network_header;
2225 u32 len_cur, len_diff_abs = abs(len_diff);
2226 u32 len_min = bpf_skb_net_base_len(skb);
2227 u32 len_max = __bpf_skb_max_len(skb);
2228 __be16 proto = skb->protocol;
2229 bool shrink = len_diff < 0;
2230 int ret;
2231
2232 if (unlikely(len_diff_abs > 0xfffU))
2233 return -EFAULT;
2234 if (unlikely(proto != htons(ETH_P_IP) &&
2235 proto != htons(ETH_P_IPV6)))
2236 return -ENOTSUPP;
2237
2238 len_cur = skb->len - skb_network_offset(skb);
2239 if (skb_transport_header_was_set(skb) && !trans_same)
2240 len_cur = skb_network_header_len(skb);
2241 if ((shrink && (len_diff_abs >= len_cur ||
2242 len_cur - len_diff_abs < len_min)) ||
2243 (!shrink && (skb->len + len_diff_abs > len_max &&
2244 !skb_is_gso(skb))))
2245 return -ENOTSUPP;
2246
2247 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2248 bpf_skb_net_grow(skb, len_diff_abs);
2249
2250 bpf_compute_data_end(skb);
2251 return ret;
2252 }
2253
2254 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2255 u32, mode, u64, flags)
2256 {
2257 if (unlikely(flags))
2258 return -EINVAL;
2259 if (likely(mode == BPF_ADJ_ROOM_NET))
2260 return bpf_skb_adjust_net(skb, len_diff);
2261
2262 return -ENOTSUPP;
2263 }
2264
2265 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2266 .func = bpf_skb_adjust_room,
2267 .gpl_only = false,
2268 .ret_type = RET_INTEGER,
2269 .arg1_type = ARG_PTR_TO_CTX,
2270 .arg2_type = ARG_ANYTHING,
2271 .arg3_type = ARG_ANYTHING,
2272 .arg4_type = ARG_ANYTHING,
2273 };
2274
2275 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2276 {
2277 u32 min_len = skb_network_offset(skb);
2278
2279 if (skb_transport_header_was_set(skb))
2280 min_len = skb_transport_offset(skb);
2281 if (skb->ip_summed == CHECKSUM_PARTIAL)
2282 min_len = skb_checksum_start_offset(skb) +
2283 skb->csum_offset + sizeof(__sum16);
2284 return min_len;
2285 }
2286
2287 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2288 {
2289 unsigned int old_len = skb->len;
2290 int ret;
2291
2292 ret = __skb_grow_rcsum(skb, new_len);
2293 if (!ret)
2294 memset(skb->data + old_len, 0, new_len - old_len);
2295 return ret;
2296 }
2297
2298 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2299 {
2300 return __skb_trim_rcsum(skb, new_len);
2301 }
2302
2303 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2304 u64, flags)
2305 {
2306 u32 max_len = __bpf_skb_max_len(skb);
2307 u32 min_len = __bpf_skb_min_len(skb);
2308 int ret;
2309
2310 if (unlikely(flags || new_len > max_len || new_len < min_len))
2311 return -EINVAL;
2312 if (skb->encapsulation)
2313 return -ENOTSUPP;
2314
2315 /* The basic idea of this helper is that it's performing the
2316 * needed work to either grow or trim an skb, and eBPF program
2317 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2318 * bpf_lX_csum_replace() and others rather than passing a raw
2319 * buffer here. This one is a slow path helper and intended
2320 * for replies with control messages.
2321 *
2322 * Like in bpf_skb_change_proto(), we want to keep this rather
2323 * minimal and without protocol specifics so that we are able
2324 * to separate concerns as in bpf_skb_store_bytes() should only
2325 * be the one responsible for writing buffers.
2326 *
2327 * It's really expected to be a slow path operation here for
2328 * control message replies, so we're implicitly linearizing,
2329 * uncloning and drop offloads from the skb by this.
2330 */
2331 ret = __bpf_try_make_writable(skb, skb->len);
2332 if (!ret) {
2333 if (new_len > skb->len)
2334 ret = bpf_skb_grow_rcsum(skb, new_len);
2335 else if (new_len < skb->len)
2336 ret = bpf_skb_trim_rcsum(skb, new_len);
2337 if (!ret && skb_is_gso(skb))
2338 skb_gso_reset(skb);
2339 }
2340
2341 bpf_compute_data_end(skb);
2342 return ret;
2343 }
2344
2345 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2346 .func = bpf_skb_change_tail,
2347 .gpl_only = false,
2348 .ret_type = RET_INTEGER,
2349 .arg1_type = ARG_PTR_TO_CTX,
2350 .arg2_type = ARG_ANYTHING,
2351 .arg3_type = ARG_ANYTHING,
2352 };
2353
2354 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
2355 u64, flags)
2356 {
2357 u32 max_len = __bpf_skb_max_len(skb);
2358 u32 new_len = skb->len + head_room;
2359 int ret;
2360
2361 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2362 new_len < skb->len))
2363 return -EINVAL;
2364
2365 ret = skb_cow(skb, head_room);
2366 if (likely(!ret)) {
2367 /* Idea for this helper is that we currently only
2368 * allow to expand on mac header. This means that
2369 * skb->protocol network header, etc, stay as is.
2370 * Compared to bpf_skb_change_tail(), we're more
2371 * flexible due to not needing to linearize or
2372 * reset GSO. Intention for this helper is to be
2373 * used by an L3 skb that needs to push mac header
2374 * for redirection into L2 device.
2375 */
2376 __skb_push(skb, head_room);
2377 memset(skb->data, 0, head_room);
2378 skb_reset_mac_header(skb);
2379 }
2380
2381 bpf_compute_data_end(skb);
2382 return 0;
2383 }
2384
2385 static const struct bpf_func_proto bpf_skb_change_head_proto = {
2386 .func = bpf_skb_change_head,
2387 .gpl_only = false,
2388 .ret_type = RET_INTEGER,
2389 .arg1_type = ARG_PTR_TO_CTX,
2390 .arg2_type = ARG_ANYTHING,
2391 .arg3_type = ARG_ANYTHING,
2392 };
2393
2394 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
2395 {
2396 void *data = xdp->data + offset;
2397
2398 if (unlikely(data < xdp->data_hard_start ||
2399 data > xdp->data_end - ETH_HLEN))
2400 return -EINVAL;
2401
2402 xdp->data = data;
2403
2404 return 0;
2405 }
2406
2407 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
2408 .func = bpf_xdp_adjust_head,
2409 .gpl_only = false,
2410 .ret_type = RET_INTEGER,
2411 .arg1_type = ARG_PTR_TO_CTX,
2412 .arg2_type = ARG_ANYTHING,
2413 };
2414
2415 bool bpf_helper_changes_pkt_data(void *func)
2416 {
2417 if (func == bpf_skb_vlan_push ||
2418 func == bpf_skb_vlan_pop ||
2419 func == bpf_skb_store_bytes ||
2420 func == bpf_skb_change_proto ||
2421 func == bpf_skb_change_head ||
2422 func == bpf_skb_change_tail ||
2423 func == bpf_skb_adjust_room ||
2424 func == bpf_skb_pull_data ||
2425 func == bpf_clone_redirect ||
2426 func == bpf_l3_csum_replace ||
2427 func == bpf_l4_csum_replace ||
2428 func == bpf_xdp_adjust_head)
2429 return true;
2430
2431 return false;
2432 }
2433
2434 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
2435 unsigned long off, unsigned long len)
2436 {
2437 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
2438
2439 if (unlikely(!ptr))
2440 return len;
2441 if (ptr != dst_buff)
2442 memcpy(dst_buff, ptr, len);
2443
2444 return 0;
2445 }
2446
2447 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2448 u64, flags, void *, meta, u64, meta_size)
2449 {
2450 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2451
2452 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2453 return -EINVAL;
2454 if (unlikely(skb_size > skb->len))
2455 return -EFAULT;
2456
2457 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2458 bpf_skb_copy);
2459 }
2460
2461 static const struct bpf_func_proto bpf_skb_event_output_proto = {
2462 .func = bpf_skb_event_output,
2463 .gpl_only = true,
2464 .ret_type = RET_INTEGER,
2465 .arg1_type = ARG_PTR_TO_CTX,
2466 .arg2_type = ARG_CONST_MAP_PTR,
2467 .arg3_type = ARG_ANYTHING,
2468 .arg4_type = ARG_PTR_TO_MEM,
2469 .arg5_type = ARG_CONST_SIZE,
2470 };
2471
2472 static unsigned short bpf_tunnel_key_af(u64 flags)
2473 {
2474 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2475 }
2476
2477 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2478 u32, size, u64, flags)
2479 {
2480 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2481 u8 compat[sizeof(struct bpf_tunnel_key)];
2482 void *to_orig = to;
2483 int err;
2484
2485 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2486 err = -EINVAL;
2487 goto err_clear;
2488 }
2489 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2490 err = -EPROTO;
2491 goto err_clear;
2492 }
2493 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2494 err = -EINVAL;
2495 switch (size) {
2496 case offsetof(struct bpf_tunnel_key, tunnel_label):
2497 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2498 goto set_compat;
2499 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2500 /* Fixup deprecated structure layouts here, so we have
2501 * a common path later on.
2502 */
2503 if (ip_tunnel_info_af(info) != AF_INET)
2504 goto err_clear;
2505 set_compat:
2506 to = (struct bpf_tunnel_key *)compat;
2507 break;
2508 default:
2509 goto err_clear;
2510 }
2511 }
2512
2513 to->tunnel_id = be64_to_cpu(info->key.tun_id);
2514 to->tunnel_tos = info->key.tos;
2515 to->tunnel_ttl = info->key.ttl;
2516
2517 if (flags & BPF_F_TUNINFO_IPV6) {
2518 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2519 sizeof(to->remote_ipv6));
2520 to->tunnel_label = be32_to_cpu(info->key.label);
2521 } else {
2522 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
2523 }
2524
2525 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
2526 memcpy(to_orig, to, size);
2527
2528 return 0;
2529 err_clear:
2530 memset(to_orig, 0, size);
2531 return err;
2532 }
2533
2534 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
2535 .func = bpf_skb_get_tunnel_key,
2536 .gpl_only = false,
2537 .ret_type = RET_INTEGER,
2538 .arg1_type = ARG_PTR_TO_CTX,
2539 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2540 .arg3_type = ARG_CONST_SIZE,
2541 .arg4_type = ARG_ANYTHING,
2542 };
2543
2544 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
2545 {
2546 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2547 int err;
2548
2549 if (unlikely(!info ||
2550 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2551 err = -ENOENT;
2552 goto err_clear;
2553 }
2554 if (unlikely(size < info->options_len)) {
2555 err = -ENOMEM;
2556 goto err_clear;
2557 }
2558
2559 ip_tunnel_info_opts_get(to, info);
2560 if (size > info->options_len)
2561 memset(to + info->options_len, 0, size - info->options_len);
2562
2563 return info->options_len;
2564 err_clear:
2565 memset(to, 0, size);
2566 return err;
2567 }
2568
2569 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2570 .func = bpf_skb_get_tunnel_opt,
2571 .gpl_only = false,
2572 .ret_type = RET_INTEGER,
2573 .arg1_type = ARG_PTR_TO_CTX,
2574 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
2575 .arg3_type = ARG_CONST_SIZE,
2576 };
2577
2578 static struct metadata_dst __percpu *md_dst;
2579
2580 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2581 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
2582 {
2583 struct metadata_dst *md = this_cpu_ptr(md_dst);
2584 u8 compat[sizeof(struct bpf_tunnel_key)];
2585 struct ip_tunnel_info *info;
2586
2587 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2588 BPF_F_DONT_FRAGMENT)))
2589 return -EINVAL;
2590 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2591 switch (size) {
2592 case offsetof(struct bpf_tunnel_key, tunnel_label):
2593 case offsetof(struct bpf_tunnel_key, tunnel_ext):
2594 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2595 /* Fixup deprecated structure layouts here, so we have
2596 * a common path later on.
2597 */
2598 memcpy(compat, from, size);
2599 memset(compat + size, 0, sizeof(compat) - size);
2600 from = (const struct bpf_tunnel_key *) compat;
2601 break;
2602 default:
2603 return -EINVAL;
2604 }
2605 }
2606 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2607 from->tunnel_ext))
2608 return -EINVAL;
2609
2610 skb_dst_drop(skb);
2611 dst_hold((struct dst_entry *) md);
2612 skb_dst_set(skb, (struct dst_entry *) md);
2613
2614 info = &md->u.tun_info;
2615 info->mode = IP_TUNNEL_INFO_TX;
2616
2617 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
2618 if (flags & BPF_F_DONT_FRAGMENT)
2619 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2620
2621 info->key.tun_id = cpu_to_be64(from->tunnel_id);
2622 info->key.tos = from->tunnel_tos;
2623 info->key.ttl = from->tunnel_ttl;
2624
2625 if (flags & BPF_F_TUNINFO_IPV6) {
2626 info->mode |= IP_TUNNEL_INFO_IPV6;
2627 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2628 sizeof(from->remote_ipv6));
2629 info->key.label = cpu_to_be32(from->tunnel_label) &
2630 IPV6_FLOWLABEL_MASK;
2631 } else {
2632 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
2633 if (flags & BPF_F_ZERO_CSUM_TX)
2634 info->key.tun_flags &= ~TUNNEL_CSUM;
2635 }
2636
2637 return 0;
2638 }
2639
2640 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
2641 .func = bpf_skb_set_tunnel_key,
2642 .gpl_only = false,
2643 .ret_type = RET_INTEGER,
2644 .arg1_type = ARG_PTR_TO_CTX,
2645 .arg2_type = ARG_PTR_TO_MEM,
2646 .arg3_type = ARG_CONST_SIZE,
2647 .arg4_type = ARG_ANYTHING,
2648 };
2649
2650 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2651 const u8 *, from, u32, size)
2652 {
2653 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2654 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2655
2656 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2657 return -EINVAL;
2658 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
2659 return -ENOMEM;
2660
2661 ip_tunnel_info_opts_set(info, from, size);
2662
2663 return 0;
2664 }
2665
2666 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2667 .func = bpf_skb_set_tunnel_opt,
2668 .gpl_only = false,
2669 .ret_type = RET_INTEGER,
2670 .arg1_type = ARG_PTR_TO_CTX,
2671 .arg2_type = ARG_PTR_TO_MEM,
2672 .arg3_type = ARG_CONST_SIZE,
2673 };
2674
2675 static const struct bpf_func_proto *
2676 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
2677 {
2678 if (!md_dst) {
2679 /* Race is not possible, since it's called from verifier
2680 * that is holding verifier mutex.
2681 */
2682 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
2683 METADATA_IP_TUNNEL,
2684 GFP_KERNEL);
2685 if (!md_dst)
2686 return NULL;
2687 }
2688
2689 switch (which) {
2690 case BPF_FUNC_skb_set_tunnel_key:
2691 return &bpf_skb_set_tunnel_key_proto;
2692 case BPF_FUNC_skb_set_tunnel_opt:
2693 return &bpf_skb_set_tunnel_opt_proto;
2694 default:
2695 return NULL;
2696 }
2697 }
2698
2699 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2700 u32, idx)
2701 {
2702 struct bpf_array *array = container_of(map, struct bpf_array, map);
2703 struct cgroup *cgrp;
2704 struct sock *sk;
2705
2706 sk = skb_to_full_sk(skb);
2707 if (!sk || !sk_fullsock(sk))
2708 return -ENOENT;
2709 if (unlikely(idx >= array->map.max_entries))
2710 return -E2BIG;
2711
2712 cgrp = READ_ONCE(array->ptrs[idx]);
2713 if (unlikely(!cgrp))
2714 return -EAGAIN;
2715
2716 return sk_under_cgroup_hierarchy(sk, cgrp);
2717 }
2718
2719 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2720 .func = bpf_skb_under_cgroup,
2721 .gpl_only = false,
2722 .ret_type = RET_INTEGER,
2723 .arg1_type = ARG_PTR_TO_CTX,
2724 .arg2_type = ARG_CONST_MAP_PTR,
2725 .arg3_type = ARG_ANYTHING,
2726 };
2727
2728 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2729 unsigned long off, unsigned long len)
2730 {
2731 memcpy(dst_buff, src_buff + off, len);
2732 return 0;
2733 }
2734
2735 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2736 u64, flags, void *, meta, u64, meta_size)
2737 {
2738 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2739
2740 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2741 return -EINVAL;
2742 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2743 return -EFAULT;
2744
2745 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
2746 xdp_size, bpf_xdp_copy);
2747 }
2748
2749 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2750 .func = bpf_xdp_event_output,
2751 .gpl_only = true,
2752 .ret_type = RET_INTEGER,
2753 .arg1_type = ARG_PTR_TO_CTX,
2754 .arg2_type = ARG_CONST_MAP_PTR,
2755 .arg3_type = ARG_ANYTHING,
2756 .arg4_type = ARG_PTR_TO_MEM,
2757 .arg5_type = ARG_CONST_SIZE,
2758 };
2759
2760 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
2761 {
2762 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
2763 }
2764
2765 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
2766 .func = bpf_get_socket_cookie,
2767 .gpl_only = false,
2768 .ret_type = RET_INTEGER,
2769 .arg1_type = ARG_PTR_TO_CTX,
2770 };
2771
2772 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
2773 {
2774 struct sock *sk = sk_to_full_sk(skb->sk);
2775 kuid_t kuid;
2776
2777 if (!sk || !sk_fullsock(sk))
2778 return overflowuid;
2779 kuid = sock_net_uid(sock_net(sk), sk);
2780 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
2781 }
2782
2783 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
2784 .func = bpf_get_socket_uid,
2785 .gpl_only = false,
2786 .ret_type = RET_INTEGER,
2787 .arg1_type = ARG_PTR_TO_CTX,
2788 };
2789
2790 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
2791 int, level, int, optname, char *, optval, int, optlen)
2792 {
2793 struct sock *sk = bpf_sock->sk;
2794 int ret = 0;
2795 int val;
2796
2797 if (!sk_fullsock(sk))
2798 return -EINVAL;
2799
2800 if (level == SOL_SOCKET) {
2801 if (optlen != sizeof(int))
2802 return -EINVAL;
2803 val = *((int *)optval);
2804
2805 /* Only some socketops are supported */
2806 switch (optname) {
2807 case SO_RCVBUF:
2808 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
2809 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
2810 break;
2811 case SO_SNDBUF:
2812 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
2813 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
2814 break;
2815 case SO_MAX_PACING_RATE:
2816 sk->sk_max_pacing_rate = val;
2817 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
2818 sk->sk_max_pacing_rate);
2819 break;
2820 case SO_PRIORITY:
2821 sk->sk_priority = val;
2822 break;
2823 case SO_RCVLOWAT:
2824 if (val < 0)
2825 val = INT_MAX;
2826 sk->sk_rcvlowat = val ? : 1;
2827 break;
2828 case SO_MARK:
2829 sk->sk_mark = val;
2830 break;
2831 default:
2832 ret = -EINVAL;
2833 }
2834 #ifdef CONFIG_INET
2835 } else if (level == SOL_TCP &&
2836 sk->sk_prot->setsockopt == tcp_setsockopt) {
2837 if (optname == TCP_CONGESTION) {
2838 char name[TCP_CA_NAME_MAX];
2839
2840 strncpy(name, optval, min_t(long, optlen,
2841 TCP_CA_NAME_MAX-1));
2842 name[TCP_CA_NAME_MAX-1] = 0;
2843 ret = tcp_set_congestion_control(sk, name, false);
2844 if (!ret && bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN)
2845 /* replacing an existing ca */
2846 tcp_reinit_congestion_control(sk,
2847 inet_csk(sk)->icsk_ca_ops);
2848 } else {
2849 struct tcp_sock *tp = tcp_sk(sk);
2850
2851 if (optlen != sizeof(int))
2852 return -EINVAL;
2853
2854 val = *((int *)optval);
2855 /* Only some options are supported */
2856 switch (optname) {
2857 case TCP_BPF_IW:
2858 if (val <= 0 || tp->data_segs_out > 0)
2859 ret = -EINVAL;
2860 else
2861 tp->snd_cwnd = val;
2862 break;
2863 case TCP_BPF_SNDCWND_CLAMP:
2864 if (val <= 0) {
2865 ret = -EINVAL;
2866 } else {
2867 tp->snd_cwnd_clamp = val;
2868 tp->snd_ssthresh = val;
2869 }
2870 break;
2871 default:
2872 ret = -EINVAL;
2873 }
2874 }
2875 ret = -EINVAL;
2876 #endif
2877 } else {
2878 ret = -EINVAL;
2879 }
2880 return ret;
2881 }
2882
2883 static const struct bpf_func_proto bpf_setsockopt_proto = {
2884 .func = bpf_setsockopt,
2885 .gpl_only = true,
2886 .ret_type = RET_INTEGER,
2887 .arg1_type = ARG_PTR_TO_CTX,
2888 .arg2_type = ARG_ANYTHING,
2889 .arg3_type = ARG_ANYTHING,
2890 .arg4_type = ARG_PTR_TO_MEM,
2891 .arg5_type = ARG_CONST_SIZE,
2892 };
2893
2894 static const struct bpf_func_proto *
2895 bpf_base_func_proto(enum bpf_func_id func_id)
2896 {
2897 switch (func_id) {
2898 case BPF_FUNC_map_lookup_elem:
2899 return &bpf_map_lookup_elem_proto;
2900 case BPF_FUNC_map_update_elem:
2901 return &bpf_map_update_elem_proto;
2902 case BPF_FUNC_map_delete_elem:
2903 return &bpf_map_delete_elem_proto;
2904 case BPF_FUNC_get_prandom_u32:
2905 return &bpf_get_prandom_u32_proto;
2906 case BPF_FUNC_get_smp_processor_id:
2907 return &bpf_get_raw_smp_processor_id_proto;
2908 case BPF_FUNC_get_numa_node_id:
2909 return &bpf_get_numa_node_id_proto;
2910 case BPF_FUNC_tail_call:
2911 return &bpf_tail_call_proto;
2912 case BPF_FUNC_ktime_get_ns:
2913 return &bpf_ktime_get_ns_proto;
2914 case BPF_FUNC_trace_printk:
2915 if (capable(CAP_SYS_ADMIN))
2916 return bpf_get_trace_printk_proto();
2917 default:
2918 return NULL;
2919 }
2920 }
2921
2922 static const struct bpf_func_proto *
2923 sk_filter_func_proto(enum bpf_func_id func_id)
2924 {
2925 switch (func_id) {
2926 case BPF_FUNC_skb_load_bytes:
2927 return &bpf_skb_load_bytes_proto;
2928 case BPF_FUNC_get_socket_cookie:
2929 return &bpf_get_socket_cookie_proto;
2930 case BPF_FUNC_get_socket_uid:
2931 return &bpf_get_socket_uid_proto;
2932 default:
2933 return bpf_base_func_proto(func_id);
2934 }
2935 }
2936
2937 static const struct bpf_func_proto *
2938 tc_cls_act_func_proto(enum bpf_func_id func_id)
2939 {
2940 switch (func_id) {
2941 case BPF_FUNC_skb_store_bytes:
2942 return &bpf_skb_store_bytes_proto;
2943 case BPF_FUNC_skb_load_bytes:
2944 return &bpf_skb_load_bytes_proto;
2945 case BPF_FUNC_skb_pull_data:
2946 return &bpf_skb_pull_data_proto;
2947 case BPF_FUNC_csum_diff:
2948 return &bpf_csum_diff_proto;
2949 case BPF_FUNC_csum_update:
2950 return &bpf_csum_update_proto;
2951 case BPF_FUNC_l3_csum_replace:
2952 return &bpf_l3_csum_replace_proto;
2953 case BPF_FUNC_l4_csum_replace:
2954 return &bpf_l4_csum_replace_proto;
2955 case BPF_FUNC_clone_redirect:
2956 return &bpf_clone_redirect_proto;
2957 case BPF_FUNC_get_cgroup_classid:
2958 return &bpf_get_cgroup_classid_proto;
2959 case BPF_FUNC_skb_vlan_push:
2960 return &bpf_skb_vlan_push_proto;
2961 case BPF_FUNC_skb_vlan_pop:
2962 return &bpf_skb_vlan_pop_proto;
2963 case BPF_FUNC_skb_change_proto:
2964 return &bpf_skb_change_proto_proto;
2965 case BPF_FUNC_skb_change_type:
2966 return &bpf_skb_change_type_proto;
2967 case BPF_FUNC_skb_adjust_room:
2968 return &bpf_skb_adjust_room_proto;
2969 case BPF_FUNC_skb_change_tail:
2970 return &bpf_skb_change_tail_proto;
2971 case BPF_FUNC_skb_get_tunnel_key:
2972 return &bpf_skb_get_tunnel_key_proto;
2973 case BPF_FUNC_skb_set_tunnel_key:
2974 return bpf_get_skb_set_tunnel_proto(func_id);
2975 case BPF_FUNC_skb_get_tunnel_opt:
2976 return &bpf_skb_get_tunnel_opt_proto;
2977 case BPF_FUNC_skb_set_tunnel_opt:
2978 return bpf_get_skb_set_tunnel_proto(func_id);
2979 case BPF_FUNC_redirect:
2980 return &bpf_redirect_proto;
2981 case BPF_FUNC_get_route_realm:
2982 return &bpf_get_route_realm_proto;
2983 case BPF_FUNC_get_hash_recalc:
2984 return &bpf_get_hash_recalc_proto;
2985 case BPF_FUNC_set_hash_invalid:
2986 return &bpf_set_hash_invalid_proto;
2987 case BPF_FUNC_set_hash:
2988 return &bpf_set_hash_proto;
2989 case BPF_FUNC_perf_event_output:
2990 return &bpf_skb_event_output_proto;
2991 case BPF_FUNC_get_smp_processor_id:
2992 return &bpf_get_smp_processor_id_proto;
2993 case BPF_FUNC_skb_under_cgroup:
2994 return &bpf_skb_under_cgroup_proto;
2995 case BPF_FUNC_get_socket_cookie:
2996 return &bpf_get_socket_cookie_proto;
2997 case BPF_FUNC_get_socket_uid:
2998 return &bpf_get_socket_uid_proto;
2999 default:
3000 return bpf_base_func_proto(func_id);
3001 }
3002 }
3003
3004 static const struct bpf_func_proto *
3005 xdp_func_proto(enum bpf_func_id func_id)
3006 {
3007 switch (func_id) {
3008 case BPF_FUNC_perf_event_output:
3009 return &bpf_xdp_event_output_proto;
3010 case BPF_FUNC_get_smp_processor_id:
3011 return &bpf_get_smp_processor_id_proto;
3012 case BPF_FUNC_xdp_adjust_head:
3013 return &bpf_xdp_adjust_head_proto;
3014 default:
3015 return bpf_base_func_proto(func_id);
3016 }
3017 }
3018
3019 static const struct bpf_func_proto *
3020 lwt_inout_func_proto(enum bpf_func_id func_id)
3021 {
3022 switch (func_id) {
3023 case BPF_FUNC_skb_load_bytes:
3024 return &bpf_skb_load_bytes_proto;
3025 case BPF_FUNC_skb_pull_data:
3026 return &bpf_skb_pull_data_proto;
3027 case BPF_FUNC_csum_diff:
3028 return &bpf_csum_diff_proto;
3029 case BPF_FUNC_get_cgroup_classid:
3030 return &bpf_get_cgroup_classid_proto;
3031 case BPF_FUNC_get_route_realm:
3032 return &bpf_get_route_realm_proto;
3033 case BPF_FUNC_get_hash_recalc:
3034 return &bpf_get_hash_recalc_proto;
3035 case BPF_FUNC_perf_event_output:
3036 return &bpf_skb_event_output_proto;
3037 case BPF_FUNC_get_smp_processor_id:
3038 return &bpf_get_smp_processor_id_proto;
3039 case BPF_FUNC_skb_under_cgroup:
3040 return &bpf_skb_under_cgroup_proto;
3041 default:
3042 return bpf_base_func_proto(func_id);
3043 }
3044 }
3045
3046 static const struct bpf_func_proto *
3047 sock_ops_func_proto(enum bpf_func_id func_id)
3048 {
3049 switch (func_id) {
3050 case BPF_FUNC_setsockopt:
3051 return &bpf_setsockopt_proto;
3052 default:
3053 return bpf_base_func_proto(func_id);
3054 }
3055 }
3056
3057 static const struct bpf_func_proto *
3058 lwt_xmit_func_proto(enum bpf_func_id func_id)
3059 {
3060 switch (func_id) {
3061 case BPF_FUNC_skb_get_tunnel_key:
3062 return &bpf_skb_get_tunnel_key_proto;
3063 case BPF_FUNC_skb_set_tunnel_key:
3064 return bpf_get_skb_set_tunnel_proto(func_id);
3065 case BPF_FUNC_skb_get_tunnel_opt:
3066 return &bpf_skb_get_tunnel_opt_proto;
3067 case BPF_FUNC_skb_set_tunnel_opt:
3068 return bpf_get_skb_set_tunnel_proto(func_id);
3069 case BPF_FUNC_redirect:
3070 return &bpf_redirect_proto;
3071 case BPF_FUNC_clone_redirect:
3072 return &bpf_clone_redirect_proto;
3073 case BPF_FUNC_skb_change_tail:
3074 return &bpf_skb_change_tail_proto;
3075 case BPF_FUNC_skb_change_head:
3076 return &bpf_skb_change_head_proto;
3077 case BPF_FUNC_skb_store_bytes:
3078 return &bpf_skb_store_bytes_proto;
3079 case BPF_FUNC_csum_update:
3080 return &bpf_csum_update_proto;
3081 case BPF_FUNC_l3_csum_replace:
3082 return &bpf_l3_csum_replace_proto;
3083 case BPF_FUNC_l4_csum_replace:
3084 return &bpf_l4_csum_replace_proto;
3085 case BPF_FUNC_set_hash_invalid:
3086 return &bpf_set_hash_invalid_proto;
3087 default:
3088 return lwt_inout_func_proto(func_id);
3089 }
3090 }
3091
3092 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
3093 struct bpf_insn_access_aux *info)
3094 {
3095 const int size_default = sizeof(__u32);
3096
3097 if (off < 0 || off >= sizeof(struct __sk_buff))
3098 return false;
3099
3100 /* The verifier guarantees that size > 0. */
3101 if (off % size != 0)
3102 return false;
3103
3104 switch (off) {
3105 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3106 if (off + size > offsetofend(struct __sk_buff, cb[4]))
3107 return false;
3108 break;
3109 case bpf_ctx_range(struct __sk_buff, data):
3110 case bpf_ctx_range(struct __sk_buff, data_end):
3111 if (size != size_default)
3112 return false;
3113 break;
3114 default:
3115 /* Only narrow read access allowed for now. */
3116 if (type == BPF_WRITE) {
3117 if (size != size_default)
3118 return false;
3119 } else {
3120 bpf_ctx_record_field_size(info, size_default);
3121 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
3122 return false;
3123 }
3124 }
3125
3126 return true;
3127 }
3128
3129 static bool sk_filter_is_valid_access(int off, int size,
3130 enum bpf_access_type type,
3131 struct bpf_insn_access_aux *info)
3132 {
3133 switch (off) {
3134 case bpf_ctx_range(struct __sk_buff, tc_classid):
3135 case bpf_ctx_range(struct __sk_buff, data):
3136 case bpf_ctx_range(struct __sk_buff, data_end):
3137 return false;
3138 }
3139
3140 if (type == BPF_WRITE) {
3141 switch (off) {
3142 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3143 break;
3144 default:
3145 return false;
3146 }
3147 }
3148
3149 return bpf_skb_is_valid_access(off, size, type, info);
3150 }
3151
3152 static bool lwt_is_valid_access(int off, int size,
3153 enum bpf_access_type type,
3154 struct bpf_insn_access_aux *info)
3155 {
3156 switch (off) {
3157 case bpf_ctx_range(struct __sk_buff, tc_classid):
3158 return false;
3159 }
3160
3161 if (type == BPF_WRITE) {
3162 switch (off) {
3163 case bpf_ctx_range(struct __sk_buff, mark):
3164 case bpf_ctx_range(struct __sk_buff, priority):
3165 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3166 break;
3167 default:
3168 return false;
3169 }
3170 }
3171
3172 switch (off) {
3173 case bpf_ctx_range(struct __sk_buff, data):
3174 info->reg_type = PTR_TO_PACKET;
3175 break;
3176 case bpf_ctx_range(struct __sk_buff, data_end):
3177 info->reg_type = PTR_TO_PACKET_END;
3178 break;
3179 }
3180
3181 return bpf_skb_is_valid_access(off, size, type, info);
3182 }
3183
3184 static bool sock_filter_is_valid_access(int off, int size,
3185 enum bpf_access_type type,
3186 struct bpf_insn_access_aux *info)
3187 {
3188 if (type == BPF_WRITE) {
3189 switch (off) {
3190 case offsetof(struct bpf_sock, bound_dev_if):
3191 break;
3192 default:
3193 return false;
3194 }
3195 }
3196
3197 if (off < 0 || off + size > sizeof(struct bpf_sock))
3198 return false;
3199 /* The verifier guarantees that size > 0. */
3200 if (off % size != 0)
3201 return false;
3202 if (size != sizeof(__u32))
3203 return false;
3204
3205 return true;
3206 }
3207
3208 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
3209 const struct bpf_prog *prog)
3210 {
3211 struct bpf_insn *insn = insn_buf;
3212
3213 if (!direct_write)
3214 return 0;
3215
3216 /* if (!skb->cloned)
3217 * goto start;
3218 *
3219 * (Fast-path, otherwise approximation that we might be
3220 * a clone, do the rest in helper.)
3221 */
3222 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
3223 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
3224 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
3225
3226 /* ret = bpf_skb_pull_data(skb, 0); */
3227 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
3228 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
3229 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
3230 BPF_FUNC_skb_pull_data);
3231 /* if (!ret)
3232 * goto restore;
3233 * return TC_ACT_SHOT;
3234 */
3235 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
3236 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT);
3237 *insn++ = BPF_EXIT_INSN();
3238
3239 /* restore: */
3240 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
3241 /* start: */
3242 *insn++ = prog->insnsi[0];
3243
3244 return insn - insn_buf;
3245 }
3246
3247 static bool tc_cls_act_is_valid_access(int off, int size,
3248 enum bpf_access_type type,
3249 struct bpf_insn_access_aux *info)
3250 {
3251 if (type == BPF_WRITE) {
3252 switch (off) {
3253 case bpf_ctx_range(struct __sk_buff, mark):
3254 case bpf_ctx_range(struct __sk_buff, tc_index):
3255 case bpf_ctx_range(struct __sk_buff, priority):
3256 case bpf_ctx_range(struct __sk_buff, tc_classid):
3257 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
3258 break;
3259 default:
3260 return false;
3261 }
3262 }
3263
3264 switch (off) {
3265 case bpf_ctx_range(struct __sk_buff, data):
3266 info->reg_type = PTR_TO_PACKET;
3267 break;
3268 case bpf_ctx_range(struct __sk_buff, data_end):
3269 info->reg_type = PTR_TO_PACKET_END;
3270 break;
3271 }
3272
3273 return bpf_skb_is_valid_access(off, size, type, info);
3274 }
3275
3276 static bool __is_valid_xdp_access(int off, int size)
3277 {
3278 if (off < 0 || off >= sizeof(struct xdp_md))
3279 return false;
3280 if (off % size != 0)
3281 return false;
3282 if (size != sizeof(__u32))
3283 return false;
3284
3285 return true;
3286 }
3287
3288 static bool xdp_is_valid_access(int off, int size,
3289 enum bpf_access_type type,
3290 struct bpf_insn_access_aux *info)
3291 {
3292 if (type == BPF_WRITE)
3293 return false;
3294
3295 switch (off) {
3296 case offsetof(struct xdp_md, data):
3297 info->reg_type = PTR_TO_PACKET;
3298 break;
3299 case offsetof(struct xdp_md, data_end):
3300 info->reg_type = PTR_TO_PACKET_END;
3301 break;
3302 }
3303
3304 return __is_valid_xdp_access(off, size);
3305 }
3306
3307 void bpf_warn_invalid_xdp_action(u32 act)
3308 {
3309 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
3310 }
3311 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
3312
3313 static bool __is_valid_sock_ops_access(int off, int size)
3314 {
3315 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
3316 return false;
3317 /* The verifier guarantees that size > 0. */
3318 if (off % size != 0)
3319 return false;
3320 if (size != sizeof(__u32))
3321 return false;
3322
3323 return true;
3324 }
3325
3326 static bool sock_ops_is_valid_access(int off, int size,
3327 enum bpf_access_type type,
3328 struct bpf_insn_access_aux *info)
3329 {
3330 if (type == BPF_WRITE) {
3331 switch (off) {
3332 case offsetof(struct bpf_sock_ops, op) ...
3333 offsetof(struct bpf_sock_ops, replylong[3]):
3334 break;
3335 default:
3336 return false;
3337 }
3338 }
3339
3340 return __is_valid_sock_ops_access(off, size);
3341 }
3342
3343 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
3344 const struct bpf_insn *si,
3345 struct bpf_insn *insn_buf,
3346 struct bpf_prog *prog, u32 *target_size)
3347 {
3348 struct bpf_insn *insn = insn_buf;
3349 int off;
3350
3351 switch (si->off) {
3352 case offsetof(struct __sk_buff, len):
3353 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3354 bpf_target_off(struct sk_buff, len, 4,
3355 target_size));
3356 break;
3357
3358 case offsetof(struct __sk_buff, protocol):
3359 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3360 bpf_target_off(struct sk_buff, protocol, 2,
3361 target_size));
3362 break;
3363
3364 case offsetof(struct __sk_buff, vlan_proto):
3365 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3366 bpf_target_off(struct sk_buff, vlan_proto, 2,
3367 target_size));
3368 break;
3369
3370 case offsetof(struct __sk_buff, priority):
3371 if (type == BPF_WRITE)
3372 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3373 bpf_target_off(struct sk_buff, priority, 4,
3374 target_size));
3375 else
3376 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3377 bpf_target_off(struct sk_buff, priority, 4,
3378 target_size));
3379 break;
3380
3381 case offsetof(struct __sk_buff, ingress_ifindex):
3382 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3383 bpf_target_off(struct sk_buff, skb_iif, 4,
3384 target_size));
3385 break;
3386
3387 case offsetof(struct __sk_buff, ifindex):
3388 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
3389 si->dst_reg, si->src_reg,
3390 offsetof(struct sk_buff, dev));
3391 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
3392 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3393 bpf_target_off(struct net_device, ifindex, 4,
3394 target_size));
3395 break;
3396
3397 case offsetof(struct __sk_buff, hash):
3398 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3399 bpf_target_off(struct sk_buff, hash, 4,
3400 target_size));
3401 break;
3402
3403 case offsetof(struct __sk_buff, mark):
3404 if (type == BPF_WRITE)
3405 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3406 bpf_target_off(struct sk_buff, mark, 4,
3407 target_size));
3408 else
3409 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3410 bpf_target_off(struct sk_buff, mark, 4,
3411 target_size));
3412 break;
3413
3414 case offsetof(struct __sk_buff, pkt_type):
3415 *target_size = 1;
3416 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
3417 PKT_TYPE_OFFSET());
3418 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
3419 #ifdef __BIG_ENDIAN_BITFIELD
3420 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
3421 #endif
3422 break;
3423
3424 case offsetof(struct __sk_buff, queue_mapping):
3425 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3426 bpf_target_off(struct sk_buff, queue_mapping, 2,
3427 target_size));
3428 break;
3429
3430 case offsetof(struct __sk_buff, vlan_present):
3431 case offsetof(struct __sk_buff, vlan_tci):
3432 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
3433
3434 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3435 bpf_target_off(struct sk_buff, vlan_tci, 2,
3436 target_size));
3437 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
3438 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
3439 ~VLAN_TAG_PRESENT);
3440 } else {
3441 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
3442 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
3443 }
3444 break;
3445
3446 case offsetof(struct __sk_buff, cb[0]) ...
3447 offsetofend(struct __sk_buff, cb[4]) - 1:
3448 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
3449 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
3450 offsetof(struct qdisc_skb_cb, data)) %
3451 sizeof(__u64));
3452
3453 prog->cb_access = 1;
3454 off = si->off;
3455 off -= offsetof(struct __sk_buff, cb[0]);
3456 off += offsetof(struct sk_buff, cb);
3457 off += offsetof(struct qdisc_skb_cb, data);
3458 if (type == BPF_WRITE)
3459 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
3460 si->src_reg, off);
3461 else
3462 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
3463 si->src_reg, off);
3464 break;
3465
3466 case offsetof(struct __sk_buff, tc_classid):
3467 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
3468
3469 off = si->off;
3470 off -= offsetof(struct __sk_buff, tc_classid);
3471 off += offsetof(struct sk_buff, cb);
3472 off += offsetof(struct qdisc_skb_cb, tc_classid);
3473 *target_size = 2;
3474 if (type == BPF_WRITE)
3475 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
3476 si->src_reg, off);
3477 else
3478 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
3479 si->src_reg, off);
3480 break;
3481
3482 case offsetof(struct __sk_buff, data):
3483 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
3484 si->dst_reg, si->src_reg,
3485 offsetof(struct sk_buff, data));
3486 break;
3487
3488 case offsetof(struct __sk_buff, data_end):
3489 off = si->off;
3490 off -= offsetof(struct __sk_buff, data_end);
3491 off += offsetof(struct sk_buff, cb);
3492 off += offsetof(struct bpf_skb_data_end, data_end);
3493 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
3494 si->src_reg, off);
3495 break;
3496
3497 case offsetof(struct __sk_buff, tc_index):
3498 #ifdef CONFIG_NET_SCHED
3499 if (type == BPF_WRITE)
3500 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
3501 bpf_target_off(struct sk_buff, tc_index, 2,
3502 target_size));
3503 else
3504 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3505 bpf_target_off(struct sk_buff, tc_index, 2,
3506 target_size));
3507 #else
3508 *target_size = 2;
3509 if (type == BPF_WRITE)
3510 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
3511 else
3512 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3513 #endif
3514 break;
3515
3516 case offsetof(struct __sk_buff, napi_id):
3517 #if defined(CONFIG_NET_RX_BUSY_POLL)
3518 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3519 bpf_target_off(struct sk_buff, napi_id, 4,
3520 target_size));
3521 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
3522 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3523 #else
3524 *target_size = 4;
3525 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
3526 #endif
3527 break;
3528 }
3529
3530 return insn - insn_buf;
3531 }
3532
3533 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
3534 const struct bpf_insn *si,
3535 struct bpf_insn *insn_buf,
3536 struct bpf_prog *prog, u32 *target_size)
3537 {
3538 struct bpf_insn *insn = insn_buf;
3539
3540 switch (si->off) {
3541 case offsetof(struct bpf_sock, bound_dev_if):
3542 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
3543
3544 if (type == BPF_WRITE)
3545 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3546 offsetof(struct sock, sk_bound_dev_if));
3547 else
3548 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3549 offsetof(struct sock, sk_bound_dev_if));
3550 break;
3551
3552 case offsetof(struct bpf_sock, family):
3553 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
3554
3555 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
3556 offsetof(struct sock, sk_family));
3557 break;
3558
3559 case offsetof(struct bpf_sock, type):
3560 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3561 offsetof(struct sock, __sk_flags_offset));
3562 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
3563 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
3564 break;
3565
3566 case offsetof(struct bpf_sock, protocol):
3567 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3568 offsetof(struct sock, __sk_flags_offset));
3569 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
3570 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
3571 break;
3572 }
3573
3574 return insn - insn_buf;
3575 }
3576
3577 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
3578 const struct bpf_insn *si,
3579 struct bpf_insn *insn_buf,
3580 struct bpf_prog *prog, u32 *target_size)
3581 {
3582 struct bpf_insn *insn = insn_buf;
3583
3584 switch (si->off) {
3585 case offsetof(struct __sk_buff, ifindex):
3586 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
3587 si->dst_reg, si->src_reg,
3588 offsetof(struct sk_buff, dev));
3589 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3590 bpf_target_off(struct net_device, ifindex, 4,
3591 target_size));
3592 break;
3593 default:
3594 return bpf_convert_ctx_access(type, si, insn_buf, prog,
3595 target_size);
3596 }
3597
3598 return insn - insn_buf;
3599 }
3600
3601 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
3602 const struct bpf_insn *si,
3603 struct bpf_insn *insn_buf,
3604 struct bpf_prog *prog, u32 *target_size)
3605 {
3606 struct bpf_insn *insn = insn_buf;
3607
3608 switch (si->off) {
3609 case offsetof(struct xdp_md, data):
3610 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
3611 si->dst_reg, si->src_reg,
3612 offsetof(struct xdp_buff, data));
3613 break;
3614 case offsetof(struct xdp_md, data_end):
3615 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
3616 si->dst_reg, si->src_reg,
3617 offsetof(struct xdp_buff, data_end));
3618 break;
3619 }
3620
3621 return insn - insn_buf;
3622 }
3623
3624 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
3625 const struct bpf_insn *si,
3626 struct bpf_insn *insn_buf,
3627 struct bpf_prog *prog,
3628 u32 *target_size)
3629 {
3630 struct bpf_insn *insn = insn_buf;
3631 int off;
3632
3633 switch (si->off) {
3634 case offsetof(struct bpf_sock_ops, op) ...
3635 offsetof(struct bpf_sock_ops, replylong[3]):
3636 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
3637 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
3638 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
3639 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
3640 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
3641 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
3642 off = si->off;
3643 off -= offsetof(struct bpf_sock_ops, op);
3644 off += offsetof(struct bpf_sock_ops_kern, op);
3645 if (type == BPF_WRITE)
3646 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
3647 off);
3648 else
3649 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
3650 off);
3651 break;
3652
3653 case offsetof(struct bpf_sock_ops, family):
3654 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
3655
3656 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3657 struct bpf_sock_ops_kern, sk),
3658 si->dst_reg, si->src_reg,
3659 offsetof(struct bpf_sock_ops_kern, sk));
3660 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3661 offsetof(struct sock_common, skc_family));
3662 break;
3663
3664 case offsetof(struct bpf_sock_ops, remote_ip4):
3665 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
3666
3667 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3668 struct bpf_sock_ops_kern, sk),
3669 si->dst_reg, si->src_reg,
3670 offsetof(struct bpf_sock_ops_kern, sk));
3671 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3672 offsetof(struct sock_common, skc_daddr));
3673 break;
3674
3675 case offsetof(struct bpf_sock_ops, local_ip4):
3676 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_rcv_saddr) != 4);
3677
3678 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3679 struct bpf_sock_ops_kern, sk),
3680 si->dst_reg, si->src_reg,
3681 offsetof(struct bpf_sock_ops_kern, sk));
3682 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3683 offsetof(struct sock_common,
3684 skc_rcv_saddr));
3685 break;
3686
3687 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
3688 offsetof(struct bpf_sock_ops, remote_ip6[3]):
3689 #if IS_ENABLED(CONFIG_IPV6)
3690 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3691 skc_v6_daddr.s6_addr32[0]) != 4);
3692
3693 off = si->off;
3694 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
3695 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3696 struct bpf_sock_ops_kern, sk),
3697 si->dst_reg, si->src_reg,
3698 offsetof(struct bpf_sock_ops_kern, sk));
3699 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3700 offsetof(struct sock_common,
3701 skc_v6_daddr.s6_addr32[0]) +
3702 off);
3703 #else
3704 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3705 #endif
3706 break;
3707
3708 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
3709 offsetof(struct bpf_sock_ops, local_ip6[3]):
3710 #if IS_ENABLED(CONFIG_IPV6)
3711 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
3712 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
3713
3714 off = si->off;
3715 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
3716 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3717 struct bpf_sock_ops_kern, sk),
3718 si->dst_reg, si->src_reg,
3719 offsetof(struct bpf_sock_ops_kern, sk));
3720 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
3721 offsetof(struct sock_common,
3722 skc_v6_rcv_saddr.s6_addr32[0]) +
3723 off);
3724 #else
3725 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
3726 #endif
3727 break;
3728
3729 case offsetof(struct bpf_sock_ops, remote_port):
3730 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
3731
3732 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3733 struct bpf_sock_ops_kern, sk),
3734 si->dst_reg, si->src_reg,
3735 offsetof(struct bpf_sock_ops_kern, sk));
3736 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3737 offsetof(struct sock_common, skc_dport));
3738 #ifndef __BIG_ENDIAN_BITFIELD
3739 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
3740 #endif
3741 break;
3742
3743 case offsetof(struct bpf_sock_ops, local_port):
3744 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
3745
3746 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
3747 struct bpf_sock_ops_kern, sk),
3748 si->dst_reg, si->src_reg,
3749 offsetof(struct bpf_sock_ops_kern, sk));
3750 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
3751 offsetof(struct sock_common, skc_num));
3752 break;
3753 }
3754 return insn - insn_buf;
3755 }
3756
3757 const struct bpf_verifier_ops sk_filter_prog_ops = {
3758 .get_func_proto = sk_filter_func_proto,
3759 .is_valid_access = sk_filter_is_valid_access,
3760 .convert_ctx_access = bpf_convert_ctx_access,
3761 };
3762
3763 const struct bpf_verifier_ops tc_cls_act_prog_ops = {
3764 .get_func_proto = tc_cls_act_func_proto,
3765 .is_valid_access = tc_cls_act_is_valid_access,
3766 .convert_ctx_access = tc_cls_act_convert_ctx_access,
3767 .gen_prologue = tc_cls_act_prologue,
3768 .test_run = bpf_prog_test_run_skb,
3769 };
3770
3771 const struct bpf_verifier_ops xdp_prog_ops = {
3772 .get_func_proto = xdp_func_proto,
3773 .is_valid_access = xdp_is_valid_access,
3774 .convert_ctx_access = xdp_convert_ctx_access,
3775 .test_run = bpf_prog_test_run_xdp,
3776 };
3777
3778 const struct bpf_verifier_ops cg_skb_prog_ops = {
3779 .get_func_proto = sk_filter_func_proto,
3780 .is_valid_access = sk_filter_is_valid_access,
3781 .convert_ctx_access = bpf_convert_ctx_access,
3782 .test_run = bpf_prog_test_run_skb,
3783 };
3784
3785 const struct bpf_verifier_ops lwt_inout_prog_ops = {
3786 .get_func_proto = lwt_inout_func_proto,
3787 .is_valid_access = lwt_is_valid_access,
3788 .convert_ctx_access = bpf_convert_ctx_access,
3789 .test_run = bpf_prog_test_run_skb,
3790 };
3791
3792 const struct bpf_verifier_ops lwt_xmit_prog_ops = {
3793 .get_func_proto = lwt_xmit_func_proto,
3794 .is_valid_access = lwt_is_valid_access,
3795 .convert_ctx_access = bpf_convert_ctx_access,
3796 .gen_prologue = tc_cls_act_prologue,
3797 .test_run = bpf_prog_test_run_skb,
3798 };
3799
3800 const struct bpf_verifier_ops cg_sock_prog_ops = {
3801 .get_func_proto = bpf_base_func_proto,
3802 .is_valid_access = sock_filter_is_valid_access,
3803 .convert_ctx_access = sock_filter_convert_ctx_access,
3804 };
3805
3806 const struct bpf_verifier_ops sock_ops_prog_ops = {
3807 .get_func_proto = sock_ops_func_proto,
3808 .is_valid_access = sock_ops_is_valid_access,
3809 .convert_ctx_access = sock_ops_convert_ctx_access,
3810 };
3811
3812 int sk_detach_filter(struct sock *sk)
3813 {
3814 int ret = -ENOENT;
3815 struct sk_filter *filter;
3816
3817 if (sock_flag(sk, SOCK_FILTER_LOCKED))
3818 return -EPERM;
3819
3820 filter = rcu_dereference_protected(sk->sk_filter,
3821 lockdep_sock_is_held(sk));
3822 if (filter) {
3823 RCU_INIT_POINTER(sk->sk_filter, NULL);
3824 sk_filter_uncharge(sk, filter);
3825 ret = 0;
3826 }
3827
3828 return ret;
3829 }
3830 EXPORT_SYMBOL_GPL(sk_detach_filter);
3831
3832 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
3833 unsigned int len)
3834 {
3835 struct sock_fprog_kern *fprog;
3836 struct sk_filter *filter;
3837 int ret = 0;
3838
3839 lock_sock(sk);
3840 filter = rcu_dereference_protected(sk->sk_filter,
3841 lockdep_sock_is_held(sk));
3842 if (!filter)
3843 goto out;
3844
3845 /* We're copying the filter that has been originally attached,
3846 * so no conversion/decode needed anymore. eBPF programs that
3847 * have no original program cannot be dumped through this.
3848 */
3849 ret = -EACCES;
3850 fprog = filter->prog->orig_prog;
3851 if (!fprog)
3852 goto out;
3853
3854 ret = fprog->len;
3855 if (!len)
3856 /* User space only enquires number of filter blocks. */
3857 goto out;
3858
3859 ret = -EINVAL;
3860 if (len < fprog->len)
3861 goto out;
3862
3863 ret = -EFAULT;
3864 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
3865 goto out;
3866
3867 /* Instead of bytes, the API requests to return the number
3868 * of filter blocks.
3869 */
3870 ret = fprog->len;
3871 out:
3872 release_sock(sk);
3873 return ret;
3874 }