blob: a83766be1ad2f6527105864900d2bf964e3ef8b4 [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/**
Willem de Bruijnf4979fc2016-07-12 18:18:56 -040056 * sk_filter_trim_cap - run a packet through a socket filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -070057 * @sk: sock associated with &sk_buff
58 * @skb: buffer to filter
Willem de Bruijnf4979fc2016-07-12 18:18:56 -040059 * @cap: limit on how short the eBPF program may trim the packet
Stephen Hemminger43db6d62008-04-10 01:43:09 -070060 *
Alexei Starovoitovff936a02015-10-07 10:55:41 -070061 * Run the eBPF program and then cut skb->data to correct size returned by
62 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
Stephen Hemminger43db6d62008-04-10 01:43:09 -070063 * than pkt_len we keep whole skb->data. This is the socket level
Alexei Starovoitovff936a02015-10-07 10:55:41 -070064 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
Stephen Hemminger43db6d62008-04-10 01:43:09 -070065 * be accepted or -EPERM if the packet should be tossed.
66 *
67 */
Willem de Bruijnf4979fc2016-07-12 18:18:56 -040068int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
Stephen Hemminger43db6d62008-04-10 01:43:09 -070069{
70 int err;
71 struct sk_filter *filter;
72
Mel Gormanc93bdd02012-07-31 16:44:19 -070073 /*
74 * If the skb was allocated from pfmemalloc reserves, only
75 * allow SOCK_MEMALLOC sockets to use it as this socket is
76 * helping free memory
77 */
78 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
79 return -ENOMEM;
80
Stephen Hemminger43db6d62008-04-10 01:43:09 -070081 err = security_sock_rcv_skb(sk, skb);
82 if (err)
83 return err;
84
Eric Dumazet80f8f102011-01-18 07:46:52 +000085 rcu_read_lock();
86 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -070087 if (filter) {
Alexei Starovoitovff936a02015-10-07 10:55:41 -070088 unsigned int pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
Willem de Bruijnf4979fc2016-07-12 18:18:56 -040089 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
Stephen Hemminger43db6d62008-04-10 01:43:09 -070090 }
Eric Dumazet80f8f102011-01-18 07:46:52 +000091 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -070092
93 return err;
94}
Willem de Bruijnf4979fc2016-07-12 18:18:56 -040095EXPORT_SYMBOL(sk_filter_trim_cap);
Stephen Hemminger43db6d62008-04-10 01:43:09 -070096
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
Daniel Borkmann80b48c42016-06-28 12:18:26 +0200153static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
154 .func = __get_raw_cpu_id,
155 .gpl_only = false,
156 .ret_type = RET_INTEGER,
157};
158
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700159static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
160 struct bpf_insn *insn_buf)
161{
162 struct bpf_insn *insn = insn_buf;
163
164 switch (skb_field) {
165 case SKF_AD_MARK:
166 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
167
168 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
169 offsetof(struct sk_buff, mark));
170 break;
171
172 case SKF_AD_PKTTYPE:
173 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
174 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
175#ifdef __BIG_ENDIAN_BITFIELD
176 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
177#endif
178 break;
179
180 case SKF_AD_QUEUE:
181 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
182
183 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
184 offsetof(struct sk_buff, queue_mapping));
185 break;
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700186
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700187 case SKF_AD_VLAN_TAG:
188 case SKF_AD_VLAN_TAG_PRESENT:
189 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
190 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
191
192 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
193 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
194 offsetof(struct sk_buff, vlan_tci));
195 if (skb_field == SKF_AD_VLAN_TAG) {
196 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
197 ~VLAN_TAG_PRESENT);
198 } else {
199 /* dst_reg >>= 12 */
200 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
201 /* dst_reg &= 1 */
202 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
203 }
204 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700205 }
206
207 return insn - insn_buf;
208}
209
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100210static bool convert_bpf_extensions(struct sock_filter *fp,
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700211 struct bpf_insn **insnp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100212{
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700213 struct bpf_insn *insn = *insnp;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700214 u32 cnt;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100215
216 switch (fp->k) {
217 case SKF_AD_OFF + SKF_AD_PROTOCOL:
Daniel Borkmann0b8c7072015-03-19 19:38:27 +0100218 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
219
220 /* A = *(u16 *) (CTX + offsetof(protocol)) */
221 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
222 offsetof(struct sk_buff, protocol));
223 /* A = ntohs(A) [emitting a nop or swap16] */
224 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100225 break;
226
227 case SKF_AD_OFF + SKF_AD_PKTTYPE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700228 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
229 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100230 break;
231
232 case SKF_AD_OFF + SKF_AD_IFINDEX:
233 case SKF_AD_OFF + SKF_AD_HATYPE:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100234 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
235 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200236 BUILD_BUG_ON(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)) < 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100237
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200238 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
239 BPF_REG_TMP, BPF_REG_CTX,
240 offsetof(struct sk_buff, dev));
241 /* if (tmp != 0) goto pc + 1 */
242 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
243 *insn++ = BPF_EXIT_INSN();
244 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
245 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
246 offsetof(struct net_device, ifindex));
247 else
248 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
249 offsetof(struct net_device, type));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100250 break;
251
252 case SKF_AD_OFF + SKF_AD_MARK:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700253 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
254 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100255 break;
256
257 case SKF_AD_OFF + SKF_AD_RXHASH:
258 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
259
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700260 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
261 offsetof(struct sk_buff, hash));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100262 break;
263
264 case SKF_AD_OFF + SKF_AD_QUEUE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700265 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
266 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100267 break;
268
269 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700270 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
271 BPF_REG_A, BPF_REG_CTX, insn);
272 insn += cnt - 1;
273 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100274
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700275 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
276 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
277 BPF_REG_A, BPF_REG_CTX, insn);
278 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100279 break;
280
Michal Sekletar27cd5452015-03-24 14:48:41 +0100281 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
282 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
283
284 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
285 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
286 offsetof(struct sk_buff, vlan_proto));
287 /* A = ntohs(A) [emitting a nop or swap16] */
288 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
289 break;
290
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100291 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
292 case SKF_AD_OFF + SKF_AD_NLATTR:
293 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
294 case SKF_AD_OFF + SKF_AD_CPU:
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700295 case SKF_AD_OFF + SKF_AD_RANDOM:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700296 /* arg1 = CTX */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200297 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100298 /* arg2 = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200299 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100300 /* arg3 = X */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200301 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
Alexei Starovoitove430f342014-06-06 14:46:06 -0700302 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100303 switch (fp->k) {
304 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200305 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100306 break;
307 case SKF_AD_OFF + SKF_AD_NLATTR:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200308 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100309 break;
310 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200311 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100312 break;
313 case SKF_AD_OFF + SKF_AD_CPU:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200314 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100315 break;
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700316 case SKF_AD_OFF + SKF_AD_RANDOM:
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200317 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
318 bpf_user_rnd_init_once();
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700319 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100320 }
321 break;
322
323 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700324 /* A ^= X */
325 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100326 break;
327
328 default:
329 /* This is just a dummy call to avoid letting the compiler
330 * evict __bpf_call_base() as an optimization. Placed here
331 * where no-one bothers.
332 */
333 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
334 return false;
335 }
336
337 *insnp = insn;
338 return true;
339}
340
341/**
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700342 * bpf_convert_filter - convert filter program
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100343 * @prog: the user passed filter program
344 * @len: the length of the user passed filter program
345 * @new_prog: buffer where converted program will be stored
346 * @new_len: pointer to store length of converted program
347 *
348 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
349 * Conversion workflow:
350 *
351 * 1) First pass for calculating the new program length:
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700352 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100353 *
354 * 2) 2nd pass to remap in two passes: 1st pass finds new
355 * jump offsets, 2nd pass remapping:
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700356 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700357 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100358 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200359static int bpf_convert_filter(struct sock_filter *prog, int len,
360 struct bpf_insn *new_prog, int *new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100361{
362 int new_flen = 0, pass = 0, target, i;
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700363 struct bpf_insn *new_insn;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100364 struct sock_filter *fp;
365 int *addrs = NULL;
366 u8 bpf_src;
367
368 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
Daniel Borkmann30743832014-05-01 18:34:19 +0200369 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100370
Kees Cook6f9a0932014-06-18 15:34:57 -0700371 if (len <= 0 || len > BPF_MAXINSNS)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100372 return -EINVAL;
373
374 if (new_prog) {
Daniel Borkmann658da932015-05-06 16:12:29 +0200375 addrs = kcalloc(len, sizeof(*addrs),
376 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100377 if (!addrs)
378 return -ENOMEM;
379 }
380
381do_pass:
382 new_insn = new_prog;
383 fp = prog;
384
Daniel Borkmann8b614ae2015-12-17 23:51:54 +0100385 /* Classic BPF related prologue emission. */
386 if (new_insn) {
387 /* Classic BPF expects A and X to be reset first. These need
388 * to be guaranteed to be the first two instructions.
389 */
390 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
391 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
392
393 /* All programs must keep CTX in callee saved BPF_REG_CTX.
394 * In eBPF case it's done by the compiler, here we need to
395 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
396 */
397 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
398 } else {
399 new_insn += 3;
400 }
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100401
402 for (i = 0; i < len; fp++, i++) {
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700403 struct bpf_insn tmp_insns[6] = { };
404 struct bpf_insn *insn = tmp_insns;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100405
406 if (addrs)
407 addrs[i] = new_insn - new_prog;
408
409 switch (fp->code) {
410 /* All arithmetic insns and skb loads map as-is. */
411 case BPF_ALU | BPF_ADD | BPF_X:
412 case BPF_ALU | BPF_ADD | BPF_K:
413 case BPF_ALU | BPF_SUB | BPF_X:
414 case BPF_ALU | BPF_SUB | BPF_K:
415 case BPF_ALU | BPF_AND | BPF_X:
416 case BPF_ALU | BPF_AND | BPF_K:
417 case BPF_ALU | BPF_OR | BPF_X:
418 case BPF_ALU | BPF_OR | BPF_K:
419 case BPF_ALU | BPF_LSH | BPF_X:
420 case BPF_ALU | BPF_LSH | BPF_K:
421 case BPF_ALU | BPF_RSH | BPF_X:
422 case BPF_ALU | BPF_RSH | BPF_K:
423 case BPF_ALU | BPF_XOR | BPF_X:
424 case BPF_ALU | BPF_XOR | BPF_K:
425 case BPF_ALU | BPF_MUL | BPF_X:
426 case BPF_ALU | BPF_MUL | BPF_K:
427 case BPF_ALU | BPF_DIV | BPF_X:
428 case BPF_ALU | BPF_DIV | BPF_K:
429 case BPF_ALU | BPF_MOD | BPF_X:
430 case BPF_ALU | BPF_MOD | BPF_K:
431 case BPF_ALU | BPF_NEG:
432 case BPF_LD | BPF_ABS | BPF_W:
433 case BPF_LD | BPF_ABS | BPF_H:
434 case BPF_LD | BPF_ABS | BPF_B:
435 case BPF_LD | BPF_IND | BPF_W:
436 case BPF_LD | BPF_IND | BPF_H:
437 case BPF_LD | BPF_IND | BPF_B:
438 /* Check for overloaded BPF extension and
439 * directly convert it if found, otherwise
440 * just move on with mapping.
441 */
442 if (BPF_CLASS(fp->code) == BPF_LD &&
443 BPF_MODE(fp->code) == BPF_ABS &&
444 convert_bpf_extensions(fp, &insn))
445 break;
446
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200447 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100448 break;
449
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200450 /* Jump transformation cannot use BPF block macros
451 * everywhere as offset calculation and target updates
452 * require a bit more work than the rest, i.e. jump
453 * opcodes map as-is, but offsets need adjustment.
454 */
455
456#define BPF_EMIT_JMP \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100457 do { \
458 if (target >= len || target < 0) \
459 goto err; \
460 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
461 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
462 insn->off -= insn - tmp_insns; \
463 } while (0)
464
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200465 case BPF_JMP | BPF_JA:
466 target = i + fp->k + 1;
467 insn->code = fp->code;
468 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100469 break;
470
471 case BPF_JMP | BPF_JEQ | BPF_K:
472 case BPF_JMP | BPF_JEQ | BPF_X:
473 case BPF_JMP | BPF_JSET | BPF_K:
474 case BPF_JMP | BPF_JSET | BPF_X:
475 case BPF_JMP | BPF_JGT | BPF_K:
476 case BPF_JMP | BPF_JGT | BPF_X:
477 case BPF_JMP | BPF_JGE | BPF_K:
478 case BPF_JMP | BPF_JGE | BPF_X:
479 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
480 /* BPF immediates are signed, zero extend
481 * immediate into tmp register and use it
482 * in compare insn.
483 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200484 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100485
Alexei Starovoitove430f342014-06-06 14:46:06 -0700486 insn->dst_reg = BPF_REG_A;
487 insn->src_reg = BPF_REG_TMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100488 bpf_src = BPF_X;
489 } else {
Alexei Starovoitove430f342014-06-06 14:46:06 -0700490 insn->dst_reg = BPF_REG_A;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100491 insn->imm = fp->k;
492 bpf_src = BPF_SRC(fp->code);
Tycho Andersen19539ce2015-09-10 18:25:07 -0600493 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100494 }
495
496 /* Common case where 'jump_false' is next insn. */
497 if (fp->jf == 0) {
498 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
499 target = i + fp->jt + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200500 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100501 break;
502 }
503
504 /* Convert JEQ into JNE when 'jump_true' is next insn. */
505 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
506 insn->code = BPF_JMP | BPF_JNE | bpf_src;
507 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200508 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100509 break;
510 }
511
512 /* Other jumps are mapped into two insns: Jxx and JA. */
513 target = i + fp->jt + 1;
514 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200515 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100516 insn++;
517
518 insn->code = BPF_JMP | BPF_JA;
519 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200520 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100521 break;
522
523 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
524 case BPF_LDX | BPF_MSH | BPF_B:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700525 /* tmp = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200526 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
David S. Miller1268e252014-05-13 13:13:33 -0400527 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200528 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700529 /* A &= 0xf */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200530 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700531 /* A <<= 2 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200532 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700533 /* X = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200534 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700535 /* A = tmp */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200536 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100537 break;
538
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100539 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
540 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
541 */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100542 case BPF_RET | BPF_A:
543 case BPF_RET | BPF_K:
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100544 if (BPF_RVAL(fp->code) == BPF_K)
545 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
546 0, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700547 *insn = BPF_EXIT_INSN();
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100548 break;
549
550 /* Store to stack. */
551 case BPF_ST:
552 case BPF_STX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200553 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
554 BPF_ST ? BPF_REG_A : BPF_REG_X,
555 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100556 break;
557
558 /* Load from stack. */
559 case BPF_LD | BPF_MEM:
560 case BPF_LDX | BPF_MEM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200561 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
562 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
563 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100564 break;
565
566 /* A = K or X = K */
567 case BPF_LD | BPF_IMM:
568 case BPF_LDX | BPF_IMM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200569 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
570 BPF_REG_A : BPF_REG_X, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100571 break;
572
573 /* X = A */
574 case BPF_MISC | BPF_TAX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200575 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100576 break;
577
578 /* A = X */
579 case BPF_MISC | BPF_TXA:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200580 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100581 break;
582
583 /* A = skb->len or X = skb->len */
584 case BPF_LD | BPF_W | BPF_LEN:
585 case BPF_LDX | BPF_W | BPF_LEN:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200586 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
587 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
588 offsetof(struct sk_buff, len));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100589 break;
590
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200591 /* Access seccomp_data fields. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100592 case BPF_LDX | BPF_ABS | BPF_W:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700593 /* A = *(u32 *) (ctx + K) */
594 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100595 break;
596
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -0500597 /* Unknown instruction. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100598 default:
599 goto err;
600 }
601
602 insn++;
603 if (new_prog)
604 memcpy(new_insn, tmp_insns,
605 sizeof(*insn) * (insn - tmp_insns));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100606 new_insn += insn - tmp_insns;
607 }
608
609 if (!new_prog) {
610 /* Only calculating new length. */
611 *new_len = new_insn - new_prog;
612 return 0;
613 }
614
615 pass++;
616 if (new_flen != new_insn - new_prog) {
617 new_flen = new_insn - new_prog;
618 if (pass > 2)
619 goto err;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100620 goto do_pass;
621 }
622
623 kfree(addrs);
624 BUG_ON(*new_len != new_flen);
625 return 0;
626err:
627 kfree(addrs);
628 return -EINVAL;
629}
630
631/* Security:
632 *
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000633 * As we dont want to clear mem[] array for each packet going through
Li RongQing8ea6e342014-10-10 13:56:51 +0800634 * __bpf_prog_run(), we check that filter loaded by user never try to read
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000635 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300636 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000637 */
Eric Dumazetec31a052014-07-12 15:49:16 +0200638static int check_load_and_stores(const struct sock_filter *filter, int flen)
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000639{
Daniel Borkmann34805932014-05-29 10:22:50 +0200640 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000641 int pc, ret = 0;
642
643 BUILD_BUG_ON(BPF_MEMWORDS > 16);
Daniel Borkmann34805932014-05-29 10:22:50 +0200644
Tobias Klauser99e72a02014-06-24 15:33:22 +0200645 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000646 if (!masks)
647 return -ENOMEM;
Daniel Borkmann34805932014-05-29 10:22:50 +0200648
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000649 memset(masks, 0xff, flen * sizeof(*masks));
650
651 for (pc = 0; pc < flen; pc++) {
652 memvalid &= masks[pc];
653
654 switch (filter[pc].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200655 case BPF_ST:
656 case BPF_STX:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000657 memvalid |= (1 << filter[pc].k);
658 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200659 case BPF_LD | BPF_MEM:
660 case BPF_LDX | BPF_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000661 if (!(memvalid & (1 << filter[pc].k))) {
662 ret = -EINVAL;
663 goto error;
664 }
665 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200666 case BPF_JMP | BPF_JA:
667 /* A jump must set masks on target */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000668 masks[pc + 1 + filter[pc].k] &= memvalid;
669 memvalid = ~0;
670 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200671 case BPF_JMP | BPF_JEQ | BPF_K:
672 case BPF_JMP | BPF_JEQ | BPF_X:
673 case BPF_JMP | BPF_JGE | BPF_K:
674 case BPF_JMP | BPF_JGE | BPF_X:
675 case BPF_JMP | BPF_JGT | BPF_K:
676 case BPF_JMP | BPF_JGT | BPF_X:
677 case BPF_JMP | BPF_JSET | BPF_K:
678 case BPF_JMP | BPF_JSET | BPF_X:
679 /* A jump must set masks on targets */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000680 masks[pc + 1 + filter[pc].jt] &= memvalid;
681 masks[pc + 1 + filter[pc].jf] &= memvalid;
682 memvalid = ~0;
683 break;
684 }
685 }
686error:
687 kfree(masks);
688 return ret;
689}
690
Daniel Borkmann34805932014-05-29 10:22:50 +0200691static bool chk_code_allowed(u16 code_to_probe)
692{
693 static const bool codes[] = {
694 /* 32 bit ALU operations */
695 [BPF_ALU | BPF_ADD | BPF_K] = true,
696 [BPF_ALU | BPF_ADD | BPF_X] = true,
697 [BPF_ALU | BPF_SUB | BPF_K] = true,
698 [BPF_ALU | BPF_SUB | BPF_X] = true,
699 [BPF_ALU | BPF_MUL | BPF_K] = true,
700 [BPF_ALU | BPF_MUL | BPF_X] = true,
701 [BPF_ALU | BPF_DIV | BPF_K] = true,
702 [BPF_ALU | BPF_DIV | BPF_X] = true,
703 [BPF_ALU | BPF_MOD | BPF_K] = true,
704 [BPF_ALU | BPF_MOD | BPF_X] = true,
705 [BPF_ALU | BPF_AND | BPF_K] = true,
706 [BPF_ALU | BPF_AND | BPF_X] = true,
707 [BPF_ALU | BPF_OR | BPF_K] = true,
708 [BPF_ALU | BPF_OR | BPF_X] = true,
709 [BPF_ALU | BPF_XOR | BPF_K] = true,
710 [BPF_ALU | BPF_XOR | BPF_X] = true,
711 [BPF_ALU | BPF_LSH | BPF_K] = true,
712 [BPF_ALU | BPF_LSH | BPF_X] = true,
713 [BPF_ALU | BPF_RSH | BPF_K] = true,
714 [BPF_ALU | BPF_RSH | BPF_X] = true,
715 [BPF_ALU | BPF_NEG] = true,
716 /* Load instructions */
717 [BPF_LD | BPF_W | BPF_ABS] = true,
718 [BPF_LD | BPF_H | BPF_ABS] = true,
719 [BPF_LD | BPF_B | BPF_ABS] = true,
720 [BPF_LD | BPF_W | BPF_LEN] = true,
721 [BPF_LD | BPF_W | BPF_IND] = true,
722 [BPF_LD | BPF_H | BPF_IND] = true,
723 [BPF_LD | BPF_B | BPF_IND] = true,
724 [BPF_LD | BPF_IMM] = true,
725 [BPF_LD | BPF_MEM] = true,
726 [BPF_LDX | BPF_W | BPF_LEN] = true,
727 [BPF_LDX | BPF_B | BPF_MSH] = true,
728 [BPF_LDX | BPF_IMM] = true,
729 [BPF_LDX | BPF_MEM] = true,
730 /* Store instructions */
731 [BPF_ST] = true,
732 [BPF_STX] = true,
733 /* Misc instructions */
734 [BPF_MISC | BPF_TAX] = true,
735 [BPF_MISC | BPF_TXA] = true,
736 /* Return instructions */
737 [BPF_RET | BPF_K] = true,
738 [BPF_RET | BPF_A] = true,
739 /* Jump instructions */
740 [BPF_JMP | BPF_JA] = true,
741 [BPF_JMP | BPF_JEQ | BPF_K] = true,
742 [BPF_JMP | BPF_JEQ | BPF_X] = true,
743 [BPF_JMP | BPF_JGE | BPF_K] = true,
744 [BPF_JMP | BPF_JGE | BPF_X] = true,
745 [BPF_JMP | BPF_JGT | BPF_K] = true,
746 [BPF_JMP | BPF_JGT | BPF_X] = true,
747 [BPF_JMP | BPF_JSET | BPF_K] = true,
748 [BPF_JMP | BPF_JSET | BPF_X] = true,
749 };
750
751 if (code_to_probe >= ARRAY_SIZE(codes))
752 return false;
753
754 return codes[code_to_probe];
755}
756
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +0200757static bool bpf_check_basics_ok(const struct sock_filter *filter,
758 unsigned int flen)
759{
760 if (filter == NULL)
761 return false;
762 if (flen == 0 || flen > BPF_MAXINSNS)
763 return false;
764
765 return true;
766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/**
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -0700769 * bpf_check_classic - verify socket filter code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * @filter: filter to verify
771 * @flen: length of filter
772 *
773 * Check the user's filter code. If we let some ugly
774 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800775 * no references or jumps that are out of range, no illegal
776 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800778 * All jumps are forward as they are not signed.
779 *
780 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200782static int bpf_check_classic(const struct sock_filter *filter,
783 unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000785 bool anc_found;
Daniel Borkmann34805932014-05-29 10:22:50 +0200786 int pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Daniel Borkmann34805932014-05-29 10:22:50 +0200788 /* Check the filter code now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 for (pc = 0; pc < flen; pc++) {
Eric Dumazetec31a052014-07-12 15:49:16 +0200790 const struct sock_filter *ftest = &filter[pc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Daniel Borkmann34805932014-05-29 10:22:50 +0200792 /* May we actually operate on this code? */
793 if (!chk_code_allowed(ftest->code))
Tetsuo Handacba328f2010-11-16 15:19:51 +0000794 return -EINVAL;
Daniel Borkmann34805932014-05-29 10:22:50 +0200795
Kris Katterjohn93699862006-01-04 13:58:36 -0800796 /* Some instructions need special checks */
Daniel Borkmann34805932014-05-29 10:22:50 +0200797 switch (ftest->code) {
798 case BPF_ALU | BPF_DIV | BPF_K:
799 case BPF_ALU | BPF_MOD | BPF_K:
800 /* Check for division by zero */
Eric Dumazetb6069a92012-09-07 22:03:35 +0000801 if (ftest->k == 0)
802 return -EINVAL;
803 break;
Rabin Vincent229394e2016-01-12 20:17:08 +0100804 case BPF_ALU | BPF_LSH | BPF_K:
805 case BPF_ALU | BPF_RSH | BPF_K:
806 if (ftest->k >= 32)
807 return -EINVAL;
808 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200809 case BPF_LD | BPF_MEM:
810 case BPF_LDX | BPF_MEM:
811 case BPF_ST:
812 case BPF_STX:
813 /* Check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800814 if (ftest->k >= BPF_MEMWORDS)
815 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000816 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200817 case BPF_JMP | BPF_JA:
818 /* Note, the large ftest->k might cause loops.
Kris Katterjohn93699862006-01-04 13:58:36 -0800819 * Compare this with conditional jumps below,
820 * where offsets are limited. --ANK (981016)
821 */
Daniel Borkmann34805932014-05-29 10:22:50 +0200822 if (ftest->k >= (unsigned int)(flen - pc - 1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800823 return -EINVAL;
824 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200825 case BPF_JMP | BPF_JEQ | BPF_K:
826 case BPF_JMP | BPF_JEQ | BPF_X:
827 case BPF_JMP | BPF_JGE | BPF_K:
828 case BPF_JMP | BPF_JGE | BPF_X:
829 case BPF_JMP | BPF_JGT | BPF_K:
830 case BPF_JMP | BPF_JGT | BPF_X:
831 case BPF_JMP | BPF_JSET | BPF_K:
832 case BPF_JMP | BPF_JSET | BPF_X:
833 /* Both conditionals must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000834 if (pc + ftest->jt + 1 >= flen ||
835 pc + ftest->jf + 1 >= flen)
836 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000837 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200838 case BPF_LD | BPF_W | BPF_ABS:
839 case BPF_LD | BPF_H | BPF_ABS:
840 case BPF_LD | BPF_B | BPF_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000841 anc_found = false;
Daniel Borkmann34805932014-05-29 10:22:50 +0200842 if (bpf_anc_helper(ftest) & BPF_ANC)
843 anc_found = true;
844 /* Ancillary operation unknown or unsupported */
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000845 if (anc_found == false && ftest->k >= SKF_AD_OFF)
846 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Daniel Borkmann34805932014-05-29 10:22:50 +0200850 /* Last instruction must be a RET code */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000851 switch (filter[flen - 1].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200852 case BPF_RET | BPF_K:
853 case BPF_RET | BPF_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000854 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000855 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200856
Tetsuo Handacba328f2010-11-16 15:19:51 +0000857 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700860static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
861 const struct sock_fprog *fprog)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100862{
Alexei Starovoitov009937e2014-07-30 20:34:13 -0700863 unsigned int fsize = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100864 struct sock_fprog_kern *fkprog;
865
866 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
867 if (!fp->orig_prog)
868 return -ENOMEM;
869
870 fkprog = fp->orig_prog;
871 fkprog->len = fprog->len;
Daniel Borkmann658da932015-05-06 16:12:29 +0200872
873 fkprog->filter = kmemdup(fp->insns, fsize,
874 GFP_KERNEL | __GFP_NOWARN);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100875 if (!fkprog->filter) {
876 kfree(fp->orig_prog);
877 return -ENOMEM;
878 }
879
880 return 0;
881}
882
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700883static void bpf_release_orig_filter(struct bpf_prog *fp)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100884{
885 struct sock_fprog_kern *fprog = fp->orig_prog;
886
887 if (fprog) {
888 kfree(fprog->filter);
889 kfree(fprog);
890 }
891}
892
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700893static void __bpf_prog_release(struct bpf_prog *prog)
894{
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100895 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800896 bpf_prog_put(prog);
897 } else {
898 bpf_release_orig_filter(prog);
899 bpf_prog_free(prog);
900 }
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700901}
902
Pablo Neira34c5bd62014-07-29 17:36:28 +0200903static void __sk_filter_release(struct sk_filter *fp)
904{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700905 __bpf_prog_release(fp->prog);
906 kfree(fp);
Pablo Neira34c5bd62014-07-29 17:36:28 +0200907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800910 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700911 * @rcu: rcu_head that contains the sk_filter to free
912 */
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100913static void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700914{
915 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
916
Pablo Neira34c5bd62014-07-29 17:36:28 +0200917 __sk_filter_release(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700918}
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100919
920/**
921 * sk_filter_release - release a socket filter
922 * @fp: filter to remove
923 *
924 * Remove a filter from a socket and release its resources.
925 */
926static void sk_filter_release(struct sk_filter *fp)
927{
928 if (atomic_dec_and_test(&fp->refcnt))
929 call_rcu(&fp->rcu, sk_filter_release_rcu);
930}
931
932void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
933{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700934 u32 filter_size = bpf_prog_size(fp->prog->len);
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700935
936 atomic_sub(filter_size, &sk->sk_omem_alloc);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100937 sk_filter_release(fp);
938}
939
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700940/* try to charge the socket memory if there is space available
941 * return true on success
942 */
943bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100944{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700945 u32 filter_size = bpf_prog_size(fp->prog->len);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700946
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700947 /* same check as in sock_kmalloc() */
948 if (filter_size <= sysctl_optmem_max &&
949 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
950 atomic_inc(&fp->refcnt);
951 atomic_add(filter_size, &sk->sk_omem_alloc);
952 return true;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100953 }
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700954 return false;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100955}
956
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700957static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100958{
959 struct sock_filter *old_prog;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700960 struct bpf_prog *old_fp;
Daniel Borkmann34805932014-05-29 10:22:50 +0200961 int err, new_len, old_len = fp->len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100962
963 /* We are free to overwrite insns et al right here as it
964 * won't be used at this point in time anymore internally
965 * after the migration to the internal BPF instruction
966 * representation.
967 */
968 BUILD_BUG_ON(sizeof(struct sock_filter) !=
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700969 sizeof(struct bpf_insn));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100970
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100971 /* Conversion cannot happen on overlapping memory areas,
972 * so we need to keep the user BPF around until the 2nd
973 * pass. At this time, the user BPF is stored in fp->insns.
974 */
975 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
Daniel Borkmann658da932015-05-06 16:12:29 +0200976 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100977 if (!old_prog) {
978 err = -ENOMEM;
979 goto out_err;
980 }
981
982 /* 1st pass: calculate the new program length. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700983 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100984 if (err)
985 goto out_err_free;
986
987 /* Expand fp for appending the new filter representation. */
988 old_fp = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200989 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100990 if (!fp) {
991 /* The old_fp is still around in case we couldn't
992 * allocate new memory, so uncharge on that one.
993 */
994 fp = old_fp;
995 err = -ENOMEM;
996 goto out_err_free;
997 }
998
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100999 fp->len = new_len;
1000
Alexei Starovoitov2695fb52014-07-24 16:38:21 -07001001 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -07001002 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001003 if (err)
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -07001004 /* 2nd bpf_convert_filter() can fail only if it fails
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001005 * to allocate memory, remapping must succeed. Note,
1006 * that at this time old_fp has already been released
Alexei Starovoitov278571b2014-07-30 20:34:12 -07001007 * by krealloc().
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001008 */
1009 goto out_err_free;
1010
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001011 /* We are guaranteed to never error here with cBPF to eBPF
1012 * transitions, since there's no issue with type compatibility
1013 * checks on program arrays.
1014 */
1015 fp = bpf_prog_select_runtime(fp, &err);
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001016
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001017 kfree(old_prog);
1018 return fp;
1019
1020out_err_free:
1021 kfree(old_prog);
1022out_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);
1025}
1026
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001027static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1028 bpf_aux_classic_check_t trans)
Jiri Pirko302d6632012-03-31 11:01:19 +00001029{
1030 int err;
1031
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001032 fp->bpf_func = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001033 fp->jited = 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001034
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -07001035 err = bpf_check_classic(fp->insns, fp->len);
Leon Yu418c96a2014-06-01 05:37:25 +00001036 if (err) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001037 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001038 return ERR_PTR(err);
Leon Yu418c96a2014-06-01 05:37:25 +00001039 }
Jiri Pirko302d6632012-03-31 11:01:19 +00001040
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001041 /* There might be additional checks and transformations
1042 * needed on classic filters, f.e. in case of seccomp.
1043 */
1044 if (trans) {
1045 err = trans(fp->insns, fp->len);
1046 if (err) {
1047 __bpf_prog_release(fp);
1048 return ERR_PTR(err);
1049 }
1050 }
1051
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001052 /* Probe if we can JIT compile the filter and if so, do
1053 * the compilation of the filter.
1054 */
Jiri Pirko302d6632012-03-31 11:01:19 +00001055 bpf_jit_compile(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001056
1057 /* JIT compiler couldn't process this filter, so do the
1058 * internal BPF translation for the optimized interpreter.
1059 */
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001060 if (!fp->jited)
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001061 fp = bpf_migrate_filter(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001062
1063 return fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001064}
1065
1066/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001067 * bpf_prog_create - create an unattached filter
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001068 * @pfp: the unattached filter that is created
Tobias Klauser677a9fd2014-06-24 15:33:21 +02001069 * @fprog: the filter program
Jiri Pirko302d6632012-03-31 11:01:19 +00001070 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001071 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +00001072 * sanity checks on it to make sure it does not explode on us later.
1073 * If an error occurs or there is insufficient memory for the filter
1074 * a negative errno code is returned. On success the return is zero.
1075 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001076int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
Jiri Pirko302d6632012-03-31 11:01:19 +00001077{
Alexei Starovoitov009937e2014-07-30 20:34:13 -07001078 unsigned int fsize = bpf_classic_proglen(fprog);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001079 struct bpf_prog *fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001080
1081 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001082 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Jiri Pirko302d6632012-03-31 11:01:19 +00001083 return -EINVAL;
1084
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001085 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Jiri Pirko302d6632012-03-31 11:01:19 +00001086 if (!fp)
1087 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001088
Jiri Pirko302d6632012-03-31 11:01:19 +00001089 memcpy(fp->insns, fprog->filter, fsize);
1090
Jiri Pirko302d6632012-03-31 11:01:19 +00001091 fp->len = fprog->len;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001092 /* Since unattached filters are not copied back to user
1093 * space through sk_get_filter(), we do not need to hold
1094 * a copy here, and can spare us the work.
1095 */
1096 fp->orig_prog = NULL;
Jiri Pirko302d6632012-03-31 11:01:19 +00001097
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001098 /* bpf_prepare_filter() already takes care of freeing
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001099 * memory in case something goes wrong.
1100 */
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001101 fp = bpf_prepare_filter(fp, NULL);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001102 if (IS_ERR(fp))
1103 return PTR_ERR(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001104
1105 *pfp = fp;
1106 return 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001107}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001108EXPORT_SYMBOL_GPL(bpf_prog_create);
Jiri Pirko302d6632012-03-31 11:01:19 +00001109
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001110/**
1111 * bpf_prog_create_from_user - create an unattached filter from user buffer
1112 * @pfp: the unattached filter that is created
1113 * @fprog: the filter program
1114 * @trans: post-classic verifier transformation handler
Daniel Borkmannbab18992015-10-02 15:17:33 +02001115 * @save_orig: save classic BPF program
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001116 *
1117 * This function effectively does the same as bpf_prog_create(), only
1118 * that it builds up its insns buffer from user space provided buffer.
1119 * It also allows for passing a bpf_aux_classic_check_t handler.
1120 */
1121int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
Daniel Borkmannbab18992015-10-02 15:17:33 +02001122 bpf_aux_classic_check_t trans, bool save_orig)
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001123{
1124 unsigned int fsize = bpf_classic_proglen(fprog);
1125 struct bpf_prog *fp;
Daniel Borkmannbab18992015-10-02 15:17:33 +02001126 int err;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001127
1128 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001129 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001130 return -EINVAL;
1131
1132 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1133 if (!fp)
1134 return -ENOMEM;
1135
1136 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1137 __bpf_prog_free(fp);
1138 return -EFAULT;
1139 }
1140
1141 fp->len = fprog->len;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001142 fp->orig_prog = NULL;
1143
Daniel Borkmannbab18992015-10-02 15:17:33 +02001144 if (save_orig) {
1145 err = bpf_prog_store_orig_filter(fp, fprog);
1146 if (err) {
1147 __bpf_prog_free(fp);
1148 return -ENOMEM;
1149 }
1150 }
1151
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001152 /* bpf_prepare_filter() already takes care of freeing
1153 * memory in case something goes wrong.
1154 */
1155 fp = bpf_prepare_filter(fp, trans);
1156 if (IS_ERR(fp))
1157 return PTR_ERR(fp);
1158
1159 *pfp = fp;
1160 return 0;
1161}
David S. Miller2ea273d2015-08-17 14:37:06 -07001162EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001163
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001164void bpf_prog_destroy(struct bpf_prog *fp)
Jiri Pirko302d6632012-03-31 11:01:19 +00001165{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001166 __bpf_prog_release(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001167}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001168EXPORT_SYMBOL_GPL(bpf_prog_destroy);
Jiri Pirko302d6632012-03-31 11:01:19 +00001169
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001170static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001171{
1172 struct sk_filter *fp, *old_fp;
1173
1174 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1175 if (!fp)
1176 return -ENOMEM;
1177
1178 fp->prog = prog;
1179 atomic_set(&fp->refcnt, 0);
1180
1181 if (!sk_filter_charge(sk, fp)) {
1182 kfree(fp);
1183 return -ENOMEM;
1184 }
1185
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001186 old_fp = rcu_dereference_protected(sk->sk_filter,
1187 lockdep_sock_is_held(sk));
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001188 rcu_assign_pointer(sk->sk_filter, fp);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001189
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001190 if (old_fp)
1191 sk_filter_uncharge(sk, old_fp);
1192
1193 return 0;
1194}
1195
Craig Gallek538950a2016-01-04 17:41:47 -05001196static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1197{
1198 struct bpf_prog *old_prog;
1199 int err;
1200
1201 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1202 return -ENOMEM;
1203
Craig Gallekfa463492016-02-10 11:50:39 -05001204 if (sk_unhashed(sk) && sk->sk_reuseport) {
Craig Gallek538950a2016-01-04 17:41:47 -05001205 err = reuseport_alloc(sk);
1206 if (err)
1207 return err;
1208 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1209 /* The socket wasn't bound with SO_REUSEPORT */
1210 return -EINVAL;
1211 }
1212
1213 old_prog = reuseport_attach_prog(sk, prog);
1214 if (old_prog)
1215 bpf_prog_destroy(old_prog);
1216
1217 return 0;
1218}
1219
1220static
1221struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1222{
1223 unsigned int fsize = bpf_classic_proglen(fprog);
Craig Gallek538950a2016-01-04 17:41:47 -05001224 struct bpf_prog *prog;
1225 int err;
1226
1227 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1228 return ERR_PTR(-EPERM);
1229
1230 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001231 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Craig Gallek538950a2016-01-04 17:41:47 -05001232 return ERR_PTR(-EINVAL);
1233
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001234 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Craig Gallek538950a2016-01-04 17:41:47 -05001235 if (!prog)
1236 return ERR_PTR(-ENOMEM);
1237
1238 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1239 __bpf_prog_free(prog);
1240 return ERR_PTR(-EFAULT);
1241 }
1242
1243 prog->len = fprog->len;
1244
1245 err = bpf_prog_store_orig_filter(prog, fprog);
1246 if (err) {
1247 __bpf_prog_free(prog);
1248 return ERR_PTR(-ENOMEM);
1249 }
1250
1251 /* bpf_prepare_filter() already takes care of freeing
1252 * memory in case something goes wrong.
1253 */
1254 return bpf_prepare_filter(prog, NULL);
1255}
1256
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001257/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * sk_attach_filter - attach a socket filter
1259 * @fprog: the filter program
1260 * @sk: the socket to use
1261 *
1262 * Attach the user's filter code. We first run some sanity checks on
1263 * it to make sure it does not explode on us later. If an error
1264 * occurs or there is insufficient memory for the filter a negative
1265 * errno code is returned. On success the return is zero.
1266 */
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001267int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Craig Gallek538950a2016-01-04 17:41:47 -05001269 struct bpf_prog *prog = __get_filter(fprog, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 int err;
1271
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001272 if (IS_ERR(prog))
1273 return PTR_ERR(prog);
1274
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001275 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001276 if (err < 0) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001277 __bpf_prog_release(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001278 return err;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001279 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001280
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001283EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Craig Gallek538950a2016-01-04 17:41:47 -05001285int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001286{
Craig Gallek538950a2016-01-04 17:41:47 -05001287 struct bpf_prog *prog = __get_filter(fprog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001288 int err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001289
Alexei Starovoitov198bf1b2014-12-10 20:14:55 -08001290 if (IS_ERR(prog))
1291 return PTR_ERR(prog);
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001292
Craig Gallek538950a2016-01-04 17:41:47 -05001293 err = __reuseport_attach_prog(prog, sk);
1294 if (err < 0) {
1295 __bpf_prog_release(prog);
1296 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001297 }
1298
Craig Gallek538950a2016-01-04 17:41:47 -05001299 return 0;
1300}
1301
1302static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1303{
Craig Gallek538950a2016-01-04 17:41:47 -05001304 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1305 return ERR_PTR(-EPERM);
1306
Daniel Borkmann113214b2016-06-30 17:24:44 +02001307 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
Craig Gallek538950a2016-01-04 17:41:47 -05001308}
1309
1310int sk_attach_bpf(u32 ufd, struct sock *sk)
1311{
1312 struct bpf_prog *prog = __get_bpf(ufd, sk);
1313 int err;
1314
1315 if (IS_ERR(prog))
1316 return PTR_ERR(prog);
1317
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001318 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001319 if (err < 0) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001320 bpf_prog_put(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001321 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001322 }
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001323
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001324 return 0;
1325}
1326
Craig Gallek538950a2016-01-04 17:41:47 -05001327int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1328{
1329 struct bpf_prog *prog = __get_bpf(ufd, sk);
1330 int err;
1331
1332 if (IS_ERR(prog))
1333 return PTR_ERR(prog);
1334
1335 err = __reuseport_attach_prog(prog, sk);
1336 if (err < 0) {
1337 bpf_prog_put(prog);
1338 return err;
1339 }
1340
1341 return 0;
1342}
1343
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001344struct bpf_scratchpad {
1345 union {
1346 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1347 u8 buff[MAX_BPF_STACK];
1348 };
1349};
1350
1351static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001352
Daniel Borkmann5293efe2016-08-18 01:00:39 +02001353static inline int __bpf_try_make_writable(struct sk_buff *skb,
1354 unsigned int write_len)
1355{
1356 return skb_ensure_writable(skb, write_len);
1357}
1358
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001359static inline int bpf_try_make_writable(struct sk_buff *skb,
1360 unsigned int write_len)
1361{
Daniel Borkmann5293efe2016-08-18 01:00:39 +02001362 int err = __bpf_try_make_writable(skb, write_len);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001363
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001364 bpf_compute_data_end(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001365 return err;
1366}
1367
Daniel Borkmanna2bfe6b2016-08-05 00:11:11 +02001368static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1369{
1370 if (skb_at_tc_ingress(skb))
1371 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1372}
1373
Daniel Borkmann80656942016-08-05 00:11:13 +02001374static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1375{
1376 if (skb_at_tc_ingress(skb))
1377 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1378}
1379
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001380static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001381{
1382 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001383 unsigned int offset = (unsigned int) r2;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001384 void *from = (void *) (long) r3;
1385 unsigned int len = (unsigned int) r4;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001386 void *ptr;
1387
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001388 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001389 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001390 if (unlikely(offset > 0xffff))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001391 return -EFAULT;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001392 if (unlikely(bpf_try_make_writable(skb, offset + len)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001393 return -EFAULT;
1394
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001395 ptr = skb->data + offset;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001396 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmann479ffcc2016-08-05 00:11:12 +02001397 __skb_postpull_rcsum(skb, ptr, len, offset);
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001398
1399 memcpy(ptr, from, len);
1400
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001401 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmann479ffcc2016-08-05 00:11:12 +02001402 __skb_postpush_rcsum(skb, ptr, len, offset);
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001403 if (flags & BPF_F_INVALIDATE_HASH)
1404 skb_clear_hash(skb);
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001405
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001406 return 0;
1407}
1408
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001409static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001410 .func = bpf_skb_store_bytes,
1411 .gpl_only = false,
1412 .ret_type = RET_INTEGER,
1413 .arg1_type = ARG_PTR_TO_CTX,
1414 .arg2_type = ARG_ANYTHING,
1415 .arg3_type = ARG_PTR_TO_STACK,
1416 .arg4_type = ARG_CONST_STACK_SIZE,
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001417 .arg5_type = ARG_ANYTHING,
1418};
1419
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001420static u64 bpf_skb_load_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1421{
1422 const struct sk_buff *skb = (const struct sk_buff *)(unsigned long) r1;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001423 unsigned int offset = (unsigned int) r2;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001424 void *to = (void *)(unsigned long) r3;
1425 unsigned int len = (unsigned int) r4;
1426 void *ptr;
1427
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001428 if (unlikely(offset > 0xffff))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001429 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001430
1431 ptr = skb_header_pointer(skb, offset, len, to);
1432 if (unlikely(!ptr))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001433 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001434 if (ptr != to)
1435 memcpy(to, ptr, len);
1436
1437 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02001438err_clear:
1439 memset(to, 0, len);
1440 return -EFAULT;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001441}
1442
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001443static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001444 .func = bpf_skb_load_bytes,
1445 .gpl_only = false,
1446 .ret_type = RET_INTEGER,
1447 .arg1_type = ARG_PTR_TO_CTX,
1448 .arg2_type = ARG_ANYTHING,
Daniel Borkmann074f5282016-04-13 00:10:52 +02001449 .arg3_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001450 .arg4_type = ARG_CONST_STACK_SIZE,
1451};
1452
Alexei Starovoitova1661512015-04-15 12:55:45 -07001453static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001454{
1455 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001456 unsigned int offset = (unsigned int) r2;
1457 __sum16 *ptr;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001458
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001459 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1460 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001461 if (unlikely(offset > 0xffff || offset & 1))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001462 return -EFAULT;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001463 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001464 return -EFAULT;
1465
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001466 ptr = (__sum16 *)(skb->data + offset);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001467 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann8050c0f2016-03-04 15:15:02 +01001468 case 0:
1469 if (unlikely(from != 0))
1470 return -EINVAL;
1471
1472 csum_replace_by_diff(ptr, to);
1473 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001474 case 2:
1475 csum_replace2(ptr, from, to);
1476 break;
1477 case 4:
1478 csum_replace4(ptr, from, to);
1479 break;
1480 default:
1481 return -EINVAL;
1482 }
1483
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001484 return 0;
1485}
1486
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001487static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001488 .func = bpf_l3_csum_replace,
1489 .gpl_only = false,
1490 .ret_type = RET_INTEGER,
1491 .arg1_type = ARG_PTR_TO_CTX,
1492 .arg2_type = ARG_ANYTHING,
1493 .arg3_type = ARG_ANYTHING,
1494 .arg4_type = ARG_ANYTHING,
1495 .arg5_type = ARG_ANYTHING,
1496};
1497
Alexei Starovoitova1661512015-04-15 12:55:45 -07001498static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001499{
1500 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001501 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
Daniel Borkmann2f729592016-02-19 23:05:26 +01001502 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001503 unsigned int offset = (unsigned int) r2;
1504 __sum16 *ptr;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001505
Daniel Borkmann2f729592016-02-19 23:05:26 +01001506 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR |
1507 BPF_F_HDR_FIELD_MASK)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001508 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001509 if (unlikely(offset > 0xffff || offset & 1))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001510 return -EFAULT;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001511 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001512 return -EFAULT;
1513
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001514 ptr = (__sum16 *)(skb->data + offset);
Daniel Borkmann2f729592016-02-19 23:05:26 +01001515 if (is_mmzero && !*ptr)
1516 return 0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001517
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001518 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001519 case 0:
1520 if (unlikely(from != 0))
1521 return -EINVAL;
1522
1523 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1524 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001525 case 2:
1526 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1527 break;
1528 case 4:
1529 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1530 break;
1531 default:
1532 return -EINVAL;
1533 }
1534
Daniel Borkmann2f729592016-02-19 23:05:26 +01001535 if (is_mmzero && !*ptr)
1536 *ptr = CSUM_MANGLED_0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001537 return 0;
1538}
1539
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001540static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001541 .func = bpf_l4_csum_replace,
1542 .gpl_only = false,
1543 .ret_type = RET_INTEGER,
1544 .arg1_type = ARG_PTR_TO_CTX,
1545 .arg2_type = ARG_ANYTHING,
1546 .arg3_type = ARG_ANYTHING,
1547 .arg4_type = ARG_ANYTHING,
1548 .arg5_type = ARG_ANYTHING,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001549};
1550
Daniel Borkmann7d672342016-02-19 23:05:23 +01001551static u64 bpf_csum_diff(u64 r1, u64 from_size, u64 r3, u64 to_size, u64 seed)
1552{
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001553 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
Daniel Borkmann7d672342016-02-19 23:05:23 +01001554 u64 diff_size = from_size + to_size;
1555 __be32 *from = (__be32 *) (long) r1;
1556 __be32 *to = (__be32 *) (long) r3;
1557 int i, j = 0;
1558
1559 /* This is quite flexible, some examples:
1560 *
1561 * from_size == 0, to_size > 0, seed := csum --> pushing data
1562 * from_size > 0, to_size == 0, seed := csum --> pulling data
1563 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1564 *
1565 * Even for diffing, from_size and to_size don't need to be equal.
1566 */
1567 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1568 diff_size > sizeof(sp->diff)))
1569 return -EINVAL;
1570
1571 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1572 sp->diff[j] = ~from[i];
1573 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1574 sp->diff[j] = to[i];
1575
1576 return csum_partial(sp->diff, diff_size, seed);
1577}
1578
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001579static const struct bpf_func_proto bpf_csum_diff_proto = {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001580 .func = bpf_csum_diff,
1581 .gpl_only = false,
1582 .ret_type = RET_INTEGER,
1583 .arg1_type = ARG_PTR_TO_STACK,
1584 .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1585 .arg3_type = ARG_PTR_TO_STACK,
1586 .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1587 .arg5_type = ARG_ANYTHING,
1588};
1589
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001590static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1591{
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001592 return dev_forward_skb(dev, skb);
1593}
1594
1595static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1596{
1597 int ret;
1598
1599 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1600 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1601 kfree_skb(skb);
1602 return -ENETDOWN;
1603 }
1604
1605 skb->dev = dev;
1606
1607 __this_cpu_inc(xmit_recursion);
1608 ret = dev_queue_xmit(skb);
1609 __this_cpu_dec(xmit_recursion);
1610
1611 return ret;
1612}
1613
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001614static u64 bpf_clone_redirect(u64 r1, u64 ifindex, u64 flags, u64 r4, u64 r5)
1615{
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001616 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001617 struct net_device *dev;
1618
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001619 if (unlikely(flags & ~(BPF_F_INGRESS)))
1620 return -EINVAL;
1621
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001622 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1623 if (unlikely(!dev))
1624 return -EINVAL;
1625
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001626 skb = skb_clone(skb, GFP_ATOMIC);
1627 if (unlikely(!skb))
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001628 return -ENOMEM;
1629
Daniel Borkmanna2bfe6b2016-08-05 00:11:11 +02001630 bpf_push_mac_rcsum(skb);
1631
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001632 return flags & BPF_F_INGRESS ?
1633 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001634}
1635
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001636static const struct bpf_func_proto bpf_clone_redirect_proto = {
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001637 .func = bpf_clone_redirect,
1638 .gpl_only = false,
1639 .ret_type = RET_INTEGER,
1640 .arg1_type = ARG_PTR_TO_CTX,
1641 .arg2_type = ARG_ANYTHING,
1642 .arg3_type = ARG_ANYTHING,
1643};
1644
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001645struct redirect_info {
1646 u32 ifindex;
1647 u32 flags;
1648};
1649
1650static DEFINE_PER_CPU(struct redirect_info, redirect_info);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001651
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001652static u64 bpf_redirect(u64 ifindex, u64 flags, u64 r3, u64 r4, u64 r5)
1653{
1654 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1655
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001656 if (unlikely(flags & ~(BPF_F_INGRESS)))
1657 return TC_ACT_SHOT;
1658
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001659 ri->ifindex = ifindex;
1660 ri->flags = flags;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001661
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001662 return TC_ACT_REDIRECT;
1663}
1664
1665int skb_do_redirect(struct sk_buff *skb)
1666{
1667 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1668 struct net_device *dev;
1669
1670 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1671 ri->ifindex = 0;
1672 if (unlikely(!dev)) {
1673 kfree_skb(skb);
1674 return -EINVAL;
1675 }
1676
Daniel Borkmanna2bfe6b2016-08-05 00:11:11 +02001677 bpf_push_mac_rcsum(skb);
1678
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001679 return ri->flags & BPF_F_INGRESS ?
1680 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001681}
1682
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001683static const struct bpf_func_proto bpf_redirect_proto = {
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001684 .func = bpf_redirect,
1685 .gpl_only = false,
1686 .ret_type = RET_INTEGER,
1687 .arg1_type = ARG_ANYTHING,
1688 .arg2_type = ARG_ANYTHING,
1689};
1690
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001691static u64 bpf_get_cgroup_classid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1692{
1693 return task_get_classid((struct sk_buff *) (unsigned long) r1);
1694}
1695
1696static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1697 .func = bpf_get_cgroup_classid,
1698 .gpl_only = false,
1699 .ret_type = RET_INTEGER,
1700 .arg1_type = ARG_PTR_TO_CTX,
1701};
1702
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001703static u64 bpf_get_route_realm(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1704{
Daniel Borkmann808c1b62016-03-16 01:42:50 +01001705 return dst_tclassid((struct sk_buff *) (unsigned long) r1);
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001706}
1707
1708static const struct bpf_func_proto bpf_get_route_realm_proto = {
1709 .func = bpf_get_route_realm,
1710 .gpl_only = false,
1711 .ret_type = RET_INTEGER,
1712 .arg1_type = ARG_PTR_TO_CTX,
1713};
1714
Daniel Borkmann13c5c242016-07-03 01:28:47 +02001715static u64 bpf_get_hash_recalc(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1716{
1717 /* If skb_clear_hash() was called due to mangling, we can
1718 * trigger SW recalculation here. Later access to hash
1719 * can then use the inline skb->hash via context directly
1720 * instead of calling this helper again.
1721 */
1722 return skb_get_hash((struct sk_buff *) (unsigned long) r1);
1723}
1724
1725static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1726 .func = bpf_get_hash_recalc,
1727 .gpl_only = false,
1728 .ret_type = RET_INTEGER,
1729 .arg1_type = ARG_PTR_TO_CTX,
1730};
1731
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001732static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
1733{
1734 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1735 __be16 vlan_proto = (__force __be16) r2;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001736 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001737
1738 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1739 vlan_proto != htons(ETH_P_8021AD)))
1740 vlan_proto = htons(ETH_P_8021Q);
1741
Daniel Borkmann80656942016-08-05 00:11:13 +02001742 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001743 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
Daniel Borkmann80656942016-08-05 00:11:13 +02001744 bpf_pull_mac_rcsum(skb);
1745
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001746 bpf_compute_data_end(skb);
1747 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001748}
1749
1750const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1751 .func = bpf_skb_vlan_push,
1752 .gpl_only = false,
1753 .ret_type = RET_INTEGER,
1754 .arg1_type = ARG_PTR_TO_CTX,
1755 .arg2_type = ARG_ANYTHING,
1756 .arg3_type = ARG_ANYTHING,
1757};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001758EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001759
1760static u64 bpf_skb_vlan_pop(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1761{
1762 struct sk_buff *skb = (struct sk_buff *) (long) r1;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001763 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001764
Daniel Borkmann80656942016-08-05 00:11:13 +02001765 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001766 ret = skb_vlan_pop(skb);
Daniel Borkmann80656942016-08-05 00:11:13 +02001767 bpf_pull_mac_rcsum(skb);
1768
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001769 bpf_compute_data_end(skb);
1770 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001771}
1772
1773const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1774 .func = bpf_skb_vlan_pop,
1775 .gpl_only = false,
1776 .ret_type = RET_INTEGER,
1777 .arg1_type = ARG_PTR_TO_CTX,
1778};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001779EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001780
Daniel Borkmann65781712016-06-28 12:18:27 +02001781static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1782{
1783 /* Caller already did skb_cow() with len as headroom,
1784 * so no need to do it here.
1785 */
1786 skb_push(skb, len);
1787 memmove(skb->data, skb->data + len, off);
1788 memset(skb->data + off, 0, len);
1789
1790 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1791 * needed here as it does not change the skb->csum
1792 * result for checksum complete when summing over
1793 * zeroed blocks.
1794 */
1795 return 0;
1796}
1797
1798static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1799{
1800 /* skb_ensure_writable() is not needed here, as we're
1801 * already working on an uncloned skb.
1802 */
1803 if (unlikely(!pskb_may_pull(skb, off + len)))
1804 return -ENOMEM;
1805
1806 skb_postpull_rcsum(skb, skb->data + off, len);
1807 memmove(skb->data + len, skb->data, off);
1808 __skb_pull(skb, len);
1809
1810 return 0;
1811}
1812
1813static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1814{
1815 bool trans_same = skb->transport_header == skb->network_header;
1816 int ret;
1817
1818 /* There's no need for __skb_push()/__skb_pull() pair to
1819 * get to the start of the mac header as we're guaranteed
1820 * to always start from here under eBPF.
1821 */
1822 ret = bpf_skb_generic_push(skb, off, len);
1823 if (likely(!ret)) {
1824 skb->mac_header -= len;
1825 skb->network_header -= len;
1826 if (trans_same)
1827 skb->transport_header = skb->network_header;
1828 }
1829
1830 return ret;
1831}
1832
1833static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1834{
1835 bool trans_same = skb->transport_header == skb->network_header;
1836 int ret;
1837
1838 /* Same here, __skb_push()/__skb_pull() pair not needed. */
1839 ret = bpf_skb_generic_pop(skb, off, len);
1840 if (likely(!ret)) {
1841 skb->mac_header += len;
1842 skb->network_header += len;
1843 if (trans_same)
1844 skb->transport_header = skb->network_header;
1845 }
1846
1847 return ret;
1848}
1849
1850static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
1851{
1852 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1853 u32 off = skb->network_header - skb->mac_header;
1854 int ret;
1855
1856 ret = skb_cow(skb, len_diff);
1857 if (unlikely(ret < 0))
1858 return ret;
1859
1860 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
1861 if (unlikely(ret < 0))
1862 return ret;
1863
1864 if (skb_is_gso(skb)) {
1865 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
1866 * be changed into SKB_GSO_TCPV6.
1867 */
1868 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1869 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
1870 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
1871 }
1872
1873 /* Due to IPv6 header, MSS needs to be downgraded. */
1874 skb_shinfo(skb)->gso_size -= len_diff;
1875 /* Header must be checked, and gso_segs recomputed. */
1876 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1877 skb_shinfo(skb)->gso_segs = 0;
1878 }
1879
1880 skb->protocol = htons(ETH_P_IPV6);
1881 skb_clear_hash(skb);
1882
1883 return 0;
1884}
1885
1886static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
1887{
1888 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1889 u32 off = skb->network_header - skb->mac_header;
1890 int ret;
1891
1892 ret = skb_unclone(skb, GFP_ATOMIC);
1893 if (unlikely(ret < 0))
1894 return ret;
1895
1896 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
1897 if (unlikely(ret < 0))
1898 return ret;
1899
1900 if (skb_is_gso(skb)) {
1901 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
1902 * be changed into SKB_GSO_TCPV4.
1903 */
1904 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
1905 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
1906 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
1907 }
1908
1909 /* Due to IPv4 header, MSS can be upgraded. */
1910 skb_shinfo(skb)->gso_size += len_diff;
1911 /* Header must be checked, and gso_segs recomputed. */
1912 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1913 skb_shinfo(skb)->gso_segs = 0;
1914 }
1915
1916 skb->protocol = htons(ETH_P_IP);
1917 skb_clear_hash(skb);
1918
1919 return 0;
1920}
1921
1922static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
1923{
1924 __be16 from_proto = skb->protocol;
1925
1926 if (from_proto == htons(ETH_P_IP) &&
1927 to_proto == htons(ETH_P_IPV6))
1928 return bpf_skb_proto_4_to_6(skb);
1929
1930 if (from_proto == htons(ETH_P_IPV6) &&
1931 to_proto == htons(ETH_P_IP))
1932 return bpf_skb_proto_6_to_4(skb);
1933
1934 return -ENOTSUPP;
1935}
1936
1937static u64 bpf_skb_change_proto(u64 r1, u64 r2, u64 flags, u64 r4, u64 r5)
1938{
1939 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1940 __be16 proto = (__force __be16) r2;
1941 int ret;
1942
1943 if (unlikely(flags))
1944 return -EINVAL;
1945
1946 /* General idea is that this helper does the basic groundwork
1947 * needed for changing the protocol, and eBPF program fills the
1948 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
1949 * and other helpers, rather than passing a raw buffer here.
1950 *
1951 * The rationale is to keep this minimal and without a need to
1952 * deal with raw packet data. F.e. even if we would pass buffers
1953 * here, the program still needs to call the bpf_lX_csum_replace()
1954 * helpers anyway. Plus, this way we keep also separation of
1955 * concerns, since f.e. bpf_skb_store_bytes() should only take
1956 * care of stores.
1957 *
1958 * Currently, additional options and extension header space are
1959 * not supported, but flags register is reserved so we can adapt
1960 * that. For offloads, we mark packet as dodgy, so that headers
1961 * need to be verified first.
1962 */
1963 ret = bpf_skb_proto_xlat(skb, proto);
1964 bpf_compute_data_end(skb);
1965 return ret;
1966}
1967
1968static const struct bpf_func_proto bpf_skb_change_proto_proto = {
1969 .func = bpf_skb_change_proto,
1970 .gpl_only = false,
1971 .ret_type = RET_INTEGER,
1972 .arg1_type = ARG_PTR_TO_CTX,
1973 .arg2_type = ARG_ANYTHING,
1974 .arg3_type = ARG_ANYTHING,
1975};
1976
Daniel Borkmannd2485c42016-06-28 12:18:28 +02001977static u64 bpf_skb_change_type(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1978{
1979 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1980 u32 pkt_type = r2;
1981
1982 /* We only allow a restricted subset to be changed for now. */
Daniel Borkmann45c7fff2016-08-18 01:00:38 +02001983 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
1984 !skb_pkt_type_ok(pkt_type)))
Daniel Borkmannd2485c42016-06-28 12:18:28 +02001985 return -EINVAL;
1986
1987 skb->pkt_type = pkt_type;
1988 return 0;
1989}
1990
1991static const struct bpf_func_proto bpf_skb_change_type_proto = {
1992 .func = bpf_skb_change_type,
1993 .gpl_only = false,
1994 .ret_type = RET_INTEGER,
1995 .arg1_type = ARG_PTR_TO_CTX,
1996 .arg2_type = ARG_ANYTHING,
1997};
1998
Daniel Borkmann5293efe2016-08-18 01:00:39 +02001999static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2000{
2001 u32 min_len = skb_network_offset(skb);
2002
2003 if (skb_transport_header_was_set(skb))
2004 min_len = skb_transport_offset(skb);
2005 if (skb->ip_summed == CHECKSUM_PARTIAL)
2006 min_len = skb_checksum_start_offset(skb) +
2007 skb->csum_offset + sizeof(__sum16);
2008 return min_len;
2009}
2010
2011static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2012{
2013 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
2014 65536;
2015}
2016
2017static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2018{
2019 unsigned int old_len = skb->len;
2020 int ret;
2021
2022 ret = __skb_grow_rcsum(skb, new_len);
2023 if (!ret)
2024 memset(skb->data + old_len, 0, new_len - old_len);
2025 return ret;
2026}
2027
2028static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2029{
2030 return __skb_trim_rcsum(skb, new_len);
2031}
2032
2033static u64 bpf_skb_change_tail(u64 r1, u64 r2, u64 flags, u64 r4, u64 r5)
2034{
2035 struct sk_buff *skb = (struct sk_buff *)(long) r1;
2036 u32 max_len = __bpf_skb_max_len(skb);
2037 u32 min_len = __bpf_skb_min_len(skb);
2038 u32 new_len = (u32) r2;
2039 int ret;
2040
2041 if (unlikely(flags || new_len > max_len || new_len < min_len))
2042 return -EINVAL;
2043 if (skb->encapsulation)
2044 return -ENOTSUPP;
2045
2046 /* The basic idea of this helper is that it's performing the
2047 * needed work to either grow or trim an skb, and eBPF program
2048 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2049 * bpf_lX_csum_replace() and others rather than passing a raw
2050 * buffer here. This one is a slow path helper and intended
2051 * for replies with control messages.
2052 *
2053 * Like in bpf_skb_change_proto(), we want to keep this rather
2054 * minimal and without protocol specifics so that we are able
2055 * to separate concerns as in bpf_skb_store_bytes() should only
2056 * be the one responsible for writing buffers.
2057 *
2058 * It's really expected to be a slow path operation here for
2059 * control message replies, so we're implicitly linearizing,
2060 * uncloning and drop offloads from the skb by this.
2061 */
2062 ret = __bpf_try_make_writable(skb, skb->len);
2063 if (!ret) {
2064 if (new_len > skb->len)
2065 ret = bpf_skb_grow_rcsum(skb, new_len);
2066 else if (new_len < skb->len)
2067 ret = bpf_skb_trim_rcsum(skb, new_len);
2068 if (!ret && skb_is_gso(skb))
2069 skb_gso_reset(skb);
2070 }
2071
2072 bpf_compute_data_end(skb);
2073 return ret;
2074}
2075
2076static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2077 .func = bpf_skb_change_tail,
2078 .gpl_only = false,
2079 .ret_type = RET_INTEGER,
2080 .arg1_type = ARG_PTR_TO_CTX,
2081 .arg2_type = ARG_ANYTHING,
2082 .arg3_type = ARG_ANYTHING,
2083};
2084
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002085bool bpf_helper_changes_skb_data(void *func)
2086{
2087 if (func == bpf_skb_vlan_push)
2088 return true;
2089 if (func == bpf_skb_vlan_pop)
2090 return true;
Daniel Borkmann36976492016-02-19 23:05:25 +01002091 if (func == bpf_skb_store_bytes)
2092 return true;
Daniel Borkmann65781712016-06-28 12:18:27 +02002093 if (func == bpf_skb_change_proto)
2094 return true;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002095 if (func == bpf_skb_change_tail)
2096 return true;
Daniel Borkmann36976492016-02-19 23:05:25 +01002097 if (func == bpf_l3_csum_replace)
2098 return true;
2099 if (func == bpf_l4_csum_replace)
2100 return true;
2101
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002102 return false;
2103}
2104
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002105static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002106 unsigned long off, unsigned long len)
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002107{
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002108 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002109
2110 if (unlikely(!ptr))
2111 return len;
2112 if (ptr != dst_buff)
2113 memcpy(dst_buff, ptr, len);
2114
2115 return 0;
2116}
2117
2118static u64 bpf_skb_event_output(u64 r1, u64 r2, u64 flags, u64 r4,
2119 u64 meta_size)
2120{
2121 struct sk_buff *skb = (struct sk_buff *)(long) r1;
2122 struct bpf_map *map = (struct bpf_map *)(long) r2;
2123 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2124 void *meta = (void *)(long) r4;
2125
2126 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2127 return -EINVAL;
2128 if (unlikely(skb_size > skb->len))
2129 return -EFAULT;
2130
2131 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2132 bpf_skb_copy);
2133}
2134
2135static const struct bpf_func_proto bpf_skb_event_output_proto = {
2136 .func = bpf_skb_event_output,
2137 .gpl_only = true,
2138 .ret_type = RET_INTEGER,
2139 .arg1_type = ARG_PTR_TO_CTX,
2140 .arg2_type = ARG_CONST_MAP_PTR,
2141 .arg3_type = ARG_ANYTHING,
2142 .arg4_type = ARG_PTR_TO_STACK,
2143 .arg5_type = ARG_CONST_STACK_SIZE,
2144};
2145
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002146static unsigned short bpf_tunnel_key_af(u64 flags)
2147{
2148 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2149}
2150
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002151static u64 bpf_skb_get_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
2152{
2153 struct sk_buff *skb = (struct sk_buff *) (long) r1;
2154 struct bpf_tunnel_key *to = (struct bpf_tunnel_key *) (long) r2;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002155 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2156 u8 compat[sizeof(struct bpf_tunnel_key)];
Daniel Borkmann074f5282016-04-13 00:10:52 +02002157 void *to_orig = to;
2158 int err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002159
Daniel Borkmann074f5282016-04-13 00:10:52 +02002160 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2161 err = -EINVAL;
2162 goto err_clear;
2163 }
2164 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2165 err = -EPROTO;
2166 goto err_clear;
2167 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002168 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
Daniel Borkmann074f5282016-04-13 00:10:52 +02002169 err = -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002170 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002171 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002172 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002173 goto set_compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002174 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2175 /* Fixup deprecated structure layouts here, so we have
2176 * a common path later on.
2177 */
2178 if (ip_tunnel_info_af(info) != AF_INET)
Daniel Borkmann074f5282016-04-13 00:10:52 +02002179 goto err_clear;
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002180set_compat:
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002181 to = (struct bpf_tunnel_key *)compat;
2182 break;
2183 default:
Daniel Borkmann074f5282016-04-13 00:10:52 +02002184 goto err_clear;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002185 }
2186 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002187
2188 to->tunnel_id = be64_to_cpu(info->key.tun_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002189 to->tunnel_tos = info->key.tos;
2190 to->tunnel_ttl = info->key.ttl;
2191
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002192 if (flags & BPF_F_TUNINFO_IPV6) {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002193 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2194 sizeof(to->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002195 to->tunnel_label = be32_to_cpu(info->key.label);
2196 } else {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002197 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002198 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002199
2200 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
Daniel Borkmann074f5282016-04-13 00:10:52 +02002201 memcpy(to_orig, to, size);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002202
2203 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002204err_clear:
2205 memset(to_orig, 0, size);
2206 return err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002207}
2208
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002209static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002210 .func = bpf_skb_get_tunnel_key,
2211 .gpl_only = false,
2212 .ret_type = RET_INTEGER,
2213 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002214 .arg2_type = ARG_PTR_TO_RAW_STACK,
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002215 .arg3_type = ARG_CONST_STACK_SIZE,
2216 .arg4_type = ARG_ANYTHING,
2217};
2218
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002219static u64 bpf_skb_get_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
2220{
2221 struct sk_buff *skb = (struct sk_buff *) (long) r1;
2222 u8 *to = (u8 *) (long) r2;
2223 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002224 int err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002225
2226 if (unlikely(!info ||
Daniel Borkmann074f5282016-04-13 00:10:52 +02002227 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2228 err = -ENOENT;
2229 goto err_clear;
2230 }
2231 if (unlikely(size < info->options_len)) {
2232 err = -ENOMEM;
2233 goto err_clear;
2234 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002235
2236 ip_tunnel_info_opts_get(to, info);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002237 if (size > info->options_len)
2238 memset(to + info->options_len, 0, size - info->options_len);
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002239
2240 return info->options_len;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002241err_clear:
2242 memset(to, 0, size);
2243 return err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002244}
2245
2246static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2247 .func = bpf_skb_get_tunnel_opt,
2248 .gpl_only = false,
2249 .ret_type = RET_INTEGER,
2250 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002251 .arg2_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002252 .arg3_type = ARG_CONST_STACK_SIZE,
2253};
2254
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002255static struct metadata_dst __percpu *md_dst;
2256
2257static u64 bpf_skb_set_tunnel_key(u64 r1, u64 r2, u64 size, u64 flags, u64 r5)
2258{
2259 struct sk_buff *skb = (struct sk_buff *) (long) r1;
2260 struct bpf_tunnel_key *from = (struct bpf_tunnel_key *) (long) r2;
2261 struct metadata_dst *md = this_cpu_ptr(md_dst);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002262 u8 compat[sizeof(struct bpf_tunnel_key)];
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002263 struct ip_tunnel_info *info;
2264
Daniel Borkmann22080872016-03-04 15:15:05 +01002265 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2266 BPF_F_DONT_FRAGMENT)))
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002267 return -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002268 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2269 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002270 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002271 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002272 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2273 /* Fixup deprecated structure layouts here, so we have
2274 * a common path later on.
2275 */
2276 memcpy(compat, from, size);
2277 memset(compat + size, 0, sizeof(compat) - size);
2278 from = (struct bpf_tunnel_key *)compat;
2279 break;
2280 default:
2281 return -EINVAL;
2282 }
2283 }
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002284 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2285 from->tunnel_ext))
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002286 return -EINVAL;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002287
2288 skb_dst_drop(skb);
2289 dst_hold((struct dst_entry *) md);
2290 skb_dst_set(skb, (struct dst_entry *) md);
2291
2292 info = &md->u.tun_info;
2293 info->mode = IP_TUNNEL_INFO_TX;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002294
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002295 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
Daniel Borkmann22080872016-03-04 15:15:05 +01002296 if (flags & BPF_F_DONT_FRAGMENT)
2297 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2298
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002299 info->key.tun_id = cpu_to_be64(from->tunnel_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002300 info->key.tos = from->tunnel_tos;
2301 info->key.ttl = from->tunnel_ttl;
2302
2303 if (flags & BPF_F_TUNINFO_IPV6) {
2304 info->mode |= IP_TUNNEL_INFO_IPV6;
2305 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2306 sizeof(from->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002307 info->key.label = cpu_to_be32(from->tunnel_label) &
2308 IPV6_FLOWLABEL_MASK;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002309 } else {
2310 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
Daniel Borkmann2da897e2016-02-23 02:05:26 +01002311 if (flags & BPF_F_ZERO_CSUM_TX)
2312 info->key.tun_flags &= ~TUNNEL_CSUM;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002313 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002314
2315 return 0;
2316}
2317
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002318static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002319 .func = bpf_skb_set_tunnel_key,
2320 .gpl_only = false,
2321 .ret_type = RET_INTEGER,
2322 .arg1_type = ARG_PTR_TO_CTX,
2323 .arg2_type = ARG_PTR_TO_STACK,
2324 .arg3_type = ARG_CONST_STACK_SIZE,
2325 .arg4_type = ARG_ANYTHING,
2326};
2327
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002328static u64 bpf_skb_set_tunnel_opt(u64 r1, u64 r2, u64 size, u64 r4, u64 r5)
2329{
2330 struct sk_buff *skb = (struct sk_buff *) (long) r1;
2331 u8 *from = (u8 *) (long) r2;
2332 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2333 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2334
2335 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2336 return -EINVAL;
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002337 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002338 return -ENOMEM;
2339
2340 ip_tunnel_info_opts_set(info, from, size);
2341
2342 return 0;
2343}
2344
2345static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2346 .func = bpf_skb_set_tunnel_opt,
2347 .gpl_only = false,
2348 .ret_type = RET_INTEGER,
2349 .arg1_type = ARG_PTR_TO_CTX,
2350 .arg2_type = ARG_PTR_TO_STACK,
2351 .arg3_type = ARG_CONST_STACK_SIZE,
2352};
2353
2354static const struct bpf_func_proto *
2355bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002356{
2357 if (!md_dst) {
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002358 /* Race is not possible, since it's called from verifier
2359 * that is holding verifier mutex.
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002360 */
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002361 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002362 GFP_KERNEL);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002363 if (!md_dst)
2364 return NULL;
2365 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002366
2367 switch (which) {
2368 case BPF_FUNC_skb_set_tunnel_key:
2369 return &bpf_skb_set_tunnel_key_proto;
2370 case BPF_FUNC_skb_set_tunnel_opt:
2371 return &bpf_skb_set_tunnel_opt_proto;
2372 default:
2373 return NULL;
2374 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002375}
2376
Daniel Borkmann747ea552016-08-12 22:17:17 +02002377static u64 bpf_skb_under_cgroup(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002378{
2379 struct sk_buff *skb = (struct sk_buff *)(long)r1;
2380 struct bpf_map *map = (struct bpf_map *)(long)r2;
2381 struct bpf_array *array = container_of(map, struct bpf_array, map);
2382 struct cgroup *cgrp;
2383 struct sock *sk;
2384 u32 i = (u32)r3;
2385
2386 sk = skb->sk;
2387 if (!sk || !sk_fullsock(sk))
2388 return -ENOENT;
2389
2390 if (unlikely(i >= array->map.max_entries))
2391 return -E2BIG;
2392
2393 cgrp = READ_ONCE(array->ptrs[i]);
2394 if (unlikely(!cgrp))
2395 return -EAGAIN;
2396
Daniel Borkmann54fd9c22016-08-18 01:00:41 +02002397 return sk_under_cgroup_hierarchy(sk, cgrp);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002398}
2399
Daniel Borkmann747ea552016-08-12 22:17:17 +02002400static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2401 .func = bpf_skb_under_cgroup,
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002402 .gpl_only = false,
2403 .ret_type = RET_INTEGER,
2404 .arg1_type = ARG_PTR_TO_CTX,
2405 .arg2_type = ARG_CONST_MAP_PTR,
2406 .arg3_type = ARG_ANYTHING,
2407};
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002408
Daniel Borkmann4de16962016-08-18 01:00:40 +02002409static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2410 unsigned long off, unsigned long len)
2411{
2412 memcpy(dst_buff, src_buff + off, len);
2413 return 0;
2414}
2415
2416static u64 bpf_xdp_event_output(u64 r1, u64 r2, u64 flags, u64 r4,
2417 u64 meta_size)
2418{
2419 struct xdp_buff *xdp = (struct xdp_buff *)(long) r1;
2420 struct bpf_map *map = (struct bpf_map *)(long) r2;
2421 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
2422 void *meta = (void *)(long) r4;
2423
2424 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2425 return -EINVAL;
2426 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2427 return -EFAULT;
2428
2429 return bpf_event_output(map, flags, meta, meta_size, xdp, xdp_size,
2430 bpf_xdp_copy);
2431}
2432
2433static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2434 .func = bpf_xdp_event_output,
2435 .gpl_only = true,
2436 .ret_type = RET_INTEGER,
2437 .arg1_type = ARG_PTR_TO_CTX,
2438 .arg2_type = ARG_CONST_MAP_PTR,
2439 .arg3_type = ARG_ANYTHING,
2440 .arg4_type = ARG_PTR_TO_STACK,
2441 .arg5_type = ARG_CONST_STACK_SIZE,
2442};
2443
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002444static const struct bpf_func_proto *
2445sk_filter_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002446{
2447 switch (func_id) {
2448 case BPF_FUNC_map_lookup_elem:
2449 return &bpf_map_lookup_elem_proto;
2450 case BPF_FUNC_map_update_elem:
2451 return &bpf_map_update_elem_proto;
2452 case BPF_FUNC_map_delete_elem:
2453 return &bpf_map_delete_elem_proto;
Daniel Borkmann03e69b52015-03-14 02:27:16 +01002454 case BPF_FUNC_get_prandom_u32:
2455 return &bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01002456 case BPF_FUNC_get_smp_processor_id:
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002457 return &bpf_get_raw_smp_processor_id_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07002458 case BPF_FUNC_tail_call:
2459 return &bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02002460 case BPF_FUNC_ktime_get_ns:
2461 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07002462 case BPF_FUNC_trace_printk:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002463 if (capable(CAP_SYS_ADMIN))
2464 return bpf_get_trace_printk_proto();
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002465 default:
2466 return NULL;
2467 }
2468}
2469
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002470static const struct bpf_func_proto *
2471tc_cls_act_func_proto(enum bpf_func_id func_id)
2472{
2473 switch (func_id) {
2474 case BPF_FUNC_skb_store_bytes:
2475 return &bpf_skb_store_bytes_proto;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01002476 case BPF_FUNC_skb_load_bytes:
2477 return &bpf_skb_load_bytes_proto;
Daniel Borkmann7d672342016-02-19 23:05:23 +01002478 case BPF_FUNC_csum_diff:
2479 return &bpf_csum_diff_proto;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07002480 case BPF_FUNC_l3_csum_replace:
2481 return &bpf_l3_csum_replace_proto;
2482 case BPF_FUNC_l4_csum_replace:
2483 return &bpf_l4_csum_replace_proto;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07002484 case BPF_FUNC_clone_redirect:
2485 return &bpf_clone_redirect_proto;
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02002486 case BPF_FUNC_get_cgroup_classid:
2487 return &bpf_get_cgroup_classid_proto;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002488 case BPF_FUNC_skb_vlan_push:
2489 return &bpf_skb_vlan_push_proto;
2490 case BPF_FUNC_skb_vlan_pop:
2491 return &bpf_skb_vlan_pop_proto;
Daniel Borkmann65781712016-06-28 12:18:27 +02002492 case BPF_FUNC_skb_change_proto:
2493 return &bpf_skb_change_proto_proto;
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002494 case BPF_FUNC_skb_change_type:
2495 return &bpf_skb_change_type_proto;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002496 case BPF_FUNC_skb_change_tail:
2497 return &bpf_skb_change_tail_proto;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002498 case BPF_FUNC_skb_get_tunnel_key:
2499 return &bpf_skb_get_tunnel_key_proto;
2500 case BPF_FUNC_skb_set_tunnel_key:
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002501 return bpf_get_skb_set_tunnel_proto(func_id);
2502 case BPF_FUNC_skb_get_tunnel_opt:
2503 return &bpf_skb_get_tunnel_opt_proto;
2504 case BPF_FUNC_skb_set_tunnel_opt:
2505 return bpf_get_skb_set_tunnel_proto(func_id);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07002506 case BPF_FUNC_redirect:
2507 return &bpf_redirect_proto;
Daniel Borkmannc46646d2015-09-30 01:41:51 +02002508 case BPF_FUNC_get_route_realm:
2509 return &bpf_get_route_realm_proto;
Daniel Borkmann13c5c242016-07-03 01:28:47 +02002510 case BPF_FUNC_get_hash_recalc:
2511 return &bpf_get_hash_recalc_proto;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02002512 case BPF_FUNC_perf_event_output:
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002513 return &bpf_skb_event_output_proto;
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002514 case BPF_FUNC_get_smp_processor_id:
2515 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann747ea552016-08-12 22:17:17 +02002516 case BPF_FUNC_skb_under_cgroup:
2517 return &bpf_skb_under_cgroup_proto;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002518 default:
2519 return sk_filter_func_proto(func_id);
2520 }
2521}
2522
Brenden Blanco6a773a12016-07-19 12:16:47 -07002523static const struct bpf_func_proto *
2524xdp_func_proto(enum bpf_func_id func_id)
2525{
Daniel Borkmann4de16962016-08-18 01:00:40 +02002526 switch (func_id) {
2527 case BPF_FUNC_perf_event_output:
2528 return &bpf_xdp_event_output_proto;
2529 default:
2530 return sk_filter_func_proto(func_id);
2531 }
Brenden Blanco6a773a12016-07-19 12:16:47 -07002532}
2533
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002534static bool __is_valid_access(int off, int size, enum bpf_access_type type)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002535{
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002536 if (off < 0 || off >= sizeof(struct __sk_buff))
2537 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002538 /* The verifier guarantees that size > 0. */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002539 if (off % size != 0)
2540 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002541 if (size != sizeof(__u32))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002542 return false;
2543
2544 return true;
2545}
2546
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002547static bool sk_filter_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002548 enum bpf_access_type type,
2549 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002550{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002551 switch (off) {
2552 case offsetof(struct __sk_buff, tc_classid):
2553 case offsetof(struct __sk_buff, data):
2554 case offsetof(struct __sk_buff, data_end):
Daniel Borkmann045efa82015-09-15 23:05:42 -07002555 return false;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002556 }
Daniel Borkmann045efa82015-09-15 23:05:42 -07002557
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002558 if (type == BPF_WRITE) {
2559 switch (off) {
2560 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann4936e352016-05-13 19:08:26 +02002561 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002562 break;
2563 default:
2564 return false;
2565 }
2566 }
2567
2568 return __is_valid_access(off, size, type);
2569}
2570
2571static bool tc_cls_act_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002572 enum bpf_access_type type,
2573 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002574{
2575 if (type == BPF_WRITE) {
2576 switch (off) {
2577 case offsetof(struct __sk_buff, mark):
2578 case offsetof(struct __sk_buff, tc_index):
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002579 case offsetof(struct __sk_buff, priority):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002580 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002581 offsetof(struct __sk_buff, cb[4]):
2582 case offsetof(struct __sk_buff, tc_classid):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002583 break;
2584 default:
2585 return false;
2586 }
2587 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002588
2589 switch (off) {
2590 case offsetof(struct __sk_buff, data):
2591 *reg_type = PTR_TO_PACKET;
2592 break;
2593 case offsetof(struct __sk_buff, data_end):
2594 *reg_type = PTR_TO_PACKET_END;
2595 break;
2596 }
2597
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002598 return __is_valid_access(off, size, type);
2599}
2600
Brenden Blanco6a773a12016-07-19 12:16:47 -07002601static bool __is_valid_xdp_access(int off, int size,
2602 enum bpf_access_type type)
2603{
2604 if (off < 0 || off >= sizeof(struct xdp_md))
2605 return false;
2606 if (off % size != 0)
2607 return false;
2608 if (size != 4)
2609 return false;
2610
2611 return true;
2612}
2613
2614static bool xdp_is_valid_access(int off, int size,
2615 enum bpf_access_type type,
2616 enum bpf_reg_type *reg_type)
2617{
2618 if (type == BPF_WRITE)
2619 return false;
2620
2621 switch (off) {
2622 case offsetof(struct xdp_md, data):
2623 *reg_type = PTR_TO_PACKET;
2624 break;
2625 case offsetof(struct xdp_md, data_end):
2626 *reg_type = PTR_TO_PACKET_END;
2627 break;
2628 }
2629
2630 return __is_valid_xdp_access(off, size, type);
2631}
2632
2633void bpf_warn_invalid_xdp_action(u32 act)
2634{
2635 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
2636}
2637EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
2638
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002639static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2640 int src_reg, int ctx_off,
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002641 struct bpf_insn *insn_buf,
2642 struct bpf_prog *prog)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002643{
2644 struct bpf_insn *insn = insn_buf;
2645
2646 switch (ctx_off) {
2647 case offsetof(struct __sk_buff, len):
2648 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2649
2650 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2651 offsetof(struct sk_buff, len));
2652 break;
2653
Daniel Borkmann0b8c7072015-03-19 19:38:27 +01002654 case offsetof(struct __sk_buff, protocol):
2655 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2656
2657 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2658 offsetof(struct sk_buff, protocol));
2659 break;
2660
Michal Sekletar27cd5452015-03-24 14:48:41 +01002661 case offsetof(struct __sk_buff, vlan_proto):
2662 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2663
2664 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2665 offsetof(struct sk_buff, vlan_proto));
2666 break;
2667
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002668 case offsetof(struct __sk_buff, priority):
2669 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2670
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002671 if (type == BPF_WRITE)
2672 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2673 offsetof(struct sk_buff, priority));
2674 else
2675 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2676 offsetof(struct sk_buff, priority));
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002677 break;
2678
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002679 case offsetof(struct __sk_buff, ingress_ifindex):
2680 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2681
2682 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2683 offsetof(struct sk_buff, skb_iif));
2684 break;
2685
2686 case offsetof(struct __sk_buff, ifindex):
2687 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2688
2689 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, dev)),
2690 dst_reg, src_reg,
2691 offsetof(struct sk_buff, dev));
2692 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2693 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2694 offsetof(struct net_device, ifindex));
2695 break;
2696
Daniel Borkmannba7591d2015-08-01 00:46:29 +02002697 case offsetof(struct __sk_buff, hash):
2698 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2699
2700 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2701 offsetof(struct sk_buff, hash));
2702 break;
2703
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002704 case offsetof(struct __sk_buff, mark):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002705 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2706
2707 if (type == BPF_WRITE)
2708 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2709 offsetof(struct sk_buff, mark));
2710 else
2711 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2712 offsetof(struct sk_buff, mark));
2713 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002714
2715 case offsetof(struct __sk_buff, pkt_type):
2716 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2717
2718 case offsetof(struct __sk_buff, queue_mapping):
2719 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002720
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002721 case offsetof(struct __sk_buff, vlan_present):
2722 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2723 dst_reg, src_reg, insn);
2724
2725 case offsetof(struct __sk_buff, vlan_tci):
2726 return convert_skb_access(SKF_AD_VLAN_TAG,
2727 dst_reg, src_reg, insn);
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002728
2729 case offsetof(struct __sk_buff, cb[0]) ...
2730 offsetof(struct __sk_buff, cb[4]):
2731 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2732
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002733 prog->cb_access = 1;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002734 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2735 ctx_off += offsetof(struct sk_buff, cb);
2736 ctx_off += offsetof(struct qdisc_skb_cb, data);
2737 if (type == BPF_WRITE)
2738 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2739 else
2740 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2741 break;
2742
Daniel Borkmann045efa82015-09-15 23:05:42 -07002743 case offsetof(struct __sk_buff, tc_classid):
2744 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2745 ctx_off += offsetof(struct sk_buff, cb);
2746 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002747 if (type == BPF_WRITE)
2748 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2749 else
2750 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
Daniel Borkmann045efa82015-09-15 23:05:42 -07002751 break;
2752
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002753 case offsetof(struct __sk_buff, data):
2754 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct sk_buff, data)),
2755 dst_reg, src_reg,
2756 offsetof(struct sk_buff, data));
2757 break;
2758
2759 case offsetof(struct __sk_buff, data_end):
2760 ctx_off -= offsetof(struct __sk_buff, data_end);
2761 ctx_off += offsetof(struct sk_buff, cb);
2762 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
2763 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(sizeof(void *)),
2764 dst_reg, src_reg, ctx_off);
2765 break;
2766
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002767 case offsetof(struct __sk_buff, tc_index):
2768#ifdef CONFIG_NET_SCHED
2769 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2770
2771 if (type == BPF_WRITE)
2772 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2773 offsetof(struct sk_buff, tc_index));
2774 else
2775 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2776 offsetof(struct sk_buff, tc_index));
2777 break;
2778#else
2779 if (type == BPF_WRITE)
2780 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2781 else
2782 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2783 break;
2784#endif
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002785 }
2786
2787 return insn - insn_buf;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002788}
2789
Brenden Blanco6a773a12016-07-19 12:16:47 -07002790static u32 xdp_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2791 int src_reg, int ctx_off,
2792 struct bpf_insn *insn_buf,
2793 struct bpf_prog *prog)
2794{
2795 struct bpf_insn *insn = insn_buf;
2796
2797 switch (ctx_off) {
2798 case offsetof(struct xdp_md, data):
2799 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct xdp_buff, data)),
2800 dst_reg, src_reg,
2801 offsetof(struct xdp_buff, data));
2802 break;
2803 case offsetof(struct xdp_md, data_end):
2804 *insn++ = BPF_LDX_MEM(bytes_to_bpf_size(FIELD_SIZEOF(struct xdp_buff, data_end)),
2805 dst_reg, src_reg,
2806 offsetof(struct xdp_buff, data_end));
2807 break;
2808 }
2809
2810 return insn - insn_buf;
2811}
2812
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002813static const struct bpf_verifier_ops sk_filter_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002814 .get_func_proto = sk_filter_func_proto,
2815 .is_valid_access = sk_filter_is_valid_access,
2816 .convert_ctx_access = bpf_net_convert_ctx_access,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002817};
2818
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002819static const struct bpf_verifier_ops tc_cls_act_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002820 .get_func_proto = tc_cls_act_func_proto,
2821 .is_valid_access = tc_cls_act_is_valid_access,
2822 .convert_ctx_access = bpf_net_convert_ctx_access,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002823};
2824
Brenden Blanco6a773a12016-07-19 12:16:47 -07002825static const struct bpf_verifier_ops xdp_ops = {
2826 .get_func_proto = xdp_func_proto,
2827 .is_valid_access = xdp_is_valid_access,
2828 .convert_ctx_access = xdp_convert_ctx_access,
2829};
2830
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002831static struct bpf_prog_type_list sk_filter_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002832 .ops = &sk_filter_ops,
2833 .type = BPF_PROG_TYPE_SOCKET_FILTER,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002834};
2835
Daniel Borkmann96be4322015-03-01 12:31:46 +01002836static struct bpf_prog_type_list sched_cls_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002837 .ops = &tc_cls_act_ops,
2838 .type = BPF_PROG_TYPE_SCHED_CLS,
Daniel Borkmann96be4322015-03-01 12:31:46 +01002839};
2840
Daniel Borkmann94caee82015-03-20 15:11:11 +01002841static struct bpf_prog_type_list sched_act_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002842 .ops = &tc_cls_act_ops,
2843 .type = BPF_PROG_TYPE_SCHED_ACT,
Daniel Borkmann94caee82015-03-20 15:11:11 +01002844};
2845
Brenden Blanco6a773a12016-07-19 12:16:47 -07002846static struct bpf_prog_type_list xdp_type __read_mostly = {
2847 .ops = &xdp_ops,
2848 .type = BPF_PROG_TYPE_XDP,
2849};
2850
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002851static int __init register_sk_filter_ops(void)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002852{
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002853 bpf_register_prog_type(&sk_filter_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01002854 bpf_register_prog_type(&sched_cls_type);
Daniel Borkmann94caee82015-03-20 15:11:11 +01002855 bpf_register_prog_type(&sched_act_type);
Brenden Blanco6a773a12016-07-19 12:16:47 -07002856 bpf_register_prog_type(&xdp_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01002857
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002858 return 0;
2859}
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002860late_initcall(register_sk_filter_ops);
2861
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002862int sk_detach_filter(struct sock *sk)
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002863{
2864 int ret = -ENOENT;
2865 struct sk_filter *filter;
2866
Vincent Bernatd59577b2013-01-16 22:55:49 +01002867 if (sock_flag(sk, SOCK_FILTER_LOCKED))
2868 return -EPERM;
2869
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002870 filter = rcu_dereference_protected(sk->sk_filter,
2871 lockdep_sock_is_held(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002872 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002873 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -08002874 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002875 ret = 0;
2876 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002877
Pavel Emelyanov55b33322007-10-17 21:21:26 -07002878 return ret;
2879}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002880EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002881
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002882int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
2883 unsigned int len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002884{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002885 struct sock_fprog_kern *fprog;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002886 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002887 int ret = 0;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002888
2889 lock_sock(sk);
2890 filter = rcu_dereference_protected(sk->sk_filter,
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02002891 lockdep_sock_is_held(sk));
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002892 if (!filter)
2893 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002894
2895 /* We're copying the filter that has been originally attached,
Daniel Borkmann93d08b62015-10-02 12:06:03 +02002896 * so no conversion/decode needed anymore. eBPF programs that
2897 * have no original program cannot be dumped through this.
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002898 */
Daniel Borkmann93d08b62015-10-02 12:06:03 +02002899 ret = -EACCES;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07002900 fprog = filter->prog->orig_prog;
Daniel Borkmann93d08b62015-10-02 12:06:03 +02002901 if (!fprog)
2902 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002903
2904 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002905 if (!len)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002906 /* User space only enquires number of filter blocks. */
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002907 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002908
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002909 ret = -EINVAL;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002910 if (len < fprog->len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002911 goto out;
2912
2913 ret = -EFAULT;
Alexei Starovoitov009937e2014-07-30 20:34:13 -07002914 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002915 goto out;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002916
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01002917 /* Instead of bytes, the API requests to return the number
2918 * of filter blocks.
2919 */
2920 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00002921out:
2922 release_sock(sk);
2923 return ret;
2924}