]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/sched/cls_api.c
Merge tag 'nfsd-5.3-1' of git://linux-nfs.org/~bfields/linux
[mirror_ubuntu-jammy-kernel.git] / net / sched / cls_api.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * net/sched/cls_api.c Packet classifier API.
4 *
1da177e4
LT
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 *
7 * Changes:
8 *
9 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
1da177e4
LT
10 */
11
1da177e4
LT
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kernel.h>
1da177e4 15#include <linux/string.h>
1da177e4 16#include <linux/errno.h>
33a48927 17#include <linux/err.h>
1da177e4 18#include <linux/skbuff.h>
1da177e4
LT
19#include <linux/init.h>
20#include <linux/kmod.h>
5a0e3ad6 21#include <linux/slab.h>
48617387 22#include <linux/idr.h>
7f76fa36 23#include <linux/rhashtable.h>
b854272b
DL
24#include <net/net_namespace.h>
25#include <net/sock.h>
dc5fc579 26#include <net/netlink.h>
1da177e4
LT
27#include <net/pkt_sched.h>
28#include <net/pkt_cls.h>
e3ab786b 29#include <net/tc_act/tc_pedit.h>
3a7b6861
PNA
30#include <net/tc_act/tc_mirred.h>
31#include <net/tc_act/tc_vlan.h>
32#include <net/tc_act/tc_tunnel_key.h>
33#include <net/tc_act/tc_csum.h>
34#include <net/tc_act/tc_gact.h>
8c8cfc6e 35#include <net/tc_act/tc_police.h>
a7a7be60 36#include <net/tc_act/tc_sample.h>
3a7b6861 37#include <net/tc_act/tc_skbedit.h>
b57dc7c1 38#include <net/tc_act/tc_ct.h>
1da177e4 39
e331473f
DC
40extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
41
1da177e4 42/* The list of all installed classifier types */
36272874 43static LIST_HEAD(tcf_proto_base);
1da177e4
LT
44
45/* Protects list of registered TC modules. It is pure SMP lock. */
46static DEFINE_RWLOCK(cls_mod_lock);
47
48/* Find classifier type by string name */
49
f34e8bff 50static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
1da177e4 51{
dcd76081 52 const struct tcf_proto_ops *t, *res = NULL;
1da177e4
LT
53
54 if (kind) {
55 read_lock(&cls_mod_lock);
36272874 56 list_for_each_entry(t, &tcf_proto_base, head) {
33a48927 57 if (strcmp(kind, t->kind) == 0) {
dcd76081
ED
58 if (try_module_get(t->owner))
59 res = t;
1da177e4
LT
60 break;
61 }
62 }
63 read_unlock(&cls_mod_lock);
64 }
dcd76081 65 return res;
1da177e4
LT
66}
67
f34e8bff 68static const struct tcf_proto_ops *
12db03b6
VB
69tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
70 struct netlink_ext_ack *extack)
f34e8bff
JP
71{
72 const struct tcf_proto_ops *ops;
73
74 ops = __tcf_proto_lookup_ops(kind);
75 if (ops)
76 return ops;
77#ifdef CONFIG_MODULES
12db03b6
VB
78 if (rtnl_held)
79 rtnl_unlock();
f34e8bff 80 request_module("cls_%s", kind);
12db03b6
VB
81 if (rtnl_held)
82 rtnl_lock();
f34e8bff
JP
83 ops = __tcf_proto_lookup_ops(kind);
84 /* We dropped the RTNL semaphore in order to perform
85 * the module load. So, even if we succeeded in loading
86 * the module we have to replay the request. We indicate
87 * this using -EAGAIN.
88 */
89 if (ops) {
90 module_put(ops->owner);
91 return ERR_PTR(-EAGAIN);
92 }
93#endif
94 NL_SET_ERR_MSG(extack, "TC classifier not found");
95 return ERR_PTR(-ENOENT);
96}
97
1da177e4
LT
98/* Register(unregister) new classifier type */
99
100int register_tcf_proto_ops(struct tcf_proto_ops *ops)
101{
36272874 102 struct tcf_proto_ops *t;
1da177e4
LT
103 int rc = -EEXIST;
104
105 write_lock(&cls_mod_lock);
36272874 106 list_for_each_entry(t, &tcf_proto_base, head)
1da177e4
LT
107 if (!strcmp(ops->kind, t->kind))
108 goto out;
109
36272874 110 list_add_tail(&ops->head, &tcf_proto_base);
1da177e4
LT
111 rc = 0;
112out:
113 write_unlock(&cls_mod_lock);
114 return rc;
115}
aa767bfe 116EXPORT_SYMBOL(register_tcf_proto_ops);
1da177e4 117
7aa0045d
CW
118static struct workqueue_struct *tc_filter_wq;
119
1da177e4
LT
120int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
121{
36272874 122 struct tcf_proto_ops *t;
1da177e4
LT
123 int rc = -ENOENT;
124
c78e1746
DB
125 /* Wait for outstanding call_rcu()s, if any, from a
126 * tcf_proto_ops's destroy() handler.
127 */
128 rcu_barrier();
7aa0045d 129 flush_workqueue(tc_filter_wq);
c78e1746 130
1da177e4 131 write_lock(&cls_mod_lock);
dcd76081
ED
132 list_for_each_entry(t, &tcf_proto_base, head) {
133 if (t == ops) {
134 list_del(&t->head);
135 rc = 0;
1da177e4 136 break;
dcd76081
ED
137 }
138 }
1da177e4
LT
139 write_unlock(&cls_mod_lock);
140 return rc;
141}
aa767bfe 142EXPORT_SYMBOL(unregister_tcf_proto_ops);
1da177e4 143
aaa908ff 144bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
7aa0045d 145{
aaa908ff
CW
146 INIT_RCU_WORK(rwork, func);
147 return queue_rcu_work(tc_filter_wq, rwork);
7aa0045d
CW
148}
149EXPORT_SYMBOL(tcf_queue_work);
150
1da177e4
LT
151/* Select new prio value from the range, managed by kernel. */
152
aa767bfe 153static inline u32 tcf_auto_prio(struct tcf_proto *tp)
1da177e4 154{
aa767bfe 155 u32 first = TC_H_MAKE(0xC0000000U, 0U);
1da177e4
LT
156
157 if (tp)
cc7ec456 158 first = tp->prio - 1;
1da177e4 159
7961973a 160 return TC_H_MAJ(first);
1da177e4
LT
161}
162
470502de
VB
163static bool tcf_proto_is_unlocked(const char *kind)
164{
165 const struct tcf_proto_ops *ops;
166 bool ret;
167
168 ops = tcf_proto_lookup_ops(kind, false, NULL);
169 /* On error return false to take rtnl lock. Proto lookup/create
170 * functions will perform lookup again and properly handle errors.
171 */
172 if (IS_ERR(ops))
173 return false;
174
175 ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
176 module_put(ops->owner);
177 return ret;
178}
179
33a48927 180static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
c35a4acc 181 u32 prio, struct tcf_chain *chain,
12db03b6 182 bool rtnl_held,
c35a4acc 183 struct netlink_ext_ack *extack)
33a48927
JP
184{
185 struct tcf_proto *tp;
186 int err;
187
188 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
189 if (!tp)
190 return ERR_PTR(-ENOBUFS);
191
12db03b6 192 tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
f34e8bff
JP
193 if (IS_ERR(tp->ops)) {
194 err = PTR_ERR(tp->ops);
d68d75fd 195 goto errout;
33a48927
JP
196 }
197 tp->classify = tp->ops->classify;
198 tp->protocol = protocol;
199 tp->prio = prio;
5bc17018 200 tp->chain = chain;
8b64678e 201 spin_lock_init(&tp->lock);
4dbfa766 202 refcount_set(&tp->refcnt, 1);
33a48927
JP
203
204 err = tp->ops->init(tp);
205 if (err) {
206 module_put(tp->ops->owner);
207 goto errout;
208 }
209 return tp;
210
211errout:
212 kfree(tp);
213 return ERR_PTR(err);
214}
215
4dbfa766
VB
216static void tcf_proto_get(struct tcf_proto *tp)
217{
218 refcount_inc(&tp->refcnt);
219}
220
221static void tcf_chain_put(struct tcf_chain *chain);
222
12db03b6 223static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
715df5ec 224 struct netlink_ext_ack *extack)
cf1facda 225{
12db03b6 226 tp->ops->destroy(tp, rtnl_held, extack);
4dbfa766 227 tcf_chain_put(tp->chain);
763dbf63
WC
228 module_put(tp->ops->owner);
229 kfree_rcu(tp, rcu);
cf1facda
JP
230}
231
12db03b6 232static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
4dbfa766
VB
233 struct netlink_ext_ack *extack)
234{
235 if (refcount_dec_and_test(&tp->refcnt))
12db03b6 236 tcf_proto_destroy(tp, rtnl_held, extack);
4dbfa766
VB
237}
238
268a351d 239static int walker_check_empty(struct tcf_proto *tp, void *fh,
6676d5e4 240 struct tcf_walker *arg)
8b64678e 241{
268a351d 242 if (fh) {
6676d5e4
VB
243 arg->nonempty = true;
244 return -1;
245 }
246 return 0;
8b64678e
VB
247}
248
12db03b6 249static bool tcf_proto_is_empty(struct tcf_proto *tp, bool rtnl_held)
8b64678e 250{
6676d5e4 251 struct tcf_walker walker = { .fn = walker_check_empty, };
8b64678e
VB
252
253 if (tp->ops->walk) {
12db03b6 254 tp->ops->walk(tp, &walker, rtnl_held);
6676d5e4 255 return !walker.nonempty;
8b64678e
VB
256 }
257 return true;
258}
259
12db03b6 260static bool tcf_proto_check_delete(struct tcf_proto *tp, bool rtnl_held)
8b64678e
VB
261{
262 spin_lock(&tp->lock);
12db03b6 263 if (tcf_proto_is_empty(tp, rtnl_held))
8b64678e
VB
264 tp->deleting = true;
265 spin_unlock(&tp->lock);
266 return tp->deleting;
267}
268
269static void tcf_proto_mark_delete(struct tcf_proto *tp)
270{
271 spin_lock(&tp->lock);
272 tp->deleting = true;
273 spin_unlock(&tp->lock);
274}
275
276static bool tcf_proto_is_deleting(struct tcf_proto *tp)
277{
278 bool deleting;
279
280 spin_lock(&tp->lock);
281 deleting = tp->deleting;
282 spin_unlock(&tp->lock);
283
284 return deleting;
285}
286
c266f64d
VB
287#define ASSERT_BLOCK_LOCKED(block) \
288 lockdep_assert_held(&(block)->lock)
289
a9b19443
JP
290struct tcf_filter_chain_list_item {
291 struct list_head list;
292 tcf_chain_head_change_t *chain_head_change;
293 void *chain_head_change_priv;
294};
295
5bc17018
JP
296static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
297 u32 chain_index)
2190d1d0 298{
5bc17018
JP
299 struct tcf_chain *chain;
300
c266f64d
VB
301 ASSERT_BLOCK_LOCKED(block);
302
5bc17018
JP
303 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
304 if (!chain)
305 return NULL;
306 list_add_tail(&chain->list, &block->chain_list);
ed76f5ed 307 mutex_init(&chain->filter_chain_lock);
5bc17018
JP
308 chain->block = block;
309 chain->index = chain_index;
e2ef7544 310 chain->refcnt = 1;
f71e0ca4
JP
311 if (!chain->index)
312 block->chain0.chain = chain;
5bc17018 313 return chain;
2190d1d0
JP
314}
315
a9b19443
JP
316static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
317 struct tcf_proto *tp_head)
318{
319 if (item->chain_head_change)
320 item->chain_head_change(tp_head, item->chain_head_change_priv);
321}
f71e0ca4
JP
322
323static void tcf_chain0_head_change(struct tcf_chain *chain,
324 struct tcf_proto *tp_head)
c7eb7d72 325{
a9b19443 326 struct tcf_filter_chain_list_item *item;
f71e0ca4 327 struct tcf_block *block = chain->block;
a9b19443 328
f71e0ca4
JP
329 if (chain->index)
330 return;
165f0135
VB
331
332 mutex_lock(&block->lock);
f71e0ca4 333 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
a9b19443 334 tcf_chain_head_change_item(item, tp_head);
165f0135 335 mutex_unlock(&block->lock);
c7eb7d72
JP
336}
337
c266f64d
VB
338/* Returns true if block can be safely freed. */
339
340static bool tcf_chain_detach(struct tcf_chain *chain)
f93e1cdc 341{
efbf7897
CW
342 struct tcf_block *block = chain->block;
343
c266f64d
VB
344 ASSERT_BLOCK_LOCKED(block);
345
e2ef7544 346 list_del(&chain->list);
f71e0ca4
JP
347 if (!chain->index)
348 block->chain0.chain = NULL;
c266f64d
VB
349
350 if (list_empty(&block->chain_list) &&
351 refcount_read(&block->refcnt) == 0)
352 return true;
353
354 return false;
355}
356
357static void tcf_block_destroy(struct tcf_block *block)
358{
359 mutex_destroy(&block->lock);
360 kfree_rcu(block, rcu);
361}
362
363static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
364{
365 struct tcf_block *block = chain->block;
366
ed76f5ed 367 mutex_destroy(&chain->filter_chain_lock);
ee3bbfe8 368 kfree_rcu(chain, rcu);
c266f64d
VB
369 if (free_block)
370 tcf_block_destroy(block);
e2ef7544 371}
744a4cf6 372
e2ef7544
CW
373static void tcf_chain_hold(struct tcf_chain *chain)
374{
c266f64d
VB
375 ASSERT_BLOCK_LOCKED(chain->block);
376
e2ef7544 377 ++chain->refcnt;
2190d1d0
JP
378}
379
3d32f4c5 380static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
1f3ed383 381{
c266f64d
VB
382 ASSERT_BLOCK_LOCKED(chain->block);
383
1f3ed383 384 /* In case all the references are action references, this
3d32f4c5 385 * chain should not be shown to the user.
1f3ed383
JP
386 */
387 return chain->refcnt == chain->action_refcnt;
388}
389
32a4f5ec
JP
390static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
391 u32 chain_index)
5bc17018
JP
392{
393 struct tcf_chain *chain;
394
c266f64d
VB
395 ASSERT_BLOCK_LOCKED(block);
396
5bc17018 397 list_for_each_entry(chain, &block->chain_list, list) {
32a4f5ec 398 if (chain->index == chain_index)
e2ef7544 399 return chain;
32a4f5ec
JP
400 }
401 return NULL;
402}
403
404static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
405 u32 seq, u16 flags, int event, bool unicast);
406
53681407
JP
407static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
408 u32 chain_index, bool create,
409 bool by_act)
32a4f5ec 410{
c266f64d
VB
411 struct tcf_chain *chain = NULL;
412 bool is_first_reference;
32a4f5ec 413
c266f64d
VB
414 mutex_lock(&block->lock);
415 chain = tcf_chain_lookup(block, chain_index);
32a4f5ec
JP
416 if (chain) {
417 tcf_chain_hold(chain);
53681407
JP
418 } else {
419 if (!create)
c266f64d 420 goto errout;
53681407
JP
421 chain = tcf_chain_create(block, chain_index);
422 if (!chain)
c266f64d 423 goto errout;
5bc17018 424 }
80532384 425
53681407
JP
426 if (by_act)
427 ++chain->action_refcnt;
c266f64d
VB
428 is_first_reference = chain->refcnt - chain->action_refcnt == 1;
429 mutex_unlock(&block->lock);
53681407
JP
430
431 /* Send notification only in case we got the first
432 * non-action reference. Until then, the chain acts only as
433 * a placeholder for actions pointing to it and user ought
434 * not know about them.
435 */
c266f64d 436 if (is_first_reference && !by_act)
53681407
JP
437 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
438 RTM_NEWCHAIN, false);
439
32a4f5ec 440 return chain;
c266f64d
VB
441
442errout:
443 mutex_unlock(&block->lock);
444 return chain;
5bc17018 445}
53681407 446
290b1c8b
JP
447static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
448 bool create)
53681407
JP
449{
450 return __tcf_chain_get(block, chain_index, create, false);
451}
5bc17018 452
1f3ed383
JP
453struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
454{
53681407 455 return __tcf_chain_get(block, chain_index, true, true);
1f3ed383
JP
456}
457EXPORT_SYMBOL(tcf_chain_get_by_act);
458
a5654820
VB
459static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
460 void *tmplt_priv);
461static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
462 void *tmplt_priv, u32 chain_index,
463 struct tcf_block *block, struct sk_buff *oskb,
464 u32 seq, u16 flags, bool unicast);
9f407f17 465
91052fa1
VB
466static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
467 bool explicitly_created)
5bc17018 468{
c266f64d 469 struct tcf_block *block = chain->block;
a5654820 470 const struct tcf_proto_ops *tmplt_ops;
b62989fc 471 bool free_block = false;
c266f64d 472 unsigned int refcnt;
a5654820 473 void *tmplt_priv;
c266f64d
VB
474
475 mutex_lock(&block->lock);
91052fa1
VB
476 if (explicitly_created) {
477 if (!chain->explicitly_created) {
478 mutex_unlock(&block->lock);
479 return;
480 }
481 chain->explicitly_created = false;
482 }
483
53681407
JP
484 if (by_act)
485 chain->action_refcnt--;
c266f64d
VB
486
487 /* tc_chain_notify_delete can't be called while holding block lock.
488 * However, when block is unlocked chain can be changed concurrently, so
489 * save these to temporary variables.
490 */
491 refcnt = --chain->refcnt;
a5654820
VB
492 tmplt_ops = chain->tmplt_ops;
493 tmplt_priv = chain->tmplt_priv;
53681407
JP
494
495 /* The last dropped non-action reference will trigger notification. */
b62989fc
VB
496 if (refcnt - chain->action_refcnt == 0 && !by_act) {
497 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index,
a5654820 498 block, NULL, 0, 0, false);
726d0612
VB
499 /* Last reference to chain, no need to lock. */
500 chain->flushing = false;
501 }
53681407 502
b62989fc
VB
503 if (refcnt == 0)
504 free_block = tcf_chain_detach(chain);
505 mutex_unlock(&block->lock);
506
c266f64d 507 if (refcnt == 0) {
a5654820 508 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
c266f64d 509 tcf_chain_destroy(chain, free_block);
32a4f5ec 510 }
5bc17018 511}
53681407 512
290b1c8b 513static void tcf_chain_put(struct tcf_chain *chain)
53681407 514{
91052fa1 515 __tcf_chain_put(chain, false, false);
53681407 516}
5bc17018 517
1f3ed383
JP
518void tcf_chain_put_by_act(struct tcf_chain *chain)
519{
91052fa1 520 __tcf_chain_put(chain, true, false);
1f3ed383
JP
521}
522EXPORT_SYMBOL(tcf_chain_put_by_act);
523
32a4f5ec
JP
524static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
525{
91052fa1 526 __tcf_chain_put(chain, false, true);
32a4f5ec
JP
527}
528
12db03b6 529static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
290b1c8b 530{
4dbfa766 531 struct tcf_proto *tp, *tp_next;
290b1c8b 532
ed76f5ed
VB
533 mutex_lock(&chain->filter_chain_lock);
534 tp = tcf_chain_dereference(chain->filter_chain, chain);
4dbfa766 535 RCU_INIT_POINTER(chain->filter_chain, NULL);
290b1c8b 536 tcf_chain0_head_change(chain, NULL);
726d0612 537 chain->flushing = true;
ed76f5ed
VB
538 mutex_unlock(&chain->filter_chain_lock);
539
290b1c8b 540 while (tp) {
4dbfa766 541 tp_next = rcu_dereference_protected(tp->next, 1);
12db03b6 542 tcf_proto_put(tp, rtnl_held, NULL);
4dbfa766 543 tp = tp_next;
290b1c8b
JP
544 }
545}
546
7f76fa36
JH
547static struct tcf_block *tc_dev_ingress_block(struct net_device *dev)
548{
549 const struct Qdisc_class_ops *cops;
550 struct Qdisc *qdisc;
551
552 if (!dev_ingress_queue(dev))
553 return NULL;
554
555 qdisc = dev_ingress_queue(dev)->qdisc_sleeping;
556 if (!qdisc)
557 return NULL;
558
559 cops = qdisc->ops->cl_ops;
560 if (!cops)
561 return NULL;
562
563 if (!cops->tcf_block)
564 return NULL;
565
566 return cops->tcf_block(qdisc, TC_H_MIN_INGRESS, NULL);
567}
568
569static struct rhashtable indr_setup_block_ht;
570
571struct tc_indr_block_dev {
572 struct rhash_head ht_node;
573 struct net_device *dev;
574 unsigned int refcnt;
575 struct list_head cb_list;
576 struct tcf_block *block;
577};
578
579struct tc_indr_block_cb {
580 struct list_head list;
581 void *cb_priv;
582 tc_indr_block_bind_cb_t *cb;
583 void *cb_ident;
584};
585
586static const struct rhashtable_params tc_indr_setup_block_ht_params = {
587 .key_offset = offsetof(struct tc_indr_block_dev, dev),
588 .head_offset = offsetof(struct tc_indr_block_dev, ht_node),
589 .key_len = sizeof(struct net_device *),
590};
591
592static struct tc_indr_block_dev *
593tc_indr_block_dev_lookup(struct net_device *dev)
594{
595 return rhashtable_lookup_fast(&indr_setup_block_ht, &dev,
596 tc_indr_setup_block_ht_params);
597}
598
599static struct tc_indr_block_dev *tc_indr_block_dev_get(struct net_device *dev)
600{
601 struct tc_indr_block_dev *indr_dev;
602
603 indr_dev = tc_indr_block_dev_lookup(dev);
604 if (indr_dev)
605 goto inc_ref;
606
607 indr_dev = kzalloc(sizeof(*indr_dev), GFP_KERNEL);
608 if (!indr_dev)
609 return NULL;
610
611 INIT_LIST_HEAD(&indr_dev->cb_list);
612 indr_dev->dev = dev;
613 indr_dev->block = tc_dev_ingress_block(dev);
614 if (rhashtable_insert_fast(&indr_setup_block_ht, &indr_dev->ht_node,
615 tc_indr_setup_block_ht_params)) {
616 kfree(indr_dev);
617 return NULL;
618 }
619
620inc_ref:
621 indr_dev->refcnt++;
622 return indr_dev;
623}
624
625static void tc_indr_block_dev_put(struct tc_indr_block_dev *indr_dev)
626{
627 if (--indr_dev->refcnt)
628 return;
629
630 rhashtable_remove_fast(&indr_setup_block_ht, &indr_dev->ht_node,
631 tc_indr_setup_block_ht_params);
632 kfree(indr_dev);
633}
634
635static struct tc_indr_block_cb *
636tc_indr_block_cb_lookup(struct tc_indr_block_dev *indr_dev,
637 tc_indr_block_bind_cb_t *cb, void *cb_ident)
638{
639 struct tc_indr_block_cb *indr_block_cb;
640
641 list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list)
642 if (indr_block_cb->cb == cb &&
643 indr_block_cb->cb_ident == cb_ident)
644 return indr_block_cb;
645 return NULL;
646}
647
648static struct tc_indr_block_cb *
649tc_indr_block_cb_add(struct tc_indr_block_dev *indr_dev, void *cb_priv,
650 tc_indr_block_bind_cb_t *cb, void *cb_ident)
651{
652 struct tc_indr_block_cb *indr_block_cb;
653
654 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
655 if (indr_block_cb)
656 return ERR_PTR(-EEXIST);
657
658 indr_block_cb = kzalloc(sizeof(*indr_block_cb), GFP_KERNEL);
659 if (!indr_block_cb)
660 return ERR_PTR(-ENOMEM);
661
662 indr_block_cb->cb_priv = cb_priv;
663 indr_block_cb->cb = cb;
664 indr_block_cb->cb_ident = cb_ident;
665 list_add(&indr_block_cb->list, &indr_dev->cb_list);
666
667 return indr_block_cb;
668}
669
670static void tc_indr_block_cb_del(struct tc_indr_block_cb *indr_block_cb)
671{
672 list_del(&indr_block_cb->list);
673 kfree(indr_block_cb);
674}
675
59094b1e
PNA
676static int tcf_block_setup(struct tcf_block *block,
677 struct flow_block_offload *bo);
678
7f76fa36
JH
679static void tc_indr_block_ing_cmd(struct tc_indr_block_dev *indr_dev,
680 struct tc_indr_block_cb *indr_block_cb,
9c0e189e 681 enum flow_block_command command)
7f76fa36 682{
955bcb6e 683 struct flow_block_offload bo = {
7f76fa36 684 .command = command,
32f8c409 685 .binder_type = FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
da3eeb90 686 .net = dev_net(indr_dev->dev),
c1a970d0 687 .block_shared = tcf_block_non_null_shared(indr_dev->block),
7f76fa36 688 };
59094b1e 689 INIT_LIST_HEAD(&bo.cb_list);
7f76fa36
JH
690
691 if (!indr_dev->block)
692 return;
693
14bfb13f
PNA
694 bo.block = &indr_dev->block->flow_block;
695
7f76fa36
JH
696 indr_block_cb->cb(indr_dev->dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK,
697 &bo);
59094b1e 698 tcf_block_setup(indr_dev->block, &bo);
7f76fa36
JH
699}
700
701int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
702 tc_indr_block_bind_cb_t *cb, void *cb_ident)
703{
704 struct tc_indr_block_cb *indr_block_cb;
705 struct tc_indr_block_dev *indr_dev;
706 int err;
707
708 indr_dev = tc_indr_block_dev_get(dev);
709 if (!indr_dev)
710 return -ENOMEM;
711
712 indr_block_cb = tc_indr_block_cb_add(indr_dev, cb_priv, cb, cb_ident);
713 err = PTR_ERR_OR_ZERO(indr_block_cb);
714 if (err)
715 goto err_dev_put;
716
9c0e189e 717 tc_indr_block_ing_cmd(indr_dev, indr_block_cb, FLOW_BLOCK_BIND);
7f76fa36
JH
718 return 0;
719
720err_dev_put:
721 tc_indr_block_dev_put(indr_dev);
722 return err;
723}
724EXPORT_SYMBOL_GPL(__tc_indr_block_cb_register);
725
726int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
727 tc_indr_block_bind_cb_t *cb, void *cb_ident)
728{
729 int err;
730
731 rtnl_lock();
732 err = __tc_indr_block_cb_register(dev, cb_priv, cb, cb_ident);
733 rtnl_unlock();
734
735 return err;
736}
737EXPORT_SYMBOL_GPL(tc_indr_block_cb_register);
738
739void __tc_indr_block_cb_unregister(struct net_device *dev,
740 tc_indr_block_bind_cb_t *cb, void *cb_ident)
741{
742 struct tc_indr_block_cb *indr_block_cb;
743 struct tc_indr_block_dev *indr_dev;
744
745 indr_dev = tc_indr_block_dev_lookup(dev);
746 if (!indr_dev)
747 return;
748
749 indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident);
750 if (!indr_block_cb)
751 return;
752
753 /* Send unbind message if required to free any block cbs. */
9c0e189e 754 tc_indr_block_ing_cmd(indr_dev, indr_block_cb, FLOW_BLOCK_UNBIND);
7f76fa36
JH
755 tc_indr_block_cb_del(indr_block_cb);
756 tc_indr_block_dev_put(indr_dev);
757}
758EXPORT_SYMBOL_GPL(__tc_indr_block_cb_unregister);
759
760void tc_indr_block_cb_unregister(struct net_device *dev,
761 tc_indr_block_bind_cb_t *cb, void *cb_ident)
762{
763 rtnl_lock();
764 __tc_indr_block_cb_unregister(dev, cb, cb_ident);
765 rtnl_unlock();
766}
767EXPORT_SYMBOL_GPL(tc_indr_block_cb_unregister);
768
769static void tc_indr_block_call(struct tcf_block *block, struct net_device *dev,
770 struct tcf_block_ext_info *ei,
9c0e189e 771 enum flow_block_command command,
7f76fa36
JH
772 struct netlink_ext_ack *extack)
773{
774 struct tc_indr_block_cb *indr_block_cb;
775 struct tc_indr_block_dev *indr_dev;
955bcb6e 776 struct flow_block_offload bo = {
7f76fa36
JH
777 .command = command,
778 .binder_type = ei->binder_type,
da3eeb90 779 .net = dev_net(dev),
14bfb13f 780 .block = &block->flow_block,
955bcb6e 781 .block_shared = tcf_block_shared(block),
7f76fa36
JH
782 .extack = extack,
783 };
59094b1e 784 INIT_LIST_HEAD(&bo.cb_list);
7f76fa36
JH
785
786 indr_dev = tc_indr_block_dev_lookup(dev);
787 if (!indr_dev)
788 return;
789
9c0e189e 790 indr_dev->block = command == FLOW_BLOCK_BIND ? block : NULL;
7f76fa36
JH
791
792 list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list)
793 indr_block_cb->cb(dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK,
794 &bo);
59094b1e
PNA
795
796 tcf_block_setup(block, &bo);
7f76fa36
JH
797}
798
caa72601
JP
799static bool tcf_block_offload_in_use(struct tcf_block *block)
800{
801 return block->offloadcnt;
802}
803
804static int tcf_block_offload_cmd(struct tcf_block *block,
805 struct net_device *dev,
806 struct tcf_block_ext_info *ei,
9c0e189e 807 enum flow_block_command command,
60513bd8 808 struct netlink_ext_ack *extack)
8c4083b3 809{
955bcb6e 810 struct flow_block_offload bo = {};
59094b1e 811 int err;
8c4083b3 812
da3eeb90 813 bo.net = dev_net(dev);
8c4083b3
JP
814 bo.command = command;
815 bo.binder_type = ei->binder_type;
14bfb13f 816 bo.block = &block->flow_block;
955bcb6e 817 bo.block_shared = tcf_block_shared(block);
60513bd8 818 bo.extack = extack;
59094b1e
PNA
819 INIT_LIST_HEAD(&bo.cb_list);
820
821 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
822 if (err < 0)
823 return err;
824
825 return tcf_block_setup(block, &bo);
8c4083b3
JP
826}
827
caa72601 828static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
60513bd8
JH
829 struct tcf_block_ext_info *ei,
830 struct netlink_ext_ack *extack)
8c4083b3 831{
caa72601
JP
832 struct net_device *dev = q->dev_queue->dev;
833 int err;
834
835 if (!dev->netdev_ops->ndo_setup_tc)
836 goto no_offload_dev_inc;
837
838 /* If tc offload feature is disabled and the block we try to bind
839 * to already has some offloaded filters, forbid to bind.
840 */
60513bd8
JH
841 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
842 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
caa72601 843 return -EOPNOTSUPP;
60513bd8 844 }
caa72601 845
9c0e189e 846 err = tcf_block_offload_cmd(block, dev, ei, FLOW_BLOCK_BIND, extack);
caa72601
JP
847 if (err == -EOPNOTSUPP)
848 goto no_offload_dev_inc;
7f76fa36
JH
849 if (err)
850 return err;
851
9c0e189e 852 tc_indr_block_call(block, dev, ei, FLOW_BLOCK_BIND, extack);
7f76fa36 853 return 0;
caa72601
JP
854
855no_offload_dev_inc:
856 if (tcf_block_offload_in_use(block))
857 return -EOPNOTSUPP;
858 block->nooffloaddevcnt++;
9c0e189e 859 tc_indr_block_call(block, dev, ei, FLOW_BLOCK_BIND, extack);
caa72601 860 return 0;
8c4083b3
JP
861}
862
863static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
864 struct tcf_block_ext_info *ei)
865{
caa72601
JP
866 struct net_device *dev = q->dev_queue->dev;
867 int err;
868
9c0e189e 869 tc_indr_block_call(block, dev, ei, FLOW_BLOCK_UNBIND, NULL);
7f76fa36 870
caa72601
JP
871 if (!dev->netdev_ops->ndo_setup_tc)
872 goto no_offload_dev_dec;
9c0e189e 873 err = tcf_block_offload_cmd(block, dev, ei, FLOW_BLOCK_UNBIND, NULL);
caa72601
JP
874 if (err == -EOPNOTSUPP)
875 goto no_offload_dev_dec;
876 return;
877
878no_offload_dev_dec:
879 WARN_ON(block->nooffloaddevcnt-- == 0);
8c4083b3
JP
880}
881
a9b19443 882static int
f71e0ca4
JP
883tcf_chain0_head_change_cb_add(struct tcf_block *block,
884 struct tcf_block_ext_info *ei,
885 struct netlink_ext_ack *extack)
a9b19443
JP
886{
887 struct tcf_filter_chain_list_item *item;
165f0135 888 struct tcf_chain *chain0;
a9b19443
JP
889
890 item = kmalloc(sizeof(*item), GFP_KERNEL);
891 if (!item) {
892 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
893 return -ENOMEM;
894 }
895 item->chain_head_change = ei->chain_head_change;
896 item->chain_head_change_priv = ei->chain_head_change_priv;
165f0135
VB
897
898 mutex_lock(&block->lock);
899 chain0 = block->chain0.chain;
ed76f5ed
VB
900 if (chain0)
901 tcf_chain_hold(chain0);
902 else
903 list_add(&item->list, &block->chain0.filter_chain_list);
165f0135
VB
904 mutex_unlock(&block->lock);
905
ed76f5ed
VB
906 if (chain0) {
907 struct tcf_proto *tp_head;
908
909 mutex_lock(&chain0->filter_chain_lock);
910
911 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
912 if (tp_head)
913 tcf_chain_head_change_item(item, tp_head);
914
915 mutex_lock(&block->lock);
916 list_add(&item->list, &block->chain0.filter_chain_list);
917 mutex_unlock(&block->lock);
918
919 mutex_unlock(&chain0->filter_chain_lock);
920 tcf_chain_put(chain0);
921 }
922
a9b19443
JP
923 return 0;
924}
925
926static void
f71e0ca4
JP
927tcf_chain0_head_change_cb_del(struct tcf_block *block,
928 struct tcf_block_ext_info *ei)
a9b19443
JP
929{
930 struct tcf_filter_chain_list_item *item;
931
165f0135 932 mutex_lock(&block->lock);
f71e0ca4 933 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
a9b19443
JP
934 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
935 (item->chain_head_change == ei->chain_head_change &&
936 item->chain_head_change_priv == ei->chain_head_change_priv)) {
165f0135 937 if (block->chain0.chain)
f71e0ca4 938 tcf_chain_head_change_item(item, NULL);
a9b19443 939 list_del(&item->list);
165f0135
VB
940 mutex_unlock(&block->lock);
941
a9b19443
JP
942 kfree(item);
943 return;
944 }
945 }
165f0135 946 mutex_unlock(&block->lock);
a9b19443
JP
947 WARN_ON(1);
948}
949
48617387 950struct tcf_net {
ab281629 951 spinlock_t idr_lock; /* Protects idr */
48617387
JP
952 struct idr idr;
953};
954
955static unsigned int tcf_net_id;
956
957static int tcf_block_insert(struct tcf_block *block, struct net *net,
bb047ddd 958 struct netlink_ext_ack *extack)
a9b19443 959{
48617387 960 struct tcf_net *tn = net_generic(net, tcf_net_id);
ab281629
VB
961 int err;
962
963 idr_preload(GFP_KERNEL);
964 spin_lock(&tn->idr_lock);
965 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
966 GFP_NOWAIT);
967 spin_unlock(&tn->idr_lock);
968 idr_preload_end();
48617387 969
ab281629 970 return err;
a9b19443
JP
971}
972
48617387
JP
973static void tcf_block_remove(struct tcf_block *block, struct net *net)
974{
975 struct tcf_net *tn = net_generic(net, tcf_net_id);
976
ab281629 977 spin_lock(&tn->idr_lock);
9c160941 978 idr_remove(&tn->idr, block->index);
ab281629 979 spin_unlock(&tn->idr_lock);
48617387
JP
980}
981
982static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
bb047ddd 983 u32 block_index,
48617387 984 struct netlink_ext_ack *extack)
6529eaba 985{
48617387 986 struct tcf_block *block;
6529eaba 987
48617387 988 block = kzalloc(sizeof(*block), GFP_KERNEL);
8d1a77f9
AA
989 if (!block) {
990 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
48617387 991 return ERR_PTR(-ENOMEM);
8d1a77f9 992 }
c266f64d 993 mutex_init(&block->lock);
14bfb13f 994 flow_block_init(&block->flow_block);
5bc17018 995 INIT_LIST_HEAD(&block->chain_list);
f36fe1c4 996 INIT_LIST_HEAD(&block->owner_list);
f71e0ca4 997 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
acb67442 998
cfebd7e2 999 refcount_set(&block->refcnt, 1);
48617387 1000 block->net = net;
bb047ddd
JP
1001 block->index = block_index;
1002
1003 /* Don't store q pointer for blocks which are shared */
1004 if (!tcf_block_shared(block))
1005 block->q = q;
48617387 1006 return block;
48617387
JP
1007}
1008
1009static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
1010{
1011 struct tcf_net *tn = net_generic(net, tcf_net_id);
1012
322d884b 1013 return idr_find(&tn->idr, block_index);
48617387
JP
1014}
1015
0607e439
VB
1016static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
1017{
1018 struct tcf_block *block;
1019
1020 rcu_read_lock();
1021 block = tcf_block_lookup(net, block_index);
1022 if (block && !refcount_inc_not_zero(&block->refcnt))
1023 block = NULL;
1024 rcu_read_unlock();
1025
1026 return block;
1027}
1028
bbf73830
VB
1029static struct tcf_chain *
1030__tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
f0023436 1031{
bbf73830
VB
1032 mutex_lock(&block->lock);
1033 if (chain)
1034 chain = list_is_last(&chain->list, &block->chain_list) ?
1035 NULL : list_next_entry(chain, list);
1036 else
1037 chain = list_first_entry_or_null(&block->chain_list,
1038 struct tcf_chain, list);
f0023436 1039
bbf73830
VB
1040 /* skip all action-only chains */
1041 while (chain && tcf_chain_held_by_acts_only(chain))
1042 chain = list_is_last(&chain->list, &block->chain_list) ?
1043 NULL : list_next_entry(chain, list);
1044
1045 if (chain)
f0023436 1046 tcf_chain_hold(chain);
bbf73830 1047 mutex_unlock(&block->lock);
f0023436 1048
bbf73830 1049 return chain;
f0023436
VB
1050}
1051
bbf73830
VB
1052/* Function to be used by all clients that want to iterate over all chains on
1053 * block. It properly obtains block->lock and takes reference to chain before
1054 * returning it. Users of this function must be tolerant to concurrent chain
1055 * insertion/deletion or ensure that no concurrent chain modification is
1056 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1057 * consistent dump because rtnl lock is released each time skb is filled with
1058 * data and sent to user-space.
1059 */
1060
1061struct tcf_chain *
1062tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
f0023436 1063{
bbf73830 1064 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
f0023436 1065
bbf73830 1066 if (chain)
f0023436 1067 tcf_chain_put(chain);
bbf73830
VB
1068
1069 return chain_next;
1070}
1071EXPORT_SYMBOL(tcf_get_next_chain);
1072
fe2923af
VB
1073static struct tcf_proto *
1074__tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
1075{
8b64678e
VB
1076 u32 prio = 0;
1077
fe2923af
VB
1078 ASSERT_RTNL();
1079 mutex_lock(&chain->filter_chain_lock);
1080
8b64678e 1081 if (!tp) {
fe2923af 1082 tp = tcf_chain_dereference(chain->filter_chain, chain);
8b64678e
VB
1083 } else if (tcf_proto_is_deleting(tp)) {
1084 /* 'deleting' flag is set and chain->filter_chain_lock was
1085 * unlocked, which means next pointer could be invalid. Restart
1086 * search.
1087 */
1088 prio = tp->prio + 1;
1089 tp = tcf_chain_dereference(chain->filter_chain, chain);
1090
1091 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
1092 if (!tp->deleting && tp->prio >= prio)
1093 break;
1094 } else {
fe2923af 1095 tp = tcf_chain_dereference(tp->next, chain);
8b64678e 1096 }
fe2923af
VB
1097
1098 if (tp)
1099 tcf_proto_get(tp);
1100
1101 mutex_unlock(&chain->filter_chain_lock);
1102
1103 return tp;
1104}
1105
1106/* Function to be used by all clients that want to iterate over all tp's on
1107 * chain. Users of this function must be tolerant to concurrent tp
1108 * insertion/deletion or ensure that no concurrent chain modification is
1109 * possible. Note that all netlink dump callbacks cannot guarantee to provide
1110 * consistent dump because rtnl lock is released each time skb is filled with
1111 * data and sent to user-space.
1112 */
1113
1114struct tcf_proto *
12db03b6
VB
1115tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp,
1116 bool rtnl_held)
fe2923af
VB
1117{
1118 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
1119
1120 if (tp)
12db03b6 1121 tcf_proto_put(tp, rtnl_held, NULL);
fe2923af
VB
1122
1123 return tp_next;
1124}
1125EXPORT_SYMBOL(tcf_get_next_proto);
1126
12db03b6 1127static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
bbf73830
VB
1128{
1129 struct tcf_chain *chain;
1130
1131 /* Last reference to block. At this point chains cannot be added or
1132 * removed concurrently.
1133 */
1134 for (chain = tcf_get_next_chain(block, NULL);
1135 chain;
1136 chain = tcf_get_next_chain(block, chain)) {
1137 tcf_chain_put_explicitly_created(chain);
12db03b6 1138 tcf_chain_flush(chain, rtnl_held);
f0023436
VB
1139 }
1140}
1141
18d3eefb
VB
1142/* Lookup Qdisc and increments its reference counter.
1143 * Set parent, if necessary.
1144 */
1145
1146static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1147 u32 *parent, int ifindex, bool rtnl_held,
1148 struct netlink_ext_ack *extack)
1149{
1150 const struct Qdisc_class_ops *cops;
1151 struct net_device *dev;
1152 int err = 0;
1153
1154 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1155 return 0;
1156
1157 rcu_read_lock();
1158
1159 /* Find link */
1160 dev = dev_get_by_index_rcu(net, ifindex);
1161 if (!dev) {
1162 rcu_read_unlock();
1163 return -ENODEV;
1164 }
1165
1166 /* Find qdisc */
1167 if (!*parent) {
1168 *q = dev->qdisc;
1169 *parent = (*q)->handle;
1170 } else {
1171 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1172 if (!*q) {
1173 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1174 err = -EINVAL;
1175 goto errout_rcu;
1176 }
1177 }
1178
1179 *q = qdisc_refcount_inc_nz(*q);
1180 if (!*q) {
1181 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1182 err = -EINVAL;
1183 goto errout_rcu;
1184 }
1185
1186 /* Is it classful? */
1187 cops = (*q)->ops->cl_ops;
1188 if (!cops) {
1189 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1190 err = -EINVAL;
1191 goto errout_qdisc;
1192 }
1193
1194 if (!cops->tcf_block) {
1195 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1196 err = -EOPNOTSUPP;
1197 goto errout_qdisc;
1198 }
1199
1200errout_rcu:
1201 /* At this point we know that qdisc is not noop_qdisc,
1202 * which means that qdisc holds a reference to net_device
1203 * and we hold a reference to qdisc, so it is safe to release
1204 * rcu read lock.
1205 */
1206 rcu_read_unlock();
1207 return err;
1208
1209errout_qdisc:
1210 rcu_read_unlock();
1211
1212 if (rtnl_held)
1213 qdisc_put(*q);
1214 else
1215 qdisc_put_unlocked(*q);
1216 *q = NULL;
1217
1218 return err;
1219}
1220
1221static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1222 int ifindex, struct netlink_ext_ack *extack)
1223{
1224 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1225 return 0;
1226
1227 /* Do we search for filter, attached to class? */
1228 if (TC_H_MIN(parent)) {
1229 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1230
1231 *cl = cops->find(q, parent);
1232 if (*cl == 0) {
1233 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1234 return -ENOENT;
1235 }
1236 }
1237
1238 return 0;
1239}
1240
1241static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1242 unsigned long cl, int ifindex,
1243 u32 block_index,
1244 struct netlink_ext_ack *extack)
1245{
1246 struct tcf_block *block;
1247
1248 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1249 block = tcf_block_refcnt_get(net, block_index);
1250 if (!block) {
1251 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1252 return ERR_PTR(-EINVAL);
1253 }
1254 } else {
1255 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1256
1257 block = cops->tcf_block(q, cl, extack);
1258 if (!block)
1259 return ERR_PTR(-EINVAL);
1260
1261 if (tcf_block_shared(block)) {
1262 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1263 return ERR_PTR(-EOPNOTSUPP);
1264 }
1265
1266 /* Always take reference to block in order to support execution
1267 * of rules update path of cls API without rtnl lock. Caller
1268 * must release block when it is finished using it. 'if' block
1269 * of this conditional obtain reference to block by calling
1270 * tcf_block_refcnt_get().
1271 */
1272 refcount_inc(&block->refcnt);
1273 }
1274
1275 return block;
1276}
1277
0607e439 1278static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
12db03b6 1279 struct tcf_block_ext_info *ei, bool rtnl_held)
0607e439 1280{
c266f64d 1281 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
0607e439
VB
1282 /* Flushing/putting all chains will cause the block to be
1283 * deallocated when last chain is freed. However, if chain_list
1284 * is empty, block has to be manually deallocated. After block
1285 * reference counter reached 0, it is no longer possible to
1286 * increment it or add new chains to block.
1287 */
1288 bool free_block = list_empty(&block->chain_list);
1289
c266f64d 1290 mutex_unlock(&block->lock);
0607e439
VB
1291 if (tcf_block_shared(block))
1292 tcf_block_remove(block, block->net);
0607e439
VB
1293
1294 if (q)
1295 tcf_block_offload_unbind(block, q, ei);
1296
1297 if (free_block)
c266f64d 1298 tcf_block_destroy(block);
0607e439 1299 else
12db03b6 1300 tcf_block_flush_all_chains(block, rtnl_held);
0607e439
VB
1301 } else if (q) {
1302 tcf_block_offload_unbind(block, q, ei);
1303 }
1304}
1305
12db03b6 1306static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
0607e439 1307{
12db03b6 1308 __tcf_block_put(block, NULL, NULL, rtnl_held);
0607e439
VB
1309}
1310
c431f89b
VB
1311/* Find tcf block.
1312 * Set q, parent, cl when appropriate.
1313 */
1314
1315static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1316 u32 *parent, unsigned long *cl,
1317 int ifindex, u32 block_index,
1318 struct netlink_ext_ack *extack)
1319{
1320 struct tcf_block *block;
e368fdb6 1321 int err = 0;
c431f89b 1322
18d3eefb 1323 ASSERT_RTNL();
e368fdb6 1324
18d3eefb
VB
1325 err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1326 if (err)
1327 goto errout;
c431f89b 1328
18d3eefb
VB
1329 err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1330 if (err)
1331 goto errout_qdisc;
787ce6d0 1332
18d3eefb 1333 block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
af736bf0
DC
1334 if (IS_ERR(block)) {
1335 err = PTR_ERR(block);
18d3eefb 1336 goto errout_qdisc;
af736bf0 1337 }
c431f89b
VB
1338
1339 return block;
e368fdb6 1340
e368fdb6 1341errout_qdisc:
18d3eefb 1342 if (*q)
e368fdb6 1343 qdisc_put(*q);
18d3eefb
VB
1344errout:
1345 *q = NULL;
e368fdb6
VB
1346 return ERR_PTR(err);
1347}
1348
12db03b6
VB
1349static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1350 bool rtnl_held)
e368fdb6 1351{
787ce6d0 1352 if (!IS_ERR_OR_NULL(block))
12db03b6 1353 tcf_block_refcnt_put(block, rtnl_held);
787ce6d0 1354
470502de
VB
1355 if (q) {
1356 if (rtnl_held)
1357 qdisc_put(q);
1358 else
1359 qdisc_put_unlocked(q);
1360 }
c431f89b
VB
1361}
1362
f36fe1c4
JP
1363struct tcf_block_owner_item {
1364 struct list_head list;
1365 struct Qdisc *q;
32f8c409 1366 enum flow_block_binder_type binder_type;
f36fe1c4
JP
1367};
1368
1369static void
1370tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1371 struct Qdisc *q,
32f8c409 1372 enum flow_block_binder_type binder_type)
f36fe1c4
JP
1373{
1374 if (block->keep_dst &&
32f8c409
PNA
1375 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1376 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
f36fe1c4
JP
1377 netif_keep_dst(qdisc_dev(q));
1378}
1379
1380void tcf_block_netif_keep_dst(struct tcf_block *block)
1381{
1382 struct tcf_block_owner_item *item;
1383
1384 block->keep_dst = true;
1385 list_for_each_entry(item, &block->owner_list, list)
1386 tcf_block_owner_netif_keep_dst(block, item->q,
1387 item->binder_type);
1388}
1389EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1390
1391static int tcf_block_owner_add(struct tcf_block *block,
1392 struct Qdisc *q,
32f8c409 1393 enum flow_block_binder_type binder_type)
f36fe1c4
JP
1394{
1395 struct tcf_block_owner_item *item;
1396
1397 item = kmalloc(sizeof(*item), GFP_KERNEL);
1398 if (!item)
1399 return -ENOMEM;
1400 item->q = q;
1401 item->binder_type = binder_type;
1402 list_add(&item->list, &block->owner_list);
1403 return 0;
1404}
1405
1406static void tcf_block_owner_del(struct tcf_block *block,
1407 struct Qdisc *q,
32f8c409 1408 enum flow_block_binder_type binder_type)
f36fe1c4
JP
1409{
1410 struct tcf_block_owner_item *item;
1411
1412 list_for_each_entry(item, &block->owner_list, list) {
1413 if (item->q == q && item->binder_type == binder_type) {
1414 list_del(&item->list);
1415 kfree(item);
1416 return;
1417 }
1418 }
1419 WARN_ON(1);
1420}
1421
48617387
JP
1422int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1423 struct tcf_block_ext_info *ei,
1424 struct netlink_ext_ack *extack)
1425{
1426 struct net *net = qdisc_net(q);
1427 struct tcf_block *block = NULL;
48617387
JP
1428 int err;
1429
787ce6d0 1430 if (ei->block_index)
48617387 1431 /* block_index not 0 means the shared block is requested */
787ce6d0 1432 block = tcf_block_refcnt_get(net, ei->block_index);
48617387
JP
1433
1434 if (!block) {
bb047ddd 1435 block = tcf_block_create(net, q, ei->block_index, extack);
48617387
JP
1436 if (IS_ERR(block))
1437 return PTR_ERR(block);
bb047ddd
JP
1438 if (tcf_block_shared(block)) {
1439 err = tcf_block_insert(block, net, extack);
48617387
JP
1440 if (err)
1441 goto err_block_insert;
1442 }
1443 }
1444
f36fe1c4
JP
1445 err = tcf_block_owner_add(block, q, ei->binder_type);
1446 if (err)
1447 goto err_block_owner_add;
1448
1449 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1450
f71e0ca4 1451 err = tcf_chain0_head_change_cb_add(block, ei, extack);
a9b19443 1452 if (err)
f71e0ca4 1453 goto err_chain0_head_change_cb_add;
caa72601 1454
60513bd8 1455 err = tcf_block_offload_bind(block, q, ei, extack);
caa72601
JP
1456 if (err)
1457 goto err_block_offload_bind;
1458
6529eaba
JP
1459 *p_block = block;
1460 return 0;
2190d1d0 1461
caa72601 1462err_block_offload_bind:
f71e0ca4
JP
1463 tcf_chain0_head_change_cb_del(block, ei);
1464err_chain0_head_change_cb_add:
f36fe1c4
JP
1465 tcf_block_owner_del(block, q, ei->binder_type);
1466err_block_owner_add:
48617387 1467err_block_insert:
12db03b6 1468 tcf_block_refcnt_put(block, true);
2190d1d0 1469 return err;
6529eaba 1470}
8c4083b3
JP
1471EXPORT_SYMBOL(tcf_block_get_ext);
1472
c7eb7d72
JP
1473static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1474{
1475 struct tcf_proto __rcu **p_filter_chain = priv;
1476
1477 rcu_assign_pointer(*p_filter_chain, tp_head);
1478}
1479
8c4083b3 1480int tcf_block_get(struct tcf_block **p_block,
8d1a77f9
AA
1481 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1482 struct netlink_ext_ack *extack)
8c4083b3 1483{
c7eb7d72
JP
1484 struct tcf_block_ext_info ei = {
1485 .chain_head_change = tcf_chain_head_change_dflt,
1486 .chain_head_change_priv = p_filter_chain,
1487 };
8c4083b3 1488
c7eb7d72 1489 WARN_ON(!p_filter_chain);
8d1a77f9 1490 return tcf_block_get_ext(p_block, q, &ei, extack);
8c4083b3 1491}
6529eaba
JP
1492EXPORT_SYMBOL(tcf_block_get);
1493
7aa0045d 1494/* XXX: Standalone actions are not allowed to jump to any chain, and bound
a60b3f51 1495 * actions should be all removed after flushing.
7aa0045d 1496 */
c7eb7d72 1497void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
e1ea2f98 1498 struct tcf_block_ext_info *ei)
7aa0045d 1499{
c30abd5e
DM
1500 if (!block)
1501 return;
f71e0ca4 1502 tcf_chain0_head_change_cb_del(block, ei);
f36fe1c4 1503 tcf_block_owner_del(block, q, ei->binder_type);
a60b3f51 1504
12db03b6 1505 __tcf_block_put(block, q, ei, true);
6529eaba 1506}
8c4083b3
JP
1507EXPORT_SYMBOL(tcf_block_put_ext);
1508
1509void tcf_block_put(struct tcf_block *block)
1510{
1511 struct tcf_block_ext_info ei = {0, };
1512
4853f128
JP
1513 if (!block)
1514 return;
c7eb7d72 1515 tcf_block_put_ext(block, block->q, &ei);
8c4083b3 1516}
e1ea2f98 1517
6529eaba 1518EXPORT_SYMBOL(tcf_block_put);
cf1facda 1519
32636742 1520static int
a7323311 1521tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
32636742
JH
1522 void *cb_priv, bool add, bool offload_in_use,
1523 struct netlink_ext_ack *extack)
1524{
bbf73830 1525 struct tcf_chain *chain, *chain_prev;
fe2923af 1526 struct tcf_proto *tp, *tp_prev;
32636742
JH
1527 int err;
1528
bbf73830
VB
1529 for (chain = __tcf_get_next_chain(block, NULL);
1530 chain;
1531 chain_prev = chain,
1532 chain = __tcf_get_next_chain(block, chain),
1533 tcf_chain_put(chain_prev)) {
fe2923af
VB
1534 for (tp = __tcf_get_next_proto(chain, NULL); tp;
1535 tp_prev = tp,
1536 tp = __tcf_get_next_proto(chain, tp),
12db03b6 1537 tcf_proto_put(tp_prev, true, NULL)) {
32636742
JH
1538 if (tp->ops->reoffload) {
1539 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1540 extack);
1541 if (err && add)
1542 goto err_playback_remove;
1543 } else if (add && offload_in_use) {
1544 err = -EOPNOTSUPP;
1545 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1546 goto err_playback_remove;
1547 }
1548 }
1549 }
1550
1551 return 0;
1552
1553err_playback_remove:
12db03b6 1554 tcf_proto_put(tp, true, NULL);
bbf73830 1555 tcf_chain_put(chain);
32636742
JH
1556 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1557 extack);
1558 return err;
1559}
1560
59094b1e
PNA
1561static int tcf_block_bind(struct tcf_block *block,
1562 struct flow_block_offload *bo)
1563{
1564 struct flow_block_cb *block_cb, *next;
1565 int err, i = 0;
1566
1567 list_for_each_entry(block_cb, &bo->cb_list, list) {
1568 err = tcf_block_playback_offloads(block, block_cb->cb,
1569 block_cb->cb_priv, true,
1570 tcf_block_offload_in_use(block),
1571 bo->extack);
1572 if (err)
1573 goto err_unroll;
1574
1575 i++;
1576 }
14bfb13f 1577 list_splice(&bo->cb_list, &block->flow_block.cb_list);
59094b1e
PNA
1578
1579 return 0;
1580
1581err_unroll:
1582 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1583 if (i-- > 0) {
1584 list_del(&block_cb->list);
1585 tcf_block_playback_offloads(block, block_cb->cb,
1586 block_cb->cb_priv, false,
1587 tcf_block_offload_in_use(block),
1588 NULL);
1589 }
1590 flow_block_cb_free(block_cb);
1591 }
1592
1593 return err;
1594}
1595
1596static void tcf_block_unbind(struct tcf_block *block,
1597 struct flow_block_offload *bo)
1598{
1599 struct flow_block_cb *block_cb, *next;
1600
1601 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1602 tcf_block_playback_offloads(block, block_cb->cb,
1603 block_cb->cb_priv, false,
1604 tcf_block_offload_in_use(block),
1605 NULL);
1606 list_del(&block_cb->list);
1607 flow_block_cb_free(block_cb);
1608 }
1609}
1610
1611static int tcf_block_setup(struct tcf_block *block,
1612 struct flow_block_offload *bo)
1613{
1614 int err;
1615
1616 switch (bo->command) {
1617 case FLOW_BLOCK_BIND:
1618 err = tcf_block_bind(block, bo);
1619 break;
1620 case FLOW_BLOCK_UNBIND:
1621 err = 0;
1622 tcf_block_unbind(block, bo);
1623 break;
1624 default:
1625 WARN_ON_ONCE(1);
1626 err = -EOPNOTSUPP;
1627 }
1628
1629 return err;
1630}
1631
87d83093
JP
1632/* Main classifier routine: scans classifier chain attached
1633 * to this qdisc, (optionally) tests for protocol and asks
1634 * specific classifiers.
1635 */
1636int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1637 struct tcf_result *res, bool compat_mode)
1638{
87d83093
JP
1639#ifdef CONFIG_NET_CLS_ACT
1640 const int max_reclassify_loop = 4;
ee538dce
JP
1641 const struct tcf_proto *orig_tp = tp;
1642 const struct tcf_proto *first_tp;
87d83093
JP
1643 int limit = 0;
1644
1645reclassify:
1646#endif
1647 for (; tp; tp = rcu_dereference_bh(tp->next)) {
cd0c4e70 1648 __be16 protocol = tc_skb_protocol(skb);
87d83093
JP
1649 int err;
1650
1651 if (tp->protocol != protocol &&
1652 tp->protocol != htons(ETH_P_ALL))
1653 continue;
1654
1655 err = tp->classify(skb, tp, res);
1656#ifdef CONFIG_NET_CLS_ACT
db50514f 1657 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
ee538dce 1658 first_tp = orig_tp;
87d83093 1659 goto reset;
db50514f 1660 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
ee538dce 1661 first_tp = res->goto_tp;
db50514f
JP
1662 goto reset;
1663 }
87d83093
JP
1664#endif
1665 if (err >= 0)
1666 return err;
1667 }
1668
1669 return TC_ACT_UNSPEC; /* signal: continue lookup */
1670#ifdef CONFIG_NET_CLS_ACT
1671reset:
1672 if (unlikely(limit++ >= max_reclassify_loop)) {
9d3aaff3
JP
1673 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1674 tp->chain->block->index,
1675 tp->prio & 0xffff,
87d83093
JP
1676 ntohs(tp->protocol));
1677 return TC_ACT_SHOT;
1678 }
1679
ee538dce 1680 tp = first_tp;
87d83093
JP
1681 goto reclassify;
1682#endif
1683}
1684EXPORT_SYMBOL(tcf_classify);
1685
2190d1d0
JP
1686struct tcf_chain_info {
1687 struct tcf_proto __rcu **pprev;
1688 struct tcf_proto __rcu *next;
1689};
1690
ed76f5ed
VB
1691static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1692 struct tcf_chain_info *chain_info)
2190d1d0 1693{
ed76f5ed 1694 return tcf_chain_dereference(*chain_info->pprev, chain);
2190d1d0
JP
1695}
1696
726d0612
VB
1697static int tcf_chain_tp_insert(struct tcf_chain *chain,
1698 struct tcf_chain_info *chain_info,
1699 struct tcf_proto *tp)
2190d1d0 1700{
726d0612
VB
1701 if (chain->flushing)
1702 return -EAGAIN;
1703
c7eb7d72 1704 if (*chain_info->pprev == chain->filter_chain)
f71e0ca4 1705 tcf_chain0_head_change(chain, tp);
4dbfa766 1706 tcf_proto_get(tp);
ed76f5ed 1707 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
2190d1d0 1708 rcu_assign_pointer(*chain_info->pprev, tp);
726d0612
VB
1709
1710 return 0;
2190d1d0
JP
1711}
1712
1713static void tcf_chain_tp_remove(struct tcf_chain *chain,
1714 struct tcf_chain_info *chain_info,
1715 struct tcf_proto *tp)
1716{
ed76f5ed 1717 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
2190d1d0 1718
8b64678e 1719 tcf_proto_mark_delete(tp);
c7eb7d72 1720 if (tp == chain->filter_chain)
f71e0ca4 1721 tcf_chain0_head_change(chain, next);
2190d1d0
JP
1722 RCU_INIT_POINTER(*chain_info->pprev, next);
1723}
1724
8b64678e
VB
1725static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1726 struct tcf_chain_info *chain_info,
1727 u32 protocol, u32 prio,
1728 bool prio_allocate);
1729
1730/* Try to insert new proto.
1731 * If proto with specified priority already exists, free new proto
1732 * and return existing one.
1733 */
1734
1735static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1736 struct tcf_proto *tp_new,
12db03b6
VB
1737 u32 protocol, u32 prio,
1738 bool rtnl_held)
8b64678e
VB
1739{
1740 struct tcf_chain_info chain_info;
1741 struct tcf_proto *tp;
726d0612 1742 int err = 0;
8b64678e
VB
1743
1744 mutex_lock(&chain->filter_chain_lock);
1745
1746 tp = tcf_chain_tp_find(chain, &chain_info,
1747 protocol, prio, false);
1748 if (!tp)
726d0612 1749 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
8b64678e
VB
1750 mutex_unlock(&chain->filter_chain_lock);
1751
1752 if (tp) {
12db03b6 1753 tcf_proto_destroy(tp_new, rtnl_held, NULL);
8b64678e 1754 tp_new = tp;
726d0612 1755 } else if (err) {
12db03b6 1756 tcf_proto_destroy(tp_new, rtnl_held, NULL);
726d0612 1757 tp_new = ERR_PTR(err);
8b64678e
VB
1758 }
1759
1760 return tp_new;
1761}
1762
1763static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
12db03b6 1764 struct tcf_proto *tp, bool rtnl_held,
8b64678e
VB
1765 struct netlink_ext_ack *extack)
1766{
1767 struct tcf_chain_info chain_info;
1768 struct tcf_proto *tp_iter;
1769 struct tcf_proto **pprev;
1770 struct tcf_proto *next;
1771
1772 mutex_lock(&chain->filter_chain_lock);
1773
1774 /* Atomically find and remove tp from chain. */
1775 for (pprev = &chain->filter_chain;
1776 (tp_iter = tcf_chain_dereference(*pprev, chain));
1777 pprev = &tp_iter->next) {
1778 if (tp_iter == tp) {
1779 chain_info.pprev = pprev;
1780 chain_info.next = tp_iter->next;
1781 WARN_ON(tp_iter->deleting);
1782 break;
1783 }
1784 }
1785 /* Verify that tp still exists and no new filters were inserted
1786 * concurrently.
1787 * Mark tp for deletion if it is empty.
1788 */
12db03b6 1789 if (!tp_iter || !tcf_proto_check_delete(tp, rtnl_held)) {
8b64678e
VB
1790 mutex_unlock(&chain->filter_chain_lock);
1791 return;
1792 }
1793
1794 next = tcf_chain_dereference(chain_info.next, chain);
1795 if (tp == chain->filter_chain)
1796 tcf_chain0_head_change(chain, next);
1797 RCU_INIT_POINTER(*chain_info.pprev, next);
1798 mutex_unlock(&chain->filter_chain_lock);
1799
12db03b6 1800 tcf_proto_put(tp, rtnl_held, extack);
8b64678e
VB
1801}
1802
2190d1d0
JP
1803static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1804 struct tcf_chain_info *chain_info,
1805 u32 protocol, u32 prio,
1806 bool prio_allocate)
1807{
1808 struct tcf_proto **pprev;
1809 struct tcf_proto *tp;
1810
1811 /* Check the chain for existence of proto-tcf with this priority */
1812 for (pprev = &chain->filter_chain;
ed76f5ed
VB
1813 (tp = tcf_chain_dereference(*pprev, chain));
1814 pprev = &tp->next) {
2190d1d0
JP
1815 if (tp->prio >= prio) {
1816 if (tp->prio == prio) {
1817 if (prio_allocate ||
1818 (tp->protocol != protocol && protocol))
1819 return ERR_PTR(-EINVAL);
1820 } else {
1821 tp = NULL;
1822 }
1823 break;
1824 }
1825 }
1826 chain_info->pprev = pprev;
4dbfa766
VB
1827 if (tp) {
1828 chain_info->next = tp->next;
1829 tcf_proto_get(tp);
1830 } else {
1831 chain_info->next = NULL;
1832 }
2190d1d0
JP
1833 return tp;
1834}
1835
7120371c 1836static int tcf_fill_node(struct net *net, struct sk_buff *skb,
7960d1da
JP
1837 struct tcf_proto *tp, struct tcf_block *block,
1838 struct Qdisc *q, u32 parent, void *fh,
12db03b6
VB
1839 u32 portid, u32 seq, u16 flags, int event,
1840 bool rtnl_held)
7120371c
WC
1841{
1842 struct tcmsg *tcm;
1843 struct nlmsghdr *nlh;
1844 unsigned char *b = skb_tail_pointer(skb);
1845
1846 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1847 if (!nlh)
1848 goto out_nlmsg_trim;
1849 tcm = nlmsg_data(nlh);
1850 tcm->tcm_family = AF_UNSPEC;
1851 tcm->tcm__pad1 = 0;
1852 tcm->tcm__pad2 = 0;
7960d1da
JP
1853 if (q) {
1854 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1855 tcm->tcm_parent = parent;
1856 } else {
1857 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1858 tcm->tcm_block_index = block->index;
1859 }
7120371c
WC
1860 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1861 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1862 goto nla_put_failure;
1863 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1864 goto nla_put_failure;
1865 if (!fh) {
1866 tcm->tcm_handle = 0;
1867 } else {
12db03b6
VB
1868 if (tp->ops->dump &&
1869 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
7120371c
WC
1870 goto nla_put_failure;
1871 }
1872 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1873 return skb->len;
1874
1875out_nlmsg_trim:
1876nla_put_failure:
1877 nlmsg_trim(skb, b);
1878 return -1;
1879}
1880
1881static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1882 struct nlmsghdr *n, struct tcf_proto *tp,
7960d1da 1883 struct tcf_block *block, struct Qdisc *q,
12db03b6
VB
1884 u32 parent, void *fh, int event, bool unicast,
1885 bool rtnl_held)
7120371c
WC
1886{
1887 struct sk_buff *skb;
1888 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
5b5f99b1 1889 int err = 0;
7120371c
WC
1890
1891 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1892 if (!skb)
1893 return -ENOBUFS;
1894
7960d1da 1895 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
12db03b6
VB
1896 n->nlmsg_seq, n->nlmsg_flags, event,
1897 rtnl_held) <= 0) {
7120371c
WC
1898 kfree_skb(skb);
1899 return -EINVAL;
1900 }
1901
1902 if (unicast)
5b5f99b1
ZW
1903 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1904 else
1905 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1906 n->nlmsg_flags & NLM_F_ECHO);
7120371c 1907
5b5f99b1
ZW
1908 if (err > 0)
1909 err = 0;
1910 return err;
7120371c
WC
1911}
1912
1913static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1914 struct nlmsghdr *n, struct tcf_proto *tp,
7960d1da 1915 struct tcf_block *block, struct Qdisc *q,
c35a4acc 1916 u32 parent, void *fh, bool unicast, bool *last,
12db03b6 1917 bool rtnl_held, struct netlink_ext_ack *extack)
7120371c
WC
1918{
1919 struct sk_buff *skb;
1920 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1921 int err;
1922
1923 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1924 if (!skb)
1925 return -ENOBUFS;
1926
7960d1da 1927 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
12db03b6
VB
1928 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
1929 rtnl_held) <= 0) {
c35a4acc 1930 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
7120371c
WC
1931 kfree_skb(skb);
1932 return -EINVAL;
1933 }
1934
12db03b6 1935 err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
7120371c
WC
1936 if (err) {
1937 kfree_skb(skb);
1938 return err;
1939 }
1940
1941 if (unicast)
5b5f99b1
ZW
1942 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1943 else
1944 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1945 n->nlmsg_flags & NLM_F_ECHO);
c35a4acc
AA
1946 if (err < 0)
1947 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
5b5f99b1
ZW
1948
1949 if (err > 0)
1950 err = 0;
c35a4acc 1951 return err;
7120371c
WC
1952}
1953
1954static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
7960d1da
JP
1955 struct tcf_block *block, struct Qdisc *q,
1956 u32 parent, struct nlmsghdr *n,
12db03b6
VB
1957 struct tcf_chain *chain, int event,
1958 bool rtnl_held)
7120371c
WC
1959{
1960 struct tcf_proto *tp;
1961
12db03b6
VB
1962 for (tp = tcf_get_next_proto(chain, NULL, rtnl_held);
1963 tp; tp = tcf_get_next_proto(chain, tp, rtnl_held))
7960d1da 1964 tfilter_notify(net, oskb, n, tp, block,
12db03b6 1965 q, parent, NULL, event, false, rtnl_held);
7120371c
WC
1966}
1967
7d5509fa
VB
1968static void tfilter_put(struct tcf_proto *tp, void *fh)
1969{
1970 if (tp->ops->put && fh)
1971 tp->ops->put(tp, fh);
1972}
1973
c431f89b 1974static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
c21ef3e3 1975 struct netlink_ext_ack *extack)
1da177e4 1976{
3b1e0a65 1977 struct net *net = sock_net(skb->sk);
add93b61 1978 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
1979 struct tcmsg *t;
1980 u32 protocol;
1981 u32 prio;
9d36d9e5 1982 bool prio_allocate;
1da177e4 1983 u32 parent;
5bc17018 1984 u32 chain_index;
7960d1da 1985 struct Qdisc *q = NULL;
2190d1d0 1986 struct tcf_chain_info chain_info;
5bc17018 1987 struct tcf_chain *chain = NULL;
6529eaba 1988 struct tcf_block *block;
1da177e4 1989 struct tcf_proto *tp;
1da177e4 1990 unsigned long cl;
8113c095 1991 void *fh;
1da177e4 1992 int err;
628185cf 1993 int tp_created;
470502de 1994 bool rtnl_held = false;
1da177e4 1995
c431f89b 1996 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
dfc47ef8 1997 return -EPERM;
de179c8c 1998
1da177e4 1999replay:
628185cf
DB
2000 tp_created = 0;
2001
8cb08174
JB
2002 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2003 rtm_tca_policy, extack);
de179c8c
H
2004 if (err < 0)
2005 return err;
2006
942b8165 2007 t = nlmsg_data(n);
1da177e4
LT
2008 protocol = TC_H_MIN(t->tcm_info);
2009 prio = TC_H_MAJ(t->tcm_info);
9d36d9e5 2010 prio_allocate = false;
1da177e4 2011 parent = t->tcm_parent;
4dbfa766 2012 tp = NULL;
1da177e4 2013 cl = 0;
470502de 2014 block = NULL;
1da177e4
LT
2015
2016 if (prio == 0) {
c431f89b
VB
2017 /* If no priority is provided by the user,
2018 * we allocate one.
2019 */
2020 if (n->nlmsg_flags & NLM_F_CREATE) {
2021 prio = TC_H_MAKE(0x80000000U, 0U);
2022 prio_allocate = true;
2023 } else {
c35a4acc 2024 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1da177e4 2025 return -ENOENT;
ea7f8277 2026 }
1da177e4
LT
2027 }
2028
2029 /* Find head of filter chain. */
2030
470502de
VB
2031 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2032 if (err)
2033 return err;
2034
2035 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2036 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2037 * type is not specified, classifier is not unlocked.
2038 */
2039 if (rtnl_held ||
2040 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2041 !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) {
2042 rtnl_held = true;
2043 rtnl_lock();
2044 }
2045
2046 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2047 if (err)
2048 goto errout;
2049
2050 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2051 extack);
c431f89b
VB
2052 if (IS_ERR(block)) {
2053 err = PTR_ERR(block);
2054 goto errout;
6bb16e7a 2055 }
5bc17018
JP
2056
2057 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2058 if (chain_index > TC_ACT_EXT_VAL_MASK) {
c35a4acc 2059 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
5bc17018
JP
2060 err = -EINVAL;
2061 goto errout;
2062 }
c431f89b 2063 chain = tcf_chain_get(block, chain_index, true);
5bc17018 2064 if (!chain) {
d5ed72a5 2065 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
c431f89b 2066 err = -ENOMEM;
ea7f8277
DB
2067 goto errout;
2068 }
1da177e4 2069
ed76f5ed 2070 mutex_lock(&chain->filter_chain_lock);
2190d1d0
JP
2071 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2072 prio, prio_allocate);
2073 if (IS_ERR(tp)) {
c35a4acc 2074 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2190d1d0 2075 err = PTR_ERR(tp);
ed76f5ed 2076 goto errout_locked;
1da177e4
LT
2077 }
2078
2079 if (tp == NULL) {
8b64678e
VB
2080 struct tcf_proto *tp_new = NULL;
2081
726d0612
VB
2082 if (chain->flushing) {
2083 err = -EAGAIN;
2084 goto errout_locked;
2085 }
2086
1da177e4
LT
2087 /* Proto-tcf does not exist, create new one */
2088
6bb16e7a 2089 if (tca[TCA_KIND] == NULL || !protocol) {
c35a4acc 2090 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
6bb16e7a 2091 err = -EINVAL;
ed76f5ed 2092 goto errout_locked;
6bb16e7a 2093 }
1da177e4 2094
c431f89b 2095 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
c35a4acc 2096 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
6bb16e7a 2097 err = -ENOENT;
ed76f5ed 2098 goto errout_locked;
6bb16e7a 2099 }
1da177e4 2100
9d36d9e5 2101 if (prio_allocate)
ed76f5ed
VB
2102 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2103 &chain_info));
1da177e4 2104
ed76f5ed 2105 mutex_unlock(&chain->filter_chain_lock);
8b64678e 2106 tp_new = tcf_proto_create(nla_data(tca[TCA_KIND]),
12db03b6
VB
2107 protocol, prio, chain, rtnl_held,
2108 extack);
8b64678e
VB
2109 if (IS_ERR(tp_new)) {
2110 err = PTR_ERR(tp_new);
726d0612 2111 goto errout_tp;
1da177e4 2112 }
ed76f5ed 2113
12186be7 2114 tp_created = 1;
12db03b6
VB
2115 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2116 rtnl_held);
726d0612
VB
2117 if (IS_ERR(tp)) {
2118 err = PTR_ERR(tp);
2119 goto errout_tp;
2120 }
ed76f5ed
VB
2121 } else {
2122 mutex_unlock(&chain->filter_chain_lock);
6bb16e7a 2123 }
1da177e4 2124
8b64678e
VB
2125 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2126 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2127 err = -EINVAL;
2128 goto errout;
2129 }
2130
1da177e4
LT
2131 fh = tp->ops->get(tp, t->tcm_handle);
2132
8113c095 2133 if (!fh) {
c431f89b 2134 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
c35a4acc 2135 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
6bb16e7a 2136 err = -ENOENT;
1da177e4 2137 goto errout;
6bb16e7a 2138 }
c431f89b 2139 } else if (n->nlmsg_flags & NLM_F_EXCL) {
7d5509fa 2140 tfilter_put(tp, fh);
c431f89b
VB
2141 NL_SET_ERR_MSG(extack, "Filter already exists");
2142 err = -EEXIST;
2143 goto errout;
1da177e4
LT
2144 }
2145
9f407f17
JP
2146 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2147 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2148 err = -EINVAL;
2149 goto errout;
2150 }
2151
2f7ef2f8 2152 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
7306db38 2153 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
12db03b6 2154 rtnl_held, extack);
7d5509fa 2155 if (err == 0) {
7960d1da 2156 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
12db03b6 2157 RTM_NEWTFILTER, false, rtnl_held);
7d5509fa 2158 tfilter_put(tp, fh);
503d81d4
VB
2159 /* q pointer is NULL for shared blocks */
2160 if (q)
2161 q->flags &= ~TCQ_F_CAN_BYPASS;
7d5509fa 2162 }
1da177e4
LT
2163
2164errout:
8b64678e 2165 if (err && tp_created)
12db03b6 2166 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
726d0612 2167errout_tp:
4dbfa766
VB
2168 if (chain) {
2169 if (tp && !IS_ERR(tp))
12db03b6 2170 tcf_proto_put(tp, rtnl_held, NULL);
4dbfa766
VB
2171 if (!tp_created)
2172 tcf_chain_put(chain);
2173 }
12db03b6 2174 tcf_block_release(q, block, rtnl_held);
470502de
VB
2175
2176 if (rtnl_held)
2177 rtnl_unlock();
2178
2179 if (err == -EAGAIN) {
2180 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2181 * of target chain.
2182 */
2183 rtnl_held = true;
1da177e4
LT
2184 /* Replay the request. */
2185 goto replay;
470502de 2186 }
1da177e4 2187 return err;
ed76f5ed
VB
2188
2189errout_locked:
2190 mutex_unlock(&chain->filter_chain_lock);
2191 goto errout;
1da177e4
LT
2192}
2193
c431f89b
VB
2194static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2195 struct netlink_ext_ack *extack)
2196{
2197 struct net *net = sock_net(skb->sk);
2198 struct nlattr *tca[TCA_MAX + 1];
2199 struct tcmsg *t;
2200 u32 protocol;
2201 u32 prio;
2202 u32 parent;
2203 u32 chain_index;
2204 struct Qdisc *q = NULL;
2205 struct tcf_chain_info chain_info;
2206 struct tcf_chain *chain = NULL;
470502de 2207 struct tcf_block *block = NULL;
c431f89b
VB
2208 struct tcf_proto *tp = NULL;
2209 unsigned long cl = 0;
2210 void *fh = NULL;
2211 int err;
470502de 2212 bool rtnl_held = false;
c431f89b
VB
2213
2214 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2215 return -EPERM;
2216
8cb08174
JB
2217 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2218 rtm_tca_policy, extack);
c431f89b
VB
2219 if (err < 0)
2220 return err;
2221
2222 t = nlmsg_data(n);
2223 protocol = TC_H_MIN(t->tcm_info);
2224 prio = TC_H_MAJ(t->tcm_info);
2225 parent = t->tcm_parent;
2226
2227 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2228 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2229 return -ENOENT;
2230 }
2231
2232 /* Find head of filter chain. */
2233
470502de
VB
2234 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2235 if (err)
2236 return err;
2237
2238 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2239 * found), qdisc is not unlocked, classifier type is not specified,
2240 * classifier is not unlocked.
2241 */
2242 if (!prio ||
2243 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2244 !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) {
2245 rtnl_held = true;
2246 rtnl_lock();
2247 }
2248
2249 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2250 if (err)
2251 goto errout;
2252
2253 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2254 extack);
c431f89b
VB
2255 if (IS_ERR(block)) {
2256 err = PTR_ERR(block);
2257 goto errout;
2258 }
2259
2260 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2261 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2262 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2263 err = -EINVAL;
2264 goto errout;
2265 }
2266 chain = tcf_chain_get(block, chain_index, false);
2267 if (!chain) {
5ca8a25c
JP
2268 /* User requested flush on non-existent chain. Nothing to do,
2269 * so just return success.
2270 */
2271 if (prio == 0) {
2272 err = 0;
2273 goto errout;
2274 }
c431f89b 2275 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
b7b4247d 2276 err = -ENOENT;
c431f89b
VB
2277 goto errout;
2278 }
2279
2280 if (prio == 0) {
2281 tfilter_notify_chain(net, skb, block, q, parent, n,
12db03b6
VB
2282 chain, RTM_DELTFILTER, rtnl_held);
2283 tcf_chain_flush(chain, rtnl_held);
c431f89b
VB
2284 err = 0;
2285 goto errout;
2286 }
2287
ed76f5ed 2288 mutex_lock(&chain->filter_chain_lock);
c431f89b
VB
2289 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2290 prio, false);
2291 if (!tp || IS_ERR(tp)) {
2292 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
0e399035 2293 err = tp ? PTR_ERR(tp) : -ENOENT;
ed76f5ed 2294 goto errout_locked;
c431f89b
VB
2295 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2296 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2297 err = -EINVAL;
ed76f5ed
VB
2298 goto errout_locked;
2299 } else if (t->tcm_handle == 0) {
2300 tcf_chain_tp_remove(chain, &chain_info, tp);
2301 mutex_unlock(&chain->filter_chain_lock);
2302
12db03b6 2303 tcf_proto_put(tp, rtnl_held, NULL);
ed76f5ed 2304 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
12db03b6 2305 RTM_DELTFILTER, false, rtnl_held);
ed76f5ed 2306 err = 0;
c431f89b
VB
2307 goto errout;
2308 }
ed76f5ed 2309 mutex_unlock(&chain->filter_chain_lock);
c431f89b
VB
2310
2311 fh = tp->ops->get(tp, t->tcm_handle);
2312
2313 if (!fh) {
ed76f5ed
VB
2314 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2315 err = -ENOENT;
c431f89b
VB
2316 } else {
2317 bool last;
2318
2319 err = tfilter_del_notify(net, skb, n, tp, block,
2320 q, parent, fh, false, &last,
12db03b6
VB
2321 rtnl_held, extack);
2322
c431f89b
VB
2323 if (err)
2324 goto errout;
8b64678e 2325 if (last)
12db03b6 2326 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
c431f89b
VB
2327 }
2328
2329errout:
4dbfa766
VB
2330 if (chain) {
2331 if (tp && !IS_ERR(tp))
12db03b6 2332 tcf_proto_put(tp, rtnl_held, NULL);
c431f89b 2333 tcf_chain_put(chain);
4dbfa766 2334 }
12db03b6 2335 tcf_block_release(q, block, rtnl_held);
470502de
VB
2336
2337 if (rtnl_held)
2338 rtnl_unlock();
2339
c431f89b 2340 return err;
ed76f5ed
VB
2341
2342errout_locked:
2343 mutex_unlock(&chain->filter_chain_lock);
2344 goto errout;
c431f89b
VB
2345}
2346
2347static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2348 struct netlink_ext_ack *extack)
2349{
2350 struct net *net = sock_net(skb->sk);
2351 struct nlattr *tca[TCA_MAX + 1];
2352 struct tcmsg *t;
2353 u32 protocol;
2354 u32 prio;
2355 u32 parent;
2356 u32 chain_index;
2357 struct Qdisc *q = NULL;
2358 struct tcf_chain_info chain_info;
2359 struct tcf_chain *chain = NULL;
470502de 2360 struct tcf_block *block = NULL;
c431f89b
VB
2361 struct tcf_proto *tp = NULL;
2362 unsigned long cl = 0;
2363 void *fh = NULL;
2364 int err;
470502de 2365 bool rtnl_held = false;
c431f89b 2366
8cb08174
JB
2367 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2368 rtm_tca_policy, extack);
c431f89b
VB
2369 if (err < 0)
2370 return err;
2371
2372 t = nlmsg_data(n);
2373 protocol = TC_H_MIN(t->tcm_info);
2374 prio = TC_H_MAJ(t->tcm_info);
2375 parent = t->tcm_parent;
2376
2377 if (prio == 0) {
2378 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2379 return -ENOENT;
2380 }
2381
2382 /* Find head of filter chain. */
2383
470502de
VB
2384 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2385 if (err)
2386 return err;
2387
2388 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2389 * unlocked, classifier type is not specified, classifier is not
2390 * unlocked.
2391 */
2392 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
2393 !tca[TCA_KIND] || !tcf_proto_is_unlocked(nla_data(tca[TCA_KIND]))) {
2394 rtnl_held = true;
2395 rtnl_lock();
2396 }
2397
2398 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2399 if (err)
2400 goto errout;
2401
2402 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2403 extack);
c431f89b
VB
2404 if (IS_ERR(block)) {
2405 err = PTR_ERR(block);
2406 goto errout;
2407 }
2408
2409 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2410 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2411 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2412 err = -EINVAL;
2413 goto errout;
2414 }
2415 chain = tcf_chain_get(block, chain_index, false);
2416 if (!chain) {
2417 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2418 err = -EINVAL;
2419 goto errout;
2420 }
2421
ed76f5ed 2422 mutex_lock(&chain->filter_chain_lock);
c431f89b
VB
2423 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2424 prio, false);
ed76f5ed 2425 mutex_unlock(&chain->filter_chain_lock);
c431f89b
VB
2426 if (!tp || IS_ERR(tp)) {
2427 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
0e399035 2428 err = tp ? PTR_ERR(tp) : -ENOENT;
c431f89b
VB
2429 goto errout;
2430 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2431 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2432 err = -EINVAL;
2433 goto errout;
2434 }
2435
2436 fh = tp->ops->get(tp, t->tcm_handle);
2437
2438 if (!fh) {
2439 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2440 err = -ENOENT;
2441 } else {
2442 err = tfilter_notify(net, skb, n, tp, block, q, parent,
12db03b6 2443 fh, RTM_NEWTFILTER, true, rtnl_held);
c431f89b
VB
2444 if (err < 0)
2445 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2446 }
2447
7d5509fa 2448 tfilter_put(tp, fh);
c431f89b 2449errout:
4dbfa766
VB
2450 if (chain) {
2451 if (tp && !IS_ERR(tp))
12db03b6 2452 tcf_proto_put(tp, rtnl_held, NULL);
c431f89b 2453 tcf_chain_put(chain);
4dbfa766 2454 }
12db03b6 2455 tcf_block_release(q, block, rtnl_held);
470502de
VB
2456
2457 if (rtnl_held)
2458 rtnl_unlock();
2459
c431f89b
VB
2460 return err;
2461}
2462
aa767bfe 2463struct tcf_dump_args {
1da177e4
LT
2464 struct tcf_walker w;
2465 struct sk_buff *skb;
2466 struct netlink_callback *cb;
7960d1da 2467 struct tcf_block *block;
a10fa201
JP
2468 struct Qdisc *q;
2469 u32 parent;
1da177e4
LT
2470};
2471
8113c095 2472static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
1da177e4 2473{
aa767bfe 2474 struct tcf_dump_args *a = (void *)arg;
832d1d5b 2475 struct net *net = sock_net(a->skb->sk);
1da177e4 2476
7960d1da 2477 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
a10fa201 2478 n, NETLINK_CB(a->cb->skb).portid,
5a7a5555 2479 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
12db03b6 2480 RTM_NEWTFILTER, true);
1da177e4
LT
2481}
2482
a10fa201
JP
2483static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2484 struct sk_buff *skb, struct netlink_callback *cb,
acb31fae
JP
2485 long index_start, long *p_index)
2486{
2487 struct net *net = sock_net(skb->sk);
7960d1da 2488 struct tcf_block *block = chain->block;
acb31fae 2489 struct tcmsg *tcm = nlmsg_data(cb->nlh);
fe2923af 2490 struct tcf_proto *tp, *tp_prev;
acb31fae 2491 struct tcf_dump_args arg;
acb31fae 2492
fe2923af
VB
2493 for (tp = __tcf_get_next_proto(chain, NULL);
2494 tp;
2495 tp_prev = tp,
2496 tp = __tcf_get_next_proto(chain, tp),
12db03b6 2497 tcf_proto_put(tp_prev, true, NULL),
fe2923af 2498 (*p_index)++) {
acb31fae
JP
2499 if (*p_index < index_start)
2500 continue;
2501 if (TC_H_MAJ(tcm->tcm_info) &&
2502 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2503 continue;
2504 if (TC_H_MIN(tcm->tcm_info) &&
2505 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2506 continue;
2507 if (*p_index > index_start)
2508 memset(&cb->args[1], 0,
2509 sizeof(cb->args) - sizeof(cb->args[0]));
2510 if (cb->args[1] == 0) {
53189183 2511 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
acb31fae
JP
2512 NETLINK_CB(cb->skb).portid,
2513 cb->nlh->nlmsg_seq, NLM_F_MULTI,
12db03b6 2514 RTM_NEWTFILTER, true) <= 0)
fe2923af 2515 goto errout;
acb31fae
JP
2516 cb->args[1] = 1;
2517 }
2518 if (!tp->ops->walk)
2519 continue;
2520 arg.w.fn = tcf_node_dump;
2521 arg.skb = skb;
2522 arg.cb = cb;
7960d1da 2523 arg.block = block;
a10fa201
JP
2524 arg.q = q;
2525 arg.parent = parent;
acb31fae
JP
2526 arg.w.stop = 0;
2527 arg.w.skip = cb->args[1] - 1;
2528 arg.w.count = 0;
01683a14 2529 arg.w.cookie = cb->args[2];
12db03b6 2530 tp->ops->walk(tp, &arg.w, true);
01683a14 2531 cb->args[2] = arg.w.cookie;
acb31fae
JP
2532 cb->args[1] = arg.w.count + 1;
2533 if (arg.w.stop)
fe2923af 2534 goto errout;
acb31fae 2535 }
5bc17018 2536 return true;
fe2923af
VB
2537
2538errout:
12db03b6 2539 tcf_proto_put(tp, true, NULL);
fe2923af 2540 return false;
acb31fae
JP
2541}
2542
bd27a875 2543/* called with RTNL */
1da177e4
LT
2544static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2545{
bbf73830 2546 struct tcf_chain *chain, *chain_prev;
3b1e0a65 2547 struct net *net = sock_net(skb->sk);
5bc17018 2548 struct nlattr *tca[TCA_MAX + 1];
7960d1da 2549 struct Qdisc *q = NULL;
6529eaba 2550 struct tcf_block *block;
942b8165 2551 struct tcmsg *tcm = nlmsg_data(cb->nlh);
acb31fae
JP
2552 long index_start;
2553 long index;
a10fa201 2554 u32 parent;
5bc17018 2555 int err;
1da177e4 2556
573ce260 2557 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1da177e4 2558 return skb->len;
5bc17018 2559
8cb08174
JB
2560 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2561 NULL, cb->extack);
5bc17018
JP
2562 if (err)
2563 return err;
2564
7960d1da 2565 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
787ce6d0 2566 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
7960d1da
JP
2567 if (!block)
2568 goto out;
d680b352
JP
2569 /* If we work with block index, q is NULL and parent value
2570 * will never be used in the following code. The check
2571 * in tcf_fill_node prevents it. However, compiler does not
2572 * see that far, so set parent to zero to silence the warning
2573 * about parent being uninitialized.
2574 */
2575 parent = 0;
a10fa201 2576 } else {
7960d1da
JP
2577 const struct Qdisc_class_ops *cops;
2578 struct net_device *dev;
2579 unsigned long cl = 0;
2580
2581 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2582 if (!dev)
2583 return skb->len;
2584
2585 parent = tcm->tcm_parent;
2586 if (!parent) {
2587 q = dev->qdisc;
2588 parent = q->handle;
2589 } else {
2590 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2591 }
2592 if (!q)
2593 goto out;
2594 cops = q->ops->cl_ops;
2595 if (!cops)
143976ce 2596 goto out;
7960d1da
JP
2597 if (!cops->tcf_block)
2598 goto out;
2599 if (TC_H_MIN(tcm->tcm_parent)) {
2600 cl = cops->find(q, tcm->tcm_parent);
2601 if (cl == 0)
2602 goto out;
2603 }
2604 block = cops->tcf_block(q, cl, NULL);
2605 if (!block)
2606 goto out;
2607 if (tcf_block_shared(block))
2608 q = NULL;
1da177e4 2609 }
1da177e4 2610
acb31fae
JP
2611 index_start = cb->args[0];
2612 index = 0;
5bc17018 2613
bbf73830
VB
2614 for (chain = __tcf_get_next_chain(block, NULL);
2615 chain;
2616 chain_prev = chain,
2617 chain = __tcf_get_next_chain(block, chain),
2618 tcf_chain_put(chain_prev)) {
5bc17018
JP
2619 if (tca[TCA_CHAIN] &&
2620 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2621 continue;
a10fa201 2622 if (!tcf_chain_dump(chain, q, parent, skb, cb,
5ae437ad 2623 index_start, &index)) {
bbf73830 2624 tcf_chain_put(chain);
5ae437ad 2625 err = -EMSGSIZE;
5bc17018 2626 break;
5ae437ad 2627 }
5bc17018
JP
2628 }
2629
787ce6d0 2630 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
12db03b6 2631 tcf_block_refcnt_put(block, true);
acb31fae 2632 cb->args[0] = index;
1da177e4 2633
1da177e4 2634out:
5ae437ad
RK
2635 /* If we did no progress, the error (EMSGSIZE) is real */
2636 if (skb->len == 0 && err)
2637 return err;
1da177e4
LT
2638 return skb->len;
2639}
2640
a5654820
VB
2641static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2642 void *tmplt_priv, u32 chain_index,
2643 struct net *net, struct sk_buff *skb,
2644 struct tcf_block *block,
32a4f5ec
JP
2645 u32 portid, u32 seq, u16 flags, int event)
2646{
2647 unsigned char *b = skb_tail_pointer(skb);
9f407f17 2648 const struct tcf_proto_ops *ops;
32a4f5ec
JP
2649 struct nlmsghdr *nlh;
2650 struct tcmsg *tcm;
9f407f17
JP
2651 void *priv;
2652
a5654820
VB
2653 ops = tmplt_ops;
2654 priv = tmplt_priv;
32a4f5ec
JP
2655
2656 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2657 if (!nlh)
2658 goto out_nlmsg_trim;
2659 tcm = nlmsg_data(nlh);
2660 tcm->tcm_family = AF_UNSPEC;
2661 tcm->tcm__pad1 = 0;
2662 tcm->tcm__pad2 = 0;
2663 tcm->tcm_handle = 0;
2664 if (block->q) {
2665 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2666 tcm->tcm_parent = block->q->handle;
2667 } else {
2668 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2669 tcm->tcm_block_index = block->index;
2670 }
2671
a5654820 2672 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
32a4f5ec
JP
2673 goto nla_put_failure;
2674
9f407f17
JP
2675 if (ops) {
2676 if (nla_put_string(skb, TCA_KIND, ops->kind))
2677 goto nla_put_failure;
2678 if (ops->tmplt_dump(skb, net, priv) < 0)
2679 goto nla_put_failure;
2680 }
2681
32a4f5ec
JP
2682 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2683 return skb->len;
2684
2685out_nlmsg_trim:
2686nla_put_failure:
2687 nlmsg_trim(skb, b);
2688 return -EMSGSIZE;
2689}
2690
2691static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2692 u32 seq, u16 flags, int event, bool unicast)
2693{
2694 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2695 struct tcf_block *block = chain->block;
2696 struct net *net = block->net;
2697 struct sk_buff *skb;
5b5f99b1 2698 int err = 0;
32a4f5ec
JP
2699
2700 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2701 if (!skb)
2702 return -ENOBUFS;
2703
a5654820
VB
2704 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2705 chain->index, net, skb, block, portid,
32a4f5ec
JP
2706 seq, flags, event) <= 0) {
2707 kfree_skb(skb);
2708 return -EINVAL;
2709 }
2710
2711 if (unicast)
5b5f99b1
ZW
2712 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2713 else
2714 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2715 flags & NLM_F_ECHO);
32a4f5ec 2716
5b5f99b1
ZW
2717 if (err > 0)
2718 err = 0;
2719 return err;
32a4f5ec
JP
2720}
2721
a5654820
VB
2722static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2723 void *tmplt_priv, u32 chain_index,
2724 struct tcf_block *block, struct sk_buff *oskb,
2725 u32 seq, u16 flags, bool unicast)
2726{
2727 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2728 struct net *net = block->net;
2729 struct sk_buff *skb;
2730
2731 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2732 if (!skb)
2733 return -ENOBUFS;
2734
2735 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2736 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2737 kfree_skb(skb);
2738 return -EINVAL;
2739 }
2740
2741 if (unicast)
2742 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2743
2744 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2745}
2746
9f407f17
JP
2747static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2748 struct nlattr **tca,
2749 struct netlink_ext_ack *extack)
2750{
2751 const struct tcf_proto_ops *ops;
2752 void *tmplt_priv;
2753
2754 /* If kind is not set, user did not specify template. */
2755 if (!tca[TCA_KIND])
2756 return 0;
2757
12db03b6 2758 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), true, extack);
9f407f17
JP
2759 if (IS_ERR(ops))
2760 return PTR_ERR(ops);
2761 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2762 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2763 return -EOPNOTSUPP;
2764 }
2765
2766 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2767 if (IS_ERR(tmplt_priv)) {
2768 module_put(ops->owner);
2769 return PTR_ERR(tmplt_priv);
2770 }
2771 chain->tmplt_ops = ops;
2772 chain->tmplt_priv = tmplt_priv;
2773 return 0;
2774}
2775
a5654820
VB
2776static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2777 void *tmplt_priv)
9f407f17 2778{
9f407f17 2779 /* If template ops are set, no work to do for us. */
a5654820 2780 if (!tmplt_ops)
9f407f17
JP
2781 return;
2782
a5654820
VB
2783 tmplt_ops->tmplt_destroy(tmplt_priv);
2784 module_put(tmplt_ops->owner);
9f407f17
JP
2785}
2786
32a4f5ec
JP
2787/* Add/delete/get a chain */
2788
2789static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2790 struct netlink_ext_ack *extack)
2791{
2792 struct net *net = sock_net(skb->sk);
2793 struct nlattr *tca[TCA_MAX + 1];
2794 struct tcmsg *t;
2795 u32 parent;
2796 u32 chain_index;
2797 struct Qdisc *q = NULL;
2798 struct tcf_chain *chain = NULL;
2799 struct tcf_block *block;
2800 unsigned long cl;
2801 int err;
2802
2803 if (n->nlmsg_type != RTM_GETCHAIN &&
2804 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2805 return -EPERM;
2806
2807replay:
8cb08174
JB
2808 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2809 rtm_tca_policy, extack);
32a4f5ec
JP
2810 if (err < 0)
2811 return err;
2812
2813 t = nlmsg_data(n);
2814 parent = t->tcm_parent;
2815 cl = 0;
2816
2817 block = tcf_block_find(net, &q, &parent, &cl,
2818 t->tcm_ifindex, t->tcm_block_index, extack);
2819 if (IS_ERR(block))
2820 return PTR_ERR(block);
2821
2822 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2823 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2824 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
e368fdb6
VB
2825 err = -EINVAL;
2826 goto errout_block;
32a4f5ec 2827 }
2cbfab07
VB
2828
2829 mutex_lock(&block->lock);
32a4f5ec
JP
2830 chain = tcf_chain_lookup(block, chain_index);
2831 if (n->nlmsg_type == RTM_NEWCHAIN) {
2832 if (chain) {
3d32f4c5 2833 if (tcf_chain_held_by_acts_only(chain)) {
1f3ed383 2834 /* The chain exists only because there is
3d32f4c5 2835 * some action referencing it.
1f3ed383
JP
2836 */
2837 tcf_chain_hold(chain);
2838 } else {
2839 NL_SET_ERR_MSG(extack, "Filter chain already exists");
e368fdb6 2840 err = -EEXIST;
2cbfab07 2841 goto errout_block_locked;
1f3ed383
JP
2842 }
2843 } else {
2844 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2845 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
e368fdb6 2846 err = -ENOENT;
2cbfab07 2847 goto errout_block_locked;
1f3ed383
JP
2848 }
2849 chain = tcf_chain_create(block, chain_index);
2850 if (!chain) {
2851 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
e368fdb6 2852 err = -ENOMEM;
2cbfab07 2853 goto errout_block_locked;
1f3ed383 2854 }
32a4f5ec
JP
2855 }
2856 } else {
3d32f4c5 2857 if (!chain || tcf_chain_held_by_acts_only(chain)) {
32a4f5ec 2858 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
e368fdb6 2859 err = -EINVAL;
2cbfab07 2860 goto errout_block_locked;
32a4f5ec
JP
2861 }
2862 tcf_chain_hold(chain);
2863 }
2864
2cbfab07
VB
2865 if (n->nlmsg_type == RTM_NEWCHAIN) {
2866 /* Modifying chain requires holding parent block lock. In case
2867 * the chain was successfully added, take a reference to the
2868 * chain. This ensures that an empty chain does not disappear at
2869 * the end of this function.
2870 */
2871 tcf_chain_hold(chain);
2872 chain->explicitly_created = true;
2873 }
2874 mutex_unlock(&block->lock);
2875
32a4f5ec
JP
2876 switch (n->nlmsg_type) {
2877 case RTM_NEWCHAIN:
9f407f17 2878 err = tc_chain_tmplt_add(chain, net, tca, extack);
2cbfab07
VB
2879 if (err) {
2880 tcf_chain_put_explicitly_created(chain);
9f407f17 2881 goto errout;
2cbfab07
VB
2882 }
2883
32a4f5ec
JP
2884 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2885 RTM_NEWCHAIN, false);
2886 break;
2887 case RTM_DELCHAIN:
f5b9bac7 2888 tfilter_notify_chain(net, skb, block, q, parent, n,
12db03b6 2889 chain, RTM_DELTFILTER, true);
32a4f5ec 2890 /* Flush the chain first as the user requested chain removal. */
12db03b6 2891 tcf_chain_flush(chain, true);
32a4f5ec
JP
2892 /* In case the chain was successfully deleted, put a reference
2893 * to the chain previously taken during addition.
2894 */
2895 tcf_chain_put_explicitly_created(chain);
2896 break;
2897 case RTM_GETCHAIN:
32a4f5ec
JP
2898 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2899 n->nlmsg_seq, n->nlmsg_type, true);
2900 if (err < 0)
2901 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2902 break;
2903 default:
2904 err = -EOPNOTSUPP;
2905 NL_SET_ERR_MSG(extack, "Unsupported message type");
2906 goto errout;
2907 }
2908
2909errout:
2910 tcf_chain_put(chain);
e368fdb6 2911errout_block:
12db03b6 2912 tcf_block_release(q, block, true);
32a4f5ec
JP
2913 if (err == -EAGAIN)
2914 /* Replay the request. */
2915 goto replay;
2916 return err;
2cbfab07
VB
2917
2918errout_block_locked:
2919 mutex_unlock(&block->lock);
2920 goto errout_block;
32a4f5ec
JP
2921}
2922
2923/* called with RTNL */
2924static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2925{
2926 struct net *net = sock_net(skb->sk);
2927 struct nlattr *tca[TCA_MAX + 1];
2928 struct Qdisc *q = NULL;
2929 struct tcf_block *block;
32a4f5ec 2930 struct tcmsg *tcm = nlmsg_data(cb->nlh);
ace4a267 2931 struct tcf_chain *chain;
32a4f5ec
JP
2932 long index_start;
2933 long index;
2934 u32 parent;
2935 int err;
2936
2937 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2938 return skb->len;
2939
8cb08174
JB
2940 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2941 rtm_tca_policy, cb->extack);
32a4f5ec
JP
2942 if (err)
2943 return err;
2944
2945 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
787ce6d0 2946 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
32a4f5ec
JP
2947 if (!block)
2948 goto out;
2949 /* If we work with block index, q is NULL and parent value
2950 * will never be used in the following code. The check
2951 * in tcf_fill_node prevents it. However, compiler does not
2952 * see that far, so set parent to zero to silence the warning
2953 * about parent being uninitialized.
2954 */
2955 parent = 0;
2956 } else {
2957 const struct Qdisc_class_ops *cops;
2958 struct net_device *dev;
2959 unsigned long cl = 0;
2960
2961 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2962 if (!dev)
2963 return skb->len;
2964
2965 parent = tcm->tcm_parent;
2966 if (!parent) {
2967 q = dev->qdisc;
2968 parent = q->handle;
2969 } else {
2970 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2971 }
2972 if (!q)
2973 goto out;
2974 cops = q->ops->cl_ops;
2975 if (!cops)
2976 goto out;
2977 if (!cops->tcf_block)
2978 goto out;
2979 if (TC_H_MIN(tcm->tcm_parent)) {
2980 cl = cops->find(q, tcm->tcm_parent);
2981 if (cl == 0)
2982 goto out;
2983 }
2984 block = cops->tcf_block(q, cl, NULL);
2985 if (!block)
2986 goto out;
2987 if (tcf_block_shared(block))
2988 q = NULL;
2989 }
2990
2991 index_start = cb->args[0];
2992 index = 0;
2993
ace4a267
VB
2994 mutex_lock(&block->lock);
2995 list_for_each_entry(chain, &block->chain_list, list) {
32a4f5ec
JP
2996 if ((tca[TCA_CHAIN] &&
2997 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2998 continue;
2999 if (index < index_start) {
3000 index++;
3001 continue;
3002 }
ace4a267
VB
3003 if (tcf_chain_held_by_acts_only(chain))
3004 continue;
a5654820
VB
3005 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3006 chain->index, net, skb, block,
32a4f5ec
JP
3007 NETLINK_CB(cb->skb).portid,
3008 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3009 RTM_NEWCHAIN);
ace4a267 3010 if (err <= 0)
32a4f5ec
JP
3011 break;
3012 index++;
3013 }
ace4a267 3014 mutex_unlock(&block->lock);
32a4f5ec 3015
787ce6d0 3016 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
12db03b6 3017 tcf_block_refcnt_put(block, true);
32a4f5ec
JP
3018 cb->args[0] = index;
3019
3020out:
3021 /* If we did no progress, the error (EMSGSIZE) is real */
3022 if (skb->len == 0 && err)
3023 return err;
3024 return skb->len;
3025}
3026
18d0264f 3027void tcf_exts_destroy(struct tcf_exts *exts)
1da177e4
LT
3028{
3029#ifdef CONFIG_NET_CLS_ACT
90b73b77 3030 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
22dc13c8
WC
3031 kfree(exts->actions);
3032 exts->nr_actions = 0;
1da177e4
LT
3033#endif
3034}
aa767bfe 3035EXPORT_SYMBOL(tcf_exts_destroy);
1da177e4 3036
c1b52739 3037int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
50a56190 3038 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
ec6743a1 3039 bool rtnl_held, struct netlink_ext_ack *extack)
1da177e4 3040{
1da177e4
LT
3041#ifdef CONFIG_NET_CLS_ACT
3042 {
1da177e4 3043 struct tc_action *act;
d04e6990 3044 size_t attr_size = 0;
1da177e4 3045
5da57f42 3046 if (exts->police && tb[exts->police]) {
9fb9f251
JP
3047 act = tcf_action_init_1(net, tp, tb[exts->police],
3048 rate_tlv, "police", ovr,
ec6743a1
VB
3049 TCA_ACT_BIND, rtnl_held,
3050 extack);
ab27cfb8
PM
3051 if (IS_ERR(act))
3052 return PTR_ERR(act);
1da177e4 3053
33be6271 3054 act->type = exts->type = TCA_OLD_COMPAT;
22dc13c8
WC
3055 exts->actions[0] = act;
3056 exts->nr_actions = 1;
5da57f42 3057 } else if (exts->action && tb[exts->action]) {
90b73b77 3058 int err;
22dc13c8 3059
9fb9f251
JP
3060 err = tcf_action_init(net, tp, tb[exts->action],
3061 rate_tlv, NULL, ovr, TCA_ACT_BIND,
ec6743a1
VB
3062 exts->actions, &attr_size,
3063 rtnl_held, extack);
90b73b77 3064 if (err < 0)
33be6271 3065 return err;
90b73b77 3066 exts->nr_actions = err;
1da177e4
LT
3067 }
3068 }
1da177e4 3069#else
5da57f42 3070 if ((exts->action && tb[exts->action]) ||
50a56190
AA
3071 (exts->police && tb[exts->police])) {
3072 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
1da177e4 3073 return -EOPNOTSUPP;
50a56190 3074 }
1da177e4
LT
3075#endif
3076
3077 return 0;
3078}
aa767bfe 3079EXPORT_SYMBOL(tcf_exts_validate);
1da177e4 3080
9b0d4446 3081void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1da177e4
LT
3082{
3083#ifdef CONFIG_NET_CLS_ACT
22dc13c8
WC
3084 struct tcf_exts old = *dst;
3085
9b0d4446 3086 *dst = *src;
22dc13c8 3087 tcf_exts_destroy(&old);
1da177e4
LT
3088#endif
3089}
aa767bfe 3090EXPORT_SYMBOL(tcf_exts_change);
1da177e4 3091
22dc13c8
WC
3092#ifdef CONFIG_NET_CLS_ACT
3093static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3094{
3095 if (exts->nr_actions == 0)
3096 return NULL;
3097 else
3098 return exts->actions[0];
3099}
3100#endif
33be6271 3101
5da57f42 3102int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
3103{
3104#ifdef CONFIG_NET_CLS_ACT
9cc63db5
CW
3105 struct nlattr *nest;
3106
978dfd8d 3107 if (exts->action && tcf_exts_has_actions(exts)) {
1da177e4
LT
3108 /*
3109 * again for backward compatible mode - we want
3110 * to work with both old and new modes of entering
3111 * tc data even if iproute2 was newer - jhs
3112 */
33be6271 3113 if (exts->type != TCA_OLD_COMPAT) {
ae0be8de 3114 nest = nla_nest_start_noflag(skb, exts->action);
4b3550ef
PM
3115 if (nest == NULL)
3116 goto nla_put_failure;
22dc13c8 3117
90b73b77 3118 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
add93b61 3119 goto nla_put_failure;
4b3550ef 3120 nla_nest_end(skb, nest);
5da57f42 3121 } else if (exts->police) {
33be6271 3122 struct tc_action *act = tcf_exts_first_act(exts);
ae0be8de 3123 nest = nla_nest_start_noflag(skb, exts->police);
63acd680 3124 if (nest == NULL || !act)
4b3550ef 3125 goto nla_put_failure;
33be6271 3126 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
add93b61 3127 goto nla_put_failure;
4b3550ef 3128 nla_nest_end(skb, nest);
1da177e4
LT
3129 }
3130 }
1da177e4 3131 return 0;
9cc63db5
CW
3132
3133nla_put_failure:
3134 nla_nest_cancel(skb, nest);
1da177e4 3135 return -1;
9cc63db5
CW
3136#else
3137 return 0;
3138#endif
1da177e4 3139}
aa767bfe 3140EXPORT_SYMBOL(tcf_exts_dump);
1da177e4 3141
aa767bfe 3142
5da57f42 3143int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
3144{
3145#ifdef CONFIG_NET_CLS_ACT
33be6271 3146 struct tc_action *a = tcf_exts_first_act(exts);
b057df24 3147 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
33be6271 3148 return -1;
1da177e4
LT
3149#endif
3150 return 0;
1da177e4 3151}
aa767bfe 3152EXPORT_SYMBOL(tcf_exts_dump_stats);
1da177e4 3153
aeb3fecd
CW
3154int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3155 void *type_data, bool err_stop)
717503b9 3156{
955bcb6e 3157 struct flow_block_cb *block_cb;
aeb3fecd
CW
3158 int ok_count = 0;
3159 int err;
3160
3161 /* Make sure all netdevs sharing this block are offload-capable. */
3162 if (block->nooffloaddevcnt && err_stop)
3163 return -EOPNOTSUPP;
3164
14bfb13f 3165 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
aeb3fecd
CW
3166 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3167 if (err) {
3168 if (err_stop)
3169 return err;
3170 } else {
3171 ok_count++;
3172 }
3173 }
3174 return ok_count;
717503b9
JP
3175}
3176EXPORT_SYMBOL(tc_setup_cb_call);
b3f55bdd 3177
3a7b6861
PNA
3178int tc_setup_flow_action(struct flow_action *flow_action,
3179 const struct tcf_exts *exts)
3180{
3181 const struct tc_action *act;
3182 int i, j, k;
3183
3184 if (!exts)
3185 return 0;
3186
3187 j = 0;
3188 tcf_exts_for_each_action(i, act, exts) {
3189 struct flow_action_entry *entry;
3190
3191 entry = &flow_action->entries[j];
3192 if (is_tcf_gact_ok(act)) {
3193 entry->id = FLOW_ACTION_ACCEPT;
3194 } else if (is_tcf_gact_shot(act)) {
3195 entry->id = FLOW_ACTION_DROP;
3196 } else if (is_tcf_gact_trap(act)) {
3197 entry->id = FLOW_ACTION_TRAP;
3198 } else if (is_tcf_gact_goto_chain(act)) {
3199 entry->id = FLOW_ACTION_GOTO;
3200 entry->chain_index = tcf_gact_goto_chain_index(act);
3201 } else if (is_tcf_mirred_egress_redirect(act)) {
3202 entry->id = FLOW_ACTION_REDIRECT;
3203 entry->dev = tcf_mirred_dev(act);
3204 } else if (is_tcf_mirred_egress_mirror(act)) {
3205 entry->id = FLOW_ACTION_MIRRED;
3206 entry->dev = tcf_mirred_dev(act);
3207 } else if (is_tcf_vlan(act)) {
3208 switch (tcf_vlan_action(act)) {
3209 case TCA_VLAN_ACT_PUSH:
3210 entry->id = FLOW_ACTION_VLAN_PUSH;
3211 entry->vlan.vid = tcf_vlan_push_vid(act);
3212 entry->vlan.proto = tcf_vlan_push_proto(act);
3213 entry->vlan.prio = tcf_vlan_push_prio(act);
3214 break;
3215 case TCA_VLAN_ACT_POP:
3216 entry->id = FLOW_ACTION_VLAN_POP;
3217 break;
3218 case TCA_VLAN_ACT_MODIFY:
3219 entry->id = FLOW_ACTION_VLAN_MANGLE;
3220 entry->vlan.vid = tcf_vlan_push_vid(act);
3221 entry->vlan.proto = tcf_vlan_push_proto(act);
3222 entry->vlan.prio = tcf_vlan_push_prio(act);
3223 break;
3224 default:
3225 goto err_out;
3226 }
3227 } else if (is_tcf_tunnel_set(act)) {
3228 entry->id = FLOW_ACTION_TUNNEL_ENCAP;
3229 entry->tunnel = tcf_tunnel_info(act);
3230 } else if (is_tcf_tunnel_release(act)) {
3231 entry->id = FLOW_ACTION_TUNNEL_DECAP;
3a7b6861
PNA
3232 } else if (is_tcf_pedit(act)) {
3233 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
3234 switch (tcf_pedit_cmd(act, k)) {
3235 case TCA_PEDIT_KEY_EX_CMD_SET:
3236 entry->id = FLOW_ACTION_MANGLE;
3237 break;
3238 case TCA_PEDIT_KEY_EX_CMD_ADD:
3239 entry->id = FLOW_ACTION_ADD;
3240 break;
3241 default:
3242 goto err_out;
3243 }
3244 entry->mangle.htype = tcf_pedit_htype(act, k);
3245 entry->mangle.mask = tcf_pedit_mask(act, k);
3246 entry->mangle.val = tcf_pedit_val(act, k);
3247 entry->mangle.offset = tcf_pedit_offset(act, k);
3248 entry = &flow_action->entries[++j];
3249 }
3250 } else if (is_tcf_csum(act)) {
3251 entry->id = FLOW_ACTION_CSUM;
3252 entry->csum_flags = tcf_csum_update_flags(act);
3253 } else if (is_tcf_skbedit_mark(act)) {
3254 entry->id = FLOW_ACTION_MARK;
3255 entry->mark = tcf_skbedit_mark(act);
a7a7be60
PJV
3256 } else if (is_tcf_sample(act)) {
3257 entry->id = FLOW_ACTION_SAMPLE;
3258 entry->sample.psample_group =
3259 tcf_sample_psample_group(act);
3260 entry->sample.trunc_size = tcf_sample_trunc_size(act);
3261 entry->sample.truncate = tcf_sample_truncate(act);
3262 entry->sample.rate = tcf_sample_rate(act);
8c8cfc6e
PJV
3263 } else if (is_tcf_police(act)) {
3264 entry->id = FLOW_ACTION_POLICE;
3265 entry->police.burst = tcf_police_tcfp_burst(act);
3266 entry->police.rate_bytes_ps =
3267 tcf_police_rate_bytes_ps(act);
b57dc7c1
PB
3268 } else if (is_tcf_ct(act)) {
3269 entry->id = FLOW_ACTION_CT;
3270 entry->ct.action = tcf_ct_action(act);
3271 entry->ct.zone = tcf_ct_zone(act);
3a7b6861
PNA
3272 } else {
3273 goto err_out;
3274 }
3275
3276 if (!is_tcf_pedit(act))
3277 j++;
3278 }
3279 return 0;
3280err_out:
3281 return -EOPNOTSUPP;
3282}
3283EXPORT_SYMBOL(tc_setup_flow_action);
3284
e3ab786b
PNA
3285unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3286{
3287 unsigned int num_acts = 0;
3288 struct tc_action *act;
3289 int i;
3290
3291 tcf_exts_for_each_action(i, act, exts) {
3292 if (is_tcf_pedit(act))
3293 num_acts += tcf_pedit_nkeys(act);
3294 else
3295 num_acts++;
3296 }
3297 return num_acts;
3298}
3299EXPORT_SYMBOL(tcf_exts_num_actions);
3300
48617387
JP
3301static __net_init int tcf_net_init(struct net *net)
3302{
3303 struct tcf_net *tn = net_generic(net, tcf_net_id);
3304
ab281629 3305 spin_lock_init(&tn->idr_lock);
48617387
JP
3306 idr_init(&tn->idr);
3307 return 0;
3308}
3309
3310static void __net_exit tcf_net_exit(struct net *net)
3311{
3312 struct tcf_net *tn = net_generic(net, tcf_net_id);
3313
3314 idr_destroy(&tn->idr);
3315}
3316
3317static struct pernet_operations tcf_net_ops = {
3318 .init = tcf_net_init,
3319 .exit = tcf_net_exit,
3320 .id = &tcf_net_id,
3321 .size = sizeof(struct tcf_net),
3322};
3323
1da177e4
LT
3324static int __init tc_filter_init(void)
3325{
48617387
JP
3326 int err;
3327
7aa0045d
CW
3328 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3329 if (!tc_filter_wq)
3330 return -ENOMEM;
3331
48617387
JP
3332 err = register_pernet_subsys(&tcf_net_ops);
3333 if (err)
3334 goto err_register_pernet_subsys;
3335
7f76fa36
JH
3336 err = rhashtable_init(&indr_setup_block_ht,
3337 &tc_indr_setup_block_ht_params);
3338 if (err)
3339 goto err_rhash_setup_block_ht;
3340
470502de
VB
3341 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3342 RTNL_FLAG_DOIT_UNLOCKED);
3343 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3344 RTNL_FLAG_DOIT_UNLOCKED);
c431f89b 3345 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
470502de 3346 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
32a4f5ec
JP
3347 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3348 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3349 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3350 tc_dump_chain, 0);
1da177e4 3351
1da177e4 3352 return 0;
48617387 3353
7f76fa36
JH
3354err_rhash_setup_block_ht:
3355 unregister_pernet_subsys(&tcf_net_ops);
48617387
JP
3356err_register_pernet_subsys:
3357 destroy_workqueue(tc_filter_wq);
3358 return err;
1da177e4
LT
3359}
3360
3361subsys_initcall(tc_filter_init);