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 <asm/uaccess.h>
18 #include <asm/system.h>
19 #include <asm/bitops.h>
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/string.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
30 #include <linux/errno.h>
31 #include <linux/interrupt.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/init.h>
36 #include <linux/kmod.h>
38 #include <net/pkt_sched.h>
41 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
43 #define DPRINTK(format,args...)
46 /* The list of all installed classifier types */
48 static struct tcf_proto_ops
*tcf_proto_base
;
50 /* Protects list of registered TC modules. It is pure SMP lock. */
51 static rwlock_t cls_mod_lock
= RW_LOCK_UNLOCKED
;
53 /* Find classifier type by string name */
55 struct tcf_proto_ops
* tcf_proto_lookup_ops(struct rtattr
*kind
)
57 struct tcf_proto_ops
*t
= NULL
;
60 read_lock(&cls_mod_lock
);
61 for (t
= tcf_proto_base
; t
; t
= t
->next
) {
62 if (rtattr_strcmp(kind
, t
->kind
) == 0)
65 read_unlock(&cls_mod_lock
);
70 /* Register(unregister) new classifier type */
72 int register_tcf_proto_ops(struct tcf_proto_ops
*ops
)
74 struct tcf_proto_ops
*t
, **tp
;
77 write_lock(&cls_mod_lock
);
78 for (tp
= &tcf_proto_base
; (t
= *tp
) != NULL
; tp
= &t
->next
)
79 if (!strcmp(ops
->kind
, t
->kind
))
86 write_unlock(&cls_mod_lock
);
90 int unregister_tcf_proto_ops(struct tcf_proto_ops
*ops
)
92 struct tcf_proto_ops
*t
, **tp
;
95 write_lock(&cls_mod_lock
);
96 for (tp
= &tcf_proto_base
; (t
=*tp
) != NULL
; tp
= &t
->next
)
105 write_unlock(&cls_mod_lock
);
109 static int tfilter_notify(struct sk_buff
*oskb
, struct nlmsghdr
*n
,
110 struct tcf_proto
*tp
, unsigned long fh
, int event
);
113 /* Select new prio value from the range, managed by kernel. */
115 static __inline__ u32
tcf_auto_prio(struct tcf_proto
*tp
)
117 u32 first
= TC_H_MAKE(0xC0000000U
,0U);
125 /* Add/change/delete/get a filter node */
127 static int tc_ctl_tfilter(struct sk_buff
*skb
, struct nlmsghdr
*n
, void *arg
)
129 struct rtattr
**tca
= arg
;
130 struct tcmsg
*t
= NLMSG_DATA(n
);
131 u32 protocol
= TC_H_MIN(t
->tcm_info
);
132 u32 prio
= TC_H_MAJ(t
->tcm_info
);
134 u32 parent
= t
->tcm_parent
;
135 struct net_device
*dev
;
137 struct tcf_proto
**back
, **chain
;
138 struct tcf_proto
*tp
= NULL
;
139 struct tcf_proto_ops
*tp_ops
;
140 struct Qdisc_class_ops
*cops
;
141 unsigned long cl
= 0;
146 /* If no priority is given, user wants we allocated it. */
147 if (n
->nlmsg_type
!= RTM_NEWTFILTER
|| !(n
->nlmsg_flags
&NLM_F_CREATE
))
149 prio
= TC_H_MAKE(0x80000000U
,0U);
152 /* Find head of filter chain. */
155 if ((dev
= __dev_get_by_index(t
->tcm_ifindex
)) == NULL
)
160 q
= dev
->qdisc_sleeping
;
162 } else if ((q
= qdisc_lookup(dev
, TC_H_MAJ(t
->tcm_parent
))) == NULL
)
165 /* Is it classful? */
166 if ((cops
= q
->ops
->cl_ops
) == NULL
)
169 /* Do we search for filter, attached to class? */
170 if (TC_H_MIN(parent
)) {
171 cl
= cops
->get(q
, parent
);
176 /* And the last stroke */
177 chain
= cops
->tcf_chain(q
, cl
);
182 /* Check the chain for existence of proto-tcf with this priority */
183 for (back
= chain
; (tp
=*back
) != NULL
; back
= &tp
->next
) {
184 if (tp
->prio
>= prio
) {
185 if (tp
->prio
== prio
) {
186 if (!nprio
|| (tp
->protocol
!= protocol
&& protocol
))
195 /* Proto-tcf does not exist, create new one */
197 if (tca
[TCA_KIND
-1] == NULL
|| !protocol
)
201 if (n
->nlmsg_type
!= RTM_NEWTFILTER
|| !(n
->nlmsg_flags
&NLM_F_CREATE
))
205 /* Create new proto tcf */
208 if ((tp
= kmalloc(sizeof(*tp
), GFP_KERNEL
)) == NULL
)
210 tp_ops
= tcf_proto_lookup_ops(tca
[TCA_KIND
-1]);
212 if (tp_ops
==NULL
&& tca
[TCA_KIND
-1] != NULL
) {
213 struct rtattr
*kind
= tca
[TCA_KIND
-1];
215 if (RTA_PAYLOAD(kind
) <= IFNAMSIZ
) {
216 request_module("cls_%s", (char*)RTA_DATA(kind
));
217 tp_ops
= tcf_proto_lookup_ops(kind
);
221 if (tp_ops
== NULL
) {
226 memset(tp
, 0, sizeof(*tp
));
228 tp
->protocol
= protocol
;
229 tp
->prio
= nprio
? : tcf_auto_prio(*back
);
231 tp
->classify
= tp_ops
->classify
;
232 tp
->classid
= parent
;
234 if (!try_module_get(tp_ops
->owner
)) {
238 if ((err
= tp_ops
->init(tp
)) != 0) {
239 module_put(tp_ops
->owner
);
244 qdisc_lock_tree(dev
);
247 qdisc_unlock_tree(dev
);
249 } else if (tca
[TCA_KIND
-1] && rtattr_strcmp(tca
[TCA_KIND
-1], tp
->ops
->kind
))
252 fh
= tp
->ops
->get(tp
, t
->tcm_handle
);
255 if (n
->nlmsg_type
== RTM_DELTFILTER
&& t
->tcm_handle
== 0) {
256 qdisc_lock_tree(dev
);
258 qdisc_unlock_tree(dev
);
260 tfilter_notify(skb
, n
, tp
, fh
, RTM_DELTFILTER
);
267 if (n
->nlmsg_type
!= RTM_NEWTFILTER
|| !(n
->nlmsg_flags
&NLM_F_CREATE
))
270 switch (n
->nlmsg_type
) {
273 if (n
->nlmsg_flags
&NLM_F_EXCL
)
277 err
= tp
->ops
->delete(tp
, fh
);
279 tfilter_notify(skb
, n
, tp
, fh
, RTM_DELTFILTER
);
282 err
= tfilter_notify(skb
, n
, tp
, fh
, RTM_NEWTFILTER
);
290 err
= tp
->ops
->change(tp
, cl
, t
->tcm_handle
, tca
, &fh
);
292 tfilter_notify(skb
, n
, tp
, fh
, RTM_NEWTFILTER
);
300 unsigned long tcf_set_class(struct tcf_proto
*tp
, unsigned long *clp
,
303 unsigned long old_cl
;
306 old_cl
= __cls_set_class(clp
, cl
);
314 tcf_fill_node(struct sk_buff
*skb
, struct tcf_proto
*tp
, unsigned long fh
,
315 u32 pid
, u32 seq
, unsigned flags
, int event
)
318 struct nlmsghdr
*nlh
;
319 unsigned char *b
= skb
->tail
;
321 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(*tcm
));
322 nlh
->nlmsg_flags
= flags
;
323 tcm
= NLMSG_DATA(nlh
);
324 tcm
->tcm_family
= AF_UNSPEC
;
325 tcm
->tcm_ifindex
= tp
->q
->dev
->ifindex
;
326 tcm
->tcm_parent
= tp
->classid
;
327 tcm
->tcm_info
= TC_H_MAKE(tp
->prio
, tp
->protocol
);
328 RTA_PUT(skb
, TCA_KIND
, IFNAMSIZ
, tp
->ops
->kind
);
329 tcm
->tcm_handle
= fh
;
330 if (RTM_DELTFILTER
!= event
) {
332 if (tp
->ops
->dump
&& tp
->ops
->dump(tp
, fh
, skb
, tcm
) < 0)
335 nlh
->nlmsg_len
= skb
->tail
- b
;
340 skb_trim(skb
, b
- skb
->data
);
344 static int tfilter_notify(struct sk_buff
*oskb
, struct nlmsghdr
*n
,
345 struct tcf_proto
*tp
, unsigned long fh
, int event
)
348 u32 pid
= oskb
? NETLINK_CB(oskb
).pid
: 0;
350 skb
= alloc_skb(NLMSG_GOODSIZE
, GFP_KERNEL
);
354 if (tcf_fill_node(skb
, tp
, fh
, pid
, n
->nlmsg_seq
, 0, event
) <= 0) {
359 return rtnetlink_send(skb
, pid
, RTMGRP_TC
, n
->nlmsg_flags
&NLM_F_ECHO
);
366 struct netlink_callback
*cb
;
369 static int tcf_node_dump(struct tcf_proto
*tp
, unsigned long n
, struct tcf_walker
*arg
)
371 struct tcf_dump_args
*a
= (void*)arg
;
373 return tcf_fill_node(a
->skb
, tp
, n
, NETLINK_CB(a
->cb
->skb
).pid
,
374 a
->cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, RTM_NEWTFILTER
);
377 static int tc_dump_tfilter(struct sk_buff
*skb
, struct netlink_callback
*cb
)
381 struct net_device
*dev
;
383 struct tcf_proto
*tp
, **chain
;
384 struct tcmsg
*tcm
= (struct tcmsg
*)NLMSG_DATA(cb
->nlh
);
385 unsigned long cl
= 0;
386 struct Qdisc_class_ops
*cops
;
387 struct tcf_dump_args arg
;
389 if (cb
->nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(*tcm
)))
391 if ((dev
= dev_get_by_index(tcm
->tcm_ifindex
)) == NULL
)
394 read_lock_bh(&qdisc_tree_lock
);
395 if (!tcm
->tcm_parent
)
396 q
= dev
->qdisc_sleeping
;
398 q
= qdisc_lookup(dev
, TC_H_MAJ(tcm
->tcm_parent
));
401 if ((cops
= q
->ops
->cl_ops
) == NULL
)
403 if (TC_H_MIN(tcm
->tcm_parent
)) {
404 cl
= cops
->get(q
, tcm
->tcm_parent
);
408 chain
= cops
->tcf_chain(q
, cl
);
414 for (tp
=*chain
, t
=0; tp
; tp
= tp
->next
, t
++) {
415 if (t
< s_t
) continue;
416 if (TC_H_MAJ(tcm
->tcm_info
) &&
417 TC_H_MAJ(tcm
->tcm_info
) != tp
->prio
)
419 if (TC_H_MIN(tcm
->tcm_info
) &&
420 TC_H_MIN(tcm
->tcm_info
) != tp
->protocol
)
423 memset(&cb
->args
[1], 0, sizeof(cb
->args
)-sizeof(cb
->args
[0]));
424 if (cb
->args
[1] == 0) {
425 if (tcf_fill_node(skb
, tp
, 0, NETLINK_CB(cb
->skb
).pid
,
426 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, RTM_NEWTFILTER
) <= 0) {
431 if (tp
->ops
->walk
== NULL
)
433 arg
.w
.fn
= tcf_node_dump
;
437 arg
.w
.skip
= cb
->args
[1]-1;
439 tp
->ops
->walk(tp
, &arg
.w
);
440 cb
->args
[1] = arg
.w
.count
+1;
451 read_unlock_bh(&qdisc_tree_lock
);
457 static int __init
tc_filter_init(void)
459 struct rtnetlink_link
*link_p
= rtnetlink_links
[PF_UNSPEC
];
461 /* Setup rtnetlink links. It is made here to avoid
462 exporting large number of public symbols.
466 link_p
[RTM_NEWTFILTER
-RTM_BASE
].doit
= tc_ctl_tfilter
;
467 link_p
[RTM_DELTFILTER
-RTM_BASE
].doit
= tc_ctl_tfilter
;
468 link_p
[RTM_GETTFILTER
-RTM_BASE
].doit
= tc_ctl_tfilter
;
469 link_p
[RTM_GETTFILTER
-RTM_BASE
].dumpit
= tc_dump_tfilter
;
474 subsys_initcall(tc_filter_init
);
476 EXPORT_SYMBOL(register_tcf_proto_ops
);
477 EXPORT_SYMBOL(unregister_tcf_proto_ops
);
478 EXPORT_SYMBOL(tcf_set_class
);