]>
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> | |
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; | |
1f947bf1 | 30 | struct rcu_head rcu; |
7d1d65cb DB |
31 | }; |
32 | ||
33 | struct cls_bpf_prog { | |
7ae457c1 | 34 | struct bpf_prog *filter; |
7d1d65cb DB |
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; | |
1f947bf1 JF |
41 | struct tcf_proto *tp; |
42 | struct rcu_head rcu; | |
7d1d65cb DB |
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 | ||
7d1d65cb DB |
52 | static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
53 | struct tcf_result *res) | |
54 | { | |
80dcbd12 | 55 | struct cls_bpf_head *head = rcu_dereference_bh(tp->root); |
7d1d65cb DB |
56 | struct cls_bpf_prog *prog; |
57 | int ret; | |
58 | ||
1f947bf1 | 59 | list_for_each_entry_rcu(prog, &head->plist, link) { |
7ae457c1 | 60 | int filter_res = BPF_PROG_RUN(prog->filter, skb); |
7d1d65cb DB |
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 | ||
1f947bf1 JF |
87 | INIT_LIST_HEAD_RCU(&head->plist); |
88 | rcu_assign_pointer(tp->root, head); | |
7d1d65cb DB |
89 | |
90 | return 0; | |
91 | } | |
92 | ||
93 | static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog) | |
94 | { | |
18d0264f | 95 | tcf_exts_destroy(&prog->exts); |
7d1d65cb | 96 | |
7ae457c1 | 97 | bpf_prog_destroy(prog->filter); |
7d1d65cb DB |
98 | |
99 | kfree(prog->bpf_ops); | |
100 | kfree(prog); | |
101 | } | |
102 | ||
1f947bf1 JF |
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 | ||
7d1d65cb DB |
110 | static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg) |
111 | { | |
472f5837 | 112 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg; |
7d1d65cb | 113 | |
472f5837 JP |
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; | |
7d1d65cb DB |
118 | } |
119 | ||
120 | static void cls_bpf_destroy(struct tcf_proto *tp) | |
121 | { | |
1f947bf1 | 122 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
123 | struct cls_bpf_prog *prog, *tmp; |
124 | ||
125 | list_for_each_entry_safe(prog, tmp, &head->plist, link) { | |
1f947bf1 | 126 | list_del_rcu(&prog->link); |
18cdb37e | 127 | tcf_unbind_filter(tp, &prog->res); |
1f947bf1 | 128 | call_rcu(&prog->rcu, __cls_bpf_delete_prog); |
7d1d65cb DB |
129 | } |
130 | ||
1f947bf1 JF |
131 | RCU_INIT_POINTER(tp->root, NULL); |
132 | kfree_rcu(head, rcu); | |
7d1d65cb DB |
133 | } |
134 | ||
135 | static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle) | |
136 | { | |
1f947bf1 | 137 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
138 | struct cls_bpf_prog *prog; |
139 | unsigned long ret = 0UL; | |
140 | ||
141 | if (head == NULL) | |
142 | return 0UL; | |
143 | ||
1f947bf1 | 144 | list_for_each_entry_rcu(prog, &head->plist, link) { |
7d1d65cb DB |
145 | if (prog->handle == handle) { |
146 | ret = (unsigned long) prog; | |
147 | break; | |
148 | } | |
149 | } | |
150 | ||
151 | return ret; | |
152 | } | |
153 | ||
154 | static void cls_bpf_put(struct tcf_proto *tp, unsigned long f) | |
155 | { | |
156 | } | |
157 | ||
158 | static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp, | |
159 | struct cls_bpf_prog *prog, | |
160 | unsigned long base, struct nlattr **tb, | |
2f7ef2f8 | 161 | struct nlattr *est, bool ovr) |
7d1d65cb | 162 | { |
1f947bf1 | 163 | struct sock_filter *bpf_ops; |
7d1d65cb | 164 | struct tcf_exts exts; |
b1fcd35c | 165 | struct sock_fprog_kern tmp; |
1f947bf1 | 166 | struct bpf_prog *fp; |
7d1d65cb DB |
167 | u16 bpf_size, bpf_len; |
168 | u32 classid; | |
169 | int ret; | |
170 | ||
171 | if (!tb[TCA_BPF_OPS_LEN] || !tb[TCA_BPF_OPS] || !tb[TCA_BPF_CLASSID]) | |
172 | return -EINVAL; | |
173 | ||
5da57f42 | 174 | tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
2f7ef2f8 | 175 | ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr); |
7d1d65cb DB |
176 | if (ret < 0) |
177 | return ret; | |
178 | ||
179 | classid = nla_get_u32(tb[TCA_BPF_CLASSID]); | |
180 | bpf_len = nla_get_u16(tb[TCA_BPF_OPS_LEN]); | |
181 | if (bpf_len > BPF_MAXINSNS || bpf_len == 0) { | |
182 | ret = -EINVAL; | |
183 | goto errout; | |
184 | } | |
185 | ||
186 | bpf_size = bpf_len * sizeof(*bpf_ops); | |
187 | bpf_ops = kzalloc(bpf_size, GFP_KERNEL); | |
188 | if (bpf_ops == NULL) { | |
189 | ret = -ENOMEM; | |
190 | goto errout; | |
191 | } | |
192 | ||
193 | memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); | |
194 | ||
195 | tmp.len = bpf_len; | |
b1fcd35c | 196 | tmp.filter = bpf_ops; |
7d1d65cb | 197 | |
7ae457c1 | 198 | ret = bpf_prog_create(&fp, &tmp); |
7d1d65cb DB |
199 | if (ret) |
200 | goto errout_free; | |
201 | ||
7d1d65cb DB |
202 | prog->bpf_len = bpf_len; |
203 | prog->bpf_ops = bpf_ops; | |
204 | prog->filter = fp; | |
205 | prog->res.classid = classid; | |
7d1d65cb DB |
206 | |
207 | tcf_bind_filter(tp, &prog->res, base); | |
208 | tcf_exts_change(tp, &prog->exts, &exts); | |
209 | ||
7d1d65cb | 210 | return 0; |
7d1d65cb DB |
211 | errout_free: |
212 | kfree(bpf_ops); | |
213 | errout: | |
18d0264f | 214 | tcf_exts_destroy(&exts); |
7d1d65cb DB |
215 | return ret; |
216 | } | |
217 | ||
218 | static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp, | |
219 | struct cls_bpf_head *head) | |
220 | { | |
221 | unsigned int i = 0x80000000; | |
222 | ||
223 | do { | |
224 | if (++head->hgen == 0x7FFFFFFF) | |
225 | head->hgen = 1; | |
226 | } while (--i > 0 && cls_bpf_get(tp, head->hgen)); | |
227 | if (i == 0) | |
228 | pr_err("Insufficient number of handles\n"); | |
229 | ||
230 | return i; | |
231 | } | |
232 | ||
233 | static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, | |
234 | struct tcf_proto *tp, unsigned long base, | |
235 | u32 handle, struct nlattr **tca, | |
2f7ef2f8 | 236 | unsigned long *arg, bool ovr) |
7d1d65cb | 237 | { |
1f947bf1 JF |
238 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
239 | struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg; | |
7d1d65cb | 240 | struct nlattr *tb[TCA_BPF_MAX + 1]; |
1f947bf1 | 241 | struct cls_bpf_prog *prog; |
7d1d65cb DB |
242 | int ret; |
243 | ||
244 | if (tca[TCA_OPTIONS] == NULL) | |
245 | return -EINVAL; | |
246 | ||
247 | ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy); | |
248 | if (ret < 0) | |
249 | return ret; | |
250 | ||
7d1d65cb | 251 | prog = kzalloc(sizeof(*prog), GFP_KERNEL); |
1f947bf1 | 252 | if (!prog) |
7d1d65cb DB |
253 | return -ENOBUFS; |
254 | ||
5da57f42 | 255 | tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
1f947bf1 JF |
256 | |
257 | if (oldprog) { | |
258 | if (handle && oldprog->handle != handle) { | |
259 | ret = -EINVAL; | |
260 | goto errout; | |
261 | } | |
262 | } | |
263 | ||
7d1d65cb DB |
264 | if (handle == 0) |
265 | prog->handle = cls_bpf_grab_new_handle(tp, head); | |
266 | else | |
267 | prog->handle = handle; | |
268 | if (prog->handle == 0) { | |
269 | ret = -EINVAL; | |
270 | goto errout; | |
271 | } | |
272 | ||
2f7ef2f8 | 273 | ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr); |
7d1d65cb DB |
274 | if (ret < 0) |
275 | goto errout; | |
276 | ||
1f947bf1 JF |
277 | if (oldprog) { |
278 | list_replace_rcu(&prog->link, &oldprog->link); | |
18cdb37e | 279 | tcf_unbind_filter(tp, &oldprog->res); |
1f947bf1 JF |
280 | call_rcu(&oldprog->rcu, __cls_bpf_delete_prog); |
281 | } else { | |
282 | list_add_rcu(&prog->link, &head->plist); | |
283 | } | |
7d1d65cb DB |
284 | |
285 | *arg = (unsigned long) prog; | |
7d1d65cb DB |
286 | return 0; |
287 | errout: | |
1f947bf1 | 288 | kfree(prog); |
7d1d65cb DB |
289 | |
290 | return ret; | |
291 | } | |
292 | ||
832d1d5b | 293 | static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, |
7d1d65cb DB |
294 | struct sk_buff *skb, struct tcmsg *tm) |
295 | { | |
296 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh; | |
297 | struct nlattr *nest, *nla; | |
298 | ||
299 | if (prog == NULL) | |
300 | return skb->len; | |
301 | ||
302 | tm->tcm_handle = prog->handle; | |
303 | ||
304 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
305 | if (nest == NULL) | |
306 | goto nla_put_failure; | |
307 | ||
308 | if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) | |
309 | goto nla_put_failure; | |
310 | if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_len)) | |
311 | goto nla_put_failure; | |
312 | ||
313 | nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_len * | |
314 | sizeof(struct sock_filter)); | |
315 | if (nla == NULL) | |
316 | goto nla_put_failure; | |
317 | ||
1fab9abc | 318 | memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); |
7d1d65cb | 319 | |
5da57f42 | 320 | if (tcf_exts_dump(skb, &prog->exts) < 0) |
7d1d65cb DB |
321 | goto nla_put_failure; |
322 | ||
323 | nla_nest_end(skb, nest); | |
324 | ||
5da57f42 | 325 | if (tcf_exts_dump_stats(skb, &prog->exts) < 0) |
7d1d65cb DB |
326 | goto nla_put_failure; |
327 | ||
328 | return skb->len; | |
329 | ||
330 | nla_put_failure: | |
331 | nla_nest_cancel(skb, nest); | |
332 | return -1; | |
333 | } | |
334 | ||
335 | static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg) | |
336 | { | |
1f947bf1 | 337 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
7d1d65cb DB |
338 | struct cls_bpf_prog *prog; |
339 | ||
1f947bf1 | 340 | list_for_each_entry_rcu(prog, &head->plist, link) { |
7d1d65cb DB |
341 | if (arg->count < arg->skip) |
342 | goto skip; | |
343 | if (arg->fn(tp, (unsigned long) prog, arg) < 0) { | |
344 | arg->stop = 1; | |
345 | break; | |
346 | } | |
347 | skip: | |
348 | arg->count++; | |
349 | } | |
350 | } | |
351 | ||
352 | static struct tcf_proto_ops cls_bpf_ops __read_mostly = { | |
353 | .kind = "bpf", | |
354 | .owner = THIS_MODULE, | |
355 | .classify = cls_bpf_classify, | |
356 | .init = cls_bpf_init, | |
357 | .destroy = cls_bpf_destroy, | |
358 | .get = cls_bpf_get, | |
359 | .put = cls_bpf_put, | |
360 | .change = cls_bpf_change, | |
361 | .delete = cls_bpf_delete, | |
362 | .walk = cls_bpf_walk, | |
363 | .dump = cls_bpf_dump, | |
364 | }; | |
365 | ||
366 | static int __init cls_bpf_init_mod(void) | |
367 | { | |
368 | return register_tcf_proto_ops(&cls_bpf_ops); | |
369 | } | |
370 | ||
371 | static void __exit cls_bpf_exit_mod(void) | |
372 | { | |
373 | unregister_tcf_proto_ops(&cls_bpf_ops); | |
374 | } | |
375 | ||
376 | module_init(cls_bpf_init_mod); | |
377 | module_exit(cls_bpf_exit_mod); |