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>
32 /* The list of all installed classifier types */
34 static struct tcf_proto_ops *tcf_proto_base __read_mostly;
36 /* Protects list of registered TC modules. It is pure SMP lock. */
37 static DEFINE_RWLOCK(cls_mod_lock);
39 /* Find classifier type by string name */
41 static struct tcf_proto_ops *tcf_proto_lookup_ops(struct nlattr *kind)
43 struct tcf_proto_ops *t = NULL;
46 read_lock(&cls_mod_lock);
47 for (t = tcf_proto_base; t; t = t->next) {
48 if (nla_strcmp(kind, t->kind) == 0) {
49 if (!try_module_get(t->owner))
54 read_unlock(&cls_mod_lock);
59 /* Register(unregister) new classifier type */
61 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
63 struct tcf_proto_ops *t, **tp;
66 write_lock(&cls_mod_lock);
67 for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next)
68 if (!strcmp(ops->kind, t->kind))
75 write_unlock(&cls_mod_lock);
78 EXPORT_SYMBOL(register_tcf_proto_ops);
80 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
82 struct tcf_proto_ops *t, **tp;
85 write_lock(&cls_mod_lock);
86 for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next)
95 write_unlock(&cls_mod_lock);
98 EXPORT_SYMBOL(unregister_tcf_proto_ops);
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 net *net = skb->sk->sk_net;
121 struct nlattr *tca[TCA_MAX + 1];
127 struct net_device *dev;
129 struct tcf_proto **back, **chain;
130 struct tcf_proto *tp;
131 struct tcf_proto_ops *tp_ops;
132 const struct Qdisc_class_ops *cops;
137 if (net != &init_net)
142 protocol = TC_H_MIN(t->tcm_info);
143 prio = TC_H_MAJ(t->tcm_info);
145 parent = t->tcm_parent;
149 /* If no priority is given, user wants we allocated it. */
150 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
152 prio = TC_H_MAKE(0x80000000U, 0U);
155 /* Find head of filter chain. */
158 dev = __dev_get_by_index(&init_net, t->tcm_ifindex);
162 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
168 q = dev->qdisc_sleeping;
171 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
176 /* Is it classful? */
177 if ((cops = q->ops->cl_ops) == NULL)
180 /* Do we search for filter, attached to class? */
181 if (TC_H_MIN(parent)) {
182 cl = cops->get(q, parent);
187 /* And the last stroke */
188 chain = cops->tcf_chain(q, cl);
193 /* Check the chain for existence of proto-tcf with this priority */
194 for (back = chain; (tp=*back) != NULL; back = &tp->next) {
195 if (tp->prio >= prio) {
196 if (tp->prio == prio) {
197 if (!nprio || (tp->protocol != protocol && protocol))
206 /* Proto-tcf does not exist, create new one */
208 if (tca[TCA_KIND] == NULL || !protocol)
212 if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE))
216 /* Create new proto tcf */
219 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
223 tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
224 if (tp_ops == NULL) {
226 struct nlattr *kind = tca[TCA_KIND];
230 nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
232 request_module("cls_%s", name);
234 tp_ops = tcf_proto_lookup_ops(kind);
235 /* We dropped the RTNL semaphore in order to
236 * perform the module load. So, even if we
237 * succeeded in loading the module we have to
238 * replay the request. We indicate this using
241 if (tp_ops != NULL) {
242 module_put(tp_ops->owner);
251 tp->protocol = protocol;
252 tp->prio = nprio ? : tcf_auto_prio(*back);
254 tp->classify = tp_ops->classify;
255 tp->classid = parent;
257 err = tp_ops->init(tp);
259 module_put(tp_ops->owner);
264 qdisc_lock_tree(dev);
267 qdisc_unlock_tree(dev);
269 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], 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 ||
288 !(n->nlmsg_flags & NLM_F_CREATE))
291 switch (n->nlmsg_type) {
294 if (n->nlmsg_flags & NLM_F_EXCL)
298 err = tp->ops->delete(tp, fh);
300 tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER);
303 err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
311 err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh);
313 tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER);
319 /* Replay the request. */
324 static int tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp,
325 unsigned long fh, u32 pid, u32 seq, u16 flags, int event)
328 struct nlmsghdr *nlh;
329 unsigned char *b = skb_tail_pointer(skb);
331 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags);
332 tcm = NLMSG_DATA(nlh);
333 tcm->tcm_family = AF_UNSPEC;
336 tcm->tcm_ifindex = tp->q->dev->ifindex;
337 tcm->tcm_parent = tp->classid;
338 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
339 NLA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind);
340 tcm->tcm_handle = fh;
341 if (RTM_DELTFILTER != event) {
343 if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
344 goto nla_put_failure;
346 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
355 static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n,
356 struct tcf_proto *tp, unsigned long fh, int event)
359 u32 pid = oskb ? NETLINK_CB(oskb).pid : 0;
361 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
365 if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) {
370 return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC,
371 n->nlmsg_flags & NLM_F_ECHO);
374 struct tcf_dump_args {
377 struct netlink_callback *cb;
380 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
381 struct tcf_walker *arg)
383 struct tcf_dump_args *a = (void *)arg;
385 return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid,
386 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
389 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
391 struct net *net = skb->sk->sk_net;
394 struct net_device *dev;
396 struct tcf_proto *tp, **chain;
397 struct tcmsg *tcm = (struct tcmsg *)NLMSG_DATA(cb->nlh);
398 unsigned long cl = 0;
399 const struct Qdisc_class_ops *cops;
400 struct tcf_dump_args arg;
402 if (net != &init_net)
405 if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
407 if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL)
410 if (!tcm->tcm_parent)
411 q = dev->qdisc_sleeping;
413 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
416 if ((cops = q->ops->cl_ops) == NULL)
418 if (TC_H_MIN(tcm->tcm_parent)) {
419 cl = cops->get(q, tcm->tcm_parent);
423 chain = cops->tcf_chain(q, cl);
429 for (tp=*chain, t=0; tp; tp = tp->next, t++) {
430 if (t < s_t) continue;
431 if (TC_H_MAJ(tcm->tcm_info) &&
432 TC_H_MAJ(tcm->tcm_info) != tp->prio)
434 if (TC_H_MIN(tcm->tcm_info) &&
435 TC_H_MIN(tcm->tcm_info) != tp->protocol)
438 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
439 if (cb->args[1] == 0) {
440 if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid,
441 cb->nlh->nlmsg_seq, NLM_F_MULTI,
442 RTM_NEWTFILTER) <= 0)
447 if (tp->ops->walk == NULL)
449 arg.w.fn = tcf_node_dump;
453 arg.w.skip = cb->args[1]-1;
455 tp->ops->walk(tp, &arg.w);
456 cb->args[1] = arg.w.count+1;
471 void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
473 #ifdef CONFIG_NET_CLS_ACT
475 tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
480 EXPORT_SYMBOL(tcf_exts_destroy);
482 int tcf_exts_validate(struct tcf_proto *tp, struct nlattr **tb,
483 struct nlattr *rate_tlv, struct tcf_exts *exts,
484 struct tcf_ext_map *map)
486 memset(exts, 0, sizeof(*exts));
488 #ifdef CONFIG_NET_CLS_ACT
491 struct tc_action *act;
493 if (map->police && tb[map->police]) {
494 act = tcf_action_init_1((struct rtattr *)tb[map->police],
495 (struct rtattr *)rate_tlv,
496 "police", TCA_ACT_NOREPLACE,
501 act->type = TCA_OLD_COMPAT;
503 } else if (map->action && tb[map->action]) {
504 act = tcf_action_init((struct rtattr *)tb[map->action],
505 (struct rtattr *)rate_tlv, NULL,
506 TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err);
514 if ((map->action && tb[map->action]) ||
515 (map->police && tb[map->police]))
521 EXPORT_SYMBOL(tcf_exts_validate);
523 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
524 struct tcf_exts *src)
526 #ifdef CONFIG_NET_CLS_ACT
528 struct tc_action *act;
530 act = xchg(&dst->action, src->action);
533 tcf_action_destroy(act, TCA_ACT_UNBIND);
537 EXPORT_SYMBOL(tcf_exts_change);
539 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts,
540 struct tcf_ext_map *map)
542 #ifdef CONFIG_NET_CLS_ACT
543 if (map->action && exts->action) {
545 * again for backward compatible mode - we want
546 * to work with both old and new modes of entering
547 * tc data even if iproute2 was newer - jhs
549 struct nlattr *p_rta = (struct nlattr *)skb_tail_pointer(skb);
551 if (exts->action->type != TCA_OLD_COMPAT) {
552 NLA_PUT(skb, map->action, 0, NULL);
553 if (tcf_action_dump(skb, exts->action, 0, 0) < 0)
554 goto nla_put_failure;
555 p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta;
556 } else if (map->police) {
557 NLA_PUT(skb, map->police, 0, NULL);
558 if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0)
559 goto nla_put_failure;
560 p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta;
565 nla_put_failure: __attribute__ ((unused))
568 EXPORT_SYMBOL(tcf_exts_dump);
571 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts,
572 struct tcf_ext_map *map)
574 #ifdef CONFIG_NET_CLS_ACT
576 if (tcf_action_copy_stats(skb, exts->action, 1) < 0)
577 goto nla_put_failure;
580 nla_put_failure: __attribute__ ((unused))
583 EXPORT_SYMBOL(tcf_exts_dump_stats);
585 static int __init tc_filter_init(void)
587 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL);
588 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL);
589 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
595 subsys_initcall(tc_filter_init);