blob: 6bd2e350e751c86a459cd2f0bbe76b503b692294 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux Socket Filter - Kernel level socket filtering
3 *
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01004 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01007 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
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.
Kris Katterjohn93699862006-01-04 13:58:36 -080021 * Kris Katterjohn - Added many additional checks in sk_chk_filter()
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
24#include <linux/module.h>
25#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/ip.h>
35#include <net/protocol.h>
Patrick McHardy4738c1d2008-04-10 02:02:28 -070036#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/skbuff.h>
38#include <net/sock.h>
39#include <linux/errno.h>
40#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
Dmitry Mishin40daafc2006-04-18 14:50:10 -070042#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/filter.h>
David S. Miller86e4ca62011-05-26 15:00:31 -040044#include <linux/ratelimit.h>
Will Drewry46b325c2012-04-12 16:47:52 -050045#include <linux/seccomp.h>
Eric Dumazetf3335032012-10-27 02:26:17 +000046#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Daniel Borkmann30743832014-05-01 18:34:19 +020048/* Registers */
David S. Miller1268e252014-05-13 13:13:33 -040049#define BPF_R0 regs[BPF_REG_0]
50#define BPF_R1 regs[BPF_REG_1]
51#define BPF_R2 regs[BPF_REG_2]
52#define BPF_R3 regs[BPF_REG_3]
53#define BPF_R4 regs[BPF_REG_4]
54#define BPF_R5 regs[BPF_REG_5]
55#define BPF_R6 regs[BPF_REG_6]
56#define BPF_R7 regs[BPF_REG_7]
57#define BPF_R8 regs[BPF_REG_8]
58#define BPF_R9 regs[BPF_REG_9]
59#define BPF_R10 regs[BPF_REG_10]
Daniel Borkmann30743832014-05-01 18:34:19 +020060
61/* Named registers */
62#define A regs[insn->a_reg]
63#define X regs[insn->x_reg]
64#define FP regs[BPF_REG_FP]
65#define ARG1 regs[BPF_REG_ARG1]
66#define CTX regs[BPF_REG_CTX]
67#define K insn->imm
68
Jan Seiffertf03fb3f2012-03-30 05:08:19 +000069/* No hurry in this branch
70 *
71 * Exported for the bpf jit load helper.
72 */
73void *bpf_internal_load_pointer_neg_helper(const struct sk_buff *skb, int k, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 u8 *ptr = NULL;
76
77 if (k >= SKF_NET_OFF)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070078 ptr = skb_network_header(skb) + k - SKF_NET_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 else if (k >= SKF_LL_OFF)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -070080 ptr = skb_mac_header(skb) + k - SKF_LL_OFF;
Eric Dumazet4bc65dd2010-12-07 22:26:15 +000081 if (ptr >= skb->head && ptr + size <= skb_tail_pointer(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return ptr;
Daniel Borkmanneb9672f2014-05-01 18:34:20 +020083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return NULL;
85}
86
Eric Dumazet62ab0812010-12-06 20:50:09 +000087static inline void *load_pointer(const struct sk_buff *skb, int k,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090088 unsigned int size, void *buffer)
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070089{
90 if (k >= 0)
91 return skb_header_pointer(skb, k, size, buffer);
Daniel Borkmanneb9672f2014-05-01 18:34:20 +020092
Jan Seiffertf03fb3f2012-03-30 05:08:19 +000093 return bpf_internal_load_pointer_neg_helper(skb, k, size);
Patrick McHardy0b05b2a2005-07-05 14:10:21 -070094}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/**
Stephen Hemminger43db6d62008-04-10 01:43:09 -070097 * sk_filter - run a packet through a socket filter
98 * @sk: sock associated with &sk_buff
99 * @skb: buffer to filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700100 *
101 * Run the filter code and then cut skb->data to correct size returned by
102 * sk_run_filter. If pkt_len is 0 we toss packet. If skb->len is smaller
103 * than pkt_len we keep whole skb->data. This is the socket level
104 * wrapper to sk_run_filter. It returns 0 if the packet should
105 * be accepted or -EPERM if the packet should be tossed.
106 *
107 */
108int sk_filter(struct sock *sk, struct sk_buff *skb)
109{
110 int err;
111 struct sk_filter *filter;
112
Mel Gormanc93bdd02012-07-31 16:44:19 -0700113 /*
114 * If the skb was allocated from pfmemalloc reserves, only
115 * allow SOCK_MEMALLOC sockets to use it as this socket is
116 * helping free memory
117 */
118 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
119 return -ENOMEM;
120
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700121 err = security_sock_rcv_skb(sk, skb);
122 if (err)
123 return err;
124
Eric Dumazet80f8f102011-01-18 07:46:52 +0000125 rcu_read_lock();
126 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700127 if (filter) {
Eric Dumazet0a148422011-04-20 09:27:32 +0000128 unsigned int pkt_len = SK_RUN_FILTER(filter, skb);
Eric Dumazet0d7da9d2010-10-25 03:47:05 +0000129
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700130 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
131 }
Eric Dumazet80f8f102011-01-18 07:46:52 +0000132 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700133
134 return err;
135}
136EXPORT_SYMBOL(sk_filter);
137
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100138/* Base function for offset calculation. Needs to go into .text section,
139 * therefore keeping it non-static as well; will also be used by JITs
140 * anyway later on, so do not let the compiler omit it.
141 */
142noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
143{
144 return 0;
145}
146
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700147/**
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100148 * __sk_run_filter - run a filter on a given context
149 * @ctx: buffer to run the filter on
Daniel Borkmann01d32f62014-04-01 19:38:01 +0200150 * @insn: filter to apply
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100152 * Decode and apply filter instructions to the skb->data. Return length to
Daniel Borkmann01d32f62014-04-01 19:38:01 +0200153 * keep, 0 for none. @ctx is the data we are operating on, @insn is the
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100154 * array of filter instructions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -0700156static unsigned int __sk_run_filter(void *ctx, const struct sock_filter_int *insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100158 u64 stack[MAX_BPF_STACK / sizeof(u64)];
159 u64 regs[MAX_BPF_REG], tmp;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100160 static const void *jumptable[256] = {
161 [0 ... 255] = &&default_label,
162 /* Now overwrite non-defaults ... */
Daniel Borkmann8556ce72014-05-23 18:43:57 +0200163 /* 32 bit ALU operations */
164 [BPF_ALU | BPF_ADD | BPF_X] = &&ALU_ADD_X,
165 [BPF_ALU | BPF_ADD | BPF_K] = &&ALU_ADD_K,
166 [BPF_ALU | BPF_SUB | BPF_X] = &&ALU_SUB_X,
167 [BPF_ALU | BPF_SUB | BPF_K] = &&ALU_SUB_K,
168 [BPF_ALU | BPF_AND | BPF_X] = &&ALU_AND_X,
169 [BPF_ALU | BPF_AND | BPF_K] = &&ALU_AND_K,
170 [BPF_ALU | BPF_OR | BPF_X] = &&ALU_OR_X,
171 [BPF_ALU | BPF_OR | BPF_K] = &&ALU_OR_K,
172 [BPF_ALU | BPF_LSH | BPF_X] = &&ALU_LSH_X,
173 [BPF_ALU | BPF_LSH | BPF_K] = &&ALU_LSH_K,
174 [BPF_ALU | BPF_RSH | BPF_X] = &&ALU_RSH_X,
175 [BPF_ALU | BPF_RSH | BPF_K] = &&ALU_RSH_K,
176 [BPF_ALU | BPF_XOR | BPF_X] = &&ALU_XOR_X,
177 [BPF_ALU | BPF_XOR | BPF_K] = &&ALU_XOR_K,
178 [BPF_ALU | BPF_MUL | BPF_X] = &&ALU_MUL_X,
179 [BPF_ALU | BPF_MUL | BPF_K] = &&ALU_MUL_K,
180 [BPF_ALU | BPF_MOV | BPF_X] = &&ALU_MOV_X,
181 [BPF_ALU | BPF_MOV | BPF_K] = &&ALU_MOV_K,
182 [BPF_ALU | BPF_DIV | BPF_X] = &&ALU_DIV_X,
183 [BPF_ALU | BPF_DIV | BPF_K] = &&ALU_DIV_K,
184 [BPF_ALU | BPF_MOD | BPF_X] = &&ALU_MOD_X,
185 [BPF_ALU | BPF_MOD | BPF_K] = &&ALU_MOD_K,
186 [BPF_ALU | BPF_NEG] = &&ALU_NEG,
187 [BPF_ALU | BPF_END | BPF_TO_BE] = &&ALU_END_TO_BE,
188 [BPF_ALU | BPF_END | BPF_TO_LE] = &&ALU_END_TO_LE,
189 /* 64 bit ALU operations */
190 [BPF_ALU64 | BPF_ADD | BPF_X] = &&ALU64_ADD_X,
191 [BPF_ALU64 | BPF_ADD | BPF_K] = &&ALU64_ADD_K,
192 [BPF_ALU64 | BPF_SUB | BPF_X] = &&ALU64_SUB_X,
193 [BPF_ALU64 | BPF_SUB | BPF_K] = &&ALU64_SUB_K,
194 [BPF_ALU64 | BPF_AND | BPF_X] = &&ALU64_AND_X,
195 [BPF_ALU64 | BPF_AND | BPF_K] = &&ALU64_AND_K,
196 [BPF_ALU64 | BPF_OR | BPF_X] = &&ALU64_OR_X,
197 [BPF_ALU64 | BPF_OR | BPF_K] = &&ALU64_OR_K,
198 [BPF_ALU64 | BPF_LSH | BPF_X] = &&ALU64_LSH_X,
199 [BPF_ALU64 | BPF_LSH | BPF_K] = &&ALU64_LSH_K,
200 [BPF_ALU64 | BPF_RSH | BPF_X] = &&ALU64_RSH_X,
201 [BPF_ALU64 | BPF_RSH | BPF_K] = &&ALU64_RSH_K,
202 [BPF_ALU64 | BPF_XOR | BPF_X] = &&ALU64_XOR_X,
203 [BPF_ALU64 | BPF_XOR | BPF_K] = &&ALU64_XOR_K,
204 [BPF_ALU64 | BPF_MUL | BPF_X] = &&ALU64_MUL_X,
205 [BPF_ALU64 | BPF_MUL | BPF_K] = &&ALU64_MUL_K,
206 [BPF_ALU64 | BPF_MOV | BPF_X] = &&ALU64_MOV_X,
207 [BPF_ALU64 | BPF_MOV | BPF_K] = &&ALU64_MOV_K,
208 [BPF_ALU64 | BPF_ARSH | BPF_X] = &&ALU64_ARSH_X,
209 [BPF_ALU64 | BPF_ARSH | BPF_K] = &&ALU64_ARSH_K,
210 [BPF_ALU64 | BPF_DIV | BPF_X] = &&ALU64_DIV_X,
211 [BPF_ALU64 | BPF_DIV | BPF_K] = &&ALU64_DIV_K,
212 [BPF_ALU64 | BPF_MOD | BPF_X] = &&ALU64_MOD_X,
213 [BPF_ALU64 | BPF_MOD | BPF_K] = &&ALU64_MOD_K,
214 [BPF_ALU64 | BPF_NEG] = &&ALU64_NEG,
215 /* Call instruction */
216 [BPF_JMP | BPF_CALL] = &&JMP_CALL,
217 /* Jumps */
218 [BPF_JMP | BPF_JA] = &&JMP_JA,
219 [BPF_JMP | BPF_JEQ | BPF_X] = &&JMP_JEQ_X,
220 [BPF_JMP | BPF_JEQ | BPF_K] = &&JMP_JEQ_K,
221 [BPF_JMP | BPF_JNE | BPF_X] = &&JMP_JNE_X,
222 [BPF_JMP | BPF_JNE | BPF_K] = &&JMP_JNE_K,
223 [BPF_JMP | BPF_JGT | BPF_X] = &&JMP_JGT_X,
224 [BPF_JMP | BPF_JGT | BPF_K] = &&JMP_JGT_K,
225 [BPF_JMP | BPF_JGE | BPF_X] = &&JMP_JGE_X,
226 [BPF_JMP | BPF_JGE | BPF_K] = &&JMP_JGE_K,
227 [BPF_JMP | BPF_JSGT | BPF_X] = &&JMP_JSGT_X,
228 [BPF_JMP | BPF_JSGT | BPF_K] = &&JMP_JSGT_K,
229 [BPF_JMP | BPF_JSGE | BPF_X] = &&JMP_JSGE_X,
230 [BPF_JMP | BPF_JSGE | BPF_K] = &&JMP_JSGE_K,
231 [BPF_JMP | BPF_JSET | BPF_X] = &&JMP_JSET_X,
232 [BPF_JMP | BPF_JSET | BPF_K] = &&JMP_JSET_K,
233 /* Program return */
234 [BPF_JMP | BPF_EXIT] = &&JMP_EXIT,
235 /* Store instructions */
236 [BPF_STX | BPF_MEM | BPF_B] = &&STX_MEM_B,
237 [BPF_STX | BPF_MEM | BPF_H] = &&STX_MEM_H,
238 [BPF_STX | BPF_MEM | BPF_W] = &&STX_MEM_W,
239 [BPF_STX | BPF_MEM | BPF_DW] = &&STX_MEM_DW,
240 [BPF_STX | BPF_XADD | BPF_W] = &&STX_XADD_W,
241 [BPF_STX | BPF_XADD | BPF_DW] = &&STX_XADD_DW,
242 [BPF_ST | BPF_MEM | BPF_B] = &&ST_MEM_B,
243 [BPF_ST | BPF_MEM | BPF_H] = &&ST_MEM_H,
244 [BPF_ST | BPF_MEM | BPF_W] = &&ST_MEM_W,
245 [BPF_ST | BPF_MEM | BPF_DW] = &&ST_MEM_DW,
246 /* Load instructions */
247 [BPF_LDX | BPF_MEM | BPF_B] = &&LDX_MEM_B,
248 [BPF_LDX | BPF_MEM | BPF_H] = &&LDX_MEM_H,
249 [BPF_LDX | BPF_MEM | BPF_W] = &&LDX_MEM_W,
250 [BPF_LDX | BPF_MEM | BPF_DW] = &&LDX_MEM_DW,
251 [BPF_LD | BPF_ABS | BPF_W] = &&LD_ABS_W,
252 [BPF_LD | BPF_ABS | BPF_H] = &&LD_ABS_H,
253 [BPF_LD | BPF_ABS | BPF_B] = &&LD_ABS_B,
254 [BPF_LD | BPF_IND | BPF_W] = &&LD_IND_W,
255 [BPF_LD | BPF_IND | BPF_H] = &&LD_IND_H,
256 [BPF_LD | BPF_IND | BPF_B] = &&LD_IND_B,
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100257 };
Daniel Borkmann30743832014-05-01 18:34:19 +0200258 void *ptr;
259 int off;
Patrick McHardy4738c1d2008-04-10 02:02:28 -0700260
Daniel Borkmann30743832014-05-01 18:34:19 +0200261#define CONT ({ insn++; goto select_insn; })
262#define CONT_JMP ({ insn++; goto select_insn; })
263
264 FP = (u64) (unsigned long) &stack[ARRAY_SIZE(stack)];
265 ARG1 = (u64) (unsigned long) ctx;
266
267 /* Register for user BPF programs need to be reset first. */
268 regs[BPF_REG_A] = 0;
269 regs[BPF_REG_X] = 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100270
271select_insn:
272 goto *jumptable[insn->code];
273
274 /* ALU */
275#define ALU(OPCODE, OP) \
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200276 ALU64_##OPCODE##_X: \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100277 A = A OP X; \
278 CONT; \
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200279 ALU_##OPCODE##_X: \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100280 A = (u32) A OP (u32) X; \
281 CONT; \
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200282 ALU64_##OPCODE##_K: \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100283 A = A OP K; \
284 CONT; \
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200285 ALU_##OPCODE##_K: \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100286 A = (u32) A OP (u32) K; \
287 CONT;
288
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200289 ALU(ADD, +)
290 ALU(SUB, -)
291 ALU(AND, &)
292 ALU(OR, |)
293 ALU(LSH, <<)
294 ALU(RSH, >>)
295 ALU(XOR, ^)
296 ALU(MUL, *)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100297#undef ALU
Daniel Borkmann8556ce72014-05-23 18:43:57 +0200298 ALU_NEG:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100299 A = (u32) -A;
300 CONT;
Daniel Borkmann8556ce72014-05-23 18:43:57 +0200301 ALU64_NEG:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100302 A = -A;
303 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200304 ALU_MOV_X:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100305 A = (u32) X;
306 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200307 ALU_MOV_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100308 A = (u32) K;
309 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200310 ALU64_MOV_X:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100311 A = X;
312 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200313 ALU64_MOV_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100314 A = K;
315 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200316 ALU64_ARSH_X:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100317 (*(s64 *) &A) >>= X;
318 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200319 ALU64_ARSH_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100320 (*(s64 *) &A) >>= K;
321 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200322 ALU64_MOD_X:
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200323 if (unlikely(X == 0))
324 return 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100325 tmp = A;
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200326 A = do_div(tmp, X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100327 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200328 ALU_MOD_X:
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200329 if (unlikely(X == 0))
330 return 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100331 tmp = (u32) A;
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200332 A = do_div(tmp, (u32) X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100333 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200334 ALU64_MOD_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100335 tmp = A;
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200336 A = do_div(tmp, K);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100337 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200338 ALU_MOD_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100339 tmp = (u32) A;
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200340 A = do_div(tmp, (u32) K);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100341 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200342 ALU64_DIV_X:
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200343 if (unlikely(X == 0))
344 return 0;
345 do_div(A, X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100346 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200347 ALU_DIV_X:
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200348 if (unlikely(X == 0))
349 return 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100350 tmp = (u32) A;
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200351 do_div(tmp, (u32) X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100352 A = (u32) tmp;
353 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200354 ALU64_DIV_K:
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200355 do_div(A, K);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100356 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200357 ALU_DIV_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100358 tmp = (u32) A;
Daniel Borkmann5f9fde52014-04-05 01:04:03 +0200359 do_div(tmp, (u32) K);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100360 A = (u32) tmp;
361 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200362 ALU_END_TO_BE:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100363 switch (K) {
364 case 16:
365 A = (__force u16) cpu_to_be16(A);
366 break;
367 case 32:
368 A = (__force u32) cpu_to_be32(A);
369 break;
370 case 64:
371 A = (__force u64) cpu_to_be64(A);
372 break;
Patrick McHardy4738c1d2008-04-10 02:02:28 -0700373 }
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100374 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200375 ALU_END_TO_LE:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100376 switch (K) {
377 case 16:
378 A = (__force u16) cpu_to_le16(A);
379 break;
380 case 32:
381 A = (__force u32) cpu_to_le32(A);
382 break;
383 case 64:
384 A = (__force u64) cpu_to_le64(A);
385 break;
Pablo Neira Ayusod214c752008-11-20 00:49:27 -0800386 }
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100387 CONT;
388
389 /* CALL */
Daniel Borkmann8556ce72014-05-23 18:43:57 +0200390 JMP_CALL:
David S. Miller1268e252014-05-13 13:13:33 -0400391 /* Function call scratches BPF_R1-BPF_R5 registers,
392 * preserves BPF_R6-BPF_R9, and stores return value
393 * into BPF_R0.
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100394 */
David S. Miller1268e252014-05-13 13:13:33 -0400395 BPF_R0 = (__bpf_call_base + insn->imm)(BPF_R1, BPF_R2, BPF_R3,
396 BPF_R4, BPF_R5);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100397 CONT;
398
399 /* JMP */
Daniel Borkmann8556ce72014-05-23 18:43:57 +0200400 JMP_JA:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100401 insn += insn->off;
402 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200403 JMP_JEQ_X:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100404 if (A == X) {
405 insn += insn->off;
406 CONT_JMP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100408 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200409 JMP_JEQ_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100410 if (A == K) {
411 insn += insn->off;
412 CONT_JMP;
413 }
414 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200415 JMP_JNE_X:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100416 if (A != X) {
417 insn += insn->off;
418 CONT_JMP;
419 }
420 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200421 JMP_JNE_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100422 if (A != K) {
423 insn += insn->off;
424 CONT_JMP;
425 }
426 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200427 JMP_JGT_X:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100428 if (A > X) {
429 insn += insn->off;
430 CONT_JMP;
431 }
432 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200433 JMP_JGT_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100434 if (A > K) {
435 insn += insn->off;
436 CONT_JMP;
437 }
438 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200439 JMP_JGE_X:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100440 if (A >= X) {
441 insn += insn->off;
442 CONT_JMP;
443 }
444 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200445 JMP_JGE_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100446 if (A >= K) {
447 insn += insn->off;
448 CONT_JMP;
449 }
450 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200451 JMP_JSGT_X:
452 if (((s64) A) > ((s64) X)) {
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100453 insn += insn->off;
454 CONT_JMP;
455 }
456 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200457 JMP_JSGT_K:
458 if (((s64) A) > ((s64) K)) {
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100459 insn += insn->off;
460 CONT_JMP;
461 }
462 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200463 JMP_JSGE_X:
464 if (((s64) A) >= ((s64) X)) {
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100465 insn += insn->off;
466 CONT_JMP;
467 }
468 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200469 JMP_JSGE_K:
470 if (((s64) A) >= ((s64) K)) {
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100471 insn += insn->off;
472 CONT_JMP;
473 }
474 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200475 JMP_JSET_X:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100476 if (A & X) {
477 insn += insn->off;
478 CONT_JMP;
479 }
480 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200481 JMP_JSET_K:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100482 if (A & K) {
483 insn += insn->off;
484 CONT_JMP;
485 }
486 CONT;
Daniel Borkmann8556ce72014-05-23 18:43:57 +0200487 JMP_EXIT:
David S. Miller1268e252014-05-13 13:13:33 -0400488 return BPF_R0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100489
490 /* STX and ST and LDX*/
491#define LDST(SIZEOP, SIZE) \
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200492 STX_MEM_##SIZEOP: \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100493 *(SIZE *)(unsigned long) (A + insn->off) = X; \
494 CONT; \
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200495 ST_MEM_##SIZEOP: \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100496 *(SIZE *)(unsigned long) (A + insn->off) = K; \
497 CONT; \
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200498 LDX_MEM_##SIZEOP: \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100499 A = *(SIZE *)(unsigned long) (X + insn->off); \
500 CONT;
501
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200502 LDST(B, u8)
503 LDST(H, u16)
504 LDST(W, u32)
505 LDST(DW, u64)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100506#undef LDST
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200507 STX_XADD_W: /* lock xadd *(u32 *)(A + insn->off) += X */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100508 atomic_add((u32) X, (atomic_t *)(unsigned long)
509 (A + insn->off));
510 CONT;
Daniel Borkmann5bcfedf2014-05-01 18:34:18 +0200511 STX_XADD_DW: /* lock xadd *(u64 *)(A + insn->off) += X */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100512 atomic64_add((u64) X, (atomic64_t *)(unsigned long)
513 (A + insn->off));
514 CONT;
David S. Miller1268e252014-05-13 13:13:33 -0400515 LD_ABS_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + K)) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100516 off = K;
517load_word:
David S. Miller1268e252014-05-13 13:13:33 -0400518 /* BPF_LD + BPD_ABS and BPF_LD + BPF_IND insns are
519 * only appearing in the programs where ctx ==
520 * skb. All programs keep 'ctx' in regs[BPF_REG_CTX]
521 * == BPF_R6, sk_convert_filter() saves it in BPF_R6,
522 * internal BPF verifier will check that BPF_R6 ==
523 * ctx.
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100524 *
David S. Miller1268e252014-05-13 13:13:33 -0400525 * BPF_ABS and BPF_IND are wrappers of function calls,
526 * so they scratch BPF_R1-BPF_R5 registers, preserve
527 * BPF_R6-BPF_R9, and store return value into BPF_R0.
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100528 *
529 * Implicit input:
530 * ctx
531 *
532 * Explicit input:
533 * X == any register
534 * K == 32-bit immediate
535 *
536 * Output:
David S. Miller1268e252014-05-13 13:13:33 -0400537 * BPF_R0 - 8/16/32-bit skb data converted to cpu endianness
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100538 */
Daniel Borkmann34805932014-05-29 10:22:50 +0200539
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100540 ptr = load_pointer((struct sk_buff *) ctx, off, 4, &tmp);
541 if (likely(ptr != NULL)) {
David S. Miller1268e252014-05-13 13:13:33 -0400542 BPF_R0 = get_unaligned_be32(ptr);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100543 CONT;
544 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200545
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100546 return 0;
David S. Miller1268e252014-05-13 13:13:33 -0400547 LD_ABS_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + K)) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100548 off = K;
549load_half:
550 ptr = load_pointer((struct sk_buff *) ctx, off, 2, &tmp);
551 if (likely(ptr != NULL)) {
David S. Miller1268e252014-05-13 13:13:33 -0400552 BPF_R0 = get_unaligned_be16(ptr);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100553 CONT;
554 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200555
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100556 return 0;
David S. Miller1268e252014-05-13 13:13:33 -0400557 LD_ABS_B: /* BPF_R0 = *(u8 *) (ctx + K) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100558 off = K;
559load_byte:
560 ptr = load_pointer((struct sk_buff *) ctx, off, 1, &tmp);
561 if (likely(ptr != NULL)) {
David S. Miller1268e252014-05-13 13:13:33 -0400562 BPF_R0 = *(u8 *)ptr;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100563 CONT;
564 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200565
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100566 return 0;
David S. Miller1268e252014-05-13 13:13:33 -0400567 LD_IND_W: /* BPF_R0 = ntohl(*(u32 *) (skb->data + X + K)) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100568 off = K + X;
569 goto load_word;
David S. Miller1268e252014-05-13 13:13:33 -0400570 LD_IND_H: /* BPF_R0 = ntohs(*(u16 *) (skb->data + X + K)) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100571 off = K + X;
572 goto load_half;
David S. Miller1268e252014-05-13 13:13:33 -0400573 LD_IND_B: /* BPF_R0 = *(u8 *) (skb->data + X + K) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100574 off = K + X;
575 goto load_byte;
576
577 default_label:
578 /* If we ever reach this, we have a bug somewhere. */
579 WARN_RATELIMIT(1, "unknown opcode %02x\n", insn->code);
580 return 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100581}
582
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100583/* Helper to find the offset of pkt_type in sk_buff structure. We want
584 * to make sure its still a 3bit field starting at a byte boundary;
585 * taken from arch/x86/net/bpf_jit_comp.c.
586 */
Alexei Starovoitov0dcceab2014-06-05 14:39:36 -0700587#ifdef __BIG_ENDIAN_BITFIELD
588#define PKT_TYPE_MAX (7 << 5)
589#else
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100590#define PKT_TYPE_MAX 7
Alexei Starovoitov0dcceab2014-06-05 14:39:36 -0700591#endif
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100592static unsigned int pkt_type_offset(void)
593{
594 struct sk_buff skb_probe = { .pkt_type = ~0, };
595 u8 *ct = (u8 *) &skb_probe;
596 unsigned int off;
597
598 for (off = 0; off < sizeof(struct sk_buff); off++) {
599 if (ct[off] == PKT_TYPE_MAX)
600 return off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100603 pr_err_once("Please fix %s, as pkt_type couldn't be found!\n", __func__);
604 return -1;
605}
606
Daniel Borkmann30743832014-05-01 18:34:19 +0200607static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100608{
Daniel Borkmanneb9672f2014-05-01 18:34:20 +0200609 return __skb_get_poff((struct sk_buff *)(unsigned long) ctx);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100610}
611
Daniel Borkmann30743832014-05-01 18:34:19 +0200612static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100613{
Daniel Borkmanneb9672f2014-05-01 18:34:20 +0200614 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100615 struct nlattr *nla;
616
617 if (skb_is_nonlinear(skb))
618 return 0;
619
Mathias Krause05ab8f22014-04-13 18:23:33 +0200620 if (skb->len < sizeof(struct nlattr))
621 return 0;
622
Daniel Borkmann30743832014-05-01 18:34:19 +0200623 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100624 return 0;
625
Daniel Borkmann30743832014-05-01 18:34:19 +0200626 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100627 if (nla)
628 return (void *) nla - (void *) skb->data;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return 0;
631}
632
Daniel Borkmann30743832014-05-01 18:34:19 +0200633static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100634{
Daniel Borkmanneb9672f2014-05-01 18:34:20 +0200635 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100636 struct nlattr *nla;
637
638 if (skb_is_nonlinear(skb))
639 return 0;
640
Mathias Krause05ab8f22014-04-13 18:23:33 +0200641 if (skb->len < sizeof(struct nlattr))
642 return 0;
643
Daniel Borkmann30743832014-05-01 18:34:19 +0200644 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100645 return 0;
646
Daniel Borkmann30743832014-05-01 18:34:19 +0200647 nla = (struct nlattr *) &skb->data[a];
648 if (nla->nla_len > skb->len - a)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100649 return 0;
650
Daniel Borkmann30743832014-05-01 18:34:19 +0200651 nla = nla_find_nested(nla, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100652 if (nla)
653 return (void *) nla - (void *) skb->data;
654
655 return 0;
656}
657
Daniel Borkmann30743832014-05-01 18:34:19 +0200658static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100659{
660 return raw_smp_processor_id();
661}
662
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700663/* note that this only generates 32-bit random numbers */
Daniel Borkmann30743832014-05-01 18:34:19 +0200664static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700665{
Daniel Borkmanneb9672f2014-05-01 18:34:20 +0200666 return prandom_u32();
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700667}
668
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100669static bool convert_bpf_extensions(struct sock_filter *fp,
670 struct sock_filter_int **insnp)
671{
672 struct sock_filter_int *insn = *insnp;
673
674 switch (fp->k) {
675 case SKF_AD_OFF + SKF_AD_PROTOCOL:
676 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
677
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700678 /* A = *(u16 *) (ctx + offsetof(protocol)) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200679 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
680 offsetof(struct sk_buff, protocol));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100681 /* A = ntohs(A) [emitting a nop or swap16] */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200682 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100683 break;
684
685 case SKF_AD_OFF + SKF_AD_PKTTYPE:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700686 *insn = BPF_LDX_MEM(BPF_B, BPF_REG_A, BPF_REG_CTX,
687 pkt_type_offset());
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100688 if (insn->off < 0)
689 return false;
690 insn++;
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700691 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, PKT_TYPE_MAX);
Alexei Starovoitov0dcceab2014-06-05 14:39:36 -0700692#ifdef __BIG_ENDIAN_BITFIELD
693 insn++;
David S. Millerf666f872014-06-05 16:22:02 -0700694 *insn = BPF_ALU32_IMM(BPF_RSH, BPF_REG_A, 5);
Alexei Starovoitov0dcceab2014-06-05 14:39:36 -0700695#endif
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100696 break;
697
698 case SKF_AD_OFF + SKF_AD_IFINDEX:
699 case SKF_AD_OFF + SKF_AD_HATYPE:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100700 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
701 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200702 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100703
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200704 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
705 BPF_REG_TMP, BPF_REG_CTX,
706 offsetof(struct sk_buff, dev));
707 /* if (tmp != 0) goto pc + 1 */
708 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
709 *insn++ = BPF_EXIT_INSN();
710 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
711 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
712 offsetof(struct net_device, ifindex));
713 else
714 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
715 offsetof(struct net_device, type));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100716 break;
717
718 case SKF_AD_OFF + SKF_AD_MARK:
719 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
720
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700721 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
722 offsetof(struct sk_buff, mark));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100723 break;
724
725 case SKF_AD_OFF + SKF_AD_RXHASH:
726 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
727
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700728 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
729 offsetof(struct sk_buff, hash));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100730 break;
731
732 case SKF_AD_OFF + SKF_AD_QUEUE:
733 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
734
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700735 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
736 offsetof(struct sk_buff, queue_mapping));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100737 break;
738
739 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
740 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
741 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100742 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
743
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200744 /* A = *(u16 *) (ctx + offsetof(vlan_tci)) */
745 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
746 offsetof(struct sk_buff, vlan_tci));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100747 if (fp->k == SKF_AD_OFF + SKF_AD_VLAN_TAG) {
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700748 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A,
749 ~VLAN_TAG_PRESENT);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100750 } else {
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700751 /* A >>= 12 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200752 *insn++ = BPF_ALU32_IMM(BPF_RSH, BPF_REG_A, 12);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700753 /* A &= 1 */
754 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 1);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100755 }
756 break;
757
758 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
759 case SKF_AD_OFF + SKF_AD_NLATTR:
760 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
761 case SKF_AD_OFF + SKF_AD_CPU:
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700762 case SKF_AD_OFF + SKF_AD_RANDOM:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100763 /* arg1 = ctx */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200764 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100765 /* arg2 = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200766 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100767 /* arg3 = X */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200768 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100769 /* Emit call(ctx, arg2=A, arg3=X) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100770 switch (fp->k) {
771 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200772 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100773 break;
774 case SKF_AD_OFF + SKF_AD_NLATTR:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200775 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100776 break;
777 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200778 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100779 break;
780 case SKF_AD_OFF + SKF_AD_CPU:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200781 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100782 break;
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700783 case SKF_AD_OFF + SKF_AD_RANDOM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200784 *insn = BPF_EMIT_CALL(__get_random_u32);
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700785 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100786 }
787 break;
788
789 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700790 /* A ^= X */
791 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100792 break;
793
794 default:
795 /* This is just a dummy call to avoid letting the compiler
796 * evict __bpf_call_base() as an optimization. Placed here
797 * where no-one bothers.
798 */
799 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
800 return false;
801 }
802
803 *insnp = insn;
804 return true;
805}
806
807/**
808 * sk_convert_filter - convert filter program
809 * @prog: the user passed filter program
810 * @len: the length of the user passed filter program
811 * @new_prog: buffer where converted program will be stored
812 * @new_len: pointer to store length of converted program
813 *
814 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
815 * Conversion workflow:
816 *
817 * 1) First pass for calculating the new program length:
818 * sk_convert_filter(old_prog, old_len, NULL, &new_len)
819 *
820 * 2) 2nd pass to remap in two passes: 1st pass finds new
821 * jump offsets, 2nd pass remapping:
822 * new_prog = kmalloc(sizeof(struct sock_filter_int) * new_len);
823 * sk_convert_filter(old_prog, old_len, new_prog, &new_len);
824 *
825 * User BPF's register A is mapped to our BPF register 6, user BPF
826 * register X is mapped to BPF register 7; frame pointer is always
827 * register 10; Context 'void *ctx' is stored in register 1, that is,
828 * for socket filters: ctx == 'struct sk_buff *', for seccomp:
829 * ctx == 'struct seccomp_data *'.
830 */
831int sk_convert_filter(struct sock_filter *prog, int len,
832 struct sock_filter_int *new_prog, int *new_len)
833{
834 int new_flen = 0, pass = 0, target, i;
835 struct sock_filter_int *new_insn;
836 struct sock_filter *fp;
837 int *addrs = NULL;
838 u8 bpf_src;
839
840 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
Daniel Borkmann30743832014-05-01 18:34:19 +0200841 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100842
843 if (len <= 0 || len >= BPF_MAXINSNS)
844 return -EINVAL;
845
846 if (new_prog) {
847 addrs = kzalloc(len * sizeof(*addrs), GFP_KERNEL);
848 if (!addrs)
849 return -ENOMEM;
850 }
851
852do_pass:
853 new_insn = new_prog;
854 fp = prog;
855
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200856 if (new_insn)
857 *new_insn = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100858 new_insn++;
859
860 for (i = 0; i < len; fp++, i++) {
861 struct sock_filter_int tmp_insns[6] = { };
862 struct sock_filter_int *insn = tmp_insns;
863
864 if (addrs)
865 addrs[i] = new_insn - new_prog;
866
867 switch (fp->code) {
868 /* All arithmetic insns and skb loads map as-is. */
869 case BPF_ALU | BPF_ADD | BPF_X:
870 case BPF_ALU | BPF_ADD | BPF_K:
871 case BPF_ALU | BPF_SUB | BPF_X:
872 case BPF_ALU | BPF_SUB | BPF_K:
873 case BPF_ALU | BPF_AND | BPF_X:
874 case BPF_ALU | BPF_AND | BPF_K:
875 case BPF_ALU | BPF_OR | BPF_X:
876 case BPF_ALU | BPF_OR | BPF_K:
877 case BPF_ALU | BPF_LSH | BPF_X:
878 case BPF_ALU | BPF_LSH | BPF_K:
879 case BPF_ALU | BPF_RSH | BPF_X:
880 case BPF_ALU | BPF_RSH | BPF_K:
881 case BPF_ALU | BPF_XOR | BPF_X:
882 case BPF_ALU | BPF_XOR | BPF_K:
883 case BPF_ALU | BPF_MUL | BPF_X:
884 case BPF_ALU | BPF_MUL | BPF_K:
885 case BPF_ALU | BPF_DIV | BPF_X:
886 case BPF_ALU | BPF_DIV | BPF_K:
887 case BPF_ALU | BPF_MOD | BPF_X:
888 case BPF_ALU | BPF_MOD | BPF_K:
889 case BPF_ALU | BPF_NEG:
890 case BPF_LD | BPF_ABS | BPF_W:
891 case BPF_LD | BPF_ABS | BPF_H:
892 case BPF_LD | BPF_ABS | BPF_B:
893 case BPF_LD | BPF_IND | BPF_W:
894 case BPF_LD | BPF_IND | BPF_H:
895 case BPF_LD | BPF_IND | BPF_B:
896 /* Check for overloaded BPF extension and
897 * directly convert it if found, otherwise
898 * just move on with mapping.
899 */
900 if (BPF_CLASS(fp->code) == BPF_LD &&
901 BPF_MODE(fp->code) == BPF_ABS &&
902 convert_bpf_extensions(fp, &insn))
903 break;
904
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200905 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100906 break;
907
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200908 /* Jump transformation cannot use BPF block macros
909 * everywhere as offset calculation and target updates
910 * require a bit more work than the rest, i.e. jump
911 * opcodes map as-is, but offsets need adjustment.
912 */
913
914#define BPF_EMIT_JMP \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100915 do { \
916 if (target >= len || target < 0) \
917 goto err; \
918 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
919 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
920 insn->off -= insn - tmp_insns; \
921 } while (0)
922
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200923 case BPF_JMP | BPF_JA:
924 target = i + fp->k + 1;
925 insn->code = fp->code;
926 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100927 break;
928
929 case BPF_JMP | BPF_JEQ | BPF_K:
930 case BPF_JMP | BPF_JEQ | BPF_X:
931 case BPF_JMP | BPF_JSET | BPF_K:
932 case BPF_JMP | BPF_JSET | BPF_X:
933 case BPF_JMP | BPF_JGT | BPF_K:
934 case BPF_JMP | BPF_JGT | BPF_X:
935 case BPF_JMP | BPF_JGE | BPF_K:
936 case BPF_JMP | BPF_JGE | BPF_X:
937 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
938 /* BPF immediates are signed, zero extend
939 * immediate into tmp register and use it
940 * in compare insn.
941 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200942 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100943
Daniel Borkmann30743832014-05-01 18:34:19 +0200944 insn->a_reg = BPF_REG_A;
945 insn->x_reg = BPF_REG_TMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100946 bpf_src = BPF_X;
947 } else {
Daniel Borkmann30743832014-05-01 18:34:19 +0200948 insn->a_reg = BPF_REG_A;
949 insn->x_reg = BPF_REG_X;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100950 insn->imm = fp->k;
951 bpf_src = BPF_SRC(fp->code);
952 }
953
954 /* Common case where 'jump_false' is next insn. */
955 if (fp->jf == 0) {
956 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
957 target = i + fp->jt + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200958 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100959 break;
960 }
961
962 /* Convert JEQ into JNE when 'jump_true' is next insn. */
963 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
964 insn->code = BPF_JMP | BPF_JNE | bpf_src;
965 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200966 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100967 break;
968 }
969
970 /* Other jumps are mapped into two insns: Jxx and JA. */
971 target = i + fp->jt + 1;
972 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200973 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100974 insn++;
975
976 insn->code = BPF_JMP | BPF_JA;
977 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200978 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100979 break;
980
981 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
982 case BPF_LDX | BPF_MSH | BPF_B:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700983 /* tmp = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200984 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
David S. Miller1268e252014-05-13 13:13:33 -0400985 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200986 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700987 /* A &= 0xf */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200988 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700989 /* A <<= 2 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200990 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700991 /* X = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200992 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700993 /* A = tmp */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200994 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100995 break;
996
997 /* RET_K, RET_A are remaped into 2 insns. */
998 case BPF_RET | BPF_A:
999 case BPF_RET | BPF_K:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +02001000 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
1001 BPF_K : BPF_X, BPF_REG_0,
1002 BPF_REG_A, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -07001003 *insn = BPF_EXIT_INSN();
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001004 break;
1005
1006 /* Store to stack. */
1007 case BPF_ST:
1008 case BPF_STX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +02001009 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
1010 BPF_ST ? BPF_REG_A : BPF_REG_X,
1011 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001012 break;
1013
1014 /* Load from stack. */
1015 case BPF_LD | BPF_MEM:
1016 case BPF_LDX | BPF_MEM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +02001017 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
1018 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
1019 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001020 break;
1021
1022 /* A = K or X = K */
1023 case BPF_LD | BPF_IMM:
1024 case BPF_LDX | BPF_IMM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +02001025 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
1026 BPF_REG_A : BPF_REG_X, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001027 break;
1028
1029 /* X = A */
1030 case BPF_MISC | BPF_TAX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +02001031 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001032 break;
1033
1034 /* A = X */
1035 case BPF_MISC | BPF_TXA:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +02001036 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001037 break;
1038
1039 /* A = skb->len or X = skb->len */
1040 case BPF_LD | BPF_W | BPF_LEN:
1041 case BPF_LDX | BPF_W | BPF_LEN:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +02001042 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
1043 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
1044 offsetof(struct sk_buff, len));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001045 break;
1046
Daniel Borkmannf8f6d672014-05-29 10:22:51 +02001047 /* Access seccomp_data fields. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001048 case BPF_LDX | BPF_ABS | BPF_W:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -07001049 /* A = *(u32 *) (ctx + K) */
1050 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001051 break;
1052
Daniel Borkmannf8f6d672014-05-29 10:22:51 +02001053 /* Unkown instruction. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001054 default:
1055 goto err;
1056 }
1057
1058 insn++;
1059 if (new_prog)
1060 memcpy(new_insn, tmp_insns,
1061 sizeof(*insn) * (insn - tmp_insns));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001062 new_insn += insn - tmp_insns;
1063 }
1064
1065 if (!new_prog) {
1066 /* Only calculating new length. */
1067 *new_len = new_insn - new_prog;
1068 return 0;
1069 }
1070
1071 pass++;
1072 if (new_flen != new_insn - new_prog) {
1073 new_flen = new_insn - new_prog;
1074 if (pass > 2)
1075 goto err;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001076 goto do_pass;
1077 }
1078
1079 kfree(addrs);
1080 BUG_ON(*new_len != new_flen);
1081 return 0;
1082err:
1083 kfree(addrs);
1084 return -EINVAL;
1085}
1086
1087/* Security:
1088 *
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001089 * A BPF program is able to use 16 cells of memory to store intermediate
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001090 * values (check u32 mem[BPF_MEMWORDS] in sk_run_filter()).
1091 *
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001092 * As we dont want to clear mem[] array for each packet going through
1093 * sk_run_filter(), we check that filter loaded by user never try to read
1094 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001095 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001096 */
1097static int check_load_and_stores(struct sock_filter *filter, int flen)
1098{
Daniel Borkmann34805932014-05-29 10:22:50 +02001099 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001100 int pc, ret = 0;
1101
1102 BUILD_BUG_ON(BPF_MEMWORDS > 16);
Daniel Borkmann34805932014-05-29 10:22:50 +02001103
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001104 masks = kmalloc(flen * sizeof(*masks), GFP_KERNEL);
1105 if (!masks)
1106 return -ENOMEM;
Daniel Borkmann34805932014-05-29 10:22:50 +02001107
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001108 memset(masks, 0xff, flen * sizeof(*masks));
1109
1110 for (pc = 0; pc < flen; pc++) {
1111 memvalid &= masks[pc];
1112
1113 switch (filter[pc].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +02001114 case BPF_ST:
1115 case BPF_STX:
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001116 memvalid |= (1 << filter[pc].k);
1117 break;
Daniel Borkmann34805932014-05-29 10:22:50 +02001118 case BPF_LD | BPF_MEM:
1119 case BPF_LDX | BPF_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001120 if (!(memvalid & (1 << filter[pc].k))) {
1121 ret = -EINVAL;
1122 goto error;
1123 }
1124 break;
Daniel Borkmann34805932014-05-29 10:22:50 +02001125 case BPF_JMP | BPF_JA:
1126 /* A jump must set masks on target */
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001127 masks[pc + 1 + filter[pc].k] &= memvalid;
1128 memvalid = ~0;
1129 break;
Daniel Borkmann34805932014-05-29 10:22:50 +02001130 case BPF_JMP | BPF_JEQ | BPF_K:
1131 case BPF_JMP | BPF_JEQ | BPF_X:
1132 case BPF_JMP | BPF_JGE | BPF_K:
1133 case BPF_JMP | BPF_JGE | BPF_X:
1134 case BPF_JMP | BPF_JGT | BPF_K:
1135 case BPF_JMP | BPF_JGT | BPF_X:
1136 case BPF_JMP | BPF_JSET | BPF_K:
1137 case BPF_JMP | BPF_JSET | BPF_X:
1138 /* A jump must set masks on targets */
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001139 masks[pc + 1 + filter[pc].jt] &= memvalid;
1140 masks[pc + 1 + filter[pc].jf] &= memvalid;
1141 memvalid = ~0;
1142 break;
1143 }
1144 }
1145error:
1146 kfree(masks);
1147 return ret;
1148}
1149
Daniel Borkmann34805932014-05-29 10:22:50 +02001150static bool chk_code_allowed(u16 code_to_probe)
1151{
1152 static const bool codes[] = {
1153 /* 32 bit ALU operations */
1154 [BPF_ALU | BPF_ADD | BPF_K] = true,
1155 [BPF_ALU | BPF_ADD | BPF_X] = true,
1156 [BPF_ALU | BPF_SUB | BPF_K] = true,
1157 [BPF_ALU | BPF_SUB | BPF_X] = true,
1158 [BPF_ALU | BPF_MUL | BPF_K] = true,
1159 [BPF_ALU | BPF_MUL | BPF_X] = true,
1160 [BPF_ALU | BPF_DIV | BPF_K] = true,
1161 [BPF_ALU | BPF_DIV | BPF_X] = true,
1162 [BPF_ALU | BPF_MOD | BPF_K] = true,
1163 [BPF_ALU | BPF_MOD | BPF_X] = true,
1164 [BPF_ALU | BPF_AND | BPF_K] = true,
1165 [BPF_ALU | BPF_AND | BPF_X] = true,
1166 [BPF_ALU | BPF_OR | BPF_K] = true,
1167 [BPF_ALU | BPF_OR | BPF_X] = true,
1168 [BPF_ALU | BPF_XOR | BPF_K] = true,
1169 [BPF_ALU | BPF_XOR | BPF_X] = true,
1170 [BPF_ALU | BPF_LSH | BPF_K] = true,
1171 [BPF_ALU | BPF_LSH | BPF_X] = true,
1172 [BPF_ALU | BPF_RSH | BPF_K] = true,
1173 [BPF_ALU | BPF_RSH | BPF_X] = true,
1174 [BPF_ALU | BPF_NEG] = true,
1175 /* Load instructions */
1176 [BPF_LD | BPF_W | BPF_ABS] = true,
1177 [BPF_LD | BPF_H | BPF_ABS] = true,
1178 [BPF_LD | BPF_B | BPF_ABS] = true,
1179 [BPF_LD | BPF_W | BPF_LEN] = true,
1180 [BPF_LD | BPF_W | BPF_IND] = true,
1181 [BPF_LD | BPF_H | BPF_IND] = true,
1182 [BPF_LD | BPF_B | BPF_IND] = true,
1183 [BPF_LD | BPF_IMM] = true,
1184 [BPF_LD | BPF_MEM] = true,
1185 [BPF_LDX | BPF_W | BPF_LEN] = true,
1186 [BPF_LDX | BPF_B | BPF_MSH] = true,
1187 [BPF_LDX | BPF_IMM] = true,
1188 [BPF_LDX | BPF_MEM] = true,
1189 /* Store instructions */
1190 [BPF_ST] = true,
1191 [BPF_STX] = true,
1192 /* Misc instructions */
1193 [BPF_MISC | BPF_TAX] = true,
1194 [BPF_MISC | BPF_TXA] = true,
1195 /* Return instructions */
1196 [BPF_RET | BPF_K] = true,
1197 [BPF_RET | BPF_A] = true,
1198 /* Jump instructions */
1199 [BPF_JMP | BPF_JA] = true,
1200 [BPF_JMP | BPF_JEQ | BPF_K] = true,
1201 [BPF_JMP | BPF_JEQ | BPF_X] = true,
1202 [BPF_JMP | BPF_JGE | BPF_K] = true,
1203 [BPF_JMP | BPF_JGE | BPF_X] = true,
1204 [BPF_JMP | BPF_JGT | BPF_K] = true,
1205 [BPF_JMP | BPF_JGT | BPF_X] = true,
1206 [BPF_JMP | BPF_JSET | BPF_K] = true,
1207 [BPF_JMP | BPF_JSET | BPF_X] = true,
1208 };
1209
1210 if (code_to_probe >= ARRAY_SIZE(codes))
1211 return false;
1212
1213 return codes[code_to_probe];
1214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/**
1217 * sk_chk_filter - verify socket filter code
1218 * @filter: filter to verify
1219 * @flen: length of filter
1220 *
1221 * Check the user's filter code. If we let some ugly
1222 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -08001223 * no references or jumps that are out of range, no illegal
1224 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -08001226 * All jumps are forward as they are not signed.
1227 *
1228 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
Dan Carpenter4f25af22011-10-17 21:04:20 +00001230int sk_chk_filter(struct sock_filter *filter, unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Daniel Borkmannaa1113d2012-12-28 10:50:17 +00001232 bool anc_found;
Daniel Borkmann34805932014-05-29 10:22:50 +02001233 int pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
David S. Miller1b93ae642005-12-27 13:57:59 -08001235 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return -EINVAL;
1237
Daniel Borkmann34805932014-05-29 10:22:50 +02001238 /* Check the filter code now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 for (pc = 0; pc < flen; pc++) {
Tetsuo Handacba328f2010-11-16 15:19:51 +00001240 struct sock_filter *ftest = &filter[pc];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Daniel Borkmann34805932014-05-29 10:22:50 +02001242 /* May we actually operate on this code? */
1243 if (!chk_code_allowed(ftest->code))
Tetsuo Handacba328f2010-11-16 15:19:51 +00001244 return -EINVAL;
Daniel Borkmann34805932014-05-29 10:22:50 +02001245
Kris Katterjohn93699862006-01-04 13:58:36 -08001246 /* Some instructions need special checks */
Daniel Borkmann34805932014-05-29 10:22:50 +02001247 switch (ftest->code) {
1248 case BPF_ALU | BPF_DIV | BPF_K:
1249 case BPF_ALU | BPF_MOD | BPF_K:
1250 /* Check for division by zero */
Eric Dumazetb6069a92012-09-07 22:03:35 +00001251 if (ftest->k == 0)
1252 return -EINVAL;
1253 break;
Daniel Borkmann34805932014-05-29 10:22:50 +02001254 case BPF_LD | BPF_MEM:
1255 case BPF_LDX | BPF_MEM:
1256 case BPF_ST:
1257 case BPF_STX:
1258 /* Check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -08001259 if (ftest->k >= BPF_MEMWORDS)
1260 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +00001261 break;
Daniel Borkmann34805932014-05-29 10:22:50 +02001262 case BPF_JMP | BPF_JA:
1263 /* Note, the large ftest->k might cause loops.
Kris Katterjohn93699862006-01-04 13:58:36 -08001264 * Compare this with conditional jumps below,
1265 * where offsets are limited. --ANK (981016)
1266 */
Daniel Borkmann34805932014-05-29 10:22:50 +02001267 if (ftest->k >= (unsigned int)(flen - pc - 1))
Kris Katterjohn93699862006-01-04 13:58:36 -08001268 return -EINVAL;
1269 break;
Daniel Borkmann34805932014-05-29 10:22:50 +02001270 case BPF_JMP | BPF_JEQ | BPF_K:
1271 case BPF_JMP | BPF_JEQ | BPF_X:
1272 case BPF_JMP | BPF_JGE | BPF_K:
1273 case BPF_JMP | BPF_JGE | BPF_X:
1274 case BPF_JMP | BPF_JGT | BPF_K:
1275 case BPF_JMP | BPF_JGT | BPF_X:
1276 case BPF_JMP | BPF_JSET | BPF_K:
1277 case BPF_JMP | BPF_JSET | BPF_X:
1278 /* Both conditionals must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +00001279 if (pc + ftest->jt + 1 >= flen ||
1280 pc + ftest->jf + 1 >= flen)
1281 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +00001282 break;
Daniel Borkmann34805932014-05-29 10:22:50 +02001283 case BPF_LD | BPF_W | BPF_ABS:
1284 case BPF_LD | BPF_H | BPF_ABS:
1285 case BPF_LD | BPF_B | BPF_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +00001286 anc_found = false;
Daniel Borkmann34805932014-05-29 10:22:50 +02001287 if (bpf_anc_helper(ftest) & BPF_ANC)
1288 anc_found = true;
1289 /* Ancillary operation unknown or unsupported */
Daniel Borkmannaa1113d2012-12-28 10:50:17 +00001290 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1291 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +00001292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
Daniel Borkmann34805932014-05-29 10:22:50 +02001295 /* Last instruction must be a RET code */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +00001296 switch (filter[flen - 1].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +02001297 case BPF_RET | BPF_K:
1298 case BPF_RET | BPF_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +00001299 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +00001300 }
Daniel Borkmann34805932014-05-29 10:22:50 +02001301
Tetsuo Handacba328f2010-11-16 15:19:51 +00001302 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
Stephen Hemmingerb7156312008-04-10 01:33:47 -07001304EXPORT_SYMBOL(sk_chk_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001306static int sk_store_orig_filter(struct sk_filter *fp,
1307 const struct sock_fprog *fprog)
1308{
1309 unsigned int fsize = sk_filter_proglen(fprog);
1310 struct sock_fprog_kern *fkprog;
1311
1312 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1313 if (!fp->orig_prog)
1314 return -ENOMEM;
1315
1316 fkprog = fp->orig_prog;
1317 fkprog->len = fprog->len;
1318 fkprog->filter = kmemdup(fp->insns, fsize, GFP_KERNEL);
1319 if (!fkprog->filter) {
1320 kfree(fp->orig_prog);
1321 return -ENOMEM;
1322 }
1323
1324 return 0;
1325}
1326
1327static void sk_release_orig_filter(struct sk_filter *fp)
1328{
1329 struct sock_fprog_kern *fprog = fp->orig_prog;
1330
1331 if (fprog) {
1332 kfree(fprog->filter);
1333 kfree(fprog);
1334 }
1335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/**
Eric Dumazet46bcf142010-12-06 09:29:43 -08001338 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001339 * @rcu: rcu_head that contains the sk_filter to free
1340 */
Daniel Borkmannfbc907f2014-03-28 18:58:20 +01001341static void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001342{
1343 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1344
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001345 sk_release_orig_filter(fp);
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001346 sk_filter_free(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001347}
Daniel Borkmannfbc907f2014-03-28 18:58:20 +01001348
1349/**
1350 * sk_filter_release - release a socket filter
1351 * @fp: filter to remove
1352 *
1353 * Remove a filter from a socket and release its resources.
1354 */
1355static void sk_filter_release(struct sk_filter *fp)
1356{
1357 if (atomic_dec_and_test(&fp->refcnt))
1358 call_rcu(&fp->rcu, sk_filter_release_rcu);
1359}
1360
1361void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1362{
1363 atomic_sub(sk_filter_size(fp->len), &sk->sk_omem_alloc);
1364 sk_filter_release(fp);
1365}
1366
1367void sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1368{
1369 atomic_inc(&fp->refcnt);
1370 atomic_add(sk_filter_size(fp->len), &sk->sk_omem_alloc);
1371}
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001372
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001373static struct sk_filter *__sk_migrate_realloc(struct sk_filter *fp,
1374 struct sock *sk,
1375 unsigned int len)
1376{
1377 struct sk_filter *fp_new;
1378
1379 if (sk == NULL)
1380 return krealloc(fp, len, GFP_KERNEL);
1381
1382 fp_new = sock_kmalloc(sk, len, GFP_KERNEL);
1383 if (fp_new) {
Daniel Borkmanneb9672f2014-05-01 18:34:20 +02001384 *fp_new = *fp;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001385 /* As we're kepping orig_prog in fp_new along,
1386 * we need to make sure we're not evicting it
1387 * from the old fp.
1388 */
1389 fp->orig_prog = NULL;
1390 sk_filter_uncharge(sk, fp);
1391 }
1392
1393 return fp_new;
1394}
1395
1396static struct sk_filter *__sk_migrate_filter(struct sk_filter *fp,
1397 struct sock *sk)
1398{
1399 struct sock_filter *old_prog;
1400 struct sk_filter *old_fp;
Daniel Borkmann34805932014-05-29 10:22:50 +02001401 int err, new_len, old_len = fp->len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001402
1403 /* We are free to overwrite insns et al right here as it
1404 * won't be used at this point in time anymore internally
1405 * after the migration to the internal BPF instruction
1406 * representation.
1407 */
1408 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1409 sizeof(struct sock_filter_int));
1410
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001411 /* Conversion cannot happen on overlapping memory areas,
1412 * so we need to keep the user BPF around until the 2nd
1413 * pass. At this time, the user BPF is stored in fp->insns.
1414 */
1415 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1416 GFP_KERNEL);
1417 if (!old_prog) {
1418 err = -ENOMEM;
1419 goto out_err;
1420 }
1421
1422 /* 1st pass: calculate the new program length. */
1423 err = sk_convert_filter(old_prog, old_len, NULL, &new_len);
1424 if (err)
1425 goto out_err_free;
1426
1427 /* Expand fp for appending the new filter representation. */
1428 old_fp = fp;
1429 fp = __sk_migrate_realloc(old_fp, sk, sk_filter_size(new_len));
1430 if (!fp) {
1431 /* The old_fp is still around in case we couldn't
1432 * allocate new memory, so uncharge on that one.
1433 */
1434 fp = old_fp;
1435 err = -ENOMEM;
1436 goto out_err_free;
1437 }
1438
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001439 fp->len = new_len;
1440
1441 /* 2nd pass: remap sock_filter insns into sock_filter_int insns. */
1442 err = sk_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
1443 if (err)
1444 /* 2nd sk_convert_filter() can fail only if it fails
1445 * to allocate memory, remapping must succeed. Note,
1446 * that at this time old_fp has already been released
1447 * by __sk_migrate_realloc().
1448 */
1449 goto out_err_free;
1450
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001451 sk_filter_select_runtime(fp);
1452
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001453 kfree(old_prog);
1454 return fp;
1455
1456out_err_free:
1457 kfree(old_prog);
1458out_err:
1459 /* Rollback filter setup. */
1460 if (sk != NULL)
1461 sk_filter_uncharge(sk, fp);
1462 else
1463 kfree(fp);
1464 return ERR_PTR(err);
1465}
1466
Alexei Starovoitov62258272014-05-13 19:50:46 -07001467void __weak bpf_int_jit_compile(struct sk_filter *prog)
1468{
1469}
1470
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001471/**
1472 * sk_filter_select_runtime - select execution runtime for BPF program
1473 * @fp: sk_filter populated with internal BPF program
1474 *
1475 * try to JIT internal BPF program, if JIT is not available select interpreter
1476 * BPF program will be executed via SK_RUN_FILTER() macro
1477 */
1478void sk_filter_select_runtime(struct sk_filter *fp)
1479{
1480 fp->bpf_func = (void *) __sk_run_filter;
1481
1482 /* Probe if internal BPF can be JITed */
1483 bpf_int_jit_compile(fp);
1484}
1485EXPORT_SYMBOL_GPL(sk_filter_select_runtime);
1486
1487/* free internal BPF program */
1488void sk_filter_free(struct sk_filter *fp)
1489{
1490 bpf_jit_free(fp);
1491}
1492EXPORT_SYMBOL_GPL(sk_filter_free);
1493
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001494static struct sk_filter *__sk_prepare_filter(struct sk_filter *fp,
1495 struct sock *sk)
Jiri Pirko302d6632012-03-31 11:01:19 +00001496{
1497 int err;
1498
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001499 fp->bpf_func = NULL;
Daniel Borkmannf8bbbfc2014-03-28 18:58:18 +01001500 fp->jited = 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001501
1502 err = sk_chk_filter(fp->insns, fp->len);
Leon Yu418c96a2014-06-01 05:37:25 +00001503 if (err) {
1504 if (sk != NULL)
1505 sk_filter_uncharge(sk, fp);
1506 else
1507 kfree(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001508 return ERR_PTR(err);
Leon Yu418c96a2014-06-01 05:37:25 +00001509 }
Jiri Pirko302d6632012-03-31 11:01:19 +00001510
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001511 /* Probe if we can JIT compile the filter and if so, do
1512 * the compilation of the filter.
1513 */
Jiri Pirko302d6632012-03-31 11:01:19 +00001514 bpf_jit_compile(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001515
1516 /* JIT compiler couldn't process this filter, so do the
1517 * internal BPF translation for the optimized interpreter.
1518 */
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001519 if (!fp->jited)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001520 fp = __sk_migrate_filter(fp, sk);
1521
1522 return fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001523}
1524
1525/**
1526 * sk_unattached_filter_create - create an unattached filter
1527 * @fprog: the filter program
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001528 * @pfp: the unattached filter that is created
Jiri Pirko302d6632012-03-31 11:01:19 +00001529 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001530 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +00001531 * sanity checks on it to make sure it does not explode on us later.
1532 * If an error occurs or there is insufficient memory for the filter
1533 * a negative errno code is returned. On success the return is zero.
1534 */
1535int sk_unattached_filter_create(struct sk_filter **pfp,
Daniel Borkmannb1fcd352014-05-23 18:43:58 +02001536 struct sock_fprog_kern *fprog)
Jiri Pirko302d6632012-03-31 11:01:19 +00001537{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001538 unsigned int fsize = sk_filter_proglen(fprog);
Jiri Pirko302d6632012-03-31 11:01:19 +00001539 struct sk_filter *fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001540
1541 /* Make sure new filter is there and in the right amounts. */
1542 if (fprog->filter == NULL)
1543 return -EINVAL;
1544
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -07001545 fp = kmalloc(sk_filter_size(fprog->len), GFP_KERNEL);
Jiri Pirko302d6632012-03-31 11:01:19 +00001546 if (!fp)
1547 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001548
Jiri Pirko302d6632012-03-31 11:01:19 +00001549 memcpy(fp->insns, fprog->filter, fsize);
1550
1551 atomic_set(&fp->refcnt, 1);
1552 fp->len = fprog->len;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001553 /* Since unattached filters are not copied back to user
1554 * space through sk_get_filter(), we do not need to hold
1555 * a copy here, and can spare us the work.
1556 */
1557 fp->orig_prog = NULL;
Jiri Pirko302d6632012-03-31 11:01:19 +00001558
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001559 /* __sk_prepare_filter() already takes care of uncharging
1560 * memory in case something goes wrong.
1561 */
1562 fp = __sk_prepare_filter(fp, NULL);
1563 if (IS_ERR(fp))
1564 return PTR_ERR(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001565
1566 *pfp = fp;
1567 return 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001568}
1569EXPORT_SYMBOL_GPL(sk_unattached_filter_create);
1570
1571void sk_unattached_filter_destroy(struct sk_filter *fp)
1572{
1573 sk_filter_release(fp);
1574}
1575EXPORT_SYMBOL_GPL(sk_unattached_filter_destroy);
1576
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001577/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * sk_attach_filter - attach a socket filter
1579 * @fprog: the filter program
1580 * @sk: the socket to use
1581 *
1582 * Attach the user's filter code. We first run some sanity checks on
1583 * it to make sure it does not explode on us later. If an error
1584 * occurs or there is insufficient memory for the filter a negative
1585 * errno code is returned. On success the return is zero.
1586 */
1587int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1588{
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001589 struct sk_filter *fp, *old_fp;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001590 unsigned int fsize = sk_filter_proglen(fprog);
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -07001591 unsigned int sk_fsize = sk_filter_size(fprog->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int err;
1593
Vincent Bernatd59577b2013-01-16 22:55:49 +01001594 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1595 return -EPERM;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* Make sure new filter is there and in the right amounts. */
Kris Katterjohne35bedf2006-01-17 02:25:52 -08001598 if (fprog->filter == NULL)
1599 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -07001601 fp = sock_kmalloc(sk, sk_fsize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (!fp)
1603 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
Alexei Starovoitovd45ed4a2013-10-04 00:14:06 -07001606 sock_kfree_s(sk, fp, sk_fsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return -EFAULT;
1608 }
1609
1610 atomic_set(&fp->refcnt, 1);
1611 fp->len = fprog->len;
1612
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001613 err = sk_store_orig_filter(fp, fprog);
1614 if (err) {
1615 sk_filter_uncharge(sk, fp);
1616 return -ENOMEM;
1617 }
1618
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001619 /* __sk_prepare_filter() already takes care of uncharging
1620 * memory in case something goes wrong.
1621 */
1622 fp = __sk_prepare_filter(fp, sk);
1623 if (IS_ERR(fp))
1624 return PTR_ERR(fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Eric Dumazetf91ff5b2010-09-27 06:07:30 +00001626 old_fp = rcu_dereference_protected(sk->sk_filter,
1627 sock_owned_by_user(sk));
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001628 rcu_assign_pointer(sk->sk_filter, fp);
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001629
Olof Johansson9b013e02007-10-18 21:48:39 -07001630 if (old_fp)
Eric Dumazet46bcf142010-12-06 09:29:43 -08001631 sk_filter_uncharge(sk, old_fp);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001632
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +00001635EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Pavel Emelyanov55b33322007-10-17 21:21:26 -07001637int sk_detach_filter(struct sock *sk)
1638{
1639 int ret = -ENOENT;
1640 struct sk_filter *filter;
1641
Vincent Bernatd59577b2013-01-16 22:55:49 +01001642 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1643 return -EPERM;
1644
Eric Dumazetf91ff5b2010-09-27 06:07:30 +00001645 filter = rcu_dereference_protected(sk->sk_filter,
1646 sock_owned_by_user(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -07001647 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001648 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -08001649 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -07001650 ret = 0;
1651 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001652
Pavel Emelyanov55b33322007-10-17 21:21:26 -07001653 return ret;
1654}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +00001655EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001656
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001657int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
1658 unsigned int len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001659{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001660 struct sock_fprog_kern *fprog;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001661 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001662 int ret = 0;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001663
1664 lock_sock(sk);
1665 filter = rcu_dereference_protected(sk->sk_filter,
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001666 sock_owned_by_user(sk));
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001667 if (!filter)
1668 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001669
1670 /* We're copying the filter that has been originally attached,
1671 * so no conversion/decode needed anymore.
1672 */
1673 fprog = filter->orig_prog;
1674
1675 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001676 if (!len)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001677 /* User space only enquires number of filter blocks. */
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001678 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001679
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001680 ret = -EINVAL;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001681 if (len < fprog->len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001682 goto out;
1683
1684 ret = -EFAULT;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001685 if (copy_to_user(ubuf, fprog->filter, sk_filter_proglen(fprog)))
1686 goto out;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001687
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001688 /* Instead of bytes, the API requests to return the number
1689 * of filter blocks.
1690 */
1691 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00001692out:
1693 release_sock(sk);
1694 return ret;
1695}