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 <linux/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>
39 #include <net/pkt_cls.h>
42 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
44 #define DPRINTK(format,args...)
47 /* The list of all installed classifier types */
49 static struct tcf_proto_ops *tcf_proto_base;
51 /* Protects list of registered TC modules. It is pure SMP lock. */
52 static rwlock_t cls_mod_lock = RW_LOCK_UNLOCKED;
54 /* Find classifier type by string name */
56 static struct tcf_proto_ops * tcf_proto_lookup_ops(struct rtattr *kind)
58 struct tcf_proto_ops *t = NULL;
61 read_lock(&cls_mod_lock);
62 for (t = tcf_proto_base; t; t = t->next) {
63 if (rtattr_strcmp(kind, t->kind) == 0) {
64 if (!try_module_get(t->owner))
69 read_unlock(&cls_mod_lock);
74 /* Register(unregister) new classifier type */
76 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
78 struct tcf_proto_ops *t, **tp;
81 write_lock(&cls_mod_lock);
82 for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next)
83 if (!strcmp(ops->kind, t->kind))
90 write_unlock(&cls_mod_lock);
94 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
96 struct tcf_proto_ops *t, **tp;
99 write_lock(&cls_mod_lock);
100 for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
109 write_unlock(&cls_mod_lock);
113 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
114 struct tcf_proto *tp, unsigned long fh, int event);
117 /* Select new prio value from the range, managed by kernel. */
119 static __inline__ u32 tcf_auto_prio(struct tcf_proto *tp)
121 u32 first = TC_H_MAKE(0xC0000000U,0U);
129 /* Add/change/delete/get a filter node */
131 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
133 struct rtattr **tca = arg;
134 struct tcmsg *t = NLMSG_DATA(n);
135 u32 protocol = TC_H_MIN(t->tcm_info);
136 u32 prio = TC_H_MAJ(t->tcm_info);
138 u32 parent = t->tcm_parent;
139 struct net_device *dev;
141 struct tcf_proto **back, **chain;
142 struct tcf_proto *tp = NULL;
143 struct tcf_proto_ops *tp_ops;
144 struct Qdisc_class_ops *cops;
145 unsigned long cl = 0;
150 /* If no priority is given, user wants we allocated it. */
151 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
153 prio = TC_H_MAKE(0x80000000U,0U);
156 /* Find head of filter chain. */
159 if ((dev = __dev_get_by_index(t->tcm_ifindex)) == NULL)
164 q = dev->qdisc_sleeping;
166 } else if ((q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent))) == NULL)
169 /* Is it classful? */
170 if ((cops = q->ops->cl_ops) == NULL)
173 /* Do we search for filter, attached to class? */
174 if (TC_H_MIN(parent)) {
175 cl = cops->get(q, parent);
180 /* And the last stroke */
181 chain = cops->tcf_chain(q, cl);
186 /* Check the chain for existence of proto-tcf with this priority */
187 for (back = chain; (tp=*back) != NULL; back = &tp->next) {
188 if (tp->prio >= prio) {
189 if (tp->prio == prio) {
190 if (!nprio || (tp->protocol != protocol && protocol))
199 /* Proto-tcf does not exist, create new one */
201 if (tca[TCA_KIND-1] == NULL || !protocol)
205 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
209 /* Create new proto tcf */
212 if ((tp = kmalloc(sizeof(*tp), GFP_KERNEL)) == NULL)
214 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
216 if (tp_ops==NULL && tca[TCA_KIND-1] != NULL) {
217 struct rtattr *kind = tca[TCA_KIND-1];
219 if (RTA_PAYLOAD(kind) <= IFNAMSIZ) {
220 request_module("cls_%s", (char*)RTA_DATA(kind));
221 tp_ops = tcf_proto_lookup_ops(kind);
225 if (tp_ops == NULL) {
230 memset(tp, 0, sizeof(*tp));
232 tp->protocol = protocol;
233 tp->prio = nprio ? : tcf_auto_prio(*back);
235 tp->classify = tp_ops->classify;
236 tp->classid = parent;
237 if ((err = tp_ops->init(tp)) != 0) {
238 module_put(tp_ops->owner);
243 qdisc_lock_tree(dev);
246 qdisc_unlock_tree(dev);
248 } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
251 fh = tp->ops->get(tp, t->tcm_handle);
254 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
255 qdisc_lock_tree(dev);
257 qdisc_unlock_tree(dev);
259 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
266 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
269 switch (n->nlmsg_type) {
272 if (n->nlmsg_flags&NLM_F_EXCL)
276 err = tp->ops->delete(tp, fh);
278 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
281 err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
289 err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
291 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
300 tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
301 u32 pid, u32 seq, unsigned flags, int event)
304 struct nlmsghdr *nlh;
305 unsigned char *b = skb->tail;
307 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*tcm));
308 nlh->nlmsg_flags = flags;
309 tcm = NLMSG_DATA(nlh);
310 tcm->tcm_family = AF_UNSPEC;
311 tcm->tcm_ifindex = tp->q->dev->ifindex;
312 tcm->tcm_parent = tp->classid;
313 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
314 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
315 tcm->tcm_handle = fh;
316 if (RTM_DELTFILTER != event) {
318 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
321 nlh->nlmsg_len = skb->tail - b;
326 skb_trim(skb, b - skb->data);
330 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
331 struct tcf_proto *tp, unsigned long fh, int event)
334 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
336 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
340 if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
345 return rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
352 struct netlink_callback *cb;
355 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
357 struct tcf_dump_args *a = (void*)arg;
359 return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
360 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
363 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
367 struct net_device *dev;
369 struct tcf_proto *tp, **chain;
370 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
371 unsigned long cl = 0;
372 struct Qdisc_class_ops *cops;
373 struct tcf_dump_args arg;
375 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
377 if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
380 read_lock_bh(&qdisc_tree_lock);
381 if (!tcm->tcm_parent)
382 q = dev->qdisc_sleeping;
384 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
387 if ((cops = q->ops->cl_ops) == NULL)
389 if (TC_H_MIN(tcm->tcm_parent)) {
390 cl = cops->get(q, tcm->tcm_parent);
394 chain = cops->tcf_chain(q, cl);
400 for (tp=*chain, t=0; tp; tp = tp->next, t++) {
401 if (t < s_t) continue;
402 if (TC_H_MAJ(tcm->tcm_info) &&
403 TC_H_MAJ(tcm->tcm_info) != tp->prio)
405 if (TC_H_MIN(tcm->tcm_info) &&
406 TC_H_MIN(tcm->tcm_info) != tp->protocol)
409 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
410 if (cb->args[1] == 0) {
411 if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
412 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
417 if (tp->ops->walk == NULL)
419 arg.w.fn = tcf_node_dump;
423 arg.w.skip = cb->args[1]-1;
425 tp->ops->walk(tp, &arg.w);
426 cb->args[1] = arg.w.count+1;
437 read_unlock_bh(&qdisc_tree_lock);
443 static int __init tc_filter_init(void)
445 struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
447 /* Setup rtnetlink links. It is made here to avoid
448 exporting large number of public symbols.
452 link_p[RTM_NEWTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
453 link_p[RTM_DELTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
454 link_p[RTM_GETTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
455 link_p[RTM_GETTFILTER-RTM_BASE].dumpit = tc_dump_tfilter;
460 subsys_initcall(tc_filter_init);
462 EXPORT_SYMBOL(register_tcf_proto_ops);
463 EXPORT_SYMBOL(unregister_tcf_proto_ops);