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 static struct workqueue_struct *tc_filter_wq;
82 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
84 struct tcf_proto_ops *t;
87 /* Wait for outstanding call_rcu()s, if any, from a
88 * tcf_proto_ops's destroy() handler.
91 flush_workqueue(tc_filter_wq);
93 write_lock(&cls_mod_lock);
94 list_for_each_entry(t, &tcf_proto_base, head) {
101 write_unlock(&cls_mod_lock);
104 EXPORT_SYMBOL(unregister_tcf_proto_ops);
106 bool tcf_queue_work(struct work_struct *work)
108 return queue_work(tc_filter_wq, work);
110 EXPORT_SYMBOL(tcf_queue_work);
112 /* Select new prio value from the range, managed by kernel. */
114 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
116 u32 first = TC_H_MAKE(0xC0000000U, 0U);
119 first = tp->prio - 1;
121 return TC_H_MAJ(first);
124 static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
125 u32 prio, u32 parent, struct Qdisc *q,
126 struct tcf_chain *chain)
128 struct tcf_proto *tp;
131 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
133 return ERR_PTR(-ENOBUFS);
136 tp->ops = tcf_proto_lookup_ops(kind);
138 #ifdef CONFIG_MODULES
140 request_module("cls_%s", kind);
142 tp->ops = tcf_proto_lookup_ops(kind);
143 /* We dropped the RTNL semaphore in order to perform
144 * the module load. So, even if we succeeded in loading
145 * the module we have to replay the request. We indicate
146 * this using -EAGAIN.
149 module_put(tp->ops->owner);
157 tp->classify = tp->ops->classify;
158 tp->protocol = protocol;
160 tp->classid = parent;
164 err = tp->ops->init(tp);
166 module_put(tp->ops->owner);
176 static void tcf_proto_destroy(struct tcf_proto *tp)
178 tp->ops->destroy(tp);
179 module_put(tp->ops->owner);
183 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
186 struct tcf_chain *chain;
188 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
191 list_add_tail(&chain->list, &block->chain_list);
192 chain->block = block;
193 chain->index = chain_index;
198 static void tcf_chain_flush(struct tcf_chain *chain)
200 struct tcf_proto *tp;
202 if (chain->p_filter_chain)
203 RCU_INIT_POINTER(*chain->p_filter_chain, NULL);
204 while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
205 RCU_INIT_POINTER(chain->filter_chain, tp->next);
206 tcf_chain_put(chain);
207 tcf_proto_destroy(tp);
211 static void tcf_chain_destroy(struct tcf_chain *chain)
213 list_del(&chain->list);
217 static void tcf_chain_hold(struct tcf_chain *chain)
222 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
225 struct tcf_chain *chain;
227 list_for_each_entry(chain, &block->chain_list, list) {
228 if (chain->index == chain_index) {
229 tcf_chain_hold(chain);
234 return create ? tcf_chain_create(block, chain_index) : NULL;
236 EXPORT_SYMBOL(tcf_chain_get);
238 void tcf_chain_put(struct tcf_chain *chain)
240 if (--chain->refcnt == 0)
241 tcf_chain_destroy(chain);
243 EXPORT_SYMBOL(tcf_chain_put);
246 tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
247 struct tcf_proto __rcu **p_filter_chain)
249 chain->p_filter_chain = p_filter_chain;
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,
280 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
281 struct tcf_block_ext_info *ei)
283 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
284 struct tcf_chain *chain;
289 INIT_LIST_HEAD(&block->chain_list);
290 INIT_LIST_HEAD(&block->cb_list);
292 /* Create chain 0 by default, it has to be always present. */
293 chain = tcf_chain_create(block, 0);
296 goto err_chain_create;
298 tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
299 block->net = qdisc_net(q);
301 tcf_block_offload_bind(block, q, ei);
309 EXPORT_SYMBOL(tcf_block_get_ext);
311 int tcf_block_get(struct tcf_block **p_block,
312 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
314 struct tcf_block_ext_info ei = {0, };
316 return tcf_block_get_ext(p_block, p_filter_chain, q, &ei);
318 EXPORT_SYMBOL(tcf_block_get);
320 static void tcf_block_put_final(struct work_struct *work)
322 struct tcf_block *block = container_of(work, struct tcf_block, work);
323 struct tcf_chain *chain, *tmp;
326 /* Only chain 0 should be still here. */
327 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
328 tcf_chain_put(chain);
333 /* XXX: Standalone actions are not allowed to jump to any chain, and bound
334 * actions should be all removed after flushing. However, filters are now
335 * destroyed in tc filter workqueue with RTNL lock, they can not race here.
337 void tcf_block_put_ext(struct tcf_block *block,
338 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
339 struct tcf_block_ext_info *ei)
341 struct tcf_chain *chain, *tmp;
343 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
344 tcf_chain_flush(chain);
346 tcf_block_offload_unbind(block, q, ei);
348 INIT_WORK(&block->work, tcf_block_put_final);
349 /* Wait for existing RCU callbacks to cool down, make sure their works
350 * have been queued before this. We can not flush pending works here
351 * because we are holding the RTNL lock.
354 tcf_queue_work(&block->work);
356 EXPORT_SYMBOL(tcf_block_put_ext);
358 void tcf_block_put(struct tcf_block *block)
360 struct tcf_block_ext_info ei = {0, };
364 tcf_block_put_ext(block, NULL, block->q, &ei);
367 EXPORT_SYMBOL(tcf_block_put);
369 struct tcf_block_cb {
370 struct list_head list;
377 void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
379 return block_cb->cb_priv;
381 EXPORT_SYMBOL(tcf_block_cb_priv);
383 struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
384 tc_setup_cb_t *cb, void *cb_ident)
385 { struct tcf_block_cb *block_cb;
387 list_for_each_entry(block_cb, &block->cb_list, list)
388 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
392 EXPORT_SYMBOL(tcf_block_cb_lookup);
394 void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
398 EXPORT_SYMBOL(tcf_block_cb_incref);
400 unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
402 return --block_cb->refcnt;
404 EXPORT_SYMBOL(tcf_block_cb_decref);
406 struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
407 tc_setup_cb_t *cb, void *cb_ident,
410 struct tcf_block_cb *block_cb;
412 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
416 block_cb->cb_ident = cb_ident;
417 block_cb->cb_priv = cb_priv;
418 list_add(&block_cb->list, &block->cb_list);
421 EXPORT_SYMBOL(__tcf_block_cb_register);
423 int tcf_block_cb_register(struct tcf_block *block,
424 tc_setup_cb_t *cb, void *cb_ident,
427 struct tcf_block_cb *block_cb;
429 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
430 return block_cb ? 0 : -ENOMEM;
432 EXPORT_SYMBOL(tcf_block_cb_register);
434 void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
436 list_del(&block_cb->list);
439 EXPORT_SYMBOL(__tcf_block_cb_unregister);
441 void tcf_block_cb_unregister(struct tcf_block *block,
442 tc_setup_cb_t *cb, void *cb_ident)
444 struct tcf_block_cb *block_cb;
446 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
449 __tcf_block_cb_unregister(block_cb);
451 EXPORT_SYMBOL(tcf_block_cb_unregister);
453 static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
454 void *type_data, bool err_stop)
456 struct tcf_block_cb *block_cb;
460 list_for_each_entry(block_cb, &block->cb_list, list) {
461 err = block_cb->cb(type, type_data, block_cb->cb_priv);
472 /* Main classifier routine: scans classifier chain attached
473 * to this qdisc, (optionally) tests for protocol and asks
474 * specific classifiers.
476 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
477 struct tcf_result *res, bool compat_mode)
479 __be16 protocol = tc_skb_protocol(skb);
480 #ifdef CONFIG_NET_CLS_ACT
481 const int max_reclassify_loop = 4;
482 const struct tcf_proto *orig_tp = tp;
483 const struct tcf_proto *first_tp;
488 for (; tp; tp = rcu_dereference_bh(tp->next)) {
491 if (tp->protocol != protocol &&
492 tp->protocol != htons(ETH_P_ALL))
495 err = tp->classify(skb, tp, res);
496 #ifdef CONFIG_NET_CLS_ACT
497 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
500 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
501 first_tp = res->goto_tp;
509 return TC_ACT_UNSPEC; /* signal: continue lookup */
510 #ifdef CONFIG_NET_CLS_ACT
512 if (unlikely(limit++ >= max_reclassify_loop)) {
513 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
514 tp->q->ops->id, tp->prio & 0xffff,
515 ntohs(tp->protocol));
520 protocol = tc_skb_protocol(skb);
524 EXPORT_SYMBOL(tcf_classify);
526 struct tcf_chain_info {
527 struct tcf_proto __rcu **pprev;
528 struct tcf_proto __rcu *next;
531 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
533 return rtnl_dereference(*chain_info->pprev);
536 static void tcf_chain_tp_insert(struct tcf_chain *chain,
537 struct tcf_chain_info *chain_info,
538 struct tcf_proto *tp)
540 if (chain->p_filter_chain &&
541 *chain_info->pprev == chain->filter_chain)
542 rcu_assign_pointer(*chain->p_filter_chain, tp);
543 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
544 rcu_assign_pointer(*chain_info->pprev, tp);
545 tcf_chain_hold(chain);
548 static void tcf_chain_tp_remove(struct tcf_chain *chain,
549 struct tcf_chain_info *chain_info,
550 struct tcf_proto *tp)
552 struct tcf_proto *next = rtnl_dereference(chain_info->next);
554 if (chain->p_filter_chain && tp == chain->filter_chain)
555 RCU_INIT_POINTER(*chain->p_filter_chain, next);
556 RCU_INIT_POINTER(*chain_info->pprev, next);
557 tcf_chain_put(chain);
560 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
561 struct tcf_chain_info *chain_info,
562 u32 protocol, u32 prio,
565 struct tcf_proto **pprev;
566 struct tcf_proto *tp;
568 /* Check the chain for existence of proto-tcf with this priority */
569 for (pprev = &chain->filter_chain;
570 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
571 if (tp->prio >= prio) {
572 if (tp->prio == prio) {
574 (tp->protocol != protocol && protocol))
575 return ERR_PTR(-EINVAL);
582 chain_info->pprev = pprev;
583 chain_info->next = tp ? tp->next : NULL;
587 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
588 struct tcf_proto *tp, struct Qdisc *q, u32 parent,
589 void *fh, u32 portid, u32 seq, u16 flags, int event)
592 struct nlmsghdr *nlh;
593 unsigned char *b = skb_tail_pointer(skb);
595 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
598 tcm = nlmsg_data(nlh);
599 tcm->tcm_family = AF_UNSPEC;
602 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
603 tcm->tcm_parent = parent;
604 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
605 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
606 goto nla_put_failure;
607 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
608 goto nla_put_failure;
612 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
613 goto nla_put_failure;
615 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
624 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
625 struct nlmsghdr *n, struct tcf_proto *tp,
626 struct Qdisc *q, u32 parent,
627 void *fh, int event, bool unicast)
630 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
632 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
636 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
637 n->nlmsg_flags, event) <= 0) {
643 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
645 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
646 n->nlmsg_flags & NLM_F_ECHO);
649 static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
650 struct nlmsghdr *n, struct tcf_proto *tp,
651 struct Qdisc *q, u32 parent,
652 void *fh, bool unicast, bool *last)
655 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
658 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
662 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
663 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
668 err = tp->ops->delete(tp, fh, last);
675 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
677 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
678 n->nlmsg_flags & NLM_F_ECHO);
681 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
682 struct Qdisc *q, u32 parent,
684 struct tcf_chain *chain, int event)
686 struct tcf_proto *tp;
688 for (tp = rtnl_dereference(chain->filter_chain);
689 tp; tp = rtnl_dereference(tp->next))
690 tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
693 /* Add/change/delete/get a filter node */
695 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
696 struct netlink_ext_ack *extack)
698 struct net *net = sock_net(skb->sk);
699 struct nlattr *tca[TCA_MAX + 1];
706 struct net_device *dev;
708 struct tcf_chain_info chain_info;
709 struct tcf_chain *chain = NULL;
710 struct tcf_block *block;
711 struct tcf_proto *tp;
712 const struct Qdisc_class_ops *cops;
718 if ((n->nlmsg_type != RTM_GETTFILTER) &&
719 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
725 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
730 protocol = TC_H_MIN(t->tcm_info);
731 prio = TC_H_MAJ(t->tcm_info);
732 prio_allocate = false;
733 parent = t->tcm_parent;
737 switch (n->nlmsg_type) {
739 if (protocol || t->tcm_handle || tca[TCA_KIND])
743 /* If no priority is provided by the user,
746 if (n->nlmsg_flags & NLM_F_CREATE) {
747 prio = TC_H_MAKE(0x80000000U, 0U);
748 prio_allocate = true;
757 /* Find head of filter chain. */
760 dev = __dev_get_by_index(net, t->tcm_ifindex);
769 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
774 /* Is it classful? */
775 cops = q->ops->cl_ops;
779 if (!cops->tcf_block)
782 /* Do we search for filter, attached to class? */
783 if (TC_H_MIN(parent)) {
784 cl = cops->find(q, parent);
789 /* And the last stroke */
790 block = cops->tcf_block(q, cl);
796 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
797 if (chain_index > TC_ACT_EXT_VAL_MASK) {
801 chain = tcf_chain_get(block, chain_index,
802 n->nlmsg_type == RTM_NEWTFILTER);
804 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
808 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
809 tfilter_notify_chain(net, skb, q, parent, n,
810 chain, RTM_DELTFILTER);
811 tcf_chain_flush(chain);
816 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
817 prio, prio_allocate);
824 /* Proto-tcf does not exist, create new one */
826 if (tca[TCA_KIND] == NULL || !protocol) {
831 if (n->nlmsg_type != RTM_NEWTFILTER ||
832 !(n->nlmsg_flags & NLM_F_CREATE)) {
838 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
840 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
841 protocol, prio, parent, q, chain);
847 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
852 fh = tp->ops->get(tp, t->tcm_handle);
855 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
856 tcf_chain_tp_remove(chain, &chain_info, tp);
857 tfilter_notify(net, skb, n, tp, q, parent, fh,
858 RTM_DELTFILTER, false);
859 tcf_proto_destroy(tp);
864 if (n->nlmsg_type != RTM_NEWTFILTER ||
865 !(n->nlmsg_flags & NLM_F_CREATE)) {
872 switch (n->nlmsg_type) {
874 if (n->nlmsg_flags & NLM_F_EXCL) {
876 tcf_proto_destroy(tp);
882 err = tfilter_del_notify(net, skb, n, tp, q, parent,
887 tcf_chain_tp_remove(chain, &chain_info, tp);
888 tcf_proto_destroy(tp);
892 err = tfilter_notify(net, skb, n, tp, q, parent, fh,
893 RTM_NEWTFILTER, true);
901 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
902 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
905 tcf_chain_tp_insert(chain, &chain_info, tp);
906 tfilter_notify(net, skb, n, tp, q, parent, fh,
907 RTM_NEWTFILTER, false);
910 tcf_proto_destroy(tp);
915 tcf_chain_put(chain);
917 /* Replay the request. */
922 struct tcf_dump_args {
925 struct netlink_callback *cb;
930 static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
932 struct tcf_dump_args *a = (void *)arg;
933 struct net *net = sock_net(a->skb->sk);
935 return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
936 n, NETLINK_CB(a->cb->skb).portid,
937 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
941 static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
942 struct sk_buff *skb, struct netlink_callback *cb,
943 long index_start, long *p_index)
945 struct net *net = sock_net(skb->sk);
946 struct tcmsg *tcm = nlmsg_data(cb->nlh);
947 struct tcf_dump_args arg;
948 struct tcf_proto *tp;
950 for (tp = rtnl_dereference(chain->filter_chain);
951 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
952 if (*p_index < index_start)
954 if (TC_H_MAJ(tcm->tcm_info) &&
955 TC_H_MAJ(tcm->tcm_info) != tp->prio)
957 if (TC_H_MIN(tcm->tcm_info) &&
958 TC_H_MIN(tcm->tcm_info) != tp->protocol)
960 if (*p_index > index_start)
961 memset(&cb->args[1], 0,
962 sizeof(cb->args) - sizeof(cb->args[0]));
963 if (cb->args[1] == 0) {
964 if (tcf_fill_node(net, skb, tp, q, parent, 0,
965 NETLINK_CB(cb->skb).portid,
966 cb->nlh->nlmsg_seq, NLM_F_MULTI,
967 RTM_NEWTFILTER) <= 0)
974 arg.w.fn = tcf_node_dump;
980 arg.w.skip = cb->args[1] - 1;
982 tp->ops->walk(tp, &arg.w);
983 cb->args[1] = arg.w.count + 1;
990 /* called with RTNL */
991 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
993 struct net *net = sock_net(skb->sk);
994 struct nlattr *tca[TCA_MAX + 1];
995 struct net_device *dev;
997 struct tcf_block *block;
998 struct tcf_chain *chain;
999 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1000 unsigned long cl = 0;
1001 const struct Qdisc_class_ops *cops;
1007 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1010 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1014 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1018 parent = tcm->tcm_parent;
1023 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1027 cops = q->ops->cl_ops;
1030 if (!cops->tcf_block)
1032 if (TC_H_MIN(tcm->tcm_parent)) {
1033 cl = cops->find(q, tcm->tcm_parent);
1037 block = cops->tcf_block(q, cl);
1041 index_start = cb->args[0];
1044 list_for_each_entry(chain, &block->chain_list, list) {
1045 if (tca[TCA_CHAIN] &&
1046 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1048 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1049 index_start, &index))
1053 cb->args[0] = index;
1059 void tcf_exts_destroy(struct tcf_exts *exts)
1061 #ifdef CONFIG_NET_CLS_ACT
1065 tcf_exts_to_list(exts, &actions);
1066 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1067 kfree(exts->actions);
1068 exts->nr_actions = 0;
1071 EXPORT_SYMBOL(tcf_exts_destroy);
1073 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
1074 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
1076 #ifdef CONFIG_NET_CLS_ACT
1078 struct tc_action *act;
1080 if (exts->police && tb[exts->police]) {
1081 act = tcf_action_init_1(net, tp, tb[exts->police],
1082 rate_tlv, "police", ovr,
1085 return PTR_ERR(act);
1087 act->type = exts->type = TCA_OLD_COMPAT;
1088 exts->actions[0] = act;
1089 exts->nr_actions = 1;
1090 } else if (exts->action && tb[exts->action]) {
1094 err = tcf_action_init(net, tp, tb[exts->action],
1095 rate_tlv, NULL, ovr, TCA_ACT_BIND,
1099 list_for_each_entry(act, &actions, list)
1100 exts->actions[i++] = act;
1101 exts->nr_actions = i;
1105 if ((exts->action && tb[exts->action]) ||
1106 (exts->police && tb[exts->police]))
1112 EXPORT_SYMBOL(tcf_exts_validate);
1114 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1116 #ifdef CONFIG_NET_CLS_ACT
1117 struct tcf_exts old = *dst;
1120 tcf_exts_destroy(&old);
1123 EXPORT_SYMBOL(tcf_exts_change);
1125 #ifdef CONFIG_NET_CLS_ACT
1126 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1128 if (exts->nr_actions == 0)
1131 return exts->actions[0];
1135 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1137 #ifdef CONFIG_NET_CLS_ACT
1138 struct nlattr *nest;
1140 if (exts->action && tcf_exts_has_actions(exts)) {
1142 * again for backward compatible mode - we want
1143 * to work with both old and new modes of entering
1144 * tc data even if iproute2 was newer - jhs
1146 if (exts->type != TCA_OLD_COMPAT) {
1149 nest = nla_nest_start(skb, exts->action);
1151 goto nla_put_failure;
1153 tcf_exts_to_list(exts, &actions);
1154 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
1155 goto nla_put_failure;
1156 nla_nest_end(skb, nest);
1157 } else if (exts->police) {
1158 struct tc_action *act = tcf_exts_first_act(exts);
1159 nest = nla_nest_start(skb, exts->police);
1160 if (nest == NULL || !act)
1161 goto nla_put_failure;
1162 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
1163 goto nla_put_failure;
1164 nla_nest_end(skb, nest);
1170 nla_nest_cancel(skb, nest);
1176 EXPORT_SYMBOL(tcf_exts_dump);
1179 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1181 #ifdef CONFIG_NET_CLS_ACT
1182 struct tc_action *a = tcf_exts_first_act(exts);
1183 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
1188 EXPORT_SYMBOL(tcf_exts_dump_stats);
1190 static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1191 enum tc_setup_type type,
1192 void *type_data, bool err_stop)
1195 #ifdef CONFIG_NET_CLS_ACT
1196 const struct tc_action *a;
1197 struct net_device *dev;
1200 if (!tcf_exts_has_actions(exts))
1203 for (i = 0; i < exts->nr_actions; i++) {
1204 a = exts->actions[i];
1205 if (!a->ops->get_dev)
1207 dev = a->ops->get_dev(a);
1210 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1219 int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1220 enum tc_setup_type type, void *type_data, bool err_stop)
1225 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1232 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1239 EXPORT_SYMBOL(tc_setup_cb_call);
1241 static int __init tc_filter_init(void)
1243 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1247 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1248 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
1249 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
1250 tc_dump_tfilter, 0);
1255 subsys_initcall(tc_filter_init);