blob: 3de47e99b788f81e914efebe9ef89e7f32c1f4bc [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);
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300243 if (list_empty(&block->chain_list) && !refcount_read(&block->refcnt))
Vlad Buslov0607e432018-09-24 19:22:57 +0300244 kfree_rcu(block, rcu);
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 {
Vlad Buslovab281622018-09-24 19:22:56 +0300476 spinlock_t idr_lock; /* Protects idr */
Jiri Pirko48617382018-01-17 11:46:46 +0100477 struct idr idr;
478};
479
480static unsigned int tcf_net_id;
481
482static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100483 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100484{
Jiri Pirko48617382018-01-17 11:46:46 +0100485 struct tcf_net *tn = net_generic(net, tcf_net_id);
Vlad Buslovab281622018-09-24 19:22:56 +0300486 int err;
Jiri Pirko48617382018-01-17 11:46:46 +0100487
Vlad Buslovab281622018-09-24 19:22:56 +0300488 idr_preload(GFP_KERNEL);
489 spin_lock(&tn->idr_lock);
490 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
491 GFP_NOWAIT);
492 spin_unlock(&tn->idr_lock);
493 idr_preload_end();
494
495 return err;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100496}
497
Jiri Pirko48617382018-01-17 11:46:46 +0100498static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200499{
Jiri Pirko48617382018-01-17 11:46:46 +0100500 struct tcf_net *tn = net_generic(net, tcf_net_id);
501
Vlad Buslovab281622018-09-24 19:22:56 +0300502 spin_lock(&tn->idr_lock);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500503 idr_remove(&tn->idr, block->index);
Vlad Buslovab281622018-09-24 19:22:56 +0300504 spin_unlock(&tn->idr_lock);
Jiri Pirko48617382018-01-17 11:46:46 +0100505}
506
507static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100508 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100509 struct netlink_ext_ack *extack)
510{
511 struct tcf_block *block;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200512
Jiri Pirko48617382018-01-17 11:46:46 +0100513 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500514 if (!block) {
515 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100516 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500517 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200518 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200519 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100520 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200521 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200522
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300523 refcount_set(&block->refcnt, 1);
Jiri Pirko48617382018-01-17 11:46:46 +0100524 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100525 block->index = block_index;
526
527 /* Don't store q pointer for blocks which are shared */
528 if (!tcf_block_shared(block))
529 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100530 return block;
Jiri Pirko48617382018-01-17 11:46:46 +0100531}
532
533static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
534{
535 struct tcf_net *tn = net_generic(net, tcf_net_id);
536
Matthew Wilcox322d8842017-11-28 10:01:24 -0500537 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100538}
539
Vlad Buslov0607e432018-09-24 19:22:57 +0300540static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
541{
542 struct tcf_block *block;
543
544 rcu_read_lock();
545 block = tcf_block_lookup(net, block_index);
546 if (block && !refcount_inc_not_zero(&block->refcnt))
547 block = NULL;
548 rcu_read_unlock();
549
550 return block;
551}
552
Vlad Buslovf0023432018-09-24 19:22:55 +0300553static void tcf_block_flush_all_chains(struct tcf_block *block)
554{
555 struct tcf_chain *chain;
556
557 /* Hold a refcnt for all chains, so that they don't disappear
558 * while we are iterating.
559 */
560 list_for_each_entry(chain, &block->chain_list, list)
561 tcf_chain_hold(chain);
562
563 list_for_each_entry(chain, &block->chain_list, list)
564 tcf_chain_flush(chain);
565}
566
567static void tcf_block_put_all_chains(struct tcf_block *block)
568{
569 struct tcf_chain *chain, *tmp;
570
571 /* At this point, all the chains should have refcnt >= 1. */
572 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
573 tcf_chain_put_explicitly_created(chain);
574 tcf_chain_put(chain);
575 }
576}
577
Vlad Buslov0607e432018-09-24 19:22:57 +0300578static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
579 struct tcf_block_ext_info *ei)
580{
581 if (refcount_dec_and_test(&block->refcnt)) {
582 /* Flushing/putting all chains will cause the block to be
583 * deallocated when last chain is freed. However, if chain_list
584 * is empty, block has to be manually deallocated. After block
585 * reference counter reached 0, it is no longer possible to
586 * increment it or add new chains to block.
587 */
588 bool free_block = list_empty(&block->chain_list);
589
590 if (tcf_block_shared(block))
591 tcf_block_remove(block, block->net);
592 if (!free_block)
593 tcf_block_flush_all_chains(block);
594
595 if (q)
596 tcf_block_offload_unbind(block, q, ei);
597
598 if (free_block)
599 kfree_rcu(block, rcu);
600 else
601 tcf_block_put_all_chains(block);
602 } else if (q) {
603 tcf_block_offload_unbind(block, q, ei);
604 }
605}
606
607static void tcf_block_refcnt_put(struct tcf_block *block)
608{
609 __tcf_block_put(block, NULL, NULL);
610}
611
Vlad Buslovc431f892018-05-31 09:52:53 +0300612/* Find tcf block.
613 * Set q, parent, cl when appropriate.
614 */
615
616static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
617 u32 *parent, unsigned long *cl,
618 int ifindex, u32 block_index,
619 struct netlink_ext_ack *extack)
620{
621 struct tcf_block *block;
Vlad Buslove368fdb2018-09-24 19:22:53 +0300622 int err = 0;
Vlad Buslovc431f892018-05-31 09:52:53 +0300623
624 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
Vlad Buslov787ce6d2018-09-24 19:22:58 +0300625 block = tcf_block_refcnt_get(net, block_index);
Vlad Buslovc431f892018-05-31 09:52:53 +0300626 if (!block) {
627 NL_SET_ERR_MSG(extack, "Block of given index was not found");
628 return ERR_PTR(-EINVAL);
629 }
630 } else {
631 const struct Qdisc_class_ops *cops;
632 struct net_device *dev;
633
Vlad Buslove368fdb2018-09-24 19:22:53 +0300634 rcu_read_lock();
635
Vlad Buslovc431f892018-05-31 09:52:53 +0300636 /* Find link */
Vlad Buslove368fdb2018-09-24 19:22:53 +0300637 dev = dev_get_by_index_rcu(net, ifindex);
638 if (!dev) {
639 rcu_read_unlock();
Vlad Buslovc431f892018-05-31 09:52:53 +0300640 return ERR_PTR(-ENODEV);
Vlad Buslove368fdb2018-09-24 19:22:53 +0300641 }
Vlad Buslovc431f892018-05-31 09:52:53 +0300642
643 /* Find qdisc */
644 if (!*parent) {
645 *q = dev->qdisc;
646 *parent = (*q)->handle;
647 } else {
Vlad Buslove368fdb2018-09-24 19:22:53 +0300648 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
Vlad Buslovc431f892018-05-31 09:52:53 +0300649 if (!*q) {
650 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300651 err = -EINVAL;
652 goto errout_rcu;
Vlad Buslovc431f892018-05-31 09:52:53 +0300653 }
654 }
655
Vlad Buslove368fdb2018-09-24 19:22:53 +0300656 *q = qdisc_refcount_inc_nz(*q);
657 if (!*q) {
658 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
659 err = -EINVAL;
660 goto errout_rcu;
661 }
662
Vlad Buslovc431f892018-05-31 09:52:53 +0300663 /* Is it classful? */
664 cops = (*q)->ops->cl_ops;
665 if (!cops) {
666 NL_SET_ERR_MSG(extack, "Qdisc not classful");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300667 err = -EINVAL;
668 goto errout_rcu;
Vlad Buslovc431f892018-05-31 09:52:53 +0300669 }
670
671 if (!cops->tcf_block) {
672 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300673 err = -EOPNOTSUPP;
674 goto errout_rcu;
Vlad Buslovc431f892018-05-31 09:52:53 +0300675 }
676
Vlad Buslove368fdb2018-09-24 19:22:53 +0300677 /* At this point we know that qdisc is not noop_qdisc,
678 * which means that qdisc holds a reference to net_device
679 * and we hold a reference to qdisc, so it is safe to release
680 * rcu read lock.
681 */
682 rcu_read_unlock();
683
Vlad Buslovc431f892018-05-31 09:52:53 +0300684 /* Do we search for filter, attached to class? */
685 if (TC_H_MIN(*parent)) {
686 *cl = cops->find(*q, *parent);
687 if (*cl == 0) {
688 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300689 err = -ENOENT;
690 goto errout_qdisc;
Vlad Buslovc431f892018-05-31 09:52:53 +0300691 }
692 }
693
694 /* And the last stroke */
695 block = cops->tcf_block(*q, *cl, extack);
Vlad Buslove368fdb2018-09-24 19:22:53 +0300696 if (!block) {
697 err = -EINVAL;
698 goto errout_qdisc;
699 }
Vlad Buslovc431f892018-05-31 09:52:53 +0300700 if (tcf_block_shared(block)) {
701 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 +0300702 err = -EOPNOTSUPP;
703 goto errout_qdisc;
Vlad Buslovc431f892018-05-31 09:52:53 +0300704 }
Vlad Buslov787ce6d2018-09-24 19:22:58 +0300705
706 /* Always take reference to block in order to support execution
707 * of rules update path of cls API without rtnl lock. Caller
708 * must release block when it is finished using it. 'if' block
709 * of this conditional obtain reference to block by calling
710 * tcf_block_refcnt_get().
711 */
712 refcount_inc(&block->refcnt);
Vlad Buslovc431f892018-05-31 09:52:53 +0300713 }
714
715 return block;
Vlad Buslove368fdb2018-09-24 19:22:53 +0300716
717errout_rcu:
718 rcu_read_unlock();
719errout_qdisc:
720 if (*q)
721 qdisc_put(*q);
722 return ERR_PTR(err);
723}
724
725static void tcf_block_release(struct Qdisc *q, struct tcf_block *block)
726{
Vlad Buslov787ce6d2018-09-24 19:22:58 +0300727 if (!IS_ERR_OR_NULL(block))
728 tcf_block_refcnt_put(block);
729
Vlad Buslove368fdb2018-09-24 19:22:53 +0300730 if (q)
731 qdisc_put(q);
Vlad Buslovc431f892018-05-31 09:52:53 +0300732}
733
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100734struct tcf_block_owner_item {
735 struct list_head list;
736 struct Qdisc *q;
737 enum tcf_block_binder_type binder_type;
738};
739
740static void
741tcf_block_owner_netif_keep_dst(struct tcf_block *block,
742 struct Qdisc *q,
743 enum tcf_block_binder_type binder_type)
744{
745 if (block->keep_dst &&
746 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
747 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
748 netif_keep_dst(qdisc_dev(q));
749}
750
751void tcf_block_netif_keep_dst(struct tcf_block *block)
752{
753 struct tcf_block_owner_item *item;
754
755 block->keep_dst = true;
756 list_for_each_entry(item, &block->owner_list, list)
757 tcf_block_owner_netif_keep_dst(block, item->q,
758 item->binder_type);
759}
760EXPORT_SYMBOL(tcf_block_netif_keep_dst);
761
762static int tcf_block_owner_add(struct tcf_block *block,
763 struct Qdisc *q,
764 enum tcf_block_binder_type binder_type)
765{
766 struct tcf_block_owner_item *item;
767
768 item = kmalloc(sizeof(*item), GFP_KERNEL);
769 if (!item)
770 return -ENOMEM;
771 item->q = q;
772 item->binder_type = binder_type;
773 list_add(&item->list, &block->owner_list);
774 return 0;
775}
776
777static void tcf_block_owner_del(struct tcf_block *block,
778 struct Qdisc *q,
779 enum tcf_block_binder_type binder_type)
780{
781 struct tcf_block_owner_item *item;
782
783 list_for_each_entry(item, &block->owner_list, list) {
784 if (item->q == q && item->binder_type == binder_type) {
785 list_del(&item->list);
786 kfree(item);
787 return;
788 }
789 }
790 WARN_ON(1);
791}
792
Jiri Pirko48617382018-01-17 11:46:46 +0100793int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
794 struct tcf_block_ext_info *ei,
795 struct netlink_ext_ack *extack)
796{
797 struct net *net = qdisc_net(q);
798 struct tcf_block *block = NULL;
Jiri Pirko48617382018-01-17 11:46:46 +0100799 int err;
800
Vlad Buslov787ce6d2018-09-24 19:22:58 +0300801 if (ei->block_index)
Jiri Pirko48617382018-01-17 11:46:46 +0100802 /* block_index not 0 means the shared block is requested */
Vlad Buslov787ce6d2018-09-24 19:22:58 +0300803 block = tcf_block_refcnt_get(net, ei->block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100804
805 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100806 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100807 if (IS_ERR(block))
808 return PTR_ERR(block);
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100809 if (tcf_block_shared(block)) {
810 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100811 if (err)
812 goto err_block_insert;
813 }
814 }
815
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100816 err = tcf_block_owner_add(block, q, ei->binder_type);
817 if (err)
818 goto err_block_owner_add;
819
820 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
821
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200822 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100823 if (err)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200824 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100825
John Hurley60513bd2018-06-25 14:30:04 -0700826 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100827 if (err)
828 goto err_block_offload_bind;
829
Jiri Pirko6529eab2017-05-17 11:07:55 +0200830 *p_block = block;
831 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200832
Jiri Pirkocaa72602018-01-17 11:46:50 +0100833err_block_offload_bind:
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200834 tcf_chain0_head_change_cb_del(block, ei);
835err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100836 tcf_block_owner_del(block, q, ei->binder_type);
837err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100838err_block_insert:
Vlad Buslov787ce6d2018-09-24 19:22:58 +0300839 tcf_block_refcnt_put(block);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200840 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200841}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200842EXPORT_SYMBOL(tcf_block_get_ext);
843
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100844static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
845{
846 struct tcf_proto __rcu **p_filter_chain = priv;
847
848 rcu_assign_pointer(*p_filter_chain, tp_head);
849}
850
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200851int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500852 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
853 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200854{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100855 struct tcf_block_ext_info ei = {
856 .chain_head_change = tcf_chain_head_change_dflt,
857 .chain_head_change_priv = p_filter_chain,
858 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200859
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100860 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500861 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200862}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200863EXPORT_SYMBOL(tcf_block_get);
864
Cong Wang7aa00452017-10-26 18:24:28 -0700865/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100866 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700867 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100868void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900869 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700870{
David S. Millerc30abd52017-12-16 22:11:55 -0500871 if (!block)
872 return;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200873 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100874 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100875
Vlad Buslov0607e432018-09-24 19:22:57 +0300876 __tcf_block_put(block, q, ei);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200877}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200878EXPORT_SYMBOL(tcf_block_put_ext);
879
880void tcf_block_put(struct tcf_block *block)
881{
882 struct tcf_block_ext_info ei = {0, };
883
Jiri Pirko4853f122017-12-21 13:13:59 +0100884 if (!block)
885 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100886 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200887}
David S. Millere1ea2f92017-10-30 14:10:01 +0900888
Jiri Pirko6529eab2017-05-17 11:07:55 +0200889EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100890
Jiri Pirkoacb67442017-10-19 15:50:31 +0200891struct tcf_block_cb {
892 struct list_head list;
893 tc_setup_cb_t *cb;
894 void *cb_ident;
895 void *cb_priv;
896 unsigned int refcnt;
897};
898
899void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
900{
901 return block_cb->cb_priv;
902}
903EXPORT_SYMBOL(tcf_block_cb_priv);
904
905struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
906 tc_setup_cb_t *cb, void *cb_ident)
907{ struct tcf_block_cb *block_cb;
908
909 list_for_each_entry(block_cb, &block->cb_list, list)
910 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
911 return block_cb;
912 return NULL;
913}
914EXPORT_SYMBOL(tcf_block_cb_lookup);
915
916void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
917{
918 block_cb->refcnt++;
919}
920EXPORT_SYMBOL(tcf_block_cb_incref);
921
922unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
923{
924 return --block_cb->refcnt;
925}
926EXPORT_SYMBOL(tcf_block_cb_decref);
927
John Hurley32636742018-06-25 14:30:10 -0700928static int
929tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
930 void *cb_priv, bool add, bool offload_in_use,
931 struct netlink_ext_ack *extack)
932{
933 struct tcf_chain *chain;
934 struct tcf_proto *tp;
935 int err;
936
937 list_for_each_entry(chain, &block->chain_list, list) {
938 for (tp = rtnl_dereference(chain->filter_chain); tp;
939 tp = rtnl_dereference(tp->next)) {
940 if (tp->ops->reoffload) {
941 err = tp->ops->reoffload(tp, add, cb, cb_priv,
942 extack);
943 if (err && add)
944 goto err_playback_remove;
945 } else if (add && offload_in_use) {
946 err = -EOPNOTSUPP;
947 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
948 goto err_playback_remove;
949 }
950 }
951 }
952
953 return 0;
954
955err_playback_remove:
956 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
957 extack);
958 return err;
959}
960
Jiri Pirkoacb67442017-10-19 15:50:31 +0200961struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
962 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700963 void *cb_priv,
964 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200965{
966 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700967 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200968
John Hurley32636742018-06-25 14:30:10 -0700969 /* Replay any already present rules */
970 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
971 tcf_block_offload_in_use(block),
972 extack);
973 if (err)
974 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100975
Jiri Pirkoacb67442017-10-19 15:50:31 +0200976 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
977 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100978 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200979 block_cb->cb = cb;
980 block_cb->cb_ident = cb_ident;
981 block_cb->cb_priv = cb_priv;
982 list_add(&block_cb->list, &block->cb_list);
983 return block_cb;
984}
985EXPORT_SYMBOL(__tcf_block_cb_register);
986
987int tcf_block_cb_register(struct tcf_block *block,
988 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700989 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200990{
991 struct tcf_block_cb *block_cb;
992
John Hurley60513bd2018-06-25 14:30:04 -0700993 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
994 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500995 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200996}
997EXPORT_SYMBOL(tcf_block_cb_register);
998
John Hurley32636742018-06-25 14:30:10 -0700999void __tcf_block_cb_unregister(struct tcf_block *block,
1000 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +02001001{
John Hurley32636742018-06-25 14:30:10 -07001002 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
1003 false, tcf_block_offload_in_use(block),
1004 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +02001005 list_del(&block_cb->list);
1006 kfree(block_cb);
1007}
1008EXPORT_SYMBOL(__tcf_block_cb_unregister);
1009
1010void tcf_block_cb_unregister(struct tcf_block *block,
1011 tc_setup_cb_t *cb, void *cb_ident)
1012{
1013 struct tcf_block_cb *block_cb;
1014
1015 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
1016 if (!block_cb)
1017 return;
John Hurley32636742018-06-25 14:30:10 -07001018 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +02001019}
1020EXPORT_SYMBOL(tcf_block_cb_unregister);
1021
1022static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
1023 void *type_data, bool err_stop)
1024{
1025 struct tcf_block_cb *block_cb;
1026 int ok_count = 0;
1027 int err;
1028
David S. Miller9a99dc12018-06-06 13:55:47 -04001029 /* Make sure all netdevs sharing this block are offload-capable. */
1030 if (block->nooffloaddevcnt && err_stop)
1031 return -EOPNOTSUPP;
1032
Jiri Pirkoacb67442017-10-19 15:50:31 +02001033 list_for_each_entry(block_cb, &block->cb_list, list) {
1034 err = block_cb->cb(type, type_data, block_cb->cb_priv);
1035 if (err) {
1036 if (err_stop)
1037 return err;
1038 } else {
1039 ok_count++;
1040 }
1041 }
1042 return ok_count;
1043}
1044
Jiri Pirko87d83092017-05-17 11:07:54 +02001045/* Main classifier routine: scans classifier chain attached
1046 * to this qdisc, (optionally) tests for protocol and asks
1047 * specific classifiers.
1048 */
1049int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1050 struct tcf_result *res, bool compat_mode)
1051{
1052 __be16 protocol = tc_skb_protocol(skb);
1053#ifdef CONFIG_NET_CLS_ACT
1054 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001055 const struct tcf_proto *orig_tp = tp;
1056 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001057 int limit = 0;
1058
1059reclassify:
1060#endif
1061 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1062 int err;
1063
1064 if (tp->protocol != protocol &&
1065 tp->protocol != htons(ETH_P_ALL))
1066 continue;
1067
1068 err = tp->classify(skb, tp, res);
1069#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +02001070 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001071 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001072 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +02001073 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001074 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +02001075 goto reset;
1076 }
Jiri Pirko87d83092017-05-17 11:07:54 +02001077#endif
1078 if (err >= 0)
1079 return err;
1080 }
1081
1082 return TC_ACT_UNSPEC; /* signal: continue lookup */
1083#ifdef CONFIG_NET_CLS_ACT
1084reset:
1085 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +01001086 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1087 tp->chain->block->index,
1088 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +02001089 ntohs(tp->protocol));
1090 return TC_ACT_SHOT;
1091 }
1092
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001093 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001094 protocol = tc_skb_protocol(skb);
1095 goto reclassify;
1096#endif
1097}
1098EXPORT_SYMBOL(tcf_classify);
1099
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001100struct tcf_chain_info {
1101 struct tcf_proto __rcu **pprev;
1102 struct tcf_proto __rcu *next;
1103};
1104
1105static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1106{
1107 return rtnl_dereference(*chain_info->pprev);
1108}
1109
1110static void tcf_chain_tp_insert(struct tcf_chain *chain,
1111 struct tcf_chain_info *chain_info,
1112 struct tcf_proto *tp)
1113{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001114 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001115 tcf_chain0_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001116 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1117 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -07001118 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001119}
1120
1121static void tcf_chain_tp_remove(struct tcf_chain *chain,
1122 struct tcf_chain_info *chain_info,
1123 struct tcf_proto *tp)
1124{
1125 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1126
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001127 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001128 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001129 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -07001130 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001131}
1132
1133static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1134 struct tcf_chain_info *chain_info,
1135 u32 protocol, u32 prio,
1136 bool prio_allocate)
1137{
1138 struct tcf_proto **pprev;
1139 struct tcf_proto *tp;
1140
1141 /* Check the chain for existence of proto-tcf with this priority */
1142 for (pprev = &chain->filter_chain;
1143 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1144 if (tp->prio >= prio) {
1145 if (tp->prio == prio) {
1146 if (prio_allocate ||
1147 (tp->protocol != protocol && protocol))
1148 return ERR_PTR(-EINVAL);
1149 } else {
1150 tp = NULL;
1151 }
1152 break;
1153 }
1154 }
1155 chain_info->pprev = pprev;
1156 chain_info->next = tp ? tp->next : NULL;
1157 return tp;
1158}
1159
WANG Cong71203712017-08-07 15:26:50 -07001160static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001161 struct tcf_proto *tp, struct tcf_block *block,
1162 struct Qdisc *q, u32 parent, void *fh,
1163 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -07001164{
1165 struct tcmsg *tcm;
1166 struct nlmsghdr *nlh;
1167 unsigned char *b = skb_tail_pointer(skb);
1168
1169 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1170 if (!nlh)
1171 goto out_nlmsg_trim;
1172 tcm = nlmsg_data(nlh);
1173 tcm->tcm_family = AF_UNSPEC;
1174 tcm->tcm__pad1 = 0;
1175 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001176 if (q) {
1177 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1178 tcm->tcm_parent = parent;
1179 } else {
1180 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1181 tcm->tcm_block_index = block->index;
1182 }
WANG Cong71203712017-08-07 15:26:50 -07001183 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1184 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1185 goto nla_put_failure;
1186 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1187 goto nla_put_failure;
1188 if (!fh) {
1189 tcm->tcm_handle = 0;
1190 } else {
1191 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1192 goto nla_put_failure;
1193 }
1194 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1195 return skb->len;
1196
1197out_nlmsg_trim:
1198nla_put_failure:
1199 nlmsg_trim(skb, b);
1200 return -1;
1201}
1202
1203static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1204 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001205 struct tcf_block *block, struct Qdisc *q,
1206 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001207{
1208 struct sk_buff *skb;
1209 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1210
1211 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1212 if (!skb)
1213 return -ENOBUFS;
1214
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001215 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1216 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001217 kfree_skb(skb);
1218 return -EINVAL;
1219 }
1220
1221 if (unicast)
1222 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1223
1224 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1225 n->nlmsg_flags & NLM_F_ECHO);
1226}
1227
1228static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1229 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001230 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001231 u32 parent, void *fh, bool unicast, bool *last,
1232 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001233{
1234 struct sk_buff *skb;
1235 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1236 int err;
1237
1238 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1239 if (!skb)
1240 return -ENOBUFS;
1241
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001242 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1243 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001244 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001245 kfree_skb(skb);
1246 return -EINVAL;
1247 }
1248
Alexander Aring571acf22018-01-18 11:20:53 -05001249 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001250 if (err) {
1251 kfree_skb(skb);
1252 return err;
1253 }
1254
1255 if (unicast)
1256 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1257
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001258 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1259 n->nlmsg_flags & NLM_F_ECHO);
1260 if (err < 0)
1261 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1262 return err;
WANG Cong71203712017-08-07 15:26:50 -07001263}
1264
1265static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001266 struct tcf_block *block, struct Qdisc *q,
1267 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001268 struct tcf_chain *chain, int event)
1269{
1270 struct tcf_proto *tp;
1271
1272 for (tp = rtnl_dereference(chain->filter_chain);
1273 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001274 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001275 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001276}
1277
Vlad Buslovc431f892018-05-31 09:52:53 +03001278static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001279 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001281 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001282 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct tcmsg *t;
1284 u32 protocol;
1285 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001286 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001288 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001289 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001290 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001291 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001292 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001295 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001297 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Vlad Buslovc431f892018-05-31 09:52:53 +03001299 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001300 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001303 tp_created = 0;
1304
David Ahernc21ef3e2017-04-16 09:48:24 -07001305 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001306 if (err < 0)
1307 return err;
1308
David S. Miller942b8162012-06-26 21:48:50 -07001309 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 protocol = TC_H_MIN(t->tcm_info);
1311 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001312 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 parent = t->tcm_parent;
1314 cl = 0;
1315
1316 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001317 /* If no priority is provided by the user,
1318 * we allocate one.
1319 */
1320 if (n->nlmsg_flags & NLM_F_CREATE) {
1321 prio = TC_H_MAKE(0x80000000U, 0U);
1322 prio_allocate = true;
1323 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001324 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
1329 /* Find head of filter chain. */
1330
Vlad Buslovc431f892018-05-31 09:52:53 +03001331 block = tcf_block_find(net, &q, &parent, &cl,
1332 t->tcm_ifindex, t->tcm_block_index, extack);
1333 if (IS_ERR(block)) {
1334 err = PTR_ERR(block);
1335 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001336 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001337
1338 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1339 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001340 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001341 err = -EINVAL;
1342 goto errout;
1343 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001344 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001345 if (!chain) {
Jiri Pirkod5ed72a2018-08-27 20:58:43 +02001346 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001347 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001348 goto errout;
1349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001351 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1352 prio, prio_allocate);
1353 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001354 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001355 err = PTR_ERR(tp);
1356 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358
1359 if (tp == NULL) {
1360 /* Proto-tcf does not exist, create new one */
1361
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001362 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001363 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001364 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Vlad Buslovc431f892018-05-31 09:52:53 +03001368 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001369 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 +01001370 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001374 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001375 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jiri Pirko33a48922017-02-09 14:38:57 +01001377 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001378 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001379 if (IS_ERR(tp)) {
1380 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 goto errout;
1382 }
Minoru Usui12186be2009-06-02 02:17:34 -07001383 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001384 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001385 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001386 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 fh = tp->ops->get(tp, t->tcm_handle);
1391
WANG Cong8113c092017-08-04 21:31:43 -07001392 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001393 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001394 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 +01001395 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001397 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001398 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1399 NL_SET_ERR_MSG(extack, "Filter already exists");
1400 err = -EEXIST;
1401 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
Jiri Pirko9f407f12018-07-23 09:23:07 +02001404 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1405 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1406 err = -EINVAL;
1407 goto errout;
1408 }
1409
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001410 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001411 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1412 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001413 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001414 if (tp_created)
1415 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001416 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001417 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001418 } else {
1419 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001420 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001424 if (chain)
1425 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001426 tcf_block_release(q, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (err == -EAGAIN)
1428 /* Replay the request. */
1429 goto replay;
1430 return err;
1431}
1432
Vlad Buslovc431f892018-05-31 09:52:53 +03001433static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1434 struct netlink_ext_ack *extack)
1435{
1436 struct net *net = sock_net(skb->sk);
1437 struct nlattr *tca[TCA_MAX + 1];
1438 struct tcmsg *t;
1439 u32 protocol;
1440 u32 prio;
1441 u32 parent;
1442 u32 chain_index;
1443 struct Qdisc *q = NULL;
1444 struct tcf_chain_info chain_info;
1445 struct tcf_chain *chain = NULL;
1446 struct tcf_block *block;
1447 struct tcf_proto *tp = NULL;
1448 unsigned long cl = 0;
1449 void *fh = NULL;
1450 int err;
1451
1452 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1453 return -EPERM;
1454
1455 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1456 if (err < 0)
1457 return err;
1458
1459 t = nlmsg_data(n);
1460 protocol = TC_H_MIN(t->tcm_info);
1461 prio = TC_H_MAJ(t->tcm_info);
1462 parent = t->tcm_parent;
1463
1464 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1465 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1466 return -ENOENT;
1467 }
1468
1469 /* Find head of filter chain. */
1470
1471 block = tcf_block_find(net, &q, &parent, &cl,
1472 t->tcm_ifindex, t->tcm_block_index, extack);
1473 if (IS_ERR(block)) {
1474 err = PTR_ERR(block);
1475 goto errout;
1476 }
1477
1478 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1479 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1480 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1481 err = -EINVAL;
1482 goto errout;
1483 }
1484 chain = tcf_chain_get(block, chain_index, false);
1485 if (!chain) {
Jiri Pirko5ca8a252018-08-03 11:08:47 +02001486 /* User requested flush on non-existent chain. Nothing to do,
1487 * so just return success.
1488 */
1489 if (prio == 0) {
1490 err = 0;
1491 goto errout;
1492 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001493 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Jiri Pirkob7b42472018-08-27 20:58:44 +02001494 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001495 goto errout;
1496 }
1497
1498 if (prio == 0) {
1499 tfilter_notify_chain(net, skb, block, q, parent, n,
1500 chain, RTM_DELTFILTER);
1501 tcf_chain_flush(chain);
1502 err = 0;
1503 goto errout;
1504 }
1505
1506 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1507 prio, false);
1508 if (!tp || IS_ERR(tp)) {
1509 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001510 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001511 goto errout;
1512 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1513 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1514 err = -EINVAL;
1515 goto errout;
1516 }
1517
1518 fh = tp->ops->get(tp, t->tcm_handle);
1519
1520 if (!fh) {
1521 if (t->tcm_handle == 0) {
1522 tcf_chain_tp_remove(chain, &chain_info, tp);
1523 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1524 RTM_DELTFILTER, false);
1525 tcf_proto_destroy(tp, extack);
1526 err = 0;
1527 } else {
1528 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1529 err = -ENOENT;
1530 }
1531 } else {
1532 bool last;
1533
1534 err = tfilter_del_notify(net, skb, n, tp, block,
1535 q, parent, fh, false, &last,
1536 extack);
1537 if (err)
1538 goto errout;
1539 if (last) {
1540 tcf_chain_tp_remove(chain, &chain_info, tp);
1541 tcf_proto_destroy(tp, extack);
1542 }
1543 }
1544
1545errout:
1546 if (chain)
1547 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001548 tcf_block_release(q, block);
Vlad Buslovc431f892018-05-31 09:52:53 +03001549 return err;
1550}
1551
1552static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1553 struct netlink_ext_ack *extack)
1554{
1555 struct net *net = sock_net(skb->sk);
1556 struct nlattr *tca[TCA_MAX + 1];
1557 struct tcmsg *t;
1558 u32 protocol;
1559 u32 prio;
1560 u32 parent;
1561 u32 chain_index;
1562 struct Qdisc *q = NULL;
1563 struct tcf_chain_info chain_info;
1564 struct tcf_chain *chain = NULL;
1565 struct tcf_block *block;
1566 struct tcf_proto *tp = NULL;
1567 unsigned long cl = 0;
1568 void *fh = NULL;
1569 int err;
1570
1571 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1572 if (err < 0)
1573 return err;
1574
1575 t = nlmsg_data(n);
1576 protocol = TC_H_MIN(t->tcm_info);
1577 prio = TC_H_MAJ(t->tcm_info);
1578 parent = t->tcm_parent;
1579
1580 if (prio == 0) {
1581 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1582 return -ENOENT;
1583 }
1584
1585 /* Find head of filter chain. */
1586
1587 block = tcf_block_find(net, &q, &parent, &cl,
1588 t->tcm_ifindex, t->tcm_block_index, extack);
1589 if (IS_ERR(block)) {
1590 err = PTR_ERR(block);
1591 goto errout;
1592 }
1593
1594 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1595 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1596 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1597 err = -EINVAL;
1598 goto errout;
1599 }
1600 chain = tcf_chain_get(block, chain_index, false);
1601 if (!chain) {
1602 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1603 err = -EINVAL;
1604 goto errout;
1605 }
1606
1607 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1608 prio, false);
1609 if (!tp || IS_ERR(tp)) {
1610 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001611 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001612 goto errout;
1613 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1614 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1615 err = -EINVAL;
1616 goto errout;
1617 }
1618
1619 fh = tp->ops->get(tp, t->tcm_handle);
1620
1621 if (!fh) {
1622 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1623 err = -ENOENT;
1624 } else {
1625 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1626 fh, RTM_NEWTFILTER, true);
1627 if (err < 0)
1628 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1629 }
1630
1631errout:
1632 if (chain)
1633 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001634 tcf_block_release(q, block);
Vlad Buslovc431f892018-05-31 09:52:53 +03001635 return err;
1636}
1637
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001638struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct tcf_walker w;
1640 struct sk_buff *skb;
1641 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001642 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001643 struct Qdisc *q;
1644 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645};
1646
WANG Cong8113c092017-08-04 21:31:43 -07001647static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001649 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001650 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001652 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001653 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001654 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1655 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001658static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1659 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001660 long index_start, long *p_index)
1661{
1662 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001663 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001664 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1665 struct tcf_dump_args arg;
1666 struct tcf_proto *tp;
1667
1668 for (tp = rtnl_dereference(chain->filter_chain);
1669 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1670 if (*p_index < index_start)
1671 continue;
1672 if (TC_H_MAJ(tcm->tcm_info) &&
1673 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1674 continue;
1675 if (TC_H_MIN(tcm->tcm_info) &&
1676 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1677 continue;
1678 if (*p_index > index_start)
1679 memset(&cb->args[1], 0,
1680 sizeof(cb->args) - sizeof(cb->args[0]));
1681 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001682 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001683 NETLINK_CB(cb->skb).portid,
1684 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1685 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001686 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001687
1688 cb->args[1] = 1;
1689 }
1690 if (!tp->ops->walk)
1691 continue;
1692 arg.w.fn = tcf_node_dump;
1693 arg.skb = skb;
1694 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001695 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001696 arg.q = q;
1697 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001698 arg.w.stop = 0;
1699 arg.w.skip = cb->args[1] - 1;
1700 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001701 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001702 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001703 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001704 cb->args[1] = arg.w.count + 1;
1705 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001706 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001707 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001708 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001709}
1710
Eric Dumazetbd27a872009-11-05 20:57:26 -08001711/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1713{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001714 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001715 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001716 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001717 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001718 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001719 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001720 long index_start;
1721 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001722 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001723 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Hong zhi guo573ce262013-03-27 06:47:04 +00001725 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001727
1728 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1729 if (err)
1730 return err;
1731
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001732 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001733 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001734 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001735 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001736 /* If we work with block index, q is NULL and parent value
1737 * will never be used in the following code. The check
1738 * in tcf_fill_node prevents it. However, compiler does not
1739 * see that far, so set parent to zero to silence the warning
1740 * about parent being uninitialized.
1741 */
1742 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001743 } else {
1744 const struct Qdisc_class_ops *cops;
1745 struct net_device *dev;
1746 unsigned long cl = 0;
1747
1748 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1749 if (!dev)
1750 return skb->len;
1751
1752 parent = tcm->tcm_parent;
1753 if (!parent) {
1754 q = dev->qdisc;
1755 parent = q->handle;
1756 } else {
1757 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1758 }
1759 if (!q)
1760 goto out;
1761 cops = q->ops->cl_ops;
1762 if (!cops)
1763 goto out;
1764 if (!cops->tcf_block)
1765 goto out;
1766 if (TC_H_MIN(tcm->tcm_parent)) {
1767 cl = cops->find(q, tcm->tcm_parent);
1768 if (cl == 0)
1769 goto out;
1770 }
1771 block = cops->tcf_block(q, cl, NULL);
1772 if (!block)
1773 goto out;
1774 if (tcf_block_shared(block))
1775 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001778 index_start = cb->args[0];
1779 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001780
1781 list_for_each_entry(chain, &block->chain_list, list) {
1782 if (tca[TCA_CHAIN] &&
1783 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1784 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001785 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001786 index_start, &index)) {
1787 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001788 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001789 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001790 }
1791
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001792 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1793 tcf_block_refcnt_put(block);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001794 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001797 /* If we did no progress, the error (EMSGSIZE) is real */
1798 if (skb->len == 0 && err)
1799 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return skb->len;
1801}
1802
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001803static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1804 struct sk_buff *skb, struct tcf_block *block,
1805 u32 portid, u32 seq, u16 flags, int event)
1806{
1807 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02001808 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001809 struct nlmsghdr *nlh;
1810 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02001811 void *priv;
1812
1813 ops = chain->tmplt_ops;
1814 priv = chain->tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001815
1816 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1817 if (!nlh)
1818 goto out_nlmsg_trim;
1819 tcm = nlmsg_data(nlh);
1820 tcm->tcm_family = AF_UNSPEC;
1821 tcm->tcm__pad1 = 0;
1822 tcm->tcm__pad2 = 0;
1823 tcm->tcm_handle = 0;
1824 if (block->q) {
1825 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1826 tcm->tcm_parent = block->q->handle;
1827 } else {
1828 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1829 tcm->tcm_block_index = block->index;
1830 }
1831
1832 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1833 goto nla_put_failure;
1834
Jiri Pirko9f407f12018-07-23 09:23:07 +02001835 if (ops) {
1836 if (nla_put_string(skb, TCA_KIND, ops->kind))
1837 goto nla_put_failure;
1838 if (ops->tmplt_dump(skb, net, priv) < 0)
1839 goto nla_put_failure;
1840 }
1841
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001842 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1843 return skb->len;
1844
1845out_nlmsg_trim:
1846nla_put_failure:
1847 nlmsg_trim(skb, b);
1848 return -EMSGSIZE;
1849}
1850
1851static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1852 u32 seq, u16 flags, int event, bool unicast)
1853{
1854 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1855 struct tcf_block *block = chain->block;
1856 struct net *net = block->net;
1857 struct sk_buff *skb;
1858
1859 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1860 if (!skb)
1861 return -ENOBUFS;
1862
1863 if (tc_chain_fill_node(chain, net, skb, block, portid,
1864 seq, flags, event) <= 0) {
1865 kfree_skb(skb);
1866 return -EINVAL;
1867 }
1868
1869 if (unicast)
1870 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1871
1872 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1873}
1874
Jiri Pirko9f407f12018-07-23 09:23:07 +02001875static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1876 struct nlattr **tca,
1877 struct netlink_ext_ack *extack)
1878{
1879 const struct tcf_proto_ops *ops;
1880 void *tmplt_priv;
1881
1882 /* If kind is not set, user did not specify template. */
1883 if (!tca[TCA_KIND])
1884 return 0;
1885
1886 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1887 if (IS_ERR(ops))
1888 return PTR_ERR(ops);
1889 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1890 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1891 return -EOPNOTSUPP;
1892 }
1893
1894 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1895 if (IS_ERR(tmplt_priv)) {
1896 module_put(ops->owner);
1897 return PTR_ERR(tmplt_priv);
1898 }
1899 chain->tmplt_ops = ops;
1900 chain->tmplt_priv = tmplt_priv;
1901 return 0;
1902}
1903
1904static void tc_chain_tmplt_del(struct tcf_chain *chain)
1905{
1906 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1907
1908 /* If template ops are set, no work to do for us. */
1909 if (!ops)
1910 return;
1911
1912 ops->tmplt_destroy(chain->tmplt_priv);
1913 module_put(ops->owner);
1914}
1915
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001916/* Add/delete/get a chain */
1917
1918static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1919 struct netlink_ext_ack *extack)
1920{
1921 struct net *net = sock_net(skb->sk);
1922 struct nlattr *tca[TCA_MAX + 1];
1923 struct tcmsg *t;
1924 u32 parent;
1925 u32 chain_index;
1926 struct Qdisc *q = NULL;
1927 struct tcf_chain *chain = NULL;
1928 struct tcf_block *block;
1929 unsigned long cl;
1930 int err;
1931
1932 if (n->nlmsg_type != RTM_GETCHAIN &&
1933 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1934 return -EPERM;
1935
1936replay:
1937 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1938 if (err < 0)
1939 return err;
1940
1941 t = nlmsg_data(n);
1942 parent = t->tcm_parent;
1943 cl = 0;
1944
1945 block = tcf_block_find(net, &q, &parent, &cl,
1946 t->tcm_ifindex, t->tcm_block_index, extack);
1947 if (IS_ERR(block))
1948 return PTR_ERR(block);
1949
1950 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1951 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1952 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001953 err = -EINVAL;
1954 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001955 }
1956 chain = tcf_chain_lookup(block, chain_index);
1957 if (n->nlmsg_type == RTM_NEWCHAIN) {
1958 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001959 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001960 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001961 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001962 */
1963 tcf_chain_hold(chain);
1964 } else {
1965 NL_SET_ERR_MSG(extack, "Filter chain already exists");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001966 err = -EEXIST;
1967 goto errout_block;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001968 }
1969 } else {
1970 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1971 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 +03001972 err = -ENOENT;
1973 goto errout_block;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001974 }
1975 chain = tcf_chain_create(block, chain_index);
1976 if (!chain) {
1977 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001978 err = -ENOMEM;
1979 goto errout_block;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001980 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001981 }
1982 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001983 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001984 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001985 err = -EINVAL;
1986 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001987 }
1988 tcf_chain_hold(chain);
1989 }
1990
1991 switch (n->nlmsg_type) {
1992 case RTM_NEWCHAIN:
Jiri Pirko9f407f12018-07-23 09:23:07 +02001993 err = tc_chain_tmplt_add(chain, net, tca, extack);
1994 if (err)
1995 goto errout;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001996 /* In case the chain was successfully added, take a reference
1997 * to the chain. This ensures that an empty chain
1998 * does not disappear at the end of this function.
1999 */
2000 tcf_chain_hold(chain);
2001 chain->explicitly_created = true;
2002 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2003 RTM_NEWCHAIN, false);
2004 break;
2005 case RTM_DELCHAIN:
Cong Wangf5b9bac2018-09-11 14:22:23 -07002006 tfilter_notify_chain(net, skb, block, q, parent, n,
2007 chain, RTM_DELTFILTER);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002008 /* Flush the chain first as the user requested chain removal. */
2009 tcf_chain_flush(chain);
2010 /* In case the chain was successfully deleted, put a reference
2011 * to the chain previously taken during addition.
2012 */
2013 tcf_chain_put_explicitly_created(chain);
Jiri Pirkoc921d7d2018-07-26 18:27:58 +02002014 chain->explicitly_created = false;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002015 break;
2016 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002017 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2018 n->nlmsg_seq, n->nlmsg_type, true);
2019 if (err < 0)
2020 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2021 break;
2022 default:
2023 err = -EOPNOTSUPP;
2024 NL_SET_ERR_MSG(extack, "Unsupported message type");
2025 goto errout;
2026 }
2027
2028errout:
2029 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03002030errout_block:
2031 tcf_block_release(q, block);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002032 if (err == -EAGAIN)
2033 /* Replay the request. */
2034 goto replay;
2035 return err;
2036}
2037
2038/* called with RTNL */
2039static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2040{
2041 struct net *net = sock_net(skb->sk);
2042 struct nlattr *tca[TCA_MAX + 1];
2043 struct Qdisc *q = NULL;
2044 struct tcf_block *block;
2045 struct tcf_chain *chain;
2046 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2047 long index_start;
2048 long index;
2049 u32 parent;
2050 int err;
2051
2052 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2053 return skb->len;
2054
2055 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
2056 if (err)
2057 return err;
2058
2059 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002060 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002061 if (!block)
2062 goto out;
2063 /* If we work with block index, q is NULL and parent value
2064 * will never be used in the following code. The check
2065 * in tcf_fill_node prevents it. However, compiler does not
2066 * see that far, so set parent to zero to silence the warning
2067 * about parent being uninitialized.
2068 */
2069 parent = 0;
2070 } else {
2071 const struct Qdisc_class_ops *cops;
2072 struct net_device *dev;
2073 unsigned long cl = 0;
2074
2075 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2076 if (!dev)
2077 return skb->len;
2078
2079 parent = tcm->tcm_parent;
2080 if (!parent) {
2081 q = dev->qdisc;
2082 parent = q->handle;
2083 } else {
2084 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2085 }
2086 if (!q)
2087 goto out;
2088 cops = q->ops->cl_ops;
2089 if (!cops)
2090 goto out;
2091 if (!cops->tcf_block)
2092 goto out;
2093 if (TC_H_MIN(tcm->tcm_parent)) {
2094 cl = cops->find(q, tcm->tcm_parent);
2095 if (cl == 0)
2096 goto out;
2097 }
2098 block = cops->tcf_block(q, cl, NULL);
2099 if (!block)
2100 goto out;
2101 if (tcf_block_shared(block))
2102 q = NULL;
2103 }
2104
2105 index_start = cb->args[0];
2106 index = 0;
2107
2108 list_for_each_entry(chain, &block->chain_list, list) {
2109 if ((tca[TCA_CHAIN] &&
2110 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2111 continue;
2112 if (index < index_start) {
2113 index++;
2114 continue;
2115 }
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002116 if (tcf_chain_held_by_acts_only(chain))
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002117 continue;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002118 err = tc_chain_fill_node(chain, net, skb, block,
2119 NETLINK_CB(cb->skb).portid,
2120 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2121 RTM_NEWCHAIN);
2122 if (err <= 0)
2123 break;
2124 index++;
2125 }
2126
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002127 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
2128 tcf_block_refcnt_put(block);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002129 cb->args[0] = index;
2130
2131out:
2132 /* If we did no progress, the error (EMSGSIZE) is real */
2133 if (skb->len == 0 && err)
2134 return err;
2135 return skb->len;
2136}
2137
WANG Cong18d02642014-09-25 10:26:37 -07002138void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140#ifdef CONFIG_NET_CLS_ACT
Vlad Buslov90b73b72018-07-05 17:24:33 +03002141 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
WANG Cong22dc13c2016-08-13 22:35:00 -07002142 kfree(exts->actions);
2143 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144#endif
2145}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002146EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00002148int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05002149 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2150 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152#ifdef CONFIG_NET_CLS_ACT
2153 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05002155 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
WANG Cong5da57f42013-12-15 20:15:07 -08002157 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002158 act = tcf_action_init_1(net, tp, tb[exts->police],
2159 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03002160 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08002161 if (IS_ERR(act))
2162 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
WANG Cong33be6272013-12-15 20:15:05 -08002164 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07002165 exts->actions[0] = act;
2166 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08002167 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03002168 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07002169
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002170 err = tcf_action_init(net, tp, tb[exts->action],
2171 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03002172 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03002173 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03002174 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002175 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03002176 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
Cong Wange4b95c42017-11-06 13:47:19 -08002178 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180#else
WANG Cong5da57f42013-12-15 20:15:07 -08002181 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05002182 (exts->police && tb[exts->police])) {
2183 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05002185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#endif
2187
2188 return 0;
2189}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002190EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002192void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07002195 struct tcf_exts old = *dst;
2196
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002197 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07002198 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199#endif
2200}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002201EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
WANG Cong22dc13c2016-08-13 22:35:00 -07002203#ifdef CONFIG_NET_CLS_ACT
2204static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2205{
2206 if (exts->nr_actions == 0)
2207 return NULL;
2208 else
2209 return exts->actions[0];
2210}
2211#endif
WANG Cong33be6272013-12-15 20:15:05 -08002212
WANG Cong5da57f42013-12-15 20:15:07 -08002213int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
2215#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07002216 struct nlattr *nest;
2217
Jiri Pirko978dfd82017-08-04 14:29:03 +02002218 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 /*
2220 * again for backward compatible mode - we want
2221 * to work with both old and new modes of entering
2222 * tc data even if iproute2 was newer - jhs
2223 */
WANG Cong33be6272013-12-15 20:15:05 -08002224 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08002225 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002226 if (nest == NULL)
2227 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07002228
Vlad Buslov90b73b72018-07-05 17:24:33 +03002229 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002230 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002231 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08002232 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08002233 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08002234 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05002235 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002236 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08002237 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002238 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002239 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
2241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07002243
2244nla_put_failure:
2245 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07002247#else
2248 return 0;
2249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002251EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002253
WANG Cong5da57f42013-12-15 20:15:07 -08002254int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
2256#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08002257 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01002258 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002259 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260#endif
2261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002263EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Jiri Pirko717503b2017-10-11 09:41:09 +02002265static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2266 enum tc_setup_type type,
2267 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002268{
2269 int ok_count = 0;
2270#ifdef CONFIG_NET_CLS_ACT
2271 const struct tc_action *a;
2272 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002273 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002274
2275 if (!tcf_exts_has_actions(exts))
2276 return 0;
2277
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002278 for (i = 0; i < exts->nr_actions; i++) {
2279 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002280 if (!a->ops->get_dev)
2281 continue;
2282 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01002283 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002284 continue;
2285 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
Vlad Buslov84a75b32018-08-10 20:51:52 +03002286 a->ops->put_dev(dev);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002287 if (ret < 0)
2288 return ret;
2289 ok_count += ret;
2290 }
2291#endif
2292 return ok_count;
2293}
Jiri Pirko717503b2017-10-11 09:41:09 +02002294
Jiri Pirko208c0f42017-10-19 15:50:32 +02002295int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2296 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02002297{
David S. Miller9a99dc12018-06-06 13:55:47 -04002298 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002299 int ret;
2300
David S. Miller9a99dc12018-06-06 13:55:47 -04002301 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2302 if (ret < 0)
2303 return ret;
2304 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002305
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03002306 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04002307 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002308 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2309 if (ret < 0)
2310 return ret;
2311 ok_count += ret;
2312
2313 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02002314}
2315EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002316
Jiri Pirko48617382018-01-17 11:46:46 +01002317static __net_init int tcf_net_init(struct net *net)
2318{
2319 struct tcf_net *tn = net_generic(net, tcf_net_id);
2320
Vlad Buslovab281622018-09-24 19:22:56 +03002321 spin_lock_init(&tn->idr_lock);
Jiri Pirko48617382018-01-17 11:46:46 +01002322 idr_init(&tn->idr);
2323 return 0;
2324}
2325
2326static void __net_exit tcf_net_exit(struct net *net)
2327{
2328 struct tcf_net *tn = net_generic(net, tcf_net_id);
2329
2330 idr_destroy(&tn->idr);
2331}
2332
2333static struct pernet_operations tcf_net_ops = {
2334 .init = tcf_net_init,
2335 .exit = tcf_net_exit,
2336 .id = &tcf_net_id,
2337 .size = sizeof(struct tcf_net),
2338};
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340static int __init tc_filter_init(void)
2341{
Jiri Pirko48617382018-01-17 11:46:46 +01002342 int err;
2343
Cong Wang7aa00452017-10-26 18:24:28 -07002344 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2345 if (!tc_filter_wq)
2346 return -ENOMEM;
2347
Jiri Pirko48617382018-01-17 11:46:46 +01002348 err = register_pernet_subsys(&tcf_net_ops);
2349 if (err)
2350 goto err_register_pernet_subsys;
2351
Vlad Buslovc431f892018-05-31 09:52:53 +03002352 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2353 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2354 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02002355 tc_dump_tfilter, 0);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002356 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2357 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2358 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2359 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01002362
2363err_register_pernet_subsys:
2364 destroy_workqueue(tc_filter_wq);
2365 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
2368subsys_initcall(tc_filter_init);