xref: /openbmc/linux/net/sched/cls_bpf.c (revision 867a0e05)
1 /*
2  * Berkeley Packet Filter based traffic classifier
3  *
4  * Might be used to classify traffic through flexible, user-defined and
5  * possibly JIT-ed BPF filters for traffic control as an alternative to
6  * ematches.
7  *
8  * (C) 2013 Daniel Borkmann <dborkman@redhat.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14 
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/skbuff.h>
18 #include <linux/filter.h>
19 #include <net/rtnetlink.h>
20 #include <net/pkt_cls.h>
21 #include <net/sock.h>
22 
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
25 MODULE_DESCRIPTION("TC BPF based classifier");
26 
27 struct cls_bpf_head {
28 	struct list_head plist;
29 	u32 hgen;
30 	struct rcu_head rcu;
31 };
32 
33 struct cls_bpf_prog {
34 	struct bpf_prog *filter;
35 	struct sock_filter *bpf_ops;
36 	struct tcf_exts exts;
37 	struct tcf_result res;
38 	struct list_head link;
39 	u32 handle;
40 	u16 bpf_len;
41 	struct tcf_proto *tp;
42 	struct rcu_head rcu;
43 };
44 
45 static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
46 	[TCA_BPF_CLASSID]	= { .type = NLA_U32 },
47 	[TCA_BPF_OPS_LEN]	= { .type = NLA_U16 },
48 	[TCA_BPF_OPS]		= { .type = NLA_BINARY,
49 				    .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
50 };
51 
52 static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
53 			    struct tcf_result *res)
54 {
55 	struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
56 	struct cls_bpf_prog *prog;
57 	int ret;
58 
59 	list_for_each_entry_rcu(prog, &head->plist, link) {
60 		int filter_res = BPF_PROG_RUN(prog->filter, skb);
61 
62 		if (filter_res == 0)
63 			continue;
64 
65 		*res = prog->res;
66 		if (filter_res != -1)
67 			res->classid = filter_res;
68 
69 		ret = tcf_exts_exec(skb, &prog->exts, res);
70 		if (ret < 0)
71 			continue;
72 
73 		return ret;
74 	}
75 
76 	return -1;
77 }
78 
79 static int cls_bpf_init(struct tcf_proto *tp)
80 {
81 	struct cls_bpf_head *head;
82 
83 	head = kzalloc(sizeof(*head), GFP_KERNEL);
84 	if (head == NULL)
85 		return -ENOBUFS;
86 
87 	INIT_LIST_HEAD_RCU(&head->plist);
88 	rcu_assign_pointer(tp->root, head);
89 
90 	return 0;
91 }
92 
93 static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
94 {
95 	tcf_exts_destroy(&prog->exts);
96 
97 	bpf_prog_destroy(prog->filter);
98 
99 	kfree(prog->bpf_ops);
100 	kfree(prog);
101 }
102 
103 static void __cls_bpf_delete_prog(struct rcu_head *rcu)
104 {
105 	struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu);
106 
107 	cls_bpf_delete_prog(prog->tp, prog);
108 }
109 
110 static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
111 {
112 	struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg;
113 
114 	list_del_rcu(&prog->link);
115 	tcf_unbind_filter(tp, &prog->res);
116 	call_rcu(&prog->rcu, __cls_bpf_delete_prog);
117 	return 0;
118 }
119 
120 static void cls_bpf_destroy(struct tcf_proto *tp)
121 {
122 	struct cls_bpf_head *head = rtnl_dereference(tp->root);
123 	struct cls_bpf_prog *prog, *tmp;
124 
125 	list_for_each_entry_safe(prog, tmp, &head->plist, link) {
126 		list_del_rcu(&prog->link);
127 		tcf_unbind_filter(tp, &prog->res);
128 		call_rcu(&prog->rcu, __cls_bpf_delete_prog);
129 	}
130 
131 	RCU_INIT_POINTER(tp->root, NULL);
132 	kfree_rcu(head, rcu);
133 }
134 
135 static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
136 {
137 	struct cls_bpf_head *head = rtnl_dereference(tp->root);
138 	struct cls_bpf_prog *prog;
139 	unsigned long ret = 0UL;
140 
141 	if (head == NULL)
142 		return 0UL;
143 
144 	list_for_each_entry(prog, &head->plist, link) {
145 		if (prog->handle == handle) {
146 			ret = (unsigned long) prog;
147 			break;
148 		}
149 	}
150 
151 	return ret;
152 }
153 
154 static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
155 				   struct cls_bpf_prog *prog,
156 				   unsigned long base, struct nlattr **tb,
157 				   struct nlattr *est, bool ovr)
158 {
159 	struct sock_filter *bpf_ops;
160 	struct tcf_exts exts;
161 	struct sock_fprog_kern tmp;
162 	struct bpf_prog *fp;
163 	u16 bpf_size, bpf_len;
164 	u32 classid;
165 	int ret;
166 
167 	if (!tb[TCA_BPF_OPS_LEN] || !tb[TCA_BPF_OPS] || !tb[TCA_BPF_CLASSID])
168 		return -EINVAL;
169 
170 	tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
171 	ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
172 	if (ret < 0)
173 		return ret;
174 
175 	classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
176 	bpf_len = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
177 	if (bpf_len > BPF_MAXINSNS || bpf_len == 0) {
178 		ret = -EINVAL;
179 		goto errout;
180 	}
181 
182 	bpf_size = bpf_len * sizeof(*bpf_ops);
183 	bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
184 	if (bpf_ops == NULL) {
185 		ret = -ENOMEM;
186 		goto errout;
187 	}
188 
189 	memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
190 
191 	tmp.len = bpf_len;
192 	tmp.filter = bpf_ops;
193 
194 	ret = bpf_prog_create(&fp, &tmp);
195 	if (ret)
196 		goto errout_free;
197 
198 	prog->bpf_len = bpf_len;
199 	prog->bpf_ops = bpf_ops;
200 	prog->filter = fp;
201 	prog->res.classid = classid;
202 
203 	tcf_bind_filter(tp, &prog->res, base);
204 	tcf_exts_change(tp, &prog->exts, &exts);
205 
206 	return 0;
207 errout_free:
208 	kfree(bpf_ops);
209 errout:
210 	tcf_exts_destroy(&exts);
211 	return ret;
212 }
213 
214 static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
215 				   struct cls_bpf_head *head)
216 {
217 	unsigned int i = 0x80000000;
218 
219 	do {
220 		if (++head->hgen == 0x7FFFFFFF)
221 			head->hgen = 1;
222 	} while (--i > 0 && cls_bpf_get(tp, head->hgen));
223 	if (i == 0)
224 		pr_err("Insufficient number of handles\n");
225 
226 	return i;
227 }
228 
229 static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
230 			  struct tcf_proto *tp, unsigned long base,
231 			  u32 handle, struct nlattr **tca,
232 			  unsigned long *arg, bool ovr)
233 {
234 	struct cls_bpf_head *head = rtnl_dereference(tp->root);
235 	struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg;
236 	struct nlattr *tb[TCA_BPF_MAX + 1];
237 	struct cls_bpf_prog *prog;
238 	int ret;
239 
240 	if (tca[TCA_OPTIONS] == NULL)
241 		return -EINVAL;
242 
243 	ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy);
244 	if (ret < 0)
245 		return ret;
246 
247 	prog = kzalloc(sizeof(*prog), GFP_KERNEL);
248 	if (!prog)
249 		return -ENOBUFS;
250 
251 	tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
252 
253 	if (oldprog) {
254 		if (handle && oldprog->handle != handle) {
255 			ret = -EINVAL;
256 			goto errout;
257 		}
258 	}
259 
260 	if (handle == 0)
261 		prog->handle = cls_bpf_grab_new_handle(tp, head);
262 	else
263 		prog->handle = handle;
264 	if (prog->handle == 0) {
265 		ret = -EINVAL;
266 		goto errout;
267 	}
268 
269 	ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr);
270 	if (ret < 0)
271 		goto errout;
272 
273 	if (oldprog) {
274 		list_replace_rcu(&prog->link, &oldprog->link);
275 		tcf_unbind_filter(tp, &oldprog->res);
276 		call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
277 	} else {
278 		list_add_rcu(&prog->link, &head->plist);
279 	}
280 
281 	*arg = (unsigned long) prog;
282 	return 0;
283 errout:
284 	kfree(prog);
285 
286 	return ret;
287 }
288 
289 static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
290 			struct sk_buff *skb, struct tcmsg *tm)
291 {
292 	struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
293 	struct nlattr *nest, *nla;
294 
295 	if (prog == NULL)
296 		return skb->len;
297 
298 	tm->tcm_handle = prog->handle;
299 
300 	nest = nla_nest_start(skb, TCA_OPTIONS);
301 	if (nest == NULL)
302 		goto nla_put_failure;
303 
304 	if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
305 		goto nla_put_failure;
306 	if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_len))
307 		goto nla_put_failure;
308 
309 	nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_len *
310 			  sizeof(struct sock_filter));
311 	if (nla == NULL)
312 		goto nla_put_failure;
313 
314 	memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
315 
316 	if (tcf_exts_dump(skb, &prog->exts) < 0)
317 		goto nla_put_failure;
318 
319 	nla_nest_end(skb, nest);
320 
321 	if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
322 		goto nla_put_failure;
323 
324 	return skb->len;
325 
326 nla_put_failure:
327 	nla_nest_cancel(skb, nest);
328 	return -1;
329 }
330 
331 static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
332 {
333 	struct cls_bpf_head *head = rtnl_dereference(tp->root);
334 	struct cls_bpf_prog *prog;
335 
336 	list_for_each_entry(prog, &head->plist, link) {
337 		if (arg->count < arg->skip)
338 			goto skip;
339 		if (arg->fn(tp, (unsigned long) prog, arg) < 0) {
340 			arg->stop = 1;
341 			break;
342 		}
343 skip:
344 		arg->count++;
345 	}
346 }
347 
348 static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
349 	.kind		=	"bpf",
350 	.owner		=	THIS_MODULE,
351 	.classify	=	cls_bpf_classify,
352 	.init		=	cls_bpf_init,
353 	.destroy	=	cls_bpf_destroy,
354 	.get		=	cls_bpf_get,
355 	.change		=	cls_bpf_change,
356 	.delete		=	cls_bpf_delete,
357 	.walk		=	cls_bpf_walk,
358 	.dump		=	cls_bpf_dump,
359 };
360 
361 static int __init cls_bpf_init_mod(void)
362 {
363 	return register_tcf_proto_ops(&cls_bpf_ops);
364 }
365 
366 static void __exit cls_bpf_exit_mod(void)
367 {
368 	unregister_tcf_proto_ops(&cls_bpf_ops);
369 }
370 
371 module_init(cls_bpf_init_mod);
372 module_exit(cls_bpf_exit_mod);
373