blob: b391209838efa914948a2c178661089c4eb124d5 [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 Borkmannf3694e02016-09-09 02:45:31 +020097BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010098{
Daniel Borkmannf3694e02016-09-09 02:45:31 +020099 return skb_get_poff(skb);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100100}
101
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200102BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100103{
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100104 struct nlattr *nla;
105
106 if (skb_is_nonlinear(skb))
107 return 0;
108
Mathias Krause05ab8f22014-04-13 18:23:33 +0200109 if (skb->len < sizeof(struct nlattr))
110 return 0;
111
Daniel Borkmann30743832014-05-01 18:34:19 +0200112 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100113 return 0;
114
Daniel Borkmann30743832014-05-01 18:34:19 +0200115 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100116 if (nla)
117 return (void *) nla - (void *) skb->data;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return 0;
120}
121
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200122BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100123{
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100124 struct nlattr *nla;
125
126 if (skb_is_nonlinear(skb))
127 return 0;
128
Mathias Krause05ab8f22014-04-13 18:23:33 +0200129 if (skb->len < sizeof(struct nlattr))
130 return 0;
131
Daniel Borkmann30743832014-05-01 18:34:19 +0200132 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100133 return 0;
134
Daniel Borkmann30743832014-05-01 18:34:19 +0200135 nla = (struct nlattr *) &skb->data[a];
136 if (nla->nla_len > skb->len - a)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100137 return 0;
138
Daniel Borkmann30743832014-05-01 18:34:19 +0200139 nla = nla_find_nested(nla, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100140 if (nla)
141 return (void *) nla - (void *) skb->data;
142
143 return 0;
144}
145
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200146BPF_CALL_0(__get_raw_cpu_id)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100147{
148 return raw_smp_processor_id();
149}
150
Daniel Borkmann80b48c42016-06-28 12:18:26 +0200151static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
152 .func = __get_raw_cpu_id,
153 .gpl_only = false,
154 .ret_type = RET_INTEGER,
155};
156
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700157static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
158 struct bpf_insn *insn_buf)
159{
160 struct bpf_insn *insn = insn_buf;
161
162 switch (skb_field) {
163 case SKF_AD_MARK:
164 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
165
166 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
167 offsetof(struct sk_buff, mark));
168 break;
169
170 case SKF_AD_PKTTYPE:
171 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
172 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
173#ifdef __BIG_ENDIAN_BITFIELD
174 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
175#endif
176 break;
177
178 case SKF_AD_QUEUE:
179 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
180
181 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
182 offsetof(struct sk_buff, queue_mapping));
183 break;
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700184
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700185 case SKF_AD_VLAN_TAG:
186 case SKF_AD_VLAN_TAG_PRESENT:
187 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
188 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
189
190 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
191 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
192 offsetof(struct sk_buff, vlan_tci));
193 if (skb_field == SKF_AD_VLAN_TAG) {
194 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
195 ~VLAN_TAG_PRESENT);
196 } else {
197 /* dst_reg >>= 12 */
198 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
199 /* dst_reg &= 1 */
200 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
201 }
202 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700203 }
204
205 return insn - insn_buf;
206}
207
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100208static bool convert_bpf_extensions(struct sock_filter *fp,
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700209 struct bpf_insn **insnp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100210{
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700211 struct bpf_insn *insn = *insnp;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700212 u32 cnt;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100213
214 switch (fp->k) {
215 case SKF_AD_OFF + SKF_AD_PROTOCOL:
Daniel Borkmann0b8c7072015-03-19 19:38:27 +0100216 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
217
218 /* A = *(u16 *) (CTX + offsetof(protocol)) */
219 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
220 offsetof(struct sk_buff, protocol));
221 /* A = ntohs(A) [emitting a nop or swap16] */
222 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100223 break;
224
225 case SKF_AD_OFF + SKF_AD_PKTTYPE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700226 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
227 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100228 break;
229
230 case SKF_AD_OFF + SKF_AD_IFINDEX:
231 case SKF_AD_OFF + SKF_AD_HATYPE:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100232 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
233 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
234
Daniel Borkmannf035a512016-09-09 02:45:29 +0200235 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200236 BPF_REG_TMP, BPF_REG_CTX,
237 offsetof(struct sk_buff, dev));
238 /* if (tmp != 0) goto pc + 1 */
239 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
240 *insn++ = BPF_EXIT_INSN();
241 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
242 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
243 offsetof(struct net_device, ifindex));
244 else
245 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
246 offsetof(struct net_device, type));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100247 break;
248
249 case SKF_AD_OFF + SKF_AD_MARK:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700250 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
251 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100252 break;
253
254 case SKF_AD_OFF + SKF_AD_RXHASH:
255 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
256
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700257 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
258 offsetof(struct sk_buff, hash));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100259 break;
260
261 case SKF_AD_OFF + SKF_AD_QUEUE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700262 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
263 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100264 break;
265
266 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700267 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
268 BPF_REG_A, BPF_REG_CTX, insn);
269 insn += cnt - 1;
270 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100271
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700272 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
273 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
274 BPF_REG_A, BPF_REG_CTX, insn);
275 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100276 break;
277
Michal Sekletar27cd5452015-03-24 14:48:41 +0100278 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
279 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
280
281 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
282 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
283 offsetof(struct sk_buff, vlan_proto));
284 /* A = ntohs(A) [emitting a nop or swap16] */
285 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
286 break;
287
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100288 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
289 case SKF_AD_OFF + SKF_AD_NLATTR:
290 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
291 case SKF_AD_OFF + SKF_AD_CPU:
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700292 case SKF_AD_OFF + SKF_AD_RANDOM:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700293 /* arg1 = CTX */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200294 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100295 /* arg2 = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200296 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100297 /* arg3 = X */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200298 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
Alexei Starovoitove430f342014-06-06 14:46:06 -0700299 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100300 switch (fp->k) {
301 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200302 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100303 break;
304 case SKF_AD_OFF + SKF_AD_NLATTR:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200305 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100306 break;
307 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200308 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100309 break;
310 case SKF_AD_OFF + SKF_AD_CPU:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200311 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100312 break;
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700313 case SKF_AD_OFF + SKF_AD_RANDOM:
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200314 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
315 bpf_user_rnd_init_once();
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700316 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100317 }
318 break;
319
320 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700321 /* A ^= X */
322 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100323 break;
324
325 default:
326 /* This is just a dummy call to avoid letting the compiler
327 * evict __bpf_call_base() as an optimization. Placed here
328 * where no-one bothers.
329 */
330 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
331 return false;
332 }
333
334 *insnp = insn;
335 return true;
336}
337
338/**
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700339 * bpf_convert_filter - convert filter program
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100340 * @prog: the user passed filter program
341 * @len: the length of the user passed filter program
342 * @new_prog: buffer where converted program will be stored
343 * @new_len: pointer to store length of converted program
344 *
345 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
346 * Conversion workflow:
347 *
348 * 1) First pass for calculating the new program length:
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700349 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100350 *
351 * 2) 2nd pass to remap in two passes: 1st pass finds new
352 * jump offsets, 2nd pass remapping:
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700353 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700354 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100355 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200356static int bpf_convert_filter(struct sock_filter *prog, int len,
357 struct bpf_insn *new_prog, int *new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100358{
359 int new_flen = 0, pass = 0, target, i;
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700360 struct bpf_insn *new_insn;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100361 struct sock_filter *fp;
362 int *addrs = NULL;
363 u8 bpf_src;
364
365 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
Daniel Borkmann30743832014-05-01 18:34:19 +0200366 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100367
Kees Cook6f9a0932014-06-18 15:34:57 -0700368 if (len <= 0 || len > BPF_MAXINSNS)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100369 return -EINVAL;
370
371 if (new_prog) {
Daniel Borkmann658da932015-05-06 16:12:29 +0200372 addrs = kcalloc(len, sizeof(*addrs),
373 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100374 if (!addrs)
375 return -ENOMEM;
376 }
377
378do_pass:
379 new_insn = new_prog;
380 fp = prog;
381
Daniel Borkmann8b614ae2015-12-17 23:51:54 +0100382 /* Classic BPF related prologue emission. */
383 if (new_insn) {
384 /* Classic BPF expects A and X to be reset first. These need
385 * to be guaranteed to be the first two instructions.
386 */
387 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
388 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
389
390 /* All programs must keep CTX in callee saved BPF_REG_CTX.
391 * In eBPF case it's done by the compiler, here we need to
392 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
393 */
394 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
395 } else {
396 new_insn += 3;
397 }
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100398
399 for (i = 0; i < len; fp++, i++) {
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700400 struct bpf_insn tmp_insns[6] = { };
401 struct bpf_insn *insn = tmp_insns;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100402
403 if (addrs)
404 addrs[i] = new_insn - new_prog;
405
406 switch (fp->code) {
407 /* All arithmetic insns and skb loads map as-is. */
408 case BPF_ALU | BPF_ADD | BPF_X:
409 case BPF_ALU | BPF_ADD | BPF_K:
410 case BPF_ALU | BPF_SUB | BPF_X:
411 case BPF_ALU | BPF_SUB | BPF_K:
412 case BPF_ALU | BPF_AND | BPF_X:
413 case BPF_ALU | BPF_AND | BPF_K:
414 case BPF_ALU | BPF_OR | BPF_X:
415 case BPF_ALU | BPF_OR | BPF_K:
416 case BPF_ALU | BPF_LSH | BPF_X:
417 case BPF_ALU | BPF_LSH | BPF_K:
418 case BPF_ALU | BPF_RSH | BPF_X:
419 case BPF_ALU | BPF_RSH | BPF_K:
420 case BPF_ALU | BPF_XOR | BPF_X:
421 case BPF_ALU | BPF_XOR | BPF_K:
422 case BPF_ALU | BPF_MUL | BPF_X:
423 case BPF_ALU | BPF_MUL | BPF_K:
424 case BPF_ALU | BPF_DIV | BPF_X:
425 case BPF_ALU | BPF_DIV | BPF_K:
426 case BPF_ALU | BPF_MOD | BPF_X:
427 case BPF_ALU | BPF_MOD | BPF_K:
428 case BPF_ALU | BPF_NEG:
429 case BPF_LD | BPF_ABS | BPF_W:
430 case BPF_LD | BPF_ABS | BPF_H:
431 case BPF_LD | BPF_ABS | BPF_B:
432 case BPF_LD | BPF_IND | BPF_W:
433 case BPF_LD | BPF_IND | BPF_H:
434 case BPF_LD | BPF_IND | BPF_B:
435 /* Check for overloaded BPF extension and
436 * directly convert it if found, otherwise
437 * just move on with mapping.
438 */
439 if (BPF_CLASS(fp->code) == BPF_LD &&
440 BPF_MODE(fp->code) == BPF_ABS &&
441 convert_bpf_extensions(fp, &insn))
442 break;
443
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200444 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100445 break;
446
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200447 /* Jump transformation cannot use BPF block macros
448 * everywhere as offset calculation and target updates
449 * require a bit more work than the rest, i.e. jump
450 * opcodes map as-is, but offsets need adjustment.
451 */
452
453#define BPF_EMIT_JMP \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100454 do { \
455 if (target >= len || target < 0) \
456 goto err; \
457 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
458 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
459 insn->off -= insn - tmp_insns; \
460 } while (0)
461
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200462 case BPF_JMP | BPF_JA:
463 target = i + fp->k + 1;
464 insn->code = fp->code;
465 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100466 break;
467
468 case BPF_JMP | BPF_JEQ | BPF_K:
469 case BPF_JMP | BPF_JEQ | BPF_X:
470 case BPF_JMP | BPF_JSET | BPF_K:
471 case BPF_JMP | BPF_JSET | BPF_X:
472 case BPF_JMP | BPF_JGT | BPF_K:
473 case BPF_JMP | BPF_JGT | BPF_X:
474 case BPF_JMP | BPF_JGE | BPF_K:
475 case BPF_JMP | BPF_JGE | BPF_X:
476 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
477 /* BPF immediates are signed, zero extend
478 * immediate into tmp register and use it
479 * in compare insn.
480 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200481 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100482
Alexei Starovoitove430f342014-06-06 14:46:06 -0700483 insn->dst_reg = BPF_REG_A;
484 insn->src_reg = BPF_REG_TMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100485 bpf_src = BPF_X;
486 } else {
Alexei Starovoitove430f342014-06-06 14:46:06 -0700487 insn->dst_reg = BPF_REG_A;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100488 insn->imm = fp->k;
489 bpf_src = BPF_SRC(fp->code);
Tycho Andersen19539ce2015-09-10 18:25:07 -0600490 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100491 }
492
493 /* Common case where 'jump_false' is next insn. */
494 if (fp->jf == 0) {
495 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
496 target = i + fp->jt + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200497 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100498 break;
499 }
500
501 /* Convert JEQ into JNE when 'jump_true' is next insn. */
502 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
503 insn->code = BPF_JMP | BPF_JNE | bpf_src;
504 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200505 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100506 break;
507 }
508
509 /* Other jumps are mapped into two insns: Jxx and JA. */
510 target = i + fp->jt + 1;
511 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200512 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100513 insn++;
514
515 insn->code = BPF_JMP | BPF_JA;
516 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200517 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100518 break;
519
520 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
521 case BPF_LDX | BPF_MSH | BPF_B:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700522 /* tmp = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200523 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
David S. Miller1268e252014-05-13 13:13:33 -0400524 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200525 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700526 /* A &= 0xf */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200527 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700528 /* A <<= 2 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200529 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700530 /* X = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200531 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700532 /* A = tmp */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200533 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100534 break;
535
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100536 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
537 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
538 */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100539 case BPF_RET | BPF_A:
540 case BPF_RET | BPF_K:
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100541 if (BPF_RVAL(fp->code) == BPF_K)
542 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
543 0, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700544 *insn = BPF_EXIT_INSN();
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100545 break;
546
547 /* Store to stack. */
548 case BPF_ST:
549 case BPF_STX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200550 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
551 BPF_ST ? BPF_REG_A : BPF_REG_X,
552 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100553 break;
554
555 /* Load from stack. */
556 case BPF_LD | BPF_MEM:
557 case BPF_LDX | BPF_MEM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200558 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
559 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
560 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100561 break;
562
563 /* A = K or X = K */
564 case BPF_LD | BPF_IMM:
565 case BPF_LDX | BPF_IMM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200566 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
567 BPF_REG_A : BPF_REG_X, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100568 break;
569
570 /* X = A */
571 case BPF_MISC | BPF_TAX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200572 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100573 break;
574
575 /* A = X */
576 case BPF_MISC | BPF_TXA:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200577 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100578 break;
579
580 /* A = skb->len or X = skb->len */
581 case BPF_LD | BPF_W | BPF_LEN:
582 case BPF_LDX | BPF_W | BPF_LEN:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200583 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
584 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
585 offsetof(struct sk_buff, len));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100586 break;
587
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200588 /* Access seccomp_data fields. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100589 case BPF_LDX | BPF_ABS | BPF_W:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700590 /* A = *(u32 *) (ctx + K) */
591 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100592 break;
593
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -0500594 /* Unknown instruction. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100595 default:
596 goto err;
597 }
598
599 insn++;
600 if (new_prog)
601 memcpy(new_insn, tmp_insns,
602 sizeof(*insn) * (insn - tmp_insns));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100603 new_insn += insn - tmp_insns;
604 }
605
606 if (!new_prog) {
607 /* Only calculating new length. */
608 *new_len = new_insn - new_prog;
609 return 0;
610 }
611
612 pass++;
613 if (new_flen != new_insn - new_prog) {
614 new_flen = new_insn - new_prog;
615 if (pass > 2)
616 goto err;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100617 goto do_pass;
618 }
619
620 kfree(addrs);
621 BUG_ON(*new_len != new_flen);
622 return 0;
623err:
624 kfree(addrs);
625 return -EINVAL;
626}
627
628/* Security:
629 *
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000630 * As we dont want to clear mem[] array for each packet going through
Li RongQing8ea6e342014-10-10 13:56:51 +0800631 * __bpf_prog_run(), we check that filter loaded by user never try to read
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000632 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300633 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000634 */
Eric Dumazetec31a052014-07-12 15:49:16 +0200635static int check_load_and_stores(const struct sock_filter *filter, int flen)
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000636{
Daniel Borkmann34805932014-05-29 10:22:50 +0200637 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000638 int pc, ret = 0;
639
640 BUILD_BUG_ON(BPF_MEMWORDS > 16);
Daniel Borkmann34805932014-05-29 10:22:50 +0200641
Tobias Klauser99e72a02014-06-24 15:33:22 +0200642 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000643 if (!masks)
644 return -ENOMEM;
Daniel Borkmann34805932014-05-29 10:22:50 +0200645
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000646 memset(masks, 0xff, flen * sizeof(*masks));
647
648 for (pc = 0; pc < flen; pc++) {
649 memvalid &= masks[pc];
650
651 switch (filter[pc].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200652 case BPF_ST:
653 case BPF_STX:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000654 memvalid |= (1 << filter[pc].k);
655 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200656 case BPF_LD | BPF_MEM:
657 case BPF_LDX | BPF_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000658 if (!(memvalid & (1 << filter[pc].k))) {
659 ret = -EINVAL;
660 goto error;
661 }
662 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200663 case BPF_JMP | BPF_JA:
664 /* A jump must set masks on target */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000665 masks[pc + 1 + filter[pc].k] &= memvalid;
666 memvalid = ~0;
667 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200668 case BPF_JMP | BPF_JEQ | BPF_K:
669 case BPF_JMP | BPF_JEQ | BPF_X:
670 case BPF_JMP | BPF_JGE | BPF_K:
671 case BPF_JMP | BPF_JGE | BPF_X:
672 case BPF_JMP | BPF_JGT | BPF_K:
673 case BPF_JMP | BPF_JGT | BPF_X:
674 case BPF_JMP | BPF_JSET | BPF_K:
675 case BPF_JMP | BPF_JSET | BPF_X:
676 /* A jump must set masks on targets */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000677 masks[pc + 1 + filter[pc].jt] &= memvalid;
678 masks[pc + 1 + filter[pc].jf] &= memvalid;
679 memvalid = ~0;
680 break;
681 }
682 }
683error:
684 kfree(masks);
685 return ret;
686}
687
Daniel Borkmann34805932014-05-29 10:22:50 +0200688static bool chk_code_allowed(u16 code_to_probe)
689{
690 static const bool codes[] = {
691 /* 32 bit ALU operations */
692 [BPF_ALU | BPF_ADD | BPF_K] = true,
693 [BPF_ALU | BPF_ADD | BPF_X] = true,
694 [BPF_ALU | BPF_SUB | BPF_K] = true,
695 [BPF_ALU | BPF_SUB | BPF_X] = true,
696 [BPF_ALU | BPF_MUL | BPF_K] = true,
697 [BPF_ALU | BPF_MUL | BPF_X] = true,
698 [BPF_ALU | BPF_DIV | BPF_K] = true,
699 [BPF_ALU | BPF_DIV | BPF_X] = true,
700 [BPF_ALU | BPF_MOD | BPF_K] = true,
701 [BPF_ALU | BPF_MOD | BPF_X] = true,
702 [BPF_ALU | BPF_AND | BPF_K] = true,
703 [BPF_ALU | BPF_AND | BPF_X] = true,
704 [BPF_ALU | BPF_OR | BPF_K] = true,
705 [BPF_ALU | BPF_OR | BPF_X] = true,
706 [BPF_ALU | BPF_XOR | BPF_K] = true,
707 [BPF_ALU | BPF_XOR | BPF_X] = true,
708 [BPF_ALU | BPF_LSH | BPF_K] = true,
709 [BPF_ALU | BPF_LSH | BPF_X] = true,
710 [BPF_ALU | BPF_RSH | BPF_K] = true,
711 [BPF_ALU | BPF_RSH | BPF_X] = true,
712 [BPF_ALU | BPF_NEG] = true,
713 /* Load instructions */
714 [BPF_LD | BPF_W | BPF_ABS] = true,
715 [BPF_LD | BPF_H | BPF_ABS] = true,
716 [BPF_LD | BPF_B | BPF_ABS] = true,
717 [BPF_LD | BPF_W | BPF_LEN] = true,
718 [BPF_LD | BPF_W | BPF_IND] = true,
719 [BPF_LD | BPF_H | BPF_IND] = true,
720 [BPF_LD | BPF_B | BPF_IND] = true,
721 [BPF_LD | BPF_IMM] = true,
722 [BPF_LD | BPF_MEM] = true,
723 [BPF_LDX | BPF_W | BPF_LEN] = true,
724 [BPF_LDX | BPF_B | BPF_MSH] = true,
725 [BPF_LDX | BPF_IMM] = true,
726 [BPF_LDX | BPF_MEM] = true,
727 /* Store instructions */
728 [BPF_ST] = true,
729 [BPF_STX] = true,
730 /* Misc instructions */
731 [BPF_MISC | BPF_TAX] = true,
732 [BPF_MISC | BPF_TXA] = true,
733 /* Return instructions */
734 [BPF_RET | BPF_K] = true,
735 [BPF_RET | BPF_A] = true,
736 /* Jump instructions */
737 [BPF_JMP | BPF_JA] = true,
738 [BPF_JMP | BPF_JEQ | BPF_K] = true,
739 [BPF_JMP | BPF_JEQ | BPF_X] = true,
740 [BPF_JMP | BPF_JGE | BPF_K] = true,
741 [BPF_JMP | BPF_JGE | BPF_X] = true,
742 [BPF_JMP | BPF_JGT | BPF_K] = true,
743 [BPF_JMP | BPF_JGT | BPF_X] = true,
744 [BPF_JMP | BPF_JSET | BPF_K] = true,
745 [BPF_JMP | BPF_JSET | BPF_X] = true,
746 };
747
748 if (code_to_probe >= ARRAY_SIZE(codes))
749 return false;
750
751 return codes[code_to_probe];
752}
753
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +0200754static bool bpf_check_basics_ok(const struct sock_filter *filter,
755 unsigned int flen)
756{
757 if (filter == NULL)
758 return false;
759 if (flen == 0 || flen > BPF_MAXINSNS)
760 return false;
761
762 return true;
763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/**
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -0700766 * bpf_check_classic - verify socket filter code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 * @filter: filter to verify
768 * @flen: length of filter
769 *
770 * Check the user's filter code. If we let some ugly
771 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800772 * no references or jumps that are out of range, no illegal
773 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800775 * All jumps are forward as they are not signed.
776 *
777 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200779static int bpf_check_classic(const struct sock_filter *filter,
780 unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000782 bool anc_found;
Daniel Borkmann34805932014-05-29 10:22:50 +0200783 int pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Daniel Borkmann34805932014-05-29 10:22:50 +0200785 /* Check the filter code now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 for (pc = 0; pc < flen; pc++) {
Eric Dumazetec31a052014-07-12 15:49:16 +0200787 const struct sock_filter *ftest = &filter[pc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Daniel Borkmann34805932014-05-29 10:22:50 +0200789 /* May we actually operate on this code? */
790 if (!chk_code_allowed(ftest->code))
Tetsuo Handacba328f2010-11-16 15:19:51 +0000791 return -EINVAL;
Daniel Borkmann34805932014-05-29 10:22:50 +0200792
Kris Katterjohn93699862006-01-04 13:58:36 -0800793 /* Some instructions need special checks */
Daniel Borkmann34805932014-05-29 10:22:50 +0200794 switch (ftest->code) {
795 case BPF_ALU | BPF_DIV | BPF_K:
796 case BPF_ALU | BPF_MOD | BPF_K:
797 /* Check for division by zero */
Eric Dumazetb6069a92012-09-07 22:03:35 +0000798 if (ftest->k == 0)
799 return -EINVAL;
800 break;
Rabin Vincent229394e2016-01-12 20:17:08 +0100801 case BPF_ALU | BPF_LSH | BPF_K:
802 case BPF_ALU | BPF_RSH | BPF_K:
803 if (ftest->k >= 32)
804 return -EINVAL;
805 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200806 case BPF_LD | BPF_MEM:
807 case BPF_LDX | BPF_MEM:
808 case BPF_ST:
809 case BPF_STX:
810 /* Check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800811 if (ftest->k >= BPF_MEMWORDS)
812 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000813 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200814 case BPF_JMP | BPF_JA:
815 /* Note, the large ftest->k might cause loops.
Kris Katterjohn93699862006-01-04 13:58:36 -0800816 * Compare this with conditional jumps below,
817 * where offsets are limited. --ANK (981016)
818 */
Daniel Borkmann34805932014-05-29 10:22:50 +0200819 if (ftest->k >= (unsigned int)(flen - pc - 1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800820 return -EINVAL;
821 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200822 case BPF_JMP | BPF_JEQ | BPF_K:
823 case BPF_JMP | BPF_JEQ | BPF_X:
824 case BPF_JMP | BPF_JGE | BPF_K:
825 case BPF_JMP | BPF_JGE | BPF_X:
826 case BPF_JMP | BPF_JGT | BPF_K:
827 case BPF_JMP | BPF_JGT | BPF_X:
828 case BPF_JMP | BPF_JSET | BPF_K:
829 case BPF_JMP | BPF_JSET | BPF_X:
830 /* Both conditionals must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000831 if (pc + ftest->jt + 1 >= flen ||
832 pc + ftest->jf + 1 >= flen)
833 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000834 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200835 case BPF_LD | BPF_W | BPF_ABS:
836 case BPF_LD | BPF_H | BPF_ABS:
837 case BPF_LD | BPF_B | BPF_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000838 anc_found = false;
Daniel Borkmann34805932014-05-29 10:22:50 +0200839 if (bpf_anc_helper(ftest) & BPF_ANC)
840 anc_found = true;
841 /* Ancillary operation unknown or unsupported */
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000842 if (anc_found == false && ftest->k >= SKF_AD_OFF)
843 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
Daniel Borkmann34805932014-05-29 10:22:50 +0200847 /* Last instruction must be a RET code */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000848 switch (filter[flen - 1].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200849 case BPF_RET | BPF_K:
850 case BPF_RET | BPF_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000851 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000852 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200853
Tetsuo Handacba328f2010-11-16 15:19:51 +0000854 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700857static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
858 const struct sock_fprog *fprog)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100859{
Alexei Starovoitov009937e2014-07-30 20:34:13 -0700860 unsigned int fsize = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100861 struct sock_fprog_kern *fkprog;
862
863 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
864 if (!fp->orig_prog)
865 return -ENOMEM;
866
867 fkprog = fp->orig_prog;
868 fkprog->len = fprog->len;
Daniel Borkmann658da932015-05-06 16:12:29 +0200869
870 fkprog->filter = kmemdup(fp->insns, fsize,
871 GFP_KERNEL | __GFP_NOWARN);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100872 if (!fkprog->filter) {
873 kfree(fp->orig_prog);
874 return -ENOMEM;
875 }
876
877 return 0;
878}
879
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700880static void bpf_release_orig_filter(struct bpf_prog *fp)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100881{
882 struct sock_fprog_kern *fprog = fp->orig_prog;
883
884 if (fprog) {
885 kfree(fprog->filter);
886 kfree(fprog);
887 }
888}
889
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700890static void __bpf_prog_release(struct bpf_prog *prog)
891{
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100892 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800893 bpf_prog_put(prog);
894 } else {
895 bpf_release_orig_filter(prog);
896 bpf_prog_free(prog);
897 }
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700898}
899
Pablo Neira34c5bd62014-07-29 17:36:28 +0200900static void __sk_filter_release(struct sk_filter *fp)
901{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700902 __bpf_prog_release(fp->prog);
903 kfree(fp);
Pablo Neira34c5bd62014-07-29 17:36:28 +0200904}
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800907 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700908 * @rcu: rcu_head that contains the sk_filter to free
909 */
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100910static void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700911{
912 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
913
Pablo Neira34c5bd62014-07-29 17:36:28 +0200914 __sk_filter_release(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700915}
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100916
917/**
918 * sk_filter_release - release a socket filter
919 * @fp: filter to remove
920 *
921 * Remove a filter from a socket and release its resources.
922 */
923static void sk_filter_release(struct sk_filter *fp)
924{
925 if (atomic_dec_and_test(&fp->refcnt))
926 call_rcu(&fp->rcu, sk_filter_release_rcu);
927}
928
929void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
930{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700931 u32 filter_size = bpf_prog_size(fp->prog->len);
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700932
933 atomic_sub(filter_size, &sk->sk_omem_alloc);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100934 sk_filter_release(fp);
935}
936
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700937/* try to charge the socket memory if there is space available
938 * return true on success
939 */
940bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100941{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700942 u32 filter_size = bpf_prog_size(fp->prog->len);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700943
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700944 /* same check as in sock_kmalloc() */
945 if (filter_size <= sysctl_optmem_max &&
946 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
947 atomic_inc(&fp->refcnt);
948 atomic_add(filter_size, &sk->sk_omem_alloc);
949 return true;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100950 }
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700951 return false;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100952}
953
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700954static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100955{
956 struct sock_filter *old_prog;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700957 struct bpf_prog *old_fp;
Daniel Borkmann34805932014-05-29 10:22:50 +0200958 int err, new_len, old_len = fp->len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100959
960 /* We are free to overwrite insns et al right here as it
961 * won't be used at this point in time anymore internally
962 * after the migration to the internal BPF instruction
963 * representation.
964 */
965 BUILD_BUG_ON(sizeof(struct sock_filter) !=
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700966 sizeof(struct bpf_insn));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100967
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100968 /* Conversion cannot happen on overlapping memory areas,
969 * so we need to keep the user BPF around until the 2nd
970 * pass. At this time, the user BPF is stored in fp->insns.
971 */
972 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
Daniel Borkmann658da932015-05-06 16:12:29 +0200973 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100974 if (!old_prog) {
975 err = -ENOMEM;
976 goto out_err;
977 }
978
979 /* 1st pass: calculate the new program length. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700980 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100981 if (err)
982 goto out_err_free;
983
984 /* Expand fp for appending the new filter representation. */
985 old_fp = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200986 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100987 if (!fp) {
988 /* The old_fp is still around in case we couldn't
989 * allocate new memory, so uncharge on that one.
990 */
991 fp = old_fp;
992 err = -ENOMEM;
993 goto out_err_free;
994 }
995
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100996 fp->len = new_len;
997
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700998 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700999 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001000 if (err)
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -07001001 /* 2nd bpf_convert_filter() can fail only if it fails
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001002 * to allocate memory, remapping must succeed. Note,
1003 * that at this time old_fp has already been released
Alexei Starovoitov278571b2014-07-30 20:34:12 -07001004 * by krealloc().
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001005 */
1006 goto out_err_free;
1007
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001008 /* We are guaranteed to never error here with cBPF to eBPF
1009 * transitions, since there's no issue with type compatibility
1010 * checks on program arrays.
1011 */
1012 fp = bpf_prog_select_runtime(fp, &err);
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001013
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001014 kfree(old_prog);
1015 return fp;
1016
1017out_err_free:
1018 kfree(old_prog);
1019out_err:
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001020 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001021 return ERR_PTR(err);
1022}
1023
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001024static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1025 bpf_aux_classic_check_t trans)
Jiri Pirko302d6632012-03-31 11:01:19 +00001026{
1027 int err;
1028
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001029 fp->bpf_func = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001030 fp->jited = 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001031
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -07001032 err = bpf_check_classic(fp->insns, fp->len);
Leon Yu418c96a2014-06-01 05:37:25 +00001033 if (err) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001034 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001035 return ERR_PTR(err);
Leon Yu418c96a2014-06-01 05:37:25 +00001036 }
Jiri Pirko302d6632012-03-31 11:01:19 +00001037
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001038 /* There might be additional checks and transformations
1039 * needed on classic filters, f.e. in case of seccomp.
1040 */
1041 if (trans) {
1042 err = trans(fp->insns, fp->len);
1043 if (err) {
1044 __bpf_prog_release(fp);
1045 return ERR_PTR(err);
1046 }
1047 }
1048
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001049 /* Probe if we can JIT compile the filter and if so, do
1050 * the compilation of the filter.
1051 */
Jiri Pirko302d6632012-03-31 11:01:19 +00001052 bpf_jit_compile(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001053
1054 /* JIT compiler couldn't process this filter, so do the
1055 * internal BPF translation for the optimized interpreter.
1056 */
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001057 if (!fp->jited)
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001058 fp = bpf_migrate_filter(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001059
1060 return fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001061}
1062
1063/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001064 * bpf_prog_create - create an unattached filter
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001065 * @pfp: the unattached filter that is created
Tobias Klauser677a9fd2014-06-24 15:33:21 +02001066 * @fprog: the filter program
Jiri Pirko302d6632012-03-31 11:01:19 +00001067 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001068 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +00001069 * sanity checks on it to make sure it does not explode on us later.
1070 * If an error occurs or there is insufficient memory for the filter
1071 * a negative errno code is returned. On success the return is zero.
1072 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001073int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
Jiri Pirko302d6632012-03-31 11:01:19 +00001074{
Alexei Starovoitov009937e2014-07-30 20:34:13 -07001075 unsigned int fsize = bpf_classic_proglen(fprog);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001076 struct bpf_prog *fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001077
1078 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001079 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Jiri Pirko302d6632012-03-31 11:01:19 +00001080 return -EINVAL;
1081
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001082 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Jiri Pirko302d6632012-03-31 11:01:19 +00001083 if (!fp)
1084 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001085
Jiri Pirko302d6632012-03-31 11:01:19 +00001086 memcpy(fp->insns, fprog->filter, fsize);
1087
Jiri Pirko302d6632012-03-31 11:01:19 +00001088 fp->len = fprog->len;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001089 /* Since unattached filters are not copied back to user
1090 * space through sk_get_filter(), we do not need to hold
1091 * a copy here, and can spare us the work.
1092 */
1093 fp->orig_prog = NULL;
Jiri Pirko302d6632012-03-31 11:01:19 +00001094
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001095 /* bpf_prepare_filter() already takes care of freeing
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001096 * memory in case something goes wrong.
1097 */
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001098 fp = bpf_prepare_filter(fp, NULL);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001099 if (IS_ERR(fp))
1100 return PTR_ERR(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001101
1102 *pfp = fp;
1103 return 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001104}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001105EXPORT_SYMBOL_GPL(bpf_prog_create);
Jiri Pirko302d6632012-03-31 11:01:19 +00001106
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001107/**
1108 * bpf_prog_create_from_user - create an unattached filter from user buffer
1109 * @pfp: the unattached filter that is created
1110 * @fprog: the filter program
1111 * @trans: post-classic verifier transformation handler
Daniel Borkmannbab18992015-10-02 15:17:33 +02001112 * @save_orig: save classic BPF program
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001113 *
1114 * This function effectively does the same as bpf_prog_create(), only
1115 * that it builds up its insns buffer from user space provided buffer.
1116 * It also allows for passing a bpf_aux_classic_check_t handler.
1117 */
1118int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
Daniel Borkmannbab18992015-10-02 15:17:33 +02001119 bpf_aux_classic_check_t trans, bool save_orig)
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001120{
1121 unsigned int fsize = bpf_classic_proglen(fprog);
1122 struct bpf_prog *fp;
Daniel Borkmannbab18992015-10-02 15:17:33 +02001123 int err;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001124
1125 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001126 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001127 return -EINVAL;
1128
1129 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1130 if (!fp)
1131 return -ENOMEM;
1132
1133 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1134 __bpf_prog_free(fp);
1135 return -EFAULT;
1136 }
1137
1138 fp->len = fprog->len;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001139 fp->orig_prog = NULL;
1140
Daniel Borkmannbab18992015-10-02 15:17:33 +02001141 if (save_orig) {
1142 err = bpf_prog_store_orig_filter(fp, fprog);
1143 if (err) {
1144 __bpf_prog_free(fp);
1145 return -ENOMEM;
1146 }
1147 }
1148
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001149 /* bpf_prepare_filter() already takes care of freeing
1150 * memory in case something goes wrong.
1151 */
1152 fp = bpf_prepare_filter(fp, trans);
1153 if (IS_ERR(fp))
1154 return PTR_ERR(fp);
1155
1156 *pfp = fp;
1157 return 0;
1158}
David S. Miller2ea273d2015-08-17 14:37:06 -07001159EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001160
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001161void bpf_prog_destroy(struct bpf_prog *fp)
Jiri Pirko302d6632012-03-31 11:01:19 +00001162{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001163 __bpf_prog_release(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001164}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001165EXPORT_SYMBOL_GPL(bpf_prog_destroy);
Jiri Pirko302d6632012-03-31 11:01:19 +00001166
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001167static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001168{
1169 struct sk_filter *fp, *old_fp;
1170
1171 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1172 if (!fp)
1173 return -ENOMEM;
1174
1175 fp->prog = prog;
1176 atomic_set(&fp->refcnt, 0);
1177
1178 if (!sk_filter_charge(sk, fp)) {
1179 kfree(fp);
1180 return -ENOMEM;
1181 }
1182
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001183 old_fp = rcu_dereference_protected(sk->sk_filter,
1184 lockdep_sock_is_held(sk));
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001185 rcu_assign_pointer(sk->sk_filter, fp);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001186
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001187 if (old_fp)
1188 sk_filter_uncharge(sk, old_fp);
1189
1190 return 0;
1191}
1192
Craig Gallek538950a2016-01-04 17:41:47 -05001193static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1194{
1195 struct bpf_prog *old_prog;
1196 int err;
1197
1198 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1199 return -ENOMEM;
1200
Craig Gallekfa463492016-02-10 11:50:39 -05001201 if (sk_unhashed(sk) && sk->sk_reuseport) {
Craig Gallek538950a2016-01-04 17:41:47 -05001202 err = reuseport_alloc(sk);
1203 if (err)
1204 return err;
1205 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1206 /* The socket wasn't bound with SO_REUSEPORT */
1207 return -EINVAL;
1208 }
1209
1210 old_prog = reuseport_attach_prog(sk, prog);
1211 if (old_prog)
1212 bpf_prog_destroy(old_prog);
1213
1214 return 0;
1215}
1216
1217static
1218struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1219{
1220 unsigned int fsize = bpf_classic_proglen(fprog);
Craig Gallek538950a2016-01-04 17:41:47 -05001221 struct bpf_prog *prog;
1222 int err;
1223
1224 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1225 return ERR_PTR(-EPERM);
1226
1227 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001228 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Craig Gallek538950a2016-01-04 17:41:47 -05001229 return ERR_PTR(-EINVAL);
1230
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001231 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Craig Gallek538950a2016-01-04 17:41:47 -05001232 if (!prog)
1233 return ERR_PTR(-ENOMEM);
1234
1235 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1236 __bpf_prog_free(prog);
1237 return ERR_PTR(-EFAULT);
1238 }
1239
1240 prog->len = fprog->len;
1241
1242 err = bpf_prog_store_orig_filter(prog, fprog);
1243 if (err) {
1244 __bpf_prog_free(prog);
1245 return ERR_PTR(-ENOMEM);
1246 }
1247
1248 /* bpf_prepare_filter() already takes care of freeing
1249 * memory in case something goes wrong.
1250 */
1251 return bpf_prepare_filter(prog, NULL);
1252}
1253
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001254/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 * sk_attach_filter - attach a socket filter
1256 * @fprog: the filter program
1257 * @sk: the socket to use
1258 *
1259 * Attach the user's filter code. We first run some sanity checks on
1260 * it to make sure it does not explode on us later. If an error
1261 * occurs or there is insufficient memory for the filter a negative
1262 * errno code is returned. On success the return is zero.
1263 */
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001264int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Craig Gallek538950a2016-01-04 17:41:47 -05001266 struct bpf_prog *prog = __get_filter(fprog, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int err;
1268
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001269 if (IS_ERR(prog))
1270 return PTR_ERR(prog);
1271
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001272 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001273 if (err < 0) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001274 __bpf_prog_release(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001275 return err;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001276 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001277
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001280EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Craig Gallek538950a2016-01-04 17:41:47 -05001282int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001283{
Craig Gallek538950a2016-01-04 17:41:47 -05001284 struct bpf_prog *prog = __get_filter(fprog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001285 int err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001286
Alexei Starovoitov198bf1b2014-12-10 20:14:55 -08001287 if (IS_ERR(prog))
1288 return PTR_ERR(prog);
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001289
Craig Gallek538950a2016-01-04 17:41:47 -05001290 err = __reuseport_attach_prog(prog, sk);
1291 if (err < 0) {
1292 __bpf_prog_release(prog);
1293 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001294 }
1295
Craig Gallek538950a2016-01-04 17:41:47 -05001296 return 0;
1297}
1298
1299static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1300{
Craig Gallek538950a2016-01-04 17:41:47 -05001301 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1302 return ERR_PTR(-EPERM);
1303
Daniel Borkmann113214b2016-06-30 17:24:44 +02001304 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
Craig Gallek538950a2016-01-04 17:41:47 -05001305}
1306
1307int sk_attach_bpf(u32 ufd, struct sock *sk)
1308{
1309 struct bpf_prog *prog = __get_bpf(ufd, sk);
1310 int err;
1311
1312 if (IS_ERR(prog))
1313 return PTR_ERR(prog);
1314
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001315 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001316 if (err < 0) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001317 bpf_prog_put(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001318 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001319 }
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001320
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001321 return 0;
1322}
1323
Craig Gallek538950a2016-01-04 17:41:47 -05001324int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1325{
1326 struct bpf_prog *prog = __get_bpf(ufd, sk);
1327 int err;
1328
1329 if (IS_ERR(prog))
1330 return PTR_ERR(prog);
1331
1332 err = __reuseport_attach_prog(prog, sk);
1333 if (err < 0) {
1334 bpf_prog_put(prog);
1335 return err;
1336 }
1337
1338 return 0;
1339}
1340
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001341struct bpf_scratchpad {
1342 union {
1343 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1344 u8 buff[MAX_BPF_STACK];
1345 };
1346};
1347
1348static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001349
Daniel Borkmann5293efe2016-08-18 01:00:39 +02001350static inline int __bpf_try_make_writable(struct sk_buff *skb,
1351 unsigned int write_len)
1352{
1353 return skb_ensure_writable(skb, write_len);
1354}
1355
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001356static inline int bpf_try_make_writable(struct sk_buff *skb,
1357 unsigned int write_len)
1358{
Daniel Borkmann5293efe2016-08-18 01:00:39 +02001359 int err = __bpf_try_make_writable(skb, write_len);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001360
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001361 bpf_compute_data_end(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001362 return err;
1363}
1364
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001365static int bpf_try_make_head_writable(struct sk_buff *skb)
1366{
1367 return bpf_try_make_writable(skb, skb_headlen(skb));
1368}
1369
Daniel Borkmanna2bfe6b2016-08-05 00:11:11 +02001370static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1371{
1372 if (skb_at_tc_ingress(skb))
1373 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1374}
1375
Daniel Borkmann80656942016-08-05 00:11:13 +02001376static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1377{
1378 if (skb_at_tc_ingress(skb))
1379 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1380}
1381
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001382BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1383 const void *, from, u32, len, u64, flags)
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001384{
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001385 void *ptr;
1386
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001387 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001388 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001389 if (unlikely(offset > 0xffff))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001390 return -EFAULT;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001391 if (unlikely(bpf_try_make_writable(skb, offset + len)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001392 return -EFAULT;
1393
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001394 ptr = skb->data + offset;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001395 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmann479ffcc2016-08-05 00:11:12 +02001396 __skb_postpull_rcsum(skb, ptr, len, offset);
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001397
1398 memcpy(ptr, from, len);
1399
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001400 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmann479ffcc2016-08-05 00:11:12 +02001401 __skb_postpush_rcsum(skb, ptr, len, offset);
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001402 if (flags & BPF_F_INVALIDATE_HASH)
1403 skb_clear_hash(skb);
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001404
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001405 return 0;
1406}
1407
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001408static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001409 .func = bpf_skb_store_bytes,
1410 .gpl_only = false,
1411 .ret_type = RET_INTEGER,
1412 .arg1_type = ARG_PTR_TO_CTX,
1413 .arg2_type = ARG_ANYTHING,
1414 .arg3_type = ARG_PTR_TO_STACK,
1415 .arg4_type = ARG_CONST_STACK_SIZE,
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001416 .arg5_type = ARG_ANYTHING,
1417};
1418
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001419BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1420 void *, to, u32, len)
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001421{
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001422 void *ptr;
1423
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001424 if (unlikely(offset > 0xffff))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001425 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001426
1427 ptr = skb_header_pointer(skb, offset, len, to);
1428 if (unlikely(!ptr))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001429 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001430 if (ptr != to)
1431 memcpy(to, ptr, len);
1432
1433 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02001434err_clear:
1435 memset(to, 0, len);
1436 return -EFAULT;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001437}
1438
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001439static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001440 .func = bpf_skb_load_bytes,
1441 .gpl_only = false,
1442 .ret_type = RET_INTEGER,
1443 .arg1_type = ARG_PTR_TO_CTX,
1444 .arg2_type = ARG_ANYTHING,
Daniel Borkmann074f5282016-04-13 00:10:52 +02001445 .arg3_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001446 .arg4_type = ARG_CONST_STACK_SIZE,
1447};
1448
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001449BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1450{
1451 /* Idea is the following: should the needed direct read/write
1452 * test fail during runtime, we can pull in more data and redo
1453 * again, since implicitly, we invalidate previous checks here.
1454 *
1455 * Or, since we know how much we need to make read/writeable,
1456 * this can be done once at the program beginning for direct
1457 * access case. By this we overcome limitations of only current
1458 * headroom being accessible.
1459 */
1460 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1461}
1462
1463static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1464 .func = bpf_skb_pull_data,
1465 .gpl_only = false,
1466 .ret_type = RET_INTEGER,
1467 .arg1_type = ARG_PTR_TO_CTX,
1468 .arg2_type = ARG_ANYTHING,
1469};
1470
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001471BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1472 u64, from, u64, to, u64, flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001473{
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001474 __sum16 *ptr;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001475
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001476 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1477 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001478 if (unlikely(offset > 0xffff || offset & 1))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001479 return -EFAULT;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001480 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001481 return -EFAULT;
1482
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001483 ptr = (__sum16 *)(skb->data + offset);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001484 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann8050c0f2016-03-04 15:15:02 +01001485 case 0:
1486 if (unlikely(from != 0))
1487 return -EINVAL;
1488
1489 csum_replace_by_diff(ptr, to);
1490 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001491 case 2:
1492 csum_replace2(ptr, from, to);
1493 break;
1494 case 4:
1495 csum_replace4(ptr, from, to);
1496 break;
1497 default:
1498 return -EINVAL;
1499 }
1500
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001501 return 0;
1502}
1503
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001504static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001505 .func = bpf_l3_csum_replace,
1506 .gpl_only = false,
1507 .ret_type = RET_INTEGER,
1508 .arg1_type = ARG_PTR_TO_CTX,
1509 .arg2_type = ARG_ANYTHING,
1510 .arg3_type = ARG_ANYTHING,
1511 .arg4_type = ARG_ANYTHING,
1512 .arg5_type = ARG_ANYTHING,
1513};
1514
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001515BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1516 u64, from, u64, to, u64, flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001517{
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001518 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
Daniel Borkmann2f729592016-02-19 23:05:26 +01001519 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001520 __sum16 *ptr;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001521
Daniel Borkmann2f729592016-02-19 23:05:26 +01001522 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR |
1523 BPF_F_HDR_FIELD_MASK)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001524 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001525 if (unlikely(offset > 0xffff || offset & 1))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001526 return -EFAULT;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001527 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001528 return -EFAULT;
1529
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001530 ptr = (__sum16 *)(skb->data + offset);
Daniel Borkmann2f729592016-02-19 23:05:26 +01001531 if (is_mmzero && !*ptr)
1532 return 0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001533
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001534 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001535 case 0:
1536 if (unlikely(from != 0))
1537 return -EINVAL;
1538
1539 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1540 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001541 case 2:
1542 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1543 break;
1544 case 4:
1545 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1546 break;
1547 default:
1548 return -EINVAL;
1549 }
1550
Daniel Borkmann2f729592016-02-19 23:05:26 +01001551 if (is_mmzero && !*ptr)
1552 *ptr = CSUM_MANGLED_0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001553 return 0;
1554}
1555
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001556static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001557 .func = bpf_l4_csum_replace,
1558 .gpl_only = false,
1559 .ret_type = RET_INTEGER,
1560 .arg1_type = ARG_PTR_TO_CTX,
1561 .arg2_type = ARG_ANYTHING,
1562 .arg3_type = ARG_ANYTHING,
1563 .arg4_type = ARG_ANYTHING,
1564 .arg5_type = ARG_ANYTHING,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001565};
1566
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001567BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1568 __be32 *, to, u32, to_size, __wsum, seed)
Daniel Borkmann7d672342016-02-19 23:05:23 +01001569{
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001570 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001571 u32 diff_size = from_size + to_size;
Daniel Borkmann7d672342016-02-19 23:05:23 +01001572 int i, j = 0;
1573
1574 /* This is quite flexible, some examples:
1575 *
1576 * from_size == 0, to_size > 0, seed := csum --> pushing data
1577 * from_size > 0, to_size == 0, seed := csum --> pulling data
1578 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1579 *
1580 * Even for diffing, from_size and to_size don't need to be equal.
1581 */
1582 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1583 diff_size > sizeof(sp->diff)))
1584 return -EINVAL;
1585
1586 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1587 sp->diff[j] = ~from[i];
1588 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1589 sp->diff[j] = to[i];
1590
1591 return csum_partial(sp->diff, diff_size, seed);
1592}
1593
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001594static const struct bpf_func_proto bpf_csum_diff_proto = {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001595 .func = bpf_csum_diff,
1596 .gpl_only = false,
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001597 .pkt_access = true,
Daniel Borkmann7d672342016-02-19 23:05:23 +01001598 .ret_type = RET_INTEGER,
1599 .arg1_type = ARG_PTR_TO_STACK,
1600 .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1601 .arg3_type = ARG_PTR_TO_STACK,
1602 .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1603 .arg5_type = ARG_ANYTHING,
1604};
1605
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001606BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1607{
1608 /* The interface is to be used in combination with bpf_csum_diff()
1609 * for direct packet writes. csum rotation for alignment as well
1610 * as emulating csum_sub() can be done from the eBPF program.
1611 */
1612 if (skb->ip_summed == CHECKSUM_COMPLETE)
1613 return (skb->csum = csum_add(skb->csum, csum));
1614
1615 return -ENOTSUPP;
1616}
1617
1618static const struct bpf_func_proto bpf_csum_update_proto = {
1619 .func = bpf_csum_update,
1620 .gpl_only = false,
1621 .ret_type = RET_INTEGER,
1622 .arg1_type = ARG_PTR_TO_CTX,
1623 .arg2_type = ARG_ANYTHING,
1624};
1625
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001626static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1627{
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001628 return dev_forward_skb(dev, skb);
1629}
1630
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001631static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1632 struct sk_buff *skb)
1633{
1634 int ret = ____dev_forward_skb(dev, skb);
1635
1636 if (likely(!ret)) {
1637 skb->dev = dev;
1638 ret = netif_rx(skb);
1639 }
1640
1641 return ret;
1642}
1643
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001644static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1645{
1646 int ret;
1647
1648 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1649 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1650 kfree_skb(skb);
1651 return -ENETDOWN;
1652 }
1653
1654 skb->dev = dev;
1655
1656 __this_cpu_inc(xmit_recursion);
1657 ret = dev_queue_xmit(skb);
1658 __this_cpu_dec(xmit_recursion);
1659
1660 return ret;
1661}
1662
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001663static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1664 u32 flags)
1665{
1666 /* skb->mac_len is not set on normal egress */
1667 unsigned int mlen = skb->network_header - skb->mac_header;
1668
1669 __skb_pull(skb, mlen);
1670
1671 /* At ingress, the mac header has already been pulled once.
1672 * At egress, skb_pospull_rcsum has to be done in case that
1673 * the skb is originated from ingress (i.e. a forwarded skb)
1674 * to ensure that rcsum starts at net header.
1675 */
1676 if (!skb_at_tc_ingress(skb))
1677 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1678 skb_pop_mac_header(skb);
1679 skb_reset_mac_len(skb);
1680 return flags & BPF_F_INGRESS ?
1681 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1682}
1683
1684static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1685 u32 flags)
1686{
1687 bpf_push_mac_rcsum(skb);
1688 return flags & BPF_F_INGRESS ?
1689 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1690}
1691
1692static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1693 u32 flags)
1694{
1695 switch (dev->type) {
1696 case ARPHRD_TUNNEL:
1697 case ARPHRD_TUNNEL6:
1698 case ARPHRD_SIT:
1699 case ARPHRD_IPGRE:
1700 case ARPHRD_VOID:
1701 case ARPHRD_NONE:
1702 return __bpf_redirect_no_mac(skb, dev, flags);
1703 default:
1704 return __bpf_redirect_common(skb, dev, flags);
1705 }
1706}
1707
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001708BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001709{
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001710 struct net_device *dev;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001711 struct sk_buff *clone;
1712 int ret;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001713
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001714 if (unlikely(flags & ~(BPF_F_INGRESS)))
1715 return -EINVAL;
1716
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001717 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1718 if (unlikely(!dev))
1719 return -EINVAL;
1720
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001721 clone = skb_clone(skb, GFP_ATOMIC);
1722 if (unlikely(!clone))
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001723 return -ENOMEM;
1724
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001725 /* For direct write, we need to keep the invariant that the skbs
1726 * we're dealing with need to be uncloned. Should uncloning fail
1727 * here, we need to free the just generated clone to unclone once
1728 * again.
1729 */
1730 ret = bpf_try_make_head_writable(skb);
1731 if (unlikely(ret)) {
1732 kfree_skb(clone);
1733 return -ENOMEM;
1734 }
1735
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001736 return __bpf_redirect(clone, dev, flags);
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001737}
1738
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001739static const struct bpf_func_proto bpf_clone_redirect_proto = {
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001740 .func = bpf_clone_redirect,
1741 .gpl_only = false,
1742 .ret_type = RET_INTEGER,
1743 .arg1_type = ARG_PTR_TO_CTX,
1744 .arg2_type = ARG_ANYTHING,
1745 .arg3_type = ARG_ANYTHING,
1746};
1747
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001748struct redirect_info {
1749 u32 ifindex;
1750 u32 flags;
1751};
1752
1753static DEFINE_PER_CPU(struct redirect_info, redirect_info);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001754
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001755BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001756{
1757 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1758
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001759 if (unlikely(flags & ~(BPF_F_INGRESS)))
1760 return TC_ACT_SHOT;
1761
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001762 ri->ifindex = ifindex;
1763 ri->flags = flags;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001764
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001765 return TC_ACT_REDIRECT;
1766}
1767
1768int skb_do_redirect(struct sk_buff *skb)
1769{
1770 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1771 struct net_device *dev;
1772
1773 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1774 ri->ifindex = 0;
1775 if (unlikely(!dev)) {
1776 kfree_skb(skb);
1777 return -EINVAL;
1778 }
1779
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001780 return __bpf_redirect(skb, dev, ri->flags);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001781}
1782
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001783static const struct bpf_func_proto bpf_redirect_proto = {
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001784 .func = bpf_redirect,
1785 .gpl_only = false,
1786 .ret_type = RET_INTEGER,
1787 .arg1_type = ARG_ANYTHING,
1788 .arg2_type = ARG_ANYTHING,
1789};
1790
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001791BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001792{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001793 return task_get_classid(skb);
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001794}
1795
1796static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1797 .func = bpf_get_cgroup_classid,
1798 .gpl_only = false,
1799 .ret_type = RET_INTEGER,
1800 .arg1_type = ARG_PTR_TO_CTX,
1801};
1802
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001803BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001804{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001805 return dst_tclassid(skb);
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001806}
1807
1808static const struct bpf_func_proto bpf_get_route_realm_proto = {
1809 .func = bpf_get_route_realm,
1810 .gpl_only = false,
1811 .ret_type = RET_INTEGER,
1812 .arg1_type = ARG_PTR_TO_CTX,
1813};
1814
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001815BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
Daniel Borkmann13c5c242016-07-03 01:28:47 +02001816{
1817 /* If skb_clear_hash() was called due to mangling, we can
1818 * trigger SW recalculation here. Later access to hash
1819 * can then use the inline skb->hash via context directly
1820 * instead of calling this helper again.
1821 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001822 return skb_get_hash(skb);
Daniel Borkmann13c5c242016-07-03 01:28:47 +02001823}
1824
1825static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1826 .func = bpf_get_hash_recalc,
1827 .gpl_only = false,
1828 .ret_type = RET_INTEGER,
1829 .arg1_type = ARG_PTR_TO_CTX,
1830};
1831
Daniel Borkmann7a4b28c2016-09-23 01:28:37 +02001832BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1833{
1834 /* After all direct packet write, this can be used once for
1835 * triggering a lazy recalc on next skb_get_hash() invocation.
1836 */
1837 skb_clear_hash(skb);
1838 return 0;
1839}
1840
1841static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1842 .func = bpf_set_hash_invalid,
1843 .gpl_only = false,
1844 .ret_type = RET_INTEGER,
1845 .arg1_type = ARG_PTR_TO_CTX,
1846};
1847
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001848BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1849 u16, vlan_tci)
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001850{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001851 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001852
1853 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1854 vlan_proto != htons(ETH_P_8021AD)))
1855 vlan_proto = htons(ETH_P_8021Q);
1856
Daniel Borkmann80656942016-08-05 00:11:13 +02001857 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001858 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
Daniel Borkmann80656942016-08-05 00:11:13 +02001859 bpf_pull_mac_rcsum(skb);
1860
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001861 bpf_compute_data_end(skb);
1862 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001863}
1864
1865const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1866 .func = bpf_skb_vlan_push,
1867 .gpl_only = false,
1868 .ret_type = RET_INTEGER,
1869 .arg1_type = ARG_PTR_TO_CTX,
1870 .arg2_type = ARG_ANYTHING,
1871 .arg3_type = ARG_ANYTHING,
1872};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001873EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001874
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001875BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001876{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001877 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001878
Daniel Borkmann80656942016-08-05 00:11:13 +02001879 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001880 ret = skb_vlan_pop(skb);
Daniel Borkmann80656942016-08-05 00:11:13 +02001881 bpf_pull_mac_rcsum(skb);
1882
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001883 bpf_compute_data_end(skb);
1884 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001885}
1886
1887const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1888 .func = bpf_skb_vlan_pop,
1889 .gpl_only = false,
1890 .ret_type = RET_INTEGER,
1891 .arg1_type = ARG_PTR_TO_CTX,
1892};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001893EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001894
Daniel Borkmann65781712016-06-28 12:18:27 +02001895static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1896{
1897 /* Caller already did skb_cow() with len as headroom,
1898 * so no need to do it here.
1899 */
1900 skb_push(skb, len);
1901 memmove(skb->data, skb->data + len, off);
1902 memset(skb->data + off, 0, len);
1903
1904 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1905 * needed here as it does not change the skb->csum
1906 * result for checksum complete when summing over
1907 * zeroed blocks.
1908 */
1909 return 0;
1910}
1911
1912static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1913{
1914 /* skb_ensure_writable() is not needed here, as we're
1915 * already working on an uncloned skb.
1916 */
1917 if (unlikely(!pskb_may_pull(skb, off + len)))
1918 return -ENOMEM;
1919
1920 skb_postpull_rcsum(skb, skb->data + off, len);
1921 memmove(skb->data + len, skb->data, off);
1922 __skb_pull(skb, len);
1923
1924 return 0;
1925}
1926
1927static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1928{
1929 bool trans_same = skb->transport_header == skb->network_header;
1930 int ret;
1931
1932 /* There's no need for __skb_push()/__skb_pull() pair to
1933 * get to the start of the mac header as we're guaranteed
1934 * to always start from here under eBPF.
1935 */
1936 ret = bpf_skb_generic_push(skb, off, len);
1937 if (likely(!ret)) {
1938 skb->mac_header -= len;
1939 skb->network_header -= len;
1940 if (trans_same)
1941 skb->transport_header = skb->network_header;
1942 }
1943
1944 return ret;
1945}
1946
1947static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1948{
1949 bool trans_same = skb->transport_header == skb->network_header;
1950 int ret;
1951
1952 /* Same here, __skb_push()/__skb_pull() pair not needed. */
1953 ret = bpf_skb_generic_pop(skb, off, len);
1954 if (likely(!ret)) {
1955 skb->mac_header += len;
1956 skb->network_header += len;
1957 if (trans_same)
1958 skb->transport_header = skb->network_header;
1959 }
1960
1961 return ret;
1962}
1963
1964static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
1965{
1966 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1967 u32 off = skb->network_header - skb->mac_header;
1968 int ret;
1969
1970 ret = skb_cow(skb, len_diff);
1971 if (unlikely(ret < 0))
1972 return ret;
1973
1974 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
1975 if (unlikely(ret < 0))
1976 return ret;
1977
1978 if (skb_is_gso(skb)) {
1979 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
1980 * be changed into SKB_GSO_TCPV6.
1981 */
1982 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1983 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
1984 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
1985 }
1986
1987 /* Due to IPv6 header, MSS needs to be downgraded. */
1988 skb_shinfo(skb)->gso_size -= len_diff;
1989 /* Header must be checked, and gso_segs recomputed. */
1990 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1991 skb_shinfo(skb)->gso_segs = 0;
1992 }
1993
1994 skb->protocol = htons(ETH_P_IPV6);
1995 skb_clear_hash(skb);
1996
1997 return 0;
1998}
1999
2000static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2001{
2002 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2003 u32 off = skb->network_header - skb->mac_header;
2004 int ret;
2005
2006 ret = skb_unclone(skb, GFP_ATOMIC);
2007 if (unlikely(ret < 0))
2008 return ret;
2009
2010 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2011 if (unlikely(ret < 0))
2012 return ret;
2013
2014 if (skb_is_gso(skb)) {
2015 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
2016 * be changed into SKB_GSO_TCPV4.
2017 */
2018 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2019 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2020 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2021 }
2022
2023 /* Due to IPv4 header, MSS can be upgraded. */
2024 skb_shinfo(skb)->gso_size += len_diff;
2025 /* Header must be checked, and gso_segs recomputed. */
2026 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2027 skb_shinfo(skb)->gso_segs = 0;
2028 }
2029
2030 skb->protocol = htons(ETH_P_IP);
2031 skb_clear_hash(skb);
2032
2033 return 0;
2034}
2035
2036static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2037{
2038 __be16 from_proto = skb->protocol;
2039
2040 if (from_proto == htons(ETH_P_IP) &&
2041 to_proto == htons(ETH_P_IPV6))
2042 return bpf_skb_proto_4_to_6(skb);
2043
2044 if (from_proto == htons(ETH_P_IPV6) &&
2045 to_proto == htons(ETH_P_IP))
2046 return bpf_skb_proto_6_to_4(skb);
2047
2048 return -ENOTSUPP;
2049}
2050
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002051BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2052 u64, flags)
Daniel Borkmann65781712016-06-28 12:18:27 +02002053{
Daniel Borkmann65781712016-06-28 12:18:27 +02002054 int ret;
2055
2056 if (unlikely(flags))
2057 return -EINVAL;
2058
2059 /* General idea is that this helper does the basic groundwork
2060 * needed for changing the protocol, and eBPF program fills the
2061 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2062 * and other helpers, rather than passing a raw buffer here.
2063 *
2064 * The rationale is to keep this minimal and without a need to
2065 * deal with raw packet data. F.e. even if we would pass buffers
2066 * here, the program still needs to call the bpf_lX_csum_replace()
2067 * helpers anyway. Plus, this way we keep also separation of
2068 * concerns, since f.e. bpf_skb_store_bytes() should only take
2069 * care of stores.
2070 *
2071 * Currently, additional options and extension header space are
2072 * not supported, but flags register is reserved so we can adapt
2073 * that. For offloads, we mark packet as dodgy, so that headers
2074 * need to be verified first.
2075 */
2076 ret = bpf_skb_proto_xlat(skb, proto);
2077 bpf_compute_data_end(skb);
2078 return ret;
2079}
2080
2081static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2082 .func = bpf_skb_change_proto,
2083 .gpl_only = false,
2084 .ret_type = RET_INTEGER,
2085 .arg1_type = ARG_PTR_TO_CTX,
2086 .arg2_type = ARG_ANYTHING,
2087 .arg3_type = ARG_ANYTHING,
2088};
2089
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002090BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002091{
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002092 /* We only allow a restricted subset to be changed for now. */
Daniel Borkmann45c7fff2016-08-18 01:00:38 +02002093 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2094 !skb_pkt_type_ok(pkt_type)))
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002095 return -EINVAL;
2096
2097 skb->pkt_type = pkt_type;
2098 return 0;
2099}
2100
2101static const struct bpf_func_proto bpf_skb_change_type_proto = {
2102 .func = bpf_skb_change_type,
2103 .gpl_only = false,
2104 .ret_type = RET_INTEGER,
2105 .arg1_type = ARG_PTR_TO_CTX,
2106 .arg2_type = ARG_ANYTHING,
2107};
2108
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002109static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2110{
2111 u32 min_len = skb_network_offset(skb);
2112
2113 if (skb_transport_header_was_set(skb))
2114 min_len = skb_transport_offset(skb);
2115 if (skb->ip_summed == CHECKSUM_PARTIAL)
2116 min_len = skb_checksum_start_offset(skb) +
2117 skb->csum_offset + sizeof(__sum16);
2118 return min_len;
2119}
2120
2121static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2122{
Daniel Borkmann6088b582016-09-09 02:45:28 +02002123 return skb->dev->mtu + skb->dev->hard_header_len;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002124}
2125
2126static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2127{
2128 unsigned int old_len = skb->len;
2129 int ret;
2130
2131 ret = __skb_grow_rcsum(skb, new_len);
2132 if (!ret)
2133 memset(skb->data + old_len, 0, new_len - old_len);
2134 return ret;
2135}
2136
2137static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2138{
2139 return __skb_trim_rcsum(skb, new_len);
2140}
2141
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002142BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2143 u64, flags)
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002144{
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002145 u32 max_len = __bpf_skb_max_len(skb);
2146 u32 min_len = __bpf_skb_min_len(skb);
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002147 int ret;
2148
2149 if (unlikely(flags || new_len > max_len || new_len < min_len))
2150 return -EINVAL;
2151 if (skb->encapsulation)
2152 return -ENOTSUPP;
2153
2154 /* The basic idea of this helper is that it's performing the
2155 * needed work to either grow or trim an skb, and eBPF program
2156 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2157 * bpf_lX_csum_replace() and others rather than passing a raw
2158 * buffer here. This one is a slow path helper and intended
2159 * for replies with control messages.
2160 *
2161 * Like in bpf_skb_change_proto(), we want to keep this rather
2162 * minimal and without protocol specifics so that we are able
2163 * to separate concerns as in bpf_skb_store_bytes() should only
2164 * be the one responsible for writing buffers.
2165 *
2166 * It's really expected to be a slow path operation here for
2167 * control message replies, so we're implicitly linearizing,
2168 * uncloning and drop offloads from the skb by this.
2169 */
2170 ret = __bpf_try_make_writable(skb, skb->len);
2171 if (!ret) {
2172 if (new_len > skb->len)
2173 ret = bpf_skb_grow_rcsum(skb, new_len);
2174 else if (new_len < skb->len)
2175 ret = bpf_skb_trim_rcsum(skb, new_len);
2176 if (!ret && skb_is_gso(skb))
2177 skb_gso_reset(skb);
2178 }
2179
2180 bpf_compute_data_end(skb);
2181 return ret;
2182}
2183
2184static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2185 .func = bpf_skb_change_tail,
2186 .gpl_only = false,
2187 .ret_type = RET_INTEGER,
2188 .arg1_type = ARG_PTR_TO_CTX,
2189 .arg2_type = ARG_ANYTHING,
2190 .arg3_type = ARG_ANYTHING,
2191};
2192
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002193bool bpf_helper_changes_skb_data(void *func)
2194{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002195 if (func == bpf_skb_vlan_push ||
2196 func == bpf_skb_vlan_pop ||
2197 func == bpf_skb_store_bytes ||
2198 func == bpf_skb_change_proto ||
2199 func == bpf_skb_change_tail ||
2200 func == bpf_skb_pull_data ||
2201 func == bpf_l3_csum_replace ||
2202 func == bpf_l4_csum_replace)
Daniel Borkmann36976492016-02-19 23:05:25 +01002203 return true;
2204
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002205 return false;
2206}
2207
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002208static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002209 unsigned long off, unsigned long len)
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002210{
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002211 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002212
2213 if (unlikely(!ptr))
2214 return len;
2215 if (ptr != dst_buff)
2216 memcpy(dst_buff, ptr, len);
2217
2218 return 0;
2219}
2220
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002221BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2222 u64, flags, void *, meta, u64, meta_size)
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002223{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002224 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002225
2226 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2227 return -EINVAL;
2228 if (unlikely(skb_size > skb->len))
2229 return -EFAULT;
2230
2231 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2232 bpf_skb_copy);
2233}
2234
2235static const struct bpf_func_proto bpf_skb_event_output_proto = {
2236 .func = bpf_skb_event_output,
2237 .gpl_only = true,
2238 .ret_type = RET_INTEGER,
2239 .arg1_type = ARG_PTR_TO_CTX,
2240 .arg2_type = ARG_CONST_MAP_PTR,
2241 .arg3_type = ARG_ANYTHING,
2242 .arg4_type = ARG_PTR_TO_STACK,
2243 .arg5_type = ARG_CONST_STACK_SIZE,
2244};
2245
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002246static unsigned short bpf_tunnel_key_af(u64 flags)
2247{
2248 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2249}
2250
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002251BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2252 u32, size, u64, flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002253{
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002254 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2255 u8 compat[sizeof(struct bpf_tunnel_key)];
Daniel Borkmann074f5282016-04-13 00:10:52 +02002256 void *to_orig = to;
2257 int err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002258
Daniel Borkmann074f5282016-04-13 00:10:52 +02002259 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2260 err = -EINVAL;
2261 goto err_clear;
2262 }
2263 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2264 err = -EPROTO;
2265 goto err_clear;
2266 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002267 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
Daniel Borkmann074f5282016-04-13 00:10:52 +02002268 err = -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002269 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 Borkmann4018ab12016-03-09 03:00:05 +01002272 goto set_compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002273 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2274 /* Fixup deprecated structure layouts here, so we have
2275 * a common path later on.
2276 */
2277 if (ip_tunnel_info_af(info) != AF_INET)
Daniel Borkmann074f5282016-04-13 00:10:52 +02002278 goto err_clear;
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002279set_compat:
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002280 to = (struct bpf_tunnel_key *)compat;
2281 break;
2282 default:
Daniel Borkmann074f5282016-04-13 00:10:52 +02002283 goto err_clear;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002284 }
2285 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002286
2287 to->tunnel_id = be64_to_cpu(info->key.tun_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002288 to->tunnel_tos = info->key.tos;
2289 to->tunnel_ttl = info->key.ttl;
2290
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002291 if (flags & BPF_F_TUNINFO_IPV6) {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002292 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2293 sizeof(to->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002294 to->tunnel_label = be32_to_cpu(info->key.label);
2295 } else {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002296 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002297 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002298
2299 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
Daniel Borkmann074f5282016-04-13 00:10:52 +02002300 memcpy(to_orig, to, size);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002301
2302 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002303err_clear:
2304 memset(to_orig, 0, size);
2305 return err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002306}
2307
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002308static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002309 .func = bpf_skb_get_tunnel_key,
2310 .gpl_only = false,
2311 .ret_type = RET_INTEGER,
2312 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002313 .arg2_type = ARG_PTR_TO_RAW_STACK,
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002314 .arg3_type = ARG_CONST_STACK_SIZE,
2315 .arg4_type = ARG_ANYTHING,
2316};
2317
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002318BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002319{
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002320 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002321 int err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002322
2323 if (unlikely(!info ||
Daniel Borkmann074f5282016-04-13 00:10:52 +02002324 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2325 err = -ENOENT;
2326 goto err_clear;
2327 }
2328 if (unlikely(size < info->options_len)) {
2329 err = -ENOMEM;
2330 goto err_clear;
2331 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002332
2333 ip_tunnel_info_opts_get(to, info);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002334 if (size > info->options_len)
2335 memset(to + info->options_len, 0, size - info->options_len);
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002336
2337 return info->options_len;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002338err_clear:
2339 memset(to, 0, size);
2340 return err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002341}
2342
2343static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2344 .func = bpf_skb_get_tunnel_opt,
2345 .gpl_only = false,
2346 .ret_type = RET_INTEGER,
2347 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002348 .arg2_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002349 .arg3_type = ARG_CONST_STACK_SIZE,
2350};
2351
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002352static struct metadata_dst __percpu *md_dst;
2353
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002354BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2355 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002356{
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002357 struct metadata_dst *md = this_cpu_ptr(md_dst);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002358 u8 compat[sizeof(struct bpf_tunnel_key)];
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002359 struct ip_tunnel_info *info;
2360
Daniel Borkmann22080872016-03-04 15:15:05 +01002361 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2362 BPF_F_DONT_FRAGMENT)))
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002363 return -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002364 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2365 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002366 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002367 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002368 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2369 /* Fixup deprecated structure layouts here, so we have
2370 * a common path later on.
2371 */
2372 memcpy(compat, from, size);
2373 memset(compat + size, 0, sizeof(compat) - size);
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002374 from = (const struct bpf_tunnel_key *) compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002375 break;
2376 default:
2377 return -EINVAL;
2378 }
2379 }
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002380 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2381 from->tunnel_ext))
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002382 return -EINVAL;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002383
2384 skb_dst_drop(skb);
2385 dst_hold((struct dst_entry *) md);
2386 skb_dst_set(skb, (struct dst_entry *) md);
2387
2388 info = &md->u.tun_info;
2389 info->mode = IP_TUNNEL_INFO_TX;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002390
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002391 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
Daniel Borkmann22080872016-03-04 15:15:05 +01002392 if (flags & BPF_F_DONT_FRAGMENT)
2393 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2394
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002395 info->key.tun_id = cpu_to_be64(from->tunnel_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002396 info->key.tos = from->tunnel_tos;
2397 info->key.ttl = from->tunnel_ttl;
2398
2399 if (flags & BPF_F_TUNINFO_IPV6) {
2400 info->mode |= IP_TUNNEL_INFO_IPV6;
2401 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2402 sizeof(from->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002403 info->key.label = cpu_to_be32(from->tunnel_label) &
2404 IPV6_FLOWLABEL_MASK;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002405 } else {
2406 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
Daniel Borkmann2da897e2016-02-23 02:05:26 +01002407 if (flags & BPF_F_ZERO_CSUM_TX)
2408 info->key.tun_flags &= ~TUNNEL_CSUM;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002409 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002410
2411 return 0;
2412}
2413
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002414static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002415 .func = bpf_skb_set_tunnel_key,
2416 .gpl_only = false,
2417 .ret_type = RET_INTEGER,
2418 .arg1_type = ARG_PTR_TO_CTX,
2419 .arg2_type = ARG_PTR_TO_STACK,
2420 .arg3_type = ARG_CONST_STACK_SIZE,
2421 .arg4_type = ARG_ANYTHING,
2422};
2423
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002424BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2425 const u8 *, from, u32, size)
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002426{
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002427 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2428 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2429
2430 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2431 return -EINVAL;
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002432 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002433 return -ENOMEM;
2434
2435 ip_tunnel_info_opts_set(info, from, size);
2436
2437 return 0;
2438}
2439
2440static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2441 .func = bpf_skb_set_tunnel_opt,
2442 .gpl_only = false,
2443 .ret_type = RET_INTEGER,
2444 .arg1_type = ARG_PTR_TO_CTX,
2445 .arg2_type = ARG_PTR_TO_STACK,
2446 .arg3_type = ARG_CONST_STACK_SIZE,
2447};
2448
2449static const struct bpf_func_proto *
2450bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002451{
2452 if (!md_dst) {
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002453 /* Race is not possible, since it's called from verifier
2454 * that is holding verifier mutex.
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002455 */
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002456 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002457 GFP_KERNEL);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002458 if (!md_dst)
2459 return NULL;
2460 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002461
2462 switch (which) {
2463 case BPF_FUNC_skb_set_tunnel_key:
2464 return &bpf_skb_set_tunnel_key_proto;
2465 case BPF_FUNC_skb_set_tunnel_opt:
2466 return &bpf_skb_set_tunnel_opt_proto;
2467 default:
2468 return NULL;
2469 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002470}
2471
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002472BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2473 u32, idx)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002474{
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002475 struct bpf_array *array = container_of(map, struct bpf_array, map);
2476 struct cgroup *cgrp;
2477 struct sock *sk;
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002478
Daniel Borkmann2d48c5f2016-09-23 01:28:35 +02002479 sk = skb_to_full_sk(skb);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002480 if (!sk || !sk_fullsock(sk))
2481 return -ENOENT;
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002482 if (unlikely(idx >= array->map.max_entries))
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002483 return -E2BIG;
2484
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002485 cgrp = READ_ONCE(array->ptrs[idx]);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002486 if (unlikely(!cgrp))
2487 return -EAGAIN;
2488
Daniel Borkmann54fd9c22016-08-18 01:00:41 +02002489 return sk_under_cgroup_hierarchy(sk, cgrp);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002490}
2491
Daniel Borkmann747ea552016-08-12 22:17:17 +02002492static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2493 .func = bpf_skb_under_cgroup,
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002494 .gpl_only = false,
2495 .ret_type = RET_INTEGER,
2496 .arg1_type = ARG_PTR_TO_CTX,
2497 .arg2_type = ARG_CONST_MAP_PTR,
2498 .arg3_type = ARG_ANYTHING,
2499};
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002500
Daniel Borkmann4de16962016-08-18 01:00:40 +02002501static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2502 unsigned long off, unsigned long len)
2503{
2504 memcpy(dst_buff, src_buff + off, len);
2505 return 0;
2506}
2507
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002508BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2509 u64, flags, void *, meta, u64, meta_size)
Daniel Borkmann4de16962016-08-18 01:00:40 +02002510{
Daniel Borkmann4de16962016-08-18 01:00:40 +02002511 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
Daniel Borkmann4de16962016-08-18 01:00:40 +02002512
2513 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2514 return -EINVAL;
2515 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2516 return -EFAULT;
2517
2518 return bpf_event_output(map, flags, meta, meta_size, xdp, xdp_size,
2519 bpf_xdp_copy);
2520}
2521
2522static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2523 .func = bpf_xdp_event_output,
2524 .gpl_only = true,
2525 .ret_type = RET_INTEGER,
2526 .arg1_type = ARG_PTR_TO_CTX,
2527 .arg2_type = ARG_CONST_MAP_PTR,
2528 .arg3_type = ARG_ANYTHING,
2529 .arg4_type = ARG_PTR_TO_STACK,
2530 .arg5_type = ARG_CONST_STACK_SIZE,
2531};
2532
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002533static const struct bpf_func_proto *
2534sk_filter_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002535{
2536 switch (func_id) {
2537 case BPF_FUNC_map_lookup_elem:
2538 return &bpf_map_lookup_elem_proto;
2539 case BPF_FUNC_map_update_elem:
2540 return &bpf_map_update_elem_proto;
2541 case BPF_FUNC_map_delete_elem:
2542 return &bpf_map_delete_elem_proto;
Daniel Borkmann03e69b52015-03-14 02:27:16 +01002543 case BPF_FUNC_get_prandom_u32:
2544 return &bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01002545 case BPF_FUNC_get_smp_processor_id:
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002546 return &bpf_get_raw_smp_processor_id_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07002547 case BPF_FUNC_tail_call:
2548 return &bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02002549 case BPF_FUNC_ktime_get_ns:
2550 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07002551 case BPF_FUNC_trace_printk:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002552 if (capable(CAP_SYS_ADMIN))
2553 return bpf_get_trace_printk_proto();
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002554 default:
2555 return NULL;
2556 }
2557}
2558
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002559static const struct bpf_func_proto *
2560tc_cls_act_func_proto(enum bpf_func_id func_id)
2561{
2562 switch (func_id) {
2563 case BPF_FUNC_skb_store_bytes:
2564 return &bpf_skb_store_bytes_proto;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01002565 case BPF_FUNC_skb_load_bytes:
2566 return &bpf_skb_load_bytes_proto;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002567 case BPF_FUNC_skb_pull_data:
2568 return &bpf_skb_pull_data_proto;
Daniel Borkmann7d672342016-02-19 23:05:23 +01002569 case BPF_FUNC_csum_diff:
2570 return &bpf_csum_diff_proto;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002571 case BPF_FUNC_csum_update:
2572 return &bpf_csum_update_proto;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07002573 case BPF_FUNC_l3_csum_replace:
2574 return &bpf_l3_csum_replace_proto;
2575 case BPF_FUNC_l4_csum_replace:
2576 return &bpf_l4_csum_replace_proto;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07002577 case BPF_FUNC_clone_redirect:
2578 return &bpf_clone_redirect_proto;
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02002579 case BPF_FUNC_get_cgroup_classid:
2580 return &bpf_get_cgroup_classid_proto;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002581 case BPF_FUNC_skb_vlan_push:
2582 return &bpf_skb_vlan_push_proto;
2583 case BPF_FUNC_skb_vlan_pop:
2584 return &bpf_skb_vlan_pop_proto;
Daniel Borkmann65781712016-06-28 12:18:27 +02002585 case BPF_FUNC_skb_change_proto:
2586 return &bpf_skb_change_proto_proto;
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002587 case BPF_FUNC_skb_change_type:
2588 return &bpf_skb_change_type_proto;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002589 case BPF_FUNC_skb_change_tail:
2590 return &bpf_skb_change_tail_proto;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002591 case BPF_FUNC_skb_get_tunnel_key:
2592 return &bpf_skb_get_tunnel_key_proto;
2593 case BPF_FUNC_skb_set_tunnel_key:
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002594 return bpf_get_skb_set_tunnel_proto(func_id);
2595 case BPF_FUNC_skb_get_tunnel_opt:
2596 return &bpf_skb_get_tunnel_opt_proto;
2597 case BPF_FUNC_skb_set_tunnel_opt:
2598 return bpf_get_skb_set_tunnel_proto(func_id);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07002599 case BPF_FUNC_redirect:
2600 return &bpf_redirect_proto;
Daniel Borkmannc46646d2015-09-30 01:41:51 +02002601 case BPF_FUNC_get_route_realm:
2602 return &bpf_get_route_realm_proto;
Daniel Borkmann13c5c242016-07-03 01:28:47 +02002603 case BPF_FUNC_get_hash_recalc:
2604 return &bpf_get_hash_recalc_proto;
Daniel Borkmann7a4b28c2016-09-23 01:28:37 +02002605 case BPF_FUNC_set_hash_invalid:
2606 return &bpf_set_hash_invalid_proto;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02002607 case BPF_FUNC_perf_event_output:
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002608 return &bpf_skb_event_output_proto;
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002609 case BPF_FUNC_get_smp_processor_id:
2610 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann747ea552016-08-12 22:17:17 +02002611 case BPF_FUNC_skb_under_cgroup:
2612 return &bpf_skb_under_cgroup_proto;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002613 default:
2614 return sk_filter_func_proto(func_id);
2615 }
2616}
2617
Brenden Blanco6a773a12016-07-19 12:16:47 -07002618static const struct bpf_func_proto *
2619xdp_func_proto(enum bpf_func_id func_id)
2620{
Daniel Borkmann4de16962016-08-18 01:00:40 +02002621 switch (func_id) {
2622 case BPF_FUNC_perf_event_output:
2623 return &bpf_xdp_event_output_proto;
Daniel Borkmann669dc4d2016-09-23 01:28:36 +02002624 case BPF_FUNC_get_smp_processor_id:
2625 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann4de16962016-08-18 01:00:40 +02002626 default:
2627 return sk_filter_func_proto(func_id);
2628 }
Brenden Blanco6a773a12016-07-19 12:16:47 -07002629}
2630
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002631static bool __is_valid_access(int off, int size, enum bpf_access_type type)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002632{
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002633 if (off < 0 || off >= sizeof(struct __sk_buff))
2634 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002635 /* The verifier guarantees that size > 0. */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002636 if (off % size != 0)
2637 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002638 if (size != sizeof(__u32))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002639 return false;
2640
2641 return true;
2642}
2643
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002644static bool sk_filter_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002645 enum bpf_access_type type,
2646 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002647{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002648 switch (off) {
2649 case offsetof(struct __sk_buff, tc_classid):
2650 case offsetof(struct __sk_buff, data):
2651 case offsetof(struct __sk_buff, data_end):
Daniel Borkmann045efa82015-09-15 23:05:42 -07002652 return false;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002653 }
Daniel Borkmann045efa82015-09-15 23:05:42 -07002654
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002655 if (type == BPF_WRITE) {
2656 switch (off) {
2657 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann4936e352016-05-13 19:08:26 +02002658 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002659 break;
2660 default:
2661 return false;
2662 }
2663 }
2664
2665 return __is_valid_access(off, size, type);
2666}
2667
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002668static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
2669 const struct bpf_prog *prog)
2670{
2671 struct bpf_insn *insn = insn_buf;
2672
2673 if (!direct_write)
2674 return 0;
2675
2676 /* if (!skb->cloned)
2677 * goto start;
2678 *
2679 * (Fast-path, otherwise approximation that we might be
2680 * a clone, do the rest in helper.)
2681 */
2682 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
2683 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
2684 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
2685
2686 /* ret = bpf_skb_pull_data(skb, 0); */
2687 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
2688 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
2689 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2690 BPF_FUNC_skb_pull_data);
2691 /* if (!ret)
2692 * goto restore;
2693 * return TC_ACT_SHOT;
2694 */
2695 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
2696 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT);
2697 *insn++ = BPF_EXIT_INSN();
2698
2699 /* restore: */
2700 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
2701 /* start: */
2702 *insn++ = prog->insnsi[0];
2703
2704 return insn - insn_buf;
2705}
2706
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002707static bool tc_cls_act_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002708 enum bpf_access_type type,
2709 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002710{
2711 if (type == BPF_WRITE) {
2712 switch (off) {
2713 case offsetof(struct __sk_buff, mark):
2714 case offsetof(struct __sk_buff, tc_index):
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002715 case offsetof(struct __sk_buff, priority):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002716 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002717 offsetof(struct __sk_buff, cb[4]):
2718 case offsetof(struct __sk_buff, tc_classid):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002719 break;
2720 default:
2721 return false;
2722 }
2723 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002724
2725 switch (off) {
2726 case offsetof(struct __sk_buff, data):
2727 *reg_type = PTR_TO_PACKET;
2728 break;
2729 case offsetof(struct __sk_buff, data_end):
2730 *reg_type = PTR_TO_PACKET_END;
2731 break;
2732 }
2733
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002734 return __is_valid_access(off, size, type);
2735}
2736
Brenden Blanco6a773a12016-07-19 12:16:47 -07002737static bool __is_valid_xdp_access(int off, int size,
2738 enum bpf_access_type type)
2739{
2740 if (off < 0 || off >= sizeof(struct xdp_md))
2741 return false;
2742 if (off % size != 0)
2743 return false;
Daniel Borkmann6088b582016-09-09 02:45:28 +02002744 if (size != sizeof(__u32))
Brenden Blanco6a773a12016-07-19 12:16:47 -07002745 return false;
2746
2747 return true;
2748}
2749
2750static bool xdp_is_valid_access(int off, int size,
2751 enum bpf_access_type type,
2752 enum bpf_reg_type *reg_type)
2753{
2754 if (type == BPF_WRITE)
2755 return false;
2756
2757 switch (off) {
2758 case offsetof(struct xdp_md, data):
2759 *reg_type = PTR_TO_PACKET;
2760 break;
2761 case offsetof(struct xdp_md, data_end):
2762 *reg_type = PTR_TO_PACKET_END;
2763 break;
2764 }
2765
2766 return __is_valid_xdp_access(off, size, type);
2767}
2768
2769void bpf_warn_invalid_xdp_action(u32 act)
2770{
2771 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
2772}
2773EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
2774
Daniel Borkmann374fb542016-09-09 02:45:30 +02002775static u32 sk_filter_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2776 int src_reg, int ctx_off,
2777 struct bpf_insn *insn_buf,
2778 struct bpf_prog *prog)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002779{
2780 struct bpf_insn *insn = insn_buf;
2781
2782 switch (ctx_off) {
2783 case offsetof(struct __sk_buff, len):
2784 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2785
2786 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2787 offsetof(struct sk_buff, len));
2788 break;
2789
Daniel Borkmann0b8c7072015-03-19 19:38:27 +01002790 case offsetof(struct __sk_buff, protocol):
2791 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2792
2793 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2794 offsetof(struct sk_buff, protocol));
2795 break;
2796
Michal Sekletar27cd5452015-03-24 14:48:41 +01002797 case offsetof(struct __sk_buff, vlan_proto):
2798 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2799
2800 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2801 offsetof(struct sk_buff, vlan_proto));
2802 break;
2803
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002804 case offsetof(struct __sk_buff, priority):
2805 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2806
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002807 if (type == BPF_WRITE)
2808 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2809 offsetof(struct sk_buff, priority));
2810 else
2811 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2812 offsetof(struct sk_buff, priority));
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002813 break;
2814
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002815 case offsetof(struct __sk_buff, ingress_ifindex):
2816 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2817
2818 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2819 offsetof(struct sk_buff, skb_iif));
2820 break;
2821
2822 case offsetof(struct __sk_buff, ifindex):
2823 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2824
Daniel Borkmannf035a512016-09-09 02:45:29 +02002825 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002826 dst_reg, src_reg,
2827 offsetof(struct sk_buff, dev));
2828 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2829 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2830 offsetof(struct net_device, ifindex));
2831 break;
2832
Daniel Borkmannba7591d2015-08-01 00:46:29 +02002833 case offsetof(struct __sk_buff, hash):
2834 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2835
2836 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2837 offsetof(struct sk_buff, hash));
2838 break;
2839
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002840 case offsetof(struct __sk_buff, mark):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002841 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2842
2843 if (type == BPF_WRITE)
2844 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2845 offsetof(struct sk_buff, mark));
2846 else
2847 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2848 offsetof(struct sk_buff, mark));
2849 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002850
2851 case offsetof(struct __sk_buff, pkt_type):
2852 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2853
2854 case offsetof(struct __sk_buff, queue_mapping):
2855 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002856
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002857 case offsetof(struct __sk_buff, vlan_present):
2858 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2859 dst_reg, src_reg, insn);
2860
2861 case offsetof(struct __sk_buff, vlan_tci):
2862 return convert_skb_access(SKF_AD_VLAN_TAG,
2863 dst_reg, src_reg, insn);
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002864
2865 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann6088b582016-09-09 02:45:28 +02002866 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002867 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2868
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002869 prog->cb_access = 1;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002870 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2871 ctx_off += offsetof(struct sk_buff, cb);
2872 ctx_off += offsetof(struct qdisc_skb_cb, data);
2873 if (type == BPF_WRITE)
2874 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2875 else
2876 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2877 break;
2878
Daniel Borkmann045efa82015-09-15 23:05:42 -07002879 case offsetof(struct __sk_buff, tc_classid):
2880 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2881 ctx_off += offsetof(struct sk_buff, cb);
2882 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002883 if (type == BPF_WRITE)
2884 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2885 else
2886 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
Daniel Borkmann045efa82015-09-15 23:05:42 -07002887 break;
2888
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002889 case offsetof(struct __sk_buff, data):
Daniel Borkmannf035a512016-09-09 02:45:29 +02002890 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002891 dst_reg, src_reg,
2892 offsetof(struct sk_buff, data));
2893 break;
2894
2895 case offsetof(struct __sk_buff, data_end):
2896 ctx_off -= offsetof(struct __sk_buff, data_end);
2897 ctx_off += offsetof(struct sk_buff, cb);
2898 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
Daniel Borkmannf035a512016-09-09 02:45:29 +02002899 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), dst_reg, src_reg,
2900 ctx_off);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002901 break;
2902
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002903 case offsetof(struct __sk_buff, tc_index):
2904#ifdef CONFIG_NET_SCHED
2905 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2906
2907 if (type == BPF_WRITE)
2908 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2909 offsetof(struct sk_buff, tc_index));
2910 else
2911 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2912 offsetof(struct sk_buff, tc_index));
2913 break;
2914#else
2915 if (type == BPF_WRITE)
2916 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2917 else
2918 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2919 break;
2920#endif
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002921 }
2922
2923 return insn - insn_buf;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002924}
2925
Daniel Borkmann374fb542016-09-09 02:45:30 +02002926static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2927 int src_reg, int ctx_off,
2928 struct bpf_insn *insn_buf,
2929 struct bpf_prog *prog)
2930{
2931 struct bpf_insn *insn = insn_buf;
2932
2933 switch (ctx_off) {
2934 case offsetof(struct __sk_buff, ifindex):
2935 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2936
2937 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
2938 dst_reg, src_reg,
2939 offsetof(struct sk_buff, dev));
2940 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2941 offsetof(struct net_device, ifindex));
2942 break;
2943 default:
2944 return sk_filter_convert_ctx_access(type, dst_reg, src_reg,
2945 ctx_off, insn_buf, prog);
2946 }
2947
2948 return insn - insn_buf;
2949}
2950
Brenden Blanco6a773a12016-07-19 12:16:47 -07002951static u32 xdp_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2952 int src_reg, int ctx_off,
2953 struct bpf_insn *insn_buf,
2954 struct bpf_prog *prog)
2955{
2956 struct bpf_insn *insn = insn_buf;
2957
2958 switch (ctx_off) {
2959 case offsetof(struct xdp_md, data):
Daniel Borkmannf035a512016-09-09 02:45:29 +02002960 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
Brenden Blanco6a773a12016-07-19 12:16:47 -07002961 dst_reg, src_reg,
2962 offsetof(struct xdp_buff, data));
2963 break;
2964 case offsetof(struct xdp_md, data_end):
Daniel Borkmannf035a512016-09-09 02:45:29 +02002965 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
Brenden Blanco6a773a12016-07-19 12:16:47 -07002966 dst_reg, src_reg,
2967 offsetof(struct xdp_buff, data_end));
2968 break;
2969 }
2970
2971 return insn - insn_buf;
2972}
2973
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002974static const struct bpf_verifier_ops sk_filter_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002975 .get_func_proto = sk_filter_func_proto,
2976 .is_valid_access = sk_filter_is_valid_access,
Daniel Borkmann374fb542016-09-09 02:45:30 +02002977 .convert_ctx_access = sk_filter_convert_ctx_access,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002978};
2979
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002980static const struct bpf_verifier_ops tc_cls_act_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002981 .get_func_proto = tc_cls_act_func_proto,
2982 .is_valid_access = tc_cls_act_is_valid_access,
Daniel Borkmann374fb542016-09-09 02:45:30 +02002983 .convert_ctx_access = tc_cls_act_convert_ctx_access,
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002984 .gen_prologue = tc_cls_act_prologue,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002985};
2986
Brenden Blanco6a773a12016-07-19 12:16:47 -07002987static const struct bpf_verifier_ops xdp_ops = {
2988 .get_func_proto = xdp_func_proto,
2989 .is_valid_access = xdp_is_valid_access,
2990 .convert_ctx_access = xdp_convert_ctx_access,
2991};
2992
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002993static struct bpf_prog_type_list sk_filter_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002994 .ops = &sk_filter_ops,
2995 .type = BPF_PROG_TYPE_SOCKET_FILTER,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002996};
2997
Daniel Borkmann96be4322015-03-01 12:31:46 +01002998static struct bpf_prog_type_list sched_cls_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02002999 .ops = &tc_cls_act_ops,
3000 .type = BPF_PROG_TYPE_SCHED_CLS,
Daniel Borkmann96be4322015-03-01 12:31:46 +01003001};
3002
Daniel Borkmann94caee82015-03-20 15:11:11 +01003003static struct bpf_prog_type_list sched_act_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003004 .ops = &tc_cls_act_ops,
3005 .type = BPF_PROG_TYPE_SCHED_ACT,
Daniel Borkmann94caee82015-03-20 15:11:11 +01003006};
3007
Brenden Blanco6a773a12016-07-19 12:16:47 -07003008static struct bpf_prog_type_list xdp_type __read_mostly = {
3009 .ops = &xdp_ops,
3010 .type = BPF_PROG_TYPE_XDP,
3011};
3012
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003013static int __init register_sk_filter_ops(void)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003014{
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003015 bpf_register_prog_type(&sk_filter_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01003016 bpf_register_prog_type(&sched_cls_type);
Daniel Borkmann94caee82015-03-20 15:11:11 +01003017 bpf_register_prog_type(&sched_act_type);
Brenden Blanco6a773a12016-07-19 12:16:47 -07003018 bpf_register_prog_type(&xdp_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01003019
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003020 return 0;
3021}
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003022late_initcall(register_sk_filter_ops);
3023
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003024int sk_detach_filter(struct sock *sk)
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003025{
3026 int ret = -ENOENT;
3027 struct sk_filter *filter;
3028
Vincent Bernatd59577b2013-01-16 22:55:49 +01003029 if (sock_flag(sk, SOCK_FILTER_LOCKED))
3030 return -EPERM;
3031
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003032 filter = rcu_dereference_protected(sk->sk_filter,
3033 lockdep_sock_is_held(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003034 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003035 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -08003036 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003037 ret = 0;
3038 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003039
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003040 return ret;
3041}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003042EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003043
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003044int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
3045 unsigned int len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003046{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003047 struct sock_fprog_kern *fprog;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003048 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003049 int ret = 0;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003050
3051 lock_sock(sk);
3052 filter = rcu_dereference_protected(sk->sk_filter,
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003053 lockdep_sock_is_held(sk));
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003054 if (!filter)
3055 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003056
3057 /* We're copying the filter that has been originally attached,
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003058 * so no conversion/decode needed anymore. eBPF programs that
3059 * have no original program cannot be dumped through this.
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003060 */
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003061 ret = -EACCES;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003062 fprog = filter->prog->orig_prog;
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003063 if (!fprog)
3064 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003065
3066 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003067 if (!len)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003068 /* User space only enquires number of filter blocks. */
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003069 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003070
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003071 ret = -EINVAL;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003072 if (len < fprog->len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003073 goto out;
3074
3075 ret = -EFAULT;
Alexei Starovoitov009937e2014-07-30 20:34:13 -07003076 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003077 goto out;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003078
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003079 /* Instead of bytes, the API requests to return the number
3080 * of filter blocks.
3081 */
3082 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003083out:
3084 release_sock(sk);
3085 return ret;
3086}