blob: 22b977d40e1db09726909cc8a76fe8240aaa1d66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
Jiri Pirko33a48922017-02-09 14:38:57 +010022#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110027#include <net/net_namespace.h>
28#include <net/sock.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070029#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/pkt_sched.h>
31#include <net/pkt_cls.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/* The list of all installed classifier types */
WANG Cong36272872013-12-15 20:15:11 -080034static LIST_HEAD(tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/* Protects list of registered TC modules. It is pure SMP lock. */
37static DEFINE_RWLOCK(cls_mod_lock);
38
39/* Find classifier type by string name */
40
Jiri Pirko33a48922017-02-09 14:38:57 +010041static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Eric Dumazetdcd76082013-12-20 10:04:18 -080043 const struct tcf_proto_ops *t, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if (kind) {
46 read_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080047 list_for_each_entry(t, &tcf_proto_base, head) {
Jiri Pirko33a48922017-02-09 14:38:57 +010048 if (strcmp(kind, t->kind) == 0) {
Eric Dumazetdcd76082013-12-20 10:04:18 -080049 if (try_module_get(t->owner))
50 res = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 break;
52 }
53 }
54 read_unlock(&cls_mod_lock);
55 }
Eric Dumazetdcd76082013-12-20 10:04:18 -080056 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59/* Register(unregister) new classifier type */
60
61int register_tcf_proto_ops(struct tcf_proto_ops *ops)
62{
WANG Cong36272872013-12-15 20:15:11 -080063 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int rc = -EEXIST;
65
66 write_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080067 list_for_each_entry(t, &tcf_proto_base, head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!strcmp(ops->kind, t->kind))
69 goto out;
70
WANG Cong36272872013-12-15 20:15:11 -080071 list_add_tail(&ops->head, &tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 rc = 0;
73out:
74 write_unlock(&cls_mod_lock);
75 return rc;
76}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -080077EXPORT_SYMBOL(register_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Cong Wang7aa00452017-10-26 18:24:28 -070079static struct workqueue_struct *tc_filter_wq;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
82{
WANG Cong36272872013-12-15 20:15:11 -080083 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 int rc = -ENOENT;
85
Daniel Borkmannc78e1742015-05-20 17:13:33 +020086 /* Wait for outstanding call_rcu()s, if any, from a
87 * tcf_proto_ops's destroy() handler.
88 */
89 rcu_barrier();
Cong Wang7aa00452017-10-26 18:24:28 -070090 flush_workqueue(tc_filter_wq);
Daniel Borkmannc78e1742015-05-20 17:13:33 +020091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 write_lock(&cls_mod_lock);
Eric Dumazetdcd76082013-12-20 10:04:18 -080093 list_for_each_entry(t, &tcf_proto_base, head) {
94 if (t == ops) {
95 list_del(&t->head);
96 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 break;
Eric Dumazetdcd76082013-12-20 10:04:18 -080098 }
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 write_unlock(&cls_mod_lock);
101 return rc;
102}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800103EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Cong Wang7aa00452017-10-26 18:24:28 -0700105bool tcf_queue_work(struct work_struct *work)
106{
107 return queue_work(tc_filter_wq, work);
108}
109EXPORT_SYMBOL(tcf_queue_work);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* Select new prio value from the range, managed by kernel. */
112
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800113static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800115 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000118 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Jiri Pirko79619732017-05-17 11:07:58 +0200120 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Jiri Pirko33a48922017-02-09 14:38:57 +0100123static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200124 u32 prio, u32 parent, struct Qdisc *q,
Jiri Pirko5bc17012017-05-17 11:08:01 +0200125 struct tcf_chain *chain)
Jiri Pirko33a48922017-02-09 14:38:57 +0100126{
127 struct tcf_proto *tp;
128 int err;
129
130 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
131 if (!tp)
132 return ERR_PTR(-ENOBUFS);
133
134 err = -ENOENT;
135 tp->ops = tcf_proto_lookup_ops(kind);
136 if (!tp->ops) {
137#ifdef CONFIG_MODULES
138 rtnl_unlock();
139 request_module("cls_%s", kind);
140 rtnl_lock();
141 tp->ops = tcf_proto_lookup_ops(kind);
142 /* We dropped the RTNL semaphore in order to perform
143 * the module load. So, even if we succeeded in loading
144 * the module we have to replay the request. We indicate
145 * this using -EAGAIN.
146 */
147 if (tp->ops) {
148 module_put(tp->ops->owner);
149 err = -EAGAIN;
150 } else {
151 err = -ENOENT;
152 }
153 goto errout;
154#endif
155 }
156 tp->classify = tp->ops->classify;
157 tp->protocol = protocol;
158 tp->prio = prio;
159 tp->classid = parent;
160 tp->q = q;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200161 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100162
163 err = tp->ops->init(tp);
164 if (err) {
165 module_put(tp->ops->owner);
166 goto errout;
167 }
168 return tp;
169
170errout:
171 kfree(tp);
172 return ERR_PTR(err);
173}
174
WANG Cong763dbf62017-04-19 14:21:21 -0700175static void tcf_proto_destroy(struct tcf_proto *tp)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100176{
WANG Cong763dbf62017-04-19 14:21:21 -0700177 tp->ops->destroy(tp);
178 module_put(tp->ops->owner);
179 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100180}
181
Jiri Pirko5bc17012017-05-17 11:08:01 +0200182static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
183 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200184{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200185 struct tcf_chain *chain;
186
187 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
188 if (!chain)
189 return NULL;
190 list_add_tail(&chain->list, &block->chain_list);
191 chain->block = block;
192 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700193 chain->refcnt = 1;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200194 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200195}
196
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100197static void tcf_chain_head_change(struct tcf_chain *chain,
198 struct tcf_proto *tp_head)
199{
200 if (chain->chain_head_change)
201 chain->chain_head_change(tp_head,
202 chain->chain_head_change_priv);
203}
204
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200205static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100206{
Roman Kapld7aa04a2017-11-20 22:21:13 +0100207 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100208
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100209 tcf_chain_head_change(chain, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100210 while (tp) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200211 RCU_INIT_POINTER(chain->filter_chain, tp->next);
WANG Cong763dbf62017-04-19 14:21:21 -0700212 tcf_proto_destroy(tp);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100213 tp = rtnl_dereference(chain->filter_chain);
214 tcf_chain_put(chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100215 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200216}
217
218static void tcf_chain_destroy(struct tcf_chain *chain)
219{
Cong Wangefbf7892017-12-04 10:48:18 -0800220 struct tcf_block *block = chain->block;
221
Cong Wange2ef7542017-09-11 16:33:31 -0700222 list_del(&chain->list);
223 kfree(chain);
Cong Wangefbf7892017-12-04 10:48:18 -0800224 if (list_empty(&block->chain_list))
225 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700226}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200227
Cong Wange2ef7542017-09-11 16:33:31 -0700228static void tcf_chain_hold(struct tcf_chain *chain)
229{
230 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200231}
232
WANG Cong367a8ce2017-05-23 09:42:37 -0700233struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
234 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200235{
236 struct tcf_chain *chain;
237
238 list_for_each_entry(chain, &block->chain_list, list) {
Cong Wange2ef7542017-09-11 16:33:31 -0700239 if (chain->index == chain_index) {
240 tcf_chain_hold(chain);
241 return chain;
242 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200243 }
Jiri Pirko80532382017-09-06 13:14:19 +0200244
Cong Wange2ef7542017-09-11 16:33:31 -0700245 return create ? tcf_chain_create(block, chain_index) : NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200246}
247EXPORT_SYMBOL(tcf_chain_get);
248
249void tcf_chain_put(struct tcf_chain *chain)
250{
Cong Wange2ef7542017-09-11 16:33:31 -0700251 if (--chain->refcnt == 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200252 tcf_chain_destroy(chain);
253}
254EXPORT_SYMBOL(tcf_chain_put);
255
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200256static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q,
257 struct tcf_block_ext_info *ei,
258 enum tc_block_command command)
259{
260 struct net_device *dev = q->dev_queue->dev;
261 struct tc_block_offload bo = {};
262
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100263 if (!dev->netdev_ops->ndo_setup_tc)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200264 return;
265 bo.command = command;
266 bo.binder_type = ei->binder_type;
267 bo.block = block;
268 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
269}
270
271static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
272 struct tcf_block_ext_info *ei)
273{
274 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND);
275}
276
277static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
278 struct tcf_block_ext_info *ei)
279{
280 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND);
281}
282
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100283int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200284 struct tcf_block_ext_info *ei)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200285{
286 struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200287 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200288 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200289
290 if (!block)
291 return -ENOMEM;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200292 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200293 INIT_LIST_HEAD(&block->cb_list);
294
Jiri Pirko5bc17012017-05-17 11:08:01 +0200295 /* Create chain 0 by default, it has to be always present. */
296 chain = tcf_chain_create(block, 0);
297 if (!chain) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200298 err = -ENOMEM;
299 goto err_chain_create;
300 }
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100301 WARN_ON(!ei->chain_head_change);
302 chain->chain_head_change = ei->chain_head_change;
303 chain->chain_head_change_priv = ei->chain_head_change_priv;
Jiri Pirko855319b2017-10-13 14:00:58 +0200304 block->net = qdisc_net(q);
Jiri Pirko69d78ef2017-10-13 14:00:57 +0200305 block->q = q;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200306 tcf_block_offload_bind(block, q, ei);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200307 *p_block = block;
308 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200309
310err_chain_create:
311 kfree(block);
312 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200313}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200314EXPORT_SYMBOL(tcf_block_get_ext);
315
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100316static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
317{
318 struct tcf_proto __rcu **p_filter_chain = priv;
319
320 rcu_assign_pointer(*p_filter_chain, tp_head);
321}
322
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200323int tcf_block_get(struct tcf_block **p_block,
324 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
325{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100326 struct tcf_block_ext_info ei = {
327 .chain_head_change = tcf_chain_head_change_dflt,
328 .chain_head_change_priv = p_filter_chain,
329 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200330
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100331 WARN_ON(!p_filter_chain);
332 return tcf_block_get_ext(p_block, q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200333}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200334EXPORT_SYMBOL(tcf_block_get);
335
Cong Wang7aa00452017-10-26 18:24:28 -0700336/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100337 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700338 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100339void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900340 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700341{
Cong Wangefbf7892017-12-04 10:48:18 -0800342 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700343
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100344 /* Hold a refcnt for all chains, so that they don't disappear
Roman Kapla60b3f52017-11-24 12:27:58 +0100345 * while we are iterating.
346 */
David S. Millerc30abd52017-12-16 22:11:55 -0500347 if (!block)
348 return;
Roman Kapla60b3f52017-11-24 12:27:58 +0100349 list_for_each_entry(chain, &block->chain_list, list)
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100350 tcf_chain_hold(chain);
Roman Kapla60b3f52017-11-24 12:27:58 +0100351
352 list_for_each_entry(chain, &block->chain_list, list)
Cong Wang1697c4b2017-09-11 16:33:32 -0700353 tcf_chain_flush(chain);
354
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100355 tcf_block_offload_unbind(block, q, ei);
356
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100357 /* At this point, all the chains should have refcnt >= 1. */
Cong Wangefbf7892017-12-04 10:48:18 -0800358 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
359 tcf_chain_put(chain);
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100360
361 /* Finally, put chain 0 and allow block to be freed. */
362 chain = list_first_entry(&block->chain_list, struct tcf_chain, list);
363 tcf_chain_put(chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200364}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200365EXPORT_SYMBOL(tcf_block_put_ext);
366
367void tcf_block_put(struct tcf_block *block)
368{
369 struct tcf_block_ext_info ei = {0, };
370
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100371 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200372}
David S. Millere1ea2f92017-10-30 14:10:01 +0900373
Jiri Pirko6529eab2017-05-17 11:07:55 +0200374EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100375
Jiri Pirkoacb67442017-10-19 15:50:31 +0200376struct tcf_block_cb {
377 struct list_head list;
378 tc_setup_cb_t *cb;
379 void *cb_ident;
380 void *cb_priv;
381 unsigned int refcnt;
382};
383
384void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
385{
386 return block_cb->cb_priv;
387}
388EXPORT_SYMBOL(tcf_block_cb_priv);
389
390struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
391 tc_setup_cb_t *cb, void *cb_ident)
392{ struct tcf_block_cb *block_cb;
393
394 list_for_each_entry(block_cb, &block->cb_list, list)
395 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
396 return block_cb;
397 return NULL;
398}
399EXPORT_SYMBOL(tcf_block_cb_lookup);
400
401void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
402{
403 block_cb->refcnt++;
404}
405EXPORT_SYMBOL(tcf_block_cb_incref);
406
407unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
408{
409 return --block_cb->refcnt;
410}
411EXPORT_SYMBOL(tcf_block_cb_decref);
412
413struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
414 tc_setup_cb_t *cb, void *cb_ident,
415 void *cb_priv)
416{
417 struct tcf_block_cb *block_cb;
418
419 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
420 if (!block_cb)
421 return NULL;
422 block_cb->cb = cb;
423 block_cb->cb_ident = cb_ident;
424 block_cb->cb_priv = cb_priv;
425 list_add(&block_cb->list, &block->cb_list);
426 return block_cb;
427}
428EXPORT_SYMBOL(__tcf_block_cb_register);
429
430int tcf_block_cb_register(struct tcf_block *block,
431 tc_setup_cb_t *cb, void *cb_ident,
432 void *cb_priv)
433{
434 struct tcf_block_cb *block_cb;
435
436 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
437 return block_cb ? 0 : -ENOMEM;
438}
439EXPORT_SYMBOL(tcf_block_cb_register);
440
441void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
442{
443 list_del(&block_cb->list);
444 kfree(block_cb);
445}
446EXPORT_SYMBOL(__tcf_block_cb_unregister);
447
448void tcf_block_cb_unregister(struct tcf_block *block,
449 tc_setup_cb_t *cb, void *cb_ident)
450{
451 struct tcf_block_cb *block_cb;
452
453 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
454 if (!block_cb)
455 return;
456 __tcf_block_cb_unregister(block_cb);
457}
458EXPORT_SYMBOL(tcf_block_cb_unregister);
459
460static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
461 void *type_data, bool err_stop)
462{
463 struct tcf_block_cb *block_cb;
464 int ok_count = 0;
465 int err;
466
467 list_for_each_entry(block_cb, &block->cb_list, list) {
468 err = block_cb->cb(type, type_data, block_cb->cb_priv);
469 if (err) {
470 if (err_stop)
471 return err;
472 } else {
473 ok_count++;
474 }
475 }
476 return ok_count;
477}
478
Jiri Pirko87d83092017-05-17 11:07:54 +0200479/* Main classifier routine: scans classifier chain attached
480 * to this qdisc, (optionally) tests for protocol and asks
481 * specific classifiers.
482 */
483int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
484 struct tcf_result *res, bool compat_mode)
485{
486 __be16 protocol = tc_skb_protocol(skb);
487#ifdef CONFIG_NET_CLS_ACT
488 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200489 const struct tcf_proto *orig_tp = tp;
490 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200491 int limit = 0;
492
493reclassify:
494#endif
495 for (; tp; tp = rcu_dereference_bh(tp->next)) {
496 int err;
497
498 if (tp->protocol != protocol &&
499 tp->protocol != htons(ETH_P_ALL))
500 continue;
501
502 err = tp->classify(skb, tp, res);
503#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200504 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200505 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200506 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200507 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200508 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200509 goto reset;
510 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200511#endif
512 if (err >= 0)
513 return err;
514 }
515
516 return TC_ACT_UNSPEC; /* signal: continue lookup */
517#ifdef CONFIG_NET_CLS_ACT
518reset:
519 if (unlikely(limit++ >= max_reclassify_loop)) {
520 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
521 tp->q->ops->id, tp->prio & 0xffff,
522 ntohs(tp->protocol));
523 return TC_ACT_SHOT;
524 }
525
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200526 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200527 protocol = tc_skb_protocol(skb);
528 goto reclassify;
529#endif
530}
531EXPORT_SYMBOL(tcf_classify);
532
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200533struct tcf_chain_info {
534 struct tcf_proto __rcu **pprev;
535 struct tcf_proto __rcu *next;
536};
537
538static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
539{
540 return rtnl_dereference(*chain_info->pprev);
541}
542
543static void tcf_chain_tp_insert(struct tcf_chain *chain,
544 struct tcf_chain_info *chain_info,
545 struct tcf_proto *tp)
546{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100547 if (*chain_info->pprev == chain->filter_chain)
548 tcf_chain_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200549 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
550 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700551 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200552}
553
554static void tcf_chain_tp_remove(struct tcf_chain *chain,
555 struct tcf_chain_info *chain_info,
556 struct tcf_proto *tp)
557{
558 struct tcf_proto *next = rtnl_dereference(chain_info->next);
559
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100560 if (tp == chain->filter_chain)
561 tcf_chain_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200562 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700563 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200564}
565
566static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
567 struct tcf_chain_info *chain_info,
568 u32 protocol, u32 prio,
569 bool prio_allocate)
570{
571 struct tcf_proto **pprev;
572 struct tcf_proto *tp;
573
574 /* Check the chain for existence of proto-tcf with this priority */
575 for (pprev = &chain->filter_chain;
576 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
577 if (tp->prio >= prio) {
578 if (tp->prio == prio) {
579 if (prio_allocate ||
580 (tp->protocol != protocol && protocol))
581 return ERR_PTR(-EINVAL);
582 } else {
583 tp = NULL;
584 }
585 break;
586 }
587 }
588 chain_info->pprev = pprev;
589 chain_info->next = tp ? tp->next : NULL;
590 return tp;
591}
592
WANG Cong71203712017-08-07 15:26:50 -0700593static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200594 struct tcf_proto *tp, struct Qdisc *q, u32 parent,
595 void *fh, u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700596{
597 struct tcmsg *tcm;
598 struct nlmsghdr *nlh;
599 unsigned char *b = skb_tail_pointer(skb);
600
601 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
602 if (!nlh)
603 goto out_nlmsg_trim;
604 tcm = nlmsg_data(nlh);
605 tcm->tcm_family = AF_UNSPEC;
606 tcm->tcm__pad1 = 0;
607 tcm->tcm__pad2 = 0;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200608 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
609 tcm->tcm_parent = parent;
WANG Cong71203712017-08-07 15:26:50 -0700610 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
611 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
612 goto nla_put_failure;
613 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
614 goto nla_put_failure;
615 if (!fh) {
616 tcm->tcm_handle = 0;
617 } else {
618 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
619 goto nla_put_failure;
620 }
621 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
622 return skb->len;
623
624out_nlmsg_trim:
625nla_put_failure:
626 nlmsg_trim(skb, b);
627 return -1;
628}
629
630static int tfilter_notify(struct net *net, struct sk_buff *oskb,
631 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200632 struct Qdisc *q, u32 parent,
WANG Cong71203712017-08-07 15:26:50 -0700633 void *fh, int event, bool unicast)
634{
635 struct sk_buff *skb;
636 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
637
638 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
639 if (!skb)
640 return -ENOBUFS;
641
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200642 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
WANG Cong71203712017-08-07 15:26:50 -0700643 n->nlmsg_flags, event) <= 0) {
644 kfree_skb(skb);
645 return -EINVAL;
646 }
647
648 if (unicast)
649 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
650
651 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
652 n->nlmsg_flags & NLM_F_ECHO);
653}
654
655static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
656 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200657 struct Qdisc *q, u32 parent,
WANG Cong71203712017-08-07 15:26:50 -0700658 void *fh, bool unicast, bool *last)
659{
660 struct sk_buff *skb;
661 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
662 int err;
663
664 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
665 if (!skb)
666 return -ENOBUFS;
667
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200668 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
WANG Cong71203712017-08-07 15:26:50 -0700669 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
670 kfree_skb(skb);
671 return -EINVAL;
672 }
673
674 err = tp->ops->delete(tp, fh, last);
675 if (err) {
676 kfree_skb(skb);
677 return err;
678 }
679
680 if (unicast)
681 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
682
683 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
684 n->nlmsg_flags & NLM_F_ECHO);
685}
686
687static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200688 struct Qdisc *q, u32 parent,
WANG Cong71203712017-08-07 15:26:50 -0700689 struct nlmsghdr *n,
690 struct tcf_chain *chain, int event)
691{
692 struct tcf_proto *tp;
693
694 for (tp = rtnl_dereference(chain->filter_chain);
695 tp; tp = rtnl_dereference(tp->next))
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200696 tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
WANG Cong71203712017-08-07 15:26:50 -0700697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/* Add/change/delete/get a filter node */
700
David Ahernc21ef3e2017-04-16 09:48:24 -0700701static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
702 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900704 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800705 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct tcmsg *t;
707 u32 protocol;
708 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200709 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200711 u32 chain_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct net_device *dev;
713 struct Qdisc *q;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200714 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200715 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200716 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct tcf_proto *tp;
Eric Dumazet20fea082007-11-14 01:44:41 -0800718 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -0700720 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +0100722 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Stéphane Graber4e8bbb82014-04-30 11:25:43 -0400724 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -0400725 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000726 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +0000727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +0100729 tp_created = 0;
730
David Ahernc21ef3e2017-04-16 09:48:24 -0700731 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +0000732 if (err < 0)
733 return err;
734
David S. Miller942b8162012-06-26 21:48:50 -0700735 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 protocol = TC_H_MIN(t->tcm_info);
737 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200738 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 parent = t->tcm_parent;
740 cl = 0;
741
742 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200743 switch (n->nlmsg_type) {
744 case RTM_DELTFILTER:
Daniel Borkmann9f6ed032016-06-16 23:19:29 +0200745 if (protocol || t->tcm_handle || tca[TCA_KIND])
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200746 return -ENOENT;
747 break;
748 case RTM_NEWTFILTER:
749 /* If no priority is provided by the user,
750 * we allocate one.
751 */
752 if (n->nlmsg_flags & NLM_F_CREATE) {
753 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200754 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200755 break;
756 }
757 /* fall-through */
758 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
763 /* Find head of filter chain. */
764
765 /* Find link */
Tom Goff7316ae82010-03-19 15:40:13 +0000766 dev = __dev_get_by_index(net, t->tcm_ifindex);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800767 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return -ENODEV;
769
770 /* Find qdisc */
771 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000772 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 parent = q->handle;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800774 } else {
775 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
776 if (q == NULL)
777 return -EINVAL;
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* Is it classful? */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000781 cops = q->ops->cl_ops;
782 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return -EINVAL;
784
Jiri Pirko6529eab2017-05-17 11:07:55 +0200785 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000786 return -EOPNOTSUPP;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* Do we search for filter, attached to class? */
789 if (TC_H_MIN(parent)) {
WANG Cong143976c2017-08-24 16:51:29 -0700790 cl = cops->find(q, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (cl == 0)
792 return -ENOENT;
793 }
794
795 /* And the last stroke */
Alexander Aringcbaacc42017-12-20 12:35:16 -0500796 block = cops->tcf_block(q, cl, extack);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200797 if (!block) {
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100798 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100800 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200801
802 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
803 if (chain_index > TC_ACT_EXT_VAL_MASK) {
804 err = -EINVAL;
805 goto errout;
806 }
WANG Cong367a8ce2017-05-23 09:42:37 -0700807 chain = tcf_chain_get(block, chain_index,
808 n->nlmsg_type == RTM_NEWTFILTER);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200809 if (!chain) {
WANG Cong367a8ce2017-05-23 09:42:37 -0700810 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200811 goto errout;
812 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200813
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200814 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200815 tfilter_notify_chain(net, skb, q, parent, n,
816 chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200817 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200818 err = 0;
819 goto errout;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200822 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
823 prio, prio_allocate);
824 if (IS_ERR(tp)) {
825 err = PTR_ERR(tp);
826 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
829 if (tp == NULL) {
830 /* Proto-tcf does not exist, create new one */
831
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100832 if (tca[TCA_KIND] == NULL || !protocol) {
833 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000837 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100838 !(n->nlmsg_flags & NLM_F_CREATE)) {
839 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200843 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200844 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Jiri Pirko33a48922017-02-09 14:38:57 +0100846 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Jiri Pirko5bc17012017-05-17 11:08:01 +0200847 protocol, prio, parent, q, chain);
Jiri Pirko33a48922017-02-09 14:38:57 +0100848 if (IS_ERR(tp)) {
849 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 goto errout;
851 }
Minoru Usui12186be2009-06-02 02:17:34 -0700852 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100853 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
854 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 fh = tp->ops->get(tp, t->tcm_handle);
859
WANG Cong8113c092017-08-04 21:31:43 -0700860 if (!fh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200862 tcf_chain_tp_remove(chain, &chain_info, tp);
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200863 tfilter_notify(net, skb, n, tp, q, parent, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700864 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -0700865 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 err = 0;
867 goto errout;
868 }
869
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800870 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100871 !(n->nlmsg_flags & NLM_F_CREATE)) {
872 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 } else {
WANG Cong763dbf62017-04-19 14:21:21 -0700876 bool last;
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900879 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -0700880 if (n->nlmsg_flags & NLM_F_EXCL) {
881 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700882 tcf_proto_destroy(tp);
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100883 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -0700885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
887 case RTM_DELTFILTER:
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200888 err = tfilter_del_notify(net, skb, n, tp, q, parent,
889 fh, false, &last);
Jiri Pirko40c81b22017-02-09 14:39:00 +0100890 if (err)
891 goto errout;
WANG Cong763dbf62017-04-19 14:21:21 -0700892 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200893 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -0700894 tcf_proto_destroy(tp);
895 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +0100896 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 case RTM_GETTFILTER:
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200898 err = tfilter_notify(net, skb, n, tp, q, parent, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -0700899 RTM_NEWTFILTER, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto errout;
901 default:
902 err = -EINVAL;
903 goto errout;
904 }
905 }
906
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700907 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
908 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -0700909 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200910 if (tp_created)
911 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200912 tfilter_notify(net, skb, n, tp, q, parent, fh,
913 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -0700914 } else {
915 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -0700916 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -0700917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +0200920 if (chain)
921 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (err == -EAGAIN)
923 /* Replay the request. */
924 goto replay;
925 return err;
926}
927
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800928struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct tcf_walker w;
930 struct sk_buff *skb;
931 struct netlink_callback *cb;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200932 struct Qdisc *q;
933 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934};
935
WANG Cong8113c092017-08-04 21:31:43 -0700936static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800938 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -0800939 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200941 return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
942 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400943 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
944 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200947static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
948 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200949 long index_start, long *p_index)
950{
951 struct net *net = sock_net(skb->sk);
952 struct tcmsg *tcm = nlmsg_data(cb->nlh);
953 struct tcf_dump_args arg;
954 struct tcf_proto *tp;
955
956 for (tp = rtnl_dereference(chain->filter_chain);
957 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
958 if (*p_index < index_start)
959 continue;
960 if (TC_H_MAJ(tcm->tcm_info) &&
961 TC_H_MAJ(tcm->tcm_info) != tp->prio)
962 continue;
963 if (TC_H_MIN(tcm->tcm_info) &&
964 TC_H_MIN(tcm->tcm_info) != tp->protocol)
965 continue;
966 if (*p_index > index_start)
967 memset(&cb->args[1], 0,
968 sizeof(cb->args) - sizeof(cb->args[0]));
969 if (cb->args[1] == 0) {
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200970 if (tcf_fill_node(net, skb, tp, q, parent, 0,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200971 NETLINK_CB(cb->skb).portid,
972 cb->nlh->nlmsg_seq, NLM_F_MULTI,
973 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200974 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200975
976 cb->args[1] = 1;
977 }
978 if (!tp->ops->walk)
979 continue;
980 arg.w.fn = tcf_node_dump;
981 arg.skb = skb;
982 arg.cb = cb;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200983 arg.q = q;
984 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200985 arg.w.stop = 0;
986 arg.w.skip = cb->args[1] - 1;
987 arg.w.count = 0;
988 tp->ops->walk(tp, &arg.w);
989 cb->args[1] = arg.w.count + 1;
990 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200991 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200992 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200993 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +0200994}
995
Eric Dumazetbd27a872009-11-05 20:57:26 -0800996/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
998{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900999 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001000 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 struct net_device *dev;
1002 struct Qdisc *q;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001003 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001004 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001005 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 unsigned long cl = 0;
Eric Dumazet20fea082007-11-14 01:44:41 -08001007 const struct Qdisc_class_ops *cops;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001008 long index_start;
1009 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001010 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001011 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Hong zhi guo573ce262013-03-27 06:47:04 +00001013 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001015
1016 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1017 if (err)
1018 return err;
1019
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001020 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1021 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return skb->len;
1023
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001024 parent = tcm->tcm_parent;
1025 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +00001026 q = dev->qdisc;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001027 parent = q->handle;
1028 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (!q)
1032 goto out;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001033 cops = q->ops->cl_ops;
1034 if (!cops)
WANG Cong143976c2017-08-24 16:51:29 -07001035 goto out;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001036 if (!cops->tcf_block)
WANG Cong143976c2017-08-24 16:51:29 -07001037 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (TC_H_MIN(tcm->tcm_parent)) {
WANG Cong143976c2017-08-24 16:51:29 -07001039 cl = cops->find(q, tcm->tcm_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (cl == 0)
WANG Cong143976c2017-08-24 16:51:29 -07001041 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Alexander Aringcbaacc42017-12-20 12:35:16 -05001043 block = cops->tcf_block(q, cl, NULL);
Jiri Pirko6529eab2017-05-17 11:07:55 +02001044 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001047 index_start = cb->args[0];
1048 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001049
1050 list_for_each_entry(chain, &block->chain_list, list) {
1051 if (tca[TCA_CHAIN] &&
1052 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1053 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001054 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1055 index_start, &index))
Jiri Pirko5bc17012017-05-17 11:08:01 +02001056 break;
1057 }
1058
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001059 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return skb->len;
1063}
1064
WANG Cong18d02642014-09-25 10:26:37 -07001065void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001068 LIST_HEAD(actions);
1069
Cong Wang2d132eb2017-10-26 18:24:40 -07001070 ASSERT_RTNL();
WANG Cong22dc13c2016-08-13 22:35:00 -07001071 tcf_exts_to_list(exts, &actions);
1072 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1073 kfree(exts->actions);
1074 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075#endif
1076}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001077EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00001079int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001080 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082#ifdef CONFIG_NET_CLS_ACT
1083 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct tc_action *act;
1085
WANG Cong5da57f42013-12-15 20:15:07 -08001086 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001087 act = tcf_action_init_1(net, tp, tb[exts->police],
1088 rate_tlv, "police", ovr,
1089 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001090 if (IS_ERR(act))
1091 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
WANG Cong33be6272013-12-15 20:15:05 -08001093 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07001094 exts->actions[0] = act;
1095 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08001096 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001097 LIST_HEAD(actions);
1098 int err, i = 0;
1099
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001100 err = tcf_action_init(net, tp, tb[exts->action],
1101 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001102 &actions);
WANG Cong33be6272013-12-15 20:15:05 -08001103 if (err)
1104 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -07001105 list_for_each_entry(act, &actions, list)
1106 exts->actions[i++] = act;
1107 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Cong Wange4b95c42017-11-06 13:47:19 -08001109 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111#else
WANG Cong5da57f42013-12-15 20:15:07 -08001112 if ((exts->action && tb[exts->action]) ||
1113 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return -EOPNOTSUPP;
1115#endif
1116
1117 return 0;
1118}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001119EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001121void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001124 struct tcf_exts old = *dst;
1125
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001126 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07001127 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#endif
1129}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001130EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
WANG Cong22dc13c2016-08-13 22:35:00 -07001132#ifdef CONFIG_NET_CLS_ACT
1133static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1134{
1135 if (exts->nr_actions == 0)
1136 return NULL;
1137 else
1138 return exts->actions[0];
1139}
1140#endif
WANG Cong33be6272013-12-15 20:15:05 -08001141
WANG Cong5da57f42013-12-15 20:15:07 -08001142int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07001145 struct nlattr *nest;
1146
Jiri Pirko978dfd82017-08-04 14:29:03 +02001147 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
1149 * again for backward compatible mode - we want
1150 * to work with both old and new modes of entering
1151 * tc data even if iproute2 was newer - jhs
1152 */
WANG Cong33be6272013-12-15 20:15:05 -08001153 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001154 LIST_HEAD(actions);
1155
WANG Cong5da57f42013-12-15 20:15:07 -08001156 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001157 if (nest == NULL)
1158 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07001159
1160 tcf_exts_to_list(exts, &actions);
1161 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001162 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001163 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08001164 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08001165 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08001166 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001167 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001168 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08001169 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001170 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001171 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001175
1176nla_put_failure:
1177 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001179#else
1180 return 0;
1181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001183EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001185
WANG Cong5da57f42013-12-15 20:15:07 -08001186int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001189 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001190 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001191 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192#endif
1193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001195EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Jiri Pirko717503b2017-10-11 09:41:09 +02001197static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1198 enum tc_setup_type type,
1199 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001200{
1201 int ok_count = 0;
1202#ifdef CONFIG_NET_CLS_ACT
1203 const struct tc_action *a;
1204 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001205 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001206
1207 if (!tcf_exts_has_actions(exts))
1208 return 0;
1209
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001210 for (i = 0; i < exts->nr_actions; i++) {
1211 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001212 if (!a->ops->get_dev)
1213 continue;
1214 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01001215 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001216 continue;
1217 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1218 if (ret < 0)
1219 return ret;
1220 ok_count += ret;
1221 }
1222#endif
1223 return ok_count;
1224}
Jiri Pirko717503b2017-10-11 09:41:09 +02001225
Jiri Pirko208c0f42017-10-19 15:50:32 +02001226int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1227 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02001228{
Jiri Pirko208c0f42017-10-19 15:50:32 +02001229 int ok_count;
1230 int ret;
1231
1232 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1233 if (ret < 0)
1234 return ret;
1235 ok_count = ret;
1236
1237 if (!exts)
1238 return ok_count;
1239 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1240 if (ret < 0)
1241 return ret;
1242 ok_count += ret;
1243
1244 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02001245}
1246EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static int __init tc_filter_init(void)
1249{
Cong Wang7aa00452017-10-26 18:24:28 -07001250 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1251 if (!tc_filter_wq)
1252 return -ENOMEM;
1253
Florian Westphalb97bac62017-08-09 20:41:48 +02001254 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1255 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
Thomas Graf82623c02007-03-22 11:56:22 -07001256 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001257 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260}
1261
1262subsys_initcall(tc_filter_init);