]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/sched/cls_api.c
net: sched: Avoid implicit chain 0 creation
[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
f34e8bff 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
f34e8bff
JP
60static const struct tcf_proto_ops *
61tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
62{
63 const struct tcf_proto_ops *ops;
64
65 ops = __tcf_proto_lookup_ops(kind);
66 if (ops)
67 return ops;
68#ifdef CONFIG_MODULES
69 rtnl_unlock();
70 request_module("cls_%s", kind);
71 rtnl_lock();
72 ops = __tcf_proto_lookup_ops(kind);
73 /* We dropped the RTNL semaphore in order to perform
74 * the module load. So, even if we succeeded in loading
75 * the module we have to replay the request. We indicate
76 * this using -EAGAIN.
77 */
78 if (ops) {
79 module_put(ops->owner);
80 return ERR_PTR(-EAGAIN);
81 }
82#endif
83 NL_SET_ERR_MSG(extack, "TC classifier not found");
84 return ERR_PTR(-ENOENT);
85}
86
1da177e4
LT
87/* Register(unregister) new classifier type */
88
89int register_tcf_proto_ops(struct tcf_proto_ops *ops)
90{
36272874 91 struct tcf_proto_ops *t;
1da177e4
LT
92 int rc = -EEXIST;
93
94 write_lock(&cls_mod_lock);
36272874 95 list_for_each_entry(t, &tcf_proto_base, head)
1da177e4
LT
96 if (!strcmp(ops->kind, t->kind))
97 goto out;
98
36272874 99 list_add_tail(&ops->head, &tcf_proto_base);
1da177e4
LT
100 rc = 0;
101out:
102 write_unlock(&cls_mod_lock);
103 return rc;
104}
aa767bfe 105EXPORT_SYMBOL(register_tcf_proto_ops);
1da177e4 106
7aa0045d
CW
107static struct workqueue_struct *tc_filter_wq;
108
1da177e4
LT
109int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
110{
36272874 111 struct tcf_proto_ops *t;
1da177e4
LT
112 int rc = -ENOENT;
113
c78e1746
DB
114 /* Wait for outstanding call_rcu()s, if any, from a
115 * tcf_proto_ops's destroy() handler.
116 */
117 rcu_barrier();
7aa0045d 118 flush_workqueue(tc_filter_wq);
c78e1746 119
1da177e4 120 write_lock(&cls_mod_lock);
dcd76081
ED
121 list_for_each_entry(t, &tcf_proto_base, head) {
122 if (t == ops) {
123 list_del(&t->head);
124 rc = 0;
1da177e4 125 break;
dcd76081
ED
126 }
127 }
1da177e4
LT
128 write_unlock(&cls_mod_lock);
129 return rc;
130}
aa767bfe 131EXPORT_SYMBOL(unregister_tcf_proto_ops);
1da177e4 132
aaa908ff 133bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
7aa0045d 134{
aaa908ff
CW
135 INIT_RCU_WORK(rwork, func);
136 return queue_rcu_work(tc_filter_wq, rwork);
7aa0045d
CW
137}
138EXPORT_SYMBOL(tcf_queue_work);
139
1da177e4
LT
140/* Select new prio value from the range, managed by kernel. */
141
aa767bfe 142static inline u32 tcf_auto_prio(struct tcf_proto *tp)
1da177e4 143{
aa767bfe 144 u32 first = TC_H_MAKE(0xC0000000U, 0U);
1da177e4
LT
145
146 if (tp)
cc7ec456 147 first = tp->prio - 1;
1da177e4 148
7961973a 149 return TC_H_MAJ(first);
1da177e4
LT
150}
151
33a48927 152static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
c35a4acc
AA
153 u32 prio, struct tcf_chain *chain,
154 struct netlink_ext_ack *extack)
33a48927
JP
155{
156 struct tcf_proto *tp;
157 int err;
158
159 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
160 if (!tp)
161 return ERR_PTR(-ENOBUFS);
162
f34e8bff
JP
163 tp->ops = tcf_proto_lookup_ops(kind, extack);
164 if (IS_ERR(tp->ops)) {
165 err = PTR_ERR(tp->ops);
d68d75fd 166 goto errout;
33a48927
JP
167 }
168 tp->classify = tp->ops->classify;
169 tp->protocol = protocol;
170 tp->prio = prio;
5bc17018 171 tp->chain = chain;
33a48927
JP
172
173 err = tp->ops->init(tp);
174 if (err) {
175 module_put(tp->ops->owner);
176 goto errout;
177 }
178 return tp;
179
180errout:
181 kfree(tp);
182 return ERR_PTR(err);
183}
184
715df5ec
JK
185static void tcf_proto_destroy(struct tcf_proto *tp,
186 struct netlink_ext_ack *extack)
cf1facda 187{
715df5ec 188 tp->ops->destroy(tp, extack);
763dbf63
WC
189 module_put(tp->ops->owner);
190 kfree_rcu(tp, rcu);
cf1facda
JP
191}
192
a9b19443
JP
193struct tcf_filter_chain_list_item {
194 struct list_head list;
195 tcf_chain_head_change_t *chain_head_change;
196 void *chain_head_change_priv;
197};
198
5bc17018
JP
199static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
200 u32 chain_index)
2190d1d0 201{
5bc17018
JP
202 struct tcf_chain *chain;
203
204 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
205 if (!chain)
206 return NULL;
207 list_add_tail(&chain->list, &block->chain_list);
208 chain->block = block;
209 chain->index = chain_index;
e2ef7544 210 chain->refcnt = 1;
f71e0ca4
JP
211 if (!chain->index)
212 block->chain0.chain = chain;
5bc17018 213 return chain;
2190d1d0
JP
214}
215
a9b19443
JP
216static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
217 struct tcf_proto *tp_head)
218{
219 if (item->chain_head_change)
220 item->chain_head_change(tp_head, item->chain_head_change_priv);
221}
f71e0ca4
JP
222
223static void tcf_chain0_head_change(struct tcf_chain *chain,
224 struct tcf_proto *tp_head)
c7eb7d72 225{
a9b19443 226 struct tcf_filter_chain_list_item *item;
f71e0ca4 227 struct tcf_block *block = chain->block;
a9b19443 228
f71e0ca4
JP
229 if (chain->index)
230 return;
231 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
a9b19443 232 tcf_chain_head_change_item(item, tp_head);
c7eb7d72
JP
233}
234
f93e1cdc 235static void tcf_chain_flush(struct tcf_chain *chain)
cf1facda 236{
d7aa04a5 237 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
cf1facda 238
f71e0ca4 239 tcf_chain0_head_change(chain, NULL);
d7aa04a5 240 while (tp) {
2190d1d0 241 RCU_INIT_POINTER(chain->filter_chain, tp->next);
715df5ec 242 tcf_proto_destroy(tp, NULL);
d7aa04a5
RK
243 tp = rtnl_dereference(chain->filter_chain);
244 tcf_chain_put(chain);
cf1facda 245 }
f93e1cdc
JP
246}
247
248static void tcf_chain_destroy(struct tcf_chain *chain)
249{
efbf7897
CW
250 struct tcf_block *block = chain->block;
251
e2ef7544 252 list_del(&chain->list);
f71e0ca4
JP
253 if (!chain->index)
254 block->chain0.chain = NULL;
e2ef7544 255 kfree(chain);
f71e0ca4 256 if (list_empty(&block->chain_list) && block->refcnt == 0)
efbf7897 257 kfree(block);
e2ef7544 258}
744a4cf6 259
e2ef7544
CW
260static void tcf_chain_hold(struct tcf_chain *chain)
261{
262 ++chain->refcnt;
2190d1d0
JP
263}
264
367a8ce8
WC
265struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
266 bool create)
5bc17018
JP
267{
268 struct tcf_chain *chain;
269
270 list_for_each_entry(chain, &block->chain_list, list) {
e2ef7544
CW
271 if (chain->index == chain_index) {
272 tcf_chain_hold(chain);
273 return chain;
274 }
5bc17018 275 }
80532384 276
e2ef7544 277 return create ? tcf_chain_create(block, chain_index) : NULL;
5bc17018
JP
278}
279EXPORT_SYMBOL(tcf_chain_get);
280
281void tcf_chain_put(struct tcf_chain *chain)
282{
e2ef7544 283 if (--chain->refcnt == 0)
5bc17018
JP
284 tcf_chain_destroy(chain);
285}
286EXPORT_SYMBOL(tcf_chain_put);
287
caa72601
JP
288static bool tcf_block_offload_in_use(struct tcf_block *block)
289{
290 return block->offloadcnt;
291}
292
293static int tcf_block_offload_cmd(struct tcf_block *block,
294 struct net_device *dev,
295 struct tcf_block_ext_info *ei,
60513bd8
JH
296 enum tc_block_command command,
297 struct netlink_ext_ack *extack)
8c4083b3 298{
8c4083b3
JP
299 struct tc_block_offload bo = {};
300
8c4083b3
JP
301 bo.command = command;
302 bo.binder_type = ei->binder_type;
303 bo.block = block;
60513bd8 304 bo.extack = extack;
caa72601 305 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
8c4083b3
JP
306}
307
caa72601 308static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
60513bd8
JH
309 struct tcf_block_ext_info *ei,
310 struct netlink_ext_ack *extack)
8c4083b3 311{
caa72601
JP
312 struct net_device *dev = q->dev_queue->dev;
313 int err;
314
315 if (!dev->netdev_ops->ndo_setup_tc)
316 goto no_offload_dev_inc;
317
318 /* If tc offload feature is disabled and the block we try to bind
319 * to already has some offloaded filters, forbid to bind.
320 */
60513bd8
JH
321 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
322 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
caa72601 323 return -EOPNOTSUPP;
60513bd8 324 }
caa72601 325
60513bd8 326 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
caa72601
JP
327 if (err == -EOPNOTSUPP)
328 goto no_offload_dev_inc;
329 return err;
330
331no_offload_dev_inc:
332 if (tcf_block_offload_in_use(block))
333 return -EOPNOTSUPP;
334 block->nooffloaddevcnt++;
335 return 0;
8c4083b3
JP
336}
337
338static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
339 struct tcf_block_ext_info *ei)
340{
caa72601
JP
341 struct net_device *dev = q->dev_queue->dev;
342 int err;
343
344 if (!dev->netdev_ops->ndo_setup_tc)
345 goto no_offload_dev_dec;
60513bd8 346 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
caa72601
JP
347 if (err == -EOPNOTSUPP)
348 goto no_offload_dev_dec;
349 return;
350
351no_offload_dev_dec:
352 WARN_ON(block->nooffloaddevcnt-- == 0);
8c4083b3
JP
353}
354
a9b19443 355static int
f71e0ca4
JP
356tcf_chain0_head_change_cb_add(struct tcf_block *block,
357 struct tcf_block_ext_info *ei,
358 struct netlink_ext_ack *extack)
a9b19443 359{
f71e0ca4 360 struct tcf_chain *chain0 = block->chain0.chain;
a9b19443
JP
361 struct tcf_filter_chain_list_item *item;
362
363 item = kmalloc(sizeof(*item), GFP_KERNEL);
364 if (!item) {
365 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
366 return -ENOMEM;
367 }
368 item->chain_head_change = ei->chain_head_change;
369 item->chain_head_change_priv = ei->chain_head_change_priv;
f71e0ca4
JP
370 if (chain0 && chain0->filter_chain)
371 tcf_chain_head_change_item(item, chain0->filter_chain);
372 list_add(&item->list, &block->chain0.filter_chain_list);
a9b19443
JP
373 return 0;
374}
375
376static void
f71e0ca4
JP
377tcf_chain0_head_change_cb_del(struct tcf_block *block,
378 struct tcf_block_ext_info *ei)
a9b19443 379{
f71e0ca4 380 struct tcf_chain *chain0 = block->chain0.chain;
a9b19443
JP
381 struct tcf_filter_chain_list_item *item;
382
f71e0ca4 383 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
a9b19443
JP
384 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
385 (item->chain_head_change == ei->chain_head_change &&
386 item->chain_head_change_priv == ei->chain_head_change_priv)) {
f71e0ca4
JP
387 if (chain0)
388 tcf_chain_head_change_item(item, NULL);
a9b19443
JP
389 list_del(&item->list);
390 kfree(item);
391 return;
392 }
393 }
394 WARN_ON(1);
395}
396
48617387
JP
397struct tcf_net {
398 struct idr idr;
399};
400
401static unsigned int tcf_net_id;
402
403static int tcf_block_insert(struct tcf_block *block, struct net *net,
bb047ddd 404 struct netlink_ext_ack *extack)
a9b19443 405{
48617387 406 struct tcf_net *tn = net_generic(net, tcf_net_id);
48617387 407
bb047ddd
JP
408 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
409 GFP_KERNEL);
a9b19443
JP
410}
411
48617387
JP
412static void tcf_block_remove(struct tcf_block *block, struct net *net)
413{
414 struct tcf_net *tn = net_generic(net, tcf_net_id);
415
9c160941 416 idr_remove(&tn->idr, block->index);
48617387
JP
417}
418
419static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
bb047ddd 420 u32 block_index,
48617387 421 struct netlink_ext_ack *extack)
6529eaba 422{
48617387 423 struct tcf_block *block;
6529eaba 424
48617387 425 block = kzalloc(sizeof(*block), GFP_KERNEL);
8d1a77f9
AA
426 if (!block) {
427 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
48617387 428 return ERR_PTR(-ENOMEM);
8d1a77f9 429 }
5bc17018 430 INIT_LIST_HEAD(&block->chain_list);
acb67442 431 INIT_LIST_HEAD(&block->cb_list);
f36fe1c4 432 INIT_LIST_HEAD(&block->owner_list);
f71e0ca4 433 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
acb67442 434
48617387
JP
435 block->refcnt = 1;
436 block->net = net;
bb047ddd
JP
437 block->index = block_index;
438
439 /* Don't store q pointer for blocks which are shared */
440 if (!tcf_block_shared(block))
441 block->q = q;
48617387 442 return block;
48617387
JP
443}
444
445static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
446{
447 struct tcf_net *tn = net_generic(net, tcf_net_id);
448
322d884b 449 return idr_find(&tn->idr, block_index);
48617387
JP
450}
451
c431f89b
VB
452/* Find tcf block.
453 * Set q, parent, cl when appropriate.
454 */
455
456static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
457 u32 *parent, unsigned long *cl,
458 int ifindex, u32 block_index,
459 struct netlink_ext_ack *extack)
460{
461 struct tcf_block *block;
462
463 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
464 block = tcf_block_lookup(net, block_index);
465 if (!block) {
466 NL_SET_ERR_MSG(extack, "Block of given index was not found");
467 return ERR_PTR(-EINVAL);
468 }
469 } else {
470 const struct Qdisc_class_ops *cops;
471 struct net_device *dev;
472
473 /* Find link */
474 dev = __dev_get_by_index(net, ifindex);
475 if (!dev)
476 return ERR_PTR(-ENODEV);
477
478 /* Find qdisc */
479 if (!*parent) {
480 *q = dev->qdisc;
481 *parent = (*q)->handle;
482 } else {
483 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
484 if (!*q) {
485 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
486 return ERR_PTR(-EINVAL);
487 }
488 }
489
490 /* Is it classful? */
491 cops = (*q)->ops->cl_ops;
492 if (!cops) {
493 NL_SET_ERR_MSG(extack, "Qdisc not classful");
494 return ERR_PTR(-EINVAL);
495 }
496
497 if (!cops->tcf_block) {
498 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
499 return ERR_PTR(-EOPNOTSUPP);
500 }
501
502 /* Do we search for filter, attached to class? */
503 if (TC_H_MIN(*parent)) {
504 *cl = cops->find(*q, *parent);
505 if (*cl == 0) {
506 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
507 return ERR_PTR(-ENOENT);
508 }
509 }
510
511 /* And the last stroke */
512 block = cops->tcf_block(*q, *cl, extack);
513 if (!block)
514 return ERR_PTR(-EINVAL);
515 if (tcf_block_shared(block)) {
516 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
517 return ERR_PTR(-EOPNOTSUPP);
518 }
519 }
520
521 return block;
522}
523
f36fe1c4
JP
524struct tcf_block_owner_item {
525 struct list_head list;
526 struct Qdisc *q;
527 enum tcf_block_binder_type binder_type;
528};
529
530static void
531tcf_block_owner_netif_keep_dst(struct tcf_block *block,
532 struct Qdisc *q,
533 enum tcf_block_binder_type binder_type)
534{
535 if (block->keep_dst &&
536 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
537 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
538 netif_keep_dst(qdisc_dev(q));
539}
540
541void tcf_block_netif_keep_dst(struct tcf_block *block)
542{
543 struct tcf_block_owner_item *item;
544
545 block->keep_dst = true;
546 list_for_each_entry(item, &block->owner_list, list)
547 tcf_block_owner_netif_keep_dst(block, item->q,
548 item->binder_type);
549}
550EXPORT_SYMBOL(tcf_block_netif_keep_dst);
551
552static int tcf_block_owner_add(struct tcf_block *block,
553 struct Qdisc *q,
554 enum tcf_block_binder_type binder_type)
555{
556 struct tcf_block_owner_item *item;
557
558 item = kmalloc(sizeof(*item), GFP_KERNEL);
559 if (!item)
560 return -ENOMEM;
561 item->q = q;
562 item->binder_type = binder_type;
563 list_add(&item->list, &block->owner_list);
564 return 0;
565}
566
567static void tcf_block_owner_del(struct tcf_block *block,
568 struct Qdisc *q,
569 enum tcf_block_binder_type binder_type)
570{
571 struct tcf_block_owner_item *item;
572
573 list_for_each_entry(item, &block->owner_list, list) {
574 if (item->q == q && item->binder_type == binder_type) {
575 list_del(&item->list);
576 kfree(item);
577 return;
578 }
579 }
580 WARN_ON(1);
581}
582
48617387
JP
583int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
584 struct tcf_block_ext_info *ei,
585 struct netlink_ext_ack *extack)
586{
587 struct net *net = qdisc_net(q);
588 struct tcf_block *block = NULL;
589 bool created = false;
590 int err;
591
592 if (ei->block_index) {
593 /* block_index not 0 means the shared block is requested */
594 block = tcf_block_lookup(net, ei->block_index);
595 if (block)
596 block->refcnt++;
597 }
598
599 if (!block) {
bb047ddd 600 block = tcf_block_create(net, q, ei->block_index, extack);
48617387
JP
601 if (IS_ERR(block))
602 return PTR_ERR(block);
603 created = true;
bb047ddd
JP
604 if (tcf_block_shared(block)) {
605 err = tcf_block_insert(block, net, extack);
48617387
JP
606 if (err)
607 goto err_block_insert;
608 }
609 }
610
f36fe1c4
JP
611 err = tcf_block_owner_add(block, q, ei->binder_type);
612 if (err)
613 goto err_block_owner_add;
614
615 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
616
f71e0ca4 617 err = tcf_chain0_head_change_cb_add(block, ei, extack);
a9b19443 618 if (err)
f71e0ca4 619 goto err_chain0_head_change_cb_add;
caa72601 620
60513bd8 621 err = tcf_block_offload_bind(block, q, ei, extack);
caa72601
JP
622 if (err)
623 goto err_block_offload_bind;
624
6529eaba
JP
625 *p_block = block;
626 return 0;
2190d1d0 627
caa72601 628err_block_offload_bind:
f71e0ca4
JP
629 tcf_chain0_head_change_cb_del(block, ei);
630err_chain0_head_change_cb_add:
f36fe1c4
JP
631 tcf_block_owner_del(block, q, ei->binder_type);
632err_block_owner_add:
48617387
JP
633 if (created) {
634 if (tcf_block_shared(block))
635 tcf_block_remove(block, net);
636err_block_insert:
48617387
JP
637 kfree(block);
638 } else {
639 block->refcnt--;
640 }
2190d1d0 641 return err;
6529eaba 642}
8c4083b3
JP
643EXPORT_SYMBOL(tcf_block_get_ext);
644
c7eb7d72
JP
645static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
646{
647 struct tcf_proto __rcu **p_filter_chain = priv;
648
649 rcu_assign_pointer(*p_filter_chain, tp_head);
650}
651
8c4083b3 652int tcf_block_get(struct tcf_block **p_block,
8d1a77f9
AA
653 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
654 struct netlink_ext_ack *extack)
8c4083b3 655{
c7eb7d72
JP
656 struct tcf_block_ext_info ei = {
657 .chain_head_change = tcf_chain_head_change_dflt,
658 .chain_head_change_priv = p_filter_chain,
659 };
8c4083b3 660
c7eb7d72 661 WARN_ON(!p_filter_chain);
8d1a77f9 662 return tcf_block_get_ext(p_block, q, &ei, extack);
8c4083b3 663}
6529eaba
JP
664EXPORT_SYMBOL(tcf_block_get);
665
7aa0045d 666/* XXX: Standalone actions are not allowed to jump to any chain, and bound
a60b3f51 667 * actions should be all removed after flushing.
7aa0045d 668 */
c7eb7d72 669void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
e1ea2f98 670 struct tcf_block_ext_info *ei)
7aa0045d 671{
efbf7897 672 struct tcf_chain *chain, *tmp;
1697c4bb 673
c30abd5e
DM
674 if (!block)
675 return;
f71e0ca4 676 tcf_chain0_head_change_cb_del(block, ei);
f36fe1c4 677 tcf_block_owner_del(block, q, ei->binder_type);
a60b3f51 678
f71e0ca4 679 if (block->refcnt == 1) {
48617387
JP
680 if (tcf_block_shared(block))
681 tcf_block_remove(block, block->net);
682
683 /* Hold a refcnt for all chains, so that they don't disappear
684 * while we are iterating.
685 */
686 list_for_each_entry(chain, &block->chain_list, list)
687 tcf_chain_hold(chain);
688
689 list_for_each_entry(chain, &block->chain_list, list)
690 tcf_chain_flush(chain);
691 }
e2ef7544 692
4bb1b116
JP
693 tcf_block_offload_unbind(block, q, ei);
694
f71e0ca4 695 if (block->refcnt == 1) {
48617387
JP
696 /* At this point, all the chains should have refcnt >= 1. */
697 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
698 tcf_chain_put(chain);
df45bf84 699
f71e0ca4
JP
700 block->refcnt--;
701 if (list_empty(&block->chain_list))
702 kfree(block);
48617387 703 }
6529eaba 704}
8c4083b3
JP
705EXPORT_SYMBOL(tcf_block_put_ext);
706
707void tcf_block_put(struct tcf_block *block)
708{
709 struct tcf_block_ext_info ei = {0, };
710
4853f128
JP
711 if (!block)
712 return;
c7eb7d72 713 tcf_block_put_ext(block, block->q, &ei);
8c4083b3 714}
e1ea2f98 715
6529eaba 716EXPORT_SYMBOL(tcf_block_put);
cf1facda 717
acb67442
JP
718struct tcf_block_cb {
719 struct list_head list;
720 tc_setup_cb_t *cb;
721 void *cb_ident;
722 void *cb_priv;
723 unsigned int refcnt;
724};
725
726void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
727{
728 return block_cb->cb_priv;
729}
730EXPORT_SYMBOL(tcf_block_cb_priv);
731
732struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
733 tc_setup_cb_t *cb, void *cb_ident)
734{ struct tcf_block_cb *block_cb;
735
736 list_for_each_entry(block_cb, &block->cb_list, list)
737 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
738 return block_cb;
739 return NULL;
740}
741EXPORT_SYMBOL(tcf_block_cb_lookup);
742
743void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
744{
745 block_cb->refcnt++;
746}
747EXPORT_SYMBOL(tcf_block_cb_incref);
748
749unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
750{
751 return --block_cb->refcnt;
752}
753EXPORT_SYMBOL(tcf_block_cb_decref);
754
32636742
JH
755static int
756tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
757 void *cb_priv, bool add, bool offload_in_use,
758 struct netlink_ext_ack *extack)
759{
760 struct tcf_chain *chain;
761 struct tcf_proto *tp;
762 int err;
763
764 list_for_each_entry(chain, &block->chain_list, list) {
765 for (tp = rtnl_dereference(chain->filter_chain); tp;
766 tp = rtnl_dereference(tp->next)) {
767 if (tp->ops->reoffload) {
768 err = tp->ops->reoffload(tp, add, cb, cb_priv,
769 extack);
770 if (err && add)
771 goto err_playback_remove;
772 } else if (add && offload_in_use) {
773 err = -EOPNOTSUPP;
774 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
775 goto err_playback_remove;
776 }
777 }
778 }
779
780 return 0;
781
782err_playback_remove:
783 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
784 extack);
785 return err;
786}
787
acb67442
JP
788struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
789 tc_setup_cb_t *cb, void *cb_ident,
60513bd8
JH
790 void *cb_priv,
791 struct netlink_ext_ack *extack)
acb67442
JP
792{
793 struct tcf_block_cb *block_cb;
32636742 794 int err;
acb67442 795
32636742
JH
796 /* Replay any already present rules */
797 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
798 tcf_block_offload_in_use(block),
799 extack);
800 if (err)
801 return ERR_PTR(err);
caa72601 802
acb67442
JP
803 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
804 if (!block_cb)
caa72601 805 return ERR_PTR(-ENOMEM);
acb67442
JP
806 block_cb->cb = cb;
807 block_cb->cb_ident = cb_ident;
808 block_cb->cb_priv = cb_priv;
809 list_add(&block_cb->list, &block->cb_list);
810 return block_cb;
811}
812EXPORT_SYMBOL(__tcf_block_cb_register);
813
814int tcf_block_cb_register(struct tcf_block *block,
815 tc_setup_cb_t *cb, void *cb_ident,
60513bd8 816 void *cb_priv, struct netlink_ext_ack *extack)
acb67442
JP
817{
818 struct tcf_block_cb *block_cb;
819
60513bd8
JH
820 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
821 extack);
baa2d2b1 822 return PTR_ERR_OR_ZERO(block_cb);
acb67442
JP
823}
824EXPORT_SYMBOL(tcf_block_cb_register);
825
32636742
JH
826void __tcf_block_cb_unregister(struct tcf_block *block,
827 struct tcf_block_cb *block_cb)
acb67442 828{
32636742
JH
829 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
830 false, tcf_block_offload_in_use(block),
831 NULL);
acb67442
JP
832 list_del(&block_cb->list);
833 kfree(block_cb);
834}
835EXPORT_SYMBOL(__tcf_block_cb_unregister);
836
837void tcf_block_cb_unregister(struct tcf_block *block,
838 tc_setup_cb_t *cb, void *cb_ident)
839{
840 struct tcf_block_cb *block_cb;
841
842 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
843 if (!block_cb)
844 return;
32636742 845 __tcf_block_cb_unregister(block, block_cb);
acb67442
JP
846}
847EXPORT_SYMBOL(tcf_block_cb_unregister);
848
849static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
850 void *type_data, bool err_stop)
851{
852 struct tcf_block_cb *block_cb;
853 int ok_count = 0;
854 int err;
855
9a99dc1c
DM
856 /* Make sure all netdevs sharing this block are offload-capable. */
857 if (block->nooffloaddevcnt && err_stop)
858 return -EOPNOTSUPP;
859
acb67442
JP
860 list_for_each_entry(block_cb, &block->cb_list, list) {
861 err = block_cb->cb(type, type_data, block_cb->cb_priv);
862 if (err) {
863 if (err_stop)
864 return err;
865 } else {
866 ok_count++;
867 }
868 }
869 return ok_count;
870}
871
87d83093
JP
872/* Main classifier routine: scans classifier chain attached
873 * to this qdisc, (optionally) tests for protocol and asks
874 * specific classifiers.
875 */
876int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
877 struct tcf_result *res, bool compat_mode)
878{
879 __be16 protocol = tc_skb_protocol(skb);
880#ifdef CONFIG_NET_CLS_ACT
881 const int max_reclassify_loop = 4;
ee538dce
JP
882 const struct tcf_proto *orig_tp = tp;
883 const struct tcf_proto *first_tp;
87d83093
JP
884 int limit = 0;
885
886reclassify:
887#endif
888 for (; tp; tp = rcu_dereference_bh(tp->next)) {
889 int err;
890
891 if (tp->protocol != protocol &&
892 tp->protocol != htons(ETH_P_ALL))
893 continue;
894
895 err = tp->classify(skb, tp, res);
896#ifdef CONFIG_NET_CLS_ACT
db50514f 897 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
ee538dce 898 first_tp = orig_tp;
87d83093 899 goto reset;
db50514f 900 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
ee538dce 901 first_tp = res->goto_tp;
db50514f
JP
902 goto reset;
903 }
87d83093
JP
904#endif
905 if (err >= 0)
906 return err;
907 }
908
909 return TC_ACT_UNSPEC; /* signal: continue lookup */
910#ifdef CONFIG_NET_CLS_ACT
911reset:
912 if (unlikely(limit++ >= max_reclassify_loop)) {
9d3aaff3
JP
913 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
914 tp->chain->block->index,
915 tp->prio & 0xffff,
87d83093
JP
916 ntohs(tp->protocol));
917 return TC_ACT_SHOT;
918 }
919
ee538dce 920 tp = first_tp;
87d83093
JP
921 protocol = tc_skb_protocol(skb);
922 goto reclassify;
923#endif
924}
925EXPORT_SYMBOL(tcf_classify);
926
2190d1d0
JP
927struct tcf_chain_info {
928 struct tcf_proto __rcu **pprev;
929 struct tcf_proto __rcu *next;
930};
931
932static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
933{
934 return rtnl_dereference(*chain_info->pprev);
935}
936
937static void tcf_chain_tp_insert(struct tcf_chain *chain,
938 struct tcf_chain_info *chain_info,
939 struct tcf_proto *tp)
940{
c7eb7d72 941 if (*chain_info->pprev == chain->filter_chain)
f71e0ca4 942 tcf_chain0_head_change(chain, tp);
2190d1d0
JP
943 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
944 rcu_assign_pointer(*chain_info->pprev, tp);
e2ef7544 945 tcf_chain_hold(chain);
2190d1d0
JP
946}
947
948static void tcf_chain_tp_remove(struct tcf_chain *chain,
949 struct tcf_chain_info *chain_info,
950 struct tcf_proto *tp)
951{
952 struct tcf_proto *next = rtnl_dereference(chain_info->next);
953
c7eb7d72 954 if (tp == chain->filter_chain)
f71e0ca4 955 tcf_chain0_head_change(chain, next);
2190d1d0 956 RCU_INIT_POINTER(*chain_info->pprev, next);
e2ef7544 957 tcf_chain_put(chain);
2190d1d0
JP
958}
959
960static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
961 struct tcf_chain_info *chain_info,
962 u32 protocol, u32 prio,
963 bool prio_allocate)
964{
965 struct tcf_proto **pprev;
966 struct tcf_proto *tp;
967
968 /* Check the chain for existence of proto-tcf with this priority */
969 for (pprev = &chain->filter_chain;
970 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
971 if (tp->prio >= prio) {
972 if (tp->prio == prio) {
973 if (prio_allocate ||
974 (tp->protocol != protocol && protocol))
975 return ERR_PTR(-EINVAL);
976 } else {
977 tp = NULL;
978 }
979 break;
980 }
981 }
982 chain_info->pprev = pprev;
983 chain_info->next = tp ? tp->next : NULL;
984 return tp;
985}
986
7120371c 987static int tcf_fill_node(struct net *net, struct sk_buff *skb,
7960d1da
JP
988 struct tcf_proto *tp, struct tcf_block *block,
989 struct Qdisc *q, u32 parent, void *fh,
990 u32 portid, u32 seq, u16 flags, int event)
7120371c
WC
991{
992 struct tcmsg *tcm;
993 struct nlmsghdr *nlh;
994 unsigned char *b = skb_tail_pointer(skb);
995
996 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
997 if (!nlh)
998 goto out_nlmsg_trim;
999 tcm = nlmsg_data(nlh);
1000 tcm->tcm_family = AF_UNSPEC;
1001 tcm->tcm__pad1 = 0;
1002 tcm->tcm__pad2 = 0;
7960d1da
JP
1003 if (q) {
1004 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1005 tcm->tcm_parent = parent;
1006 } else {
1007 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1008 tcm->tcm_block_index = block->index;
1009 }
7120371c
WC
1010 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1011 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1012 goto nla_put_failure;
1013 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1014 goto nla_put_failure;
1015 if (!fh) {
1016 tcm->tcm_handle = 0;
1017 } else {
1018 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1019 goto nla_put_failure;
1020 }
1021 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1022 return skb->len;
1023
1024out_nlmsg_trim:
1025nla_put_failure:
1026 nlmsg_trim(skb, b);
1027 return -1;
1028}
1029
1030static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1031 struct nlmsghdr *n, struct tcf_proto *tp,
7960d1da
JP
1032 struct tcf_block *block, struct Qdisc *q,
1033 u32 parent, void *fh, int event, bool unicast)
7120371c
WC
1034{
1035 struct sk_buff *skb;
1036 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1037
1038 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1039 if (!skb)
1040 return -ENOBUFS;
1041
7960d1da
JP
1042 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1043 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
7120371c
WC
1044 kfree_skb(skb);
1045 return -EINVAL;
1046 }
1047
1048 if (unicast)
1049 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1050
1051 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1052 n->nlmsg_flags & NLM_F_ECHO);
1053}
1054
1055static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1056 struct nlmsghdr *n, struct tcf_proto *tp,
7960d1da 1057 struct tcf_block *block, struct Qdisc *q,
c35a4acc
AA
1058 u32 parent, void *fh, bool unicast, bool *last,
1059 struct netlink_ext_ack *extack)
7120371c
WC
1060{
1061 struct sk_buff *skb;
1062 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1063 int err;
1064
1065 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1066 if (!skb)
1067 return -ENOBUFS;
1068
7960d1da
JP
1069 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1070 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
c35a4acc 1071 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
7120371c
WC
1072 kfree_skb(skb);
1073 return -EINVAL;
1074 }
1075
571acf21 1076 err = tp->ops->delete(tp, fh, last, extack);
7120371c
WC
1077 if (err) {
1078 kfree_skb(skb);
1079 return err;
1080 }
1081
1082 if (unicast)
1083 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1084
c35a4acc
AA
1085 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1086 n->nlmsg_flags & NLM_F_ECHO);
1087 if (err < 0)
1088 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1089 return err;
7120371c
WC
1090}
1091
1092static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
7960d1da
JP
1093 struct tcf_block *block, struct Qdisc *q,
1094 u32 parent, struct nlmsghdr *n,
7120371c
WC
1095 struct tcf_chain *chain, int event)
1096{
1097 struct tcf_proto *tp;
1098
1099 for (tp = rtnl_dereference(chain->filter_chain);
1100 tp; tp = rtnl_dereference(tp->next))
7960d1da 1101 tfilter_notify(net, oskb, n, tp, block,
53189183 1102 q, parent, NULL, event, false);
7120371c
WC
1103}
1104
c431f89b 1105static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
c21ef3e3 1106 struct netlink_ext_ack *extack)
1da177e4 1107{
3b1e0a65 1108 struct net *net = sock_net(skb->sk);
add93b61 1109 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
1110 struct tcmsg *t;
1111 u32 protocol;
1112 u32 prio;
9d36d9e5 1113 bool prio_allocate;
1da177e4 1114 u32 parent;
5bc17018 1115 u32 chain_index;
7960d1da 1116 struct Qdisc *q = NULL;
2190d1d0 1117 struct tcf_chain_info chain_info;
5bc17018 1118 struct tcf_chain *chain = NULL;
6529eaba 1119 struct tcf_block *block;
1da177e4 1120 struct tcf_proto *tp;
1da177e4 1121 unsigned long cl;
8113c095 1122 void *fh;
1da177e4 1123 int err;
628185cf 1124 int tp_created;
1da177e4 1125
c431f89b 1126 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
dfc47ef8 1127 return -EPERM;
de179c8c 1128
1da177e4 1129replay:
628185cf
DB
1130 tp_created = 0;
1131
c21ef3e3 1132 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
de179c8c
H
1133 if (err < 0)
1134 return err;
1135
942b8165 1136 t = nlmsg_data(n);
1da177e4
LT
1137 protocol = TC_H_MIN(t->tcm_info);
1138 prio = TC_H_MAJ(t->tcm_info);
9d36d9e5 1139 prio_allocate = false;
1da177e4
LT
1140 parent = t->tcm_parent;
1141 cl = 0;
1142
1143 if (prio == 0) {
c431f89b
VB
1144 /* If no priority is provided by the user,
1145 * we allocate one.
1146 */
1147 if (n->nlmsg_flags & NLM_F_CREATE) {
1148 prio = TC_H_MAKE(0x80000000U, 0U);
1149 prio_allocate = true;
1150 } else {
c35a4acc 1151 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1da177e4 1152 return -ENOENT;
ea7f8277 1153 }
1da177e4
LT
1154 }
1155
1156 /* Find head of filter chain. */
1157
c431f89b
VB
1158 block = tcf_block_find(net, &q, &parent, &cl,
1159 t->tcm_ifindex, t->tcm_block_index, extack);
1160 if (IS_ERR(block)) {
1161 err = PTR_ERR(block);
1162 goto errout;
6bb16e7a 1163 }
5bc17018
JP
1164
1165 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1166 if (chain_index > TC_ACT_EXT_VAL_MASK) {
c35a4acc 1167 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
5bc17018
JP
1168 err = -EINVAL;
1169 goto errout;
1170 }
c431f89b 1171 chain = tcf_chain_get(block, chain_index, true);
5bc17018 1172 if (!chain) {
c35a4acc 1173 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
c431f89b 1174 err = -ENOMEM;
ea7f8277
DB
1175 goto errout;
1176 }
1da177e4 1177
2190d1d0
JP
1178 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1179 prio, prio_allocate);
1180 if (IS_ERR(tp)) {
c35a4acc 1181 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2190d1d0
JP
1182 err = PTR_ERR(tp);
1183 goto errout;
1da177e4
LT
1184 }
1185
1186 if (tp == NULL) {
1187 /* Proto-tcf does not exist, create new one */
1188
6bb16e7a 1189 if (tca[TCA_KIND] == NULL || !protocol) {
c35a4acc 1190 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
6bb16e7a 1191 err = -EINVAL;
1da177e4 1192 goto errout;
6bb16e7a 1193 }
1da177e4 1194
c431f89b 1195 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
c35a4acc 1196 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
6bb16e7a 1197 err = -ENOENT;
1da177e4 1198 goto errout;
6bb16e7a 1199 }
1da177e4 1200
9d36d9e5 1201 if (prio_allocate)
2190d1d0 1202 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
1da177e4 1203
33a48927 1204 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
c35a4acc 1205 protocol, prio, chain, extack);
33a48927
JP
1206 if (IS_ERR(tp)) {
1207 err = PTR_ERR(tp);
1da177e4
LT
1208 goto errout;
1209 }
12186be7 1210 tp_created = 1;
6bb16e7a 1211 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
c35a4acc 1212 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
6bb16e7a 1213 err = -EINVAL;
1da177e4 1214 goto errout;
6bb16e7a 1215 }
1da177e4
LT
1216
1217 fh = tp->ops->get(tp, t->tcm_handle);
1218
8113c095 1219 if (!fh) {
c431f89b 1220 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
c35a4acc 1221 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
6bb16e7a 1222 err = -ENOENT;
1da177e4 1223 goto errout;
6bb16e7a 1224 }
c431f89b
VB
1225 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1226 NL_SET_ERR_MSG(extack, "Filter already exists");
1227 err = -EEXIST;
1228 goto errout;
1da177e4
LT
1229 }
1230
2f7ef2f8 1231 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
7306db38
AA
1232 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1233 extack);
12186be7 1234 if (err == 0) {
2190d1d0
JP
1235 if (tp_created)
1236 tcf_chain_tp_insert(chain, &chain_info, tp);
7960d1da 1237 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
a10fa201 1238 RTM_NEWTFILTER, false);
12186be7
MU
1239 } else {
1240 if (tp_created)
715df5ec 1241 tcf_proto_destroy(tp, NULL);
12186be7 1242 }
1da177e4
LT
1243
1244errout:
5bc17018
JP
1245 if (chain)
1246 tcf_chain_put(chain);
1da177e4
LT
1247 if (err == -EAGAIN)
1248 /* Replay the request. */
1249 goto replay;
1250 return err;
1251}
1252
c431f89b
VB
1253static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1254 struct netlink_ext_ack *extack)
1255{
1256 struct net *net = sock_net(skb->sk);
1257 struct nlattr *tca[TCA_MAX + 1];
1258 struct tcmsg *t;
1259 u32 protocol;
1260 u32 prio;
1261 u32 parent;
1262 u32 chain_index;
1263 struct Qdisc *q = NULL;
1264 struct tcf_chain_info chain_info;
1265 struct tcf_chain *chain = NULL;
1266 struct tcf_block *block;
1267 struct tcf_proto *tp = NULL;
1268 unsigned long cl = 0;
1269 void *fh = NULL;
1270 int err;
1271
1272 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1273 return -EPERM;
1274
1275 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1276 if (err < 0)
1277 return err;
1278
1279 t = nlmsg_data(n);
1280 protocol = TC_H_MIN(t->tcm_info);
1281 prio = TC_H_MAJ(t->tcm_info);
1282 parent = t->tcm_parent;
1283
1284 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1285 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1286 return -ENOENT;
1287 }
1288
1289 /* Find head of filter chain. */
1290
1291 block = tcf_block_find(net, &q, &parent, &cl,
1292 t->tcm_ifindex, t->tcm_block_index, extack);
1293 if (IS_ERR(block)) {
1294 err = PTR_ERR(block);
1295 goto errout;
1296 }
1297
1298 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1299 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1300 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1301 err = -EINVAL;
1302 goto errout;
1303 }
1304 chain = tcf_chain_get(block, chain_index, false);
1305 if (!chain) {
1306 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1307 err = -EINVAL;
1308 goto errout;
1309 }
1310
1311 if (prio == 0) {
1312 tfilter_notify_chain(net, skb, block, q, parent, n,
1313 chain, RTM_DELTFILTER);
1314 tcf_chain_flush(chain);
1315 err = 0;
1316 goto errout;
1317 }
1318
1319 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1320 prio, false);
1321 if (!tp || IS_ERR(tp)) {
1322 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
0e399035 1323 err = tp ? PTR_ERR(tp) : -ENOENT;
c431f89b
VB
1324 goto errout;
1325 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1326 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1327 err = -EINVAL;
1328 goto errout;
1329 }
1330
1331 fh = tp->ops->get(tp, t->tcm_handle);
1332
1333 if (!fh) {
1334 if (t->tcm_handle == 0) {
1335 tcf_chain_tp_remove(chain, &chain_info, tp);
1336 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1337 RTM_DELTFILTER, false);
1338 tcf_proto_destroy(tp, extack);
1339 err = 0;
1340 } else {
1341 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1342 err = -ENOENT;
1343 }
1344 } else {
1345 bool last;
1346
1347 err = tfilter_del_notify(net, skb, n, tp, block,
1348 q, parent, fh, false, &last,
1349 extack);
1350 if (err)
1351 goto errout;
1352 if (last) {
1353 tcf_chain_tp_remove(chain, &chain_info, tp);
1354 tcf_proto_destroy(tp, extack);
1355 }
1356 }
1357
1358errout:
1359 if (chain)
1360 tcf_chain_put(chain);
1361 return err;
1362}
1363
1364static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1365 struct netlink_ext_ack *extack)
1366{
1367 struct net *net = sock_net(skb->sk);
1368 struct nlattr *tca[TCA_MAX + 1];
1369 struct tcmsg *t;
1370 u32 protocol;
1371 u32 prio;
1372 u32 parent;
1373 u32 chain_index;
1374 struct Qdisc *q = NULL;
1375 struct tcf_chain_info chain_info;
1376 struct tcf_chain *chain = NULL;
1377 struct tcf_block *block;
1378 struct tcf_proto *tp = NULL;
1379 unsigned long cl = 0;
1380 void *fh = NULL;
1381 int err;
1382
1383 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1384 if (err < 0)
1385 return err;
1386
1387 t = nlmsg_data(n);
1388 protocol = TC_H_MIN(t->tcm_info);
1389 prio = TC_H_MAJ(t->tcm_info);
1390 parent = t->tcm_parent;
1391
1392 if (prio == 0) {
1393 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1394 return -ENOENT;
1395 }
1396
1397 /* Find head of filter chain. */
1398
1399 block = tcf_block_find(net, &q, &parent, &cl,
1400 t->tcm_ifindex, t->tcm_block_index, extack);
1401 if (IS_ERR(block)) {
1402 err = PTR_ERR(block);
1403 goto errout;
1404 }
1405
1406 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1407 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1408 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1409 err = -EINVAL;
1410 goto errout;
1411 }
1412 chain = tcf_chain_get(block, chain_index, false);
1413 if (!chain) {
1414 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1415 err = -EINVAL;
1416 goto errout;
1417 }
1418
1419 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1420 prio, false);
1421 if (!tp || IS_ERR(tp)) {
1422 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
0e399035 1423 err = tp ? PTR_ERR(tp) : -ENOENT;
c431f89b
VB
1424 goto errout;
1425 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1426 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1427 err = -EINVAL;
1428 goto errout;
1429 }
1430
1431 fh = tp->ops->get(tp, t->tcm_handle);
1432
1433 if (!fh) {
1434 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1435 err = -ENOENT;
1436 } else {
1437 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1438 fh, RTM_NEWTFILTER, true);
1439 if (err < 0)
1440 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1441 }
1442
1443errout:
1444 if (chain)
1445 tcf_chain_put(chain);
1446 return err;
1447}
1448
aa767bfe 1449struct tcf_dump_args {
1da177e4
LT
1450 struct tcf_walker w;
1451 struct sk_buff *skb;
1452 struct netlink_callback *cb;
7960d1da 1453 struct tcf_block *block;
a10fa201
JP
1454 struct Qdisc *q;
1455 u32 parent;
1da177e4
LT
1456};
1457
8113c095 1458static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
1da177e4 1459{
aa767bfe 1460 struct tcf_dump_args *a = (void *)arg;
832d1d5b 1461 struct net *net = sock_net(a->skb->sk);
1da177e4 1462
7960d1da 1463 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
a10fa201 1464 n, NETLINK_CB(a->cb->skb).portid,
5a7a5555
JHS
1465 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1466 RTM_NEWTFILTER);
1da177e4
LT
1467}
1468
a10fa201
JP
1469static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1470 struct sk_buff *skb, struct netlink_callback *cb,
acb31fae
JP
1471 long index_start, long *p_index)
1472{
1473 struct net *net = sock_net(skb->sk);
7960d1da 1474 struct tcf_block *block = chain->block;
acb31fae
JP
1475 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1476 struct tcf_dump_args arg;
1477 struct tcf_proto *tp;
1478
1479 for (tp = rtnl_dereference(chain->filter_chain);
1480 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1481 if (*p_index < index_start)
1482 continue;
1483 if (TC_H_MAJ(tcm->tcm_info) &&
1484 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1485 continue;
1486 if (TC_H_MIN(tcm->tcm_info) &&
1487 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1488 continue;
1489 if (*p_index > index_start)
1490 memset(&cb->args[1], 0,
1491 sizeof(cb->args) - sizeof(cb->args[0]));
1492 if (cb->args[1] == 0) {
53189183 1493 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
acb31fae
JP
1494 NETLINK_CB(cb->skb).portid,
1495 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1496 RTM_NEWTFILTER) <= 0)
5bc17018 1497 return false;
acb31fae
JP
1498
1499 cb->args[1] = 1;
1500 }
1501 if (!tp->ops->walk)
1502 continue;
1503 arg.w.fn = tcf_node_dump;
1504 arg.skb = skb;
1505 arg.cb = cb;
7960d1da 1506 arg.block = block;
a10fa201
JP
1507 arg.q = q;
1508 arg.parent = parent;
acb31fae
JP
1509 arg.w.stop = 0;
1510 arg.w.skip = cb->args[1] - 1;
1511 arg.w.count = 0;
01683a14 1512 arg.w.cookie = cb->args[2];
acb31fae 1513 tp->ops->walk(tp, &arg.w);
01683a14 1514 cb->args[2] = arg.w.cookie;
acb31fae
JP
1515 cb->args[1] = arg.w.count + 1;
1516 if (arg.w.stop)
5bc17018 1517 return false;
acb31fae 1518 }
5bc17018 1519 return true;
acb31fae
JP
1520}
1521
bd27a875 1522/* called with RTNL */
1da177e4
LT
1523static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1524{
3b1e0a65 1525 struct net *net = sock_net(skb->sk);
5bc17018 1526 struct nlattr *tca[TCA_MAX + 1];
7960d1da 1527 struct Qdisc *q = NULL;
6529eaba 1528 struct tcf_block *block;
2190d1d0 1529 struct tcf_chain *chain;
942b8165 1530 struct tcmsg *tcm = nlmsg_data(cb->nlh);
acb31fae
JP
1531 long index_start;
1532 long index;
a10fa201 1533 u32 parent;
5bc17018 1534 int err;
1da177e4 1535
573ce260 1536 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1da177e4 1537 return skb->len;
5bc17018
JP
1538
1539 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1540 if (err)
1541 return err;
1542
7960d1da
JP
1543 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1544 block = tcf_block_lookup(net, tcm->tcm_block_index);
1545 if (!block)
1546 goto out;
d680b352
JP
1547 /* If we work with block index, q is NULL and parent value
1548 * will never be used in the following code. The check
1549 * in tcf_fill_node prevents it. However, compiler does not
1550 * see that far, so set parent to zero to silence the warning
1551 * about parent being uninitialized.
1552 */
1553 parent = 0;
a10fa201 1554 } else {
7960d1da
JP
1555 const struct Qdisc_class_ops *cops;
1556 struct net_device *dev;
1557 unsigned long cl = 0;
1558
1559 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1560 if (!dev)
1561 return skb->len;
1562
1563 parent = tcm->tcm_parent;
1564 if (!parent) {
1565 q = dev->qdisc;
1566 parent = q->handle;
1567 } else {
1568 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1569 }
1570 if (!q)
1571 goto out;
1572 cops = q->ops->cl_ops;
1573 if (!cops)
143976ce 1574 goto out;
7960d1da
JP
1575 if (!cops->tcf_block)
1576 goto out;
1577 if (TC_H_MIN(tcm->tcm_parent)) {
1578 cl = cops->find(q, tcm->tcm_parent);
1579 if (cl == 0)
1580 goto out;
1581 }
1582 block = cops->tcf_block(q, cl, NULL);
1583 if (!block)
1584 goto out;
1585 if (tcf_block_shared(block))
1586 q = NULL;
1da177e4 1587 }
1da177e4 1588
acb31fae
JP
1589 index_start = cb->args[0];
1590 index = 0;
5bc17018
JP
1591
1592 list_for_each_entry(chain, &block->chain_list, list) {
1593 if (tca[TCA_CHAIN] &&
1594 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1595 continue;
a10fa201 1596 if (!tcf_chain_dump(chain, q, parent, skb, cb,
5ae437ad
RK
1597 index_start, &index)) {
1598 err = -EMSGSIZE;
5bc17018 1599 break;
5ae437ad 1600 }
5bc17018
JP
1601 }
1602
acb31fae 1603 cb->args[0] = index;
1da177e4 1604
1da177e4 1605out:
5ae437ad
RK
1606 /* If we did no progress, the error (EMSGSIZE) is real */
1607 if (skb->len == 0 && err)
1608 return err;
1da177e4
LT
1609 return skb->len;
1610}
1611
18d0264f 1612void tcf_exts_destroy(struct tcf_exts *exts)
1da177e4
LT
1613{
1614#ifdef CONFIG_NET_CLS_ACT
90b73b77 1615 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
22dc13c8
WC
1616 kfree(exts->actions);
1617 exts->nr_actions = 0;
1da177e4
LT
1618#endif
1619}
aa767bfe 1620EXPORT_SYMBOL(tcf_exts_destroy);
1da177e4 1621
c1b52739 1622int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
50a56190
AA
1623 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
1624 struct netlink_ext_ack *extack)
1da177e4 1625{
1da177e4
LT
1626#ifdef CONFIG_NET_CLS_ACT
1627 {
1da177e4 1628 struct tc_action *act;
d04e6990 1629 size_t attr_size = 0;
1da177e4 1630
5da57f42 1631 if (exts->police && tb[exts->police]) {
9fb9f251
JP
1632 act = tcf_action_init_1(net, tp, tb[exts->police],
1633 rate_tlv, "police", ovr,
789871bb 1634 TCA_ACT_BIND, true, extack);
ab27cfb8
PM
1635 if (IS_ERR(act))
1636 return PTR_ERR(act);
1da177e4 1637
33be6271 1638 act->type = exts->type = TCA_OLD_COMPAT;
22dc13c8
WC
1639 exts->actions[0] = act;
1640 exts->nr_actions = 1;
5da57f42 1641 } else if (exts->action && tb[exts->action]) {
90b73b77 1642 int err;
22dc13c8 1643
9fb9f251
JP
1644 err = tcf_action_init(net, tp, tb[exts->action],
1645 rate_tlv, NULL, ovr, TCA_ACT_BIND,
90b73b77 1646 exts->actions, &attr_size, true,
789871bb 1647 extack);
90b73b77 1648 if (err < 0)
33be6271 1649 return err;
90b73b77 1650 exts->nr_actions = err;
1da177e4 1651 }
e4b95c41 1652 exts->net = net;
1da177e4 1653 }
1da177e4 1654#else
5da57f42 1655 if ((exts->action && tb[exts->action]) ||
50a56190
AA
1656 (exts->police && tb[exts->police])) {
1657 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
1da177e4 1658 return -EOPNOTSUPP;
50a56190 1659 }
1da177e4
LT
1660#endif
1661
1662 return 0;
1663}
aa767bfe 1664EXPORT_SYMBOL(tcf_exts_validate);
1da177e4 1665
9b0d4446 1666void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1da177e4
LT
1667{
1668#ifdef CONFIG_NET_CLS_ACT
22dc13c8
WC
1669 struct tcf_exts old = *dst;
1670
9b0d4446 1671 *dst = *src;
22dc13c8 1672 tcf_exts_destroy(&old);
1da177e4
LT
1673#endif
1674}
aa767bfe 1675EXPORT_SYMBOL(tcf_exts_change);
1da177e4 1676
22dc13c8
WC
1677#ifdef CONFIG_NET_CLS_ACT
1678static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1679{
1680 if (exts->nr_actions == 0)
1681 return NULL;
1682 else
1683 return exts->actions[0];
1684}
1685#endif
33be6271 1686
5da57f42 1687int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
1688{
1689#ifdef CONFIG_NET_CLS_ACT
9cc63db5
CW
1690 struct nlattr *nest;
1691
978dfd8d 1692 if (exts->action && tcf_exts_has_actions(exts)) {
1da177e4
LT
1693 /*
1694 * again for backward compatible mode - we want
1695 * to work with both old and new modes of entering
1696 * tc data even if iproute2 was newer - jhs
1697 */
33be6271 1698 if (exts->type != TCA_OLD_COMPAT) {
5da57f42 1699 nest = nla_nest_start(skb, exts->action);
4b3550ef
PM
1700 if (nest == NULL)
1701 goto nla_put_failure;
22dc13c8 1702
90b73b77 1703 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
add93b61 1704 goto nla_put_failure;
4b3550ef 1705 nla_nest_end(skb, nest);
5da57f42 1706 } else if (exts->police) {
33be6271 1707 struct tc_action *act = tcf_exts_first_act(exts);
5da57f42 1708 nest = nla_nest_start(skb, exts->police);
63acd680 1709 if (nest == NULL || !act)
4b3550ef 1710 goto nla_put_failure;
33be6271 1711 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
add93b61 1712 goto nla_put_failure;
4b3550ef 1713 nla_nest_end(skb, nest);
1da177e4
LT
1714 }
1715 }
1da177e4 1716 return 0;
9cc63db5
CW
1717
1718nla_put_failure:
1719 nla_nest_cancel(skb, nest);
1da177e4 1720 return -1;
9cc63db5
CW
1721#else
1722 return 0;
1723#endif
1da177e4 1724}
aa767bfe 1725EXPORT_SYMBOL(tcf_exts_dump);
1da177e4 1726
aa767bfe 1727
5da57f42 1728int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
1729{
1730#ifdef CONFIG_NET_CLS_ACT
33be6271 1731 struct tc_action *a = tcf_exts_first_act(exts);
b057df24 1732 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
33be6271 1733 return -1;
1da177e4
LT
1734#endif
1735 return 0;
1da177e4 1736}
aa767bfe 1737EXPORT_SYMBOL(tcf_exts_dump_stats);
1da177e4 1738
717503b9
JP
1739static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1740 enum tc_setup_type type,
1741 void *type_data, bool err_stop)
b3f55bdd
JP
1742{
1743 int ok_count = 0;
1744#ifdef CONFIG_NET_CLS_ACT
1745 const struct tc_action *a;
1746 struct net_device *dev;
9d452ceb 1747 int i, ret;
b3f55bdd
JP
1748
1749 if (!tcf_exts_has_actions(exts))
1750 return 0;
1751
9d452ceb
OG
1752 for (i = 0; i < exts->nr_actions; i++) {
1753 a = exts->actions[i];
b3f55bdd
JP
1754 if (!a->ops->get_dev)
1755 continue;
1756 dev = a->ops->get_dev(a);
7612fb03 1757 if (!dev)
b3f55bdd
JP
1758 continue;
1759 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1760 if (ret < 0)
1761 return ret;
1762 ok_count += ret;
1763 }
1764#endif
1765 return ok_count;
1766}
717503b9 1767
208c0f4b
JP
1768int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1769 enum tc_setup_type type, void *type_data, bool err_stop)
717503b9 1770{
9a99dc1c 1771 int ok_count;
208c0f4b
JP
1772 int ret;
1773
9a99dc1c
DM
1774 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1775 if (ret < 0)
1776 return ret;
1777 ok_count = ret;
208c0f4b 1778
f8f4bef3 1779 if (!exts || ok_count)
9a99dc1c 1780 return ok_count;
208c0f4b
JP
1781 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1782 if (ret < 0)
1783 return ret;
1784 ok_count += ret;
1785
1786 return ok_count;
717503b9
JP
1787}
1788EXPORT_SYMBOL(tc_setup_cb_call);
b3f55bdd 1789
48617387
JP
1790static __net_init int tcf_net_init(struct net *net)
1791{
1792 struct tcf_net *tn = net_generic(net, tcf_net_id);
1793
1794 idr_init(&tn->idr);
1795 return 0;
1796}
1797
1798static void __net_exit tcf_net_exit(struct net *net)
1799{
1800 struct tcf_net *tn = net_generic(net, tcf_net_id);
1801
1802 idr_destroy(&tn->idr);
1803}
1804
1805static struct pernet_operations tcf_net_ops = {
1806 .init = tcf_net_init,
1807 .exit = tcf_net_exit,
1808 .id = &tcf_net_id,
1809 .size = sizeof(struct tcf_net),
1810};
1811
1da177e4
LT
1812static int __init tc_filter_init(void)
1813{
48617387
JP
1814 int err;
1815
7aa0045d
CW
1816 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1817 if (!tc_filter_wq)
1818 return -ENOMEM;
1819
48617387
JP
1820 err = register_pernet_subsys(&tcf_net_ops);
1821 if (err)
1822 goto err_register_pernet_subsys;
1823
c431f89b
VB
1824 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
1825 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
1826 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
b97bac64 1827 tc_dump_tfilter, 0);
1da177e4 1828
1da177e4 1829 return 0;
48617387
JP
1830
1831err_register_pernet_subsys:
1832 destroy_workqueue(tc_filter_wq);
1833 return err;
1da177e4
LT
1834}
1835
1836subsys_initcall(tc_filter_init);