blob: c33636e7b43127c108f0fefdbe9032d3bbc7fe5c [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;
Vlad Buslove368fdb2018-09-24 19:22:53 +0300540 int err = 0;
Vlad Buslovc431f892018-05-31 09:52:53 +0300541
542 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
543 block = tcf_block_lookup(net, block_index);
544 if (!block) {
545 NL_SET_ERR_MSG(extack, "Block of given index was not found");
546 return ERR_PTR(-EINVAL);
547 }
548 } else {
549 const struct Qdisc_class_ops *cops;
550 struct net_device *dev;
551
Vlad Buslove368fdb2018-09-24 19:22:53 +0300552 rcu_read_lock();
553
Vlad Buslovc431f892018-05-31 09:52:53 +0300554 /* Find link */
Vlad Buslove368fdb2018-09-24 19:22:53 +0300555 dev = dev_get_by_index_rcu(net, ifindex);
556 if (!dev) {
557 rcu_read_unlock();
Vlad Buslovc431f892018-05-31 09:52:53 +0300558 return ERR_PTR(-ENODEV);
Vlad Buslove368fdb2018-09-24 19:22:53 +0300559 }
Vlad Buslovc431f892018-05-31 09:52:53 +0300560
561 /* Find qdisc */
562 if (!*parent) {
563 *q = dev->qdisc;
564 *parent = (*q)->handle;
565 } else {
Vlad Buslove368fdb2018-09-24 19:22:53 +0300566 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
Vlad Buslovc431f892018-05-31 09:52:53 +0300567 if (!*q) {
568 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300569 err = -EINVAL;
570 goto errout_rcu;
Vlad Buslovc431f892018-05-31 09:52:53 +0300571 }
572 }
573
Vlad Buslove368fdb2018-09-24 19:22:53 +0300574 *q = qdisc_refcount_inc_nz(*q);
575 if (!*q) {
576 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
577 err = -EINVAL;
578 goto errout_rcu;
579 }
580
Vlad Buslovc431f892018-05-31 09:52:53 +0300581 /* Is it classful? */
582 cops = (*q)->ops->cl_ops;
583 if (!cops) {
584 NL_SET_ERR_MSG(extack, "Qdisc not classful");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300585 err = -EINVAL;
586 goto errout_rcu;
Vlad Buslovc431f892018-05-31 09:52:53 +0300587 }
588
589 if (!cops->tcf_block) {
590 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300591 err = -EOPNOTSUPP;
592 goto errout_rcu;
Vlad Buslovc431f892018-05-31 09:52:53 +0300593 }
594
Vlad Buslove368fdb2018-09-24 19:22:53 +0300595 /* At this point we know that qdisc is not noop_qdisc,
596 * which means that qdisc holds a reference to net_device
597 * and we hold a reference to qdisc, so it is safe to release
598 * rcu read lock.
599 */
600 rcu_read_unlock();
601
Vlad Buslovc431f892018-05-31 09:52:53 +0300602 /* Do we search for filter, attached to class? */
603 if (TC_H_MIN(*parent)) {
604 *cl = cops->find(*q, *parent);
605 if (*cl == 0) {
606 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300607 err = -ENOENT;
608 goto errout_qdisc;
Vlad Buslovc431f892018-05-31 09:52:53 +0300609 }
610 }
611
612 /* And the last stroke */
613 block = cops->tcf_block(*q, *cl, extack);
Vlad Buslove368fdb2018-09-24 19:22:53 +0300614 if (!block) {
615 err = -EINVAL;
616 goto errout_qdisc;
617 }
Vlad Buslovc431f892018-05-31 09:52:53 +0300618 if (tcf_block_shared(block)) {
619 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300620 err = -EOPNOTSUPP;
621 goto errout_qdisc;
Vlad Buslovc431f892018-05-31 09:52:53 +0300622 }
623 }
624
625 return block;
Vlad Buslove368fdb2018-09-24 19:22:53 +0300626
627errout_rcu:
628 rcu_read_unlock();
629errout_qdisc:
630 if (*q)
631 qdisc_put(*q);
632 return ERR_PTR(err);
633}
634
635static void tcf_block_release(struct Qdisc *q, struct tcf_block *block)
636{
637 if (q)
638 qdisc_put(q);
Vlad Buslovc431f892018-05-31 09:52:53 +0300639}
640
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100641struct tcf_block_owner_item {
642 struct list_head list;
643 struct Qdisc *q;
644 enum tcf_block_binder_type binder_type;
645};
646
647static void
648tcf_block_owner_netif_keep_dst(struct tcf_block *block,
649 struct Qdisc *q,
650 enum tcf_block_binder_type binder_type)
651{
652 if (block->keep_dst &&
653 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
654 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
655 netif_keep_dst(qdisc_dev(q));
656}
657
658void tcf_block_netif_keep_dst(struct tcf_block *block)
659{
660 struct tcf_block_owner_item *item;
661
662 block->keep_dst = true;
663 list_for_each_entry(item, &block->owner_list, list)
664 tcf_block_owner_netif_keep_dst(block, item->q,
665 item->binder_type);
666}
667EXPORT_SYMBOL(tcf_block_netif_keep_dst);
668
669static int tcf_block_owner_add(struct tcf_block *block,
670 struct Qdisc *q,
671 enum tcf_block_binder_type binder_type)
672{
673 struct tcf_block_owner_item *item;
674
675 item = kmalloc(sizeof(*item), GFP_KERNEL);
676 if (!item)
677 return -ENOMEM;
678 item->q = q;
679 item->binder_type = binder_type;
680 list_add(&item->list, &block->owner_list);
681 return 0;
682}
683
684static void tcf_block_owner_del(struct tcf_block *block,
685 struct Qdisc *q,
686 enum tcf_block_binder_type binder_type)
687{
688 struct tcf_block_owner_item *item;
689
690 list_for_each_entry(item, &block->owner_list, list) {
691 if (item->q == q && item->binder_type == binder_type) {
692 list_del(&item->list);
693 kfree(item);
694 return;
695 }
696 }
697 WARN_ON(1);
698}
699
Jiri Pirko48617382018-01-17 11:46:46 +0100700int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
701 struct tcf_block_ext_info *ei,
702 struct netlink_ext_ack *extack)
703{
704 struct net *net = qdisc_net(q);
705 struct tcf_block *block = NULL;
706 bool created = false;
707 int err;
708
709 if (ei->block_index) {
710 /* block_index not 0 means the shared block is requested */
711 block = tcf_block_lookup(net, ei->block_index);
712 if (block)
713 block->refcnt++;
714 }
715
716 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100717 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100718 if (IS_ERR(block))
719 return PTR_ERR(block);
720 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100721 if (tcf_block_shared(block)) {
722 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100723 if (err)
724 goto err_block_insert;
725 }
726 }
727
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100728 err = tcf_block_owner_add(block, q, ei->binder_type);
729 if (err)
730 goto err_block_owner_add;
731
732 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
733
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200734 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100735 if (err)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200736 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100737
John Hurley60513bd2018-06-25 14:30:04 -0700738 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100739 if (err)
740 goto err_block_offload_bind;
741
Jiri Pirko6529eab2017-05-17 11:07:55 +0200742 *p_block = block;
743 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200744
Jiri Pirkocaa72602018-01-17 11:46:50 +0100745err_block_offload_bind:
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200746 tcf_chain0_head_change_cb_del(block, ei);
747err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100748 tcf_block_owner_del(block, q, ei->binder_type);
749err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100750 if (created) {
751 if (tcf_block_shared(block))
752 tcf_block_remove(block, net);
753err_block_insert:
Jiri Pirko48617382018-01-17 11:46:46 +0100754 kfree(block);
755 } else {
756 block->refcnt--;
757 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200758 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200759}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200760EXPORT_SYMBOL(tcf_block_get_ext);
761
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100762static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
763{
764 struct tcf_proto __rcu **p_filter_chain = priv;
765
766 rcu_assign_pointer(*p_filter_chain, tp_head);
767}
768
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200769int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500770 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
771 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200772{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100773 struct tcf_block_ext_info ei = {
774 .chain_head_change = tcf_chain_head_change_dflt,
775 .chain_head_change_priv = p_filter_chain,
776 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200777
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100778 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500779 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200780}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200781EXPORT_SYMBOL(tcf_block_get);
782
Cong Wang7aa00452017-10-26 18:24:28 -0700783/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100784 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700785 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100786void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900787 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700788{
Cong Wangefbf7892017-12-04 10:48:18 -0800789 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700790
David S. Millerc30abd52017-12-16 22:11:55 -0500791 if (!block)
792 return;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200793 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100794 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100795
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200796 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100797 if (tcf_block_shared(block))
798 tcf_block_remove(block, block->net);
799
800 /* Hold a refcnt for all chains, so that they don't disappear
801 * while we are iterating.
802 */
803 list_for_each_entry(chain, &block->chain_list, list)
804 tcf_chain_hold(chain);
805
806 list_for_each_entry(chain, &block->chain_list, list)
807 tcf_chain_flush(chain);
808 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700809
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100810 tcf_block_offload_unbind(block, q, ei);
811
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200812 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100813 /* At this point, all the chains should have refcnt >= 1. */
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200814 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
815 tcf_chain_put_explicitly_created(chain);
Jiri Pirko48617382018-01-17 11:46:46 +0100816 tcf_chain_put(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200817 }
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100818
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200819 block->refcnt--;
820 if (list_empty(&block->chain_list))
821 kfree(block);
Jiri Pirko63cc5bc2018-08-08 14:04:13 +0200822 } else {
823 block->refcnt--;
Jiri Pirko48617382018-01-17 11:46:46 +0100824 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200825}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200826EXPORT_SYMBOL(tcf_block_put_ext);
827
828void tcf_block_put(struct tcf_block *block)
829{
830 struct tcf_block_ext_info ei = {0, };
831
Jiri Pirko4853f122017-12-21 13:13:59 +0100832 if (!block)
833 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100834 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200835}
David S. Millere1ea2f92017-10-30 14:10:01 +0900836
Jiri Pirko6529eab2017-05-17 11:07:55 +0200837EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100838
Jiri Pirkoacb67442017-10-19 15:50:31 +0200839struct tcf_block_cb {
840 struct list_head list;
841 tc_setup_cb_t *cb;
842 void *cb_ident;
843 void *cb_priv;
844 unsigned int refcnt;
845};
846
847void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
848{
849 return block_cb->cb_priv;
850}
851EXPORT_SYMBOL(tcf_block_cb_priv);
852
853struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
854 tc_setup_cb_t *cb, void *cb_ident)
855{ struct tcf_block_cb *block_cb;
856
857 list_for_each_entry(block_cb, &block->cb_list, list)
858 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
859 return block_cb;
860 return NULL;
861}
862EXPORT_SYMBOL(tcf_block_cb_lookup);
863
864void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
865{
866 block_cb->refcnt++;
867}
868EXPORT_SYMBOL(tcf_block_cb_incref);
869
870unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
871{
872 return --block_cb->refcnt;
873}
874EXPORT_SYMBOL(tcf_block_cb_decref);
875
John Hurley32636742018-06-25 14:30:10 -0700876static int
877tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
878 void *cb_priv, bool add, bool offload_in_use,
879 struct netlink_ext_ack *extack)
880{
881 struct tcf_chain *chain;
882 struct tcf_proto *tp;
883 int err;
884
885 list_for_each_entry(chain, &block->chain_list, list) {
886 for (tp = rtnl_dereference(chain->filter_chain); tp;
887 tp = rtnl_dereference(tp->next)) {
888 if (tp->ops->reoffload) {
889 err = tp->ops->reoffload(tp, add, cb, cb_priv,
890 extack);
891 if (err && add)
892 goto err_playback_remove;
893 } else if (add && offload_in_use) {
894 err = -EOPNOTSUPP;
895 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
896 goto err_playback_remove;
897 }
898 }
899 }
900
901 return 0;
902
903err_playback_remove:
904 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
905 extack);
906 return err;
907}
908
Jiri Pirkoacb67442017-10-19 15:50:31 +0200909struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
910 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700911 void *cb_priv,
912 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200913{
914 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700915 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200916
John Hurley32636742018-06-25 14:30:10 -0700917 /* Replay any already present rules */
918 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
919 tcf_block_offload_in_use(block),
920 extack);
921 if (err)
922 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100923
Jiri Pirkoacb67442017-10-19 15:50:31 +0200924 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
925 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100926 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200927 block_cb->cb = cb;
928 block_cb->cb_ident = cb_ident;
929 block_cb->cb_priv = cb_priv;
930 list_add(&block_cb->list, &block->cb_list);
931 return block_cb;
932}
933EXPORT_SYMBOL(__tcf_block_cb_register);
934
935int tcf_block_cb_register(struct tcf_block *block,
936 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700937 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200938{
939 struct tcf_block_cb *block_cb;
940
John Hurley60513bd2018-06-25 14:30:04 -0700941 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
942 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500943 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200944}
945EXPORT_SYMBOL(tcf_block_cb_register);
946
John Hurley32636742018-06-25 14:30:10 -0700947void __tcf_block_cb_unregister(struct tcf_block *block,
948 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200949{
John Hurley32636742018-06-25 14:30:10 -0700950 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
951 false, tcf_block_offload_in_use(block),
952 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200953 list_del(&block_cb->list);
954 kfree(block_cb);
955}
956EXPORT_SYMBOL(__tcf_block_cb_unregister);
957
958void tcf_block_cb_unregister(struct tcf_block *block,
959 tc_setup_cb_t *cb, void *cb_ident)
960{
961 struct tcf_block_cb *block_cb;
962
963 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
964 if (!block_cb)
965 return;
John Hurley32636742018-06-25 14:30:10 -0700966 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200967}
968EXPORT_SYMBOL(tcf_block_cb_unregister);
969
970static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
971 void *type_data, bool err_stop)
972{
973 struct tcf_block_cb *block_cb;
974 int ok_count = 0;
975 int err;
976
David S. Miller9a99dc12018-06-06 13:55:47 -0400977 /* Make sure all netdevs sharing this block are offload-capable. */
978 if (block->nooffloaddevcnt && err_stop)
979 return -EOPNOTSUPP;
980
Jiri Pirkoacb67442017-10-19 15:50:31 +0200981 list_for_each_entry(block_cb, &block->cb_list, list) {
982 err = block_cb->cb(type, type_data, block_cb->cb_priv);
983 if (err) {
984 if (err_stop)
985 return err;
986 } else {
987 ok_count++;
988 }
989 }
990 return ok_count;
991}
992
Jiri Pirko87d83092017-05-17 11:07:54 +0200993/* Main classifier routine: scans classifier chain attached
994 * to this qdisc, (optionally) tests for protocol and asks
995 * specific classifiers.
996 */
997int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
998 struct tcf_result *res, bool compat_mode)
999{
1000 __be16 protocol = tc_skb_protocol(skb);
1001#ifdef CONFIG_NET_CLS_ACT
1002 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001003 const struct tcf_proto *orig_tp = tp;
1004 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001005 int limit = 0;
1006
1007reclassify:
1008#endif
1009 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1010 int err;
1011
1012 if (tp->protocol != protocol &&
1013 tp->protocol != htons(ETH_P_ALL))
1014 continue;
1015
1016 err = tp->classify(skb, tp, res);
1017#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +02001018 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001019 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001020 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +02001021 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001022 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +02001023 goto reset;
1024 }
Jiri Pirko87d83092017-05-17 11:07:54 +02001025#endif
1026 if (err >= 0)
1027 return err;
1028 }
1029
1030 return TC_ACT_UNSPEC; /* signal: continue lookup */
1031#ifdef CONFIG_NET_CLS_ACT
1032reset:
1033 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +01001034 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1035 tp->chain->block->index,
1036 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +02001037 ntohs(tp->protocol));
1038 return TC_ACT_SHOT;
1039 }
1040
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001041 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001042 protocol = tc_skb_protocol(skb);
1043 goto reclassify;
1044#endif
1045}
1046EXPORT_SYMBOL(tcf_classify);
1047
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001048struct tcf_chain_info {
1049 struct tcf_proto __rcu **pprev;
1050 struct tcf_proto __rcu *next;
1051};
1052
1053static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1054{
1055 return rtnl_dereference(*chain_info->pprev);
1056}
1057
1058static void tcf_chain_tp_insert(struct tcf_chain *chain,
1059 struct tcf_chain_info *chain_info,
1060 struct tcf_proto *tp)
1061{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001062 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001063 tcf_chain0_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001064 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1065 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -07001066 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001067}
1068
1069static void tcf_chain_tp_remove(struct tcf_chain *chain,
1070 struct tcf_chain_info *chain_info,
1071 struct tcf_proto *tp)
1072{
1073 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1074
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001075 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001076 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001077 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -07001078 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001079}
1080
1081static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1082 struct tcf_chain_info *chain_info,
1083 u32 protocol, u32 prio,
1084 bool prio_allocate)
1085{
1086 struct tcf_proto **pprev;
1087 struct tcf_proto *tp;
1088
1089 /* Check the chain for existence of proto-tcf with this priority */
1090 for (pprev = &chain->filter_chain;
1091 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1092 if (tp->prio >= prio) {
1093 if (tp->prio == prio) {
1094 if (prio_allocate ||
1095 (tp->protocol != protocol && protocol))
1096 return ERR_PTR(-EINVAL);
1097 } else {
1098 tp = NULL;
1099 }
1100 break;
1101 }
1102 }
1103 chain_info->pprev = pprev;
1104 chain_info->next = tp ? tp->next : NULL;
1105 return tp;
1106}
1107
WANG Cong71203712017-08-07 15:26:50 -07001108static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001109 struct tcf_proto *tp, struct tcf_block *block,
1110 struct Qdisc *q, u32 parent, void *fh,
1111 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -07001112{
1113 struct tcmsg *tcm;
1114 struct nlmsghdr *nlh;
1115 unsigned char *b = skb_tail_pointer(skb);
1116
1117 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1118 if (!nlh)
1119 goto out_nlmsg_trim;
1120 tcm = nlmsg_data(nlh);
1121 tcm->tcm_family = AF_UNSPEC;
1122 tcm->tcm__pad1 = 0;
1123 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001124 if (q) {
1125 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1126 tcm->tcm_parent = parent;
1127 } else {
1128 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1129 tcm->tcm_block_index = block->index;
1130 }
WANG Cong71203712017-08-07 15:26:50 -07001131 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1132 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1133 goto nla_put_failure;
1134 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1135 goto nla_put_failure;
1136 if (!fh) {
1137 tcm->tcm_handle = 0;
1138 } else {
1139 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1140 goto nla_put_failure;
1141 }
1142 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1143 return skb->len;
1144
1145out_nlmsg_trim:
1146nla_put_failure:
1147 nlmsg_trim(skb, b);
1148 return -1;
1149}
1150
1151static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1152 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001153 struct tcf_block *block, struct Qdisc *q,
1154 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001155{
1156 struct sk_buff *skb;
1157 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1158
1159 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1160 if (!skb)
1161 return -ENOBUFS;
1162
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001163 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1164 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001165 kfree_skb(skb);
1166 return -EINVAL;
1167 }
1168
1169 if (unicast)
1170 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1171
1172 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1173 n->nlmsg_flags & NLM_F_ECHO);
1174}
1175
1176static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1177 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001178 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001179 u32 parent, void *fh, bool unicast, bool *last,
1180 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001181{
1182 struct sk_buff *skb;
1183 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1184 int err;
1185
1186 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1187 if (!skb)
1188 return -ENOBUFS;
1189
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001190 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1191 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001192 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001193 kfree_skb(skb);
1194 return -EINVAL;
1195 }
1196
Alexander Aring571acf22018-01-18 11:20:53 -05001197 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001198 if (err) {
1199 kfree_skb(skb);
1200 return err;
1201 }
1202
1203 if (unicast)
1204 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1205
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001206 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1207 n->nlmsg_flags & NLM_F_ECHO);
1208 if (err < 0)
1209 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1210 return err;
WANG Cong71203712017-08-07 15:26:50 -07001211}
1212
1213static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001214 struct tcf_block *block, struct Qdisc *q,
1215 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001216 struct tcf_chain *chain, int event)
1217{
1218 struct tcf_proto *tp;
1219
1220 for (tp = rtnl_dereference(chain->filter_chain);
1221 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001222 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001223 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001224}
1225
Vlad Buslovc431f892018-05-31 09:52:53 +03001226static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001227 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001229 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001230 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct tcmsg *t;
1232 u32 protocol;
1233 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001234 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001236 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001237 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001238 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001239 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001240 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001243 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001245 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Vlad Buslovc431f892018-05-31 09:52:53 +03001247 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001248 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001251 tp_created = 0;
1252
David Ahernc21ef3e2017-04-16 09:48:24 -07001253 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001254 if (err < 0)
1255 return err;
1256
David S. Miller942b8162012-06-26 21:48:50 -07001257 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 protocol = TC_H_MIN(t->tcm_info);
1259 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001260 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 parent = t->tcm_parent;
1262 cl = 0;
1263
1264 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001265 /* If no priority is provided by the user,
1266 * we allocate one.
1267 */
1268 if (n->nlmsg_flags & NLM_F_CREATE) {
1269 prio = TC_H_MAKE(0x80000000U, 0U);
1270 prio_allocate = true;
1271 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001272 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
1277 /* Find head of filter chain. */
1278
Vlad Buslovc431f892018-05-31 09:52:53 +03001279 block = tcf_block_find(net, &q, &parent, &cl,
1280 t->tcm_ifindex, t->tcm_block_index, extack);
1281 if (IS_ERR(block)) {
1282 err = PTR_ERR(block);
1283 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001284 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001285
1286 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1287 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001288 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001289 err = -EINVAL;
1290 goto errout;
1291 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001292 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001293 if (!chain) {
Jiri Pirkod5ed72a2018-08-27 20:58:43 +02001294 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001295 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001296 goto errout;
1297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001299 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1300 prio, prio_allocate);
1301 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001302 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001303 err = PTR_ERR(tp);
1304 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
1307 if (tp == NULL) {
1308 /* Proto-tcf does not exist, create new one */
1309
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001310 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001311 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001312 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Vlad Buslovc431f892018-05-31 09:52:53 +03001316 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001317 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 +01001318 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001322 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001323 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Jiri Pirko33a48922017-02-09 14:38:57 +01001325 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001326 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001327 if (IS_ERR(tp)) {
1328 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto errout;
1330 }
Minoru Usui12186be2009-06-02 02:17:34 -07001331 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001332 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001333 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001334 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 fh = tp->ops->get(tp, t->tcm_handle);
1339
WANG Cong8113c092017-08-04 21:31:43 -07001340 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001341 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001342 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 +01001343 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001345 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001346 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1347 NL_SET_ERR_MSG(extack, "Filter already exists");
1348 err = -EEXIST;
1349 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
Jiri Pirko9f407f12018-07-23 09:23:07 +02001352 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1353 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1354 err = -EINVAL;
1355 goto errout;
1356 }
1357
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001358 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001359 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1360 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001361 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001362 if (tp_created)
1363 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001364 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001365 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001366 } else {
1367 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001368 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001372 if (chain)
1373 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001374 tcf_block_release(q, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (err == -EAGAIN)
1376 /* Replay the request. */
1377 goto replay;
1378 return err;
1379}
1380
Vlad Buslovc431f892018-05-31 09:52:53 +03001381static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1382 struct netlink_ext_ack *extack)
1383{
1384 struct net *net = sock_net(skb->sk);
1385 struct nlattr *tca[TCA_MAX + 1];
1386 struct tcmsg *t;
1387 u32 protocol;
1388 u32 prio;
1389 u32 parent;
1390 u32 chain_index;
1391 struct Qdisc *q = NULL;
1392 struct tcf_chain_info chain_info;
1393 struct tcf_chain *chain = NULL;
1394 struct tcf_block *block;
1395 struct tcf_proto *tp = NULL;
1396 unsigned long cl = 0;
1397 void *fh = NULL;
1398 int err;
1399
1400 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1401 return -EPERM;
1402
1403 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1404 if (err < 0)
1405 return err;
1406
1407 t = nlmsg_data(n);
1408 protocol = TC_H_MIN(t->tcm_info);
1409 prio = TC_H_MAJ(t->tcm_info);
1410 parent = t->tcm_parent;
1411
1412 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1413 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1414 return -ENOENT;
1415 }
1416
1417 /* Find head of filter chain. */
1418
1419 block = tcf_block_find(net, &q, &parent, &cl,
1420 t->tcm_ifindex, t->tcm_block_index, extack);
1421 if (IS_ERR(block)) {
1422 err = PTR_ERR(block);
1423 goto errout;
1424 }
1425
1426 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1427 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1428 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1429 err = -EINVAL;
1430 goto errout;
1431 }
1432 chain = tcf_chain_get(block, chain_index, false);
1433 if (!chain) {
Jiri Pirko5ca8a252018-08-03 11:08:47 +02001434 /* User requested flush on non-existent chain. Nothing to do,
1435 * so just return success.
1436 */
1437 if (prio == 0) {
1438 err = 0;
1439 goto errout;
1440 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001441 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Jiri Pirkob7b42472018-08-27 20:58:44 +02001442 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001443 goto errout;
1444 }
1445
1446 if (prio == 0) {
1447 tfilter_notify_chain(net, skb, block, q, parent, n,
1448 chain, RTM_DELTFILTER);
1449 tcf_chain_flush(chain);
1450 err = 0;
1451 goto errout;
1452 }
1453
1454 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1455 prio, false);
1456 if (!tp || IS_ERR(tp)) {
1457 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001458 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001459 goto errout;
1460 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1461 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1462 err = -EINVAL;
1463 goto errout;
1464 }
1465
1466 fh = tp->ops->get(tp, t->tcm_handle);
1467
1468 if (!fh) {
1469 if (t->tcm_handle == 0) {
1470 tcf_chain_tp_remove(chain, &chain_info, tp);
1471 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1472 RTM_DELTFILTER, false);
1473 tcf_proto_destroy(tp, extack);
1474 err = 0;
1475 } else {
1476 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1477 err = -ENOENT;
1478 }
1479 } else {
1480 bool last;
1481
1482 err = tfilter_del_notify(net, skb, n, tp, block,
1483 q, parent, fh, false, &last,
1484 extack);
1485 if (err)
1486 goto errout;
1487 if (last) {
1488 tcf_chain_tp_remove(chain, &chain_info, tp);
1489 tcf_proto_destroy(tp, extack);
1490 }
1491 }
1492
1493errout:
1494 if (chain)
1495 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001496 tcf_block_release(q, block);
Vlad Buslovc431f892018-05-31 09:52:53 +03001497 return err;
1498}
1499
1500static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1501 struct netlink_ext_ack *extack)
1502{
1503 struct net *net = sock_net(skb->sk);
1504 struct nlattr *tca[TCA_MAX + 1];
1505 struct tcmsg *t;
1506 u32 protocol;
1507 u32 prio;
1508 u32 parent;
1509 u32 chain_index;
1510 struct Qdisc *q = NULL;
1511 struct tcf_chain_info chain_info;
1512 struct tcf_chain *chain = NULL;
1513 struct tcf_block *block;
1514 struct tcf_proto *tp = NULL;
1515 unsigned long cl = 0;
1516 void *fh = NULL;
1517 int err;
1518
1519 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1520 if (err < 0)
1521 return err;
1522
1523 t = nlmsg_data(n);
1524 protocol = TC_H_MIN(t->tcm_info);
1525 prio = TC_H_MAJ(t->tcm_info);
1526 parent = t->tcm_parent;
1527
1528 if (prio == 0) {
1529 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1530 return -ENOENT;
1531 }
1532
1533 /* Find head of filter chain. */
1534
1535 block = tcf_block_find(net, &q, &parent, &cl,
1536 t->tcm_ifindex, t->tcm_block_index, extack);
1537 if (IS_ERR(block)) {
1538 err = PTR_ERR(block);
1539 goto errout;
1540 }
1541
1542 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1543 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1544 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1545 err = -EINVAL;
1546 goto errout;
1547 }
1548 chain = tcf_chain_get(block, chain_index, false);
1549 if (!chain) {
1550 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1551 err = -EINVAL;
1552 goto errout;
1553 }
1554
1555 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1556 prio, false);
1557 if (!tp || IS_ERR(tp)) {
1558 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001559 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001560 goto errout;
1561 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1562 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1563 err = -EINVAL;
1564 goto errout;
1565 }
1566
1567 fh = tp->ops->get(tp, t->tcm_handle);
1568
1569 if (!fh) {
1570 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1571 err = -ENOENT;
1572 } else {
1573 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1574 fh, RTM_NEWTFILTER, true);
1575 if (err < 0)
1576 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1577 }
1578
1579errout:
1580 if (chain)
1581 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001582 tcf_block_release(q, block);
Vlad Buslovc431f892018-05-31 09:52:53 +03001583 return err;
1584}
1585
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001586struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct tcf_walker w;
1588 struct sk_buff *skb;
1589 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001590 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001591 struct Qdisc *q;
1592 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593};
1594
WANG Cong8113c092017-08-04 21:31:43 -07001595static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001597 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001598 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001600 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001601 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001602 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1603 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001606static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1607 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001608 long index_start, long *p_index)
1609{
1610 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001611 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001612 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1613 struct tcf_dump_args arg;
1614 struct tcf_proto *tp;
1615
1616 for (tp = rtnl_dereference(chain->filter_chain);
1617 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1618 if (*p_index < index_start)
1619 continue;
1620 if (TC_H_MAJ(tcm->tcm_info) &&
1621 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1622 continue;
1623 if (TC_H_MIN(tcm->tcm_info) &&
1624 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1625 continue;
1626 if (*p_index > index_start)
1627 memset(&cb->args[1], 0,
1628 sizeof(cb->args) - sizeof(cb->args[0]));
1629 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001630 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001631 NETLINK_CB(cb->skb).portid,
1632 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1633 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001634 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001635
1636 cb->args[1] = 1;
1637 }
1638 if (!tp->ops->walk)
1639 continue;
1640 arg.w.fn = tcf_node_dump;
1641 arg.skb = skb;
1642 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001643 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001644 arg.q = q;
1645 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001646 arg.w.stop = 0;
1647 arg.w.skip = cb->args[1] - 1;
1648 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001649 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001650 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001651 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001652 cb->args[1] = arg.w.count + 1;
1653 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001654 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001655 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001656 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001657}
1658
Eric Dumazetbd27a872009-11-05 20:57:26 -08001659/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1661{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001662 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001663 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001664 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001665 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001666 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001667 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001668 long index_start;
1669 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001670 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001671 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Hong zhi guo573ce262013-03-27 06:47:04 +00001673 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001675
1676 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1677 if (err)
1678 return err;
1679
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001680 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1681 block = tcf_block_lookup(net, tcm->tcm_block_index);
1682 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001683 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001684 /* If we work with block index, q is NULL and parent value
1685 * will never be used in the following code. The check
1686 * in tcf_fill_node prevents it. However, compiler does not
1687 * see that far, so set parent to zero to silence the warning
1688 * about parent being uninitialized.
1689 */
1690 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001691 } else {
1692 const struct Qdisc_class_ops *cops;
1693 struct net_device *dev;
1694 unsigned long cl = 0;
1695
1696 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1697 if (!dev)
1698 return skb->len;
1699
1700 parent = tcm->tcm_parent;
1701 if (!parent) {
1702 q = dev->qdisc;
1703 parent = q->handle;
1704 } else {
1705 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1706 }
1707 if (!q)
1708 goto out;
1709 cops = q->ops->cl_ops;
1710 if (!cops)
1711 goto out;
1712 if (!cops->tcf_block)
1713 goto out;
1714 if (TC_H_MIN(tcm->tcm_parent)) {
1715 cl = cops->find(q, tcm->tcm_parent);
1716 if (cl == 0)
1717 goto out;
1718 }
1719 block = cops->tcf_block(q, cl, NULL);
1720 if (!block)
1721 goto out;
1722 if (tcf_block_shared(block))
1723 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001726 index_start = cb->args[0];
1727 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001728
1729 list_for_each_entry(chain, &block->chain_list, list) {
1730 if (tca[TCA_CHAIN] &&
1731 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1732 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001733 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001734 index_start, &index)) {
1735 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001736 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001737 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001738 }
1739
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001740 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001743 /* If we did no progress, the error (EMSGSIZE) is real */
1744 if (skb->len == 0 && err)
1745 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return skb->len;
1747}
1748
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001749static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1750 struct sk_buff *skb, struct tcf_block *block,
1751 u32 portid, u32 seq, u16 flags, int event)
1752{
1753 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02001754 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001755 struct nlmsghdr *nlh;
1756 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02001757 void *priv;
1758
1759 ops = chain->tmplt_ops;
1760 priv = chain->tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001761
1762 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1763 if (!nlh)
1764 goto out_nlmsg_trim;
1765 tcm = nlmsg_data(nlh);
1766 tcm->tcm_family = AF_UNSPEC;
1767 tcm->tcm__pad1 = 0;
1768 tcm->tcm__pad2 = 0;
1769 tcm->tcm_handle = 0;
1770 if (block->q) {
1771 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1772 tcm->tcm_parent = block->q->handle;
1773 } else {
1774 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1775 tcm->tcm_block_index = block->index;
1776 }
1777
1778 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1779 goto nla_put_failure;
1780
Jiri Pirko9f407f12018-07-23 09:23:07 +02001781 if (ops) {
1782 if (nla_put_string(skb, TCA_KIND, ops->kind))
1783 goto nla_put_failure;
1784 if (ops->tmplt_dump(skb, net, priv) < 0)
1785 goto nla_put_failure;
1786 }
1787
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001788 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1789 return skb->len;
1790
1791out_nlmsg_trim:
1792nla_put_failure:
1793 nlmsg_trim(skb, b);
1794 return -EMSGSIZE;
1795}
1796
1797static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1798 u32 seq, u16 flags, int event, bool unicast)
1799{
1800 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1801 struct tcf_block *block = chain->block;
1802 struct net *net = block->net;
1803 struct sk_buff *skb;
1804
1805 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1806 if (!skb)
1807 return -ENOBUFS;
1808
1809 if (tc_chain_fill_node(chain, net, skb, block, portid,
1810 seq, flags, event) <= 0) {
1811 kfree_skb(skb);
1812 return -EINVAL;
1813 }
1814
1815 if (unicast)
1816 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1817
1818 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1819}
1820
Jiri Pirko9f407f12018-07-23 09:23:07 +02001821static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1822 struct nlattr **tca,
1823 struct netlink_ext_ack *extack)
1824{
1825 const struct tcf_proto_ops *ops;
1826 void *tmplt_priv;
1827
1828 /* If kind is not set, user did not specify template. */
1829 if (!tca[TCA_KIND])
1830 return 0;
1831
1832 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1833 if (IS_ERR(ops))
1834 return PTR_ERR(ops);
1835 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1836 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1837 return -EOPNOTSUPP;
1838 }
1839
1840 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1841 if (IS_ERR(tmplt_priv)) {
1842 module_put(ops->owner);
1843 return PTR_ERR(tmplt_priv);
1844 }
1845 chain->tmplt_ops = ops;
1846 chain->tmplt_priv = tmplt_priv;
1847 return 0;
1848}
1849
1850static void tc_chain_tmplt_del(struct tcf_chain *chain)
1851{
1852 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1853
1854 /* If template ops are set, no work to do for us. */
1855 if (!ops)
1856 return;
1857
1858 ops->tmplt_destroy(chain->tmplt_priv);
1859 module_put(ops->owner);
1860}
1861
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001862/* Add/delete/get a chain */
1863
1864static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1865 struct netlink_ext_ack *extack)
1866{
1867 struct net *net = sock_net(skb->sk);
1868 struct nlattr *tca[TCA_MAX + 1];
1869 struct tcmsg *t;
1870 u32 parent;
1871 u32 chain_index;
1872 struct Qdisc *q = NULL;
1873 struct tcf_chain *chain = NULL;
1874 struct tcf_block *block;
1875 unsigned long cl;
1876 int err;
1877
1878 if (n->nlmsg_type != RTM_GETCHAIN &&
1879 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1880 return -EPERM;
1881
1882replay:
1883 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1884 if (err < 0)
1885 return err;
1886
1887 t = nlmsg_data(n);
1888 parent = t->tcm_parent;
1889 cl = 0;
1890
1891 block = tcf_block_find(net, &q, &parent, &cl,
1892 t->tcm_ifindex, t->tcm_block_index, extack);
1893 if (IS_ERR(block))
1894 return PTR_ERR(block);
1895
1896 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1897 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1898 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001899 err = -EINVAL;
1900 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001901 }
1902 chain = tcf_chain_lookup(block, chain_index);
1903 if (n->nlmsg_type == RTM_NEWCHAIN) {
1904 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001905 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001906 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001907 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001908 */
1909 tcf_chain_hold(chain);
1910 } else {
1911 NL_SET_ERR_MSG(extack, "Filter chain already exists");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001912 err = -EEXIST;
1913 goto errout_block;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001914 }
1915 } else {
1916 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1917 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001918 err = -ENOENT;
1919 goto errout_block;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001920 }
1921 chain = tcf_chain_create(block, chain_index);
1922 if (!chain) {
1923 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001924 err = -ENOMEM;
1925 goto errout_block;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001926 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001927 }
1928 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001929 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001930 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001931 err = -EINVAL;
1932 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001933 }
1934 tcf_chain_hold(chain);
1935 }
1936
1937 switch (n->nlmsg_type) {
1938 case RTM_NEWCHAIN:
Jiri Pirko9f407f12018-07-23 09:23:07 +02001939 err = tc_chain_tmplt_add(chain, net, tca, extack);
1940 if (err)
1941 goto errout;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001942 /* In case the chain was successfully added, take a reference
1943 * to the chain. This ensures that an empty chain
1944 * does not disappear at the end of this function.
1945 */
1946 tcf_chain_hold(chain);
1947 chain->explicitly_created = true;
1948 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1949 RTM_NEWCHAIN, false);
1950 break;
1951 case RTM_DELCHAIN:
Cong Wangf5b9bac2018-09-11 14:22:23 -07001952 tfilter_notify_chain(net, skb, block, q, parent, n,
1953 chain, RTM_DELTFILTER);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001954 /* Flush the chain first as the user requested chain removal. */
1955 tcf_chain_flush(chain);
1956 /* In case the chain was successfully deleted, put a reference
1957 * to the chain previously taken during addition.
1958 */
1959 tcf_chain_put_explicitly_created(chain);
Jiri Pirkoc921d7d2018-07-26 18:27:58 +02001960 chain->explicitly_created = false;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001961 break;
1962 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001963 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1964 n->nlmsg_seq, n->nlmsg_type, true);
1965 if (err < 0)
1966 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1967 break;
1968 default:
1969 err = -EOPNOTSUPP;
1970 NL_SET_ERR_MSG(extack, "Unsupported message type");
1971 goto errout;
1972 }
1973
1974errout:
1975 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001976errout_block:
1977 tcf_block_release(q, block);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001978 if (err == -EAGAIN)
1979 /* Replay the request. */
1980 goto replay;
1981 return err;
1982}
1983
1984/* called with RTNL */
1985static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
1986{
1987 struct net *net = sock_net(skb->sk);
1988 struct nlattr *tca[TCA_MAX + 1];
1989 struct Qdisc *q = NULL;
1990 struct tcf_block *block;
1991 struct tcf_chain *chain;
1992 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1993 long index_start;
1994 long index;
1995 u32 parent;
1996 int err;
1997
1998 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1999 return skb->len;
2000
2001 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
2002 if (err)
2003 return err;
2004
2005 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2006 block = tcf_block_lookup(net, tcm->tcm_block_index);
2007 if (!block)
2008 goto out;
2009 /* If we work with block index, q is NULL and parent value
2010 * will never be used in the following code. The check
2011 * in tcf_fill_node prevents it. However, compiler does not
2012 * see that far, so set parent to zero to silence the warning
2013 * about parent being uninitialized.
2014 */
2015 parent = 0;
2016 } else {
2017 const struct Qdisc_class_ops *cops;
2018 struct net_device *dev;
2019 unsigned long cl = 0;
2020
2021 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2022 if (!dev)
2023 return skb->len;
2024
2025 parent = tcm->tcm_parent;
2026 if (!parent) {
2027 q = dev->qdisc;
2028 parent = q->handle;
2029 } else {
2030 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2031 }
2032 if (!q)
2033 goto out;
2034 cops = q->ops->cl_ops;
2035 if (!cops)
2036 goto out;
2037 if (!cops->tcf_block)
2038 goto out;
2039 if (TC_H_MIN(tcm->tcm_parent)) {
2040 cl = cops->find(q, tcm->tcm_parent);
2041 if (cl == 0)
2042 goto out;
2043 }
2044 block = cops->tcf_block(q, cl, NULL);
2045 if (!block)
2046 goto out;
2047 if (tcf_block_shared(block))
2048 q = NULL;
2049 }
2050
2051 index_start = cb->args[0];
2052 index = 0;
2053
2054 list_for_each_entry(chain, &block->chain_list, list) {
2055 if ((tca[TCA_CHAIN] &&
2056 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2057 continue;
2058 if (index < index_start) {
2059 index++;
2060 continue;
2061 }
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002062 if (tcf_chain_held_by_acts_only(chain))
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002063 continue;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002064 err = tc_chain_fill_node(chain, net, skb, block,
2065 NETLINK_CB(cb->skb).portid,
2066 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2067 RTM_NEWCHAIN);
2068 if (err <= 0)
2069 break;
2070 index++;
2071 }
2072
2073 cb->args[0] = index;
2074
2075out:
2076 /* If we did no progress, the error (EMSGSIZE) is real */
2077 if (skb->len == 0 && err)
2078 return err;
2079 return skb->len;
2080}
2081
WANG Cong18d02642014-09-25 10:26:37 -07002082void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084#ifdef CONFIG_NET_CLS_ACT
Vlad Buslov90b73b72018-07-05 17:24:33 +03002085 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
WANG Cong22dc13c2016-08-13 22:35:00 -07002086 kfree(exts->actions);
2087 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088#endif
2089}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002090EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00002092int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05002093 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2094 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096#ifdef CONFIG_NET_CLS_ACT
2097 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05002099 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
WANG Cong5da57f42013-12-15 20:15:07 -08002101 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002102 act = tcf_action_init_1(net, tp, tb[exts->police],
2103 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03002104 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08002105 if (IS_ERR(act))
2106 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
WANG Cong33be6272013-12-15 20:15:05 -08002108 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07002109 exts->actions[0] = act;
2110 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08002111 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03002112 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07002113
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002114 err = tcf_action_init(net, tp, tb[exts->action],
2115 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03002116 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03002117 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03002118 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002119 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03002120 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Cong Wange4b95c42017-11-06 13:47:19 -08002122 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124#else
WANG Cong5da57f42013-12-15 20:15:07 -08002125 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05002126 (exts->police && tb[exts->police])) {
2127 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05002129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130#endif
2131
2132 return 0;
2133}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002134EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002136void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07002139 struct tcf_exts old = *dst;
2140
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002141 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07002142 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143#endif
2144}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002145EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
WANG Cong22dc13c2016-08-13 22:35:00 -07002147#ifdef CONFIG_NET_CLS_ACT
2148static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2149{
2150 if (exts->nr_actions == 0)
2151 return NULL;
2152 else
2153 return exts->actions[0];
2154}
2155#endif
WANG Cong33be6272013-12-15 20:15:05 -08002156
WANG Cong5da57f42013-12-15 20:15:07 -08002157int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
2159#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07002160 struct nlattr *nest;
2161
Jiri Pirko978dfd82017-08-04 14:29:03 +02002162 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /*
2164 * again for backward compatible mode - we want
2165 * to work with both old and new modes of entering
2166 * tc data even if iproute2 was newer - jhs
2167 */
WANG Cong33be6272013-12-15 20:15:05 -08002168 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08002169 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002170 if (nest == NULL)
2171 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07002172
Vlad Buslov90b73b72018-07-05 17:24:33 +03002173 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002174 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002175 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08002176 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08002177 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08002178 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05002179 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002180 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08002181 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002182 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002183 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
2185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07002187
2188nla_put_failure:
2189 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07002191#else
2192 return 0;
2193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002195EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002197
WANG Cong5da57f42013-12-15 20:15:07 -08002198int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08002201 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01002202 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002203 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204#endif
2205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002207EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Jiri Pirko717503b2017-10-11 09:41:09 +02002209static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2210 enum tc_setup_type type,
2211 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002212{
2213 int ok_count = 0;
2214#ifdef CONFIG_NET_CLS_ACT
2215 const struct tc_action *a;
2216 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002217 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002218
2219 if (!tcf_exts_has_actions(exts))
2220 return 0;
2221
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002222 for (i = 0; i < exts->nr_actions; i++) {
2223 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002224 if (!a->ops->get_dev)
2225 continue;
2226 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01002227 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002228 continue;
2229 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
Vlad Buslov84a75b32018-08-10 20:51:52 +03002230 a->ops->put_dev(dev);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002231 if (ret < 0)
2232 return ret;
2233 ok_count += ret;
2234 }
2235#endif
2236 return ok_count;
2237}
Jiri Pirko717503b2017-10-11 09:41:09 +02002238
Jiri Pirko208c0f42017-10-19 15:50:32 +02002239int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2240 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02002241{
David S. Miller9a99dc12018-06-06 13:55:47 -04002242 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002243 int ret;
2244
David S. Miller9a99dc12018-06-06 13:55:47 -04002245 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2246 if (ret < 0)
2247 return ret;
2248 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002249
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03002250 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04002251 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002252 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2253 if (ret < 0)
2254 return ret;
2255 ok_count += ret;
2256
2257 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02002258}
2259EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002260
Jiri Pirko48617382018-01-17 11:46:46 +01002261static __net_init int tcf_net_init(struct net *net)
2262{
2263 struct tcf_net *tn = net_generic(net, tcf_net_id);
2264
2265 idr_init(&tn->idr);
2266 return 0;
2267}
2268
2269static void __net_exit tcf_net_exit(struct net *net)
2270{
2271 struct tcf_net *tn = net_generic(net, tcf_net_id);
2272
2273 idr_destroy(&tn->idr);
2274}
2275
2276static struct pernet_operations tcf_net_ops = {
2277 .init = tcf_net_init,
2278 .exit = tcf_net_exit,
2279 .id = &tcf_net_id,
2280 .size = sizeof(struct tcf_net),
2281};
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283static int __init tc_filter_init(void)
2284{
Jiri Pirko48617382018-01-17 11:46:46 +01002285 int err;
2286
Cong Wang7aa00452017-10-26 18:24:28 -07002287 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2288 if (!tc_filter_wq)
2289 return -ENOMEM;
2290
Jiri Pirko48617382018-01-17 11:46:46 +01002291 err = register_pernet_subsys(&tcf_net_ops);
2292 if (err)
2293 goto err_register_pernet_subsys;
2294
Vlad Buslovc431f892018-05-31 09:52:53 +03002295 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2296 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2297 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02002298 tc_dump_tfilter, 0);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002299 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2300 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2301 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2302 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01002305
2306err_register_pernet_subsys:
2307 destroy_workqueue(tc_filter_wq);
2308 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
2311subsys_initcall(tc_filter_init);