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 DEFINE_RWLOCK(cls_mod_lock);
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)
139 struct net_device *dev;
141 struct tcf_proto **back, **chain;
142 struct tcf_proto *tp;
143 struct tcf_proto_ops *tp_ops;
144 struct Qdisc_class_ops *cops;
152 protocol = TC_H_MIN(t->tcm_info);
153 prio = TC_H_MAJ(t->tcm_info);
155 parent = t->tcm_parent;
159 /* If no priority is given, user wants we allocated it. */
160 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
162 prio = TC_H_MAKE(0x80000000U,0U);
165 /* Find head of filter chain. */
168 if ((dev = __dev_get_by_index(t->tcm_ifindex)) == NULL)
173 q = dev->qdisc_sleeping;
175 } else if ((q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent))) == NULL)
178 /* Is it classful? */
179 if ((cops = q->ops->cl_ops) == NULL)
182 /* Do we search for filter, attached to class? */
183 if (TC_H_MIN(parent)) {
184 cl = cops->get(q, parent);
189 /* And the last stroke */
190 chain = cops->tcf_chain(q, cl);
195 /* Check the chain for existence of proto-tcf with this priority */
196 for (back = chain; (tp=*back) != NULL; back = &tp->next) {
197 if (tp->prio >= prio) {
198 if (tp->prio == prio) {
199 if (!nprio || (tp->protocol != protocol && protocol))
208 /* Proto-tcf does not exist, create new one */
210 if (tca[TCA_KIND-1] == NULL || !protocol)
214 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
218 /* Create new proto tcf */
221 if ((tp = kmalloc(sizeof(*tp), GFP_KERNEL)) == NULL)
224 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
225 if (tp_ops == NULL) {
227 struct rtattr *kind = tca[TCA_KIND-1];
231 rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
233 request_module("cls_%s", name);
235 tp_ops = tcf_proto_lookup_ops(kind);
236 /* We dropped the RTNL semaphore in order to
237 * perform the module load. So, even if we
238 * succeeded in loading the module we have to
239 * replay the request. We indicate this using
242 if (tp_ops != NULL) {
243 module_put(tp_ops->owner);
251 memset(tp, 0, sizeof(*tp));
253 tp->protocol = protocol;
254 tp->prio = nprio ? : tcf_auto_prio(*back);
256 tp->classify = tp_ops->classify;
257 tp->classid = parent;
258 if ((err = tp_ops->init(tp)) != 0) {
259 module_put(tp_ops->owner);
264 qdisc_lock_tree(dev);
267 qdisc_unlock_tree(dev);
269 } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
272 fh = tp->ops->get(tp, t->tcm_handle);
275 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
276 qdisc_lock_tree(dev);
278 qdisc_unlock_tree(dev);
280 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
287 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
290 switch (n->nlmsg_type) {
293 if (n->nlmsg_flags&NLM_F_EXCL)
297 err = tp->ops->delete(tp, fh);
299 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
302 err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
310 err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
312 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
318 /* Replay the request. */
324 tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
325 u32 pid, u32 seq, unsigned flags, int event)
328 struct nlmsghdr *nlh;
329 unsigned char *b = skb->tail;
331 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*tcm));
332 nlh->nlmsg_flags = flags;
333 tcm = NLMSG_DATA(nlh);
334 tcm->tcm_family = AF_UNSPEC;
335 tcm->tcm_ifindex = tp->q->dev->ifindex;
336 tcm->tcm_parent = tp->classid;
337 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
338 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
339 tcm->tcm_handle = fh;
340 if (RTM_DELTFILTER != event) {
342 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
345 nlh->nlmsg_len = skb->tail - b;
350 skb_trim(skb, b - skb->data);
354 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
355 struct tcf_proto *tp, unsigned long fh, int event)
358 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
360 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
364 if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
369 return rtnetlink_send(skb, pid, RTMGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
376 struct netlink_callback *cb;
379 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
381 struct tcf_dump_args *a = (void*)arg;
383 return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
384 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
387 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
391 struct net_device *dev;
393 struct tcf_proto *tp, **chain;
394 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
395 unsigned long cl = 0;
396 struct Qdisc_class_ops *cops;
397 struct tcf_dump_args arg;
399 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
401 if ((dev = dev_get_by_index(tcm->tcm_ifindex)) == NULL)
404 read_lock_bh(&qdisc_tree_lock);
405 if (!tcm->tcm_parent)
406 q = dev->qdisc_sleeping;
408 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
411 if ((cops = q->ops->cl_ops) == NULL)
413 if (TC_H_MIN(tcm->tcm_parent)) {
414 cl = cops->get(q, tcm->tcm_parent);
418 chain = cops->tcf_chain(q, cl);
424 for (tp=*chain, t=0; tp; tp = tp->next, t++) {
425 if (t < s_t) continue;
426 if (TC_H_MAJ(tcm->tcm_info) &&
427 TC_H_MAJ(tcm->tcm_info) != tp->prio)
429 if (TC_H_MIN(tcm->tcm_info) &&
430 TC_H_MIN(tcm->tcm_info) != tp->protocol)
433 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
434 if (cb->args[1] == 0) {
435 if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
436 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
441 if (tp->ops->walk == NULL)
443 arg.w.fn = tcf_node_dump;
447 arg.w.skip = cb->args[1]-1;
449 tp->ops->walk(tp, &arg.w);
450 cb->args[1] = arg.w.count+1;
461 read_unlock_bh(&qdisc_tree_lock);
467 tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
469 #ifdef CONFIG_NET_CLS_ACT
471 tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
474 #elif defined CONFIG_NET_CLS_POLICE
476 tcf_police_release(exts->police, TCA_ACT_UNBIND);
484 tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb,
485 struct rtattr *rate_tlv, struct tcf_exts *exts,
486 struct tcf_ext_map *map)
488 memset(exts, 0, sizeof(*exts));
490 #ifdef CONFIG_NET_CLS_ACT
493 struct tc_action *act;
495 if (map->police && tb[map->police-1]) {
496 act = tcf_action_init_1(tb[map->police-1], rate_tlv, "police",
497 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
501 act->type = TCA_OLD_COMPAT;
503 } else if (map->action && tb[map->action-1]) {
504 act = tcf_action_init(tb[map->action-1], rate_tlv, NULL,
505 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
512 #elif defined CONFIG_NET_CLS_POLICE
513 if (map->police && tb[map->police-1]) {
514 struct tcf_police *p;
516 p = tcf_police_locate(tb[map->police-1], rate_tlv);
521 } else if (map->action && tb[map->action-1])
524 if ((map->action && tb[map->action-1]) ||
525 (map->police && tb[map->police-1]))
533 tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
534 struct tcf_exts *src)
536 #ifdef CONFIG_NET_CLS_ACT
538 struct tc_action *act;
540 act = xchg(&dst->action, src->action);
543 tcf_action_destroy(act, TCA_ACT_UNBIND);
545 #elif defined CONFIG_NET_CLS_POLICE
547 struct tcf_police *p;
549 p = xchg(&dst->police, src->police);
552 tcf_police_release(p, TCA_ACT_UNBIND);
558 tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
559 struct tcf_ext_map *map)
561 #ifdef CONFIG_NET_CLS_ACT
562 if (map->action && exts->action) {
564 * again for backward compatible mode - we want
565 * to work with both old and new modes of entering
566 * tc data even if iproute2 was newer - jhs
568 struct rtattr * p_rta = (struct rtattr*) skb->tail;
570 if (exts->action->type != TCA_OLD_COMPAT) {
571 RTA_PUT(skb, map->action, 0, NULL);
572 if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
574 p_rta->rta_len = skb->tail - (u8*)p_rta;
575 } else if (map->police) {
576 RTA_PUT(skb, map->police, 0, NULL);
577 if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
579 p_rta->rta_len = skb->tail - (u8*)p_rta;
582 #elif defined CONFIG_NET_CLS_POLICE
583 if (map->police && exts->police) {
584 struct rtattr * p_rta = (struct rtattr*) skb->tail;
586 RTA_PUT(skb, map->police, 0, NULL);
588 if (tcf_police_dump(skb, exts->police) < 0)
591 p_rta->rta_len = skb->tail - (u8*)p_rta;
595 rtattr_failure: __attribute__ ((unused))
600 tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
601 struct tcf_ext_map *map)
603 #ifdef CONFIG_NET_CLS_ACT
605 if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
607 #elif defined CONFIG_NET_CLS_POLICE
609 if (tcf_police_dump_stats(skb, exts->police) < 0)
613 rtattr_failure: __attribute__ ((unused))
617 static int __init tc_filter_init(void)
619 struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC];
621 /* Setup rtnetlink links. It is made here to avoid
622 exporting large number of public symbols.
626 link_p[RTM_NEWTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
627 link_p[RTM_DELTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
628 link_p[RTM_GETTFILTER-RTM_BASE].doit = tc_ctl_tfilter;
629 link_p[RTM_GETTFILTER-RTM_BASE].dumpit = tc_dump_tfilter;
634 subsys_initcall(tc_filter_init);
636 EXPORT_SYMBOL(register_tcf_proto_ops);
637 EXPORT_SYMBOL(unregister_tcf_proto_ops);
638 EXPORT_SYMBOL(tcf_exts_validate);
639 EXPORT_SYMBOL(tcf_exts_destroy);
640 EXPORT_SYMBOL(tcf_exts_change);
641 EXPORT_SYMBOL(tcf_exts_dump);
642 EXPORT_SYMBOL(tcf_exts_dump_stats);