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
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <asm/bitops.h>
19 #include <linux/config.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/string.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
28 #include <linux/errno.h>
29 #include <linux/interrupt.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/init.h>
34 #include <linux/kmod.h>
36 #include <net/pkt_sched.h>
38 /* The list of all installed classifier types */
40 static struct tcf_proto_ops *tcf_proto_base;
42 /* Protects list of registered TC modules. It is pure SMP lock. */
43 static rwlock_t cls_mod_lock = RW_LOCK_UNLOCKED;
45 /* Find classifier type by string name */
47 struct tcf_proto_ops * tcf_proto_lookup_ops(struct rtattr *kind)
49 struct tcf_proto_ops *t = NULL;
52 read_lock(&cls_mod_lock);
53 for (t = tcf_proto_base; t; t = t->next) {
54 if (rtattr_strcmp(kind, t->kind) == 0)
57 read_unlock(&cls_mod_lock);
62 /* Register(unregister) new classifier type */
64 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
66 struct tcf_proto_ops *t, **tp;
68 write_lock(&cls_mod_lock);
69 for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next) {
70 if (strcmp(ops->kind, t->kind) == 0) {
71 write_unlock(&cls_mod_lock);
78 write_unlock(&cls_mod_lock);
82 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
84 struct tcf_proto_ops *t, **tp;
86 write_lock(&cls_mod_lock);
87 for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
92 write_unlock(&cls_mod_lock);
96 write_unlock(&cls_mod_lock);
100 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
101 struct tcf_proto *tp, unsigned long fh, int event);
104 /* Select new prio value from the range, managed by kernel. */
106 static __inline__ u32 tcf_auto_prio(struct tcf_proto *tp)
108 u32 first = TC_H_MAKE(0xC0000000U,0U);
116 /* Add/change/delete/get a filter node */
118 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
120 struct rtattr **tca = arg;
121 struct tcmsg *t = NLMSG_DATA(n);
122 u32 protocol = TC_H_MIN(t->tcm_info);
123 u32 prio = TC_H_MAJ(t->tcm_info);
125 u32 parent = t->tcm_parent;
126 struct net_device *dev;
128 struct tcf_proto **back, **chain;
129 struct tcf_proto *tp = NULL;
130 struct tcf_proto_ops *tp_ops;
131 struct Qdisc_class_ops *cops;
132 unsigned long cl = 0;
137 /* If no priority is given, user wants we allocated it. */
138 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
140 prio = TC_H_MAKE(0x80000000U,0U);
143 /* Find head of filter chain. */
146 if ((dev = __dev_get_by_index(t->tcm_ifindex)) == NULL)
151 q = dev->qdisc_sleeping;
153 } else if ((q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent))) == NULL)
156 /* Is it classful? */
157 if ((cops = q->ops->cl_ops) == NULL)
160 /* Do we search for filter, attached to class? */
161 if (TC_H_MIN(parent)) {
162 cl = cops->get(q, parent);
167 /* And the last stroke */
168 chain = cops->tcf_chain(q, cl);
173 /* Check the chain for existence of proto-tcf with this priority */
174 for (back = chain; (tp=*back) != NULL; back = &tp->next) {
175 if (tp->prio >= prio) {
176 if (tp->prio == prio) {
177 if (!nprio || (tp->protocol != protocol && protocol))
186 /* Proto-tcf does not exist, create new one */
188 if (tca[TCA_KIND-1] == NULL || !protocol)
192 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
196 /* Create new proto tcf */
199 if ((tp = kmalloc(sizeof(*tp), GFP_KERNEL)) == NULL)
201 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
203 if (tp_ops==NULL && tca[TCA_KIND-1] != NULL) {
204 struct rtattr *kind = tca[TCA_KIND-1];
205 char module_name[4 + IFNAMSIZ + 1];
207 if (RTA_PAYLOAD(kind) <= IFNAMSIZ) {
208 sprintf(module_name, "cls_%s", (char*)RTA_DATA(kind));
209 request_module (module_name);
210 tp_ops = tcf_proto_lookup_ops(kind);
214 if (tp_ops == NULL) {
219 memset(tp, 0, sizeof(*tp));
221 tp->protocol = protocol;
222 tp->prio = nprio ? : tcf_auto_prio(*back);
224 tp->classify = tp_ops->classify;
225 tp->classid = parent;
226 err = tp_ops->init(tp);
231 write_lock(&qdisc_tree_lock);
232 spin_lock_bh(&dev->queue_lock);
235 spin_unlock_bh(&dev->queue_lock);
236 write_unlock(&qdisc_tree_lock);
237 } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
240 fh = tp->ops->get(tp, t->tcm_handle);
243 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
244 write_lock(&qdisc_tree_lock);
245 spin_lock_bh(&dev->queue_lock);
247 spin_unlock_bh(&dev->queue_lock);
248 write_unlock(&qdisc_tree_lock);
250 tp->ops->destroy(tp);
257 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
260 switch (n->nlmsg_type) {
263 if (n->nlmsg_flags&NLM_F_EXCL)
267 err = tp->ops->delete(tp, fh);
270 err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
278 err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
280 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
289 tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
290 u32 pid, u32 seq, unsigned flags, int event)
293 struct nlmsghdr *nlh;
294 unsigned char *b = skb->tail;
296 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*tcm));
297 nlh->nlmsg_flags = flags;
298 tcm = NLMSG_DATA(nlh);
299 tcm->tcm_family = AF_UNSPEC;
300 tcm->tcm_ifindex = tp->q->dev->ifindex;
301 tcm->tcm_parent = tp->classid;
303 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
304 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
305 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
307 nlh->nlmsg_len = skb->tail - b;
312 skb_trim(skb, b - skb->data);
316 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
317 struct tcf_proto *tp, unsigned long fh, int event)
320 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
322 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
326 if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
331 return rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
338 struct netlink_callback *cb;
341 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
343 struct tcf_dump_args *a = (void*)arg;
345 return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
346 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
349 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
353 struct net_device *dev;
355 struct tcf_proto *tp, **chain;
356 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
357 unsigned long cl = 0;
358 struct Qdisc_class_ops *cops;
359 struct tcf_dump_args arg;
361 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
363 if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
366 read_lock(&qdisc_tree_lock);
367 if (!tcm->tcm_parent)
368 q = dev->qdisc_sleeping;
370 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
372 read_unlock(&qdisc_tree_lock);
376 if ((cops = q->ops->cl_ops) == NULL)
378 if (TC_H_MIN(tcm->tcm_parent)) {
379 cl = cops->get(q, tcm->tcm_parent);
383 chain = cops->tcf_chain(q, cl);
389 for (tp=*chain, t=0; tp; tp = tp->next, t++) {
390 if (t < s_t) continue;
391 if (TC_H_MAJ(tcm->tcm_info) &&
392 TC_H_MAJ(tcm->tcm_info) != tp->prio)
394 if (TC_H_MIN(tcm->tcm_info) &&
395 TC_H_MIN(tcm->tcm_info) != tp->protocol)
398 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
399 if (cb->args[1] == 0) {
400 if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
401 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
406 if (tp->ops->walk == NULL)
408 arg.w.fn = tcf_node_dump;
412 arg.w.skip = cb->args[1]-1;
414 tp->ops->walk(tp, &arg.w);
415 cb->args[1] = arg.w.count+1;
426 read_unlock(&qdisc_tree_lock);
432 int __init tc_filter_init(void)
434 struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
436 /* Setup rtnetlink links. It is made here to avoid
437 exporting large number of public symbols.
441 link_p[RTM_NEWTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
442 link_p[RTM_DELTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
443 link_p[RTM_GETTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
444 link_p[RTM_GETTFILTER-RTM_BASE].dumpit = tc_dump_tfilter;
446 #define INIT_TC_FILTER(name) { \
447 extern struct tcf_proto_ops cls_##name##_ops; \
448 register_tcf_proto_ops(&cls_##name##_ops); \
451 #ifdef CONFIG_NET_CLS_U32
454 #ifdef CONFIG_NET_CLS_ROUTE4
455 INIT_TC_FILTER(route4);
457 #ifdef CONFIG_NET_CLS_FW
460 #ifdef CONFIG_NET_CLS_RSVP
461 INIT_TC_FILTER(rsvp);
463 #ifdef CONFIG_NET_CLS_TCINDEX
464 INIT_TC_FILTER(tcindex);
466 #ifdef CONFIG_NET_CLS_RSVP6
467 INIT_TC_FILTER(rsvp6);