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(struct route4_filter
*f
)
262 tcf_exts_destroy(&f
->exts
);
263 tcf_exts_put_net(&f
->exts
);
267 static void route4_delete_filter_work(struct work_struct
*work
)
269 struct route4_filter
*f
= container_of(work
, struct route4_filter
, work
);
272 __route4_delete_filter(f
);
276 static void route4_delete_filter(struct rcu_head
*head
)
278 struct route4_filter
*f
= container_of(head
, struct route4_filter
, rcu
);
280 INIT_WORK(&f
->work
, route4_delete_filter_work
);
281 tcf_queue_work(&f
->work
);
284 static void route4_destroy(struct tcf_proto
*tp
, struct netlink_ext_ack
*extack
)
286 struct route4_head
*head
= rtnl_dereference(tp
->root
);
292 for (h1
= 0; h1
<= 256; h1
++) {
293 struct route4_bucket
*b
;
295 b
= rtnl_dereference(head
->table
[h1
]);
297 for (h2
= 0; h2
<= 32; h2
++) {
298 struct route4_filter
*f
;
300 while ((f
= rtnl_dereference(b
->ht
[h2
])) != NULL
) {
301 struct route4_filter
*next
;
303 next
= rtnl_dereference(f
->next
);
304 RCU_INIT_POINTER(b
->ht
[h2
], next
);
305 tcf_unbind_filter(tp
, &f
->res
);
306 if (tcf_exts_get_net(&f
->exts
))
307 call_rcu(&f
->rcu
, route4_delete_filter
);
309 __route4_delete_filter(f
);
312 RCU_INIT_POINTER(head
->table
[h1
], NULL
);
316 kfree_rcu(head
, rcu
);
319 static int route4_delete(struct tcf_proto
*tp
, void *arg
, bool *last
,
320 struct netlink_ext_ack
*extack
)
322 struct route4_head
*head
= rtnl_dereference(tp
->root
);
323 struct route4_filter
*f
= arg
;
324 struct route4_filter __rcu
**fp
;
325 struct route4_filter
*nf
;
326 struct route4_bucket
*b
;
336 fp
= &b
->ht
[from_hash(h
>> 16)];
337 for (nf
= rtnl_dereference(*fp
); nf
;
338 fp
= &nf
->next
, nf
= rtnl_dereference(*fp
)) {
341 RCU_INIT_POINTER(*fp
, rtnl_dereference(f
->next
));
343 /* Remove any fastmap lookups that might ref filter
344 * notice we unlink'd the filter so we can't get it
345 * back in the fastmap.
347 route4_reset_fastmap(head
);
350 tcf_unbind_filter(tp
, &f
->res
);
351 tcf_exts_get_net(&f
->exts
);
352 call_rcu(&f
->rcu
, route4_delete_filter
);
354 /* Strip RTNL protected tree */
355 for (i
= 0; i
<= 32; i
++) {
356 struct route4_filter
*rt
;
358 rt
= rtnl_dereference(b
->ht
[i
]);
363 /* OK, session has no flows */
364 RCU_INIT_POINTER(head
->table
[to_hash(h
)], NULL
);
372 for (h1
= 0; h1
<= 256; h1
++) {
373 if (rcu_access_pointer(head
->table
[h1
])) {
382 static const struct nla_policy route4_policy
[TCA_ROUTE4_MAX
+ 1] = {
383 [TCA_ROUTE4_CLASSID
] = { .type
= NLA_U32
},
384 [TCA_ROUTE4_TO
] = { .type
= NLA_U32
},
385 [TCA_ROUTE4_FROM
] = { .type
= NLA_U32
},
386 [TCA_ROUTE4_IIF
] = { .type
= NLA_U32
},
389 static int route4_set_parms(struct net
*net
, struct tcf_proto
*tp
,
390 unsigned long base
, struct route4_filter
*f
,
391 u32 handle
, struct route4_head
*head
,
392 struct nlattr
**tb
, struct nlattr
*est
, int new,
393 bool ovr
, struct netlink_ext_ack
*extack
)
395 u32 id
= 0, to
= 0, nhandle
= 0x8000;
396 struct route4_filter
*fp
;
398 struct route4_bucket
*b
;
401 err
= tcf_exts_validate(net
, tp
, tb
, est
, &f
->exts
, ovr
, extack
);
405 if (tb
[TCA_ROUTE4_TO
]) {
406 if (new && handle
& 0x8000)
408 to
= nla_get_u32(tb
[TCA_ROUTE4_TO
]);
414 if (tb
[TCA_ROUTE4_FROM
]) {
415 if (tb
[TCA_ROUTE4_IIF
])
417 id
= nla_get_u32(tb
[TCA_ROUTE4_FROM
]);
421 } else if (tb
[TCA_ROUTE4_IIF
]) {
422 id
= nla_get_u32(tb
[TCA_ROUTE4_IIF
]);
425 nhandle
|= (id
| 0x8000) << 16;
427 nhandle
|= 0xFFFF << 16;
430 nhandle
|= handle
& 0x7F00;
431 if (nhandle
!= handle
)
435 h1
= to_hash(nhandle
);
436 b
= rtnl_dereference(head
->table
[h1
]);
438 b
= kzalloc(sizeof(struct route4_bucket
), GFP_KERNEL
);
442 rcu_assign_pointer(head
->table
[h1
], b
);
444 unsigned int h2
= from_hash(nhandle
>> 16);
446 for (fp
= rtnl_dereference(b
->ht
[h2
]);
448 fp
= rtnl_dereference(fp
->next
))
449 if (fp
->handle
== f
->handle
)
453 if (tb
[TCA_ROUTE4_TO
])
456 if (tb
[TCA_ROUTE4_FROM
])
458 else if (tb
[TCA_ROUTE4_IIF
])
465 if (tb
[TCA_ROUTE4_CLASSID
]) {
466 f
->res
.classid
= nla_get_u32(tb
[TCA_ROUTE4_CLASSID
]);
467 tcf_bind_filter(tp
, &f
->res
, base
);
473 static int route4_change(struct net
*net
, struct sk_buff
*in_skb
,
474 struct tcf_proto
*tp
, unsigned long base
, u32 handle
,
475 struct nlattr
**tca
, void **arg
, bool ovr
,
476 struct netlink_ext_ack
*extack
)
478 struct route4_head
*head
= rtnl_dereference(tp
->root
);
479 struct route4_filter __rcu
**fp
;
480 struct route4_filter
*fold
, *f1
, *pfp
, *f
= NULL
;
481 struct route4_bucket
*b
;
482 struct nlattr
*opt
= tca
[TCA_OPTIONS
];
483 struct nlattr
*tb
[TCA_ROUTE4_MAX
+ 1];
489 return handle
? -EINVAL
: 0;
491 err
= nla_parse_nested(tb
, TCA_ROUTE4_MAX
, opt
, route4_policy
, NULL
);
496 if (fold
&& handle
&& fold
->handle
!= handle
)
500 f
= kzalloc(sizeof(struct route4_filter
), GFP_KERNEL
);
504 err
= tcf_exts_init(&f
->exts
, TCA_ROUTE4_ACT
, TCA_ROUTE4_POLICE
);
512 f
->handle
= fold
->handle
;
519 err
= route4_set_parms(net
, tp
, base
, f
, handle
, head
, tb
,
520 tca
[TCA_RATE
], new, ovr
, extack
);
524 h
= from_hash(f
->handle
>> 16);
526 for (pfp
= rtnl_dereference(*fp
);
527 (f1
= rtnl_dereference(*fp
)) != NULL
;
529 if (f
->handle
< f1
->handle
)
532 tcf_block_netif_keep_dst(tp
->chain
->block
);
533 rcu_assign_pointer(f
->next
, f1
);
534 rcu_assign_pointer(*fp
, f
);
536 if (fold
&& fold
->handle
&& f
->handle
!= fold
->handle
) {
537 th
= to_hash(fold
->handle
);
538 h
= from_hash(fold
->handle
>> 16);
539 b
= rtnl_dereference(head
->table
[th
]);
542 for (pfp
= rtnl_dereference(*fp
); pfp
;
543 fp
= &pfp
->next
, pfp
= rtnl_dereference(*fp
)) {
552 route4_reset_fastmap(head
);
555 tcf_unbind_filter(tp
, &fold
->res
);
556 tcf_exts_get_net(&fold
->exts
);
557 call_rcu(&fold
->rcu
, route4_delete_filter
);
563 tcf_exts_destroy(&f
->exts
);
568 static void route4_walk(struct tcf_proto
*tp
, struct tcf_walker
*arg
)
570 struct route4_head
*head
= rtnl_dereference(tp
->root
);
579 for (h
= 0; h
<= 256; h
++) {
580 struct route4_bucket
*b
= rtnl_dereference(head
->table
[h
]);
583 for (h1
= 0; h1
<= 32; h1
++) {
584 struct route4_filter
*f
;
586 for (f
= rtnl_dereference(b
->ht
[h1
]);
588 f
= rtnl_dereference(f
->next
)) {
589 if (arg
->count
< arg
->skip
) {
593 if (arg
->fn(tp
, f
, arg
) < 0) {
604 static int route4_dump(struct net
*net
, struct tcf_proto
*tp
, void *fh
,
605 struct sk_buff
*skb
, struct tcmsg
*t
)
607 struct route4_filter
*f
= fh
;
614 t
->tcm_handle
= f
->handle
;
616 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
618 goto nla_put_failure
;
620 if (!(f
->handle
& 0x8000)) {
622 if (nla_put_u32(skb
, TCA_ROUTE4_TO
, id
))
623 goto nla_put_failure
;
625 if (f
->handle
& 0x80000000) {
626 if ((f
->handle
>> 16) != 0xFFFF &&
627 nla_put_u32(skb
, TCA_ROUTE4_IIF
, f
->iif
))
628 goto nla_put_failure
;
631 if (nla_put_u32(skb
, TCA_ROUTE4_FROM
, id
))
632 goto nla_put_failure
;
634 if (f
->res
.classid
&&
635 nla_put_u32(skb
, TCA_ROUTE4_CLASSID
, f
->res
.classid
))
636 goto nla_put_failure
;
638 if (tcf_exts_dump(skb
, &f
->exts
) < 0)
639 goto nla_put_failure
;
641 nla_nest_end(skb
, nest
);
643 if (tcf_exts_dump_stats(skb
, &f
->exts
) < 0)
644 goto nla_put_failure
;
649 nla_nest_cancel(skb
, nest
);
653 static void route4_bind_class(void *fh
, u32 classid
, unsigned long cl
)
655 struct route4_filter
*f
= fh
;
657 if (f
&& f
->res
.classid
== classid
)
661 static struct tcf_proto_ops cls_route4_ops __read_mostly
= {
663 .classify
= route4_classify
,
665 .destroy
= route4_destroy
,
667 .change
= route4_change
,
668 .delete = route4_delete
,
671 .bind_class
= route4_bind_class
,
672 .owner
= THIS_MODULE
,
675 static int __init
init_route4(void)
677 return register_tcf_proto_ops(&cls_route4_ops
);
680 static void __exit
exit_route4(void)
682 unregister_tcf_proto_ops(&cls_route4_ops
);
685 module_init(init_route4
)
686 module_exit(exit_route4
)
687 MODULE_LICENSE("GPL");