2 * Berkeley Packet Filter based traffic classifier
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
8 * (C) 2013 Daniel Borkmann <dborkman@redhat.com>
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.
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>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
25 MODULE_DESCRIPTION("TC BPF based classifier");
28 struct list_head plist;
34 struct bpf_prog *filter;
35 struct sock_filter *bpf_ops;
37 struct tcf_result res;
38 struct list_head link;
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 },
52 static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
53 struct tcf_result *res)
55 struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
56 struct cls_bpf_prog *prog;
59 list_for_each_entry_rcu(prog, &head->plist, link) {
60 int filter_res = BPF_PROG_RUN(prog->filter, skb);
67 res->classid = filter_res;
69 ret = tcf_exts_exec(skb, &prog->exts, res);
79 static int cls_bpf_init(struct tcf_proto *tp)
81 struct cls_bpf_head *head;
83 head = kzalloc(sizeof(*head), GFP_KERNEL);
87 INIT_LIST_HEAD_RCU(&head->plist);
88 rcu_assign_pointer(tp->root, head);
93 static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
95 tcf_unbind_filter(tp, &prog->res);
96 tcf_exts_destroy(tp, &prog->exts);
98 bpf_prog_destroy(prog->filter);
100 kfree(prog->bpf_ops);
104 static void __cls_bpf_delete_prog(struct rcu_head *rcu)
106 struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu);
108 cls_bpf_delete_prog(prog->tp, prog);
111 static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
113 struct cls_bpf_head *head = rtnl_dereference(tp->root);
114 struct cls_bpf_prog *prog, *todel = (struct cls_bpf_prog *) arg;
116 list_for_each_entry(prog, &head->plist, link) {
118 list_del_rcu(&prog->link);
119 call_rcu(&prog->rcu, __cls_bpf_delete_prog);
127 static void cls_bpf_destroy(struct tcf_proto *tp)
129 struct cls_bpf_head *head = rtnl_dereference(tp->root);
130 struct cls_bpf_prog *prog, *tmp;
132 list_for_each_entry_safe(prog, tmp, &head->plist, link) {
133 list_del_rcu(&prog->link);
134 call_rcu(&prog->rcu, __cls_bpf_delete_prog);
137 RCU_INIT_POINTER(tp->root, NULL);
138 kfree_rcu(head, rcu);
141 static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
143 struct cls_bpf_head *head = rtnl_dereference(tp->root);
144 struct cls_bpf_prog *prog;
145 unsigned long ret = 0UL;
150 list_for_each_entry_rcu(prog, &head->plist, link) {
151 if (prog->handle == handle) {
152 ret = (unsigned long) prog;
160 static void cls_bpf_put(struct tcf_proto *tp, unsigned long f)
164 static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
165 struct cls_bpf_prog *prog,
166 unsigned long base, struct nlattr **tb,
167 struct nlattr *est, bool ovr)
169 struct sock_filter *bpf_ops;
170 struct tcf_exts exts;
171 struct sock_fprog_kern tmp;
173 u16 bpf_size, bpf_len;
177 if (!tb[TCA_BPF_OPS_LEN] || !tb[TCA_BPF_OPS] || !tb[TCA_BPF_CLASSID])
180 tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
181 ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
185 classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
186 bpf_len = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
187 if (bpf_len > BPF_MAXINSNS || bpf_len == 0) {
192 bpf_size = bpf_len * sizeof(*bpf_ops);
193 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
194 if (bpf_ops == NULL) {
199 memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
202 tmp.filter = bpf_ops;
204 ret = bpf_prog_create(&fp, &tmp);
208 prog->bpf_len = bpf_len;
209 prog->bpf_ops = bpf_ops;
211 prog->res.classid = classid;
213 tcf_bind_filter(tp, &prog->res, base);
214 tcf_exts_change(tp, &prog->exts, &exts);
220 tcf_exts_destroy(tp, &exts);
224 static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
225 struct cls_bpf_head *head)
227 unsigned int i = 0x80000000;
230 if (++head->hgen == 0x7FFFFFFF)
232 } while (--i > 0 && cls_bpf_get(tp, head->hgen));
234 pr_err("Insufficient number of handles\n");
239 static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
240 struct tcf_proto *tp, unsigned long base,
241 u32 handle, struct nlattr **tca,
242 unsigned long *arg, bool ovr)
244 struct cls_bpf_head *head = rtnl_dereference(tp->root);
245 struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg;
246 struct nlattr *tb[TCA_BPF_MAX + 1];
247 struct cls_bpf_prog *prog;
250 if (tca[TCA_OPTIONS] == NULL)
253 ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy);
257 prog = kzalloc(sizeof(*prog), GFP_KERNEL);
261 tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
264 if (handle && oldprog->handle != handle) {
271 prog->handle = cls_bpf_grab_new_handle(tp, head);
273 prog->handle = handle;
274 if (prog->handle == 0) {
279 ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr);
284 list_replace_rcu(&prog->link, &oldprog->link);
285 call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
287 list_add_rcu(&prog->link, &head->plist);
290 *arg = (unsigned long) prog;
298 static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
299 struct sk_buff *skb, struct tcmsg *tm)
301 struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
302 struct nlattr *nest, *nla;
307 tm->tcm_handle = prog->handle;
309 nest = nla_nest_start(skb, TCA_OPTIONS);
311 goto nla_put_failure;
313 if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
314 goto nla_put_failure;
315 if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_len))
316 goto nla_put_failure;
318 nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_len *
319 sizeof(struct sock_filter));
321 goto nla_put_failure;
323 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
325 if (tcf_exts_dump(skb, &prog->exts) < 0)
326 goto nla_put_failure;
328 nla_nest_end(skb, nest);
330 if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
331 goto nla_put_failure;
336 nla_nest_cancel(skb, nest);
340 static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
342 struct cls_bpf_head *head = rtnl_dereference(tp->root);
343 struct cls_bpf_prog *prog;
345 list_for_each_entry_rcu(prog, &head->plist, link) {
346 if (arg->count < arg->skip)
348 if (arg->fn(tp, (unsigned long) prog, arg) < 0) {
357 static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
359 .owner = THIS_MODULE,
360 .classify = cls_bpf_classify,
361 .init = cls_bpf_init,
362 .destroy = cls_bpf_destroy,
365 .change = cls_bpf_change,
366 .delete = cls_bpf_delete,
367 .walk = cls_bpf_walk,
368 .dump = cls_bpf_dump,
371 static int __init cls_bpf_init_mod(void)
373 return register_tcf_proto_ops(&cls_bpf_ops);
376 static void __exit cls_bpf_exit_mod(void)
378 unregister_tcf_proto_ops(&cls_bpf_ops);
381 module_init(cls_bpf_init_mod);
382 module_exit(cls_bpf_exit_mod);