]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/sched/cls_api.c
net: sched: change tcf block reference counter type to refcount_t
[thirdparty/kernel/stable.git] / net / sched / cls_api.c
CommitLineData
1da177e4
LT
1/*
2 * net/sched/cls_api.c Packet classifier API.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Changes:
12 *
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
14 *
15 */
16
1da177e4
LT
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
1da177e4 20#include <linux/string.h>
1da177e4 21#include <linux/errno.h>
33a48927 22#include <linux/err.h>
1da177e4 23#include <linux/skbuff.h>
1da177e4
LT
24#include <linux/init.h>
25#include <linux/kmod.h>
5a0e3ad6 26#include <linux/slab.h>
48617387 27#include <linux/idr.h>
b854272b
DL
28#include <net/net_namespace.h>
29#include <net/sock.h>
dc5fc579 30#include <net/netlink.h>
1da177e4
LT
31#include <net/pkt_sched.h>
32#include <net/pkt_cls.h>
33
1da177e4 34/* The list of all installed classifier types */
36272874 35static LIST_HEAD(tcf_proto_base);
1da177e4
LT
36
37/* Protects list of registered TC modules. It is pure SMP lock. */
38static DEFINE_RWLOCK(cls_mod_lock);
39
40/* Find classifier type by string name */
41
f34e8bff 42static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
1da177e4 43{
dcd76081 44 const struct tcf_proto_ops *t, *res = NULL;
1da177e4
LT
45
46 if (kind) {
47 read_lock(&cls_mod_lock);
36272874 48 list_for_each_entry(t, &tcf_proto_base, head) {
33a48927 49 if (strcmp(kind, t->kind) == 0) {
dcd76081
ED
50 if (try_module_get(t->owner))
51 res = t;
1da177e4
LT
52 break;
53 }
54 }
55 read_unlock(&cls_mod_lock);
56 }
dcd76081 57 return res;
1da177e4
LT
58}
59
f34e8bff
JP
60static const struct tcf_proto_ops *
61tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
62{
63 const struct tcf_proto_ops *ops;
64
65 ops = __tcf_proto_lookup_ops(kind);
66 if (ops)
67 return ops;
68#ifdef CONFIG_MODULES
69 rtnl_unlock();
70 request_module("cls_%s", kind);
71 rtnl_lock();
72 ops = __tcf_proto_lookup_ops(kind);
73 /* We dropped the RTNL semaphore in order to perform
74 * the module load. So, even if we succeeded in loading
75 * the module we have to replay the request. We indicate
76 * this using -EAGAIN.
77 */
78 if (ops) {
79 module_put(ops->owner);
80 return ERR_PTR(-EAGAIN);
81 }
82#endif
83 NL_SET_ERR_MSG(extack, "TC classifier not found");
84 return ERR_PTR(-ENOENT);
85}
86
1da177e4
LT
87/* Register(unregister) new classifier type */
88
89int register_tcf_proto_ops(struct tcf_proto_ops *ops)
90{
36272874 91 struct tcf_proto_ops *t;
1da177e4
LT
92 int rc = -EEXIST;
93
94 write_lock(&cls_mod_lock);
36272874 95 list_for_each_entry(t, &tcf_proto_base, head)
1da177e4
LT
96 if (!strcmp(ops->kind, t->kind))
97 goto out;
98
36272874 99 list_add_tail(&ops->head, &tcf_proto_base);
1da177e4
LT
100 rc = 0;
101out:
102 write_unlock(&cls_mod_lock);
103 return rc;
104}
aa767bfe 105EXPORT_SYMBOL(register_tcf_proto_ops);
1da177e4 106
7aa0045d
CW
107static struct workqueue_struct *tc_filter_wq;
108
1da177e4
LT
109int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
110{
36272874 111 struct tcf_proto_ops *t;
1da177e4
LT
112 int rc = -ENOENT;
113
c78e1746
DB
114 /* Wait for outstanding call_rcu()s, if any, from a
115 * tcf_proto_ops's destroy() handler.
116 */
117 rcu_barrier();
7aa0045d 118 flush_workqueue(tc_filter_wq);
c78e1746 119
1da177e4 120 write_lock(&cls_mod_lock);
dcd76081
ED
121 list_for_each_entry(t, &tcf_proto_base, head) {
122 if (t == ops) {
123 list_del(&t->head);
124 rc = 0;
1da177e4 125 break;
dcd76081
ED
126 }
127 }
1da177e4
LT
128 write_unlock(&cls_mod_lock);
129 return rc;
130}
aa767bfe 131EXPORT_SYMBOL(unregister_tcf_proto_ops);
1da177e4 132
aaa908ff 133bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
7aa0045d 134{
aaa908ff
CW
135 INIT_RCU_WORK(rwork, func);
136 return queue_rcu_work(tc_filter_wq, rwork);
7aa0045d
CW
137}
138EXPORT_SYMBOL(tcf_queue_work);
139
1da177e4
LT
140/* Select new prio value from the range, managed by kernel. */
141
aa767bfe 142static inline u32 tcf_auto_prio(struct tcf_proto *tp)
1da177e4 143{
aa767bfe 144 u32 first = TC_H_MAKE(0xC0000000U, 0U);
1da177e4
LT
145
146 if (tp)
cc7ec456 147 first = tp->prio - 1;
1da177e4 148
7961973a 149 return TC_H_MAJ(first);
1da177e4
LT
150}
151
33a48927 152static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
c35a4acc
AA
153 u32 prio, struct tcf_chain *chain,
154 struct netlink_ext_ack *extack)
33a48927
JP
155{
156 struct tcf_proto *tp;
157 int err;
158
159 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
160 if (!tp)
161 return ERR_PTR(-ENOBUFS);
162
f34e8bff
JP
163 tp->ops = tcf_proto_lookup_ops(kind, extack);
164 if (IS_ERR(tp->ops)) {
165 err = PTR_ERR(tp->ops);
d68d75fd 166 goto errout;
33a48927
JP
167 }
168 tp->classify = tp->ops->classify;
169 tp->protocol = protocol;
170 tp->prio = prio;
5bc17018 171 tp->chain = chain;
33a48927
JP
172
173 err = tp->ops->init(tp);
174 if (err) {
175 module_put(tp->ops->owner);
176 goto errout;
177 }
178 return tp;
179
180errout:
181 kfree(tp);
182 return ERR_PTR(err);
183}
184
715df5ec
JK
185static void tcf_proto_destroy(struct tcf_proto *tp,
186 struct netlink_ext_ack *extack)
cf1facda 187{
715df5ec 188 tp->ops->destroy(tp, extack);
763dbf63
WC
189 module_put(tp->ops->owner);
190 kfree_rcu(tp, rcu);
cf1facda
JP
191}
192
a9b19443
JP
193struct tcf_filter_chain_list_item {
194 struct list_head list;
195 tcf_chain_head_change_t *chain_head_change;
196 void *chain_head_change_priv;
197};
198
5bc17018
JP
199static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
200 u32 chain_index)
2190d1d0 201{
5bc17018
JP
202 struct tcf_chain *chain;
203
204 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
205 if (!chain)
206 return NULL;
207 list_add_tail(&chain->list, &block->chain_list);
208 chain->block = block;
209 chain->index = chain_index;
e2ef7544 210 chain->refcnt = 1;
f71e0ca4
JP
211 if (!chain->index)
212 block->chain0.chain = chain;
5bc17018 213 return chain;
2190d1d0
JP
214}
215
a9b19443
JP
216static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
217 struct tcf_proto *tp_head)
218{
219 if (item->chain_head_change)
220 item->chain_head_change(tp_head, item->chain_head_change_priv);
221}
f71e0ca4
JP
222
223static void tcf_chain0_head_change(struct tcf_chain *chain,
224 struct tcf_proto *tp_head)
c7eb7d72 225{
a9b19443 226 struct tcf_filter_chain_list_item *item;
f71e0ca4 227 struct tcf_block *block = chain->block;
a9b19443 228
f71e0ca4
JP
229 if (chain->index)
230 return;
231 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
a9b19443 232 tcf_chain_head_change_item(item, tp_head);
c7eb7d72
JP
233}
234
f93e1cdc
JP
235static void tcf_chain_destroy(struct tcf_chain *chain)
236{
efbf7897
CW
237 struct tcf_block *block = chain->block;
238
e2ef7544 239 list_del(&chain->list);
f71e0ca4
JP
240 if (!chain->index)
241 block->chain0.chain = NULL;
e2ef7544 242 kfree(chain);
cfebd7e2 243 if (list_empty(&block->chain_list) && !refcount_read(&block->refcnt))
efbf7897 244 kfree(block);
e2ef7544 245}
744a4cf6 246
e2ef7544
CW
247static void tcf_chain_hold(struct tcf_chain *chain)
248{
249 ++chain->refcnt;
2190d1d0
JP
250}
251
3d32f4c5 252static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
1f3ed383
JP
253{
254 /* In case all the references are action references, this
3d32f4c5 255 * chain should not be shown to the user.
1f3ed383
JP
256 */
257 return chain->refcnt == chain->action_refcnt;
258}
259
32a4f5ec
JP
260static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
261 u32 chain_index)
5bc17018
JP
262{
263 struct tcf_chain *chain;
264
265 list_for_each_entry(chain, &block->chain_list, list) {
32a4f5ec 266 if (chain->index == chain_index)
e2ef7544 267 return chain;
32a4f5ec
JP
268 }
269 return NULL;
270}
271
272static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
273 u32 seq, u16 flags, int event, bool unicast);
274
53681407
JP
275static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
276 u32 chain_index, bool create,
277 bool by_act)
32a4f5ec
JP
278{
279 struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
280
281 if (chain) {
282 tcf_chain_hold(chain);
53681407
JP
283 } else {
284 if (!create)
285 return NULL;
286 chain = tcf_chain_create(block, chain_index);
287 if (!chain)
288 return NULL;
5bc17018 289 }
80532384 290
53681407
JP
291 if (by_act)
292 ++chain->action_refcnt;
293
294 /* Send notification only in case we got the first
295 * non-action reference. Until then, the chain acts only as
296 * a placeholder for actions pointing to it and user ought
297 * not know about them.
298 */
299 if (chain->refcnt - chain->action_refcnt == 1 && !by_act)
300 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
301 RTM_NEWCHAIN, false);
302
32a4f5ec 303 return chain;
5bc17018 304}
53681407 305
290b1c8b
JP
306static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
307 bool create)
53681407
JP
308{
309 return __tcf_chain_get(block, chain_index, create, false);
310}
5bc17018 311
1f3ed383
JP
312struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
313{
53681407 314 return __tcf_chain_get(block, chain_index, true, true);
1f3ed383
JP
315}
316EXPORT_SYMBOL(tcf_chain_get_by_act);
317
9f407f17
JP
318static void tc_chain_tmplt_del(struct tcf_chain *chain);
319
53681407 320static void __tcf_chain_put(struct tcf_chain *chain, bool by_act)
5bc17018 321{
53681407
JP
322 if (by_act)
323 chain->action_refcnt--;
324 chain->refcnt--;
325
326 /* The last dropped non-action reference will trigger notification. */
327 if (chain->refcnt - chain->action_refcnt == 0 && !by_act)
32a4f5ec 328 tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
53681407
JP
329
330 if (chain->refcnt == 0) {
9f407f17 331 tc_chain_tmplt_del(chain);
5bc17018 332 tcf_chain_destroy(chain);
32a4f5ec 333 }
5bc17018 334}
53681407 335
290b1c8b 336static void tcf_chain_put(struct tcf_chain *chain)
53681407
JP
337{
338 __tcf_chain_put(chain, false);
339}
5bc17018 340
1f3ed383
JP
341void tcf_chain_put_by_act(struct tcf_chain *chain)
342{
53681407 343 __tcf_chain_put(chain, true);
1f3ed383
JP
344}
345EXPORT_SYMBOL(tcf_chain_put_by_act);
346
32a4f5ec
JP
347static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
348{
349 if (chain->explicitly_created)
350 tcf_chain_put(chain);
351}
352
290b1c8b
JP
353static void tcf_chain_flush(struct tcf_chain *chain)
354{
355 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
356
357 tcf_chain0_head_change(chain, NULL);
358 while (tp) {
359 RCU_INIT_POINTER(chain->filter_chain, tp->next);
360 tcf_proto_destroy(tp, NULL);
361 tp = rtnl_dereference(chain->filter_chain);
362 tcf_chain_put(chain);
363 }
364}
365
caa72601
JP
366static bool tcf_block_offload_in_use(struct tcf_block *block)
367{
368 return block->offloadcnt;
369}
370
371static int tcf_block_offload_cmd(struct tcf_block *block,
372 struct net_device *dev,
373 struct tcf_block_ext_info *ei,
60513bd8
JH
374 enum tc_block_command command,
375 struct netlink_ext_ack *extack)
8c4083b3 376{
8c4083b3
JP
377 struct tc_block_offload bo = {};
378
8c4083b3
JP
379 bo.command = command;
380 bo.binder_type = ei->binder_type;
381 bo.block = block;
60513bd8 382 bo.extack = extack;
caa72601 383 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
8c4083b3
JP
384}
385
caa72601 386static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
60513bd8
JH
387 struct tcf_block_ext_info *ei,
388 struct netlink_ext_ack *extack)
8c4083b3 389{
caa72601
JP
390 struct net_device *dev = q->dev_queue->dev;
391 int err;
392
393 if (!dev->netdev_ops->ndo_setup_tc)
394 goto no_offload_dev_inc;
395
396 /* If tc offload feature is disabled and the block we try to bind
397 * to already has some offloaded filters, forbid to bind.
398 */
60513bd8
JH
399 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
400 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
caa72601 401 return -EOPNOTSUPP;
60513bd8 402 }
caa72601 403
60513bd8 404 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
caa72601
JP
405 if (err == -EOPNOTSUPP)
406 goto no_offload_dev_inc;
407 return err;
408
409no_offload_dev_inc:
410 if (tcf_block_offload_in_use(block))
411 return -EOPNOTSUPP;
412 block->nooffloaddevcnt++;
413 return 0;
8c4083b3
JP
414}
415
416static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
417 struct tcf_block_ext_info *ei)
418{
caa72601
JP
419 struct net_device *dev = q->dev_queue->dev;
420 int err;
421
422 if (!dev->netdev_ops->ndo_setup_tc)
423 goto no_offload_dev_dec;
60513bd8 424 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
caa72601
JP
425 if (err == -EOPNOTSUPP)
426 goto no_offload_dev_dec;
427 return;
428
429no_offload_dev_dec:
430 WARN_ON(block->nooffloaddevcnt-- == 0);
8c4083b3
JP
431}
432
a9b19443 433static int
f71e0ca4
JP
434tcf_chain0_head_change_cb_add(struct tcf_block *block,
435 struct tcf_block_ext_info *ei,
436 struct netlink_ext_ack *extack)
a9b19443 437{
f71e0ca4 438 struct tcf_chain *chain0 = block->chain0.chain;
a9b19443
JP
439 struct tcf_filter_chain_list_item *item;
440
441 item = kmalloc(sizeof(*item), GFP_KERNEL);
442 if (!item) {
443 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
444 return -ENOMEM;
445 }
446 item->chain_head_change = ei->chain_head_change;
447 item->chain_head_change_priv = ei->chain_head_change_priv;
f71e0ca4
JP
448 if (chain0 && chain0->filter_chain)
449 tcf_chain_head_change_item(item, chain0->filter_chain);
450 list_add(&item->list, &block->chain0.filter_chain_list);
a9b19443
JP
451 return 0;
452}
453
454static void
f71e0ca4
JP
455tcf_chain0_head_change_cb_del(struct tcf_block *block,
456 struct tcf_block_ext_info *ei)
a9b19443 457{
f71e0ca4 458 struct tcf_chain *chain0 = block->chain0.chain;
a9b19443
JP
459 struct tcf_filter_chain_list_item *item;
460
f71e0ca4 461 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
a9b19443
JP
462 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
463 (item->chain_head_change == ei->chain_head_change &&
464 item->chain_head_change_priv == ei->chain_head_change_priv)) {
f71e0ca4
JP
465 if (chain0)
466 tcf_chain_head_change_item(item, NULL);
a9b19443
JP
467 list_del(&item->list);
468 kfree(item);
469 return;
470 }
471 }
472 WARN_ON(1);
473}
474
48617387
JP
475struct tcf_net {
476 struct idr idr;
477};
478
479static unsigned int tcf_net_id;
480
481static int tcf_block_insert(struct tcf_block *block, struct net *net,
bb047ddd 482 struct netlink_ext_ack *extack)
a9b19443 483{
48617387 484 struct tcf_net *tn = net_generic(net, tcf_net_id);
48617387 485
bb047ddd
JP
486 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
487 GFP_KERNEL);
a9b19443
JP
488}
489
48617387
JP
490static void tcf_block_remove(struct tcf_block *block, struct net *net)
491{
492 struct tcf_net *tn = net_generic(net, tcf_net_id);
493
9c160941 494 idr_remove(&tn->idr, block->index);
48617387
JP
495}
496
497static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
bb047ddd 498 u32 block_index,
48617387 499 struct netlink_ext_ack *extack)
6529eaba 500{
48617387 501 struct tcf_block *block;
6529eaba 502
48617387 503 block = kzalloc(sizeof(*block), GFP_KERNEL);
8d1a77f9
AA
504 if (!block) {
505 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
48617387 506 return ERR_PTR(-ENOMEM);
8d1a77f9 507 }
5bc17018 508 INIT_LIST_HEAD(&block->chain_list);
acb67442 509 INIT_LIST_HEAD(&block->cb_list);
f36fe1c4 510 INIT_LIST_HEAD(&block->owner_list);
f71e0ca4 511 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
acb67442 512
cfebd7e2 513 refcount_set(&block->refcnt, 1);
48617387 514 block->net = net;
bb047ddd
JP
515 block->index = block_index;
516
517 /* Don't store q pointer for blocks which are shared */
518 if (!tcf_block_shared(block))
519 block->q = q;
48617387 520 return block;
48617387
JP
521}
522
523static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
524{
525 struct tcf_net *tn = net_generic(net, tcf_net_id);
526
322d884b 527 return idr_find(&tn->idr, block_index);
48617387
JP
528}
529
c431f89b
VB
530/* Find tcf block.
531 * Set q, parent, cl when appropriate.
532 */
533
534static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
535 u32 *parent, unsigned long *cl,
536 int ifindex, u32 block_index,
537 struct netlink_ext_ack *extack)
538{
539 struct tcf_block *block;
e368fdb6 540 int err = 0;
c431f89b
VB
541
542 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
543 block = tcf_block_lookup(net, block_index);
544 if (!block) {
545 NL_SET_ERR_MSG(extack, "Block of given index was not found");
546 return ERR_PTR(-EINVAL);
547 }
548 } else {
549 const struct Qdisc_class_ops *cops;
550 struct net_device *dev;
551
e368fdb6
VB
552 rcu_read_lock();
553
c431f89b 554 /* Find link */
e368fdb6
VB
555 dev = dev_get_by_index_rcu(net, ifindex);
556 if (!dev) {
557 rcu_read_unlock();
c431f89b 558 return ERR_PTR(-ENODEV);
e368fdb6 559 }
c431f89b
VB
560
561 /* Find qdisc */
562 if (!*parent) {
563 *q = dev->qdisc;
564 *parent = (*q)->handle;
565 } else {
e368fdb6 566 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
c431f89b
VB
567 if (!*q) {
568 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
e368fdb6
VB
569 err = -EINVAL;
570 goto errout_rcu;
c431f89b
VB
571 }
572 }
573
e368fdb6
VB
574 *q = qdisc_refcount_inc_nz(*q);
575 if (!*q) {
576 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
577 err = -EINVAL;
578 goto errout_rcu;
579 }
580
c431f89b
VB
581 /* Is it classful? */
582 cops = (*q)->ops->cl_ops;
583 if (!cops) {
584 NL_SET_ERR_MSG(extack, "Qdisc not classful");
e368fdb6
VB
585 err = -EINVAL;
586 goto errout_rcu;
c431f89b
VB
587 }
588
589 if (!cops->tcf_block) {
590 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
e368fdb6
VB
591 err = -EOPNOTSUPP;
592 goto errout_rcu;
c431f89b
VB
593 }
594
e368fdb6
VB
595 /* At this point we know that qdisc is not noop_qdisc,
596 * which means that qdisc holds a reference to net_device
597 * and we hold a reference to qdisc, so it is safe to release
598 * rcu read lock.
599 */
600 rcu_read_unlock();
601
c431f89b
VB
602 /* Do we search for filter, attached to class? */
603 if (TC_H_MIN(*parent)) {
604 *cl = cops->find(*q, *parent);
605 if (*cl == 0) {
606 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
e368fdb6
VB
607 err = -ENOENT;
608 goto errout_qdisc;
c431f89b
VB
609 }
610 }
611
612 /* And the last stroke */
613 block = cops->tcf_block(*q, *cl, extack);
e368fdb6
VB
614 if (!block) {
615 err = -EINVAL;
616 goto errout_qdisc;
617 }
c431f89b
VB
618 if (tcf_block_shared(block)) {
619 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
e368fdb6
VB
620 err = -EOPNOTSUPP;
621 goto errout_qdisc;
c431f89b
VB
622 }
623 }
624
625 return block;
e368fdb6
VB
626
627errout_rcu:
628 rcu_read_unlock();
629errout_qdisc:
630 if (*q)
631 qdisc_put(*q);
632 return ERR_PTR(err);
633}
634
635static void tcf_block_release(struct Qdisc *q, struct tcf_block *block)
636{
637 if (q)
638 qdisc_put(q);
c431f89b
VB
639}
640
f36fe1c4
JP
641struct tcf_block_owner_item {
642 struct list_head list;
643 struct Qdisc *q;
644 enum tcf_block_binder_type binder_type;
645};
646
647static void
648tcf_block_owner_netif_keep_dst(struct tcf_block *block,
649 struct Qdisc *q,
650 enum tcf_block_binder_type binder_type)
651{
652 if (block->keep_dst &&
653 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
654 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
655 netif_keep_dst(qdisc_dev(q));
656}
657
658void tcf_block_netif_keep_dst(struct tcf_block *block)
659{
660 struct tcf_block_owner_item *item;
661
662 block->keep_dst = true;
663 list_for_each_entry(item, &block->owner_list, list)
664 tcf_block_owner_netif_keep_dst(block, item->q,
665 item->binder_type);
666}
667EXPORT_SYMBOL(tcf_block_netif_keep_dst);
668
669static int tcf_block_owner_add(struct tcf_block *block,
670 struct Qdisc *q,
671 enum tcf_block_binder_type binder_type)
672{
673 struct tcf_block_owner_item *item;
674
675 item = kmalloc(sizeof(*item), GFP_KERNEL);
676 if (!item)
677 return -ENOMEM;
678 item->q = q;
679 item->binder_type = binder_type;
680 list_add(&item->list, &block->owner_list);
681 return 0;
682}
683
684static void tcf_block_owner_del(struct tcf_block *block,
685 struct Qdisc *q,
686 enum tcf_block_binder_type binder_type)
687{
688 struct tcf_block_owner_item *item;
689
690 list_for_each_entry(item, &block->owner_list, list) {
691 if (item->q == q && item->binder_type == binder_type) {
692 list_del(&item->list);
693 kfree(item);
694 return;
695 }
696 }
697 WARN_ON(1);
698}
699
48617387
JP
700int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
701 struct tcf_block_ext_info *ei,
702 struct netlink_ext_ack *extack)
703{
704 struct net *net = qdisc_net(q);
705 struct tcf_block *block = NULL;
706 bool created = false;
707 int err;
708
709 if (ei->block_index) {
710 /* block_index not 0 means the shared block is requested */
711 block = tcf_block_lookup(net, ei->block_index);
712 if (block)
cfebd7e2 713 refcount_inc(&block->refcnt);
48617387
JP
714 }
715
716 if (!block) {
bb047ddd 717 block = tcf_block_create(net, q, ei->block_index, extack);
48617387
JP
718 if (IS_ERR(block))
719 return PTR_ERR(block);
720 created = true;
bb047ddd
JP
721 if (tcf_block_shared(block)) {
722 err = tcf_block_insert(block, net, extack);
48617387
JP
723 if (err)
724 goto err_block_insert;
725 }
726 }
727
f36fe1c4
JP
728 err = tcf_block_owner_add(block, q, ei->binder_type);
729 if (err)
730 goto err_block_owner_add;
731
732 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
733
f71e0ca4 734 err = tcf_chain0_head_change_cb_add(block, ei, extack);
a9b19443 735 if (err)
f71e0ca4 736 goto err_chain0_head_change_cb_add;
caa72601 737
60513bd8 738 err = tcf_block_offload_bind(block, q, ei, extack);
caa72601
JP
739 if (err)
740 goto err_block_offload_bind;
741
6529eaba
JP
742 *p_block = block;
743 return 0;
2190d1d0 744
caa72601 745err_block_offload_bind:
f71e0ca4
JP
746 tcf_chain0_head_change_cb_del(block, ei);
747err_chain0_head_change_cb_add:
f36fe1c4
JP
748 tcf_block_owner_del(block, q, ei->binder_type);
749err_block_owner_add:
48617387
JP
750 if (created) {
751 if (tcf_block_shared(block))
752 tcf_block_remove(block, net);
753err_block_insert:
48617387
JP
754 kfree(block);
755 } else {
cfebd7e2 756 refcount_dec(&block->refcnt);
48617387 757 }
2190d1d0 758 return err;
6529eaba 759}
8c4083b3
JP
760EXPORT_SYMBOL(tcf_block_get_ext);
761
c7eb7d72
JP
762static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
763{
764 struct tcf_proto __rcu **p_filter_chain = priv;
765
766 rcu_assign_pointer(*p_filter_chain, tp_head);
767}
768
8c4083b3 769int tcf_block_get(struct tcf_block **p_block,
8d1a77f9
AA
770 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
771 struct netlink_ext_ack *extack)
8c4083b3 772{
c7eb7d72
JP
773 struct tcf_block_ext_info ei = {
774 .chain_head_change = tcf_chain_head_change_dflt,
775 .chain_head_change_priv = p_filter_chain,
776 };
8c4083b3 777
c7eb7d72 778 WARN_ON(!p_filter_chain);
8d1a77f9 779 return tcf_block_get_ext(p_block, q, &ei, extack);
8c4083b3 780}
6529eaba
JP
781EXPORT_SYMBOL(tcf_block_get);
782
7aa0045d 783/* XXX: Standalone actions are not allowed to jump to any chain, and bound
a60b3f51 784 * actions should be all removed after flushing.
7aa0045d 785 */
c7eb7d72 786void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
e1ea2f98 787 struct tcf_block_ext_info *ei)
7aa0045d 788{
efbf7897 789 struct tcf_chain *chain, *tmp;
1697c4bb 790
c30abd5e
DM
791 if (!block)
792 return;
f71e0ca4 793 tcf_chain0_head_change_cb_del(block, ei);
f36fe1c4 794 tcf_block_owner_del(block, q, ei->binder_type);
a60b3f51 795
cfebd7e2
VB
796 if (refcount_dec_and_test(&block->refcnt)) {
797 /* Flushing/putting all chains will cause the block to be
798 * deallocated when last chain is freed. However, if chain_list
799 * is empty, block has to be manually deallocated. After block
800 * reference counter reached 0, it is no longer possible to
801 * increment it or add new chains to block.
48617387 802 */
cfebd7e2 803 bool free_block = list_empty(&block->chain_list);
48617387 804
cfebd7e2
VB
805 if (tcf_block_shared(block))
806 tcf_block_remove(block, block->net);
e2ef7544 807
cfebd7e2
VB
808 if (!free_block) {
809 /* Hold a refcnt for all chains, so that they don't
810 * disappear while we are iterating.
811 */
812 list_for_each_entry(chain, &block->chain_list, list)
813 tcf_chain_hold(chain);
4bb1b116 814
cfebd7e2
VB
815 list_for_each_entry(chain, &block->chain_list, list)
816 tcf_chain_flush(chain);
32a4f5ec 817 }
df45bf84 818
cfebd7e2
VB
819 tcf_block_offload_unbind(block, q, ei);
820
821 if (free_block) {
f71e0ca4 822 kfree(block);
cfebd7e2
VB
823 } else {
824 /* At this point, all the chains should have
825 * refcnt >= 1.
826 */
827 list_for_each_entry_safe(chain, tmp, &block->chain_list,
828 list) {
829 tcf_chain_put_explicitly_created(chain);
830 tcf_chain_put(chain);
831 }
832 }
63cc5bcc 833 } else {
cfebd7e2 834 tcf_block_offload_unbind(block, q, ei);
48617387 835 }
6529eaba 836}
8c4083b3
JP
837EXPORT_SYMBOL(tcf_block_put_ext);
838
839void tcf_block_put(struct tcf_block *block)
840{
841 struct tcf_block_ext_info ei = {0, };
842
4853f128
JP
843 if (!block)
844 return;
c7eb7d72 845 tcf_block_put_ext(block, block->q, &ei);
8c4083b3 846}
e1ea2f98 847
6529eaba 848EXPORT_SYMBOL(tcf_block_put);
cf1facda 849
acb67442
JP
850struct tcf_block_cb {
851 struct list_head list;
852 tc_setup_cb_t *cb;
853 void *cb_ident;
854 void *cb_priv;
855 unsigned int refcnt;
856};
857
858void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
859{
860 return block_cb->cb_priv;
861}
862EXPORT_SYMBOL(tcf_block_cb_priv);
863
864struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
865 tc_setup_cb_t *cb, void *cb_ident)
866{ struct tcf_block_cb *block_cb;
867
868 list_for_each_entry(block_cb, &block->cb_list, list)
869 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
870 return block_cb;
871 return NULL;
872}
873EXPORT_SYMBOL(tcf_block_cb_lookup);
874
875void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
876{
877 block_cb->refcnt++;
878}
879EXPORT_SYMBOL(tcf_block_cb_incref);
880
881unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
882{
883 return --block_cb->refcnt;
884}
885EXPORT_SYMBOL(tcf_block_cb_decref);
886
32636742
JH
887static int
888tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
889 void *cb_priv, bool add, bool offload_in_use,
890 struct netlink_ext_ack *extack)
891{
892 struct tcf_chain *chain;
893 struct tcf_proto *tp;
894 int err;
895
896 list_for_each_entry(chain, &block->chain_list, list) {
897 for (tp = rtnl_dereference(chain->filter_chain); tp;
898 tp = rtnl_dereference(tp->next)) {
899 if (tp->ops->reoffload) {
900 err = tp->ops->reoffload(tp, add, cb, cb_priv,
901 extack);
902 if (err && add)
903 goto err_playback_remove;
904 } else if (add && offload_in_use) {
905 err = -EOPNOTSUPP;
906 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
907 goto err_playback_remove;
908 }
909 }
910 }
911
912 return 0;
913
914err_playback_remove:
915 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
916 extack);
917 return err;
918}
919
acb67442
JP
920struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
921 tc_setup_cb_t *cb, void *cb_ident,
60513bd8
JH
922 void *cb_priv,
923 struct netlink_ext_ack *extack)
acb67442
JP
924{
925 struct tcf_block_cb *block_cb;
32636742 926 int err;
acb67442 927
32636742
JH
928 /* Replay any already present rules */
929 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
930 tcf_block_offload_in_use(block),
931 extack);
932 if (err)
933 return ERR_PTR(err);
caa72601 934
acb67442
JP
935 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
936 if (!block_cb)
caa72601 937 return ERR_PTR(-ENOMEM);
acb67442
JP
938 block_cb->cb = cb;
939 block_cb->cb_ident = cb_ident;
940 block_cb->cb_priv = cb_priv;
941 list_add(&block_cb->list, &block->cb_list);
942 return block_cb;
943}
944EXPORT_SYMBOL(__tcf_block_cb_register);
945
946int tcf_block_cb_register(struct tcf_block *block,
947 tc_setup_cb_t *cb, void *cb_ident,
60513bd8 948 void *cb_priv, struct netlink_ext_ack *extack)
acb67442
JP
949{
950 struct tcf_block_cb *block_cb;
951
60513bd8
JH
952 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
953 extack);
baa2d2b1 954 return PTR_ERR_OR_ZERO(block_cb);
acb67442
JP
955}
956EXPORT_SYMBOL(tcf_block_cb_register);
957
32636742
JH
958void __tcf_block_cb_unregister(struct tcf_block *block,
959 struct tcf_block_cb *block_cb)
acb67442 960{
32636742
JH
961 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
962 false, tcf_block_offload_in_use(block),
963 NULL);
acb67442
JP
964 list_del(&block_cb->list);
965 kfree(block_cb);
966}
967EXPORT_SYMBOL(__tcf_block_cb_unregister);
968
969void tcf_block_cb_unregister(struct tcf_block *block,
970 tc_setup_cb_t *cb, void *cb_ident)
971{
972 struct tcf_block_cb *block_cb;
973
974 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
975 if (!block_cb)
976 return;
32636742 977 __tcf_block_cb_unregister(block, block_cb);
acb67442
JP
978}
979EXPORT_SYMBOL(tcf_block_cb_unregister);
980
981static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
982 void *type_data, bool err_stop)
983{
984 struct tcf_block_cb *block_cb;
985 int ok_count = 0;
986 int err;
987
9a99dc1c
DM
988 /* Make sure all netdevs sharing this block are offload-capable. */
989 if (block->nooffloaddevcnt && err_stop)
990 return -EOPNOTSUPP;
991
acb67442
JP
992 list_for_each_entry(block_cb, &block->cb_list, list) {
993 err = block_cb->cb(type, type_data, block_cb->cb_priv);
994 if (err) {
995 if (err_stop)
996 return err;
997 } else {
998 ok_count++;
999 }
1000 }
1001 return ok_count;
1002}
1003
87d83093
JP
1004/* Main classifier routine: scans classifier chain attached
1005 * to this qdisc, (optionally) tests for protocol and asks
1006 * specific classifiers.
1007 */
1008int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1009 struct tcf_result *res, bool compat_mode)
1010{
1011 __be16 protocol = tc_skb_protocol(skb);
1012#ifdef CONFIG_NET_CLS_ACT
1013 const int max_reclassify_loop = 4;
ee538dce
JP
1014 const struct tcf_proto *orig_tp = tp;
1015 const struct tcf_proto *first_tp;
87d83093
JP
1016 int limit = 0;
1017
1018reclassify:
1019#endif
1020 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1021 int err;
1022
1023 if (tp->protocol != protocol &&
1024 tp->protocol != htons(ETH_P_ALL))
1025 continue;
1026
1027 err = tp->classify(skb, tp, res);
1028#ifdef CONFIG_NET_CLS_ACT
db50514f 1029 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
ee538dce 1030 first_tp = orig_tp;
87d83093 1031 goto reset;
db50514f 1032 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
ee538dce 1033 first_tp = res->goto_tp;
db50514f
JP
1034 goto reset;
1035 }
87d83093
JP
1036#endif
1037 if (err >= 0)
1038 return err;
1039 }
1040
1041 return TC_ACT_UNSPEC; /* signal: continue lookup */
1042#ifdef CONFIG_NET_CLS_ACT
1043reset:
1044 if (unlikely(limit++ >= max_reclassify_loop)) {
9d3aaff3
JP
1045 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1046 tp->chain->block->index,
1047 tp->prio & 0xffff,
87d83093
JP
1048 ntohs(tp->protocol));
1049 return TC_ACT_SHOT;
1050 }
1051
ee538dce 1052 tp = first_tp;
87d83093
JP
1053 protocol = tc_skb_protocol(skb);
1054 goto reclassify;
1055#endif
1056}
1057EXPORT_SYMBOL(tcf_classify);
1058
2190d1d0
JP
1059struct tcf_chain_info {
1060 struct tcf_proto __rcu **pprev;
1061 struct tcf_proto __rcu *next;
1062};
1063
1064static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1065{
1066 return rtnl_dereference(*chain_info->pprev);
1067}
1068
1069static void tcf_chain_tp_insert(struct tcf_chain *chain,
1070 struct tcf_chain_info *chain_info,
1071 struct tcf_proto *tp)
1072{
c7eb7d72 1073 if (*chain_info->pprev == chain->filter_chain)
f71e0ca4 1074 tcf_chain0_head_change(chain, tp);
2190d1d0
JP
1075 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1076 rcu_assign_pointer(*chain_info->pprev, tp);
e2ef7544 1077 tcf_chain_hold(chain);
2190d1d0
JP
1078}
1079
1080static void tcf_chain_tp_remove(struct tcf_chain *chain,
1081 struct tcf_chain_info *chain_info,
1082 struct tcf_proto *tp)
1083{
1084 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1085
c7eb7d72 1086 if (tp == chain->filter_chain)
f71e0ca4 1087 tcf_chain0_head_change(chain, next);
2190d1d0 1088 RCU_INIT_POINTER(*chain_info->pprev, next);
e2ef7544 1089 tcf_chain_put(chain);
2190d1d0
JP
1090}
1091
1092static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1093 struct tcf_chain_info *chain_info,
1094 u32 protocol, u32 prio,
1095 bool prio_allocate)
1096{
1097 struct tcf_proto **pprev;
1098 struct tcf_proto *tp;
1099
1100 /* Check the chain for existence of proto-tcf with this priority */
1101 for (pprev = &chain->filter_chain;
1102 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1103 if (tp->prio >= prio) {
1104 if (tp->prio == prio) {
1105 if (prio_allocate ||
1106 (tp->protocol != protocol && protocol))
1107 return ERR_PTR(-EINVAL);
1108 } else {
1109 tp = NULL;
1110 }
1111 break;
1112 }
1113 }
1114 chain_info->pprev = pprev;
1115 chain_info->next = tp ? tp->next : NULL;
1116 return tp;
1117}
1118
7120371c 1119static int tcf_fill_node(struct net *net, struct sk_buff *skb,
7960d1da
JP
1120 struct tcf_proto *tp, struct tcf_block *block,
1121 struct Qdisc *q, u32 parent, void *fh,
1122 u32 portid, u32 seq, u16 flags, int event)
7120371c
WC
1123{
1124 struct tcmsg *tcm;
1125 struct nlmsghdr *nlh;
1126 unsigned char *b = skb_tail_pointer(skb);
1127
1128 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1129 if (!nlh)
1130 goto out_nlmsg_trim;
1131 tcm = nlmsg_data(nlh);
1132 tcm->tcm_family = AF_UNSPEC;
1133 tcm->tcm__pad1 = 0;
1134 tcm->tcm__pad2 = 0;
7960d1da
JP
1135 if (q) {
1136 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1137 tcm->tcm_parent = parent;
1138 } else {
1139 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1140 tcm->tcm_block_index = block->index;
1141 }
7120371c
WC
1142 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1143 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1144 goto nla_put_failure;
1145 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1146 goto nla_put_failure;
1147 if (!fh) {
1148 tcm->tcm_handle = 0;
1149 } else {
1150 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1151 goto nla_put_failure;
1152 }
1153 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1154 return skb->len;
1155
1156out_nlmsg_trim:
1157nla_put_failure:
1158 nlmsg_trim(skb, b);
1159 return -1;
1160}
1161
1162static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1163 struct nlmsghdr *n, struct tcf_proto *tp,
7960d1da
JP
1164 struct tcf_block *block, struct Qdisc *q,
1165 u32 parent, void *fh, int event, bool unicast)
7120371c
WC
1166{
1167 struct sk_buff *skb;
1168 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1169
1170 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1171 if (!skb)
1172 return -ENOBUFS;
1173
7960d1da
JP
1174 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1175 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
7120371c
WC
1176 kfree_skb(skb);
1177 return -EINVAL;
1178 }
1179
1180 if (unicast)
1181 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1182
1183 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1184 n->nlmsg_flags & NLM_F_ECHO);
1185}
1186
1187static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1188 struct nlmsghdr *n, struct tcf_proto *tp,
7960d1da 1189 struct tcf_block *block, struct Qdisc *q,
c35a4acc
AA
1190 u32 parent, void *fh, bool unicast, bool *last,
1191 struct netlink_ext_ack *extack)
7120371c
WC
1192{
1193 struct sk_buff *skb;
1194 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1195 int err;
1196
1197 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1198 if (!skb)
1199 return -ENOBUFS;
1200
7960d1da
JP
1201 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1202 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
c35a4acc 1203 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
7120371c
WC
1204 kfree_skb(skb);
1205 return -EINVAL;
1206 }
1207
571acf21 1208 err = tp->ops->delete(tp, fh, last, extack);
7120371c
WC
1209 if (err) {
1210 kfree_skb(skb);
1211 return err;
1212 }
1213
1214 if (unicast)
1215 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1216
c35a4acc
AA
1217 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1218 n->nlmsg_flags & NLM_F_ECHO);
1219 if (err < 0)
1220 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1221 return err;
7120371c
WC
1222}
1223
1224static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
7960d1da
JP
1225 struct tcf_block *block, struct Qdisc *q,
1226 u32 parent, struct nlmsghdr *n,
7120371c
WC
1227 struct tcf_chain *chain, int event)
1228{
1229 struct tcf_proto *tp;
1230
1231 for (tp = rtnl_dereference(chain->filter_chain);
1232 tp; tp = rtnl_dereference(tp->next))
7960d1da 1233 tfilter_notify(net, oskb, n, tp, block,
53189183 1234 q, parent, NULL, event, false);
7120371c
WC
1235}
1236
c431f89b 1237static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
c21ef3e3 1238 struct netlink_ext_ack *extack)
1da177e4 1239{
3b1e0a65 1240 struct net *net = sock_net(skb->sk);
add93b61 1241 struct nlattr *tca[TCA_MAX + 1];
1da177e4
LT
1242 struct tcmsg *t;
1243 u32 protocol;
1244 u32 prio;
9d36d9e5 1245 bool prio_allocate;
1da177e4 1246 u32 parent;
5bc17018 1247 u32 chain_index;
7960d1da 1248 struct Qdisc *q = NULL;
2190d1d0 1249 struct tcf_chain_info chain_info;
5bc17018 1250 struct tcf_chain *chain = NULL;
6529eaba 1251 struct tcf_block *block;
1da177e4 1252 struct tcf_proto *tp;
1da177e4 1253 unsigned long cl;
8113c095 1254 void *fh;
1da177e4 1255 int err;
628185cf 1256 int tp_created;
1da177e4 1257
c431f89b 1258 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
dfc47ef8 1259 return -EPERM;
de179c8c 1260
1da177e4 1261replay:
628185cf
DB
1262 tp_created = 0;
1263
c21ef3e3 1264 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
de179c8c
H
1265 if (err < 0)
1266 return err;
1267
942b8165 1268 t = nlmsg_data(n);
1da177e4
LT
1269 protocol = TC_H_MIN(t->tcm_info);
1270 prio = TC_H_MAJ(t->tcm_info);
9d36d9e5 1271 prio_allocate = false;
1da177e4
LT
1272 parent = t->tcm_parent;
1273 cl = 0;
1274
1275 if (prio == 0) {
c431f89b
VB
1276 /* If no priority is provided by the user,
1277 * we allocate one.
1278 */
1279 if (n->nlmsg_flags & NLM_F_CREATE) {
1280 prio = TC_H_MAKE(0x80000000U, 0U);
1281 prio_allocate = true;
1282 } else {
c35a4acc 1283 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1da177e4 1284 return -ENOENT;
ea7f8277 1285 }
1da177e4
LT
1286 }
1287
1288 /* Find head of filter chain. */
1289
c431f89b
VB
1290 block = tcf_block_find(net, &q, &parent, &cl,
1291 t->tcm_ifindex, t->tcm_block_index, extack);
1292 if (IS_ERR(block)) {
1293 err = PTR_ERR(block);
1294 goto errout;
6bb16e7a 1295 }
5bc17018
JP
1296
1297 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1298 if (chain_index > TC_ACT_EXT_VAL_MASK) {
c35a4acc 1299 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
5bc17018
JP
1300 err = -EINVAL;
1301 goto errout;
1302 }
c431f89b 1303 chain = tcf_chain_get(block, chain_index, true);
5bc17018 1304 if (!chain) {
d5ed72a5 1305 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
c431f89b 1306 err = -ENOMEM;
ea7f8277
DB
1307 goto errout;
1308 }
1da177e4 1309
2190d1d0
JP
1310 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1311 prio, prio_allocate);
1312 if (IS_ERR(tp)) {
c35a4acc 1313 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
2190d1d0
JP
1314 err = PTR_ERR(tp);
1315 goto errout;
1da177e4
LT
1316 }
1317
1318 if (tp == NULL) {
1319 /* Proto-tcf does not exist, create new one */
1320
6bb16e7a 1321 if (tca[TCA_KIND] == NULL || !protocol) {
c35a4acc 1322 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
6bb16e7a 1323 err = -EINVAL;
1da177e4 1324 goto errout;
6bb16e7a 1325 }
1da177e4 1326
c431f89b 1327 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
c35a4acc 1328 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
6bb16e7a 1329 err = -ENOENT;
1da177e4 1330 goto errout;
6bb16e7a 1331 }
1da177e4 1332
9d36d9e5 1333 if (prio_allocate)
2190d1d0 1334 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
1da177e4 1335
33a48927 1336 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
c35a4acc 1337 protocol, prio, chain, extack);
33a48927
JP
1338 if (IS_ERR(tp)) {
1339 err = PTR_ERR(tp);
1da177e4
LT
1340 goto errout;
1341 }
12186be7 1342 tp_created = 1;
6bb16e7a 1343 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
c35a4acc 1344 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
6bb16e7a 1345 err = -EINVAL;
1da177e4 1346 goto errout;
6bb16e7a 1347 }
1da177e4
LT
1348
1349 fh = tp->ops->get(tp, t->tcm_handle);
1350
8113c095 1351 if (!fh) {
c431f89b 1352 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
c35a4acc 1353 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
6bb16e7a 1354 err = -ENOENT;
1da177e4 1355 goto errout;
6bb16e7a 1356 }
c431f89b
VB
1357 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1358 NL_SET_ERR_MSG(extack, "Filter already exists");
1359 err = -EEXIST;
1360 goto errout;
1da177e4
LT
1361 }
1362
9f407f17
JP
1363 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1364 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1365 err = -EINVAL;
1366 goto errout;
1367 }
1368
2f7ef2f8 1369 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
7306db38
AA
1370 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1371 extack);
12186be7 1372 if (err == 0) {
2190d1d0
JP
1373 if (tp_created)
1374 tcf_chain_tp_insert(chain, &chain_info, tp);
7960d1da 1375 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
a10fa201 1376 RTM_NEWTFILTER, false);
12186be7
MU
1377 } else {
1378 if (tp_created)
715df5ec 1379 tcf_proto_destroy(tp, NULL);
12186be7 1380 }
1da177e4
LT
1381
1382errout:
5bc17018
JP
1383 if (chain)
1384 tcf_chain_put(chain);
e368fdb6 1385 tcf_block_release(q, block);
1da177e4
LT
1386 if (err == -EAGAIN)
1387 /* Replay the request. */
1388 goto replay;
1389 return err;
1390}
1391
c431f89b
VB
1392static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1393 struct netlink_ext_ack *extack)
1394{
1395 struct net *net = sock_net(skb->sk);
1396 struct nlattr *tca[TCA_MAX + 1];
1397 struct tcmsg *t;
1398 u32 protocol;
1399 u32 prio;
1400 u32 parent;
1401 u32 chain_index;
1402 struct Qdisc *q = NULL;
1403 struct tcf_chain_info chain_info;
1404 struct tcf_chain *chain = NULL;
1405 struct tcf_block *block;
1406 struct tcf_proto *tp = NULL;
1407 unsigned long cl = 0;
1408 void *fh = NULL;
1409 int err;
1410
1411 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1412 return -EPERM;
1413
1414 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1415 if (err < 0)
1416 return err;
1417
1418 t = nlmsg_data(n);
1419 protocol = TC_H_MIN(t->tcm_info);
1420 prio = TC_H_MAJ(t->tcm_info);
1421 parent = t->tcm_parent;
1422
1423 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1424 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1425 return -ENOENT;
1426 }
1427
1428 /* Find head of filter chain. */
1429
1430 block = tcf_block_find(net, &q, &parent, &cl,
1431 t->tcm_ifindex, t->tcm_block_index, extack);
1432 if (IS_ERR(block)) {
1433 err = PTR_ERR(block);
1434 goto errout;
1435 }
1436
1437 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1438 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1439 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1440 err = -EINVAL;
1441 goto errout;
1442 }
1443 chain = tcf_chain_get(block, chain_index, false);
1444 if (!chain) {
5ca8a25c
JP
1445 /* User requested flush on non-existent chain. Nothing to do,
1446 * so just return success.
1447 */
1448 if (prio == 0) {
1449 err = 0;
1450 goto errout;
1451 }
c431f89b 1452 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
b7b4247d 1453 err = -ENOENT;
c431f89b
VB
1454 goto errout;
1455 }
1456
1457 if (prio == 0) {
1458 tfilter_notify_chain(net, skb, block, q, parent, n,
1459 chain, RTM_DELTFILTER);
1460 tcf_chain_flush(chain);
1461 err = 0;
1462 goto errout;
1463 }
1464
1465 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1466 prio, false);
1467 if (!tp || IS_ERR(tp)) {
1468 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
0e399035 1469 err = tp ? PTR_ERR(tp) : -ENOENT;
c431f89b
VB
1470 goto errout;
1471 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1472 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1473 err = -EINVAL;
1474 goto errout;
1475 }
1476
1477 fh = tp->ops->get(tp, t->tcm_handle);
1478
1479 if (!fh) {
1480 if (t->tcm_handle == 0) {
1481 tcf_chain_tp_remove(chain, &chain_info, tp);
1482 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1483 RTM_DELTFILTER, false);
1484 tcf_proto_destroy(tp, extack);
1485 err = 0;
1486 } else {
1487 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1488 err = -ENOENT;
1489 }
1490 } else {
1491 bool last;
1492
1493 err = tfilter_del_notify(net, skb, n, tp, block,
1494 q, parent, fh, false, &last,
1495 extack);
1496 if (err)
1497 goto errout;
1498 if (last) {
1499 tcf_chain_tp_remove(chain, &chain_info, tp);
1500 tcf_proto_destroy(tp, extack);
1501 }
1502 }
1503
1504errout:
1505 if (chain)
1506 tcf_chain_put(chain);
e368fdb6 1507 tcf_block_release(q, block);
c431f89b
VB
1508 return err;
1509}
1510
1511static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1512 struct netlink_ext_ack *extack)
1513{
1514 struct net *net = sock_net(skb->sk);
1515 struct nlattr *tca[TCA_MAX + 1];
1516 struct tcmsg *t;
1517 u32 protocol;
1518 u32 prio;
1519 u32 parent;
1520 u32 chain_index;
1521 struct Qdisc *q = NULL;
1522 struct tcf_chain_info chain_info;
1523 struct tcf_chain *chain = NULL;
1524 struct tcf_block *block;
1525 struct tcf_proto *tp = NULL;
1526 unsigned long cl = 0;
1527 void *fh = NULL;
1528 int err;
1529
1530 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1531 if (err < 0)
1532 return err;
1533
1534 t = nlmsg_data(n);
1535 protocol = TC_H_MIN(t->tcm_info);
1536 prio = TC_H_MAJ(t->tcm_info);
1537 parent = t->tcm_parent;
1538
1539 if (prio == 0) {
1540 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1541 return -ENOENT;
1542 }
1543
1544 /* Find head of filter chain. */
1545
1546 block = tcf_block_find(net, &q, &parent, &cl,
1547 t->tcm_ifindex, t->tcm_block_index, extack);
1548 if (IS_ERR(block)) {
1549 err = PTR_ERR(block);
1550 goto errout;
1551 }
1552
1553 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1554 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1555 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1556 err = -EINVAL;
1557 goto errout;
1558 }
1559 chain = tcf_chain_get(block, chain_index, false);
1560 if (!chain) {
1561 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1562 err = -EINVAL;
1563 goto errout;
1564 }
1565
1566 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1567 prio, false);
1568 if (!tp || IS_ERR(tp)) {
1569 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
0e399035 1570 err = tp ? PTR_ERR(tp) : -ENOENT;
c431f89b
VB
1571 goto errout;
1572 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1573 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1574 err = -EINVAL;
1575 goto errout;
1576 }
1577
1578 fh = tp->ops->get(tp, t->tcm_handle);
1579
1580 if (!fh) {
1581 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1582 err = -ENOENT;
1583 } else {
1584 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1585 fh, RTM_NEWTFILTER, true);
1586 if (err < 0)
1587 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1588 }
1589
1590errout:
1591 if (chain)
1592 tcf_chain_put(chain);
e368fdb6 1593 tcf_block_release(q, block);
c431f89b
VB
1594 return err;
1595}
1596
aa767bfe 1597struct tcf_dump_args {
1da177e4
LT
1598 struct tcf_walker w;
1599 struct sk_buff *skb;
1600 struct netlink_callback *cb;
7960d1da 1601 struct tcf_block *block;
a10fa201
JP
1602 struct Qdisc *q;
1603 u32 parent;
1da177e4
LT
1604};
1605
8113c095 1606static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
1da177e4 1607{
aa767bfe 1608 struct tcf_dump_args *a = (void *)arg;
832d1d5b 1609 struct net *net = sock_net(a->skb->sk);
1da177e4 1610
7960d1da 1611 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
a10fa201 1612 n, NETLINK_CB(a->cb->skb).portid,
5a7a5555
JHS
1613 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1614 RTM_NEWTFILTER);
1da177e4
LT
1615}
1616
a10fa201
JP
1617static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1618 struct sk_buff *skb, struct netlink_callback *cb,
acb31fae
JP
1619 long index_start, long *p_index)
1620{
1621 struct net *net = sock_net(skb->sk);
7960d1da 1622 struct tcf_block *block = chain->block;
acb31fae
JP
1623 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1624 struct tcf_dump_args arg;
1625 struct tcf_proto *tp;
1626
1627 for (tp = rtnl_dereference(chain->filter_chain);
1628 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1629 if (*p_index < index_start)
1630 continue;
1631 if (TC_H_MAJ(tcm->tcm_info) &&
1632 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1633 continue;
1634 if (TC_H_MIN(tcm->tcm_info) &&
1635 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1636 continue;
1637 if (*p_index > index_start)
1638 memset(&cb->args[1], 0,
1639 sizeof(cb->args) - sizeof(cb->args[0]));
1640 if (cb->args[1] == 0) {
53189183 1641 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
acb31fae
JP
1642 NETLINK_CB(cb->skb).portid,
1643 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1644 RTM_NEWTFILTER) <= 0)
5bc17018 1645 return false;
acb31fae
JP
1646
1647 cb->args[1] = 1;
1648 }
1649 if (!tp->ops->walk)
1650 continue;
1651 arg.w.fn = tcf_node_dump;
1652 arg.skb = skb;
1653 arg.cb = cb;
7960d1da 1654 arg.block = block;
a10fa201
JP
1655 arg.q = q;
1656 arg.parent = parent;
acb31fae
JP
1657 arg.w.stop = 0;
1658 arg.w.skip = cb->args[1] - 1;
1659 arg.w.count = 0;
01683a14 1660 arg.w.cookie = cb->args[2];
acb31fae 1661 tp->ops->walk(tp, &arg.w);
01683a14 1662 cb->args[2] = arg.w.cookie;
acb31fae
JP
1663 cb->args[1] = arg.w.count + 1;
1664 if (arg.w.stop)
5bc17018 1665 return false;
acb31fae 1666 }
5bc17018 1667 return true;
acb31fae
JP
1668}
1669
bd27a875 1670/* called with RTNL */
1da177e4
LT
1671static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1672{
3b1e0a65 1673 struct net *net = sock_net(skb->sk);
5bc17018 1674 struct nlattr *tca[TCA_MAX + 1];
7960d1da 1675 struct Qdisc *q = NULL;
6529eaba 1676 struct tcf_block *block;
2190d1d0 1677 struct tcf_chain *chain;
942b8165 1678 struct tcmsg *tcm = nlmsg_data(cb->nlh);
acb31fae
JP
1679 long index_start;
1680 long index;
a10fa201 1681 u32 parent;
5bc17018 1682 int err;
1da177e4 1683
573ce260 1684 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1da177e4 1685 return skb->len;
5bc17018
JP
1686
1687 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1688 if (err)
1689 return err;
1690
7960d1da
JP
1691 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1692 block = tcf_block_lookup(net, tcm->tcm_block_index);
1693 if (!block)
1694 goto out;
d680b352
JP
1695 /* If we work with block index, q is NULL and parent value
1696 * will never be used in the following code. The check
1697 * in tcf_fill_node prevents it. However, compiler does not
1698 * see that far, so set parent to zero to silence the warning
1699 * about parent being uninitialized.
1700 */
1701 parent = 0;
a10fa201 1702 } else {
7960d1da
JP
1703 const struct Qdisc_class_ops *cops;
1704 struct net_device *dev;
1705 unsigned long cl = 0;
1706
1707 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1708 if (!dev)
1709 return skb->len;
1710
1711 parent = tcm->tcm_parent;
1712 if (!parent) {
1713 q = dev->qdisc;
1714 parent = q->handle;
1715 } else {
1716 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1717 }
1718 if (!q)
1719 goto out;
1720 cops = q->ops->cl_ops;
1721 if (!cops)
143976ce 1722 goto out;
7960d1da
JP
1723 if (!cops->tcf_block)
1724 goto out;
1725 if (TC_H_MIN(tcm->tcm_parent)) {
1726 cl = cops->find(q, tcm->tcm_parent);
1727 if (cl == 0)
1728 goto out;
1729 }
1730 block = cops->tcf_block(q, cl, NULL);
1731 if (!block)
1732 goto out;
1733 if (tcf_block_shared(block))
1734 q = NULL;
1da177e4 1735 }
1da177e4 1736
acb31fae
JP
1737 index_start = cb->args[0];
1738 index = 0;
5bc17018
JP
1739
1740 list_for_each_entry(chain, &block->chain_list, list) {
1741 if (tca[TCA_CHAIN] &&
1742 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1743 continue;
a10fa201 1744 if (!tcf_chain_dump(chain, q, parent, skb, cb,
5ae437ad
RK
1745 index_start, &index)) {
1746 err = -EMSGSIZE;
5bc17018 1747 break;
5ae437ad 1748 }
5bc17018
JP
1749 }
1750
acb31fae 1751 cb->args[0] = index;
1da177e4 1752
1da177e4 1753out:
5ae437ad
RK
1754 /* If we did no progress, the error (EMSGSIZE) is real */
1755 if (skb->len == 0 && err)
1756 return err;
1da177e4
LT
1757 return skb->len;
1758}
1759
32a4f5ec
JP
1760static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1761 struct sk_buff *skb, struct tcf_block *block,
1762 u32 portid, u32 seq, u16 flags, int event)
1763{
1764 unsigned char *b = skb_tail_pointer(skb);
9f407f17 1765 const struct tcf_proto_ops *ops;
32a4f5ec
JP
1766 struct nlmsghdr *nlh;
1767 struct tcmsg *tcm;
9f407f17
JP
1768 void *priv;
1769
1770 ops = chain->tmplt_ops;
1771 priv = chain->tmplt_priv;
32a4f5ec
JP
1772
1773 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1774 if (!nlh)
1775 goto out_nlmsg_trim;
1776 tcm = nlmsg_data(nlh);
1777 tcm->tcm_family = AF_UNSPEC;
1778 tcm->tcm__pad1 = 0;
1779 tcm->tcm__pad2 = 0;
1780 tcm->tcm_handle = 0;
1781 if (block->q) {
1782 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1783 tcm->tcm_parent = block->q->handle;
1784 } else {
1785 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1786 tcm->tcm_block_index = block->index;
1787 }
1788
1789 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1790 goto nla_put_failure;
1791
9f407f17
JP
1792 if (ops) {
1793 if (nla_put_string(skb, TCA_KIND, ops->kind))
1794 goto nla_put_failure;
1795 if (ops->tmplt_dump(skb, net, priv) < 0)
1796 goto nla_put_failure;
1797 }
1798
32a4f5ec
JP
1799 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1800 return skb->len;
1801
1802out_nlmsg_trim:
1803nla_put_failure:
1804 nlmsg_trim(skb, b);
1805 return -EMSGSIZE;
1806}
1807
1808static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1809 u32 seq, u16 flags, int event, bool unicast)
1810{
1811 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1812 struct tcf_block *block = chain->block;
1813 struct net *net = block->net;
1814 struct sk_buff *skb;
1815
1816 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1817 if (!skb)
1818 return -ENOBUFS;
1819
1820 if (tc_chain_fill_node(chain, net, skb, block, portid,
1821 seq, flags, event) <= 0) {
1822 kfree_skb(skb);
1823 return -EINVAL;
1824 }
1825
1826 if (unicast)
1827 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1828
1829 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1830}
1831
9f407f17
JP
1832static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1833 struct nlattr **tca,
1834 struct netlink_ext_ack *extack)
1835{
1836 const struct tcf_proto_ops *ops;
1837 void *tmplt_priv;
1838
1839 /* If kind is not set, user did not specify template. */
1840 if (!tca[TCA_KIND])
1841 return 0;
1842
1843 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1844 if (IS_ERR(ops))
1845 return PTR_ERR(ops);
1846 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1847 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1848 return -EOPNOTSUPP;
1849 }
1850
1851 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1852 if (IS_ERR(tmplt_priv)) {
1853 module_put(ops->owner);
1854 return PTR_ERR(tmplt_priv);
1855 }
1856 chain->tmplt_ops = ops;
1857 chain->tmplt_priv = tmplt_priv;
1858 return 0;
1859}
1860
1861static void tc_chain_tmplt_del(struct tcf_chain *chain)
1862{
1863 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1864
1865 /* If template ops are set, no work to do for us. */
1866 if (!ops)
1867 return;
1868
1869 ops->tmplt_destroy(chain->tmplt_priv);
1870 module_put(ops->owner);
1871}
1872
32a4f5ec
JP
1873/* Add/delete/get a chain */
1874
1875static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1876 struct netlink_ext_ack *extack)
1877{
1878 struct net *net = sock_net(skb->sk);
1879 struct nlattr *tca[TCA_MAX + 1];
1880 struct tcmsg *t;
1881 u32 parent;
1882 u32 chain_index;
1883 struct Qdisc *q = NULL;
1884 struct tcf_chain *chain = NULL;
1885 struct tcf_block *block;
1886 unsigned long cl;
1887 int err;
1888
1889 if (n->nlmsg_type != RTM_GETCHAIN &&
1890 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1891 return -EPERM;
1892
1893replay:
1894 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1895 if (err < 0)
1896 return err;
1897
1898 t = nlmsg_data(n);
1899 parent = t->tcm_parent;
1900 cl = 0;
1901
1902 block = tcf_block_find(net, &q, &parent, &cl,
1903 t->tcm_ifindex, t->tcm_block_index, extack);
1904 if (IS_ERR(block))
1905 return PTR_ERR(block);
1906
1907 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1908 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1909 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
e368fdb6
VB
1910 err = -EINVAL;
1911 goto errout_block;
32a4f5ec
JP
1912 }
1913 chain = tcf_chain_lookup(block, chain_index);
1914 if (n->nlmsg_type == RTM_NEWCHAIN) {
1915 if (chain) {
3d32f4c5 1916 if (tcf_chain_held_by_acts_only(chain)) {
1f3ed383 1917 /* The chain exists only because there is
3d32f4c5 1918 * some action referencing it.
1f3ed383
JP
1919 */
1920 tcf_chain_hold(chain);
1921 } else {
1922 NL_SET_ERR_MSG(extack, "Filter chain already exists");
e368fdb6
VB
1923 err = -EEXIST;
1924 goto errout_block;
1f3ed383
JP
1925 }
1926 } else {
1927 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1928 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
e368fdb6
VB
1929 err = -ENOENT;
1930 goto errout_block;
1f3ed383
JP
1931 }
1932 chain = tcf_chain_create(block, chain_index);
1933 if (!chain) {
1934 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
e368fdb6
VB
1935 err = -ENOMEM;
1936 goto errout_block;
1f3ed383 1937 }
32a4f5ec
JP
1938 }
1939 } else {
3d32f4c5 1940 if (!chain || tcf_chain_held_by_acts_only(chain)) {
32a4f5ec 1941 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
e368fdb6
VB
1942 err = -EINVAL;
1943 goto errout_block;
32a4f5ec
JP
1944 }
1945 tcf_chain_hold(chain);
1946 }
1947
1948 switch (n->nlmsg_type) {
1949 case RTM_NEWCHAIN:
9f407f17
JP
1950 err = tc_chain_tmplt_add(chain, net, tca, extack);
1951 if (err)
1952 goto errout;
32a4f5ec
JP
1953 /* In case the chain was successfully added, take a reference
1954 * to the chain. This ensures that an empty chain
1955 * does not disappear at the end of this function.
1956 */
1957 tcf_chain_hold(chain);
1958 chain->explicitly_created = true;
1959 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1960 RTM_NEWCHAIN, false);
1961 break;
1962 case RTM_DELCHAIN:
f5b9bac7
CW
1963 tfilter_notify_chain(net, skb, block, q, parent, n,
1964 chain, RTM_DELTFILTER);
32a4f5ec
JP
1965 /* Flush the chain first as the user requested chain removal. */
1966 tcf_chain_flush(chain);
1967 /* In case the chain was successfully deleted, put a reference
1968 * to the chain previously taken during addition.
1969 */
1970 tcf_chain_put_explicitly_created(chain);
c921d7db 1971 chain->explicitly_created = false;
32a4f5ec
JP
1972 break;
1973 case RTM_GETCHAIN:
32a4f5ec
JP
1974 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1975 n->nlmsg_seq, n->nlmsg_type, true);
1976 if (err < 0)
1977 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1978 break;
1979 default:
1980 err = -EOPNOTSUPP;
1981 NL_SET_ERR_MSG(extack, "Unsupported message type");
1982 goto errout;
1983 }
1984
1985errout:
1986 tcf_chain_put(chain);
e368fdb6
VB
1987errout_block:
1988 tcf_block_release(q, block);
32a4f5ec
JP
1989 if (err == -EAGAIN)
1990 /* Replay the request. */
1991 goto replay;
1992 return err;
1993}
1994
1995/* called with RTNL */
1996static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
1997{
1998 struct net *net = sock_net(skb->sk);
1999 struct nlattr *tca[TCA_MAX + 1];
2000 struct Qdisc *q = NULL;
2001 struct tcf_block *block;
2002 struct tcf_chain *chain;
2003 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2004 long index_start;
2005 long index;
2006 u32 parent;
2007 int err;
2008
2009 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2010 return skb->len;
2011
2012 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
2013 if (err)
2014 return err;
2015
2016 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2017 block = tcf_block_lookup(net, tcm->tcm_block_index);
2018 if (!block)
2019 goto out;
2020 /* If we work with block index, q is NULL and parent value
2021 * will never be used in the following code. The check
2022 * in tcf_fill_node prevents it. However, compiler does not
2023 * see that far, so set parent to zero to silence the warning
2024 * about parent being uninitialized.
2025 */
2026 parent = 0;
2027 } else {
2028 const struct Qdisc_class_ops *cops;
2029 struct net_device *dev;
2030 unsigned long cl = 0;
2031
2032 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2033 if (!dev)
2034 return skb->len;
2035
2036 parent = tcm->tcm_parent;
2037 if (!parent) {
2038 q = dev->qdisc;
2039 parent = q->handle;
2040 } else {
2041 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2042 }
2043 if (!q)
2044 goto out;
2045 cops = q->ops->cl_ops;
2046 if (!cops)
2047 goto out;
2048 if (!cops->tcf_block)
2049 goto out;
2050 if (TC_H_MIN(tcm->tcm_parent)) {
2051 cl = cops->find(q, tcm->tcm_parent);
2052 if (cl == 0)
2053 goto out;
2054 }
2055 block = cops->tcf_block(q, cl, NULL);
2056 if (!block)
2057 goto out;
2058 if (tcf_block_shared(block))
2059 q = NULL;
2060 }
2061
2062 index_start = cb->args[0];
2063 index = 0;
2064
2065 list_for_each_entry(chain, &block->chain_list, list) {
2066 if ((tca[TCA_CHAIN] &&
2067 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2068 continue;
2069 if (index < index_start) {
2070 index++;
2071 continue;
2072 }
3d32f4c5 2073 if (tcf_chain_held_by_acts_only(chain))
1f3ed383 2074 continue;
32a4f5ec
JP
2075 err = tc_chain_fill_node(chain, net, skb, block,
2076 NETLINK_CB(cb->skb).portid,
2077 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2078 RTM_NEWCHAIN);
2079 if (err <= 0)
2080 break;
2081 index++;
2082 }
2083
2084 cb->args[0] = index;
2085
2086out:
2087 /* If we did no progress, the error (EMSGSIZE) is real */
2088 if (skb->len == 0 && err)
2089 return err;
2090 return skb->len;
2091}
2092
18d0264f 2093void tcf_exts_destroy(struct tcf_exts *exts)
1da177e4
LT
2094{
2095#ifdef CONFIG_NET_CLS_ACT
90b73b77 2096 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
22dc13c8
WC
2097 kfree(exts->actions);
2098 exts->nr_actions = 0;
1da177e4
LT
2099#endif
2100}
aa767bfe 2101EXPORT_SYMBOL(tcf_exts_destroy);
1da177e4 2102
c1b52739 2103int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
50a56190
AA
2104 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2105 struct netlink_ext_ack *extack)
1da177e4 2106{
1da177e4
LT
2107#ifdef CONFIG_NET_CLS_ACT
2108 {
1da177e4 2109 struct tc_action *act;
d04e6990 2110 size_t attr_size = 0;
1da177e4 2111
5da57f42 2112 if (exts->police && tb[exts->police]) {
9fb9f251
JP
2113 act = tcf_action_init_1(net, tp, tb[exts->police],
2114 rate_tlv, "police", ovr,
789871bb 2115 TCA_ACT_BIND, true, extack);
ab27cfb8
PM
2116 if (IS_ERR(act))
2117 return PTR_ERR(act);
1da177e4 2118
33be6271 2119 act->type = exts->type = TCA_OLD_COMPAT;
22dc13c8
WC
2120 exts->actions[0] = act;
2121 exts->nr_actions = 1;
5da57f42 2122 } else if (exts->action && tb[exts->action]) {
90b73b77 2123 int err;
22dc13c8 2124
9fb9f251
JP
2125 err = tcf_action_init(net, tp, tb[exts->action],
2126 rate_tlv, NULL, ovr, TCA_ACT_BIND,
90b73b77 2127 exts->actions, &attr_size, true,
789871bb 2128 extack);
90b73b77 2129 if (err < 0)
33be6271 2130 return err;
90b73b77 2131 exts->nr_actions = err;
1da177e4 2132 }
e4b95c41 2133 exts->net = net;
1da177e4 2134 }
1da177e4 2135#else
5da57f42 2136 if ((exts->action && tb[exts->action]) ||
50a56190
AA
2137 (exts->police && tb[exts->police])) {
2138 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
1da177e4 2139 return -EOPNOTSUPP;
50a56190 2140 }
1da177e4
LT
2141#endif
2142
2143 return 0;
2144}
aa767bfe 2145EXPORT_SYMBOL(tcf_exts_validate);
1da177e4 2146
9b0d4446 2147void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
1da177e4
LT
2148{
2149#ifdef CONFIG_NET_CLS_ACT
22dc13c8
WC
2150 struct tcf_exts old = *dst;
2151
9b0d4446 2152 *dst = *src;
22dc13c8 2153 tcf_exts_destroy(&old);
1da177e4
LT
2154#endif
2155}
aa767bfe 2156EXPORT_SYMBOL(tcf_exts_change);
1da177e4 2157
22dc13c8
WC
2158#ifdef CONFIG_NET_CLS_ACT
2159static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2160{
2161 if (exts->nr_actions == 0)
2162 return NULL;
2163 else
2164 return exts->actions[0];
2165}
2166#endif
33be6271 2167
5da57f42 2168int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
2169{
2170#ifdef CONFIG_NET_CLS_ACT
9cc63db5
CW
2171 struct nlattr *nest;
2172
978dfd8d 2173 if (exts->action && tcf_exts_has_actions(exts)) {
1da177e4
LT
2174 /*
2175 * again for backward compatible mode - we want
2176 * to work with both old and new modes of entering
2177 * tc data even if iproute2 was newer - jhs
2178 */
33be6271 2179 if (exts->type != TCA_OLD_COMPAT) {
5da57f42 2180 nest = nla_nest_start(skb, exts->action);
4b3550ef
PM
2181 if (nest == NULL)
2182 goto nla_put_failure;
22dc13c8 2183
90b73b77 2184 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
add93b61 2185 goto nla_put_failure;
4b3550ef 2186 nla_nest_end(skb, nest);
5da57f42 2187 } else if (exts->police) {
33be6271 2188 struct tc_action *act = tcf_exts_first_act(exts);
5da57f42 2189 nest = nla_nest_start(skb, exts->police);
63acd680 2190 if (nest == NULL || !act)
4b3550ef 2191 goto nla_put_failure;
33be6271 2192 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
add93b61 2193 goto nla_put_failure;
4b3550ef 2194 nla_nest_end(skb, nest);
1da177e4
LT
2195 }
2196 }
1da177e4 2197 return 0;
9cc63db5
CW
2198
2199nla_put_failure:
2200 nla_nest_cancel(skb, nest);
1da177e4 2201 return -1;
9cc63db5
CW
2202#else
2203 return 0;
2204#endif
1da177e4 2205}
aa767bfe 2206EXPORT_SYMBOL(tcf_exts_dump);
1da177e4 2207
aa767bfe 2208
5da57f42 2209int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
1da177e4
LT
2210{
2211#ifdef CONFIG_NET_CLS_ACT
33be6271 2212 struct tc_action *a = tcf_exts_first_act(exts);
b057df24 2213 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
33be6271 2214 return -1;
1da177e4
LT
2215#endif
2216 return 0;
1da177e4 2217}
aa767bfe 2218EXPORT_SYMBOL(tcf_exts_dump_stats);
1da177e4 2219
717503b9
JP
2220static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2221 enum tc_setup_type type,
2222 void *type_data, bool err_stop)
b3f55bdd
JP
2223{
2224 int ok_count = 0;
2225#ifdef CONFIG_NET_CLS_ACT
2226 const struct tc_action *a;
2227 struct net_device *dev;
9d452ceb 2228 int i, ret;
b3f55bdd
JP
2229
2230 if (!tcf_exts_has_actions(exts))
2231 return 0;
2232
9d452ceb
OG
2233 for (i = 0; i < exts->nr_actions; i++) {
2234 a = exts->actions[i];
b3f55bdd
JP
2235 if (!a->ops->get_dev)
2236 continue;
2237 dev = a->ops->get_dev(a);
7612fb03 2238 if (!dev)
b3f55bdd
JP
2239 continue;
2240 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
84a75b32 2241 a->ops->put_dev(dev);
b3f55bdd
JP
2242 if (ret < 0)
2243 return ret;
2244 ok_count += ret;
2245 }
2246#endif
2247 return ok_count;
2248}
717503b9 2249
208c0f4b
JP
2250int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2251 enum tc_setup_type type, void *type_data, bool err_stop)
717503b9 2252{
9a99dc1c 2253 int ok_count;
208c0f4b
JP
2254 int ret;
2255
9a99dc1c
DM
2256 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2257 if (ret < 0)
2258 return ret;
2259 ok_count = ret;
208c0f4b 2260
f8f4bef3 2261 if (!exts || ok_count)
9a99dc1c 2262 return ok_count;
208c0f4b
JP
2263 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2264 if (ret < 0)
2265 return ret;
2266 ok_count += ret;
2267
2268 return ok_count;
717503b9
JP
2269}
2270EXPORT_SYMBOL(tc_setup_cb_call);
b3f55bdd 2271
48617387
JP
2272static __net_init int tcf_net_init(struct net *net)
2273{
2274 struct tcf_net *tn = net_generic(net, tcf_net_id);
2275
2276 idr_init(&tn->idr);
2277 return 0;
2278}
2279
2280static void __net_exit tcf_net_exit(struct net *net)
2281{
2282 struct tcf_net *tn = net_generic(net, tcf_net_id);
2283
2284 idr_destroy(&tn->idr);
2285}
2286
2287static struct pernet_operations tcf_net_ops = {
2288 .init = tcf_net_init,
2289 .exit = tcf_net_exit,
2290 .id = &tcf_net_id,
2291 .size = sizeof(struct tcf_net),
2292};
2293
1da177e4
LT
2294static int __init tc_filter_init(void)
2295{
48617387
JP
2296 int err;
2297
7aa0045d
CW
2298 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2299 if (!tc_filter_wq)
2300 return -ENOMEM;
2301
48617387
JP
2302 err = register_pernet_subsys(&tcf_net_ops);
2303 if (err)
2304 goto err_register_pernet_subsys;
2305
c431f89b
VB
2306 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2307 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2308 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
b97bac64 2309 tc_dump_tfilter, 0);
32a4f5ec
JP
2310 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2311 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2312 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2313 tc_dump_chain, 0);
1da177e4 2314
1da177e4 2315 return 0;
48617387
JP
2316
2317err_register_pernet_subsys:
2318 destroy_workqueue(tc_filter_wq);
2319 return err;
1da177e4
LT
2320}
2321
2322subsys_initcall(tc_filter_init);