]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/sched/cls_api.c
UBUNTU: [Config] CONFIG_BCM2835_THERMAL=y
[mirror_ubuntu-artful-kernel.git] / net / sched / cls_api.c
1 /*
2 * net/sched/cls_api.c Packet classifier API.
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Changes:
12 *
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
14 *
15 */
16
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>
33
34 /* The list of all installed classifier types */
35 static LIST_HEAD(tcf_proto_base);
36
37 /* Protects list of registered TC modules. It is pure SMP lock. */
38 static DEFINE_RWLOCK(cls_mod_lock);
39
40 /* Find classifier type by string name */
41
42 static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
43 {
44 const struct tcf_proto_ops *t, *res = NULL;
45
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;
53 }
54 }
55 read_unlock(&cls_mod_lock);
56 }
57 return res;
58 }
59
60 /* Register(unregister) new classifier type */
61
62 int register_tcf_proto_ops(struct tcf_proto_ops *ops)
63 {
64 struct tcf_proto_ops *t;
65 int rc = -EEXIST;
66
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;
71
72 list_add_tail(&ops->head, &tcf_proto_base);
73 rc = 0;
74 out:
75 write_unlock(&cls_mod_lock);
76 return rc;
77 }
78 EXPORT_SYMBOL(register_tcf_proto_ops);
79
80 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
81 {
82 struct tcf_proto_ops *t;
83 int rc = -ENOENT;
84
85 /* Wait for outstanding call_rcu()s, if any, from a
86 * tcf_proto_ops's destroy() handler.
87 */
88 rcu_barrier();
89
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;
96 }
97 }
98 write_unlock(&cls_mod_lock);
99 return rc;
100 }
101 EXPORT_SYMBOL(unregister_tcf_proto_ops);
102
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);
106
107 static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
108 struct nlmsghdr *n,
109 struct tcf_chain *chain, int event)
110 {
111 struct tcf_proto *tp;
112
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);
116 }
117
118 /* Select new prio value from the range, managed by kernel. */
119
120 static inline u32 tcf_auto_prio(struct tcf_proto *tp)
121 {
122 u32 first = TC_H_MAKE(0xC0000000U, 0U);
123
124 if (tp)
125 first = tp->prio - 1;
126
127 return TC_H_MAJ(first);
128 }
129
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)
133 {
134 struct tcf_proto *tp;
135 int err;
136
137 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
138 if (!tp)
139 return ERR_PTR(-ENOBUFS);
140
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.
153 */
154 if (tp->ops) {
155 module_put(tp->ops->owner);
156 err = -EAGAIN;
157 } else {
158 err = -ENOENT;
159 }
160 goto errout;
161 #endif
162 }
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;
169
170 err = tp->ops->init(tp);
171 if (err) {
172 module_put(tp->ops->owner);
173 goto errout;
174 }
175 return tp;
176
177 errout:
178 kfree(tp);
179 return ERR_PTR(err);
180 }
181
182 static void tcf_proto_destroy(struct tcf_proto *tp)
183 {
184 tp->ops->destroy(tp);
185 module_put(tp->ops->owner);
186 kfree_rcu(tp, rcu);
187 }
188
189 static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
190 u32 chain_index)
191 {
192 struct tcf_chain *chain;
193
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;
202 }
203
204 static void tcf_chain_flush(struct tcf_chain *chain)
205 {
206 struct tcf_proto *tp;
207
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);
213 }
214 }
215
216 static void tcf_chain_destroy(struct tcf_chain *chain)
217 {
218 /* May be already removed from the list by the previous call. */
219 if (!list_empty(&chain->list))
220 list_del_init(&chain->list);
221
222 /* There might still be a reference held when we got here from
223 * tcf_block_put. Wait for the user to drop reference before free.
224 */
225 if (!chain->refcnt)
226 kfree(chain);
227 }
228
229 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
230 bool create)
231 {
232 struct tcf_chain *chain;
233
234 list_for_each_entry(chain, &block->chain_list, list) {
235 if (chain->index == chain_index) {
236 chain->refcnt++;
237 return chain;
238 }
239 }
240 if (create)
241 return tcf_chain_create(block, chain_index);
242 else
243 return NULL;
244 }
245 EXPORT_SYMBOL(tcf_chain_get);
246
247 void tcf_chain_put(struct tcf_chain *chain)
248 {
249 /* Destroy unused chain, with exception of chain 0, which is the
250 * default one and has to be always present.
251 */
252 if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
253 tcf_chain_destroy(chain);
254 }
255 EXPORT_SYMBOL(tcf_chain_put);
256
257 static void
258 tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
259 struct tcf_proto __rcu **p_filter_chain)
260 {
261 chain->p_filter_chain = p_filter_chain;
262 }
263
264 int tcf_block_get(struct tcf_block **p_block,
265 struct tcf_proto __rcu **p_filter_chain)
266 {
267 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
268 struct tcf_chain *chain;
269 int err;
270
271 if (!block)
272 return -ENOMEM;
273 INIT_LIST_HEAD(&block->chain_list);
274 /* Create chain 0 by default, it has to be always present. */
275 chain = tcf_chain_create(block, 0);
276 if (!chain) {
277 err = -ENOMEM;
278 goto err_chain_create;
279 }
280 tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
281 *p_block = block;
282 return 0;
283
284 err_chain_create:
285 kfree(block);
286 return err;
287 }
288 EXPORT_SYMBOL(tcf_block_get);
289
290 void tcf_block_put(struct tcf_block *block)
291 {
292 struct tcf_chain *chain, *tmp;
293
294 if (!block)
295 return;
296
297 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
298 tcf_chain_flush(chain);
299 tcf_chain_destroy(chain);
300 }
301 kfree(block);
302 }
303 EXPORT_SYMBOL(tcf_block_put);
304
305 /* Main classifier routine: scans classifier chain attached
306 * to this qdisc, (optionally) tests for protocol and asks
307 * specific classifiers.
308 */
309 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
310 struct tcf_result *res, bool compat_mode)
311 {
312 __be16 protocol = tc_skb_protocol(skb);
313 #ifdef CONFIG_NET_CLS_ACT
314 const int max_reclassify_loop = 4;
315 const struct tcf_proto *orig_tp = tp;
316 const struct tcf_proto *first_tp;
317 int limit = 0;
318
319 reclassify:
320 #endif
321 for (; tp; tp = rcu_dereference_bh(tp->next)) {
322 int err;
323
324 if (tp->protocol != protocol &&
325 tp->protocol != htons(ETH_P_ALL))
326 continue;
327
328 err = tp->classify(skb, tp, res);
329 #ifdef CONFIG_NET_CLS_ACT
330 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
331 first_tp = orig_tp;
332 goto reset;
333 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
334 first_tp = res->goto_tp;
335 goto reset;
336 }
337 #endif
338 if (err >= 0)
339 return err;
340 }
341
342 return TC_ACT_UNSPEC; /* signal: continue lookup */
343 #ifdef CONFIG_NET_CLS_ACT
344 reset:
345 if (unlikely(limit++ >= max_reclassify_loop)) {
346 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
347 tp->q->ops->id, tp->prio & 0xffff,
348 ntohs(tp->protocol));
349 return TC_ACT_SHOT;
350 }
351
352 tp = first_tp;
353 protocol = tc_skb_protocol(skb);
354 goto reclassify;
355 #endif
356 }
357 EXPORT_SYMBOL(tcf_classify);
358
359 struct tcf_chain_info {
360 struct tcf_proto __rcu **pprev;
361 struct tcf_proto __rcu *next;
362 };
363
364 static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
365 {
366 return rtnl_dereference(*chain_info->pprev);
367 }
368
369 static void tcf_chain_tp_insert(struct tcf_chain *chain,
370 struct tcf_chain_info *chain_info,
371 struct tcf_proto *tp)
372 {
373 if (chain->p_filter_chain &&
374 *chain_info->pprev == chain->filter_chain)
375 rcu_assign_pointer(*chain->p_filter_chain, tp);
376 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
377 rcu_assign_pointer(*chain_info->pprev, tp);
378 }
379
380 static void tcf_chain_tp_remove(struct tcf_chain *chain,
381 struct tcf_chain_info *chain_info,
382 struct tcf_proto *tp)
383 {
384 struct tcf_proto *next = rtnl_dereference(chain_info->next);
385
386 if (chain->p_filter_chain && tp == chain->filter_chain)
387 RCU_INIT_POINTER(*chain->p_filter_chain, next);
388 RCU_INIT_POINTER(*chain_info->pprev, next);
389 }
390
391 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
392 struct tcf_chain_info *chain_info,
393 u32 protocol, u32 prio,
394 bool prio_allocate)
395 {
396 struct tcf_proto **pprev;
397 struct tcf_proto *tp;
398
399 /* Check the chain for existence of proto-tcf with this priority */
400 for (pprev = &chain->filter_chain;
401 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
402 if (tp->prio >= prio) {
403 if (tp->prio == prio) {
404 if (prio_allocate ||
405 (tp->protocol != protocol && protocol))
406 return ERR_PTR(-EINVAL);
407 } else {
408 tp = NULL;
409 }
410 break;
411 }
412 }
413 chain_info->pprev = pprev;
414 chain_info->next = tp ? tp->next : NULL;
415 return tp;
416 }
417
418 /* Add/change/delete/get a filter node */
419
420 static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
421 struct netlink_ext_ack *extack)
422 {
423 struct net *net = sock_net(skb->sk);
424 struct nlattr *tca[TCA_MAX + 1];
425 struct tcmsg *t;
426 u32 protocol;
427 u32 prio;
428 bool prio_allocate;
429 u32 parent;
430 u32 chain_index;
431 struct net_device *dev;
432 struct Qdisc *q;
433 struct tcf_chain_info chain_info;
434 struct tcf_chain *chain = NULL;
435 struct tcf_block *block;
436 struct tcf_proto *tp;
437 const struct Qdisc_class_ops *cops;
438 unsigned long cl;
439 unsigned long fh;
440 int err;
441 int tp_created;
442
443 if ((n->nlmsg_type != RTM_GETTFILTER) &&
444 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
445 return -EPERM;
446
447 replay:
448 tp_created = 0;
449
450 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
451 if (err < 0)
452 return err;
453
454 t = nlmsg_data(n);
455 protocol = TC_H_MIN(t->tcm_info);
456 prio = TC_H_MAJ(t->tcm_info);
457 prio_allocate = false;
458 parent = t->tcm_parent;
459 cl = 0;
460
461 if (prio == 0) {
462 switch (n->nlmsg_type) {
463 case RTM_DELTFILTER:
464 if (protocol || t->tcm_handle || tca[TCA_KIND])
465 return -ENOENT;
466 break;
467 case RTM_NEWTFILTER:
468 /* If no priority is provided by the user,
469 * we allocate one.
470 */
471 if (n->nlmsg_flags & NLM_F_CREATE) {
472 prio = TC_H_MAKE(0x80000000U, 0U);
473 prio_allocate = true;
474 break;
475 }
476 /* fall-through */
477 default:
478 return -ENOENT;
479 }
480 }
481
482 /* Find head of filter chain. */
483
484 /* Find link */
485 dev = __dev_get_by_index(net, t->tcm_ifindex);
486 if (dev == NULL)
487 return -ENODEV;
488
489 /* Find qdisc */
490 if (!parent) {
491 q = dev->qdisc;
492 parent = q->handle;
493 } else {
494 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
495 if (q == NULL)
496 return -EINVAL;
497 }
498
499 /* Is it classful? */
500 cops = q->ops->cl_ops;
501 if (!cops)
502 return -EINVAL;
503
504 if (!cops->tcf_block)
505 return -EOPNOTSUPP;
506
507 /* Do we search for filter, attached to class? */
508 if (TC_H_MIN(parent)) {
509 cl = cops->get(q, parent);
510 if (cl == 0)
511 return -ENOENT;
512 }
513
514 /* And the last stroke */
515 block = cops->tcf_block(q, cl);
516 if (!block) {
517 err = -EINVAL;
518 goto errout;
519 }
520
521 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
522 if (chain_index > TC_ACT_EXT_VAL_MASK) {
523 err = -EINVAL;
524 goto errout;
525 }
526 chain = tcf_chain_get(block, chain_index,
527 n->nlmsg_type == RTM_NEWTFILTER);
528 if (!chain) {
529 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
530 goto errout;
531 }
532
533 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
534 tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
535 tcf_chain_flush(chain);
536 err = 0;
537 goto errout;
538 }
539
540 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
541 prio, prio_allocate);
542 if (IS_ERR(tp)) {
543 err = PTR_ERR(tp);
544 goto errout;
545 }
546
547 if (tp == NULL) {
548 /* Proto-tcf does not exist, create new one */
549
550 if (tca[TCA_KIND] == NULL || !protocol) {
551 err = -EINVAL;
552 goto errout;
553 }
554
555 if (n->nlmsg_type != RTM_NEWTFILTER ||
556 !(n->nlmsg_flags & NLM_F_CREATE)) {
557 err = -ENOENT;
558 goto errout;
559 }
560
561 if (prio_allocate)
562 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
563
564 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
565 protocol, prio, parent, q, chain);
566 if (IS_ERR(tp)) {
567 err = PTR_ERR(tp);
568 goto errout;
569 }
570 tp_created = 1;
571 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
572 err = -EINVAL;
573 goto errout;
574 }
575
576 fh = tp->ops->get(tp, t->tcm_handle);
577
578 if (fh == 0) {
579 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
580 tcf_chain_tp_remove(chain, &chain_info, tp);
581 tfilter_notify(net, skb, n, tp, fh,
582 RTM_DELTFILTER, false);
583 tcf_proto_destroy(tp);
584 err = 0;
585 goto errout;
586 }
587
588 if (n->nlmsg_type != RTM_NEWTFILTER ||
589 !(n->nlmsg_flags & NLM_F_CREATE)) {
590 err = -ENOENT;
591 goto errout;
592 }
593 } else {
594 bool last;
595
596 switch (n->nlmsg_type) {
597 case RTM_NEWTFILTER:
598 if (n->nlmsg_flags & NLM_F_EXCL) {
599 if (tp_created)
600 tcf_proto_destroy(tp);
601 err = -EEXIST;
602 goto errout;
603 }
604 break;
605 case RTM_DELTFILTER:
606 err = tp->ops->delete(tp, fh, &last);
607 if (err)
608 goto errout;
609 tfilter_notify(net, skb, n, tp, t->tcm_handle,
610 RTM_DELTFILTER, false);
611 if (last) {
612 tcf_chain_tp_remove(chain, &chain_info, tp);
613 tcf_proto_destroy(tp);
614 }
615 goto errout;
616 case RTM_GETTFILTER:
617 err = tfilter_notify(net, skb, n, tp, fh,
618 RTM_NEWTFILTER, true);
619 goto errout;
620 default:
621 err = -EINVAL;
622 goto errout;
623 }
624 }
625
626 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
627 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
628 if (err == 0) {
629 if (tp_created)
630 tcf_chain_tp_insert(chain, &chain_info, tp);
631 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
632 } else {
633 if (tp_created)
634 tcf_proto_destroy(tp);
635 }
636
637 errout:
638 if (chain)
639 tcf_chain_put(chain);
640 if (cl)
641 cops->put(q, cl);
642 if (err == -EAGAIN)
643 /* Replay the request. */
644 goto replay;
645 return err;
646 }
647
648 static int tcf_fill_node(struct net *net, struct sk_buff *skb,
649 struct tcf_proto *tp, unsigned long fh, u32 portid,
650 u32 seq, u16 flags, int event)
651 {
652 struct tcmsg *tcm;
653 struct nlmsghdr *nlh;
654 unsigned char *b = skb_tail_pointer(skb);
655
656 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
657 if (!nlh)
658 goto out_nlmsg_trim;
659 tcm = nlmsg_data(nlh);
660 tcm->tcm_family = AF_UNSPEC;
661 tcm->tcm__pad1 = 0;
662 tcm->tcm__pad2 = 0;
663 tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
664 tcm->tcm_parent = tp->classid;
665 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
666 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
667 goto nla_put_failure;
668 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
669 goto nla_put_failure;
670 tcm->tcm_handle = fh;
671 if (RTM_DELTFILTER != event) {
672 tcm->tcm_handle = 0;
673 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
674 goto nla_put_failure;
675 }
676 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
677 return skb->len;
678
679 out_nlmsg_trim:
680 nla_put_failure:
681 nlmsg_trim(skb, b);
682 return -1;
683 }
684
685 static int tfilter_notify(struct net *net, struct sk_buff *oskb,
686 struct nlmsghdr *n, struct tcf_proto *tp,
687 unsigned long fh, int event, bool unicast)
688 {
689 struct sk_buff *skb;
690 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
691
692 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
693 if (!skb)
694 return -ENOBUFS;
695
696 if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
697 n->nlmsg_flags, event) <= 0) {
698 kfree_skb(skb);
699 return -EINVAL;
700 }
701
702 if (unicast)
703 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
704
705 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
706 n->nlmsg_flags & NLM_F_ECHO);
707 }
708
709 struct tcf_dump_args {
710 struct tcf_walker w;
711 struct sk_buff *skb;
712 struct netlink_callback *cb;
713 };
714
715 static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
716 struct tcf_walker *arg)
717 {
718 struct tcf_dump_args *a = (void *)arg;
719 struct net *net = sock_net(a->skb->sk);
720
721 return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
722 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
723 RTM_NEWTFILTER);
724 }
725
726 static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
727 struct netlink_callback *cb,
728 long index_start, long *p_index)
729 {
730 struct net *net = sock_net(skb->sk);
731 struct tcmsg *tcm = nlmsg_data(cb->nlh);
732 struct tcf_dump_args arg;
733 struct tcf_proto *tp;
734
735 for (tp = rtnl_dereference(chain->filter_chain);
736 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
737 if (*p_index < index_start)
738 continue;
739 if (TC_H_MAJ(tcm->tcm_info) &&
740 TC_H_MAJ(tcm->tcm_info) != tp->prio)
741 continue;
742 if (TC_H_MIN(tcm->tcm_info) &&
743 TC_H_MIN(tcm->tcm_info) != tp->protocol)
744 continue;
745 if (*p_index > index_start)
746 memset(&cb->args[1], 0,
747 sizeof(cb->args) - sizeof(cb->args[0]));
748 if (cb->args[1] == 0) {
749 if (tcf_fill_node(net, skb, tp, 0,
750 NETLINK_CB(cb->skb).portid,
751 cb->nlh->nlmsg_seq, NLM_F_MULTI,
752 RTM_NEWTFILTER) <= 0)
753 return false;
754
755 cb->args[1] = 1;
756 }
757 if (!tp->ops->walk)
758 continue;
759 arg.w.fn = tcf_node_dump;
760 arg.skb = skb;
761 arg.cb = cb;
762 arg.w.stop = 0;
763 arg.w.skip = cb->args[1] - 1;
764 arg.w.count = 0;
765 tp->ops->walk(tp, &arg.w);
766 cb->args[1] = arg.w.count + 1;
767 if (arg.w.stop)
768 return false;
769 }
770 return true;
771 }
772
773 /* called with RTNL */
774 static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
775 {
776 struct net *net = sock_net(skb->sk);
777 struct nlattr *tca[TCA_MAX + 1];
778 struct net_device *dev;
779 struct Qdisc *q;
780 struct tcf_block *block;
781 struct tcf_chain *chain;
782 struct tcmsg *tcm = nlmsg_data(cb->nlh);
783 unsigned long cl = 0;
784 const struct Qdisc_class_ops *cops;
785 long index_start;
786 long index;
787 int err;
788
789 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
790 return skb->len;
791
792 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
793 if (err)
794 return err;
795
796 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
797 if (!dev)
798 return skb->len;
799
800 if (!tcm->tcm_parent)
801 q = dev->qdisc;
802 else
803 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
804 if (!q)
805 goto out;
806 cops = q->ops->cl_ops;
807 if (!cops)
808 goto errout;
809 if (!cops->tcf_block)
810 goto errout;
811 if (TC_H_MIN(tcm->tcm_parent)) {
812 cl = cops->get(q, tcm->tcm_parent);
813 if (cl == 0)
814 goto errout;
815 }
816 block = cops->tcf_block(q, cl);
817 if (!block)
818 goto errout;
819
820 index_start = cb->args[0];
821 index = 0;
822
823 list_for_each_entry(chain, &block->chain_list, list) {
824 if (tca[TCA_CHAIN] &&
825 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
826 continue;
827 if (!tcf_chain_dump(chain, skb, cb, index_start, &index))
828 break;
829 }
830
831 cb->args[0] = index;
832
833 errout:
834 if (cl)
835 cops->put(q, cl);
836 out:
837 return skb->len;
838 }
839
840 void tcf_exts_destroy(struct tcf_exts *exts)
841 {
842 #ifdef CONFIG_NET_CLS_ACT
843 LIST_HEAD(actions);
844
845 tcf_exts_to_list(exts, &actions);
846 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
847 kfree(exts->actions);
848 exts->nr_actions = 0;
849 #endif
850 }
851 EXPORT_SYMBOL(tcf_exts_destroy);
852
853 int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
854 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
855 {
856 #ifdef CONFIG_NET_CLS_ACT
857 {
858 struct tc_action *act;
859
860 if (exts->police && tb[exts->police]) {
861 act = tcf_action_init_1(net, tp, tb[exts->police],
862 rate_tlv, "police", ovr,
863 TCA_ACT_BIND);
864 if (IS_ERR(act))
865 return PTR_ERR(act);
866
867 act->type = exts->type = TCA_OLD_COMPAT;
868 exts->actions[0] = act;
869 exts->nr_actions = 1;
870 } else if (exts->action && tb[exts->action]) {
871 LIST_HEAD(actions);
872 int err, i = 0;
873
874 err = tcf_action_init(net, tp, tb[exts->action],
875 rate_tlv, NULL, ovr, TCA_ACT_BIND,
876 &actions);
877 if (err)
878 return err;
879 list_for_each_entry(act, &actions, list)
880 exts->actions[i++] = act;
881 exts->nr_actions = i;
882 }
883 }
884 #else
885 if ((exts->action && tb[exts->action]) ||
886 (exts->police && tb[exts->police]))
887 return -EOPNOTSUPP;
888 #endif
889
890 return 0;
891 }
892 EXPORT_SYMBOL(tcf_exts_validate);
893
894 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
895 struct tcf_exts *src)
896 {
897 #ifdef CONFIG_NET_CLS_ACT
898 struct tcf_exts old = *dst;
899
900 tcf_tree_lock(tp);
901 dst->nr_actions = src->nr_actions;
902 dst->actions = src->actions;
903 dst->type = src->type;
904 tcf_tree_unlock(tp);
905
906 tcf_exts_destroy(&old);
907 #endif
908 }
909 EXPORT_SYMBOL(tcf_exts_change);
910
911 #ifdef CONFIG_NET_CLS_ACT
912 static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
913 {
914 if (exts->nr_actions == 0)
915 return NULL;
916 else
917 return exts->actions[0];
918 }
919 #endif
920
921 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
922 {
923 #ifdef CONFIG_NET_CLS_ACT
924 struct nlattr *nest;
925
926 if (exts->action && exts->nr_actions) {
927 /*
928 * again for backward compatible mode - we want
929 * to work with both old and new modes of entering
930 * tc data even if iproute2 was newer - jhs
931 */
932 if (exts->type != TCA_OLD_COMPAT) {
933 LIST_HEAD(actions);
934
935 nest = nla_nest_start(skb, exts->action);
936 if (nest == NULL)
937 goto nla_put_failure;
938
939 tcf_exts_to_list(exts, &actions);
940 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
941 goto nla_put_failure;
942 nla_nest_end(skb, nest);
943 } else if (exts->police) {
944 struct tc_action *act = tcf_exts_first_act(exts);
945 nest = nla_nest_start(skb, exts->police);
946 if (nest == NULL || !act)
947 goto nla_put_failure;
948 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
949 goto nla_put_failure;
950 nla_nest_end(skb, nest);
951 }
952 }
953 return 0;
954
955 nla_put_failure:
956 nla_nest_cancel(skb, nest);
957 return -1;
958 #else
959 return 0;
960 #endif
961 }
962 EXPORT_SYMBOL(tcf_exts_dump);
963
964
965 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
966 {
967 #ifdef CONFIG_NET_CLS_ACT
968 struct tc_action *a = tcf_exts_first_act(exts);
969 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
970 return -1;
971 #endif
972 return 0;
973 }
974 EXPORT_SYMBOL(tcf_exts_dump_stats);
975
976 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
977 struct net_device **hw_dev)
978 {
979 #ifdef CONFIG_NET_CLS_ACT
980 const struct tc_action *a;
981 LIST_HEAD(actions);
982
983 if (tc_no_actions(exts))
984 return -EINVAL;
985
986 tcf_exts_to_list(exts, &actions);
987 list_for_each_entry(a, &actions, list) {
988 if (a->ops->get_dev) {
989 a->ops->get_dev(a, dev_net(dev), hw_dev);
990 break;
991 }
992 }
993 if (*hw_dev)
994 return 0;
995 #endif
996 return -EOPNOTSUPP;
997 }
998 EXPORT_SYMBOL(tcf_exts_get_dev);
999
1000 static int __init tc_filter_init(void)
1001 {
1002 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, NULL);
1003 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, NULL);
1004 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
1005 tc_dump_tfilter, NULL);
1006
1007 return 0;
1008 }
1009
1010 subsys_initcall(tc_filter_init);