2 * net/sched/cls_route.c ROUTE4 classifier.
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>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/skbuff.h>
20 #include <net/route.h>
21 #include <net/netlink.h>
22 #include <net/act_api.h>
23 #include <net/pkt_cls.h>
26 * 1. For now we assume that route tags < 256.
27 * It allows to use direct table lookups, instead of hash tables.
28 * 2. For now we assume that "from TAG" and "fromdev DEV" statements
29 * are mutually exclusive.
30 * 3. "to TAG from ANY" has higher priority, than "to ANY from XXX"
32 struct route4_fastmap {
33 struct route4_filter *filter;
39 struct route4_fastmap fastmap[16];
40 struct route4_bucket __rcu *table[256 + 1];
44 struct route4_bucket {
45 /* 16 FROM buckets + 16 IIF buckets + 1 wildcard bucket */
46 struct route4_filter __rcu *ht[16 + 16 + 1];
50 struct route4_filter {
51 struct route4_filter __rcu *next;
55 struct tcf_result res;
58 struct route4_bucket *bkt;
61 struct work_struct work;
66 #define ROUTE4_FAILURE ((struct route4_filter *)(-1L))
68 static inline int route4_fastmap_hash(u32 id, int iif)
73 static DEFINE_SPINLOCK(fastmap_lock);
75 route4_reset_fastmap(struct route4_head *head)
77 spin_lock_bh(&fastmap_lock);
78 memset(head->fastmap, 0, sizeof(head->fastmap));
79 spin_unlock_bh(&fastmap_lock);
83 route4_set_fastmap(struct route4_head *head, u32 id, int iif,
84 struct route4_filter *f)
86 int h = route4_fastmap_hash(id, iif);
88 /* fastmap updates must look atomic to aling id, iff, filter */
89 spin_lock_bh(&fastmap_lock);
90 head->fastmap[h].id = id;
91 head->fastmap[h].iif = iif;
92 head->fastmap[h].filter = f;
93 spin_unlock_bh(&fastmap_lock);
96 static inline int route4_hash_to(u32 id)
101 static inline int route4_hash_from(u32 id)
103 return (id >> 16) & 0xF;
106 static inline int route4_hash_iif(int iif)
108 return 16 + ((iif >> 16) & 0xF);
111 static inline int route4_hash_wild(void)
116 #define ROUTE4_APPLY_RESULT() \
119 if (tcf_exts_has_actions(&f->exts)) { \
120 int r = tcf_exts_exec(skb, &f->exts, res); \
126 } else if (!dont_cache) \
127 route4_set_fastmap(head, id, iif, f); \
131 static int route4_classify(struct sk_buff *skb, const struct tcf_proto *tp,
132 struct tcf_result *res)
134 struct route4_head *head = rcu_dereference_bh(tp->root);
135 struct dst_entry *dst;
136 struct route4_bucket *b;
137 struct route4_filter *f;
139 int iif, dont_cache = 0;
149 h = route4_fastmap_hash(id, iif);
151 spin_lock(&fastmap_lock);
152 if (id == head->fastmap[h].id &&
153 iif == head->fastmap[h].iif &&
154 (f = head->fastmap[h].filter) != NULL) {
155 if (f == ROUTE4_FAILURE) {
156 spin_unlock(&fastmap_lock);
161 spin_unlock(&fastmap_lock);
164 spin_unlock(&fastmap_lock);
166 h = route4_hash_to(id);
169 b = rcu_dereference_bh(head->table[h]);
171 for (f = rcu_dereference_bh(b->ht[route4_hash_from(id)]);
173 f = rcu_dereference_bh(f->next))
175 ROUTE4_APPLY_RESULT();
177 for (f = rcu_dereference_bh(b->ht[route4_hash_iif(iif)]);
179 f = rcu_dereference_bh(f->next))
181 ROUTE4_APPLY_RESULT();
183 for (f = rcu_dereference_bh(b->ht[route4_hash_wild()]);
185 f = rcu_dereference_bh(f->next))
186 ROUTE4_APPLY_RESULT();
195 route4_set_fastmap(head, id, iif, ROUTE4_FAILURE);
200 static inline u32 to_hash(u32 id)
209 static inline u32 from_hash(u32 id)
214 if (!(id & 0x8000)) {
219 return 16 + (id & 0xF);
222 static void *route4_get(struct tcf_proto *tp, u32 handle)
224 struct route4_head *head = rtnl_dereference(tp->root);
225 struct route4_bucket *b;
226 struct route4_filter *f;
229 h1 = to_hash(handle);
233 h2 = from_hash(handle >> 16);
237 b = rtnl_dereference(head->table[h1]);
239 for (f = rtnl_dereference(b->ht[h2]);
241 f = rtnl_dereference(f->next))
242 if (f->handle == handle)
248 static int route4_init(struct tcf_proto *tp)
250 struct route4_head *head;
252 head = kzalloc(sizeof(struct route4_head), GFP_KERNEL);
256 rcu_assign_pointer(tp->root, head);
260 static void route4_delete_filter_work(struct work_struct *work)
262 struct route4_filter *f = container_of(work, struct route4_filter, work);
265 tcf_exts_destroy(&f->exts);
270 static void route4_delete_filter(struct rcu_head *head)
272 struct route4_filter *f = container_of(head, struct route4_filter, rcu);
274 INIT_WORK(&f->work, route4_delete_filter_work);
275 tcf_queue_work(&f->work);
278 static void route4_destroy(struct tcf_proto *tp)
280 struct route4_head *head = rtnl_dereference(tp->root);
286 for (h1 = 0; h1 <= 256; h1++) {
287 struct route4_bucket *b;
289 b = rtnl_dereference(head->table[h1]);
291 for (h2 = 0; h2 <= 32; h2++) {
292 struct route4_filter *f;
294 while ((f = rtnl_dereference(b->ht[h2])) != NULL) {
295 struct route4_filter *next;
297 next = rtnl_dereference(f->next);
298 RCU_INIT_POINTER(b->ht[h2], next);
299 tcf_unbind_filter(tp, &f->res);
300 call_rcu(&f->rcu, route4_delete_filter);
303 RCU_INIT_POINTER(head->table[h1], NULL);
307 kfree_rcu(head, rcu);
310 static int route4_delete(struct tcf_proto *tp, void *arg, bool *last)
312 struct route4_head *head = rtnl_dereference(tp->root);
313 struct route4_filter *f = arg;
314 struct route4_filter __rcu **fp;
315 struct route4_filter *nf;
316 struct route4_bucket *b;
326 fp = &b->ht[from_hash(h >> 16)];
327 for (nf = rtnl_dereference(*fp); nf;
328 fp = &nf->next, nf = rtnl_dereference(*fp)) {
331 RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
333 /* Remove any fastmap lookups that might ref filter
334 * notice we unlink'd the filter so we can't get it
335 * back in the fastmap.
337 route4_reset_fastmap(head);
340 tcf_unbind_filter(tp, &f->res);
341 call_rcu(&f->rcu, route4_delete_filter);
343 /* Strip RTNL protected tree */
344 for (i = 0; i <= 32; i++) {
345 struct route4_filter *rt;
347 rt = rtnl_dereference(b->ht[i]);
352 /* OK, session has no flows */
353 RCU_INIT_POINTER(head->table[to_hash(h)], NULL);
361 for (h1 = 0; h1 <= 256; h1++) {
362 if (rcu_access_pointer(head->table[h1])) {
371 static const struct nla_policy route4_policy[TCA_ROUTE4_MAX + 1] = {
372 [TCA_ROUTE4_CLASSID] = { .type = NLA_U32 },
373 [TCA_ROUTE4_TO] = { .type = NLA_U32 },
374 [TCA_ROUTE4_FROM] = { .type = NLA_U32 },
375 [TCA_ROUTE4_IIF] = { .type = NLA_U32 },
378 static int route4_set_parms(struct net *net, struct tcf_proto *tp,
379 unsigned long base, struct route4_filter *f,
380 u32 handle, struct route4_head *head,
381 struct nlattr **tb, struct nlattr *est, int new,
384 u32 id = 0, to = 0, nhandle = 0x8000;
385 struct route4_filter *fp;
387 struct route4_bucket *b;
390 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
394 if (tb[TCA_ROUTE4_TO]) {
395 if (new && handle & 0x8000)
397 to = nla_get_u32(tb[TCA_ROUTE4_TO]);
403 if (tb[TCA_ROUTE4_FROM]) {
404 if (tb[TCA_ROUTE4_IIF])
406 id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
410 } else if (tb[TCA_ROUTE4_IIF]) {
411 id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
414 nhandle |= (id | 0x8000) << 16;
416 nhandle |= 0xFFFF << 16;
419 nhandle |= handle & 0x7F00;
420 if (nhandle != handle)
424 h1 = to_hash(nhandle);
425 b = rtnl_dereference(head->table[h1]);
427 b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
431 rcu_assign_pointer(head->table[h1], b);
433 unsigned int h2 = from_hash(nhandle >> 16);
435 for (fp = rtnl_dereference(b->ht[h2]);
437 fp = rtnl_dereference(fp->next))
438 if (fp->handle == f->handle)
442 if (tb[TCA_ROUTE4_TO])
445 if (tb[TCA_ROUTE4_FROM])
447 else if (tb[TCA_ROUTE4_IIF])
454 if (tb[TCA_ROUTE4_CLASSID]) {
455 f->res.classid = nla_get_u32(tb[TCA_ROUTE4_CLASSID]);
456 tcf_bind_filter(tp, &f->res, base);
462 static int route4_change(struct net *net, struct sk_buff *in_skb,
463 struct tcf_proto *tp, unsigned long base, u32 handle,
464 struct nlattr **tca, void **arg, bool ovr)
466 struct route4_head *head = rtnl_dereference(tp->root);
467 struct route4_filter __rcu **fp;
468 struct route4_filter *fold, *f1, *pfp, *f = NULL;
469 struct route4_bucket *b;
470 struct nlattr *opt = tca[TCA_OPTIONS];
471 struct nlattr *tb[TCA_ROUTE4_MAX + 1];
477 return handle ? -EINVAL : 0;
479 err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy, NULL);
484 if (fold && handle && fold->handle != handle)
488 f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
492 err = tcf_exts_init(&f->exts, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
500 f->handle = fold->handle;
507 err = route4_set_parms(net, tp, base, f, handle, head, tb,
508 tca[TCA_RATE], new, ovr);
512 h = from_hash(f->handle >> 16);
514 for (pfp = rtnl_dereference(*fp);
515 (f1 = rtnl_dereference(*fp)) != NULL;
517 if (f->handle < f1->handle)
520 netif_keep_dst(qdisc_dev(tp->q));
521 rcu_assign_pointer(f->next, f1);
522 rcu_assign_pointer(*fp, f);
524 if (fold && fold->handle && f->handle != fold->handle) {
525 th = to_hash(fold->handle);
526 h = from_hash(fold->handle >> 16);
527 b = rtnl_dereference(head->table[th]);
530 for (pfp = rtnl_dereference(*fp); pfp;
531 fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
540 route4_reset_fastmap(head);
543 tcf_unbind_filter(tp, &fold->res);
544 call_rcu(&fold->rcu, route4_delete_filter);
550 tcf_exts_destroy(&f->exts);
555 static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg)
557 struct route4_head *head = rtnl_dereference(tp->root);
566 for (h = 0; h <= 256; h++) {
567 struct route4_bucket *b = rtnl_dereference(head->table[h]);
570 for (h1 = 0; h1 <= 32; h1++) {
571 struct route4_filter *f;
573 for (f = rtnl_dereference(b->ht[h1]);
575 f = rtnl_dereference(f->next)) {
576 if (arg->count < arg->skip) {
580 if (arg->fn(tp, f, arg) < 0) {
591 static int route4_dump(struct net *net, struct tcf_proto *tp, void *fh,
592 struct sk_buff *skb, struct tcmsg *t)
594 struct route4_filter *f = fh;
601 t->tcm_handle = f->handle;
603 nest = nla_nest_start(skb, TCA_OPTIONS);
605 goto nla_put_failure;
607 if (!(f->handle & 0x8000)) {
609 if (nla_put_u32(skb, TCA_ROUTE4_TO, id))
610 goto nla_put_failure;
612 if (f->handle & 0x80000000) {
613 if ((f->handle >> 16) != 0xFFFF &&
614 nla_put_u32(skb, TCA_ROUTE4_IIF, f->iif))
615 goto nla_put_failure;
618 if (nla_put_u32(skb, TCA_ROUTE4_FROM, id))
619 goto nla_put_failure;
621 if (f->res.classid &&
622 nla_put_u32(skb, TCA_ROUTE4_CLASSID, f->res.classid))
623 goto nla_put_failure;
625 if (tcf_exts_dump(skb, &f->exts) < 0)
626 goto nla_put_failure;
628 nla_nest_end(skb, nest);
630 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
631 goto nla_put_failure;
636 nla_nest_cancel(skb, nest);
640 static void route4_bind_class(void *fh, u32 classid, unsigned long cl)
642 struct route4_filter *f = fh;
644 if (f && f->res.classid == classid)
648 static struct tcf_proto_ops cls_route4_ops __read_mostly = {
650 .classify = route4_classify,
652 .destroy = route4_destroy,
654 .change = route4_change,
655 .delete = route4_delete,
658 .bind_class = route4_bind_class,
659 .owner = THIS_MODULE,
662 static int __init init_route4(void)
664 return register_tcf_proto_ops(&cls_route4_ops);
667 static void __exit exit_route4(void)
669 unregister_tcf_proto_ops(&cls_route4_ops);
672 module_init(init_route4)
673 module_exit(exit_route4)
674 MODULE_LICENSE("GPL");