]>
Commit | Line | Data |
---|---|---|
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/in.h> | |
30 | #include <linux/inet.h> | |
31 | #include <linux/netdevice.h> | |
32 | #include <linux/if_packet.h> | |
33 | #include <linux/gfp.h> | |
34 | #include <net/ip.h> | |
35 | #include <net/protocol.h> | |
36 | #include <net/netlink.h> | |
37 | #include <linux/skbuff.h> | |
38 | #include <net/sock.h> | |
39 | #include <net/flow_dissector.h> | |
40 | #include <linux/errno.h> | |
41 | #include <linux/timer.h> | |
42 | #include <asm/uaccess.h> | |
43 | #include <asm/unaligned.h> | |
44 | #include <linux/filter.h> | |
45 | #include <linux/ratelimit.h> | |
46 | #include <linux/seccomp.h> | |
47 | #include <linux/if_vlan.h> | |
48 | #include <linux/bpf.h> | |
49 | #include <net/sch_generic.h> | |
50 | #include <net/cls_cgroup.h> | |
51 | #include <net/dst_metadata.h> | |
52 | #include <net/dst.h> | |
53 | #include <net/sock_reuseport.h> | |
54 | ||
55 | /** | |
56 | * sk_filter - run a packet through a socket filter | |
57 | * @sk: sock associated with &sk_buff | |
58 | * @skb: buffer to filter | |
59 | * | |
60 | * Run the eBPF program and then cut skb->data to correct size returned by | |
61 | * the program. If pkt_len is 0 we toss packet. If skb->len is smaller | |
62 | * than pkt_len we keep whole skb->data. This is the socket level | |
63 | * wrapper to BPF_PROG_RUN. It returns 0 if the packet should | |
64 | * be accepted or -EPERM if the packet should be tossed. | |
65 | * | |
66 | */ | |
67 | int sk_filter(struct sock *sk, struct sk_buff *skb) | |
68 | { | |
69 | int err; | |
70 | struct sk_filter *filter; | |
71 | ||
72 | /* | |
73 | * If the skb was allocated from pfmemalloc reserves, only | |
74 | * allow SOCK_MEMALLOC sockets to use it as this socket is | |
75 | * helping free memory | |
76 | */ | |
77 | if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) | |
78 | return -ENOMEM; | |
79 | ||
80 | err = security_sock_rcv_skb(sk, skb); | |
81 | if (err) | |
82 | return err; | |
83 | ||
84 | rcu_read_lock(); | |
85 | filter = rcu_dereference(sk->sk_filter); | |
86 | if (filter) { | |
87 | unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb); | |
88 | ||
89 | err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM; | |
90 | } | |
91 | rcu_read_unlock(); | |
92 | ||
93 | return err; | |
94 | } | |
95 | EXPORT_SYMBOL(sk_filter); | |
96 | ||
97 | static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) | |
98 | { | |
99 | return skb_get_poff((struct sk_buff *)(unsigned long) ctx); | |
100 | } | |
101 | ||
102 | static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) | |
103 | { | |
104 | struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx; | |
105 | struct nlattr *nla; | |
106 | ||
107 | if (skb_is_nonlinear(skb)) | |
108 | return 0; | |
109 | ||
110 | if (skb->len < sizeof(struct nlattr)) | |
111 | return 0; | |
112 | ||
113 | if (a > skb->len - sizeof(struct nlattr)) | |
114 | return 0; | |
115 | ||
116 | nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x); | |
117 | if (nla) | |
118 | return (void *) nla - (void *) skb->data; | |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
123 | static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) | |
124 | { | |
125 | struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx; | |
126 | struct nlattr *nla; | |
127 | ||
128 | if (skb_is_nonlinear(skb)) | |
129 | return 0; | |
130 | ||
131 | if (skb->len < sizeof(struct nlattr)) | |
132 | return 0; | |
133 | ||
134 | if (a > skb->len - sizeof(struct nlattr)) | |
135 | return 0; | |
136 | ||
137 | nla = (struct nlattr *) &skb->data[a]; | |
138 | if (nla->nla_len > skb->len - a) | |
139 | return 0; | |
140 | ||
141 | nla = nla_find_nested(nla, x); | |
142 | if (nla) | |
143 | return (void *) nla - (void *) skb->data; | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) | |
149 | { | |
150 | return raw_smp_processor_id(); | |
151 | } | |
152 | ||
153 | static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = { | |
154 | .func = __get_raw_cpu_id, | |
155 | .gpl_only = false, | |
156 | .ret_type = RET_INTEGER, | |
157 | }; | |
158 | ||
159 | static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg, | |
160 | struct bpf_insn *insn_buf) | |
161 | { | |
162 | struct bpf_insn *insn = insn_buf; | |
163 | ||
164 | switch (skb_field) { | |
165 | case SKF_AD_MARK: | |
166 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); | |
167 | ||
168 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
169 | offsetof(struct sk_buff, mark)); | |
170 | break; | |
171 | ||
172 | case SKF_AD_PKTTYPE: | |
173 | *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET()); | |
174 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX); | |
175 | #ifdef __BIG_ENDIAN_BITFIELD | |
176 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5); | |
177 | #endif | |
178 | break; | |
179 | ||
180 | case SKF_AD_QUEUE: | |
181 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2); | |
182 | ||
183 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
184 | offsetof(struct sk_buff, queue_mapping)); | |
185 | break; | |
186 | ||
187 | case SKF_AD_VLAN_TAG: | |
188 | case SKF_AD_VLAN_TAG_PRESENT: | |
189 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); | |
190 | BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000); | |
191 | ||
192 | /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */ | |
193 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
194 | offsetof(struct sk_buff, vlan_tci)); | |
195 | if (skb_field == SKF_AD_VLAN_TAG) { | |
196 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, | |
197 | ~VLAN_TAG_PRESENT); | |
198 | } else { | |
199 | /* dst_reg >>= 12 */ | |
200 | *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12); | |
201 | /* dst_reg &= 1 */ | |
202 | *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1); | |
203 | } | |
204 | break; | |
205 | } | |
206 | ||
207 | return insn - insn_buf; | |
208 | } | |
209 | ||
210 | static bool convert_bpf_extensions(struct sock_filter *fp, | |
211 | struct bpf_insn **insnp) | |
212 | { | |
213 | struct bpf_insn *insn = *insnp; | |
214 | u32 cnt; | |
215 | ||
216 | switch (fp->k) { | |
217 | case SKF_AD_OFF + SKF_AD_PROTOCOL: | |
218 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); | |
219 | ||
220 | /* A = *(u16 *) (CTX + offsetof(protocol)) */ | |
221 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, | |
222 | offsetof(struct sk_buff, protocol)); | |
223 | /* A = ntohs(A) [emitting a nop or swap16] */ | |
224 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); | |
225 | break; | |
226 | ||
227 | case SKF_AD_OFF + SKF_AD_PKTTYPE: | |
228 | cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn); | |
229 | insn += cnt - 1; | |
230 | break; | |
231 | ||
232 | case SKF_AD_OFF + SKF_AD_IFINDEX: | |
233 | case SKF_AD_OFF + SKF_AD_HATYPE: | |
234 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); | |
235 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2); | |
236 | BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0); | |
237 | ||
238 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)), | |
239 | BPF_REG_TMP, BPF_REG_CTX, | |
240 | offsetof(struct sk_buff, dev)); | |
241 | /* if (tmp != 0) goto pc + 1 */ | |
242 | *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1); | |
243 | *insn++ = BPF_EXIT_INSN(); | |
244 | if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX) | |
245 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP, | |
246 | offsetof(struct net_device, ifindex)); | |
247 | else | |
248 | *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP, | |
249 | offsetof(struct net_device, type)); | |
250 | break; | |
251 | ||
252 | case SKF_AD_OFF + SKF_AD_MARK: | |
253 | cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn); | |
254 | insn += cnt - 1; | |
255 | break; | |
256 | ||
257 | case SKF_AD_OFF + SKF_AD_RXHASH: | |
258 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); | |
259 | ||
260 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, | |
261 | offsetof(struct sk_buff, hash)); | |
262 | break; | |
263 | ||
264 | case SKF_AD_OFF + SKF_AD_QUEUE: | |
265 | cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn); | |
266 | insn += cnt - 1; | |
267 | break; | |
268 | ||
269 | case SKF_AD_OFF + SKF_AD_VLAN_TAG: | |
270 | cnt = convert_skb_access(SKF_AD_VLAN_TAG, | |
271 | BPF_REG_A, BPF_REG_CTX, insn); | |
272 | insn += cnt - 1; | |
273 | break; | |
274 | ||
275 | case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: | |
276 | cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, | |
277 | BPF_REG_A, BPF_REG_CTX, insn); | |
278 | insn += cnt - 1; | |
279 | break; | |
280 | ||
281 | case SKF_AD_OFF + SKF_AD_VLAN_TPID: | |
282 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); | |
283 | ||
284 | /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */ | |
285 | *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, | |
286 | offsetof(struct sk_buff, vlan_proto)); | |
287 | /* A = ntohs(A) [emitting a nop or swap16] */ | |
288 | *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16); | |
289 | break; | |
290 | ||
291 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: | |
292 | case SKF_AD_OFF + SKF_AD_NLATTR: | |
293 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: | |
294 | case SKF_AD_OFF + SKF_AD_CPU: | |
295 | case SKF_AD_OFF + SKF_AD_RANDOM: | |
296 | /* arg1 = CTX */ | |
297 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX); | |
298 | /* arg2 = A */ | |
299 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A); | |
300 | /* arg3 = X */ | |
301 | *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X); | |
302 | /* Emit call(arg1=CTX, arg2=A, arg3=X) */ | |
303 | switch (fp->k) { | |
304 | case SKF_AD_OFF + SKF_AD_PAY_OFFSET: | |
305 | *insn = BPF_EMIT_CALL(__skb_get_pay_offset); | |
306 | break; | |
307 | case SKF_AD_OFF + SKF_AD_NLATTR: | |
308 | *insn = BPF_EMIT_CALL(__skb_get_nlattr); | |
309 | break; | |
310 | case SKF_AD_OFF + SKF_AD_NLATTR_NEST: | |
311 | *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest); | |
312 | break; | |
313 | case SKF_AD_OFF + SKF_AD_CPU: | |
314 | *insn = BPF_EMIT_CALL(__get_raw_cpu_id); | |
315 | break; | |
316 | case SKF_AD_OFF + SKF_AD_RANDOM: | |
317 | *insn = BPF_EMIT_CALL(bpf_user_rnd_u32); | |
318 | bpf_user_rnd_init_once(); | |
319 | break; | |
320 | } | |
321 | break; | |
322 | ||
323 | case SKF_AD_OFF + SKF_AD_ALU_XOR_X: | |
324 | /* A ^= X */ | |
325 | *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X); | |
326 | break; | |
327 | ||
328 | default: | |
329 | /* This is just a dummy call to avoid letting the compiler | |
330 | * evict __bpf_call_base() as an optimization. Placed here | |
331 | * where no-one bothers. | |
332 | */ | |
333 | BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0); | |
334 | return false; | |
335 | } | |
336 | ||
337 | *insnp = insn; | |
338 | return true; | |
339 | } | |
340 | ||
341 | /** | |
342 | * bpf_convert_filter - convert filter program | |
343 | * @prog: the user passed filter program | |
344 | * @len: the length of the user passed filter program | |
345 | * @new_prog: buffer where converted program will be stored | |
346 | * @new_len: pointer to store length of converted program | |
347 | * | |
348 | * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style. | |
349 | * Conversion workflow: | |
350 | * | |
351 | * 1) First pass for calculating the new program length: | |
352 | * bpf_convert_filter(old_prog, old_len, NULL, &new_len) | |
353 | * | |
354 | * 2) 2nd pass to remap in two passes: 1st pass finds new | |
355 | * jump offsets, 2nd pass remapping: | |
356 | * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len); | |
357 | * bpf_convert_filter(old_prog, old_len, new_prog, &new_len); | |
358 | */ | |
359 | static int bpf_convert_filter(struct sock_filter *prog, int len, | |
360 | struct bpf_insn *new_prog, int *new_len) | |
361 | { | |
362 | int new_flen = 0, pass = 0, target, i; | |
363 | struct bpf_insn *new_insn; | |
364 | struct sock_filter *fp; | |
365 | int *addrs = NULL; | |
366 | u8 bpf_src; | |
367 | ||
368 | BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK); | |
369 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); | |
370 | ||
371 | if (len <= 0 || len > BPF_MAXINSNS) | |
372 | return -EINVAL; | |
373 | ||
374 | if (new_prog) { | |
375 | addrs = kcalloc(len, sizeof(*addrs), | |
376 | GFP_KERNEL | __GFP_NOWARN); | |
377 | if (!addrs) | |
378 | return -ENOMEM; | |
379 | } | |
380 | ||
381 | do_pass: | |
382 | new_insn = new_prog; | |
383 | fp = prog; | |
384 | ||
385 | /* Classic BPF related prologue emission. */ | |
386 | if (new_insn) { | |
387 | /* Classic BPF expects A and X to be reset first. These need | |
388 | * to be guaranteed to be the first two instructions. | |
389 | */ | |
390 | *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A); | |
391 | *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X); | |
392 | ||
393 | /* All programs must keep CTX in callee saved BPF_REG_CTX. | |
394 | * In eBPF case it's done by the compiler, here we need to | |
395 | * do this ourself. Initial CTX is present in BPF_REG_ARG1. | |
396 | */ | |
397 | *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1); | |
398 | } else { | |
399 | new_insn += 3; | |
400 | } | |
401 | ||
402 | for (i = 0; i < len; fp++, i++) { | |
403 | struct bpf_insn tmp_insns[6] = { }; | |
404 | struct bpf_insn *insn = tmp_insns; | |
405 | ||
406 | if (addrs) | |
407 | addrs[i] = new_insn - new_prog; | |
408 | ||
409 | switch (fp->code) { | |
410 | /* All arithmetic insns and skb loads map as-is. */ | |
411 | case BPF_ALU | BPF_ADD | BPF_X: | |
412 | case BPF_ALU | BPF_ADD | BPF_K: | |
413 | case BPF_ALU | BPF_SUB | BPF_X: | |
414 | case BPF_ALU | BPF_SUB | BPF_K: | |
415 | case BPF_ALU | BPF_AND | BPF_X: | |
416 | case BPF_ALU | BPF_AND | BPF_K: | |
417 | case BPF_ALU | BPF_OR | BPF_X: | |
418 | case BPF_ALU | BPF_OR | BPF_K: | |
419 | case BPF_ALU | BPF_LSH | BPF_X: | |
420 | case BPF_ALU | BPF_LSH | BPF_K: | |
421 | case BPF_ALU | BPF_RSH | BPF_X: | |
422 | case BPF_ALU | BPF_RSH | BPF_K: | |
423 | case BPF_ALU | BPF_XOR | BPF_X: | |
424 | case BPF_ALU | BPF_XOR | BPF_K: | |
425 | case BPF_ALU | BPF_MUL | BPF_X: | |
426 | case BPF_ALU | BPF_MUL | BPF_K: | |
427 | case BPF_ALU | BPF_DIV | BPF_X: | |
428 | case BPF_ALU | BPF_DIV | BPF_K: | |
429 | case BPF_ALU | BPF_MOD | BPF_X: | |
430 | case BPF_ALU | BPF_MOD | BPF_K: | |
431 | case BPF_ALU | BPF_NEG: | |
432 | case BPF_LD | BPF_ABS | BPF_W: | |
433 | case BPF_LD | BPF_ABS | BPF_H: | |
434 | case BPF_LD | BPF_ABS | BPF_B: | |
435 | case BPF_LD | BPF_IND | BPF_W: | |
436 | case BPF_LD | BPF_IND | BPF_H: | |
437 | case BPF_LD | BPF_IND | BPF_B: | |
438 | /* Check for overloaded BPF extension and | |
439 | * directly convert it if found, otherwise | |
440 | * just move on with mapping. | |
441 | */ | |
442 | if (BPF_CLASS(fp->code) == BPF_LD && | |
443 | BPF_MODE(fp->code) == BPF_ABS && | |
444 | convert_bpf_extensions(fp, &insn)) | |
445 | break; | |
446 | ||
447 | *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k); | |
448 | break; | |
449 | ||
450 | /* Jump transformation cannot use BPF block macros | |
451 | * everywhere as offset calculation and target updates | |
452 | * require a bit more work than the rest, i.e. jump | |
453 | * opcodes map as-is, but offsets need adjustment. | |
454 | */ | |
455 | ||
456 | #define BPF_EMIT_JMP \ | |
457 | do { \ | |
458 | if (target >= len || target < 0) \ | |
459 | goto err; \ | |
460 | insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \ | |
461 | /* Adjust pc relative offset for 2nd or 3rd insn. */ \ | |
462 | insn->off -= insn - tmp_insns; \ | |
463 | } while (0) | |
464 | ||
465 | case BPF_JMP | BPF_JA: | |
466 | target = i + fp->k + 1; | |
467 | insn->code = fp->code; | |
468 | BPF_EMIT_JMP; | |
469 | break; | |
470 | ||
471 | case BPF_JMP | BPF_JEQ | BPF_K: | |
472 | case BPF_JMP | BPF_JEQ | BPF_X: | |
473 | case BPF_JMP | BPF_JSET | BPF_K: | |
474 | case BPF_JMP | BPF_JSET | BPF_X: | |
475 | case BPF_JMP | BPF_JGT | BPF_K: | |
476 | case BPF_JMP | BPF_JGT | BPF_X: | |
477 | case BPF_JMP | BPF_JGE | BPF_K: | |
478 | case BPF_JMP | BPF_JGE | BPF_X: | |
479 | if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) { | |
480 | /* BPF immediates are signed, zero extend | |
481 | * immediate into tmp register and use it | |
482 | * in compare insn. | |
483 | */ | |
484 | *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k); | |
485 | ||
486 | insn->dst_reg = BPF_REG_A; | |
487 | insn->src_reg = BPF_REG_TMP; | |
488 | bpf_src = BPF_X; | |
489 | } else { | |
490 | insn->dst_reg = BPF_REG_A; | |
491 | insn->imm = fp->k; | |
492 | bpf_src = BPF_SRC(fp->code); | |
493 | insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0; | |
494 | } | |
495 | ||
496 | /* Common case where 'jump_false' is next insn. */ | |
497 | if (fp->jf == 0) { | |
498 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; | |
499 | target = i + fp->jt + 1; | |
500 | BPF_EMIT_JMP; | |
501 | break; | |
502 | } | |
503 | ||
504 | /* Convert JEQ into JNE when 'jump_true' is next insn. */ | |
505 | if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) { | |
506 | insn->code = BPF_JMP | BPF_JNE | bpf_src; | |
507 | target = i + fp->jf + 1; | |
508 | BPF_EMIT_JMP; | |
509 | break; | |
510 | } | |
511 | ||
512 | /* Other jumps are mapped into two insns: Jxx and JA. */ | |
513 | target = i + fp->jt + 1; | |
514 | insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src; | |
515 | BPF_EMIT_JMP; | |
516 | insn++; | |
517 | ||
518 | insn->code = BPF_JMP | BPF_JA; | |
519 | target = i + fp->jf + 1; | |
520 | BPF_EMIT_JMP; | |
521 | break; | |
522 | ||
523 | /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */ | |
524 | case BPF_LDX | BPF_MSH | BPF_B: | |
525 | /* tmp = A */ | |
526 | *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A); | |
527 | /* A = BPF_R0 = *(u8 *) (skb->data + K) */ | |
528 | *insn++ = BPF_LD_ABS(BPF_B, fp->k); | |
529 | /* A &= 0xf */ | |
530 | *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf); | |
531 | /* A <<= 2 */ | |
532 | *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2); | |
533 | /* X = A */ | |
534 | *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); | |
535 | /* A = tmp */ | |
536 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP); | |
537 | break; | |
538 | ||
539 | /* RET_K is remaped into 2 insns. RET_A case doesn't need an | |
540 | * extra mov as BPF_REG_0 is already mapped into BPF_REG_A. | |
541 | */ | |
542 | case BPF_RET | BPF_A: | |
543 | case BPF_RET | BPF_K: | |
544 | if (BPF_RVAL(fp->code) == BPF_K) | |
545 | *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0, | |
546 | 0, fp->k); | |
547 | *insn = BPF_EXIT_INSN(); | |
548 | break; | |
549 | ||
550 | /* Store to stack. */ | |
551 | case BPF_ST: | |
552 | case BPF_STX: | |
553 | *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) == | |
554 | BPF_ST ? BPF_REG_A : BPF_REG_X, | |
555 | -(BPF_MEMWORDS - fp->k) * 4); | |
556 | break; | |
557 | ||
558 | /* Load from stack. */ | |
559 | case BPF_LD | BPF_MEM: | |
560 | case BPF_LDX | BPF_MEM: | |
561 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? | |
562 | BPF_REG_A : BPF_REG_X, BPF_REG_FP, | |
563 | -(BPF_MEMWORDS - fp->k) * 4); | |
564 | break; | |
565 | ||
566 | /* A = K or X = K */ | |
567 | case BPF_LD | BPF_IMM: | |
568 | case BPF_LDX | BPF_IMM: | |
569 | *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ? | |
570 | BPF_REG_A : BPF_REG_X, fp->k); | |
571 | break; | |
572 | ||
573 | /* X = A */ | |
574 | case BPF_MISC | BPF_TAX: | |
575 | *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A); | |
576 | break; | |
577 | ||
578 | /* A = X */ | |
579 | case BPF_MISC | BPF_TXA: | |
580 | *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X); | |
581 | break; | |
582 | ||
583 | /* A = skb->len or X = skb->len */ | |
584 | case BPF_LD | BPF_W | BPF_LEN: | |
585 | case BPF_LDX | BPF_W | BPF_LEN: | |
586 | *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ? | |
587 | BPF_REG_A : BPF_REG_X, BPF_REG_CTX, | |
588 | offsetof(struct sk_buff, len)); | |
589 | break; | |
590 | ||
591 | /* Access seccomp_data fields. */ | |
592 | case BPF_LDX | BPF_ABS | BPF_W: | |
593 | /* A = *(u32 *) (ctx + K) */ | |
594 | *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k); | |
595 | break; | |
596 | ||
597 | /* Unknown instruction. */ | |
598 | default: | |
599 | goto err; | |
600 | } | |
601 | ||
602 | insn++; | |
603 | if (new_prog) | |
604 | memcpy(new_insn, tmp_insns, | |
605 | sizeof(*insn) * (insn - tmp_insns)); | |
606 | new_insn += insn - tmp_insns; | |
607 | } | |
608 | ||
609 | if (!new_prog) { | |
610 | /* Only calculating new length. */ | |
611 | *new_len = new_insn - new_prog; | |
612 | return 0; | |
613 | } | |
614 | ||
615 | pass++; | |
616 | if (new_flen != new_insn - new_prog) { | |
617 | new_flen = new_insn - new_prog; | |
618 | if (pass > 2) | |
619 | goto err; | |
620 | goto do_pass; | |
621 | } | |
622 | ||
623 | kfree(addrs); | |
624 | BUG_ON(*new_len != new_flen); | |
625 | return 0; | |
626 | err: | |
627 | kfree(addrs); | |
628 | return -EINVAL; | |
629 | } | |
630 | ||
631 | /* Security: | |
632 | * | |
633 | * As we dont want to clear mem[] array for each packet going through | |
634 | * __bpf_prog_run(), we check that filter loaded by user never try to read | |
635 | * a cell if not previously written, and we check all branches to be sure | |
636 | * a malicious user doesn't try to abuse us. | |
637 | */ | |
638 | static int check_load_and_stores(const struct sock_filter *filter, int flen) | |
639 | { | |
640 | u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */ | |
641 | int pc, ret = 0; | |
642 | ||
643 | BUILD_BUG_ON(BPF_MEMWORDS > 16); | |
644 | ||
645 | masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL); | |
646 | if (!masks) | |
647 | return -ENOMEM; | |
648 | ||
649 | memset(masks, 0xff, flen * sizeof(*masks)); | |
650 | ||
651 | for (pc = 0; pc < flen; pc++) { | |
652 | memvalid &= masks[pc]; | |
653 | ||
654 | switch (filter[pc].code) { | |
655 | case BPF_ST: | |
656 | case BPF_STX: | |
657 | memvalid |= (1 << filter[pc].k); | |
658 | break; | |
659 | case BPF_LD | BPF_MEM: | |
660 | case BPF_LDX | BPF_MEM: | |
661 | if (!(memvalid & (1 << filter[pc].k))) { | |
662 | ret = -EINVAL; | |
663 | goto error; | |
664 | } | |
665 | break; | |
666 | case BPF_JMP | BPF_JA: | |
667 | /* A jump must set masks on target */ | |
668 | masks[pc + 1 + filter[pc].k] &= memvalid; | |
669 | memvalid = ~0; | |
670 | break; | |
671 | case BPF_JMP | BPF_JEQ | BPF_K: | |
672 | case BPF_JMP | BPF_JEQ | BPF_X: | |
673 | case BPF_JMP | BPF_JGE | BPF_K: | |
674 | case BPF_JMP | BPF_JGE | BPF_X: | |
675 | case BPF_JMP | BPF_JGT | BPF_K: | |
676 | case BPF_JMP | BPF_JGT | BPF_X: | |
677 | case BPF_JMP | BPF_JSET | BPF_K: | |
678 | case BPF_JMP | BPF_JSET | BPF_X: | |
679 | /* A jump must set masks on targets */ | |
680 | masks[pc + 1 + filter[pc].jt] &= memvalid; | |
681 | masks[pc + 1 + filter[pc].jf] &= memvalid; | |
682 | memvalid = ~0; | |
683 | break; | |
684 | } | |
685 | } | |
686 | error: | |
687 | kfree(masks); | |
688 | return ret; | |
689 | } | |
690 | ||
691 | static bool chk_code_allowed(u16 code_to_probe) | |
692 | { | |
693 | static const bool codes[] = { | |
694 | /* 32 bit ALU operations */ | |
695 | [BPF_ALU | BPF_ADD | BPF_K] = true, | |
696 | [BPF_ALU | BPF_ADD | BPF_X] = true, | |
697 | [BPF_ALU | BPF_SUB | BPF_K] = true, | |
698 | [BPF_ALU | BPF_SUB | BPF_X] = true, | |
699 | [BPF_ALU | BPF_MUL | BPF_K] = true, | |
700 | [BPF_ALU | BPF_MUL | BPF_X] = true, | |
701 | [BPF_ALU | BPF_DIV | BPF_K] = true, | |
702 | [BPF_ALU | BPF_DIV | BPF_X] = true, | |
703 | [BPF_ALU | BPF_MOD | BPF_K] = true, | |
704 | [BPF_ALU | BPF_MOD | BPF_X] = true, | |
705 | [BPF_ALU | BPF_AND | BPF_K] = true, | |
706 | [BPF_ALU | BPF_AND | BPF_X] = true, | |
707 | [BPF_ALU | BPF_OR | BPF_K] = true, | |
708 | [BPF_ALU | BPF_OR | BPF_X] = true, | |
709 | [BPF_ALU | BPF_XOR | BPF_K] = true, | |
710 | [BPF_ALU | BPF_XOR | BPF_X] = true, | |
711 | [BPF_ALU | BPF_LSH | BPF_K] = true, | |
712 | [BPF_ALU | BPF_LSH | BPF_X] = true, | |
713 | [BPF_ALU | BPF_RSH | BPF_K] = true, | |
714 | [BPF_ALU | BPF_RSH | BPF_X] = true, | |
715 | [BPF_ALU | BPF_NEG] = true, | |
716 | /* Load instructions */ | |
717 | [BPF_LD | BPF_W | BPF_ABS] = true, | |
718 | [BPF_LD | BPF_H | BPF_ABS] = true, | |
719 | [BPF_LD | BPF_B | BPF_ABS] = true, | |
720 | [BPF_LD | BPF_W | BPF_LEN] = true, | |
721 | [BPF_LD | BPF_W | BPF_IND] = true, | |
722 | [BPF_LD | BPF_H | BPF_IND] = true, | |
723 | [BPF_LD | BPF_B | BPF_IND] = true, | |
724 | [BPF_LD | BPF_IMM] = true, | |
725 | [BPF_LD | BPF_MEM] = true, | |
726 | [BPF_LDX | BPF_W | BPF_LEN] = true, | |
727 | [BPF_LDX | BPF_B | BPF_MSH] = true, | |
728 | [BPF_LDX | BPF_IMM] = true, | |
729 | [BPF_LDX | BPF_MEM] = true, | |
730 | /* Store instructions */ | |
731 | [BPF_ST] = true, | |
732 | [BPF_STX] = true, | |
733 | /* Misc instructions */ | |
734 | [BPF_MISC | BPF_TAX] = true, | |
735 | [BPF_MISC | BPF_TXA] = true, | |
736 | /* Return instructions */ | |
737 | [BPF_RET | BPF_K] = true, | |
738 | [BPF_RET | BPF_A] = true, | |
739 | /* Jump instructions */ | |
740 | [BPF_JMP | BPF_JA] = true, | |
741 | [BPF_JMP | BPF_JEQ | BPF_K] = true, | |
742 | [BPF_JMP | BPF_JEQ | BPF_X] = true, | |
743 | [BPF_JMP | BPF_JGE | BPF_K] = true, | |
744 | [BPF_JMP | BPF_JGE | BPF_X] = true, | |
745 | [BPF_JMP | BPF_JGT | BPF_K] = true, | |
746 | [BPF_JMP | BPF_JGT | BPF_X] = true, | |
747 | [BPF_JMP | BPF_JSET | BPF_K] = true, | |
748 | [BPF_JMP | BPF_JSET | BPF_X] = true, | |
749 | }; | |
750 | ||
751 | if (code_to_probe >= ARRAY_SIZE(codes)) | |
752 | return false; | |
753 | ||
754 | return codes[code_to_probe]; | |
755 | } | |
756 | ||
757 | static bool bpf_check_basics_ok(const struct sock_filter *filter, | |
758 | unsigned int flen) | |
759 | { | |
760 | if (filter == NULL) | |
761 | return false; | |
762 | if (flen == 0 || flen > BPF_MAXINSNS) | |
763 | return false; | |
764 | ||
765 | return true; | |
766 | } | |
767 | ||
768 | /** | |
769 | * bpf_check_classic - verify socket filter code | |
770 | * @filter: filter to verify | |
771 | * @flen: length of filter | |
772 | * | |
773 | * Check the user's filter code. If we let some ugly | |
774 | * filter code slip through kaboom! The filter must contain | |
775 | * no references or jumps that are out of range, no illegal | |
776 | * instructions, and must end with a RET instruction. | |
777 | * | |
778 | * All jumps are forward as they are not signed. | |
779 | * | |
780 | * Returns 0 if the rule set is legal or -EINVAL if not. | |
781 | */ | |
782 | static int bpf_check_classic(const struct sock_filter *filter, | |
783 | unsigned int flen) | |
784 | { | |
785 | bool anc_found; | |
786 | int pc; | |
787 | ||
788 | /* Check the filter code now */ | |
789 | for (pc = 0; pc < flen; pc++) { | |
790 | const struct sock_filter *ftest = &filter[pc]; | |
791 | ||
792 | /* May we actually operate on this code? */ | |
793 | if (!chk_code_allowed(ftest->code)) | |
794 | return -EINVAL; | |
795 | ||
796 | /* Some instructions need special checks */ | |
797 | switch (ftest->code) { | |
798 | case BPF_ALU | BPF_DIV | BPF_K: | |
799 | case BPF_ALU | BPF_MOD | BPF_K: | |
800 | /* Check for division by zero */ | |
801 | if (ftest->k == 0) | |
802 | return -EINVAL; | |
803 | break; | |
804 | case BPF_ALU | BPF_LSH | BPF_K: | |
805 | case BPF_ALU | BPF_RSH | BPF_K: | |
806 | if (ftest->k >= 32) | |
807 | return -EINVAL; | |
808 | break; | |
809 | case BPF_LD | BPF_MEM: | |
810 | case BPF_LDX | BPF_MEM: | |
811 | case BPF_ST: | |
812 | case BPF_STX: | |
813 | /* Check for invalid memory addresses */ | |
814 | if (ftest->k >= BPF_MEMWORDS) | |
815 | return -EINVAL; | |
816 | break; | |
817 | case BPF_JMP | BPF_JA: | |
818 | /* Note, the large ftest->k might cause loops. | |
819 | * Compare this with conditional jumps below, | |
820 | * where offsets are limited. --ANK (981016) | |
821 | */ | |
822 | if (ftest->k >= (unsigned int)(flen - pc - 1)) | |
823 | return -EINVAL; | |
824 | break; | |
825 | case BPF_JMP | BPF_JEQ | BPF_K: | |
826 | case BPF_JMP | BPF_JEQ | BPF_X: | |
827 | case BPF_JMP | BPF_JGE | BPF_K: | |
828 | case BPF_JMP | BPF_JGE | BPF_X: | |
829 | case BPF_JMP | BPF_JGT | BPF_K: | |
830 | case BPF_JMP | BPF_JGT | BPF_X: | |
831 | case BPF_JMP | BPF_JSET | BPF_K: | |
832 | case BPF_JMP | BPF_JSET | BPF_X: | |
833 | /* Both conditionals must be safe */ | |
834 | if (pc + ftest->jt + 1 >= flen || | |
835 | pc + ftest->jf + 1 >= flen) | |
836 | return -EINVAL; | |
837 | break; | |
838 | case BPF_LD | BPF_W | BPF_ABS: | |
839 | case BPF_LD | BPF_H | BPF_ABS: | |
840 | case BPF_LD | BPF_B | BPF_ABS: | |
841 | anc_found = false; | |
842 | if (bpf_anc_helper(ftest) & BPF_ANC) | |
843 | anc_found = true; | |
844 | /* Ancillary operation unknown or unsupported */ | |
845 | if (anc_found == false && ftest->k >= SKF_AD_OFF) | |
846 | return -EINVAL; | |
847 | } | |
848 | } | |
849 | ||
850 | /* Last instruction must be a RET code */ | |
851 | switch (filter[flen - 1].code) { | |
852 | case BPF_RET | BPF_K: | |
853 | case BPF_RET | BPF_A: | |
854 | return check_load_and_stores(filter, flen); | |
855 | } | |
856 | ||
857 | return -EINVAL; | |
858 | } | |
859 | ||
860 | static int bpf_prog_store_orig_filter(struct bpf_prog *fp, | |
861 | const struct sock_fprog *fprog) | |
862 | { | |
863 | unsigned int fsize = bpf_classic_proglen(fprog); | |
864 | struct sock_fprog_kern *fkprog; | |
865 | ||
866 | fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL); | |
867 | if (!fp->orig_prog) | |
868 | return -ENOMEM; | |
869 | ||
870 | fkprog = fp->orig_prog; | |
871 | fkprog->len = fprog->len; | |
872 | ||
873 | fkprog->filter = kmemdup(fp->insns, fsize, | |
874 | GFP_KERNEL | __GFP_NOWARN); | |
875 | if (!fkprog->filter) { | |
876 | kfree(fp->orig_prog); | |
877 | return -ENOMEM; | |
878 | } | |
879 | ||
880 | return 0; | |
881 | } | |
882 | ||
883 | static void bpf_release_orig_filter(struct bpf_prog *fp) | |
884 | { | |
885 | struct sock_fprog_kern *fprog = fp->orig_prog; | |
886 | ||
887 | if (fprog) { | |
888 | kfree(fprog->filter); | |
889 | kfree(fprog); | |
890 | } | |
891 | } | |
892 | ||
893 | static void __bpf_prog_release(struct bpf_prog *prog) | |
894 | { | |
895 | if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) { | |
896 | bpf_prog_put(prog); | |
897 | } else { | |
898 | bpf_release_orig_filter(prog); | |
899 | bpf_prog_free(prog); | |
900 | } | |
901 | } | |
902 | ||
903 | static void __sk_filter_release(struct sk_filter *fp) | |
904 | { | |
905 | __bpf_prog_release(fp->prog); | |
906 | kfree(fp); | |
907 | } | |
908 | ||
909 | /** | |
910 | * sk_filter_release_rcu - Release a socket filter by rcu_head | |
911 | * @rcu: rcu_head that contains the sk_filter to free | |
912 | */ | |
913 | static void sk_filter_release_rcu(struct rcu_head *rcu) | |
914 | { | |
915 | struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu); | |
916 | ||
917 | __sk_filter_release(fp); | |
918 | } | |
919 | ||
920 | /** | |
921 | * sk_filter_release - release a socket filter | |
922 | * @fp: filter to remove | |
923 | * | |
924 | * Remove a filter from a socket and release its resources. | |
925 | */ | |
926 | static void sk_filter_release(struct sk_filter *fp) | |
927 | { | |
928 | if (atomic_dec_and_test(&fp->refcnt)) | |
929 | call_rcu(&fp->rcu, sk_filter_release_rcu); | |
930 | } | |
931 | ||
932 | void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp) | |
933 | { | |
934 | u32 filter_size = bpf_prog_size(fp->prog->len); | |
935 | ||
936 | atomic_sub(filter_size, &sk->sk_omem_alloc); | |
937 | sk_filter_release(fp); | |
938 | } | |
939 | ||
940 | /* try to charge the socket memory if there is space available | |
941 | * return true on success | |
942 | */ | |
943 | bool sk_filter_charge(struct sock *sk, struct sk_filter *fp) | |
944 | { | |
945 | u32 filter_size = bpf_prog_size(fp->prog->len); | |
946 | ||
947 | /* same check as in sock_kmalloc() */ | |
948 | if (filter_size <= sysctl_optmem_max && | |
949 | atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) { | |
950 | atomic_inc(&fp->refcnt); | |
951 | atomic_add(filter_size, &sk->sk_omem_alloc); | |
952 | return true; | |
953 | } | |
954 | return false; | |
955 | } | |
956 | ||
957 | static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) | |
958 | { | |
959 | struct sock_filter *old_prog; | |
960 | struct bpf_prog *old_fp; | |
961 | int err, new_len, old_len = fp->len; | |
962 | ||
963 | /* We are free to overwrite insns et al right here as it | |
964 | * won't be used at this point in time anymore internally | |
965 | * after the migration to the internal BPF instruction | |
966 | * representation. | |
967 | */ | |
968 | BUILD_BUG_ON(sizeof(struct sock_filter) != | |
969 | sizeof(struct bpf_insn)); | |
970 | ||
971 | /* Conversion cannot happen on overlapping memory areas, | |
972 | * so we need to keep the user BPF around until the 2nd | |
973 | * pass. At this time, the user BPF is stored in fp->insns. | |
974 | */ | |
975 | old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter), | |
976 | GFP_KERNEL | __GFP_NOWARN); | |
977 | if (!old_prog) { | |
978 | err = -ENOMEM; | |
979 | goto out_err; | |
980 | } | |
981 | ||
982 | /* 1st pass: calculate the new program length. */ | |
983 | err = bpf_convert_filter(old_prog, old_len, NULL, &new_len); | |
984 | if (err) | |
985 | goto out_err_free; | |
986 | ||
987 | /* Expand fp for appending the new filter representation. */ | |
988 | old_fp = fp; | |
989 | fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0); | |
990 | if (!fp) { | |
991 | /* The old_fp is still around in case we couldn't | |
992 | * allocate new memory, so uncharge on that one. | |
993 | */ | |
994 | fp = old_fp; | |
995 | err = -ENOMEM; | |
996 | goto out_err_free; | |
997 | } | |
998 | ||
999 | fp->len = new_len; | |
1000 | ||
1001 | /* 2nd pass: remap sock_filter insns into bpf_insn insns. */ | |
1002 | err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len); | |
1003 | if (err) | |
1004 | /* 2nd bpf_convert_filter() can fail only if it fails | |
1005 | * to allocate memory, remapping must succeed. Note, | |
1006 | * that at this time old_fp has already been released | |
1007 | * by krealloc(). | |
1008 | */ | |
1009 | goto out_err_free; | |
1010 | ||
1011 | /* We are guaranteed to never error here with cBPF to eBPF | |
1012 | * transitions, since there's no issue with type compatibility | |
1013 | * checks on program arrays. | |
1014 | */ | |
1015 | fp = bpf_prog_select_runtime(fp, &err); | |
1016 | ||
1017 | kfree(old_prog); | |
1018 | return fp; | |
1019 | ||
1020 | out_err_free: | |
1021 | kfree(old_prog); | |
1022 | out_err: | |
1023 | __bpf_prog_release(fp); | |
1024 | return ERR_PTR(err); | |
1025 | } | |
1026 | ||
1027 | static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp, | |
1028 | bpf_aux_classic_check_t trans) | |
1029 | { | |
1030 | int err; | |
1031 | ||
1032 | fp->bpf_func = NULL; | |
1033 | fp->jited = 0; | |
1034 | ||
1035 | err = bpf_check_classic(fp->insns, fp->len); | |
1036 | if (err) { | |
1037 | __bpf_prog_release(fp); | |
1038 | return ERR_PTR(err); | |
1039 | } | |
1040 | ||
1041 | /* There might be additional checks and transformations | |
1042 | * needed on classic filters, f.e. in case of seccomp. | |
1043 | */ | |
1044 | if (trans) { | |
1045 | err = trans(fp->insns, fp->len); | |
1046 | if (err) { | |
1047 | __bpf_prog_release(fp); | |
1048 | return ERR_PTR(err); | |
1049 | } | |
1050 | } | |
1051 | ||
1052 | /* Probe if we can JIT compile the filter and if so, do | |
1053 | * the compilation of the filter. | |
1054 | */ | |
1055 | bpf_jit_compile(fp); | |
1056 | ||
1057 | /* JIT compiler couldn't process this filter, so do the | |
1058 | * internal BPF translation for the optimized interpreter. | |
1059 | */ | |
1060 | if (!fp->jited) | |
1061 | fp = bpf_migrate_filter(fp); | |
1062 | ||
1063 | return fp; | |
1064 | } | |
1065 | ||
1066 | /** | |
1067 | * bpf_prog_create - create an unattached filter | |
1068 | * @pfp: the unattached filter that is created | |
1069 | * @fprog: the filter program | |
1070 | * | |
1071 | * Create a filter independent of any socket. We first run some | |
1072 | * sanity checks on it to make sure it does not explode on us later. | |
1073 | * If an error occurs or there is insufficient memory for the filter | |
1074 | * a negative errno code is returned. On success the return is zero. | |
1075 | */ | |
1076 | int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog) | |
1077 | { | |
1078 | unsigned int fsize = bpf_classic_proglen(fprog); | |
1079 | struct bpf_prog *fp; | |
1080 | ||
1081 | /* Make sure new filter is there and in the right amounts. */ | |
1082 | if (!bpf_check_basics_ok(fprog->filter, fprog->len)) | |
1083 | return -EINVAL; | |
1084 | ||
1085 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); | |
1086 | if (!fp) | |
1087 | return -ENOMEM; | |
1088 | ||
1089 | memcpy(fp->insns, fprog->filter, fsize); | |
1090 | ||
1091 | fp->len = fprog->len; | |
1092 | /* Since unattached filters are not copied back to user | |
1093 | * space through sk_get_filter(), we do not need to hold | |
1094 | * a copy here, and can spare us the work. | |
1095 | */ | |
1096 | fp->orig_prog = NULL; | |
1097 | ||
1098 | /* bpf_prepare_filter() already takes care of freeing | |
1099 | * memory in case something goes wrong. | |
1100 | */ | |
1101 | fp = bpf_prepare_filter(fp, NULL); | |
1102 | if (IS_ERR(fp)) | |
1103 | return PTR_ERR(fp); | |
1104 | ||
1105 | *pfp = fp; | |
1106 | return 0; | |
1107 | } | |
1108 | EXPORT_SYMBOL_GPL(bpf_prog_create); | |
1109 | ||
1110 | /** | |
1111 | * bpf_prog_create_from_user - create an unattached filter from user buffer | |
1112 | * @pfp: the unattached filter that is created | |
1113 | * @fprog: the filter program | |
1114 | * @trans: post-classic verifier transformation handler | |
1115 | * @save_orig: save classic BPF program | |
1116 | * | |
1117 | * This function effectively does the same as bpf_prog_create(), only | |
1118 | * that it builds up its insns buffer from user space provided buffer. | |
1119 | * It also allows for passing a bpf_aux_classic_check_t handler. | |
1120 | */ | |
1121 | int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog, | |
1122 | bpf_aux_classic_check_t trans, bool save_orig) | |
1123 | { | |
1124 | unsigned int fsize = bpf_classic_proglen(fprog); | |
1125 | struct bpf_prog *fp; | |
1126 | int err; | |
1127 | ||
1128 | /* Make sure new filter is there and in the right amounts. */ | |
1129 | if (!bpf_check_basics_ok(fprog->filter, fprog->len)) | |
1130 | return -EINVAL; | |
1131 | ||
1132 | fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); | |
1133 | if (!fp) | |
1134 | return -ENOMEM; | |
1135 | ||
1136 | if (copy_from_user(fp->insns, fprog->filter, fsize)) { | |
1137 | __bpf_prog_free(fp); | |
1138 | return -EFAULT; | |
1139 | } | |
1140 | ||
1141 | fp->len = fprog->len; | |
1142 | fp->orig_prog = NULL; | |
1143 | ||
1144 | if (save_orig) { | |
1145 | err = bpf_prog_store_orig_filter(fp, fprog); | |
1146 | if (err) { | |
1147 | __bpf_prog_free(fp); | |
1148 | return -ENOMEM; | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | /* bpf_prepare_filter() already takes care of freeing | |
1153 | * memory in case something goes wrong. | |
1154 | */ | |
1155 | fp = bpf_prepare_filter(fp, trans); | |
1156 | if (IS_ERR(fp)) | |
1157 | return PTR_ERR(fp); | |
1158 | ||
1159 | *pfp = fp; | |
1160 | return 0; | |
1161 | } | |
1162 | EXPORT_SYMBOL_GPL(bpf_prog_create_from_user); | |
1163 | ||
1164 | void bpf_prog_destroy(struct bpf_prog *fp) | |
1165 | { | |
1166 | __bpf_prog_release(fp); | |
1167 | } | |
1168 | EXPORT_SYMBOL_GPL(bpf_prog_destroy); | |
1169 | ||
1170 | static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk) | |
1171 | { | |
1172 | struct sk_filter *fp, *old_fp; | |
1173 | ||
1174 | fp = kmalloc(sizeof(*fp), GFP_KERNEL); | |
1175 | if (!fp) | |
1176 | return -ENOMEM; | |
1177 | ||
1178 | fp->prog = prog; | |
1179 | atomic_set(&fp->refcnt, 0); | |
1180 | ||
1181 | if (!sk_filter_charge(sk, fp)) { | |
1182 | kfree(fp); | |
1183 | return -ENOMEM; | |
1184 | } | |
1185 | ||
1186 | old_fp = rcu_dereference_protected(sk->sk_filter, | |
1187 | lockdep_sock_is_held(sk)); | |
1188 | rcu_assign_pointer(sk->sk_filter, fp); | |
1189 | ||
1190 | if (old_fp) | |
1191 | sk_filter_uncharge(sk, old_fp); | |
1192 | ||
1193 | return 0; | |
1194 | } | |
1195 | ||
1196 | static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk) | |
1197 | { | |
1198 | struct bpf_prog *old_prog; | |
1199 | int err; | |
1200 | ||
1201 | if (bpf_prog_size(prog->len) > sysctl_optmem_max) | |
1202 | return -ENOMEM; | |
1203 | ||
1204 | if (sk_unhashed(sk) && sk->sk_reuseport) { | |
1205 | err = reuseport_alloc(sk); | |
1206 | if (err) | |
1207 | return err; | |
1208 | } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) { | |
1209 | /* The socket wasn't bound with SO_REUSEPORT */ | |
1210 | return -EINVAL; | |
1211 | } | |
1212 | ||
1213 | old_prog = reuseport_attach_prog(sk, prog); | |
1214 | if (old_prog) | |
1215 | bpf_prog_destroy(old_prog); | |
1216 | ||
1217 | return 0; | |
1218 | } | |
1219 | ||
1220 | static | |
1221 | struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk) | |
1222 | { | |
1223 | unsigned int fsize = bpf_classic_proglen(fprog); | |
1224 | struct bpf_prog *prog; | |
1225 | int err; | |
1226 | ||
1227 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) | |
1228 | return ERR_PTR(-EPERM); | |
1229 | ||
1230 | /* Make sure new filter is there and in the right amounts. */ | |
1231 | if (!bpf_check_basics_ok(fprog->filter, fprog->len)) | |
1232 | return ERR_PTR(-EINVAL); | |
1233 | ||
1234 | prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0); | |
1235 | if (!prog) | |
1236 | return ERR_PTR(-ENOMEM); | |
1237 | ||
1238 | if (copy_from_user(prog->insns, fprog->filter, fsize)) { | |
1239 | __bpf_prog_free(prog); | |
1240 | return ERR_PTR(-EFAULT); | |
1241 | } | |
1242 | ||
1243 | prog->len = fprog->len; | |
1244 | ||
1245 | err = bpf_prog_store_orig_filter(prog, fprog); | |
1246 | if (err) { | |
1247 | __bpf_prog_free(prog); | |
1248 | return ERR_PTR(-ENOMEM); | |
1249 | } | |
1250 | ||
1251 | /* bpf_prepare_filter() already takes care of freeing | |
1252 | * memory in case something goes wrong. | |
1253 | */ | |
1254 | return bpf_prepare_filter(prog, NULL); | |
1255 | } | |
1256 | ||
1257 | /** | |
1258 | * sk_attach_filter - attach a socket filter | |
1259 | * @fprog: the filter program | |
1260 | * @sk: the socket to use | |
1261 | * | |
1262 | * Attach the user's filter code. We first run some sanity checks on | |
1263 | * it to make sure it does not explode on us later. If an error | |
1264 | * occurs or there is insufficient memory for the filter a negative | |
1265 | * errno code is returned. On success the return is zero. | |
1266 | */ | |
1267 | int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) | |
1268 | { | |
1269 | struct bpf_prog *prog = __get_filter(fprog, sk); | |
1270 | int err; | |
1271 | ||
1272 | if (IS_ERR(prog)) | |
1273 | return PTR_ERR(prog); | |
1274 | ||
1275 | err = __sk_attach_prog(prog, sk); | |
1276 | if (err < 0) { | |
1277 | __bpf_prog_release(prog); | |
1278 | return err; | |
1279 | } | |
1280 | ||
1281 | return 0; | |
1282 | } | |
1283 | EXPORT_SYMBOL_GPL(sk_attach_filter); | |
1284 | ||
1285 | int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk) | |
1286 | { | |
1287 | struct bpf_prog *prog = __get_filter(fprog, sk); | |
1288 | int err; | |
1289 | ||
1290 | if (IS_ERR(prog)) | |
1291 | return PTR_ERR(prog); | |
1292 | ||
1293 | err = __reuseport_attach_prog(prog, sk); | |
1294 | if (err < 0) { | |
1295 | __bpf_prog_release(prog); | |
1296 | return err; | |
1297 | } | |
1298 | ||
1299 | return 0; | |
1300 | } | |
1301 | ||
1302 | static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk) | |
1303 | { | |
1304 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) | |
1305 | return ERR_PTR(-EPERM); | |
1306 | ||
1307 | return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER); | |
1308 | } | |
1309 | ||
1310 | int sk_attach_bpf(u32 ufd, struct sock *sk) | |
1311 | { | |
1312 | struct bpf_prog *prog = __get_bpf(ufd, sk); | |
1313 | int err; | |
1314 | ||
1315 | if (IS_ERR(prog)) | |
1316 | return PTR_ERR(prog); | |
1317 | ||
1318 | err = __sk_attach_prog(prog, sk); | |
1319 | if (err < 0) { | |
1320 | bpf_prog_put(prog); | |
1321 | return err; | |
1322 | } | |
1323 | ||
1324 | return 0; | |
1325 | } | |
1326 | ||
1327 | int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk) | |
1328 | { | |
1329 | struct bpf_prog *prog = __get_bpf(ufd, sk); | |
1330 | int err; | |
1331 | ||
1332 | if (IS_ERR(prog)) | |
1333 | return PTR_ERR(prog); | |
1334 | ||
1335 | err = __reuseport_attach_prog(prog, sk); | |
1336 | if (err < 0) { | |
1337 | bpf_prog_put(prog); | |
1338 | return err; | |
1339 | } | |
1340 | ||
1341 | return 0; | |
1342 | } | |
1343 | ||
1344 | struct bpf_scratchpad { | |
1345 | union { | |
1346 | __be32 diff[MAX_BPF_STACK / sizeof(__be32)]; | |
1347 | u8 buff[MAX_BPF_STACK]; | |
1348 | }; | |
1349 | }; | |
1350 | ||
1351 | static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp); | |
1352 | ||
1353 | static inline int bpf_try_make_writable(struct sk_buff *skb, | |
1354 | unsigned int write_len) | |
1355 | { | |
1356 | int err; | |
1357 | ||
1358 | if (!skb_cloned(skb)) | |
1359 | return 0; | |
1360 | if (skb_clone_writable(skb, write_len)) | |
1361 | return 0; | |
1362 | err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); | |
1363 | if (!err) | |
1364 | bpf_compute_data_end(skb); | |
1365 | return err; | |
1366 | } | |
1367 | ||
1368 | static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags) | |
1369 | { | |
1370 | struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp); | |
1371 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1372 | int offset = (int) r2; | |
1373 | void *from = (void *) (long) r3; | |
1374 | unsigned int len = (unsigned int) r4; | |
1375 | void *ptr; | |
1376 | ||
1377 | if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH))) | |
1378 | return -EINVAL; | |
1379 | ||
1380 | /* bpf verifier guarantees that: | |
1381 | * 'from' pointer points to bpf program stack | |
1382 | * 'len' bytes of it were initialized | |
1383 | * 'len' > 0 | |
1384 | * 'skb' is a valid pointer to 'struct sk_buff' | |
1385 | * | |
1386 | * so check for invalid 'offset' and too large 'len' | |
1387 | */ | |
1388 | if (unlikely((u32) offset > 0xffff || len > sizeof(sp->buff))) | |
1389 | return -EFAULT; | |
1390 | if (unlikely(bpf_try_make_writable(skb, offset + len))) | |
1391 | return -EFAULT; | |
1392 | ||
1393 | ptr = skb_header_pointer(skb, offset, len, sp->buff); | |
1394 | if (unlikely(!ptr)) | |
1395 | return -EFAULT; | |
1396 | ||
1397 | if (flags & BPF_F_RECOMPUTE_CSUM) | |
1398 | skb_postpull_rcsum(skb, ptr, len); | |
1399 | ||
1400 | memcpy(ptr, from, len); | |
1401 | ||
1402 | if (ptr == sp->buff) | |
1403 | /* skb_store_bits cannot return -EFAULT here */ | |
1404 | skb_store_bits(skb, offset, ptr, len); | |
1405 | ||
1406 | if (flags & BPF_F_RECOMPUTE_CSUM) | |
1407 | skb_postpush_rcsum(skb, ptr, len); | |
1408 | if (flags & BPF_F_INVALIDATE_HASH) | |
1409 | skb_clear_hash(skb); | |
1410 | ||
1411 | return 0; | |
1412 | } | |
1413 | ||
1414 | static const struct bpf_func_proto bpf_skb_store_bytes_proto = { | |
1415 | .func = bpf_skb_store_bytes, | |
1416 | .gpl_only = false, | |
1417 | .ret_type = RET_INTEGER, | |
1418 | .arg1_type = ARG_PTR_TO_CTX, | |
1419 | .arg2_type = ARG_ANYTHING, | |
1420 | .arg3_type = ARG_PTR_TO_STACK, | |
1421 | .arg4_type = ARG_CONST_STACK_SIZE, | |
1422 | .arg5_type = ARG_ANYTHING, | |
1423 | }; | |
1424 | ||
1425 | static u64 bpf_skb_load_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
1426 | { | |
1427 | const struct sk_buff *skb = (const struct sk_buff *)(unsigned long) r1; | |
1428 | int offset = (int) r2; | |
1429 | void *to = (void *)(unsigned long) r3; | |
1430 | unsigned int len = (unsigned int) r4; | |
1431 | void *ptr; | |
1432 | ||
1433 | if (unlikely((u32) offset > 0xffff)) | |
1434 | goto err_clear; | |
1435 | ||
1436 | ptr = skb_header_pointer(skb, offset, len, to); | |
1437 | if (unlikely(!ptr)) | |
1438 | goto err_clear; | |
1439 | if (ptr != to) | |
1440 | memcpy(to, ptr, len); | |
1441 | ||
1442 | return 0; | |
1443 | err_clear: | |
1444 | memset(to, 0, len); | |
1445 | return -EFAULT; | |
1446 | } | |
1447 | ||
1448 | static const struct bpf_func_proto bpf_skb_load_bytes_proto = { | |
1449 | .func = bpf_skb_load_bytes, | |
1450 | .gpl_only = false, | |
1451 | .ret_type = RET_INTEGER, | |
1452 | .arg1_type = ARG_PTR_TO_CTX, | |
1453 | .arg2_type = ARG_ANYTHING, | |
1454 | .arg3_type = ARG_PTR_TO_RAW_STACK, | |
1455 | .arg4_type = ARG_CONST_STACK_SIZE, | |
1456 | }; | |
1457 | ||
1458 | static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags) | |
1459 | { | |
1460 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1461 | int offset = (int) r2; | |
1462 | __sum16 sum, *ptr; | |
1463 | ||
1464 | if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK))) | |
1465 | return -EINVAL; | |
1466 | if (unlikely((u32) offset > 0xffff)) | |
1467 | return -EFAULT; | |
1468 | if (unlikely(bpf_try_make_writable(skb, offset + sizeof(sum)))) | |
1469 | return -EFAULT; | |
1470 | ||
1471 | ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum); | |
1472 | if (unlikely(!ptr)) | |
1473 | return -EFAULT; | |
1474 | ||
1475 | switch (flags & BPF_F_HDR_FIELD_MASK) { | |
1476 | case 0: | |
1477 | if (unlikely(from != 0)) | |
1478 | return -EINVAL; | |
1479 | ||
1480 | csum_replace_by_diff(ptr, to); | |
1481 | break; | |
1482 | case 2: | |
1483 | csum_replace2(ptr, from, to); | |
1484 | break; | |
1485 | case 4: | |
1486 | csum_replace4(ptr, from, to); | |
1487 | break; | |
1488 | default: | |
1489 | return -EINVAL; | |
1490 | } | |
1491 | ||
1492 | if (ptr == &sum) | |
1493 | /* skb_store_bits guaranteed to not return -EFAULT here */ | |
1494 | skb_store_bits(skb, offset, ptr, sizeof(sum)); | |
1495 | ||
1496 | return 0; | |
1497 | } | |
1498 | ||
1499 | static const struct bpf_func_proto bpf_l3_csum_replace_proto = { | |
1500 | .func = bpf_l3_csum_replace, | |
1501 | .gpl_only = false, | |
1502 | .ret_type = RET_INTEGER, | |
1503 | .arg1_type = ARG_PTR_TO_CTX, | |
1504 | .arg2_type = ARG_ANYTHING, | |
1505 | .arg3_type = ARG_ANYTHING, | |
1506 | .arg4_type = ARG_ANYTHING, | |
1507 | .arg5_type = ARG_ANYTHING, | |
1508 | }; | |
1509 | ||
1510 | static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags) | |
1511 | { | |
1512 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1513 | bool is_pseudo = flags & BPF_F_PSEUDO_HDR; | |
1514 | bool is_mmzero = flags & BPF_F_MARK_MANGLED_0; | |
1515 | int offset = (int) r2; | |
1516 | __sum16 sum, *ptr; | |
1517 | ||
1518 | if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR | | |
1519 | BPF_F_HDR_FIELD_MASK))) | |
1520 | return -EINVAL; | |
1521 | if (unlikely((u32) offset > 0xffff)) | |
1522 | return -EFAULT; | |
1523 | if (unlikely(bpf_try_make_writable(skb, offset + sizeof(sum)))) | |
1524 | return -EFAULT; | |
1525 | ||
1526 | ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum); | |
1527 | if (unlikely(!ptr)) | |
1528 | return -EFAULT; | |
1529 | if (is_mmzero && !*ptr) | |
1530 | return 0; | |
1531 | ||
1532 | switch (flags & BPF_F_HDR_FIELD_MASK) { | |
1533 | case 0: | |
1534 | if (unlikely(from != 0)) | |
1535 | return -EINVAL; | |
1536 | ||
1537 | inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo); | |
1538 | break; | |
1539 | case 2: | |
1540 | inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo); | |
1541 | break; | |
1542 | case 4: | |
1543 | inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo); | |
1544 | break; | |
1545 | default: | |
1546 | return -EINVAL; | |
1547 | } | |
1548 | ||
1549 | if (is_mmzero && !*ptr) | |
1550 | *ptr = CSUM_MANGLED_0; | |
1551 | if (ptr == &sum) | |
1552 | /* skb_store_bits guaranteed to not return -EFAULT here */ | |
1553 | skb_store_bits(skb, offset, ptr, sizeof(sum)); | |
1554 | ||
1555 | return 0; | |
1556 | } | |
1557 | ||
1558 | static const struct bpf_func_proto bpf_l4_csum_replace_proto = { | |
1559 | .func = bpf_l4_csum_replace, | |
1560 | .gpl_only = false, | |
1561 | .ret_type = RET_INTEGER, | |
1562 | .arg1_type = ARG_PTR_TO_CTX, | |
1563 | .arg2_type = ARG_ANYTHING, | |
1564 | .arg3_type = ARG_ANYTHING, | |
1565 | .arg4_type = ARG_ANYTHING, | |
1566 | .arg5_type = ARG_ANYTHING, | |
1567 | }; | |
1568 | ||
1569 | static u64 bpf_csum_diff(u64 r1, u64 from_size, u64 r3, u64 to_size, u64 seed) | |
1570 | { | |
1571 | struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp); | |
1572 | u64 diff_size = from_size + to_size; | |
1573 | __be32 *from = (__be32 *) (long) r1; | |
1574 | __be32 *to = (__be32 *) (long) r3; | |
1575 | int i, j = 0; | |
1576 | ||
1577 | /* This is quite flexible, some examples: | |
1578 | * | |
1579 | * from_size == 0, to_size > 0, seed := csum --> pushing data | |
1580 | * from_size > 0, to_size == 0, seed := csum --> pulling data | |
1581 | * from_size > 0, to_size > 0, seed := 0 --> diffing data | |
1582 | * | |
1583 | * Even for diffing, from_size and to_size don't need to be equal. | |
1584 | */ | |
1585 | if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) || | |
1586 | diff_size > sizeof(sp->diff))) | |
1587 | return -EINVAL; | |
1588 | ||
1589 | for (i = 0; i < from_size / sizeof(__be32); i++, j++) | |
1590 | sp->diff[j] = ~from[i]; | |
1591 | for (i = 0; i < to_size / sizeof(__be32); i++, j++) | |
1592 | sp->diff[j] = to[i]; | |
1593 | ||
1594 | return csum_partial(sp->diff, diff_size, seed); | |
1595 | } | |
1596 | ||
1597 | static const struct bpf_func_proto bpf_csum_diff_proto = { | |
1598 | .func = bpf_csum_diff, | |
1599 | .gpl_only = false, | |
1600 | .ret_type = RET_INTEGER, | |
1601 | .arg1_type = ARG_PTR_TO_STACK, | |
1602 | .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO, | |
1603 | .arg3_type = ARG_PTR_TO_STACK, | |
1604 | .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO, | |
1605 | .arg5_type = ARG_ANYTHING, | |
1606 | }; | |
1607 | ||
1608 | static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb) | |
1609 | { | |
1610 | if (skb_at_tc_ingress(skb)) | |
1611 | skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len); | |
1612 | ||
1613 | return dev_forward_skb(dev, skb); | |
1614 | } | |
1615 | ||
1616 | static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb) | |
1617 | { | |
1618 | int ret; | |
1619 | ||
1620 | if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) { | |
1621 | net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n"); | |
1622 | kfree_skb(skb); | |
1623 | return -ENETDOWN; | |
1624 | } | |
1625 | ||
1626 | skb->dev = dev; | |
1627 | ||
1628 | __this_cpu_inc(xmit_recursion); | |
1629 | ret = dev_queue_xmit(skb); | |
1630 | __this_cpu_dec(xmit_recursion); | |
1631 | ||
1632 | return ret; | |
1633 | } | |
1634 | ||
1635 | static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5) | |
1636 | { | |
1637 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1638 | struct net_device *dev; | |
1639 | ||
1640 | if (unlikely(flags & ~(BPF_F_INGRESS))) | |
1641 | return -EINVAL; | |
1642 | ||
1643 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex); | |
1644 | if (unlikely(!dev)) | |
1645 | return -EINVAL; | |
1646 | ||
1647 | skb = skb_clone(skb, GFP_ATOMIC); | |
1648 | if (unlikely(!skb)) | |
1649 | return -ENOMEM; | |
1650 | ||
1651 | return flags & BPF_F_INGRESS ? | |
1652 | __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb); | |
1653 | } | |
1654 | ||
1655 | static const struct bpf_func_proto bpf_clone_redirect_proto = { | |
1656 | .func = bpf_clone_redirect, | |
1657 | .gpl_only = false, | |
1658 | .ret_type = RET_INTEGER, | |
1659 | .arg1_type = ARG_PTR_TO_CTX, | |
1660 | .arg2_type = ARG_ANYTHING, | |
1661 | .arg3_type = ARG_ANYTHING, | |
1662 | }; | |
1663 | ||
1664 | struct redirect_info { | |
1665 | u32 ifindex; | |
1666 | u32 flags; | |
1667 | }; | |
1668 | ||
1669 | static DEFINE_PER_CPU(struct redirect_info, redirect_info); | |
1670 | ||
1671 | static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5) | |
1672 | { | |
1673 | struct redirect_info *ri = this_cpu_ptr(&redirect_info); | |
1674 | ||
1675 | if (unlikely(flags & ~(BPF_F_INGRESS))) | |
1676 | return TC_ACT_SHOT; | |
1677 | ||
1678 | ri->ifindex = ifindex; | |
1679 | ri->flags = flags; | |
1680 | ||
1681 | return TC_ACT_REDIRECT; | |
1682 | } | |
1683 | ||
1684 | int skb_do_redirect(struct sk_buff *skb) | |
1685 | { | |
1686 | struct redirect_info *ri = this_cpu_ptr(&redirect_info); | |
1687 | struct net_device *dev; | |
1688 | ||
1689 | dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex); | |
1690 | ri->ifindex = 0; | |
1691 | if (unlikely(!dev)) { | |
1692 | kfree_skb(skb); | |
1693 | return -EINVAL; | |
1694 | } | |
1695 | ||
1696 | return ri->flags & BPF_F_INGRESS ? | |
1697 | __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb); | |
1698 | } | |
1699 | ||
1700 | static const struct bpf_func_proto bpf_redirect_proto = { | |
1701 | .func = bpf_redirect, | |
1702 | .gpl_only = false, | |
1703 | .ret_type = RET_INTEGER, | |
1704 | .arg1_type = ARG_ANYTHING, | |
1705 | .arg2_type = ARG_ANYTHING, | |
1706 | }; | |
1707 | ||
1708 | static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
1709 | { | |
1710 | return task_get_classid((struct sk_buff *) (unsigned long) r1); | |
1711 | } | |
1712 | ||
1713 | static const struct bpf_func_proto bpf_get_cgroup_classid_proto = { | |
1714 | .func = bpf_get_cgroup_classid, | |
1715 | .gpl_only = false, | |
1716 | .ret_type = RET_INTEGER, | |
1717 | .arg1_type = ARG_PTR_TO_CTX, | |
1718 | }; | |
1719 | ||
1720 | static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
1721 | { | |
1722 | return dst_tclassid((struct sk_buff *) (unsigned long) r1); | |
1723 | } | |
1724 | ||
1725 | static const struct bpf_func_proto bpf_get_route_realm_proto = { | |
1726 | .func = bpf_get_route_realm, | |
1727 | .gpl_only = false, | |
1728 | .ret_type = RET_INTEGER, | |
1729 | .arg1_type = ARG_PTR_TO_CTX, | |
1730 | }; | |
1731 | ||
1732 | static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5) | |
1733 | { | |
1734 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1735 | __be16 vlan_proto = (__force __be16) r2; | |
1736 | int ret; | |
1737 | ||
1738 | if (unlikely(vlan_proto != htons(ETH_P_8021Q) && | |
1739 | vlan_proto != htons(ETH_P_8021AD))) | |
1740 | vlan_proto = htons(ETH_P_8021Q); | |
1741 | ||
1742 | ret = skb_vlan_push(skb, vlan_proto, vlan_tci); | |
1743 | bpf_compute_data_end(skb); | |
1744 | return ret; | |
1745 | } | |
1746 | ||
1747 | const struct bpf_func_proto bpf_skb_vlan_push_proto = { | |
1748 | .func = bpf_skb_vlan_push, | |
1749 | .gpl_only = false, | |
1750 | .ret_type = RET_INTEGER, | |
1751 | .arg1_type = ARG_PTR_TO_CTX, | |
1752 | .arg2_type = ARG_ANYTHING, | |
1753 | .arg3_type = ARG_ANYTHING, | |
1754 | }; | |
1755 | EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto); | |
1756 | ||
1757 | static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
1758 | { | |
1759 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1760 | int ret; | |
1761 | ||
1762 | ret = skb_vlan_pop(skb); | |
1763 | bpf_compute_data_end(skb); | |
1764 | return ret; | |
1765 | } | |
1766 | ||
1767 | const struct bpf_func_proto bpf_skb_vlan_pop_proto = { | |
1768 | .func = bpf_skb_vlan_pop, | |
1769 | .gpl_only = false, | |
1770 | .ret_type = RET_INTEGER, | |
1771 | .arg1_type = ARG_PTR_TO_CTX, | |
1772 | }; | |
1773 | EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto); | |
1774 | ||
1775 | static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len) | |
1776 | { | |
1777 | /* Caller already did skb_cow() with len as headroom, | |
1778 | * so no need to do it here. | |
1779 | */ | |
1780 | skb_push(skb, len); | |
1781 | memmove(skb->data, skb->data + len, off); | |
1782 | memset(skb->data + off, 0, len); | |
1783 | ||
1784 | /* No skb_postpush_rcsum(skb, skb->data + off, len) | |
1785 | * needed here as it does not change the skb->csum | |
1786 | * result for checksum complete when summing over | |
1787 | * zeroed blocks. | |
1788 | */ | |
1789 | return 0; | |
1790 | } | |
1791 | ||
1792 | static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len) | |
1793 | { | |
1794 | /* skb_ensure_writable() is not needed here, as we're | |
1795 | * already working on an uncloned skb. | |
1796 | */ | |
1797 | if (unlikely(!pskb_may_pull(skb, off + len))) | |
1798 | return -ENOMEM; | |
1799 | ||
1800 | skb_postpull_rcsum(skb, skb->data + off, len); | |
1801 | memmove(skb->data + len, skb->data, off); | |
1802 | __skb_pull(skb, len); | |
1803 | ||
1804 | return 0; | |
1805 | } | |
1806 | ||
1807 | static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len) | |
1808 | { | |
1809 | bool trans_same = skb->transport_header == skb->network_header; | |
1810 | int ret; | |
1811 | ||
1812 | /* There's no need for __skb_push()/__skb_pull() pair to | |
1813 | * get to the start of the mac header as we're guaranteed | |
1814 | * to always start from here under eBPF. | |
1815 | */ | |
1816 | ret = bpf_skb_generic_push(skb, off, len); | |
1817 | if (likely(!ret)) { | |
1818 | skb->mac_header -= len; | |
1819 | skb->network_header -= len; | |
1820 | if (trans_same) | |
1821 | skb->transport_header = skb->network_header; | |
1822 | } | |
1823 | ||
1824 | return ret; | |
1825 | } | |
1826 | ||
1827 | static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len) | |
1828 | { | |
1829 | bool trans_same = skb->transport_header == skb->network_header; | |
1830 | int ret; | |
1831 | ||
1832 | /* Same here, __skb_push()/__skb_pull() pair not needed. */ | |
1833 | ret = bpf_skb_generic_pop(skb, off, len); | |
1834 | if (likely(!ret)) { | |
1835 | skb->mac_header += len; | |
1836 | skb->network_header += len; | |
1837 | if (trans_same) | |
1838 | skb->transport_header = skb->network_header; | |
1839 | } | |
1840 | ||
1841 | return ret; | |
1842 | } | |
1843 | ||
1844 | static int bpf_skb_proto_4_to_6(struct sk_buff *skb) | |
1845 | { | |
1846 | const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); | |
1847 | u32 off = skb->network_header - skb->mac_header; | |
1848 | int ret; | |
1849 | ||
1850 | ret = skb_cow(skb, len_diff); | |
1851 | if (unlikely(ret < 0)) | |
1852 | return ret; | |
1853 | ||
1854 | ret = bpf_skb_net_hdr_push(skb, off, len_diff); | |
1855 | if (unlikely(ret < 0)) | |
1856 | return ret; | |
1857 | ||
1858 | if (skb_is_gso(skb)) { | |
1859 | /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to | |
1860 | * be changed into SKB_GSO_TCPV6. | |
1861 | */ | |
1862 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { | |
1863 | skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4; | |
1864 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6; | |
1865 | } | |
1866 | ||
1867 | /* Due to IPv6 header, MSS needs to be downgraded. */ | |
1868 | skb_shinfo(skb)->gso_size -= len_diff; | |
1869 | /* Header must be checked, and gso_segs recomputed. */ | |
1870 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
1871 | skb_shinfo(skb)->gso_segs = 0; | |
1872 | } | |
1873 | ||
1874 | skb->protocol = htons(ETH_P_IPV6); | |
1875 | skb_clear_hash(skb); | |
1876 | ||
1877 | return 0; | |
1878 | } | |
1879 | ||
1880 | static int bpf_skb_proto_6_to_4(struct sk_buff *skb) | |
1881 | { | |
1882 | const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr); | |
1883 | u32 off = skb->network_header - skb->mac_header; | |
1884 | int ret; | |
1885 | ||
1886 | ret = skb_unclone(skb, GFP_ATOMIC); | |
1887 | if (unlikely(ret < 0)) | |
1888 | return ret; | |
1889 | ||
1890 | ret = bpf_skb_net_hdr_pop(skb, off, len_diff); | |
1891 | if (unlikely(ret < 0)) | |
1892 | return ret; | |
1893 | ||
1894 | if (skb_is_gso(skb)) { | |
1895 | /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to | |
1896 | * be changed into SKB_GSO_TCPV4. | |
1897 | */ | |
1898 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) { | |
1899 | skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6; | |
1900 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4; | |
1901 | } | |
1902 | ||
1903 | /* Due to IPv4 header, MSS can be upgraded. */ | |
1904 | skb_shinfo(skb)->gso_size += len_diff; | |
1905 | /* Header must be checked, and gso_segs recomputed. */ | |
1906 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; | |
1907 | skb_shinfo(skb)->gso_segs = 0; | |
1908 | } | |
1909 | ||
1910 | skb->protocol = htons(ETH_P_IP); | |
1911 | skb_clear_hash(skb); | |
1912 | ||
1913 | return 0; | |
1914 | } | |
1915 | ||
1916 | static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto) | |
1917 | { | |
1918 | __be16 from_proto = skb->protocol; | |
1919 | ||
1920 | if (from_proto == htons(ETH_P_IP) && | |
1921 | to_proto == htons(ETH_P_IPV6)) | |
1922 | return bpf_skb_proto_4_to_6(skb); | |
1923 | ||
1924 | if (from_proto == htons(ETH_P_IPV6) && | |
1925 | to_proto == htons(ETH_P_IP)) | |
1926 | return bpf_skb_proto_6_to_4(skb); | |
1927 | ||
1928 | return -ENOTSUPP; | |
1929 | } | |
1930 | ||
1931 | static u64 bpf_skb_change_proto(u64 r1, u64 r2, u64 flags, u64 r4, u64 r5) | |
1932 | { | |
1933 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1934 | __be16 proto = (__force __be16) r2; | |
1935 | int ret; | |
1936 | ||
1937 | if (unlikely(flags)) | |
1938 | return -EINVAL; | |
1939 | ||
1940 | /* General idea is that this helper does the basic groundwork | |
1941 | * needed for changing the protocol, and eBPF program fills the | |
1942 | * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace() | |
1943 | * and other helpers, rather than passing a raw buffer here. | |
1944 | * | |
1945 | * The rationale is to keep this minimal and without a need to | |
1946 | * deal with raw packet data. F.e. even if we would pass buffers | |
1947 | * here, the program still needs to call the bpf_lX_csum_replace() | |
1948 | * helpers anyway. Plus, this way we keep also separation of | |
1949 | * concerns, since f.e. bpf_skb_store_bytes() should only take | |
1950 | * care of stores. | |
1951 | * | |
1952 | * Currently, additional options and extension header space are | |
1953 | * not supported, but flags register is reserved so we can adapt | |
1954 | * that. For offloads, we mark packet as dodgy, so that headers | |
1955 | * need to be verified first. | |
1956 | */ | |
1957 | ret = bpf_skb_proto_xlat(skb, proto); | |
1958 | bpf_compute_data_end(skb); | |
1959 | return ret; | |
1960 | } | |
1961 | ||
1962 | static const struct bpf_func_proto bpf_skb_change_proto_proto = { | |
1963 | .func = bpf_skb_change_proto, | |
1964 | .gpl_only = false, | |
1965 | .ret_type = RET_INTEGER, | |
1966 | .arg1_type = ARG_PTR_TO_CTX, | |
1967 | .arg2_type = ARG_ANYTHING, | |
1968 | .arg3_type = ARG_ANYTHING, | |
1969 | }; | |
1970 | ||
1971 | static u64 bpf_skb_change_type(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
1972 | { | |
1973 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
1974 | u32 pkt_type = r2; | |
1975 | ||
1976 | /* We only allow a restricted subset to be changed for now. */ | |
1977 | if (unlikely(skb->pkt_type > PACKET_OTHERHOST || | |
1978 | pkt_type > PACKET_OTHERHOST)) | |
1979 | return -EINVAL; | |
1980 | ||
1981 | skb->pkt_type = pkt_type; | |
1982 | return 0; | |
1983 | } | |
1984 | ||
1985 | static const struct bpf_func_proto bpf_skb_change_type_proto = { | |
1986 | .func = bpf_skb_change_type, | |
1987 | .gpl_only = false, | |
1988 | .ret_type = RET_INTEGER, | |
1989 | .arg1_type = ARG_PTR_TO_CTX, | |
1990 | .arg2_type = ARG_ANYTHING, | |
1991 | }; | |
1992 | ||
1993 | bool bpf_helper_changes_skb_data(void *func) | |
1994 | { | |
1995 | if (func == bpf_skb_vlan_push) | |
1996 | return true; | |
1997 | if (func == bpf_skb_vlan_pop) | |
1998 | return true; | |
1999 | if (func == bpf_skb_store_bytes) | |
2000 | return true; | |
2001 | if (func == bpf_skb_change_proto) | |
2002 | return true; | |
2003 | if (func == bpf_l3_csum_replace) | |
2004 | return true; | |
2005 | if (func == bpf_l4_csum_replace) | |
2006 | return true; | |
2007 | ||
2008 | return false; | |
2009 | } | |
2010 | ||
2011 | static unsigned short bpf_tunnel_key_af(u64 flags) | |
2012 | { | |
2013 | return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET; | |
2014 | } | |
2015 | ||
2016 | static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5) | |
2017 | { | |
2018 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
2019 | struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2; | |
2020 | const struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
2021 | u8 compat[sizeof(struct bpf_tunnel_key)]; | |
2022 | void *to_orig = to; | |
2023 | int err; | |
2024 | ||
2025 | if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) { | |
2026 | err = -EINVAL; | |
2027 | goto err_clear; | |
2028 | } | |
2029 | if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) { | |
2030 | err = -EPROTO; | |
2031 | goto err_clear; | |
2032 | } | |
2033 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { | |
2034 | err = -EINVAL; | |
2035 | switch (size) { | |
2036 | case offsetof(struct bpf_tunnel_key, tunnel_label): | |
2037 | case offsetof(struct bpf_tunnel_key, tunnel_ext): | |
2038 | goto set_compat; | |
2039 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): | |
2040 | /* Fixup deprecated structure layouts here, so we have | |
2041 | * a common path later on. | |
2042 | */ | |
2043 | if (ip_tunnel_info_af(info) != AF_INET) | |
2044 | goto err_clear; | |
2045 | set_compat: | |
2046 | to = (struct bpf_tunnel_key *)compat; | |
2047 | break; | |
2048 | default: | |
2049 | goto err_clear; | |
2050 | } | |
2051 | } | |
2052 | ||
2053 | to->tunnel_id = be64_to_cpu(info->key.tun_id); | |
2054 | to->tunnel_tos = info->key.tos; | |
2055 | to->tunnel_ttl = info->key.ttl; | |
2056 | ||
2057 | if (flags & BPF_F_TUNINFO_IPV6) { | |
2058 | memcpy(to->remote_ipv6, &info->key.u.ipv6.src, | |
2059 | sizeof(to->remote_ipv6)); | |
2060 | to->tunnel_label = be32_to_cpu(info->key.label); | |
2061 | } else { | |
2062 | to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src); | |
2063 | } | |
2064 | ||
2065 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) | |
2066 | memcpy(to_orig, to, size); | |
2067 | ||
2068 | return 0; | |
2069 | err_clear: | |
2070 | memset(to_orig, 0, size); | |
2071 | return err; | |
2072 | } | |
2073 | ||
2074 | static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = { | |
2075 | .func = bpf_skb_get_tunnel_key, | |
2076 | .gpl_only = false, | |
2077 | .ret_type = RET_INTEGER, | |
2078 | .arg1_type = ARG_PTR_TO_CTX, | |
2079 | .arg2_type = ARG_PTR_TO_RAW_STACK, | |
2080 | .arg3_type = ARG_CONST_STACK_SIZE, | |
2081 | .arg4_type = ARG_ANYTHING, | |
2082 | }; | |
2083 | ||
2084 | static u64 bpf_skb_get_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5) | |
2085 | { | |
2086 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
2087 | u8 *to = (u8 *) (long) r2; | |
2088 | const struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
2089 | int err; | |
2090 | ||
2091 | if (unlikely(!info || | |
2092 | !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) { | |
2093 | err = -ENOENT; | |
2094 | goto err_clear; | |
2095 | } | |
2096 | if (unlikely(size < info->options_len)) { | |
2097 | err = -ENOMEM; | |
2098 | goto err_clear; | |
2099 | } | |
2100 | ||
2101 | ip_tunnel_info_opts_get(to, info); | |
2102 | if (size > info->options_len) | |
2103 | memset(to + info->options_len, 0, size - info->options_len); | |
2104 | ||
2105 | return info->options_len; | |
2106 | err_clear: | |
2107 | memset(to, 0, size); | |
2108 | return err; | |
2109 | } | |
2110 | ||
2111 | static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = { | |
2112 | .func = bpf_skb_get_tunnel_opt, | |
2113 | .gpl_only = false, | |
2114 | .ret_type = RET_INTEGER, | |
2115 | .arg1_type = ARG_PTR_TO_CTX, | |
2116 | .arg2_type = ARG_PTR_TO_RAW_STACK, | |
2117 | .arg3_type = ARG_CONST_STACK_SIZE, | |
2118 | }; | |
2119 | ||
2120 | static struct metadata_dst __percpu *md_dst; | |
2121 | ||
2122 | static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5) | |
2123 | { | |
2124 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
2125 | struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2; | |
2126 | struct metadata_dst *md = this_cpu_ptr(md_dst); | |
2127 | u8 compat[sizeof(struct bpf_tunnel_key)]; | |
2128 | struct ip_tunnel_info *info; | |
2129 | ||
2130 | if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX | | |
2131 | BPF_F_DONT_FRAGMENT))) | |
2132 | return -EINVAL; | |
2133 | if (unlikely(size != sizeof(struct bpf_tunnel_key))) { | |
2134 | switch (size) { | |
2135 | case offsetof(struct bpf_tunnel_key, tunnel_label): | |
2136 | case offsetof(struct bpf_tunnel_key, tunnel_ext): | |
2137 | case offsetof(struct bpf_tunnel_key, remote_ipv6[1]): | |
2138 | /* Fixup deprecated structure layouts here, so we have | |
2139 | * a common path later on. | |
2140 | */ | |
2141 | memcpy(compat, from, size); | |
2142 | memset(compat + size, 0, sizeof(compat) - size); | |
2143 | from = (struct bpf_tunnel_key *)compat; | |
2144 | break; | |
2145 | default: | |
2146 | return -EINVAL; | |
2147 | } | |
2148 | } | |
2149 | if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) || | |
2150 | from->tunnel_ext)) | |
2151 | return -EINVAL; | |
2152 | ||
2153 | skb_dst_drop(skb); | |
2154 | dst_hold((struct dst_entry *) md); | |
2155 | skb_dst_set(skb, (struct dst_entry *) md); | |
2156 | ||
2157 | info = &md->u.tun_info; | |
2158 | info->mode = IP_TUNNEL_INFO_TX; | |
2159 | ||
2160 | info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE; | |
2161 | if (flags & BPF_F_DONT_FRAGMENT) | |
2162 | info->key.tun_flags |= TUNNEL_DONT_FRAGMENT; | |
2163 | ||
2164 | info->key.tun_id = cpu_to_be64(from->tunnel_id); | |
2165 | info->key.tos = from->tunnel_tos; | |
2166 | info->key.ttl = from->tunnel_ttl; | |
2167 | ||
2168 | if (flags & BPF_F_TUNINFO_IPV6) { | |
2169 | info->mode |= IP_TUNNEL_INFO_IPV6; | |
2170 | memcpy(&info->key.u.ipv6.dst, from->remote_ipv6, | |
2171 | sizeof(from->remote_ipv6)); | |
2172 | info->key.label = cpu_to_be32(from->tunnel_label) & | |
2173 | IPV6_FLOWLABEL_MASK; | |
2174 | } else { | |
2175 | info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4); | |
2176 | if (flags & BPF_F_ZERO_CSUM_TX) | |
2177 | info->key.tun_flags &= ~TUNNEL_CSUM; | |
2178 | } | |
2179 | ||
2180 | return 0; | |
2181 | } | |
2182 | ||
2183 | static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = { | |
2184 | .func = bpf_skb_set_tunnel_key, | |
2185 | .gpl_only = false, | |
2186 | .ret_type = RET_INTEGER, | |
2187 | .arg1_type = ARG_PTR_TO_CTX, | |
2188 | .arg2_type = ARG_PTR_TO_STACK, | |
2189 | .arg3_type = ARG_CONST_STACK_SIZE, | |
2190 | .arg4_type = ARG_ANYTHING, | |
2191 | }; | |
2192 | ||
2193 | static u64 bpf_skb_set_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5) | |
2194 | { | |
2195 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | |
2196 | u8 *from = (u8 *) (long) r2; | |
2197 | struct ip_tunnel_info *info = skb_tunnel_info(skb); | |
2198 | const struct metadata_dst *md = this_cpu_ptr(md_dst); | |
2199 | ||
2200 | if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1)))) | |
2201 | return -EINVAL; | |
2202 | if (unlikely(size > IP_TUNNEL_OPTS_MAX)) | |
2203 | return -ENOMEM; | |
2204 | ||
2205 | ip_tunnel_info_opts_set(info, from, size); | |
2206 | ||
2207 | return 0; | |
2208 | } | |
2209 | ||
2210 | static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = { | |
2211 | .func = bpf_skb_set_tunnel_opt, | |
2212 | .gpl_only = false, | |
2213 | .ret_type = RET_INTEGER, | |
2214 | .arg1_type = ARG_PTR_TO_CTX, | |
2215 | .arg2_type = ARG_PTR_TO_STACK, | |
2216 | .arg3_type = ARG_CONST_STACK_SIZE, | |
2217 | }; | |
2218 | ||
2219 | static const struct bpf_func_proto * | |
2220 | bpf_get_skb_set_tunnel_proto(enum bpf_func_id which) | |
2221 | { | |
2222 | if (!md_dst) { | |
2223 | /* Race is not possible, since it's called from verifier | |
2224 | * that is holding verifier mutex. | |
2225 | */ | |
2226 | md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX, | |
2227 | GFP_KERNEL); | |
2228 | if (!md_dst) | |
2229 | return NULL; | |
2230 | } | |
2231 | ||
2232 | switch (which) { | |
2233 | case BPF_FUNC_skb_set_tunnel_key: | |
2234 | return &bpf_skb_set_tunnel_key_proto; | |
2235 | case BPF_FUNC_skb_set_tunnel_opt: | |
2236 | return &bpf_skb_set_tunnel_opt_proto; | |
2237 | default: | |
2238 | return NULL; | |
2239 | } | |
2240 | } | |
2241 | ||
2242 | #ifdef CONFIG_SOCK_CGROUP_DATA | |
2243 | static u64 bpf_skb_in_cgroup(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | |
2244 | { | |
2245 | struct sk_buff *skb = (struct sk_buff *)(long)r1; | |
2246 | struct bpf_map *map = (struct bpf_map *)(long)r2; | |
2247 | struct bpf_array *array = container_of(map, struct bpf_array, map); | |
2248 | struct cgroup *cgrp; | |
2249 | struct sock *sk; | |
2250 | u32 i = (u32)r3; | |
2251 | ||
2252 | sk = skb->sk; | |
2253 | if (!sk || !sk_fullsock(sk)) | |
2254 | return -ENOENT; | |
2255 | ||
2256 | if (unlikely(i >= array->map.max_entries)) | |
2257 | return -E2BIG; | |
2258 | ||
2259 | cgrp = READ_ONCE(array->ptrs[i]); | |
2260 | if (unlikely(!cgrp)) | |
2261 | return -EAGAIN; | |
2262 | ||
2263 | return cgroup_is_descendant(sock_cgroup_ptr(&sk->sk_cgrp_data), cgrp); | |
2264 | } | |
2265 | ||
2266 | static const struct bpf_func_proto bpf_skb_in_cgroup_proto = { | |
2267 | .func = bpf_skb_in_cgroup, | |
2268 | .gpl_only = false, | |
2269 | .ret_type = RET_INTEGER, | |
2270 | .arg1_type = ARG_PTR_TO_CTX, | |
2271 | .arg2_type = ARG_CONST_MAP_PTR, | |
2272 | .arg3_type = ARG_ANYTHING, | |
2273 | }; | |
2274 | #endif | |
2275 | ||
2276 | static const struct bpf_func_proto * | |
2277 | sk_filter_func_proto(enum bpf_func_id func_id) | |
2278 | { | |
2279 | switch (func_id) { | |
2280 | case BPF_FUNC_map_lookup_elem: | |
2281 | return &bpf_map_lookup_elem_proto; | |
2282 | case BPF_FUNC_map_update_elem: | |
2283 | return &bpf_map_update_elem_proto; | |
2284 | case BPF_FUNC_map_delete_elem: | |
2285 | return &bpf_map_delete_elem_proto; | |
2286 | case BPF_FUNC_get_prandom_u32: | |
2287 | return &bpf_get_prandom_u32_proto; | |
2288 | case BPF_FUNC_get_smp_processor_id: | |
2289 | return &bpf_get_raw_smp_processor_id_proto; | |
2290 | case BPF_FUNC_tail_call: | |
2291 | return &bpf_tail_call_proto; | |
2292 | case BPF_FUNC_ktime_get_ns: | |
2293 | return &bpf_ktime_get_ns_proto; | |
2294 | case BPF_FUNC_trace_printk: | |
2295 | if (capable(CAP_SYS_ADMIN)) | |
2296 | return bpf_get_trace_printk_proto(); | |
2297 | default: | |
2298 | return NULL; | |
2299 | } | |
2300 | } | |
2301 | ||
2302 | static const struct bpf_func_proto * | |
2303 | tc_cls_act_func_proto(enum bpf_func_id func_id) | |
2304 | { | |
2305 | switch (func_id) { | |
2306 | case BPF_FUNC_skb_store_bytes: | |
2307 | return &bpf_skb_store_bytes_proto; | |
2308 | case BPF_FUNC_skb_load_bytes: | |
2309 | return &bpf_skb_load_bytes_proto; | |
2310 | case BPF_FUNC_csum_diff: | |
2311 | return &bpf_csum_diff_proto; | |
2312 | case BPF_FUNC_l3_csum_replace: | |
2313 | return &bpf_l3_csum_replace_proto; | |
2314 | case BPF_FUNC_l4_csum_replace: | |
2315 | return &bpf_l4_csum_replace_proto; | |
2316 | case BPF_FUNC_clone_redirect: | |
2317 | return &bpf_clone_redirect_proto; | |
2318 | case BPF_FUNC_get_cgroup_classid: | |
2319 | return &bpf_get_cgroup_classid_proto; | |
2320 | case BPF_FUNC_skb_vlan_push: | |
2321 | return &bpf_skb_vlan_push_proto; | |
2322 | case BPF_FUNC_skb_vlan_pop: | |
2323 | return &bpf_skb_vlan_pop_proto; | |
2324 | case BPF_FUNC_skb_change_proto: | |
2325 | return &bpf_skb_change_proto_proto; | |
2326 | case BPF_FUNC_skb_change_type: | |
2327 | return &bpf_skb_change_type_proto; | |
2328 | case BPF_FUNC_skb_get_tunnel_key: | |
2329 | return &bpf_skb_get_tunnel_key_proto; | |
2330 | case BPF_FUNC_skb_set_tunnel_key: | |
2331 | return bpf_get_skb_set_tunnel_proto(func_id); | |
2332 | case BPF_FUNC_skb_get_tunnel_opt: | |
2333 | return &bpf_skb_get_tunnel_opt_proto; | |
2334 | case BPF_FUNC_skb_set_tunnel_opt: | |
2335 | return bpf_get_skb_set_tunnel_proto(func_id); | |
2336 | case BPF_FUNC_redirect: | |
2337 | return &bpf_redirect_proto; | |
2338 | case BPF_FUNC_get_route_realm: | |
2339 | return &bpf_get_route_realm_proto; | |
2340 | case BPF_FUNC_perf_event_output: | |
2341 | return bpf_get_event_output_proto(); | |
2342 | case BPF_FUNC_get_smp_processor_id: | |
2343 | return &bpf_get_smp_processor_id_proto; | |
2344 | #ifdef CONFIG_SOCK_CGROUP_DATA | |
2345 | case BPF_FUNC_skb_in_cgroup: | |
2346 | return &bpf_skb_in_cgroup_proto; | |
2347 | #endif | |
2348 | default: | |
2349 | return sk_filter_func_proto(func_id); | |
2350 | } | |
2351 | } | |
2352 | ||
2353 | static bool __is_valid_access(int off, int size, enum bpf_access_type type) | |
2354 | { | |
2355 | if (off < 0 || off >= sizeof(struct __sk_buff)) | |
2356 | return false; | |
2357 | /* The verifier guarantees that size > 0. */ | |
2358 | if (off % size != 0) | |
2359 | return false; | |
2360 | if (size != sizeof(__u32)) | |
2361 | return false; | |
2362 | ||
2363 | return true; | |
2364 | } | |
2365 | ||
2366 | static bool sk_filter_is_valid_access(int off, int size, | |
2367 | enum bpf_access_type type, | |
2368 | enum bpf_reg_type *reg_type) | |
2369 | { | |
2370 | switch (off) { | |
2371 | case offsetof(struct __sk_buff, tc_classid): | |
2372 | case offsetof(struct __sk_buff, data): | |
2373 | case offsetof(struct __sk_buff, data_end): | |
2374 | return false; | |
2375 | } | |
2376 | ||
2377 | if (type == BPF_WRITE) { | |
2378 | switch (off) { | |
2379 | case offsetof(struct __sk_buff, cb[0]) ... | |
2380 | offsetof(struct __sk_buff, cb[4]): | |
2381 | break; | |
2382 | default: | |
2383 | return false; | |
2384 | } | |
2385 | } | |
2386 | ||
2387 | return __is_valid_access(off, size, type); | |
2388 | } | |
2389 | ||
2390 | static bool tc_cls_act_is_valid_access(int off, int size, | |
2391 | enum bpf_access_type type, | |
2392 | enum bpf_reg_type *reg_type) | |
2393 | { | |
2394 | if (type == BPF_WRITE) { | |
2395 | switch (off) { | |
2396 | case offsetof(struct __sk_buff, mark): | |
2397 | case offsetof(struct __sk_buff, tc_index): | |
2398 | case offsetof(struct __sk_buff, priority): | |
2399 | case offsetof(struct __sk_buff, cb[0]) ... | |
2400 | offsetof(struct __sk_buff, cb[4]): | |
2401 | case offsetof(struct __sk_buff, tc_classid): | |
2402 | break; | |
2403 | default: | |
2404 | return false; | |
2405 | } | |
2406 | } | |
2407 | ||
2408 | switch (off) { | |
2409 | case offsetof(struct __sk_buff, data): | |
2410 | *reg_type = PTR_TO_PACKET; | |
2411 | break; | |
2412 | case offsetof(struct __sk_buff, data_end): | |
2413 | *reg_type = PTR_TO_PACKET_END; | |
2414 | break; | |
2415 | } | |
2416 | ||
2417 | return __is_valid_access(off, size, type); | |
2418 | } | |
2419 | ||
2420 | static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg, | |
2421 | int src_reg, int ctx_off, | |
2422 | struct bpf_insn *insn_buf, | |
2423 | struct bpf_prog *prog) | |
2424 | { | |
2425 | struct bpf_insn *insn = insn_buf; | |
2426 | ||
2427 | switch (ctx_off) { | |
2428 | case offsetof(struct __sk_buff, len): | |
2429 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4); | |
2430 | ||
2431 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
2432 | offsetof(struct sk_buff, len)); | |
2433 | break; | |
2434 | ||
2435 | case offsetof(struct __sk_buff, protocol): | |
2436 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2); | |
2437 | ||
2438 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
2439 | offsetof(struct sk_buff, protocol)); | |
2440 | break; | |
2441 | ||
2442 | case offsetof(struct __sk_buff, vlan_proto): | |
2443 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2); | |
2444 | ||
2445 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
2446 | offsetof(struct sk_buff, vlan_proto)); | |
2447 | break; | |
2448 | ||
2449 | case offsetof(struct __sk_buff, priority): | |
2450 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4); | |
2451 | ||
2452 | if (type == BPF_WRITE) | |
2453 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, | |
2454 | offsetof(struct sk_buff, priority)); | |
2455 | else | |
2456 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
2457 | offsetof(struct sk_buff, priority)); | |
2458 | break; | |
2459 | ||
2460 | case offsetof(struct __sk_buff, ingress_ifindex): | |
2461 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4); | |
2462 | ||
2463 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
2464 | offsetof(struct sk_buff, skb_iif)); | |
2465 | break; | |
2466 | ||
2467 | case offsetof(struct __sk_buff, ifindex): | |
2468 | BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4); | |
2469 | ||
2470 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)), | |
2471 | dst_reg, src_reg, | |
2472 | offsetof(struct sk_buff, dev)); | |
2473 | *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1); | |
2474 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg, | |
2475 | offsetof(struct net_device, ifindex)); | |
2476 | break; | |
2477 | ||
2478 | case offsetof(struct __sk_buff, hash): | |
2479 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4); | |
2480 | ||
2481 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
2482 | offsetof(struct sk_buff, hash)); | |
2483 | break; | |
2484 | ||
2485 | case offsetof(struct __sk_buff, mark): | |
2486 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); | |
2487 | ||
2488 | if (type == BPF_WRITE) | |
2489 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, | |
2490 | offsetof(struct sk_buff, mark)); | |
2491 | else | |
2492 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, | |
2493 | offsetof(struct sk_buff, mark)); | |
2494 | break; | |
2495 | ||
2496 | case offsetof(struct __sk_buff, pkt_type): | |
2497 | return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn); | |
2498 | ||
2499 | case offsetof(struct __sk_buff, queue_mapping): | |
2500 | return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn); | |
2501 | ||
2502 | case offsetof(struct __sk_buff, vlan_present): | |
2503 | return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, | |
2504 | dst_reg, src_reg, insn); | |
2505 | ||
2506 | case offsetof(struct __sk_buff, vlan_tci): | |
2507 | return convert_skb_access(SKF_AD_VLAN_TAG, | |
2508 | dst_reg, src_reg, insn); | |
2509 | ||
2510 | case offsetof(struct __sk_buff, cb[0]) ... | |
2511 | offsetof(struct __sk_buff, cb[4]): | |
2512 | BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20); | |
2513 | ||
2514 | prog->cb_access = 1; | |
2515 | ctx_off -= offsetof(struct __sk_buff, cb[0]); | |
2516 | ctx_off += offsetof(struct sk_buff, cb); | |
2517 | ctx_off += offsetof(struct qdisc_skb_cb, data); | |
2518 | if (type == BPF_WRITE) | |
2519 | *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off); | |
2520 | else | |
2521 | *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off); | |
2522 | break; | |
2523 | ||
2524 | case offsetof(struct __sk_buff, tc_classid): | |
2525 | ctx_off -= offsetof(struct __sk_buff, tc_classid); | |
2526 | ctx_off += offsetof(struct sk_buff, cb); | |
2527 | ctx_off += offsetof(struct qdisc_skb_cb, tc_classid); | |
2528 | if (type == BPF_WRITE) | |
2529 | *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off); | |
2530 | else | |
2531 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off); | |
2532 | break; | |
2533 | ||
2534 | case offsetof(struct __sk_buff, data): | |
2535 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, data)), | |
2536 | dst_reg, src_reg, | |
2537 | offsetof(struct sk_buff, data)); | |
2538 | break; | |
2539 | ||
2540 | case offsetof(struct __sk_buff, data_end): | |
2541 | ctx_off -= offsetof(struct __sk_buff, data_end); | |
2542 | ctx_off += offsetof(struct sk_buff, cb); | |
2543 | ctx_off += offsetof(struct bpf_skb_data_end, data_end); | |
2544 | *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(sizeof(void *)), | |
2545 | dst_reg, src_reg, ctx_off); | |
2546 | break; | |
2547 | ||
2548 | case offsetof(struct __sk_buff, tc_index): | |
2549 | #ifdef CONFIG_NET_SCHED | |
2550 | BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2); | |
2551 | ||
2552 | if (type == BPF_WRITE) | |
2553 | *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, | |
2554 | offsetof(struct sk_buff, tc_index)); | |
2555 | else | |
2556 | *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, | |
2557 | offsetof(struct sk_buff, tc_index)); | |
2558 | break; | |
2559 | #else | |
2560 | if (type == BPF_WRITE) | |
2561 | *insn++ = BPF_MOV64_REG(dst_reg, dst_reg); | |
2562 | else | |
2563 | *insn++ = BPF_MOV64_IMM(dst_reg, 0); | |
2564 | break; | |
2565 | #endif | |
2566 | } | |
2567 | ||
2568 | return insn - insn_buf; | |
2569 | } | |
2570 | ||
2571 | static const struct bpf_verifier_ops sk_filter_ops = { | |
2572 | .get_func_proto = sk_filter_func_proto, | |
2573 | .is_valid_access = sk_filter_is_valid_access, | |
2574 | .convert_ctx_access = bpf_net_convert_ctx_access, | |
2575 | }; | |
2576 | ||
2577 | static const struct bpf_verifier_ops tc_cls_act_ops = { | |
2578 | .get_func_proto = tc_cls_act_func_proto, | |
2579 | .is_valid_access = tc_cls_act_is_valid_access, | |
2580 | .convert_ctx_access = bpf_net_convert_ctx_access, | |
2581 | }; | |
2582 | ||
2583 | static struct bpf_prog_type_list sk_filter_type __read_mostly = { | |
2584 | .ops = &sk_filter_ops, | |
2585 | .type = BPF_PROG_TYPE_SOCKET_FILTER, | |
2586 | }; | |
2587 | ||
2588 | static struct bpf_prog_type_list sched_cls_type __read_mostly = { | |
2589 | .ops = &tc_cls_act_ops, | |
2590 | .type = BPF_PROG_TYPE_SCHED_CLS, | |
2591 | }; | |
2592 | ||
2593 | static struct bpf_prog_type_list sched_act_type __read_mostly = { | |
2594 | .ops = &tc_cls_act_ops, | |
2595 | .type = BPF_PROG_TYPE_SCHED_ACT, | |
2596 | }; | |
2597 | ||
2598 | static int __init register_sk_filter_ops(void) | |
2599 | { | |
2600 | bpf_register_prog_type(&sk_filter_type); | |
2601 | bpf_register_prog_type(&sched_cls_type); | |
2602 | bpf_register_prog_type(&sched_act_type); | |
2603 | ||
2604 | return 0; | |
2605 | } | |
2606 | late_initcall(register_sk_filter_ops); | |
2607 | ||
2608 | int sk_detach_filter(struct sock *sk) | |
2609 | { | |
2610 | int ret = -ENOENT; | |
2611 | struct sk_filter *filter; | |
2612 | ||
2613 | if (sock_flag(sk, SOCK_FILTER_LOCKED)) | |
2614 | return -EPERM; | |
2615 | ||
2616 | filter = rcu_dereference_protected(sk->sk_filter, | |
2617 | lockdep_sock_is_held(sk)); | |
2618 | if (filter) { | |
2619 | RCU_INIT_POINTER(sk->sk_filter, NULL); | |
2620 | sk_filter_uncharge(sk, filter); | |
2621 | ret = 0; | |
2622 | } | |
2623 | ||
2624 | return ret; | |
2625 | } | |
2626 | EXPORT_SYMBOL_GPL(sk_detach_filter); | |
2627 | ||
2628 | int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, | |
2629 | unsigned int len) | |
2630 | { | |
2631 | struct sock_fprog_kern *fprog; | |
2632 | struct sk_filter *filter; | |
2633 | int ret = 0; | |
2634 | ||
2635 | lock_sock(sk); | |
2636 | filter = rcu_dereference_protected(sk->sk_filter, | |
2637 | lockdep_sock_is_held(sk)); | |
2638 | if (!filter) | |
2639 | goto out; | |
2640 | ||
2641 | /* We're copying the filter that has been originally attached, | |
2642 | * so no conversion/decode needed anymore. eBPF programs that | |
2643 | * have no original program cannot be dumped through this. | |
2644 | */ | |
2645 | ret = -EACCES; | |
2646 | fprog = filter->prog->orig_prog; | |
2647 | if (!fprog) | |
2648 | goto out; | |
2649 | ||
2650 | ret = fprog->len; | |
2651 | if (!len) | |
2652 | /* User space only enquires number of filter blocks. */ | |
2653 | goto out; | |
2654 | ||
2655 | ret = -EINVAL; | |
2656 | if (len < fprog->len) | |
2657 | goto out; | |
2658 | ||
2659 | ret = -EFAULT; | |
2660 | if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog))) | |
2661 | goto out; | |
2662 | ||
2663 | /* Instead of bytes, the API requests to return the number | |
2664 | * of filter blocks. | |
2665 | */ | |
2666 | ret = fprog->len; | |
2667 | out: | |
2668 | release_sock(sk); | |
2669 | return ret; | |
2670 | } |