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/net_namespace.h>
28 #include <net/netlink.h>
29 #include <net/pkt_sched.h>
30 #include <net/pkt_cls.h>
33 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
35 #define DPRINTK(format,args...)
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 DEFINE_RWLOCK(cls_mod_lock);
45 /* Find classifier type by string name */
47 static 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) {
55 if (!try_module_get(t->owner))
60 read_unlock(&cls_mod_lock);
65 /* Register(unregister) new classifier type */
67 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
69 struct tcf_proto_ops *t, **tp;
72 write_lock(&cls_mod_lock);
73 for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next)
74 if (!strcmp(ops->kind, t->kind))
81 write_unlock(&cls_mod_lock);
85 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
87 struct tcf_proto_ops *t, **tp;
90 write_lock(&cls_mod_lock);
91 for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
100 write_unlock(&cls_mod_lock);
104 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
105 struct tcf_proto *tp, unsigned long fh, int event);
108 /* Select new prio value from the range, managed by kernel. */
110 static __inline__ u32 tcf_auto_prio(struct tcf_proto *tp)
112 u32 first = TC_H_MAKE(0xC0000000U,0U);
120 /* Add/change/delete/get a filter node */
122 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
124 struct net *net = skb->sk->sk_net;
131 struct net_device *dev;
133 struct tcf_proto **back, **chain;
134 struct tcf_proto *tp;
135 struct tcf_proto_ops *tp_ops;
136 const struct Qdisc_class_ops *cops;
141 if (net != &init_net)
147 protocol = TC_H_MIN(t->tcm_info);
148 prio = TC_H_MAJ(t->tcm_info);
150 parent = t->tcm_parent;
154 /* If no priority is given, user wants we allocated it. */
155 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
157 prio = TC_H_MAKE(0x80000000U,0U);
160 /* Find head of filter chain. */
163 if ((dev = __dev_get_by_index(&init_net, t->tcm_ifindex)) == NULL)
168 q = dev->qdisc_sleeping;
170 } else if ((q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent))) == NULL)
173 /* Is it classful? */
174 if ((cops = q->ops->cl_ops) == NULL)
177 /* Do we search for filter, attached to class? */
178 if (TC_H_MIN(parent)) {
179 cl = cops->get(q, parent);
184 /* And the last stroke */
185 chain = cops->tcf_chain(q, cl);
190 /* Check the chain for existence of proto-tcf with this priority */
191 for (back = chain; (tp=*back) != NULL; back = &tp->next) {
192 if (tp->prio >= prio) {
193 if (tp->prio == prio) {
194 if (!nprio || (tp->protocol != protocol && protocol))
203 /* Proto-tcf does not exist, create new one */
205 if (tca[TCA_KIND-1] == NULL || !protocol)
209 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
213 /* Create new proto tcf */
216 if ((tp = kzalloc(sizeof(*tp), GFP_KERNEL)) == NULL)
219 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND-1]);
220 if (tp_ops == NULL) {
222 struct rtattr *kind = tca[TCA_KIND-1];
226 rtattr_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
228 request_module("cls_%s", name);
230 tp_ops = tcf_proto_lookup_ops(kind);
231 /* We dropped the RTNL semaphore in order to
232 * perform the module load. So, even if we
233 * succeeded in loading the module we have to
234 * replay the request. We indicate this using
237 if (tp_ops != NULL) {
238 module_put(tp_ops->owner);
247 tp->protocol = protocol;
248 tp->prio = nprio ? : tcf_auto_prio(*back);
250 tp->classify = tp_ops->classify;
251 tp->classid = parent;
252 if ((err = tp_ops->init(tp)) != 0) {
253 module_put(tp_ops->owner);
258 qdisc_lock_tree(dev);
261 qdisc_unlock_tree(dev);
263 } else if (tca[TCA_KIND-1] && rtattr_strcmp(tca[TCA_KIND-1], tp->ops->kind))
266 fh = tp->ops->get(tp, t->tcm_handle);
269 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
270 qdisc_lock_tree(dev);
272 qdisc_unlock_tree(dev);
274 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
281 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
284 switch (n->nlmsg_type) {
287 if (n->nlmsg_flags&NLM_F_EXCL)
291 err = tp->ops->delete(tp, fh);
293 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
296 err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
304 err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
306 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
312 /* Replay the request. */
318 tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, unsigned long fh,
319 u32 pid, u32 seq, u16 flags, int event)
322 struct nlmsghdr *nlh;
323 unsigned char *b = skb_tail_pointer(skb);
325 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
326 tcm = NLMSG_DATA(nlh);
327 tcm->tcm_family = AF_UNSPEC;
330 tcm->tcm_ifindex = tp->q->dev->ifindex;
331 tcm->tcm_parent = tp->classid;
332 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
333 RTA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
334 tcm->tcm_handle = fh;
335 if (RTM_DELTFILTER != event) {
337 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
340 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
349 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
350 struct tcf_proto *tp, unsigned long fh, int event)
353 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
355 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
359 if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
364 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
371 struct netlink_callback *cb;
374 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, struct tcf_walker *arg)
376 struct tcf_dump_args *a = (void*)arg;
378 return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
379 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
382 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
384 struct net *net = skb->sk->sk_net;
387 struct net_device *dev;
389 struct tcf_proto *tp, **chain;
390 struct tcmsg *tcm = (struct tcmsg*)NLMSG_DATA(cb->nlh);
391 unsigned long cl = 0;
392 const struct Qdisc_class_ops *cops;
393 struct tcf_dump_args arg;
395 if (net != &init_net)
398 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
400 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
403 if (!tcm->tcm_parent)
404 q = dev->qdisc_sleeping;
406 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
409 if ((cops = q->ops->cl_ops) == NULL)
411 if (TC_H_MIN(tcm->tcm_parent)) {
412 cl = cops->get(q, tcm->tcm_parent);
416 chain = cops->tcf_chain(q, cl);
422 for (tp=*chain, t=0; tp; tp = tp->next, t++) {
423 if (t < s_t) continue;
424 if (TC_H_MAJ(tcm->tcm_info) &&
425 TC_H_MAJ(tcm->tcm_info) != tp->prio)
427 if (TC_H_MIN(tcm->tcm_info) &&
428 TC_H_MIN(tcm->tcm_info) != tp->protocol)
431 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
432 if (cb->args[1] == 0) {
433 if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
434 cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER) <= 0) {
439 if (tp->ops->walk == NULL)
441 arg.w.fn = tcf_node_dump;
445 arg.w.skip = cb->args[1]-1;
447 tp->ops->walk(tp, &arg.w);
448 cb->args[1] = arg.w.count+1;
464 tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
466 #ifdef CONFIG_NET_CLS_ACT
468 tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
476 tcf_exts_validate(struct tcf_proto *tp, struct rtattr **tb,
477 struct rtattr *rate_tlv, struct tcf_exts *exts,
478 struct tcf_ext_map *map)
480 memset(exts, 0, sizeof(*exts));
482 #ifdef CONFIG_NET_CLS_ACT
485 struct tc_action *act;
487 if (map->police && tb[map->police-1]) {
488 act = tcf_action_init_1(tb[map->police-1], rate_tlv, "police",
489 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
493 act->type = TCA_OLD_COMPAT;
495 } else if (map->action && tb[map->action-1]) {
496 act = tcf_action_init(tb[map->action-1], rate_tlv, NULL,
497 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
505 if ((map->action && tb[map->action-1]) ||
506 (map->police && tb[map->police-1]))
514 tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
515 struct tcf_exts *src)
517 #ifdef CONFIG_NET_CLS_ACT
519 struct tc_action *act;
521 act = xchg(&dst->action, src->action);
524 tcf_action_destroy(act, TCA_ACT_UNBIND);
530 tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
531 struct tcf_ext_map *map)
533 #ifdef CONFIG_NET_CLS_ACT
534 if (map->action && exts->action) {
536 * again for backward compatible mode - we want
537 * to work with both old and new modes of entering
538 * tc data even if iproute2 was newer - jhs
540 struct rtattr *p_rta = (struct rtattr *)skb_tail_pointer(skb);
542 if (exts->action->type != TCA_OLD_COMPAT) {
543 RTA_PUT(skb, map->action, 0, NULL);
544 if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
546 p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
547 } else if (map->police) {
548 RTA_PUT(skb, map->police, 0, NULL);
549 if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
551 p_rta->rta_len = skb_tail_pointer(skb) - (u8 *)p_rta;
556 rtattr_failure: __attribute__ ((unused))
561 tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
562 struct tcf_ext_map *map)
564 #ifdef CONFIG_NET_CLS_ACT
566 if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
570 rtattr_failure: __attribute__ ((unused))
574 static int __init tc_filter_init(void)
576 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL);
577 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL);
578 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
584 subsys_initcall(tc_filter_init);
586 EXPORT_SYMBOL(register_tcf_proto_ops);
587 EXPORT_SYMBOL(unregister_tcf_proto_ops);
588 EXPORT_SYMBOL(tcf_exts_validate);
589 EXPORT_SYMBOL(tcf_exts_destroy);
590 EXPORT_SYMBOL(tcf_exts_change);
591 EXPORT_SYMBOL(tcf_exts_dump);
592 EXPORT_SYMBOL(tcf_exts_dump_stats);