blob: c385c55af653b569651f28f31b92de9d181c5b1c [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
Alexei Starovoitov265d7652018-01-29 02:49:00 +0100454 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
455 fp->code == (BPF_ALU | BPF_MOD | BPF_X))
456 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
457
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200458 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100459 break;
460
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200461 /* Jump transformation cannot use BPF block macros
462 * everywhere as offset calculation and target updates
463 * require a bit more work than the rest, i.e. jump
464 * opcodes map as-is, but offsets need adjustment.
465 */
466
467#define BPF_EMIT_JMP \
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100468 do { \
469 if (target >= len || target < 0) \
470 goto err; \
471 insn->off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
472 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
473 insn->off -= insn - tmp_insns; \
474 } while (0)
475
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200476 case BPF_JMP | BPF_JA:
477 target = i + fp->k + 1;
478 insn->code = fp->code;
479 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100480 break;
481
482 case BPF_JMP | BPF_JEQ | BPF_K:
483 case BPF_JMP | BPF_JEQ | BPF_X:
484 case BPF_JMP | BPF_JSET | BPF_K:
485 case BPF_JMP | BPF_JSET | BPF_X:
486 case BPF_JMP | BPF_JGT | BPF_K:
487 case BPF_JMP | BPF_JGT | BPF_X:
488 case BPF_JMP | BPF_JGE | BPF_K:
489 case BPF_JMP | BPF_JGE | BPF_X:
490 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
491 /* BPF immediates are signed, zero extend
492 * immediate into tmp register and use it
493 * in compare insn.
494 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200495 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100496
Alexei Starovoitove430f342014-06-06 14:46:06 -0700497 insn->dst_reg = BPF_REG_A;
498 insn->src_reg = BPF_REG_TMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100499 bpf_src = BPF_X;
500 } else {
Alexei Starovoitove430f342014-06-06 14:46:06 -0700501 insn->dst_reg = BPF_REG_A;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100502 insn->imm = fp->k;
503 bpf_src = BPF_SRC(fp->code);
Tycho Andersen19539ce2015-09-10 18:25:07 -0600504 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100505 }
506
507 /* Common case where 'jump_false' is next insn. */
508 if (fp->jf == 0) {
509 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
510 target = i + fp->jt + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200511 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100512 break;
513 }
514
515 /* Convert JEQ into JNE when 'jump_true' is next insn. */
516 if (fp->jt == 0 && BPF_OP(fp->code) == BPF_JEQ) {
517 insn->code = BPF_JMP | BPF_JNE | bpf_src;
518 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200519 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100520 break;
521 }
522
523 /* Other jumps are mapped into two insns: Jxx and JA. */
524 target = i + fp->jt + 1;
525 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200526 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100527 insn++;
528
529 insn->code = BPF_JMP | BPF_JA;
530 target = i + fp->jf + 1;
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200531 BPF_EMIT_JMP;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100532 break;
533
534 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
535 case BPF_LDX | BPF_MSH | BPF_B:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700536 /* tmp = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200537 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_A);
David S. Miller1268e252014-05-13 13:13:33 -0400538 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200539 *insn++ = BPF_LD_ABS(BPF_B, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700540 /* A &= 0xf */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200541 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700542 /* A <<= 2 */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200543 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700544 /* X = A */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200545 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700546 /* A = tmp */
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200547 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100548 break;
549
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100550 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
551 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
552 */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100553 case BPF_RET | BPF_A:
554 case BPF_RET | BPF_K:
Daniel Borkmann6205b9c2016-02-19 23:05:27 +0100555 if (BPF_RVAL(fp->code) == BPF_K)
556 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
557 0, fp->k);
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700558 *insn = BPF_EXIT_INSN();
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100559 break;
560
561 /* Store to stack. */
562 case BPF_ST:
563 case BPF_STX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200564 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
565 BPF_ST ? BPF_REG_A : BPF_REG_X,
566 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100567 break;
568
569 /* Load from stack. */
570 case BPF_LD | BPF_MEM:
571 case BPF_LDX | BPF_MEM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200572 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
573 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
574 -(BPF_MEMWORDS - fp->k) * 4);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100575 break;
576
577 /* A = K or X = K */
578 case BPF_LD | BPF_IMM:
579 case BPF_LDX | BPF_IMM:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200580 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
581 BPF_REG_A : BPF_REG_X, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100582 break;
583
584 /* X = A */
585 case BPF_MISC | BPF_TAX:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200586 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100587 break;
588
589 /* A = X */
590 case BPF_MISC | BPF_TXA:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200591 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100592 break;
593
594 /* A = skb->len or X = skb->len */
595 case BPF_LD | BPF_W | BPF_LEN:
596 case BPF_LDX | BPF_W | BPF_LEN:
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200597 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
598 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
599 offsetof(struct sk_buff, len));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100600 break;
601
Daniel Borkmannf8f6d672014-05-29 10:22:51 +0200602 /* Access seccomp_data fields. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100603 case BPF_LDX | BPF_ABS | BPF_W:
Alexei Starovoitov9739eef2014-05-08 14:10:51 -0700604 /* A = *(u32 *) (ctx + K) */
605 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100606 break;
607
Stephen Hemmingerca9f1fd2015-02-14 13:47:54 -0500608 /* Unknown instruction. */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100609 default:
610 goto err;
611 }
612
613 insn++;
614 if (new_prog)
615 memcpy(new_insn, tmp_insns,
616 sizeof(*insn) * (insn - tmp_insns));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100617 new_insn += insn - tmp_insns;
618 }
619
620 if (!new_prog) {
621 /* Only calculating new length. */
622 *new_len = new_insn - new_prog;
623 return 0;
624 }
625
626 pass++;
627 if (new_flen != new_insn - new_prog) {
628 new_flen = new_insn - new_prog;
629 if (pass > 2)
630 goto err;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100631 goto do_pass;
632 }
633
634 kfree(addrs);
635 BUG_ON(*new_len != new_flen);
636 return 0;
637err:
638 kfree(addrs);
639 return -EINVAL;
640}
641
642/* Security:
643 *
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000644 * As we dont want to clear mem[] array for each packet going through
Li RongQing8ea6e342014-10-10 13:56:51 +0800645 * __bpf_prog_run(), we check that filter loaded by user never try to read
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000646 * a cell if not previously written, and we check all branches to be sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300647 * a malicious user doesn't try to abuse us.
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000648 */
Eric Dumazetec31a052014-07-12 15:49:16 +0200649static int check_load_and_stores(const struct sock_filter *filter, int flen)
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000650{
Daniel Borkmann34805932014-05-29 10:22:50 +0200651 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000652 int pc, ret = 0;
653
654 BUILD_BUG_ON(BPF_MEMWORDS > 16);
Daniel Borkmann34805932014-05-29 10:22:50 +0200655
Tobias Klauser99e72a02014-06-24 15:33:22 +0200656 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000657 if (!masks)
658 return -ENOMEM;
Daniel Borkmann34805932014-05-29 10:22:50 +0200659
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000660 memset(masks, 0xff, flen * sizeof(*masks));
661
662 for (pc = 0; pc < flen; pc++) {
663 memvalid &= masks[pc];
664
665 switch (filter[pc].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200666 case BPF_ST:
667 case BPF_STX:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000668 memvalid |= (1 << filter[pc].k);
669 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200670 case BPF_LD | BPF_MEM:
671 case BPF_LDX | BPF_MEM:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000672 if (!(memvalid & (1 << filter[pc].k))) {
673 ret = -EINVAL;
674 goto error;
675 }
676 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200677 case BPF_JMP | BPF_JA:
678 /* A jump must set masks on target */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000679 masks[pc + 1 + filter[pc].k] &= memvalid;
680 memvalid = ~0;
681 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200682 case BPF_JMP | BPF_JEQ | BPF_K:
683 case BPF_JMP | BPF_JEQ | BPF_X:
684 case BPF_JMP | BPF_JGE | BPF_K:
685 case BPF_JMP | BPF_JGE | BPF_X:
686 case BPF_JMP | BPF_JGT | BPF_K:
687 case BPF_JMP | BPF_JGT | BPF_X:
688 case BPF_JMP | BPF_JSET | BPF_K:
689 case BPF_JMP | BPF_JSET | BPF_X:
690 /* A jump must set masks on targets */
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000691 masks[pc + 1 + filter[pc].jt] &= memvalid;
692 masks[pc + 1 + filter[pc].jf] &= memvalid;
693 memvalid = ~0;
694 break;
695 }
696 }
697error:
698 kfree(masks);
699 return ret;
700}
701
Daniel Borkmann34805932014-05-29 10:22:50 +0200702static bool chk_code_allowed(u16 code_to_probe)
703{
704 static const bool codes[] = {
705 /* 32 bit ALU operations */
706 [BPF_ALU | BPF_ADD | BPF_K] = true,
707 [BPF_ALU | BPF_ADD | BPF_X] = true,
708 [BPF_ALU | BPF_SUB | BPF_K] = true,
709 [BPF_ALU | BPF_SUB | BPF_X] = true,
710 [BPF_ALU | BPF_MUL | BPF_K] = true,
711 [BPF_ALU | BPF_MUL | BPF_X] = true,
712 [BPF_ALU | BPF_DIV | BPF_K] = true,
713 [BPF_ALU | BPF_DIV | BPF_X] = true,
714 [BPF_ALU | BPF_MOD | BPF_K] = true,
715 [BPF_ALU | BPF_MOD | BPF_X] = true,
716 [BPF_ALU | BPF_AND | BPF_K] = true,
717 [BPF_ALU | BPF_AND | BPF_X] = true,
718 [BPF_ALU | BPF_OR | BPF_K] = true,
719 [BPF_ALU | BPF_OR | BPF_X] = true,
720 [BPF_ALU | BPF_XOR | BPF_K] = true,
721 [BPF_ALU | BPF_XOR | BPF_X] = true,
722 [BPF_ALU | BPF_LSH | BPF_K] = true,
723 [BPF_ALU | BPF_LSH | BPF_X] = true,
724 [BPF_ALU | BPF_RSH | BPF_K] = true,
725 [BPF_ALU | BPF_RSH | BPF_X] = true,
726 [BPF_ALU | BPF_NEG] = true,
727 /* Load instructions */
728 [BPF_LD | BPF_W | BPF_ABS] = true,
729 [BPF_LD | BPF_H | BPF_ABS] = true,
730 [BPF_LD | BPF_B | BPF_ABS] = true,
731 [BPF_LD | BPF_W | BPF_LEN] = true,
732 [BPF_LD | BPF_W | BPF_IND] = true,
733 [BPF_LD | BPF_H | BPF_IND] = true,
734 [BPF_LD | BPF_B | BPF_IND] = true,
735 [BPF_LD | BPF_IMM] = true,
736 [BPF_LD | BPF_MEM] = true,
737 [BPF_LDX | BPF_W | BPF_LEN] = true,
738 [BPF_LDX | BPF_B | BPF_MSH] = true,
739 [BPF_LDX | BPF_IMM] = true,
740 [BPF_LDX | BPF_MEM] = true,
741 /* Store instructions */
742 [BPF_ST] = true,
743 [BPF_STX] = true,
744 /* Misc instructions */
745 [BPF_MISC | BPF_TAX] = true,
746 [BPF_MISC | BPF_TXA] = true,
747 /* Return instructions */
748 [BPF_RET | BPF_K] = true,
749 [BPF_RET | BPF_A] = true,
750 /* Jump instructions */
751 [BPF_JMP | BPF_JA] = true,
752 [BPF_JMP | BPF_JEQ | BPF_K] = true,
753 [BPF_JMP | BPF_JEQ | BPF_X] = true,
754 [BPF_JMP | BPF_JGE | BPF_K] = true,
755 [BPF_JMP | BPF_JGE | BPF_X] = true,
756 [BPF_JMP | BPF_JGT | BPF_K] = true,
757 [BPF_JMP | BPF_JGT | BPF_X] = true,
758 [BPF_JMP | BPF_JSET | BPF_K] = true,
759 [BPF_JMP | BPF_JSET | BPF_X] = true,
760 };
761
762 if (code_to_probe >= ARRAY_SIZE(codes))
763 return false;
764
765 return codes[code_to_probe];
766}
767
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +0200768static bool bpf_check_basics_ok(const struct sock_filter *filter,
769 unsigned int flen)
770{
771 if (filter == NULL)
772 return false;
773 if (flen == 0 || flen > BPF_MAXINSNS)
774 return false;
775
776 return true;
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779/**
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -0700780 * bpf_check_classic - verify socket filter code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * @filter: filter to verify
782 * @flen: length of filter
783 *
784 * Check the user's filter code. If we let some ugly
785 * filter code slip through kaboom! The filter must contain
Kris Katterjohn93699862006-01-04 13:58:36 -0800786 * no references or jumps that are out of range, no illegal
787 * instructions, and must end with a RET instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *
Kris Katterjohn7b11f692006-01-13 14:33:06 -0800789 * All jumps are forward as they are not signed.
790 *
791 * Returns 0 if the rule set is legal or -EINVAL if not.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Nicolas Schichand9e12f42015-05-06 16:12:28 +0200793static int bpf_check_classic(const struct sock_filter *filter,
794 unsigned int flen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000796 bool anc_found;
Daniel Borkmann34805932014-05-29 10:22:50 +0200797 int pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Daniel Borkmann34805932014-05-29 10:22:50 +0200799 /* Check the filter code now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 for (pc = 0; pc < flen; pc++) {
Eric Dumazetec31a052014-07-12 15:49:16 +0200801 const struct sock_filter *ftest = &filter[pc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Daniel Borkmann34805932014-05-29 10:22:50 +0200803 /* May we actually operate on this code? */
804 if (!chk_code_allowed(ftest->code))
Tetsuo Handacba328f2010-11-16 15:19:51 +0000805 return -EINVAL;
Daniel Borkmann34805932014-05-29 10:22:50 +0200806
Kris Katterjohn93699862006-01-04 13:58:36 -0800807 /* Some instructions need special checks */
Daniel Borkmann34805932014-05-29 10:22:50 +0200808 switch (ftest->code) {
809 case BPF_ALU | BPF_DIV | BPF_K:
810 case BPF_ALU | BPF_MOD | BPF_K:
811 /* Check for division by zero */
Eric Dumazetb6069a92012-09-07 22:03:35 +0000812 if (ftest->k == 0)
813 return -EINVAL;
814 break;
Rabin Vincent229394e2016-01-12 20:17:08 +0100815 case BPF_ALU | BPF_LSH | BPF_K:
816 case BPF_ALU | BPF_RSH | BPF_K:
817 if (ftest->k >= 32)
818 return -EINVAL;
819 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200820 case BPF_LD | BPF_MEM:
821 case BPF_LDX | BPF_MEM:
822 case BPF_ST:
823 case BPF_STX:
824 /* Check for invalid memory addresses */
Kris Katterjohn93699862006-01-04 13:58:36 -0800825 if (ftest->k >= BPF_MEMWORDS)
826 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000827 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200828 case BPF_JMP | BPF_JA:
829 /* Note, the large ftest->k might cause loops.
Kris Katterjohn93699862006-01-04 13:58:36 -0800830 * Compare this with conditional jumps below,
831 * where offsets are limited. --ANK (981016)
832 */
Daniel Borkmann34805932014-05-29 10:22:50 +0200833 if (ftest->k >= (unsigned int)(flen - pc - 1))
Kris Katterjohn93699862006-01-04 13:58:36 -0800834 return -EINVAL;
835 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200836 case BPF_JMP | BPF_JEQ | BPF_K:
837 case BPF_JMP | BPF_JEQ | BPF_X:
838 case BPF_JMP | BPF_JGE | BPF_K:
839 case BPF_JMP | BPF_JGE | BPF_X:
840 case BPF_JMP | BPF_JGT | BPF_K:
841 case BPF_JMP | BPF_JGT | BPF_X:
842 case BPF_JMP | BPF_JSET | BPF_K:
843 case BPF_JMP | BPF_JSET | BPF_X:
844 /* Both conditionals must be safe */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000845 if (pc + ftest->jt + 1 >= flen ||
846 pc + ftest->jf + 1 >= flen)
847 return -EINVAL;
Tetsuo Handacba328f2010-11-16 15:19:51 +0000848 break;
Daniel Borkmann34805932014-05-29 10:22:50 +0200849 case BPF_LD | BPF_W | BPF_ABS:
850 case BPF_LD | BPF_H | BPF_ABS:
851 case BPF_LD | BPF_B | BPF_ABS:
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000852 anc_found = false;
Daniel Borkmann34805932014-05-29 10:22:50 +0200853 if (bpf_anc_helper(ftest) & BPF_ANC)
854 anc_found = true;
855 /* Ancillary operation unknown or unsupported */
Daniel Borkmannaa1113d2012-12-28 10:50:17 +0000856 if (anc_found == false && ftest->k >= SKF_AD_OFF)
857 return -EINVAL;
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
Daniel Borkmann34805932014-05-29 10:22:50 +0200861 /* Last instruction must be a RET code */
Hagen Paul Pfeifer01f2f3f2010-06-19 17:05:36 +0000862 switch (filter[flen - 1].code) {
Daniel Borkmann34805932014-05-29 10:22:50 +0200863 case BPF_RET | BPF_K:
864 case BPF_RET | BPF_A:
Eric Dumazet2d5311e2010-12-01 20:46:24 +0000865 return check_load_and_stores(filter, flen);
Tetsuo Handacba328f2010-11-16 15:19:51 +0000866 }
Daniel Borkmann34805932014-05-29 10:22:50 +0200867
Tetsuo Handacba328f2010-11-16 15:19:51 +0000868 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700871static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
872 const struct sock_fprog *fprog)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100873{
Alexei Starovoitov009937e2014-07-30 20:34:13 -0700874 unsigned int fsize = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100875 struct sock_fprog_kern *fkprog;
876
877 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
878 if (!fp->orig_prog)
879 return -ENOMEM;
880
881 fkprog = fp->orig_prog;
882 fkprog->len = fprog->len;
Daniel Borkmann658da932015-05-06 16:12:29 +0200883
884 fkprog->filter = kmemdup(fp->insns, fsize,
885 GFP_KERNEL | __GFP_NOWARN);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100886 if (!fkprog->filter) {
887 kfree(fp->orig_prog);
888 return -ENOMEM;
889 }
890
891 return 0;
892}
893
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700894static void bpf_release_orig_filter(struct bpf_prog *fp)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +0100895{
896 struct sock_fprog_kern *fprog = fp->orig_prog;
897
898 if (fprog) {
899 kfree(fprog->filter);
900 kfree(fprog);
901 }
902}
903
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700904static void __bpf_prog_release(struct bpf_prog *prog)
905{
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100906 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -0800907 bpf_prog_put(prog);
908 } else {
909 bpf_release_orig_filter(prog);
910 bpf_prog_free(prog);
911 }
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700912}
913
Pablo Neira34c5bd62014-07-29 17:36:28 +0200914static void __sk_filter_release(struct sk_filter *fp)
915{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700916 __bpf_prog_release(fp->prog);
917 kfree(fp);
Pablo Neira34c5bd62014-07-29 17:36:28 +0200918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/**
Eric Dumazet46bcf142010-12-06 09:29:43 -0800921 * sk_filter_release_rcu - Release a socket filter by rcu_head
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700922 * @rcu: rcu_head that contains the sk_filter to free
923 */
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100924static void sk_filter_release_rcu(struct rcu_head *rcu)
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700925{
926 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
927
Pablo Neira34c5bd62014-07-29 17:36:28 +0200928 __sk_filter_release(fp);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700929}
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100930
931/**
932 * sk_filter_release - release a socket filter
933 * @fp: filter to remove
934 *
935 * Remove a filter from a socket and release its resources.
936 */
937static void sk_filter_release(struct sk_filter *fp)
938{
939 if (atomic_dec_and_test(&fp->refcnt))
940 call_rcu(&fp->rcu, sk_filter_release_rcu);
941}
942
943void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
944{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700945 u32 filter_size = bpf_prog_size(fp->prog->len);
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700946
947 atomic_sub(filter_size, &sk->sk_omem_alloc);
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100948 sk_filter_release(fp);
949}
950
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700951/* try to charge the socket memory if there is space available
952 * return true on success
953 */
Greg Kroah-Hartman00449622017-10-12 21:21:39 +0200954bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
Daniel Borkmannfbc907f2014-03-28 18:58:20 +0100955{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700956 u32 filter_size = bpf_prog_size(fp->prog->len);
Pavel Emelyanov47e958e2007-10-17 21:22:42 -0700957
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700958 /* same check as in sock_kmalloc() */
959 if (filter_size <= sysctl_optmem_max &&
960 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
Greg Kroah-Hartman00449622017-10-12 21:21:39 +0200961 atomic_inc(&fp->refcnt);
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700962 atomic_add(filter_size, &sk->sk_omem_alloc);
963 return true;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100964 }
Alexei Starovoitov278571b2014-07-30 20:34:12 -0700965 return false;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100966}
967
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700968static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100969{
970 struct sock_filter *old_prog;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -0700971 struct bpf_prog *old_fp;
Daniel Borkmann34805932014-05-29 10:22:50 +0200972 int err, new_len, old_len = fp->len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100973
974 /* We are free to overwrite insns et al right here as it
975 * won't be used at this point in time anymore internally
976 * after the migration to the internal BPF instruction
977 * representation.
978 */
979 BUILD_BUG_ON(sizeof(struct sock_filter) !=
Alexei Starovoitov2695fb52014-07-24 16:38:21 -0700980 sizeof(struct bpf_insn));
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100981
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100982 /* Conversion cannot happen on overlapping memory areas,
983 * so we need to keep the user BPF around until the 2nd
984 * pass. At this time, the user BPF is stored in fp->insns.
985 */
986 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
Daniel Borkmann658da932015-05-06 16:12:29 +0200987 GFP_KERNEL | __GFP_NOWARN);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100988 if (!old_prog) {
989 err = -ENOMEM;
990 goto out_err;
991 }
992
993 /* 1st pass: calculate the new program length. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -0700994 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100995 if (err)
996 goto out_err_free;
997
998 /* Expand fp for appending the new filter representation. */
999 old_fp = fp;
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001000 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001001 if (!fp) {
1002 /* The old_fp is still around in case we couldn't
1003 * allocate new memory, so uncharge on that one.
1004 */
1005 fp = old_fp;
1006 err = -ENOMEM;
1007 goto out_err_free;
1008 }
1009
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001010 fp->len = new_len;
1011
Alexei Starovoitov2695fb52014-07-24 16:38:21 -07001012 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -07001013 err = bpf_convert_filter(old_prog, old_len, fp->insnsi, &new_len);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001014 if (err)
Alexei Starovoitov8fb575c2014-07-30 20:34:15 -07001015 /* 2nd bpf_convert_filter() can fail only if it fails
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001016 * to allocate memory, remapping must succeed. Note,
1017 * that at this time old_fp has already been released
Alexei Starovoitov278571b2014-07-30 20:34:12 -07001018 * by krealloc().
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001019 */
1020 goto out_err_free;
1021
Daniel Borkmannd1c55ab2016-05-13 19:08:31 +02001022 fp = bpf_prog_select_runtime(fp, &err);
Alexei Starovoitova3d6dd62018-01-29 02:48:56 +01001023 if (err)
1024 goto out_err_free;
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001025
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001026 kfree(old_prog);
1027 return fp;
1028
1029out_err_free:
1030 kfree(old_prog);
1031out_err:
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001032 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001033 return ERR_PTR(err);
1034}
1035
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001036static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1037 bpf_aux_classic_check_t trans)
Jiri Pirko302d6632012-03-31 11:01:19 +00001038{
1039 int err;
1040
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001041 fp->bpf_func = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02001042 fp->jited = 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001043
Alexei Starovoitov4df95ff2014-07-30 20:34:14 -07001044 err = bpf_check_classic(fp->insns, fp->len);
Leon Yu418c96a2014-06-01 05:37:25 +00001045 if (err) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001046 __bpf_prog_release(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001047 return ERR_PTR(err);
Leon Yu418c96a2014-06-01 05:37:25 +00001048 }
Jiri Pirko302d6632012-03-31 11:01:19 +00001049
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001050 /* There might be additional checks and transformations
1051 * needed on classic filters, f.e. in case of seccomp.
1052 */
1053 if (trans) {
1054 err = trans(fp->insns, fp->len);
1055 if (err) {
1056 __bpf_prog_release(fp);
1057 return ERR_PTR(err);
1058 }
1059 }
1060
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001061 /* Probe if we can JIT compile the filter and if so, do
1062 * the compilation of the filter.
1063 */
Jiri Pirko302d6632012-03-31 11:01:19 +00001064 bpf_jit_compile(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001065
1066 /* JIT compiler couldn't process this filter, so do the
1067 * internal BPF translation for the optimized interpreter.
1068 */
Alexei Starovoitov5fe821a2014-05-19 14:56:14 -07001069 if (!fp->jited)
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001070 fp = bpf_migrate_filter(fp);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001071
1072 return fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001073}
1074
1075/**
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001076 * bpf_prog_create - create an unattached filter
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001077 * @pfp: the unattached filter that is created
Tobias Klauser677a9fd2014-06-24 15:33:21 +02001078 * @fprog: the filter program
Jiri Pirko302d6632012-03-31 11:01:19 +00001079 *
Randy Dunlapc6c4b972012-06-08 14:01:44 +00001080 * Create a filter independent of any socket. We first run some
Jiri Pirko302d6632012-03-31 11:01:19 +00001081 * sanity checks on it to make sure it does not explode on us later.
1082 * If an error occurs or there is insufficient memory for the filter
1083 * a negative errno code is returned. On success the return is zero.
1084 */
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001085int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
Jiri Pirko302d6632012-03-31 11:01:19 +00001086{
Alexei Starovoitov009937e2014-07-30 20:34:13 -07001087 unsigned int fsize = bpf_classic_proglen(fprog);
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001088 struct bpf_prog *fp;
Jiri Pirko302d6632012-03-31 11:01:19 +00001089
1090 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001091 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Jiri Pirko302d6632012-03-31 11:01:19 +00001092 return -EINVAL;
1093
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001094 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Jiri Pirko302d6632012-03-31 11:01:19 +00001095 if (!fp)
1096 return -ENOMEM;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001097
Jiri Pirko302d6632012-03-31 11:01:19 +00001098 memcpy(fp->insns, fprog->filter, fsize);
1099
Jiri Pirko302d6632012-03-31 11:01:19 +00001100 fp->len = fprog->len;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001101 /* Since unattached filters are not copied back to user
1102 * space through sk_get_filter(), we do not need to hold
1103 * a copy here, and can spare us the work.
1104 */
1105 fp->orig_prog = NULL;
Jiri Pirko302d6632012-03-31 11:01:19 +00001106
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001107 /* bpf_prepare_filter() already takes care of freeing
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001108 * memory in case something goes wrong.
1109 */
Nicolas Schichan4ae92bc2015-05-06 16:12:27 +02001110 fp = bpf_prepare_filter(fp, NULL);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +01001111 if (IS_ERR(fp))
1112 return PTR_ERR(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001113
1114 *pfp = fp;
1115 return 0;
Jiri Pirko302d6632012-03-31 11:01:19 +00001116}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001117EXPORT_SYMBOL_GPL(bpf_prog_create);
Jiri Pirko302d6632012-03-31 11:01:19 +00001118
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001119/**
1120 * bpf_prog_create_from_user - create an unattached filter from user buffer
1121 * @pfp: the unattached filter that is created
1122 * @fprog: the filter program
1123 * @trans: post-classic verifier transformation handler
Daniel Borkmannbab18992015-10-02 15:17:33 +02001124 * @save_orig: save classic BPF program
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001125 *
1126 * This function effectively does the same as bpf_prog_create(), only
1127 * that it builds up its insns buffer from user space provided buffer.
1128 * It also allows for passing a bpf_aux_classic_check_t handler.
1129 */
1130int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
Daniel Borkmannbab18992015-10-02 15:17:33 +02001131 bpf_aux_classic_check_t trans, bool save_orig)
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001132{
1133 unsigned int fsize = bpf_classic_proglen(fprog);
1134 struct bpf_prog *fp;
Daniel Borkmannbab18992015-10-02 15:17:33 +02001135 int err;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001136
1137 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001138 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001139 return -EINVAL;
1140
1141 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1142 if (!fp)
1143 return -ENOMEM;
1144
1145 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1146 __bpf_prog_free(fp);
1147 return -EFAULT;
1148 }
1149
1150 fp->len = fprog->len;
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001151 fp->orig_prog = NULL;
1152
Daniel Borkmannbab18992015-10-02 15:17:33 +02001153 if (save_orig) {
1154 err = bpf_prog_store_orig_filter(fp, fprog);
1155 if (err) {
1156 __bpf_prog_free(fp);
1157 return -ENOMEM;
1158 }
1159 }
1160
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001161 /* bpf_prepare_filter() already takes care of freeing
1162 * memory in case something goes wrong.
1163 */
1164 fp = bpf_prepare_filter(fp, trans);
1165 if (IS_ERR(fp))
1166 return PTR_ERR(fp);
1167
1168 *pfp = fp;
1169 return 0;
1170}
David S. Miller2ea273d2015-08-17 14:37:06 -07001171EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
Daniel Borkmannac67eb22015-05-06 16:12:30 +02001172
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001173void bpf_prog_destroy(struct bpf_prog *fp)
Jiri Pirko302d6632012-03-31 11:01:19 +00001174{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001175 __bpf_prog_release(fp);
Jiri Pirko302d6632012-03-31 11:01:19 +00001176}
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001177EXPORT_SYMBOL_GPL(bpf_prog_destroy);
Jiri Pirko302d6632012-03-31 11:01:19 +00001178
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001179static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001180{
1181 struct sk_filter *fp, *old_fp;
1182
1183 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1184 if (!fp)
1185 return -ENOMEM;
1186
1187 fp->prog = prog;
1188 atomic_set(&fp->refcnt, 0);
1189
1190 if (!sk_filter_charge(sk, fp)) {
1191 kfree(fp);
1192 return -ENOMEM;
1193 }
1194
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001195 old_fp = rcu_dereference_protected(sk->sk_filter,
1196 lockdep_sock_is_held(sk));
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001197 rcu_assign_pointer(sk->sk_filter, fp);
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001198
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001199 if (old_fp)
1200 sk_filter_uncharge(sk, old_fp);
1201
1202 return 0;
1203}
1204
Craig Gallek538950a2016-01-04 17:41:47 -05001205static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1206{
1207 struct bpf_prog *old_prog;
1208 int err;
1209
1210 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1211 return -ENOMEM;
1212
Craig Gallekfa463492016-02-10 11:50:39 -05001213 if (sk_unhashed(sk) && sk->sk_reuseport) {
Craig Gallek538950a2016-01-04 17:41:47 -05001214 err = reuseport_alloc(sk);
1215 if (err)
1216 return err;
1217 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1218 /* The socket wasn't bound with SO_REUSEPORT */
1219 return -EINVAL;
1220 }
1221
1222 old_prog = reuseport_attach_prog(sk, prog);
1223 if (old_prog)
1224 bpf_prog_destroy(old_prog);
1225
1226 return 0;
1227}
1228
1229static
1230struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1231{
1232 unsigned int fsize = bpf_classic_proglen(fprog);
Craig Gallek538950a2016-01-04 17:41:47 -05001233 struct bpf_prog *prog;
1234 int err;
1235
1236 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1237 return ERR_PTR(-EPERM);
1238
1239 /* Make sure new filter is there and in the right amounts. */
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001240 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
Craig Gallek538950a2016-01-04 17:41:47 -05001241 return ERR_PTR(-EINVAL);
1242
Daniel Borkmannf7bd9e32016-06-10 21:19:07 +02001243 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
Craig Gallek538950a2016-01-04 17:41:47 -05001244 if (!prog)
1245 return ERR_PTR(-ENOMEM);
1246
1247 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1248 __bpf_prog_free(prog);
1249 return ERR_PTR(-EFAULT);
1250 }
1251
1252 prog->len = fprog->len;
1253
1254 err = bpf_prog_store_orig_filter(prog, fprog);
1255 if (err) {
1256 __bpf_prog_free(prog);
1257 return ERR_PTR(-ENOMEM);
1258 }
1259
1260 /* bpf_prepare_filter() already takes care of freeing
1261 * memory in case something goes wrong.
1262 */
1263 return bpf_prepare_filter(prog, NULL);
1264}
1265
Pavel Emelyanov47e958e2007-10-17 21:22:42 -07001266/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 * sk_attach_filter - attach a socket filter
1268 * @fprog: the filter program
1269 * @sk: the socket to use
1270 *
1271 * Attach the user's filter code. We first run some sanity checks on
1272 * it to make sure it does not explode on us later. If an error
1273 * occurs or there is insufficient memory for the filter a negative
1274 * errno code is returned. On success the return is zero.
1275 */
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001276int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Craig Gallek538950a2016-01-04 17:41:47 -05001278 struct bpf_prog *prog = __get_filter(fprog, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int err;
1280
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001281 if (IS_ERR(prog))
1282 return PTR_ERR(prog);
1283
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001284 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001285 if (err < 0) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001286 __bpf_prog_release(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001287 return err;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001288 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01001289
Pavel Emelyanovd3904b72007-10-17 21:22:17 -07001290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001292EXPORT_SYMBOL_GPL(sk_attach_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Craig Gallek538950a2016-01-04 17:41:47 -05001294int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001295{
Craig Gallek538950a2016-01-04 17:41:47 -05001296 struct bpf_prog *prog = __get_filter(fprog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001297 int err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001298
Alexei Starovoitov198bf1b2014-12-10 20:14:55 -08001299 if (IS_ERR(prog))
1300 return PTR_ERR(prog);
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001301
Craig Gallek538950a2016-01-04 17:41:47 -05001302 err = __reuseport_attach_prog(prog, sk);
1303 if (err < 0) {
1304 __bpf_prog_release(prog);
1305 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001306 }
1307
Craig Gallek538950a2016-01-04 17:41:47 -05001308 return 0;
1309}
1310
1311static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1312{
Craig Gallek538950a2016-01-04 17:41:47 -05001313 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1314 return ERR_PTR(-EPERM);
1315
Daniel Borkmann113214b2016-06-30 17:24:44 +02001316 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
Craig Gallek538950a2016-01-04 17:41:47 -05001317}
1318
1319int sk_attach_bpf(u32 ufd, struct sock *sk)
1320{
1321 struct bpf_prog *prog = __get_bpf(ufd, sk);
1322 int err;
1323
1324 if (IS_ERR(prog))
1325 return PTR_ERR(prog);
1326
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02001327 err = __sk_attach_prog(prog, sk);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001328 if (err < 0) {
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001329 bpf_prog_put(prog);
Daniel Borkmann49b31e52015-03-02 12:25:51 +01001330 return err;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001331 }
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001332
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08001333 return 0;
1334}
1335
Craig Gallek538950a2016-01-04 17:41:47 -05001336int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1337{
1338 struct bpf_prog *prog = __get_bpf(ufd, sk);
1339 int err;
1340
1341 if (IS_ERR(prog))
1342 return PTR_ERR(prog);
1343
1344 err = __reuseport_attach_prog(prog, sk);
1345 if (err < 0) {
1346 bpf_prog_put(prog);
1347 return err;
1348 }
1349
1350 return 0;
1351}
1352
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001353struct bpf_scratchpad {
1354 union {
1355 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1356 u8 buff[MAX_BPF_STACK];
1357 };
1358};
1359
1360static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001361
Daniel Borkmann5293efe2016-08-18 01:00:39 +02001362static inline int __bpf_try_make_writable(struct sk_buff *skb,
1363 unsigned int write_len)
1364{
1365 return skb_ensure_writable(skb, write_len);
1366}
1367
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001368static inline int bpf_try_make_writable(struct sk_buff *skb,
1369 unsigned int write_len)
1370{
Daniel Borkmann5293efe2016-08-18 01:00:39 +02001371 int err = __bpf_try_make_writable(skb, write_len);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001372
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001373 bpf_compute_data_end(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001374 return err;
1375}
1376
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001377static int bpf_try_make_head_writable(struct sk_buff *skb)
1378{
1379 return bpf_try_make_writable(skb, skb_headlen(skb));
1380}
1381
Daniel Borkmanna2bfe6b2016-08-05 00:11:11 +02001382static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1383{
1384 if (skb_at_tc_ingress(skb))
1385 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1386}
1387
Daniel Borkmann80656942016-08-05 00:11:13 +02001388static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1389{
1390 if (skb_at_tc_ingress(skb))
1391 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1392}
1393
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001394BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1395 const void *, from, u32, len, u64, flags)
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001396{
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001397 void *ptr;
1398
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001399 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001400 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001401 if (unlikely(offset > 0xffff))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001402 return -EFAULT;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001403 if (unlikely(bpf_try_make_writable(skb, offset + len)))
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001404 return -EFAULT;
1405
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001406 ptr = skb->data + offset;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001407 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmann479ffcc2016-08-05 00:11:12 +02001408 __skb_postpull_rcsum(skb, ptr, len, offset);
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001409
1410 memcpy(ptr, from, len);
1411
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001412 if (flags & BPF_F_RECOMPUTE_CSUM)
Daniel Borkmann479ffcc2016-08-05 00:11:12 +02001413 __skb_postpush_rcsum(skb, ptr, len, offset);
Daniel Borkmann8afd54c2016-03-04 15:15:03 +01001414 if (flags & BPF_F_INVALIDATE_HASH)
1415 skb_clear_hash(skb);
Daniel Borkmannf8ffad692016-01-07 15:50:23 +01001416
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001417 return 0;
1418}
1419
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001420static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001421 .func = bpf_skb_store_bytes,
1422 .gpl_only = false,
1423 .ret_type = RET_INTEGER,
1424 .arg1_type = ARG_PTR_TO_CTX,
1425 .arg2_type = ARG_ANYTHING,
1426 .arg3_type = ARG_PTR_TO_STACK,
1427 .arg4_type = ARG_CONST_STACK_SIZE,
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001428 .arg5_type = ARG_ANYTHING,
1429};
1430
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001431BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1432 void *, to, u32, len)
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001433{
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001434 void *ptr;
1435
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001436 if (unlikely(offset > 0xffff))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001437 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001438
1439 ptr = skb_header_pointer(skb, offset, len, to);
1440 if (unlikely(!ptr))
Daniel Borkmann074f5282016-04-13 00:10:52 +02001441 goto err_clear;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001442 if (ptr != to)
1443 memcpy(to, ptr, len);
1444
1445 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02001446err_clear:
1447 memset(to, 0, len);
1448 return -EFAULT;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001449}
1450
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001451static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001452 .func = bpf_skb_load_bytes,
1453 .gpl_only = false,
1454 .ret_type = RET_INTEGER,
1455 .arg1_type = ARG_PTR_TO_CTX,
1456 .arg2_type = ARG_ANYTHING,
Daniel Borkmann074f5282016-04-13 00:10:52 +02001457 .arg3_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann05c74e52015-12-17 23:51:53 +01001458 .arg4_type = ARG_CONST_STACK_SIZE,
1459};
1460
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001461BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1462{
1463 /* Idea is the following: should the needed direct read/write
1464 * test fail during runtime, we can pull in more data and redo
1465 * again, since implicitly, we invalidate previous checks here.
1466 *
1467 * Or, since we know how much we need to make read/writeable,
1468 * this can be done once at the program beginning for direct
1469 * access case. By this we overcome limitations of only current
1470 * headroom being accessible.
1471 */
1472 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1473}
1474
1475static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1476 .func = bpf_skb_pull_data,
1477 .gpl_only = false,
1478 .ret_type = RET_INTEGER,
1479 .arg1_type = ARG_PTR_TO_CTX,
1480 .arg2_type = ARG_ANYTHING,
1481};
1482
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001483BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1484 u64, from, u64, to, u64, flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001485{
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001486 __sum16 *ptr;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001487
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001488 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1489 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001490 if (unlikely(offset > 0xffff || offset & 1))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001491 return -EFAULT;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001492 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001493 return -EFAULT;
1494
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001495 ptr = (__sum16 *)(skb->data + offset);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001496 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann8050c0f2016-03-04 15:15:02 +01001497 case 0:
1498 if (unlikely(from != 0))
1499 return -EINVAL;
1500
1501 csum_replace_by_diff(ptr, to);
1502 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001503 case 2:
1504 csum_replace2(ptr, from, to);
1505 break;
1506 case 4:
1507 csum_replace4(ptr, from, to);
1508 break;
1509 default:
1510 return -EINVAL;
1511 }
1512
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001513 return 0;
1514}
1515
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001516static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001517 .func = bpf_l3_csum_replace,
1518 .gpl_only = false,
1519 .ret_type = RET_INTEGER,
1520 .arg1_type = ARG_PTR_TO_CTX,
1521 .arg2_type = ARG_ANYTHING,
1522 .arg3_type = ARG_ANYTHING,
1523 .arg4_type = ARG_ANYTHING,
1524 .arg5_type = ARG_ANYTHING,
1525};
1526
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001527BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1528 u64, from, u64, to, u64, flags)
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001529{
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001530 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
Daniel Borkmann2f729592016-02-19 23:05:26 +01001531 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001532 __sum16 *ptr;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001533
Daniel Borkmann2f729592016-02-19 23:05:26 +01001534 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_PSEUDO_HDR |
1535 BPF_F_HDR_FIELD_MASK)))
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001536 return -EINVAL;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001537 if (unlikely(offset > 0xffff || offset & 1))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001538 return -EFAULT;
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001539 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001540 return -EFAULT;
1541
Daniel Borkmann0ed661d2016-08-11 21:38:37 +02001542 ptr = (__sum16 *)(skb->data + offset);
Daniel Borkmann2f729592016-02-19 23:05:26 +01001543 if (is_mmzero && !*ptr)
1544 return 0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001545
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001546 switch (flags & BPF_F_HDR_FIELD_MASK) {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001547 case 0:
1548 if (unlikely(from != 0))
1549 return -EINVAL;
1550
1551 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1552 break;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001553 case 2:
1554 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1555 break;
1556 case 4:
1557 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1558 break;
1559 default:
1560 return -EINVAL;
1561 }
1562
Daniel Borkmann2f729592016-02-19 23:05:26 +01001563 if (is_mmzero && !*ptr)
1564 *ptr = CSUM_MANGLED_0;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001565 return 0;
1566}
1567
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001568static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07001569 .func = bpf_l4_csum_replace,
1570 .gpl_only = false,
1571 .ret_type = RET_INTEGER,
1572 .arg1_type = ARG_PTR_TO_CTX,
1573 .arg2_type = ARG_ANYTHING,
1574 .arg3_type = ARG_ANYTHING,
1575 .arg4_type = ARG_ANYTHING,
1576 .arg5_type = ARG_ANYTHING,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07001577};
1578
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001579BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1580 __be32 *, to, u32, to_size, __wsum, seed)
Daniel Borkmann7d672342016-02-19 23:05:23 +01001581{
Daniel Borkmann21cafc12016-02-19 23:05:24 +01001582 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001583 u32 diff_size = from_size + to_size;
Daniel Borkmann7d672342016-02-19 23:05:23 +01001584 int i, j = 0;
1585
1586 /* This is quite flexible, some examples:
1587 *
1588 * from_size == 0, to_size > 0, seed := csum --> pushing data
1589 * from_size > 0, to_size == 0, seed := csum --> pulling data
1590 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1591 *
1592 * Even for diffing, from_size and to_size don't need to be equal.
1593 */
1594 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1595 diff_size > sizeof(sp->diff)))
1596 return -EINVAL;
1597
1598 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1599 sp->diff[j] = ~from[i];
1600 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1601 sp->diff[j] = to[i];
1602
1603 return csum_partial(sp->diff, diff_size, seed);
1604}
1605
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001606static const struct bpf_func_proto bpf_csum_diff_proto = {
Daniel Borkmann7d672342016-02-19 23:05:23 +01001607 .func = bpf_csum_diff,
1608 .gpl_only = false,
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001609 .pkt_access = true,
Daniel Borkmann7d672342016-02-19 23:05:23 +01001610 .ret_type = RET_INTEGER,
1611 .arg1_type = ARG_PTR_TO_STACK,
1612 .arg2_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1613 .arg3_type = ARG_PTR_TO_STACK,
1614 .arg4_type = ARG_CONST_STACK_SIZE_OR_ZERO,
1615 .arg5_type = ARG_ANYTHING,
1616};
1617
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001618BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1619{
1620 /* The interface is to be used in combination with bpf_csum_diff()
1621 * for direct packet writes. csum rotation for alignment as well
1622 * as emulating csum_sub() can be done from the eBPF program.
1623 */
1624 if (skb->ip_summed == CHECKSUM_COMPLETE)
1625 return (skb->csum = csum_add(skb->csum, csum));
1626
1627 return -ENOTSUPP;
1628}
1629
1630static const struct bpf_func_proto bpf_csum_update_proto = {
1631 .func = bpf_csum_update,
1632 .gpl_only = false,
1633 .ret_type = RET_INTEGER,
1634 .arg1_type = ARG_PTR_TO_CTX,
1635 .arg2_type = ARG_ANYTHING,
1636};
1637
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001638static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1639{
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001640 return dev_forward_skb(dev, skb);
1641}
1642
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001643static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1644 struct sk_buff *skb)
1645{
1646 int ret = ____dev_forward_skb(dev, skb);
1647
1648 if (likely(!ret)) {
1649 skb->dev = dev;
1650 ret = netif_rx(skb);
1651 }
1652
1653 return ret;
1654}
1655
Daniel Borkmanna70b5062016-06-10 21:19:06 +02001656static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1657{
1658 int ret;
1659
1660 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1661 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1662 kfree_skb(skb);
1663 return -ENETDOWN;
1664 }
1665
1666 skb->dev = dev;
1667
1668 __this_cpu_inc(xmit_recursion);
1669 ret = dev_queue_xmit(skb);
1670 __this_cpu_dec(xmit_recursion);
1671
1672 return ret;
1673}
1674
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001675static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
1676 u32 flags)
1677{
1678 /* skb->mac_len is not set on normal egress */
1679 unsigned int mlen = skb->network_header - skb->mac_header;
1680
1681 __skb_pull(skb, mlen);
1682
1683 /* At ingress, the mac header has already been pulled once.
1684 * At egress, skb_pospull_rcsum has to be done in case that
1685 * the skb is originated from ingress (i.e. a forwarded skb)
1686 * to ensure that rcsum starts at net header.
1687 */
1688 if (!skb_at_tc_ingress(skb))
1689 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
1690 skb_pop_mac_header(skb);
1691 skb_reset_mac_len(skb);
1692 return flags & BPF_F_INGRESS ?
1693 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
1694}
1695
1696static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
1697 u32 flags)
1698{
1699 bpf_push_mac_rcsum(skb);
1700 return flags & BPF_F_INGRESS ?
1701 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
1702}
1703
1704static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
1705 u32 flags)
1706{
1707 switch (dev->type) {
1708 case ARPHRD_TUNNEL:
1709 case ARPHRD_TUNNEL6:
1710 case ARPHRD_SIT:
1711 case ARPHRD_IPGRE:
1712 case ARPHRD_VOID:
1713 case ARPHRD_NONE:
1714 return __bpf_redirect_no_mac(skb, dev, flags);
1715 default:
1716 return __bpf_redirect_common(skb, dev, flags);
1717 }
1718}
1719
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001720BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001721{
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001722 struct net_device *dev;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001723 struct sk_buff *clone;
1724 int ret;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001725
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001726 if (unlikely(flags & ~(BPF_F_INGRESS)))
1727 return -EINVAL;
1728
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001729 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1730 if (unlikely(!dev))
1731 return -EINVAL;
1732
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001733 clone = skb_clone(skb, GFP_ATOMIC);
1734 if (unlikely(!clone))
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001735 return -ENOMEM;
1736
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001737 /* For direct write, we need to keep the invariant that the skbs
1738 * we're dealing with need to be uncloned. Should uncloning fail
1739 * here, we need to free the just generated clone to unclone once
1740 * again.
1741 */
1742 ret = bpf_try_make_head_writable(skb);
1743 if (unlikely(ret)) {
1744 kfree_skb(clone);
1745 return -ENOMEM;
1746 }
1747
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001748 return __bpf_redirect(clone, dev, flags);
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001749}
1750
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001751static const struct bpf_func_proto bpf_clone_redirect_proto = {
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001752 .func = bpf_clone_redirect,
1753 .gpl_only = false,
1754 .ret_type = RET_INTEGER,
1755 .arg1_type = ARG_PTR_TO_CTX,
1756 .arg2_type = ARG_ANYTHING,
1757 .arg3_type = ARG_ANYTHING,
1758};
1759
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001760struct redirect_info {
1761 u32 ifindex;
1762 u32 flags;
1763};
1764
1765static DEFINE_PER_CPU(struct redirect_info, redirect_info);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001766
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001767BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001768{
1769 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1770
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001771 if (unlikely(flags & ~(BPF_F_INGRESS)))
1772 return TC_ACT_SHOT;
1773
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001774 ri->ifindex = ifindex;
1775 ri->flags = flags;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001776
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001777 return TC_ACT_REDIRECT;
1778}
1779
1780int skb_do_redirect(struct sk_buff *skb)
1781{
1782 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1783 struct net_device *dev;
1784
1785 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1786 ri->ifindex = 0;
1787 if (unlikely(!dev)) {
1788 kfree_skb(skb);
1789 return -EINVAL;
1790 }
1791
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001792 return __bpf_redirect(skb, dev, ri->flags);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001793}
1794
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001795static const struct bpf_func_proto bpf_redirect_proto = {
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001796 .func = bpf_redirect,
1797 .gpl_only = false,
1798 .ret_type = RET_INTEGER,
1799 .arg1_type = ARG_ANYTHING,
1800 .arg2_type = ARG_ANYTHING,
1801};
1802
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001803BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001804{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001805 return task_get_classid(skb);
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001806}
1807
1808static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1809 .func = bpf_get_cgroup_classid,
1810 .gpl_only = false,
1811 .ret_type = RET_INTEGER,
1812 .arg1_type = ARG_PTR_TO_CTX,
1813};
1814
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001815BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001816{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001817 return dst_tclassid(skb);
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001818}
1819
1820static const struct bpf_func_proto bpf_get_route_realm_proto = {
1821 .func = bpf_get_route_realm,
1822 .gpl_only = false,
1823 .ret_type = RET_INTEGER,
1824 .arg1_type = ARG_PTR_TO_CTX,
1825};
1826
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001827BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
Daniel Borkmann13c5c242016-07-03 01:28:47 +02001828{
1829 /* If skb_clear_hash() was called due to mangling, we can
1830 * trigger SW recalculation here. Later access to hash
1831 * can then use the inline skb->hash via context directly
1832 * instead of calling this helper again.
1833 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001834 return skb_get_hash(skb);
Daniel Borkmann13c5c242016-07-03 01:28:47 +02001835}
1836
1837static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1838 .func = bpf_get_hash_recalc,
1839 .gpl_only = false,
1840 .ret_type = RET_INTEGER,
1841 .arg1_type = ARG_PTR_TO_CTX,
1842};
1843
Daniel Borkmann7a4b28c2016-09-23 01:28:37 +02001844BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1845{
1846 /* After all direct packet write, this can be used once for
1847 * triggering a lazy recalc on next skb_get_hash() invocation.
1848 */
1849 skb_clear_hash(skb);
1850 return 0;
1851}
1852
1853static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1854 .func = bpf_set_hash_invalid,
1855 .gpl_only = false,
1856 .ret_type = RET_INTEGER,
1857 .arg1_type = ARG_PTR_TO_CTX,
1858};
1859
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001860BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1861 u16, vlan_tci)
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001862{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001863 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001864
1865 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1866 vlan_proto != htons(ETH_P_8021AD)))
1867 vlan_proto = htons(ETH_P_8021Q);
1868
Daniel Borkmann80656942016-08-05 00:11:13 +02001869 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001870 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
Daniel Borkmann80656942016-08-05 00:11:13 +02001871 bpf_pull_mac_rcsum(skb);
1872
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001873 bpf_compute_data_end(skb);
1874 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001875}
1876
1877const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1878 .func = bpf_skb_vlan_push,
1879 .gpl_only = false,
1880 .ret_type = RET_INTEGER,
1881 .arg1_type = ARG_PTR_TO_CTX,
1882 .arg2_type = ARG_ANYTHING,
1883 .arg3_type = ARG_ANYTHING,
1884};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001885EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001886
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001887BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001888{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001889 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001890
Daniel Borkmann80656942016-08-05 00:11:13 +02001891 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001892 ret = skb_vlan_pop(skb);
Daniel Borkmann80656942016-08-05 00:11:13 +02001893 bpf_pull_mac_rcsum(skb);
1894
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001895 bpf_compute_data_end(skb);
1896 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001897}
1898
1899const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1900 .func = bpf_skb_vlan_pop,
1901 .gpl_only = false,
1902 .ret_type = RET_INTEGER,
1903 .arg1_type = ARG_PTR_TO_CTX,
1904};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001905EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001906
Daniel Borkmann65781712016-06-28 12:18:27 +02001907static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1908{
1909 /* Caller already did skb_cow() with len as headroom,
1910 * so no need to do it here.
1911 */
1912 skb_push(skb, len);
1913 memmove(skb->data, skb->data + len, off);
1914 memset(skb->data + off, 0, len);
1915
1916 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1917 * needed here as it does not change the skb->csum
1918 * result for checksum complete when summing over
1919 * zeroed blocks.
1920 */
1921 return 0;
1922}
1923
1924static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1925{
1926 /* skb_ensure_writable() is not needed here, as we're
1927 * already working on an uncloned skb.
1928 */
1929 if (unlikely(!pskb_may_pull(skb, off + len)))
1930 return -ENOMEM;
1931
1932 skb_postpull_rcsum(skb, skb->data + off, len);
1933 memmove(skb->data + len, skb->data, off);
1934 __skb_pull(skb, len);
1935
1936 return 0;
1937}
1938
1939static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1940{
1941 bool trans_same = skb->transport_header == skb->network_header;
1942 int ret;
1943
1944 /* There's no need for __skb_push()/__skb_pull() pair to
1945 * get to the start of the mac header as we're guaranteed
1946 * to always start from here under eBPF.
1947 */
1948 ret = bpf_skb_generic_push(skb, off, len);
1949 if (likely(!ret)) {
1950 skb->mac_header -= len;
1951 skb->network_header -= len;
1952 if (trans_same)
1953 skb->transport_header = skb->network_header;
1954 }
1955
1956 return ret;
1957}
1958
1959static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1960{
1961 bool trans_same = skb->transport_header == skb->network_header;
1962 int ret;
1963
1964 /* Same here, __skb_push()/__skb_pull() pair not needed. */
1965 ret = bpf_skb_generic_pop(skb, off, len);
1966 if (likely(!ret)) {
1967 skb->mac_header += len;
1968 skb->network_header += len;
1969 if (trans_same)
1970 skb->transport_header = skb->network_header;
1971 }
1972
1973 return ret;
1974}
1975
1976static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
1977{
1978 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1979 u32 off = skb->network_header - skb->mac_header;
1980 int ret;
1981
1982 ret = skb_cow(skb, len_diff);
1983 if (unlikely(ret < 0))
1984 return ret;
1985
1986 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
1987 if (unlikely(ret < 0))
1988 return ret;
1989
1990 if (skb_is_gso(skb)) {
1991 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
1992 * be changed into SKB_GSO_TCPV6.
1993 */
1994 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1995 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
1996 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
1997 }
1998
1999 /* Due to IPv6 header, MSS needs to be downgraded. */
2000 skb_shinfo(skb)->gso_size -= len_diff;
2001 /* Header must be checked, and gso_segs recomputed. */
2002 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2003 skb_shinfo(skb)->gso_segs = 0;
2004 }
2005
2006 skb->protocol = htons(ETH_P_IPV6);
2007 skb_clear_hash(skb);
2008
2009 return 0;
2010}
2011
2012static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2013{
2014 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2015 u32 off = skb->network_header - skb->mac_header;
2016 int ret;
2017
2018 ret = skb_unclone(skb, GFP_ATOMIC);
2019 if (unlikely(ret < 0))
2020 return ret;
2021
2022 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2023 if (unlikely(ret < 0))
2024 return ret;
2025
2026 if (skb_is_gso(skb)) {
2027 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
2028 * be changed into SKB_GSO_TCPV4.
2029 */
2030 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2031 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2032 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2033 }
2034
2035 /* Due to IPv4 header, MSS can be upgraded. */
2036 skb_shinfo(skb)->gso_size += len_diff;
2037 /* Header must be checked, and gso_segs recomputed. */
2038 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2039 skb_shinfo(skb)->gso_segs = 0;
2040 }
2041
2042 skb->protocol = htons(ETH_P_IP);
2043 skb_clear_hash(skb);
2044
2045 return 0;
2046}
2047
2048static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2049{
2050 __be16 from_proto = skb->protocol;
2051
2052 if (from_proto == htons(ETH_P_IP) &&
2053 to_proto == htons(ETH_P_IPV6))
2054 return bpf_skb_proto_4_to_6(skb);
2055
2056 if (from_proto == htons(ETH_P_IPV6) &&
2057 to_proto == htons(ETH_P_IP))
2058 return bpf_skb_proto_6_to_4(skb);
2059
2060 return -ENOTSUPP;
2061}
2062
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002063BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2064 u64, flags)
Daniel Borkmann65781712016-06-28 12:18:27 +02002065{
Daniel Borkmann65781712016-06-28 12:18:27 +02002066 int ret;
2067
2068 if (unlikely(flags))
2069 return -EINVAL;
2070
2071 /* General idea is that this helper does the basic groundwork
2072 * needed for changing the protocol, and eBPF program fills the
2073 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2074 * and other helpers, rather than passing a raw buffer here.
2075 *
2076 * The rationale is to keep this minimal and without a need to
2077 * deal with raw packet data. F.e. even if we would pass buffers
2078 * here, the program still needs to call the bpf_lX_csum_replace()
2079 * helpers anyway. Plus, this way we keep also separation of
2080 * concerns, since f.e. bpf_skb_store_bytes() should only take
2081 * care of stores.
2082 *
2083 * Currently, additional options and extension header space are
2084 * not supported, but flags register is reserved so we can adapt
2085 * that. For offloads, we mark packet as dodgy, so that headers
2086 * need to be verified first.
2087 */
2088 ret = bpf_skb_proto_xlat(skb, proto);
2089 bpf_compute_data_end(skb);
2090 return ret;
2091}
2092
2093static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2094 .func = bpf_skb_change_proto,
2095 .gpl_only = false,
2096 .ret_type = RET_INTEGER,
2097 .arg1_type = ARG_PTR_TO_CTX,
2098 .arg2_type = ARG_ANYTHING,
2099 .arg3_type = ARG_ANYTHING,
2100};
2101
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002102BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002103{
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002104 /* We only allow a restricted subset to be changed for now. */
Daniel Borkmann45c7fff2016-08-18 01:00:38 +02002105 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2106 !skb_pkt_type_ok(pkt_type)))
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002107 return -EINVAL;
2108
2109 skb->pkt_type = pkt_type;
2110 return 0;
2111}
2112
2113static const struct bpf_func_proto bpf_skb_change_type_proto = {
2114 .func = bpf_skb_change_type,
2115 .gpl_only = false,
2116 .ret_type = RET_INTEGER,
2117 .arg1_type = ARG_PTR_TO_CTX,
2118 .arg2_type = ARG_ANYTHING,
2119};
2120
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002121static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2122{
2123 u32 min_len = skb_network_offset(skb);
2124
2125 if (skb_transport_header_was_set(skb))
2126 min_len = skb_transport_offset(skb);
2127 if (skb->ip_summed == CHECKSUM_PARTIAL)
2128 min_len = skb_checksum_start_offset(skb) +
2129 skb->csum_offset + sizeof(__sum16);
2130 return min_len;
2131}
2132
2133static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2134{
Daniel Borkmann6088b582016-09-09 02:45:28 +02002135 return skb->dev->mtu + skb->dev->hard_header_len;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002136}
2137
2138static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2139{
2140 unsigned int old_len = skb->len;
2141 int ret;
2142
2143 ret = __skb_grow_rcsum(skb, new_len);
2144 if (!ret)
2145 memset(skb->data + old_len, 0, new_len - old_len);
2146 return ret;
2147}
2148
2149static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2150{
2151 return __skb_trim_rcsum(skb, new_len);
2152}
2153
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002154BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2155 u64, flags)
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002156{
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002157 u32 max_len = __bpf_skb_max_len(skb);
2158 u32 min_len = __bpf_skb_min_len(skb);
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002159 int ret;
2160
2161 if (unlikely(flags || new_len > max_len || new_len < min_len))
2162 return -EINVAL;
2163 if (skb->encapsulation)
2164 return -ENOTSUPP;
2165
2166 /* The basic idea of this helper is that it's performing the
2167 * needed work to either grow or trim an skb, and eBPF program
2168 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2169 * bpf_lX_csum_replace() and others rather than passing a raw
2170 * buffer here. This one is a slow path helper and intended
2171 * for replies with control messages.
2172 *
2173 * Like in bpf_skb_change_proto(), we want to keep this rather
2174 * minimal and without protocol specifics so that we are able
2175 * to separate concerns as in bpf_skb_store_bytes() should only
2176 * be the one responsible for writing buffers.
2177 *
2178 * It's really expected to be a slow path operation here for
2179 * control message replies, so we're implicitly linearizing,
2180 * uncloning and drop offloads from the skb by this.
2181 */
2182 ret = __bpf_try_make_writable(skb, skb->len);
2183 if (!ret) {
2184 if (new_len > skb->len)
2185 ret = bpf_skb_grow_rcsum(skb, new_len);
2186 else if (new_len < skb->len)
2187 ret = bpf_skb_trim_rcsum(skb, new_len);
2188 if (!ret && skb_is_gso(skb))
2189 skb_gso_reset(skb);
2190 }
2191
2192 bpf_compute_data_end(skb);
2193 return ret;
2194}
2195
2196static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2197 .func = bpf_skb_change_tail,
2198 .gpl_only = false,
2199 .ret_type = RET_INTEGER,
2200 .arg1_type = ARG_PTR_TO_CTX,
2201 .arg2_type = ARG_ANYTHING,
2202 .arg3_type = ARG_ANYTHING,
2203};
2204
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002205bool bpf_helper_changes_skb_data(void *func)
2206{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002207 if (func == bpf_skb_vlan_push ||
2208 func == bpf_skb_vlan_pop ||
2209 func == bpf_skb_store_bytes ||
2210 func == bpf_skb_change_proto ||
2211 func == bpf_skb_change_tail ||
2212 func == bpf_skb_pull_data ||
Daniel Borkmannc1133c62017-05-25 01:05:07 +02002213 func == bpf_clone_redirect ||
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002214 func == bpf_l3_csum_replace ||
2215 func == bpf_l4_csum_replace)
Daniel Borkmann36976492016-02-19 23:05:25 +01002216 return true;
2217
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002218 return false;
2219}
2220
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002221static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002222 unsigned long off, unsigned long len)
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002223{
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002224 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002225
2226 if (unlikely(!ptr))
2227 return len;
2228 if (ptr != dst_buff)
2229 memcpy(dst_buff, ptr, len);
2230
2231 return 0;
2232}
2233
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002234BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2235 u64, flags, void *, meta, u64, meta_size)
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002236{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002237 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002238
2239 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2240 return -EINVAL;
2241 if (unlikely(skb_size > skb->len))
2242 return -EFAULT;
2243
2244 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2245 bpf_skb_copy);
2246}
2247
2248static const struct bpf_func_proto bpf_skb_event_output_proto = {
2249 .func = bpf_skb_event_output,
2250 .gpl_only = true,
2251 .ret_type = RET_INTEGER,
2252 .arg1_type = ARG_PTR_TO_CTX,
2253 .arg2_type = ARG_CONST_MAP_PTR,
2254 .arg3_type = ARG_ANYTHING,
2255 .arg4_type = ARG_PTR_TO_STACK,
2256 .arg5_type = ARG_CONST_STACK_SIZE,
2257};
2258
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002259static unsigned short bpf_tunnel_key_af(u64 flags)
2260{
2261 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2262}
2263
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002264BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2265 u32, size, u64, flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002266{
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002267 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2268 u8 compat[sizeof(struct bpf_tunnel_key)];
Daniel Borkmann074f5282016-04-13 00:10:52 +02002269 void *to_orig = to;
2270 int err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002271
Daniel Borkmann074f5282016-04-13 00:10:52 +02002272 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2273 err = -EINVAL;
2274 goto err_clear;
2275 }
2276 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2277 err = -EPROTO;
2278 goto err_clear;
2279 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002280 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
Daniel Borkmann074f5282016-04-13 00:10:52 +02002281 err = -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002282 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002283 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002284 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002285 goto set_compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002286 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2287 /* Fixup deprecated structure layouts here, so we have
2288 * a common path later on.
2289 */
2290 if (ip_tunnel_info_af(info) != AF_INET)
Daniel Borkmann074f5282016-04-13 00:10:52 +02002291 goto err_clear;
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002292set_compat:
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002293 to = (struct bpf_tunnel_key *)compat;
2294 break;
2295 default:
Daniel Borkmann074f5282016-04-13 00:10:52 +02002296 goto err_clear;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002297 }
2298 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002299
2300 to->tunnel_id = be64_to_cpu(info->key.tun_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002301 to->tunnel_tos = info->key.tos;
2302 to->tunnel_ttl = info->key.ttl;
2303
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002304 if (flags & BPF_F_TUNINFO_IPV6) {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002305 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2306 sizeof(to->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002307 to->tunnel_label = be32_to_cpu(info->key.label);
2308 } else {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002309 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002310 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002311
2312 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
Daniel Borkmann074f5282016-04-13 00:10:52 +02002313 memcpy(to_orig, to, size);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002314
2315 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002316err_clear:
2317 memset(to_orig, 0, size);
2318 return err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002319}
2320
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002321static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002322 .func = bpf_skb_get_tunnel_key,
2323 .gpl_only = false,
2324 .ret_type = RET_INTEGER,
2325 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002326 .arg2_type = ARG_PTR_TO_RAW_STACK,
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002327 .arg3_type = ARG_CONST_STACK_SIZE,
2328 .arg4_type = ARG_ANYTHING,
2329};
2330
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002331BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002332{
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002333 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002334 int err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002335
2336 if (unlikely(!info ||
Daniel Borkmann074f5282016-04-13 00:10:52 +02002337 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2338 err = -ENOENT;
2339 goto err_clear;
2340 }
2341 if (unlikely(size < info->options_len)) {
2342 err = -ENOMEM;
2343 goto err_clear;
2344 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002345
2346 ip_tunnel_info_opts_get(to, info);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002347 if (size > info->options_len)
2348 memset(to + info->options_len, 0, size - info->options_len);
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002349
2350 return info->options_len;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002351err_clear:
2352 memset(to, 0, size);
2353 return err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002354}
2355
2356static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2357 .func = bpf_skb_get_tunnel_opt,
2358 .gpl_only = false,
2359 .ret_type = RET_INTEGER,
2360 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002361 .arg2_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002362 .arg3_type = ARG_CONST_STACK_SIZE,
2363};
2364
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002365static struct metadata_dst __percpu *md_dst;
2366
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002367BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2368 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002369{
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002370 struct metadata_dst *md = this_cpu_ptr(md_dst);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002371 u8 compat[sizeof(struct bpf_tunnel_key)];
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002372 struct ip_tunnel_info *info;
2373
Daniel Borkmann22080872016-03-04 15:15:05 +01002374 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2375 BPF_F_DONT_FRAGMENT)))
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002376 return -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002377 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2378 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002379 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002380 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002381 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2382 /* Fixup deprecated structure layouts here, so we have
2383 * a common path later on.
2384 */
2385 memcpy(compat, from, size);
2386 memset(compat + size, 0, sizeof(compat) - size);
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002387 from = (const struct bpf_tunnel_key *) compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002388 break;
2389 default:
2390 return -EINVAL;
2391 }
2392 }
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002393 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2394 from->tunnel_ext))
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002395 return -EINVAL;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002396
2397 skb_dst_drop(skb);
2398 dst_hold((struct dst_entry *) md);
2399 skb_dst_set(skb, (struct dst_entry *) md);
2400
2401 info = &md->u.tun_info;
2402 info->mode = IP_TUNNEL_INFO_TX;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002403
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002404 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
Daniel Borkmann22080872016-03-04 15:15:05 +01002405 if (flags & BPF_F_DONT_FRAGMENT)
2406 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2407
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002408 info->key.tun_id = cpu_to_be64(from->tunnel_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002409 info->key.tos = from->tunnel_tos;
2410 info->key.ttl = from->tunnel_ttl;
2411
2412 if (flags & BPF_F_TUNINFO_IPV6) {
2413 info->mode |= IP_TUNNEL_INFO_IPV6;
2414 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2415 sizeof(from->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002416 info->key.label = cpu_to_be32(from->tunnel_label) &
2417 IPV6_FLOWLABEL_MASK;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002418 } else {
2419 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
Daniel Borkmann2da897e2016-02-23 02:05:26 +01002420 if (flags & BPF_F_ZERO_CSUM_TX)
2421 info->key.tun_flags &= ~TUNNEL_CSUM;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002422 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002423
2424 return 0;
2425}
2426
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002427static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002428 .func = bpf_skb_set_tunnel_key,
2429 .gpl_only = false,
2430 .ret_type = RET_INTEGER,
2431 .arg1_type = ARG_PTR_TO_CTX,
2432 .arg2_type = ARG_PTR_TO_STACK,
2433 .arg3_type = ARG_CONST_STACK_SIZE,
2434 .arg4_type = ARG_ANYTHING,
2435};
2436
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002437BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2438 const u8 *, from, u32, size)
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002439{
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002440 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2441 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2442
2443 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2444 return -EINVAL;
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002445 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002446 return -ENOMEM;
2447
2448 ip_tunnel_info_opts_set(info, from, size);
2449
2450 return 0;
2451}
2452
2453static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2454 .func = bpf_skb_set_tunnel_opt,
2455 .gpl_only = false,
2456 .ret_type = RET_INTEGER,
2457 .arg1_type = ARG_PTR_TO_CTX,
2458 .arg2_type = ARG_PTR_TO_STACK,
2459 .arg3_type = ARG_CONST_STACK_SIZE,
2460};
2461
2462static const struct bpf_func_proto *
2463bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002464{
2465 if (!md_dst) {
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002466 /* Race is not possible, since it's called from verifier
2467 * that is holding verifier mutex.
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002468 */
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002469 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002470 GFP_KERNEL);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002471 if (!md_dst)
2472 return NULL;
2473 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002474
2475 switch (which) {
2476 case BPF_FUNC_skb_set_tunnel_key:
2477 return &bpf_skb_set_tunnel_key_proto;
2478 case BPF_FUNC_skb_set_tunnel_opt:
2479 return &bpf_skb_set_tunnel_opt_proto;
2480 default:
2481 return NULL;
2482 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002483}
2484
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002485BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2486 u32, idx)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002487{
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002488 struct bpf_array *array = container_of(map, struct bpf_array, map);
2489 struct cgroup *cgrp;
2490 struct sock *sk;
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002491
Daniel Borkmann2d48c5f2016-09-23 01:28:35 +02002492 sk = skb_to_full_sk(skb);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002493 if (!sk || !sk_fullsock(sk))
2494 return -ENOENT;
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002495 if (unlikely(idx >= array->map.max_entries))
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002496 return -E2BIG;
2497
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002498 cgrp = READ_ONCE(array->ptrs[idx]);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002499 if (unlikely(!cgrp))
2500 return -EAGAIN;
2501
Daniel Borkmann54fd9c22016-08-18 01:00:41 +02002502 return sk_under_cgroup_hierarchy(sk, cgrp);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002503}
2504
Daniel Borkmann747ea552016-08-12 22:17:17 +02002505static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2506 .func = bpf_skb_under_cgroup,
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002507 .gpl_only = false,
2508 .ret_type = RET_INTEGER,
2509 .arg1_type = ARG_PTR_TO_CTX,
2510 .arg2_type = ARG_CONST_MAP_PTR,
2511 .arg3_type = ARG_ANYTHING,
2512};
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002513
Daniel Borkmann4de16962016-08-18 01:00:40 +02002514static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2515 unsigned long off, unsigned long len)
2516{
2517 memcpy(dst_buff, src_buff + off, len);
2518 return 0;
2519}
2520
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002521BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2522 u64, flags, void *, meta, u64, meta_size)
Daniel Borkmann4de16962016-08-18 01:00:40 +02002523{
Daniel Borkmann4de16962016-08-18 01:00:40 +02002524 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
Daniel Borkmann4de16962016-08-18 01:00:40 +02002525
2526 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2527 return -EINVAL;
2528 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2529 return -EFAULT;
2530
2531 return bpf_event_output(map, flags, meta, meta_size, xdp, xdp_size,
2532 bpf_xdp_copy);
2533}
2534
2535static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2536 .func = bpf_xdp_event_output,
2537 .gpl_only = true,
2538 .ret_type = RET_INTEGER,
2539 .arg1_type = ARG_PTR_TO_CTX,
2540 .arg2_type = ARG_CONST_MAP_PTR,
2541 .arg3_type = ARG_ANYTHING,
2542 .arg4_type = ARG_PTR_TO_STACK,
2543 .arg5_type = ARG_CONST_STACK_SIZE,
2544};
2545
Chenbo Feng09313662017-03-22 17:27:34 -07002546BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
2547{
2548 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
2549}
2550
2551static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
2552 .func = bpf_get_socket_cookie,
2553 .gpl_only = false,
2554 .ret_type = RET_INTEGER,
2555 .arg1_type = ARG_PTR_TO_CTX,
2556};
2557
Chenbo Fenga29c81e2017-03-22 17:27:35 -07002558BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
2559{
2560 struct sock *sk = sk_to_full_sk(skb->sk);
2561 kuid_t kuid;
2562
2563 if (!sk || !sk_fullsock(sk))
2564 return overflowuid;
2565 kuid = sock_net_uid(sock_net(sk), sk);
2566 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
2567}
2568
2569static const struct bpf_func_proto bpf_get_socket_uid_proto = {
2570 .func = bpf_get_socket_uid,
2571 .gpl_only = false,
2572 .ret_type = RET_INTEGER,
2573 .arg1_type = ARG_PTR_TO_CTX,
2574};
2575
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002576static const struct bpf_func_proto *
2577sk_filter_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002578{
2579 switch (func_id) {
2580 case BPF_FUNC_map_lookup_elem:
2581 return &bpf_map_lookup_elem_proto;
2582 case BPF_FUNC_map_update_elem:
2583 return &bpf_map_update_elem_proto;
2584 case BPF_FUNC_map_delete_elem:
2585 return &bpf_map_delete_elem_proto;
Daniel Borkmann03e69b52015-03-14 02:27:16 +01002586 case BPF_FUNC_get_prandom_u32:
2587 return &bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01002588 case BPF_FUNC_get_smp_processor_id:
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002589 return &bpf_get_raw_smp_processor_id_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07002590 case BPF_FUNC_tail_call:
2591 return &bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02002592 case BPF_FUNC_ktime_get_ns:
2593 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07002594 case BPF_FUNC_trace_printk:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002595 if (capable(CAP_SYS_ADMIN))
2596 return bpf_get_trace_printk_proto();
Chenbo Feng09313662017-03-22 17:27:34 -07002597 case BPF_FUNC_get_socket_cookie:
2598 return &bpf_get_socket_cookie_proto;
Chenbo Fenga29c81e2017-03-22 17:27:35 -07002599 case BPF_FUNC_get_socket_uid:
2600 return &bpf_get_socket_uid_proto;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002601 default:
2602 return NULL;
2603 }
2604}
2605
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002606static const struct bpf_func_proto *
2607tc_cls_act_func_proto(enum bpf_func_id func_id)
2608{
2609 switch (func_id) {
2610 case BPF_FUNC_skb_store_bytes:
2611 return &bpf_skb_store_bytes_proto;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01002612 case BPF_FUNC_skb_load_bytes:
2613 return &bpf_skb_load_bytes_proto;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002614 case BPF_FUNC_skb_pull_data:
2615 return &bpf_skb_pull_data_proto;
Daniel Borkmann7d672342016-02-19 23:05:23 +01002616 case BPF_FUNC_csum_diff:
2617 return &bpf_csum_diff_proto;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002618 case BPF_FUNC_csum_update:
2619 return &bpf_csum_update_proto;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07002620 case BPF_FUNC_l3_csum_replace:
2621 return &bpf_l3_csum_replace_proto;
2622 case BPF_FUNC_l4_csum_replace:
2623 return &bpf_l4_csum_replace_proto;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07002624 case BPF_FUNC_clone_redirect:
2625 return &bpf_clone_redirect_proto;
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02002626 case BPF_FUNC_get_cgroup_classid:
2627 return &bpf_get_cgroup_classid_proto;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002628 case BPF_FUNC_skb_vlan_push:
2629 return &bpf_skb_vlan_push_proto;
2630 case BPF_FUNC_skb_vlan_pop:
2631 return &bpf_skb_vlan_pop_proto;
Daniel Borkmann65781712016-06-28 12:18:27 +02002632 case BPF_FUNC_skb_change_proto:
2633 return &bpf_skb_change_proto_proto;
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002634 case BPF_FUNC_skb_change_type:
2635 return &bpf_skb_change_type_proto;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002636 case BPF_FUNC_skb_change_tail:
2637 return &bpf_skb_change_tail_proto;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002638 case BPF_FUNC_skb_get_tunnel_key:
2639 return &bpf_skb_get_tunnel_key_proto;
2640 case BPF_FUNC_skb_set_tunnel_key:
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002641 return bpf_get_skb_set_tunnel_proto(func_id);
2642 case BPF_FUNC_skb_get_tunnel_opt:
2643 return &bpf_skb_get_tunnel_opt_proto;
2644 case BPF_FUNC_skb_set_tunnel_opt:
2645 return bpf_get_skb_set_tunnel_proto(func_id);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07002646 case BPF_FUNC_redirect:
2647 return &bpf_redirect_proto;
Daniel Borkmannc46646d2015-09-30 01:41:51 +02002648 case BPF_FUNC_get_route_realm:
2649 return &bpf_get_route_realm_proto;
Daniel Borkmann13c5c242016-07-03 01:28:47 +02002650 case BPF_FUNC_get_hash_recalc:
2651 return &bpf_get_hash_recalc_proto;
Daniel Borkmann7a4b28c2016-09-23 01:28:37 +02002652 case BPF_FUNC_set_hash_invalid:
2653 return &bpf_set_hash_invalid_proto;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02002654 case BPF_FUNC_perf_event_output:
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002655 return &bpf_skb_event_output_proto;
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002656 case BPF_FUNC_get_smp_processor_id:
2657 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann747ea552016-08-12 22:17:17 +02002658 case BPF_FUNC_skb_under_cgroup:
2659 return &bpf_skb_under_cgroup_proto;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002660 default:
2661 return sk_filter_func_proto(func_id);
2662 }
2663}
2664
Brenden Blanco6a773a12016-07-19 12:16:47 -07002665static const struct bpf_func_proto *
2666xdp_func_proto(enum bpf_func_id func_id)
2667{
Daniel Borkmann4de16962016-08-18 01:00:40 +02002668 switch (func_id) {
2669 case BPF_FUNC_perf_event_output:
2670 return &bpf_xdp_event_output_proto;
Daniel Borkmann669dc4d2016-09-23 01:28:36 +02002671 case BPF_FUNC_get_smp_processor_id:
2672 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann4de16962016-08-18 01:00:40 +02002673 default:
2674 return sk_filter_func_proto(func_id);
2675 }
Brenden Blanco6a773a12016-07-19 12:16:47 -07002676}
2677
Daniel Mack760b55c2016-11-23 16:52:25 +01002678static const struct bpf_func_proto *
2679cg_skb_func_proto(enum bpf_func_id func_id)
2680{
2681 switch (func_id) {
2682 case BPF_FUNC_skb_load_bytes:
2683 return &bpf_skb_load_bytes_proto;
2684 default:
2685 return sk_filter_func_proto(func_id);
2686 }
2687}
2688
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002689static bool __is_valid_access(int off, int size, enum bpf_access_type type)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002690{
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002691 if (off < 0 || off >= sizeof(struct __sk_buff))
2692 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002693 /* The verifier guarantees that size > 0. */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002694 if (off % size != 0)
2695 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002696 if (size != sizeof(__u32))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002697 return false;
2698
2699 return true;
2700}
2701
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002702static bool sk_filter_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002703 enum bpf_access_type type,
2704 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002705{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002706 switch (off) {
2707 case offsetof(struct __sk_buff, tc_classid):
2708 case offsetof(struct __sk_buff, data):
2709 case offsetof(struct __sk_buff, data_end):
Daniel Borkmann045efa82015-09-15 23:05:42 -07002710 return false;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002711 }
Daniel Borkmann045efa82015-09-15 23:05:42 -07002712
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002713 if (type == BPF_WRITE) {
2714 switch (off) {
2715 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann4936e352016-05-13 19:08:26 +02002716 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002717 break;
2718 default:
2719 return false;
2720 }
2721 }
2722
2723 return __is_valid_access(off, size, type);
2724}
2725
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002726static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
2727 const struct bpf_prog *prog)
2728{
2729 struct bpf_insn *insn = insn_buf;
2730
2731 if (!direct_write)
2732 return 0;
2733
2734 /* if (!skb->cloned)
2735 * goto start;
2736 *
2737 * (Fast-path, otherwise approximation that we might be
2738 * a clone, do the rest in helper.)
2739 */
2740 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
2741 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
2742 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
2743
2744 /* ret = bpf_skb_pull_data(skb, 0); */
2745 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
2746 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
2747 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2748 BPF_FUNC_skb_pull_data);
2749 /* if (!ret)
2750 * goto restore;
2751 * return TC_ACT_SHOT;
2752 */
2753 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
2754 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT);
2755 *insn++ = BPF_EXIT_INSN();
2756
2757 /* restore: */
2758 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
2759 /* start: */
2760 *insn++ = prog->insnsi[0];
2761
2762 return insn - insn_buf;
2763}
2764
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002765static bool tc_cls_act_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002766 enum bpf_access_type type,
2767 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002768{
2769 if (type == BPF_WRITE) {
2770 switch (off) {
2771 case offsetof(struct __sk_buff, mark):
2772 case offsetof(struct __sk_buff, tc_index):
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002773 case offsetof(struct __sk_buff, priority):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002774 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002775 offsetof(struct __sk_buff, cb[4]):
2776 case offsetof(struct __sk_buff, tc_classid):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002777 break;
2778 default:
2779 return false;
2780 }
2781 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002782
2783 switch (off) {
2784 case offsetof(struct __sk_buff, data):
2785 *reg_type = PTR_TO_PACKET;
2786 break;
2787 case offsetof(struct __sk_buff, data_end):
2788 *reg_type = PTR_TO_PACKET_END;
2789 break;
2790 }
2791
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002792 return __is_valid_access(off, size, type);
2793}
2794
Brenden Blanco6a773a12016-07-19 12:16:47 -07002795static bool __is_valid_xdp_access(int off, int size,
2796 enum bpf_access_type type)
2797{
2798 if (off < 0 || off >= sizeof(struct xdp_md))
2799 return false;
2800 if (off % size != 0)
2801 return false;
Daniel Borkmann6088b582016-09-09 02:45:28 +02002802 if (size != sizeof(__u32))
Brenden Blanco6a773a12016-07-19 12:16:47 -07002803 return false;
2804
2805 return true;
2806}
2807
2808static bool xdp_is_valid_access(int off, int size,
2809 enum bpf_access_type type,
2810 enum bpf_reg_type *reg_type)
2811{
2812 if (type == BPF_WRITE)
2813 return false;
2814
2815 switch (off) {
2816 case offsetof(struct xdp_md, data):
2817 *reg_type = PTR_TO_PACKET;
2818 break;
2819 case offsetof(struct xdp_md, data_end):
2820 *reg_type = PTR_TO_PACKET_END;
2821 break;
2822 }
2823
2824 return __is_valid_xdp_access(off, size, type);
2825}
2826
2827void bpf_warn_invalid_xdp_action(u32 act)
2828{
2829 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
2830}
2831EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
2832
Daniel Borkmann374fb542016-09-09 02:45:30 +02002833static u32 sk_filter_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2834 int src_reg, int ctx_off,
2835 struct bpf_insn *insn_buf,
2836 struct bpf_prog *prog)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002837{
2838 struct bpf_insn *insn = insn_buf;
2839
2840 switch (ctx_off) {
2841 case offsetof(struct __sk_buff, len):
2842 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2843
2844 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2845 offsetof(struct sk_buff, len));
2846 break;
2847
Daniel Borkmann0b8c7072015-03-19 19:38:27 +01002848 case offsetof(struct __sk_buff, protocol):
2849 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2850
2851 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2852 offsetof(struct sk_buff, protocol));
2853 break;
2854
Michal Sekletar27cd5452015-03-24 14:48:41 +01002855 case offsetof(struct __sk_buff, vlan_proto):
2856 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2857
2858 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2859 offsetof(struct sk_buff, vlan_proto));
2860 break;
2861
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002862 case offsetof(struct __sk_buff, priority):
2863 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2864
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002865 if (type == BPF_WRITE)
2866 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2867 offsetof(struct sk_buff, priority));
2868 else
2869 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2870 offsetof(struct sk_buff, priority));
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002871 break;
2872
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002873 case offsetof(struct __sk_buff, ingress_ifindex):
2874 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2875
2876 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2877 offsetof(struct sk_buff, skb_iif));
2878 break;
2879
2880 case offsetof(struct __sk_buff, ifindex):
2881 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2882
Daniel Borkmannf035a512016-09-09 02:45:29 +02002883 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002884 dst_reg, src_reg,
2885 offsetof(struct sk_buff, dev));
2886 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2887 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2888 offsetof(struct net_device, ifindex));
2889 break;
2890
Daniel Borkmannba7591d2015-08-01 00:46:29 +02002891 case offsetof(struct __sk_buff, hash):
2892 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2893
2894 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2895 offsetof(struct sk_buff, hash));
2896 break;
2897
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002898 case offsetof(struct __sk_buff, mark):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002899 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2900
2901 if (type == BPF_WRITE)
2902 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2903 offsetof(struct sk_buff, mark));
2904 else
2905 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2906 offsetof(struct sk_buff, mark));
2907 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002908
2909 case offsetof(struct __sk_buff, pkt_type):
2910 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2911
2912 case offsetof(struct __sk_buff, queue_mapping):
2913 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002914
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002915 case offsetof(struct __sk_buff, vlan_present):
2916 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2917 dst_reg, src_reg, insn);
2918
2919 case offsetof(struct __sk_buff, vlan_tci):
2920 return convert_skb_access(SKF_AD_VLAN_TAG,
2921 dst_reg, src_reg, insn);
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002922
2923 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann6088b582016-09-09 02:45:28 +02002924 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002925 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2926
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002927 prog->cb_access = 1;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002928 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2929 ctx_off += offsetof(struct sk_buff, cb);
2930 ctx_off += offsetof(struct qdisc_skb_cb, data);
2931 if (type == BPF_WRITE)
2932 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2933 else
2934 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2935 break;
2936
Daniel Borkmann045efa82015-09-15 23:05:42 -07002937 case offsetof(struct __sk_buff, tc_classid):
2938 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2939 ctx_off += offsetof(struct sk_buff, cb);
2940 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002941 if (type == BPF_WRITE)
2942 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2943 else
2944 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
Daniel Borkmann045efa82015-09-15 23:05:42 -07002945 break;
2946
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002947 case offsetof(struct __sk_buff, data):
Daniel Borkmannf035a512016-09-09 02:45:29 +02002948 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002949 dst_reg, src_reg,
2950 offsetof(struct sk_buff, data));
2951 break;
2952
2953 case offsetof(struct __sk_buff, data_end):
2954 ctx_off -= offsetof(struct __sk_buff, data_end);
2955 ctx_off += offsetof(struct sk_buff, cb);
2956 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
Daniel Borkmannf035a512016-09-09 02:45:29 +02002957 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), dst_reg, src_reg,
2958 ctx_off);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002959 break;
2960
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002961 case offsetof(struct __sk_buff, tc_index):
2962#ifdef CONFIG_NET_SCHED
2963 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2964
2965 if (type == BPF_WRITE)
2966 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2967 offsetof(struct sk_buff, tc_index));
2968 else
2969 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2970 offsetof(struct sk_buff, tc_index));
2971 break;
2972#else
2973 if (type == BPF_WRITE)
2974 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2975 else
2976 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2977 break;
2978#endif
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002979 }
2980
2981 return insn - insn_buf;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002982}
2983
Daniel Borkmann374fb542016-09-09 02:45:30 +02002984static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2985 int src_reg, int ctx_off,
2986 struct bpf_insn *insn_buf,
2987 struct bpf_prog *prog)
2988{
2989 struct bpf_insn *insn = insn_buf;
2990
2991 switch (ctx_off) {
2992 case offsetof(struct __sk_buff, ifindex):
2993 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2994
2995 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
2996 dst_reg, src_reg,
2997 offsetof(struct sk_buff, dev));
2998 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2999 offsetof(struct net_device, ifindex));
3000 break;
3001 default:
3002 return sk_filter_convert_ctx_access(type, dst_reg, src_reg,
3003 ctx_off, insn_buf, prog);
3004 }
3005
3006 return insn - insn_buf;
3007}
3008
Brenden Blanco6a773a12016-07-19 12:16:47 -07003009static u32 xdp_convert_ctx_access(enum bpf_access_type type, int dst_reg,
3010 int src_reg, int ctx_off,
3011 struct bpf_insn *insn_buf,
3012 struct bpf_prog *prog)
3013{
3014 struct bpf_insn *insn = insn_buf;
3015
3016 switch (ctx_off) {
3017 case offsetof(struct xdp_md, data):
Daniel Borkmannf035a512016-09-09 02:45:29 +02003018 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
Brenden Blanco6a773a12016-07-19 12:16:47 -07003019 dst_reg, src_reg,
3020 offsetof(struct xdp_buff, data));
3021 break;
3022 case offsetof(struct xdp_md, data_end):
Daniel Borkmannf035a512016-09-09 02:45:29 +02003023 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
Brenden Blanco6a773a12016-07-19 12:16:47 -07003024 dst_reg, src_reg,
3025 offsetof(struct xdp_buff, data_end));
3026 break;
3027 }
3028
3029 return insn - insn_buf;
3030}
3031
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003032static const struct bpf_verifier_ops sk_filter_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003033 .get_func_proto = sk_filter_func_proto,
3034 .is_valid_access = sk_filter_is_valid_access,
Daniel Borkmann374fb542016-09-09 02:45:30 +02003035 .convert_ctx_access = sk_filter_convert_ctx_access,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003036};
3037
Alexei Starovoitov608cd712015-03-26 19:53:57 -07003038static const struct bpf_verifier_ops tc_cls_act_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003039 .get_func_proto = tc_cls_act_func_proto,
3040 .is_valid_access = tc_cls_act_is_valid_access,
Daniel Borkmann374fb542016-09-09 02:45:30 +02003041 .convert_ctx_access = tc_cls_act_convert_ctx_access,
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003042 .gen_prologue = tc_cls_act_prologue,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07003043};
3044
Brenden Blanco6a773a12016-07-19 12:16:47 -07003045static const struct bpf_verifier_ops xdp_ops = {
3046 .get_func_proto = xdp_func_proto,
3047 .is_valid_access = xdp_is_valid_access,
3048 .convert_ctx_access = xdp_convert_ctx_access,
3049};
3050
Daniel Mack760b55c2016-11-23 16:52:25 +01003051static const struct bpf_verifier_ops cg_skb_ops = {
3052 .get_func_proto = cg_skb_func_proto,
3053 .is_valid_access = sk_filter_is_valid_access,
3054 .convert_ctx_access = sk_filter_convert_ctx_access,
3055};
3056
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003057static struct bpf_prog_type_list sk_filter_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003058 .ops = &sk_filter_ops,
3059 .type = BPF_PROG_TYPE_SOCKET_FILTER,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003060};
3061
Daniel Borkmann96be4322015-03-01 12:31:46 +01003062static struct bpf_prog_type_list sched_cls_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003063 .ops = &tc_cls_act_ops,
3064 .type = BPF_PROG_TYPE_SCHED_CLS,
Daniel Borkmann96be4322015-03-01 12:31:46 +01003065};
3066
Daniel Borkmann94caee82015-03-20 15:11:11 +01003067static struct bpf_prog_type_list sched_act_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003068 .ops = &tc_cls_act_ops,
3069 .type = BPF_PROG_TYPE_SCHED_ACT,
Daniel Borkmann94caee82015-03-20 15:11:11 +01003070};
3071
Brenden Blanco6a773a12016-07-19 12:16:47 -07003072static struct bpf_prog_type_list xdp_type __read_mostly = {
3073 .ops = &xdp_ops,
3074 .type = BPF_PROG_TYPE_XDP,
3075};
3076
Daniel Mack760b55c2016-11-23 16:52:25 +01003077static struct bpf_prog_type_list cg_skb_type __read_mostly = {
3078 .ops = &cg_skb_ops,
3079 .type = BPF_PROG_TYPE_CGROUP_SKB,
3080};
3081
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003082static int __init register_sk_filter_ops(void)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003083{
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003084 bpf_register_prog_type(&sk_filter_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01003085 bpf_register_prog_type(&sched_cls_type);
Daniel Borkmann94caee82015-03-20 15:11:11 +01003086 bpf_register_prog_type(&sched_act_type);
Brenden Blanco6a773a12016-07-19 12:16:47 -07003087 bpf_register_prog_type(&xdp_type);
Daniel Mack760b55c2016-11-23 16:52:25 +01003088 bpf_register_prog_type(&cg_skb_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01003089
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003090 return 0;
3091}
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003092late_initcall(register_sk_filter_ops);
3093
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003094int sk_detach_filter(struct sock *sk)
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003095{
3096 int ret = -ENOENT;
3097 struct sk_filter *filter;
3098
Vincent Bernatd59577b2013-01-16 22:55:49 +01003099 if (sock_flag(sk, SOCK_FILTER_LOCKED))
3100 return -EPERM;
3101
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003102 filter = rcu_dereference_protected(sk->sk_filter,
3103 lockdep_sock_is_held(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003104 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003105 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -08003106 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003107 ret = 0;
3108 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003109
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003110 return ret;
3111}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003112EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003113
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003114int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
3115 unsigned int len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003116{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003117 struct sock_fprog_kern *fprog;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003118 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003119 int ret = 0;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003120
3121 lock_sock(sk);
3122 filter = rcu_dereference_protected(sk->sk_filter,
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003123 lockdep_sock_is_held(sk));
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003124 if (!filter)
3125 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003126
3127 /* We're copying the filter that has been originally attached,
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003128 * so no conversion/decode needed anymore. eBPF programs that
3129 * have no original program cannot be dumped through this.
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003130 */
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003131 ret = -EACCES;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003132 fprog = filter->prog->orig_prog;
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003133 if (!fprog)
3134 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003135
3136 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003137 if (!len)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003138 /* User space only enquires number of filter blocks. */
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003139 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003140
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003141 ret = -EINVAL;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003142 if (len < fprog->len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003143 goto out;
3144
3145 ret = -EFAULT;
Alexei Starovoitov009937e2014-07-30 20:34:13 -07003146 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003147 goto out;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003148
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003149 /* Instead of bytes, the API requests to return the number
3150 * of filter blocks.
3151 */
3152 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003153out:
3154 release_sock(sk);
3155 return ret;
3156}