Commit | Line | Data |
---|---|---|
7d1d65cb DB |
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> | |
e2e9b654 DB |
19 | #include <linux/bpf.h> |
20 | ||
7d1d65cb DB |
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 | ||
e2e9b654 DB |
29 | #define CLS_BPF_NAME_LEN 256 |
30 | ||
7d1d65cb DB |
31 | struct cls_bpf_head { |
32 | struct list_head plist; | |
33 | u32 hgen; | |
1f947bf1 | 34 | struct rcu_head rcu; |
7d1d65cb DB |
35 | }; |
36 | ||
37 | struct cls_bpf_prog { | |
7ae457c1 | 38 | struct bpf_prog *filter; |
7d1d65cb | 39 | struct list_head link; |
e2e9b654 DB |
40 | struct tcf_result res; |
41 | struct tcf_exts exts; | |
7d1d65cb | 42 | u32 handle; |
e2e9b654 DB |
43 | union { |
44 | u32 bpf_fd; | |
45 | u16 bpf_num_ops; | |
46 | }; | |
47 | struct sock_filter *bpf_ops; | |
48 | const char *bpf_name; | |
1f947bf1 JF |
49 | struct tcf_proto *tp; |
50 | struct rcu_head rcu; | |
7d1d65cb DB |
51 | }; |
52 | ||
53 | static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = { | |
54 | [TCA_BPF_CLASSID] = { .type = NLA_U32 }, | |
e2e9b654 DB |
55 | [TCA_BPF_FD] = { .type = NLA_U32 }, |
56 | [TCA_BPF_NAME] = { .type = NLA_NUL_STRING, .len = CLS_BPF_NAME_LEN }, | |
7d1d65cb DB |
57 | [TCA_BPF_OPS_LEN] = { .type = NLA_U16 }, |
58 | [TCA_BPF_OPS] = { .type = NLA_BINARY, | |
59 | .len = sizeof(struct sock_filter) * BPF_MAXINSNS }, | |
60 | }; | |
61 | ||
7d1d65cb DB |
62 | static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
63 | struct tcf_result *res) | |
64 | { | |
80dcbd12 | 65 | struct cls_bpf_head *head = rcu_dereference_bh(tp->root); |
7d1d65cb DB |
66 | struct cls_bpf_prog *prog; |
67 | int ret; | |
68 | ||
1f947bf1 | 69 | list_for_each_entry_rcu(prog, &head->plist, link) { |
7ae457c1 | 70 | int filter_res = BPF_PROG_RUN(prog->filter, skb); |
7d1d65cb DB |
71 | |
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 | ||
e2e9b654 DB |
89 | static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog) |
90 | { | |
91 | return !prog->bpf_ops; | |
92 | } | |
93 | ||
7d1d65cb DB |
94 | static 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 | ||
1f947bf1 JF |
102 | INIT_LIST_HEAD_RCU(&head->plist); |
103 | rcu_assign_pointer(tp->root, head); | |
7d1d65cb DB |
104 | |
105 | return 0; | |
106 | } | |
107 | ||
108 | static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog) | |
109 | { | |
18d0264f | 110 | tcf_exts_destroy(&prog->exts); |
7d1d65cb | 111 | |
e2e9b654 DB |
112 | if (cls_bpf_is_ebpf(prog)) |
113 | bpf_prog_put(prog->filter); | |
114 | else | |
115 | bpf_prog_destroy(prog->filter); | |
7d1d65cb | 116 | |
e2e9b654 | 117 | kfree(prog->bpf_name); |
7d1d65cb DB |
118 | kfree(prog->bpf_ops); |
119 | kfree(prog); | |
120 | } | |
121 | ||
1f947bf1 JF |
122 | static 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 | ||
7d1d65cb DB |
129 | static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg) |
130 | { | |
472f5837 | 131 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg; |
7d1d65cb | 132 | |
472f5837 JP |
133 | list_del_rcu(&prog->link); |
134 | tcf_unbind_filter(tp, &prog->res); | |
135 | call_rcu(&prog->rcu, __cls_bpf_delete_prog); | |
e2e9b654 | 136 | |
472f5837 | 137 | return 0; |
7d1d65cb DB |
138 | } |
139 | ||
140 | static void cls_bpf_destroy(struct tcf_proto *tp) | |
141 | { | |
1f947bf1 | 142 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
143 | struct cls_bpf_prog *prog, *tmp; |
144 | ||
145 | list_for_each_entry_safe(prog, tmp, &head->plist, link) { | |
1f947bf1 | 146 | list_del_rcu(&prog->link); |
18cdb37e | 147 | tcf_unbind_filter(tp, &prog->res); |
1f947bf1 | 148 | call_rcu(&prog->rcu, __cls_bpf_delete_prog); |
7d1d65cb DB |
149 | } |
150 | ||
1f947bf1 JF |
151 | RCU_INIT_POINTER(tp->root, NULL); |
152 | kfree_rcu(head, rcu); | |
7d1d65cb DB |
153 | } |
154 | ||
155 | static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle) | |
156 | { | |
1f947bf1 | 157 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
158 | struct cls_bpf_prog *prog; |
159 | unsigned long ret = 0UL; | |
160 | ||
161 | if (head == NULL) | |
162 | return 0UL; | |
163 | ||
3fe6b49e | 164 | list_for_each_entry(prog, &head->plist, link) { |
7d1d65cb DB |
165 | if (prog->handle == handle) { |
166 | ret = (unsigned long) prog; | |
167 | break; | |
168 | } | |
169 | } | |
170 | ||
171 | return ret; | |
172 | } | |
173 | ||
e2e9b654 DB |
174 | static int cls_bpf_prog_from_ops(struct nlattr **tb, |
175 | struct cls_bpf_prog *prog, u32 classid) | |
7d1d65cb | 176 | { |
1f947bf1 | 177 | struct sock_filter *bpf_ops; |
e2e9b654 | 178 | struct sock_fprog_kern fprog_tmp; |
1f947bf1 | 179 | struct bpf_prog *fp; |
33e9fcc6 | 180 | u16 bpf_size, bpf_num_ops; |
7d1d65cb DB |
181 | int ret; |
182 | ||
33e9fcc6 | 183 | bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]); |
e2e9b654 DB |
184 | if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) |
185 | return -EINVAL; | |
7d1d65cb | 186 | |
33e9fcc6 | 187 | bpf_size = bpf_num_ops * sizeof(*bpf_ops); |
e2e9b654 DB |
188 | if (bpf_size != nla_len(tb[TCA_BPF_OPS])) |
189 | return -EINVAL; | |
7913ecf6 | 190 | |
7d1d65cb | 191 | bpf_ops = kzalloc(bpf_size, GFP_KERNEL); |
e2e9b654 DB |
192 | if (bpf_ops == NULL) |
193 | return -ENOMEM; | |
7d1d65cb DB |
194 | |
195 | memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); | |
196 | ||
e2e9b654 DB |
197 | fprog_tmp.len = bpf_num_ops; |
198 | fprog_tmp.filter = bpf_ops; | |
7d1d65cb | 199 | |
e2e9b654 DB |
200 | ret = bpf_prog_create(&fp, &fprog_tmp); |
201 | if (ret < 0) { | |
202 | kfree(bpf_ops); | |
203 | return ret; | |
204 | } | |
7d1d65cb | 205 | |
7d1d65cb | 206 | prog->bpf_ops = bpf_ops; |
e2e9b654 DB |
207 | prog->bpf_num_ops = bpf_num_ops; |
208 | prog->bpf_name = NULL; | |
209 | ||
7d1d65cb DB |
210 | prog->filter = fp; |
211 | prog->res.classid = classid; | |
7d1d65cb | 212 | |
e2e9b654 DB |
213 | return 0; |
214 | } | |
215 | ||
216 | static int cls_bpf_prog_from_efd(struct nlattr **tb, | |
217 | struct cls_bpf_prog *prog, u32 classid) | |
218 | { | |
219 | struct bpf_prog *fp; | |
220 | char *name = NULL; | |
221 | u32 bpf_fd; | |
222 | ||
223 | bpf_fd = nla_get_u32(tb[TCA_BPF_FD]); | |
224 | ||
225 | fp = bpf_prog_get(bpf_fd); | |
226 | if (IS_ERR(fp)) | |
227 | return PTR_ERR(fp); | |
228 | ||
229 | if (fp->type != BPF_PROG_TYPE_SCHED_CLS) { | |
230 | bpf_prog_put(fp); | |
231 | return -EINVAL; | |
232 | } | |
233 | ||
234 | if (tb[TCA_BPF_NAME]) { | |
235 | name = kmemdup(nla_data(tb[TCA_BPF_NAME]), | |
236 | nla_len(tb[TCA_BPF_NAME]), | |
237 | GFP_KERNEL); | |
238 | if (!name) { | |
239 | bpf_prog_put(fp); | |
240 | return -ENOMEM; | |
241 | } | |
242 | } | |
243 | ||
244 | prog->bpf_ops = NULL; | |
245 | prog->bpf_fd = bpf_fd; | |
246 | prog->bpf_name = name; | |
247 | ||
248 | prog->filter = fp; | |
249 | prog->res.classid = classid; | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
254 | static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp, | |
255 | struct cls_bpf_prog *prog, | |
256 | unsigned long base, struct nlattr **tb, | |
257 | struct nlattr *est, bool ovr) | |
258 | { | |
259 | struct tcf_exts exts; | |
260 | bool is_bpf, is_ebpf; | |
261 | u32 classid; | |
262 | int ret; | |
263 | ||
264 | is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS]; | |
265 | is_ebpf = tb[TCA_BPF_FD]; | |
266 | ||
267 | if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) || | |
268 | !tb[TCA_BPF_CLASSID]) | |
269 | return -EINVAL; | |
270 | ||
271 | tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE); | |
272 | ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr); | |
273 | if (ret < 0) | |
274 | return ret; | |
275 | ||
276 | classid = nla_get_u32(tb[TCA_BPF_CLASSID]); | |
277 | ||
278 | ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog, classid) : | |
279 | cls_bpf_prog_from_efd(tb, prog, classid); | |
280 | if (ret < 0) { | |
281 | tcf_exts_destroy(&exts); | |
282 | return ret; | |
283 | } | |
284 | ||
7d1d65cb DB |
285 | tcf_bind_filter(tp, &prog->res, base); |
286 | tcf_exts_change(tp, &prog->exts, &exts); | |
287 | ||
7d1d65cb | 288 | return 0; |
7d1d65cb DB |
289 | } |
290 | ||
291 | static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp, | |
292 | struct cls_bpf_head *head) | |
293 | { | |
294 | unsigned int i = 0x80000000; | |
3f2ab135 | 295 | u32 handle; |
7d1d65cb DB |
296 | |
297 | do { | |
298 | if (++head->hgen == 0x7FFFFFFF) | |
299 | head->hgen = 1; | |
300 | } while (--i > 0 && cls_bpf_get(tp, head->hgen)); | |
3f2ab135 DB |
301 | |
302 | if (unlikely(i == 0)) { | |
7d1d65cb | 303 | pr_err("Insufficient number of handles\n"); |
3f2ab135 DB |
304 | handle = 0; |
305 | } else { | |
306 | handle = head->hgen; | |
307 | } | |
7d1d65cb | 308 | |
3f2ab135 | 309 | return handle; |
7d1d65cb DB |
310 | } |
311 | ||
312 | static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, | |
313 | struct tcf_proto *tp, unsigned long base, | |
314 | u32 handle, struct nlattr **tca, | |
2f7ef2f8 | 315 | unsigned long *arg, bool ovr) |
7d1d65cb | 316 | { |
1f947bf1 JF |
317 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
318 | struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg; | |
7d1d65cb | 319 | struct nlattr *tb[TCA_BPF_MAX + 1]; |
1f947bf1 | 320 | struct cls_bpf_prog *prog; |
7d1d65cb DB |
321 | int ret; |
322 | ||
323 | if (tca[TCA_OPTIONS] == NULL) | |
324 | return -EINVAL; | |
325 | ||
326 | ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy); | |
327 | if (ret < 0) | |
328 | return ret; | |
329 | ||
7d1d65cb | 330 | prog = kzalloc(sizeof(*prog), GFP_KERNEL); |
1f947bf1 | 331 | if (!prog) |
7d1d65cb DB |
332 | return -ENOBUFS; |
333 | ||
5da57f42 | 334 | tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
1f947bf1 JF |
335 | |
336 | if (oldprog) { | |
337 | if (handle && oldprog->handle != handle) { | |
338 | ret = -EINVAL; | |
339 | goto errout; | |
340 | } | |
341 | } | |
342 | ||
7d1d65cb DB |
343 | if (handle == 0) |
344 | prog->handle = cls_bpf_grab_new_handle(tp, head); | |
345 | else | |
346 | prog->handle = handle; | |
347 | if (prog->handle == 0) { | |
348 | ret = -EINVAL; | |
349 | goto errout; | |
350 | } | |
351 | ||
2f7ef2f8 | 352 | ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr); |
7d1d65cb DB |
353 | if (ret < 0) |
354 | goto errout; | |
355 | ||
1f947bf1 JF |
356 | if (oldprog) { |
357 | list_replace_rcu(&prog->link, &oldprog->link); | |
18cdb37e | 358 | tcf_unbind_filter(tp, &oldprog->res); |
1f947bf1 JF |
359 | call_rcu(&oldprog->rcu, __cls_bpf_delete_prog); |
360 | } else { | |
361 | list_add_rcu(&prog->link, &head->plist); | |
362 | } | |
7d1d65cb DB |
363 | |
364 | *arg = (unsigned long) prog; | |
7d1d65cb DB |
365 | return 0; |
366 | errout: | |
1f947bf1 | 367 | kfree(prog); |
7d1d65cb DB |
368 | |
369 | return ret; | |
370 | } | |
371 | ||
e2e9b654 DB |
372 | static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog, |
373 | struct sk_buff *skb) | |
374 | { | |
375 | struct nlattr *nla; | |
376 | ||
377 | if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops)) | |
378 | return -EMSGSIZE; | |
379 | ||
380 | nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops * | |
381 | sizeof(struct sock_filter)); | |
382 | if (nla == NULL) | |
383 | return -EMSGSIZE; | |
384 | ||
385 | memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog, | |
391 | struct sk_buff *skb) | |
392 | { | |
393 | if (nla_put_u32(skb, TCA_BPF_FD, prog->bpf_fd)) | |
394 | return -EMSGSIZE; | |
395 | ||
396 | if (prog->bpf_name && | |
397 | nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name)) | |
398 | return -EMSGSIZE; | |
399 | ||
400 | return 0; | |
401 | } | |
402 | ||
832d1d5b | 403 | static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, |
7d1d65cb DB |
404 | struct sk_buff *skb, struct tcmsg *tm) |
405 | { | |
406 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh; | |
e2e9b654 DB |
407 | struct nlattr *nest; |
408 | int ret; | |
7d1d65cb DB |
409 | |
410 | if (prog == NULL) | |
411 | return skb->len; | |
412 | ||
413 | tm->tcm_handle = prog->handle; | |
414 | ||
415 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
416 | if (nest == NULL) | |
417 | goto nla_put_failure; | |
418 | ||
419 | if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) | |
420 | goto nla_put_failure; | |
7d1d65cb | 421 | |
e2e9b654 DB |
422 | if (cls_bpf_is_ebpf(prog)) |
423 | ret = cls_bpf_dump_ebpf_info(prog, skb); | |
424 | else | |
425 | ret = cls_bpf_dump_bpf_info(prog, skb); | |
426 | if (ret) | |
7d1d65cb DB |
427 | goto nla_put_failure; |
428 | ||
5da57f42 | 429 | if (tcf_exts_dump(skb, &prog->exts) < 0) |
7d1d65cb DB |
430 | goto nla_put_failure; |
431 | ||
432 | nla_nest_end(skb, nest); | |
433 | ||
5da57f42 | 434 | if (tcf_exts_dump_stats(skb, &prog->exts) < 0) |
7d1d65cb DB |
435 | goto nla_put_failure; |
436 | ||
437 | return skb->len; | |
438 | ||
439 | nla_put_failure: | |
440 | nla_nest_cancel(skb, nest); | |
441 | return -1; | |
442 | } | |
443 | ||
444 | static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg) | |
445 | { | |
1f947bf1 | 446 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
447 | struct cls_bpf_prog *prog; |
448 | ||
3fe6b49e | 449 | list_for_each_entry(prog, &head->plist, link) { |
7d1d65cb DB |
450 | if (arg->count < arg->skip) |
451 | goto skip; | |
452 | if (arg->fn(tp, (unsigned long) prog, arg) < 0) { | |
453 | arg->stop = 1; | |
454 | break; | |
455 | } | |
456 | skip: | |
457 | arg->count++; | |
458 | } | |
459 | } | |
460 | ||
461 | static struct tcf_proto_ops cls_bpf_ops __read_mostly = { | |
462 | .kind = "bpf", | |
463 | .owner = THIS_MODULE, | |
464 | .classify = cls_bpf_classify, | |
465 | .init = cls_bpf_init, | |
466 | .destroy = cls_bpf_destroy, | |
467 | .get = cls_bpf_get, | |
7d1d65cb DB |
468 | .change = cls_bpf_change, |
469 | .delete = cls_bpf_delete, | |
470 | .walk = cls_bpf_walk, | |
471 | .dump = cls_bpf_dump, | |
472 | }; | |
473 | ||
474 | static int __init cls_bpf_init_mod(void) | |
475 | { | |
476 | return register_tcf_proto_ops(&cls_bpf_ops); | |
477 | } | |
478 | ||
479 | static void __exit cls_bpf_exit_mod(void) | |
480 | { | |
481 | unregister_tcf_proto_ops(&cls_bpf_ops); | |
482 | } | |
483 | ||
484 | module_init(cls_bpf_init_mod); | |
485 | module_exit(cls_bpf_exit_mod); |