xref: /linux/net/sched/cls_bpf.c (revision 9ee0034b8f49aaaa7e7c2da8db1038915db99c19)
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 <linux/bpf.h>
20 
21 #include <net/rtnetlink.h>
22 #include <net/pkt_cls.h>
23 #include <net/sock.h>
24 
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
27 MODULE_DESCRIPTION("TC BPF based classifier");
28 
29 #define CLS_BPF_NAME_LEN	256
30 
31 struct cls_bpf_head {
32 	struct list_head plist;
33 	u32 hgen;
34 	struct rcu_head rcu;
35 };
36 
37 struct cls_bpf_prog {
38 	struct bpf_prog *filter;
39 	struct list_head link;
40 	struct tcf_result res;
41 	bool exts_integrated;
42 	struct tcf_exts exts;
43 	u32 handle;
44 	union {
45 		u32 bpf_fd;
46 		u16 bpf_num_ops;
47 	};
48 	struct sock_filter *bpf_ops;
49 	const char *bpf_name;
50 	struct tcf_proto *tp;
51 	struct rcu_head rcu;
52 };
53 
54 static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
55 	[TCA_BPF_CLASSID]	= { .type = NLA_U32 },
56 	[TCA_BPF_FLAGS]		= { .type = NLA_U32 },
57 	[TCA_BPF_FD]		= { .type = NLA_U32 },
58 	[TCA_BPF_NAME]		= { .type = NLA_NUL_STRING, .len = CLS_BPF_NAME_LEN },
59 	[TCA_BPF_OPS_LEN]	= { .type = NLA_U16 },
60 	[TCA_BPF_OPS]		= { .type = NLA_BINARY,
61 				    .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
62 };
63 
64 static int cls_bpf_exec_opcode(int code)
65 {
66 	switch (code) {
67 	case TC_ACT_OK:
68 	case TC_ACT_SHOT:
69 	case TC_ACT_STOLEN:
70 	case TC_ACT_REDIRECT:
71 	case TC_ACT_UNSPEC:
72 		return code;
73 	default:
74 		return TC_ACT_UNSPEC;
75 	}
76 }
77 
78 static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
79 			    struct tcf_result *res)
80 {
81 	struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
82 	bool at_ingress = skb_at_tc_ingress(skb);
83 	struct cls_bpf_prog *prog;
84 	int ret = -1;
85 
86 	if (unlikely(!skb_mac_header_was_set(skb)))
87 		return -1;
88 
89 	/* Needed here for accessing maps. */
90 	rcu_read_lock();
91 	list_for_each_entry_rcu(prog, &head->plist, link) {
92 		int filter_res;
93 
94 		qdisc_skb_cb(skb)->tc_classid = prog->res.classid;
95 
96 		if (at_ingress) {
97 			/* It is safe to push/pull even if skb_shared() */
98 			__skb_push(skb, skb->mac_len);
99 			bpf_compute_data_end(skb);
100 			filter_res = BPF_PROG_RUN(prog->filter, skb);
101 			__skb_pull(skb, skb->mac_len);
102 		} else {
103 			bpf_compute_data_end(skb);
104 			filter_res = BPF_PROG_RUN(prog->filter, skb);
105 		}
106 
107 		if (prog->exts_integrated) {
108 			res->class   = 0;
109 			res->classid = TC_H_MAJ(prog->res.classid) |
110 				       qdisc_skb_cb(skb)->tc_classid;
111 
112 			ret = cls_bpf_exec_opcode(filter_res);
113 			if (ret == TC_ACT_UNSPEC)
114 				continue;
115 			break;
116 		}
117 
118 		if (filter_res == 0)
119 			continue;
120 		if (filter_res != -1) {
121 			res->class   = 0;
122 			res->classid = filter_res;
123 		} else {
124 			*res = prog->res;
125 		}
126 
127 		ret = tcf_exts_exec(skb, &prog->exts, res);
128 		if (ret < 0)
129 			continue;
130 
131 		break;
132 	}
133 	rcu_read_unlock();
134 
135 	return ret;
136 }
137 
138 static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog)
139 {
140 	return !prog->bpf_ops;
141 }
142 
143 static int cls_bpf_init(struct tcf_proto *tp)
144 {
145 	struct cls_bpf_head *head;
146 
147 	head = kzalloc(sizeof(*head), GFP_KERNEL);
148 	if (head == NULL)
149 		return -ENOBUFS;
150 
151 	INIT_LIST_HEAD_RCU(&head->plist);
152 	rcu_assign_pointer(tp->root, head);
153 
154 	return 0;
155 }
156 
157 static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
158 {
159 	tcf_exts_destroy(&prog->exts);
160 
161 	if (cls_bpf_is_ebpf(prog))
162 		bpf_prog_put(prog->filter);
163 	else
164 		bpf_prog_destroy(prog->filter);
165 
166 	kfree(prog->bpf_name);
167 	kfree(prog->bpf_ops);
168 	kfree(prog);
169 }
170 
171 static void __cls_bpf_delete_prog(struct rcu_head *rcu)
172 {
173 	struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu);
174 
175 	cls_bpf_delete_prog(prog->tp, prog);
176 }
177 
178 static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
179 {
180 	struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg;
181 
182 	list_del_rcu(&prog->link);
183 	tcf_unbind_filter(tp, &prog->res);
184 	call_rcu(&prog->rcu, __cls_bpf_delete_prog);
185 
186 	return 0;
187 }
188 
189 static bool cls_bpf_destroy(struct tcf_proto *tp, bool force)
190 {
191 	struct cls_bpf_head *head = rtnl_dereference(tp->root);
192 	struct cls_bpf_prog *prog, *tmp;
193 
194 	if (!force && !list_empty(&head->plist))
195 		return false;
196 
197 	list_for_each_entry_safe(prog, tmp, &head->plist, link) {
198 		list_del_rcu(&prog->link);
199 		tcf_unbind_filter(tp, &prog->res);
200 		call_rcu(&prog->rcu, __cls_bpf_delete_prog);
201 	}
202 
203 	RCU_INIT_POINTER(tp->root, NULL);
204 	kfree_rcu(head, rcu);
205 	return true;
206 }
207 
208 static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
209 {
210 	struct cls_bpf_head *head = rtnl_dereference(tp->root);
211 	struct cls_bpf_prog *prog;
212 	unsigned long ret = 0UL;
213 
214 	if (head == NULL)
215 		return 0UL;
216 
217 	list_for_each_entry(prog, &head->plist, link) {
218 		if (prog->handle == handle) {
219 			ret = (unsigned long) prog;
220 			break;
221 		}
222 	}
223 
224 	return ret;
225 }
226 
227 static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
228 {
229 	struct sock_filter *bpf_ops;
230 	struct sock_fprog_kern fprog_tmp;
231 	struct bpf_prog *fp;
232 	u16 bpf_size, bpf_num_ops;
233 	int ret;
234 
235 	bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
236 	if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
237 		return -EINVAL;
238 
239 	bpf_size = bpf_num_ops * sizeof(*bpf_ops);
240 	if (bpf_size != nla_len(tb[TCA_BPF_OPS]))
241 		return -EINVAL;
242 
243 	bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
244 	if (bpf_ops == NULL)
245 		return -ENOMEM;
246 
247 	memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
248 
249 	fprog_tmp.len = bpf_num_ops;
250 	fprog_tmp.filter = bpf_ops;
251 
252 	ret = bpf_prog_create(&fp, &fprog_tmp);
253 	if (ret < 0) {
254 		kfree(bpf_ops);
255 		return ret;
256 	}
257 
258 	prog->bpf_ops = bpf_ops;
259 	prog->bpf_num_ops = bpf_num_ops;
260 	prog->bpf_name = NULL;
261 	prog->filter = fp;
262 
263 	return 0;
264 }
265 
266 static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
267 				 const struct tcf_proto *tp)
268 {
269 	struct bpf_prog *fp;
270 	char *name = NULL;
271 	u32 bpf_fd;
272 
273 	bpf_fd = nla_get_u32(tb[TCA_BPF_FD]);
274 
275 	fp = bpf_prog_get_type(bpf_fd, BPF_PROG_TYPE_SCHED_CLS);
276 	if (IS_ERR(fp))
277 		return PTR_ERR(fp);
278 
279 	if (tb[TCA_BPF_NAME]) {
280 		name = kmemdup(nla_data(tb[TCA_BPF_NAME]),
281 			       nla_len(tb[TCA_BPF_NAME]),
282 			       GFP_KERNEL);
283 		if (!name) {
284 			bpf_prog_put(fp);
285 			return -ENOMEM;
286 		}
287 	}
288 
289 	prog->bpf_ops = NULL;
290 	prog->bpf_fd = bpf_fd;
291 	prog->bpf_name = name;
292 	prog->filter = fp;
293 
294 	if (fp->dst_needed && !(tp->q->flags & TCQ_F_INGRESS))
295 		netif_keep_dst(qdisc_dev(tp->q));
296 
297 	return 0;
298 }
299 
300 static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
301 				   struct cls_bpf_prog *prog,
302 				   unsigned long base, struct nlattr **tb,
303 				   struct nlattr *est, bool ovr)
304 {
305 	bool is_bpf, is_ebpf, have_exts = false;
306 	struct tcf_exts exts;
307 	int ret;
308 
309 	is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
310 	is_ebpf = tb[TCA_BPF_FD];
311 	if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf))
312 		return -EINVAL;
313 
314 	ret = tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
315 	if (ret < 0)
316 		return ret;
317 	ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
318 	if (ret < 0)
319 		goto errout;
320 
321 	if (tb[TCA_BPF_FLAGS]) {
322 		u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]);
323 
324 		if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT) {
325 			ret = -EINVAL;
326 			goto errout;
327 		}
328 
329 		have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT;
330 	}
331 
332 	prog->exts_integrated = have_exts;
333 
334 	ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
335 		       cls_bpf_prog_from_efd(tb, prog, tp);
336 	if (ret < 0)
337 		goto errout;
338 
339 	if (tb[TCA_BPF_CLASSID]) {
340 		prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
341 		tcf_bind_filter(tp, &prog->res, base);
342 	}
343 
344 	tcf_exts_change(tp, &prog->exts, &exts);
345 	return 0;
346 
347 errout:
348 	tcf_exts_destroy(&exts);
349 	return ret;
350 }
351 
352 static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
353 				   struct cls_bpf_head *head)
354 {
355 	unsigned int i = 0x80000000;
356 	u32 handle;
357 
358 	do {
359 		if (++head->hgen == 0x7FFFFFFF)
360 			head->hgen = 1;
361 	} while (--i > 0 && cls_bpf_get(tp, head->hgen));
362 
363 	if (unlikely(i == 0)) {
364 		pr_err("Insufficient number of handles\n");
365 		handle = 0;
366 	} else {
367 		handle = head->hgen;
368 	}
369 
370 	return handle;
371 }
372 
373 static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
374 			  struct tcf_proto *tp, unsigned long base,
375 			  u32 handle, struct nlattr **tca,
376 			  unsigned long *arg, bool ovr)
377 {
378 	struct cls_bpf_head *head = rtnl_dereference(tp->root);
379 	struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg;
380 	struct nlattr *tb[TCA_BPF_MAX + 1];
381 	struct cls_bpf_prog *prog;
382 	int ret;
383 
384 	if (tca[TCA_OPTIONS] == NULL)
385 		return -EINVAL;
386 
387 	ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy);
388 	if (ret < 0)
389 		return ret;
390 
391 	prog = kzalloc(sizeof(*prog), GFP_KERNEL);
392 	if (!prog)
393 		return -ENOBUFS;
394 
395 	ret = tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
396 	if (ret < 0)
397 		goto errout;
398 
399 	if (oldprog) {
400 		if (handle && oldprog->handle != handle) {
401 			ret = -EINVAL;
402 			goto errout;
403 		}
404 	}
405 
406 	if (handle == 0)
407 		prog->handle = cls_bpf_grab_new_handle(tp, head);
408 	else
409 		prog->handle = handle;
410 	if (prog->handle == 0) {
411 		ret = -EINVAL;
412 		goto errout;
413 	}
414 
415 	ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr);
416 	if (ret < 0)
417 		goto errout;
418 
419 	if (oldprog) {
420 		list_replace_rcu(&oldprog->link, &prog->link);
421 		tcf_unbind_filter(tp, &oldprog->res);
422 		call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
423 	} else {
424 		list_add_rcu(&prog->link, &head->plist);
425 	}
426 
427 	*arg = (unsigned long) prog;
428 	return 0;
429 
430 errout:
431 	tcf_exts_destroy(&prog->exts);
432 	kfree(prog);
433 	return ret;
434 }
435 
436 static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog,
437 				 struct sk_buff *skb)
438 {
439 	struct nlattr *nla;
440 
441 	if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
442 		return -EMSGSIZE;
443 
444 	nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
445 			  sizeof(struct sock_filter));
446 	if (nla == NULL)
447 		return -EMSGSIZE;
448 
449 	memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
450 
451 	return 0;
452 }
453 
454 static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog,
455 				  struct sk_buff *skb)
456 {
457 	if (nla_put_u32(skb, TCA_BPF_FD, prog->bpf_fd))
458 		return -EMSGSIZE;
459 
460 	if (prog->bpf_name &&
461 	    nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name))
462 		return -EMSGSIZE;
463 
464 	return 0;
465 }
466 
467 static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
468 			struct sk_buff *skb, struct tcmsg *tm)
469 {
470 	struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
471 	struct nlattr *nest;
472 	u32 bpf_flags = 0;
473 	int ret;
474 
475 	if (prog == NULL)
476 		return skb->len;
477 
478 	tm->tcm_handle = prog->handle;
479 
480 	nest = nla_nest_start(skb, TCA_OPTIONS);
481 	if (nest == NULL)
482 		goto nla_put_failure;
483 
484 	if (prog->res.classid &&
485 	    nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
486 		goto nla_put_failure;
487 
488 	if (cls_bpf_is_ebpf(prog))
489 		ret = cls_bpf_dump_ebpf_info(prog, skb);
490 	else
491 		ret = cls_bpf_dump_bpf_info(prog, skb);
492 	if (ret)
493 		goto nla_put_failure;
494 
495 	if (tcf_exts_dump(skb, &prog->exts) < 0)
496 		goto nla_put_failure;
497 
498 	if (prog->exts_integrated)
499 		bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
500 	if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags))
501 		goto nla_put_failure;
502 
503 	nla_nest_end(skb, nest);
504 
505 	if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
506 		goto nla_put_failure;
507 
508 	return skb->len;
509 
510 nla_put_failure:
511 	nla_nest_cancel(skb, nest);
512 	return -1;
513 }
514 
515 static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
516 {
517 	struct cls_bpf_head *head = rtnl_dereference(tp->root);
518 	struct cls_bpf_prog *prog;
519 
520 	list_for_each_entry(prog, &head->plist, link) {
521 		if (arg->count < arg->skip)
522 			goto skip;
523 		if (arg->fn(tp, (unsigned long) prog, arg) < 0) {
524 			arg->stop = 1;
525 			break;
526 		}
527 skip:
528 		arg->count++;
529 	}
530 }
531 
532 static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
533 	.kind		=	"bpf",
534 	.owner		=	THIS_MODULE,
535 	.classify	=	cls_bpf_classify,
536 	.init		=	cls_bpf_init,
537 	.destroy	=	cls_bpf_destroy,
538 	.get		=	cls_bpf_get,
539 	.change		=	cls_bpf_change,
540 	.delete		=	cls_bpf_delete,
541 	.walk		=	cls_bpf_walk,
542 	.dump		=	cls_bpf_dump,
543 };
544 
545 static int __init cls_bpf_init_mod(void)
546 {
547 	return register_tcf_proto_ops(&cls_bpf_ops);
548 }
549 
550 static void __exit cls_bpf_exit_mod(void)
551 {
552 	unregister_tcf_proto_ops(&cls_bpf_ops);
553 }
554 
555 module_init(cls_bpf_init_mod);
556 module_exit(cls_bpf_exit_mod);
557