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