blob: 243c9f225a734799cde949a1ff91c1a554e5d584 [file] [log] [blame]
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +01001/*
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>
Daniel Borkmanne2e9b652015-03-01 12:31:48 +010019#include <linux/bpf.h>
20
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010021#include <net/rtnetlink.h>
22#include <net/pkt_cls.h>
23#include <net/sock.h>
24
25MODULE_LICENSE("GPL");
26MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
27MODULE_DESCRIPTION("TC BPF based classifier");
28
Daniel Borkmanne2e9b652015-03-01 12:31:48 +010029#define CLS_BPF_NAME_LEN 256
30
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010031struct cls_bpf_head {
32 struct list_head plist;
33 u32 hgen;
John Fastabend1f947bf2014-09-12 20:10:24 -070034 struct rcu_head rcu;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010035};
36
37struct cls_bpf_prog {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -070038 struct bpf_prog *filter;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010039 struct list_head link;
Daniel Borkmanne2e9b652015-03-01 12:31:48 +010040 struct tcf_result res;
41 struct tcf_exts exts;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010042 u32 handle;
Daniel Borkmanne2e9b652015-03-01 12:31:48 +010043 union {
44 u32 bpf_fd;
45 u16 bpf_num_ops;
46 };
47 struct sock_filter *bpf_ops;
48 const char *bpf_name;
John Fastabend1f947bf2014-09-12 20:10:24 -070049 struct tcf_proto *tp;
50 struct rcu_head rcu;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010051};
52
53static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
54 [TCA_BPF_CLASSID] = { .type = NLA_U32 },
Daniel Borkmanne2e9b652015-03-01 12:31:48 +010055 [TCA_BPF_FD] = { .type = NLA_U32 },
56 [TCA_BPF_NAME] = { .type = NLA_NUL_STRING, .len = CLS_BPF_NAME_LEN },
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010057 [TCA_BPF_OPS_LEN] = { .type = NLA_U16 },
58 [TCA_BPF_OPS] = { .type = NLA_BINARY,
59 .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
60};
61
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010062static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
63 struct tcf_result *res)
64{
WANG Cong80dcbd12014-09-15 14:21:50 -070065 struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010066 struct cls_bpf_prog *prog;
67 int ret;
68
John Fastabend1f947bf2014-09-12 20:10:24 -070069 list_for_each_entry_rcu(prog, &head->plist, link) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -070070 int filter_res = BPF_PROG_RUN(prog->filter, skb);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010071
72 if (filter_res == 0)
73 continue;
74
75 *res = prog->res;
76 if (filter_res != -1)
77 res->classid = filter_res;
78
79 ret = tcf_exts_exec(skb, &prog->exts, res);
80 if (ret < 0)
81 continue;
82
83 return ret;
84 }
85
86 return -1;
87}
88
Daniel Borkmanne2e9b652015-03-01 12:31:48 +010089static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog)
90{
91 return !prog->bpf_ops;
92}
93
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +010094static int cls_bpf_init(struct tcf_proto *tp)
95{
96 struct cls_bpf_head *head;
97
98 head = kzalloc(sizeof(*head), GFP_KERNEL);
99 if (head == NULL)
100 return -ENOBUFS;
101
John Fastabend1f947bf2014-09-12 20:10:24 -0700102 INIT_LIST_HEAD_RCU(&head->plist);
103 rcu_assign_pointer(tp->root, head);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100104
105 return 0;
106}
107
108static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
109{
WANG Cong18d02642014-09-25 10:26:37 -0700110 tcf_exts_destroy(&prog->exts);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100111
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100112 if (cls_bpf_is_ebpf(prog))
113 bpf_prog_put(prog->filter);
114 else
115 bpf_prog_destroy(prog->filter);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100116
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100117 kfree(prog->bpf_name);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100118 kfree(prog->bpf_ops);
119 kfree(prog);
120}
121
John Fastabend1f947bf2014-09-12 20:10:24 -0700122static void __cls_bpf_delete_prog(struct rcu_head *rcu)
123{
124 struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu);
125
126 cls_bpf_delete_prog(prog->tp, prog);
127}
128
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100129static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
130{
Jiri Pirko472f5832014-12-02 18:00:32 +0100131 struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100132
Jiri Pirko472f5832014-12-02 18:00:32 +0100133 list_del_rcu(&prog->link);
134 tcf_unbind_filter(tp, &prog->res);
135 call_rcu(&prog->rcu, __cls_bpf_delete_prog);
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100136
Jiri Pirko472f5832014-12-02 18:00:32 +0100137 return 0;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100138}
139
Cong Wang1e052be2015-03-06 11:47:59 -0800140static bool cls_bpf_destroy(struct tcf_proto *tp, bool force)
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100141{
John Fastabend1f947bf2014-09-12 20:10:24 -0700142 struct cls_bpf_head *head = rtnl_dereference(tp->root);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100143 struct cls_bpf_prog *prog, *tmp;
144
Cong Wang1e052be2015-03-06 11:47:59 -0800145 if (!force && !list_empty(&head->plist))
146 return false;
147
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100148 list_for_each_entry_safe(prog, tmp, &head->plist, link) {
John Fastabend1f947bf2014-09-12 20:10:24 -0700149 list_del_rcu(&prog->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700150 tcf_unbind_filter(tp, &prog->res);
John Fastabend1f947bf2014-09-12 20:10:24 -0700151 call_rcu(&prog->rcu, __cls_bpf_delete_prog);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100152 }
153
John Fastabend1f947bf2014-09-12 20:10:24 -0700154 RCU_INIT_POINTER(tp->root, NULL);
155 kfree_rcu(head, rcu);
Cong Wang1e052be2015-03-06 11:47:59 -0800156 return true;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100157}
158
159static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
160{
John Fastabend1f947bf2014-09-12 20:10:24 -0700161 struct cls_bpf_head *head = rtnl_dereference(tp->root);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100162 struct cls_bpf_prog *prog;
163 unsigned long ret = 0UL;
164
165 if (head == NULL)
166 return 0UL;
167
Jiri Pirko3fe6b492014-12-02 18:00:33 +0100168 list_for_each_entry(prog, &head->plist, link) {
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100169 if (prog->handle == handle) {
170 ret = (unsigned long) prog;
171 break;
172 }
173 }
174
175 return ret;
176}
177
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100178static int cls_bpf_prog_from_ops(struct nlattr **tb,
179 struct cls_bpf_prog *prog, u32 classid)
180{
181 struct sock_filter *bpf_ops;
182 struct sock_fprog_kern fprog_tmp;
183 struct bpf_prog *fp;
184 u16 bpf_size, bpf_num_ops;
185 int ret;
186
187 bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
188 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
189 return -EINVAL;
190
191 bpf_size = bpf_num_ops * sizeof(*bpf_ops);
192 if (bpf_size != nla_len(tb[TCA_BPF_OPS]))
193 return -EINVAL;
194
195 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
196 if (bpf_ops == NULL)
197 return -ENOMEM;
198
199 memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
200
201 fprog_tmp.len = bpf_num_ops;
202 fprog_tmp.filter = bpf_ops;
203
204 ret = bpf_prog_create(&fp, &fprog_tmp);
205 if (ret < 0) {
206 kfree(bpf_ops);
207 return ret;
208 }
209
210 prog->bpf_ops = bpf_ops;
211 prog->bpf_num_ops = bpf_num_ops;
212 prog->bpf_name = NULL;
213
214 prog->filter = fp;
215 prog->res.classid = classid;
216
217 return 0;
218}
219
220static int cls_bpf_prog_from_efd(struct nlattr **tb,
221 struct cls_bpf_prog *prog, u32 classid)
222{
223 struct bpf_prog *fp;
224 char *name = NULL;
225 u32 bpf_fd;
226
227 bpf_fd = nla_get_u32(tb[TCA_BPF_FD]);
228
229 fp = bpf_prog_get(bpf_fd);
230 if (IS_ERR(fp))
231 return PTR_ERR(fp);
232
233 if (fp->type != BPF_PROG_TYPE_SCHED_CLS) {
234 bpf_prog_put(fp);
235 return -EINVAL;
236 }
237
238 if (tb[TCA_BPF_NAME]) {
239 name = kmemdup(nla_data(tb[TCA_BPF_NAME]),
240 nla_len(tb[TCA_BPF_NAME]),
241 GFP_KERNEL);
242 if (!name) {
243 bpf_prog_put(fp);
244 return -ENOMEM;
245 }
246 }
247
248 prog->bpf_ops = NULL;
249 prog->bpf_fd = bpf_fd;
250 prog->bpf_name = name;
251
252 prog->filter = fp;
253 prog->res.classid = classid;
254
255 return 0;
256}
257
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100258static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
259 struct cls_bpf_prog *prog,
260 unsigned long base, struct nlattr **tb,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700261 struct nlattr *est, bool ovr)
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100262{
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100263 struct tcf_exts exts;
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100264 bool is_bpf, is_ebpf;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100265 u32 classid;
266 int ret;
267
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100268 is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
269 is_ebpf = tb[TCA_BPF_FD];
270
271 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) ||
272 !tb[TCA_BPF_CLASSID])
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100273 return -EINVAL;
274
WANG Cong5da57f42013-12-15 20:15:07 -0800275 tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700276 ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100277 if (ret < 0)
278 return ret;
279
280 classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100281
282 ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog, classid) :
283 cls_bpf_prog_from_efd(tb, prog, classid);
284 if (ret < 0) {
285 tcf_exts_destroy(&exts);
286 return ret;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100287 }
288
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100289 tcf_bind_filter(tp, &prog->res, base);
290 tcf_exts_change(tp, &prog->exts, &exts);
291
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100292 return 0;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100293}
294
295static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
296 struct cls_bpf_head *head)
297{
298 unsigned int i = 0x80000000;
Daniel Borkmann3f2ab132015-01-22 10:41:02 +0100299 u32 handle;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100300
301 do {
302 if (++head->hgen == 0x7FFFFFFF)
303 head->hgen = 1;
304 } while (--i > 0 && cls_bpf_get(tp, head->hgen));
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100305
Daniel Borkmann3f2ab132015-01-22 10:41:02 +0100306 if (unlikely(i == 0)) {
307 pr_err("Insufficient number of handles\n");
308 handle = 0;
309 } else {
310 handle = head->hgen;
311 }
312
313 return handle;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100314}
315
316static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
317 struct tcf_proto *tp, unsigned long base,
318 u32 handle, struct nlattr **tca,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700319 unsigned long *arg, bool ovr)
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100320{
John Fastabend1f947bf2014-09-12 20:10:24 -0700321 struct cls_bpf_head *head = rtnl_dereference(tp->root);
322 struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100323 struct nlattr *tb[TCA_BPF_MAX + 1];
John Fastabend1f947bf2014-09-12 20:10:24 -0700324 struct cls_bpf_prog *prog;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100325 int ret;
326
327 if (tca[TCA_OPTIONS] == NULL)
328 return -EINVAL;
329
330 ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy);
331 if (ret < 0)
332 return ret;
333
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100334 prog = kzalloc(sizeof(*prog), GFP_KERNEL);
John Fastabend1f947bf2014-09-12 20:10:24 -0700335 if (!prog)
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100336 return -ENOBUFS;
337
WANG Cong5da57f42013-12-15 20:15:07 -0800338 tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
John Fastabend1f947bf2014-09-12 20:10:24 -0700339
340 if (oldprog) {
341 if (handle && oldprog->handle != handle) {
342 ret = -EINVAL;
343 goto errout;
344 }
345 }
346
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100347 if (handle == 0)
348 prog->handle = cls_bpf_grab_new_handle(tp, head);
349 else
350 prog->handle = handle;
351 if (prog->handle == 0) {
352 ret = -EINVAL;
353 goto errout;
354 }
355
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700356 ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100357 if (ret < 0)
358 goto errout;
359
John Fastabend1f947bf2014-09-12 20:10:24 -0700360 if (oldprog) {
361 list_replace_rcu(&prog->link, &oldprog->link);
John Fastabend18cdb372014-10-05 21:28:52 -0700362 tcf_unbind_filter(tp, &oldprog->res);
John Fastabend1f947bf2014-09-12 20:10:24 -0700363 call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
364 } else {
365 list_add_rcu(&prog->link, &head->plist);
366 }
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100367
368 *arg = (unsigned long) prog;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100369 return 0;
370errout:
John Fastabend1f947bf2014-09-12 20:10:24 -0700371 kfree(prog);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100372
373 return ret;
374}
375
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100376static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog,
377 struct sk_buff *skb)
378{
379 struct nlattr *nla;
380
381 if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
382 return -EMSGSIZE;
383
384 nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
385 sizeof(struct sock_filter));
386 if (nla == NULL)
387 return -EMSGSIZE;
388
389 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
390
391 return 0;
392}
393
394static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog,
395 struct sk_buff *skb)
396{
397 if (nla_put_u32(skb, TCA_BPF_FD, prog->bpf_fd))
398 return -EMSGSIZE;
399
400 if (prog->bpf_name &&
401 nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name))
402 return -EMSGSIZE;
403
404 return 0;
405}
406
WANG Cong832d1d52014-01-09 16:14:01 -0800407static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100408 struct sk_buff *skb, struct tcmsg *tm)
409{
410 struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100411 struct nlattr *nest;
412 int ret;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100413
414 if (prog == NULL)
415 return skb->len;
416
417 tm->tcm_handle = prog->handle;
418
419 nest = nla_nest_start(skb, TCA_OPTIONS);
420 if (nest == NULL)
421 goto nla_put_failure;
422
423 if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
424 goto nla_put_failure;
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100425
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100426 if (cls_bpf_is_ebpf(prog))
427 ret = cls_bpf_dump_ebpf_info(prog, skb);
428 else
429 ret = cls_bpf_dump_bpf_info(prog, skb);
430 if (ret)
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100431 goto nla_put_failure;
432
WANG Cong5da57f42013-12-15 20:15:07 -0800433 if (tcf_exts_dump(skb, &prog->exts) < 0)
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100434 goto nla_put_failure;
435
436 nla_nest_end(skb, nest);
437
WANG Cong5da57f42013-12-15 20:15:07 -0800438 if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100439 goto nla_put_failure;
440
441 return skb->len;
442
443nla_put_failure:
444 nla_nest_cancel(skb, nest);
445 return -1;
446}
447
448static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
449{
John Fastabend1f947bf2014-09-12 20:10:24 -0700450 struct cls_bpf_head *head = rtnl_dereference(tp->root);
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100451 struct cls_bpf_prog *prog;
452
Jiri Pirko3fe6b492014-12-02 18:00:33 +0100453 list_for_each_entry(prog, &head->plist, link) {
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100454 if (arg->count < arg->skip)
455 goto skip;
456 if (arg->fn(tp, (unsigned long) prog, arg) < 0) {
457 arg->stop = 1;
458 break;
459 }
460skip:
461 arg->count++;
462 }
463}
464
465static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
466 .kind = "bpf",
467 .owner = THIS_MODULE,
468 .classify = cls_bpf_classify,
469 .init = cls_bpf_init,
470 .destroy = cls_bpf_destroy,
471 .get = cls_bpf_get,
Daniel Borkmann7d1d65c2013-10-28 16:43:02 +0100472 .change = cls_bpf_change,
473 .delete = cls_bpf_delete,
474 .walk = cls_bpf_walk,
475 .dump = cls_bpf_dump,
476};
477
478static int __init cls_bpf_init_mod(void)
479{
480 return register_tcf_proto_ops(&cls_bpf_ops);
481}
482
483static void __exit cls_bpf_exit_mod(void)
484{
485 unregister_tcf_proto_ops(&cls_bpf_ops);
486}
487
488module_init(cls_bpf_init_mod);
489module_exit(cls_bpf_exit_mod);