2 * net/sched/cls_api.c Packet classifier API.
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>
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/skbuff.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/netlink.h>
26 #include <net/netlink.h>
27 #include <net/pkt_sched.h>
28 #include <net/pkt_cls.h>
31 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
33 #define DPRINTK(format,args...)
36 /* The list of all installed classifier types */
38 static struct tcf_proto_ops
*tcf_proto_base
;
40 /* Protects list of registered TC modules. It is pure SMP lock. */
41 static DEFINE_RWLOCK(cls_mod_lock
);
43 /* Find classifier type by string name */
45 static struct tcf_proto_ops
* tcf_proto_lookup_ops(struct rtattr
*kind
)
47 struct tcf_proto_ops
*t
= NULL
;
50 read_lock(&cls_mod_lock
);
51 for (t
= tcf_proto_base
; t
; t
= t
->next
) {
52 if (rtattr_strcmp(kind
, t
->kind
) == 0) {
53 if (!try_module_get(t
->owner
))
58 read_unlock(&cls_mod_lock
);
63 /* Register(unregister) new classifier type */
65 int register_tcf_proto_ops(struct tcf_proto_ops
*ops
)
67 struct tcf_proto_ops
*t
, **tp
;
70 write_lock(&cls_mod_lock
);
71 for (tp
= &tcf_proto_base
; (t
= *tp
) != NULL
; tp
= &t
->next
)
72 if (!strcmp(ops
->kind
, t
->kind
))
79 write_unlock(&cls_mod_lock
);
83 int unregister_tcf_proto_ops(struct tcf_proto_ops
*ops
)
85 struct tcf_proto_ops
*t
, **tp
;
88 write_lock(&cls_mod_lock
);
89 for (tp
= &tcf_proto_base
; (t
=*tp
) != NULL
; tp
= &t
->next
)
98 write_unlock(&cls_mod_lock
);
102 static int tfilter_notify(struct sk_buff
*oskb
, struct nlmsghdr
*n
,
103 struct tcf_proto
*tp
, unsigned long fh
, int event
);
106 /* Select new prio value from the range, managed by kernel. */
108 static __inline__ u32
tcf_auto_prio(struct tcf_proto
*tp
)
110 u32 first
= TC_H_MAKE(0xC0000000U
,0U);
118 /* Add/change/delete/get a filter node */
120 static int tc_ctl_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
, void *arg
)
128 struct net_device
*dev
;
130 struct tcf_proto
**back
, **chain
;
131 struct tcf_proto
*tp
;
132 struct tcf_proto_ops
*tp_ops
;
133 struct Qdisc_class_ops
*cops
;
141 protocol
= TC_H_MIN(t
->tcm_info
);
142 prio
= TC_H_MAJ(t
->tcm_info
);
144 parent
= t
->tcm_parent
;
148 /* If no priority is given, user wants we allocated it. */
149 if (n
->nlmsg_type
!= RTM_NEWTFILTER
|| !(n
->nlmsg_flags
&NLM_F_CREATE
))
151 prio
= TC_H_MAKE(0x80000000U
,0U);
154 /* Find head of filter chain. */
157 if ((dev
= __dev_get_by_index(t
->tcm_ifindex
)) == NULL
)
162 q
= dev
->qdisc_sleeping
;
164 } else if ((q
= qdisc_lookup(dev
, TC_H_MAJ(t
->tcm_parent
))) == NULL
)
167 /* Is it classful? */
168 if ((cops
= q
->ops
->cl_ops
) == NULL
)
171 /* Do we search for filter, attached to class? */
172 if (TC_H_MIN(parent
)) {
173 cl
= cops
->get(q
, parent
);
178 /* And the last stroke */
179 chain
= cops
->tcf_chain(q
, cl
);
184 /* Check the chain for existence of proto-tcf with this priority */
185 for (back
= chain
; (tp
=*back
) != NULL
; back
= &tp
->next
) {
186 if (tp
->prio
>= prio
) {
187 if (tp
->prio
== prio
) {
188 if (!nprio
|| (tp
->protocol
!= protocol
&& protocol
))
197 /* Proto-tcf does not exist, create new one */
199 if (tca
[TCA_KIND
-1] == NULL
|| !protocol
)
203 if (n
->nlmsg_type
!= RTM_NEWTFILTER
|| !(n
->nlmsg_flags
&NLM_F_CREATE
))
207 /* Create new proto tcf */
210 if ((tp
= kzalloc(sizeof(*tp
), GFP_KERNEL
)) == NULL
)
213 tp_ops
= tcf_proto_lookup_ops(tca
[TCA_KIND
-1]);
214 if (tp_ops
== NULL
) {
216 struct rtattr
*kind
= tca
[TCA_KIND
-1];
220 rtattr_strlcpy(name
, kind
, IFNAMSIZ
) < IFNAMSIZ
) {
222 request_module("cls_%s", name
);
224 tp_ops
= tcf_proto_lookup_ops(kind
);
225 /* We dropped the RTNL semaphore in order to
226 * perform the module load. So, even if we
227 * succeeded in loading the module we have to
228 * replay the request. We indicate this using
231 if (tp_ops
!= NULL
) {
232 module_put(tp_ops
->owner
);
241 tp
->protocol
= protocol
;
242 tp
->prio
= nprio
? : tcf_auto_prio(*back
);
244 tp
->classify
= tp_ops
->classify
;
245 tp
->classid
= parent
;
246 if ((err
= tp_ops
->init(tp
)) != 0) {
247 module_put(tp_ops
->owner
);
252 qdisc_lock_tree(dev
);
255 qdisc_unlock_tree(dev
);
257 } else if (tca
[TCA_KIND
-1] && rtattr_strcmp(tca
[TCA_KIND
-1], tp
->ops
->kind
))
260 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
263 if (n
->nlmsg_type
== RTM_DELTFILTER
&& t
->tcm_handle
== 0) {
264 qdisc_lock_tree(dev
);
266 qdisc_unlock_tree(dev
);
268 tfilter_notify(skb
, n
, tp
, fh
, RTM_DELTFILTER
);
275 if (n
->nlmsg_type
!= RTM_NEWTFILTER
|| !(n
->nlmsg_flags
&NLM_F_CREATE
))
278 switch (n
->nlmsg_type
) {
281 if (n
->nlmsg_flags
&NLM_F_EXCL
)
285 err
= tp
->ops
->delete(tp
, fh
);
287 tfilter_notify(skb
, n
, tp
, fh
, RTM_DELTFILTER
);
290 err
= tfilter_notify(skb
, n
, tp
, fh
, RTM_NEWTFILTER
);
298 err
= tp
->ops
->change(tp
, cl
, t
->tcm_handle
, tca
, &fh
);
300 tfilter_notify(skb
, n
, tp
, fh
, RTM_NEWTFILTER
);
306 /* Replay the request. */
312 tcf_fill_node(struct sk_buff
*skb
, struct tcf_proto
*tp
, unsigned long fh
,
313 u32 pid
, u32 seq
, u16 flags
, int event
)
316 struct nlmsghdr
*nlh
;
317 unsigned char *b
= skb_tail_pointer(skb
);
319 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*tcm
), flags
);
320 tcm
= NLMSG_DATA(nlh
);
321 tcm
->tcm_family
= AF_UNSPEC
;
324 tcm
->tcm_ifindex
= tp
->q
->dev
->ifindex
;
325 tcm
->tcm_parent
= tp
->classid
;
326 tcm
->tcm_info
= TC_H_MAKE(tp
->prio
, tp
->protocol
);
327 RTA_PUT(skb
, TCA_KIND
, IFNAMSIZ
, tp
->ops
->kind
);
328 tcm
->tcm_handle
= fh
;
329 if (RTM_DELTFILTER
!= event
) {
331 if (tp
->ops
->dump
&& tp
->ops
->dump(tp
, fh
, skb
, tcm
) < 0)
334 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
343 static int tfilter_notify(struct sk_buff
*oskb
, struct nlmsghdr
*n
,
344 struct tcf_proto
*tp
, unsigned long fh
, int event
)
347 u32 pid
= oskb
? NETLINK_CB(oskb
).pid
: 0;
349 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
353 if (tcf_fill_node(skb
, tp
, fh
, pid
, n
->nlmsg_seq
, 0, event
) <= 0) {
358 return rtnetlink_send(skb
, pid
, RTNLGRP_TC
, n
->nlmsg_flags
&NLM_F_ECHO
);
365 struct netlink_callback
*cb
;
368 static int tcf_node_dump(struct tcf_proto
*tp
, unsigned long n
, struct tcf_walker
*arg
)
370 struct tcf_dump_args
*a
= (void*)arg
;
372 return tcf_fill_node(a
->skb
, tp
, n
, NETLINK_CB(a
->cb
->skb
).pid
,
373 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, RTM_NEWTFILTER
);
376 static int tc_dump_tfilter(struct sk_buff
*skb
, struct netlink_callback
*cb
)
380 struct net_device
*dev
;
382 struct tcf_proto
*tp
, **chain
;
383 struct tcmsg
*tcm
= (struct tcmsg
*)NLMSG_DATA(cb
->nlh
);
384 unsigned long cl
= 0;
385 struct Qdisc_class_ops
*cops
;
386 struct tcf_dump_args arg
;
388 if (cb
->nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(*tcm
)))
390 if ((dev
= dev_get_by_index(tcm
->tcm_ifindex
)) == NULL
)
393 if (!tcm
->tcm_parent
)
394 q
= dev
->qdisc_sleeping
;
396 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
399 if ((cops
= q
->ops
->cl_ops
) == NULL
)
401 if (TC_H_MIN(tcm
->tcm_parent
)) {
402 cl
= cops
->get(q
, tcm
->tcm_parent
);
406 chain
= cops
->tcf_chain(q
, cl
);
412 for (tp
=*chain
, t
=0; tp
; tp
= tp
->next
, t
++) {
413 if (t
< s_t
) continue;
414 if (TC_H_MAJ(tcm
->tcm_info
) &&
415 TC_H_MAJ(tcm
->tcm_info
) != tp
->prio
)
417 if (TC_H_MIN(tcm
->tcm_info
) &&
418 TC_H_MIN(tcm
->tcm_info
) != tp
->protocol
)
421 memset(&cb
->args
[1], 0, sizeof(cb
->args
)-sizeof(cb
->args
[0]));
422 if (cb
->args
[1] == 0) {
423 if (tcf_fill_node(skb
, tp
, 0, NETLINK_CB(cb
->skb
).pid
,
424 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, RTM_NEWTFILTER
) <= 0) {
429 if (tp
->ops
->walk
== NULL
)
431 arg
.w
.fn
= tcf_node_dump
;
435 arg
.w
.skip
= cb
->args
[1]-1;
437 tp
->ops
->walk(tp
, &arg
.w
);
438 cb
->args
[1] = arg
.w
.count
+1;
454 tcf_exts_destroy(struct tcf_proto
*tp
, struct tcf_exts
*exts
)
456 #ifdef CONFIG_NET_CLS_ACT
458 tcf_action_destroy(exts
->action
, TCA_ACT_UNBIND
);
466 tcf_exts_validate(struct tcf_proto
*tp
, struct rtattr
**tb
,
467 struct rtattr
*rate_tlv
, struct tcf_exts
*exts
,
468 struct tcf_ext_map
*map
)
470 memset(exts
, 0, sizeof(*exts
));
472 #ifdef CONFIG_NET_CLS_ACT
475 struct tc_action
*act
;
477 if (map
->police
&& tb
[map
->police
-1]) {
478 act
= tcf_action_init_1(tb
[map
->police
-1], rate_tlv
, "police",
479 TCA_ACT_NOREPLACE
, TCA_ACT_BIND
, &err
);
483 act
->type
= TCA_OLD_COMPAT
;
485 } else if (map
->action
&& tb
[map
->action
-1]) {
486 act
= tcf_action_init(tb
[map
->action
-1], rate_tlv
, NULL
,
487 TCA_ACT_NOREPLACE
, TCA_ACT_BIND
, &err
);
495 if ((map
->action
&& tb
[map
->action
-1]) ||
496 (map
->police
&& tb
[map
->police
-1]))
504 tcf_exts_change(struct tcf_proto
*tp
, struct tcf_exts
*dst
,
505 struct tcf_exts
*src
)
507 #ifdef CONFIG_NET_CLS_ACT
509 struct tc_action
*act
;
511 act
= xchg(&dst
->action
, src
->action
);
514 tcf_action_destroy(act
, TCA_ACT_UNBIND
);
520 tcf_exts_dump(struct sk_buff
*skb
, struct tcf_exts
*exts
,
521 struct tcf_ext_map
*map
)
523 #ifdef CONFIG_NET_CLS_ACT
524 if (map
->action
&& exts
->action
) {
526 * again for backward compatible mode - we want
527 * to work with both old and new modes of entering
528 * tc data even if iproute2 was newer - jhs
530 struct rtattr
*p_rta
= (struct rtattr
*)skb_tail_pointer(skb
);
532 if (exts
->action
->type
!= TCA_OLD_COMPAT
) {
533 RTA_PUT(skb
, map
->action
, 0, NULL
);
534 if (tcf_action_dump(skb
, exts
->action
, 0, 0) < 0)
536 p_rta
->rta_len
= skb_tail_pointer(skb
) - (u8
*)p_rta
;
537 } else if (map
->police
) {
538 RTA_PUT(skb
, map
->police
, 0, NULL
);
539 if (tcf_action_dump_old(skb
, exts
->action
, 0, 0) < 0)
541 p_rta
->rta_len
= skb_tail_pointer(skb
) - (u8
*)p_rta
;
546 rtattr_failure
: __attribute__ ((unused
))
551 tcf_exts_dump_stats(struct sk_buff
*skb
, struct tcf_exts
*exts
,
552 struct tcf_ext_map
*map
)
554 #ifdef CONFIG_NET_CLS_ACT
556 if (tcf_action_copy_stats(skb
, exts
->action
, 1) < 0)
560 rtattr_failure
: __attribute__ ((unused
))
564 static int __init
tc_filter_init(void)
566 rtnl_register(PF_UNSPEC
, RTM_NEWTFILTER
, tc_ctl_tfilter
, NULL
);
567 rtnl_register(PF_UNSPEC
, RTM_DELTFILTER
, tc_ctl_tfilter
, NULL
);
568 rtnl_register(PF_UNSPEC
, RTM_GETTFILTER
, tc_ctl_tfilter
,
574 subsys_initcall(tc_filter_init
);
576 EXPORT_SYMBOL(register_tcf_proto_ops
);
577 EXPORT_SYMBOL(unregister_tcf_proto_ops
);
578 EXPORT_SYMBOL(tcf_exts_validate
);
579 EXPORT_SYMBOL(tcf_exts_destroy
);
580 EXPORT_SYMBOL(tcf_exts_change
);
581 EXPORT_SYMBOL(tcf_exts_dump
);
582 EXPORT_SYMBOL(tcf_exts_dump_stats
);