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/err.h>
23 #include <linux/skbuff.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <net/net_namespace.h>
30 #include <net/netlink.h>
31 #include <net/pkt_sched.h>
32 #include <net/pkt_cls.h>
34 /* The list of all installed classifier types */
35 static LIST_HEAD(tcf_proto_base);
37 /* Protects list of registered TC modules. It is pure SMP lock. */
38 static DEFINE_RWLOCK(cls_mod_lock);
40 /* Find classifier type by string name */
42 static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
44 const struct tcf_proto_ops *t, *res = NULL;
47 read_lock(&cls_mod_lock);
48 list_for_each_entry(t, &tcf_proto_base, head) {
49 if (strcmp(kind, t->kind) == 0) {
50 if (try_module_get(t->owner))
55 read_unlock(&cls_mod_lock);
60 /* Register(unregister) new classifier type */
62 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
64 struct tcf_proto_ops *t;
67 write_lock(&cls_mod_lock);
68 list_for_each_entry(t, &tcf_proto_base, head)
69 if (!strcmp(ops->kind, t->kind))
72 list_add_tail(&ops->head, &tcf_proto_base);
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;
85 /* Wait for outstanding call_rcu()s, if any, from a
86 * tcf_proto_ops's destroy() handler.
90 write_lock(&cls_mod_lock);
91 list_for_each_entry(t, &tcf_proto_base, head) {
98 write_unlock(&cls_mod_lock);
101 EXPORT_SYMBOL(unregister_tcf_proto_ops);
103 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
104 struct nlmsghdr *n, struct tcf_proto *tp,
105 unsigned long fh, int event, bool unicast);
107 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
109 struct tcf_chain *chain, int event)
111 struct tcf_proto *tp;
113 for (tp = rtnl_dereference(chain->filter_chain);
114 tp; tp = rtnl_dereference(tp->next))
115 tfilter_notify(net, oskb, n, tp, 0, event, false);
118 /* Select new prio value from the range, managed by kernel. */
120 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
122 u32 first = TC_H_MAKE(0xC0000000U, 0U);
125 first = tp->prio - 1;
127 return TC_H_MAJ(first);
130 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
131 u32 prio, u32 parent, struct Qdisc *q,
132 struct tcf_block *block)
134 struct tcf_proto *tp;
137 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
139 return ERR_PTR(-ENOBUFS);
142 tp->ops = tcf_proto_lookup_ops(kind);
144 #ifdef CONFIG_MODULES
146 request_module("cls_%s", kind);
148 tp->ops = tcf_proto_lookup_ops(kind);
149 /* We dropped the RTNL semaphore in order to perform
150 * the module load. So, even if we succeeded in loading
151 * the module we have to replay the request. We indicate
152 * this using -EAGAIN.
155 module_put(tp->ops->owner);
163 tp->classify = tp->ops->classify;
164 tp->protocol = protocol;
166 tp->classid = parent;
170 err = tp->ops->init(tp);
172 module_put(tp->ops->owner);
182 static void tcf_proto_destroy(struct tcf_proto *tp)
184 tp->ops->destroy(tp);
185 module_put(tp->ops->owner);
189 static struct tcf_chain *tcf_chain_create(void)
191 return kzalloc(sizeof(struct tcf_chain), GFP_KERNEL);
194 static void tcf_chain_destroy(struct tcf_chain *chain)
196 struct tcf_proto *tp;
198 while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
199 RCU_INIT_POINTER(chain->filter_chain, tp->next);
200 tcf_proto_destroy(tp);
206 tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
207 struct tcf_proto __rcu **p_filter_chain)
209 chain->p_filter_chain = p_filter_chain;
212 int tcf_block_get(struct tcf_block **p_block,
213 struct tcf_proto __rcu **p_filter_chain)
215 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
220 block->chain = tcf_chain_create();
223 goto err_chain_create;
225 tcf_chain_filter_chain_ptr_set(block->chain, p_filter_chain);
233 EXPORT_SYMBOL(tcf_block_get);
235 void tcf_block_put(struct tcf_block *block)
239 tcf_chain_destroy(block->chain);
242 EXPORT_SYMBOL(tcf_block_put);
244 /* Main classifier routine: scans classifier chain attached
245 * to this qdisc, (optionally) tests for protocol and asks
246 * specific classifiers.
248 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
249 struct tcf_result *res, bool compat_mode)
251 __be16 protocol = tc_skb_protocol(skb);
252 #ifdef CONFIG_NET_CLS_ACT
253 const int max_reclassify_loop = 4;
254 const struct tcf_proto *old_tp = tp;
259 for (; tp; tp = rcu_dereference_bh(tp->next)) {
262 if (tp->protocol != protocol &&
263 tp->protocol != htons(ETH_P_ALL))
266 err = tp->classify(skb, tp, res);
267 #ifdef CONFIG_NET_CLS_ACT
268 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode))
275 return TC_ACT_UNSPEC; /* signal: continue lookup */
276 #ifdef CONFIG_NET_CLS_ACT
278 if (unlikely(limit++ >= max_reclassify_loop)) {
279 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
280 tp->q->ops->id, tp->prio & 0xffff,
281 ntohs(tp->protocol));
286 protocol = tc_skb_protocol(skb);
290 EXPORT_SYMBOL(tcf_classify);
292 struct tcf_chain_info {
293 struct tcf_proto __rcu **pprev;
294 struct tcf_proto __rcu *next;
297 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
299 return rtnl_dereference(*chain_info->pprev);
302 static void tcf_chain_tp_insert(struct tcf_chain *chain,
303 struct tcf_chain_info *chain_info,
304 struct tcf_proto *tp)
306 if (chain->p_filter_chain &&
307 *chain_info->pprev == chain->filter_chain)
308 *chain->p_filter_chain = tp;
309 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
310 rcu_assign_pointer(*chain_info->pprev, tp);
313 static void tcf_chain_tp_remove(struct tcf_chain *chain,
314 struct tcf_chain_info *chain_info,
315 struct tcf_proto *tp)
317 struct tcf_proto *next = rtnl_dereference(chain_info->next);
319 if (chain->p_filter_chain && tp == chain->filter_chain)
320 *chain->p_filter_chain = next;
321 RCU_INIT_POINTER(*chain_info->pprev, next);
324 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
325 struct tcf_chain_info *chain_info,
326 u32 protocol, u32 prio,
329 struct tcf_proto **pprev;
330 struct tcf_proto *tp;
332 /* Check the chain for existence of proto-tcf with this priority */
333 for (pprev = &chain->filter_chain;
334 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
335 if (tp->prio >= prio) {
336 if (tp->prio == prio) {
338 (tp->protocol != protocol && protocol))
339 return ERR_PTR(-EINVAL);
346 chain_info->pprev = pprev;
347 chain_info->next = tp ? tp->next : NULL;
351 /* Add/change/delete/get a filter node */
353 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
354 struct netlink_ext_ack *extack)
356 struct net *net = sock_net(skb->sk);
357 struct nlattr *tca[TCA_MAX + 1];
363 struct net_device *dev;
365 struct tcf_chain_info chain_info;
366 struct tcf_chain *chain;
367 struct tcf_block *block;
368 struct tcf_proto *tp;
369 const struct Qdisc_class_ops *cops;
375 if ((n->nlmsg_type != RTM_GETTFILTER) &&
376 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
382 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
387 protocol = TC_H_MIN(t->tcm_info);
388 prio = TC_H_MAJ(t->tcm_info);
389 prio_allocate = false;
390 parent = t->tcm_parent;
394 switch (n->nlmsg_type) {
396 if (protocol || t->tcm_handle || tca[TCA_KIND])
400 /* If no priority is provided by the user,
403 if (n->nlmsg_flags & NLM_F_CREATE) {
404 prio = TC_H_MAKE(0x80000000U, 0U);
405 prio_allocate = true;
414 /* Find head of filter chain. */
417 dev = __dev_get_by_index(net, t->tcm_ifindex);
426 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
431 /* Is it classful? */
432 cops = q->ops->cl_ops;
436 if (!cops->tcf_block)
439 /* Do we search for filter, attached to class? */
440 if (TC_H_MIN(parent)) {
441 cl = cops->get(q, parent);
446 /* And the last stroke */
447 block = cops->tcf_block(q, cl);
452 chain = block->chain;
454 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
455 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
456 tcf_chain_destroy(chain);
461 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
462 prio, prio_allocate);
469 /* Proto-tcf does not exist, create new one */
471 if (tca[TCA_KIND] == NULL || !protocol) {
476 if (n->nlmsg_type != RTM_NEWTFILTER ||
477 !(n->nlmsg_flags & NLM_F_CREATE)) {
483 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
485 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
486 protocol, prio, parent, q, block);
492 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
497 fh = tp->ops->get(tp, t->tcm_handle);
500 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
501 tcf_chain_tp_remove(chain, &chain_info, tp);
502 tfilter_notify(net, skb, n, tp, fh,
503 RTM_DELTFILTER, false);
504 tcf_proto_destroy(tp);
509 if (n->nlmsg_type != RTM_NEWTFILTER ||
510 !(n->nlmsg_flags & NLM_F_CREATE)) {
517 switch (n->nlmsg_type) {
519 if (n->nlmsg_flags & NLM_F_EXCL) {
521 tcf_proto_destroy(tp);
527 err = tp->ops->delete(tp, fh, &last);
530 tfilter_notify(net, skb, n, tp, t->tcm_handle,
531 RTM_DELTFILTER, false);
533 tcf_chain_tp_remove(chain, &chain_info, tp);
534 tcf_proto_destroy(tp);
538 err = tfilter_notify(net, skb, n, tp, fh,
539 RTM_NEWTFILTER, true);
547 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
548 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
551 tcf_chain_tp_insert(chain, &chain_info, tp);
552 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
555 tcf_proto_destroy(tp);
562 /* Replay the request. */
567 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
568 struct tcf_proto *tp, unsigned long fh, u32 portid,
569 u32 seq, u16 flags, int event)
572 struct nlmsghdr *nlh;
573 unsigned char *b = skb_tail_pointer(skb);
575 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
578 tcm = nlmsg_data(nlh);
579 tcm->tcm_family = AF_UNSPEC;
582 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
583 tcm->tcm_parent = tp->classid;
584 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
585 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
586 goto nla_put_failure;
587 tcm->tcm_handle = fh;
588 if (RTM_DELTFILTER != event) {
590 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
591 goto nla_put_failure;
593 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
602 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
603 struct nlmsghdr *n, struct tcf_proto *tp,
604 unsigned long fh, int event, bool unicast)
607 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
609 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
613 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
614 n->nlmsg_flags, event) <= 0) {
620 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
622 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
623 n->nlmsg_flags & NLM_F_ECHO);
626 struct tcf_dump_args {
629 struct netlink_callback *cb;
632 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
633 struct tcf_walker *arg)
635 struct tcf_dump_args *a = (void *)arg;
636 struct net *net = sock_net(a->skb->sk);
638 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
639 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
643 /* called with RTNL */
644 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
646 struct net *net = sock_net(skb->sk);
649 struct net_device *dev;
651 struct tcf_block *block;
652 struct tcf_proto *tp;
653 struct tcf_chain *chain;
654 struct tcmsg *tcm = nlmsg_data(cb->nlh);
655 unsigned long cl = 0;
656 const struct Qdisc_class_ops *cops;
657 struct tcf_dump_args arg;
659 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
661 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
665 if (!tcm->tcm_parent)
668 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
671 cops = q->ops->cl_ops;
674 if (!cops->tcf_block)
676 if (TC_H_MIN(tcm->tcm_parent)) {
677 cl = cops->get(q, tcm->tcm_parent);
681 block = cops->tcf_block(q, cl);
684 chain = block->chain;
688 for (tp = rtnl_dereference(chain->filter_chain), t = 0;
689 tp; tp = rtnl_dereference(tp->next), t++) {
692 if (TC_H_MAJ(tcm->tcm_info) &&
693 TC_H_MAJ(tcm->tcm_info) != tp->prio)
695 if (TC_H_MIN(tcm->tcm_info) &&
696 TC_H_MIN(tcm->tcm_info) != tp->protocol)
699 memset(&cb->args[1], 0,
700 sizeof(cb->args)-sizeof(cb->args[0]));
701 if (cb->args[1] == 0) {
702 if (tcf_fill_node(net, skb, tp, 0,
703 NETLINK_CB(cb->skb).portid,
704 cb->nlh->nlmsg_seq, NLM_F_MULTI,
705 RTM_NEWTFILTER) <= 0)
710 if (tp->ops->walk == NULL)
712 arg.w.fn = tcf_node_dump;
716 arg.w.skip = cb->args[1] - 1;
718 tp->ops->walk(tp, &arg.w);
719 cb->args[1] = arg.w.count + 1;
733 void tcf_exts_destroy(struct tcf_exts *exts)
735 #ifdef CONFIG_NET_CLS_ACT
738 tcf_exts_to_list(exts, &actions);
739 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
740 kfree(exts->actions);
741 exts->nr_actions = 0;
744 EXPORT_SYMBOL(tcf_exts_destroy);
746 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
747 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
749 #ifdef CONFIG_NET_CLS_ACT
751 struct tc_action *act;
753 if (exts->police && tb[exts->police]) {
754 act = tcf_action_init_1(net, tb[exts->police], rate_tlv,
755 "police", ovr, TCA_ACT_BIND);
759 act->type = exts->type = TCA_OLD_COMPAT;
760 exts->actions[0] = act;
761 exts->nr_actions = 1;
762 } else if (exts->action && tb[exts->action]) {
766 err = tcf_action_init(net, tb[exts->action], rate_tlv,
767 NULL, ovr, TCA_ACT_BIND,
771 list_for_each_entry(act, &actions, list)
772 exts->actions[i++] = act;
773 exts->nr_actions = i;
777 if ((exts->action && tb[exts->action]) ||
778 (exts->police && tb[exts->police]))
784 EXPORT_SYMBOL(tcf_exts_validate);
786 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
787 struct tcf_exts *src)
789 #ifdef CONFIG_NET_CLS_ACT
790 struct tcf_exts old = *dst;
793 dst->nr_actions = src->nr_actions;
794 dst->actions = src->actions;
795 dst->type = src->type;
798 tcf_exts_destroy(&old);
801 EXPORT_SYMBOL(tcf_exts_change);
803 #ifdef CONFIG_NET_CLS_ACT
804 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
806 if (exts->nr_actions == 0)
809 return exts->actions[0];
813 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
815 #ifdef CONFIG_NET_CLS_ACT
818 if (exts->action && exts->nr_actions) {
820 * again for backward compatible mode - we want
821 * to work with both old and new modes of entering
822 * tc data even if iproute2 was newer - jhs
824 if (exts->type != TCA_OLD_COMPAT) {
827 nest = nla_nest_start(skb, exts->action);
829 goto nla_put_failure;
831 tcf_exts_to_list(exts, &actions);
832 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
833 goto nla_put_failure;
834 nla_nest_end(skb, nest);
835 } else if (exts->police) {
836 struct tc_action *act = tcf_exts_first_act(exts);
837 nest = nla_nest_start(skb, exts->police);
838 if (nest == NULL || !act)
839 goto nla_put_failure;
840 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
841 goto nla_put_failure;
842 nla_nest_end(skb, nest);
848 nla_nest_cancel(skb, nest);
854 EXPORT_SYMBOL(tcf_exts_dump);
857 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
859 #ifdef CONFIG_NET_CLS_ACT
860 struct tc_action *a = tcf_exts_first_act(exts);
861 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
866 EXPORT_SYMBOL(tcf_exts_dump_stats);
868 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
869 struct net_device **hw_dev)
871 #ifdef CONFIG_NET_CLS_ACT
872 const struct tc_action *a;
875 if (tc_no_actions(exts))
878 tcf_exts_to_list(exts, &actions);
879 list_for_each_entry(a, &actions, list) {
880 if (a->ops->get_dev) {
881 a->ops->get_dev(a, dev_net(dev), hw_dev);
890 EXPORT_SYMBOL(tcf_exts_get_dev);
892 static int __init tc_filter_init(void)
894 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
895 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
896 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
897 tc_dump_tfilter, NULL);
902 subsys_initcall(tc_filter_init);