blob: 2d41c5b21b487c194280d8f9610f23e4e9d06341 [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 Pirko63cc5bc2018-08-08 14:04:13 +0200783 } else {
784 block->refcnt--;
Jiri Pirko48617382018-01-17 11:46:46 +0100785 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200786}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200787EXPORT_SYMBOL(tcf_block_put_ext);
788
789void tcf_block_put(struct tcf_block *block)
790{
791 struct tcf_block_ext_info ei = {0, };
792
Jiri Pirko4853f122017-12-21 13:13:59 +0100793 if (!block)
794 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100795 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200796}
David S. Millere1ea2f92017-10-30 14:10:01 +0900797
Jiri Pirko6529eab2017-05-17 11:07:55 +0200798EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100799
Jiri Pirkoacb67442017-10-19 15:50:31 +0200800struct tcf_block_cb {
801 struct list_head list;
802 tc_setup_cb_t *cb;
803 void *cb_ident;
804 void *cb_priv;
805 unsigned int refcnt;
806};
807
808void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
809{
810 return block_cb->cb_priv;
811}
812EXPORT_SYMBOL(tcf_block_cb_priv);
813
814struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
815 tc_setup_cb_t *cb, void *cb_ident)
816{ struct tcf_block_cb *block_cb;
817
818 list_for_each_entry(block_cb, &block->cb_list, list)
819 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
820 return block_cb;
821 return NULL;
822}
823EXPORT_SYMBOL(tcf_block_cb_lookup);
824
825void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
826{
827 block_cb->refcnt++;
828}
829EXPORT_SYMBOL(tcf_block_cb_incref);
830
831unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
832{
833 return --block_cb->refcnt;
834}
835EXPORT_SYMBOL(tcf_block_cb_decref);
836
John Hurley32636742018-06-25 14:30:10 -0700837static int
838tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
839 void *cb_priv, bool add, bool offload_in_use,
840 struct netlink_ext_ack *extack)
841{
842 struct tcf_chain *chain;
843 struct tcf_proto *tp;
844 int err;
845
846 list_for_each_entry(chain, &block->chain_list, list) {
847 for (tp = rtnl_dereference(chain->filter_chain); tp;
848 tp = rtnl_dereference(tp->next)) {
849 if (tp->ops->reoffload) {
850 err = tp->ops->reoffload(tp, add, cb, cb_priv,
851 extack);
852 if (err && add)
853 goto err_playback_remove;
854 } else if (add && offload_in_use) {
855 err = -EOPNOTSUPP;
856 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
857 goto err_playback_remove;
858 }
859 }
860 }
861
862 return 0;
863
864err_playback_remove:
865 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
866 extack);
867 return err;
868}
869
Jiri Pirkoacb67442017-10-19 15:50:31 +0200870struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
871 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700872 void *cb_priv,
873 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200874{
875 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700876 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200877
John Hurley32636742018-06-25 14:30:10 -0700878 /* Replay any already present rules */
879 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
880 tcf_block_offload_in_use(block),
881 extack);
882 if (err)
883 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100884
Jiri Pirkoacb67442017-10-19 15:50:31 +0200885 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
886 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100887 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200888 block_cb->cb = cb;
889 block_cb->cb_ident = cb_ident;
890 block_cb->cb_priv = cb_priv;
891 list_add(&block_cb->list, &block->cb_list);
892 return block_cb;
893}
894EXPORT_SYMBOL(__tcf_block_cb_register);
895
896int tcf_block_cb_register(struct tcf_block *block,
897 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700898 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200899{
900 struct tcf_block_cb *block_cb;
901
John Hurley60513bd2018-06-25 14:30:04 -0700902 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
903 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500904 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200905}
906EXPORT_SYMBOL(tcf_block_cb_register);
907
John Hurley32636742018-06-25 14:30:10 -0700908void __tcf_block_cb_unregister(struct tcf_block *block,
909 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200910{
John Hurley32636742018-06-25 14:30:10 -0700911 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
912 false, tcf_block_offload_in_use(block),
913 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200914 list_del(&block_cb->list);
915 kfree(block_cb);
916}
917EXPORT_SYMBOL(__tcf_block_cb_unregister);
918
919void tcf_block_cb_unregister(struct tcf_block *block,
920 tc_setup_cb_t *cb, void *cb_ident)
921{
922 struct tcf_block_cb *block_cb;
923
924 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
925 if (!block_cb)
926 return;
John Hurley32636742018-06-25 14:30:10 -0700927 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200928}
929EXPORT_SYMBOL(tcf_block_cb_unregister);
930
931static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
932 void *type_data, bool err_stop)
933{
934 struct tcf_block_cb *block_cb;
935 int ok_count = 0;
936 int err;
937
David S. Miller9a99dc12018-06-06 13:55:47 -0400938 /* Make sure all netdevs sharing this block are offload-capable. */
939 if (block->nooffloaddevcnt && err_stop)
940 return -EOPNOTSUPP;
941
Jiri Pirkoacb67442017-10-19 15:50:31 +0200942 list_for_each_entry(block_cb, &block->cb_list, list) {
943 err = block_cb->cb(type, type_data, block_cb->cb_priv);
944 if (err) {
945 if (err_stop)
946 return err;
947 } else {
948 ok_count++;
949 }
950 }
951 return ok_count;
952}
953
Jiri Pirko87d83092017-05-17 11:07:54 +0200954/* Main classifier routine: scans classifier chain attached
955 * to this qdisc, (optionally) tests for protocol and asks
956 * specific classifiers.
957 */
958int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
959 struct tcf_result *res, bool compat_mode)
960{
961 __be16 protocol = tc_skb_protocol(skb);
962#ifdef CONFIG_NET_CLS_ACT
963 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200964 const struct tcf_proto *orig_tp = tp;
965 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200966 int limit = 0;
967
968reclassify:
969#endif
970 for (; tp; tp = rcu_dereference_bh(tp->next)) {
971 int err;
972
973 if (tp->protocol != protocol &&
974 tp->protocol != htons(ETH_P_ALL))
975 continue;
976
977 err = tp->classify(skb, tp, res);
978#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200979 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200980 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200981 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200982 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200983 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200984 goto reset;
985 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200986#endif
987 if (err >= 0)
988 return err;
989 }
990
991 return TC_ACT_UNSPEC; /* signal: continue lookup */
992#ifdef CONFIG_NET_CLS_ACT
993reset:
994 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100995 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
996 tp->chain->block->index,
997 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200998 ntohs(tp->protocol));
999 return TC_ACT_SHOT;
1000 }
1001
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001002 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001003 protocol = tc_skb_protocol(skb);
1004 goto reclassify;
1005#endif
1006}
1007EXPORT_SYMBOL(tcf_classify);
1008
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001009struct tcf_chain_info {
1010 struct tcf_proto __rcu **pprev;
1011 struct tcf_proto __rcu *next;
1012};
1013
1014static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1015{
1016 return rtnl_dereference(*chain_info->pprev);
1017}
1018
1019static void tcf_chain_tp_insert(struct tcf_chain *chain,
1020 struct tcf_chain_info *chain_info,
1021 struct tcf_proto *tp)
1022{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001023 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001024 tcf_chain0_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001025 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1026 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -07001027 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001028}
1029
1030static void tcf_chain_tp_remove(struct tcf_chain *chain,
1031 struct tcf_chain_info *chain_info,
1032 struct tcf_proto *tp)
1033{
1034 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1035
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001036 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001037 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001038 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -07001039 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001040}
1041
1042static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1043 struct tcf_chain_info *chain_info,
1044 u32 protocol, u32 prio,
1045 bool prio_allocate)
1046{
1047 struct tcf_proto **pprev;
1048 struct tcf_proto *tp;
1049
1050 /* Check the chain for existence of proto-tcf with this priority */
1051 for (pprev = &chain->filter_chain;
1052 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1053 if (tp->prio >= prio) {
1054 if (tp->prio == prio) {
1055 if (prio_allocate ||
1056 (tp->protocol != protocol && protocol))
1057 return ERR_PTR(-EINVAL);
1058 } else {
1059 tp = NULL;
1060 }
1061 break;
1062 }
1063 }
1064 chain_info->pprev = pprev;
1065 chain_info->next = tp ? tp->next : NULL;
1066 return tp;
1067}
1068
WANG Cong71203712017-08-07 15:26:50 -07001069static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001070 struct tcf_proto *tp, struct tcf_block *block,
1071 struct Qdisc *q, u32 parent, void *fh,
1072 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -07001073{
1074 struct tcmsg *tcm;
1075 struct nlmsghdr *nlh;
1076 unsigned char *b = skb_tail_pointer(skb);
1077
1078 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1079 if (!nlh)
1080 goto out_nlmsg_trim;
1081 tcm = nlmsg_data(nlh);
1082 tcm->tcm_family = AF_UNSPEC;
1083 tcm->tcm__pad1 = 0;
1084 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001085 if (q) {
1086 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1087 tcm->tcm_parent = parent;
1088 } else {
1089 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1090 tcm->tcm_block_index = block->index;
1091 }
WANG Cong71203712017-08-07 15:26:50 -07001092 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1093 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1094 goto nla_put_failure;
1095 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1096 goto nla_put_failure;
1097 if (!fh) {
1098 tcm->tcm_handle = 0;
1099 } else {
1100 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1101 goto nla_put_failure;
1102 }
1103 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1104 return skb->len;
1105
1106out_nlmsg_trim:
1107nla_put_failure:
1108 nlmsg_trim(skb, b);
1109 return -1;
1110}
1111
1112static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1113 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001114 struct tcf_block *block, struct Qdisc *q,
1115 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001116{
1117 struct sk_buff *skb;
1118 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1119
1120 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1121 if (!skb)
1122 return -ENOBUFS;
1123
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001124 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1125 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001126 kfree_skb(skb);
1127 return -EINVAL;
1128 }
1129
1130 if (unicast)
1131 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1132
1133 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1134 n->nlmsg_flags & NLM_F_ECHO);
1135}
1136
1137static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1138 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001139 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001140 u32 parent, void *fh, bool unicast, bool *last,
1141 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001142{
1143 struct sk_buff *skb;
1144 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1145 int err;
1146
1147 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1148 if (!skb)
1149 return -ENOBUFS;
1150
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001151 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1152 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001153 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001154 kfree_skb(skb);
1155 return -EINVAL;
1156 }
1157
Alexander Aring571acf22018-01-18 11:20:53 -05001158 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001159 if (err) {
1160 kfree_skb(skb);
1161 return err;
1162 }
1163
1164 if (unicast)
1165 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1166
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001167 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1168 n->nlmsg_flags & NLM_F_ECHO);
1169 if (err < 0)
1170 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1171 return err;
WANG Cong71203712017-08-07 15:26:50 -07001172}
1173
1174static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001175 struct tcf_block *block, struct Qdisc *q,
1176 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001177 struct tcf_chain *chain, int event)
1178{
1179 struct tcf_proto *tp;
1180
1181 for (tp = rtnl_dereference(chain->filter_chain);
1182 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001183 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001184 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001185}
1186
Vlad Buslovc431f892018-05-31 09:52:53 +03001187static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001188 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001190 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001191 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 struct tcmsg *t;
1193 u32 protocol;
1194 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001195 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001197 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001198 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001199 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001200 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001201 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001204 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001206 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Vlad Buslovc431f892018-05-31 09:52:53 +03001208 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001209 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001212 tp_created = 0;
1213
David Ahernc21ef3e2017-04-16 09:48:24 -07001214 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001215 if (err < 0)
1216 return err;
1217
David S. Miller942b8162012-06-26 21:48:50 -07001218 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 protocol = TC_H_MIN(t->tcm_info);
1220 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001221 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 parent = t->tcm_parent;
1223 cl = 0;
1224
1225 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001226 /* If no priority is provided by the user,
1227 * we allocate one.
1228 */
1229 if (n->nlmsg_flags & NLM_F_CREATE) {
1230 prio = TC_H_MAKE(0x80000000U, 0U);
1231 prio_allocate = true;
1232 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001233 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237
1238 /* Find head of filter chain. */
1239
Vlad Buslovc431f892018-05-31 09:52:53 +03001240 block = tcf_block_find(net, &q, &parent, &cl,
1241 t->tcm_ifindex, t->tcm_block_index, extack);
1242 if (IS_ERR(block)) {
1243 err = PTR_ERR(block);
1244 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001245 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001246
1247 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1248 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001249 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001250 err = -EINVAL;
1251 goto errout;
1252 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001253 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001254 if (!chain) {
Jiri Pirkod5ed72a2018-08-27 20:58:43 +02001255 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001256 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001257 goto errout;
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001260 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1261 prio, prio_allocate);
1262 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001263 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001264 err = PTR_ERR(tp);
1265 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
1268 if (tp == NULL) {
1269 /* Proto-tcf does not exist, create new one */
1270
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001271 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001272 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001273 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Vlad Buslovc431f892018-05-31 09:52:53 +03001277 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001278 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 +01001279 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001283 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001284 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Jiri Pirko33a48922017-02-09 14:38:57 +01001286 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001287 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001288 if (IS_ERR(tp)) {
1289 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto errout;
1291 }
Minoru Usui12186be2009-06-02 02:17:34 -07001292 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001293 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001294 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001295 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 fh = tp->ops->get(tp, t->tcm_handle);
1300
WANG Cong8113c092017-08-04 21:31:43 -07001301 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001302 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001303 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 +01001304 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001306 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001307 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1308 NL_SET_ERR_MSG(extack, "Filter already exists");
1309 err = -EEXIST;
1310 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
Jiri Pirko9f407f12018-07-23 09:23:07 +02001313 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1314 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1315 err = -EINVAL;
1316 goto errout;
1317 }
1318
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001319 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001320 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1321 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001322 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001323 if (tp_created)
1324 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001325 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001326 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001327 } else {
1328 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001329 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001333 if (chain)
1334 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (err == -EAGAIN)
1336 /* Replay the request. */
1337 goto replay;
1338 return err;
1339}
1340
Vlad Buslovc431f892018-05-31 09:52:53 +03001341static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1342 struct netlink_ext_ack *extack)
1343{
1344 struct net *net = sock_net(skb->sk);
1345 struct nlattr *tca[TCA_MAX + 1];
1346 struct tcmsg *t;
1347 u32 protocol;
1348 u32 prio;
1349 u32 parent;
1350 u32 chain_index;
1351 struct Qdisc *q = NULL;
1352 struct tcf_chain_info chain_info;
1353 struct tcf_chain *chain = NULL;
1354 struct tcf_block *block;
1355 struct tcf_proto *tp = NULL;
1356 unsigned long cl = 0;
1357 void *fh = NULL;
1358 int err;
1359
1360 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1361 return -EPERM;
1362
1363 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1364 if (err < 0)
1365 return err;
1366
1367 t = nlmsg_data(n);
1368 protocol = TC_H_MIN(t->tcm_info);
1369 prio = TC_H_MAJ(t->tcm_info);
1370 parent = t->tcm_parent;
1371
1372 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1373 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1374 return -ENOENT;
1375 }
1376
1377 /* Find head of filter chain. */
1378
1379 block = tcf_block_find(net, &q, &parent, &cl,
1380 t->tcm_ifindex, t->tcm_block_index, extack);
1381 if (IS_ERR(block)) {
1382 err = PTR_ERR(block);
1383 goto errout;
1384 }
1385
1386 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1387 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1388 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1389 err = -EINVAL;
1390 goto errout;
1391 }
1392 chain = tcf_chain_get(block, chain_index, false);
1393 if (!chain) {
Jiri Pirko5ca8a252018-08-03 11:08:47 +02001394 /* User requested flush on non-existent chain. Nothing to do,
1395 * so just return success.
1396 */
1397 if (prio == 0) {
1398 err = 0;
1399 goto errout;
1400 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001401 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1402 err = -EINVAL;
1403 goto errout;
1404 }
1405
1406 if (prio == 0) {
1407 tfilter_notify_chain(net, skb, block, q, parent, n,
1408 chain, RTM_DELTFILTER);
1409 tcf_chain_flush(chain);
1410 err = 0;
1411 goto errout;
1412 }
1413
1414 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1415 prio, false);
1416 if (!tp || IS_ERR(tp)) {
1417 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001418 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001419 goto errout;
1420 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1421 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1422 err = -EINVAL;
1423 goto errout;
1424 }
1425
1426 fh = tp->ops->get(tp, t->tcm_handle);
1427
1428 if (!fh) {
1429 if (t->tcm_handle == 0) {
1430 tcf_chain_tp_remove(chain, &chain_info, tp);
1431 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1432 RTM_DELTFILTER, false);
1433 tcf_proto_destroy(tp, extack);
1434 err = 0;
1435 } else {
1436 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1437 err = -ENOENT;
1438 }
1439 } else {
1440 bool last;
1441
1442 err = tfilter_del_notify(net, skb, n, tp, block,
1443 q, parent, fh, false, &last,
1444 extack);
1445 if (err)
1446 goto errout;
1447 if (last) {
1448 tcf_chain_tp_remove(chain, &chain_info, tp);
1449 tcf_proto_destroy(tp, extack);
1450 }
1451 }
1452
1453errout:
1454 if (chain)
1455 tcf_chain_put(chain);
1456 return err;
1457}
1458
1459static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1460 struct netlink_ext_ack *extack)
1461{
1462 struct net *net = sock_net(skb->sk);
1463 struct nlattr *tca[TCA_MAX + 1];
1464 struct tcmsg *t;
1465 u32 protocol;
1466 u32 prio;
1467 u32 parent;
1468 u32 chain_index;
1469 struct Qdisc *q = NULL;
1470 struct tcf_chain_info chain_info;
1471 struct tcf_chain *chain = NULL;
1472 struct tcf_block *block;
1473 struct tcf_proto *tp = NULL;
1474 unsigned long cl = 0;
1475 void *fh = NULL;
1476 int err;
1477
1478 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1479 if (err < 0)
1480 return err;
1481
1482 t = nlmsg_data(n);
1483 protocol = TC_H_MIN(t->tcm_info);
1484 prio = TC_H_MAJ(t->tcm_info);
1485 parent = t->tcm_parent;
1486
1487 if (prio == 0) {
1488 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1489 return -ENOENT;
1490 }
1491
1492 /* Find head of filter chain. */
1493
1494 block = tcf_block_find(net, &q, &parent, &cl,
1495 t->tcm_ifindex, t->tcm_block_index, extack);
1496 if (IS_ERR(block)) {
1497 err = PTR_ERR(block);
1498 goto errout;
1499 }
1500
1501 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1502 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1503 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1504 err = -EINVAL;
1505 goto errout;
1506 }
1507 chain = tcf_chain_get(block, chain_index, false);
1508 if (!chain) {
1509 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1510 err = -EINVAL;
1511 goto errout;
1512 }
1513
1514 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1515 prio, false);
1516 if (!tp || IS_ERR(tp)) {
1517 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001518 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001519 goto errout;
1520 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1521 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1522 err = -EINVAL;
1523 goto errout;
1524 }
1525
1526 fh = tp->ops->get(tp, t->tcm_handle);
1527
1528 if (!fh) {
1529 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1530 err = -ENOENT;
1531 } else {
1532 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1533 fh, RTM_NEWTFILTER, true);
1534 if (err < 0)
1535 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1536 }
1537
1538errout:
1539 if (chain)
1540 tcf_chain_put(chain);
1541 return err;
1542}
1543
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001544struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct tcf_walker w;
1546 struct sk_buff *skb;
1547 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001548 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001549 struct Qdisc *q;
1550 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551};
1552
WANG Cong8113c092017-08-04 21:31:43 -07001553static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001555 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001556 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001558 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001559 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001560 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1561 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001564static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1565 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001566 long index_start, long *p_index)
1567{
1568 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001569 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001570 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1571 struct tcf_dump_args arg;
1572 struct tcf_proto *tp;
1573
1574 for (tp = rtnl_dereference(chain->filter_chain);
1575 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1576 if (*p_index < index_start)
1577 continue;
1578 if (TC_H_MAJ(tcm->tcm_info) &&
1579 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1580 continue;
1581 if (TC_H_MIN(tcm->tcm_info) &&
1582 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1583 continue;
1584 if (*p_index > index_start)
1585 memset(&cb->args[1], 0,
1586 sizeof(cb->args) - sizeof(cb->args[0]));
1587 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001588 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001589 NETLINK_CB(cb->skb).portid,
1590 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1591 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001592 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001593
1594 cb->args[1] = 1;
1595 }
1596 if (!tp->ops->walk)
1597 continue;
1598 arg.w.fn = tcf_node_dump;
1599 arg.skb = skb;
1600 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001601 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001602 arg.q = q;
1603 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001604 arg.w.stop = 0;
1605 arg.w.skip = cb->args[1] - 1;
1606 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001607 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001608 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001609 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001610 cb->args[1] = arg.w.count + 1;
1611 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001612 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001613 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001614 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001615}
1616
Eric Dumazetbd27a872009-11-05 20:57:26 -08001617/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1619{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001620 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001621 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001622 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001623 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001624 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001625 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001626 long index_start;
1627 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001628 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001629 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Hong zhi guo573ce262013-03-27 06:47:04 +00001631 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001633
1634 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1635 if (err)
1636 return err;
1637
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001638 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1639 block = tcf_block_lookup(net, tcm->tcm_block_index);
1640 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001641 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001642 /* If we work with block index, q is NULL and parent value
1643 * will never be used in the following code. The check
1644 * in tcf_fill_node prevents it. However, compiler does not
1645 * see that far, so set parent to zero to silence the warning
1646 * about parent being uninitialized.
1647 */
1648 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001649 } else {
1650 const struct Qdisc_class_ops *cops;
1651 struct net_device *dev;
1652 unsigned long cl = 0;
1653
1654 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1655 if (!dev)
1656 return skb->len;
1657
1658 parent = tcm->tcm_parent;
1659 if (!parent) {
1660 q = dev->qdisc;
1661 parent = q->handle;
1662 } else {
1663 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1664 }
1665 if (!q)
1666 goto out;
1667 cops = q->ops->cl_ops;
1668 if (!cops)
1669 goto out;
1670 if (!cops->tcf_block)
1671 goto out;
1672 if (TC_H_MIN(tcm->tcm_parent)) {
1673 cl = cops->find(q, tcm->tcm_parent);
1674 if (cl == 0)
1675 goto out;
1676 }
1677 block = cops->tcf_block(q, cl, NULL);
1678 if (!block)
1679 goto out;
1680 if (tcf_block_shared(block))
1681 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001684 index_start = cb->args[0];
1685 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001686
1687 list_for_each_entry(chain, &block->chain_list, list) {
1688 if (tca[TCA_CHAIN] &&
1689 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1690 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001691 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001692 index_start, &index)) {
1693 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001694 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001695 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001696 }
1697
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001698 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001701 /* If we did no progress, the error (EMSGSIZE) is real */
1702 if (skb->len == 0 && err)
1703 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return skb->len;
1705}
1706
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001707static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1708 struct sk_buff *skb, struct tcf_block *block,
1709 u32 portid, u32 seq, u16 flags, int event)
1710{
1711 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02001712 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001713 struct nlmsghdr *nlh;
1714 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02001715 void *priv;
1716
1717 ops = chain->tmplt_ops;
1718 priv = chain->tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001719
1720 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1721 if (!nlh)
1722 goto out_nlmsg_trim;
1723 tcm = nlmsg_data(nlh);
1724 tcm->tcm_family = AF_UNSPEC;
1725 tcm->tcm__pad1 = 0;
1726 tcm->tcm__pad2 = 0;
1727 tcm->tcm_handle = 0;
1728 if (block->q) {
1729 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1730 tcm->tcm_parent = block->q->handle;
1731 } else {
1732 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1733 tcm->tcm_block_index = block->index;
1734 }
1735
1736 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1737 goto nla_put_failure;
1738
Jiri Pirko9f407f12018-07-23 09:23:07 +02001739 if (ops) {
1740 if (nla_put_string(skb, TCA_KIND, ops->kind))
1741 goto nla_put_failure;
1742 if (ops->tmplt_dump(skb, net, priv) < 0)
1743 goto nla_put_failure;
1744 }
1745
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001746 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1747 return skb->len;
1748
1749out_nlmsg_trim:
1750nla_put_failure:
1751 nlmsg_trim(skb, b);
1752 return -EMSGSIZE;
1753}
1754
1755static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1756 u32 seq, u16 flags, int event, bool unicast)
1757{
1758 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1759 struct tcf_block *block = chain->block;
1760 struct net *net = block->net;
1761 struct sk_buff *skb;
1762
1763 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1764 if (!skb)
1765 return -ENOBUFS;
1766
1767 if (tc_chain_fill_node(chain, net, skb, block, portid,
1768 seq, flags, event) <= 0) {
1769 kfree_skb(skb);
1770 return -EINVAL;
1771 }
1772
1773 if (unicast)
1774 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1775
1776 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1777}
1778
Jiri Pirko9f407f12018-07-23 09:23:07 +02001779static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1780 struct nlattr **tca,
1781 struct netlink_ext_ack *extack)
1782{
1783 const struct tcf_proto_ops *ops;
1784 void *tmplt_priv;
1785
1786 /* If kind is not set, user did not specify template. */
1787 if (!tca[TCA_KIND])
1788 return 0;
1789
1790 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1791 if (IS_ERR(ops))
1792 return PTR_ERR(ops);
1793 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1794 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1795 return -EOPNOTSUPP;
1796 }
1797
1798 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1799 if (IS_ERR(tmplt_priv)) {
1800 module_put(ops->owner);
1801 return PTR_ERR(tmplt_priv);
1802 }
1803 chain->tmplt_ops = ops;
1804 chain->tmplt_priv = tmplt_priv;
1805 return 0;
1806}
1807
1808static void tc_chain_tmplt_del(struct tcf_chain *chain)
1809{
1810 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1811
1812 /* If template ops are set, no work to do for us. */
1813 if (!ops)
1814 return;
1815
1816 ops->tmplt_destroy(chain->tmplt_priv);
1817 module_put(ops->owner);
1818}
1819
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001820/* Add/delete/get a chain */
1821
1822static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1823 struct netlink_ext_ack *extack)
1824{
1825 struct net *net = sock_net(skb->sk);
1826 struct nlattr *tca[TCA_MAX + 1];
1827 struct tcmsg *t;
1828 u32 parent;
1829 u32 chain_index;
1830 struct Qdisc *q = NULL;
1831 struct tcf_chain *chain = NULL;
1832 struct tcf_block *block;
1833 unsigned long cl;
1834 int err;
1835
1836 if (n->nlmsg_type != RTM_GETCHAIN &&
1837 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1838 return -EPERM;
1839
1840replay:
1841 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1842 if (err < 0)
1843 return err;
1844
1845 t = nlmsg_data(n);
1846 parent = t->tcm_parent;
1847 cl = 0;
1848
1849 block = tcf_block_find(net, &q, &parent, &cl,
1850 t->tcm_ifindex, t->tcm_block_index, extack);
1851 if (IS_ERR(block))
1852 return PTR_ERR(block);
1853
1854 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1855 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1856 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1857 return -EINVAL;
1858 }
1859 chain = tcf_chain_lookup(block, chain_index);
1860 if (n->nlmsg_type == RTM_NEWCHAIN) {
1861 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001862 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001863 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001864 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001865 */
1866 tcf_chain_hold(chain);
1867 } else {
1868 NL_SET_ERR_MSG(extack, "Filter chain already exists");
1869 return -EEXIST;
1870 }
1871 } else {
1872 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1873 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
1874 return -ENOENT;
1875 }
1876 chain = tcf_chain_create(block, chain_index);
1877 if (!chain) {
1878 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
1879 return -ENOMEM;
1880 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001881 }
1882 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001883 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001884 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1885 return -EINVAL;
1886 }
1887 tcf_chain_hold(chain);
1888 }
1889
1890 switch (n->nlmsg_type) {
1891 case RTM_NEWCHAIN:
Jiri Pirko9f407f12018-07-23 09:23:07 +02001892 err = tc_chain_tmplt_add(chain, net, tca, extack);
1893 if (err)
1894 goto errout;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001895 /* In case the chain was successfully added, take a reference
1896 * to the chain. This ensures that an empty chain
1897 * does not disappear at the end of this function.
1898 */
1899 tcf_chain_hold(chain);
1900 chain->explicitly_created = true;
1901 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1902 RTM_NEWCHAIN, false);
1903 break;
1904 case RTM_DELCHAIN:
1905 /* Flush the chain first as the user requested chain removal. */
1906 tcf_chain_flush(chain);
1907 /* In case the chain was successfully deleted, put a reference
1908 * to the chain previously taken during addition.
1909 */
1910 tcf_chain_put_explicitly_created(chain);
Jiri Pirkoc921d7d2018-07-26 18:27:58 +02001911 chain->explicitly_created = false;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001912 break;
1913 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001914 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1915 n->nlmsg_seq, n->nlmsg_type, true);
1916 if (err < 0)
1917 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1918 break;
1919 default:
1920 err = -EOPNOTSUPP;
1921 NL_SET_ERR_MSG(extack, "Unsupported message type");
1922 goto errout;
1923 }
1924
1925errout:
1926 tcf_chain_put(chain);
1927 if (err == -EAGAIN)
1928 /* Replay the request. */
1929 goto replay;
1930 return err;
1931}
1932
1933/* called with RTNL */
1934static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
1935{
1936 struct net *net = sock_net(skb->sk);
1937 struct nlattr *tca[TCA_MAX + 1];
1938 struct Qdisc *q = NULL;
1939 struct tcf_block *block;
1940 struct tcf_chain *chain;
1941 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1942 long index_start;
1943 long index;
1944 u32 parent;
1945 int err;
1946
1947 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1948 return skb->len;
1949
1950 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1951 if (err)
1952 return err;
1953
1954 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1955 block = tcf_block_lookup(net, tcm->tcm_block_index);
1956 if (!block)
1957 goto out;
1958 /* If we work with block index, q is NULL and parent value
1959 * will never be used in the following code. The check
1960 * in tcf_fill_node prevents it. However, compiler does not
1961 * see that far, so set parent to zero to silence the warning
1962 * about parent being uninitialized.
1963 */
1964 parent = 0;
1965 } else {
1966 const struct Qdisc_class_ops *cops;
1967 struct net_device *dev;
1968 unsigned long cl = 0;
1969
1970 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1971 if (!dev)
1972 return skb->len;
1973
1974 parent = tcm->tcm_parent;
1975 if (!parent) {
1976 q = dev->qdisc;
1977 parent = q->handle;
1978 } else {
1979 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1980 }
1981 if (!q)
1982 goto out;
1983 cops = q->ops->cl_ops;
1984 if (!cops)
1985 goto out;
1986 if (!cops->tcf_block)
1987 goto out;
1988 if (TC_H_MIN(tcm->tcm_parent)) {
1989 cl = cops->find(q, tcm->tcm_parent);
1990 if (cl == 0)
1991 goto out;
1992 }
1993 block = cops->tcf_block(q, cl, NULL);
1994 if (!block)
1995 goto out;
1996 if (tcf_block_shared(block))
1997 q = NULL;
1998 }
1999
2000 index_start = cb->args[0];
2001 index = 0;
2002
2003 list_for_each_entry(chain, &block->chain_list, list) {
2004 if ((tca[TCA_CHAIN] &&
2005 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2006 continue;
2007 if (index < index_start) {
2008 index++;
2009 continue;
2010 }
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002011 if (tcf_chain_held_by_acts_only(chain))
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002012 continue;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002013 err = tc_chain_fill_node(chain, net, skb, block,
2014 NETLINK_CB(cb->skb).portid,
2015 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2016 RTM_NEWCHAIN);
2017 if (err <= 0)
2018 break;
2019 index++;
2020 }
2021
2022 cb->args[0] = index;
2023
2024out:
2025 /* If we did no progress, the error (EMSGSIZE) is real */
2026 if (skb->len == 0 && err)
2027 return err;
2028 return skb->len;
2029}
2030
WANG Cong18d02642014-09-25 10:26:37 -07002031void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033#ifdef CONFIG_NET_CLS_ACT
Vlad Buslov90b73b72018-07-05 17:24:33 +03002034 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
WANG Cong22dc13c2016-08-13 22:35:00 -07002035 kfree(exts->actions);
2036 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037#endif
2038}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002039EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00002041int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05002042 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2043 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045#ifdef CONFIG_NET_CLS_ACT
2046 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05002048 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
WANG Cong5da57f42013-12-15 20:15:07 -08002050 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002051 act = tcf_action_init_1(net, tp, tb[exts->police],
2052 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03002053 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08002054 if (IS_ERR(act))
2055 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
WANG Cong33be6272013-12-15 20:15:05 -08002057 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07002058 exts->actions[0] = act;
2059 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08002060 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03002061 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07002062
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002063 err = tcf_action_init(net, tp, tb[exts->action],
2064 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03002065 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03002066 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03002067 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002068 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03002069 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Cong Wange4b95c42017-11-06 13:47:19 -08002071 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073#else
WANG Cong5da57f42013-12-15 20:15:07 -08002074 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05002075 (exts->police && tb[exts->police])) {
2076 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05002078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079#endif
2080
2081 return 0;
2082}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002083EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002085void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07002088 struct tcf_exts old = *dst;
2089
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002090 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07002091 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092#endif
2093}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002094EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
WANG Cong22dc13c2016-08-13 22:35:00 -07002096#ifdef CONFIG_NET_CLS_ACT
2097static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2098{
2099 if (exts->nr_actions == 0)
2100 return NULL;
2101 else
2102 return exts->actions[0];
2103}
2104#endif
WANG Cong33be6272013-12-15 20:15:05 -08002105
WANG Cong5da57f42013-12-15 20:15:07 -08002106int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07002109 struct nlattr *nest;
2110
Jiri Pirko978dfd82017-08-04 14:29:03 +02002111 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 /*
2113 * again for backward compatible mode - we want
2114 * to work with both old and new modes of entering
2115 * tc data even if iproute2 was newer - jhs
2116 */
WANG Cong33be6272013-12-15 20:15:05 -08002117 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08002118 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002119 if (nest == NULL)
2120 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07002121
Vlad Buslov90b73b72018-07-05 17:24:33 +03002122 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002123 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002124 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08002125 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08002126 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08002127 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05002128 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002129 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08002130 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002131 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002132 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07002136
2137nla_put_failure:
2138 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07002140#else
2141 return 0;
2142#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002144EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002146
WANG Cong5da57f42013-12-15 20:15:07 -08002147int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08002150 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01002151 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002152 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153#endif
2154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002156EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Jiri Pirko717503b2017-10-11 09:41:09 +02002158static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2159 enum tc_setup_type type,
2160 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002161{
2162 int ok_count = 0;
2163#ifdef CONFIG_NET_CLS_ACT
2164 const struct tc_action *a;
2165 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002166 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002167
2168 if (!tcf_exts_has_actions(exts))
2169 return 0;
2170
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002171 for (i = 0; i < exts->nr_actions; i++) {
2172 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002173 if (!a->ops->get_dev)
2174 continue;
2175 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01002176 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002177 continue;
2178 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
Vlad Buslov84a75b32018-08-10 20:51:52 +03002179 a->ops->put_dev(dev);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002180 if (ret < 0)
2181 return ret;
2182 ok_count += ret;
2183 }
2184#endif
2185 return ok_count;
2186}
Jiri Pirko717503b2017-10-11 09:41:09 +02002187
Jiri Pirko208c0f42017-10-19 15:50:32 +02002188int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2189 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02002190{
David S. Miller9a99dc12018-06-06 13:55:47 -04002191 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002192 int ret;
2193
David S. Miller9a99dc12018-06-06 13:55:47 -04002194 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2195 if (ret < 0)
2196 return ret;
2197 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002198
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03002199 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04002200 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002201 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2202 if (ret < 0)
2203 return ret;
2204 ok_count += ret;
2205
2206 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02002207}
2208EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002209
Jiri Pirko48617382018-01-17 11:46:46 +01002210static __net_init int tcf_net_init(struct net *net)
2211{
2212 struct tcf_net *tn = net_generic(net, tcf_net_id);
2213
2214 idr_init(&tn->idr);
2215 return 0;
2216}
2217
2218static void __net_exit tcf_net_exit(struct net *net)
2219{
2220 struct tcf_net *tn = net_generic(net, tcf_net_id);
2221
2222 idr_destroy(&tn->idr);
2223}
2224
2225static struct pernet_operations tcf_net_ops = {
2226 .init = tcf_net_init,
2227 .exit = tcf_net_exit,
2228 .id = &tcf_net_id,
2229 .size = sizeof(struct tcf_net),
2230};
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232static int __init tc_filter_init(void)
2233{
Jiri Pirko48617382018-01-17 11:46:46 +01002234 int err;
2235
Cong Wang7aa00452017-10-26 18:24:28 -07002236 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2237 if (!tc_filter_wq)
2238 return -ENOMEM;
2239
Jiri Pirko48617382018-01-17 11:46:46 +01002240 err = register_pernet_subsys(&tcf_net_ops);
2241 if (err)
2242 goto err_register_pernet_subsys;
2243
Vlad Buslovc431f892018-05-31 09:52:53 +03002244 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2245 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2246 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02002247 tc_dump_tfilter, 0);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002248 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2249 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2250 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2251 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01002254
2255err_register_pernet_subsys:
2256 destroy_workqueue(tc_filter_wq);
2257 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
2260subsys_initcall(tc_filter_init);