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/slab.h>
27 #include <net/net_namespace.h>
29 #include <net/netlink.h>
30 #include <net/pkt_sched.h>
31 #include <net/pkt_cls.h>
33 /* The list of all installed classifier types */
34 static LIST_HEAD(tcf_proto_base);
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 const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
43 const struct tcf_proto_ops *t, *res = NULL;
46 read_lock(&cls_mod_lock);
47 list_for_each_entry(t, &tcf_proto_base, head) {
48 if (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;
66 write_lock(&cls_mod_lock);
67 list_for_each_entry(t, &tcf_proto_base, head)
68 if (!strcmp(ops->kind, t->kind))
71 list_add_tail(&ops->head, &tcf_proto_base);
74 write_unlock(&cls_mod_lock);
77 EXPORT_SYMBOL(register_tcf_proto_ops);
79 static struct workqueue_struct *tc_filter_wq;
81 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
83 struct tcf_proto_ops *t;
86 /* Wait for outstanding call_rcu()s, if any, from a
87 * tcf_proto_ops's destroy() handler.
90 flush_workqueue(tc_filter_wq);
92 write_lock(&cls_mod_lock);
93 list_for_each_entry(t, &tcf_proto_base, head) {
100 write_unlock(&cls_mod_lock);
103 EXPORT_SYMBOL(unregister_tcf_proto_ops);
105 bool tcf_queue_work(struct work_struct *work)
107 return queue_work(tc_filter_wq, work);
109 EXPORT_SYMBOL(tcf_queue_work);
111 /* Select new prio value from the range, managed by kernel. */
113 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
115 u32 first = TC_H_MAKE(0xC0000000U, 0U);
118 first = tp->prio - 1;
120 return TC_H_MAJ(first);
123 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
124 u32 prio, u32 parent, struct Qdisc *q,
125 struct tcf_chain *chain)
127 struct tcf_proto *tp;
130 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
132 return ERR_PTR(-ENOBUFS);
135 tp->ops = tcf_proto_lookup_ops(kind);
137 #ifdef CONFIG_MODULES
139 request_module("cls_%s", kind);
141 tp->ops = tcf_proto_lookup_ops(kind);
142 /* We dropped the RTNL semaphore in order to perform
143 * the module load. So, even if we succeeded in loading
144 * the module we have to replay the request. We indicate
145 * this using -EAGAIN.
148 module_put(tp->ops->owner);
156 tp->classify = tp->ops->classify;
157 tp->protocol = protocol;
159 tp->classid = parent;
163 err = tp->ops->init(tp);
165 module_put(tp->ops->owner);
175 static void tcf_proto_destroy(struct tcf_proto *tp)
177 tp->ops->destroy(tp);
178 module_put(tp->ops->owner);
182 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
185 struct tcf_chain *chain;
187 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
190 list_add_tail(&chain->list, &block->chain_list);
191 chain->block = block;
192 chain->index = chain_index;
197 static void tcf_chain_head_change(struct tcf_chain *chain,
198 struct tcf_proto *tp_head)
200 if (chain->chain_head_change)
201 chain->chain_head_change(tp_head,
202 chain->chain_head_change_priv);
205 static void tcf_chain_flush(struct tcf_chain *chain)
207 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
209 tcf_chain_head_change(chain, NULL);
211 RCU_INIT_POINTER(chain->filter_chain, tp->next);
212 tcf_proto_destroy(tp);
213 tp = rtnl_dereference(chain->filter_chain);
214 tcf_chain_put(chain);
218 static void tcf_chain_destroy(struct tcf_chain *chain)
220 list_del(&chain->list);
224 static void tcf_chain_hold(struct tcf_chain *chain)
229 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
232 struct tcf_chain *chain;
234 list_for_each_entry(chain, &block->chain_list, list) {
235 if (chain->index == chain_index) {
236 tcf_chain_hold(chain);
241 return create ? tcf_chain_create(block, chain_index) : NULL;
243 EXPORT_SYMBOL(tcf_chain_get);
245 void tcf_chain_put(struct tcf_chain *chain)
247 if (--chain->refcnt == 0)
248 tcf_chain_destroy(chain);
250 EXPORT_SYMBOL(tcf_chain_put);
252 static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q,
253 struct tcf_block_ext_info *ei,
254 enum tc_block_command command)
256 struct net_device *dev = q->dev_queue->dev;
257 struct tc_block_offload bo = {};
259 if (!dev->netdev_ops->ndo_setup_tc)
261 bo.command = command;
262 bo.binder_type = ei->binder_type;
264 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
267 static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
268 struct tcf_block_ext_info *ei)
270 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND);
273 static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
274 struct tcf_block_ext_info *ei)
276 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND);
279 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
280 struct tcf_block_ext_info *ei)
282 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
283 struct tcf_chain *chain;
288 INIT_LIST_HEAD(&block->chain_list);
289 INIT_LIST_HEAD(&block->cb_list);
291 /* Create chain 0 by default, it has to be always present. */
292 chain = tcf_chain_create(block, 0);
295 goto err_chain_create;
297 WARN_ON(!ei->chain_head_change);
298 chain->chain_head_change = ei->chain_head_change;
299 chain->chain_head_change_priv = ei->chain_head_change_priv;
300 block->net = qdisc_net(q);
302 tcf_block_offload_bind(block, q, ei);
310 EXPORT_SYMBOL(tcf_block_get_ext);
312 static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
314 struct tcf_proto __rcu **p_filter_chain = priv;
316 rcu_assign_pointer(*p_filter_chain, tp_head);
319 int tcf_block_get(struct tcf_block **p_block,
320 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
322 struct tcf_block_ext_info ei = {
323 .chain_head_change = tcf_chain_head_change_dflt,
324 .chain_head_change_priv = p_filter_chain,
327 WARN_ON(!p_filter_chain);
328 return tcf_block_get_ext(p_block, q, &ei);
330 EXPORT_SYMBOL(tcf_block_get);
332 static void tcf_block_put_final(struct work_struct *work)
334 struct tcf_block *block = container_of(work, struct tcf_block, work);
335 struct tcf_chain *chain, *tmp;
339 /* At this point, all the chains should have refcnt == 1. */
340 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
341 tcf_chain_put(chain);
346 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
347 * actions should be all removed after flushing.
349 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
350 struct tcf_block_ext_info *ei)
352 struct tcf_chain *chain;
356 /* Hold a refcnt for all chains, except 0, so that they don't disappear
357 * while we are iterating.
359 list_for_each_entry(chain, &block->chain_list, list)
361 tcf_chain_hold(chain);
363 list_for_each_entry(chain, &block->chain_list, list)
364 tcf_chain_flush(chain);
366 tcf_block_offload_unbind(block, q, ei);
368 INIT_WORK(&block->work, tcf_block_put_final);
369 /* Wait for existing RCU callbacks to cool down, make sure their works
370 * have been queued before this. We can not flush pending works here
371 * because we are holding the RTNL lock.
374 tcf_queue_work(&block->work);
376 EXPORT_SYMBOL(tcf_block_put_ext);
378 void tcf_block_put(struct tcf_block *block)
380 struct tcf_block_ext_info ei = {0, };
384 tcf_block_put_ext(block, block->q, &ei);
387 EXPORT_SYMBOL(tcf_block_put);
389 struct tcf_block_cb {
390 struct list_head list;
397 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
399 return block_cb->cb_priv;
401 EXPORT_SYMBOL(tcf_block_cb_priv);
403 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
404 tc_setup_cb_t *cb, void *cb_ident)
405 { struct tcf_block_cb *block_cb;
407 list_for_each_entry(block_cb, &block->cb_list, list)
408 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
412 EXPORT_SYMBOL(tcf_block_cb_lookup);
414 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
418 EXPORT_SYMBOL(tcf_block_cb_incref);
420 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
422 return --block_cb->refcnt;
424 EXPORT_SYMBOL(tcf_block_cb_decref);
426 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
427 tc_setup_cb_t *cb, void *cb_ident,
430 struct tcf_block_cb *block_cb;
432 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
436 block_cb->cb_ident = cb_ident;
437 block_cb->cb_priv = cb_priv;
438 list_add(&block_cb->list, &block->cb_list);
441 EXPORT_SYMBOL(__tcf_block_cb_register);
443 int tcf_block_cb_register(struct tcf_block *block,
444 tc_setup_cb_t *cb, void *cb_ident,
447 struct tcf_block_cb *block_cb;
449 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
450 return block_cb ? 0 : -ENOMEM;
452 EXPORT_SYMBOL(tcf_block_cb_register);
454 void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
456 list_del(&block_cb->list);
459 EXPORT_SYMBOL(__tcf_block_cb_unregister);
461 void tcf_block_cb_unregister(struct tcf_block *block,
462 tc_setup_cb_t *cb, void *cb_ident)
464 struct tcf_block_cb *block_cb;
466 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
469 __tcf_block_cb_unregister(block_cb);
471 EXPORT_SYMBOL(tcf_block_cb_unregister);
473 static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
474 void *type_data, bool err_stop)
476 struct tcf_block_cb *block_cb;
480 list_for_each_entry(block_cb, &block->cb_list, list) {
481 err = block_cb->cb(type, type_data, block_cb->cb_priv);
492 /* Main classifier routine: scans classifier chain attached
493 * to this qdisc, (optionally) tests for protocol and asks
494 * specific classifiers.
496 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
497 struct tcf_result *res, bool compat_mode)
499 __be16 protocol = tc_skb_protocol(skb);
500 #ifdef CONFIG_NET_CLS_ACT
501 const int max_reclassify_loop = 4;
502 const struct tcf_proto *orig_tp = tp;
503 const struct tcf_proto *first_tp;
508 for (; tp; tp = rcu_dereference_bh(tp->next)) {
511 if (tp->protocol != protocol &&
512 tp->protocol != htons(ETH_P_ALL))
515 err = tp->classify(skb, tp, res);
516 #ifdef CONFIG_NET_CLS_ACT
517 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
520 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
521 first_tp = res->goto_tp;
529 return TC_ACT_UNSPEC; /* signal: continue lookup */
530 #ifdef CONFIG_NET_CLS_ACT
532 if (unlikely(limit++ >= max_reclassify_loop)) {
533 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
534 tp->q->ops->id, tp->prio & 0xffff,
535 ntohs(tp->protocol));
540 protocol = tc_skb_protocol(skb);
544 EXPORT_SYMBOL(tcf_classify);
546 struct tcf_chain_info {
547 struct tcf_proto __rcu **pprev;
548 struct tcf_proto __rcu *next;
551 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
553 return rtnl_dereference(*chain_info->pprev);
556 static void tcf_chain_tp_insert(struct tcf_chain *chain,
557 struct tcf_chain_info *chain_info,
558 struct tcf_proto *tp)
560 if (*chain_info->pprev == chain->filter_chain)
561 tcf_chain_head_change(chain, tp);
562 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
563 rcu_assign_pointer(*chain_info->pprev, tp);
564 tcf_chain_hold(chain);
567 static void tcf_chain_tp_remove(struct tcf_chain *chain,
568 struct tcf_chain_info *chain_info,
569 struct tcf_proto *tp)
571 struct tcf_proto *next = rtnl_dereference(chain_info->next);
573 if (tp == chain->filter_chain)
574 tcf_chain_head_change(chain, next);
575 RCU_INIT_POINTER(*chain_info->pprev, next);
576 tcf_chain_put(chain);
579 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
580 struct tcf_chain_info *chain_info,
581 u32 protocol, u32 prio,
584 struct tcf_proto **pprev;
585 struct tcf_proto *tp;
587 /* Check the chain for existence of proto-tcf with this priority */
588 for (pprev = &chain->filter_chain;
589 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
590 if (tp->prio >= prio) {
591 if (tp->prio == prio) {
593 (tp->protocol != protocol && protocol))
594 return ERR_PTR(-EINVAL);
601 chain_info->pprev = pprev;
602 chain_info->next = tp ? tp->next : NULL;
606 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
607 struct tcf_proto *tp, struct Qdisc *q, u32 parent,
608 void *fh, u32 portid, u32 seq, u16 flags, int event)
611 struct nlmsghdr *nlh;
612 unsigned char *b = skb_tail_pointer(skb);
614 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
617 tcm = nlmsg_data(nlh);
618 tcm->tcm_family = AF_UNSPEC;
621 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
622 tcm->tcm_parent = parent;
623 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
624 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
625 goto nla_put_failure;
626 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
627 goto nla_put_failure;
631 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
632 goto nla_put_failure;
634 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
643 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
644 struct nlmsghdr *n, struct tcf_proto *tp,
645 struct Qdisc *q, u32 parent,
646 void *fh, int event, bool unicast)
649 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
651 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
655 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
656 n->nlmsg_flags, event) <= 0) {
662 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
664 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
665 n->nlmsg_flags & NLM_F_ECHO);
668 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
669 struct nlmsghdr *n, struct tcf_proto *tp,
670 struct Qdisc *q, u32 parent,
671 void *fh, bool unicast, bool *last)
674 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
677 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
681 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
682 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
687 err = tp->ops->delete(tp, fh, last);
694 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
696 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
697 n->nlmsg_flags & NLM_F_ECHO);
700 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
701 struct Qdisc *q, u32 parent,
703 struct tcf_chain *chain, int event)
705 struct tcf_proto *tp;
707 for (tp = rtnl_dereference(chain->filter_chain);
708 tp; tp = rtnl_dereference(tp->next))
709 tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
712 /* Add/change/delete/get a filter node */
714 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
715 struct netlink_ext_ack *extack)
717 struct net *net = sock_net(skb->sk);
718 struct nlattr *tca[TCA_MAX + 1];
725 struct net_device *dev;
727 struct tcf_chain_info chain_info;
728 struct tcf_chain *chain = NULL;
729 struct tcf_block *block;
730 struct tcf_proto *tp;
731 const struct Qdisc_class_ops *cops;
737 if ((n->nlmsg_type != RTM_GETTFILTER) &&
738 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
744 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
749 protocol = TC_H_MIN(t->tcm_info);
750 prio = TC_H_MAJ(t->tcm_info);
751 prio_allocate = false;
752 parent = t->tcm_parent;
756 switch (n->nlmsg_type) {
758 if (protocol || t->tcm_handle || tca[TCA_KIND])
762 /* If no priority is provided by the user,
765 if (n->nlmsg_flags & NLM_F_CREATE) {
766 prio = TC_H_MAKE(0x80000000U, 0U);
767 prio_allocate = true;
776 /* Find head of filter chain. */
779 dev = __dev_get_by_index(net, t->tcm_ifindex);
788 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
793 /* Is it classful? */
794 cops = q->ops->cl_ops;
798 if (!cops->tcf_block)
801 /* Do we search for filter, attached to class? */
802 if (TC_H_MIN(parent)) {
803 cl = cops->find(q, parent);
808 /* And the last stroke */
809 block = cops->tcf_block(q, cl);
815 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
816 if (chain_index > TC_ACT_EXT_VAL_MASK) {
820 chain = tcf_chain_get(block, chain_index,
821 n->nlmsg_type == RTM_NEWTFILTER);
823 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
827 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
828 tfilter_notify_chain(net, skb, q, parent, n,
829 chain, RTM_DELTFILTER);
830 tcf_chain_flush(chain);
835 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
836 prio, prio_allocate);
843 /* Proto-tcf does not exist, create new one */
845 if (tca[TCA_KIND] == NULL || !protocol) {
850 if (n->nlmsg_type != RTM_NEWTFILTER ||
851 !(n->nlmsg_flags & NLM_F_CREATE)) {
857 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
859 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
860 protocol, prio, parent, q, chain);
866 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
871 fh = tp->ops->get(tp, t->tcm_handle);
874 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
875 tcf_chain_tp_remove(chain, &chain_info, tp);
876 tfilter_notify(net, skb, n, tp, q, parent, fh,
877 RTM_DELTFILTER, false);
878 tcf_proto_destroy(tp);
883 if (n->nlmsg_type != RTM_NEWTFILTER ||
884 !(n->nlmsg_flags & NLM_F_CREATE)) {
891 switch (n->nlmsg_type) {
893 if (n->nlmsg_flags & NLM_F_EXCL) {
895 tcf_proto_destroy(tp);
901 err = tfilter_del_notify(net, skb, n, tp, q, parent,
906 tcf_chain_tp_remove(chain, &chain_info, tp);
907 tcf_proto_destroy(tp);
911 err = tfilter_notify(net, skb, n, tp, q, parent, fh,
912 RTM_NEWTFILTER, true);
920 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
921 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
924 tcf_chain_tp_insert(chain, &chain_info, tp);
925 tfilter_notify(net, skb, n, tp, q, parent, fh,
926 RTM_NEWTFILTER, false);
929 tcf_proto_destroy(tp);
934 tcf_chain_put(chain);
936 /* Replay the request. */
941 struct tcf_dump_args {
944 struct netlink_callback *cb;
949 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
951 struct tcf_dump_args *a = (void *)arg;
952 struct net *net = sock_net(a->skb->sk);
954 return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
955 n, NETLINK_CB(a->cb->skb).portid,
956 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
960 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
961 struct sk_buff *skb, struct netlink_callback *cb,
962 long index_start, long *p_index)
964 struct net *net = sock_net(skb->sk);
965 struct tcmsg *tcm = nlmsg_data(cb->nlh);
966 struct tcf_dump_args arg;
967 struct tcf_proto *tp;
969 for (tp = rtnl_dereference(chain->filter_chain);
970 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
971 if (*p_index < index_start)
973 if (TC_H_MAJ(tcm->tcm_info) &&
974 TC_H_MAJ(tcm->tcm_info) != tp->prio)
976 if (TC_H_MIN(tcm->tcm_info) &&
977 TC_H_MIN(tcm->tcm_info) != tp->protocol)
979 if (*p_index > index_start)
980 memset(&cb->args[1], 0,
981 sizeof(cb->args) - sizeof(cb->args[0]));
982 if (cb->args[1] == 0) {
983 if (tcf_fill_node(net, skb, tp, q, parent, 0,
984 NETLINK_CB(cb->skb).portid,
985 cb->nlh->nlmsg_seq, NLM_F_MULTI,
986 RTM_NEWTFILTER) <= 0)
993 arg.w.fn = tcf_node_dump;
999 arg.w.skip = cb->args[1] - 1;
1001 tp->ops->walk(tp, &arg.w);
1002 cb->args[1] = arg.w.count + 1;
1009 /* called with RTNL */
1010 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1012 struct net *net = sock_net(skb->sk);
1013 struct nlattr *tca[TCA_MAX + 1];
1014 struct net_device *dev;
1016 struct tcf_block *block;
1017 struct tcf_chain *chain;
1018 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1019 unsigned long cl = 0;
1020 const struct Qdisc_class_ops *cops;
1026 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1029 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1033 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1037 parent = tcm->tcm_parent;
1042 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1046 cops = q->ops->cl_ops;
1049 if (!cops->tcf_block)
1051 if (TC_H_MIN(tcm->tcm_parent)) {
1052 cl = cops->find(q, tcm->tcm_parent);
1056 block = cops->tcf_block(q, cl);
1060 index_start = cb->args[0];
1063 list_for_each_entry(chain, &block->chain_list, list) {
1064 if (tca[TCA_CHAIN] &&
1065 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1067 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1068 index_start, &index))
1072 cb->args[0] = index;
1078 void tcf_exts_destroy(struct tcf_exts *exts)
1080 #ifdef CONFIG_NET_CLS_ACT
1084 tcf_exts_to_list(exts, &actions);
1085 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1086 kfree(exts->actions);
1087 exts->nr_actions = 0;
1090 EXPORT_SYMBOL(tcf_exts_destroy);
1092 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
1093 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
1095 #ifdef CONFIG_NET_CLS_ACT
1097 struct tc_action *act;
1099 if (exts->police && tb[exts->police]) {
1100 act = tcf_action_init_1(net, tp, tb[exts->police],
1101 rate_tlv, "police", ovr,
1104 return PTR_ERR(act);
1106 act->type = exts->type = TCA_OLD_COMPAT;
1107 exts->actions[0] = act;
1108 exts->nr_actions = 1;
1109 } else if (exts->action && tb[exts->action]) {
1113 err = tcf_action_init(net, tp, tb[exts->action],
1114 rate_tlv, NULL, ovr, TCA_ACT_BIND,
1118 list_for_each_entry(act, &actions, list)
1119 exts->actions[i++] = act;
1120 exts->nr_actions = i;
1125 if ((exts->action && tb[exts->action]) ||
1126 (exts->police && tb[exts->police]))
1132 EXPORT_SYMBOL(tcf_exts_validate);
1134 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1136 #ifdef CONFIG_NET_CLS_ACT
1137 struct tcf_exts old = *dst;
1140 tcf_exts_destroy(&old);
1143 EXPORT_SYMBOL(tcf_exts_change);
1145 #ifdef CONFIG_NET_CLS_ACT
1146 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1148 if (exts->nr_actions == 0)
1151 return exts->actions[0];
1155 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1157 #ifdef CONFIG_NET_CLS_ACT
1158 struct nlattr *nest;
1160 if (exts->action && tcf_exts_has_actions(exts)) {
1162 * again for backward compatible mode - we want
1163 * to work with both old and new modes of entering
1164 * tc data even if iproute2 was newer - jhs
1166 if (exts->type != TCA_OLD_COMPAT) {
1169 nest = nla_nest_start(skb, exts->action);
1171 goto nla_put_failure;
1173 tcf_exts_to_list(exts, &actions);
1174 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
1175 goto nla_put_failure;
1176 nla_nest_end(skb, nest);
1177 } else if (exts->police) {
1178 struct tc_action *act = tcf_exts_first_act(exts);
1179 nest = nla_nest_start(skb, exts->police);
1180 if (nest == NULL || !act)
1181 goto nla_put_failure;
1182 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
1183 goto nla_put_failure;
1184 nla_nest_end(skb, nest);
1190 nla_nest_cancel(skb, nest);
1196 EXPORT_SYMBOL(tcf_exts_dump);
1199 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1201 #ifdef CONFIG_NET_CLS_ACT
1202 struct tc_action *a = tcf_exts_first_act(exts);
1203 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
1208 EXPORT_SYMBOL(tcf_exts_dump_stats);
1210 static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1211 enum tc_setup_type type,
1212 void *type_data, bool err_stop)
1215 #ifdef CONFIG_NET_CLS_ACT
1216 const struct tc_action *a;
1217 struct net_device *dev;
1220 if (!tcf_exts_has_actions(exts))
1223 for (i = 0; i < exts->nr_actions; i++) {
1224 a = exts->actions[i];
1225 if (!a->ops->get_dev)
1227 dev = a->ops->get_dev(a);
1230 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1239 int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1240 enum tc_setup_type type, void *type_data, bool err_stop)
1245 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1252 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1259 EXPORT_SYMBOL(tc_setup_cb_call);
1261 static int __init tc_filter_init(void)
1263 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1267 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1268 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
1269 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
1270 tc_dump_tfilter, 0);
1275 subsys_initcall(tc_filter_init);