]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sched/cls_api.c
net_sched: switch to rcu_work
[mirror_ubuntu-bionic-kernel.git] / net / sched / cls_api.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
1da177e4 20#include <linux/string.h>
1da177e4 21#include <linux/errno.h>
33a48927 22#include <linux/err.h>
1da177e4 23#include <linux/skbuff.h>
1da177e4
LT
24#include <linux/init.h>
25#include <linux/kmod.h>
5a0e3ad6 26#include <linux/slab.h>
b854272b
DL
27#include <net/net_namespace.h>
28#include <net/sock.h>
dc5fc579 29#include <net/netlink.h>
1da177e4
LT
30#include <net/pkt_sched.h>
31#include <net/pkt_cls.h>
32
1da177e4 33/* The list of all installed classifier types */
36272874 34static LIST_HEAD(tcf_proto_base);
1da177e4
LT
35
36/* Protects list of registered TC modules. It is pure SMP lock. */
37static DEFINE_RWLOCK(cls_mod_lock);
38
39/* Find classifier type by string name */
40
33a48927 41static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
1da177e4 42{
dcd76081 43 const struct tcf_proto_ops *t, *res = NULL;
1da177e4
LT
44
45 if (kind) {
46 read_lock(&cls_mod_lock);
36272874 47 list_for_each_entry(t, &tcf_proto_base, head) {
33a48927 48 if (strcmp(kind, t->kind) == 0) {
dcd76081
ED
49 if (try_module_get(t->owner))
50 res = t;
1da177e4
LT
51 break;
52 }
53 }
54 read_unlock(&cls_mod_lock);
55 }
dcd76081 56 return res;
1da177e4
LT
57}
58
59/* Register(unregister) new classifier type */
60
61int register_tcf_proto_ops(struct tcf_proto_ops *ops)
62{
36272874 63 struct tcf_proto_ops *t;
1da177e4
LT
64 int rc = -EEXIST;
65
66 write_lock(&cls_mod_lock);
36272874 67 list_for_each_entry(t, &tcf_proto_base, head)
1da177e4
LT
68 if (!strcmp(ops->kind, t->kind))
69 goto out;
70
36272874 71 list_add_tail(&ops->head, &tcf_proto_base);
1da177e4
LT
72 rc = 0;
73out:
74 write_unlock(&cls_mod_lock);
75 return rc;
76}
aa767bfe 77EXPORT_SYMBOL(register_tcf_proto_ops);
1da177e4 78
7aa0045d
CW
79static struct workqueue_struct *tc_filter_wq;
80
1da177e4
LT
81int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
82{
36272874 83 struct tcf_proto_ops *t;
1da177e4
LT
84 int rc = -ENOENT;
85
c78e1746
DB
86 /* Wait for outstanding call_rcu()s, if any, from a
87 * tcf_proto_ops's destroy() handler.
88 */
89 rcu_barrier();
7aa0045d 90 flush_workqueue(tc_filter_wq);
c78e1746 91
1da177e4 92 write_lock(&cls_mod_lock);
dcd76081
ED
93 list_for_each_entry(t, &tcf_proto_base, head) {
94 if (t == ops) {
95 list_del(&t->head);
96 rc = 0;
1da177e4 97 break;
dcd76081
ED
98 }
99 }
1da177e4
LT
100 write_unlock(&cls_mod_lock);
101 return rc;
102}
aa767bfe 103EXPORT_SYMBOL(unregister_tcf_proto_ops);
1da177e4 104
7afe8e4f 105bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
7aa0045d 106{
7afe8e4f
CW
107 INIT_RCU_WORK(rwork, func);
108 return queue_rcu_work(tc_filter_wq, rwork);
7aa0045d
CW
109}
110EXPORT_SYMBOL(tcf_queue_work);
111
1da177e4
LT
112/* Select new prio value from the range, managed by kernel. */
113
aa767bfe 114static inline u32 tcf_auto_prio(struct tcf_proto *tp)
1da177e4 115{
aa767bfe 116 u32 first = TC_H_MAKE(0xC0000000U, 0U);
1da177e4
LT
117
118 if (tp)
cc7ec456 119 first = tp->prio - 1;
1da177e4 120
7961973a 121 return TC_H_MAJ(first);
1da177e4
LT
122}
123
33a48927 124static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
6529eaba 125 u32 prio, u32 parent, struct Qdisc *q,
5bc17018 126 struct tcf_chain *chain)
33a48927
JP
127{
128 struct tcf_proto *tp;
129 int err;
130
131 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
132 if (!tp)
133 return ERR_PTR(-ENOBUFS);
134
135 err = -ENOENT;
136 tp->ops = tcf_proto_lookup_ops(kind);
137 if (!tp->ops) {
138#ifdef CONFIG_MODULES
139 rtnl_unlock();
140 request_module("cls_%s", kind);
141 rtnl_lock();
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.
147 */
148 if (tp->ops) {
149 module_put(tp->ops->owner);
150 err = -EAGAIN;
151 } else {
152 err = -ENOENT;
153 }
33a48927 154#endif
86634952 155 goto errout;
33a48927
JP
156 }
157 tp->classify = tp->ops->classify;
158 tp->protocol = protocol;
159 tp->prio = prio;
160 tp->classid = parent;
161 tp->q = q;
5bc17018 162 tp->chain = chain;
33a48927
JP
163
164 err = tp->ops->init(tp);
165 if (err) {
166 module_put(tp->ops->owner);
167 goto errout;
168 }
169 return tp;
170
171errout:
172 kfree(tp);
173 return ERR_PTR(err);
174}
175
763dbf63 176static void tcf_proto_destroy(struct tcf_proto *tp)
cf1facda 177{
763dbf63
WC
178 tp->ops->destroy(tp);
179 module_put(tp->ops->owner);
180 kfree_rcu(tp, rcu);
cf1facda
JP
181}
182
5bc17018
JP
183static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
184 u32 chain_index)
2190d1d0 185{
5bc17018
JP
186 struct tcf_chain *chain;
187
188 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
189 if (!chain)
190 return NULL;
191 list_add_tail(&chain->list, &block->chain_list);
192 chain->block = block;
193 chain->index = chain_index;
e2ef7544 194 chain->refcnt = 1;
5bc17018 195 return chain;
2190d1d0
JP
196}
197
c7eb7d72
JP
198static void tcf_chain_head_change(struct tcf_chain *chain,
199 struct tcf_proto *tp_head)
200{
201 if (chain->chain_head_change)
202 chain->chain_head_change(tp_head,
203 chain->chain_head_change_priv);
204}
205
f93e1cdc 206static void tcf_chain_flush(struct tcf_chain *chain)
cf1facda 207{
d7aa04a5 208 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
cf1facda 209
c7eb7d72 210 tcf_chain_head_change(chain, NULL);
d7aa04a5 211 while (tp) {
2190d1d0 212 RCU_INIT_POINTER(chain->filter_chain, tp->next);
763dbf63 213 tcf_proto_destroy(tp);
d7aa04a5
RK
214 tp = rtnl_dereference(chain->filter_chain);
215 tcf_chain_put(chain);
cf1facda 216 }
f93e1cdc
JP
217}
218
219static void tcf_chain_destroy(struct tcf_chain *chain)
220{
314fab2e
CW
221 struct tcf_block *block = chain->block;
222
e2ef7544
CW
223 list_del(&chain->list);
224 kfree(chain);
314fab2e
CW
225 if (list_empty(&block->chain_list))
226 kfree(block);
e2ef7544 227}
744a4cf6 228
e2ef7544
CW
229static void tcf_chain_hold(struct tcf_chain *chain)
230{
231 ++chain->refcnt;
2190d1d0
JP
232}
233
367a8ce8
WC
234struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
235 bool create)
5bc17018
JP
236{
237 struct tcf_chain *chain;
238
239 list_for_each_entry(chain, &block->chain_list, list) {
e2ef7544
CW
240 if (chain->index == chain_index) {
241 tcf_chain_hold(chain);
242 return chain;
243 }
5bc17018 244 }
80532384 245
e2ef7544 246 return create ? tcf_chain_create(block, chain_index) : NULL;
5bc17018
JP
247}
248EXPORT_SYMBOL(tcf_chain_get);
249
250void tcf_chain_put(struct tcf_chain *chain)
251{
e2ef7544 252 if (--chain->refcnt == 0)
5bc17018
JP
253 tcf_chain_destroy(chain);
254}
255EXPORT_SYMBOL(tcf_chain_put);
256
8c4083b3
JP
257static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q,
258 struct tcf_block_ext_info *ei,
259 enum tc_block_command command)
260{
261 struct net_device *dev = q->dev_queue->dev;
262 struct tc_block_offload bo = {};
263
44ae12a7 264 if (!dev->netdev_ops->ndo_setup_tc)
8c4083b3
JP
265 return;
266 bo.command = command;
267 bo.binder_type = ei->binder_type;
268 bo.block = block;
269 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
270}
271
272static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
273 struct tcf_block_ext_info *ei)
274{
275 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND);
276}
277
278static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
279 struct tcf_block_ext_info *ei)
280{
281 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND);
282}
283
c7eb7d72 284int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
8c4083b3 285 struct tcf_block_ext_info *ei)
6529eaba
JP
286{
287 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
5bc17018 288 struct tcf_chain *chain;
2190d1d0 289 int err;
6529eaba
JP
290
291 if (!block)
292 return -ENOMEM;
5bc17018 293 INIT_LIST_HEAD(&block->chain_list);
acb67442
JP
294 INIT_LIST_HEAD(&block->cb_list);
295
5bc17018
JP
296 /* Create chain 0 by default, it has to be always present. */
297 chain = tcf_chain_create(block, 0);
298 if (!chain) {
2190d1d0
JP
299 err = -ENOMEM;
300 goto err_chain_create;
301 }
c7eb7d72
JP
302 WARN_ON(!ei->chain_head_change);
303 chain->chain_head_change = ei->chain_head_change;
304 chain->chain_head_change_priv = ei->chain_head_change_priv;
855319be 305 block->net = qdisc_net(q);
69d78ef2 306 block->q = q;
8c4083b3 307 tcf_block_offload_bind(block, q, ei);
6529eaba
JP
308 *p_block = block;
309 return 0;
2190d1d0
JP
310
311err_chain_create:
312 kfree(block);
313 return err;
6529eaba 314}
8c4083b3
JP
315EXPORT_SYMBOL(tcf_block_get_ext);
316
c7eb7d72
JP
317static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
318{
319 struct tcf_proto __rcu **p_filter_chain = priv;
320
321 rcu_assign_pointer(*p_filter_chain, tp_head);
322}
323
8c4083b3
JP
324int tcf_block_get(struct tcf_block **p_block,
325 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
326{
c7eb7d72
JP
327 struct tcf_block_ext_info ei = {
328 .chain_head_change = tcf_chain_head_change_dflt,
329 .chain_head_change_priv = p_filter_chain,
330 };
8c4083b3 331
c7eb7d72
JP
332 WARN_ON(!p_filter_chain);
333 return tcf_block_get_ext(p_block, q, &ei);
8c4083b3 334}
6529eaba
JP
335EXPORT_SYMBOL(tcf_block_get);
336
7aa0045d 337/* XXX: Standalone actions are not allowed to jump to any chain, and bound
a60b3f51 338 * actions should be all removed after flushing.
7aa0045d 339 */
c7eb7d72 340void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
e1ea2f98 341 struct tcf_block_ext_info *ei)
7aa0045d 342{
314fab2e 343 struct tcf_chain *chain, *tmp;
1697c4bb 344
343723dd
JP
345 if (!block)
346 return;
d4556ddf 347 /* Hold a refcnt for all chains, so that they don't disappear
a60b3f51
RK
348 * while we are iterating.
349 */
350 list_for_each_entry(chain, &block->chain_list, list)
d4556ddf 351 tcf_chain_hold(chain);
a60b3f51
RK
352
353 list_for_each_entry(chain, &block->chain_list, list)
30d65e8f 354 tcf_chain_flush(chain);
e2ef7544 355
4bb1b116
JP
356 tcf_block_offload_unbind(block, q, ei);
357
d4556ddf 358 /* At this point, all the chains should have refcnt >= 1. */
314fab2e
CW
359 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
360 tcf_chain_put(chain);
d4556ddf
JP
361
362 /* Finally, put chain 0 and allow block to be freed. */
363 chain = list_first_entry(&block->chain_list, struct tcf_chain, list);
364 tcf_chain_put(chain);
6529eaba 365}
8c4083b3
JP
366EXPORT_SYMBOL(tcf_block_put_ext);
367
368void tcf_block_put(struct tcf_block *block)
369{
370 struct tcf_block_ext_info ei = {0, };
371
4853f128
JP
372 if (!block)
373 return;
c7eb7d72 374 tcf_block_put_ext(block, block->q, &ei);
8c4083b3 375}
e1ea2f98 376
6529eaba 377EXPORT_SYMBOL(tcf_block_put);
cf1facda 378
acb67442
JP
379struct tcf_block_cb {
380 struct list_head list;
381 tc_setup_cb_t *cb;
382 void *cb_ident;
383 void *cb_priv;
384 unsigned int refcnt;
385};
386
387void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
388{
389 return block_cb->cb_priv;
390}
391EXPORT_SYMBOL(tcf_block_cb_priv);
392
393struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
394 tc_setup_cb_t *cb, void *cb_ident)
395{ struct tcf_block_cb *block_cb;
396
397 list_for_each_entry(block_cb, &block->cb_list, list)
398 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
399 return block_cb;
400 return NULL;
401}
402EXPORT_SYMBOL(tcf_block_cb_lookup);
403
404void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
405{
406 block_cb->refcnt++;
407}
408EXPORT_SYMBOL(tcf_block_cb_incref);
409
410unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
411{
412 return --block_cb->refcnt;
413}
414EXPORT_SYMBOL(tcf_block_cb_decref);
415
416struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
417 tc_setup_cb_t *cb, void *cb_ident,
418 void *cb_priv)
419{
420 struct tcf_block_cb *block_cb;
421
422 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
423 if (!block_cb)
424 return NULL;
425 block_cb->cb = cb;
426 block_cb->cb_ident = cb_ident;
427 block_cb->cb_priv = cb_priv;
428 list_add(&block_cb->list, &block->cb_list);
429 return block_cb;
430}
431EXPORT_SYMBOL(__tcf_block_cb_register);
432
433int tcf_block_cb_register(struct tcf_block *block,
434 tc_setup_cb_t *cb, void *cb_ident,
435 void *cb_priv)
436{
437 struct tcf_block_cb *block_cb;
438
439 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
440 return block_cb ? 0 : -ENOMEM;
441}
442EXPORT_SYMBOL(tcf_block_cb_register);
443
444void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
445{
446 list_del(&block_cb->list);
447 kfree(block_cb);
448}
449EXPORT_SYMBOL(__tcf_block_cb_unregister);
450
451void tcf_block_cb_unregister(struct tcf_block *block,
452 tc_setup_cb_t *cb, void *cb_ident)
453{
454 struct tcf_block_cb *block_cb;
455
456 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
457 if (!block_cb)
458 return;
459 __tcf_block_cb_unregister(block_cb);
460}
461EXPORT_SYMBOL(tcf_block_cb_unregister);
462
463static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
464 void *type_data, bool err_stop)
465{
466 struct tcf_block_cb *block_cb;
467 int ok_count = 0;
468 int err;
469
470 list_for_each_entry(block_cb, &block->cb_list, list) {
471 err = block_cb->cb(type, type_data, block_cb->cb_priv);
472 if (err) {
473 if (err_stop)
474 return err;
475 } else {
476 ok_count++;
477 }
478 }
479 return ok_count;
480}
481
87d83093
JP
482/* Main classifier routine: scans classifier chain attached
483 * to this qdisc, (optionally) tests for protocol and asks
484 * specific classifiers.
485 */
486int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
487 struct tcf_result *res, bool compat_mode)
488{
489 __be16 protocol = tc_skb_protocol(skb);
490#ifdef CONFIG_NET_CLS_ACT
491 const int max_reclassify_loop = 4;
ee538dce
JP
492 const struct tcf_proto *orig_tp = tp;
493 const struct tcf_proto *first_tp;
87d83093
JP
494 int limit = 0;
495
496reclassify:
497#endif
498 for (; tp; tp = rcu_dereference_bh(tp->next)) {
499 int err;
500
501 if (tp->protocol != protocol &&
502 tp->protocol != htons(ETH_P_ALL))
503 continue;
504
505 err = tp->classify(skb, tp, res);
506#ifdef CONFIG_NET_CLS_ACT
db50514f 507 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
ee538dce 508 first_tp = orig_tp;
87d83093 509 goto reset;
db50514f 510 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
ee538dce 511 first_tp = res->goto_tp;
db50514f
JP
512 goto reset;
513 }
87d83093
JP
514#endif
515 if (err >= 0)
516 return err;
517 }
518
519 return TC_ACT_UNSPEC; /* signal: continue lookup */
520#ifdef CONFIG_NET_CLS_ACT
521reset:
522 if (unlikely(limit++ >= max_reclassify_loop)) {
523 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
524 tp->q->ops->id, tp->prio & 0xffff,
525 ntohs(tp->protocol));
526 return TC_ACT_SHOT;
527 }
528
ee538dce 529 tp = first_tp;
87d83093
JP
530 protocol = tc_skb_protocol(skb);
531 goto reclassify;
532#endif
533}
534EXPORT_SYMBOL(tcf_classify);
535
2190d1d0
JP
536struct tcf_chain_info {
537 struct tcf_proto __rcu **pprev;
538 struct tcf_proto __rcu *next;
539};
540
541static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
542{
543 return rtnl_dereference(*chain_info->pprev);
544}
545
546static void tcf_chain_tp_insert(struct tcf_chain *chain,
547 struct tcf_chain_info *chain_info,
548 struct tcf_proto *tp)
549{
c7eb7d72
JP
550 if (*chain_info->pprev == chain->filter_chain)
551 tcf_chain_head_change(chain, tp);
2190d1d0
JP
552 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
553 rcu_assign_pointer(*chain_info->pprev, tp);
e2ef7544 554 tcf_chain_hold(chain);
2190d1d0
JP
555}
556
557static void tcf_chain_tp_remove(struct tcf_chain *chain,
558 struct tcf_chain_info *chain_info,
559 struct tcf_proto *tp)
560{
561 struct tcf_proto *next = rtnl_dereference(chain_info->next);
562
c7eb7d72
JP
563 if (tp == chain->filter_chain)
564 tcf_chain_head_change(chain, next);
2190d1d0 565 RCU_INIT_POINTER(*chain_info->pprev, next);
e2ef7544 566 tcf_chain_put(chain);
2190d1d0
JP
567}
568
569static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
570 struct tcf_chain_info *chain_info,
571 u32 protocol, u32 prio,
572 bool prio_allocate)
573{
574 struct tcf_proto **pprev;
575 struct tcf_proto *tp;
576
577 /* Check the chain for existence of proto-tcf with this priority */
578 for (pprev = &chain->filter_chain;
579 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
580 if (tp->prio >= prio) {
581 if (tp->prio == prio) {
582 if (prio_allocate ||
583 (tp->protocol != protocol && protocol))
584 return ERR_PTR(-EINVAL);
585 } else {
586 tp = NULL;
587 }
588 break;
589 }
590 }
591 chain_info->pprev = pprev;
592 chain_info->next = tp ? tp->next : NULL;
593 return tp;
594}
595
7120371c 596static int tcf_fill_node(struct net *net, struct sk_buff *skb,
a10fa201
JP
597 struct tcf_proto *tp, struct Qdisc *q, u32 parent,
598 void *fh, u32 portid, u32 seq, u16 flags, int event)
7120371c
WC
599{
600 struct tcmsg *tcm;
601 struct nlmsghdr *nlh;
602 unsigned char *b = skb_tail_pointer(skb);
603
604 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
605 if (!nlh)
606 goto out_nlmsg_trim;
607 tcm = nlmsg_data(nlh);
608 tcm->tcm_family = AF_UNSPEC;
609 tcm->tcm__pad1 = 0;
610 tcm->tcm__pad2 = 0;
a10fa201
JP
611 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
612 tcm->tcm_parent = parent;
7120371c
WC
613 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
614 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
615 goto nla_put_failure;
616 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
617 goto nla_put_failure;
618 if (!fh) {
619 tcm->tcm_handle = 0;
620 } else {
621 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
622 goto nla_put_failure;
623 }
624 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
625 return skb->len;
626
627out_nlmsg_trim:
628nla_put_failure:
629 nlmsg_trim(skb, b);
630 return -1;
631}
632
633static int tfilter_notify(struct net *net, struct sk_buff *oskb,
634 struct nlmsghdr *n, struct tcf_proto *tp,
a10fa201 635 struct Qdisc *q, u32 parent,
7120371c
WC
636 void *fh, int event, bool unicast)
637{
638 struct sk_buff *skb;
639 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
640
641 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
642 if (!skb)
643 return -ENOBUFS;
644
a10fa201 645 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
7120371c
WC
646 n->nlmsg_flags, event) <= 0) {
647 kfree_skb(skb);
648 return -EINVAL;
649 }
650
651 if (unicast)
652 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
653
654 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
655 n->nlmsg_flags & NLM_F_ECHO);
656}
657
658static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
659 struct nlmsghdr *n, struct tcf_proto *tp,
a10fa201 660 struct Qdisc *q, u32 parent,
7120371c
WC
661 void *fh, bool unicast, bool *last)
662{
663 struct sk_buff *skb;
664 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
665 int err;
666
667 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
668 if (!skb)
669 return -ENOBUFS;
670
a10fa201 671 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
7120371c
WC
672 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
673 kfree_skb(skb);
674 return -EINVAL;
675 }
676
677 err = tp->ops->delete(tp, fh, last);
678 if (err) {
679 kfree_skb(skb);
680 return err;
681 }
682
683 if (unicast)
684 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
685
686 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
687 n->nlmsg_flags & NLM_F_ECHO);
688}
689
690static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
a10fa201 691 struct Qdisc *q, u32 parent,
7120371c
WC
692 struct nlmsghdr *n,
693 struct tcf_chain *chain, int event)
694{
695 struct tcf_proto *tp;
696
697 for (tp = rtnl_dereference(chain->filter_chain);
698 tp; tp = rtnl_dereference(tp->next))
a10fa201 699 tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
7120371c
WC
700}
701
1da177e4
LT
702/* Add/change/delete/get a filter node */
703
c21ef3e3
DA
704static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
705 struct netlink_ext_ack *extack)
1da177e4 706{
3b1e0a65 707 struct net *net = sock_net(skb->sk);
add93b61 708 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
709 struct tcmsg *t;
710 u32 protocol;
711 u32 prio;
9d36d9e5 712 bool prio_allocate;
1da177e4 713 u32 parent;
5bc17018 714 u32 chain_index;
1da177e4
LT
715 struct net_device *dev;
716 struct Qdisc *q;
2190d1d0 717 struct tcf_chain_info chain_info;
5bc17018 718 struct tcf_chain *chain = NULL;
6529eaba 719 struct tcf_block *block;
1da177e4 720 struct tcf_proto *tp;
20fea08b 721 const struct Qdisc_class_ops *cops;
1da177e4 722 unsigned long cl;
8113c095 723 void *fh;
1da177e4 724 int err;
628185cf 725 int tp_created;
1da177e4 726
4e8bbb81 727 if ((n->nlmsg_type != RTM_GETTFILTER) &&
5f013c9b 728 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
dfc47ef8 729 return -EPERM;
de179c8c 730
1da177e4 731replay:
628185cf
DB
732 tp_created = 0;
733
c21ef3e3 734 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
de179c8c
H
735 if (err < 0)
736 return err;
737
942b8165 738 t = nlmsg_data(n);
1da177e4
LT
739 protocol = TC_H_MIN(t->tcm_info);
740 prio = TC_H_MAJ(t->tcm_info);
9d36d9e5 741 prio_allocate = false;
1da177e4
LT
742 parent = t->tcm_parent;
743 cl = 0;
744
745 if (prio == 0) {
ea7f8277
DB
746 switch (n->nlmsg_type) {
747 case RTM_DELTFILTER:
9f6ed032 748 if (protocol || t->tcm_handle || tca[TCA_KIND])
ea7f8277
DB
749 return -ENOENT;
750 break;
751 case RTM_NEWTFILTER:
752 /* If no priority is provided by the user,
753 * we allocate one.
754 */
755 if (n->nlmsg_flags & NLM_F_CREATE) {
756 prio = TC_H_MAKE(0x80000000U, 0U);
9d36d9e5 757 prio_allocate = true;
ea7f8277
DB
758 break;
759 }
760 /* fall-through */
761 default:
1da177e4 762 return -ENOENT;
ea7f8277 763 }
1da177e4
LT
764 }
765
766 /* Find head of filter chain. */
767
768 /* Find link */
7316ae88 769 dev = __dev_get_by_index(net, t->tcm_ifindex);
aa767bfe 770 if (dev == NULL)
1da177e4
LT
771 return -ENODEV;
772
773 /* Find qdisc */
774 if (!parent) {
af356afa 775 q = dev->qdisc;
1da177e4 776 parent = q->handle;
aa767bfe
SH
777 } else {
778 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
779 if (q == NULL)
780 return -EINVAL;
781 }
1da177e4
LT
782
783 /* Is it classful? */
cc7ec456
ED
784 cops = q->ops->cl_ops;
785 if (!cops)
1da177e4
LT
786 return -EINVAL;
787
6529eaba 788 if (!cops->tcf_block)
71ebe5e9
PM
789 return -EOPNOTSUPP;
790
1da177e4
LT
791 /* Do we search for filter, attached to class? */
792 if (TC_H_MIN(parent)) {
143976ce 793 cl = cops->find(q, parent);
1da177e4
LT
794 if (cl == 0)
795 return -ENOENT;
796 }
797
798 /* And the last stroke */
6529eaba
JP
799 block = cops->tcf_block(q, cl);
800 if (!block) {
6bb16e7a 801 err = -EINVAL;
1da177e4 802 goto errout;
6bb16e7a 803 }
5bc17018
JP
804
805 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
806 if (chain_index > TC_ACT_EXT_VAL_MASK) {
807 err = -EINVAL;
808 goto errout;
809 }
367a8ce8
WC
810 chain = tcf_chain_get(block, chain_index,
811 n->nlmsg_type == RTM_NEWTFILTER);
5bc17018 812 if (!chain) {
367a8ce8 813 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
5bc17018
JP
814 goto errout;
815 }
6529eaba 816
ea7f8277 817 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
a10fa201
JP
818 tfilter_notify_chain(net, skb, q, parent, n,
819 chain, RTM_DELTFILTER);
f93e1cdc 820 tcf_chain_flush(chain);
ea7f8277
DB
821 err = 0;
822 goto errout;
823 }
1da177e4 824
2190d1d0
JP
825 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
826 prio, prio_allocate);
827 if (IS_ERR(tp)) {
828 err = PTR_ERR(tp);
829 goto errout;
1da177e4
LT
830 }
831
832 if (tp == NULL) {
833 /* Proto-tcf does not exist, create new one */
834
6bb16e7a
JP
835 if (tca[TCA_KIND] == NULL || !protocol) {
836 err = -EINVAL;
1da177e4 837 goto errout;
6bb16e7a 838 }
1da177e4 839
cc7ec456 840 if (n->nlmsg_type != RTM_NEWTFILTER ||
6bb16e7a
JP
841 !(n->nlmsg_flags & NLM_F_CREATE)) {
842 err = -ENOENT;
1da177e4 843 goto errout;
6bb16e7a 844 }
1da177e4 845
9d36d9e5 846 if (prio_allocate)
2190d1d0 847 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
1da177e4 848
33a48927 849 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
5bc17018 850 protocol, prio, parent, q, chain);
33a48927
JP
851 if (IS_ERR(tp)) {
852 err = PTR_ERR(tp);
1da177e4
LT
853 goto errout;
854 }
12186be7 855 tp_created = 1;
6bb16e7a
JP
856 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
857 err = -EINVAL;
1da177e4 858 goto errout;
6bb16e7a 859 }
1da177e4
LT
860
861 fh = tp->ops->get(tp, t->tcm_handle);
862
8113c095 863 if (!fh) {
1da177e4 864 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
2190d1d0 865 tcf_chain_tp_remove(chain, &chain_info, tp);
a10fa201 866 tfilter_notify(net, skb, n, tp, q, parent, fh,
fa59b27c 867 RTM_DELTFILTER, false);
763dbf63 868 tcf_proto_destroy(tp);
1da177e4
LT
869 err = 0;
870 goto errout;
871 }
872
aa767bfe 873 if (n->nlmsg_type != RTM_NEWTFILTER ||
6bb16e7a
JP
874 !(n->nlmsg_flags & NLM_F_CREATE)) {
875 err = -ENOENT;
1da177e4 876 goto errout;
6bb16e7a 877 }
1da177e4 878 } else {
763dbf63
WC
879 bool last;
880
1da177e4 881 switch (n->nlmsg_type) {
10297b99 882 case RTM_NEWTFILTER:
12186be7
MU
883 if (n->nlmsg_flags & NLM_F_EXCL) {
884 if (tp_created)
763dbf63 885 tcf_proto_destroy(tp);
6bb16e7a 886 err = -EEXIST;
1da177e4 887 goto errout;
12186be7 888 }
1da177e4
LT
889 break;
890 case RTM_DELTFILTER:
a10fa201
JP
891 err = tfilter_del_notify(net, skb, n, tp, q, parent,
892 fh, false, &last);
40c81b25
JP
893 if (err)
894 goto errout;
763dbf63 895 if (last) {
2190d1d0 896 tcf_chain_tp_remove(chain, &chain_info, tp);
763dbf63
WC
897 tcf_proto_destroy(tp);
898 }
d7cf52c2 899 goto errout;
1da177e4 900 case RTM_GETTFILTER:
a10fa201 901 err = tfilter_notify(net, skb, n, tp, q, parent, fh,
fa59b27c 902 RTM_NEWTFILTER, true);
1da177e4
LT
903 goto errout;
904 default:
905 err = -EINVAL;
906 goto errout;
907 }
908 }
909
2f7ef2f8
CW
910 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
911 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
12186be7 912 if (err == 0) {
2190d1d0
JP
913 if (tp_created)
914 tcf_chain_tp_insert(chain, &chain_info, tp);
a10fa201
JP
915 tfilter_notify(net, skb, n, tp, q, parent, fh,
916 RTM_NEWTFILTER, false);
12186be7
MU
917 } else {
918 if (tp_created)
763dbf63 919 tcf_proto_destroy(tp);
12186be7 920 }
1da177e4
LT
921
922errout:
5bc17018
JP
923 if (chain)
924 tcf_chain_put(chain);
1da177e4
LT
925 if (err == -EAGAIN)
926 /* Replay the request. */
927 goto replay;
928 return err;
929}
930
aa767bfe 931struct tcf_dump_args {
1da177e4
LT
932 struct tcf_walker w;
933 struct sk_buff *skb;
934 struct netlink_callback *cb;
a10fa201
JP
935 struct Qdisc *q;
936 u32 parent;
1da177e4
LT
937};
938
8113c095 939static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
1da177e4 940{
aa767bfe 941 struct tcf_dump_args *a = (void *)arg;
832d1d5b 942 struct net *net = sock_net(a->skb->sk);
1da177e4 943
a10fa201
JP
944 return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
945 n, NETLINK_CB(a->cb->skb).portid,
5a7a5555
JHS
946 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
947 RTM_NEWTFILTER);
1da177e4
LT
948}
949
a10fa201
JP
950static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
951 struct sk_buff *skb, struct netlink_callback *cb,
acb31fae
JP
952 long index_start, long *p_index)
953{
954 struct net *net = sock_net(skb->sk);
955 struct tcmsg *tcm = nlmsg_data(cb->nlh);
956 struct tcf_dump_args arg;
957 struct tcf_proto *tp;
958
959 for (tp = rtnl_dereference(chain->filter_chain);
960 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
961 if (*p_index < index_start)
962 continue;
963 if (TC_H_MAJ(tcm->tcm_info) &&
964 TC_H_MAJ(tcm->tcm_info) != tp->prio)
965 continue;
966 if (TC_H_MIN(tcm->tcm_info) &&
967 TC_H_MIN(tcm->tcm_info) != tp->protocol)
968 continue;
969 if (*p_index > index_start)
970 memset(&cb->args[1], 0,
971 sizeof(cb->args) - sizeof(cb->args[0]));
972 if (cb->args[1] == 0) {
a10fa201 973 if (tcf_fill_node(net, skb, tp, q, parent, 0,
acb31fae
JP
974 NETLINK_CB(cb->skb).portid,
975 cb->nlh->nlmsg_seq, NLM_F_MULTI,
976 RTM_NEWTFILTER) <= 0)
5bc17018 977 return false;
acb31fae
JP
978
979 cb->args[1] = 1;
980 }
981 if (!tp->ops->walk)
982 continue;
983 arg.w.fn = tcf_node_dump;
984 arg.skb = skb;
985 arg.cb = cb;
a10fa201
JP
986 arg.q = q;
987 arg.parent = parent;
acb31fae
JP
988 arg.w.stop = 0;
989 arg.w.skip = cb->args[1] - 1;
990 arg.w.count = 0;
991 tp->ops->walk(tp, &arg.w);
992 cb->args[1] = arg.w.count + 1;
993 if (arg.w.stop)
5bc17018 994 return false;
acb31fae 995 }
5bc17018 996 return true;
acb31fae
JP
997}
998
bd27a875 999/* called with RTNL */
1da177e4
LT
1000static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1001{
3b1e0a65 1002 struct net *net = sock_net(skb->sk);
5bc17018 1003 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
1004 struct net_device *dev;
1005 struct Qdisc *q;
6529eaba 1006 struct tcf_block *block;
2190d1d0 1007 struct tcf_chain *chain;
942b8165 1008 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1da177e4 1009 unsigned long cl = 0;
20fea08b 1010 const struct Qdisc_class_ops *cops;
acb31fae
JP
1011 long index_start;
1012 long index;
a10fa201 1013 u32 parent;
5bc17018 1014 int err;
1da177e4 1015
573ce260 1016 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1da177e4 1017 return skb->len;
5bc17018
JP
1018
1019 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1020 if (err)
1021 return err;
1022
cc7ec456
ED
1023 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1024 if (!dev)
1da177e4
LT
1025 return skb->len;
1026
a10fa201
JP
1027 parent = tcm->tcm_parent;
1028 if (!parent) {
af356afa 1029 q = dev->qdisc;
a10fa201
JP
1030 parent = q->handle;
1031 } else {
1da177e4 1032 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
a10fa201 1033 }
1da177e4
LT
1034 if (!q)
1035 goto out;
cc7ec456
ED
1036 cops = q->ops->cl_ops;
1037 if (!cops)
143976ce 1038 goto out;
6529eaba 1039 if (!cops->tcf_block)
143976ce 1040 goto out;
1da177e4 1041 if (TC_H_MIN(tcm->tcm_parent)) {
143976ce 1042 cl = cops->find(q, tcm->tcm_parent);
1da177e4 1043 if (cl == 0)
143976ce 1044 goto out;
1da177e4 1045 }
6529eaba
JP
1046 block = cops->tcf_block(q, cl);
1047 if (!block)
143976ce 1048 goto out;
1da177e4 1049
acb31fae
JP
1050 index_start = cb->args[0];
1051 index = 0;
5bc17018
JP
1052
1053 list_for_each_entry(chain, &block->chain_list, list) {
1054 if (tca[TCA_CHAIN] &&
1055 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1056 continue;
a10fa201 1057 if (!tcf_chain_dump(chain, q, parent, skb, cb,
4c7dcb53
RK
1058 index_start, &index)) {
1059 err = -EMSGSIZE;
5bc17018 1060 break;
4c7dcb53 1061 }
5bc17018
JP
1062 }
1063
acb31fae 1064 cb->args[0] = index;
1da177e4 1065
1da177e4 1066out:
4c7dcb53
RK
1067 /* If we did no progress, the error (EMSGSIZE) is real */
1068 if (skb->len == 0 && err)
1069 return err;
1da177e4
LT
1070 return skb->len;
1071}
1072
18d0264f 1073void tcf_exts_destroy(struct tcf_exts *exts)
1da177e4
LT
1074{
1075#ifdef CONFIG_NET_CLS_ACT
22dc13c8
WC
1076 LIST_HEAD(actions);
1077
2d132eba 1078 ASSERT_RTNL();
22dc13c8
WC
1079 tcf_exts_to_list(exts, &actions);
1080 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1081 kfree(exts->actions);
1082 exts->nr_actions = 0;
1da177e4
LT
1083#endif
1084}
aa767bfe 1085EXPORT_SYMBOL(tcf_exts_destroy);
1da177e4 1086
c1b52739 1087int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
5a7a5555 1088 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
1da177e4 1089{
1da177e4
LT
1090#ifdef CONFIG_NET_CLS_ACT
1091 {
1da177e4
LT
1092 struct tc_action *act;
1093
5da57f42 1094 if (exts->police && tb[exts->police]) {
9fb9f251
JP
1095 act = tcf_action_init_1(net, tp, tb[exts->police],
1096 rate_tlv, "police", ovr,
1097 TCA_ACT_BIND);
ab27cfb8
PM
1098 if (IS_ERR(act))
1099 return PTR_ERR(act);
1da177e4 1100
33be6271 1101 act->type = exts->type = TCA_OLD_COMPAT;
22dc13c8
WC
1102 exts->actions[0] = act;
1103 exts->nr_actions = 1;
5da57f42 1104 } else if (exts->action && tb[exts->action]) {
22dc13c8
WC
1105 LIST_HEAD(actions);
1106 int err, i = 0;
1107
9fb9f251
JP
1108 err = tcf_action_init(net, tp, tb[exts->action],
1109 rate_tlv, NULL, ovr, TCA_ACT_BIND,
5a7a5555 1110 &actions);
33be6271
WC
1111 if (err)
1112 return err;
22dc13c8
WC
1113 list_for_each_entry(act, &actions, list)
1114 exts->actions[i++] = act;
1115 exts->nr_actions = i;
1da177e4 1116 }
e4b95c41 1117 exts->net = net;
1da177e4 1118 }
1da177e4 1119#else
5da57f42
WC
1120 if ((exts->action && tb[exts->action]) ||
1121 (exts->police && tb[exts->police]))
1da177e4
LT
1122 return -EOPNOTSUPP;
1123#endif
1124
1125 return 0;
1126}
aa767bfe 1127EXPORT_SYMBOL(tcf_exts_validate);
1da177e4 1128
9b0d4446 1129void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1da177e4
LT
1130{
1131#ifdef CONFIG_NET_CLS_ACT
22dc13c8
WC
1132 struct tcf_exts old = *dst;
1133
9b0d4446 1134 *dst = *src;
22dc13c8 1135 tcf_exts_destroy(&old);
1da177e4
LT
1136#endif
1137}
aa767bfe 1138EXPORT_SYMBOL(tcf_exts_change);
1da177e4 1139
22dc13c8
WC
1140#ifdef CONFIG_NET_CLS_ACT
1141static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1142{
1143 if (exts->nr_actions == 0)
1144 return NULL;
1145 else
1146 return exts->actions[0];
1147}
1148#endif
33be6271 1149
5da57f42 1150int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
1151{
1152#ifdef CONFIG_NET_CLS_ACT
9cc63db5
CW
1153 struct nlattr *nest;
1154
978dfd8d 1155 if (exts->action && tcf_exts_has_actions(exts)) {
1da177e4
LT
1156 /*
1157 * again for backward compatible mode - we want
1158 * to work with both old and new modes of entering
1159 * tc data even if iproute2 was newer - jhs
1160 */
33be6271 1161 if (exts->type != TCA_OLD_COMPAT) {
22dc13c8
WC
1162 LIST_HEAD(actions);
1163
5da57f42 1164 nest = nla_nest_start(skb, exts->action);
4b3550ef
PM
1165 if (nest == NULL)
1166 goto nla_put_failure;
22dc13c8
WC
1167
1168 tcf_exts_to_list(exts, &actions);
1169 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
add93b61 1170 goto nla_put_failure;
4b3550ef 1171 nla_nest_end(skb, nest);
5da57f42 1172 } else if (exts->police) {
33be6271 1173 struct tc_action *act = tcf_exts_first_act(exts);
5da57f42 1174 nest = nla_nest_start(skb, exts->police);
63acd680 1175 if (nest == NULL || !act)
4b3550ef 1176 goto nla_put_failure;
33be6271 1177 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
add93b61 1178 goto nla_put_failure;
4b3550ef 1179 nla_nest_end(skb, nest);
1da177e4
LT
1180 }
1181 }
1da177e4 1182 return 0;
9cc63db5
CW
1183
1184nla_put_failure:
1185 nla_nest_cancel(skb, nest);
1da177e4 1186 return -1;
9cc63db5
CW
1187#else
1188 return 0;
1189#endif
1da177e4 1190}
aa767bfe 1191EXPORT_SYMBOL(tcf_exts_dump);
1da177e4 1192
aa767bfe 1193
5da57f42 1194int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
1195{
1196#ifdef CONFIG_NET_CLS_ACT
33be6271 1197 struct tc_action *a = tcf_exts_first_act(exts);
b057df24 1198 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
33be6271 1199 return -1;
1da177e4
LT
1200#endif
1201 return 0;
1da177e4 1202}
aa767bfe 1203EXPORT_SYMBOL(tcf_exts_dump_stats);
1da177e4 1204
717503b9
JP
1205static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1206 enum tc_setup_type type,
1207 void *type_data, bool err_stop)
b3f55bdd
JP
1208{
1209 int ok_count = 0;
1210#ifdef CONFIG_NET_CLS_ACT
1211 const struct tc_action *a;
1212 struct net_device *dev;
9d452ceb 1213 int i, ret;
b3f55bdd
JP
1214
1215 if (!tcf_exts_has_actions(exts))
1216 return 0;
1217
9d452ceb
OG
1218 for (i = 0; i < exts->nr_actions; i++) {
1219 a = exts->actions[i];
b3f55bdd
JP
1220 if (!a->ops->get_dev)
1221 continue;
1222 dev = a->ops->get_dev(a);
7612fb03 1223 if (!dev)
b3f55bdd
JP
1224 continue;
1225 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1226 if (ret < 0)
1227 return ret;
1228 ok_count += ret;
1229 }
1230#endif
1231 return ok_count;
1232}
717503b9 1233
208c0f4b
JP
1234int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1235 enum tc_setup_type type, void *type_data, bool err_stop)
717503b9 1236{
208c0f4b
JP
1237 int ok_count;
1238 int ret;
1239
1240 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1241 if (ret < 0)
1242 return ret;
1243 ok_count = ret;
1244
4ac3191c 1245 if (!exts || ok_count)
208c0f4b
JP
1246 return ok_count;
1247 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1248 if (ret < 0)
1249 return ret;
1250 ok_count += ret;
1251
1252 return ok_count;
717503b9
JP
1253}
1254EXPORT_SYMBOL(tc_setup_cb_call);
b3f55bdd 1255
1da177e4
LT
1256static int __init tc_filter_init(void)
1257{
7aa0045d
CW
1258 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1259 if (!tc_filter_wq)
1260 return -ENOMEM;
1261
b97bac64
FW
1262 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1263 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
82623c0d 1264 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
b97bac64 1265 tc_dump_tfilter, 0);
1da177e4 1266
1da177e4
LT
1267 return 0;
1268}
1269
1270subsys_initcall(tc_filter_init);