x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / net / sched / cls_fw.c
blob7f45e5ab8afcddcb95e4c9e4ead3ec5a9899d3ec
1 /*
2 * net/sched/cls_fw.c Classifier mapping ipchains' fwmark to traffic class.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 * Changes:
12 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one
13 * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel).
14 * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension
16 * JHS: We should remove the CONFIG_NET_CLS_IND from here
17 * eventually when the meta match extension is made available
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
27 #include <linux/skbuff.h>
28 #include <net/netlink.h>
29 #include <net/act_api.h>
30 #include <net/pkt_cls.h>
32 #define HTSIZE 256
34 struct fw_head {
35 u32 mask;
36 struct fw_filter __rcu *ht[HTSIZE];
37 struct rcu_head rcu;
40 struct fw_filter {
41 struct fw_filter __rcu *next;
42 u32 id;
43 struct tcf_result res;
44 #ifdef CONFIG_NET_CLS_IND
45 int ifindex;
46 #endif /* CONFIG_NET_CLS_IND */
47 struct tcf_exts exts;
48 struct tcf_proto *tp;
49 union {
50 struct work_struct work;
51 struct rcu_head rcu;
55 static u32 fw_hash(u32 handle)
57 handle ^= (handle >> 16);
58 handle ^= (handle >> 8);
59 return handle % HTSIZE;
62 static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp,
63 struct tcf_result *res)
65 struct fw_head *head = rcu_dereference_bh(tp->root);
66 struct fw_filter *f;
67 int r;
68 u32 id = skb->mark;
70 if (head != NULL) {
71 id &= head->mask;
73 for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f;
74 f = rcu_dereference_bh(f->next)) {
75 if (f->id == id) {
76 *res = f->res;
77 #ifdef CONFIG_NET_CLS_IND
78 if (!tcf_match_indev(skb, f->ifindex))
79 continue;
80 #endif /* CONFIG_NET_CLS_IND */
81 r = tcf_exts_exec(skb, &f->exts, res);
82 if (r < 0)
83 continue;
85 return r;
88 } else {
89 /* Old method: classify the packet using its skb mark. */
90 if (id && (TC_H_MAJ(id) == 0 ||
91 !(TC_H_MAJ(id ^ tp->q->handle)))) {
92 res->classid = id;
93 res->class = 0;
94 return 0;
98 return -1;
101 static void *fw_get(struct tcf_proto *tp, u32 handle)
103 struct fw_head *head = rtnl_dereference(tp->root);
104 struct fw_filter *f;
106 if (head == NULL)
107 return NULL;
109 f = rtnl_dereference(head->ht[fw_hash(handle)]);
110 for (; f; f = rtnl_dereference(f->next)) {
111 if (f->id == handle)
112 return f;
114 return NULL;
117 static int fw_init(struct tcf_proto *tp)
119 /* We don't allocate fw_head here, because in the old method
120 * we don't need it at all.
122 return 0;
125 static void __fw_delete_filter(struct fw_filter *f)
127 tcf_exts_destroy(&f->exts);
128 tcf_exts_put_net(&f->exts);
129 kfree(f);
132 static void fw_delete_filter_work(struct work_struct *work)
134 struct fw_filter *f = container_of(work, struct fw_filter, work);
136 rtnl_lock();
137 __fw_delete_filter(f);
138 rtnl_unlock();
141 static void fw_delete_filter(struct rcu_head *head)
143 struct fw_filter *f = container_of(head, struct fw_filter, rcu);
145 INIT_WORK(&f->work, fw_delete_filter_work);
146 tcf_queue_work(&f->work);
149 static void fw_destroy(struct tcf_proto *tp)
151 struct fw_head *head = rtnl_dereference(tp->root);
152 struct fw_filter *f;
153 int h;
155 if (head == NULL)
156 return;
158 for (h = 0; h < HTSIZE; h++) {
159 while ((f = rtnl_dereference(head->ht[h])) != NULL) {
160 RCU_INIT_POINTER(head->ht[h],
161 rtnl_dereference(f->next));
162 tcf_unbind_filter(tp, &f->res);
163 if (tcf_exts_get_net(&f->exts))
164 call_rcu(&f->rcu, fw_delete_filter);
165 else
166 __fw_delete_filter(f);
169 kfree_rcu(head, rcu);
172 static int fw_delete(struct tcf_proto *tp, void *arg, bool *last)
174 struct fw_head *head = rtnl_dereference(tp->root);
175 struct fw_filter *f = arg;
176 struct fw_filter __rcu **fp;
177 struct fw_filter *pfp;
178 int ret = -EINVAL;
179 int h;
181 if (head == NULL || f == NULL)
182 goto out;
184 fp = &head->ht[fw_hash(f->id)];
186 for (pfp = rtnl_dereference(*fp); pfp;
187 fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
188 if (pfp == f) {
189 RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
190 tcf_unbind_filter(tp, &f->res);
191 tcf_exts_get_net(&f->exts);
192 call_rcu(&f->rcu, fw_delete_filter);
193 ret = 0;
194 break;
198 *last = true;
199 for (h = 0; h < HTSIZE; h++) {
200 if (rcu_access_pointer(head->ht[h])) {
201 *last = false;
202 break;
206 out:
207 return ret;
210 static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = {
211 [TCA_FW_CLASSID] = { .type = NLA_U32 },
212 [TCA_FW_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
213 [TCA_FW_MASK] = { .type = NLA_U32 },
216 static int fw_set_parms(struct net *net, struct tcf_proto *tp,
217 struct fw_filter *f, struct nlattr **tb,
218 struct nlattr **tca, unsigned long base, bool ovr)
220 struct fw_head *head = rtnl_dereference(tp->root);
221 u32 mask;
222 int err;
224 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr);
225 if (err < 0)
226 return err;
228 if (tb[TCA_FW_CLASSID]) {
229 f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]);
230 tcf_bind_filter(tp, &f->res, base);
233 #ifdef CONFIG_NET_CLS_IND
234 if (tb[TCA_FW_INDEV]) {
235 int ret;
236 ret = tcf_change_indev(net, tb[TCA_FW_INDEV]);
237 if (ret < 0)
238 return ret;
239 f->ifindex = ret;
241 #endif /* CONFIG_NET_CLS_IND */
243 err = -EINVAL;
244 if (tb[TCA_FW_MASK]) {
245 mask = nla_get_u32(tb[TCA_FW_MASK]);
246 if (mask != head->mask)
247 return err;
248 } else if (head->mask != 0xFFFFFFFF)
249 return err;
251 return 0;
254 static int fw_change(struct net *net, struct sk_buff *in_skb,
255 struct tcf_proto *tp, unsigned long base,
256 u32 handle, struct nlattr **tca, void **arg,
257 bool ovr)
259 struct fw_head *head = rtnl_dereference(tp->root);
260 struct fw_filter *f = *arg;
261 struct nlattr *opt = tca[TCA_OPTIONS];
262 struct nlattr *tb[TCA_FW_MAX + 1];
263 int err;
265 if (!opt)
266 return handle ? -EINVAL : 0; /* Succeed if it is old method. */
268 err = nla_parse_nested(tb, TCA_FW_MAX, opt, fw_policy, NULL);
269 if (err < 0)
270 return err;
272 if (f) {
273 struct fw_filter *pfp, *fnew;
274 struct fw_filter __rcu **fp;
276 if (f->id != handle && handle)
277 return -EINVAL;
279 fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
280 if (!fnew)
281 return -ENOBUFS;
283 fnew->id = f->id;
284 fnew->res = f->res;
285 #ifdef CONFIG_NET_CLS_IND
286 fnew->ifindex = f->ifindex;
287 #endif /* CONFIG_NET_CLS_IND */
288 fnew->tp = f->tp;
290 err = tcf_exts_init(&fnew->exts, TCA_FW_ACT, TCA_FW_POLICE);
291 if (err < 0) {
292 kfree(fnew);
293 return err;
296 err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr);
297 if (err < 0) {
298 tcf_exts_destroy(&fnew->exts);
299 kfree(fnew);
300 return err;
303 fp = &head->ht[fw_hash(fnew->id)];
304 for (pfp = rtnl_dereference(*fp); pfp;
305 fp = &pfp->next, pfp = rtnl_dereference(*fp))
306 if (pfp == f)
307 break;
309 RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next));
310 rcu_assign_pointer(*fp, fnew);
311 tcf_unbind_filter(tp, &f->res);
312 tcf_exts_get_net(&f->exts);
313 call_rcu(&f->rcu, fw_delete_filter);
315 *arg = fnew;
316 return err;
319 if (!handle)
320 return -EINVAL;
322 if (!head) {
323 u32 mask = 0xFFFFFFFF;
324 if (tb[TCA_FW_MASK])
325 mask = nla_get_u32(tb[TCA_FW_MASK]);
327 head = kzalloc(sizeof(*head), GFP_KERNEL);
328 if (!head)
329 return -ENOBUFS;
330 head->mask = mask;
332 rcu_assign_pointer(tp->root, head);
335 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
336 if (f == NULL)
337 return -ENOBUFS;
339 err = tcf_exts_init(&f->exts, TCA_FW_ACT, TCA_FW_POLICE);
340 if (err < 0)
341 goto errout;
342 f->id = handle;
343 f->tp = tp;
345 err = fw_set_parms(net, tp, f, tb, tca, base, ovr);
346 if (err < 0)
347 goto errout;
349 RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]);
350 rcu_assign_pointer(head->ht[fw_hash(handle)], f);
352 *arg = f;
353 return 0;
355 errout:
356 tcf_exts_destroy(&f->exts);
357 kfree(f);
358 return err;
361 static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg)
363 struct fw_head *head = rtnl_dereference(tp->root);
364 int h;
366 if (head == NULL)
367 arg->stop = 1;
369 if (arg->stop)
370 return;
372 for (h = 0; h < HTSIZE; h++) {
373 struct fw_filter *f;
375 for (f = rtnl_dereference(head->ht[h]); f;
376 f = rtnl_dereference(f->next)) {
377 if (arg->count < arg->skip) {
378 arg->count++;
379 continue;
381 if (arg->fn(tp, f, arg) < 0) {
382 arg->stop = 1;
383 return;
385 arg->count++;
390 static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh,
391 struct sk_buff *skb, struct tcmsg *t)
393 struct fw_head *head = rtnl_dereference(tp->root);
394 struct fw_filter *f = fh;
395 struct nlattr *nest;
397 if (f == NULL)
398 return skb->len;
400 t->tcm_handle = f->id;
402 if (!f->res.classid && !tcf_exts_has_actions(&f->exts))
403 return skb->len;
405 nest = nla_nest_start(skb, TCA_OPTIONS);
406 if (nest == NULL)
407 goto nla_put_failure;
409 if (f->res.classid &&
410 nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid))
411 goto nla_put_failure;
412 #ifdef CONFIG_NET_CLS_IND
413 if (f->ifindex) {
414 struct net_device *dev;
415 dev = __dev_get_by_index(net, f->ifindex);
416 if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name))
417 goto nla_put_failure;
419 #endif /* CONFIG_NET_CLS_IND */
420 if (head->mask != 0xFFFFFFFF &&
421 nla_put_u32(skb, TCA_FW_MASK, head->mask))
422 goto nla_put_failure;
424 if (tcf_exts_dump(skb, &f->exts) < 0)
425 goto nla_put_failure;
427 nla_nest_end(skb, nest);
429 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
430 goto nla_put_failure;
432 return skb->len;
434 nla_put_failure:
435 nla_nest_cancel(skb, nest);
436 return -1;
439 static void fw_bind_class(void *fh, u32 classid, unsigned long cl)
441 struct fw_filter *f = fh;
443 if (f && f->res.classid == classid)
444 f->res.class = cl;
447 static struct tcf_proto_ops cls_fw_ops __read_mostly = {
448 .kind = "fw",
449 .classify = fw_classify,
450 .init = fw_init,
451 .destroy = fw_destroy,
452 .get = fw_get,
453 .change = fw_change,
454 .delete = fw_delete,
455 .walk = fw_walk,
456 .dump = fw_dump,
457 .bind_class = fw_bind_class,
458 .owner = THIS_MODULE,
461 static int __init init_fw(void)
463 return register_tcf_proto_ops(&cls_fw_ops);
466 static void __exit exit_fw(void)
468 unregister_tcf_proto_ops(&cls_fw_ops);
471 module_init(init_fw)
472 module_exit(exit_fw)
473 MODULE_LICENSE("GPL");