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