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