blob: bba502f7cd575692a9ed795f83d2fdedf2428ed6 [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.
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -070021 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
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>
Jiri Pirko10b89ee42015-05-12 14:56:09 +020039#include <net/flow_dissector.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/errno.h>
41#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
Dmitry Mishin40daafc2006-04-18 14:50:10 -070043#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/filter.h>
David S. Miller86e4ca62011-05-26 15:00:31 -040045#include <linux/ratelimit.h>
Will Drewry46b325c2012-04-12 16:47:52 -050046#include <linux/seccomp.h>
Eric Dumazetf3335032012-10-27 02:26:17 +000047#include <linux/if_vlan.h>
Alexei Starovoitov89aa0752014-12-01 15:06:35 -080048#include <linux/bpf.h>
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -070049#include <net/sch_generic.h>
Daniel Borkmann8d20aab2015-07-15 14:21:42 +020050#include <net/cls_cgroup.h>
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -070051#include <net/dst_metadata.h>
Daniel Borkmannc46646d2015-09-30 01:41:51 +020052#include <net/dst.h>
Craig Gallek538950a2016-01-04 17:41:47 -050053#include <net/sock_reuseport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/**
Stephen Hemminger43db6d62008-04-10 01:43:09 -070056 * sk_filter - run a packet through a socket filter
57 * @sk: sock associated with &sk_buff
58 * @skb: buffer to filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -070059 *
Alexei Starovoitovff936a02015-10-07 10:55:41 -070060 * 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
Stephen Hemminger43db6d62008-04-10 01:43:09 -070062 * than pkt_len we keep whole skb->data. This is the socket level
Alexei Starovoitovff936a02015-10-07 10:55:41 -070063 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
Stephen Hemminger43db6d62008-04-10 01:43:09 -070064 * be accepted or -EPERM if the packet should be tossed.
65 *
66 */
67int sk_filter(struct sock *sk, struct sk_buff *skb)
68{
69 int err;
70 struct sk_filter *filter;
71
Mel Gormanc93bdd02012-07-31 16:44:19 -070072 /*
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
Stephen Hemminger43db6d62008-04-10 01:43:09 -070080 err = security_sock_rcv_skb(sk, skb);
81 if (err)
82 return err;
83
Eric Dumazet80f8f102011-01-18 07:46:52 +000084 rcu_read_lock();
85 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -070086 if (filter) {
Alexei Starovoitovff936a02015-10-07 10:55:41 -070087 unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
Eric Dumazet0d7da9d2010-10-25 03:47:05 +000088
Stephen Hemminger43db6d62008-04-10 01:43:09 -070089 err = pkt_len ? pskb_trim(skb, pkt_len) : -EPERM;
90 }
Eric Dumazet80f8f102011-01-18 07:46:52 +000091 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -070092
93 return err;
94}
95EXPORT_SYMBOL(sk_filter);
96
Daniel Borkmann30743832014-05-01 18:34:19 +020097static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010098{
Alexander Duyck56193d12014-09-05 19:20:26 -040099 return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100100}
101
Daniel Borkmann30743832014-05-01 18:34:19 +0200102static u64 __skb_get_nlattr(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100103{
Daniel Borkmanneb9672f2014-05-01 18:34:20 +0200104 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100105 struct nlattr *nla;
106
107 if (skb_is_nonlinear(skb))
108 return 0;
109
Mathias Krause05ab8f22014-04-13 18:23:33 +0200110 if (skb->len < sizeof(struct nlattr))
111 return 0;
112
Daniel Borkmann30743832014-05-01 18:34:19 +0200113 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100114 return 0;
115
Daniel Borkmann30743832014-05-01 18:34:19 +0200116 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100117 if (nla)
118 return (void *) nla - (void *) skb->data;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121}
122
Daniel Borkmann30743832014-05-01 18:34:19 +0200123static u64 __skb_get_nlattr_nest(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100124{
Daniel Borkmanneb9672f2014-05-01 18:34:20 +0200125 struct sk_buff *skb = (struct sk_buff *)(unsigned long) ctx;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100126 struct nlattr *nla;
127
128 if (skb_is_nonlinear(skb))
129 return 0;
130
Mathias Krause05ab8f22014-04-13 18:23:33 +0200131 if (skb->len < sizeof(struct nlattr))
132 return 0;
133
Daniel Borkmann30743832014-05-01 18:34:19 +0200134 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100135 return 0;
136
Daniel Borkmann30743832014-05-01 18:34:19 +0200137 nla = (struct nlattr *) &skb->data[a];
138 if (nla->nla_len > skb->len - a)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100139 return 0;
140
Daniel Borkmann30743832014-05-01 18:34:19 +0200141 nla = nla_find_nested(nla, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100142 if (nla)
143 return (void *) nla - (void *) skb->data;
144
145 return 0;
146}
147
Daniel Borkmann30743832014-05-01 18:34:19 +0200148static u64 __get_raw_cpu_id(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100149{
150 return raw_smp_processor_id();
151}
152
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700153static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
154 struct bpf_insn *insn_buf)
155{
156 struct bpf_insn *insn = insn_buf;
157
158 switch (skb_field) {
159 case SKF_AD_MARK:
160 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
161
162 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
163 offsetof(struct sk_buff, mark));
164 break;
165
166 case SKF_AD_PKTTYPE:
167 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
168 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
169#ifdef __BIG_ENDIAN_BITFIELD
170 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
171#endif
172 break;
173
174 case SKF_AD_QUEUE:
175 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
176
177 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
178 offsetof(struct sk_buff, queue_mapping));
179 break;
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700180
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700181 case SKF_AD_VLAN_TAG:
182 case SKF_AD_VLAN_TAG_PRESENT:
183 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
184 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
185
186 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
187 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
188 offsetof(struct sk_buff, vlan_tci));
189 if (skb_field == SKF_AD_VLAN_TAG) {
190 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
191 ~VLAN_TAG_PRESENT);
192 } else {
193 /* dst_reg >>= 12 */
194 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
195 /* dst_reg &= 1 */
196 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
197 }
198 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700199 }
200
201 return insn - insn_buf;
202}
203
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100204static bool convert_bpf_extensions(struct sock_filter *fp,
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700205 struct bpf_insn **insnp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100206{
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700207 struct bpf_insn *insn = *insnp;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700208 u32 cnt;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100209
210 switch (fp->k) {
211 case SKF_AD_OFF + SKF_AD_PROTOCOL:
Daniel Borkmann0b8c7072015-03-19 19:38:27 +0100212 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
213
214 /* A = *(u16 *) (CTX + offsetof(protocol)) */
215 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
216 offsetof(struct sk_buff, protocol));
217 /* A = ntohs(A) [emitting a nop or swap16] */
218 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100219 break;
220
221 case SKF_AD_OFF + SKF_AD_PKTTYPE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700222 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
223 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100224 break;
225
226 case SKF_AD_OFF + SKF_AD_IFINDEX:
227 case SKF_AD_OFF + SKF_AD_HATYPE:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100228 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
229 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200230 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100231
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200232 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
233 BPF_REG_TMP, BPF_REG_CTX,
234 offsetof(struct sk_buff, dev));
235 /* if (tmp != 0) goto pc + 1 */
236 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
237 *insn++ = BPF_EXIT_INSN();
238 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
239 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
240 offsetof(struct net_device, ifindex));
241 else
242 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
243 offsetof(struct net_device, type));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100244 break;
245
246 case SKF_AD_OFF + SKF_AD_MARK:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700247 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
248 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100249 break;
250
251 case SKF_AD_OFF + SKF_AD_RXHASH:
252 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
253
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700254 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
255 offsetof(struct sk_buff, hash));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100256 break;
257
258 case SKF_AD_OFF + SKF_AD_QUEUE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700259 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
260 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100261 break;
262
263 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700264 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
265 BPF_REG_A, BPF_REG_CTX, insn);
266 insn += cnt - 1;
267 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100268
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700269 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
270 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
271 BPF_REG_A, BPF_REG_CTX, insn);
272 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100273 break;
274
Michal Sekletar27cd5452015-03-24 14:48:41 +0100275 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
276 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
277
278 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
279 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
280 offsetof(struct sk_buff, vlan_proto));
281 /* A = ntohs(A) [emitting a nop or swap16] */
282 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
283 break;
284
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100285 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
286 case SKF_AD_OFF + SKF_AD_NLATTR:
287 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
288 case SKF_AD_OFF + SKF_AD_CPU:
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700289 case SKF_AD_OFF + SKF_AD_RANDOM:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700290 /* arg1 = CTX */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200291 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100292 /* arg2 = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200293 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100294 /* arg3 = X */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200295 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
Alexei Starovoitove430f342014-06-06 14:46:06 -0700296 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100297 switch (fp->k) {
298 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200299 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100300 break;
301 case SKF_AD_OFF + SKF_AD_NLATTR:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200302 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100303 break;
304 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200305 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100306 break;
307 case SKF_AD_OFF + SKF_AD_CPU:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200308 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100309 break;
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700310 case SKF_AD_OFF + SKF_AD_RANDOM:
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200311 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
312 bpf_user_rnd_init_once();
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700313 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100314 }
315 break;
316
317 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700318 /* A ^= X */
319 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100320 break;
321
322 default:
323 /* This is just a dummy call to avoid letting the compiler
324 * evict __bpf_call_base() as an optimization. Placed here
325 * where no-one bothers.
326 */
327 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
328 return false;
329 }
330
331 *insnp = insn;
332 return true;
333}
334
335/**
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700336 * bpf_convert_filter - convert filter program
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100337 * @prog: the user passed filter program
338 * @len: the length of the user passed filter program
339 * @new_prog: buffer where converted program will be stored
340 * @new_len: pointer to store length of converted program
341 *
342 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
343 * Conversion workflow:
344 *
345 * 1) First pass for calculating the new program length:
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700346 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100347 *
348 * 2) 2nd pass to remap in two passes: 1st pass finds new
349 * jump offsets, 2nd pass remapping:
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700350 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700351 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100352 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200353static int bpf_convert_filter(struct sock_filter *prog, int len,
354 struct bpf_insn *new_prog, int *new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100355{
356 int new_flen = 0, pass = 0, target, i;
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700357 struct bpf_insn *new_insn;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100358 struct sock_filter *fp;
359 int *addrs = NULL;
360 u8 bpf_src;
361
362 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
Daniel Borkmann30743832014-05-01 18:34:19 +0200363 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100364
Kees Cook6f9a0932014-06-18 15:34:57 -0700365 if (len <= 0 || len > BPF_MAXINSNS)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100366 return -EINVAL;
367
368 if (new_prog) {
Daniel Borkmann658da932015-05-06 16:12:29 +0200369 addrs = kcalloc(len, sizeof(*addrs),
370 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100371 if (!addrs)
372 return -ENOMEM;
373 }
374
375do_pass:
376 new_insn = new_prog;
377 fp = prog;
378
Daniel Borkmann8b614ae2015-12-17 23:51:54 +0100379 /* Classic BPF related prologue emission. */
380 if (new_insn) {
381 /* Classic BPF expects A and X to be reset first. These need
382 * to be guaranteed to be the first two instructions.
383 */
384 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
385 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
386
387 /* All programs must keep CTX in callee saved BPF_REG_CTX.
388 * In eBPF case it's done by the compiler, here we need to
389 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
390 */
391 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
392 } else {
393 new_insn += 3;
394 }
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100395
396 for (i = 0; i < len; fp++, i++) {
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700397 struct bpf_insn tmp_insns[6] = { };
398 struct bpf_insn *insn = tmp_insns;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100399
400 if (addrs)
401 addrs[i] = new_insn - new_prog;
402
403 switch (fp->code) {
404 /* All arithmetic insns and skb loads map as-is. */
405 case BPF_ALU | BPF_ADD | BPF_X:
406 case BPF_ALU | BPF_ADD | BPF_K:
407 case BPF_ALU | BPF_SUB | BPF_X:
408 case BPF_ALU | BPF_SUB | BPF_K:
409 case BPF_ALU | BPF_AND | BPF_X:
410 case BPF_ALU | BPF_AND | BPF_K:
411 case BPF_ALU | BPF_OR | BPF_X:
412 case BPF_ALU | BPF_OR | BPF_K:
413 case BPF_ALU | BPF_LSH | BPF_X:
414 case BPF_ALU | BPF_LSH | BPF_K:
415 case BPF_ALU | BPF_RSH | BPF_X:
416 case BPF_ALU | BPF_RSH | BPF_K:
417 case BPF_ALU | BPF_XOR | BPF_X:
418 case BPF_ALU | BPF_XOR | BPF_K:
419 case BPF_ALU | BPF_MUL | BPF_X:
420 case BPF_ALU | BPF_MUL | BPF_K:
421 case BPF_ALU | BPF_DIV | BPF_X:
422 case BPF_ALU | BPF_DIV | BPF_K:
423 case BPF_ALU | BPF_MOD | BPF_X:
424 case BPF_ALU | BPF_MOD | BPF_K:
425 case BPF_ALU | BPF_NEG:
426 case BPF_LD | BPF_ABS | BPF_W:
427 case BPF_LD | BPF_ABS | BPF_H:
428 case BPF_LD | BPF_ABS | BPF_B:
429 case BPF_LD | BPF_IND | BPF_W:
430 case BPF_LD | BPF_IND | BPF_H:
431 case BPF_LD | BPF_IND | BPF_B:
432 /* Check for overloaded BPF extension and
433 * directly convert it if found, otherwise
434 * just move on with mapping.
435 */
436 if (BPF_CLASS(fp->code) == BPF_LD &&
437 BPF_MODE(fp->code) == BPF_ABS &&
438 convert_bpf_extensions(fp, &insn))
439 break;
440
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200441 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100442 break;
443
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200444 /* Jump transformation cannot use BPF block macros
445 * everywhere as offset calculation and target updates
446 * require a bit more work than the rest, i.e. jump
447 * opcodes map as-is, but offsets need adjustment.
448 */
449
450#define BPF_EMIT_JMP \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100451 do { \
452 if (target >= len || target < 0) \
453 goto err; \
454 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
455 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
456 insn->off -= insn - tmp_insns; \
457 } while (0)
458
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200459 case BPF_JMP | BPF_JA:
460 target = i + fp->k + 1;
461 insn->code = fp->code;
462 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100463 break;
464
465 case BPF_JMP | BPF_JEQ | BPF_K:
466 case BPF_JMP | BPF_JEQ | BPF_X:
467 case BPF_JMP | BPF_JSET | BPF_K:
468 case BPF_JMP | BPF_JSET | BPF_X:
469 case BPF_JMP | BPF_JGT | BPF_K:
470 case BPF_JMP | BPF_JGT | BPF_X:
471 case BPF_JMP | BPF_JGE | BPF_K:
472 case BPF_JMP | BPF_JGE | BPF_X:
473 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
474 /* BPF immediates are signed, zero extend
475 * immediate into tmp register and use it
476 * in compare insn.
477 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200478 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100479
Alexei Starovoitove430f342014-06-06 14:46:06 -0700480 insn->dst_reg = BPF_REG_A;
481 insn->src_reg = BPF_REG_TMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100482 bpf_src = BPF_X;
483 } else {
Alexei Starovoitove430f342014-06-06 14:46:06 -0700484 insn->dst_reg = BPF_REG_A;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100485 insn->imm = fp->k;
486 bpf_src = BPF_SRC(fp->code);
Tycho Andersen19539ce2015-09-10 18:25:07 -0600487 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100488 }
489
490 /* Common case where 'jump_false' is next insn. */
491 if (fp->jf == 0) {
492 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
493 target = i + fp->jt + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200494 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100495 break;
496 }
497
498 /* Convert JEQ into JNE when 'jump_true' is next insn. */
499 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
500 insn->code = BPF_JMP | BPF_JNE | bpf_src;
501 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200502 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100503 break;
504 }
505
506 /* Other jumps are mapped into two insns: Jxx and JA. */
507 target = i + fp->jt + 1;
508 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200509 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100510 insn++;
511
512 insn->code = BPF_JMP | BPF_JA;
513 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200514 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100515 break;
516
517 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
518 case BPF_LDX | BPF_MSH | BPF_B:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700519 /* tmp = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200520 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
David S. Miller1268e252014-05-13 13:13:33 -0400521 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200522 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700523 /* A &= 0xf */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200524 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700525 /* A <<= 2 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200526 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700527 /* X = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200528 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700529 /* A = tmp */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200530 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100531 break;
532
533 /* RET_K, RET_A are remaped into 2 insns. */
534 case BPF_RET | BPF_A:
535 case BPF_RET | BPF_K:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200536 *insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
537 BPF_K : BPF_X, BPF_REG_0,
538 BPF_REG_A, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700539 *insn = BPF_EXIT_INSN();
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100540 break;
541
542 /* Store to stack. */
543 case BPF_ST:
544 case BPF_STX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200545 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
546 BPF_ST ? BPF_REG_A : BPF_REG_X,
547 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100548 break;
549
550 /* Load from stack. */
551 case BPF_LD | BPF_MEM:
552 case BPF_LDX | BPF_MEM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200553 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
554 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
555 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100556 break;
557
558 /* A = K or X = K */
559 case BPF_LD | BPF_IMM:
560 case BPF_LDX | BPF_IMM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200561 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
562 BPF_REG_A : BPF_REG_X, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100563 break;
564
565 /* X = A */
566 case BPF_MISC | BPF_TAX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200567 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100568 break;
569
570 /* A = X */
571 case BPF_MISC | BPF_TXA:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200572 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100573 break;
574
575 /* A = skb->len or X = skb->len */
576 case BPF_LD | BPF_W | BPF_LEN:
577 case BPF_LDX | BPF_W | BPF_LEN:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200578 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
579 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
580 offsetof(struct sk_buff, len));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100581 break;
582
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200583 /* Access seccomp_data fields. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100584 case BPF_LDX | BPF_ABS | BPF_W:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700585 /* A = *(u32 *) (ctx + K) */
586 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100587 break;
588
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -0500589 /* Unknown instruction. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100590 default:
591 goto err;
592 }
593
594 insn++;
595 if (new_prog)
596 memcpy(new_insn, tmp_insns,
597 sizeof(*insn) * (insn - tmp_insns));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100598 new_insn += insn - tmp_insns;
599 }
600
601 if (!new_prog) {
602 /* Only calculating new length. */
603 *new_len = new_insn - new_prog;
604 return 0;
605 }
606
607 pass++;
608 if (new_flen != new_insn - new_prog) {
609 new_flen = new_insn - new_prog;
610 if (pass > 2)
611 goto err;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100612 goto do_pass;
613 }
614
615 kfree(addrs);
616 BUG_ON(*new_len != new_flen);
617 return 0;
618err:
619 kfree(addrs);
620 return -EINVAL;
621}
622
623/* Security:
624 *
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000625 * As we dont want to clear mem[] array for each packet going through
Li RongQing8ea6e342014-10-10 13:56:51 +0800626 * __bpf_prog_run(), we check that filter loaded by user never try to read
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000627 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300628 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000629 */
Eric Dumazetec31a052014-07-12 15:49:16 +0200630static int check_load_and_stores(const struct sock_filter *filter, int flen)
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000631{
Daniel Borkmann34805932014-05-29 10:22:50 +0200632 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000633 int pc, ret = 0;
634
635 BUILD_BUG_ON(BPF_MEMWORDS > 16);
Daniel Borkmann34805932014-05-29 10:22:50 +0200636
Tobias Klauser99e72a02014-06-24 15:33:22 +0200637 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000638 if (!masks)
639 return -ENOMEM;
Daniel Borkmann34805932014-05-29 10:22:50 +0200640
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000641 memset(masks, 0xff, flen * sizeof(*masks));
642
643 for (pc = 0; pc < flen; pc++) {
644 memvalid &= masks[pc];
645
646 switch (filter[pc].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200647 case BPF_ST:
648 case BPF_STX:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000649 memvalid |= (1 << filter[pc].k);
650 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200651 case BPF_LD | BPF_MEM:
652 case BPF_LDX | BPF_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000653 if (!(memvalid & (1 << filter[pc].k))) {
654 ret = -EINVAL;
655 goto error;
656 }
657 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200658 case BPF_JMP | BPF_JA:
659 /* A jump must set masks on target */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000660 masks[pc + 1 + filter[pc].k] &= memvalid;
661 memvalid = ~0;
662 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200663 case BPF_JMP | BPF_JEQ | BPF_K:
664 case BPF_JMP | BPF_JEQ | BPF_X:
665 case BPF_JMP | BPF_JGE | BPF_K:
666 case BPF_JMP | BPF_JGE | BPF_X:
667 case BPF_JMP | BPF_JGT | BPF_K:
668 case BPF_JMP | BPF_JGT | BPF_X:
669 case BPF_JMP | BPF_JSET | BPF_K:
670 case BPF_JMP | BPF_JSET | BPF_X:
671 /* A jump must set masks on targets */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000672 masks[pc + 1 + filter[pc].jt] &= memvalid;
673 masks[pc + 1 + filter[pc].jf] &= memvalid;
674 memvalid = ~0;
675 break;
676 }
677 }
678error:
679 kfree(masks);
680 return ret;
681}
682
Daniel Borkmann34805932014-05-29 10:22:50 +0200683static bool chk_code_allowed(u16 code_to_probe)
684{
685 static const bool codes[] = {
686 /* 32 bit ALU operations */
687 [BPF_ALU | BPF_ADD | BPF_K] = true,
688 [BPF_ALU | BPF_ADD | BPF_X] = true,
689 [BPF_ALU | BPF_SUB | BPF_K] = true,
690 [BPF_ALU | BPF_SUB | BPF_X] = true,
691 [BPF_ALU | BPF_MUL | BPF_K] = true,
692 [BPF_ALU | BPF_MUL | BPF_X] = true,
693 [BPF_ALU | BPF_DIV | BPF_K] = true,
694 [BPF_ALU | BPF_DIV | BPF_X] = true,
695 [BPF_ALU | BPF_MOD | BPF_K] = true,
696 [BPF_ALU | BPF_MOD | BPF_X] = true,
697 [BPF_ALU | BPF_AND | BPF_K] = true,
698 [BPF_ALU | BPF_AND | BPF_X] = true,
699 [BPF_ALU | BPF_OR | BPF_K] = true,
700 [BPF_ALU | BPF_OR | BPF_X] = true,
701 [BPF_ALU | BPF_XOR | BPF_K] = true,
702 [BPF_ALU | BPF_XOR | BPF_X] = true,
703 [BPF_ALU | BPF_LSH | BPF_K] = true,
704 [BPF_ALU | BPF_LSH | BPF_X] = true,
705 [BPF_ALU | BPF_RSH | BPF_K] = true,
706 [BPF_ALU | BPF_RSH | BPF_X] = true,
707 [BPF_ALU | BPF_NEG] = true,
708 /* Load instructions */
709 [BPF_LD | BPF_W | BPF_ABS] = true,
710 [BPF_LD | BPF_H | BPF_ABS] = true,
711 [BPF_LD | BPF_B | BPF_ABS] = true,
712 [BPF_LD | BPF_W | BPF_LEN] = true,
713 [BPF_LD | BPF_W | BPF_IND] = true,
714 [BPF_LD | BPF_H | BPF_IND] = true,
715 [BPF_LD | BPF_B | BPF_IND] = true,
716 [BPF_LD | BPF_IMM] = true,
717 [BPF_LD | BPF_MEM] = true,
718 [BPF_LDX | BPF_W | BPF_LEN] = true,
719 [BPF_LDX | BPF_B | BPF_MSH] = true,
720 [BPF_LDX | BPF_IMM] = true,
721 [BPF_LDX | BPF_MEM] = true,
722 /* Store instructions */
723 [BPF_ST] = true,
724 [BPF_STX] = true,
725 /* Misc instructions */
726 [BPF_MISC | BPF_TAX] = true,
727 [BPF_MISC | BPF_TXA] = true,
728 /* Return instructions */
729 [BPF_RET | BPF_K] = true,
730 [BPF_RET | BPF_A] = true,
731 /* Jump instructions */
732 [BPF_JMP | BPF_JA] = true,
733 [BPF_JMP | BPF_JEQ | BPF_K] = true,
734 [BPF_JMP | BPF_JEQ | BPF_X] = true,
735 [BPF_JMP | BPF_JGE | BPF_K] = true,
736 [BPF_JMP | BPF_JGE | BPF_X] = true,
737 [BPF_JMP | BPF_JGT | BPF_K] = true,
738 [BPF_JMP | BPF_JGT | BPF_X] = true,
739 [BPF_JMP | BPF_JSET | BPF_K] = true,
740 [BPF_JMP | BPF_JSET | BPF_X] = true,
741 };
742
743 if (code_to_probe >= ARRAY_SIZE(codes))
744 return false;
745
746 return codes[code_to_probe];
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/**
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -0700750 * bpf_check_classic - verify socket filter code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * @filter: filter to verify
752 * @flen: length of filter
753 *
754 * Check the user's filter code. If we let some ugly
755 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800756 * no references or jumps that are out of range, no illegal
757 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800759 * All jumps are forward as they are not signed.
760 *
761 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200763static int bpf_check_classic(const struct sock_filter *filter,
764 unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000766 bool anc_found;
Daniel Borkmann34805932014-05-29 10:22:50 +0200767 int pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
David S. Miller1b93ae642005-12-27 13:57:59 -0800769 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -EINVAL;
771
Daniel Borkmann34805932014-05-29 10:22:50 +0200772 /* Check the filter code now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 for (pc = 0; pc < flen; pc++) {
Eric Dumazetec31a052014-07-12 15:49:16 +0200774 const struct sock_filter *ftest = &filter[pc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Daniel Borkmann34805932014-05-29 10:22:50 +0200776 /* May we actually operate on this code? */
777 if (!chk_code_allowed(ftest->code))
Tetsuo Handacba328f2010-11-16 15:19:51 +0000778 return -EINVAL;
Daniel Borkmann34805932014-05-29 10:22:50 +0200779
Kris Katterjohn93699862006-01-04 13:58:36 -0800780 /* Some instructions need special checks */
Daniel Borkmann34805932014-05-29 10:22:50 +0200781 switch (ftest->code) {
782 case BPF_ALU | BPF_DIV | BPF_K:
783 case BPF_ALU | BPF_MOD | BPF_K:
784 /* Check for division by zero */
Eric Dumazetb6069a92012-09-07 22:03:35 +0000785 if (ftest->k == 0)
786 return -EINVAL;
787 break;
Rabin Vincent229394e2016-01-12 20:17:08 +0100788 case BPF_ALU | BPF_LSH | BPF_K:
789 case BPF_ALU | BPF_RSH | BPF_K:
790 if (ftest->k >= 32)
791 return -EINVAL;
792 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200793 case BPF_LD | BPF_MEM:
794 case BPF_LDX | BPF_MEM:
795 case BPF_ST:
796 case BPF_STX:
797 /* Check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800798 if (ftest->k >= BPF_MEMWORDS)
799 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000800 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200801 case BPF_JMP | BPF_JA:
802 /* Note, the large ftest->k might cause loops.
Kris Katterjohn93699862006-01-04 13:58:36 -0800803 * Compare this with conditional jumps below,
804 * where offsets are limited. --ANK (981016)
805 */
Daniel Borkmann34805932014-05-29 10:22:50 +0200806 if (ftest->k >= (unsigned int)(flen - pc - 1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800807 return -EINVAL;
808 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200809 case BPF_JMP | BPF_JEQ | BPF_K:
810 case BPF_JMP | BPF_JEQ | BPF_X:
811 case BPF_JMP | BPF_JGE | BPF_K:
812 case BPF_JMP | BPF_JGE | BPF_X:
813 case BPF_JMP | BPF_JGT | BPF_K:
814 case BPF_JMP | BPF_JGT | BPF_X:
815 case BPF_JMP | BPF_JSET | BPF_K:
816 case BPF_JMP | BPF_JSET | BPF_X:
817 /* Both conditionals must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000818 if (pc + ftest->jt + 1 >= flen ||
819 pc + ftest->jf + 1 >= flen)
820 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000821 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200822 case BPF_LD | BPF_W | BPF_ABS:
823 case BPF_LD | BPF_H | BPF_ABS:
824 case BPF_LD | BPF_B | BPF_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000825 anc_found = false;
Daniel Borkmann34805932014-05-29 10:22:50 +0200826 if (bpf_anc_helper(ftest) & BPF_ANC)
827 anc_found = true;
828 /* Ancillary operation unknown or unsupported */
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000829 if (anc_found == false && ftest->k >= SKF_AD_OFF)
830 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Daniel Borkmann34805932014-05-29 10:22:50 +0200834 /* Last instruction must be a RET code */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000835 switch (filter[flen - 1].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200836 case BPF_RET | BPF_K:
837 case BPF_RET | BPF_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000838 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000839 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200840
Tetsuo Handacba328f2010-11-16 15:19:51 +0000841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700844static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
845 const struct sock_fprog *fprog)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100846{
Alexei Starovoitov009937e2014-07-30 20:34:13 -0700847 unsigned int fsize = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100848 struct sock_fprog_kern *fkprog;
849
850 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
851 if (!fp->orig_prog)
852 return -ENOMEM;
853
854 fkprog = fp->orig_prog;
855 fkprog->len = fprog->len;
Daniel Borkmann658da932015-05-06 16:12:29 +0200856
857 fkprog->filter = kmemdup(fp->insns, fsize,
858 GFP_KERNEL | __GFP_NOWARN);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100859 if (!fkprog->filter) {
860 kfree(fp->orig_prog);
861 return -ENOMEM;
862 }
863
864 return 0;
865}
866
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700867static void bpf_release_orig_filter(struct bpf_prog *fp)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100868{
869 struct sock_fprog_kern *fprog = fp->orig_prog;
870
871 if (fprog) {
872 kfree(fprog->filter);
873 kfree(fprog);
874 }
875}
876
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700877static void __bpf_prog_release(struct bpf_prog *prog)
878{
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100879 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800880 bpf_prog_put(prog);
881 } else {
882 bpf_release_orig_filter(prog);
883 bpf_prog_free(prog);
884 }
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700885}
886
Pablo Neira34c5bd62014-07-29 17:36:28 +0200887static void __sk_filter_release(struct sk_filter *fp)
888{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700889 __bpf_prog_release(fp->prog);
890 kfree(fp);
Pablo Neira34c5bd62014-07-29 17:36:28 +0200891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800894 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700895 * @rcu: rcu_head that contains the sk_filter to free
896 */
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100897static void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700898{
899 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
900
Pablo Neira34c5bd62014-07-29 17:36:28 +0200901 __sk_filter_release(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700902}
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100903
904/**
905 * sk_filter_release - release a socket filter
906 * @fp: filter to remove
907 *
908 * Remove a filter from a socket and release its resources.
909 */
910static void sk_filter_release(struct sk_filter *fp)
911{
912 if (atomic_dec_and_test(&fp->refcnt))
913 call_rcu(&fp->rcu, sk_filter_release_rcu);
914}
915
916void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
917{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700918 u32 filter_size = bpf_prog_size(fp->prog->len);
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700919
920 atomic_sub(filter_size, &sk->sk_omem_alloc);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100921 sk_filter_release(fp);
922}
923
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700924/* try to charge the socket memory if there is space available
925 * return true on success
926 */
927bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100928{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700929 u32 filter_size = bpf_prog_size(fp->prog->len);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700930
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700931 /* same check as in sock_kmalloc() */
932 if (filter_size <= sysctl_optmem_max &&
933 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
934 atomic_inc(&fp->refcnt);
935 atomic_add(filter_size, &sk->sk_omem_alloc);
936 return true;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100937 }
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700938 return false;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100939}
940
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700941static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100942{
943 struct sock_filter *old_prog;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700944 struct bpf_prog *old_fp;
Daniel Borkmann34805932014-05-29 10:22:50 +0200945 int err, new_len, old_len = fp->len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100946
947 /* We are free to overwrite insns et al right here as it
948 * won't be used at this point in time anymore internally
949 * after the migration to the internal BPF instruction
950 * representation.
951 */
952 BUILD_BUG_ON(sizeof(struct sock_filter) !=
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700953 sizeof(struct bpf_insn));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100954
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100955 /* Conversion cannot happen on overlapping memory areas,
956 * so we need to keep the user BPF around until the 2nd
957 * pass. At this time, the user BPF is stored in fp->insns.
958 */
959 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
Daniel Borkmann658da932015-05-06 16:12:29 +0200960 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100961 if (!old_prog) {
962 err = -ENOMEM;
963 goto out_err;
964 }
965
966 /* 1st pass: calculate the new program length. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700967 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100968 if (err)
969 goto out_err_free;
970
971 /* Expand fp for appending the new filter representation. */
972 old_fp = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200973 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100974 if (!fp) {
975 /* The old_fp is still around in case we couldn't
976 * allocate new memory, so uncharge on that one.
977 */
978 fp = old_fp;
979 err = -ENOMEM;
980 goto out_err_free;
981 }
982
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100983 fp->len = new_len;
984
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700985 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700986 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100987 if (err)
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700988 /* 2nd bpf_convert_filter() can fail only if it fails
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100989 * to allocate memory, remapping must succeed. Note,
990 * that at this time old_fp has already been released
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700991 * by krealloc().
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100992 */
993 goto out_err_free;
994
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700995 bpf_prog_select_runtime(fp);
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -0700996
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100997 kfree(old_prog);
998 return fp;
999
1000out_err_free:
1001 kfree(old_prog);
1002out_err:
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001003 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001004 return ERR_PTR(err);
1005}
1006
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001007static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1008 bpf_aux_classic_check_t trans)
Jiri Pirko302d6632012-03-31 11:01:19 +00001009{
1010 int err;
1011
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001012 fp->bpf_func = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001013 fp->jited = 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001014
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -07001015 err = bpf_check_classic(fp->insns, fp->len);
Leon Yu418c96a2014-06-01 05:37:25 +00001016 if (err) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001017 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001018 return ERR_PTR(err);
Leon Yu418c96a2014-06-01 05:37:25 +00001019 }
Jiri Pirko302d6632012-03-31 11:01:19 +00001020
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001021 /* There might be additional checks and transformations
1022 * needed on classic filters, f.e. in case of seccomp.
1023 */
1024 if (trans) {
1025 err = trans(fp->insns, fp->len);
1026 if (err) {
1027 __bpf_prog_release(fp);
1028 return ERR_PTR(err);
1029 }
1030 }
1031
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001032 /* Probe if we can JIT compile the filter and if so, do
1033 * the compilation of the filter.
1034 */
Jiri Pirko302d6632012-03-31 11:01:19 +00001035 bpf_jit_compile(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001036
1037 /* JIT compiler couldn't process this filter, so do the
1038 * internal BPF translation for the optimized interpreter.
1039 */
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001040 if (!fp->jited)
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001041 fp = bpf_migrate_filter(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001042
1043 return fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001044}
1045
1046/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001047 * bpf_prog_create - create an unattached filter
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001048 * @pfp: the unattached filter that is created
Tobias Klauser677a9fd2014-06-24 15:33:21 +02001049 * @fprog: the filter program
Jiri Pirko302d6632012-03-31 11:01:19 +00001050 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001051 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +00001052 * sanity checks on it to make sure it does not explode on us later.
1053 * If an error occurs or there is insufficient memory for the filter
1054 * a negative errno code is returned. On success the return is zero.
1055 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001056int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
Jiri Pirko302d6632012-03-31 11:01:19 +00001057{
Alexei Starovoitov009937e2014-07-30 20:34:13 -07001058 unsigned int fsize = bpf_classic_proglen(fprog);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001059 struct bpf_prog *fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001060
1061 /* Make sure new filter is there and in the right amounts. */
1062 if (fprog->filter == NULL)
1063 return -EINVAL;
1064
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001065 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Jiri Pirko302d6632012-03-31 11:01:19 +00001066 if (!fp)
1067 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001068
Jiri Pirko302d6632012-03-31 11:01:19 +00001069 memcpy(fp->insns, fprog->filter, fsize);
1070
Jiri Pirko302d6632012-03-31 11:01:19 +00001071 fp->len = fprog->len;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001072 /* Since unattached filters are not copied back to user
1073 * space through sk_get_filter(), we do not need to hold
1074 * a copy here, and can spare us the work.
1075 */
1076 fp->orig_prog = NULL;
Jiri Pirko302d6632012-03-31 11:01:19 +00001077
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001078 /* bpf_prepare_filter() already takes care of freeing
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001079 * memory in case something goes wrong.
1080 */
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001081 fp = bpf_prepare_filter(fp, NULL);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001082 if (IS_ERR(fp))
1083 return PTR_ERR(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001084
1085 *pfp = fp;
1086 return 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001087}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001088EXPORT_SYMBOL_GPL(bpf_prog_create);
Jiri Pirko302d6632012-03-31 11:01:19 +00001089
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001090/**
1091 * bpf_prog_create_from_user - create an unattached filter from user buffer
1092 * @pfp: the unattached filter that is created
1093 * @fprog: the filter program
1094 * @trans: post-classic verifier transformation handler
Daniel Borkmannbab18992015-10-02 15:17:33 +02001095 * @save_orig: save classic BPF program
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001096 *
1097 * This function effectively does the same as bpf_prog_create(), only
1098 * that it builds up its insns buffer from user space provided buffer.
1099 * It also allows for passing a bpf_aux_classic_check_t handler.
1100 */
1101int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
Daniel Borkmannbab18992015-10-02 15:17:33 +02001102 bpf_aux_classic_check_t trans, bool save_orig)
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001103{
1104 unsigned int fsize = bpf_classic_proglen(fprog);
1105 struct bpf_prog *fp;
Daniel Borkmannbab18992015-10-02 15:17:33 +02001106 int err;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001107
1108 /* Make sure new filter is there and in the right amounts. */
1109 if (fprog->filter == NULL)
1110 return -EINVAL;
1111
1112 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1113 if (!fp)
1114 return -ENOMEM;
1115
1116 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1117 __bpf_prog_free(fp);
1118 return -EFAULT;
1119 }
1120
1121 fp->len = fprog->len;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001122 fp->orig_prog = NULL;
1123
Daniel Borkmannbab18992015-10-02 15:17:33 +02001124 if (save_orig) {
1125 err = bpf_prog_store_orig_filter(fp, fprog);
1126 if (err) {
1127 __bpf_prog_free(fp);
1128 return -ENOMEM;
1129 }
1130 }
1131
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001132 /* bpf_prepare_filter() already takes care of freeing
1133 * memory in case something goes wrong.
1134 */
1135 fp = bpf_prepare_filter(fp, trans);
1136 if (IS_ERR(fp))
1137 return PTR_ERR(fp);
1138
1139 *pfp = fp;
1140 return 0;
1141}
David S. Miller2ea273d2015-08-17 14:37:06 -07001142EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001143
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001144void bpf_prog_destroy(struct bpf_prog *fp)
Jiri Pirko302d6632012-03-31 11:01:19 +00001145{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001146 __bpf_prog_release(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001147}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001148EXPORT_SYMBOL_GPL(bpf_prog_destroy);
Jiri Pirko302d6632012-03-31 11:01:19 +00001149
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001150static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1151{
1152 struct sk_filter *fp, *old_fp;
1153
1154 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1155 if (!fp)
1156 return -ENOMEM;
1157
1158 fp->prog = prog;
1159 atomic_set(&fp->refcnt, 0);
1160
1161 if (!sk_filter_charge(sk, fp)) {
1162 kfree(fp);
1163 return -ENOMEM;
1164 }
1165
1166 old_fp = rcu_dereference_protected(sk->sk_filter,
1167 sock_owned_by_user(sk));
1168 rcu_assign_pointer(sk->sk_filter, fp);
1169
1170 if (old_fp)
1171 sk_filter_uncharge(sk, old_fp);
1172
1173 return 0;
1174}
1175
Craig Gallek538950a2016-01-04 17:41:47 -05001176static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1177{
1178 struct bpf_prog *old_prog;
1179 int err;
1180
1181 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1182 return -ENOMEM;
1183
1184 if (sk_unhashed(sk)) {
1185 err = reuseport_alloc(sk);
1186 if (err)
1187 return err;
1188 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1189 /* The socket wasn't bound with SO_REUSEPORT */
1190 return -EINVAL;
1191 }
1192
1193 old_prog = reuseport_attach_prog(sk, prog);
1194 if (old_prog)
1195 bpf_prog_destroy(old_prog);
1196
1197 return 0;
1198}
1199
1200static
1201struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1202{
1203 unsigned int fsize = bpf_classic_proglen(fprog);
1204 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1205 struct bpf_prog *prog;
1206 int err;
1207
1208 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1209 return ERR_PTR(-EPERM);
1210
1211 /* Make sure new filter is there and in the right amounts. */
1212 if (fprog->filter == NULL)
1213 return ERR_PTR(-EINVAL);
1214
1215 prog = bpf_prog_alloc(bpf_fsize, 0);
1216 if (!prog)
1217 return ERR_PTR(-ENOMEM);
1218
1219 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1220 __bpf_prog_free(prog);
1221 return ERR_PTR(-EFAULT);
1222 }
1223
1224 prog->len = fprog->len;
1225
1226 err = bpf_prog_store_orig_filter(prog, fprog);
1227 if (err) {
1228 __bpf_prog_free(prog);
1229 return ERR_PTR(-ENOMEM);
1230 }
1231
1232 /* bpf_prepare_filter() already takes care of freeing
1233 * memory in case something goes wrong.
1234 */
1235 return bpf_prepare_filter(prog, NULL);
1236}
1237
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001238/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 * sk_attach_filter - attach a socket filter
1240 * @fprog: the filter program
1241 * @sk: the socket to use
1242 *
1243 * Attach the user's filter code. We first run some sanity checks on
1244 * it to make sure it does not explode on us later. If an error
1245 * occurs or there is insufficient memory for the filter a negative
1246 * errno code is returned. On success the return is zero.
1247 */
1248int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1249{
Craig Gallek538950a2016-01-04 17:41:47 -05001250 struct bpf_prog *prog = __get_filter(fprog, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int err;
1252
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001253 if (IS_ERR(prog))
1254 return PTR_ERR(prog);
1255
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001256 err = __sk_attach_prog(prog, sk);
1257 if (err < 0) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001258 __bpf_prog_release(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001259 return err;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001260 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001261
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +00001264EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Craig Gallek538950a2016-01-04 17:41:47 -05001266int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001267{
Craig Gallek538950a2016-01-04 17:41:47 -05001268 struct bpf_prog *prog = __get_filter(fprog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001269 int err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001270
Alexei Starovoitov198bf1b2014-12-10 20:14:55 -08001271 if (IS_ERR(prog))
1272 return PTR_ERR(prog);
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001273
Craig Gallek538950a2016-01-04 17:41:47 -05001274 err = __reuseport_attach_prog(prog, sk);
1275 if (err < 0) {
1276 __bpf_prog_release(prog);
1277 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001278 }
1279
Craig Gallek538950a2016-01-04 17:41:47 -05001280 return 0;
1281}
1282
1283static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1284{
1285 struct bpf_prog *prog;
1286
1287 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1288 return ERR_PTR(-EPERM);
1289
1290 prog = bpf_prog_get(ufd);
1291 if (IS_ERR(prog))
1292 return prog;
1293
1294 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1295 bpf_prog_put(prog);
1296 return ERR_PTR(-EINVAL);
1297 }
1298
1299 return prog;
1300}
1301
1302int sk_attach_bpf(u32 ufd, struct sock *sk)
1303{
1304 struct bpf_prog *prog = __get_bpf(ufd, sk);
1305 int err;
1306
1307 if (IS_ERR(prog))
1308 return PTR_ERR(prog);
1309
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001310 err = __sk_attach_prog(prog, sk);
1311 if (err < 0) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001312 bpf_prog_put(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001313 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001314 }
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001315
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001316 return 0;
1317}
1318
Craig Gallek538950a2016-01-04 17:41:47 -05001319int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1320{
1321 struct bpf_prog *prog = __get_bpf(ufd, sk);
1322 int err;
1323
1324 if (IS_ERR(prog))
1325 return PTR_ERR(prog);
1326
1327 err = __reuseport_attach_prog(prog, sk);
1328 if (err < 0) {
1329 bpf_prog_put(prog);
1330 return err;
1331 }
1332
1333 return 0;
1334}
1335
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001336#define BPF_LDST_LEN 16U
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001337
1338static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001339{
1340 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001341 int offset = (int) r2;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001342 void *from = (void *) (long) r3;
1343 unsigned int len = (unsigned int) r4;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001344 char buf[BPF_LDST_LEN];
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001345 void *ptr;
1346
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001347 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM)))
1348 return -EINVAL;
1349
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001350 /* bpf verifier guarantees that:
1351 * 'from' pointer points to bpf program stack
1352 * 'len' bytes of it were initialized
1353 * 'len' > 0
1354 * 'skb' is a valid pointer to 'struct sk_buff'
1355 *
1356 * so check for invalid 'offset' and too large 'len'
1357 */
Alexei Starovoitova1661512015-04-15 12:55:45 -07001358 if (unlikely((u32) offset > 0xffff || len > sizeof(buf)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001359 return -EFAULT;
1360
Alexei Starovoitova1661512015-04-15 12:55:45 -07001361 if (unlikely(skb_cloned(skb) &&
Alexei Starovoitov34312052015-06-04 10:11:53 -07001362 !skb_clone_writable(skb, offset + len)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001363 return -EFAULT;
1364
1365 ptr = skb_header_pointer(skb, offset, len, buf);
1366 if (unlikely(!ptr))
1367 return -EFAULT;
1368
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001369 if (flags & BPF_F_RECOMPUTE_CSUM)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001370 skb_postpull_rcsum(skb, ptr, len);
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001371
1372 memcpy(ptr, from, len);
1373
1374 if (ptr == buf)
1375 /* skb_store_bits cannot return -EFAULT here */
1376 skb_store_bits(skb, offset, ptr, len);
1377
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001378 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001379 skb_postpush_rcsum(skb, ptr, len);
1380
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001381 return 0;
1382}
1383
1384const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1385 .func = bpf_skb_store_bytes,
1386 .gpl_only = false,
1387 .ret_type = RET_INTEGER,
1388 .arg1_type = ARG_PTR_TO_CTX,
1389 .arg2_type = ARG_ANYTHING,
1390 .arg3_type = ARG_PTR_TO_STACK,
1391 .arg4_type = ARG_CONST_STACK_SIZE,
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001392 .arg5_type = ARG_ANYTHING,
1393};
1394
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001395static u64 bpf_skb_load_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1396{
1397 const struct sk_buff *skb = (const struct sk_buff *)(unsigned long) r1;
1398 int offset = (int) r2;
1399 void *to = (void *)(unsigned long) r3;
1400 unsigned int len = (unsigned int) r4;
1401 void *ptr;
1402
1403 if (unlikely((u32) offset > 0xffff || len > BPF_LDST_LEN))
1404 return -EFAULT;
1405
1406 ptr = skb_header_pointer(skb, offset, len, to);
1407 if (unlikely(!ptr))
1408 return -EFAULT;
1409 if (ptr != to)
1410 memcpy(to, ptr, len);
1411
1412 return 0;
1413}
1414
1415const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1416 .func = bpf_skb_load_bytes,
1417 .gpl_only = false,
1418 .ret_type = RET_INTEGER,
1419 .arg1_type = ARG_PTR_TO_CTX,
1420 .arg2_type = ARG_ANYTHING,
1421 .arg3_type = ARG_PTR_TO_STACK,
1422 .arg4_type = ARG_CONST_STACK_SIZE,
1423};
1424
Alexei Starovoitova1661512015-04-15 12:55:45 -07001425static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001426{
1427 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001428 int offset = (int) r2;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001429 __sum16 sum, *ptr;
1430
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001431 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1432 return -EINVAL;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001433 if (unlikely((u32) offset > 0xffff))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001434 return -EFAULT;
1435
Alexei Starovoitova1661512015-04-15 12:55:45 -07001436 if (unlikely(skb_cloned(skb) &&
Alexei Starovoitov34312052015-06-04 10:11:53 -07001437 !skb_clone_writable(skb, offset + sizeof(sum))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001438 return -EFAULT;
1439
1440 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1441 if (unlikely(!ptr))
1442 return -EFAULT;
1443
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001444 switch (flags & BPF_F_HDR_FIELD_MASK) {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001445 case 2:
1446 csum_replace2(ptr, from, to);
1447 break;
1448 case 4:
1449 csum_replace4(ptr, from, to);
1450 break;
1451 default:
1452 return -EINVAL;
1453 }
1454
1455 if (ptr == &sum)
1456 /* skb_store_bits guaranteed to not return -EFAULT here */
1457 skb_store_bits(skb, offset, ptr, sizeof(sum));
1458
1459 return 0;
1460}
1461
1462const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1463 .func = bpf_l3_csum_replace,
1464 .gpl_only = false,
1465 .ret_type = RET_INTEGER,
1466 .arg1_type = ARG_PTR_TO_CTX,
1467 .arg2_type = ARG_ANYTHING,
1468 .arg3_type = ARG_ANYTHING,
1469 .arg4_type = ARG_ANYTHING,
1470 .arg5_type = ARG_ANYTHING,
1471};
1472
Alexei Starovoitova1661512015-04-15 12:55:45 -07001473static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001474{
1475 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001476 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001477 int offset = (int) r2;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001478 __sum16 sum, *ptr;
1479
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001480 if (unlikely(flags & ~(BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1481 return -EINVAL;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001482 if (unlikely((u32) offset > 0xffff))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001483 return -EFAULT;
1484
Alexei Starovoitova1661512015-04-15 12:55:45 -07001485 if (unlikely(skb_cloned(skb) &&
Alexei Starovoitov34312052015-06-04 10:11:53 -07001486 !skb_clone_writable(skb, offset + sizeof(sum))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001487 return -EFAULT;
1488
1489 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1490 if (unlikely(!ptr))
1491 return -EFAULT;
1492
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001493 switch (flags & BPF_F_HDR_FIELD_MASK) {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001494 case 2:
1495 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1496 break;
1497 case 4:
1498 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1499 break;
1500 default:
1501 return -EINVAL;
1502 }
1503
1504 if (ptr == &sum)
1505 /* skb_store_bits guaranteed to not return -EFAULT here */
1506 skb_store_bits(skb, offset, ptr, sizeof(sum));
1507
1508 return 0;
1509}
1510
1511const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1512 .func = bpf_l4_csum_replace,
1513 .gpl_only = false,
1514 .ret_type = RET_INTEGER,
1515 .arg1_type = ARG_PTR_TO_CTX,
1516 .arg2_type = ARG_ANYTHING,
1517 .arg3_type = ARG_ANYTHING,
1518 .arg4_type = ARG_ANYTHING,
1519 .arg5_type = ARG_ANYTHING,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001520};
1521
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001522static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1523{
1524 struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2;
1525 struct net_device *dev;
1526
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001527 if (unlikely(flags & ~(BPF_F_INGRESS)))
1528 return -EINVAL;
1529
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001530 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1531 if (unlikely(!dev))
1532 return -EINVAL;
1533
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001534 skb2 = skb_clone(skb, GFP_ATOMIC);
1535 if (unlikely(!skb2))
1536 return -ENOMEM;
1537
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001538 if (flags & BPF_F_INGRESS) {
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001539 if (skb_at_tc_ingress(skb2))
1540 skb_postpush_rcsum(skb2, skb_mac_header(skb2),
1541 skb2->mac_len);
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001542 return dev_forward_skb(dev, skb2);
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001543 }
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001544
1545 skb2->dev = dev;
Alexei Starovoitov6bf05772015-10-06 20:46:07 -07001546 skb_sender_cpu_clear(skb2);
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001547 return dev_queue_xmit(skb2);
1548}
1549
1550const struct bpf_func_proto bpf_clone_redirect_proto = {
1551 .func = bpf_clone_redirect,
1552 .gpl_only = false,
1553 .ret_type = RET_INTEGER,
1554 .arg1_type = ARG_PTR_TO_CTX,
1555 .arg2_type = ARG_ANYTHING,
1556 .arg3_type = ARG_ANYTHING,
1557};
1558
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001559struct redirect_info {
1560 u32 ifindex;
1561 u32 flags;
1562};
1563
1564static DEFINE_PER_CPU(struct redirect_info, redirect_info);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001565
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001566static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5)
1567{
1568 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1569
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001570 if (unlikely(flags & ~(BPF_F_INGRESS)))
1571 return TC_ACT_SHOT;
1572
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001573 ri->ifindex = ifindex;
1574 ri->flags = flags;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001575
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001576 return TC_ACT_REDIRECT;
1577}
1578
1579int skb_do_redirect(struct sk_buff *skb)
1580{
1581 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1582 struct net_device *dev;
1583
1584 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1585 ri->ifindex = 0;
1586 if (unlikely(!dev)) {
1587 kfree_skb(skb);
1588 return -EINVAL;
1589 }
1590
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001591 if (ri->flags & BPF_F_INGRESS) {
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001592 if (skb_at_tc_ingress(skb))
1593 skb_postpush_rcsum(skb, skb_mac_header(skb),
1594 skb->mac_len);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001595 return dev_forward_skb(dev, skb);
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001596 }
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001597
1598 skb->dev = dev;
Daniel Borkmanncfc81b52015-10-07 10:16:09 +02001599 skb_sender_cpu_clear(skb);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001600 return dev_queue_xmit(skb);
1601}
1602
1603const struct bpf_func_proto bpf_redirect_proto = {
1604 .func = bpf_redirect,
1605 .gpl_only = false,
1606 .ret_type = RET_INTEGER,
1607 .arg1_type = ARG_ANYTHING,
1608 .arg2_type = ARG_ANYTHING,
1609};
1610
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001611static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1612{
1613 return task_get_classid((struct sk_buff *) (unsigned long) r1);
1614}
1615
1616static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1617 .func = bpf_get_cgroup_classid,
1618 .gpl_only = false,
1619 .ret_type = RET_INTEGER,
1620 .arg1_type = ARG_PTR_TO_CTX,
1621};
1622
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001623static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1624{
1625#ifdef CONFIG_IP_ROUTE_CLASSID
1626 const struct dst_entry *dst;
1627
1628 dst = skb_dst((struct sk_buff *) (unsigned long) r1);
1629 if (dst)
1630 return dst->tclassid;
1631#endif
1632 return 0;
1633}
1634
1635static const struct bpf_func_proto bpf_get_route_realm_proto = {
1636 .func = bpf_get_route_realm,
1637 .gpl_only = false,
1638 .ret_type = RET_INTEGER,
1639 .arg1_type = ARG_PTR_TO_CTX,
1640};
1641
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001642static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
1643{
1644 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1645 __be16 vlan_proto = (__force __be16) r2;
1646
1647 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1648 vlan_proto != htons(ETH_P_8021AD)))
1649 vlan_proto = htons(ETH_P_8021Q);
1650
1651 return skb_vlan_push(skb, vlan_proto, vlan_tci);
1652}
1653
1654const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1655 .func = bpf_skb_vlan_push,
1656 .gpl_only = false,
1657 .ret_type = RET_INTEGER,
1658 .arg1_type = ARG_PTR_TO_CTX,
1659 .arg2_type = ARG_ANYTHING,
1660 .arg3_type = ARG_ANYTHING,
1661};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001662EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001663
1664static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1665{
1666 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1667
1668 return skb_vlan_pop(skb);
1669}
1670
1671const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1672 .func = bpf_skb_vlan_pop,
1673 .gpl_only = false,
1674 .ret_type = RET_INTEGER,
1675 .arg1_type = ARG_PTR_TO_CTX,
1676};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001677EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001678
1679bool bpf_helper_changes_skb_data(void *func)
1680{
1681 if (func == bpf_skb_vlan_push)
1682 return true;
1683 if (func == bpf_skb_vlan_pop)
1684 return true;
1685 return false;
1686}
1687
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001688static unsigned short bpf_tunnel_key_af(u64 flags)
1689{
1690 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
1691}
1692
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001693static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1694{
1695 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1696 struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001697 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
1698 u8 compat[sizeof(struct bpf_tunnel_key)];
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001699
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001700 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6))))
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001701 return -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001702 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags))
1703 return -EPROTO;
1704 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
1705 switch (size) {
1706 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
1707 /* Fixup deprecated structure layouts here, so we have
1708 * a common path later on.
1709 */
1710 if (ip_tunnel_info_af(info) != AF_INET)
1711 return -EINVAL;
1712 to = (struct bpf_tunnel_key *)compat;
1713 break;
1714 default:
1715 return -EINVAL;
1716 }
1717 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001718
1719 to->tunnel_id = be64_to_cpu(info->key.tun_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001720 to->tunnel_tos = info->key.tos;
1721 to->tunnel_ttl = info->key.ttl;
1722
1723 if (flags & BPF_F_TUNINFO_IPV6)
1724 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
1725 sizeof(to->remote_ipv6));
1726 else
1727 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
1728
1729 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
1730 memcpy((void *)(long) r2, to, size);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001731
1732 return 0;
1733}
1734
1735const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
1736 .func = bpf_skb_get_tunnel_key,
1737 .gpl_only = false,
1738 .ret_type = RET_INTEGER,
1739 .arg1_type = ARG_PTR_TO_CTX,
1740 .arg2_type = ARG_PTR_TO_STACK,
1741 .arg3_type = ARG_CONST_STACK_SIZE,
1742 .arg4_type = ARG_ANYTHING,
1743};
1744
1745static struct metadata_dst __percpu *md_dst;
1746
1747static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1748{
1749 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1750 struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2;
1751 struct metadata_dst *md = this_cpu_ptr(md_dst);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001752 u8 compat[sizeof(struct bpf_tunnel_key)];
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001753 struct ip_tunnel_info *info;
1754
Daniel Borkmann2da897e2016-02-23 02:05:26 +01001755 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX)))
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001756 return -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001757 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
1758 switch (size) {
1759 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
1760 /* Fixup deprecated structure layouts here, so we have
1761 * a common path later on.
1762 */
1763 memcpy(compat, from, size);
1764 memset(compat + size, 0, sizeof(compat) - size);
1765 from = (struct bpf_tunnel_key *)compat;
1766 break;
1767 default:
1768 return -EINVAL;
1769 }
1770 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001771
1772 skb_dst_drop(skb);
1773 dst_hold((struct dst_entry *) md);
1774 skb_dst_set(skb, (struct dst_entry *) md);
1775
1776 info = &md->u.tun_info;
1777 info->mode = IP_TUNNEL_INFO_TX;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001778
Daniel Borkmann2da897e2016-02-23 02:05:26 +01001779 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001780 info->key.tun_id = cpu_to_be64(from->tunnel_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001781 info->key.tos = from->tunnel_tos;
1782 info->key.ttl = from->tunnel_ttl;
1783
1784 if (flags & BPF_F_TUNINFO_IPV6) {
1785 info->mode |= IP_TUNNEL_INFO_IPV6;
1786 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
1787 sizeof(from->remote_ipv6));
1788 } else {
1789 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
Daniel Borkmann2da897e2016-02-23 02:05:26 +01001790 if (flags & BPF_F_ZERO_CSUM_TX)
1791 info->key.tun_flags &= ~TUNNEL_CSUM;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001792 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001793
1794 return 0;
1795}
1796
1797const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
1798 .func = bpf_skb_set_tunnel_key,
1799 .gpl_only = false,
1800 .ret_type = RET_INTEGER,
1801 .arg1_type = ARG_PTR_TO_CTX,
1802 .arg2_type = ARG_PTR_TO_STACK,
1803 .arg3_type = ARG_CONST_STACK_SIZE,
1804 .arg4_type = ARG_ANYTHING,
1805};
1806
1807static const struct bpf_func_proto *bpf_get_skb_set_tunnel_key_proto(void)
1808{
1809 if (!md_dst) {
1810 /* race is not possible, since it's called from
1811 * verifier that is holding verifier mutex
1812 */
1813 md_dst = metadata_dst_alloc_percpu(0, GFP_KERNEL);
1814 if (!md_dst)
1815 return NULL;
1816 }
1817 return &bpf_skb_set_tunnel_key_proto;
1818}
1819
Daniel Borkmannd4052c42015-03-01 12:31:45 +01001820static const struct bpf_func_proto *
1821sk_filter_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001822{
1823 switch (func_id) {
1824 case BPF_FUNC_map_lookup_elem:
1825 return &bpf_map_lookup_elem_proto;
1826 case BPF_FUNC_map_update_elem:
1827 return &bpf_map_update_elem_proto;
1828 case BPF_FUNC_map_delete_elem:
1829 return &bpf_map_delete_elem_proto;
Daniel Borkmann03e69b52015-03-14 02:27:16 +01001830 case BPF_FUNC_get_prandom_u32:
1831 return &bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01001832 case BPF_FUNC_get_smp_processor_id:
1833 return &bpf_get_smp_processor_id_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07001834 case BPF_FUNC_tail_call:
1835 return &bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02001836 case BPF_FUNC_ktime_get_ns:
1837 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07001838 case BPF_FUNC_trace_printk:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001839 if (capable(CAP_SYS_ADMIN))
1840 return bpf_get_trace_printk_proto();
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001841 default:
1842 return NULL;
1843 }
1844}
1845
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001846static const struct bpf_func_proto *
1847tc_cls_act_func_proto(enum bpf_func_id func_id)
1848{
1849 switch (func_id) {
1850 case BPF_FUNC_skb_store_bytes:
1851 return &bpf_skb_store_bytes_proto;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001852 case BPF_FUNC_skb_load_bytes:
1853 return &bpf_skb_load_bytes_proto;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001854 case BPF_FUNC_l3_csum_replace:
1855 return &bpf_l3_csum_replace_proto;
1856 case BPF_FUNC_l4_csum_replace:
1857 return &bpf_l4_csum_replace_proto;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001858 case BPF_FUNC_clone_redirect:
1859 return &bpf_clone_redirect_proto;
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001860 case BPF_FUNC_get_cgroup_classid:
1861 return &bpf_get_cgroup_classid_proto;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001862 case BPF_FUNC_skb_vlan_push:
1863 return &bpf_skb_vlan_push_proto;
1864 case BPF_FUNC_skb_vlan_pop:
1865 return &bpf_skb_vlan_pop_proto;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001866 case BPF_FUNC_skb_get_tunnel_key:
1867 return &bpf_skb_get_tunnel_key_proto;
1868 case BPF_FUNC_skb_set_tunnel_key:
1869 return bpf_get_skb_set_tunnel_key_proto();
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001870 case BPF_FUNC_redirect:
1871 return &bpf_redirect_proto;
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001872 case BPF_FUNC_get_route_realm:
1873 return &bpf_get_route_realm_proto;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001874 default:
1875 return sk_filter_func_proto(func_id);
1876 }
1877}
1878
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001879static bool __is_valid_access(int off, int size, enum bpf_access_type type)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001880{
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001881 /* check bounds */
1882 if (off < 0 || off >= sizeof(struct __sk_buff))
1883 return false;
1884
1885 /* disallow misaligned access */
1886 if (off % size != 0)
1887 return false;
1888
1889 /* all __sk_buff fields are __u32 */
1890 if (size != 4)
1891 return false;
1892
1893 return true;
1894}
1895
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001896static bool sk_filter_is_valid_access(int off, int size,
1897 enum bpf_access_type type)
1898{
Daniel Borkmann045efa82015-09-15 23:05:42 -07001899 if (off == offsetof(struct __sk_buff, tc_classid))
1900 return false;
1901
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001902 if (type == BPF_WRITE) {
1903 switch (off) {
1904 case offsetof(struct __sk_buff, cb[0]) ...
1905 offsetof(struct __sk_buff, cb[4]):
1906 break;
1907 default:
1908 return false;
1909 }
1910 }
1911
1912 return __is_valid_access(off, size, type);
1913}
1914
1915static bool tc_cls_act_is_valid_access(int off, int size,
1916 enum bpf_access_type type)
1917{
Daniel Borkmann045efa82015-09-15 23:05:42 -07001918 if (off == offsetof(struct __sk_buff, tc_classid))
1919 return type == BPF_WRITE ? true : false;
1920
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001921 if (type == BPF_WRITE) {
1922 switch (off) {
1923 case offsetof(struct __sk_buff, mark):
1924 case offsetof(struct __sk_buff, tc_index):
Daniel Borkmann754f1e62015-09-30 01:41:52 +02001925 case offsetof(struct __sk_buff, priority):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07001926 case offsetof(struct __sk_buff, cb[0]) ...
1927 offsetof(struct __sk_buff, cb[4]):
1928 break;
1929 default:
1930 return false;
1931 }
1932 }
1933 return __is_valid_access(off, size, type);
1934}
1935
1936static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
1937 int src_reg, int ctx_off,
Alexei Starovoitovff936a02015-10-07 10:55:41 -07001938 struct bpf_insn *insn_buf,
1939 struct bpf_prog *prog)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07001940{
1941 struct bpf_insn *insn = insn_buf;
1942
1943 switch (ctx_off) {
1944 case offsetof(struct __sk_buff, len):
1945 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
1946
1947 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1948 offsetof(struct sk_buff, len));
1949 break;
1950
Daniel Borkmann0b8c7072015-03-19 19:38:27 +01001951 case offsetof(struct __sk_buff, protocol):
1952 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1953
1954 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1955 offsetof(struct sk_buff, protocol));
1956 break;
1957
Michal Sekletar27cd5452015-03-24 14:48:41 +01001958 case offsetof(struct __sk_buff, vlan_proto):
1959 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
1960
1961 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1962 offsetof(struct sk_buff, vlan_proto));
1963 break;
1964
Daniel Borkmannbcad5712015-04-03 20:52:24 +02001965 case offsetof(struct __sk_buff, priority):
1966 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
1967
Daniel Borkmann754f1e62015-09-30 01:41:52 +02001968 if (type == BPF_WRITE)
1969 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
1970 offsetof(struct sk_buff, priority));
1971 else
1972 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1973 offsetof(struct sk_buff, priority));
Daniel Borkmannbcad5712015-04-03 20:52:24 +02001974 break;
1975
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07001976 case offsetof(struct __sk_buff, ingress_ifindex):
1977 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
1978
1979 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1980 offsetof(struct sk_buff, skb_iif));
1981 break;
1982
1983 case offsetof(struct __sk_buff, ifindex):
1984 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
1985
1986 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
1987 dst_reg, src_reg,
1988 offsetof(struct sk_buff, dev));
1989 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
1990 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
1991 offsetof(struct net_device, ifindex));
1992 break;
1993
Daniel Borkmannba7591d2015-08-01 00:46:29 +02001994 case offsetof(struct __sk_buff, hash):
1995 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1996
1997 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1998 offsetof(struct sk_buff, hash));
1999 break;
2000
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002001 case offsetof(struct __sk_buff, mark):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002002 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2003
2004 if (type == BPF_WRITE)
2005 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2006 offsetof(struct sk_buff, mark));
2007 else
2008 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2009 offsetof(struct sk_buff, mark));
2010 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002011
2012 case offsetof(struct __sk_buff, pkt_type):
2013 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2014
2015 case offsetof(struct __sk_buff, queue_mapping):
2016 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002017
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002018 case offsetof(struct __sk_buff, vlan_present):
2019 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2020 dst_reg, src_reg, insn);
2021
2022 case offsetof(struct __sk_buff, vlan_tci):
2023 return convert_skb_access(SKF_AD_VLAN_TAG,
2024 dst_reg, src_reg, insn);
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002025
2026 case offsetof(struct __sk_buff, cb[0]) ...
2027 offsetof(struct __sk_buff, cb[4]):
2028 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2029
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002030 prog->cb_access = 1;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002031 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2032 ctx_off += offsetof(struct sk_buff, cb);
2033 ctx_off += offsetof(struct qdisc_skb_cb, data);
2034 if (type == BPF_WRITE)
2035 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2036 else
2037 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2038 break;
2039
Daniel Borkmann045efa82015-09-15 23:05:42 -07002040 case offsetof(struct __sk_buff, tc_classid):
2041 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2042 ctx_off += offsetof(struct sk_buff, cb);
2043 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
2044 WARN_ON(type != BPF_WRITE);
2045 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2046 break;
2047
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002048 case offsetof(struct __sk_buff, tc_index):
2049#ifdef CONFIG_NET_SCHED
2050 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2051
2052 if (type == BPF_WRITE)
2053 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2054 offsetof(struct sk_buff, tc_index));
2055 else
2056 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2057 offsetof(struct sk_buff, tc_index));
2058 break;
2059#else
2060 if (type == BPF_WRITE)
2061 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2062 else
2063 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2064 break;
2065#endif
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002066 }
2067
2068 return insn - insn_buf;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002069}
2070
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002071static const struct bpf_verifier_ops sk_filter_ops = {
2072 .get_func_proto = sk_filter_func_proto,
2073 .is_valid_access = sk_filter_is_valid_access,
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002074 .convert_ctx_access = bpf_net_convert_ctx_access,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002075};
2076
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002077static const struct bpf_verifier_ops tc_cls_act_ops = {
2078 .get_func_proto = tc_cls_act_func_proto,
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002079 .is_valid_access = tc_cls_act_is_valid_access,
2080 .convert_ctx_access = bpf_net_convert_ctx_access,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002081};
2082
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002083static struct bpf_prog_type_list sk_filter_type __read_mostly = {
2084 .ops = &sk_filter_ops,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002085 .type = BPF_PROG_TYPE_SOCKET_FILTER,
2086};
2087
Daniel Borkmann96be4322015-03-01 12:31:46 +01002088static struct bpf_prog_type_list sched_cls_type __read_mostly = {
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002089 .ops = &tc_cls_act_ops,
Daniel Borkmann96be4322015-03-01 12:31:46 +01002090 .type = BPF_PROG_TYPE_SCHED_CLS,
2091};
2092
Daniel Borkmann94caee82015-03-20 15:11:11 +01002093static struct bpf_prog_type_list sched_act_type __read_mostly = {
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002094 .ops = &tc_cls_act_ops,
Daniel Borkmann94caee82015-03-20 15:11:11 +01002095 .type = BPF_PROG_TYPE_SCHED_ACT,
2096};
2097
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002098static int __init register_sk_filter_ops(void)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002099{
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002100 bpf_register_prog_type(&sk_filter_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01002101 bpf_register_prog_type(&sched_cls_type);
Daniel Borkmann94caee82015-03-20 15:11:11 +01002102 bpf_register_prog_type(&sched_act_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01002103
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002104 return 0;
2105}
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002106late_initcall(register_sk_filter_ops);
2107
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002108int sk_detach_filter(struct sock *sk)
2109{
2110 int ret = -ENOENT;
2111 struct sk_filter *filter;
2112
Vincent Bernatd59577b2013-01-16 22:55:49 +01002113 if (sock_flag(sk, SOCK_FILTER_LOCKED))
2114 return -EPERM;
2115
Eric Dumazetf91ff5b2010-09-27 06:07:30 +00002116 filter = rcu_dereference_protected(sk->sk_filter,
2117 sock_owned_by_user(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002118 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002119 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -08002120 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002121 ret = 0;
2122 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002123
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002124 return ret;
2125}
Michael S. Tsirkin5ff3f072010-02-14 01:01:00 +00002126EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002127
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002128int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
2129 unsigned int len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002130{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002131 struct sock_fprog_kern *fprog;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002132 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002133 int ret = 0;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002134
2135 lock_sock(sk);
2136 filter = rcu_dereference_protected(sk->sk_filter,
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002137 sock_owned_by_user(sk));
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002138 if (!filter)
2139 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002140
2141 /* We're copying the filter that has been originally attached,
Daniel Borkmann93d08b62015-10-02 12:06:03 +02002142 * so no conversion/decode needed anymore. eBPF programs that
2143 * have no original program cannot be dumped through this.
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002144 */
Daniel Borkmann93d08b62015-10-02 12:06:03 +02002145 ret = -EACCES;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002146 fprog = filter->prog->orig_prog;
Daniel Borkmann93d08b62015-10-02 12:06:03 +02002147 if (!fprog)
2148 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002149
2150 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002151 if (!len)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002152 /* User space only enquires number of filter blocks. */
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002153 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002154
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002155 ret = -EINVAL;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002156 if (len < fprog->len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002157 goto out;
2158
2159 ret = -EFAULT;
Alexei Starovoitov009937e2014-07-30 20:34:13 -07002160 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002161 goto out;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002162
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002163 /* Instead of bytes, the API requests to return the number
2164 * of filter blocks.
2165 */
2166 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002167out:
2168 release_sock(sk);
2169 return ret;
2170}