blob: e8b0bbd0883f3b427f9ff406346d504d545c60a3 [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_destroy(struct tcf_chain *chain)
236{
Cong Wangefbf7892017-12-04 10:48:18 -0800237 struct tcf_block *block = chain->block;
238
Cong Wange2ef7542017-09-11 16:33:31 -0700239 list_del(&chain->list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200240 if (!chain->index)
241 block->chain0.chain = NULL;
Cong Wange2ef7542017-09-11 16:33:31 -0700242 kfree(chain);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200243 if (list_empty(&block->chain_list) && block->refcnt == 0)
Cong Wangefbf7892017-12-04 10:48:18 -0800244 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700245}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200246
Cong Wange2ef7542017-09-11 16:33:31 -0700247static void tcf_chain_hold(struct tcf_chain *chain)
248{
249 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200250}
251
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200252static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200253{
254 /* In case all the references are action references, this
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200255 * chain should not be shown to the user.
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200256 */
257 return chain->refcnt == chain->action_refcnt;
258}
259
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200260static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
261 u32 chain_index)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200262{
263 struct tcf_chain *chain;
264
265 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200266 if (chain->index == chain_index)
Cong Wange2ef7542017-09-11 16:33:31 -0700267 return chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200268 }
269 return NULL;
270}
271
272static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
273 u32 seq, u16 flags, int event, bool unicast);
274
Jiri Pirko53681402018-08-01 12:36:56 +0200275static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
276 u32 chain_index, bool create,
277 bool by_act)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200278{
279 struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
280
281 if (chain) {
282 tcf_chain_hold(chain);
Jiri Pirko53681402018-08-01 12:36:56 +0200283 } else {
284 if (!create)
285 return NULL;
286 chain = tcf_chain_create(block, chain_index);
287 if (!chain)
288 return NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200289 }
Jiri Pirko80532382017-09-06 13:14:19 +0200290
Jiri Pirko53681402018-08-01 12:36:56 +0200291 if (by_act)
292 ++chain->action_refcnt;
293
294 /* Send notification only in case we got the first
295 * non-action reference. Until then, the chain acts only as
296 * a placeholder for actions pointing to it and user ought
297 * not know about them.
298 */
299 if (chain->refcnt - chain->action_refcnt == 1 && !by_act)
300 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
301 RTM_NEWCHAIN, false);
302
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200303 return chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200304}
Jiri Pirko53681402018-08-01 12:36:56 +0200305
Jiri Pirko290b1c82018-08-01 12:36:57 +0200306static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
307 bool create)
Jiri Pirko53681402018-08-01 12:36:56 +0200308{
309 return __tcf_chain_get(block, chain_index, create, false);
310}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200311
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200312struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
313{
Jiri Pirko53681402018-08-01 12:36:56 +0200314 return __tcf_chain_get(block, chain_index, true, true);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200315}
316EXPORT_SYMBOL(tcf_chain_get_by_act);
317
Jiri Pirko9f407f12018-07-23 09:23:07 +0200318static void tc_chain_tmplt_del(struct tcf_chain *chain);
319
Jiri Pirko53681402018-08-01 12:36:56 +0200320static void __tcf_chain_put(struct tcf_chain *chain, bool by_act)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200321{
Jiri Pirko53681402018-08-01 12:36:56 +0200322 if (by_act)
323 chain->action_refcnt--;
324 chain->refcnt--;
325
326 /* The last dropped non-action reference will trigger notification. */
327 if (chain->refcnt - chain->action_refcnt == 0 && !by_act)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200328 tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
Jiri Pirko53681402018-08-01 12:36:56 +0200329
330 if (chain->refcnt == 0) {
Jiri Pirko9f407f12018-07-23 09:23:07 +0200331 tc_chain_tmplt_del(chain);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200332 tcf_chain_destroy(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200333 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200334}
Jiri Pirko53681402018-08-01 12:36:56 +0200335
Jiri Pirko290b1c82018-08-01 12:36:57 +0200336static void tcf_chain_put(struct tcf_chain *chain)
Jiri Pirko53681402018-08-01 12:36:56 +0200337{
338 __tcf_chain_put(chain, false);
339}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200340
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200341void tcf_chain_put_by_act(struct tcf_chain *chain)
342{
Jiri Pirko53681402018-08-01 12:36:56 +0200343 __tcf_chain_put(chain, true);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200344}
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 Pirko290b1c82018-08-01 12:36:57 +0200353static void tcf_chain_flush(struct tcf_chain *chain)
354{
355 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
356
357 tcf_chain0_head_change(chain, NULL);
358 while (tp) {
359 RCU_INIT_POINTER(chain->filter_chain, tp->next);
360 tcf_proto_destroy(tp, NULL);
361 tp = rtnl_dereference(chain->filter_chain);
362 tcf_chain_put(chain);
363 }
364}
365
Jiri Pirkocaa72602018-01-17 11:46:50 +0100366static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200367{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100368 return block->offloadcnt;
369}
370
371static int tcf_block_offload_cmd(struct tcf_block *block,
372 struct net_device *dev,
373 struct tcf_block_ext_info *ei,
John Hurley60513bd2018-06-25 14:30:04 -0700374 enum tc_block_command command,
375 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100376{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200377 struct tc_block_offload bo = {};
378
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200379 bo.command = command;
380 bo.binder_type = ei->binder_type;
381 bo.block = block;
John Hurley60513bd2018-06-25 14:30:04 -0700382 bo.extack = extack;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100383 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200384}
385
Jiri Pirkocaa72602018-01-17 11:46:50 +0100386static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700387 struct tcf_block_ext_info *ei,
388 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200389{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100390 struct net_device *dev = q->dev_queue->dev;
391 int err;
392
393 if (!dev->netdev_ops->ndo_setup_tc)
394 goto no_offload_dev_inc;
395
396 /* If tc offload feature is disabled and the block we try to bind
397 * to already has some offloaded filters, forbid to bind.
398 */
John Hurley60513bd2018-06-25 14:30:04 -0700399 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
400 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Jiri Pirkocaa72602018-01-17 11:46:50 +0100401 return -EOPNOTSUPP;
John Hurley60513bd2018-06-25 14:30:04 -0700402 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100403
John Hurley60513bd2018-06-25 14:30:04 -0700404 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100405 if (err == -EOPNOTSUPP)
406 goto no_offload_dev_inc;
407 return err;
408
409no_offload_dev_inc:
410 if (tcf_block_offload_in_use(block))
411 return -EOPNOTSUPP;
412 block->nooffloaddevcnt++;
413 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200414}
415
416static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
417 struct tcf_block_ext_info *ei)
418{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100419 struct net_device *dev = q->dev_queue->dev;
420 int err;
421
422 if (!dev->netdev_ops->ndo_setup_tc)
423 goto no_offload_dev_dec;
John Hurley60513bd2018-06-25 14:30:04 -0700424 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100425 if (err == -EOPNOTSUPP)
426 goto no_offload_dev_dec;
427 return;
428
429no_offload_dev_dec:
430 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200431}
432
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100433static int
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200434tcf_chain0_head_change_cb_add(struct tcf_block *block,
435 struct tcf_block_ext_info *ei,
436 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100437{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200438 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100439 struct tcf_filter_chain_list_item *item;
440
441 item = kmalloc(sizeof(*item), GFP_KERNEL);
442 if (!item) {
443 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
444 return -ENOMEM;
445 }
446 item->chain_head_change = ei->chain_head_change;
447 item->chain_head_change_priv = ei->chain_head_change_priv;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200448 if (chain0 && chain0->filter_chain)
449 tcf_chain_head_change_item(item, chain0->filter_chain);
450 list_add(&item->list, &block->chain0.filter_chain_list);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100451 return 0;
452}
453
454static void
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200455tcf_chain0_head_change_cb_del(struct tcf_block *block,
456 struct tcf_block_ext_info *ei)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100457{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200458 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100459 struct tcf_filter_chain_list_item *item;
460
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200461 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100462 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
463 (item->chain_head_change == ei->chain_head_change &&
464 item->chain_head_change_priv == ei->chain_head_change_priv)) {
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200465 if (chain0)
466 tcf_chain_head_change_item(item, NULL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100467 list_del(&item->list);
468 kfree(item);
469 return;
470 }
471 }
472 WARN_ON(1);
473}
474
Jiri Pirko48617382018-01-17 11:46:46 +0100475struct tcf_net {
476 struct idr idr;
477};
478
479static unsigned int tcf_net_id;
480
481static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100482 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100483{
Jiri Pirko48617382018-01-17 11:46:46 +0100484 struct tcf_net *tn = net_generic(net, tcf_net_id);
Jiri Pirko48617382018-01-17 11:46:46 +0100485
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100486 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
487 GFP_KERNEL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100488}
489
Jiri Pirko48617382018-01-17 11:46:46 +0100490static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200491{
Jiri Pirko48617382018-01-17 11:46:46 +0100492 struct tcf_net *tn = net_generic(net, tcf_net_id);
493
Matthew Wilcox9c160942017-11-28 09:48:43 -0500494 idr_remove(&tn->idr, block->index);
Jiri Pirko48617382018-01-17 11:46:46 +0100495}
496
497static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100498 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100499 struct netlink_ext_ack *extack)
500{
501 struct tcf_block *block;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200502
Jiri Pirko48617382018-01-17 11:46:46 +0100503 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500504 if (!block) {
505 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100506 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500507 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200508 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200509 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100510 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200511 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200512
Jiri Pirko48617382018-01-17 11:46:46 +0100513 block->refcnt = 1;
514 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100515 block->index = block_index;
516
517 /* Don't store q pointer for blocks which are shared */
518 if (!tcf_block_shared(block))
519 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100520 return block;
Jiri Pirko48617382018-01-17 11:46:46 +0100521}
522
523static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
524{
525 struct tcf_net *tn = net_generic(net, tcf_net_id);
526
Matthew Wilcox322d8842017-11-28 10:01:24 -0500527 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100528}
529
Vlad Buslovc431f892018-05-31 09:52:53 +0300530/* Find tcf block.
531 * Set q, parent, cl when appropriate.
532 */
533
534static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
535 u32 *parent, unsigned long *cl,
536 int ifindex, u32 block_index,
537 struct netlink_ext_ack *extack)
538{
539 struct tcf_block *block;
540
541 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
542 block = tcf_block_lookup(net, block_index);
543 if (!block) {
544 NL_SET_ERR_MSG(extack, "Block of given index was not found");
545 return ERR_PTR(-EINVAL);
546 }
547 } else {
548 const struct Qdisc_class_ops *cops;
549 struct net_device *dev;
550
551 /* Find link */
552 dev = __dev_get_by_index(net, ifindex);
553 if (!dev)
554 return ERR_PTR(-ENODEV);
555
556 /* Find qdisc */
557 if (!*parent) {
558 *q = dev->qdisc;
559 *parent = (*q)->handle;
560 } else {
561 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
562 if (!*q) {
563 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
564 return ERR_PTR(-EINVAL);
565 }
566 }
567
568 /* Is it classful? */
569 cops = (*q)->ops->cl_ops;
570 if (!cops) {
571 NL_SET_ERR_MSG(extack, "Qdisc not classful");
572 return ERR_PTR(-EINVAL);
573 }
574
575 if (!cops->tcf_block) {
576 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
577 return ERR_PTR(-EOPNOTSUPP);
578 }
579
580 /* Do we search for filter, attached to class? */
581 if (TC_H_MIN(*parent)) {
582 *cl = cops->find(*q, *parent);
583 if (*cl == 0) {
584 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
585 return ERR_PTR(-ENOENT);
586 }
587 }
588
589 /* And the last stroke */
590 block = cops->tcf_block(*q, *cl, extack);
591 if (!block)
592 return ERR_PTR(-EINVAL);
593 if (tcf_block_shared(block)) {
594 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
595 return ERR_PTR(-EOPNOTSUPP);
596 }
597 }
598
599 return block;
600}
601
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100602struct tcf_block_owner_item {
603 struct list_head list;
604 struct Qdisc *q;
605 enum tcf_block_binder_type binder_type;
606};
607
608static void
609tcf_block_owner_netif_keep_dst(struct tcf_block *block,
610 struct Qdisc *q,
611 enum tcf_block_binder_type binder_type)
612{
613 if (block->keep_dst &&
614 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
615 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
616 netif_keep_dst(qdisc_dev(q));
617}
618
619void tcf_block_netif_keep_dst(struct tcf_block *block)
620{
621 struct tcf_block_owner_item *item;
622
623 block->keep_dst = true;
624 list_for_each_entry(item, &block->owner_list, list)
625 tcf_block_owner_netif_keep_dst(block, item->q,
626 item->binder_type);
627}
628EXPORT_SYMBOL(tcf_block_netif_keep_dst);
629
630static int tcf_block_owner_add(struct tcf_block *block,
631 struct Qdisc *q,
632 enum tcf_block_binder_type binder_type)
633{
634 struct tcf_block_owner_item *item;
635
636 item = kmalloc(sizeof(*item), GFP_KERNEL);
637 if (!item)
638 return -ENOMEM;
639 item->q = q;
640 item->binder_type = binder_type;
641 list_add(&item->list, &block->owner_list);
642 return 0;
643}
644
645static void tcf_block_owner_del(struct tcf_block *block,
646 struct Qdisc *q,
647 enum tcf_block_binder_type binder_type)
648{
649 struct tcf_block_owner_item *item;
650
651 list_for_each_entry(item, &block->owner_list, list) {
652 if (item->q == q && item->binder_type == binder_type) {
653 list_del(&item->list);
654 kfree(item);
655 return;
656 }
657 }
658 WARN_ON(1);
659}
660
Jiri Pirko48617382018-01-17 11:46:46 +0100661int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
662 struct tcf_block_ext_info *ei,
663 struct netlink_ext_ack *extack)
664{
665 struct net *net = qdisc_net(q);
666 struct tcf_block *block = NULL;
667 bool created = false;
668 int err;
669
670 if (ei->block_index) {
671 /* block_index not 0 means the shared block is requested */
672 block = tcf_block_lookup(net, ei->block_index);
673 if (block)
674 block->refcnt++;
675 }
676
677 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100678 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100679 if (IS_ERR(block))
680 return PTR_ERR(block);
681 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100682 if (tcf_block_shared(block)) {
683 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100684 if (err)
685 goto err_block_insert;
686 }
687 }
688
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100689 err = tcf_block_owner_add(block, q, ei->binder_type);
690 if (err)
691 goto err_block_owner_add;
692
693 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
694
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200695 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100696 if (err)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200697 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100698
John Hurley60513bd2018-06-25 14:30:04 -0700699 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100700 if (err)
701 goto err_block_offload_bind;
702
Jiri Pirko6529eab2017-05-17 11:07:55 +0200703 *p_block = block;
704 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200705
Jiri Pirkocaa72602018-01-17 11:46:50 +0100706err_block_offload_bind:
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200707 tcf_chain0_head_change_cb_del(block, ei);
708err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100709 tcf_block_owner_del(block, q, ei->binder_type);
710err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100711 if (created) {
712 if (tcf_block_shared(block))
713 tcf_block_remove(block, net);
714err_block_insert:
Jiri Pirko48617382018-01-17 11:46:46 +0100715 kfree(block);
716 } else {
717 block->refcnt--;
718 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200719 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200720}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200721EXPORT_SYMBOL(tcf_block_get_ext);
722
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100723static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
724{
725 struct tcf_proto __rcu **p_filter_chain = priv;
726
727 rcu_assign_pointer(*p_filter_chain, tp_head);
728}
729
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200730int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500731 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
732 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200733{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100734 struct tcf_block_ext_info ei = {
735 .chain_head_change = tcf_chain_head_change_dflt,
736 .chain_head_change_priv = p_filter_chain,
737 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200738
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100739 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500740 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200741}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200742EXPORT_SYMBOL(tcf_block_get);
743
Cong Wang7aa00452017-10-26 18:24:28 -0700744/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100745 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700746 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100747void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900748 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700749{
Cong Wangefbf7892017-12-04 10:48:18 -0800750 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700751
David S. Millerc30abd52017-12-16 22:11:55 -0500752 if (!block)
753 return;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200754 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100755 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100756
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200757 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100758 if (tcf_block_shared(block))
759 tcf_block_remove(block, block->net);
760
761 /* Hold a refcnt for all chains, so that they don't disappear
762 * while we are iterating.
763 */
764 list_for_each_entry(chain, &block->chain_list, list)
765 tcf_chain_hold(chain);
766
767 list_for_each_entry(chain, &block->chain_list, list)
768 tcf_chain_flush(chain);
769 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700770
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100771 tcf_block_offload_unbind(block, q, ei);
772
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200773 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100774 /* At this point, all the chains should have refcnt >= 1. */
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200775 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
776 tcf_chain_put_explicitly_created(chain);
Jiri Pirko48617382018-01-17 11:46:46 +0100777 tcf_chain_put(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200778 }
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100779
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200780 block->refcnt--;
781 if (list_empty(&block->chain_list))
782 kfree(block);
Jiri Pirko48617382018-01-17 11:46:46 +0100783 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200784}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200785EXPORT_SYMBOL(tcf_block_put_ext);
786
787void tcf_block_put(struct tcf_block *block)
788{
789 struct tcf_block_ext_info ei = {0, };
790
Jiri Pirko4853f122017-12-21 13:13:59 +0100791 if (!block)
792 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100793 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200794}
David S. Millere1ea2f92017-10-30 14:10:01 +0900795
Jiri Pirko6529eab2017-05-17 11:07:55 +0200796EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100797
Jiri Pirkoacb67442017-10-19 15:50:31 +0200798struct tcf_block_cb {
799 struct list_head list;
800 tc_setup_cb_t *cb;
801 void *cb_ident;
802 void *cb_priv;
803 unsigned int refcnt;
804};
805
806void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
807{
808 return block_cb->cb_priv;
809}
810EXPORT_SYMBOL(tcf_block_cb_priv);
811
812struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
813 tc_setup_cb_t *cb, void *cb_ident)
814{ struct tcf_block_cb *block_cb;
815
816 list_for_each_entry(block_cb, &block->cb_list, list)
817 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
818 return block_cb;
819 return NULL;
820}
821EXPORT_SYMBOL(tcf_block_cb_lookup);
822
823void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
824{
825 block_cb->refcnt++;
826}
827EXPORT_SYMBOL(tcf_block_cb_incref);
828
829unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
830{
831 return --block_cb->refcnt;
832}
833EXPORT_SYMBOL(tcf_block_cb_decref);
834
John Hurley32636742018-06-25 14:30:10 -0700835static int
836tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
837 void *cb_priv, bool add, bool offload_in_use,
838 struct netlink_ext_ack *extack)
839{
840 struct tcf_chain *chain;
841 struct tcf_proto *tp;
842 int err;
843
844 list_for_each_entry(chain, &block->chain_list, list) {
845 for (tp = rtnl_dereference(chain->filter_chain); tp;
846 tp = rtnl_dereference(tp->next)) {
847 if (tp->ops->reoffload) {
848 err = tp->ops->reoffload(tp, add, cb, cb_priv,
849 extack);
850 if (err && add)
851 goto err_playback_remove;
852 } else if (add && offload_in_use) {
853 err = -EOPNOTSUPP;
854 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
855 goto err_playback_remove;
856 }
857 }
858 }
859
860 return 0;
861
862err_playback_remove:
863 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
864 extack);
865 return err;
866}
867
Jiri Pirkoacb67442017-10-19 15:50:31 +0200868struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
869 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700870 void *cb_priv,
871 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200872{
873 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700874 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200875
John Hurley32636742018-06-25 14:30:10 -0700876 /* Replay any already present rules */
877 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
878 tcf_block_offload_in_use(block),
879 extack);
880 if (err)
881 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100882
Jiri Pirkoacb67442017-10-19 15:50:31 +0200883 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
884 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100885 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200886 block_cb->cb = cb;
887 block_cb->cb_ident = cb_ident;
888 block_cb->cb_priv = cb_priv;
889 list_add(&block_cb->list, &block->cb_list);
890 return block_cb;
891}
892EXPORT_SYMBOL(__tcf_block_cb_register);
893
894int tcf_block_cb_register(struct tcf_block *block,
895 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700896 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200897{
898 struct tcf_block_cb *block_cb;
899
John Hurley60513bd2018-06-25 14:30:04 -0700900 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
901 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500902 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200903}
904EXPORT_SYMBOL(tcf_block_cb_register);
905
John Hurley32636742018-06-25 14:30:10 -0700906void __tcf_block_cb_unregister(struct tcf_block *block,
907 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200908{
John Hurley32636742018-06-25 14:30:10 -0700909 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
910 false, tcf_block_offload_in_use(block),
911 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200912 list_del(&block_cb->list);
913 kfree(block_cb);
914}
915EXPORT_SYMBOL(__tcf_block_cb_unregister);
916
917void tcf_block_cb_unregister(struct tcf_block *block,
918 tc_setup_cb_t *cb, void *cb_ident)
919{
920 struct tcf_block_cb *block_cb;
921
922 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
923 if (!block_cb)
924 return;
John Hurley32636742018-06-25 14:30:10 -0700925 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200926}
927EXPORT_SYMBOL(tcf_block_cb_unregister);
928
929static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
930 void *type_data, bool err_stop)
931{
932 struct tcf_block_cb *block_cb;
933 int ok_count = 0;
934 int err;
935
David S. Miller9a99dc12018-06-06 13:55:47 -0400936 /* Make sure all netdevs sharing this block are offload-capable. */
937 if (block->nooffloaddevcnt && err_stop)
938 return -EOPNOTSUPP;
939
Jiri Pirkoacb67442017-10-19 15:50:31 +0200940 list_for_each_entry(block_cb, &block->cb_list, list) {
941 err = block_cb->cb(type, type_data, block_cb->cb_priv);
942 if (err) {
943 if (err_stop)
944 return err;
945 } else {
946 ok_count++;
947 }
948 }
949 return ok_count;
950}
951
Jiri Pirko87d83092017-05-17 11:07:54 +0200952/* Main classifier routine: scans classifier chain attached
953 * to this qdisc, (optionally) tests for protocol and asks
954 * specific classifiers.
955 */
956int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
957 struct tcf_result *res, bool compat_mode)
958{
959 __be16 protocol = tc_skb_protocol(skb);
960#ifdef CONFIG_NET_CLS_ACT
961 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200962 const struct tcf_proto *orig_tp = tp;
963 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200964 int limit = 0;
965
966reclassify:
967#endif
968 for (; tp; tp = rcu_dereference_bh(tp->next)) {
969 int err;
970
971 if (tp->protocol != protocol &&
972 tp->protocol != htons(ETH_P_ALL))
973 continue;
974
975 err = tp->classify(skb, tp, res);
976#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200977 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200978 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200979 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200980 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200981 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200982 goto reset;
983 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200984#endif
985 if (err >= 0)
986 return err;
987 }
988
989 return TC_ACT_UNSPEC; /* signal: continue lookup */
990#ifdef CONFIG_NET_CLS_ACT
991reset:
992 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100993 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
994 tp->chain->block->index,
995 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200996 ntohs(tp->protocol));
997 return TC_ACT_SHOT;
998 }
999
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001000 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001001 protocol = tc_skb_protocol(skb);
1002 goto reclassify;
1003#endif
1004}
1005EXPORT_SYMBOL(tcf_classify);
1006
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001007struct tcf_chain_info {
1008 struct tcf_proto __rcu **pprev;
1009 struct tcf_proto __rcu *next;
1010};
1011
1012static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1013{
1014 return rtnl_dereference(*chain_info->pprev);
1015}
1016
1017static void tcf_chain_tp_insert(struct tcf_chain *chain,
1018 struct tcf_chain_info *chain_info,
1019 struct tcf_proto *tp)
1020{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001021 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001022 tcf_chain0_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001023 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1024 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -07001025 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001026}
1027
1028static void tcf_chain_tp_remove(struct tcf_chain *chain,
1029 struct tcf_chain_info *chain_info,
1030 struct tcf_proto *tp)
1031{
1032 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1033
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001034 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001035 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001036 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -07001037 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001038}
1039
1040static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1041 struct tcf_chain_info *chain_info,
1042 u32 protocol, u32 prio,
1043 bool prio_allocate)
1044{
1045 struct tcf_proto **pprev;
1046 struct tcf_proto *tp;
1047
1048 /* Check the chain for existence of proto-tcf with this priority */
1049 for (pprev = &chain->filter_chain;
1050 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1051 if (tp->prio >= prio) {
1052 if (tp->prio == prio) {
1053 if (prio_allocate ||
1054 (tp->protocol != protocol && protocol))
1055 return ERR_PTR(-EINVAL);
1056 } else {
1057 tp = NULL;
1058 }
1059 break;
1060 }
1061 }
1062 chain_info->pprev = pprev;
1063 chain_info->next = tp ? tp->next : NULL;
1064 return tp;
1065}
1066
WANG Cong71203712017-08-07 15:26:50 -07001067static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001068 struct tcf_proto *tp, struct tcf_block *block,
1069 struct Qdisc *q, u32 parent, void *fh,
1070 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -07001071{
1072 struct tcmsg *tcm;
1073 struct nlmsghdr *nlh;
1074 unsigned char *b = skb_tail_pointer(skb);
1075
1076 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1077 if (!nlh)
1078 goto out_nlmsg_trim;
1079 tcm = nlmsg_data(nlh);
1080 tcm->tcm_family = AF_UNSPEC;
1081 tcm->tcm__pad1 = 0;
1082 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001083 if (q) {
1084 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1085 tcm->tcm_parent = parent;
1086 } else {
1087 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1088 tcm->tcm_block_index = block->index;
1089 }
WANG Cong71203712017-08-07 15:26:50 -07001090 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1091 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1092 goto nla_put_failure;
1093 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1094 goto nla_put_failure;
1095 if (!fh) {
1096 tcm->tcm_handle = 0;
1097 } else {
1098 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1099 goto nla_put_failure;
1100 }
1101 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1102 return skb->len;
1103
1104out_nlmsg_trim:
1105nla_put_failure:
1106 nlmsg_trim(skb, b);
1107 return -1;
1108}
1109
1110static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1111 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001112 struct tcf_block *block, struct Qdisc *q,
1113 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001114{
1115 struct sk_buff *skb;
1116 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1117
1118 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1119 if (!skb)
1120 return -ENOBUFS;
1121
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001122 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1123 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001124 kfree_skb(skb);
1125 return -EINVAL;
1126 }
1127
1128 if (unicast)
1129 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1130
1131 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1132 n->nlmsg_flags & NLM_F_ECHO);
1133}
1134
1135static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1136 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001137 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001138 u32 parent, void *fh, bool unicast, bool *last,
1139 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001140{
1141 struct sk_buff *skb;
1142 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1143 int err;
1144
1145 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1146 if (!skb)
1147 return -ENOBUFS;
1148
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001149 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1150 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001151 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001152 kfree_skb(skb);
1153 return -EINVAL;
1154 }
1155
Alexander Aring571acf22018-01-18 11:20:53 -05001156 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001157 if (err) {
1158 kfree_skb(skb);
1159 return err;
1160 }
1161
1162 if (unicast)
1163 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1164
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001165 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1166 n->nlmsg_flags & NLM_F_ECHO);
1167 if (err < 0)
1168 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1169 return err;
WANG Cong71203712017-08-07 15:26:50 -07001170}
1171
1172static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001173 struct tcf_block *block, struct Qdisc *q,
1174 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001175 struct tcf_chain *chain, int event)
1176{
1177 struct tcf_proto *tp;
1178
1179 for (tp = rtnl_dereference(chain->filter_chain);
1180 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001181 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001182 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001183}
1184
Vlad Buslovc431f892018-05-31 09:52:53 +03001185static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001186 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001188 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001189 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 struct tcmsg *t;
1191 u32 protocol;
1192 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001193 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001195 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001196 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001197 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001198 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001199 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001202 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001204 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Vlad Buslovc431f892018-05-31 09:52:53 +03001206 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001207 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001210 tp_created = 0;
1211
David Ahernc21ef3e2017-04-16 09:48:24 -07001212 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001213 if (err < 0)
1214 return err;
1215
David S. Miller942b8162012-06-26 21:48:50 -07001216 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 protocol = TC_H_MIN(t->tcm_info);
1218 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001219 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 parent = t->tcm_parent;
1221 cl = 0;
1222
1223 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001224 /* If no priority is provided by the user,
1225 * we allocate one.
1226 */
1227 if (n->nlmsg_flags & NLM_F_CREATE) {
1228 prio = TC_H_MAKE(0x80000000U, 0U);
1229 prio_allocate = true;
1230 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001231 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
1236 /* Find head of filter chain. */
1237
Vlad Buslovc431f892018-05-31 09:52:53 +03001238 block = tcf_block_find(net, &q, &parent, &cl,
1239 t->tcm_ifindex, t->tcm_block_index, extack);
1240 if (IS_ERR(block)) {
1241 err = PTR_ERR(block);
1242 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001243 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001244
1245 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1246 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001247 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001248 err = -EINVAL;
1249 goto errout;
1250 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001251 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001252 if (!chain) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001253 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001254 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001255 goto errout;
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001258 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1259 prio, prio_allocate);
1260 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001261 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001262 err = PTR_ERR(tp);
1263 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
1266 if (tp == NULL) {
1267 /* Proto-tcf does not exist, create new one */
1268
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001269 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001270 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001271 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Vlad Buslovc431f892018-05-31 09:52:53 +03001275 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001276 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 +01001277 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001281 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001282 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Jiri Pirko33a48922017-02-09 14:38:57 +01001284 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001285 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001286 if (IS_ERR(tp)) {
1287 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto errout;
1289 }
Minoru Usui12186be2009-06-02 02:17:34 -07001290 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001291 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001292 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001293 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 fh = tp->ops->get(tp, t->tcm_handle);
1298
WANG Cong8113c092017-08-04 21:31:43 -07001299 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001300 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001301 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 +01001302 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001304 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001305 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1306 NL_SET_ERR_MSG(extack, "Filter already exists");
1307 err = -EEXIST;
1308 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
Jiri Pirko9f407f12018-07-23 09:23:07 +02001311 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1312 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1313 err = -EINVAL;
1314 goto errout;
1315 }
1316
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001317 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001318 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1319 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001320 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001321 if (tp_created)
1322 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001323 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001324 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001325 } else {
1326 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001327 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001331 if (chain)
1332 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (err == -EAGAIN)
1334 /* Replay the request. */
1335 goto replay;
1336 return err;
1337}
1338
Vlad Buslovc431f892018-05-31 09:52:53 +03001339static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1340 struct netlink_ext_ack *extack)
1341{
1342 struct net *net = sock_net(skb->sk);
1343 struct nlattr *tca[TCA_MAX + 1];
1344 struct tcmsg *t;
1345 u32 protocol;
1346 u32 prio;
1347 u32 parent;
1348 u32 chain_index;
1349 struct Qdisc *q = NULL;
1350 struct tcf_chain_info chain_info;
1351 struct tcf_chain *chain = NULL;
1352 struct tcf_block *block;
1353 struct tcf_proto *tp = NULL;
1354 unsigned long cl = 0;
1355 void *fh = NULL;
1356 int err;
1357
1358 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1359 return -EPERM;
1360
1361 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1362 if (err < 0)
1363 return err;
1364
1365 t = nlmsg_data(n);
1366 protocol = TC_H_MIN(t->tcm_info);
1367 prio = TC_H_MAJ(t->tcm_info);
1368 parent = t->tcm_parent;
1369
1370 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1371 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1372 return -ENOENT;
1373 }
1374
1375 /* Find head of filter chain. */
1376
1377 block = tcf_block_find(net, &q, &parent, &cl,
1378 t->tcm_ifindex, t->tcm_block_index, extack);
1379 if (IS_ERR(block)) {
1380 err = PTR_ERR(block);
1381 goto errout;
1382 }
1383
1384 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1385 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1386 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1387 err = -EINVAL;
1388 goto errout;
1389 }
1390 chain = tcf_chain_get(block, chain_index, false);
1391 if (!chain) {
1392 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1393 err = -EINVAL;
1394 goto errout;
1395 }
1396
1397 if (prio == 0) {
1398 tfilter_notify_chain(net, skb, block, q, parent, n,
1399 chain, RTM_DELTFILTER);
1400 tcf_chain_flush(chain);
1401 err = 0;
1402 goto errout;
1403 }
1404
1405 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1406 prio, false);
1407 if (!tp || IS_ERR(tp)) {
1408 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001409 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001410 goto errout;
1411 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1412 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1413 err = -EINVAL;
1414 goto errout;
1415 }
1416
1417 fh = tp->ops->get(tp, t->tcm_handle);
1418
1419 if (!fh) {
1420 if (t->tcm_handle == 0) {
1421 tcf_chain_tp_remove(chain, &chain_info, tp);
1422 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1423 RTM_DELTFILTER, false);
1424 tcf_proto_destroy(tp, extack);
1425 err = 0;
1426 } else {
1427 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1428 err = -ENOENT;
1429 }
1430 } else {
1431 bool last;
1432
1433 err = tfilter_del_notify(net, skb, n, tp, block,
1434 q, parent, fh, false, &last,
1435 extack);
1436 if (err)
1437 goto errout;
1438 if (last) {
1439 tcf_chain_tp_remove(chain, &chain_info, tp);
1440 tcf_proto_destroy(tp, extack);
1441 }
1442 }
1443
1444errout:
1445 if (chain)
1446 tcf_chain_put(chain);
1447 return err;
1448}
1449
1450static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1451 struct netlink_ext_ack *extack)
1452{
1453 struct net *net = sock_net(skb->sk);
1454 struct nlattr *tca[TCA_MAX + 1];
1455 struct tcmsg *t;
1456 u32 protocol;
1457 u32 prio;
1458 u32 parent;
1459 u32 chain_index;
1460 struct Qdisc *q = NULL;
1461 struct tcf_chain_info chain_info;
1462 struct tcf_chain *chain = NULL;
1463 struct tcf_block *block;
1464 struct tcf_proto *tp = NULL;
1465 unsigned long cl = 0;
1466 void *fh = NULL;
1467 int err;
1468
1469 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1470 if (err < 0)
1471 return err;
1472
1473 t = nlmsg_data(n);
1474 protocol = TC_H_MIN(t->tcm_info);
1475 prio = TC_H_MAJ(t->tcm_info);
1476 parent = t->tcm_parent;
1477
1478 if (prio == 0) {
1479 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1480 return -ENOENT;
1481 }
1482
1483 /* Find head of filter chain. */
1484
1485 block = tcf_block_find(net, &q, &parent, &cl,
1486 t->tcm_ifindex, t->tcm_block_index, extack);
1487 if (IS_ERR(block)) {
1488 err = PTR_ERR(block);
1489 goto errout;
1490 }
1491
1492 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1493 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1494 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1495 err = -EINVAL;
1496 goto errout;
1497 }
1498 chain = tcf_chain_get(block, chain_index, false);
1499 if (!chain) {
1500 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1501 err = -EINVAL;
1502 goto errout;
1503 }
1504
1505 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1506 prio, false);
1507 if (!tp || IS_ERR(tp)) {
1508 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001509 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001510 goto errout;
1511 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1512 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1513 err = -EINVAL;
1514 goto errout;
1515 }
1516
1517 fh = tp->ops->get(tp, t->tcm_handle);
1518
1519 if (!fh) {
1520 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1521 err = -ENOENT;
1522 } else {
1523 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1524 fh, RTM_NEWTFILTER, true);
1525 if (err < 0)
1526 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1527 }
1528
1529errout:
1530 if (chain)
1531 tcf_chain_put(chain);
1532 return err;
1533}
1534
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001535struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 struct tcf_walker w;
1537 struct sk_buff *skb;
1538 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001539 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001540 struct Qdisc *q;
1541 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542};
1543
WANG Cong8113c092017-08-04 21:31:43 -07001544static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001546 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001547 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001549 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001550 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001551 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1552 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001555static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1556 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001557 long index_start, long *p_index)
1558{
1559 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001560 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001561 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1562 struct tcf_dump_args arg;
1563 struct tcf_proto *tp;
1564
1565 for (tp = rtnl_dereference(chain->filter_chain);
1566 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1567 if (*p_index < index_start)
1568 continue;
1569 if (TC_H_MAJ(tcm->tcm_info) &&
1570 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1571 continue;
1572 if (TC_H_MIN(tcm->tcm_info) &&
1573 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1574 continue;
1575 if (*p_index > index_start)
1576 memset(&cb->args[1], 0,
1577 sizeof(cb->args) - sizeof(cb->args[0]));
1578 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001579 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001580 NETLINK_CB(cb->skb).portid,
1581 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1582 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001583 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001584
1585 cb->args[1] = 1;
1586 }
1587 if (!tp->ops->walk)
1588 continue;
1589 arg.w.fn = tcf_node_dump;
1590 arg.skb = skb;
1591 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001592 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001593 arg.q = q;
1594 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001595 arg.w.stop = 0;
1596 arg.w.skip = cb->args[1] - 1;
1597 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001598 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001599 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001600 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001601 cb->args[1] = arg.w.count + 1;
1602 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001603 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001604 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001605 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001606}
1607
Eric Dumazetbd27a872009-11-05 20:57:26 -08001608/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1610{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001611 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001612 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001613 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001614 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001615 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001616 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001617 long index_start;
1618 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001619 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001620 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Hong zhi guo573ce262013-03-27 06:47:04 +00001622 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001624
1625 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1626 if (err)
1627 return err;
1628
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001629 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1630 block = tcf_block_lookup(net, tcm->tcm_block_index);
1631 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001632 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001633 /* If we work with block index, q is NULL and parent value
1634 * will never be used in the following code. The check
1635 * in tcf_fill_node prevents it. However, compiler does not
1636 * see that far, so set parent to zero to silence the warning
1637 * about parent being uninitialized.
1638 */
1639 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001640 } else {
1641 const struct Qdisc_class_ops *cops;
1642 struct net_device *dev;
1643 unsigned long cl = 0;
1644
1645 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1646 if (!dev)
1647 return skb->len;
1648
1649 parent = tcm->tcm_parent;
1650 if (!parent) {
1651 q = dev->qdisc;
1652 parent = q->handle;
1653 } else {
1654 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1655 }
1656 if (!q)
1657 goto out;
1658 cops = q->ops->cl_ops;
1659 if (!cops)
1660 goto out;
1661 if (!cops->tcf_block)
1662 goto out;
1663 if (TC_H_MIN(tcm->tcm_parent)) {
1664 cl = cops->find(q, tcm->tcm_parent);
1665 if (cl == 0)
1666 goto out;
1667 }
1668 block = cops->tcf_block(q, cl, NULL);
1669 if (!block)
1670 goto out;
1671 if (tcf_block_shared(block))
1672 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001675 index_start = cb->args[0];
1676 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001677
1678 list_for_each_entry(chain, &block->chain_list, list) {
1679 if (tca[TCA_CHAIN] &&
1680 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1681 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001682 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001683 index_start, &index)) {
1684 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001685 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001686 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001687 }
1688
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001689 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001692 /* If we did no progress, the error (EMSGSIZE) is real */
1693 if (skb->len == 0 && err)
1694 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return skb->len;
1696}
1697
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001698static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1699 struct sk_buff *skb, struct tcf_block *block,
1700 u32 portid, u32 seq, u16 flags, int event)
1701{
1702 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02001703 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001704 struct nlmsghdr *nlh;
1705 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02001706 void *priv;
1707
1708 ops = chain->tmplt_ops;
1709 priv = chain->tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001710
1711 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1712 if (!nlh)
1713 goto out_nlmsg_trim;
1714 tcm = nlmsg_data(nlh);
1715 tcm->tcm_family = AF_UNSPEC;
1716 tcm->tcm__pad1 = 0;
1717 tcm->tcm__pad2 = 0;
1718 tcm->tcm_handle = 0;
1719 if (block->q) {
1720 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1721 tcm->tcm_parent = block->q->handle;
1722 } else {
1723 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1724 tcm->tcm_block_index = block->index;
1725 }
1726
1727 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1728 goto nla_put_failure;
1729
Jiri Pirko9f407f12018-07-23 09:23:07 +02001730 if (ops) {
1731 if (nla_put_string(skb, TCA_KIND, ops->kind))
1732 goto nla_put_failure;
1733 if (ops->tmplt_dump(skb, net, priv) < 0)
1734 goto nla_put_failure;
1735 }
1736
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001737 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1738 return skb->len;
1739
1740out_nlmsg_trim:
1741nla_put_failure:
1742 nlmsg_trim(skb, b);
1743 return -EMSGSIZE;
1744}
1745
1746static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1747 u32 seq, u16 flags, int event, bool unicast)
1748{
1749 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1750 struct tcf_block *block = chain->block;
1751 struct net *net = block->net;
1752 struct sk_buff *skb;
1753
1754 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1755 if (!skb)
1756 return -ENOBUFS;
1757
1758 if (tc_chain_fill_node(chain, net, skb, block, portid,
1759 seq, flags, event) <= 0) {
1760 kfree_skb(skb);
1761 return -EINVAL;
1762 }
1763
1764 if (unicast)
1765 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1766
1767 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1768}
1769
Jiri Pirko9f407f12018-07-23 09:23:07 +02001770static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1771 struct nlattr **tca,
1772 struct netlink_ext_ack *extack)
1773{
1774 const struct tcf_proto_ops *ops;
1775 void *tmplt_priv;
1776
1777 /* If kind is not set, user did not specify template. */
1778 if (!tca[TCA_KIND])
1779 return 0;
1780
1781 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1782 if (IS_ERR(ops))
1783 return PTR_ERR(ops);
1784 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1785 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1786 return -EOPNOTSUPP;
1787 }
1788
1789 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1790 if (IS_ERR(tmplt_priv)) {
1791 module_put(ops->owner);
1792 return PTR_ERR(tmplt_priv);
1793 }
1794 chain->tmplt_ops = ops;
1795 chain->tmplt_priv = tmplt_priv;
1796 return 0;
1797}
1798
1799static void tc_chain_tmplt_del(struct tcf_chain *chain)
1800{
1801 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1802
1803 /* If template ops are set, no work to do for us. */
1804 if (!ops)
1805 return;
1806
1807 ops->tmplt_destroy(chain->tmplt_priv);
1808 module_put(ops->owner);
1809}
1810
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001811/* Add/delete/get a chain */
1812
1813static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1814 struct netlink_ext_ack *extack)
1815{
1816 struct net *net = sock_net(skb->sk);
1817 struct nlattr *tca[TCA_MAX + 1];
1818 struct tcmsg *t;
1819 u32 parent;
1820 u32 chain_index;
1821 struct Qdisc *q = NULL;
1822 struct tcf_chain *chain = NULL;
1823 struct tcf_block *block;
1824 unsigned long cl;
1825 int err;
1826
1827 if (n->nlmsg_type != RTM_GETCHAIN &&
1828 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1829 return -EPERM;
1830
1831replay:
1832 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1833 if (err < 0)
1834 return err;
1835
1836 t = nlmsg_data(n);
1837 parent = t->tcm_parent;
1838 cl = 0;
1839
1840 block = tcf_block_find(net, &q, &parent, &cl,
1841 t->tcm_ifindex, t->tcm_block_index, extack);
1842 if (IS_ERR(block))
1843 return PTR_ERR(block);
1844
1845 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1846 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1847 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1848 return -EINVAL;
1849 }
1850 chain = tcf_chain_lookup(block, chain_index);
1851 if (n->nlmsg_type == RTM_NEWCHAIN) {
1852 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001853 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001854 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001855 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001856 */
1857 tcf_chain_hold(chain);
1858 } else {
1859 NL_SET_ERR_MSG(extack, "Filter chain already exists");
1860 return -EEXIST;
1861 }
1862 } else {
1863 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1864 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
1865 return -ENOENT;
1866 }
1867 chain = tcf_chain_create(block, chain_index);
1868 if (!chain) {
1869 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
1870 return -ENOMEM;
1871 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001872 }
1873 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001874 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001875 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1876 return -EINVAL;
1877 }
1878 tcf_chain_hold(chain);
1879 }
1880
1881 switch (n->nlmsg_type) {
1882 case RTM_NEWCHAIN:
Jiri Pirko9f407f12018-07-23 09:23:07 +02001883 err = tc_chain_tmplt_add(chain, net, tca, extack);
1884 if (err)
1885 goto errout;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001886 /* In case the chain was successfully added, take a reference
1887 * to the chain. This ensures that an empty chain
1888 * does not disappear at the end of this function.
1889 */
1890 tcf_chain_hold(chain);
1891 chain->explicitly_created = true;
1892 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1893 RTM_NEWCHAIN, false);
1894 break;
1895 case RTM_DELCHAIN:
1896 /* Flush the chain first as the user requested chain removal. */
1897 tcf_chain_flush(chain);
1898 /* In case the chain was successfully deleted, put a reference
1899 * to the chain previously taken during addition.
1900 */
1901 tcf_chain_put_explicitly_created(chain);
Jiri Pirkoc921d7d2018-07-26 18:27:58 +02001902 chain->explicitly_created = false;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001903 break;
1904 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001905 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1906 n->nlmsg_seq, n->nlmsg_type, true);
1907 if (err < 0)
1908 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1909 break;
1910 default:
1911 err = -EOPNOTSUPP;
1912 NL_SET_ERR_MSG(extack, "Unsupported message type");
1913 goto errout;
1914 }
1915
1916errout:
1917 tcf_chain_put(chain);
1918 if (err == -EAGAIN)
1919 /* Replay the request. */
1920 goto replay;
1921 return err;
1922}
1923
1924/* called with RTNL */
1925static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
1926{
1927 struct net *net = sock_net(skb->sk);
1928 struct nlattr *tca[TCA_MAX + 1];
1929 struct Qdisc *q = NULL;
1930 struct tcf_block *block;
1931 struct tcf_chain *chain;
1932 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1933 long index_start;
1934 long index;
1935 u32 parent;
1936 int err;
1937
1938 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1939 return skb->len;
1940
1941 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1942 if (err)
1943 return err;
1944
1945 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1946 block = tcf_block_lookup(net, tcm->tcm_block_index);
1947 if (!block)
1948 goto out;
1949 /* If we work with block index, q is NULL and parent value
1950 * will never be used in the following code. The check
1951 * in tcf_fill_node prevents it. However, compiler does not
1952 * see that far, so set parent to zero to silence the warning
1953 * about parent being uninitialized.
1954 */
1955 parent = 0;
1956 } else {
1957 const struct Qdisc_class_ops *cops;
1958 struct net_device *dev;
1959 unsigned long cl = 0;
1960
1961 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1962 if (!dev)
1963 return skb->len;
1964
1965 parent = tcm->tcm_parent;
1966 if (!parent) {
1967 q = dev->qdisc;
1968 parent = q->handle;
1969 } else {
1970 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1971 }
1972 if (!q)
1973 goto out;
1974 cops = q->ops->cl_ops;
1975 if (!cops)
1976 goto out;
1977 if (!cops->tcf_block)
1978 goto out;
1979 if (TC_H_MIN(tcm->tcm_parent)) {
1980 cl = cops->find(q, tcm->tcm_parent);
1981 if (cl == 0)
1982 goto out;
1983 }
1984 block = cops->tcf_block(q, cl, NULL);
1985 if (!block)
1986 goto out;
1987 if (tcf_block_shared(block))
1988 q = NULL;
1989 }
1990
1991 index_start = cb->args[0];
1992 index = 0;
1993
1994 list_for_each_entry(chain, &block->chain_list, list) {
1995 if ((tca[TCA_CHAIN] &&
1996 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
1997 continue;
1998 if (index < index_start) {
1999 index++;
2000 continue;
2001 }
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002002 if (tcf_chain_held_by_acts_only(chain))
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002003 continue;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002004 err = tc_chain_fill_node(chain, net, skb, block,
2005 NETLINK_CB(cb->skb).portid,
2006 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2007 RTM_NEWCHAIN);
2008 if (err <= 0)
2009 break;
2010 index++;
2011 }
2012
2013 cb->args[0] = index;
2014
2015out:
2016 /* If we did no progress, the error (EMSGSIZE) is real */
2017 if (skb->len == 0 && err)
2018 return err;
2019 return skb->len;
2020}
2021
WANG Cong18d02642014-09-25 10:26:37 -07002022void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024#ifdef CONFIG_NET_CLS_ACT
Vlad Buslov90b73b72018-07-05 17:24:33 +03002025 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
WANG Cong22dc13c2016-08-13 22:35:00 -07002026 kfree(exts->actions);
2027 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028#endif
2029}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002030EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00002032int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05002033 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2034 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036#ifdef CONFIG_NET_CLS_ACT
2037 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05002039 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
WANG Cong5da57f42013-12-15 20:15:07 -08002041 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002042 act = tcf_action_init_1(net, tp, tb[exts->police],
2043 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03002044 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08002045 if (IS_ERR(act))
2046 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
WANG Cong33be6272013-12-15 20:15:05 -08002048 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07002049 exts->actions[0] = act;
2050 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08002051 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03002052 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07002053
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002054 err = tcf_action_init(net, tp, tb[exts->action],
2055 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03002056 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03002057 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03002058 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002059 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03002060 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Cong Wange4b95c42017-11-06 13:47:19 -08002062 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064#else
WANG Cong5da57f42013-12-15 20:15:07 -08002065 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05002066 (exts->police && tb[exts->police])) {
2067 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070#endif
2071
2072 return 0;
2073}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002074EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002076void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
2078#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07002079 struct tcf_exts old = *dst;
2080
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002081 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07002082 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083#endif
2084}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002085EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
WANG Cong22dc13c2016-08-13 22:35:00 -07002087#ifdef CONFIG_NET_CLS_ACT
2088static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2089{
2090 if (exts->nr_actions == 0)
2091 return NULL;
2092 else
2093 return exts->actions[0];
2094}
2095#endif
WANG Cong33be6272013-12-15 20:15:05 -08002096
WANG Cong5da57f42013-12-15 20:15:07 -08002097int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07002100 struct nlattr *nest;
2101
Jiri Pirko978dfd82017-08-04 14:29:03 +02002102 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /*
2104 * again for backward compatible mode - we want
2105 * to work with both old and new modes of entering
2106 * tc data even if iproute2 was newer - jhs
2107 */
WANG Cong33be6272013-12-15 20:15:05 -08002108 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08002109 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002110 if (nest == NULL)
2111 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07002112
Vlad Buslov90b73b72018-07-05 17:24:33 +03002113 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002114 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002115 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08002116 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08002117 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08002118 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05002119 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002120 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08002121 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002122 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002123 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07002127
2128nla_put_failure:
2129 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07002131#else
2132 return 0;
2133#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002135EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002137
WANG Cong5da57f42013-12-15 20:15:07 -08002138int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08002141 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01002142 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002143 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144#endif
2145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002147EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Jiri Pirko717503b2017-10-11 09:41:09 +02002149static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2150 enum tc_setup_type type,
2151 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002152{
2153 int ok_count = 0;
2154#ifdef CONFIG_NET_CLS_ACT
2155 const struct tc_action *a;
2156 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002157 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002158
2159 if (!tcf_exts_has_actions(exts))
2160 return 0;
2161
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002162 for (i = 0; i < exts->nr_actions; i++) {
2163 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002164 if (!a->ops->get_dev)
2165 continue;
2166 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01002167 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002168 continue;
2169 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
2170 if (ret < 0)
2171 return ret;
2172 ok_count += ret;
2173 }
2174#endif
2175 return ok_count;
2176}
Jiri Pirko717503b2017-10-11 09:41:09 +02002177
Jiri Pirko208c0f42017-10-19 15:50:32 +02002178int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2179 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02002180{
David S. Miller9a99dc12018-06-06 13:55:47 -04002181 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002182 int ret;
2183
David S. Miller9a99dc12018-06-06 13:55:47 -04002184 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2185 if (ret < 0)
2186 return ret;
2187 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002188
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03002189 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04002190 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002191 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2192 if (ret < 0)
2193 return ret;
2194 ok_count += ret;
2195
2196 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02002197}
2198EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002199
Jiri Pirko48617382018-01-17 11:46:46 +01002200static __net_init int tcf_net_init(struct net *net)
2201{
2202 struct tcf_net *tn = net_generic(net, tcf_net_id);
2203
2204 idr_init(&tn->idr);
2205 return 0;
2206}
2207
2208static void __net_exit tcf_net_exit(struct net *net)
2209{
2210 struct tcf_net *tn = net_generic(net, tcf_net_id);
2211
2212 idr_destroy(&tn->idr);
2213}
2214
2215static struct pernet_operations tcf_net_ops = {
2216 .init = tcf_net_init,
2217 .exit = tcf_net_exit,
2218 .id = &tcf_net_id,
2219 .size = sizeof(struct tcf_net),
2220};
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222static int __init tc_filter_init(void)
2223{
Jiri Pirko48617382018-01-17 11:46:46 +01002224 int err;
2225
Cong Wang7aa00452017-10-26 18:24:28 -07002226 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2227 if (!tc_filter_wq)
2228 return -ENOMEM;
2229
Jiri Pirko48617382018-01-17 11:46:46 +01002230 err = register_pernet_subsys(&tcf_net_ops);
2231 if (err)
2232 goto err_register_pernet_subsys;
2233
Vlad Buslovc431f892018-05-31 09:52:53 +03002234 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2235 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2236 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02002237 tc_dump_tfilter, 0);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002238 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2239 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2240 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2241 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01002244
2245err_register_pernet_subsys:
2246 destroy_workqueue(tc_filter_wq);
2247 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
2250subsys_initcall(tc_filter_init);