blob: 2f78341f28889d91590a5032d92ca463d28504b6 [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>
Jiri Pirko48617382018-01-17 11:46:46 +010027#include <linux/idr.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110028#include <net/net_namespace.h>
29#include <net/sock.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070030#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/pkt_sched.h>
32#include <net/pkt_cls.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* The list of all installed classifier types */
WANG Cong36272872013-12-15 20:15:11 -080035static LIST_HEAD(tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
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
Jiri Pirkof34e8bf2018-07-23 09:23:04 +020042static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Eric Dumazetdcd76082013-12-20 10:04:18 -080044 const struct tcf_proto_ops *t, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 if (kind) {
47 read_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080048 list_for_each_entry(t, &tcf_proto_base, head) {
Jiri Pirko33a48922017-02-09 14:38:57 +010049 if (strcmp(kind, t->kind) == 0) {
Eric Dumazetdcd76082013-12-20 10:04:18 -080050 if (try_module_get(t->owner))
51 res = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53 }
54 }
55 read_unlock(&cls_mod_lock);
56 }
Eric Dumazetdcd76082013-12-20 10:04:18 -080057 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Jiri Pirkof34e8bf2018-07-23 09:23:04 +020060static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Register(unregister) new classifier type */
88
89int register_tcf_proto_ops(struct tcf_proto_ops *ops)
90{
WANG Cong36272872013-12-15 20:15:11 -080091 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int rc = -EEXIST;
93
94 write_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080095 list_for_each_entry(t, &tcf_proto_base, head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (!strcmp(ops->kind, t->kind))
97 goto out;
98
WANG Cong36272872013-12-15 20:15:11 -080099 list_add_tail(&ops->head, &tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 rc = 0;
101out:
102 write_unlock(&cls_mod_lock);
103 return rc;
104}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800105EXPORT_SYMBOL(register_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Cong Wang7aa00452017-10-26 18:24:28 -0700107static struct workqueue_struct *tc_filter_wq;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
110{
WANG Cong36272872013-12-15 20:15:11 -0800111 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int rc = -ENOENT;
113
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200114 /* Wait for outstanding call_rcu()s, if any, from a
115 * tcf_proto_ops's destroy() handler.
116 */
117 rcu_barrier();
Cong Wang7aa00452017-10-26 18:24:28 -0700118 flush_workqueue(tc_filter_wq);
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 write_lock(&cls_mod_lock);
Eric Dumazetdcd76082013-12-20 10:04:18 -0800121 list_for_each_entry(t, &tcf_proto_base, head) {
122 if (t == ops) {
123 list_del(&t->head);
124 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
Eric Dumazetdcd76082013-12-20 10:04:18 -0800126 }
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 write_unlock(&cls_mod_lock);
129 return rc;
130}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800131EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Cong Wangaaa908f2018-05-23 15:26:53 -0700133bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
Cong Wang7aa00452017-10-26 18:24:28 -0700134{
Cong Wangaaa908f2018-05-23 15:26:53 -0700135 INIT_RCU_WORK(rwork, func);
136 return queue_rcu_work(tc_filter_wq, rwork);
Cong Wang7aa00452017-10-26 18:24:28 -0700137}
138EXPORT_SYMBOL(tcf_queue_work);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* Select new prio value from the range, managed by kernel. */
141
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800142static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800144 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000147 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jiri Pirko79619732017-05-17 11:07:58 +0200149 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Jiri Pirko33a48922017-02-09 14:38:57 +0100152static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500153 u32 prio, struct tcf_chain *chain,
154 struct netlink_ext_ack *extack)
Jiri Pirko33a48922017-02-09 14:38:57 +0100155{
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
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200163 tp->ops = tcf_proto_lookup_ops(kind, extack);
164 if (IS_ERR(tp->ops)) {
165 err = PTR_ERR(tp->ops);
Jiri Pirkod68d75f2018-05-11 17:45:32 +0200166 goto errout;
Jiri Pirko33a48922017-02-09 14:38:57 +0100167 }
168 tp->classify = tp->ops->classify;
169 tp->protocol = protocol;
170 tp->prio = prio;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200171 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100172
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
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800185static void tcf_proto_destroy(struct tcf_proto *tp,
186 struct netlink_ext_ack *extack)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100187{
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800188 tp->ops->destroy(tp, extack);
WANG Cong763dbf62017-04-19 14:21:21 -0700189 module_put(tp->ops->owner);
190 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100191}
192
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100193struct 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
Jiri Pirko5bc17012017-05-17 11:08:01 +0200199static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
200 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200201{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200202 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;
Cong Wange2ef7542017-09-11 16:33:31 -0700210 chain->refcnt = 1;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200211 if (!chain->index)
212 block->chain0.chain = chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200213 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200214}
215
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100216static 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}
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200222
223static void tcf_chain0_head_change(struct tcf_chain *chain,
224 struct tcf_proto *tp_head)
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100225{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100226 struct tcf_filter_chain_list_item *item;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200227 struct tcf_block *block = chain->block;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100228
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200229 if (chain->index)
230 return;
231 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100232 tcf_chain_head_change_item(item, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100233}
234
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200235static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100236{
Roman Kapld7aa04a2017-11-20 22:21:13 +0100237 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100238
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200239 tcf_chain0_head_change(chain, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100240 while (tp) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200241 RCU_INIT_POINTER(chain->filter_chain, tp->next);
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800242 tcf_proto_destroy(tp, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100243 tp = rtnl_dereference(chain->filter_chain);
244 tcf_chain_put(chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100245 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200246}
247
248static void tcf_chain_destroy(struct tcf_chain *chain)
249{
Cong Wangefbf7892017-12-04 10:48:18 -0800250 struct tcf_block *block = chain->block;
251
Cong Wange2ef7542017-09-11 16:33:31 -0700252 list_del(&chain->list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200253 if (!chain->index)
254 block->chain0.chain = NULL;
Cong Wange2ef7542017-09-11 16:33:31 -0700255 kfree(chain);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200256 if (list_empty(&block->chain_list) && block->refcnt == 0)
Cong Wangefbf7892017-12-04 10:48:18 -0800257 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700258}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200259
Cong Wange2ef7542017-09-11 16:33:31 -0700260static void tcf_chain_hold(struct tcf_chain *chain)
261{
262 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200263}
264
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200265static void tcf_chain_hold_by_act(struct tcf_chain *chain)
266{
267 ++chain->action_refcnt;
268}
269
270static void tcf_chain_release_by_act(struct tcf_chain *chain)
271{
272 --chain->action_refcnt;
273}
274
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200275static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200276{
277 /* In case all the references are action references, this
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200278 * chain should not be shown to the user.
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200279 */
280 return chain->refcnt == chain->action_refcnt;
281}
282
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200283static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
284 u32 chain_index)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200285{
286 struct tcf_chain *chain;
287
288 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200289 if (chain->index == chain_index)
Cong Wange2ef7542017-09-11 16:33:31 -0700290 return chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200291 }
292 return NULL;
293}
294
295static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
296 u32 seq, u16 flags, int event, bool unicast);
297
298struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
299 bool create)
300{
301 struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
302
303 if (chain) {
304 tcf_chain_hold(chain);
305 return chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200306 }
Jiri Pirko80532382017-09-06 13:14:19 +0200307
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200308 if (!create)
309 return NULL;
310 chain = tcf_chain_create(block, chain_index);
311 if (!chain)
312 return NULL;
313 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
314 RTM_NEWCHAIN, false);
315 return chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200316}
317EXPORT_SYMBOL(tcf_chain_get);
318
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200319struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
320{
321 struct tcf_chain *chain = tcf_chain_get(block, chain_index, true);
322
323 tcf_chain_hold_by_act(chain);
324 return chain;
325}
326EXPORT_SYMBOL(tcf_chain_get_by_act);
327
Jiri Pirko9f407f12018-07-23 09:23:07 +0200328static void tc_chain_tmplt_del(struct tcf_chain *chain);
329
Jiri Pirko5bc17012017-05-17 11:08:01 +0200330void tcf_chain_put(struct tcf_chain *chain)
331{
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200332 if (--chain->refcnt == 0) {
333 tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
Jiri Pirko9f407f12018-07-23 09:23:07 +0200334 tc_chain_tmplt_del(chain);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200335 tcf_chain_destroy(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200336 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200337}
338EXPORT_SYMBOL(tcf_chain_put);
339
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200340void tcf_chain_put_by_act(struct tcf_chain *chain)
341{
342 tcf_chain_release_by_act(chain);
343 tcf_chain_put(chain);
344}
345EXPORT_SYMBOL(tcf_chain_put_by_act);
346
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200347static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
348{
349 if (chain->explicitly_created)
350 tcf_chain_put(chain);
351}
352
Jiri Pirkocaa72602018-01-17 11:46:50 +0100353static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200354{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100355 return block->offloadcnt;
356}
357
358static int tcf_block_offload_cmd(struct tcf_block *block,
359 struct net_device *dev,
360 struct tcf_block_ext_info *ei,
John Hurley60513bd2018-06-25 14:30:04 -0700361 enum tc_block_command command,
362 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100363{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200364 struct tc_block_offload bo = {};
365
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200366 bo.command = command;
367 bo.binder_type = ei->binder_type;
368 bo.block = block;
John Hurley60513bd2018-06-25 14:30:04 -0700369 bo.extack = extack;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100370 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200371}
372
Jiri Pirkocaa72602018-01-17 11:46:50 +0100373static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700374 struct tcf_block_ext_info *ei,
375 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200376{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100377 struct net_device *dev = q->dev_queue->dev;
378 int err;
379
380 if (!dev->netdev_ops->ndo_setup_tc)
381 goto no_offload_dev_inc;
382
383 /* If tc offload feature is disabled and the block we try to bind
384 * to already has some offloaded filters, forbid to bind.
385 */
John Hurley60513bd2018-06-25 14:30:04 -0700386 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
387 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Jiri Pirkocaa72602018-01-17 11:46:50 +0100388 return -EOPNOTSUPP;
John Hurley60513bd2018-06-25 14:30:04 -0700389 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100390
John Hurley60513bd2018-06-25 14:30:04 -0700391 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100392 if (err == -EOPNOTSUPP)
393 goto no_offload_dev_inc;
394 return err;
395
396no_offload_dev_inc:
397 if (tcf_block_offload_in_use(block))
398 return -EOPNOTSUPP;
399 block->nooffloaddevcnt++;
400 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200401}
402
403static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
404 struct tcf_block_ext_info *ei)
405{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100406 struct net_device *dev = q->dev_queue->dev;
407 int err;
408
409 if (!dev->netdev_ops->ndo_setup_tc)
410 goto no_offload_dev_dec;
John Hurley60513bd2018-06-25 14:30:04 -0700411 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100412 if (err == -EOPNOTSUPP)
413 goto no_offload_dev_dec;
414 return;
415
416no_offload_dev_dec:
417 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200418}
419
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100420static int
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200421tcf_chain0_head_change_cb_add(struct tcf_block *block,
422 struct tcf_block_ext_info *ei,
423 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100424{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200425 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100426 struct tcf_filter_chain_list_item *item;
427
428 item = kmalloc(sizeof(*item), GFP_KERNEL);
429 if (!item) {
430 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
431 return -ENOMEM;
432 }
433 item->chain_head_change = ei->chain_head_change;
434 item->chain_head_change_priv = ei->chain_head_change_priv;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200435 if (chain0 && chain0->filter_chain)
436 tcf_chain_head_change_item(item, chain0->filter_chain);
437 list_add(&item->list, &block->chain0.filter_chain_list);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100438 return 0;
439}
440
441static void
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200442tcf_chain0_head_change_cb_del(struct tcf_block *block,
443 struct tcf_block_ext_info *ei)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100444{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200445 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100446 struct tcf_filter_chain_list_item *item;
447
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200448 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100449 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
450 (item->chain_head_change == ei->chain_head_change &&
451 item->chain_head_change_priv == ei->chain_head_change_priv)) {
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200452 if (chain0)
453 tcf_chain_head_change_item(item, NULL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100454 list_del(&item->list);
455 kfree(item);
456 return;
457 }
458 }
459 WARN_ON(1);
460}
461
Jiri Pirko48617382018-01-17 11:46:46 +0100462struct tcf_net {
463 struct idr idr;
464};
465
466static unsigned int tcf_net_id;
467
468static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100469 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100470{
Jiri Pirko48617382018-01-17 11:46:46 +0100471 struct tcf_net *tn = net_generic(net, tcf_net_id);
Jiri Pirko48617382018-01-17 11:46:46 +0100472
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100473 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
474 GFP_KERNEL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100475}
476
Jiri Pirko48617382018-01-17 11:46:46 +0100477static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200478{
Jiri Pirko48617382018-01-17 11:46:46 +0100479 struct tcf_net *tn = net_generic(net, tcf_net_id);
480
Matthew Wilcox9c160942017-11-28 09:48:43 -0500481 idr_remove(&tn->idr, block->index);
Jiri Pirko48617382018-01-17 11:46:46 +0100482}
483
484static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100485 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100486 struct netlink_ext_ack *extack)
487{
488 struct tcf_block *block;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200489
Jiri Pirko48617382018-01-17 11:46:46 +0100490 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500491 if (!block) {
492 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100493 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500494 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200495 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200496 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100497 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200498 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200499
Jiri Pirko48617382018-01-17 11:46:46 +0100500 block->refcnt = 1;
501 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100502 block->index = block_index;
503
504 /* Don't store q pointer for blocks which are shared */
505 if (!tcf_block_shared(block))
506 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100507 return block;
Jiri Pirko48617382018-01-17 11:46:46 +0100508}
509
510static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
511{
512 struct tcf_net *tn = net_generic(net, tcf_net_id);
513
Matthew Wilcox322d8842017-11-28 10:01:24 -0500514 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100515}
516
Vlad Buslovc431f892018-05-31 09:52:53 +0300517/* Find tcf block.
518 * Set q, parent, cl when appropriate.
519 */
520
521static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
522 u32 *parent, unsigned long *cl,
523 int ifindex, u32 block_index,
524 struct netlink_ext_ack *extack)
525{
526 struct tcf_block *block;
527
528 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
529 block = tcf_block_lookup(net, block_index);
530 if (!block) {
531 NL_SET_ERR_MSG(extack, "Block of given index was not found");
532 return ERR_PTR(-EINVAL);
533 }
534 } else {
535 const struct Qdisc_class_ops *cops;
536 struct net_device *dev;
537
538 /* Find link */
539 dev = __dev_get_by_index(net, ifindex);
540 if (!dev)
541 return ERR_PTR(-ENODEV);
542
543 /* Find qdisc */
544 if (!*parent) {
545 *q = dev->qdisc;
546 *parent = (*q)->handle;
547 } else {
548 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
549 if (!*q) {
550 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
551 return ERR_PTR(-EINVAL);
552 }
553 }
554
555 /* Is it classful? */
556 cops = (*q)->ops->cl_ops;
557 if (!cops) {
558 NL_SET_ERR_MSG(extack, "Qdisc not classful");
559 return ERR_PTR(-EINVAL);
560 }
561
562 if (!cops->tcf_block) {
563 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
564 return ERR_PTR(-EOPNOTSUPP);
565 }
566
567 /* Do we search for filter, attached to class? */
568 if (TC_H_MIN(*parent)) {
569 *cl = cops->find(*q, *parent);
570 if (*cl == 0) {
571 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
572 return ERR_PTR(-ENOENT);
573 }
574 }
575
576 /* And the last stroke */
577 block = cops->tcf_block(*q, *cl, extack);
578 if (!block)
579 return ERR_PTR(-EINVAL);
580 if (tcf_block_shared(block)) {
581 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
582 return ERR_PTR(-EOPNOTSUPP);
583 }
584 }
585
586 return block;
587}
588
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100589struct tcf_block_owner_item {
590 struct list_head list;
591 struct Qdisc *q;
592 enum tcf_block_binder_type binder_type;
593};
594
595static void
596tcf_block_owner_netif_keep_dst(struct tcf_block *block,
597 struct Qdisc *q,
598 enum tcf_block_binder_type binder_type)
599{
600 if (block->keep_dst &&
601 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
602 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
603 netif_keep_dst(qdisc_dev(q));
604}
605
606void tcf_block_netif_keep_dst(struct tcf_block *block)
607{
608 struct tcf_block_owner_item *item;
609
610 block->keep_dst = true;
611 list_for_each_entry(item, &block->owner_list, list)
612 tcf_block_owner_netif_keep_dst(block, item->q,
613 item->binder_type);
614}
615EXPORT_SYMBOL(tcf_block_netif_keep_dst);
616
617static int tcf_block_owner_add(struct tcf_block *block,
618 struct Qdisc *q,
619 enum tcf_block_binder_type binder_type)
620{
621 struct tcf_block_owner_item *item;
622
623 item = kmalloc(sizeof(*item), GFP_KERNEL);
624 if (!item)
625 return -ENOMEM;
626 item->q = q;
627 item->binder_type = binder_type;
628 list_add(&item->list, &block->owner_list);
629 return 0;
630}
631
632static void tcf_block_owner_del(struct tcf_block *block,
633 struct Qdisc *q,
634 enum tcf_block_binder_type binder_type)
635{
636 struct tcf_block_owner_item *item;
637
638 list_for_each_entry(item, &block->owner_list, list) {
639 if (item->q == q && item->binder_type == binder_type) {
640 list_del(&item->list);
641 kfree(item);
642 return;
643 }
644 }
645 WARN_ON(1);
646}
647
Jiri Pirko48617382018-01-17 11:46:46 +0100648int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
649 struct tcf_block_ext_info *ei,
650 struct netlink_ext_ack *extack)
651{
652 struct net *net = qdisc_net(q);
653 struct tcf_block *block = NULL;
654 bool created = false;
655 int err;
656
657 if (ei->block_index) {
658 /* block_index not 0 means the shared block is requested */
659 block = tcf_block_lookup(net, ei->block_index);
660 if (block)
661 block->refcnt++;
662 }
663
664 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100665 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100666 if (IS_ERR(block))
667 return PTR_ERR(block);
668 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100669 if (tcf_block_shared(block)) {
670 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100671 if (err)
672 goto err_block_insert;
673 }
674 }
675
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100676 err = tcf_block_owner_add(block, q, ei->binder_type);
677 if (err)
678 goto err_block_owner_add;
679
680 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
681
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200682 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100683 if (err)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200684 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100685
John Hurley60513bd2018-06-25 14:30:04 -0700686 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100687 if (err)
688 goto err_block_offload_bind;
689
Jiri Pirko6529eab2017-05-17 11:07:55 +0200690 *p_block = block;
691 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200692
Jiri Pirkocaa72602018-01-17 11:46:50 +0100693err_block_offload_bind:
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200694 tcf_chain0_head_change_cb_del(block, ei);
695err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100696 tcf_block_owner_del(block, q, ei->binder_type);
697err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100698 if (created) {
699 if (tcf_block_shared(block))
700 tcf_block_remove(block, net);
701err_block_insert:
Jiri Pirko48617382018-01-17 11:46:46 +0100702 kfree(block);
703 } else {
704 block->refcnt--;
705 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200706 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200707}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200708EXPORT_SYMBOL(tcf_block_get_ext);
709
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100710static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
711{
712 struct tcf_proto __rcu **p_filter_chain = priv;
713
714 rcu_assign_pointer(*p_filter_chain, tp_head);
715}
716
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200717int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500718 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
719 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200720{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100721 struct tcf_block_ext_info ei = {
722 .chain_head_change = tcf_chain_head_change_dflt,
723 .chain_head_change_priv = p_filter_chain,
724 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200725
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100726 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500727 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200728}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200729EXPORT_SYMBOL(tcf_block_get);
730
Cong Wang7aa00452017-10-26 18:24:28 -0700731/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100732 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700733 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100734void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900735 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700736{
Cong Wangefbf7892017-12-04 10:48:18 -0800737 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700738
David S. Millerc30abd52017-12-16 22:11:55 -0500739 if (!block)
740 return;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200741 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100742 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100743
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200744 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100745 if (tcf_block_shared(block))
746 tcf_block_remove(block, block->net);
747
748 /* Hold a refcnt for all chains, so that they don't disappear
749 * while we are iterating.
750 */
751 list_for_each_entry(chain, &block->chain_list, list)
752 tcf_chain_hold(chain);
753
754 list_for_each_entry(chain, &block->chain_list, list)
755 tcf_chain_flush(chain);
756 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700757
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100758 tcf_block_offload_unbind(block, q, ei);
759
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200760 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100761 /* At this point, all the chains should have refcnt >= 1. */
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200762 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
763 tcf_chain_put_explicitly_created(chain);
Jiri Pirko48617382018-01-17 11:46:46 +0100764 tcf_chain_put(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200765 }
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100766
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200767 block->refcnt--;
768 if (list_empty(&block->chain_list))
769 kfree(block);
Jiri Pirko48617382018-01-17 11:46:46 +0100770 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200771}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200772EXPORT_SYMBOL(tcf_block_put_ext);
773
774void tcf_block_put(struct tcf_block *block)
775{
776 struct tcf_block_ext_info ei = {0, };
777
Jiri Pirko4853f122017-12-21 13:13:59 +0100778 if (!block)
779 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100780 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200781}
David S. Millere1ea2f92017-10-30 14:10:01 +0900782
Jiri Pirko6529eab2017-05-17 11:07:55 +0200783EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100784
Jiri Pirkoacb67442017-10-19 15:50:31 +0200785struct tcf_block_cb {
786 struct list_head list;
787 tc_setup_cb_t *cb;
788 void *cb_ident;
789 void *cb_priv;
790 unsigned int refcnt;
791};
792
793void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
794{
795 return block_cb->cb_priv;
796}
797EXPORT_SYMBOL(tcf_block_cb_priv);
798
799struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
800 tc_setup_cb_t *cb, void *cb_ident)
801{ struct tcf_block_cb *block_cb;
802
803 list_for_each_entry(block_cb, &block->cb_list, list)
804 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
805 return block_cb;
806 return NULL;
807}
808EXPORT_SYMBOL(tcf_block_cb_lookup);
809
810void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
811{
812 block_cb->refcnt++;
813}
814EXPORT_SYMBOL(tcf_block_cb_incref);
815
816unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
817{
818 return --block_cb->refcnt;
819}
820EXPORT_SYMBOL(tcf_block_cb_decref);
821
John Hurley32636742018-06-25 14:30:10 -0700822static int
823tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
824 void *cb_priv, bool add, bool offload_in_use,
825 struct netlink_ext_ack *extack)
826{
827 struct tcf_chain *chain;
828 struct tcf_proto *tp;
829 int err;
830
831 list_for_each_entry(chain, &block->chain_list, list) {
832 for (tp = rtnl_dereference(chain->filter_chain); tp;
833 tp = rtnl_dereference(tp->next)) {
834 if (tp->ops->reoffload) {
835 err = tp->ops->reoffload(tp, add, cb, cb_priv,
836 extack);
837 if (err && add)
838 goto err_playback_remove;
839 } else if (add && offload_in_use) {
840 err = -EOPNOTSUPP;
841 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
842 goto err_playback_remove;
843 }
844 }
845 }
846
847 return 0;
848
849err_playback_remove:
850 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
851 extack);
852 return err;
853}
854
Jiri Pirkoacb67442017-10-19 15:50:31 +0200855struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
856 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700857 void *cb_priv,
858 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200859{
860 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700861 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200862
John Hurley32636742018-06-25 14:30:10 -0700863 /* Replay any already present rules */
864 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
865 tcf_block_offload_in_use(block),
866 extack);
867 if (err)
868 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100869
Jiri Pirkoacb67442017-10-19 15:50:31 +0200870 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
871 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100872 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200873 block_cb->cb = cb;
874 block_cb->cb_ident = cb_ident;
875 block_cb->cb_priv = cb_priv;
876 list_add(&block_cb->list, &block->cb_list);
877 return block_cb;
878}
879EXPORT_SYMBOL(__tcf_block_cb_register);
880
881int tcf_block_cb_register(struct tcf_block *block,
882 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700883 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200884{
885 struct tcf_block_cb *block_cb;
886
John Hurley60513bd2018-06-25 14:30:04 -0700887 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
888 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500889 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200890}
891EXPORT_SYMBOL(tcf_block_cb_register);
892
John Hurley32636742018-06-25 14:30:10 -0700893void __tcf_block_cb_unregister(struct tcf_block *block,
894 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200895{
John Hurley32636742018-06-25 14:30:10 -0700896 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
897 false, tcf_block_offload_in_use(block),
898 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200899 list_del(&block_cb->list);
900 kfree(block_cb);
901}
902EXPORT_SYMBOL(__tcf_block_cb_unregister);
903
904void tcf_block_cb_unregister(struct tcf_block *block,
905 tc_setup_cb_t *cb, void *cb_ident)
906{
907 struct tcf_block_cb *block_cb;
908
909 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
910 if (!block_cb)
911 return;
John Hurley32636742018-06-25 14:30:10 -0700912 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200913}
914EXPORT_SYMBOL(tcf_block_cb_unregister);
915
916static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
917 void *type_data, bool err_stop)
918{
919 struct tcf_block_cb *block_cb;
920 int ok_count = 0;
921 int err;
922
David S. Miller9a99dc12018-06-06 13:55:47 -0400923 /* Make sure all netdevs sharing this block are offload-capable. */
924 if (block->nooffloaddevcnt && err_stop)
925 return -EOPNOTSUPP;
926
Jiri Pirkoacb67442017-10-19 15:50:31 +0200927 list_for_each_entry(block_cb, &block->cb_list, list) {
928 err = block_cb->cb(type, type_data, block_cb->cb_priv);
929 if (err) {
930 if (err_stop)
931 return err;
932 } else {
933 ok_count++;
934 }
935 }
936 return ok_count;
937}
938
Jiri Pirko87d83092017-05-17 11:07:54 +0200939/* Main classifier routine: scans classifier chain attached
940 * to this qdisc, (optionally) tests for protocol and asks
941 * specific classifiers.
942 */
943int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
944 struct tcf_result *res, bool compat_mode)
945{
946 __be16 protocol = tc_skb_protocol(skb);
947#ifdef CONFIG_NET_CLS_ACT
948 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200949 const struct tcf_proto *orig_tp = tp;
950 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200951 int limit = 0;
952
953reclassify:
954#endif
955 for (; tp; tp = rcu_dereference_bh(tp->next)) {
956 int err;
957
958 if (tp->protocol != protocol &&
959 tp->protocol != htons(ETH_P_ALL))
960 continue;
961
962 err = tp->classify(skb, tp, res);
963#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200964 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200965 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200966 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200967 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200968 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200969 goto reset;
970 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200971#endif
972 if (err >= 0)
973 return err;
974 }
975
976 return TC_ACT_UNSPEC; /* signal: continue lookup */
977#ifdef CONFIG_NET_CLS_ACT
978reset:
979 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100980 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
981 tp->chain->block->index,
982 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200983 ntohs(tp->protocol));
984 return TC_ACT_SHOT;
985 }
986
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200987 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200988 protocol = tc_skb_protocol(skb);
989 goto reclassify;
990#endif
991}
992EXPORT_SYMBOL(tcf_classify);
993
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200994struct tcf_chain_info {
995 struct tcf_proto __rcu **pprev;
996 struct tcf_proto __rcu *next;
997};
998
999static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1000{
1001 return rtnl_dereference(*chain_info->pprev);
1002}
1003
1004static void tcf_chain_tp_insert(struct tcf_chain *chain,
1005 struct tcf_chain_info *chain_info,
1006 struct tcf_proto *tp)
1007{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001008 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001009 tcf_chain0_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001010 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1011 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -07001012 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001013}
1014
1015static void tcf_chain_tp_remove(struct tcf_chain *chain,
1016 struct tcf_chain_info *chain_info,
1017 struct tcf_proto *tp)
1018{
1019 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1020
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001021 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001022 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001023 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -07001024 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001025}
1026
1027static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1028 struct tcf_chain_info *chain_info,
1029 u32 protocol, u32 prio,
1030 bool prio_allocate)
1031{
1032 struct tcf_proto **pprev;
1033 struct tcf_proto *tp;
1034
1035 /* Check the chain for existence of proto-tcf with this priority */
1036 for (pprev = &chain->filter_chain;
1037 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1038 if (tp->prio >= prio) {
1039 if (tp->prio == prio) {
1040 if (prio_allocate ||
1041 (tp->protocol != protocol && protocol))
1042 return ERR_PTR(-EINVAL);
1043 } else {
1044 tp = NULL;
1045 }
1046 break;
1047 }
1048 }
1049 chain_info->pprev = pprev;
1050 chain_info->next = tp ? tp->next : NULL;
1051 return tp;
1052}
1053
WANG Cong71203712017-08-07 15:26:50 -07001054static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001055 struct tcf_proto *tp, struct tcf_block *block,
1056 struct Qdisc *q, u32 parent, void *fh,
1057 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -07001058{
1059 struct tcmsg *tcm;
1060 struct nlmsghdr *nlh;
1061 unsigned char *b = skb_tail_pointer(skb);
1062
1063 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1064 if (!nlh)
1065 goto out_nlmsg_trim;
1066 tcm = nlmsg_data(nlh);
1067 tcm->tcm_family = AF_UNSPEC;
1068 tcm->tcm__pad1 = 0;
1069 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001070 if (q) {
1071 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1072 tcm->tcm_parent = parent;
1073 } else {
1074 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1075 tcm->tcm_block_index = block->index;
1076 }
WANG Cong71203712017-08-07 15:26:50 -07001077 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1078 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1079 goto nla_put_failure;
1080 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1081 goto nla_put_failure;
1082 if (!fh) {
1083 tcm->tcm_handle = 0;
1084 } else {
1085 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1086 goto nla_put_failure;
1087 }
1088 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1089 return skb->len;
1090
1091out_nlmsg_trim:
1092nla_put_failure:
1093 nlmsg_trim(skb, b);
1094 return -1;
1095}
1096
1097static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1098 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001099 struct tcf_block *block, struct Qdisc *q,
1100 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001101{
1102 struct sk_buff *skb;
1103 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1104
1105 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1106 if (!skb)
1107 return -ENOBUFS;
1108
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001109 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1110 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001111 kfree_skb(skb);
1112 return -EINVAL;
1113 }
1114
1115 if (unicast)
1116 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1117
1118 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1119 n->nlmsg_flags & NLM_F_ECHO);
1120}
1121
1122static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1123 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001124 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001125 u32 parent, void *fh, bool unicast, bool *last,
1126 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001127{
1128 struct sk_buff *skb;
1129 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1130 int err;
1131
1132 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1133 if (!skb)
1134 return -ENOBUFS;
1135
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001136 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1137 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001138 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001139 kfree_skb(skb);
1140 return -EINVAL;
1141 }
1142
Alexander Aring571acf22018-01-18 11:20:53 -05001143 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001144 if (err) {
1145 kfree_skb(skb);
1146 return err;
1147 }
1148
1149 if (unicast)
1150 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1151
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001152 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1153 n->nlmsg_flags & NLM_F_ECHO);
1154 if (err < 0)
1155 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1156 return err;
WANG Cong71203712017-08-07 15:26:50 -07001157}
1158
1159static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001160 struct tcf_block *block, struct Qdisc *q,
1161 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001162 struct tcf_chain *chain, int event)
1163{
1164 struct tcf_proto *tp;
1165
1166 for (tp = rtnl_dereference(chain->filter_chain);
1167 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001168 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001169 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001170}
1171
Vlad Buslovc431f892018-05-31 09:52:53 +03001172static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001173 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001175 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001176 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 struct tcmsg *t;
1178 u32 protocol;
1179 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001180 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001182 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001183 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001184 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001185 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001186 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001189 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001191 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Vlad Buslovc431f892018-05-31 09:52:53 +03001193 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001194 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001197 tp_created = 0;
1198
David Ahernc21ef3e2017-04-16 09:48:24 -07001199 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001200 if (err < 0)
1201 return err;
1202
David S. Miller942b8162012-06-26 21:48:50 -07001203 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 protocol = TC_H_MIN(t->tcm_info);
1205 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001206 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 parent = t->tcm_parent;
1208 cl = 0;
1209
1210 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001211 /* If no priority is provided by the user,
1212 * we allocate one.
1213 */
1214 if (n->nlmsg_flags & NLM_F_CREATE) {
1215 prio = TC_H_MAKE(0x80000000U, 0U);
1216 prio_allocate = true;
1217 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001218 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 /* Find head of filter chain. */
1224
Vlad Buslovc431f892018-05-31 09:52:53 +03001225 block = tcf_block_find(net, &q, &parent, &cl,
1226 t->tcm_ifindex, t->tcm_block_index, extack);
1227 if (IS_ERR(block)) {
1228 err = PTR_ERR(block);
1229 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001230 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001231
1232 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1233 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001234 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001235 err = -EINVAL;
1236 goto errout;
1237 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001238 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001239 if (!chain) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001240 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001241 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001242 goto errout;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001245 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1246 prio, prio_allocate);
1247 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001248 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001249 err = PTR_ERR(tp);
1250 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252
1253 if (tp == NULL) {
1254 /* Proto-tcf does not exist, create new one */
1255
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001256 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001257 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001258 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Vlad Buslovc431f892018-05-31 09:52:53 +03001262 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001263 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001264 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001268 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001269 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Jiri Pirko33a48922017-02-09 14:38:57 +01001271 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001272 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001273 if (IS_ERR(tp)) {
1274 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 goto errout;
1276 }
Minoru Usui12186be2009-06-02 02:17:34 -07001277 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001278 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001279 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001280 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 fh = tp->ops->get(tp, t->tcm_handle);
1285
WANG Cong8113c092017-08-04 21:31:43 -07001286 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001287 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001288 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001289 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001291 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001292 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1293 NL_SET_ERR_MSG(extack, "Filter already exists");
1294 err = -EEXIST;
1295 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
Jiri Pirko9f407f12018-07-23 09:23:07 +02001298 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1299 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1300 err = -EINVAL;
1301 goto errout;
1302 }
1303
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001304 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001305 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1306 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001307 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001308 if (tp_created)
1309 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001310 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001311 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001312 } else {
1313 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001314 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001318 if (chain)
1319 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (err == -EAGAIN)
1321 /* Replay the request. */
1322 goto replay;
1323 return err;
1324}
1325
Vlad Buslovc431f892018-05-31 09:52:53 +03001326static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1327 struct netlink_ext_ack *extack)
1328{
1329 struct net *net = sock_net(skb->sk);
1330 struct nlattr *tca[TCA_MAX + 1];
1331 struct tcmsg *t;
1332 u32 protocol;
1333 u32 prio;
1334 u32 parent;
1335 u32 chain_index;
1336 struct Qdisc *q = NULL;
1337 struct tcf_chain_info chain_info;
1338 struct tcf_chain *chain = NULL;
1339 struct tcf_block *block;
1340 struct tcf_proto *tp = NULL;
1341 unsigned long cl = 0;
1342 void *fh = NULL;
1343 int err;
1344
1345 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1346 return -EPERM;
1347
1348 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1349 if (err < 0)
1350 return err;
1351
1352 t = nlmsg_data(n);
1353 protocol = TC_H_MIN(t->tcm_info);
1354 prio = TC_H_MAJ(t->tcm_info);
1355 parent = t->tcm_parent;
1356
1357 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1358 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1359 return -ENOENT;
1360 }
1361
1362 /* Find head of filter chain. */
1363
1364 block = tcf_block_find(net, &q, &parent, &cl,
1365 t->tcm_ifindex, t->tcm_block_index, extack);
1366 if (IS_ERR(block)) {
1367 err = PTR_ERR(block);
1368 goto errout;
1369 }
1370
1371 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1372 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1373 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1374 err = -EINVAL;
1375 goto errout;
1376 }
1377 chain = tcf_chain_get(block, chain_index, false);
1378 if (!chain) {
1379 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1380 err = -EINVAL;
1381 goto errout;
1382 }
1383
1384 if (prio == 0) {
1385 tfilter_notify_chain(net, skb, block, q, parent, n,
1386 chain, RTM_DELTFILTER);
1387 tcf_chain_flush(chain);
1388 err = 0;
1389 goto errout;
1390 }
1391
1392 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1393 prio, false);
1394 if (!tp || IS_ERR(tp)) {
1395 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001396 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001397 goto errout;
1398 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1399 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1400 err = -EINVAL;
1401 goto errout;
1402 }
1403
1404 fh = tp->ops->get(tp, t->tcm_handle);
1405
1406 if (!fh) {
1407 if (t->tcm_handle == 0) {
1408 tcf_chain_tp_remove(chain, &chain_info, tp);
1409 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1410 RTM_DELTFILTER, false);
1411 tcf_proto_destroy(tp, extack);
1412 err = 0;
1413 } else {
1414 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1415 err = -ENOENT;
1416 }
1417 } else {
1418 bool last;
1419
1420 err = tfilter_del_notify(net, skb, n, tp, block,
1421 q, parent, fh, false, &last,
1422 extack);
1423 if (err)
1424 goto errout;
1425 if (last) {
1426 tcf_chain_tp_remove(chain, &chain_info, tp);
1427 tcf_proto_destroy(tp, extack);
1428 }
1429 }
1430
1431errout:
1432 if (chain)
1433 tcf_chain_put(chain);
1434 return err;
1435}
1436
1437static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1438 struct netlink_ext_ack *extack)
1439{
1440 struct net *net = sock_net(skb->sk);
1441 struct nlattr *tca[TCA_MAX + 1];
1442 struct tcmsg *t;
1443 u32 protocol;
1444 u32 prio;
1445 u32 parent;
1446 u32 chain_index;
1447 struct Qdisc *q = NULL;
1448 struct tcf_chain_info chain_info;
1449 struct tcf_chain *chain = NULL;
1450 struct tcf_block *block;
1451 struct tcf_proto *tp = NULL;
1452 unsigned long cl = 0;
1453 void *fh = NULL;
1454 int err;
1455
1456 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1457 if (err < 0)
1458 return err;
1459
1460 t = nlmsg_data(n);
1461 protocol = TC_H_MIN(t->tcm_info);
1462 prio = TC_H_MAJ(t->tcm_info);
1463 parent = t->tcm_parent;
1464
1465 if (prio == 0) {
1466 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1467 return -ENOENT;
1468 }
1469
1470 /* Find head of filter chain. */
1471
1472 block = tcf_block_find(net, &q, &parent, &cl,
1473 t->tcm_ifindex, t->tcm_block_index, extack);
1474 if (IS_ERR(block)) {
1475 err = PTR_ERR(block);
1476 goto errout;
1477 }
1478
1479 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1480 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1481 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1482 err = -EINVAL;
1483 goto errout;
1484 }
1485 chain = tcf_chain_get(block, chain_index, false);
1486 if (!chain) {
1487 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1488 err = -EINVAL;
1489 goto errout;
1490 }
1491
1492 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1493 prio, false);
1494 if (!tp || IS_ERR(tp)) {
1495 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001496 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001497 goto errout;
1498 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1499 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1500 err = -EINVAL;
1501 goto errout;
1502 }
1503
1504 fh = tp->ops->get(tp, t->tcm_handle);
1505
1506 if (!fh) {
1507 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1508 err = -ENOENT;
1509 } else {
1510 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1511 fh, RTM_NEWTFILTER, true);
1512 if (err < 0)
1513 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1514 }
1515
1516errout:
1517 if (chain)
1518 tcf_chain_put(chain);
1519 return err;
1520}
1521
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001522struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct tcf_walker w;
1524 struct sk_buff *skb;
1525 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001526 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001527 struct Qdisc *q;
1528 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529};
1530
WANG Cong8113c092017-08-04 21:31:43 -07001531static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001533 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001534 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001536 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001537 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001538 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1539 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001542static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1543 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001544 long index_start, long *p_index)
1545{
1546 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001547 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001548 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1549 struct tcf_dump_args arg;
1550 struct tcf_proto *tp;
1551
1552 for (tp = rtnl_dereference(chain->filter_chain);
1553 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1554 if (*p_index < index_start)
1555 continue;
1556 if (TC_H_MAJ(tcm->tcm_info) &&
1557 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1558 continue;
1559 if (TC_H_MIN(tcm->tcm_info) &&
1560 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1561 continue;
1562 if (*p_index > index_start)
1563 memset(&cb->args[1], 0,
1564 sizeof(cb->args) - sizeof(cb->args[0]));
1565 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001566 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001567 NETLINK_CB(cb->skb).portid,
1568 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1569 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001570 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001571
1572 cb->args[1] = 1;
1573 }
1574 if (!tp->ops->walk)
1575 continue;
1576 arg.w.fn = tcf_node_dump;
1577 arg.skb = skb;
1578 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001579 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001580 arg.q = q;
1581 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001582 arg.w.stop = 0;
1583 arg.w.skip = cb->args[1] - 1;
1584 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001585 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001586 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001587 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001588 cb->args[1] = arg.w.count + 1;
1589 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001590 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001591 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001592 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001593}
1594
Eric Dumazetbd27a872009-11-05 20:57:26 -08001595/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1597{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001598 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001599 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001600 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001601 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001602 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001603 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001604 long index_start;
1605 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001606 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001607 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Hong zhi guo573ce262013-03-27 06:47:04 +00001609 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001611
1612 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1613 if (err)
1614 return err;
1615
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001616 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1617 block = tcf_block_lookup(net, tcm->tcm_block_index);
1618 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001619 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001620 /* If we work with block index, q is NULL and parent value
1621 * will never be used in the following code. The check
1622 * in tcf_fill_node prevents it. However, compiler does not
1623 * see that far, so set parent to zero to silence the warning
1624 * about parent being uninitialized.
1625 */
1626 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001627 } else {
1628 const struct Qdisc_class_ops *cops;
1629 struct net_device *dev;
1630 unsigned long cl = 0;
1631
1632 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1633 if (!dev)
1634 return skb->len;
1635
1636 parent = tcm->tcm_parent;
1637 if (!parent) {
1638 q = dev->qdisc;
1639 parent = q->handle;
1640 } else {
1641 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1642 }
1643 if (!q)
1644 goto out;
1645 cops = q->ops->cl_ops;
1646 if (!cops)
1647 goto out;
1648 if (!cops->tcf_block)
1649 goto out;
1650 if (TC_H_MIN(tcm->tcm_parent)) {
1651 cl = cops->find(q, tcm->tcm_parent);
1652 if (cl == 0)
1653 goto out;
1654 }
1655 block = cops->tcf_block(q, cl, NULL);
1656 if (!block)
1657 goto out;
1658 if (tcf_block_shared(block))
1659 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001662 index_start = cb->args[0];
1663 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001664
1665 list_for_each_entry(chain, &block->chain_list, list) {
1666 if (tca[TCA_CHAIN] &&
1667 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1668 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001669 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001670 index_start, &index)) {
1671 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001672 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001673 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001674 }
1675
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001676 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001679 /* If we did no progress, the error (EMSGSIZE) is real */
1680 if (skb->len == 0 && err)
1681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return skb->len;
1683}
1684
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001685static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1686 struct sk_buff *skb, struct tcf_block *block,
1687 u32 portid, u32 seq, u16 flags, int event)
1688{
1689 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02001690 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001691 struct nlmsghdr *nlh;
1692 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02001693 void *priv;
1694
1695 ops = chain->tmplt_ops;
1696 priv = chain->tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001697
1698 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1699 if (!nlh)
1700 goto out_nlmsg_trim;
1701 tcm = nlmsg_data(nlh);
1702 tcm->tcm_family = AF_UNSPEC;
1703 tcm->tcm__pad1 = 0;
1704 tcm->tcm__pad2 = 0;
1705 tcm->tcm_handle = 0;
1706 if (block->q) {
1707 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1708 tcm->tcm_parent = block->q->handle;
1709 } else {
1710 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1711 tcm->tcm_block_index = block->index;
1712 }
1713
1714 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1715 goto nla_put_failure;
1716
Jiri Pirko9f407f12018-07-23 09:23:07 +02001717 if (ops) {
1718 if (nla_put_string(skb, TCA_KIND, ops->kind))
1719 goto nla_put_failure;
1720 if (ops->tmplt_dump(skb, net, priv) < 0)
1721 goto nla_put_failure;
1722 }
1723
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001724 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1725 return skb->len;
1726
1727out_nlmsg_trim:
1728nla_put_failure:
1729 nlmsg_trim(skb, b);
1730 return -EMSGSIZE;
1731}
1732
1733static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1734 u32 seq, u16 flags, int event, bool unicast)
1735{
1736 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1737 struct tcf_block *block = chain->block;
1738 struct net *net = block->net;
1739 struct sk_buff *skb;
1740
1741 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1742 if (!skb)
1743 return -ENOBUFS;
1744
1745 if (tc_chain_fill_node(chain, net, skb, block, portid,
1746 seq, flags, event) <= 0) {
1747 kfree_skb(skb);
1748 return -EINVAL;
1749 }
1750
1751 if (unicast)
1752 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1753
1754 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1755}
1756
Jiri Pirko9f407f12018-07-23 09:23:07 +02001757static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1758 struct nlattr **tca,
1759 struct netlink_ext_ack *extack)
1760{
1761 const struct tcf_proto_ops *ops;
1762 void *tmplt_priv;
1763
1764 /* If kind is not set, user did not specify template. */
1765 if (!tca[TCA_KIND])
1766 return 0;
1767
1768 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1769 if (IS_ERR(ops))
1770 return PTR_ERR(ops);
1771 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1772 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1773 return -EOPNOTSUPP;
1774 }
1775
1776 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1777 if (IS_ERR(tmplt_priv)) {
1778 module_put(ops->owner);
1779 return PTR_ERR(tmplt_priv);
1780 }
1781 chain->tmplt_ops = ops;
1782 chain->tmplt_priv = tmplt_priv;
1783 return 0;
1784}
1785
1786static void tc_chain_tmplt_del(struct tcf_chain *chain)
1787{
1788 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1789
1790 /* If template ops are set, no work to do for us. */
1791 if (!ops)
1792 return;
1793
1794 ops->tmplt_destroy(chain->tmplt_priv);
1795 module_put(ops->owner);
1796}
1797
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001798/* Add/delete/get a chain */
1799
1800static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1801 struct netlink_ext_ack *extack)
1802{
1803 struct net *net = sock_net(skb->sk);
1804 struct nlattr *tca[TCA_MAX + 1];
1805 struct tcmsg *t;
1806 u32 parent;
1807 u32 chain_index;
1808 struct Qdisc *q = NULL;
1809 struct tcf_chain *chain = NULL;
1810 struct tcf_block *block;
1811 unsigned long cl;
1812 int err;
1813
1814 if (n->nlmsg_type != RTM_GETCHAIN &&
1815 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1816 return -EPERM;
1817
1818replay:
1819 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1820 if (err < 0)
1821 return err;
1822
1823 t = nlmsg_data(n);
1824 parent = t->tcm_parent;
1825 cl = 0;
1826
1827 block = tcf_block_find(net, &q, &parent, &cl,
1828 t->tcm_ifindex, t->tcm_block_index, extack);
1829 if (IS_ERR(block))
1830 return PTR_ERR(block);
1831
1832 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1833 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1834 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1835 return -EINVAL;
1836 }
1837 chain = tcf_chain_lookup(block, chain_index);
1838 if (n->nlmsg_type == RTM_NEWCHAIN) {
1839 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001840 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001841 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001842 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001843 */
1844 tcf_chain_hold(chain);
1845 } else {
1846 NL_SET_ERR_MSG(extack, "Filter chain already exists");
1847 return -EEXIST;
1848 }
1849 } else {
1850 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1851 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
1852 return -ENOENT;
1853 }
1854 chain = tcf_chain_create(block, chain_index);
1855 if (!chain) {
1856 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
1857 return -ENOMEM;
1858 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001859 }
1860 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001861 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001862 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1863 return -EINVAL;
1864 }
1865 tcf_chain_hold(chain);
1866 }
1867
1868 switch (n->nlmsg_type) {
1869 case RTM_NEWCHAIN:
Jiri Pirko9f407f12018-07-23 09:23:07 +02001870 err = tc_chain_tmplt_add(chain, net, tca, extack);
1871 if (err)
1872 goto errout;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001873 /* In case the chain was successfully added, take a reference
1874 * to the chain. This ensures that an empty chain
1875 * does not disappear at the end of this function.
1876 */
1877 tcf_chain_hold(chain);
1878 chain->explicitly_created = true;
1879 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1880 RTM_NEWCHAIN, false);
1881 break;
1882 case RTM_DELCHAIN:
1883 /* Flush the chain first as the user requested chain removal. */
1884 tcf_chain_flush(chain);
1885 /* In case the chain was successfully deleted, put a reference
1886 * to the chain previously taken during addition.
1887 */
1888 tcf_chain_put_explicitly_created(chain);
Jiri Pirkoc921d7d2018-07-26 18:27:58 +02001889 chain->explicitly_created = false;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001890 break;
1891 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001892 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1893 n->nlmsg_seq, n->nlmsg_type, true);
1894 if (err < 0)
1895 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1896 break;
1897 default:
1898 err = -EOPNOTSUPP;
1899 NL_SET_ERR_MSG(extack, "Unsupported message type");
1900 goto errout;
1901 }
1902
1903errout:
1904 tcf_chain_put(chain);
1905 if (err == -EAGAIN)
1906 /* Replay the request. */
1907 goto replay;
1908 return err;
1909}
1910
1911/* called with RTNL */
1912static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
1913{
1914 struct net *net = sock_net(skb->sk);
1915 struct nlattr *tca[TCA_MAX + 1];
1916 struct Qdisc *q = NULL;
1917 struct tcf_block *block;
1918 struct tcf_chain *chain;
1919 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1920 long index_start;
1921 long index;
1922 u32 parent;
1923 int err;
1924
1925 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1926 return skb->len;
1927
1928 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1929 if (err)
1930 return err;
1931
1932 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1933 block = tcf_block_lookup(net, tcm->tcm_block_index);
1934 if (!block)
1935 goto out;
1936 /* If we work with block index, q is NULL and parent value
1937 * will never be used in the following code. The check
1938 * in tcf_fill_node prevents it. However, compiler does not
1939 * see that far, so set parent to zero to silence the warning
1940 * about parent being uninitialized.
1941 */
1942 parent = 0;
1943 } else {
1944 const struct Qdisc_class_ops *cops;
1945 struct net_device *dev;
1946 unsigned long cl = 0;
1947
1948 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1949 if (!dev)
1950 return skb->len;
1951
1952 parent = tcm->tcm_parent;
1953 if (!parent) {
1954 q = dev->qdisc;
1955 parent = q->handle;
1956 } else {
1957 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1958 }
1959 if (!q)
1960 goto out;
1961 cops = q->ops->cl_ops;
1962 if (!cops)
1963 goto out;
1964 if (!cops->tcf_block)
1965 goto out;
1966 if (TC_H_MIN(tcm->tcm_parent)) {
1967 cl = cops->find(q, tcm->tcm_parent);
1968 if (cl == 0)
1969 goto out;
1970 }
1971 block = cops->tcf_block(q, cl, NULL);
1972 if (!block)
1973 goto out;
1974 if (tcf_block_shared(block))
1975 q = NULL;
1976 }
1977
1978 index_start = cb->args[0];
1979 index = 0;
1980
1981 list_for_each_entry(chain, &block->chain_list, list) {
1982 if ((tca[TCA_CHAIN] &&
1983 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
1984 continue;
1985 if (index < index_start) {
1986 index++;
1987 continue;
1988 }
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001989 if (tcf_chain_held_by_acts_only(chain))
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001990 continue;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001991 err = tc_chain_fill_node(chain, net, skb, block,
1992 NETLINK_CB(cb->skb).portid,
1993 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1994 RTM_NEWCHAIN);
1995 if (err <= 0)
1996 break;
1997 index++;
1998 }
1999
2000 cb->args[0] = index;
2001
2002out:
2003 /* If we did no progress, the error (EMSGSIZE) is real */
2004 if (skb->len == 0 && err)
2005 return err;
2006 return skb->len;
2007}
2008
WANG Cong18d02642014-09-25 10:26:37 -07002009void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011#ifdef CONFIG_NET_CLS_ACT
Vlad Buslov90b73b72018-07-05 17:24:33 +03002012 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
WANG Cong22dc13c2016-08-13 22:35:00 -07002013 kfree(exts->actions);
2014 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015#endif
2016}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002017EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00002019int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05002020 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2021 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023#ifdef CONFIG_NET_CLS_ACT
2024 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05002026 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
WANG Cong5da57f42013-12-15 20:15:07 -08002028 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002029 act = tcf_action_init_1(net, tp, tb[exts->police],
2030 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03002031 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08002032 if (IS_ERR(act))
2033 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
WANG Cong33be6272013-12-15 20:15:05 -08002035 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07002036 exts->actions[0] = act;
2037 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08002038 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03002039 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07002040
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002041 err = tcf_action_init(net, tp, tb[exts->action],
2042 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03002043 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03002044 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03002045 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002046 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03002047 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
Cong Wange4b95c42017-11-06 13:47:19 -08002049 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051#else
WANG Cong5da57f42013-12-15 20:15:07 -08002052 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05002053 (exts->police && tb[exts->police])) {
2054 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05002056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057#endif
2058
2059 return 0;
2060}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002061EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002063void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07002066 struct tcf_exts old = *dst;
2067
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002068 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07002069 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070#endif
2071}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002072EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
WANG Cong22dc13c2016-08-13 22:35:00 -07002074#ifdef CONFIG_NET_CLS_ACT
2075static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2076{
2077 if (exts->nr_actions == 0)
2078 return NULL;
2079 else
2080 return exts->actions[0];
2081}
2082#endif
WANG Cong33be6272013-12-15 20:15:05 -08002083
WANG Cong5da57f42013-12-15 20:15:07 -08002084int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07002087 struct nlattr *nest;
2088
Jiri Pirko978dfd82017-08-04 14:29:03 +02002089 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /*
2091 * again for backward compatible mode - we want
2092 * to work with both old and new modes of entering
2093 * tc data even if iproute2 was newer - jhs
2094 */
WANG Cong33be6272013-12-15 20:15:05 -08002095 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08002096 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002097 if (nest == NULL)
2098 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07002099
Vlad Buslov90b73b72018-07-05 17:24:33 +03002100 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002101 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002102 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08002103 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08002104 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08002105 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05002106 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002107 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08002108 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002109 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002110 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07002114
2115nla_put_failure:
2116 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07002118#else
2119 return 0;
2120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002122EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002124
WANG Cong5da57f42013-12-15 20:15:07 -08002125int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08002128 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01002129 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002130 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131#endif
2132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002134EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Jiri Pirko717503b2017-10-11 09:41:09 +02002136static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2137 enum tc_setup_type type,
2138 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002139{
2140 int ok_count = 0;
2141#ifdef CONFIG_NET_CLS_ACT
2142 const struct tc_action *a;
2143 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002144 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002145
2146 if (!tcf_exts_has_actions(exts))
2147 return 0;
2148
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002149 for (i = 0; i < exts->nr_actions; i++) {
2150 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002151 if (!a->ops->get_dev)
2152 continue;
2153 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01002154 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002155 continue;
2156 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
2157 if (ret < 0)
2158 return ret;
2159 ok_count += ret;
2160 }
2161#endif
2162 return ok_count;
2163}
Jiri Pirko717503b2017-10-11 09:41:09 +02002164
Jiri Pirko208c0f42017-10-19 15:50:32 +02002165int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2166 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02002167{
David S. Miller9a99dc12018-06-06 13:55:47 -04002168 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002169 int ret;
2170
David S. Miller9a99dc12018-06-06 13:55:47 -04002171 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2172 if (ret < 0)
2173 return ret;
2174 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002175
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03002176 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04002177 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002178 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2179 if (ret < 0)
2180 return ret;
2181 ok_count += ret;
2182
2183 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02002184}
2185EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002186
Jiri Pirko48617382018-01-17 11:46:46 +01002187static __net_init int tcf_net_init(struct net *net)
2188{
2189 struct tcf_net *tn = net_generic(net, tcf_net_id);
2190
2191 idr_init(&tn->idr);
2192 return 0;
2193}
2194
2195static void __net_exit tcf_net_exit(struct net *net)
2196{
2197 struct tcf_net *tn = net_generic(net, tcf_net_id);
2198
2199 idr_destroy(&tn->idr);
2200}
2201
2202static struct pernet_operations tcf_net_ops = {
2203 .init = tcf_net_init,
2204 .exit = tcf_net_exit,
2205 .id = &tcf_net_id,
2206 .size = sizeof(struct tcf_net),
2207};
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209static int __init tc_filter_init(void)
2210{
Jiri Pirko48617382018-01-17 11:46:46 +01002211 int err;
2212
Cong Wang7aa00452017-10-26 18:24:28 -07002213 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2214 if (!tc_filter_wq)
2215 return -ENOMEM;
2216
Jiri Pirko48617382018-01-17 11:46:46 +01002217 err = register_pernet_subsys(&tcf_net_ops);
2218 if (err)
2219 goto err_register_pernet_subsys;
2220
Vlad Buslovc431f892018-05-31 09:52:53 +03002221 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2222 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2223 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02002224 tc_dump_tfilter, 0);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002225 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2226 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2227 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2228 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01002231
2232err_register_pernet_subsys:
2233 destroy_workqueue(tc_filter_wq);
2234 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
2237subsys_initcall(tc_filter_init);