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"
33 struct route4_fastmap
{
34 struct route4_filter
*filter
;
40 struct route4_fastmap fastmap
[16];
41 struct route4_bucket
*table
[256 + 1];
44 struct route4_bucket
{
45 /* 16 FROM buckets + 16 IIF buckets + 1 wildcard bucket */
46 struct route4_filter
*ht
[16 + 16 + 1];
49 struct route4_filter
{
50 struct route4_filter
*next
;
54 struct tcf_result res
;
57 struct route4_bucket
*bkt
;
60 #define ROUTE4_FAILURE ((struct route4_filter *)(-1L))
62 static inline int route4_fastmap_hash(u32 id
, int iif
)
68 route4_reset_fastmap(struct Qdisc
*q
, struct route4_head
*head
, u32 id
)
70 spinlock_t
*root_lock
= qdisc_root_sleeping_lock(q
);
72 spin_lock_bh(root_lock
);
73 memset(head
->fastmap
, 0, sizeof(head
->fastmap
));
74 spin_unlock_bh(root_lock
);
78 route4_set_fastmap(struct route4_head
*head
, u32 id
, int iif
,
79 struct route4_filter
*f
)
81 int h
= route4_fastmap_hash(id
, iif
);
83 head
->fastmap
[h
].id
= id
;
84 head
->fastmap
[h
].iif
= iif
;
85 head
->fastmap
[h
].filter
= f
;
88 static inline int route4_hash_to(u32 id
)
93 static inline int route4_hash_from(u32 id
)
95 return (id
>> 16) & 0xF;
98 static inline int route4_hash_iif(int iif
)
100 return 16 + ((iif
>> 16) & 0xF);
103 static inline int route4_hash_wild(void)
108 #define ROUTE4_APPLY_RESULT() \
111 if (tcf_exts_is_available(&f->exts)) { \
112 int r = tcf_exts_exec(skb, &f->exts, res); \
118 } else if (!dont_cache) \
119 route4_set_fastmap(head, id, iif, f); \
123 static int route4_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
124 struct tcf_result
*res
)
126 struct route4_head
*head
= tp
->root
;
127 struct dst_entry
*dst
;
128 struct route4_bucket
*b
;
129 struct route4_filter
*f
;
131 int iif
, dont_cache
= 0;
143 h
= route4_fastmap_hash(id
, iif
);
144 if (id
== head
->fastmap
[h
].id
&&
145 iif
== head
->fastmap
[h
].iif
&&
146 (f
= head
->fastmap
[h
].filter
) != NULL
) {
147 if (f
== ROUTE4_FAILURE
)
154 h
= route4_hash_to(id
);
159 for (f
= b
->ht
[route4_hash_from(id
)]; f
; f
= f
->next
)
161 ROUTE4_APPLY_RESULT();
163 for (f
= b
->ht
[route4_hash_iif(iif
)]; f
; f
= f
->next
)
165 ROUTE4_APPLY_RESULT();
167 for (f
= b
->ht
[route4_hash_wild()]; f
; f
= f
->next
)
168 ROUTE4_APPLY_RESULT();
178 route4_set_fastmap(head
, id
, iif
, ROUTE4_FAILURE
);
183 if (id
&& (TC_H_MAJ(id
) == 0 ||
184 !(TC_H_MAJ(id
^tp
->q
->handle
)))) {
192 static inline u32
to_hash(u32 id
)
201 static inline u32
from_hash(u32 id
)
206 if (!(id
& 0x8000)) {
211 return 16 + (id
& 0xF);
214 static unsigned long route4_get(struct tcf_proto
*tp
, u32 handle
)
216 struct route4_head
*head
= tp
->root
;
217 struct route4_bucket
*b
;
218 struct route4_filter
*f
;
224 h1
= to_hash(handle
);
228 h2
= from_hash(handle
>> 16);
234 for (f
= b
->ht
[h2
]; f
; f
= f
->next
)
235 if (f
->handle
== handle
)
236 return (unsigned long)f
;
241 static void route4_put(struct tcf_proto
*tp
, unsigned long f
)
245 static int route4_init(struct tcf_proto
*tp
)
251 route4_delete_filter(struct tcf_proto
*tp
, struct route4_filter
*f
)
253 tcf_unbind_filter(tp
, &f
->res
);
254 tcf_exts_destroy(tp
, &f
->exts
);
258 static void route4_destroy(struct tcf_proto
*tp
)
260 struct route4_head
*head
= tp
->root
;
266 for (h1
= 0; h1
<= 256; h1
++) {
267 struct route4_bucket
*b
;
271 for (h2
= 0; h2
<= 32; h2
++) {
272 struct route4_filter
*f
;
274 while ((f
= b
->ht
[h2
]) != NULL
) {
276 route4_delete_filter(tp
, f
);
285 static int route4_delete(struct tcf_proto
*tp
, unsigned long arg
)
287 struct route4_head
*head
= tp
->root
;
288 struct route4_filter
**fp
, *f
= (struct route4_filter
*)arg
;
290 struct route4_bucket
*b
;
299 for (fp
= &b
->ht
[from_hash(h
>> 16)]; *fp
; fp
= &(*fp
)->next
) {
305 route4_reset_fastmap(tp
->q
, head
, f
->id
);
306 route4_delete_filter(tp
, f
);
310 for (i
= 0; i
<= 32; i
++)
314 /* OK, session has no flows */
316 head
->table
[to_hash(h
)] = NULL
;
326 static const struct nla_policy route4_policy
[TCA_ROUTE4_MAX
+ 1] = {
327 [TCA_ROUTE4_CLASSID
] = { .type
= NLA_U32
},
328 [TCA_ROUTE4_TO
] = { .type
= NLA_U32
},
329 [TCA_ROUTE4_FROM
] = { .type
= NLA_U32
},
330 [TCA_ROUTE4_IIF
] = { .type
= NLA_U32
},
333 static int route4_set_parms(struct net
*net
, struct tcf_proto
*tp
,
334 unsigned long base
, struct route4_filter
*f
,
335 u32 handle
, struct route4_head
*head
,
336 struct nlattr
**tb
, struct nlattr
*est
, int new,
340 u32 id
= 0, to
= 0, nhandle
= 0x8000;
341 struct route4_filter
*fp
;
343 struct route4_bucket
*b
;
346 tcf_exts_init(&e
, TCA_ROUTE4_ACT
, TCA_ROUTE4_POLICE
);
347 err
= tcf_exts_validate(net
, tp
, tb
, est
, &e
, ovr
);
352 if (tb
[TCA_ROUTE4_TO
]) {
353 if (new && handle
& 0x8000)
355 to
= nla_get_u32(tb
[TCA_ROUTE4_TO
]);
361 if (tb
[TCA_ROUTE4_FROM
]) {
362 if (tb
[TCA_ROUTE4_IIF
])
364 id
= nla_get_u32(tb
[TCA_ROUTE4_FROM
]);
368 } else if (tb
[TCA_ROUTE4_IIF
]) {
369 id
= nla_get_u32(tb
[TCA_ROUTE4_IIF
]);
372 nhandle
|= (id
| 0x8000) << 16;
374 nhandle
|= 0xFFFF << 16;
377 nhandle
|= handle
& 0x7F00;
378 if (nhandle
!= handle
)
382 h1
= to_hash(nhandle
);
386 b
= kzalloc(sizeof(struct route4_bucket
), GFP_KERNEL
);
394 unsigned int h2
= from_hash(nhandle
>> 16);
397 for (fp
= b
->ht
[h2
]; fp
; fp
= fp
->next
)
398 if (fp
->handle
== f
->handle
)
403 if (tb
[TCA_ROUTE4_TO
])
406 if (tb
[TCA_ROUTE4_FROM
])
408 else if (tb
[TCA_ROUTE4_IIF
])
415 if (tb
[TCA_ROUTE4_CLASSID
]) {
416 f
->res
.classid
= nla_get_u32(tb
[TCA_ROUTE4_CLASSID
]);
417 tcf_bind_filter(tp
, &f
->res
, base
);
420 tcf_exts_change(tp
, &f
->exts
, &e
);
424 tcf_exts_destroy(tp
, &e
);
428 static int route4_change(struct net
*net
, struct sk_buff
*in_skb
,
429 struct tcf_proto
*tp
, unsigned long base
,
432 unsigned long *arg
, bool ovr
)
434 struct route4_head
*head
= tp
->root
;
435 struct route4_filter
*f
, *f1
, **fp
;
436 struct route4_bucket
*b
;
437 struct nlattr
*opt
= tca
[TCA_OPTIONS
];
438 struct nlattr
*tb
[TCA_ROUTE4_MAX
+ 1];
444 return handle
? -EINVAL
: 0;
446 err
= nla_parse_nested(tb
, TCA_ROUTE4_MAX
, opt
, route4_policy
);
450 f
= (struct route4_filter
*)*arg
;
452 if (f
->handle
!= handle
&& handle
)
456 old_handle
= f
->handle
;
458 err
= route4_set_parms(net
, tp
, base
, f
, handle
, head
, tb
,
459 tca
[TCA_RATE
], 0, ovr
);
468 head
= kzalloc(sizeof(struct route4_head
), GFP_KERNEL
);
477 f
= kzalloc(sizeof(struct route4_filter
), GFP_KERNEL
);
481 tcf_exts_init(&f
->exts
, TCA_ROUTE4_ACT
, TCA_ROUTE4_POLICE
);
482 err
= route4_set_parms(net
, tp
, base
, f
, handle
, head
, tb
,
483 tca
[TCA_RATE
], 1, ovr
);
488 h
= from_hash(f
->handle
>> 16);
489 for (fp
= &f
->bkt
->ht
[h
]; (f1
= *fp
) != NULL
; fp
= &f1
->next
)
490 if (f
->handle
< f1
->handle
)
497 if (old_handle
&& f
->handle
!= old_handle
) {
498 th
= to_hash(old_handle
);
499 h
= from_hash(old_handle
>> 16);
502 for (fp
= &b
->ht
[h
]; *fp
; fp
= &(*fp
)->next
) {
512 route4_reset_fastmap(tp
->q
, head
, f
->id
);
513 *arg
= (unsigned long)f
;
521 static void route4_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
523 struct route4_head
*head
= tp
->root
;
532 for (h
= 0; h
<= 256; h
++) {
533 struct route4_bucket
*b
= head
->table
[h
];
536 for (h1
= 0; h1
<= 32; h1
++) {
537 struct route4_filter
*f
;
539 for (f
= b
->ht
[h1
]; f
; f
= f
->next
) {
540 if (arg
->count
< arg
->skip
) {
544 if (arg
->fn(tp
, (unsigned long)f
, arg
) < 0) {
555 static int route4_dump(struct net
*net
, struct tcf_proto
*tp
, unsigned long fh
,
556 struct sk_buff
*skb
, struct tcmsg
*t
)
558 struct route4_filter
*f
= (struct route4_filter
*)fh
;
559 unsigned char *b
= skb_tail_pointer(skb
);
566 t
->tcm_handle
= f
->handle
;
568 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
570 goto nla_put_failure
;
572 if (!(f
->handle
& 0x8000)) {
574 if (nla_put_u32(skb
, TCA_ROUTE4_TO
, id
))
575 goto nla_put_failure
;
577 if (f
->handle
& 0x80000000) {
578 if ((f
->handle
>> 16) != 0xFFFF &&
579 nla_put_u32(skb
, TCA_ROUTE4_IIF
, f
->iif
))
580 goto nla_put_failure
;
583 if (nla_put_u32(skb
, TCA_ROUTE4_FROM
, id
))
584 goto nla_put_failure
;
586 if (f
->res
.classid
&&
587 nla_put_u32(skb
, TCA_ROUTE4_CLASSID
, f
->res
.classid
))
588 goto nla_put_failure
;
590 if (tcf_exts_dump(skb
, &f
->exts
) < 0)
591 goto nla_put_failure
;
593 nla_nest_end(skb
, nest
);
595 if (tcf_exts_dump_stats(skb
, &f
->exts
) < 0)
596 goto nla_put_failure
;
605 static struct tcf_proto_ops cls_route4_ops __read_mostly
= {
607 .classify
= route4_classify
,
609 .destroy
= route4_destroy
,
612 .change
= route4_change
,
613 .delete = route4_delete
,
616 .owner
= THIS_MODULE
,
619 static int __init
init_route4(void)
621 return register_tcf_proto_ops(&cls_route4_ops
);
624 static void __exit
exit_route4(void)
626 unregister_tcf_proto_ops(&cls_route4_ops
);
629 module_init(init_route4
)
630 module_exit(exit_route4
)
631 MODULE_LICENSE("GPL");