blob: 768688bfc7df089021762928fdfdd4b54491a7ee [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:
Maciej Żenczykowski7d082632019-01-24 03:07:02 -08001714#ifdef ARPHRD_RAWIP
1715 case ARPHRD_RAWIP:
1716#endif
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001717 return __bpf_redirect_no_mac(skb, dev, flags);
1718 default:
1719 return __bpf_redirect_common(skb, dev, flags);
1720 }
1721}
1722
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001723BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001724{
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001725 struct net_device *dev;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001726 struct sk_buff *clone;
1727 int ret;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001728
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001729 if (unlikely(flags & ~(BPF_F_INGRESS)))
1730 return -EINVAL;
1731
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001732 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
1733 if (unlikely(!dev))
1734 return -EINVAL;
1735
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001736 clone = skb_clone(skb, GFP_ATOMIC);
1737 if (unlikely(!clone))
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001738 return -ENOMEM;
1739
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001740 /* For direct write, we need to keep the invariant that the skbs
1741 * we're dealing with need to be uncloned. Should uncloning fail
1742 * here, we need to free the just generated clone to unclone once
1743 * again.
1744 */
1745 ret = bpf_try_make_head_writable(skb);
1746 if (unlikely(ret)) {
1747 kfree_skb(clone);
1748 return -ENOMEM;
1749 }
1750
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001751 return __bpf_redirect(clone, dev, flags);
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001752}
1753
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001754static const struct bpf_func_proto bpf_clone_redirect_proto = {
Alexei Starovoitov3896d652015-06-02 16:03:14 -07001755 .func = bpf_clone_redirect,
1756 .gpl_only = false,
1757 .ret_type = RET_INTEGER,
1758 .arg1_type = ARG_PTR_TO_CTX,
1759 .arg2_type = ARG_ANYTHING,
1760 .arg3_type = ARG_ANYTHING,
1761};
1762
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001763struct redirect_info {
1764 u32 ifindex;
1765 u32 flags;
1766};
1767
1768static DEFINE_PER_CPU(struct redirect_info, redirect_info);
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001769
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001770BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001771{
1772 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1773
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001774 if (unlikely(flags & ~(BPF_F_INGRESS)))
1775 return TC_ACT_SHOT;
1776
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001777 ri->ifindex = ifindex;
1778 ri->flags = flags;
Daniel Borkmann781c53b2016-01-11 01:16:38 +01001779
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001780 return TC_ACT_REDIRECT;
1781}
1782
1783int skb_do_redirect(struct sk_buff *skb)
1784{
1785 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
1786 struct net_device *dev;
1787
1788 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
1789 ri->ifindex = 0;
1790 if (unlikely(!dev)) {
1791 kfree_skb(skb);
1792 return -EINVAL;
1793 }
1794
Martin KaFai Lau4e3264d2016-11-09 15:36:33 -08001795 return __bpf_redirect(skb, dev, ri->flags);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001796}
1797
Daniel Borkmann577c50a2016-03-04 15:15:04 +01001798static const struct bpf_func_proto bpf_redirect_proto = {
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07001799 .func = bpf_redirect,
1800 .gpl_only = false,
1801 .ret_type = RET_INTEGER,
1802 .arg1_type = ARG_ANYTHING,
1803 .arg2_type = ARG_ANYTHING,
1804};
1805
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001806BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001807{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001808 return task_get_classid(skb);
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02001809}
1810
1811static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
1812 .func = bpf_get_cgroup_classid,
1813 .gpl_only = false,
1814 .ret_type = RET_INTEGER,
1815 .arg1_type = ARG_PTR_TO_CTX,
1816};
1817
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001818BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001819{
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001820 return dst_tclassid(skb);
Daniel Borkmannc46646d2015-09-30 01:41:51 +02001821}
1822
1823static const struct bpf_func_proto bpf_get_route_realm_proto = {
1824 .func = bpf_get_route_realm,
1825 .gpl_only = false,
1826 .ret_type = RET_INTEGER,
1827 .arg1_type = ARG_PTR_TO_CTX,
1828};
1829
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001830BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
Daniel Borkmann13c5c242016-07-03 01:28:47 +02001831{
1832 /* If skb_clear_hash() was called due to mangling, we can
1833 * trigger SW recalculation here. Later access to hash
1834 * can then use the inline skb->hash via context directly
1835 * instead of calling this helper again.
1836 */
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001837 return skb_get_hash(skb);
Daniel Borkmann13c5c242016-07-03 01:28:47 +02001838}
1839
1840static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1841 .func = bpf_get_hash_recalc,
1842 .gpl_only = false,
1843 .ret_type = RET_INTEGER,
1844 .arg1_type = ARG_PTR_TO_CTX,
1845};
1846
Daniel Borkmann7a4b28c2016-09-23 01:28:37 +02001847BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
1848{
1849 /* After all direct packet write, this can be used once for
1850 * triggering a lazy recalc on next skb_get_hash() invocation.
1851 */
1852 skb_clear_hash(skb);
1853 return 0;
1854}
1855
1856static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
1857 .func = bpf_set_hash_invalid,
1858 .gpl_only = false,
1859 .ret_type = RET_INTEGER,
1860 .arg1_type = ARG_PTR_TO_CTX,
1861};
1862
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001863BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
1864 u16, vlan_tci)
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001865{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001866 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001867
1868 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
1869 vlan_proto != htons(ETH_P_8021AD)))
1870 vlan_proto = htons(ETH_P_8021Q);
1871
Daniel Borkmann80656942016-08-05 00:11:13 +02001872 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001873 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
Daniel Borkmann80656942016-08-05 00:11:13 +02001874 bpf_pull_mac_rcsum(skb);
1875
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001876 bpf_compute_data_end(skb);
1877 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001878}
1879
1880const struct bpf_func_proto bpf_skb_vlan_push_proto = {
1881 .func = bpf_skb_vlan_push,
1882 .gpl_only = false,
1883 .ret_type = RET_INTEGER,
1884 .arg1_type = ARG_PTR_TO_CTX,
1885 .arg2_type = ARG_ANYTHING,
1886 .arg3_type = ARG_ANYTHING,
1887};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001888EXPORT_SYMBOL_GPL(bpf_skb_vlan_push_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001889
Daniel Borkmannf3694e02016-09-09 02:45:31 +02001890BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001891{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001892 int ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001893
Daniel Borkmann80656942016-08-05 00:11:13 +02001894 bpf_push_mac_rcsum(skb);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001895 ret = skb_vlan_pop(skb);
Daniel Borkmann80656942016-08-05 00:11:13 +02001896 bpf_pull_mac_rcsum(skb);
1897
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07001898 bpf_compute_data_end(skb);
1899 return ret;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001900}
1901
1902const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
1903 .func = bpf_skb_vlan_pop,
1904 .gpl_only = false,
1905 .ret_type = RET_INTEGER,
1906 .arg1_type = ARG_PTR_TO_CTX,
1907};
Alexei Starovoitov4d9c5c52015-07-20 20:34:19 -07001908EXPORT_SYMBOL_GPL(bpf_skb_vlan_pop_proto);
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07001909
Daniel Borkmann65781712016-06-28 12:18:27 +02001910static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
1911{
1912 /* Caller already did skb_cow() with len as headroom,
1913 * so no need to do it here.
1914 */
1915 skb_push(skb, len);
1916 memmove(skb->data, skb->data + len, off);
1917 memset(skb->data + off, 0, len);
1918
1919 /* No skb_postpush_rcsum(skb, skb->data + off, len)
1920 * needed here as it does not change the skb->csum
1921 * result for checksum complete when summing over
1922 * zeroed blocks.
1923 */
1924 return 0;
1925}
1926
1927static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
1928{
1929 /* skb_ensure_writable() is not needed here, as we're
1930 * already working on an uncloned skb.
1931 */
1932 if (unlikely(!pskb_may_pull(skb, off + len)))
1933 return -ENOMEM;
1934
1935 skb_postpull_rcsum(skb, skb->data + off, len);
1936 memmove(skb->data + len, skb->data, off);
1937 __skb_pull(skb, len);
1938
1939 return 0;
1940}
1941
1942static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
1943{
1944 bool trans_same = skb->transport_header == skb->network_header;
1945 int ret;
1946
1947 /* There's no need for __skb_push()/__skb_pull() pair to
1948 * get to the start of the mac header as we're guaranteed
1949 * to always start from here under eBPF.
1950 */
1951 ret = bpf_skb_generic_push(skb, off, len);
1952 if (likely(!ret)) {
1953 skb->mac_header -= len;
1954 skb->network_header -= len;
1955 if (trans_same)
1956 skb->transport_header = skb->network_header;
1957 }
1958
1959 return ret;
1960}
1961
1962static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
1963{
1964 bool trans_same = skb->transport_header == skb->network_header;
1965 int ret;
1966
1967 /* Same here, __skb_push()/__skb_pull() pair not needed. */
1968 ret = bpf_skb_generic_pop(skb, off, len);
1969 if (likely(!ret)) {
1970 skb->mac_header += len;
1971 skb->network_header += len;
1972 if (trans_same)
1973 skb->transport_header = skb->network_header;
1974 }
1975
1976 return ret;
1977}
1978
1979static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
1980{
1981 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
1982 u32 off = skb->network_header - skb->mac_header;
1983 int ret;
1984
1985 ret = skb_cow(skb, len_diff);
1986 if (unlikely(ret < 0))
1987 return ret;
1988
1989 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
1990 if (unlikely(ret < 0))
1991 return ret;
1992
1993 if (skb_is_gso(skb)) {
1994 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV4 needs to
1995 * be changed into SKB_GSO_TCPV6.
1996 */
1997 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
1998 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV4;
1999 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
2000 }
2001
2002 /* Due to IPv6 header, MSS needs to be downgraded. */
2003 skb_shinfo(skb)->gso_size -= len_diff;
2004 /* Header must be checked, and gso_segs recomputed. */
2005 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2006 skb_shinfo(skb)->gso_segs = 0;
2007 }
2008
2009 skb->protocol = htons(ETH_P_IPV6);
2010 skb_clear_hash(skb);
2011
2012 return 0;
2013}
2014
2015static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2016{
2017 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2018 u32 off = skb->network_header - skb->mac_header;
2019 int ret;
2020
2021 ret = skb_unclone(skb, GFP_ATOMIC);
2022 if (unlikely(ret < 0))
2023 return ret;
2024
2025 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2026 if (unlikely(ret < 0))
2027 return ret;
2028
2029 if (skb_is_gso(skb)) {
2030 /* SKB_GSO_UDP stays as is. SKB_GSO_TCPV6 needs to
2031 * be changed into SKB_GSO_TCPV4.
2032 */
2033 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
2034 skb_shinfo(skb)->gso_type &= ~SKB_GSO_TCPV6;
2035 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
2036 }
2037
2038 /* Due to IPv4 header, MSS can be upgraded. */
2039 skb_shinfo(skb)->gso_size += len_diff;
2040 /* Header must be checked, and gso_segs recomputed. */
2041 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2042 skb_shinfo(skb)->gso_segs = 0;
2043 }
2044
2045 skb->protocol = htons(ETH_P_IP);
2046 skb_clear_hash(skb);
2047
2048 return 0;
2049}
2050
2051static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2052{
2053 __be16 from_proto = skb->protocol;
2054
2055 if (from_proto == htons(ETH_P_IP) &&
2056 to_proto == htons(ETH_P_IPV6))
2057 return bpf_skb_proto_4_to_6(skb);
2058
2059 if (from_proto == htons(ETH_P_IPV6) &&
2060 to_proto == htons(ETH_P_IP))
2061 return bpf_skb_proto_6_to_4(skb);
2062
2063 return -ENOTSUPP;
2064}
2065
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002066BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2067 u64, flags)
Daniel Borkmann65781712016-06-28 12:18:27 +02002068{
Daniel Borkmann65781712016-06-28 12:18:27 +02002069 int ret;
2070
2071 if (unlikely(flags))
2072 return -EINVAL;
2073
2074 /* General idea is that this helper does the basic groundwork
2075 * needed for changing the protocol, and eBPF program fills the
2076 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2077 * and other helpers, rather than passing a raw buffer here.
2078 *
2079 * The rationale is to keep this minimal and without a need to
2080 * deal with raw packet data. F.e. even if we would pass buffers
2081 * here, the program still needs to call the bpf_lX_csum_replace()
2082 * helpers anyway. Plus, this way we keep also separation of
2083 * concerns, since f.e. bpf_skb_store_bytes() should only take
2084 * care of stores.
2085 *
2086 * Currently, additional options and extension header space are
2087 * not supported, but flags register is reserved so we can adapt
2088 * that. For offloads, we mark packet as dodgy, so that headers
2089 * need to be verified first.
2090 */
2091 ret = bpf_skb_proto_xlat(skb, proto);
2092 bpf_compute_data_end(skb);
2093 return ret;
2094}
2095
2096static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2097 .func = bpf_skb_change_proto,
2098 .gpl_only = false,
2099 .ret_type = RET_INTEGER,
2100 .arg1_type = ARG_PTR_TO_CTX,
2101 .arg2_type = ARG_ANYTHING,
2102 .arg3_type = ARG_ANYTHING,
2103};
2104
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002105BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002106{
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002107 /* We only allow a restricted subset to be changed for now. */
Daniel Borkmann45c7fff2016-08-18 01:00:38 +02002108 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2109 !skb_pkt_type_ok(pkt_type)))
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002110 return -EINVAL;
2111
2112 skb->pkt_type = pkt_type;
2113 return 0;
2114}
2115
2116static const struct bpf_func_proto bpf_skb_change_type_proto = {
2117 .func = bpf_skb_change_type,
2118 .gpl_only = false,
2119 .ret_type = RET_INTEGER,
2120 .arg1_type = ARG_PTR_TO_CTX,
2121 .arg2_type = ARG_ANYTHING,
2122};
2123
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002124static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2125{
2126 u32 min_len = skb_network_offset(skb);
2127
2128 if (skb_transport_header_was_set(skb))
2129 min_len = skb_transport_offset(skb);
2130 if (skb->ip_summed == CHECKSUM_PARTIAL)
2131 min_len = skb_checksum_start_offset(skb) +
2132 skb->csum_offset + sizeof(__sum16);
2133 return min_len;
2134}
2135
2136static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2137{
Daniel Borkmann6088b582016-09-09 02:45:28 +02002138 return skb->dev->mtu + skb->dev->hard_header_len;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002139}
2140
2141static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2142{
2143 unsigned int old_len = skb->len;
2144 int ret;
2145
2146 ret = __skb_grow_rcsum(skb, new_len);
2147 if (!ret)
2148 memset(skb->data + old_len, 0, new_len - old_len);
2149 return ret;
2150}
2151
2152static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2153{
2154 return __skb_trim_rcsum(skb, new_len);
2155}
2156
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002157BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2158 u64, flags)
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002159{
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002160 u32 max_len = __bpf_skb_max_len(skb);
2161 u32 min_len = __bpf_skb_min_len(skb);
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002162 int ret;
2163
2164 if (unlikely(flags || new_len > max_len || new_len < min_len))
2165 return -EINVAL;
2166 if (skb->encapsulation)
2167 return -ENOTSUPP;
2168
2169 /* The basic idea of this helper is that it's performing the
2170 * needed work to either grow or trim an skb, and eBPF program
2171 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2172 * bpf_lX_csum_replace() and others rather than passing a raw
2173 * buffer here. This one is a slow path helper and intended
2174 * for replies with control messages.
2175 *
2176 * Like in bpf_skb_change_proto(), we want to keep this rather
2177 * minimal and without protocol specifics so that we are able
2178 * to separate concerns as in bpf_skb_store_bytes() should only
2179 * be the one responsible for writing buffers.
2180 *
2181 * It's really expected to be a slow path operation here for
2182 * control message replies, so we're implicitly linearizing,
2183 * uncloning and drop offloads from the skb by this.
2184 */
2185 ret = __bpf_try_make_writable(skb, skb->len);
2186 if (!ret) {
2187 if (new_len > skb->len)
2188 ret = bpf_skb_grow_rcsum(skb, new_len);
2189 else if (new_len < skb->len)
2190 ret = bpf_skb_trim_rcsum(skb, new_len);
2191 if (!ret && skb_is_gso(skb))
2192 skb_gso_reset(skb);
2193 }
2194
2195 bpf_compute_data_end(skb);
2196 return ret;
2197}
2198
2199static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2200 .func = bpf_skb_change_tail,
2201 .gpl_only = false,
2202 .ret_type = RET_INTEGER,
2203 .arg1_type = ARG_PTR_TO_CTX,
2204 .arg2_type = ARG_ANYTHING,
2205 .arg3_type = ARG_ANYTHING,
2206};
2207
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002208bool bpf_helper_changes_skb_data(void *func)
2209{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002210 if (func == bpf_skb_vlan_push ||
2211 func == bpf_skb_vlan_pop ||
2212 func == bpf_skb_store_bytes ||
2213 func == bpf_skb_change_proto ||
2214 func == bpf_skb_change_tail ||
2215 func == bpf_skb_pull_data ||
Daniel Borkmannc1133c62017-05-25 01:05:07 +02002216 func == bpf_clone_redirect ||
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002217 func == bpf_l3_csum_replace ||
2218 func == bpf_l4_csum_replace)
Daniel Borkmann36976492016-02-19 23:05:25 +01002219 return true;
2220
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002221 return false;
2222}
2223
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002224static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002225 unsigned long off, unsigned long len)
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002226{
Daniel Borkmannaa7145c2016-07-22 01:19:42 +02002227 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002228
2229 if (unlikely(!ptr))
2230 return len;
2231 if (ptr != dst_buff)
2232 memcpy(dst_buff, ptr, len);
2233
2234 return 0;
2235}
2236
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002237BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
2238 u64, flags, void *, meta, u64, meta_size)
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002239{
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002240 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002241
2242 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2243 return -EINVAL;
2244 if (unlikely(skb_size > skb->len))
2245 return -EFAULT;
2246
2247 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
2248 bpf_skb_copy);
2249}
2250
2251static const struct bpf_func_proto bpf_skb_event_output_proto = {
2252 .func = bpf_skb_event_output,
2253 .gpl_only = true,
2254 .ret_type = RET_INTEGER,
2255 .arg1_type = ARG_PTR_TO_CTX,
2256 .arg2_type = ARG_CONST_MAP_PTR,
2257 .arg3_type = ARG_ANYTHING,
2258 .arg4_type = ARG_PTR_TO_STACK,
2259 .arg5_type = ARG_CONST_STACK_SIZE,
2260};
2261
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002262static unsigned short bpf_tunnel_key_af(u64 flags)
2263{
2264 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
2265}
2266
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002267BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
2268 u32, size, u64, flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002269{
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002270 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
2271 u8 compat[sizeof(struct bpf_tunnel_key)];
Daniel Borkmann074f5282016-04-13 00:10:52 +02002272 void *to_orig = to;
2273 int err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002274
Daniel Borkmann074f5282016-04-13 00:10:52 +02002275 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
2276 err = -EINVAL;
2277 goto err_clear;
2278 }
2279 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
2280 err = -EPROTO;
2281 goto err_clear;
2282 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002283 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
Daniel Borkmann074f5282016-04-13 00:10:52 +02002284 err = -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002285 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002286 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002287 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002288 goto set_compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002289 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2290 /* Fixup deprecated structure layouts here, so we have
2291 * a common path later on.
2292 */
2293 if (ip_tunnel_info_af(info) != AF_INET)
Daniel Borkmann074f5282016-04-13 00:10:52 +02002294 goto err_clear;
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002295set_compat:
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002296 to = (struct bpf_tunnel_key *)compat;
2297 break;
2298 default:
Daniel Borkmann074f5282016-04-13 00:10:52 +02002299 goto err_clear;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002300 }
2301 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002302
2303 to->tunnel_id = be64_to_cpu(info->key.tun_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002304 to->tunnel_tos = info->key.tos;
2305 to->tunnel_ttl = info->key.ttl;
2306
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002307 if (flags & BPF_F_TUNINFO_IPV6) {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002308 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
2309 sizeof(to->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002310 to->tunnel_label = be32_to_cpu(info->key.label);
2311 } else {
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002312 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002313 }
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002314
2315 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
Daniel Borkmann074f5282016-04-13 00:10:52 +02002316 memcpy(to_orig, to, size);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002317
2318 return 0;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002319err_clear:
2320 memset(to_orig, 0, size);
2321 return err;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002322}
2323
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002324static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002325 .func = bpf_skb_get_tunnel_key,
2326 .gpl_only = false,
2327 .ret_type = RET_INTEGER,
2328 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002329 .arg2_type = ARG_PTR_TO_RAW_STACK,
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002330 .arg3_type = ARG_CONST_STACK_SIZE,
2331 .arg4_type = ARG_ANYTHING,
2332};
2333
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002334BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002335{
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002336 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002337 int err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002338
2339 if (unlikely(!info ||
Daniel Borkmann074f5282016-04-13 00:10:52 +02002340 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
2341 err = -ENOENT;
2342 goto err_clear;
2343 }
2344 if (unlikely(size < info->options_len)) {
2345 err = -ENOMEM;
2346 goto err_clear;
2347 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002348
2349 ip_tunnel_info_opts_get(to, info);
Daniel Borkmann074f5282016-04-13 00:10:52 +02002350 if (size > info->options_len)
2351 memset(to + info->options_len, 0, size - info->options_len);
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002352
2353 return info->options_len;
Daniel Borkmann074f5282016-04-13 00:10:52 +02002354err_clear:
2355 memset(to, 0, size);
2356 return err;
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002357}
2358
2359static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
2360 .func = bpf_skb_get_tunnel_opt,
2361 .gpl_only = false,
2362 .ret_type = RET_INTEGER,
2363 .arg1_type = ARG_PTR_TO_CTX,
Daniel Borkmann074f5282016-04-13 00:10:52 +02002364 .arg2_type = ARG_PTR_TO_RAW_STACK,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002365 .arg3_type = ARG_CONST_STACK_SIZE,
2366};
2367
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002368static struct metadata_dst __percpu *md_dst;
2369
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002370BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
2371 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002372{
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002373 struct metadata_dst *md = this_cpu_ptr(md_dst);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002374 u8 compat[sizeof(struct bpf_tunnel_key)];
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002375 struct ip_tunnel_info *info;
2376
Daniel Borkmann22080872016-03-04 15:15:05 +01002377 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
2378 BPF_F_DONT_FRAGMENT)))
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002379 return -EINVAL;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002380 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
2381 switch (size) {
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002382 case offsetof(struct bpf_tunnel_key, tunnel_label):
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002383 case offsetof(struct bpf_tunnel_key, tunnel_ext):
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002384 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
2385 /* Fixup deprecated structure layouts here, so we have
2386 * a common path later on.
2387 */
2388 memcpy(compat, from, size);
2389 memset(compat + size, 0, sizeof(compat) - size);
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002390 from = (const struct bpf_tunnel_key *) compat;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002391 break;
2392 default:
2393 return -EINVAL;
2394 }
2395 }
Daniel Borkmannc0e760c2016-03-30 00:02:00 +02002396 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
2397 from->tunnel_ext))
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002398 return -EINVAL;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002399
2400 skb_dst_drop(skb);
2401 dst_hold((struct dst_entry *) md);
2402 skb_dst_set(skb, (struct dst_entry *) md);
2403
2404 info = &md->u.tun_info;
2405 info->mode = IP_TUNNEL_INFO_TX;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002406
Daniel Borkmanndb3c6132016-03-04 15:15:07 +01002407 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
Daniel Borkmann22080872016-03-04 15:15:05 +01002408 if (flags & BPF_F_DONT_FRAGMENT)
2409 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
2410
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002411 info->key.tun_id = cpu_to_be64(from->tunnel_id);
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002412 info->key.tos = from->tunnel_tos;
2413 info->key.ttl = from->tunnel_ttl;
2414
2415 if (flags & BPF_F_TUNINFO_IPV6) {
2416 info->mode |= IP_TUNNEL_INFO_IPV6;
2417 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
2418 sizeof(from->remote_ipv6));
Daniel Borkmann4018ab12016-03-09 03:00:05 +01002419 info->key.label = cpu_to_be32(from->tunnel_label) &
2420 IPV6_FLOWLABEL_MASK;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002421 } else {
2422 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
Daniel Borkmann2da897e2016-02-23 02:05:26 +01002423 if (flags & BPF_F_ZERO_CSUM_TX)
2424 info->key.tun_flags &= ~TUNNEL_CSUM;
Daniel Borkmannc6c33452016-01-11 01:16:39 +01002425 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002426
2427 return 0;
2428}
2429
Daniel Borkmann577c50a2016-03-04 15:15:04 +01002430static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002431 .func = bpf_skb_set_tunnel_key,
2432 .gpl_only = false,
2433 .ret_type = RET_INTEGER,
2434 .arg1_type = ARG_PTR_TO_CTX,
2435 .arg2_type = ARG_PTR_TO_STACK,
2436 .arg3_type = ARG_CONST_STACK_SIZE,
2437 .arg4_type = ARG_ANYTHING,
2438};
2439
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002440BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
2441 const u8 *, from, u32, size)
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002442{
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002443 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2444 const struct metadata_dst *md = this_cpu_ptr(md_dst);
2445
2446 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
2447 return -EINVAL;
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002448 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002449 return -ENOMEM;
2450
2451 ip_tunnel_info_opts_set(info, from, size);
2452
2453 return 0;
2454}
2455
2456static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
2457 .func = bpf_skb_set_tunnel_opt,
2458 .gpl_only = false,
2459 .ret_type = RET_INTEGER,
2460 .arg1_type = ARG_PTR_TO_CTX,
2461 .arg2_type = ARG_PTR_TO_STACK,
2462 .arg3_type = ARG_CONST_STACK_SIZE,
2463};
2464
2465static const struct bpf_func_proto *
2466bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002467{
2468 if (!md_dst) {
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002469 /* Race is not possible, since it's called from verifier
2470 * that is holding verifier mutex.
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002471 */
Daniel Borkmannfca5fdf2016-03-16 01:42:51 +01002472 md_dst = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002473 GFP_KERNEL);
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002474 if (!md_dst)
2475 return NULL;
2476 }
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002477
2478 switch (which) {
2479 case BPF_FUNC_skb_set_tunnel_key:
2480 return &bpf_skb_set_tunnel_key_proto;
2481 case BPF_FUNC_skb_set_tunnel_opt:
2482 return &bpf_skb_set_tunnel_opt_proto;
2483 default:
2484 return NULL;
2485 }
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002486}
2487
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002488BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
2489 u32, idx)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002490{
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002491 struct bpf_array *array = container_of(map, struct bpf_array, map);
2492 struct cgroup *cgrp;
2493 struct sock *sk;
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002494
Daniel Borkmann2d48c5f2016-09-23 01:28:35 +02002495 sk = skb_to_full_sk(skb);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002496 if (!sk || !sk_fullsock(sk))
2497 return -ENOENT;
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002498 if (unlikely(idx >= array->map.max_entries))
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002499 return -E2BIG;
2500
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002501 cgrp = READ_ONCE(array->ptrs[idx]);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002502 if (unlikely(!cgrp))
2503 return -EAGAIN;
2504
Daniel Borkmann54fd9c22016-08-18 01:00:41 +02002505 return sk_under_cgroup_hierarchy(sk, cgrp);
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002506}
2507
Daniel Borkmann747ea552016-08-12 22:17:17 +02002508static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
2509 .func = bpf_skb_under_cgroup,
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002510 .gpl_only = false,
2511 .ret_type = RET_INTEGER,
2512 .arg1_type = ARG_PTR_TO_CTX,
2513 .arg2_type = ARG_CONST_MAP_PTR,
2514 .arg3_type = ARG_ANYTHING,
2515};
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002516
Daniel Borkmann4de16962016-08-18 01:00:40 +02002517static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
2518 unsigned long off, unsigned long len)
2519{
2520 memcpy(dst_buff, src_buff + off, len);
2521 return 0;
2522}
2523
Daniel Borkmannf3694e02016-09-09 02:45:31 +02002524BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
2525 u64, flags, void *, meta, u64, meta_size)
Daniel Borkmann4de16962016-08-18 01:00:40 +02002526{
Daniel Borkmann4de16962016-08-18 01:00:40 +02002527 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
Daniel Borkmann4de16962016-08-18 01:00:40 +02002528
2529 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
2530 return -EINVAL;
2531 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
2532 return -EFAULT;
2533
2534 return bpf_event_output(map, flags, meta, meta_size, xdp, xdp_size,
2535 bpf_xdp_copy);
2536}
2537
2538static const struct bpf_func_proto bpf_xdp_event_output_proto = {
2539 .func = bpf_xdp_event_output,
2540 .gpl_only = true,
2541 .ret_type = RET_INTEGER,
2542 .arg1_type = ARG_PTR_TO_CTX,
2543 .arg2_type = ARG_CONST_MAP_PTR,
2544 .arg3_type = ARG_ANYTHING,
2545 .arg4_type = ARG_PTR_TO_STACK,
2546 .arg5_type = ARG_CONST_STACK_SIZE,
2547};
2548
Chenbo Feng09313662017-03-22 17:27:34 -07002549BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
2550{
2551 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
2552}
2553
2554static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
2555 .func = bpf_get_socket_cookie,
2556 .gpl_only = false,
2557 .ret_type = RET_INTEGER,
2558 .arg1_type = ARG_PTR_TO_CTX,
2559};
2560
Chenbo Fenga29c81e2017-03-22 17:27:35 -07002561BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
2562{
2563 struct sock *sk = sk_to_full_sk(skb->sk);
2564 kuid_t kuid;
2565
2566 if (!sk || !sk_fullsock(sk))
2567 return overflowuid;
2568 kuid = sock_net_uid(sock_net(sk), sk);
2569 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
2570}
2571
2572static const struct bpf_func_proto bpf_get_socket_uid_proto = {
2573 .func = bpf_get_socket_uid,
2574 .gpl_only = false,
2575 .ret_type = RET_INTEGER,
2576 .arg1_type = ARG_PTR_TO_CTX,
2577};
2578
Daniel Borkmannd4052c42015-03-01 12:31:45 +01002579static const struct bpf_func_proto *
2580sk_filter_func_proto(enum bpf_func_id func_id)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002581{
2582 switch (func_id) {
2583 case BPF_FUNC_map_lookup_elem:
2584 return &bpf_map_lookup_elem_proto;
2585 case BPF_FUNC_map_update_elem:
2586 return &bpf_map_update_elem_proto;
2587 case BPF_FUNC_map_delete_elem:
2588 return &bpf_map_delete_elem_proto;
Daniel Borkmann03e69b52015-03-14 02:27:16 +01002589 case BPF_FUNC_get_prandom_u32:
2590 return &bpf_get_prandom_u32_proto;
Daniel Borkmannc04167c2015-03-14 02:27:17 +01002591 case BPF_FUNC_get_smp_processor_id:
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002592 return &bpf_get_raw_smp_processor_id_proto;
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -07002593 case BPF_FUNC_tail_call:
2594 return &bpf_tail_call_proto;
Daniel Borkmann17ca8cb2015-05-29 23:23:06 +02002595 case BPF_FUNC_ktime_get_ns:
2596 return &bpf_ktime_get_ns_proto;
Alexei Starovoitov0756ea32015-06-12 19:39:13 -07002597 case BPF_FUNC_trace_printk:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002598 if (capable(CAP_SYS_ADMIN))
2599 return bpf_get_trace_printk_proto();
Chenbo Feng09313662017-03-22 17:27:34 -07002600 case BPF_FUNC_get_socket_cookie:
2601 return &bpf_get_socket_cookie_proto;
Chenbo Fenga29c81e2017-03-22 17:27:35 -07002602 case BPF_FUNC_get_socket_uid:
2603 return &bpf_get_socket_uid_proto;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002604 default:
2605 return NULL;
2606 }
2607}
2608
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002609static const struct bpf_func_proto *
2610tc_cls_act_func_proto(enum bpf_func_id func_id)
2611{
2612 switch (func_id) {
2613 case BPF_FUNC_skb_store_bytes:
2614 return &bpf_skb_store_bytes_proto;
Daniel Borkmann05c74e52015-12-17 23:51:53 +01002615 case BPF_FUNC_skb_load_bytes:
2616 return &bpf_skb_load_bytes_proto;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002617 case BPF_FUNC_skb_pull_data:
2618 return &bpf_skb_pull_data_proto;
Daniel Borkmann7d672342016-02-19 23:05:23 +01002619 case BPF_FUNC_csum_diff:
2620 return &bpf_csum_diff_proto;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002621 case BPF_FUNC_csum_update:
2622 return &bpf_csum_update_proto;
Alexei Starovoitov91bc48222015-04-01 17:12:13 -07002623 case BPF_FUNC_l3_csum_replace:
2624 return &bpf_l3_csum_replace_proto;
2625 case BPF_FUNC_l4_csum_replace:
2626 return &bpf_l4_csum_replace_proto;
Alexei Starovoitov3896d652015-06-02 16:03:14 -07002627 case BPF_FUNC_clone_redirect:
2628 return &bpf_clone_redirect_proto;
Daniel Borkmann8d20aab2015-07-15 14:21:42 +02002629 case BPF_FUNC_get_cgroup_classid:
2630 return &bpf_get_cgroup_classid_proto;
Alexei Starovoitov4e10df92015-07-20 20:34:18 -07002631 case BPF_FUNC_skb_vlan_push:
2632 return &bpf_skb_vlan_push_proto;
2633 case BPF_FUNC_skb_vlan_pop:
2634 return &bpf_skb_vlan_pop_proto;
Daniel Borkmann65781712016-06-28 12:18:27 +02002635 case BPF_FUNC_skb_change_proto:
2636 return &bpf_skb_change_proto_proto;
Daniel Borkmannd2485c42016-06-28 12:18:28 +02002637 case BPF_FUNC_skb_change_type:
2638 return &bpf_skb_change_type_proto;
Daniel Borkmann5293efe2016-08-18 01:00:39 +02002639 case BPF_FUNC_skb_change_tail:
2640 return &bpf_skb_change_tail_proto;
Alexei Starovoitovd3aa45c2015-07-30 15:36:57 -07002641 case BPF_FUNC_skb_get_tunnel_key:
2642 return &bpf_skb_get_tunnel_key_proto;
2643 case BPF_FUNC_skb_set_tunnel_key:
Daniel Borkmann14ca0752016-03-04 15:15:06 +01002644 return bpf_get_skb_set_tunnel_proto(func_id);
2645 case BPF_FUNC_skb_get_tunnel_opt:
2646 return &bpf_skb_get_tunnel_opt_proto;
2647 case BPF_FUNC_skb_set_tunnel_opt:
2648 return bpf_get_skb_set_tunnel_proto(func_id);
Alexei Starovoitov27b29f62015-09-15 23:05:43 -07002649 case BPF_FUNC_redirect:
2650 return &bpf_redirect_proto;
Daniel Borkmannc46646d2015-09-30 01:41:51 +02002651 case BPF_FUNC_get_route_realm:
2652 return &bpf_get_route_realm_proto;
Daniel Borkmann13c5c242016-07-03 01:28:47 +02002653 case BPF_FUNC_get_hash_recalc:
2654 return &bpf_get_hash_recalc_proto;
Daniel Borkmann7a4b28c2016-09-23 01:28:37 +02002655 case BPF_FUNC_set_hash_invalid:
2656 return &bpf_set_hash_invalid_proto;
Daniel Borkmannbd570ff2016-04-18 21:01:24 +02002657 case BPF_FUNC_perf_event_output:
Daniel Borkmann555c8a82016-07-14 18:08:05 +02002658 return &bpf_skb_event_output_proto;
Daniel Borkmann80b48c42016-06-28 12:18:26 +02002659 case BPF_FUNC_get_smp_processor_id:
2660 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann747ea552016-08-12 22:17:17 +02002661 case BPF_FUNC_skb_under_cgroup:
2662 return &bpf_skb_under_cgroup_proto;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002663 default:
2664 return sk_filter_func_proto(func_id);
2665 }
2666}
2667
Brenden Blanco6a773a12016-07-19 12:16:47 -07002668static const struct bpf_func_proto *
2669xdp_func_proto(enum bpf_func_id func_id)
2670{
Daniel Borkmann4de16962016-08-18 01:00:40 +02002671 switch (func_id) {
2672 case BPF_FUNC_perf_event_output:
2673 return &bpf_xdp_event_output_proto;
Daniel Borkmann669dc4d2016-09-23 01:28:36 +02002674 case BPF_FUNC_get_smp_processor_id:
2675 return &bpf_get_smp_processor_id_proto;
Daniel Borkmann4de16962016-08-18 01:00:40 +02002676 default:
2677 return sk_filter_func_proto(func_id);
2678 }
Brenden Blanco6a773a12016-07-19 12:16:47 -07002679}
2680
Daniel Mack760b55c2016-11-23 16:52:25 +01002681static const struct bpf_func_proto *
2682cg_skb_func_proto(enum bpf_func_id func_id)
2683{
2684 switch (func_id) {
2685 case BPF_FUNC_skb_load_bytes:
2686 return &bpf_skb_load_bytes_proto;
2687 default:
2688 return sk_filter_func_proto(func_id);
2689 }
2690}
2691
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002692static bool __is_valid_access(int off, int size, enum bpf_access_type type)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002693{
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002694 if (off < 0 || off >= sizeof(struct __sk_buff))
2695 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002696 /* The verifier guarantees that size > 0. */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002697 if (off % size != 0)
2698 return false;
Daniel Borkmann4936e352016-05-13 19:08:26 +02002699 if (size != sizeof(__u32))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002700 return false;
2701
2702 return true;
2703}
2704
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002705static bool sk_filter_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002706 enum bpf_access_type type,
2707 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002708{
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002709 switch (off) {
2710 case offsetof(struct __sk_buff, tc_classid):
2711 case offsetof(struct __sk_buff, data):
2712 case offsetof(struct __sk_buff, data_end):
Daniel Borkmann045efa82015-09-15 23:05:42 -07002713 return false;
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002714 }
Daniel Borkmann045efa82015-09-15 23:05:42 -07002715
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002716 if (type == BPF_WRITE) {
2717 switch (off) {
2718 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann4936e352016-05-13 19:08:26 +02002719 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002720 break;
2721 default:
2722 return false;
2723 }
2724 }
2725
2726 return __is_valid_access(off, size, type);
2727}
2728
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002729static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
2730 const struct bpf_prog *prog)
2731{
2732 struct bpf_insn *insn = insn_buf;
2733
2734 if (!direct_write)
2735 return 0;
2736
2737 /* if (!skb->cloned)
2738 * goto start;
2739 *
2740 * (Fast-path, otherwise approximation that we might be
2741 * a clone, do the rest in helper.)
2742 */
2743 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
2744 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
2745 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
2746
2747 /* ret = bpf_skb_pull_data(skb, 0); */
2748 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
2749 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
2750 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
2751 BPF_FUNC_skb_pull_data);
2752 /* if (!ret)
2753 * goto restore;
2754 * return TC_ACT_SHOT;
2755 */
2756 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
2757 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, TC_ACT_SHOT);
2758 *insn++ = BPF_EXIT_INSN();
2759
2760 /* restore: */
2761 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
2762 /* start: */
2763 *insn++ = prog->insnsi[0];
2764
2765 return insn - insn_buf;
2766}
2767
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002768static bool tc_cls_act_is_valid_access(int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002769 enum bpf_access_type type,
2770 enum bpf_reg_type *reg_type)
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002771{
2772 if (type == BPF_WRITE) {
2773 switch (off) {
2774 case offsetof(struct __sk_buff, mark):
2775 case offsetof(struct __sk_buff, tc_index):
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002776 case offsetof(struct __sk_buff, priority):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002777 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002778 offsetof(struct __sk_buff, cb[4]):
2779 case offsetof(struct __sk_buff, tc_classid):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002780 break;
2781 default:
2782 return false;
2783 }
2784 }
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002785
2786 switch (off) {
2787 case offsetof(struct __sk_buff, data):
2788 *reg_type = PTR_TO_PACKET;
2789 break;
2790 case offsetof(struct __sk_buff, data_end):
2791 *reg_type = PTR_TO_PACKET_END;
2792 break;
2793 }
2794
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002795 return __is_valid_access(off, size, type);
2796}
2797
Brenden Blanco6a773a12016-07-19 12:16:47 -07002798static bool __is_valid_xdp_access(int off, int size,
2799 enum bpf_access_type type)
2800{
2801 if (off < 0 || off >= sizeof(struct xdp_md))
2802 return false;
2803 if (off % size != 0)
2804 return false;
Daniel Borkmann6088b582016-09-09 02:45:28 +02002805 if (size != sizeof(__u32))
Brenden Blanco6a773a12016-07-19 12:16:47 -07002806 return false;
2807
2808 return true;
2809}
2810
2811static bool xdp_is_valid_access(int off, int size,
2812 enum bpf_access_type type,
2813 enum bpf_reg_type *reg_type)
2814{
2815 if (type == BPF_WRITE)
2816 return false;
2817
2818 switch (off) {
2819 case offsetof(struct xdp_md, data):
2820 *reg_type = PTR_TO_PACKET;
2821 break;
2822 case offsetof(struct xdp_md, data_end):
2823 *reg_type = PTR_TO_PACKET_END;
2824 break;
2825 }
2826
2827 return __is_valid_xdp_access(off, size, type);
2828}
2829
2830void bpf_warn_invalid_xdp_action(u32 act)
2831{
2832 WARN_ONCE(1, "Illegal XDP return value %u, expect packet loss\n", act);
2833}
2834EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
2835
Daniel Borkmann374fb542016-09-09 02:45:30 +02002836static u32 sk_filter_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2837 int src_reg, int ctx_off,
2838 struct bpf_insn *insn_buf,
2839 struct bpf_prog *prog)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002840{
2841 struct bpf_insn *insn = insn_buf;
2842
2843 switch (ctx_off) {
2844 case offsetof(struct __sk_buff, len):
2845 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
2846
2847 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2848 offsetof(struct sk_buff, len));
2849 break;
2850
Daniel Borkmann0b8c7072015-03-19 19:38:27 +01002851 case offsetof(struct __sk_buff, protocol):
2852 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
2853
2854 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2855 offsetof(struct sk_buff, protocol));
2856 break;
2857
Michal Sekletar27cd5452015-03-24 14:48:41 +01002858 case offsetof(struct __sk_buff, vlan_proto):
2859 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
2860
2861 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2862 offsetof(struct sk_buff, vlan_proto));
2863 break;
2864
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002865 case offsetof(struct __sk_buff, priority):
2866 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
2867
Daniel Borkmann754f1e62015-09-30 01:41:52 +02002868 if (type == BPF_WRITE)
2869 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2870 offsetof(struct sk_buff, priority));
2871 else
2872 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2873 offsetof(struct sk_buff, priority));
Daniel Borkmannbcad5712015-04-03 20:52:24 +02002874 break;
2875
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002876 case offsetof(struct __sk_buff, ingress_ifindex):
2877 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, skb_iif) != 4);
2878
2879 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2880 offsetof(struct sk_buff, skb_iif));
2881 break;
2882
2883 case offsetof(struct __sk_buff, ifindex):
2884 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2885
Daniel Borkmannf035a512016-09-09 02:45:29 +02002886 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
Alexei Starovoitov37e82c22015-05-27 15:30:39 -07002887 dst_reg, src_reg,
2888 offsetof(struct sk_buff, dev));
2889 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
2890 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
2891 offsetof(struct net_device, ifindex));
2892 break;
2893
Daniel Borkmannba7591d2015-08-01 00:46:29 +02002894 case offsetof(struct __sk_buff, hash):
2895 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
2896
2897 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2898 offsetof(struct sk_buff, hash));
2899 break;
2900
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002901 case offsetof(struct __sk_buff, mark):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002902 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
2903
2904 if (type == BPF_WRITE)
2905 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg,
2906 offsetof(struct sk_buff, mark));
2907 else
2908 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
2909 offsetof(struct sk_buff, mark));
2910 break;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002911
2912 case offsetof(struct __sk_buff, pkt_type):
2913 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
2914
2915 case offsetof(struct __sk_buff, queue_mapping):
2916 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002917
Alexei Starovoitovc2497392015-03-16 18:06:02 -07002918 case offsetof(struct __sk_buff, vlan_present):
2919 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
2920 dst_reg, src_reg, insn);
2921
2922 case offsetof(struct __sk_buff, vlan_tci):
2923 return convert_skb_access(SKF_AD_VLAN_TAG,
2924 dst_reg, src_reg, insn);
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002925
2926 case offsetof(struct __sk_buff, cb[0]) ...
Daniel Borkmann6088b582016-09-09 02:45:28 +02002927 offsetof(struct __sk_buff, cb[4]):
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002928 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
2929
Alexei Starovoitovff936a02015-10-07 10:55:41 -07002930 prog->cb_access = 1;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002931 ctx_off -= offsetof(struct __sk_buff, cb[0]);
2932 ctx_off += offsetof(struct sk_buff, cb);
2933 ctx_off += offsetof(struct qdisc_skb_cb, data);
2934 if (type == BPF_WRITE)
2935 *insn++ = BPF_STX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2936 else
2937 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg, ctx_off);
2938 break;
2939
Daniel Borkmann045efa82015-09-15 23:05:42 -07002940 case offsetof(struct __sk_buff, tc_classid):
2941 ctx_off -= offsetof(struct __sk_buff, tc_classid);
2942 ctx_off += offsetof(struct sk_buff, cb);
2943 ctx_off += offsetof(struct qdisc_skb_cb, tc_classid);
Daniel Borkmann09c37a22016-03-16 01:42:49 +01002944 if (type == BPF_WRITE)
2945 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
2946 else
2947 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg, ctx_off);
Daniel Borkmann045efa82015-09-15 23:05:42 -07002948 break;
2949
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002950 case offsetof(struct __sk_buff, data):
Daniel Borkmannf035a512016-09-09 02:45:29 +02002951 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002952 dst_reg, src_reg,
2953 offsetof(struct sk_buff, data));
2954 break;
2955
2956 case offsetof(struct __sk_buff, data_end):
2957 ctx_off -= offsetof(struct __sk_buff, data_end);
2958 ctx_off += offsetof(struct sk_buff, cb);
2959 ctx_off += offsetof(struct bpf_skb_data_end, data_end);
Daniel Borkmannf035a512016-09-09 02:45:29 +02002960 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), dst_reg, src_reg,
2961 ctx_off);
Alexei Starovoitovdb58ba42016-05-05 19:49:12 -07002962 break;
2963
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07002964 case offsetof(struct __sk_buff, tc_index):
2965#ifdef CONFIG_NET_SCHED
2966 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tc_index) != 2);
2967
2968 if (type == BPF_WRITE)
2969 *insn++ = BPF_STX_MEM(BPF_H, dst_reg, src_reg,
2970 offsetof(struct sk_buff, tc_index));
2971 else
2972 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
2973 offsetof(struct sk_buff, tc_index));
2974 break;
2975#else
2976 if (type == BPF_WRITE)
2977 *insn++ = BPF_MOV64_REG(dst_reg, dst_reg);
2978 else
2979 *insn++ = BPF_MOV64_IMM(dst_reg, 0);
2980 break;
2981#endif
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07002982 }
2983
2984 return insn - insn_buf;
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08002985}
2986
Daniel Borkmann374fb542016-09-09 02:45:30 +02002987static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type, int dst_reg,
2988 int src_reg, int ctx_off,
2989 struct bpf_insn *insn_buf,
2990 struct bpf_prog *prog)
2991{
2992 struct bpf_insn *insn = insn_buf;
2993
2994 switch (ctx_off) {
2995 case offsetof(struct __sk_buff, ifindex):
2996 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
2997
2998 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
2999 dst_reg, src_reg,
3000 offsetof(struct sk_buff, dev));
3001 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, dst_reg,
3002 offsetof(struct net_device, ifindex));
3003 break;
3004 default:
3005 return sk_filter_convert_ctx_access(type, dst_reg, src_reg,
3006 ctx_off, insn_buf, prog);
3007 }
3008
3009 return insn - insn_buf;
3010}
3011
Brenden Blanco6a773a12016-07-19 12:16:47 -07003012static u32 xdp_convert_ctx_access(enum bpf_access_type type, int dst_reg,
3013 int src_reg, int ctx_off,
3014 struct bpf_insn *insn_buf,
3015 struct bpf_prog *prog)
3016{
3017 struct bpf_insn *insn = insn_buf;
3018
3019 switch (ctx_off) {
3020 case offsetof(struct xdp_md, data):
Daniel Borkmannf035a512016-09-09 02:45:29 +02003021 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
Brenden Blanco6a773a12016-07-19 12:16:47 -07003022 dst_reg, src_reg,
3023 offsetof(struct xdp_buff, data));
3024 break;
3025 case offsetof(struct xdp_md, data_end):
Daniel Borkmannf035a512016-09-09 02:45:29 +02003026 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
Brenden Blanco6a773a12016-07-19 12:16:47 -07003027 dst_reg, src_reg,
3028 offsetof(struct xdp_buff, data_end));
3029 break;
3030 }
3031
3032 return insn - insn_buf;
3033}
3034
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003035static const struct bpf_verifier_ops sk_filter_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003036 .get_func_proto = sk_filter_func_proto,
3037 .is_valid_access = sk_filter_is_valid_access,
Daniel Borkmann374fb542016-09-09 02:45:30 +02003038 .convert_ctx_access = sk_filter_convert_ctx_access,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003039};
3040
Alexei Starovoitov608cd712015-03-26 19:53:57 -07003041static const struct bpf_verifier_ops tc_cls_act_ops = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003042 .get_func_proto = tc_cls_act_func_proto,
3043 .is_valid_access = tc_cls_act_is_valid_access,
Daniel Borkmann374fb542016-09-09 02:45:30 +02003044 .convert_ctx_access = tc_cls_act_convert_ctx_access,
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003045 .gen_prologue = tc_cls_act_prologue,
Alexei Starovoitov608cd712015-03-26 19:53:57 -07003046};
3047
Brenden Blanco6a773a12016-07-19 12:16:47 -07003048static const struct bpf_verifier_ops xdp_ops = {
3049 .get_func_proto = xdp_func_proto,
3050 .is_valid_access = xdp_is_valid_access,
3051 .convert_ctx_access = xdp_convert_ctx_access,
3052};
3053
Daniel Mack760b55c2016-11-23 16:52:25 +01003054static const struct bpf_verifier_ops cg_skb_ops = {
3055 .get_func_proto = cg_skb_func_proto,
3056 .is_valid_access = sk_filter_is_valid_access,
3057 .convert_ctx_access = sk_filter_convert_ctx_access,
3058};
3059
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003060static struct bpf_prog_type_list sk_filter_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003061 .ops = &sk_filter_ops,
3062 .type = BPF_PROG_TYPE_SOCKET_FILTER,
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003063};
3064
Daniel Borkmann96be4322015-03-01 12:31:46 +01003065static struct bpf_prog_type_list sched_cls_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003066 .ops = &tc_cls_act_ops,
3067 .type = BPF_PROG_TYPE_SCHED_CLS,
Daniel Borkmann96be4322015-03-01 12:31:46 +01003068};
3069
Daniel Borkmann94caee82015-03-20 15:11:11 +01003070static struct bpf_prog_type_list sched_act_type __read_mostly = {
Daniel Borkmann4936e352016-05-13 19:08:26 +02003071 .ops = &tc_cls_act_ops,
3072 .type = BPF_PROG_TYPE_SCHED_ACT,
Daniel Borkmann94caee82015-03-20 15:11:11 +01003073};
3074
Brenden Blanco6a773a12016-07-19 12:16:47 -07003075static struct bpf_prog_type_list xdp_type __read_mostly = {
3076 .ops = &xdp_ops,
3077 .type = BPF_PROG_TYPE_XDP,
3078};
3079
Daniel Mack760b55c2016-11-23 16:52:25 +01003080static struct bpf_prog_type_list cg_skb_type __read_mostly = {
3081 .ops = &cg_skb_ops,
3082 .type = BPF_PROG_TYPE_CGROUP_SKB,
3083};
3084
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003085static int __init register_sk_filter_ops(void)
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003086{
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003087 bpf_register_prog_type(&sk_filter_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01003088 bpf_register_prog_type(&sched_cls_type);
Daniel Borkmann94caee82015-03-20 15:11:11 +01003089 bpf_register_prog_type(&sched_act_type);
Brenden Blanco6a773a12016-07-19 12:16:47 -07003090 bpf_register_prog_type(&xdp_type);
Daniel Mack760b55c2016-11-23 16:52:25 +01003091 bpf_register_prog_type(&cg_skb_type);
Daniel Borkmann96be4322015-03-01 12:31:46 +01003092
Alexei Starovoitov89aa0752014-12-01 15:06:35 -08003093 return 0;
3094}
Daniel Borkmannd4052c42015-03-01 12:31:45 +01003095late_initcall(register_sk_filter_ops);
3096
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003097int sk_detach_filter(struct sock *sk)
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003098{
3099 int ret = -ENOENT;
3100 struct sk_filter *filter;
3101
Vincent Bernatd59577b2013-01-16 22:55:49 +01003102 if (sock_flag(sk, SOCK_FILTER_LOCKED))
3103 return -EPERM;
3104
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003105 filter = rcu_dereference_protected(sk->sk_filter,
3106 lockdep_sock_is_held(sk));
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003107 if (filter) {
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003108 RCU_INIT_POINTER(sk->sk_filter, NULL);
Eric Dumazet46bcf142010-12-06 09:29:43 -08003109 sk_filter_uncharge(sk, filter);
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003110 ret = 0;
3111 }
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003112
Pavel Emelyanov55b33322007-10-17 21:21:26 -07003113 return ret;
3114}
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003115EXPORT_SYMBOL_GPL(sk_detach_filter);
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003116
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003117int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
3118 unsigned int len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003119{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003120 struct sock_fprog_kern *fprog;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003121 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003122 int ret = 0;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003123
3124 lock_sock(sk);
3125 filter = rcu_dereference_protected(sk->sk_filter,
Hannes Frederic Sowa8ced4252016-04-05 17:10:16 +02003126 lockdep_sock_is_held(sk));
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003127 if (!filter)
3128 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003129
3130 /* We're copying the filter that has been originally attached,
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003131 * so no conversion/decode needed anymore. eBPF programs that
3132 * have no original program cannot be dumped through this.
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003133 */
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003134 ret = -EACCES;
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07003135 fprog = filter->prog->orig_prog;
Daniel Borkmann93d08b62015-10-02 12:06:03 +02003136 if (!fprog)
3137 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003138
3139 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003140 if (!len)
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003141 /* User space only enquires number of filter blocks. */
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003142 goto out;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003143
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003144 ret = -EINVAL;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003145 if (len < fprog->len)
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003146 goto out;
3147
3148 ret = -EFAULT;
Alexei Starovoitov009937e2014-07-30 20:34:13 -07003149 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003150 goto out;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003151
Daniel Borkmanna3ea2692014-03-28 18:58:19 +01003152 /* Instead of bytes, the API requests to return the number
3153 * of filter blocks.
3154 */
3155 ret = fprog->len;
Pavel Emelyanova8fc9272012-11-01 02:01:48 +00003156out:
3157 release_sock(sk);
3158 return ret;
3159}