blob: 68adb5f52110d85fead496a8a76a7248ae8cefae [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
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100533 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
534 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
535 */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100536 case BPF_RET | BPF_A:
537 case BPF_RET | BPF_K:
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100538 if (BPF_RVAL(fp->code) == BPF_K)
539 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
540 0, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700541 *insn = BPF_EXIT_INSN();
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100542 break;
543
544 /* Store to stack. */
545 case BPF_ST:
546 case BPF_STX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200547 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
548 BPF_ST ? BPF_REG_A : BPF_REG_X,
549 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100550 break;
551
552 /* Load from stack. */
553 case BPF_LD | BPF_MEM:
554 case BPF_LDX | BPF_MEM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200555 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
556 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
557 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100558 break;
559
560 /* A = K or X = K */
561 case BPF_LD | BPF_IMM:
562 case BPF_LDX | BPF_IMM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200563 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
564 BPF_REG_A : BPF_REG_X, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100565 break;
566
567 /* X = A */
568 case BPF_MISC | BPF_TAX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200569 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100570 break;
571
572 /* A = X */
573 case BPF_MISC | BPF_TXA:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200574 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100575 break;
576
577 /* A = skb->len or X = skb->len */
578 case BPF_LD | BPF_W | BPF_LEN:
579 case BPF_LDX | BPF_W | BPF_LEN:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200580 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
581 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
582 offsetof(struct sk_buff, len));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100583 break;
584
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200585 /* Access seccomp_data fields. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100586 case BPF_LDX | BPF_ABS | BPF_W:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700587 /* A = *(u32 *) (ctx + K) */
588 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100589 break;
590
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -0500591 /* Unknown instruction. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100592 default:
593 goto err;
594 }
595
596 insn++;
597 if (new_prog)
598 memcpy(new_insn, tmp_insns,
599 sizeof(*insn) * (insn - tmp_insns));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100600 new_insn += insn - tmp_insns;
601 }
602
603 if (!new_prog) {
604 /* Only calculating new length. */
605 *new_len = new_insn - new_prog;
606 return 0;
607 }
608
609 pass++;
610 if (new_flen != new_insn - new_prog) {
611 new_flen = new_insn - new_prog;
612 if (pass > 2)
613 goto err;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100614 goto do_pass;
615 }
616
617 kfree(addrs);
618 BUG_ON(*new_len != new_flen);
619 return 0;
620err:
621 kfree(addrs);
622 return -EINVAL;
623}
624
625/* Security:
626 *
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000627 * As we dont want to clear mem[] array for each packet going through
Li RongQing8ea6e342014-10-10 13:56:51 +0800628 * __bpf_prog_run(), we check that filter loaded by user never try to read
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000629 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300630 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000631 */
Eric Dumazetec31a052014-07-12 15:49:16 +0200632static int check_load_and_stores(const struct sock_filter *filter, int flen)
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000633{
Daniel Borkmann34805932014-05-29 10:22:50 +0200634 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000635 int pc, ret = 0;
636
637 BUILD_BUG_ON(BPF_MEMWORDS > 16);
Daniel Borkmann34805932014-05-29 10:22:50 +0200638
Tobias Klauser99e72a02014-06-24 15:33:22 +0200639 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000640 if (!masks)
641 return -ENOMEM;
Daniel Borkmann34805932014-05-29 10:22:50 +0200642
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000643 memset(masks, 0xff, flen * sizeof(*masks));
644
645 for (pc = 0; pc < flen; pc++) {
646 memvalid &= masks[pc];
647
648 switch (filter[pc].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200649 case BPF_ST:
650 case BPF_STX:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000651 memvalid |= (1 << filter[pc].k);
652 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200653 case BPF_LD | BPF_MEM:
654 case BPF_LDX | BPF_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000655 if (!(memvalid & (1 << filter[pc].k))) {
656 ret = -EINVAL;
657 goto error;
658 }
659 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200660 case BPF_JMP | BPF_JA:
661 /* A jump must set masks on target */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000662 masks[pc + 1 + filter[pc].k] &= memvalid;
663 memvalid = ~0;
664 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200665 case BPF_JMP | BPF_JEQ | BPF_K:
666 case BPF_JMP | BPF_JEQ | BPF_X:
667 case BPF_JMP | BPF_JGE | BPF_K:
668 case BPF_JMP | BPF_JGE | BPF_X:
669 case BPF_JMP | BPF_JGT | BPF_K:
670 case BPF_JMP | BPF_JGT | BPF_X:
671 case BPF_JMP | BPF_JSET | BPF_K:
672 case BPF_JMP | BPF_JSET | BPF_X:
673 /* A jump must set masks on targets */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000674 masks[pc + 1 + filter[pc].jt] &= memvalid;
675 masks[pc + 1 + filter[pc].jf] &= memvalid;
676 memvalid = ~0;
677 break;
678 }
679 }
680error:
681 kfree(masks);
682 return ret;
683}
684
Daniel Borkmann34805932014-05-29 10:22:50 +0200685static bool chk_code_allowed(u16 code_to_probe)
686{
687 static const bool codes[] = {
688 /* 32 bit ALU operations */
689 [BPF_ALU | BPF_ADD | BPF_K] = true,
690 [BPF_ALU | BPF_ADD | BPF_X] = true,
691 [BPF_ALU | BPF_SUB | BPF_K] = true,
692 [BPF_ALU | BPF_SUB | BPF_X] = true,
693 [BPF_ALU | BPF_MUL | BPF_K] = true,
694 [BPF_ALU | BPF_MUL | BPF_X] = true,
695 [BPF_ALU | BPF_DIV | BPF_K] = true,
696 [BPF_ALU | BPF_DIV | BPF_X] = true,
697 [BPF_ALU | BPF_MOD | BPF_K] = true,
698 [BPF_ALU | BPF_MOD | BPF_X] = true,
699 [BPF_ALU | BPF_AND | BPF_K] = true,
700 [BPF_ALU | BPF_AND | BPF_X] = true,
701 [BPF_ALU | BPF_OR | BPF_K] = true,
702 [BPF_ALU | BPF_OR | BPF_X] = true,
703 [BPF_ALU | BPF_XOR | BPF_K] = true,
704 [BPF_ALU | BPF_XOR | BPF_X] = true,
705 [BPF_ALU | BPF_LSH | BPF_K] = true,
706 [BPF_ALU | BPF_LSH | BPF_X] = true,
707 [BPF_ALU | BPF_RSH | BPF_K] = true,
708 [BPF_ALU | BPF_RSH | BPF_X] = true,
709 [BPF_ALU | BPF_NEG] = true,
710 /* Load instructions */
711 [BPF_LD | BPF_W | BPF_ABS] = true,
712 [BPF_LD | BPF_H | BPF_ABS] = true,
713 [BPF_LD | BPF_B | BPF_ABS] = true,
714 [BPF_LD | BPF_W | BPF_LEN] = true,
715 [BPF_LD | BPF_W | BPF_IND] = true,
716 [BPF_LD | BPF_H | BPF_IND] = true,
717 [BPF_LD | BPF_B | BPF_IND] = true,
718 [BPF_LD | BPF_IMM] = true,
719 [BPF_LD | BPF_MEM] = true,
720 [BPF_LDX | BPF_W | BPF_LEN] = true,
721 [BPF_LDX | BPF_B | BPF_MSH] = true,
722 [BPF_LDX | BPF_IMM] = true,
723 [BPF_LDX | BPF_MEM] = true,
724 /* Store instructions */
725 [BPF_ST] = true,
726 [BPF_STX] = true,
727 /* Misc instructions */
728 [BPF_MISC | BPF_TAX] = true,
729 [BPF_MISC | BPF_TXA] = true,
730 /* Return instructions */
731 [BPF_RET | BPF_K] = true,
732 [BPF_RET | BPF_A] = true,
733 /* Jump instructions */
734 [BPF_JMP | BPF_JA] = true,
735 [BPF_JMP | BPF_JEQ | BPF_K] = true,
736 [BPF_JMP | BPF_JEQ | BPF_X] = true,
737 [BPF_JMP | BPF_JGE | BPF_K] = true,
738 [BPF_JMP | BPF_JGE | BPF_X] = true,
739 [BPF_JMP | BPF_JGT | BPF_K] = true,
740 [BPF_JMP | BPF_JGT | BPF_X] = true,
741 [BPF_JMP | BPF_JSET | BPF_K] = true,
742 [BPF_JMP | BPF_JSET | BPF_X] = true,
743 };
744
745 if (code_to_probe >= ARRAY_SIZE(codes))
746 return false;
747
748 return codes[code_to_probe];
749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/**
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -0700752 * bpf_check_classic - verify socket filter code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * @filter: filter to verify
754 * @flen: length of filter
755 *
756 * Check the user's filter code. If we let some ugly
757 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800758 * no references or jumps that are out of range, no illegal
759 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800761 * All jumps are forward as they are not signed.
762 *
763 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200765static int bpf_check_classic(const struct sock_filter *filter,
766 unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000768 bool anc_found;
Daniel Borkmann34805932014-05-29 10:22:50 +0200769 int pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
David S. Miller1b93ae642005-12-27 13:57:59 -0800771 if (flen == 0 || flen > BPF_MAXINSNS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -EINVAL;
773
Daniel Borkmann34805932014-05-29 10:22:50 +0200774 /* Check the filter code now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 for (pc = 0; pc < flen; pc++) {
Eric Dumazetec31a052014-07-12 15:49:16 +0200776 const struct sock_filter *ftest = &filter[pc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Daniel Borkmann34805932014-05-29 10:22:50 +0200778 /* May we actually operate on this code? */
779 if (!chk_code_allowed(ftest->code))
Tetsuo Handacba328f2010-11-16 15:19:51 +0000780 return -EINVAL;
Daniel Borkmann34805932014-05-29 10:22:50 +0200781
Kris Katterjohn93699862006-01-04 13:58:36 -0800782 /* Some instructions need special checks */
Daniel Borkmann34805932014-05-29 10:22:50 +0200783 switch (ftest->code) {
784 case BPF_ALU | BPF_DIV | BPF_K:
785 case BPF_ALU | BPF_MOD | BPF_K:
786 /* Check for division by zero */
Eric Dumazetb6069a92012-09-07 22:03:35 +0000787 if (ftest->k == 0)
788 return -EINVAL;
789 break;
Rabin Vincent229394e2016-01-12 20:17:08 +0100790 case BPF_ALU | BPF_LSH | BPF_K:
791 case BPF_ALU | BPF_RSH | BPF_K:
792 if (ftest->k >= 32)
793 return -EINVAL;
794 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200795 case BPF_LD | BPF_MEM:
796 case BPF_LDX | BPF_MEM:
797 case BPF_ST:
798 case BPF_STX:
799 /* Check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800800 if (ftest->k >= BPF_MEMWORDS)
801 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000802 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200803 case BPF_JMP | BPF_JA:
804 /* Note, the large ftest->k might cause loops.
Kris Katterjohn93699862006-01-04 13:58:36 -0800805 * Compare this with conditional jumps below,
806 * where offsets are limited. --ANK (981016)
807 */
Daniel Borkmann34805932014-05-29 10:22:50 +0200808 if (ftest->k >= (unsigned int)(flen - pc - 1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800809 return -EINVAL;
810 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200811 case BPF_JMP | BPF_JEQ | BPF_K:
812 case BPF_JMP | BPF_JEQ | BPF_X:
813 case BPF_JMP | BPF_JGE | BPF_K:
814 case BPF_JMP | BPF_JGE | BPF_X:
815 case BPF_JMP | BPF_JGT | BPF_K:
816 case BPF_JMP | BPF_JGT | BPF_X:
817 case BPF_JMP | BPF_JSET | BPF_K:
818 case BPF_JMP | BPF_JSET | BPF_X:
819 /* Both conditionals must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000820 if (pc + ftest->jt + 1 >= flen ||
821 pc + ftest->jf + 1 >= flen)
822 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000823 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200824 case BPF_LD | BPF_W | BPF_ABS:
825 case BPF_LD | BPF_H | BPF_ABS:
826 case BPF_LD | BPF_B | BPF_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000827 anc_found = false;
Daniel Borkmann34805932014-05-29 10:22:50 +0200828 if (bpf_anc_helper(ftest) & BPF_ANC)
829 anc_found = true;
830 /* Ancillary operation unknown or unsupported */
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000831 if (anc_found == false && ftest->k >= SKF_AD_OFF)
832 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Daniel Borkmann34805932014-05-29 10:22:50 +0200836 /* Last instruction must be a RET code */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000837 switch (filter[flen - 1].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200838 case BPF_RET | BPF_K:
839 case BPF_RET | BPF_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000840 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000841 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200842
Tetsuo Handacba328f2010-11-16 15:19:51 +0000843 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700846static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
847 const struct sock_fprog *fprog)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100848{
Alexei Starovoitov009937e2014-07-30 20:34:13 -0700849 unsigned int fsize = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100850 struct sock_fprog_kern *fkprog;
851
852 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
853 if (!fp->orig_prog)
854 return -ENOMEM;
855
856 fkprog = fp->orig_prog;
857 fkprog->len = fprog->len;
Daniel Borkmann658da932015-05-06 16:12:29 +0200858
859 fkprog->filter = kmemdup(fp->insns, fsize,
860 GFP_KERNEL | __GFP_NOWARN);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100861 if (!fkprog->filter) {
862 kfree(fp->orig_prog);
863 return -ENOMEM;
864 }
865
866 return 0;
867}
868
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700869static void bpf_release_orig_filter(struct bpf_prog *fp)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100870{
871 struct sock_fprog_kern *fprog = fp->orig_prog;
872
873 if (fprog) {
874 kfree(fprog->filter);
875 kfree(fprog);
876 }
877}
878
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700879static void __bpf_prog_release(struct bpf_prog *prog)
880{
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100881 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800882 bpf_prog_put(prog);
883 } else {
884 bpf_release_orig_filter(prog);
885 bpf_prog_free(prog);
886 }
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700887}
888
Pablo Neira34c5bd62014-07-29 17:36:28 +0200889static void __sk_filter_release(struct sk_filter *fp)
890{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700891 __bpf_prog_release(fp->prog);
892 kfree(fp);
Pablo Neira34c5bd62014-07-29 17:36:28 +0200893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800896 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700897 * @rcu: rcu_head that contains the sk_filter to free
898 */
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100899static void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700900{
901 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
902
Pablo Neira34c5bd62014-07-29 17:36:28 +0200903 __sk_filter_release(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700904}
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100905
906/**
907 * sk_filter_release - release a socket filter
908 * @fp: filter to remove
909 *
910 * Remove a filter from a socket and release its resources.
911 */
912static void sk_filter_release(struct sk_filter *fp)
913{
914 if (atomic_dec_and_test(&fp->refcnt))
915 call_rcu(&fp->rcu, sk_filter_release_rcu);
916}
917
918void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
919{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700920 u32 filter_size = bpf_prog_size(fp->prog->len);
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700921
922 atomic_sub(filter_size, &sk->sk_omem_alloc);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100923 sk_filter_release(fp);
924}
925
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700926/* try to charge the socket memory if there is space available
927 * return true on success
928 */
929bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100930{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700931 u32 filter_size = bpf_prog_size(fp->prog->len);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700932
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700933 /* same check as in sock_kmalloc() */
934 if (filter_size <= sysctl_optmem_max &&
935 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
936 atomic_inc(&fp->refcnt);
937 atomic_add(filter_size, &sk->sk_omem_alloc);
938 return true;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100939 }
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700940 return false;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100941}
942
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700943static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100944{
945 struct sock_filter *old_prog;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700946 struct bpf_prog *old_fp;
Daniel Borkmann34805932014-05-29 10:22:50 +0200947 int err, new_len, old_len = fp->len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100948
949 /* We are free to overwrite insns et al right here as it
950 * won't be used at this point in time anymore internally
951 * after the migration to the internal BPF instruction
952 * representation.
953 */
954 BUILD_BUG_ON(sizeof(struct sock_filter) !=
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700955 sizeof(struct bpf_insn));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100956
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100957 /* Conversion cannot happen on overlapping memory areas,
958 * so we need to keep the user BPF around until the 2nd
959 * pass. At this time, the user BPF is stored in fp->insns.
960 */
961 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
Daniel Borkmann658da932015-05-06 16:12:29 +0200962 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100963 if (!old_prog) {
964 err = -ENOMEM;
965 goto out_err;
966 }
967
968 /* 1st pass: calculate the new program length. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700969 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100970 if (err)
971 goto out_err_free;
972
973 /* Expand fp for appending the new filter representation. */
974 old_fp = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200975 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100976 if (!fp) {
977 /* The old_fp is still around in case we couldn't
978 * allocate new memory, so uncharge on that one.
979 */
980 fp = old_fp;
981 err = -ENOMEM;
982 goto out_err_free;
983 }
984
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100985 fp->len = new_len;
986
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700987 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700988 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100989 if (err)
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700990 /* 2nd bpf_convert_filter() can fail only if it fails
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100991 * to allocate memory, remapping must succeed. Note,
992 * that at this time old_fp has already been released
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700993 * by krealloc().
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100994 */
995 goto out_err_free;
996
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +0200997 /* We are guaranteed to never error here with cBPF to eBPF
998 * transitions, since there's no issue with type compatibility
999 * checks on program arrays.
1000 */
1001 fp = bpf_prog_select_runtime(fp, &err);
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001002
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001003 kfree(old_prog);
1004 return fp;
1005
1006out_err_free:
1007 kfree(old_prog);
1008out_err:
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001009 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001010 return ERR_PTR(err);
1011}
1012
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001013static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1014 bpf_aux_classic_check_t trans)
Jiri Pirko302d6632012-03-31 11:01:19 +00001015{
1016 int err;
1017
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001018 fp->bpf_func = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001019 fp->jited = 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001020
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -07001021 err = bpf_check_classic(fp->insns, fp->len);
Leon Yu418c96a2014-06-01 05:37:25 +00001022 if (err) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001023 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001024 return ERR_PTR(err);
Leon Yu418c96a2014-06-01 05:37:25 +00001025 }
Jiri Pirko302d6632012-03-31 11:01:19 +00001026
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001027 /* There might be additional checks and transformations
1028 * needed on classic filters, f.e. in case of seccomp.
1029 */
1030 if (trans) {
1031 err = trans(fp->insns, fp->len);
1032 if (err) {
1033 __bpf_prog_release(fp);
1034 return ERR_PTR(err);
1035 }
1036 }
1037
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001038 /* Probe if we can JIT compile the filter and if so, do
1039 * the compilation of the filter.
1040 */
Jiri Pirko302d6632012-03-31 11:01:19 +00001041 bpf_jit_compile(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001042
1043 /* JIT compiler couldn't process this filter, so do the
1044 * internal BPF translation for the optimized interpreter.
1045 */
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001046 if (!fp->jited)
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001047 fp = bpf_migrate_filter(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001048
1049 return fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001050}
1051
1052/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001053 * bpf_prog_create - create an unattached filter
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001054 * @pfp: the unattached filter that is created
Tobias Klauser677a9fd2014-06-24 15:33:21 +02001055 * @fprog: the filter program
Jiri Pirko302d6632012-03-31 11:01:19 +00001056 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001057 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +00001058 * sanity checks on it to make sure it does not explode on us later.
1059 * If an error occurs or there is insufficient memory for the filter
1060 * a negative errno code is returned. On success the return is zero.
1061 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001062int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
Jiri Pirko302d6632012-03-31 11:01:19 +00001063{
Alexei Starovoitov009937e2014-07-30 20:34:13 -07001064 unsigned int fsize = bpf_classic_proglen(fprog);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001065 struct bpf_prog *fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001066
1067 /* Make sure new filter is there and in the right amounts. */
1068 if (fprog->filter == NULL)
1069 return -EINVAL;
1070
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001071 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Jiri Pirko302d6632012-03-31 11:01:19 +00001072 if (!fp)
1073 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001074
Jiri Pirko302d6632012-03-31 11:01:19 +00001075 memcpy(fp->insns, fprog->filter, fsize);
1076
Jiri Pirko302d6632012-03-31 11:01:19 +00001077 fp->len = fprog->len;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001078 /* Since unattached filters are not copied back to user
1079 * space through sk_get_filter(), we do not need to hold
1080 * a copy here, and can spare us the work.
1081 */
1082 fp->orig_prog = NULL;
Jiri Pirko302d6632012-03-31 11:01:19 +00001083
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001084 /* bpf_prepare_filter() already takes care of freeing
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001085 * memory in case something goes wrong.
1086 */
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001087 fp = bpf_prepare_filter(fp, NULL);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001088 if (IS_ERR(fp))
1089 return PTR_ERR(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001090
1091 *pfp = fp;
1092 return 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001093}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001094EXPORT_SYMBOL_GPL(bpf_prog_create);
Jiri Pirko302d6632012-03-31 11:01:19 +00001095
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001096/**
1097 * bpf_prog_create_from_user - create an unattached filter from user buffer
1098 * @pfp: the unattached filter that is created
1099 * @fprog: the filter program
1100 * @trans: post-classic verifier transformation handler
Daniel Borkmannbab18992015-10-02 15:17:33 +02001101 * @save_orig: save classic BPF program
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001102 *
1103 * This function effectively does the same as bpf_prog_create(), only
1104 * that it builds up its insns buffer from user space provided buffer.
1105 * It also allows for passing a bpf_aux_classic_check_t handler.
1106 */
1107int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
Daniel Borkmannbab18992015-10-02 15:17:33 +02001108 bpf_aux_classic_check_t trans, bool save_orig)
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001109{
1110 unsigned int fsize = bpf_classic_proglen(fprog);
1111 struct bpf_prog *fp;
Daniel Borkmannbab18992015-10-02 15:17:33 +02001112 int err;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001113
1114 /* Make sure new filter is there and in the right amounts. */
1115 if (fprog->filter == NULL)
1116 return -EINVAL;
1117
1118 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1119 if (!fp)
1120 return -ENOMEM;
1121
1122 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1123 __bpf_prog_free(fp);
1124 return -EFAULT;
1125 }
1126
1127 fp->len = fprog->len;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001128 fp->orig_prog = NULL;
1129
Daniel Borkmannbab18992015-10-02 15:17:33 +02001130 if (save_orig) {
1131 err = bpf_prog_store_orig_filter(fp, fprog);
1132 if (err) {
1133 __bpf_prog_free(fp);
1134 return -ENOMEM;
1135 }
1136 }
1137
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001138 /* bpf_prepare_filter() already takes care of freeing
1139 * memory in case something goes wrong.
1140 */
1141 fp = bpf_prepare_filter(fp, trans);
1142 if (IS_ERR(fp))
1143 return PTR_ERR(fp);
1144
1145 *pfp = fp;
1146 return 0;
1147}
David S. Miller2ea273d2015-08-17 14:37:06 -07001148EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001149
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001150void bpf_prog_destroy(struct bpf_prog *fp)
Jiri Pirko302d6632012-03-31 11:01:19 +00001151{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001152 __bpf_prog_release(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001153}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001154EXPORT_SYMBOL_GPL(bpf_prog_destroy);
Jiri Pirko302d6632012-03-31 11:01:19 +00001155
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001156static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001157{
1158 struct sk_filter *fp, *old_fp;
1159
1160 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1161 if (!fp)
1162 return -ENOMEM;
1163
1164 fp->prog = prog;
1165 atomic_set(&fp->refcnt, 0);
1166
1167 if (!sk_filter_charge(sk, fp)) {
1168 kfree(fp);
1169 return -ENOMEM;
1170 }
1171
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001172 old_fp = rcu_dereference_protected(sk->sk_filter,
1173 lockdep_sock_is_held(sk));
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001174 rcu_assign_pointer(sk->sk_filter, fp);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001175
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001176 if (old_fp)
1177 sk_filter_uncharge(sk, old_fp);
1178
1179 return 0;
1180}
1181
Craig Gallek538950a2016-01-04 17:41:47 -05001182static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1183{
1184 struct bpf_prog *old_prog;
1185 int err;
1186
1187 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1188 return -ENOMEM;
1189
Craig Gallekfa463492016-02-10 11:50:39 -05001190 if (sk_unhashed(sk) && sk->sk_reuseport) {
Craig Gallek538950a2016-01-04 17:41:47 -05001191 err = reuseport_alloc(sk);
1192 if (err)
1193 return err;
1194 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1195 /* The socket wasn't bound with SO_REUSEPORT */
1196 return -EINVAL;
1197 }
1198
1199 old_prog = reuseport_attach_prog(sk, prog);
1200 if (old_prog)
1201 bpf_prog_destroy(old_prog);
1202
1203 return 0;
1204}
1205
1206static
1207struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1208{
1209 unsigned int fsize = bpf_classic_proglen(fprog);
1210 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1211 struct bpf_prog *prog;
1212 int err;
1213
1214 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1215 return ERR_PTR(-EPERM);
1216
1217 /* Make sure new filter is there and in the right amounts. */
1218 if (fprog->filter == NULL)
1219 return ERR_PTR(-EINVAL);
1220
1221 prog = bpf_prog_alloc(bpf_fsize, 0);
1222 if (!prog)
1223 return ERR_PTR(-ENOMEM);
1224
1225 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1226 __bpf_prog_free(prog);
1227 return ERR_PTR(-EFAULT);
1228 }
1229
1230 prog->len = fprog->len;
1231
1232 err = bpf_prog_store_orig_filter(prog, fprog);
1233 if (err) {
1234 __bpf_prog_free(prog);
1235 return ERR_PTR(-ENOMEM);
1236 }
1237
1238 /* bpf_prepare_filter() already takes care of freeing
1239 * memory in case something goes wrong.
1240 */
1241 return bpf_prepare_filter(prog, NULL);
1242}
1243
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001244/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 * sk_attach_filter - attach a socket filter
1246 * @fprog: the filter program
1247 * @sk: the socket to use
1248 *
1249 * Attach the user's filter code. We first run some sanity checks on
1250 * it to make sure it does not explode on us later. If an error
1251 * occurs or there is insufficient memory for the filter a negative
1252 * errno code is returned. On success the return is zero.
1253 */
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001254int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Craig Gallek538950a2016-01-04 17:41:47 -05001256 struct bpf_prog *prog = __get_filter(fprog, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 int err;
1258
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001259 if (IS_ERR(prog))
1260 return PTR_ERR(prog);
1261
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001262 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001263 if (err < 0) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001264 __bpf_prog_release(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001265 return err;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001266 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001267
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001270EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Craig Gallek538950a2016-01-04 17:41:47 -05001272int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001273{
Craig Gallek538950a2016-01-04 17:41:47 -05001274 struct bpf_prog *prog = __get_filter(fprog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001275 int err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001276
Alexei Starovoitov198bf1b2014-12-10 20:14:55 -08001277 if (IS_ERR(prog))
1278 return PTR_ERR(prog);
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001279
Craig Gallek538950a2016-01-04 17:41:47 -05001280 err = __reuseport_attach_prog(prog, sk);
1281 if (err < 0) {
1282 __bpf_prog_release(prog);
1283 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001284 }
1285
Craig Gallek538950a2016-01-04 17:41:47 -05001286 return 0;
1287}
1288
1289static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1290{
1291 struct bpf_prog *prog;
1292
1293 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1294 return ERR_PTR(-EPERM);
1295
1296 prog = bpf_prog_get(ufd);
1297 if (IS_ERR(prog))
1298 return prog;
1299
1300 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1301 bpf_prog_put(prog);
1302 return ERR_PTR(-EINVAL);
1303 }
1304
1305 return prog;
1306}
1307
1308int sk_attach_bpf(u32 ufd, struct sock *sk)
1309{
1310 struct bpf_prog *prog = __get_bpf(ufd, sk);
1311 int err;
1312
1313 if (IS_ERR(prog))
1314 return PTR_ERR(prog);
1315
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001316 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001317 if (err < 0) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001318 bpf_prog_put(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001319 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001320 }
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001321
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001322 return 0;
1323}
1324
Craig Gallek538950a2016-01-04 17:41:47 -05001325int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1326{
1327 struct bpf_prog *prog = __get_bpf(ufd, sk);
1328 int err;
1329
1330 if (IS_ERR(prog))
1331 return PTR_ERR(prog);
1332
1333 err = __reuseport_attach_prog(prog, sk);
1334 if (err < 0) {
1335 bpf_prog_put(prog);
1336 return err;
1337 }
1338
1339 return 0;
1340}
1341
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001342struct bpf_scratchpad {
1343 union {
1344 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1345 u8 buff[MAX_BPF_STACK];
1346 };
1347};
1348
1349static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001350
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001351static inline int bpf_try_make_writable(struct sk_buff *skb,
1352 unsigned int write_len)
1353{
1354 int err;
1355
1356 if (!skb_cloned(skb))
1357 return 0;
1358 if (skb_clone_writable(skb, write_len))
1359 return 0;
1360 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1361 if (!err)
1362 bpf_compute_data_end(skb);
1363 return err;
1364}
1365
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001366static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001367{
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001368 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001369 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001370 int offset = (int) r2;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001371 void *from = (void *) (long) r3;
1372 unsigned int len = (unsigned int) r4;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001373 void *ptr;
1374
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001375 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001376 return -EINVAL;
1377
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001378 /* bpf verifier guarantees that:
1379 * 'from' pointer points to bpf program stack
1380 * 'len' bytes of it were initialized
1381 * 'len' > 0
1382 * 'skb' is a valid pointer to 'struct sk_buff'
1383 *
1384 * so check for invalid 'offset' and too large 'len'
1385 */
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001386 if (unlikely((u32) offset > 0xffff || len > sizeof(sp->buff)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001387 return -EFAULT;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001388 if (unlikely(bpf_try_make_writable(skb, offset + len)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001389 return -EFAULT;
1390
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001391 ptr = skb_header_pointer(skb, offset, len, sp->buff);
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001392 if (unlikely(!ptr))
1393 return -EFAULT;
1394
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001395 if (flags & BPF_F_RECOMPUTE_CSUM)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001396 skb_postpull_rcsum(skb, ptr, len);
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001397
1398 memcpy(ptr, from, len);
1399
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001400 if (ptr == sp->buff)
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001401 /* skb_store_bits cannot return -EFAULT here */
1402 skb_store_bits(skb, offset, ptr, len);
1403
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001404 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001405 skb_postpush_rcsum(skb, ptr, len);
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001406 if (flags & BPF_F_INVALIDATE_HASH)
1407 skb_clear_hash(skb);
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001408
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001409 return 0;
1410}
1411
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001412static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001413 .func = bpf_skb_store_bytes,
1414 .gpl_only = false,
1415 .ret_type = RET_INTEGER,
1416 .arg1_type = ARG_PTR_TO_CTX,
1417 .arg2_type = ARG_ANYTHING,
1418 .arg3_type = ARG_PTR_TO_STACK,
1419 .arg4_type = ARG_CONST_STACK_SIZE,
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001420 .arg5_type = ARG_ANYTHING,
1421};
1422
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001423static u64 bpf_skb_load_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1424{
1425 const struct sk_buff *skb = (const struct sk_buff *)(unsigned long) r1;
1426 int offset = (int) r2;
1427 void *to = (void *)(unsigned long) r3;
1428 unsigned int len = (unsigned int) r4;
1429 void *ptr;
1430
Daniel Borkmann074f5282016-04-13 00:10:52 +02001431 if (unlikely((u32) offset > 0xffff))
1432 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001433
1434 ptr = skb_header_pointer(skb, offset, len, to);
1435 if (unlikely(!ptr))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001436 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001437 if (ptr != to)
1438 memcpy(to, ptr, len);
1439
1440 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02001441err_clear:
1442 memset(to, 0, len);
1443 return -EFAULT;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001444}
1445
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001446static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001447 .func = bpf_skb_load_bytes,
1448 .gpl_only = false,
1449 .ret_type = RET_INTEGER,
1450 .arg1_type = ARG_PTR_TO_CTX,
1451 .arg2_type = ARG_ANYTHING,
Daniel Borkmann074f5282016-04-13 00:10:52 +02001452 .arg3_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001453 .arg4_type = ARG_CONST_STACK_SIZE,
1454};
1455
Alexei Starovoitova1661512015-04-15 12:55:45 -07001456static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001457{
1458 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001459 int offset = (int) r2;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001460 __sum16 sum, *ptr;
1461
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001462 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1463 return -EINVAL;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001464 if (unlikely((u32) offset > 0xffff))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001465 return -EFAULT;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001466 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(sum))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001467 return -EFAULT;
1468
1469 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1470 if (unlikely(!ptr))
1471 return -EFAULT;
1472
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001473 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann8050c0f2016-03-04 15:15:02 +01001474 case 0:
1475 if (unlikely(from != 0))
1476 return -EINVAL;
1477
1478 csum_replace_by_diff(ptr, to);
1479 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001480 case 2:
1481 csum_replace2(ptr, from, to);
1482 break;
1483 case 4:
1484 csum_replace4(ptr, from, to);
1485 break;
1486 default:
1487 return -EINVAL;
1488 }
1489
1490 if (ptr == &sum)
1491 /* skb_store_bits guaranteed to not return -EFAULT here */
1492 skb_store_bits(skb, offset, ptr, sizeof(sum));
1493
1494 return 0;
1495}
1496
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001497static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001498 .func = bpf_l3_csum_replace,
1499 .gpl_only = false,
1500 .ret_type = RET_INTEGER,
1501 .arg1_type = ARG_PTR_TO_CTX,
1502 .arg2_type = ARG_ANYTHING,
1503 .arg3_type = ARG_ANYTHING,
1504 .arg4_type = ARG_ANYTHING,
1505 .arg5_type = ARG_ANYTHING,
1506};
1507
Alexei Starovoitova1661512015-04-15 12:55:45 -07001508static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001509{
1510 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001511 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
Daniel Borkmann2f729592016-02-19 23:05:26 +01001512 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001513 int offset = (int) r2;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001514 __sum16 sum, *ptr;
1515
Daniel Borkmann2f729592016-02-19 23:05:26 +01001516 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR |
1517 BPF_F_HDR_FIELD_MASK)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001518 return -EINVAL;
Alexei Starovoitova1661512015-04-15 12:55:45 -07001519 if (unlikely((u32) offset > 0xffff))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001520 return -EFAULT;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001521 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(sum))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001522 return -EFAULT;
1523
1524 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1525 if (unlikely(!ptr))
1526 return -EFAULT;
Daniel Borkmann2f729592016-02-19 23:05:26 +01001527 if (is_mmzero && !*ptr)
1528 return 0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001529
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001530 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001531 case 0:
1532 if (unlikely(from != 0))
1533 return -EINVAL;
1534
1535 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1536 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001537 case 2:
1538 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1539 break;
1540 case 4:
1541 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1542 break;
1543 default:
1544 return -EINVAL;
1545 }
1546
Daniel Borkmann2f729592016-02-19 23:05:26 +01001547 if (is_mmzero && !*ptr)
1548 *ptr = CSUM_MANGLED_0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001549 if (ptr == &sum)
1550 /* skb_store_bits guaranteed to not return -EFAULT here */
1551 skb_store_bits(skb, offset, ptr, sizeof(sum));
1552
1553 return 0;
1554}
1555
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001556static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001557 .func = bpf_l4_csum_replace,
1558 .gpl_only = false,
1559 .ret_type = RET_INTEGER,
1560 .arg1_type = ARG_PTR_TO_CTX,
1561 .arg2_type = ARG_ANYTHING,
1562 .arg3_type = ARG_ANYTHING,
1563 .arg4_type = ARG_ANYTHING,
1564 .arg5_type = ARG_ANYTHING,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001565};
1566
Daniel Borkmann7d672342016-02-19 23:05:23 +01001567static u64 bpf_csum_diff(u64 r1, u64 from_size, u64 r3, u64 to_size, u64 seed)
1568{
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001569 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
Daniel Borkmann7d672342016-02-19 23:05:23 +01001570 u64 diff_size = from_size + to_size;
1571 __be32 *from = (__be32 *) (long) r1;
1572 __be32 *to = (__be32 *) (long) r3;
1573 int i, j = 0;
1574
1575 /* This is quite flexible, some examples:
1576 *
1577 * from_size == 0, to_size > 0, seed := csum --> pushing data
1578 * from_size > 0, to_size == 0, seed := csum --> pulling data
1579 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1580 *
1581 * Even for diffing, from_size and to_size don't need to be equal.
1582 */
1583 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1584 diff_size > sizeof(sp->diff)))
1585 return -EINVAL;
1586
1587 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1588 sp->diff[j] = ~from[i];
1589 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1590 sp->diff[j] = to[i];
1591
1592 return csum_partial(sp->diff, diff_size, seed);
1593}
1594
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001595static const struct bpf_func_proto bpf_csum_diff_proto = {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001596 .func = bpf_csum_diff,
1597 .gpl_only = false,
1598 .ret_type = RET_INTEGER,
1599 .arg1_type = ARG_PTR_TO_STACK,
1600 .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1601 .arg3_type = ARG_PTR_TO_STACK,
1602 .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1603 .arg5_type = ARG_ANYTHING,
1604};
1605
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001606static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1607{
1608 struct sk_buff *skb = (struct sk_buff *) (long) r1, *skb2;
1609 struct net_device *dev;
1610
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001611 if (unlikely(flags & ~(BPF_F_INGRESS)))
1612 return -EINVAL;
1613
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001614 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1615 if (unlikely(!dev))
1616 return -EINVAL;
1617
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001618 skb2 = skb_clone(skb, GFP_ATOMIC);
1619 if (unlikely(!skb2))
1620 return -ENOMEM;
1621
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001622 if (flags & BPF_F_INGRESS) {
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001623 if (skb_at_tc_ingress(skb2))
1624 skb_postpush_rcsum(skb2, skb_mac_header(skb2),
1625 skb2->mac_len);
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001626 return dev_forward_skb(dev, skb2);
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001627 }
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001628
1629 skb2->dev = dev;
1630 return dev_queue_xmit(skb2);
1631}
1632
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001633static const struct bpf_func_proto bpf_clone_redirect_proto = {
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001634 .func = bpf_clone_redirect,
1635 .gpl_only = false,
1636 .ret_type = RET_INTEGER,
1637 .arg1_type = ARG_PTR_TO_CTX,
1638 .arg2_type = ARG_ANYTHING,
1639 .arg3_type = ARG_ANYTHING,
1640};
1641
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001642struct redirect_info {
1643 u32 ifindex;
1644 u32 flags;
1645};
1646
1647static DEFINE_PER_CPU(struct redirect_info, redirect_info);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001648
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001649static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5)
1650{
1651 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1652
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001653 if (unlikely(flags & ~(BPF_F_INGRESS)))
1654 return TC_ACT_SHOT;
1655
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001656 ri->ifindex = ifindex;
1657 ri->flags = flags;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001658
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001659 return TC_ACT_REDIRECT;
1660}
1661
1662int skb_do_redirect(struct sk_buff *skb)
1663{
1664 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1665 struct net_device *dev;
1666
1667 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1668 ri->ifindex = 0;
1669 if (unlikely(!dev)) {
1670 kfree_skb(skb);
1671 return -EINVAL;
1672 }
1673
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001674 if (ri->flags & BPF_F_INGRESS) {
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001675 if (skb_at_tc_ingress(skb))
1676 skb_postpush_rcsum(skb, skb_mac_header(skb),
1677 skb->mac_len);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001678 return dev_forward_skb(dev, skb);
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001679 }
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001680
1681 skb->dev = dev;
1682 return dev_queue_xmit(skb);
1683}
1684
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001685static const struct bpf_func_proto bpf_redirect_proto = {
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001686 .func = bpf_redirect,
1687 .gpl_only = false,
1688 .ret_type = RET_INTEGER,
1689 .arg1_type = ARG_ANYTHING,
1690 .arg2_type = ARG_ANYTHING,
1691};
1692
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001693static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1694{
1695 return task_get_classid((struct sk_buff *) (unsigned long) r1);
1696}
1697
1698static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1699 .func = bpf_get_cgroup_classid,
1700 .gpl_only = false,
1701 .ret_type = RET_INTEGER,
1702 .arg1_type = ARG_PTR_TO_CTX,
1703};
1704
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001705static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1706{
Daniel Borkmann808c1b62016-03-16 01:42:50 +01001707 return dst_tclassid((struct sk_buff *) (unsigned long) r1);
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001708}
1709
1710static const struct bpf_func_proto bpf_get_route_realm_proto = {
1711 .func = bpf_get_route_realm,
1712 .gpl_only = false,
1713 .ret_type = RET_INTEGER,
1714 .arg1_type = ARG_PTR_TO_CTX,
1715};
1716
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001717static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
1718{
1719 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1720 __be16 vlan_proto = (__force __be16) r2;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001721 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001722
1723 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1724 vlan_proto != htons(ETH_P_8021AD)))
1725 vlan_proto = htons(ETH_P_8021Q);
1726
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001727 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
1728 bpf_compute_data_end(skb);
1729 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001730}
1731
1732const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1733 .func = bpf_skb_vlan_push,
1734 .gpl_only = false,
1735 .ret_type = RET_INTEGER,
1736 .arg1_type = ARG_PTR_TO_CTX,
1737 .arg2_type = ARG_ANYTHING,
1738 .arg3_type = ARG_ANYTHING,
1739};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001740EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001741
1742static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1743{
1744 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001745 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001746
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001747 ret = skb_vlan_pop(skb);
1748 bpf_compute_data_end(skb);
1749 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001750}
1751
1752const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1753 .func = bpf_skb_vlan_pop,
1754 .gpl_only = false,
1755 .ret_type = RET_INTEGER,
1756 .arg1_type = ARG_PTR_TO_CTX,
1757};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001758EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001759
1760bool bpf_helper_changes_skb_data(void *func)
1761{
1762 if (func == bpf_skb_vlan_push)
1763 return true;
1764 if (func == bpf_skb_vlan_pop)
1765 return true;
Daniel Borkmann36976492016-02-19 23:05:25 +01001766 if (func == bpf_skb_store_bytes)
1767 return true;
1768 if (func == bpf_l3_csum_replace)
1769 return true;
1770 if (func == bpf_l4_csum_replace)
1771 return true;
1772
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001773 return false;
1774}
1775
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001776static unsigned short bpf_tunnel_key_af(u64 flags)
1777{
1778 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
1779}
1780
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001781static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1782{
1783 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1784 struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001785 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
1786 u8 compat[sizeof(struct bpf_tunnel_key)];
Daniel Borkmann074f5282016-04-13 00:10:52 +02001787 void *to_orig = to;
1788 int err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001789
Daniel Borkmann074f5282016-04-13 00:10:52 +02001790 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
1791 err = -EINVAL;
1792 goto err_clear;
1793 }
1794 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
1795 err = -EPROTO;
1796 goto err_clear;
1797 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001798 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
Daniel Borkmann074f5282016-04-13 00:10:52 +02001799 err = -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001800 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01001801 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02001802 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmann4018ab12016-03-09 03:00:05 +01001803 goto set_compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001804 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
1805 /* Fixup deprecated structure layouts here, so we have
1806 * a common path later on.
1807 */
1808 if (ip_tunnel_info_af(info) != AF_INET)
Daniel Borkmann074f5282016-04-13 00:10:52 +02001809 goto err_clear;
Daniel Borkmann4018ab12016-03-09 03:00:05 +01001810set_compat:
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001811 to = (struct bpf_tunnel_key *)compat;
1812 break;
1813 default:
Daniel Borkmann074f5282016-04-13 00:10:52 +02001814 goto err_clear;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001815 }
1816 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001817
1818 to->tunnel_id = be64_to_cpu(info->key.tun_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001819 to->tunnel_tos = info->key.tos;
1820 to->tunnel_ttl = info->key.ttl;
1821
Daniel Borkmann4018ab12016-03-09 03:00:05 +01001822 if (flags & BPF_F_TUNINFO_IPV6) {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001823 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
1824 sizeof(to->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01001825 to->tunnel_label = be32_to_cpu(info->key.label);
1826 } else {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001827 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
Daniel Borkmann4018ab12016-03-09 03:00:05 +01001828 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001829
1830 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001831 memcpy(to_orig, to, size);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001832
1833 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02001834err_clear:
1835 memset(to_orig, 0, size);
1836 return err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001837}
1838
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001839static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001840 .func = bpf_skb_get_tunnel_key,
1841 .gpl_only = false,
1842 .ret_type = RET_INTEGER,
1843 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02001844 .arg2_type = ARG_PTR_TO_RAW_STACK,
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001845 .arg3_type = ARG_CONST_STACK_SIZE,
1846 .arg4_type = ARG_ANYTHING,
1847};
1848
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001849static u64 bpf_skb_get_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
1850{
1851 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1852 u8 *to = (u8 *) (long) r2;
1853 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
Daniel Borkmann074f5282016-04-13 00:10:52 +02001854 int err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001855
1856 if (unlikely(!info ||
Daniel Borkmann074f5282016-04-13 00:10:52 +02001857 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
1858 err = -ENOENT;
1859 goto err_clear;
1860 }
1861 if (unlikely(size < info->options_len)) {
1862 err = -ENOMEM;
1863 goto err_clear;
1864 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001865
1866 ip_tunnel_info_opts_get(to, info);
Daniel Borkmann074f5282016-04-13 00:10:52 +02001867 if (size > info->options_len)
1868 memset(to + info->options_len, 0, size - info->options_len);
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001869
1870 return info->options_len;
Daniel Borkmann074f5282016-04-13 00:10:52 +02001871err_clear:
1872 memset(to, 0, size);
1873 return err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001874}
1875
1876static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
1877 .func = bpf_skb_get_tunnel_opt,
1878 .gpl_only = false,
1879 .ret_type = RET_INTEGER,
1880 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02001881 .arg2_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001882 .arg3_type = ARG_CONST_STACK_SIZE,
1883};
1884
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001885static struct metadata_dst __percpu *md_dst;
1886
1887static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
1888{
1889 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1890 struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2;
1891 struct metadata_dst *md = this_cpu_ptr(md_dst);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001892 u8 compat[sizeof(struct bpf_tunnel_key)];
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001893 struct ip_tunnel_info *info;
1894
Daniel Borkmann22080872016-03-04 15:15:05 +01001895 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
1896 BPF_F_DONT_FRAGMENT)))
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001897 return -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001898 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
1899 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01001900 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02001901 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001902 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
1903 /* Fixup deprecated structure layouts here, so we have
1904 * a common path later on.
1905 */
1906 memcpy(compat, from, size);
1907 memset(compat + size, 0, sizeof(compat) - size);
1908 from = (struct bpf_tunnel_key *)compat;
1909 break;
1910 default:
1911 return -EINVAL;
1912 }
1913 }
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02001914 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
1915 from->tunnel_ext))
Daniel Borkmann4018ab12016-03-09 03:00:05 +01001916 return -EINVAL;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001917
1918 skb_dst_drop(skb);
1919 dst_hold((struct dst_entry *) md);
1920 skb_dst_set(skb, (struct dst_entry *) md);
1921
1922 info = &md->u.tun_info;
1923 info->mode = IP_TUNNEL_INFO_TX;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001924
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01001925 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
Daniel Borkmann22080872016-03-04 15:15:05 +01001926 if (flags & BPF_F_DONT_FRAGMENT)
1927 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
1928
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001929 info->key.tun_id = cpu_to_be64(from->tunnel_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001930 info->key.tos = from->tunnel_tos;
1931 info->key.ttl = from->tunnel_ttl;
1932
1933 if (flags & BPF_F_TUNINFO_IPV6) {
1934 info->mode |= IP_TUNNEL_INFO_IPV6;
1935 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
1936 sizeof(from->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01001937 info->key.label = cpu_to_be32(from->tunnel_label) &
1938 IPV6_FLOWLABEL_MASK;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001939 } else {
1940 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
Daniel Borkmann2da897e2016-02-23 02:05:26 +01001941 if (flags & BPF_F_ZERO_CSUM_TX)
1942 info->key.tun_flags &= ~TUNNEL_CSUM;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01001943 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001944
1945 return 0;
1946}
1947
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001948static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001949 .func = bpf_skb_set_tunnel_key,
1950 .gpl_only = false,
1951 .ret_type = RET_INTEGER,
1952 .arg1_type = ARG_PTR_TO_CTX,
1953 .arg2_type = ARG_PTR_TO_STACK,
1954 .arg3_type = ARG_CONST_STACK_SIZE,
1955 .arg4_type = ARG_ANYTHING,
1956};
1957
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001958static u64 bpf_skb_set_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
1959{
1960 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1961 u8 *from = (u8 *) (long) r2;
1962 struct ip_tunnel_info *info = skb_tunnel_info(skb);
1963 const struct metadata_dst *md = this_cpu_ptr(md_dst);
1964
1965 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
1966 return -EINVAL;
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01001967 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001968 return -ENOMEM;
1969
1970 ip_tunnel_info_opts_set(info, from, size);
1971
1972 return 0;
1973}
1974
1975static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
1976 .func = bpf_skb_set_tunnel_opt,
1977 .gpl_only = false,
1978 .ret_type = RET_INTEGER,
1979 .arg1_type = ARG_PTR_TO_CTX,
1980 .arg2_type = ARG_PTR_TO_STACK,
1981 .arg3_type = ARG_CONST_STACK_SIZE,
1982};
1983
1984static const struct bpf_func_proto *
1985bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001986{
1987 if (!md_dst) {
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001988 /* Race is not possible, since it's called from verifier
1989 * that is holding verifier mutex.
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001990 */
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01001991 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001992 GFP_KERNEL);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07001993 if (!md_dst)
1994 return NULL;
1995 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01001996
1997 switch (which) {
1998 case BPF_FUNC_skb_set_tunnel_key:
1999 return &bpf_skb_set_tunnel_key_proto;
2000 case BPF_FUNC_skb_set_tunnel_opt:
2001 return &bpf_skb_set_tunnel_opt_proto;
2002 default:
2003 return NULL;
2004 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002005}
2006
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002007static const struct bpf_func_proto *
2008sk_filter_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002009{
2010 switch (func_id) {
2011 case BPF_FUNC_map_lookup_elem:
2012 return &bpf_map_lookup_elem_proto;
2013 case BPF_FUNC_map_update_elem:
2014 return &bpf_map_update_elem_proto;
2015 case BPF_FUNC_map_delete_elem:
2016 return &bpf_map_delete_elem_proto;
Daniel Borkmann03e69b52015-03-14 02:27:16 +01002017 case BPF_FUNC_get_prandom_u32:
2018 return &bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01002019 case BPF_FUNC_get_smp_processor_id:
2020 return &bpf_get_smp_processor_id_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07002021 case BPF_FUNC_tail_call:
2022 return &bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02002023 case BPF_FUNC_ktime_get_ns:
2024 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07002025 case BPF_FUNC_trace_printk:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002026 if (capable(CAP_SYS_ADMIN))
2027 return bpf_get_trace_printk_proto();
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002028 default:
2029 return NULL;
2030 }
2031}
2032
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002033static const struct bpf_func_proto *
2034tc_cls_act_func_proto(enum bpf_func_id func_id)
2035{
2036 switch (func_id) {
2037 case BPF_FUNC_skb_store_bytes:
2038 return &bpf_skb_store_bytes_proto;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01002039 case BPF_FUNC_skb_load_bytes:
2040 return &bpf_skb_load_bytes_proto;
Daniel Borkmann7d672342016-02-19 23:05:23 +01002041 case BPF_FUNC_csum_diff:
2042 return &bpf_csum_diff_proto;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07002043 case BPF_FUNC_l3_csum_replace:
2044 return &bpf_l3_csum_replace_proto;
2045 case BPF_FUNC_l4_csum_replace:
2046 return &bpf_l4_csum_replace_proto;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07002047 case BPF_FUNC_clone_redirect:
2048 return &bpf_clone_redirect_proto;
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02002049 case BPF_FUNC_get_cgroup_classid:
2050 return &bpf_get_cgroup_classid_proto;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002051 case BPF_FUNC_skb_vlan_push:
2052 return &bpf_skb_vlan_push_proto;
2053 case BPF_FUNC_skb_vlan_pop:
2054 return &bpf_skb_vlan_pop_proto;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002055 case BPF_FUNC_skb_get_tunnel_key:
2056 return &bpf_skb_get_tunnel_key_proto;
2057 case BPF_FUNC_skb_set_tunnel_key:
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002058 return bpf_get_skb_set_tunnel_proto(func_id);
2059 case BPF_FUNC_skb_get_tunnel_opt:
2060 return &bpf_skb_get_tunnel_opt_proto;
2061 case BPF_FUNC_skb_set_tunnel_opt:
2062 return bpf_get_skb_set_tunnel_proto(func_id);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07002063 case BPF_FUNC_redirect:
2064 return &bpf_redirect_proto;
Daniel Borkmannc46646d2015-09-30 01:41:51 +02002065 case BPF_FUNC_get_route_realm:
2066 return &bpf_get_route_realm_proto;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02002067 case BPF_FUNC_perf_event_output:
2068 return bpf_get_event_output_proto();
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002069 default:
2070 return sk_filter_func_proto(func_id);
2071 }
2072}
2073
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002074static bool __is_valid_access(int off, int size, enum bpf_access_type type)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002075{
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002076 if (off < 0 || off >= sizeof(struct __sk_buff))
2077 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002078 /* The verifier guarantees that size > 0. */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002079 if (off % size != 0)
2080 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002081 if (size != sizeof(__u32))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002082 return false;
2083
2084 return true;
2085}
2086
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002087static bool sk_filter_is_valid_access(int off, int size,
2088 enum bpf_access_type type)
2089{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002090 switch (off) {
2091 case offsetof(struct __sk_buff, tc_classid):
2092 case offsetof(struct __sk_buff, data):
2093 case offsetof(struct __sk_buff, data_end):
Daniel Borkmann045efa82015-09-15 23:05:42 -07002094 return false;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002095 }
Daniel Borkmann045efa82015-09-15 23:05:42 -07002096
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002097 if (type == BPF_WRITE) {
2098 switch (off) {
2099 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann4936e352016-05-13 19:08:26 +02002100 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002101 break;
2102 default:
2103 return false;
2104 }
2105 }
2106
2107 return __is_valid_access(off, size, type);
2108}
2109
2110static bool tc_cls_act_is_valid_access(int off, int size,
2111 enum bpf_access_type type)
2112{
2113 if (type == BPF_WRITE) {
2114 switch (off) {
2115 case offsetof(struct __sk_buff, mark):
2116 case offsetof(struct __sk_buff, tc_index):
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002117 case offsetof(struct __sk_buff, priority):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002118 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002119 offsetof(struct __sk_buff, cb[4]):
2120 case offsetof(struct __sk_buff, tc_classid):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002121 break;
2122 default:
2123 return false;
2124 }
2125 }
2126 return __is_valid_access(off, size, type);
2127}
2128
2129static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2130 int src_reg, int ctx_off,
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002131 struct bpf_insn *insn_buf,
2132 struct bpf_prog *prog)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002133{
2134 struct bpf_insn *insn = insn_buf;
2135
2136 switch (ctx_off) {
2137 case offsetof(struct __sk_buff, len):
2138 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2139
2140 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2141 offsetof(struct sk_buff, len));
2142 break;
2143
Daniel Borkmann0b8c7072015-03-19 19:38:27 +01002144 case offsetof(struct __sk_buff, protocol):
2145 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2146
2147 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2148 offsetof(struct sk_buff, protocol));
2149 break;
2150
Michal Sekletar27cd5452015-03-24 14:48:41 +01002151 case offsetof(struct __sk_buff, vlan_proto):
2152 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2153
2154 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2155 offsetof(struct sk_buff, vlan_proto));
2156 break;
2157
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002158 case offsetof(struct __sk_buff, priority):
2159 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2160
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002161 if (type == BPF_WRITE)
2162 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2163 offsetof(struct sk_buff, priority));
2164 else
2165 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2166 offsetof(struct sk_buff, priority));
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002167 break;
2168
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002169 case offsetof(struct __sk_buff, ingress_ifindex):
2170 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2171
2172 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2173 offsetof(struct sk_buff, skb_iif));
2174 break;
2175
2176 case offsetof(struct __sk_buff, ifindex):
2177 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2178
2179 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
2180 dst_reg, src_reg,
2181 offsetof(struct sk_buff, dev));
2182 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2183 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2184 offsetof(struct net_device, ifindex));
2185 break;
2186
Daniel Borkmannba7591d2015-08-01 00:46:29 +02002187 case offsetof(struct __sk_buff, hash):
2188 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2189
2190 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2191 offsetof(struct sk_buff, hash));
2192 break;
2193
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002194 case offsetof(struct __sk_buff, mark):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002195 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2196
2197 if (type == BPF_WRITE)
2198 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2199 offsetof(struct sk_buff, mark));
2200 else
2201 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2202 offsetof(struct sk_buff, mark));
2203 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002204
2205 case offsetof(struct __sk_buff, pkt_type):
2206 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2207
2208 case offsetof(struct __sk_buff, queue_mapping):
2209 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002210
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002211 case offsetof(struct __sk_buff, vlan_present):
2212 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2213 dst_reg, src_reg, insn);
2214
2215 case offsetof(struct __sk_buff, vlan_tci):
2216 return convert_skb_access(SKF_AD_VLAN_TAG,
2217 dst_reg, src_reg, insn);
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002218
2219 case offsetof(struct __sk_buff, cb[0]) ...
2220 offsetof(struct __sk_buff, cb[4]):
2221 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2222
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002223 prog->cb_access = 1;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002224 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2225 ctx_off += offsetof(struct sk_buff, cb);
2226 ctx_off += offsetof(struct qdisc_skb_cb, data);
2227 if (type == BPF_WRITE)
2228 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2229 else
2230 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2231 break;
2232
Daniel Borkmann045efa82015-09-15 23:05:42 -07002233 case offsetof(struct __sk_buff, tc_classid):
2234 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2235 ctx_off += offsetof(struct sk_buff, cb);
2236 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002237 if (type == BPF_WRITE)
2238 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2239 else
2240 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
Daniel Borkmann045efa82015-09-15 23:05:42 -07002241 break;
2242
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002243 case offsetof(struct __sk_buff, data):
2244 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, data)),
2245 dst_reg, src_reg,
2246 offsetof(struct sk_buff, data));
2247 break;
2248
2249 case offsetof(struct __sk_buff, data_end):
2250 ctx_off -= offsetof(struct __sk_buff, data_end);
2251 ctx_off += offsetof(struct sk_buff, cb);
2252 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
2253 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(sizeof(void *)),
2254 dst_reg, src_reg, ctx_off);
2255 break;
2256
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002257 case offsetof(struct __sk_buff, tc_index):
2258#ifdef CONFIG_NET_SCHED
2259 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2260
2261 if (type == BPF_WRITE)
2262 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2263 offsetof(struct sk_buff, tc_index));
2264 else
2265 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2266 offsetof(struct sk_buff, tc_index));
2267 break;
2268#else
2269 if (type == BPF_WRITE)
2270 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2271 else
2272 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2273 break;
2274#endif
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002275 }
2276
2277 return insn - insn_buf;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002278}
2279
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002280static const struct bpf_verifier_ops sk_filter_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002281 .get_func_proto = sk_filter_func_proto,
2282 .is_valid_access = sk_filter_is_valid_access,
2283 .convert_ctx_access = bpf_net_convert_ctx_access,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002284};
2285
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002286static const struct bpf_verifier_ops tc_cls_act_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002287 .get_func_proto = tc_cls_act_func_proto,
2288 .is_valid_access = tc_cls_act_is_valid_access,
2289 .convert_ctx_access = bpf_net_convert_ctx_access,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002290};
2291
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002292static struct bpf_prog_type_list sk_filter_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002293 .ops = &sk_filter_ops,
2294 .type = BPF_PROG_TYPE_SOCKET_FILTER,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002295};
2296
Daniel Borkmann96be4322015-03-01 12:31:46 +01002297static struct bpf_prog_type_list sched_cls_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002298 .ops = &tc_cls_act_ops,
2299 .type = BPF_PROG_TYPE_SCHED_CLS,
Daniel Borkmann96be4322015-03-01 12:31:46 +01002300};
2301
Daniel Borkmann94caee82015-03-20 15:11:11 +01002302static struct bpf_prog_type_list sched_act_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002303 .ops = &tc_cls_act_ops,
2304 .type = BPF_PROG_TYPE_SCHED_ACT,
Daniel Borkmann94caee82015-03-20 15:11:11 +01002305};
2306
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002307static int __init register_sk_filter_ops(void)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002308{
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002309 bpf_register_prog_type(&sk_filter_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01002310 bpf_register_prog_type(&sched_cls_type);
Daniel Borkmann94caee82015-03-20 15:11:11 +01002311 bpf_register_prog_type(&sched_act_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01002312
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002313 return 0;
2314}
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002315late_initcall(register_sk_filter_ops);
2316
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002317int sk_detach_filter(struct sock *sk)
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002318{
2319 int ret = -ENOENT;
2320 struct sk_filter *filter;
2321
Vincent Bernatd59577b2013-01-16 22:55:49 +01002322 if (sock_flag(sk, SOCK_FILTER_LOCKED))
2323 return -EPERM;
2324
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002325 filter = rcu_dereference_protected(sk->sk_filter,
2326 lockdep_sock_is_held(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002327 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002328 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -08002329 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002330 ret = 0;
2331 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002332
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002333 return ret;
2334}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002335EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002336
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002337int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
2338 unsigned int len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002339{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002340 struct sock_fprog_kern *fprog;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002341 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002342 int ret = 0;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002343
2344 lock_sock(sk);
2345 filter = rcu_dereference_protected(sk->sk_filter,
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002346 lockdep_sock_is_held(sk));
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002347 if (!filter)
2348 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002349
2350 /* We're copying the filter that has been originally attached,
Daniel Borkmann93d08b62015-10-02 12:06:03 +02002351 * so no conversion/decode needed anymore. eBPF programs that
2352 * have no original program cannot be dumped through this.
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002353 */
Daniel Borkmann93d08b62015-10-02 12:06:03 +02002354 ret = -EACCES;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002355 fprog = filter->prog->orig_prog;
Daniel Borkmann93d08b62015-10-02 12:06:03 +02002356 if (!fprog)
2357 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002358
2359 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002360 if (!len)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002361 /* User space only enquires number of filter blocks. */
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002362 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002363
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002364 ret = -EINVAL;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002365 if (len < fprog->len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002366 goto out;
2367
2368 ret = -EFAULT;
Alexei Starovoitov009937e2014-07-30 20:34:13 -07002369 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002370 goto out;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002371
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002372 /* Instead of bytes, the API requests to return the number
2373 * of filter blocks.
2374 */
2375 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002376out:
2377 release_sock(sk);
2378 return ret;
2379}