blob: 5e42e0e54f6f44cc56ae827f68b8f459d48cce06 [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>
Chenbo Feng09313662017-03-22 17:27:34 -070029#include <linux/sock_diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/in.h>
31#include <linux/inet.h>
32#include <linux/netdevice.h>
33#include <linux/if_packet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/ip.h>
36#include <net/protocol.h>
Patrick McHardy4738c1d2008-04-10 02:02:28 -070037#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/skbuff.h>
39#include <net/sock.h>
Jiri Pirko10b89ee42015-05-12 14:56:09 +020040#include <net/flow_dissector.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/errno.h>
42#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/uaccess.h>
Dmitry Mishin40daafc2006-04-18 14:50:10 -070044#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/filter.h>
David S. Miller86e4ca62011-05-26 15:00:31 -040046#include <linux/ratelimit.h>
Will Drewry46b325c2012-04-12 16:47:52 -050047#include <linux/seccomp.h>
Eric Dumazetf3335032012-10-27 02:26:17 +000048#include <linux/if_vlan.h>
Alexei Starovoitov89aa0752014-12-01 15:06:35 -080049#include <linux/bpf.h>
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -070050#include <net/sch_generic.h>
Daniel Borkmann8d20aab2015-07-15 14:21:42 +020051#include <net/cls_cgroup.h>
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -070052#include <net/dst_metadata.h>
Daniel Borkmannc46646d2015-09-30 01:41:51 +020053#include <net/dst.h>
Craig Gallek538950a2016-01-04 17:41:47 -050054#include <net/sock_reuseport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/**
Willem de Bruijnf4979fc2016-07-12 18:18:56 -040057 * sk_filter_trim_cap - run a packet through a socket filter
Stephen Hemminger43db6d62008-04-10 01:43:09 -070058 * @sk: sock associated with &sk_buff
59 * @skb: buffer to filter
Willem de Bruijnf4979fc2016-07-12 18:18:56 -040060 * @cap: limit on how short the eBPF program may trim the packet
Stephen Hemminger43db6d62008-04-10 01:43:09 -070061 *
Alexei Starovoitovff936a02015-10-07 10:55:41 -070062 * Run the eBPF program and then cut skb->data to correct size returned by
63 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
Stephen Hemminger43db6d62008-04-10 01:43:09 -070064 * than pkt_len we keep whole skb->data. This is the socket level
Alexei Starovoitovff936a02015-10-07 10:55:41 -070065 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
Stephen Hemminger43db6d62008-04-10 01:43:09 -070066 * be accepted or -EPERM if the packet should be tossed.
67 *
68 */
Willem de Bruijnf4979fc2016-07-12 18:18:56 -040069int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
Stephen Hemminger43db6d62008-04-10 01:43:09 -070070{
71 int err;
72 struct sk_filter *filter;
73
Mel Gormanc93bdd02012-07-31 16:44:19 -070074 /*
75 * If the skb was allocated from pfmemalloc reserves, only
76 * allow SOCK_MEMALLOC sockets to use it as this socket is
77 * helping free memory
78 */
79 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC))
80 return -ENOMEM;
81
Daniel Mack719a2972016-11-23 16:52:28 +010082 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
83 if (err)
84 return err;
85
Stephen Hemminger43db6d62008-04-10 01:43:09 -070086 err = security_sock_rcv_skb(sk, skb);
87 if (err)
88 return err;
89
Eric Dumazet80f8f102011-01-18 07:46:52 +000090 rcu_read_lock();
91 filter = rcu_dereference(sk->sk_filter);
Stephen Hemminger43db6d62008-04-10 01:43:09 -070092 if (filter) {
Willem de Bruijn2c1289c2017-04-11 14:08:08 -040093 struct sock *save_sk = skb->sk;
94 unsigned int pkt_len;
95
96 skb->sk = sk;
97 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
Willem de Bruijn2c1289c2017-04-11 14:08:08 -040098 skb->sk = save_sk;
Eric Dumazet0e9efe22017-04-26 09:09:23 -070099 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700100 }
Eric Dumazet80f8f102011-01-18 07:46:52 +0000101 rcu_read_unlock();
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700102
103 return err;
104}
Willem de Bruijnf4979fc2016-07-12 18:18:56 -0400105EXPORT_SYMBOL(sk_filter_trim_cap);
Stephen Hemminger43db6d62008-04-10 01:43:09 -0700106
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200107BPF_CALL_1(__skb_get_pay_offset, struct sk_buff *, skb)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100108{
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200109 return skb_get_poff(skb);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100110}
111
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200112BPF_CALL_3(__skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100113{
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100114 struct nlattr *nla;
115
116 if (skb_is_nonlinear(skb))
117 return 0;
118
Mathias Krause05ab8f22014-04-13 18:23:33 +0200119 if (skb->len < sizeof(struct nlattr))
120 return 0;
121
Daniel Borkmann30743832014-05-01 18:34:19 +0200122 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100123 return 0;
124
Daniel Borkmann30743832014-05-01 18:34:19 +0200125 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100126 if (nla)
127 return (void *) nla - (void *) skb->data;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
130}
131
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200132BPF_CALL_3(__skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100133{
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100134 struct nlattr *nla;
135
136 if (skb_is_nonlinear(skb))
137 return 0;
138
Mathias Krause05ab8f22014-04-13 18:23:33 +0200139 if (skb->len < sizeof(struct nlattr))
140 return 0;
141
Daniel Borkmann30743832014-05-01 18:34:19 +0200142 if (a > skb->len - sizeof(struct nlattr))
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100143 return 0;
144
Daniel Borkmann30743832014-05-01 18:34:19 +0200145 nla = (struct nlattr *) &skb->data[a];
146 if (nla->nla_len > skb->len - a)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100147 return 0;
148
Daniel Borkmann30743832014-05-01 18:34:19 +0200149 nla = nla_find_nested(nla, x);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100150 if (nla)
151 return (void *) nla - (void *) skb->data;
152
153 return 0;
154}
155
Daniel Borkmannf3694e02016-09-09 02:45:31 +0200156BPF_CALL_0(__get_raw_cpu_id)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100157{
158 return raw_smp_processor_id();
159}
160
Daniel Borkmann80b48c42016-06-28 12:18:26 +0200161static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
162 .func = __get_raw_cpu_id,
163 .gpl_only = false,
164 .ret_type = RET_INTEGER,
165};
166
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700167static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
168 struct bpf_insn *insn_buf)
169{
170 struct bpf_insn *insn = insn_buf;
171
172 switch (skb_field) {
173 case SKF_AD_MARK:
174 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
175
176 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
177 offsetof(struct sk_buff, mark));
178 break;
179
180 case SKF_AD_PKTTYPE:
181 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
182 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
183#ifdef __BIG_ENDIAN_BITFIELD
184 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
185#endif
186 break;
187
188 case SKF_AD_QUEUE:
189 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
190
191 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
192 offsetof(struct sk_buff, queue_mapping));
193 break;
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700194
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700195 case SKF_AD_VLAN_TAG:
196 case SKF_AD_VLAN_TAG_PRESENT:
197 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
198 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
199
200 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
201 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
202 offsetof(struct sk_buff, vlan_tci));
203 if (skb_field == SKF_AD_VLAN_TAG) {
204 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
205 ~VLAN_TAG_PRESENT);
206 } else {
207 /* dst_reg >>= 12 */
208 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
209 /* dst_reg &= 1 */
210 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
211 }
212 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700213 }
214
215 return insn - insn_buf;
216}
217
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100218static bool convert_bpf_extensions(struct sock_filter *fp,
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700219 struct bpf_insn **insnp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100220{
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700221 struct bpf_insn *insn = *insnp;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700222 u32 cnt;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100223
224 switch (fp->k) {
225 case SKF_AD_OFF + SKF_AD_PROTOCOL:
Daniel Borkmann0b8c7072015-03-19 19:38:27 +0100226 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
227
228 /* A = *(u16 *) (CTX + offsetof(protocol)) */
229 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
230 offsetof(struct sk_buff, protocol));
231 /* A = ntohs(A) [emitting a nop or swap16] */
232 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100233 break;
234
235 case SKF_AD_OFF + SKF_AD_PKTTYPE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700236 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
237 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100238 break;
239
240 case SKF_AD_OFF + SKF_AD_IFINDEX:
241 case SKF_AD_OFF + SKF_AD_HATYPE:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100242 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
243 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
244
Daniel Borkmannf035a512016-09-09 02:45:29 +0200245 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200246 BPF_REG_TMP, BPF_REG_CTX,
247 offsetof(struct sk_buff, dev));
248 /* if (tmp != 0) goto pc + 1 */
249 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
250 *insn++ = BPF_EXIT_INSN();
251 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
252 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
253 offsetof(struct net_device, ifindex));
254 else
255 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
256 offsetof(struct net_device, type));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100257 break;
258
259 case SKF_AD_OFF + SKF_AD_MARK:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700260 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
261 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100262 break;
263
264 case SKF_AD_OFF + SKF_AD_RXHASH:
265 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
266
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700267 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
268 offsetof(struct sk_buff, hash));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100269 break;
270
271 case SKF_AD_OFF + SKF_AD_QUEUE:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700272 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
273 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100274 break;
275
276 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700277 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
278 BPF_REG_A, BPF_REG_CTX, insn);
279 insn += cnt - 1;
280 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100281
Alexei Starovoitovc2497392015-03-16 18:06:02 -0700282 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
283 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
284 BPF_REG_A, BPF_REG_CTX, insn);
285 insn += cnt - 1;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100286 break;
287
Michal Sekletar27cd5452015-03-24 14:48:41 +0100288 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
289 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
290
291 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
292 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
293 offsetof(struct sk_buff, vlan_proto));
294 /* A = ntohs(A) [emitting a nop or swap16] */
295 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
296 break;
297
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100298 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
299 case SKF_AD_OFF + SKF_AD_NLATTR:
300 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
301 case SKF_AD_OFF + SKF_AD_CPU:
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700302 case SKF_AD_OFF + SKF_AD_RANDOM:
Alexei Starovoitove430f342014-06-06 14:46:06 -0700303 /* arg1 = CTX */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200304 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100305 /* arg2 = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200306 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100307 /* arg3 = X */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200308 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
Alexei Starovoitove430f342014-06-06 14:46:06 -0700309 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100310 switch (fp->k) {
311 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200312 *insn = BPF_EMIT_CALL(__skb_get_pay_offset);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100313 break;
314 case SKF_AD_OFF + SKF_AD_NLATTR:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200315 *insn = BPF_EMIT_CALL(__skb_get_nlattr);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100316 break;
317 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200318 *insn = BPF_EMIT_CALL(__skb_get_nlattr_nest);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100319 break;
320 case SKF_AD_OFF + SKF_AD_CPU:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200321 *insn = BPF_EMIT_CALL(__get_raw_cpu_id);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100322 break;
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700323 case SKF_AD_OFF + SKF_AD_RANDOM:
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200324 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
325 bpf_user_rnd_init_once();
Chema Gonzalez4cd36752014-04-21 09:21:24 -0700326 break;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100327 }
328 break;
329
330 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700331 /* A ^= X */
332 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100333 break;
334
335 default:
336 /* This is just a dummy call to avoid letting the compiler
337 * evict __bpf_call_base() as an optimization. Placed here
338 * where no-one bothers.
339 */
340 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
341 return false;
342 }
343
344 *insnp = insn;
345 return true;
346}
347
348/**
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700349 * bpf_convert_filter - convert filter program
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100350 * @prog: the user passed filter program
351 * @len: the length of the user passed filter program
352 * @new_prog: buffer where converted program will be stored
353 * @new_len: pointer to store length of converted program
354 *
355 * Remap 'sock_filter' style BPF instruction set to 'sock_filter_ext' style.
356 * Conversion workflow:
357 *
358 * 1) First pass for calculating the new program length:
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700359 * bpf_convert_filter(old_prog, old_len, NULL, &new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100360 *
361 * 2) 2nd pass to remap in two passes: 1st pass finds new
362 * jump offsets, 2nd pass remapping:
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700363 * new_prog = kmalloc(sizeof(struct bpf_insn) * new_len);
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700364 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100365 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200366static int bpf_convert_filter(struct sock_filter *prog, int len,
367 struct bpf_insn *new_prog, int *new_len)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100368{
369 int new_flen = 0, pass = 0, target, i;
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700370 struct bpf_insn *new_insn;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100371 struct sock_filter *fp;
372 int *addrs = NULL;
373 u8 bpf_src;
374
375 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
Daniel Borkmann30743832014-05-01 18:34:19 +0200376 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100377
Kees Cook6f9a0932014-06-18 15:34:57 -0700378 if (len <= 0 || len > BPF_MAXINSNS)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100379 return -EINVAL;
380
381 if (new_prog) {
Daniel Borkmann658da932015-05-06 16:12:29 +0200382 addrs = kcalloc(len, sizeof(*addrs),
383 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100384 if (!addrs)
385 return -ENOMEM;
386 }
387
388do_pass:
389 new_insn = new_prog;
390 fp = prog;
391
Daniel Borkmann8b614ae2015-12-17 23:51:54 +0100392 /* Classic BPF related prologue emission. */
393 if (new_insn) {
394 /* Classic BPF expects A and X to be reset first. These need
395 * to be guaranteed to be the first two instructions.
396 */
397 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
398 *new_insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
399
400 /* All programs must keep CTX in callee saved BPF_REG_CTX.
401 * In eBPF case it's done by the compiler, here we need to
402 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
403 */
404 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
405 } else {
406 new_insn += 3;
407 }
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100408
409 for (i = 0; i < len; fp++, i++) {
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700410 struct bpf_insn tmp_insns[6] = { };
411 struct bpf_insn *insn = tmp_insns;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100412
413 if (addrs)
414 addrs[i] = new_insn - new_prog;
415
416 switch (fp->code) {
417 /* All arithmetic insns and skb loads map as-is. */
418 case BPF_ALU | BPF_ADD | BPF_X:
419 case BPF_ALU | BPF_ADD | BPF_K:
420 case BPF_ALU | BPF_SUB | BPF_X:
421 case BPF_ALU | BPF_SUB | BPF_K:
422 case BPF_ALU | BPF_AND | BPF_X:
423 case BPF_ALU | BPF_AND | BPF_K:
424 case BPF_ALU | BPF_OR | BPF_X:
425 case BPF_ALU | BPF_OR | BPF_K:
426 case BPF_ALU | BPF_LSH | BPF_X:
427 case BPF_ALU | BPF_LSH | BPF_K:
428 case BPF_ALU | BPF_RSH | BPF_X:
429 case BPF_ALU | BPF_RSH | BPF_K:
430 case BPF_ALU | BPF_XOR | BPF_X:
431 case BPF_ALU | BPF_XOR | BPF_K:
432 case BPF_ALU | BPF_MUL | BPF_X:
433 case BPF_ALU | BPF_MUL | BPF_K:
434 case BPF_ALU | BPF_DIV | BPF_X:
435 case BPF_ALU | BPF_DIV | BPF_K:
436 case BPF_ALU | BPF_MOD | BPF_X:
437 case BPF_ALU | BPF_MOD | BPF_K:
438 case BPF_ALU | BPF_NEG:
439 case BPF_LD | BPF_ABS | BPF_W:
440 case BPF_LD | BPF_ABS | BPF_H:
441 case BPF_LD | BPF_ABS | BPF_B:
442 case BPF_LD | BPF_IND | BPF_W:
443 case BPF_LD | BPF_IND | BPF_H:
444 case BPF_LD | BPF_IND | BPF_B:
445 /* Check for overloaded BPF extension and
446 * directly convert it if found, otherwise
447 * just move on with mapping.
448 */
449 if (BPF_CLASS(fp->code) == BPF_LD &&
450 BPF_MODE(fp->code) == BPF_ABS &&
451 convert_bpf_extensions(fp, &insn))
452 break;
453
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200454 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100455 break;
456
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200457 /* Jump transformation cannot use BPF block macros
458 * everywhere as offset calculation and target updates
459 * require a bit more work than the rest, i.e. jump
460 * opcodes map as-is, but offsets need adjustment.
461 */
462
463#define BPF_EMIT_JMP \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100464 do { \
465 if (target >= len || target < 0) \
466 goto err; \
467 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
468 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
469 insn->off -= insn - tmp_insns; \
470 } while (0)
471
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200472 case BPF_JMP | BPF_JA:
473 target = i + fp->k + 1;
474 insn->code = fp->code;
475 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100476 break;
477
478 case BPF_JMP | BPF_JEQ | BPF_K:
479 case BPF_JMP | BPF_JEQ | BPF_X:
480 case BPF_JMP | BPF_JSET | BPF_K:
481 case BPF_JMP | BPF_JSET | BPF_X:
482 case BPF_JMP | BPF_JGT | BPF_K:
483 case BPF_JMP | BPF_JGT | BPF_X:
484 case BPF_JMP | BPF_JGE | BPF_K:
485 case BPF_JMP | BPF_JGE | BPF_X:
486 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
487 /* BPF immediates are signed, zero extend
488 * immediate into tmp register and use it
489 * in compare insn.
490 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200491 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100492
Alexei Starovoitove430f342014-06-06 14:46:06 -0700493 insn->dst_reg = BPF_REG_A;
494 insn->src_reg = BPF_REG_TMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100495 bpf_src = BPF_X;
496 } else {
Alexei Starovoitove430f342014-06-06 14:46:06 -0700497 insn->dst_reg = BPF_REG_A;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100498 insn->imm = fp->k;
499 bpf_src = BPF_SRC(fp->code);
Tycho Andersen19539ce2015-09-10 18:25:07 -0600500 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100501 }
502
503 /* Common case where 'jump_false' is next insn. */
504 if (fp->jf == 0) {
505 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
506 target = i + fp->jt + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200507 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100508 break;
509 }
510
511 /* Convert JEQ into JNE when 'jump_true' is next insn. */
512 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
513 insn->code = BPF_JMP | BPF_JNE | bpf_src;
514 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200515 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100516 break;
517 }
518
519 /* Other jumps are mapped into two insns: Jxx and JA. */
520 target = i + fp->jt + 1;
521 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200522 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100523 insn++;
524
525 insn->code = BPF_JMP | BPF_JA;
526 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200527 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100528 break;
529
530 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
531 case BPF_LDX | BPF_MSH | BPF_B:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700532 /* tmp = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200533 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
David S. Miller1268e252014-05-13 13:13:33 -0400534 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200535 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700536 /* A &= 0xf */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200537 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700538 /* A <<= 2 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200539 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700540 /* X = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200541 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700542 /* A = tmp */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200543 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100544 break;
545
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100546 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
547 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
548 */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100549 case BPF_RET | BPF_A:
550 case BPF_RET | BPF_K:
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100551 if (BPF_RVAL(fp->code) == BPF_K)
552 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
553 0, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700554 *insn = BPF_EXIT_INSN();
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100555 break;
556
557 /* Store to stack. */
558 case BPF_ST:
559 case BPF_STX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200560 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
561 BPF_ST ? BPF_REG_A : BPF_REG_X,
562 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100563 break;
564
565 /* Load from stack. */
566 case BPF_LD | BPF_MEM:
567 case BPF_LDX | BPF_MEM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200568 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
569 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
570 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100571 break;
572
573 /* A = K or X = K */
574 case BPF_LD | BPF_IMM:
575 case BPF_LDX | BPF_IMM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200576 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
577 BPF_REG_A : BPF_REG_X, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100578 break;
579
580 /* X = A */
581 case BPF_MISC | BPF_TAX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200582 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100583 break;
584
585 /* A = X */
586 case BPF_MISC | BPF_TXA:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200587 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100588 break;
589
590 /* A = skb->len or X = skb->len */
591 case BPF_LD | BPF_W | BPF_LEN:
592 case BPF_LDX | BPF_W | BPF_LEN:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200593 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
594 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
595 offsetof(struct sk_buff, len));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100596 break;
597
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200598 /* Access seccomp_data fields. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100599 case BPF_LDX | BPF_ABS | BPF_W:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700600 /* A = *(u32 *) (ctx + K) */
601 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100602 break;
603
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -0500604 /* Unknown instruction. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100605 default:
606 goto err;
607 }
608
609 insn++;
610 if (new_prog)
611 memcpy(new_insn, tmp_insns,
612 sizeof(*insn) * (insn - tmp_insns));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100613 new_insn += insn - tmp_insns;
614 }
615
616 if (!new_prog) {
617 /* Only calculating new length. */
618 *new_len = new_insn - new_prog;
619 return 0;
620 }
621
622 pass++;
623 if (new_flen != new_insn - new_prog) {
624 new_flen = new_insn - new_prog;
625 if (pass > 2)
626 goto err;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100627 goto do_pass;
628 }
629
630 kfree(addrs);
631 BUG_ON(*new_len != new_flen);
632 return 0;
633err:
634 kfree(addrs);
635 return -EINVAL;
636}
637
638/* Security:
639 *
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000640 * As we dont want to clear mem[] array for each packet going through
Li RongQing8ea6e342014-10-10 13:56:51 +0800641 * __bpf_prog_run(), we check that filter loaded by user never try to read
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000642 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300643 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000644 */
Eric Dumazetec31a052014-07-12 15:49:16 +0200645static int check_load_and_stores(const struct sock_filter *filter, int flen)
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000646{
Daniel Borkmann34805932014-05-29 10:22:50 +0200647 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000648 int pc, ret = 0;
649
650 BUILD_BUG_ON(BPF_MEMWORDS > 16);
Daniel Borkmann34805932014-05-29 10:22:50 +0200651
Tobias Klauser99e72a02014-06-24 15:33:22 +0200652 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000653 if (!masks)
654 return -ENOMEM;
Daniel Borkmann34805932014-05-29 10:22:50 +0200655
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000656 memset(masks, 0xff, flen * sizeof(*masks));
657
658 for (pc = 0; pc < flen; pc++) {
659 memvalid &= masks[pc];
660
661 switch (filter[pc].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200662 case BPF_ST:
663 case BPF_STX:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000664 memvalid |= (1 << filter[pc].k);
665 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200666 case BPF_LD | BPF_MEM:
667 case BPF_LDX | BPF_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000668 if (!(memvalid & (1 << filter[pc].k))) {
669 ret = -EINVAL;
670 goto error;
671 }
672 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200673 case BPF_JMP | BPF_JA:
674 /* A jump must set masks on target */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000675 masks[pc + 1 + filter[pc].k] &= memvalid;
676 memvalid = ~0;
677 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200678 case BPF_JMP | BPF_JEQ | BPF_K:
679 case BPF_JMP | BPF_JEQ | BPF_X:
680 case BPF_JMP | BPF_JGE | BPF_K:
681 case BPF_JMP | BPF_JGE | BPF_X:
682 case BPF_JMP | BPF_JGT | BPF_K:
683 case BPF_JMP | BPF_JGT | BPF_X:
684 case BPF_JMP | BPF_JSET | BPF_K:
685 case BPF_JMP | BPF_JSET | BPF_X:
686 /* A jump must set masks on targets */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000687 masks[pc + 1 + filter[pc].jt] &= memvalid;
688 masks[pc + 1 + filter[pc].jf] &= memvalid;
689 memvalid = ~0;
690 break;
691 }
692 }
693error:
694 kfree(masks);
695 return ret;
696}
697
Daniel Borkmann34805932014-05-29 10:22:50 +0200698static bool chk_code_allowed(u16 code_to_probe)
699{
700 static const bool codes[] = {
701 /* 32 bit ALU operations */
702 [BPF_ALU | BPF_ADD | BPF_K] = true,
703 [BPF_ALU | BPF_ADD | BPF_X] = true,
704 [BPF_ALU | BPF_SUB | BPF_K] = true,
705 [BPF_ALU | BPF_SUB | BPF_X] = true,
706 [BPF_ALU | BPF_MUL | BPF_K] = true,
707 [BPF_ALU | BPF_MUL | BPF_X] = true,
708 [BPF_ALU | BPF_DIV | BPF_K] = true,
709 [BPF_ALU | BPF_DIV | BPF_X] = true,
710 [BPF_ALU | BPF_MOD | BPF_K] = true,
711 [BPF_ALU | BPF_MOD | BPF_X] = true,
712 [BPF_ALU | BPF_AND | BPF_K] = true,
713 [BPF_ALU | BPF_AND | BPF_X] = true,
714 [BPF_ALU | BPF_OR | BPF_K] = true,
715 [BPF_ALU | BPF_OR | BPF_X] = true,
716 [BPF_ALU | BPF_XOR | BPF_K] = true,
717 [BPF_ALU | BPF_XOR | BPF_X] = true,
718 [BPF_ALU | BPF_LSH | BPF_K] = true,
719 [BPF_ALU | BPF_LSH | BPF_X] = true,
720 [BPF_ALU | BPF_RSH | BPF_K] = true,
721 [BPF_ALU | BPF_RSH | BPF_X] = true,
722 [BPF_ALU | BPF_NEG] = true,
723 /* Load instructions */
724 [BPF_LD | BPF_W | BPF_ABS] = true,
725 [BPF_LD | BPF_H | BPF_ABS] = true,
726 [BPF_LD | BPF_B | BPF_ABS] = true,
727 [BPF_LD | BPF_W | BPF_LEN] = true,
728 [BPF_LD | BPF_W | BPF_IND] = true,
729 [BPF_LD | BPF_H | BPF_IND] = true,
730 [BPF_LD | BPF_B | BPF_IND] = true,
731 [BPF_LD | BPF_IMM] = true,
732 [BPF_LD | BPF_MEM] = true,
733 [BPF_LDX | BPF_W | BPF_LEN] = true,
734 [BPF_LDX | BPF_B | BPF_MSH] = true,
735 [BPF_LDX | BPF_IMM] = true,
736 [BPF_LDX | BPF_MEM] = true,
737 /* Store instructions */
738 [BPF_ST] = true,
739 [BPF_STX] = true,
740 /* Misc instructions */
741 [BPF_MISC | BPF_TAX] = true,
742 [BPF_MISC | BPF_TXA] = true,
743 /* Return instructions */
744 [BPF_RET | BPF_K] = true,
745 [BPF_RET | BPF_A] = true,
746 /* Jump instructions */
747 [BPF_JMP | BPF_JA] = true,
748 [BPF_JMP | BPF_JEQ | BPF_K] = true,
749 [BPF_JMP | BPF_JEQ | BPF_X] = true,
750 [BPF_JMP | BPF_JGE | BPF_K] = true,
751 [BPF_JMP | BPF_JGE | BPF_X] = true,
752 [BPF_JMP | BPF_JGT | BPF_K] = true,
753 [BPF_JMP | BPF_JGT | BPF_X] = true,
754 [BPF_JMP | BPF_JSET | BPF_K] = true,
755 [BPF_JMP | BPF_JSET | BPF_X] = true,
756 };
757
758 if (code_to_probe >= ARRAY_SIZE(codes))
759 return false;
760
761 return codes[code_to_probe];
762}
763
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +0200764static bool bpf_check_basics_ok(const struct sock_filter *filter,
765 unsigned int flen)
766{
767 if (filter == NULL)
768 return false;
769 if (flen == 0 || flen > BPF_MAXINSNS)
770 return false;
771
772 return true;
773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/**
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -0700776 * bpf_check_classic - verify socket filter code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * @filter: filter to verify
778 * @flen: length of filter
779 *
780 * Check the user's filter code. If we let some ugly
781 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800782 * no references or jumps that are out of range, no illegal
783 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800785 * All jumps are forward as they are not signed.
786 *
787 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200789static int bpf_check_classic(const struct sock_filter *filter,
790 unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000792 bool anc_found;
Daniel Borkmann34805932014-05-29 10:22:50 +0200793 int pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Daniel Borkmann34805932014-05-29 10:22:50 +0200795 /* Check the filter code now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 for (pc = 0; pc < flen; pc++) {
Eric Dumazetec31a052014-07-12 15:49:16 +0200797 const struct sock_filter *ftest = &filter[pc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Daniel Borkmann34805932014-05-29 10:22:50 +0200799 /* May we actually operate on this code? */
800 if (!chk_code_allowed(ftest->code))
Tetsuo Handacba328f2010-11-16 15:19:51 +0000801 return -EINVAL;
Daniel Borkmann34805932014-05-29 10:22:50 +0200802
Kris Katterjohn93699862006-01-04 13:58:36 -0800803 /* Some instructions need special checks */
Daniel Borkmann34805932014-05-29 10:22:50 +0200804 switch (ftest->code) {
805 case BPF_ALU | BPF_DIV | BPF_K:
806 case BPF_ALU | BPF_MOD | BPF_K:
807 /* Check for division by zero */
Eric Dumazetb6069a92012-09-07 22:03:35 +0000808 if (ftest->k == 0)
809 return -EINVAL;
810 break;
Rabin Vincent229394e2016-01-12 20:17:08 +0100811 case BPF_ALU | BPF_LSH | BPF_K:
812 case BPF_ALU | BPF_RSH | BPF_K:
813 if (ftest->k >= 32)
814 return -EINVAL;
815 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200816 case BPF_LD | BPF_MEM:
817 case BPF_LDX | BPF_MEM:
818 case BPF_ST:
819 case BPF_STX:
820 /* Check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800821 if (ftest->k >= BPF_MEMWORDS)
822 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000823 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200824 case BPF_JMP | BPF_JA:
825 /* Note, the large ftest->k might cause loops.
Kris Katterjohn93699862006-01-04 13:58:36 -0800826 * Compare this with conditional jumps below,
827 * where offsets are limited. --ANK (981016)
828 */
Daniel Borkmann34805932014-05-29 10:22:50 +0200829 if (ftest->k >= (unsigned int)(flen - pc - 1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800830 return -EINVAL;
831 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200832 case BPF_JMP | BPF_JEQ | BPF_K:
833 case BPF_JMP | BPF_JEQ | BPF_X:
834 case BPF_JMP | BPF_JGE | BPF_K:
835 case BPF_JMP | BPF_JGE | BPF_X:
836 case BPF_JMP | BPF_JGT | BPF_K:
837 case BPF_JMP | BPF_JGT | BPF_X:
838 case BPF_JMP | BPF_JSET | BPF_K:
839 case BPF_JMP | BPF_JSET | BPF_X:
840 /* Both conditionals must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000841 if (pc + ftest->jt + 1 >= flen ||
842 pc + ftest->jf + 1 >= flen)
843 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000844 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200845 case BPF_LD | BPF_W | BPF_ABS:
846 case BPF_LD | BPF_H | BPF_ABS:
847 case BPF_LD | BPF_B | BPF_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000848 anc_found = false;
Daniel Borkmann34805932014-05-29 10:22:50 +0200849 if (bpf_anc_helper(ftest) & BPF_ANC)
850 anc_found = true;
851 /* Ancillary operation unknown or unsupported */
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000852 if (anc_found == false && ftest->k >= SKF_AD_OFF)
853 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
Daniel Borkmann34805932014-05-29 10:22:50 +0200857 /* Last instruction must be a RET code */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000858 switch (filter[flen - 1].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200859 case BPF_RET | BPF_K:
860 case BPF_RET | BPF_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000861 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000862 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200863
Tetsuo Handacba328f2010-11-16 15:19:51 +0000864 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700867static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
868 const struct sock_fprog *fprog)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100869{
Alexei Starovoitov009937e2014-07-30 20:34:13 -0700870 unsigned int fsize = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100871 struct sock_fprog_kern *fkprog;
872
873 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
874 if (!fp->orig_prog)
875 return -ENOMEM;
876
877 fkprog = fp->orig_prog;
878 fkprog->len = fprog->len;
Daniel Borkmann658da932015-05-06 16:12:29 +0200879
880 fkprog->filter = kmemdup(fp->insns, fsize,
881 GFP_KERNEL | __GFP_NOWARN);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100882 if (!fkprog->filter) {
883 kfree(fp->orig_prog);
884 return -ENOMEM;
885 }
886
887 return 0;
888}
889
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700890static void bpf_release_orig_filter(struct bpf_prog *fp)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100891{
892 struct sock_fprog_kern *fprog = fp->orig_prog;
893
894 if (fprog) {
895 kfree(fprog->filter);
896 kfree(fprog);
897 }
898}
899
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700900static void __bpf_prog_release(struct bpf_prog *prog)
901{
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100902 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800903 bpf_prog_put(prog);
904 } else {
905 bpf_release_orig_filter(prog);
906 bpf_prog_free(prog);
907 }
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700908}
909
Pablo Neira34c5bd62014-07-29 17:36:28 +0200910static void __sk_filter_release(struct sk_filter *fp)
911{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700912 __bpf_prog_release(fp->prog);
913 kfree(fp);
Pablo Neira34c5bd62014-07-29 17:36:28 +0200914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800917 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700918 * @rcu: rcu_head that contains the sk_filter to free
919 */
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100920static void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700921{
922 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
923
Pablo Neira34c5bd62014-07-29 17:36:28 +0200924 __sk_filter_release(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700925}
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100926
927/**
928 * sk_filter_release - release a socket filter
929 * @fp: filter to remove
930 *
931 * Remove a filter from a socket and release its resources.
932 */
933static void sk_filter_release(struct sk_filter *fp)
934{
935 if (atomic_dec_and_test(&fp->refcnt))
936 call_rcu(&fp->rcu, sk_filter_release_rcu);
937}
938
939void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
940{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700941 u32 filter_size = bpf_prog_size(fp->prog->len);
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700942
943 atomic_sub(filter_size, &sk->sk_omem_alloc);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100944 sk_filter_release(fp);
945}
946
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700947/* try to charge the socket memory if there is space available
948 * return true on success
949 */
950bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100951{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700952 u32 filter_size = bpf_prog_size(fp->prog->len);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700953
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700954 /* same check as in sock_kmalloc() */
955 if (filter_size <= sysctl_optmem_max &&
956 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
957 atomic_inc(&fp->refcnt);
958 atomic_add(filter_size, &sk->sk_omem_alloc);
959 return true;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100960 }
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700961 return false;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100962}
963
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700964static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100965{
966 struct sock_filter *old_prog;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700967 struct bpf_prog *old_fp;
Daniel Borkmann34805932014-05-29 10:22:50 +0200968 int err, new_len, old_len = fp->len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100969
970 /* We are free to overwrite insns et al right here as it
971 * won't be used at this point in time anymore internally
972 * after the migration to the internal BPF instruction
973 * representation.
974 */
975 BUILD_BUG_ON(sizeof(struct sock_filter) !=
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700976 sizeof(struct bpf_insn));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100977
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100978 /* Conversion cannot happen on overlapping memory areas,
979 * so we need to keep the user BPF around until the 2nd
980 * pass. At this time, the user BPF is stored in fp->insns.
981 */
982 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
Daniel Borkmann658da932015-05-06 16:12:29 +0200983 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100984 if (!old_prog) {
985 err = -ENOMEM;
986 goto out_err;
987 }
988
989 /* 1st pass: calculate the new program length. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700990 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100991 if (err)
992 goto out_err_free;
993
994 /* Expand fp for appending the new filter representation. */
995 old_fp = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +0200996 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100997 if (!fp) {
998 /* The old_fp is still around in case we couldn't
999 * allocate new memory, so uncharge on that one.
1000 */
1001 fp = old_fp;
1002 err = -ENOMEM;
1003 goto out_err_free;
1004 }
1005
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001006 fp->len = new_len;
1007
Alexei Starovoitov2695fb52014-07-24 16:38:21 -07001008 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -07001009 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001010 if (err)
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -07001011 /* 2nd bpf_convert_filter() can fail only if it fails
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001012 * to allocate memory, remapping must succeed. Note,
1013 * that at this time old_fp has already been released
Alexei Starovoitov278571b2014-07-30 20:34:12 -07001014 * by krealloc().
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001015 */
1016 goto out_err_free;
1017
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001018 /* We are guaranteed to never error here with cBPF to eBPF
1019 * transitions, since there's no issue with type compatibility
1020 * checks on program arrays.
1021 */
1022 fp = bpf_prog_select_runtime(fp, &err);
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001023
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001024 kfree(old_prog);
1025 return fp;
1026
1027out_err_free:
1028 kfree(old_prog);
1029out_err:
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001030 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001031 return ERR_PTR(err);
1032}
1033
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001034static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1035 bpf_aux_classic_check_t trans)
Jiri Pirko302d6632012-03-31 11:01:19 +00001036{
1037 int err;
1038
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001039 fp->bpf_func = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001040 fp->jited = 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001041
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -07001042 err = bpf_check_classic(fp->insns, fp->len);
Leon Yu418c96a2014-06-01 05:37:25 +00001043 if (err) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001044 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001045 return ERR_PTR(err);
Leon Yu418c96a2014-06-01 05:37:25 +00001046 }
Jiri Pirko302d6632012-03-31 11:01:19 +00001047
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001048 /* There might be additional checks and transformations
1049 * needed on classic filters, f.e. in case of seccomp.
1050 */
1051 if (trans) {
1052 err = trans(fp->insns, fp->len);
1053 if (err) {
1054 __bpf_prog_release(fp);
1055 return ERR_PTR(err);
1056 }
1057 }
1058
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001059 /* Probe if we can JIT compile the filter and if so, do
1060 * the compilation of the filter.
1061 */
Jiri Pirko302d6632012-03-31 11:01:19 +00001062 bpf_jit_compile(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001063
1064 /* JIT compiler couldn't process this filter, so do the
1065 * internal BPF translation for the optimized interpreter.
1066 */
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001067 if (!fp->jited)
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001068 fp = bpf_migrate_filter(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001069
1070 return fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001071}
1072
1073/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001074 * bpf_prog_create - create an unattached filter
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001075 * @pfp: the unattached filter that is created
Tobias Klauser677a9fd2014-06-24 15:33:21 +02001076 * @fprog: the filter program
Jiri Pirko302d6632012-03-31 11:01:19 +00001077 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001078 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +00001079 * sanity checks on it to make sure it does not explode on us later.
1080 * If an error occurs or there is insufficient memory for the filter
1081 * a negative errno code is returned. On success the return is zero.
1082 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001083int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
Jiri Pirko302d6632012-03-31 11:01:19 +00001084{
Alexei Starovoitov009937e2014-07-30 20:34:13 -07001085 unsigned int fsize = bpf_classic_proglen(fprog);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001086 struct bpf_prog *fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001087
1088 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001089 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Jiri Pirko302d6632012-03-31 11:01:19 +00001090 return -EINVAL;
1091
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001092 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Jiri Pirko302d6632012-03-31 11:01:19 +00001093 if (!fp)
1094 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001095
Jiri Pirko302d6632012-03-31 11:01:19 +00001096 memcpy(fp->insns, fprog->filter, fsize);
1097
Jiri Pirko302d6632012-03-31 11:01:19 +00001098 fp->len = fprog->len;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001099 /* Since unattached filters are not copied back to user
1100 * space through sk_get_filter(), we do not need to hold
1101 * a copy here, and can spare us the work.
1102 */
1103 fp->orig_prog = NULL;
Jiri Pirko302d6632012-03-31 11:01:19 +00001104
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001105 /* bpf_prepare_filter() already takes care of freeing
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001106 * memory in case something goes wrong.
1107 */
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001108 fp = bpf_prepare_filter(fp, NULL);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001109 if (IS_ERR(fp))
1110 return PTR_ERR(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001111
1112 *pfp = fp;
1113 return 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001114}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001115EXPORT_SYMBOL_GPL(bpf_prog_create);
Jiri Pirko302d6632012-03-31 11:01:19 +00001116
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001117/**
1118 * bpf_prog_create_from_user - create an unattached filter from user buffer
1119 * @pfp: the unattached filter that is created
1120 * @fprog: the filter program
1121 * @trans: post-classic verifier transformation handler
Daniel Borkmannbab18992015-10-02 15:17:33 +02001122 * @save_orig: save classic BPF program
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001123 *
1124 * This function effectively does the same as bpf_prog_create(), only
1125 * that it builds up its insns buffer from user space provided buffer.
1126 * It also allows for passing a bpf_aux_classic_check_t handler.
1127 */
1128int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
Daniel Borkmannbab18992015-10-02 15:17:33 +02001129 bpf_aux_classic_check_t trans, bool save_orig)
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001130{
1131 unsigned int fsize = bpf_classic_proglen(fprog);
1132 struct bpf_prog *fp;
Daniel Borkmannbab18992015-10-02 15:17:33 +02001133 int err;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001134
1135 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001136 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001137 return -EINVAL;
1138
1139 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1140 if (!fp)
1141 return -ENOMEM;
1142
1143 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1144 __bpf_prog_free(fp);
1145 return -EFAULT;
1146 }
1147
1148 fp->len = fprog->len;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001149 fp->orig_prog = NULL;
1150
Daniel Borkmannbab18992015-10-02 15:17:33 +02001151 if (save_orig) {
1152 err = bpf_prog_store_orig_filter(fp, fprog);
1153 if (err) {
1154 __bpf_prog_free(fp);
1155 return -ENOMEM;
1156 }
1157 }
1158
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001159 /* bpf_prepare_filter() already takes care of freeing
1160 * memory in case something goes wrong.
1161 */
1162 fp = bpf_prepare_filter(fp, trans);
1163 if (IS_ERR(fp))
1164 return PTR_ERR(fp);
1165
1166 *pfp = fp;
1167 return 0;
1168}
David S. Miller2ea273d2015-08-17 14:37:06 -07001169EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001170
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001171void bpf_prog_destroy(struct bpf_prog *fp)
Jiri Pirko302d6632012-03-31 11:01:19 +00001172{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001173 __bpf_prog_release(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001174}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001175EXPORT_SYMBOL_GPL(bpf_prog_destroy);
Jiri Pirko302d6632012-03-31 11:01:19 +00001176
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001177static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001178{
1179 struct sk_filter *fp, *old_fp;
1180
1181 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1182 if (!fp)
1183 return -ENOMEM;
1184
1185 fp->prog = prog;
1186 atomic_set(&fp->refcnt, 0);
1187
1188 if (!sk_filter_charge(sk, fp)) {
1189 kfree(fp);
1190 return -ENOMEM;
1191 }
1192
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001193 old_fp = rcu_dereference_protected(sk->sk_filter,
1194 lockdep_sock_is_held(sk));
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001195 rcu_assign_pointer(sk->sk_filter, fp);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001196
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001197 if (old_fp)
1198 sk_filter_uncharge(sk, old_fp);
1199
1200 return 0;
1201}
1202
Craig Gallek538950a2016-01-04 17:41:47 -05001203static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1204{
1205 struct bpf_prog *old_prog;
1206 int err;
1207
1208 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1209 return -ENOMEM;
1210
Craig Gallekfa463492016-02-10 11:50:39 -05001211 if (sk_unhashed(sk) && sk->sk_reuseport) {
Craig Gallek538950a2016-01-04 17:41:47 -05001212 err = reuseport_alloc(sk);
1213 if (err)
1214 return err;
1215 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1216 /* The socket wasn't bound with SO_REUSEPORT */
1217 return -EINVAL;
1218 }
1219
1220 old_prog = reuseport_attach_prog(sk, prog);
1221 if (old_prog)
1222 bpf_prog_destroy(old_prog);
1223
1224 return 0;
1225}
1226
1227static
1228struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1229{
1230 unsigned int fsize = bpf_classic_proglen(fprog);
Craig Gallek538950a2016-01-04 17:41:47 -05001231 struct bpf_prog *prog;
1232 int err;
1233
1234 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1235 return ERR_PTR(-EPERM);
1236
1237 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001238 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Craig Gallek538950a2016-01-04 17:41:47 -05001239 return ERR_PTR(-EINVAL);
1240
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001241 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Craig Gallek538950a2016-01-04 17:41:47 -05001242 if (!prog)
1243 return ERR_PTR(-ENOMEM);
1244
1245 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1246 __bpf_prog_free(prog);
1247 return ERR_PTR(-EFAULT);
1248 }
1249
1250 prog->len = fprog->len;
1251
1252 err = bpf_prog_store_orig_filter(prog, fprog);
1253 if (err) {
1254 __bpf_prog_free(prog);
1255 return ERR_PTR(-ENOMEM);
1256 }
1257
1258 /* bpf_prepare_filter() already takes care of freeing
1259 * memory in case something goes wrong.
1260 */
1261 return bpf_prepare_filter(prog, NULL);
1262}
1263
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001264/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 * sk_attach_filter - attach a socket filter
1266 * @fprog: the filter program
1267 * @sk: the socket to use
1268 *
1269 * Attach the user's filter code. We first run some sanity checks on
1270 * it to make sure it does not explode on us later. If an error
1271 * occurs or there is insufficient memory for the filter a negative
1272 * errno code is returned. On success the return is zero.
1273 */
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001274int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Craig Gallek538950a2016-01-04 17:41:47 -05001276 struct bpf_prog *prog = __get_filter(fprog, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int err;
1278
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001279 if (IS_ERR(prog))
1280 return PTR_ERR(prog);
1281
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001282 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001283 if (err < 0) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001284 __bpf_prog_release(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001285 return err;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001286 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001287
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001290EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Craig Gallek538950a2016-01-04 17:41:47 -05001292int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001293{
Craig Gallek538950a2016-01-04 17:41:47 -05001294 struct bpf_prog *prog = __get_filter(fprog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001295 int err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001296
Alexei Starovoitov198bf1b2014-12-10 20:14:55 -08001297 if (IS_ERR(prog))
1298 return PTR_ERR(prog);
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001299
Craig Gallek538950a2016-01-04 17:41:47 -05001300 err = __reuseport_attach_prog(prog, sk);
1301 if (err < 0) {
1302 __bpf_prog_release(prog);
1303 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001304 }
1305
Craig Gallek538950a2016-01-04 17:41:47 -05001306 return 0;
1307}
1308
1309static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1310{
Craig Gallek538950a2016-01-04 17:41:47 -05001311 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1312 return ERR_PTR(-EPERM);
1313
Daniel Borkmann113214b2016-06-30 17:24:44 +02001314 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
Craig Gallek538950a2016-01-04 17:41:47 -05001315}
1316
1317int sk_attach_bpf(u32 ufd, struct sock *sk)
1318{
1319 struct bpf_prog *prog = __get_bpf(ufd, sk);
1320 int err;
1321
1322 if (IS_ERR(prog))
1323 return PTR_ERR(prog);
1324
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001325 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001326 if (err < 0) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001327 bpf_prog_put(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001328 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001329 }
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001330
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001331 return 0;
1332}
1333
Craig Gallek538950a2016-01-04 17:41:47 -05001334int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1335{
1336 struct bpf_prog *prog = __get_bpf(ufd, sk);
1337 int err;
1338
1339 if (IS_ERR(prog))
1340 return PTR_ERR(prog);
1341
1342 err = __reuseport_attach_prog(prog, sk);
1343 if (err < 0) {
1344 bpf_prog_put(prog);
1345 return err;
1346 }
1347
1348 return 0;
1349}
1350
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001351struct bpf_scratchpad {
1352 union {
1353 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1354 u8 buff[MAX_BPF_STACK];
1355 };
1356};
1357
1358static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001359
Daniel Borkmann5293efe2016-08-18 01:00:39 +02001360static inline int __bpf_try_make_writable(struct sk_buff *skb,
1361 unsigned int write_len)
1362{
1363 return skb_ensure_writable(skb, write_len);
1364}
1365
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001366static inline int bpf_try_make_writable(struct sk_buff *skb,
1367 unsigned int write_len)
1368{
Daniel Borkmann5293efe2016-08-18 01:00:39 +02001369 int err = __bpf_try_make_writable(skb, write_len);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001370
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001371 bpf_compute_data_end(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001372 return err;
1373}
1374
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001375static int bpf_try_make_head_writable(struct sk_buff *skb)
1376{
1377 return bpf_try_make_writable(skb, skb_headlen(skb));
1378}
1379
Daniel Borkmanna2bfe6b2016-08-05 00:11:11 +02001380static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1381{
1382 if (skb_at_tc_ingress(skb))
1383 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1384}
1385
Daniel Borkmann80656942016-08-05 00:11:13 +02001386static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1387{
1388 if (skb_at_tc_ingress(skb))
1389 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1390}
1391
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001392BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1393 const void *, from, u32, len, u64, flags)
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001394{
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001395 void *ptr;
1396
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001397 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001398 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001399 if (unlikely(offset > 0xffff))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001400 return -EFAULT;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001401 if (unlikely(bpf_try_make_writable(skb, offset + len)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001402 return -EFAULT;
1403
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001404 ptr = skb->data + offset;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001405 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmann479ffcc2016-08-05 00:11:12 +02001406 __skb_postpull_rcsum(skb, ptr, len, offset);
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001407
1408 memcpy(ptr, from, len);
1409
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001410 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmann479ffcc2016-08-05 00:11:12 +02001411 __skb_postpush_rcsum(skb, ptr, len, offset);
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001412 if (flags & BPF_F_INVALIDATE_HASH)
1413 skb_clear_hash(skb);
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001414
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001415 return 0;
1416}
1417
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001418static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001419 .func = bpf_skb_store_bytes,
1420 .gpl_only = false,
1421 .ret_type = RET_INTEGER,
1422 .arg1_type = ARG_PTR_TO_CTX,
1423 .arg2_type = ARG_ANYTHING,
1424 .arg3_type = ARG_PTR_TO_STACK,
1425 .arg4_type = ARG_CONST_STACK_SIZE,
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001426 .arg5_type = ARG_ANYTHING,
1427};
1428
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001429BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1430 void *, to, u32, len)
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001431{
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001432 void *ptr;
1433
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001434 if (unlikely(offset > 0xffff))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001435 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001436
1437 ptr = skb_header_pointer(skb, offset, len, to);
1438 if (unlikely(!ptr))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001439 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001440 if (ptr != to)
1441 memcpy(to, ptr, len);
1442
1443 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02001444err_clear:
1445 memset(to, 0, len);
1446 return -EFAULT;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001447}
1448
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001449static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001450 .func = bpf_skb_load_bytes,
1451 .gpl_only = false,
1452 .ret_type = RET_INTEGER,
1453 .arg1_type = ARG_PTR_TO_CTX,
1454 .arg2_type = ARG_ANYTHING,
Daniel Borkmann074f5282016-04-13 00:10:52 +02001455 .arg3_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001456 .arg4_type = ARG_CONST_STACK_SIZE,
1457};
1458
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001459BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1460{
1461 /* Idea is the following: should the needed direct read/write
1462 * test fail during runtime, we can pull in more data and redo
1463 * again, since implicitly, we invalidate previous checks here.
1464 *
1465 * Or, since we know how much we need to make read/writeable,
1466 * this can be done once at the program beginning for direct
1467 * access case. By this we overcome limitations of only current
1468 * headroom being accessible.
1469 */
1470 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1471}
1472
1473static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1474 .func = bpf_skb_pull_data,
1475 .gpl_only = false,
1476 .ret_type = RET_INTEGER,
1477 .arg1_type = ARG_PTR_TO_CTX,
1478 .arg2_type = ARG_ANYTHING,
1479};
1480
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001481BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1482 u64, from, u64, to, u64, flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001483{
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001484 __sum16 *ptr;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001485
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001486 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1487 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001488 if (unlikely(offset > 0xffff || offset & 1))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001489 return -EFAULT;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001490 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001491 return -EFAULT;
1492
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001493 ptr = (__sum16 *)(skb->data + offset);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001494 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann8050c0f2016-03-04 15:15:02 +01001495 case 0:
1496 if (unlikely(from != 0))
1497 return -EINVAL;
1498
1499 csum_replace_by_diff(ptr, to);
1500 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001501 case 2:
1502 csum_replace2(ptr, from, to);
1503 break;
1504 case 4:
1505 csum_replace4(ptr, from, to);
1506 break;
1507 default:
1508 return -EINVAL;
1509 }
1510
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001511 return 0;
1512}
1513
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001514static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001515 .func = bpf_l3_csum_replace,
1516 .gpl_only = false,
1517 .ret_type = RET_INTEGER,
1518 .arg1_type = ARG_PTR_TO_CTX,
1519 .arg2_type = ARG_ANYTHING,
1520 .arg3_type = ARG_ANYTHING,
1521 .arg4_type = ARG_ANYTHING,
1522 .arg5_type = ARG_ANYTHING,
1523};
1524
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001525BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1526 u64, from, u64, to, u64, flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001527{
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001528 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
Daniel Borkmann2f729592016-02-19 23:05:26 +01001529 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001530 __sum16 *ptr;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001531
Daniel Borkmann2f729592016-02-19 23:05:26 +01001532 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR |
1533 BPF_F_HDR_FIELD_MASK)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001534 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001535 if (unlikely(offset > 0xffff || offset & 1))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001536 return -EFAULT;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001537 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001538 return -EFAULT;
1539
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001540 ptr = (__sum16 *)(skb->data + offset);
Daniel Borkmann2f729592016-02-19 23:05:26 +01001541 if (is_mmzero && !*ptr)
1542 return 0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001543
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001544 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001545 case 0:
1546 if (unlikely(from != 0))
1547 return -EINVAL;
1548
1549 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1550 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001551 case 2:
1552 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1553 break;
1554 case 4:
1555 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1556 break;
1557 default:
1558 return -EINVAL;
1559 }
1560
Daniel Borkmann2f729592016-02-19 23:05:26 +01001561 if (is_mmzero && !*ptr)
1562 *ptr = CSUM_MANGLED_0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001563 return 0;
1564}
1565
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001566static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001567 .func = bpf_l4_csum_replace,
1568 .gpl_only = false,
1569 .ret_type = RET_INTEGER,
1570 .arg1_type = ARG_PTR_TO_CTX,
1571 .arg2_type = ARG_ANYTHING,
1572 .arg3_type = ARG_ANYTHING,
1573 .arg4_type = ARG_ANYTHING,
1574 .arg5_type = ARG_ANYTHING,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001575};
1576
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001577BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1578 __be32 *, to, u32, to_size, __wsum, seed)
Daniel Borkmann7d672342016-02-19 23:05:23 +01001579{
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001580 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001581 u32 diff_size = from_size + to_size;
Daniel Borkmann7d672342016-02-19 23:05:23 +01001582 int i, j = 0;
1583
1584 /* This is quite flexible, some examples:
1585 *
1586 * from_size == 0, to_size > 0, seed := csum --> pushing data
1587 * from_size > 0, to_size == 0, seed := csum --> pulling data
1588 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1589 *
1590 * Even for diffing, from_size and to_size don't need to be equal.
1591 */
1592 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1593 diff_size > sizeof(sp->diff)))
1594 return -EINVAL;
1595
1596 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1597 sp->diff[j] = ~from[i];
1598 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1599 sp->diff[j] = to[i];
1600
1601 return csum_partial(sp->diff, diff_size, seed);
1602}
1603
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001604static const struct bpf_func_proto bpf_csum_diff_proto = {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001605 .func = bpf_csum_diff,
1606 .gpl_only = false,
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001607 .pkt_access = true,
Daniel Borkmann7d672342016-02-19 23:05:23 +01001608 .ret_type = RET_INTEGER,
1609 .arg1_type = ARG_PTR_TO_STACK,
1610 .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1611 .arg3_type = ARG_PTR_TO_STACK,
1612 .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1613 .arg5_type = ARG_ANYTHING,
1614};
1615
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001616BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1617{
1618 /* The interface is to be used in combination with bpf_csum_diff()
1619 * for direct packet writes. csum rotation for alignment as well
1620 * as emulating csum_sub() can be done from the eBPF program.
1621 */
1622 if (skb->ip_summed == CHECKSUM_COMPLETE)
1623 return (skb->csum = csum_add(skb->csum, csum));
1624
1625 return -ENOTSUPP;
1626}
1627
1628static const struct bpf_func_proto bpf_csum_update_proto = {
1629 .func = bpf_csum_update,
1630 .gpl_only = false,
1631 .ret_type = RET_INTEGER,
1632 .arg1_type = ARG_PTR_TO_CTX,
1633 .arg2_type = ARG_ANYTHING,
1634};
1635
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001636static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1637{
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001638 return dev_forward_skb(dev, skb);
1639}
1640
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001641static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1642 struct sk_buff *skb)
1643{
1644 int ret = ____dev_forward_skb(dev, skb);
1645
1646 if (likely(!ret)) {
1647 skb->dev = dev;
1648 ret = netif_rx(skb);
1649 }
1650
1651 return ret;
1652}
1653
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001654static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1655{
1656 int ret;
1657
1658 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1659 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1660 kfree_skb(skb);
1661 return -ENETDOWN;
1662 }
1663
1664 skb->dev = dev;
1665
1666 __this_cpu_inc(xmit_recursion);
1667 ret = dev_queue_xmit(skb);
1668 __this_cpu_dec(xmit_recursion);
1669
1670 return ret;
1671}
1672
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001673static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1674 u32 flags)
1675{
1676 /* skb->mac_len is not set on normal egress */
1677 unsigned int mlen = skb->network_header - skb->mac_header;
1678
1679 __skb_pull(skb, mlen);
1680
1681 /* At ingress, the mac header has already been pulled once.
1682 * At egress, skb_pospull_rcsum has to be done in case that
1683 * the skb is originated from ingress (i.e. a forwarded skb)
1684 * to ensure that rcsum starts at net header.
1685 */
1686 if (!skb_at_tc_ingress(skb))
1687 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1688 skb_pop_mac_header(skb);
1689 skb_reset_mac_len(skb);
1690 return flags & BPF_F_INGRESS ?
1691 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1692}
1693
1694static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1695 u32 flags)
1696{
1697 bpf_push_mac_rcsum(skb);
1698 return flags & BPF_F_INGRESS ?
1699 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1700}
1701
1702static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1703 u32 flags)
1704{
1705 switch (dev->type) {
1706 case ARPHRD_TUNNEL:
1707 case ARPHRD_TUNNEL6:
1708 case ARPHRD_SIT:
1709 case ARPHRD_IPGRE:
1710 case ARPHRD_VOID:
1711 case ARPHRD_NONE:
1712 return __bpf_redirect_no_mac(skb, dev, flags);
1713 default:
1714 return __bpf_redirect_common(skb, dev, flags);
1715 }
1716}
1717
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001718BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001719{
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001720 struct net_device *dev;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001721 struct sk_buff *clone;
1722 int ret;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001723
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001724 if (unlikely(flags & ~(BPF_F_INGRESS)))
1725 return -EINVAL;
1726
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001727 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1728 if (unlikely(!dev))
1729 return -EINVAL;
1730
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001731 clone = skb_clone(skb, GFP_ATOMIC);
1732 if (unlikely(!clone))
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001733 return -ENOMEM;
1734
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001735 /* For direct write, we need to keep the invariant that the skbs
1736 * we're dealing with need to be uncloned. Should uncloning fail
1737 * here, we need to free the just generated clone to unclone once
1738 * again.
1739 */
1740 ret = bpf_try_make_head_writable(skb);
1741 if (unlikely(ret)) {
1742 kfree_skb(clone);
1743 return -ENOMEM;
1744 }
1745
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001746 return __bpf_redirect(clone, dev, flags);
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001747}
1748
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001749static const struct bpf_func_proto bpf_clone_redirect_proto = {
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001750 .func = bpf_clone_redirect,
1751 .gpl_only = false,
1752 .ret_type = RET_INTEGER,
1753 .arg1_type = ARG_PTR_TO_CTX,
1754 .arg2_type = ARG_ANYTHING,
1755 .arg3_type = ARG_ANYTHING,
1756};
1757
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001758struct redirect_info {
1759 u32 ifindex;
1760 u32 flags;
1761};
1762
1763static DEFINE_PER_CPU(struct redirect_info, redirect_info);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001764
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001765BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001766{
1767 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1768
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001769 if (unlikely(flags & ~(BPF_F_INGRESS)))
1770 return TC_ACT_SHOT;
1771
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001772 ri->ifindex = ifindex;
1773 ri->flags = flags;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001774
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001775 return TC_ACT_REDIRECT;
1776}
1777
1778int skb_do_redirect(struct sk_buff *skb)
1779{
1780 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1781 struct net_device *dev;
1782
1783 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1784 ri->ifindex = 0;
1785 if (unlikely(!dev)) {
1786 kfree_skb(skb);
1787 return -EINVAL;
1788 }
1789
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001790 return __bpf_redirect(skb, dev, ri->flags);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001791}
1792
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001793static const struct bpf_func_proto bpf_redirect_proto = {
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001794 .func = bpf_redirect,
1795 .gpl_only = false,
1796 .ret_type = RET_INTEGER,
1797 .arg1_type = ARG_ANYTHING,
1798 .arg2_type = ARG_ANYTHING,
1799};
1800
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001801BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001802{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001803 return task_get_classid(skb);
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001804}
1805
1806static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1807 .func = bpf_get_cgroup_classid,
1808 .gpl_only = false,
1809 .ret_type = RET_INTEGER,
1810 .arg1_type = ARG_PTR_TO_CTX,
1811};
1812
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001813BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001814{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001815 return dst_tclassid(skb);
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001816}
1817
1818static const struct bpf_func_proto bpf_get_route_realm_proto = {
1819 .func = bpf_get_route_realm,
1820 .gpl_only = false,
1821 .ret_type = RET_INTEGER,
1822 .arg1_type = ARG_PTR_TO_CTX,
1823};
1824
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001825BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
Daniel Borkmann13c5c242016-07-03 01:28:47 +02001826{
1827 /* If skb_clear_hash() was called due to mangling, we can
1828 * trigger SW recalculation here. Later access to hash
1829 * can then use the inline skb->hash via context directly
1830 * instead of calling this helper again.
1831 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001832 return skb_get_hash(skb);
Daniel Borkmann13c5c242016-07-03 01:28:47 +02001833}
1834
1835static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1836 .func = bpf_get_hash_recalc,
1837 .gpl_only = false,
1838 .ret_type = RET_INTEGER,
1839 .arg1_type = ARG_PTR_TO_CTX,
1840};
1841
Daniel Borkmann7a4b28c2016-09-23 01:28:37 +02001842BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1843{
1844 /* After all direct packet write, this can be used once for
1845 * triggering a lazy recalc on next skb_get_hash() invocation.
1846 */
1847 skb_clear_hash(skb);
1848 return 0;
1849}
1850
1851static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1852 .func = bpf_set_hash_invalid,
1853 .gpl_only = false,
1854 .ret_type = RET_INTEGER,
1855 .arg1_type = ARG_PTR_TO_CTX,
1856};
1857
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001858BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1859 u16, vlan_tci)
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001860{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001861 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001862
1863 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1864 vlan_proto != htons(ETH_P_8021AD)))
1865 vlan_proto = htons(ETH_P_8021Q);
1866
Daniel Borkmann80656942016-08-05 00:11:13 +02001867 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001868 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
Daniel Borkmann80656942016-08-05 00:11:13 +02001869 bpf_pull_mac_rcsum(skb);
1870
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001871 bpf_compute_data_end(skb);
1872 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001873}
1874
1875const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1876 .func = bpf_skb_vlan_push,
1877 .gpl_only = false,
1878 .ret_type = RET_INTEGER,
1879 .arg1_type = ARG_PTR_TO_CTX,
1880 .arg2_type = ARG_ANYTHING,
1881 .arg3_type = ARG_ANYTHING,
1882};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001883EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001884
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001885BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001886{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001887 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001888
Daniel Borkmann80656942016-08-05 00:11:13 +02001889 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001890 ret = skb_vlan_pop(skb);
Daniel Borkmann80656942016-08-05 00:11:13 +02001891 bpf_pull_mac_rcsum(skb);
1892
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001893 bpf_compute_data_end(skb);
1894 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001895}
1896
1897const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1898 .func = bpf_skb_vlan_pop,
1899 .gpl_only = false,
1900 .ret_type = RET_INTEGER,
1901 .arg1_type = ARG_PTR_TO_CTX,
1902};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001903EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001904
Daniel Borkmann65781712016-06-28 12:18:27 +02001905static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1906{
1907 /* Caller already did skb_cow() with len as headroom,
1908 * so no need to do it here.
1909 */
1910 skb_push(skb, len);
1911 memmove(skb->data, skb->data + len, off);
1912 memset(skb->data + off, 0, len);
1913
1914 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1915 * needed here as it does not change the skb->csum
1916 * result for checksum complete when summing over
1917 * zeroed blocks.
1918 */
1919 return 0;
1920}
1921
1922static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1923{
1924 /* skb_ensure_writable() is not needed here, as we're
1925 * already working on an uncloned skb.
1926 */
1927 if (unlikely(!pskb_may_pull(skb, off + len)))
1928 return -ENOMEM;
1929
1930 skb_postpull_rcsum(skb, skb->data + off, len);
1931 memmove(skb->data + len, skb->data, off);
1932 __skb_pull(skb, len);
1933
1934 return 0;
1935}
1936
1937static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1938{
1939 bool trans_same = skb->transport_header == skb->network_header;
1940 int ret;
1941
1942 /* There's no need for __skb_push()/__skb_pull() pair to
1943 * get to the start of the mac header as we're guaranteed
1944 * to always start from here under eBPF.
1945 */
1946 ret = bpf_skb_generic_push(skb, off, len);
1947 if (likely(!ret)) {
1948 skb->mac_header -= len;
1949 skb->network_header -= len;
1950 if (trans_same)
1951 skb->transport_header = skb->network_header;
1952 }
1953
1954 return ret;
1955}
1956
1957static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1958{
1959 bool trans_same = skb->transport_header == skb->network_header;
1960 int ret;
1961
1962 /* Same here, __skb_push()/__skb_pull() pair not needed. */
1963 ret = bpf_skb_generic_pop(skb, off, len);
1964 if (likely(!ret)) {
1965 skb->mac_header += len;
1966 skb->network_header += len;
1967 if (trans_same)
1968 skb->transport_header = skb->network_header;
1969 }
1970
1971 return ret;
1972}
1973
1974static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
1975{
1976 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1977 u32 off = skb->network_header - skb->mac_header;
1978 int ret;
1979
1980 ret = skb_cow(skb, len_diff);
1981 if (unlikely(ret < 0))
1982 return ret;
1983
1984 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
1985 if (unlikely(ret < 0))
1986 return ret;
1987
1988 if (skb_is_gso(skb)) {
1989 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
1990 * be changed into SKB_GSO_TCPV6.
1991 */
1992 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1993 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
1994 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
1995 }
1996
1997 /* Due to IPv6 header, MSS needs to be downgraded. */
1998 skb_shinfo(skb)->gso_size -= len_diff;
1999 /* Header must be checked, and gso_segs recomputed. */
2000 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2001 skb_shinfo(skb)->gso_segs = 0;
2002 }
2003
2004 skb->protocol = htons(ETH_P_IPV6);
2005 skb_clear_hash(skb);
2006
2007 return 0;
2008}
2009
2010static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2011{
2012 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2013 u32 off = skb->network_header - skb->mac_header;
2014 int ret;
2015
2016 ret = skb_unclone(skb, GFP_ATOMIC);
2017 if (unlikely(ret < 0))
2018 return ret;
2019
2020 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2021 if (unlikely(ret < 0))
2022 return ret;
2023
2024 if (skb_is_gso(skb)) {
2025 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
2026 * be changed into SKB_GSO_TCPV4.
2027 */
2028 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2029 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2030 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2031 }
2032
2033 /* Due to IPv4 header, MSS can be upgraded. */
2034 skb_shinfo(skb)->gso_size += len_diff;
2035 /* Header must be checked, and gso_segs recomputed. */
2036 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2037 skb_shinfo(skb)->gso_segs = 0;
2038 }
2039
2040 skb->protocol = htons(ETH_P_IP);
2041 skb_clear_hash(skb);
2042
2043 return 0;
2044}
2045
2046static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2047{
2048 __be16 from_proto = skb->protocol;
2049
2050 if (from_proto == htons(ETH_P_IP) &&
2051 to_proto == htons(ETH_P_IPV6))
2052 return bpf_skb_proto_4_to_6(skb);
2053
2054 if (from_proto == htons(ETH_P_IPV6) &&
2055 to_proto == htons(ETH_P_IP))
2056 return bpf_skb_proto_6_to_4(skb);
2057
2058 return -ENOTSUPP;
2059}
2060
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002061BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2062 u64, flags)
Daniel Borkmann65781712016-06-28 12:18:27 +02002063{
Daniel Borkmann65781712016-06-28 12:18:27 +02002064 int ret;
2065
2066 if (unlikely(flags))
2067 return -EINVAL;
2068
2069 /* General idea is that this helper does the basic groundwork
2070 * needed for changing the protocol, and eBPF program fills the
2071 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2072 * and other helpers, rather than passing a raw buffer here.
2073 *
2074 * The rationale is to keep this minimal and without a need to
2075 * deal with raw packet data. F.e. even if we would pass buffers
2076 * here, the program still needs to call the bpf_lX_csum_replace()
2077 * helpers anyway. Plus, this way we keep also separation of
2078 * concerns, since f.e. bpf_skb_store_bytes() should only take
2079 * care of stores.
2080 *
2081 * Currently, additional options and extension header space are
2082 * not supported, but flags register is reserved so we can adapt
2083 * that. For offloads, we mark packet as dodgy, so that headers
2084 * need to be verified first.
2085 */
2086 ret = bpf_skb_proto_xlat(skb, proto);
2087 bpf_compute_data_end(skb);
2088 return ret;
2089}
2090
2091static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2092 .func = bpf_skb_change_proto,
2093 .gpl_only = false,
2094 .ret_type = RET_INTEGER,
2095 .arg1_type = ARG_PTR_TO_CTX,
2096 .arg2_type = ARG_ANYTHING,
2097 .arg3_type = ARG_ANYTHING,
2098};
2099
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002100BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002101{
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002102 /* We only allow a restricted subset to be changed for now. */
Daniel Borkmann45c7fff2016-08-18 01:00:38 +02002103 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2104 !skb_pkt_type_ok(pkt_type)))
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002105 return -EINVAL;
2106
2107 skb->pkt_type = pkt_type;
2108 return 0;
2109}
2110
2111static const struct bpf_func_proto bpf_skb_change_type_proto = {
2112 .func = bpf_skb_change_type,
2113 .gpl_only = false,
2114 .ret_type = RET_INTEGER,
2115 .arg1_type = ARG_PTR_TO_CTX,
2116 .arg2_type = ARG_ANYTHING,
2117};
2118
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002119static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2120{
2121 u32 min_len = skb_network_offset(skb);
2122
2123 if (skb_transport_header_was_set(skb))
2124 min_len = skb_transport_offset(skb);
2125 if (skb->ip_summed == CHECKSUM_PARTIAL)
2126 min_len = skb_checksum_start_offset(skb) +
2127 skb->csum_offset + sizeof(__sum16);
2128 return min_len;
2129}
2130
2131static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2132{
Daniel Borkmann6088b582016-09-09 02:45:28 +02002133 return skb->dev->mtu + skb->dev->hard_header_len;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002134}
2135
2136static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2137{
2138 unsigned int old_len = skb->len;
2139 int ret;
2140
2141 ret = __skb_grow_rcsum(skb, new_len);
2142 if (!ret)
2143 memset(skb->data + old_len, 0, new_len - old_len);
2144 return ret;
2145}
2146
2147static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2148{
2149 return __skb_trim_rcsum(skb, new_len);
2150}
2151
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002152BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2153 u64, flags)
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002154{
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002155 u32 max_len = __bpf_skb_max_len(skb);
2156 u32 min_len = __bpf_skb_min_len(skb);
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002157 int ret;
2158
2159 if (unlikely(flags || new_len > max_len || new_len < min_len))
2160 return -EINVAL;
2161 if (skb->encapsulation)
2162 return -ENOTSUPP;
2163
2164 /* The basic idea of this helper is that it's performing the
2165 * needed work to either grow or trim an skb, and eBPF program
2166 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2167 * bpf_lX_csum_replace() and others rather than passing a raw
2168 * buffer here. This one is a slow path helper and intended
2169 * for replies with control messages.
2170 *
2171 * Like in bpf_skb_change_proto(), we want to keep this rather
2172 * minimal and without protocol specifics so that we are able
2173 * to separate concerns as in bpf_skb_store_bytes() should only
2174 * be the one responsible for writing buffers.
2175 *
2176 * It's really expected to be a slow path operation here for
2177 * control message replies, so we're implicitly linearizing,
2178 * uncloning and drop offloads from the skb by this.
2179 */
2180 ret = __bpf_try_make_writable(skb, skb->len);
2181 if (!ret) {
2182 if (new_len > skb->len)
2183 ret = bpf_skb_grow_rcsum(skb, new_len);
2184 else if (new_len < skb->len)
2185 ret = bpf_skb_trim_rcsum(skb, new_len);
2186 if (!ret && skb_is_gso(skb))
2187 skb_gso_reset(skb);
2188 }
2189
2190 bpf_compute_data_end(skb);
2191 return ret;
2192}
2193
2194static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2195 .func = bpf_skb_change_tail,
2196 .gpl_only = false,
2197 .ret_type = RET_INTEGER,
2198 .arg1_type = ARG_PTR_TO_CTX,
2199 .arg2_type = ARG_ANYTHING,
2200 .arg3_type = ARG_ANYTHING,
2201};
2202
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002203bool bpf_helper_changes_skb_data(void *func)
2204{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002205 if (func == bpf_skb_vlan_push ||
2206 func == bpf_skb_vlan_pop ||
2207 func == bpf_skb_store_bytes ||
2208 func == bpf_skb_change_proto ||
2209 func == bpf_skb_change_tail ||
2210 func == bpf_skb_pull_data ||
Daniel Borkmannc1133c62017-05-25 01:05:07 +02002211 func == bpf_clone_redirect ||
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002212 func == bpf_l3_csum_replace ||
2213 func == bpf_l4_csum_replace)
Daniel Borkmann36976492016-02-19 23:05:25 +01002214 return true;
2215
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002216 return false;
2217}
2218
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002219static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002220 unsigned long off, unsigned long len)
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002221{
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002222 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002223
2224 if (unlikely(!ptr))
2225 return len;
2226 if (ptr != dst_buff)
2227 memcpy(dst_buff, ptr, len);
2228
2229 return 0;
2230}
2231
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002232BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2233 u64, flags, void *, meta, u64, meta_size)
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002234{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002235 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002236
2237 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2238 return -EINVAL;
2239 if (unlikely(skb_size > skb->len))
2240 return -EFAULT;
2241
2242 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2243 bpf_skb_copy);
2244}
2245
2246static const struct bpf_func_proto bpf_skb_event_output_proto = {
2247 .func = bpf_skb_event_output,
2248 .gpl_only = true,
2249 .ret_type = RET_INTEGER,
2250 .arg1_type = ARG_PTR_TO_CTX,
2251 .arg2_type = ARG_CONST_MAP_PTR,
2252 .arg3_type = ARG_ANYTHING,
2253 .arg4_type = ARG_PTR_TO_STACK,
2254 .arg5_type = ARG_CONST_STACK_SIZE,
2255};
2256
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002257static unsigned short bpf_tunnel_key_af(u64 flags)
2258{
2259 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2260}
2261
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002262BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2263 u32, size, u64, flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002264{
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002265 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2266 u8 compat[sizeof(struct bpf_tunnel_key)];
Daniel Borkmann074f5282016-04-13 00:10:52 +02002267 void *to_orig = to;
2268 int err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002269
Daniel Borkmann074f5282016-04-13 00:10:52 +02002270 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2271 err = -EINVAL;
2272 goto err_clear;
2273 }
2274 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2275 err = -EPROTO;
2276 goto err_clear;
2277 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002278 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
Daniel Borkmann074f5282016-04-13 00:10:52 +02002279 err = -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002280 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002281 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002282 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002283 goto set_compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002284 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2285 /* Fixup deprecated structure layouts here, so we have
2286 * a common path later on.
2287 */
2288 if (ip_tunnel_info_af(info) != AF_INET)
Daniel Borkmann074f5282016-04-13 00:10:52 +02002289 goto err_clear;
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002290set_compat:
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002291 to = (struct bpf_tunnel_key *)compat;
2292 break;
2293 default:
Daniel Borkmann074f5282016-04-13 00:10:52 +02002294 goto err_clear;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002295 }
2296 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002297
2298 to->tunnel_id = be64_to_cpu(info->key.tun_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002299 to->tunnel_tos = info->key.tos;
2300 to->tunnel_ttl = info->key.ttl;
2301
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002302 if (flags & BPF_F_TUNINFO_IPV6) {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002303 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2304 sizeof(to->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002305 to->tunnel_label = be32_to_cpu(info->key.label);
2306 } else {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002307 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002308 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002309
2310 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
Daniel Borkmann074f5282016-04-13 00:10:52 +02002311 memcpy(to_orig, to, size);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002312
2313 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002314err_clear:
2315 memset(to_orig, 0, size);
2316 return err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002317}
2318
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002319static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002320 .func = bpf_skb_get_tunnel_key,
2321 .gpl_only = false,
2322 .ret_type = RET_INTEGER,
2323 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002324 .arg2_type = ARG_PTR_TO_RAW_STACK,
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002325 .arg3_type = ARG_CONST_STACK_SIZE,
2326 .arg4_type = ARG_ANYTHING,
2327};
2328
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002329BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002330{
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002331 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002332 int err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002333
2334 if (unlikely(!info ||
Daniel Borkmann074f5282016-04-13 00:10:52 +02002335 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2336 err = -ENOENT;
2337 goto err_clear;
2338 }
2339 if (unlikely(size < info->options_len)) {
2340 err = -ENOMEM;
2341 goto err_clear;
2342 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002343
2344 ip_tunnel_info_opts_get(to, info);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002345 if (size > info->options_len)
2346 memset(to + info->options_len, 0, size - info->options_len);
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002347
2348 return info->options_len;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002349err_clear:
2350 memset(to, 0, size);
2351 return err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002352}
2353
2354static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2355 .func = bpf_skb_get_tunnel_opt,
2356 .gpl_only = false,
2357 .ret_type = RET_INTEGER,
2358 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002359 .arg2_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002360 .arg3_type = ARG_CONST_STACK_SIZE,
2361};
2362
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002363static struct metadata_dst __percpu *md_dst;
2364
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002365BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2366 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002367{
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002368 struct metadata_dst *md = this_cpu_ptr(md_dst);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002369 u8 compat[sizeof(struct bpf_tunnel_key)];
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002370 struct ip_tunnel_info *info;
2371
Daniel Borkmann22080872016-03-04 15:15:05 +01002372 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2373 BPF_F_DONT_FRAGMENT)))
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002374 return -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002375 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2376 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002377 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002378 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002379 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2380 /* Fixup deprecated structure layouts here, so we have
2381 * a common path later on.
2382 */
2383 memcpy(compat, from, size);
2384 memset(compat + size, 0, sizeof(compat) - size);
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002385 from = (const struct bpf_tunnel_key *) compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002386 break;
2387 default:
2388 return -EINVAL;
2389 }
2390 }
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002391 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2392 from->tunnel_ext))
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002393 return -EINVAL;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002394
2395 skb_dst_drop(skb);
2396 dst_hold((struct dst_entry *) md);
2397 skb_dst_set(skb, (struct dst_entry *) md);
2398
2399 info = &md->u.tun_info;
2400 info->mode = IP_TUNNEL_INFO_TX;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002401
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002402 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
Daniel Borkmann22080872016-03-04 15:15:05 +01002403 if (flags & BPF_F_DONT_FRAGMENT)
2404 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2405
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002406 info->key.tun_id = cpu_to_be64(from->tunnel_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002407 info->key.tos = from->tunnel_tos;
2408 info->key.ttl = from->tunnel_ttl;
2409
2410 if (flags & BPF_F_TUNINFO_IPV6) {
2411 info->mode |= IP_TUNNEL_INFO_IPV6;
2412 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2413 sizeof(from->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002414 info->key.label = cpu_to_be32(from->tunnel_label) &
2415 IPV6_FLOWLABEL_MASK;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002416 } else {
2417 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
Daniel Borkmann2da897e2016-02-23 02:05:26 +01002418 if (flags & BPF_F_ZERO_CSUM_TX)
2419 info->key.tun_flags &= ~TUNNEL_CSUM;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002420 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002421
2422 return 0;
2423}
2424
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002425static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002426 .func = bpf_skb_set_tunnel_key,
2427 .gpl_only = false,
2428 .ret_type = RET_INTEGER,
2429 .arg1_type = ARG_PTR_TO_CTX,
2430 .arg2_type = ARG_PTR_TO_STACK,
2431 .arg3_type = ARG_CONST_STACK_SIZE,
2432 .arg4_type = ARG_ANYTHING,
2433};
2434
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002435BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2436 const u8 *, from, u32, size)
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002437{
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002438 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2439 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2440
2441 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2442 return -EINVAL;
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002443 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002444 return -ENOMEM;
2445
2446 ip_tunnel_info_opts_set(info, from, size);
2447
2448 return 0;
2449}
2450
2451static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2452 .func = bpf_skb_set_tunnel_opt,
2453 .gpl_only = false,
2454 .ret_type = RET_INTEGER,
2455 .arg1_type = ARG_PTR_TO_CTX,
2456 .arg2_type = ARG_PTR_TO_STACK,
2457 .arg3_type = ARG_CONST_STACK_SIZE,
2458};
2459
2460static const struct bpf_func_proto *
2461bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002462{
2463 if (!md_dst) {
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002464 /* Race is not possible, since it's called from verifier
2465 * that is holding verifier mutex.
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002466 */
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002467 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002468 GFP_KERNEL);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002469 if (!md_dst)
2470 return NULL;
2471 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002472
2473 switch (which) {
2474 case BPF_FUNC_skb_set_tunnel_key:
2475 return &bpf_skb_set_tunnel_key_proto;
2476 case BPF_FUNC_skb_set_tunnel_opt:
2477 return &bpf_skb_set_tunnel_opt_proto;
2478 default:
2479 return NULL;
2480 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002481}
2482
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002483BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2484 u32, idx)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002485{
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002486 struct bpf_array *array = container_of(map, struct bpf_array, map);
2487 struct cgroup *cgrp;
2488 struct sock *sk;
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002489
Daniel Borkmann2d48c5f2016-09-23 01:28:35 +02002490 sk = skb_to_full_sk(skb);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002491 if (!sk || !sk_fullsock(sk))
2492 return -ENOENT;
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002493 if (unlikely(idx >= array->map.max_entries))
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002494 return -E2BIG;
2495
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002496 cgrp = READ_ONCE(array->ptrs[idx]);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002497 if (unlikely(!cgrp))
2498 return -EAGAIN;
2499
Daniel Borkmann54fd9c22016-08-18 01:00:41 +02002500 return sk_under_cgroup_hierarchy(sk, cgrp);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002501}
2502
Daniel Borkmann747ea552016-08-12 22:17:17 +02002503static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2504 .func = bpf_skb_under_cgroup,
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002505 .gpl_only = false,
2506 .ret_type = RET_INTEGER,
2507 .arg1_type = ARG_PTR_TO_CTX,
2508 .arg2_type = ARG_CONST_MAP_PTR,
2509 .arg3_type = ARG_ANYTHING,
2510};
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002511
Daniel Borkmann4de16962016-08-18 01:00:40 +02002512static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2513 unsigned long off, unsigned long len)
2514{
2515 memcpy(dst_buff, src_buff + off, len);
2516 return 0;
2517}
2518
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002519BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2520 u64, flags, void *, meta, u64, meta_size)
Daniel Borkmann4de16962016-08-18 01:00:40 +02002521{
Daniel Borkmann4de16962016-08-18 01:00:40 +02002522 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
Daniel Borkmann4de16962016-08-18 01:00:40 +02002523
2524 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2525 return -EINVAL;
2526 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2527 return -EFAULT;
2528
2529 return bpf_event_output(map, flags, meta, meta_size, xdp, xdp_size,
2530 bpf_xdp_copy);
2531}
2532
2533static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2534 .func = bpf_xdp_event_output,
2535 .gpl_only = true,
2536 .ret_type = RET_INTEGER,
2537 .arg1_type = ARG_PTR_TO_CTX,
2538 .arg2_type = ARG_CONST_MAP_PTR,
2539 .arg3_type = ARG_ANYTHING,
2540 .arg4_type = ARG_PTR_TO_STACK,
2541 .arg5_type = ARG_CONST_STACK_SIZE,
2542};
2543
Chenbo Feng09313662017-03-22 17:27:34 -07002544BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
2545{
2546 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
2547}
2548
2549static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
2550 .func = bpf_get_socket_cookie,
2551 .gpl_only = false,
2552 .ret_type = RET_INTEGER,
2553 .arg1_type = ARG_PTR_TO_CTX,
2554};
2555
Chenbo Fenga29c81e2017-03-22 17:27:35 -07002556BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
2557{
2558 struct sock *sk = sk_to_full_sk(skb->sk);
2559 kuid_t kuid;
2560
2561 if (!sk || !sk_fullsock(sk))
2562 return overflowuid;
2563 kuid = sock_net_uid(sock_net(sk), sk);
2564 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
2565}
2566
2567static const struct bpf_func_proto bpf_get_socket_uid_proto = {
2568 .func = bpf_get_socket_uid,
2569 .gpl_only = false,
2570 .ret_type = RET_INTEGER,
2571 .arg1_type = ARG_PTR_TO_CTX,
2572};
2573
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002574static const struct bpf_func_proto *
2575sk_filter_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002576{
2577 switch (func_id) {
2578 case BPF_FUNC_map_lookup_elem:
2579 return &bpf_map_lookup_elem_proto;
2580 case BPF_FUNC_map_update_elem:
2581 return &bpf_map_update_elem_proto;
2582 case BPF_FUNC_map_delete_elem:
2583 return &bpf_map_delete_elem_proto;
Daniel Borkmann03e69b52015-03-14 02:27:16 +01002584 case BPF_FUNC_get_prandom_u32:
2585 return &bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01002586 case BPF_FUNC_get_smp_processor_id:
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002587 return &bpf_get_raw_smp_processor_id_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07002588 case BPF_FUNC_tail_call:
2589 return &bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02002590 case BPF_FUNC_ktime_get_ns:
2591 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07002592 case BPF_FUNC_trace_printk:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002593 if (capable(CAP_SYS_ADMIN))
2594 return bpf_get_trace_printk_proto();
Chenbo Feng09313662017-03-22 17:27:34 -07002595 case BPF_FUNC_get_socket_cookie:
2596 return &bpf_get_socket_cookie_proto;
Chenbo Fenga29c81e2017-03-22 17:27:35 -07002597 case BPF_FUNC_get_socket_uid:
2598 return &bpf_get_socket_uid_proto;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002599 default:
2600 return NULL;
2601 }
2602}
2603
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002604static const struct bpf_func_proto *
2605tc_cls_act_func_proto(enum bpf_func_id func_id)
2606{
2607 switch (func_id) {
2608 case BPF_FUNC_skb_store_bytes:
2609 return &bpf_skb_store_bytes_proto;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01002610 case BPF_FUNC_skb_load_bytes:
2611 return &bpf_skb_load_bytes_proto;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002612 case BPF_FUNC_skb_pull_data:
2613 return &bpf_skb_pull_data_proto;
Daniel Borkmann7d672342016-02-19 23:05:23 +01002614 case BPF_FUNC_csum_diff:
2615 return &bpf_csum_diff_proto;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002616 case BPF_FUNC_csum_update:
2617 return &bpf_csum_update_proto;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07002618 case BPF_FUNC_l3_csum_replace:
2619 return &bpf_l3_csum_replace_proto;
2620 case BPF_FUNC_l4_csum_replace:
2621 return &bpf_l4_csum_replace_proto;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07002622 case BPF_FUNC_clone_redirect:
2623 return &bpf_clone_redirect_proto;
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02002624 case BPF_FUNC_get_cgroup_classid:
2625 return &bpf_get_cgroup_classid_proto;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002626 case BPF_FUNC_skb_vlan_push:
2627 return &bpf_skb_vlan_push_proto;
2628 case BPF_FUNC_skb_vlan_pop:
2629 return &bpf_skb_vlan_pop_proto;
Daniel Borkmann65781712016-06-28 12:18:27 +02002630 case BPF_FUNC_skb_change_proto:
2631 return &bpf_skb_change_proto_proto;
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002632 case BPF_FUNC_skb_change_type:
2633 return &bpf_skb_change_type_proto;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002634 case BPF_FUNC_skb_change_tail:
2635 return &bpf_skb_change_tail_proto;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002636 case BPF_FUNC_skb_get_tunnel_key:
2637 return &bpf_skb_get_tunnel_key_proto;
2638 case BPF_FUNC_skb_set_tunnel_key:
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002639 return bpf_get_skb_set_tunnel_proto(func_id);
2640 case BPF_FUNC_skb_get_tunnel_opt:
2641 return &bpf_skb_get_tunnel_opt_proto;
2642 case BPF_FUNC_skb_set_tunnel_opt:
2643 return bpf_get_skb_set_tunnel_proto(func_id);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07002644 case BPF_FUNC_redirect:
2645 return &bpf_redirect_proto;
Daniel Borkmannc46646d2015-09-30 01:41:51 +02002646 case BPF_FUNC_get_route_realm:
2647 return &bpf_get_route_realm_proto;
Daniel Borkmann13c5c242016-07-03 01:28:47 +02002648 case BPF_FUNC_get_hash_recalc:
2649 return &bpf_get_hash_recalc_proto;
Daniel Borkmann7a4b28c2016-09-23 01:28:37 +02002650 case BPF_FUNC_set_hash_invalid:
2651 return &bpf_set_hash_invalid_proto;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02002652 case BPF_FUNC_perf_event_output:
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002653 return &bpf_skb_event_output_proto;
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002654 case BPF_FUNC_get_smp_processor_id:
2655 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann747ea552016-08-12 22:17:17 +02002656 case BPF_FUNC_skb_under_cgroup:
2657 return &bpf_skb_under_cgroup_proto;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002658 default:
2659 return sk_filter_func_proto(func_id);
2660 }
2661}
2662
Brenden Blanco6a773a12016-07-19 12:16:47 -07002663static const struct bpf_func_proto *
2664xdp_func_proto(enum bpf_func_id func_id)
2665{
Daniel Borkmann4de16962016-08-18 01:00:40 +02002666 switch (func_id) {
2667 case BPF_FUNC_perf_event_output:
2668 return &bpf_xdp_event_output_proto;
Daniel Borkmann669dc4d2016-09-23 01:28:36 +02002669 case BPF_FUNC_get_smp_processor_id:
2670 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann4de16962016-08-18 01:00:40 +02002671 default:
2672 return sk_filter_func_proto(func_id);
2673 }
Brenden Blanco6a773a12016-07-19 12:16:47 -07002674}
2675
Daniel Mack760b55c2016-11-23 16:52:25 +01002676static const struct bpf_func_proto *
2677cg_skb_func_proto(enum bpf_func_id func_id)
2678{
2679 switch (func_id) {
2680 case BPF_FUNC_skb_load_bytes:
2681 return &bpf_skb_load_bytes_proto;
2682 default:
2683 return sk_filter_func_proto(func_id);
2684 }
2685}
2686
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002687static bool __is_valid_access(int off, int size, enum bpf_access_type type)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002688{
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002689 if (off < 0 || off >= sizeof(struct __sk_buff))
2690 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002691 /* The verifier guarantees that size > 0. */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002692 if (off % size != 0)
2693 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002694 if (size != sizeof(__u32))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002695 return false;
2696
2697 return true;
2698}
2699
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002700static bool sk_filter_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002701 enum bpf_access_type type,
2702 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002703{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002704 switch (off) {
2705 case offsetof(struct __sk_buff, tc_classid):
2706 case offsetof(struct __sk_buff, data):
2707 case offsetof(struct __sk_buff, data_end):
Daniel Borkmann045efa82015-09-15 23:05:42 -07002708 return false;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002709 }
Daniel Borkmann045efa82015-09-15 23:05:42 -07002710
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002711 if (type == BPF_WRITE) {
2712 switch (off) {
2713 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann4936e352016-05-13 19:08:26 +02002714 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002715 break;
2716 default:
2717 return false;
2718 }
2719 }
2720
2721 return __is_valid_access(off, size, type);
2722}
2723
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002724static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
2725 const struct bpf_prog *prog)
2726{
2727 struct bpf_insn *insn = insn_buf;
2728
2729 if (!direct_write)
2730 return 0;
2731
2732 /* if (!skb->cloned)
2733 * goto start;
2734 *
2735 * (Fast-path, otherwise approximation that we might be
2736 * a clone, do the rest in helper.)
2737 */
2738 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
2739 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
2740 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
2741
2742 /* ret = bpf_skb_pull_data(skb, 0); */
2743 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
2744 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
2745 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2746 BPF_FUNC_skb_pull_data);
2747 /* if (!ret)
2748 * goto restore;
2749 * return TC_ACT_SHOT;
2750 */
2751 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
2752 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT);
2753 *insn++ = BPF_EXIT_INSN();
2754
2755 /* restore: */
2756 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
2757 /* start: */
2758 *insn++ = prog->insnsi[0];
2759
2760 return insn - insn_buf;
2761}
2762
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002763static bool tc_cls_act_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002764 enum bpf_access_type type,
2765 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002766{
2767 if (type == BPF_WRITE) {
2768 switch (off) {
2769 case offsetof(struct __sk_buff, mark):
2770 case offsetof(struct __sk_buff, tc_index):
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002771 case offsetof(struct __sk_buff, priority):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002772 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002773 offsetof(struct __sk_buff, cb[4]):
2774 case offsetof(struct __sk_buff, tc_classid):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002775 break;
2776 default:
2777 return false;
2778 }
2779 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002780
2781 switch (off) {
2782 case offsetof(struct __sk_buff, data):
2783 *reg_type = PTR_TO_PACKET;
2784 break;
2785 case offsetof(struct __sk_buff, data_end):
2786 *reg_type = PTR_TO_PACKET_END;
2787 break;
2788 }
2789
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002790 return __is_valid_access(off, size, type);
2791}
2792
Brenden Blanco6a773a12016-07-19 12:16:47 -07002793static bool __is_valid_xdp_access(int off, int size,
2794 enum bpf_access_type type)
2795{
2796 if (off < 0 || off >= sizeof(struct xdp_md))
2797 return false;
2798 if (off % size != 0)
2799 return false;
Daniel Borkmann6088b582016-09-09 02:45:28 +02002800 if (size != sizeof(__u32))
Brenden Blanco6a773a12016-07-19 12:16:47 -07002801 return false;
2802
2803 return true;
2804}
2805
2806static bool xdp_is_valid_access(int off, int size,
2807 enum bpf_access_type type,
2808 enum bpf_reg_type *reg_type)
2809{
2810 if (type == BPF_WRITE)
2811 return false;
2812
2813 switch (off) {
2814 case offsetof(struct xdp_md, data):
2815 *reg_type = PTR_TO_PACKET;
2816 break;
2817 case offsetof(struct xdp_md, data_end):
2818 *reg_type = PTR_TO_PACKET_END;
2819 break;
2820 }
2821
2822 return __is_valid_xdp_access(off, size, type);
2823}
2824
2825void bpf_warn_invalid_xdp_action(u32 act)
2826{
2827 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
2828}
2829EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
2830
Daniel Borkmann374fb542016-09-09 02:45:30 +02002831static u32 sk_filter_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2832 int src_reg, int ctx_off,
2833 struct bpf_insn *insn_buf,
2834 struct bpf_prog *prog)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002835{
2836 struct bpf_insn *insn = insn_buf;
2837
2838 switch (ctx_off) {
2839 case offsetof(struct __sk_buff, len):
2840 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2841
2842 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2843 offsetof(struct sk_buff, len));
2844 break;
2845
Daniel Borkmann0b8c7072015-03-19 19:38:27 +01002846 case offsetof(struct __sk_buff, protocol):
2847 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2848
2849 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2850 offsetof(struct sk_buff, protocol));
2851 break;
2852
Michal Sekletar27cd5452015-03-24 14:48:41 +01002853 case offsetof(struct __sk_buff, vlan_proto):
2854 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2855
2856 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2857 offsetof(struct sk_buff, vlan_proto));
2858 break;
2859
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002860 case offsetof(struct __sk_buff, priority):
2861 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2862
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002863 if (type == BPF_WRITE)
2864 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2865 offsetof(struct sk_buff, priority));
2866 else
2867 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2868 offsetof(struct sk_buff, priority));
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002869 break;
2870
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002871 case offsetof(struct __sk_buff, ingress_ifindex):
2872 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2873
2874 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2875 offsetof(struct sk_buff, skb_iif));
2876 break;
2877
2878 case offsetof(struct __sk_buff, ifindex):
2879 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2880
Daniel Borkmannf035a512016-09-09 02:45:29 +02002881 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002882 dst_reg, src_reg,
2883 offsetof(struct sk_buff, dev));
2884 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2885 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2886 offsetof(struct net_device, ifindex));
2887 break;
2888
Daniel Borkmannba7591d2015-08-01 00:46:29 +02002889 case offsetof(struct __sk_buff, hash):
2890 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2891
2892 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2893 offsetof(struct sk_buff, hash));
2894 break;
2895
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002896 case offsetof(struct __sk_buff, mark):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002897 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2898
2899 if (type == BPF_WRITE)
2900 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2901 offsetof(struct sk_buff, mark));
2902 else
2903 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2904 offsetof(struct sk_buff, mark));
2905 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002906
2907 case offsetof(struct __sk_buff, pkt_type):
2908 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2909
2910 case offsetof(struct __sk_buff, queue_mapping):
2911 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002912
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002913 case offsetof(struct __sk_buff, vlan_present):
2914 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2915 dst_reg, src_reg, insn);
2916
2917 case offsetof(struct __sk_buff, vlan_tci):
2918 return convert_skb_access(SKF_AD_VLAN_TAG,
2919 dst_reg, src_reg, insn);
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002920
2921 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann6088b582016-09-09 02:45:28 +02002922 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002923 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2924
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002925 prog->cb_access = 1;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002926 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2927 ctx_off += offsetof(struct sk_buff, cb);
2928 ctx_off += offsetof(struct qdisc_skb_cb, data);
2929 if (type == BPF_WRITE)
2930 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2931 else
2932 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2933 break;
2934
Daniel Borkmann045efa82015-09-15 23:05:42 -07002935 case offsetof(struct __sk_buff, tc_classid):
2936 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2937 ctx_off += offsetof(struct sk_buff, cb);
2938 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002939 if (type == BPF_WRITE)
2940 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2941 else
2942 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
Daniel Borkmann045efa82015-09-15 23:05:42 -07002943 break;
2944
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002945 case offsetof(struct __sk_buff, data):
Daniel Borkmannf035a512016-09-09 02:45:29 +02002946 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002947 dst_reg, src_reg,
2948 offsetof(struct sk_buff, data));
2949 break;
2950
2951 case offsetof(struct __sk_buff, data_end):
2952 ctx_off -= offsetof(struct __sk_buff, data_end);
2953 ctx_off += offsetof(struct sk_buff, cb);
2954 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
Daniel Borkmannf035a512016-09-09 02:45:29 +02002955 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), dst_reg, src_reg,
2956 ctx_off);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002957 break;
2958
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002959 case offsetof(struct __sk_buff, tc_index):
2960#ifdef CONFIG_NET_SCHED
2961 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2962
2963 if (type == BPF_WRITE)
2964 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2965 offsetof(struct sk_buff, tc_index));
2966 else
2967 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2968 offsetof(struct sk_buff, tc_index));
2969 break;
2970#else
2971 if (type == BPF_WRITE)
2972 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2973 else
2974 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2975 break;
2976#endif
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002977 }
2978
2979 return insn - insn_buf;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002980}
2981
Daniel Borkmann374fb542016-09-09 02:45:30 +02002982static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2983 int src_reg, int ctx_off,
2984 struct bpf_insn *insn_buf,
2985 struct bpf_prog *prog)
2986{
2987 struct bpf_insn *insn = insn_buf;
2988
2989 switch (ctx_off) {
2990 case offsetof(struct __sk_buff, ifindex):
2991 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2992
2993 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
2994 dst_reg, src_reg,
2995 offsetof(struct sk_buff, dev));
2996 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2997 offsetof(struct net_device, ifindex));
2998 break;
2999 default:
3000 return sk_filter_convert_ctx_access(type, dst_reg, src_reg,
3001 ctx_off, insn_buf, prog);
3002 }
3003
3004 return insn - insn_buf;
3005}
3006
Brenden Blanco6a773a12016-07-19 12:16:47 -07003007static u32 xdp_convert_ctx_access(enum bpf_access_type type, int dst_reg,
3008 int src_reg, int ctx_off,
3009 struct bpf_insn *insn_buf,
3010 struct bpf_prog *prog)
3011{
3012 struct bpf_insn *insn = insn_buf;
3013
3014 switch (ctx_off) {
3015 case offsetof(struct xdp_md, data):
Daniel Borkmannf035a512016-09-09 02:45:29 +02003016 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
Brenden Blanco6a773a12016-07-19 12:16:47 -07003017 dst_reg, src_reg,
3018 offsetof(struct xdp_buff, data));
3019 break;
3020 case offsetof(struct xdp_md, data_end):
Daniel Borkmannf035a512016-09-09 02:45:29 +02003021 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
Brenden Blanco6a773a12016-07-19 12:16:47 -07003022 dst_reg, src_reg,
3023 offsetof(struct xdp_buff, data_end));
3024 break;
3025 }
3026
3027 return insn - insn_buf;
3028}
3029
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003030static const struct bpf_verifier_ops sk_filter_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003031 .get_func_proto = sk_filter_func_proto,
3032 .is_valid_access = sk_filter_is_valid_access,
Daniel Borkmann374fb542016-09-09 02:45:30 +02003033 .convert_ctx_access = sk_filter_convert_ctx_access,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003034};
3035
Alexei Starovoitov608cd712015-03-26 19:53:57 -07003036static const struct bpf_verifier_ops tc_cls_act_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003037 .get_func_proto = tc_cls_act_func_proto,
3038 .is_valid_access = tc_cls_act_is_valid_access,
Daniel Borkmann374fb542016-09-09 02:45:30 +02003039 .convert_ctx_access = tc_cls_act_convert_ctx_access,
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003040 .gen_prologue = tc_cls_act_prologue,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07003041};
3042
Brenden Blanco6a773a12016-07-19 12:16:47 -07003043static const struct bpf_verifier_ops xdp_ops = {
3044 .get_func_proto = xdp_func_proto,
3045 .is_valid_access = xdp_is_valid_access,
3046 .convert_ctx_access = xdp_convert_ctx_access,
3047};
3048
Daniel Mack760b55c2016-11-23 16:52:25 +01003049static const struct bpf_verifier_ops cg_skb_ops = {
3050 .get_func_proto = cg_skb_func_proto,
3051 .is_valid_access = sk_filter_is_valid_access,
3052 .convert_ctx_access = sk_filter_convert_ctx_access,
3053};
3054
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003055static struct bpf_prog_type_list sk_filter_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003056 .ops = &sk_filter_ops,
3057 .type = BPF_PROG_TYPE_SOCKET_FILTER,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003058};
3059
Daniel Borkmann96be4322015-03-01 12:31:46 +01003060static struct bpf_prog_type_list sched_cls_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003061 .ops = &tc_cls_act_ops,
3062 .type = BPF_PROG_TYPE_SCHED_CLS,
Daniel Borkmann96be4322015-03-01 12:31:46 +01003063};
3064
Daniel Borkmann94caee82015-03-20 15:11:11 +01003065static struct bpf_prog_type_list sched_act_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003066 .ops = &tc_cls_act_ops,
3067 .type = BPF_PROG_TYPE_SCHED_ACT,
Daniel Borkmann94caee82015-03-20 15:11:11 +01003068};
3069
Brenden Blanco6a773a12016-07-19 12:16:47 -07003070static struct bpf_prog_type_list xdp_type __read_mostly = {
3071 .ops = &xdp_ops,
3072 .type = BPF_PROG_TYPE_XDP,
3073};
3074
Daniel Mack760b55c2016-11-23 16:52:25 +01003075static struct bpf_prog_type_list cg_skb_type __read_mostly = {
3076 .ops = &cg_skb_ops,
3077 .type = BPF_PROG_TYPE_CGROUP_SKB,
3078};
3079
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003080static int __init register_sk_filter_ops(void)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003081{
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003082 bpf_register_prog_type(&sk_filter_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01003083 bpf_register_prog_type(&sched_cls_type);
Daniel Borkmann94caee82015-03-20 15:11:11 +01003084 bpf_register_prog_type(&sched_act_type);
Brenden Blanco6a773a12016-07-19 12:16:47 -07003085 bpf_register_prog_type(&xdp_type);
Daniel Mack760b55c2016-11-23 16:52:25 +01003086 bpf_register_prog_type(&cg_skb_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01003087
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003088 return 0;
3089}
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003090late_initcall(register_sk_filter_ops);
3091
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003092int sk_detach_filter(struct sock *sk)
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003093{
3094 int ret = -ENOENT;
3095 struct sk_filter *filter;
3096
Vincent Bernatd59577b2013-01-16 22:55:49 +01003097 if (sock_flag(sk, SOCK_FILTER_LOCKED))
3098 return -EPERM;
3099
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003100 filter = rcu_dereference_protected(sk->sk_filter,
3101 lockdep_sock_is_held(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003102 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003103 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -08003104 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003105 ret = 0;
3106 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003107
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003108 return ret;
3109}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003110EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003111
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003112int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
3113 unsigned int len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003114{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003115 struct sock_fprog_kern *fprog;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003116 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003117 int ret = 0;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003118
3119 lock_sock(sk);
3120 filter = rcu_dereference_protected(sk->sk_filter,
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003121 lockdep_sock_is_held(sk));
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003122 if (!filter)
3123 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003124
3125 /* We're copying the filter that has been originally attached,
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003126 * so no conversion/decode needed anymore. eBPF programs that
3127 * have no original program cannot be dumped through this.
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003128 */
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003129 ret = -EACCES;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003130 fprog = filter->prog->orig_prog;
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003131 if (!fprog)
3132 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003133
3134 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003135 if (!len)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003136 /* User space only enquires number of filter blocks. */
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003137 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003138
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003139 ret = -EINVAL;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003140 if (len < fprog->len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003141 goto out;
3142
3143 ret = -EFAULT;
Alexei Starovoitov009937e2014-07-30 20:34:13 -07003144 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003145 goto out;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003146
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003147 /* Instead of bytes, the API requests to return the number
3148 * of filter blocks.
3149 */
3150 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003151out:
3152 release_sock(sk);
3153 return ret;
3154}