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/config.h>
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/bitops.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/if_ether.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/notifier.h>
33 #include <net/route.h>
34 #include <linux/skbuff.h>
36 #include <net/pkt_sched.h>
39 1. For now we assume that route tags < 256.
40 It allows to use direct table lookups, instead of hash tables.
41 2. For now we assume that "from TAG" and "fromdev DEV" statements
42 are mutually exclusive.
43 3. "to TAG from ANY" has higher priority, than "to ANY from XXX"
48 struct route4_filter *filter;
55 struct route4_fastmap fastmap[16];
56 struct route4_bucket *table[256+1];
61 struct route4_filter *ht[16+16+1];
66 struct route4_filter *next;
70 struct tcf_result res;
71 #ifdef CONFIG_NET_CLS_POLICE
72 struct tcf_police *police;
76 struct route4_bucket *bkt;
79 #define ROUTE4_FAILURE ((struct route4_filter*)(-1L))
81 static __inline__ int route4_fastmap_hash(u32 id, int iif)
86 static void route4_reset_fastmap(struct net_device *dev, struct route4_head *head, u32 id)
88 spin_lock_bh(&dev->queue_lock);
89 memset(head->fastmap, 0, sizeof(head->fastmap));
90 spin_unlock_bh(&dev->queue_lock);
93 static void __inline__
94 route4_set_fastmap(struct route4_head *head, u32 id, int iif,
95 struct route4_filter *f)
97 int h = route4_fastmap_hash(id, iif);
98 head->fastmap[h].id = id;
99 head->fastmap[h].iif = iif;
100 head->fastmap[h].filter = f;
103 static __inline__ int route4_hash_to(u32 id)
108 static __inline__ int route4_hash_from(u32 id)
113 static __inline__ int route4_hash_iif(int iif)
115 return 16 + ((iif>>16)&0xF);
118 static __inline__ int route4_hash_wild(void)
123 #ifdef CONFIG_NET_CLS_POLICE
124 #define IF_ROUTE_POLICE \
126 int pol_res = tcf_police(skb, f->police); \
127 if (pol_res >= 0) return pol_res; \
133 #define IF_ROUTE_POLICE
137 static int route4_classify(struct sk_buff *skb, struct tcf_proto *tp,
138 struct tcf_result *res)
140 struct route4_head *head = (struct route4_head*)tp->root;
141 struct dst_entry *dst;
142 struct route4_bucket *b;
143 struct route4_filter *f;
144 #ifdef CONFIG_NET_CLS_POLICE
150 if ((dst = skb->dst) == NULL)
157 iif = ((struct rtable*)dst)->fl.iif;
159 h = route4_fastmap_hash(id, iif);
160 if (id == head->fastmap[h].id &&
161 iif == head->fastmap[h].iif &&
162 (f = head->fastmap[h].filter) != NULL) {
163 if (f == ROUTE4_FAILURE)
170 h = route4_hash_to(id);
173 if ((b = head->table[h]) != NULL) {
174 f = b->ht[route4_hash_from(id)];
176 for ( ; f; f = f->next) {
179 IF_ROUTE_POLICE route4_set_fastmap(head, id, iif, f);
184 for (f = b->ht[route4_hash_iif(iif)]; f; f = f->next) {
187 IF_ROUTE_POLICE route4_set_fastmap(head, id, iif, f);
192 for (f = b->ht[route4_hash_wild()]; f; f = f->next) {
194 IF_ROUTE_POLICE route4_set_fastmap(head, id, iif, f);
205 #ifdef CONFIG_NET_CLS_POLICE
208 route4_set_fastmap(head, id, iif, ROUTE4_FAILURE);
213 if (id && (TC_H_MAJ(id) == 0 ||
214 !(TC_H_MAJ(id^tp->q->handle)))) {
222 static u32 to_hash(u32 id)
230 static u32 from_hash(u32 id)
235 if (!(id & 0x8000)) {
240 return 16 + (id&0xF);
243 static unsigned long route4_get(struct tcf_proto *tp, u32 handle)
245 struct route4_head *head = (struct route4_head*)tp->root;
246 struct route4_bucket *b;
247 struct route4_filter *f;
253 h1 = to_hash(handle);
257 h2 = from_hash(handle>>16);
261 if ((b = head->table[h1]) != NULL) {
262 for (f = b->ht[h2]; f; f = f->next)
263 if (f->handle == handle)
264 return (unsigned long)f;
269 static void route4_put(struct tcf_proto *tp, unsigned long f)
273 static int route4_init(struct tcf_proto *tp)
278 static void route4_destroy(struct tcf_proto *tp)
280 struct route4_head *head = xchg(&tp->root, NULL);
286 for (h1=0; h1<=256; h1++) {
287 struct route4_bucket *b;
289 if ((b = head->table[h1]) != NULL) {
290 for (h2=0; h2<=32; h2++) {
291 struct route4_filter *f;
293 while ((f = b->ht[h2]) != NULL) {
297 if ((cl = __cls_set_class(&f->res.class, 0)) != 0)
298 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
299 #ifdef CONFIG_NET_CLS_POLICE
300 tcf_police_release(f->police);
311 static int route4_delete(struct tcf_proto *tp, unsigned long arg)
313 struct route4_head *head = (struct route4_head*)tp->root;
314 struct route4_filter **fp, *f = (struct route4_filter*)arg;
316 struct route4_bucket *b;
325 for (fp = &b->ht[from_hash(h>>16)]; *fp; fp = &(*fp)->next) {
333 route4_reset_fastmap(tp->q->dev, head, f->id);
335 if ((cl = cls_set_class(tp, &f->res.class, 0)) != 0)
336 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
338 #ifdef CONFIG_NET_CLS_POLICE
339 tcf_police_release(f->police);
345 for (i=0; i<=32; i++)
349 /* OK, session has no flows */
351 head->table[to_hash(h)] = NULL;
361 static int route4_change(struct tcf_proto *tp, unsigned long base,
366 struct route4_head *head = tp->root;
367 struct route4_filter *f, *f1, **ins_f;
368 struct route4_bucket *b;
369 struct rtattr *opt = tca[TCA_OPTIONS-1];
370 struct rtattr *tb[TCA_ROUTE4_MAX];
375 return handle ? -EINVAL : 0;
377 if (rtattr_parse(tb, TCA_ROUTE4_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
380 if ((f = (struct route4_filter*)*arg) != NULL) {
381 /* Node exists: adjust only classid */
383 if (f->handle != handle && handle)
385 if (tb[TCA_ROUTE4_CLASSID-1]) {
388 f->res.classid = *(u32*)RTA_DATA(tb[TCA_ROUTE4_CLASSID-1]);
389 cl = cls_set_class(tp, &f->res.class, tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid));
391 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
393 #ifdef CONFIG_NET_CLS_POLICE
394 if (tb[TCA_ROUTE4_POLICE-1]) {
395 struct tcf_police *police = tcf_police_locate(tb[TCA_ROUTE4_POLICE-1], tca[TCA_RATE-1]);
398 police = xchg(&f->police, police);
401 tcf_police_release(police);
407 /* Now more serious part... */
410 head = kmalloc(sizeof(struct route4_head), GFP_KERNEL);
413 memset(head, 0, sizeof(struct route4_head));
420 f = kmalloc(sizeof(struct route4_filter), GFP_KERNEL);
424 memset(f, 0, sizeof(*f));
428 if (tb[TCA_ROUTE4_TO-1]) {
431 if (RTA_PAYLOAD(tb[TCA_ROUTE4_TO-1]) < 4)
433 f->id = *(u32*)RTA_DATA(tb[TCA_ROUTE4_TO-1]);
438 if (tb[TCA_ROUTE4_FROM-1]) {
440 if (tb[TCA_ROUTE4_IIF-1])
442 if (RTA_PAYLOAD(tb[TCA_ROUTE4_FROM-1]) < 4)
444 sid = (*(u32*)RTA_DATA(tb[TCA_ROUTE4_FROM-1]));
447 f->handle |= sid<<16;
449 } else if (tb[TCA_ROUTE4_IIF-1]) {
450 if (RTA_PAYLOAD(tb[TCA_ROUTE4_IIF-1]) < 4)
452 f->iif = *(u32*)RTA_DATA(tb[TCA_ROUTE4_IIF-1]);
455 f->handle |= (f->iif|0x8000)<<16;
457 f->handle |= 0xFFFF<<16;
460 f->handle |= handle&0x7F00;
461 if (f->handle != handle)
465 if (tb[TCA_ROUTE4_CLASSID-1]) {
466 if (RTA_PAYLOAD(tb[TCA_ROUTE4_CLASSID-1]) < 4)
468 f->res.classid = *(u32*)RTA_DATA(tb[TCA_ROUTE4_CLASSID-1]);
471 h1 = to_hash(f->handle);
472 if ((b = head->table[h1]) == NULL) {
474 b = kmalloc(sizeof(struct route4_bucket), GFP_KERNEL);
477 memset(b, 0, sizeof(*b));
486 h2 = from_hash(f->handle>>16);
487 for (ins_f = &b->ht[h2]; (f1=*ins_f) != NULL; ins_f = &f1->next) {
488 if (f->handle < f1->handle)
490 if (f1->handle == f->handle)
494 cls_set_class(tp, &f->res.class, tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid));
495 #ifdef CONFIG_NET_CLS_POLICE
496 if (tb[TCA_ROUTE4_POLICE-1])
497 f->police = tcf_police_locate(tb[TCA_ROUTE4_POLICE-1], tca[TCA_RATE-1]);
505 route4_reset_fastmap(tp->q->dev, head, f->id);
506 *arg = (unsigned long)f;
515 static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg)
517 struct route4_head *head = tp->root;
526 for (h = 0; h <= 256; h++) {
527 struct route4_bucket *b = head->table[h];
530 for (h1 = 0; h1 <= 32; h1++) {
531 struct route4_filter *f;
533 for (f = b->ht[h1]; f; f = f->next) {
534 if (arg->count < arg->skip) {
538 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
549 static int route4_dump(struct tcf_proto *tp, unsigned long fh,
550 struct sk_buff *skb, struct tcmsg *t)
552 struct route4_filter *f = (struct route4_filter*)fh;
553 unsigned char *b = skb->tail;
560 t->tcm_handle = f->handle;
562 rta = (struct rtattr*)b;
563 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
565 if (!(f->handle&0x8000)) {
567 RTA_PUT(skb, TCA_ROUTE4_TO, sizeof(id), &id);
569 if (f->handle&0x80000000) {
570 if ((f->handle>>16) != 0xFFFF)
571 RTA_PUT(skb, TCA_ROUTE4_IIF, sizeof(f->iif), &f->iif);
574 RTA_PUT(skb, TCA_ROUTE4_FROM, sizeof(id), &id);
577 RTA_PUT(skb, TCA_ROUTE4_CLASSID, 4, &f->res.classid);
578 #ifdef CONFIG_NET_CLS_POLICE
580 struct rtattr * p_rta = (struct rtattr*)skb->tail;
582 RTA_PUT(skb, TCA_ROUTE4_POLICE, 0, NULL);
584 if (tcf_police_dump(skb, f->police) < 0)
587 p_rta->rta_len = skb->tail - (u8*)p_rta;
591 rta->rta_len = skb->tail - b;
592 #ifdef CONFIG_NET_CLS_POLICE
594 if (qdisc_copy_stats(skb, &f->police->stats))
601 skb_trim(skb, b - skb->data);
605 static struct tcf_proto_ops cls_route4_ops = {
608 .classify = route4_classify,
610 .destroy = route4_destroy,
613 .change = route4_change,
614 .delete = route4_delete,
617 .owner = THIS_MODULE,
620 static int __init init_route4(void)
622 return register_tcf_proto_ops(&cls_route4_ops);
625 static void __exit exit_route4(void)
627 unregister_tcf_proto_ops(&cls_route4_ops);
630 module_init(init_route4)
631 module_exit(exit_route4)
632 MODULE_LICENSE("GPL");