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);
461 #elif defined CONFIG_NET_CLS_POLICE
463 tcf_police_release(exts->police, TCA_ACT_UNBIND);
471 tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb,
472 struct rtattr *rate_tlv, struct tcf_exts *exts,
473 struct tcf_ext_map *map)
475 memset(exts, 0, sizeof(*exts));
477 #ifdef CONFIG_NET_CLS_ACT
480 struct tc_action *act;
482 if (map->police && tb[map->police-1]) {
483 act = tcf_action_init_1(tb[map->police-1], rate_tlv, "police",
484 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
488 act->type = TCA_OLD_COMPAT;
490 } else if (map->action && tb[map->action-1]) {
491 act = tcf_action_init(tb[map->action-1], rate_tlv, NULL,
492 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
499 #elif defined CONFIG_NET_CLS_POLICE
500 if (map->police && tb[map->police-1]) {
501 struct tcf_police *p;
503 p = tcf_police_locate(tb[map->police-1], rate_tlv);
508 } else if (map->action && tb[map->action-1])
511 if ((map->action && tb[map->action-1]) ||
512 (map->police && tb[map->police-1]))
520 tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
521 struct tcf_exts *src)
523 #ifdef CONFIG_NET_CLS_ACT
525 struct tc_action *act;
527 act = xchg(&dst->action, src->action);
530 tcf_action_destroy(act, TCA_ACT_UNBIND);
532 #elif defined CONFIG_NET_CLS_POLICE
534 struct tcf_police *p;
536 p = xchg(&dst->police, src->police);
539 tcf_police_release(p, TCA_ACT_UNBIND);
545 tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
546 struct tcf_ext_map *map)
548 #ifdef CONFIG_NET_CLS_ACT
549 if (map->action && exts->action) {
551 * again for backward compatible mode - we want
552 * to work with both old and new modes of entering
553 * tc data even if iproute2 was newer - jhs
555 struct rtattr *p_rta = (struct rtattr *)skb_tail_pointer(skb);
557 if (exts->action->type != TCA_OLD_COMPAT) {
558 RTA_PUT(skb, map->action, 0, NULL);
559 if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
561 p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
562 } else if (map->police) {
563 RTA_PUT(skb, map->police, 0, NULL);
564 if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
566 p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
569 #elif defined CONFIG_NET_CLS_POLICE
570 if (map->police && exts->police) {
571 struct rtattr *p_rta = (struct rtattr *)skb_tail_pointer(skb);
573 RTA_PUT(skb, map->police, 0, NULL);
575 if (tcf_police_dump(skb, exts->police) < 0)
578 p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
582 rtattr_failure: __attribute__ ((unused))
587 tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
588 struct tcf_ext_map *map)
590 #ifdef CONFIG_NET_CLS_ACT
592 if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
594 #elif defined CONFIG_NET_CLS_POLICE
596 if (tcf_police_dump_stats(skb, exts->police) < 0)
600 rtattr_failure: __attribute__ ((unused))
604 static int __init tc_filter_init(void)
606 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL);
607 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL);
608 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
614 subsys_initcall(tc_filter_init);
616 EXPORT_SYMBOL(register_tcf_proto_ops);
617 EXPORT_SYMBOL(unregister_tcf_proto_ops);
618 EXPORT_SYMBOL(tcf_exts_validate);
619 EXPORT_SYMBOL(tcf_exts_destroy);
620 EXPORT_SYMBOL(tcf_exts_change);
621 EXPORT_SYMBOL(tcf_exts_dump);
622 EXPORT_SYMBOL(tcf_exts_dump_stats);