net/sched: fix filter flushing
[linux-2.6/btrfs-unstable.git] / net / sched / cls_api.c
blob01a8b8b4bab870457e105aceb62dbd8b61606cec
1 /*
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>
11 * Changes:
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>
29 #include <net/sock.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;
46 if (kind) {
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))
51 res = t;
52 break;
55 read_unlock(&cls_mod_lock);
57 return res;
60 /* Register(unregister) new classifier type */
62 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
64 struct tcf_proto_ops *t;
65 int rc = -EEXIST;
67 write_lock(&cls_mod_lock);
68 list_for_each_entry(t, &tcf_proto_base, head)
69 if (!strcmp(ops->kind, t->kind))
70 goto out;
72 list_add_tail(&ops->head, &tcf_proto_base);
73 rc = 0;
74 out:
75 write_unlock(&cls_mod_lock);
76 return rc;
78 EXPORT_SYMBOL(register_tcf_proto_ops);
80 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
82 struct tcf_proto_ops *t;
83 int rc = -ENOENT;
85 /* Wait for outstanding call_rcu()s, if any, from a
86 * tcf_proto_ops's destroy() handler.
88 rcu_barrier();
90 write_lock(&cls_mod_lock);
91 list_for_each_entry(t, &tcf_proto_base, head) {
92 if (t == ops) {
93 list_del(&t->head);
94 rc = 0;
95 break;
98 write_unlock(&cls_mod_lock);
99 return rc;
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,
108 struct nlmsghdr *n,
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);
124 if (tp)
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_chain *chain)
134 struct tcf_proto *tp;
135 int err;
137 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
138 if (!tp)
139 return ERR_PTR(-ENOBUFS);
141 err = -ENOENT;
142 tp->ops = tcf_proto_lookup_ops(kind);
143 if (!tp->ops) {
144 #ifdef CONFIG_MODULES
145 rtnl_unlock();
146 request_module("cls_%s", kind);
147 rtnl_lock();
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.
154 if (tp->ops) {
155 module_put(tp->ops->owner);
156 err = -EAGAIN;
157 } else {
158 err = -ENOENT;
160 goto errout;
161 #endif
163 tp->classify = tp->ops->classify;
164 tp->protocol = protocol;
165 tp->prio = prio;
166 tp->classid = parent;
167 tp->q = q;
168 tp->chain = chain;
170 err = tp->ops->init(tp);
171 if (err) {
172 module_put(tp->ops->owner);
173 goto errout;
175 return tp;
177 errout:
178 kfree(tp);
179 return ERR_PTR(err);
182 static void tcf_proto_destroy(struct tcf_proto *tp)
184 tp->ops->destroy(tp);
185 module_put(tp->ops->owner);
186 kfree_rcu(tp, rcu);
189 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
190 u32 chain_index)
192 struct tcf_chain *chain;
194 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
195 if (!chain)
196 return NULL;
197 list_add_tail(&chain->list, &block->chain_list);
198 chain->block = block;
199 chain->index = chain_index;
200 chain->refcnt = 1;
201 return chain;
204 static void tcf_chain_flush(struct tcf_chain *chain)
206 struct tcf_proto *tp;
208 if (*chain->p_filter_chain)
209 RCU_INIT_POINTER(*chain->p_filter_chain, NULL);
210 while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
211 RCU_INIT_POINTER(chain->filter_chain, tp->next);
212 tcf_proto_destroy(tp);
216 static void tcf_chain_destroy(struct tcf_chain *chain)
218 list_del(&chain->list);
219 tcf_chain_flush(chain);
220 kfree(chain);
223 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 chain->refcnt++;
230 return chain;
233 return tcf_chain_create(block, chain_index);
235 EXPORT_SYMBOL(tcf_chain_get);
237 void tcf_chain_put(struct tcf_chain *chain)
239 /* Destroy unused chain, with exception of chain 0, which is the
240 * default one and has to be always present.
242 if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
243 tcf_chain_destroy(chain);
245 EXPORT_SYMBOL(tcf_chain_put);
247 static void
248 tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
249 struct tcf_proto __rcu **p_filter_chain)
251 chain->p_filter_chain = p_filter_chain;
254 int tcf_block_get(struct tcf_block **p_block,
255 struct tcf_proto __rcu **p_filter_chain)
257 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
258 struct tcf_chain *chain;
259 int err;
261 if (!block)
262 return -ENOMEM;
263 INIT_LIST_HEAD(&block->chain_list);
264 /* Create chain 0 by default, it has to be always present. */
265 chain = tcf_chain_create(block, 0);
266 if (!chain) {
267 err = -ENOMEM;
268 goto err_chain_create;
270 tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
271 *p_block = block;
272 return 0;
274 err_chain_create:
275 kfree(block);
276 return err;
278 EXPORT_SYMBOL(tcf_block_get);
280 void tcf_block_put(struct tcf_block *block)
282 struct tcf_chain *chain, *tmp;
284 if (!block)
285 return;
287 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
288 tcf_chain_destroy(chain);
289 kfree(block);
291 EXPORT_SYMBOL(tcf_block_put);
293 /* Main classifier routine: scans classifier chain attached
294 * to this qdisc, (optionally) tests for protocol and asks
295 * specific classifiers.
297 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
298 struct tcf_result *res, bool compat_mode)
300 __be16 protocol = tc_skb_protocol(skb);
301 #ifdef CONFIG_NET_CLS_ACT
302 const int max_reclassify_loop = 4;
303 const struct tcf_proto *old_tp = tp;
304 int limit = 0;
306 reclassify:
307 #endif
308 for (; tp; tp = rcu_dereference_bh(tp->next)) {
309 int err;
311 if (tp->protocol != protocol &&
312 tp->protocol != htons(ETH_P_ALL))
313 continue;
315 err = tp->classify(skb, tp, res);
316 #ifdef CONFIG_NET_CLS_ACT
317 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
318 goto reset;
319 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
320 old_tp = res->goto_tp;
321 goto reset;
323 #endif
324 if (err >= 0)
325 return err;
328 return TC_ACT_UNSPEC; /* signal: continue lookup */
329 #ifdef CONFIG_NET_CLS_ACT
330 reset:
331 if (unlikely(limit++ >= max_reclassify_loop)) {
332 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
333 tp->q->ops->id, tp->prio & 0xffff,
334 ntohs(tp->protocol));
335 return TC_ACT_SHOT;
338 tp = old_tp;
339 protocol = tc_skb_protocol(skb);
340 goto reclassify;
341 #endif
343 EXPORT_SYMBOL(tcf_classify);
345 struct tcf_chain_info {
346 struct tcf_proto __rcu **pprev;
347 struct tcf_proto __rcu *next;
350 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
352 return rtnl_dereference(*chain_info->pprev);
355 static void tcf_chain_tp_insert(struct tcf_chain *chain,
356 struct tcf_chain_info *chain_info,
357 struct tcf_proto *tp)
359 if (chain->p_filter_chain &&
360 *chain_info->pprev == chain->filter_chain)
361 rcu_assign_pointer(*chain->p_filter_chain, tp);
362 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
363 rcu_assign_pointer(*chain_info->pprev, tp);
366 static void tcf_chain_tp_remove(struct tcf_chain *chain,
367 struct tcf_chain_info *chain_info,
368 struct tcf_proto *tp)
370 struct tcf_proto *next = rtnl_dereference(chain_info->next);
372 if (chain->p_filter_chain && tp == chain->filter_chain)
373 RCU_INIT_POINTER(*chain->p_filter_chain, next);
374 RCU_INIT_POINTER(*chain_info->pprev, next);
377 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
378 struct tcf_chain_info *chain_info,
379 u32 protocol, u32 prio,
380 bool prio_allocate)
382 struct tcf_proto **pprev;
383 struct tcf_proto *tp;
385 /* Check the chain for existence of proto-tcf with this priority */
386 for (pprev = &chain->filter_chain;
387 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
388 if (tp->prio >= prio) {
389 if (tp->prio == prio) {
390 if (prio_allocate ||
391 (tp->protocol != protocol && protocol))
392 return ERR_PTR(-EINVAL);
393 } else {
394 tp = NULL;
396 break;
399 chain_info->pprev = pprev;
400 chain_info->next = tp ? tp->next : NULL;
401 return tp;
404 /* Add/change/delete/get a filter node */
406 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
407 struct netlink_ext_ack *extack)
409 struct net *net = sock_net(skb->sk);
410 struct nlattr *tca[TCA_MAX + 1];
411 struct tcmsg *t;
412 u32 protocol;
413 u32 prio;
414 bool prio_allocate;
415 u32 parent;
416 u32 chain_index;
417 struct net_device *dev;
418 struct Qdisc *q;
419 struct tcf_chain_info chain_info;
420 struct tcf_chain *chain = NULL;
421 struct tcf_block *block;
422 struct tcf_proto *tp;
423 const struct Qdisc_class_ops *cops;
424 unsigned long cl;
425 unsigned long fh;
426 int err;
427 int tp_created;
429 if ((n->nlmsg_type != RTM_GETTFILTER) &&
430 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
431 return -EPERM;
433 replay:
434 tp_created = 0;
436 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
437 if (err < 0)
438 return err;
440 t = nlmsg_data(n);
441 protocol = TC_H_MIN(t->tcm_info);
442 prio = TC_H_MAJ(t->tcm_info);
443 prio_allocate = false;
444 parent = t->tcm_parent;
445 cl = 0;
447 if (prio == 0) {
448 switch (n->nlmsg_type) {
449 case RTM_DELTFILTER:
450 if (protocol || t->tcm_handle || tca[TCA_KIND])
451 return -ENOENT;
452 break;
453 case RTM_NEWTFILTER:
454 /* If no priority is provided by the user,
455 * we allocate one.
457 if (n->nlmsg_flags & NLM_F_CREATE) {
458 prio = TC_H_MAKE(0x80000000U, 0U);
459 prio_allocate = true;
460 break;
462 /* fall-through */
463 default:
464 return -ENOENT;
468 /* Find head of filter chain. */
470 /* Find link */
471 dev = __dev_get_by_index(net, t->tcm_ifindex);
472 if (dev == NULL)
473 return -ENODEV;
475 /* Find qdisc */
476 if (!parent) {
477 q = dev->qdisc;
478 parent = q->handle;
479 } else {
480 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
481 if (q == NULL)
482 return -EINVAL;
485 /* Is it classful? */
486 cops = q->ops->cl_ops;
487 if (!cops)
488 return -EINVAL;
490 if (!cops->tcf_block)
491 return -EOPNOTSUPP;
493 /* Do we search for filter, attached to class? */
494 if (TC_H_MIN(parent)) {
495 cl = cops->get(q, parent);
496 if (cl == 0)
497 return -ENOENT;
500 /* And the last stroke */
501 block = cops->tcf_block(q, cl);
502 if (!block) {
503 err = -EINVAL;
504 goto errout;
507 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
508 if (chain_index > TC_ACT_EXT_VAL_MASK) {
509 err = -EINVAL;
510 goto errout;
512 chain = tcf_chain_get(block, chain_index);
513 if (!chain) {
514 err = -ENOMEM;
515 goto errout;
518 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
519 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
520 tcf_chain_flush(chain);
521 err = 0;
522 goto errout;
525 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
526 prio, prio_allocate);
527 if (IS_ERR(tp)) {
528 err = PTR_ERR(tp);
529 goto errout;
532 if (tp == NULL) {
533 /* Proto-tcf does not exist, create new one */
535 if (tca[TCA_KIND] == NULL || !protocol) {
536 err = -EINVAL;
537 goto errout;
540 if (n->nlmsg_type != RTM_NEWTFILTER ||
541 !(n->nlmsg_flags & NLM_F_CREATE)) {
542 err = -ENOENT;
543 goto errout;
546 if (prio_allocate)
547 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
549 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
550 protocol, prio, parent, q, chain);
551 if (IS_ERR(tp)) {
552 err = PTR_ERR(tp);
553 goto errout;
555 tp_created = 1;
556 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
557 err = -EINVAL;
558 goto errout;
561 fh = tp->ops->get(tp, t->tcm_handle);
563 if (fh == 0) {
564 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
565 tcf_chain_tp_remove(chain, &chain_info, tp);
566 tfilter_notify(net, skb, n, tp, fh,
567 RTM_DELTFILTER, false);
568 tcf_proto_destroy(tp);
569 err = 0;
570 goto errout;
573 if (n->nlmsg_type != RTM_NEWTFILTER ||
574 !(n->nlmsg_flags & NLM_F_CREATE)) {
575 err = -ENOENT;
576 goto errout;
578 } else {
579 bool last;
581 switch (n->nlmsg_type) {
582 case RTM_NEWTFILTER:
583 if (n->nlmsg_flags & NLM_F_EXCL) {
584 if (tp_created)
585 tcf_proto_destroy(tp);
586 err = -EEXIST;
587 goto errout;
589 break;
590 case RTM_DELTFILTER:
591 err = tp->ops->delete(tp, fh, &last);
592 if (err)
593 goto errout;
594 tfilter_notify(net, skb, n, tp, t->tcm_handle,
595 RTM_DELTFILTER, false);
596 if (last) {
597 tcf_chain_tp_remove(chain, &chain_info, tp);
598 tcf_proto_destroy(tp);
600 goto errout;
601 case RTM_GETTFILTER:
602 err = tfilter_notify(net, skb, n, tp, fh,
603 RTM_NEWTFILTER, true);
604 goto errout;
605 default:
606 err = -EINVAL;
607 goto errout;
611 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
612 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
613 if (err == 0) {
614 if (tp_created)
615 tcf_chain_tp_insert(chain, &chain_info, tp);
616 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
617 } else {
618 if (tp_created)
619 tcf_proto_destroy(tp);
622 errout:
623 if (chain)
624 tcf_chain_put(chain);
625 if (cl)
626 cops->put(q, cl);
627 if (err == -EAGAIN)
628 /* Replay the request. */
629 goto replay;
630 return err;
633 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
634 struct tcf_proto *tp, unsigned long fh, u32 portid,
635 u32 seq, u16 flags, int event)
637 struct tcmsg *tcm;
638 struct nlmsghdr *nlh;
639 unsigned char *b = skb_tail_pointer(skb);
641 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
642 if (!nlh)
643 goto out_nlmsg_trim;
644 tcm = nlmsg_data(nlh);
645 tcm->tcm_family = AF_UNSPEC;
646 tcm->tcm__pad1 = 0;
647 tcm->tcm__pad2 = 0;
648 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
649 tcm->tcm_parent = tp->classid;
650 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
651 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
652 goto nla_put_failure;
653 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
654 goto nla_put_failure;
655 tcm->tcm_handle = fh;
656 if (RTM_DELTFILTER != event) {
657 tcm->tcm_handle = 0;
658 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
659 goto nla_put_failure;
661 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
662 return skb->len;
664 out_nlmsg_trim:
665 nla_put_failure:
666 nlmsg_trim(skb, b);
667 return -1;
670 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
671 struct nlmsghdr *n, struct tcf_proto *tp,
672 unsigned long fh, int event, bool unicast)
674 struct sk_buff *skb;
675 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
677 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
678 if (!skb)
679 return -ENOBUFS;
681 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
682 n->nlmsg_flags, event) <= 0) {
683 kfree_skb(skb);
684 return -EINVAL;
687 if (unicast)
688 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
690 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
691 n->nlmsg_flags & NLM_F_ECHO);
694 struct tcf_dump_args {
695 struct tcf_walker w;
696 struct sk_buff *skb;
697 struct netlink_callback *cb;
700 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
701 struct tcf_walker *arg)
703 struct tcf_dump_args *a = (void *)arg;
704 struct net *net = sock_net(a->skb->sk);
706 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
707 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
708 RTM_NEWTFILTER);
711 static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
712 struct netlink_callback *cb,
713 long index_start, long *p_index)
715 struct net *net = sock_net(skb->sk);
716 struct tcmsg *tcm = nlmsg_data(cb->nlh);
717 struct tcf_dump_args arg;
718 struct tcf_proto *tp;
720 for (tp = rtnl_dereference(chain->filter_chain);
721 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
722 if (*p_index < index_start)
723 continue;
724 if (TC_H_MAJ(tcm->tcm_info) &&
725 TC_H_MAJ(tcm->tcm_info) != tp->prio)
726 continue;
727 if (TC_H_MIN(tcm->tcm_info) &&
728 TC_H_MIN(tcm->tcm_info) != tp->protocol)
729 continue;
730 if (*p_index > index_start)
731 memset(&cb->args[1], 0,
732 sizeof(cb->args) - sizeof(cb->args[0]));
733 if (cb->args[1] == 0) {
734 if (tcf_fill_node(net, skb, tp, 0,
735 NETLINK_CB(cb->skb).portid,
736 cb->nlh->nlmsg_seq, NLM_F_MULTI,
737 RTM_NEWTFILTER) <= 0)
738 return false;
740 cb->args[1] = 1;
742 if (!tp->ops->walk)
743 continue;
744 arg.w.fn = tcf_node_dump;
745 arg.skb = skb;
746 arg.cb = cb;
747 arg.w.stop = 0;
748 arg.w.skip = cb->args[1] - 1;
749 arg.w.count = 0;
750 tp->ops->walk(tp, &arg.w);
751 cb->args[1] = arg.w.count + 1;
752 if (arg.w.stop)
753 return false;
755 return true;
758 /* called with RTNL */
759 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
761 struct net *net = sock_net(skb->sk);
762 struct nlattr *tca[TCA_MAX + 1];
763 struct net_device *dev;
764 struct Qdisc *q;
765 struct tcf_block *block;
766 struct tcf_chain *chain;
767 struct tcmsg *tcm = nlmsg_data(cb->nlh);
768 unsigned long cl = 0;
769 const struct Qdisc_class_ops *cops;
770 long index_start;
771 long index;
772 int err;
774 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
775 return skb->len;
777 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
778 if (err)
779 return err;
781 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
782 if (!dev)
783 return skb->len;
785 if (!tcm->tcm_parent)
786 q = dev->qdisc;
787 else
788 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
789 if (!q)
790 goto out;
791 cops = q->ops->cl_ops;
792 if (!cops)
793 goto errout;
794 if (!cops->tcf_block)
795 goto errout;
796 if (TC_H_MIN(tcm->tcm_parent)) {
797 cl = cops->get(q, tcm->tcm_parent);
798 if (cl == 0)
799 goto errout;
801 block = cops->tcf_block(q, cl);
802 if (!block)
803 goto errout;
805 index_start = cb->args[0];
806 index = 0;
808 list_for_each_entry(chain, &block->chain_list, list) {
809 if (tca[TCA_CHAIN] &&
810 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
811 continue;
812 if (!tcf_chain_dump(chain, skb, cb, index_start, &index))
813 break;
816 cb->args[0] = index;
818 errout:
819 if (cl)
820 cops->put(q, cl);
821 out:
822 return skb->len;
825 void tcf_exts_destroy(struct tcf_exts *exts)
827 #ifdef CONFIG_NET_CLS_ACT
828 LIST_HEAD(actions);
830 tcf_exts_to_list(exts, &actions);
831 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
832 kfree(exts->actions);
833 exts->nr_actions = 0;
834 #endif
836 EXPORT_SYMBOL(tcf_exts_destroy);
838 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
839 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
841 #ifdef CONFIG_NET_CLS_ACT
843 struct tc_action *act;
845 if (exts->police && tb[exts->police]) {
846 act = tcf_action_init_1(net, tp, tb[exts->police],
847 rate_tlv, "police", ovr,
848 TCA_ACT_BIND);
849 if (IS_ERR(act))
850 return PTR_ERR(act);
852 act->type = exts->type = TCA_OLD_COMPAT;
853 exts->actions[0] = act;
854 exts->nr_actions = 1;
855 } else if (exts->action && tb[exts->action]) {
856 LIST_HEAD(actions);
857 int err, i = 0;
859 err = tcf_action_init(net, tp, tb[exts->action],
860 rate_tlv, NULL, ovr, TCA_ACT_BIND,
861 &actions);
862 if (err)
863 return err;
864 list_for_each_entry(act, &actions, list)
865 exts->actions[i++] = act;
866 exts->nr_actions = i;
869 #else
870 if ((exts->action && tb[exts->action]) ||
871 (exts->police && tb[exts->police]))
872 return -EOPNOTSUPP;
873 #endif
875 return 0;
877 EXPORT_SYMBOL(tcf_exts_validate);
879 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
880 struct tcf_exts *src)
882 #ifdef CONFIG_NET_CLS_ACT
883 struct tcf_exts old = *dst;
885 tcf_tree_lock(tp);
886 dst->nr_actions = src->nr_actions;
887 dst->actions = src->actions;
888 dst->type = src->type;
889 tcf_tree_unlock(tp);
891 tcf_exts_destroy(&old);
892 #endif
894 EXPORT_SYMBOL(tcf_exts_change);
896 #ifdef CONFIG_NET_CLS_ACT
897 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
899 if (exts->nr_actions == 0)
900 return NULL;
901 else
902 return exts->actions[0];
904 #endif
906 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
908 #ifdef CONFIG_NET_CLS_ACT
909 struct nlattr *nest;
911 if (exts->action && exts->nr_actions) {
913 * again for backward compatible mode - we want
914 * to work with both old and new modes of entering
915 * tc data even if iproute2 was newer - jhs
917 if (exts->type != TCA_OLD_COMPAT) {
918 LIST_HEAD(actions);
920 nest = nla_nest_start(skb, exts->action);
921 if (nest == NULL)
922 goto nla_put_failure;
924 tcf_exts_to_list(exts, &actions);
925 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
926 goto nla_put_failure;
927 nla_nest_end(skb, nest);
928 } else if (exts->police) {
929 struct tc_action *act = tcf_exts_first_act(exts);
930 nest = nla_nest_start(skb, exts->police);
931 if (nest == NULL || !act)
932 goto nla_put_failure;
933 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
934 goto nla_put_failure;
935 nla_nest_end(skb, nest);
938 return 0;
940 nla_put_failure:
941 nla_nest_cancel(skb, nest);
942 return -1;
943 #else
944 return 0;
945 #endif
947 EXPORT_SYMBOL(tcf_exts_dump);
950 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
952 #ifdef CONFIG_NET_CLS_ACT
953 struct tc_action *a = tcf_exts_first_act(exts);
954 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
955 return -1;
956 #endif
957 return 0;
959 EXPORT_SYMBOL(tcf_exts_dump_stats);
961 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
962 struct net_device **hw_dev)
964 #ifdef CONFIG_NET_CLS_ACT
965 const struct tc_action *a;
966 LIST_HEAD(actions);
968 if (tc_no_actions(exts))
969 return -EINVAL;
971 tcf_exts_to_list(exts, &actions);
972 list_for_each_entry(a, &actions, list) {
973 if (a->ops->get_dev) {
974 a->ops->get_dev(a, dev_net(dev), hw_dev);
975 break;
978 if (*hw_dev)
979 return 0;
980 #endif
981 return -EOPNOTSUPP;
983 EXPORT_SYMBOL(tcf_exts_get_dev);
985 static int __init tc_filter_init(void)
987 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
988 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
989 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
990 tc_dump_tfilter, NULL);
992 return 0;
995 subsys_initcall(tc_filter_init);