blob: 5a21888d0ee9a18fd25811b5cdf473686d798f88 [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))
Cong Wangefbf7892017-12-04 10:48:18 -0800244 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700245}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200246
Cong Wange2ef7542017-09-11 16:33:31 -0700247static void tcf_chain_hold(struct tcf_chain *chain)
248{
249 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200250}
251
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200252static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200253{
254 /* In case all the references are action references, this
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200255 * chain should not be shown to the user.
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200256 */
257 return chain->refcnt == chain->action_refcnt;
258}
259
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200260static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
261 u32 chain_index)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200262{
263 struct tcf_chain *chain;
264
265 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200266 if (chain->index == chain_index)
Cong Wange2ef7542017-09-11 16:33:31 -0700267 return chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200268 }
269 return NULL;
270}
271
272static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
273 u32 seq, u16 flags, int event, bool unicast);
274
Jiri Pirko53681402018-08-01 12:36:56 +0200275static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
276 u32 chain_index, bool create,
277 bool by_act)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200278{
279 struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
280
281 if (chain) {
282 tcf_chain_hold(chain);
Jiri Pirko53681402018-08-01 12:36:56 +0200283 } else {
284 if (!create)
285 return NULL;
286 chain = tcf_chain_create(block, chain_index);
287 if (!chain)
288 return NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200289 }
Jiri Pirko80532382017-09-06 13:14:19 +0200290
Jiri Pirko53681402018-08-01 12:36:56 +0200291 if (by_act)
292 ++chain->action_refcnt;
293
294 /* Send notification only in case we got the first
295 * non-action reference. Until then, the chain acts only as
296 * a placeholder for actions pointing to it and user ought
297 * not know about them.
298 */
299 if (chain->refcnt - chain->action_refcnt == 1 && !by_act)
300 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
301 RTM_NEWCHAIN, false);
302
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200303 return chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200304}
Jiri Pirko53681402018-08-01 12:36:56 +0200305
Jiri Pirko290b1c82018-08-01 12:36:57 +0200306static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
307 bool create)
Jiri Pirko53681402018-08-01 12:36:56 +0200308{
309 return __tcf_chain_get(block, chain_index, create, false);
310}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200311
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200312struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
313{
Jiri Pirko53681402018-08-01 12:36:56 +0200314 return __tcf_chain_get(block, chain_index, true, true);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200315}
316EXPORT_SYMBOL(tcf_chain_get_by_act);
317
Jiri Pirko9f407f12018-07-23 09:23:07 +0200318static void tc_chain_tmplt_del(struct tcf_chain *chain);
319
Jiri Pirko53681402018-08-01 12:36:56 +0200320static void __tcf_chain_put(struct tcf_chain *chain, bool by_act)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200321{
Jiri Pirko53681402018-08-01 12:36:56 +0200322 if (by_act)
323 chain->action_refcnt--;
324 chain->refcnt--;
325
326 /* The last dropped non-action reference will trigger notification. */
327 if (chain->refcnt - chain->action_refcnt == 0 && !by_act)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200328 tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
Jiri Pirko53681402018-08-01 12:36:56 +0200329
330 if (chain->refcnt == 0) {
Jiri Pirko9f407f12018-07-23 09:23:07 +0200331 tc_chain_tmplt_del(chain);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200332 tcf_chain_destroy(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200333 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200334}
Jiri Pirko53681402018-08-01 12:36:56 +0200335
Jiri Pirko290b1c82018-08-01 12:36:57 +0200336static void tcf_chain_put(struct tcf_chain *chain)
Jiri Pirko53681402018-08-01 12:36:56 +0200337{
338 __tcf_chain_put(chain, false);
339}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200340
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200341void tcf_chain_put_by_act(struct tcf_chain *chain)
342{
Jiri Pirko53681402018-08-01 12:36:56 +0200343 __tcf_chain_put(chain, true);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200344}
345EXPORT_SYMBOL(tcf_chain_put_by_act);
346
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200347static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
348{
349 if (chain->explicitly_created)
350 tcf_chain_put(chain);
351}
352
Jiri Pirko290b1c82018-08-01 12:36:57 +0200353static void tcf_chain_flush(struct tcf_chain *chain)
354{
355 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
356
357 tcf_chain0_head_change(chain, NULL);
358 while (tp) {
359 RCU_INIT_POINTER(chain->filter_chain, tp->next);
360 tcf_proto_destroy(tp, NULL);
361 tp = rtnl_dereference(chain->filter_chain);
362 tcf_chain_put(chain);
363 }
364}
365
Jiri Pirkocaa72602018-01-17 11:46:50 +0100366static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200367{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100368 return block->offloadcnt;
369}
370
371static int tcf_block_offload_cmd(struct tcf_block *block,
372 struct net_device *dev,
373 struct tcf_block_ext_info *ei,
John Hurley60513bd2018-06-25 14:30:04 -0700374 enum tc_block_command command,
375 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100376{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200377 struct tc_block_offload bo = {};
378
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200379 bo.command = command;
380 bo.binder_type = ei->binder_type;
381 bo.block = block;
John Hurley60513bd2018-06-25 14:30:04 -0700382 bo.extack = extack;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100383 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200384}
385
Jiri Pirkocaa72602018-01-17 11:46:50 +0100386static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700387 struct tcf_block_ext_info *ei,
388 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200389{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100390 struct net_device *dev = q->dev_queue->dev;
391 int err;
392
393 if (!dev->netdev_ops->ndo_setup_tc)
394 goto no_offload_dev_inc;
395
396 /* If tc offload feature is disabled and the block we try to bind
397 * to already has some offloaded filters, forbid to bind.
398 */
John Hurley60513bd2018-06-25 14:30:04 -0700399 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
400 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Jiri Pirkocaa72602018-01-17 11:46:50 +0100401 return -EOPNOTSUPP;
John Hurley60513bd2018-06-25 14:30:04 -0700402 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100403
John Hurley60513bd2018-06-25 14:30:04 -0700404 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100405 if (err == -EOPNOTSUPP)
406 goto no_offload_dev_inc;
407 return err;
408
409no_offload_dev_inc:
410 if (tcf_block_offload_in_use(block))
411 return -EOPNOTSUPP;
412 block->nooffloaddevcnt++;
413 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200414}
415
416static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
417 struct tcf_block_ext_info *ei)
418{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100419 struct net_device *dev = q->dev_queue->dev;
420 int err;
421
422 if (!dev->netdev_ops->ndo_setup_tc)
423 goto no_offload_dev_dec;
John Hurley60513bd2018-06-25 14:30:04 -0700424 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100425 if (err == -EOPNOTSUPP)
426 goto no_offload_dev_dec;
427 return;
428
429no_offload_dev_dec:
430 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200431}
432
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100433static int
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200434tcf_chain0_head_change_cb_add(struct tcf_block *block,
435 struct tcf_block_ext_info *ei,
436 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100437{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200438 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100439 struct tcf_filter_chain_list_item *item;
440
441 item = kmalloc(sizeof(*item), GFP_KERNEL);
442 if (!item) {
443 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
444 return -ENOMEM;
445 }
446 item->chain_head_change = ei->chain_head_change;
447 item->chain_head_change_priv = ei->chain_head_change_priv;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200448 if (chain0 && chain0->filter_chain)
449 tcf_chain_head_change_item(item, chain0->filter_chain);
450 list_add(&item->list, &block->chain0.filter_chain_list);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100451 return 0;
452}
453
454static void
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200455tcf_chain0_head_change_cb_del(struct tcf_block *block,
456 struct tcf_block_ext_info *ei)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100457{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200458 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100459 struct tcf_filter_chain_list_item *item;
460
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200461 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100462 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
463 (item->chain_head_change == ei->chain_head_change &&
464 item->chain_head_change_priv == ei->chain_head_change_priv)) {
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200465 if (chain0)
466 tcf_chain_head_change_item(item, NULL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100467 list_del(&item->list);
468 kfree(item);
469 return;
470 }
471 }
472 WARN_ON(1);
473}
474
Jiri Pirko48617382018-01-17 11:46:46 +0100475struct tcf_net {
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 Buslovf0023432018-09-24 19:22:55 +0300540static void tcf_block_flush_all_chains(struct tcf_block *block)
541{
542 struct tcf_chain *chain;
543
544 /* Hold a refcnt for all chains, so that they don't disappear
545 * while we are iterating.
546 */
547 list_for_each_entry(chain, &block->chain_list, list)
548 tcf_chain_hold(chain);
549
550 list_for_each_entry(chain, &block->chain_list, list)
551 tcf_chain_flush(chain);
552}
553
554static void tcf_block_put_all_chains(struct tcf_block *block)
555{
556 struct tcf_chain *chain, *tmp;
557
558 /* At this point, all the chains should have refcnt >= 1. */
559 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
560 tcf_chain_put_explicitly_created(chain);
561 tcf_chain_put(chain);
562 }
563}
564
Vlad Buslovc431f892018-05-31 09:52:53 +0300565/* Find tcf block.
566 * Set q, parent, cl when appropriate.
567 */
568
569static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
570 u32 *parent, unsigned long *cl,
571 int ifindex, u32 block_index,
572 struct netlink_ext_ack *extack)
573{
574 struct tcf_block *block;
Vlad Buslove368fdb2018-09-24 19:22:53 +0300575 int err = 0;
Vlad Buslovc431f892018-05-31 09:52:53 +0300576
577 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
578 block = tcf_block_lookup(net, block_index);
579 if (!block) {
580 NL_SET_ERR_MSG(extack, "Block of given index was not found");
581 return ERR_PTR(-EINVAL);
582 }
583 } else {
584 const struct Qdisc_class_ops *cops;
585 struct net_device *dev;
586
Vlad Buslove368fdb2018-09-24 19:22:53 +0300587 rcu_read_lock();
588
Vlad Buslovc431f892018-05-31 09:52:53 +0300589 /* Find link */
Vlad Buslove368fdb2018-09-24 19:22:53 +0300590 dev = dev_get_by_index_rcu(net, ifindex);
591 if (!dev) {
592 rcu_read_unlock();
Vlad Buslovc431f892018-05-31 09:52:53 +0300593 return ERR_PTR(-ENODEV);
Vlad Buslove368fdb2018-09-24 19:22:53 +0300594 }
Vlad Buslovc431f892018-05-31 09:52:53 +0300595
596 /* Find qdisc */
597 if (!*parent) {
598 *q = dev->qdisc;
599 *parent = (*q)->handle;
600 } else {
Vlad Buslove368fdb2018-09-24 19:22:53 +0300601 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
Vlad Buslovc431f892018-05-31 09:52:53 +0300602 if (!*q) {
603 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300604 err = -EINVAL;
605 goto errout_rcu;
Vlad Buslovc431f892018-05-31 09:52:53 +0300606 }
607 }
608
Vlad Buslove368fdb2018-09-24 19:22:53 +0300609 *q = qdisc_refcount_inc_nz(*q);
610 if (!*q) {
611 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
612 err = -EINVAL;
613 goto errout_rcu;
614 }
615
Vlad Buslovc431f892018-05-31 09:52:53 +0300616 /* Is it classful? */
617 cops = (*q)->ops->cl_ops;
618 if (!cops) {
619 NL_SET_ERR_MSG(extack, "Qdisc not classful");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300620 err = -EINVAL;
621 goto errout_rcu;
Vlad Buslovc431f892018-05-31 09:52:53 +0300622 }
623
624 if (!cops->tcf_block) {
625 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300626 err = -EOPNOTSUPP;
627 goto errout_rcu;
Vlad Buslovc431f892018-05-31 09:52:53 +0300628 }
629
Vlad Buslove368fdb2018-09-24 19:22:53 +0300630 /* At this point we know that qdisc is not noop_qdisc,
631 * which means that qdisc holds a reference to net_device
632 * and we hold a reference to qdisc, so it is safe to release
633 * rcu read lock.
634 */
635 rcu_read_unlock();
636
Vlad Buslovc431f892018-05-31 09:52:53 +0300637 /* Do we search for filter, attached to class? */
638 if (TC_H_MIN(*parent)) {
639 *cl = cops->find(*q, *parent);
640 if (*cl == 0) {
641 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
Vlad Buslove368fdb2018-09-24 19:22:53 +0300642 err = -ENOENT;
643 goto errout_qdisc;
Vlad Buslovc431f892018-05-31 09:52:53 +0300644 }
645 }
646
647 /* And the last stroke */
648 block = cops->tcf_block(*q, *cl, extack);
Vlad Buslove368fdb2018-09-24 19:22:53 +0300649 if (!block) {
650 err = -EINVAL;
651 goto errout_qdisc;
652 }
Vlad Buslovc431f892018-05-31 09:52:53 +0300653 if (tcf_block_shared(block)) {
654 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 +0300655 err = -EOPNOTSUPP;
656 goto errout_qdisc;
Vlad Buslovc431f892018-05-31 09:52:53 +0300657 }
658 }
659
660 return block;
Vlad Buslove368fdb2018-09-24 19:22:53 +0300661
662errout_rcu:
663 rcu_read_unlock();
664errout_qdisc:
665 if (*q)
666 qdisc_put(*q);
667 return ERR_PTR(err);
668}
669
670static void tcf_block_release(struct Qdisc *q, struct tcf_block *block)
671{
672 if (q)
673 qdisc_put(q);
Vlad Buslovc431f892018-05-31 09:52:53 +0300674}
675
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100676struct tcf_block_owner_item {
677 struct list_head list;
678 struct Qdisc *q;
679 enum tcf_block_binder_type binder_type;
680};
681
682static void
683tcf_block_owner_netif_keep_dst(struct tcf_block *block,
684 struct Qdisc *q,
685 enum tcf_block_binder_type binder_type)
686{
687 if (block->keep_dst &&
688 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
689 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
690 netif_keep_dst(qdisc_dev(q));
691}
692
693void tcf_block_netif_keep_dst(struct tcf_block *block)
694{
695 struct tcf_block_owner_item *item;
696
697 block->keep_dst = true;
698 list_for_each_entry(item, &block->owner_list, list)
699 tcf_block_owner_netif_keep_dst(block, item->q,
700 item->binder_type);
701}
702EXPORT_SYMBOL(tcf_block_netif_keep_dst);
703
704static int tcf_block_owner_add(struct tcf_block *block,
705 struct Qdisc *q,
706 enum tcf_block_binder_type binder_type)
707{
708 struct tcf_block_owner_item *item;
709
710 item = kmalloc(sizeof(*item), GFP_KERNEL);
711 if (!item)
712 return -ENOMEM;
713 item->q = q;
714 item->binder_type = binder_type;
715 list_add(&item->list, &block->owner_list);
716 return 0;
717}
718
719static void tcf_block_owner_del(struct tcf_block *block,
720 struct Qdisc *q,
721 enum tcf_block_binder_type binder_type)
722{
723 struct tcf_block_owner_item *item;
724
725 list_for_each_entry(item, &block->owner_list, list) {
726 if (item->q == q && item->binder_type == binder_type) {
727 list_del(&item->list);
728 kfree(item);
729 return;
730 }
731 }
732 WARN_ON(1);
733}
734
Jiri Pirko48617382018-01-17 11:46:46 +0100735int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
736 struct tcf_block_ext_info *ei,
737 struct netlink_ext_ack *extack)
738{
739 struct net *net = qdisc_net(q);
740 struct tcf_block *block = NULL;
741 bool created = false;
742 int err;
743
744 if (ei->block_index) {
745 /* block_index not 0 means the shared block is requested */
746 block = tcf_block_lookup(net, ei->block_index);
747 if (block)
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300748 refcount_inc(&block->refcnt);
Jiri Pirko48617382018-01-17 11:46:46 +0100749 }
750
751 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100752 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100753 if (IS_ERR(block))
754 return PTR_ERR(block);
755 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100756 if (tcf_block_shared(block)) {
757 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100758 if (err)
759 goto err_block_insert;
760 }
761 }
762
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100763 err = tcf_block_owner_add(block, q, ei->binder_type);
764 if (err)
765 goto err_block_owner_add;
766
767 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
768
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200769 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100770 if (err)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200771 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100772
John Hurley60513bd2018-06-25 14:30:04 -0700773 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100774 if (err)
775 goto err_block_offload_bind;
776
Jiri Pirko6529eab2017-05-17 11:07:55 +0200777 *p_block = block;
778 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200779
Jiri Pirkocaa72602018-01-17 11:46:50 +0100780err_block_offload_bind:
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200781 tcf_chain0_head_change_cb_del(block, ei);
782err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100783 tcf_block_owner_del(block, q, ei->binder_type);
784err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100785 if (created) {
786 if (tcf_block_shared(block))
787 tcf_block_remove(block, net);
788err_block_insert:
Jiri Pirko48617382018-01-17 11:46:46 +0100789 kfree(block);
790 } else {
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300791 refcount_dec(&block->refcnt);
Jiri Pirko48617382018-01-17 11:46:46 +0100792 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200793 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200794}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200795EXPORT_SYMBOL(tcf_block_get_ext);
796
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100797static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
798{
799 struct tcf_proto __rcu **p_filter_chain = priv;
800
801 rcu_assign_pointer(*p_filter_chain, tp_head);
802}
803
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200804int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500805 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
806 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200807{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100808 struct tcf_block_ext_info ei = {
809 .chain_head_change = tcf_chain_head_change_dflt,
810 .chain_head_change_priv = p_filter_chain,
811 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200812
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100813 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500814 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200815}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200816EXPORT_SYMBOL(tcf_block_get);
817
Cong Wang7aa00452017-10-26 18:24:28 -0700818/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100819 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700820 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100821void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900822 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700823{
David S. Millerc30abd52017-12-16 22:11:55 -0500824 if (!block)
825 return;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200826 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100827 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100828
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300829 if (refcount_dec_and_test(&block->refcnt)) {
830 /* Flushing/putting all chains will cause the block to be
831 * deallocated when last chain is freed. However, if chain_list
832 * is empty, block has to be manually deallocated. After block
833 * reference counter reached 0, it is no longer possible to
834 * increment it or add new chains to block.
835 */
836 bool free_block = list_empty(&block->chain_list);
837
Jiri Pirko48617382018-01-17 11:46:46 +0100838 if (tcf_block_shared(block))
839 tcf_block_remove(block, block->net);
Vlad Buslovf0023432018-09-24 19:22:55 +0300840 if (!free_block)
841 tcf_block_flush_all_chains(block);
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300842 tcf_block_offload_unbind(block, q, ei);
843
Vlad Buslovf0023432018-09-24 19:22:55 +0300844 if (free_block)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200845 kfree(block);
Vlad Buslovf0023432018-09-24 19:22:55 +0300846 else
847 tcf_block_put_all_chains(block);
Jiri Pirko63cc5bc2018-08-08 14:04:13 +0200848 } else {
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300849 tcf_block_offload_unbind(block, q, ei);
Jiri Pirko48617382018-01-17 11:46:46 +0100850 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200851}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200852EXPORT_SYMBOL(tcf_block_put_ext);
853
854void tcf_block_put(struct tcf_block *block)
855{
856 struct tcf_block_ext_info ei = {0, };
857
Jiri Pirko4853f122017-12-21 13:13:59 +0100858 if (!block)
859 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100860 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200861}
David S. Millere1ea2f92017-10-30 14:10:01 +0900862
Jiri Pirko6529eab2017-05-17 11:07:55 +0200863EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100864
Jiri Pirkoacb67442017-10-19 15:50:31 +0200865struct tcf_block_cb {
866 struct list_head list;
867 tc_setup_cb_t *cb;
868 void *cb_ident;
869 void *cb_priv;
870 unsigned int refcnt;
871};
872
873void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
874{
875 return block_cb->cb_priv;
876}
877EXPORT_SYMBOL(tcf_block_cb_priv);
878
879struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
880 tc_setup_cb_t *cb, void *cb_ident)
881{ struct tcf_block_cb *block_cb;
882
883 list_for_each_entry(block_cb, &block->cb_list, list)
884 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
885 return block_cb;
886 return NULL;
887}
888EXPORT_SYMBOL(tcf_block_cb_lookup);
889
890void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
891{
892 block_cb->refcnt++;
893}
894EXPORT_SYMBOL(tcf_block_cb_incref);
895
896unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
897{
898 return --block_cb->refcnt;
899}
900EXPORT_SYMBOL(tcf_block_cb_decref);
901
John Hurley32636742018-06-25 14:30:10 -0700902static int
903tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
904 void *cb_priv, bool add, bool offload_in_use,
905 struct netlink_ext_ack *extack)
906{
907 struct tcf_chain *chain;
908 struct tcf_proto *tp;
909 int err;
910
911 list_for_each_entry(chain, &block->chain_list, list) {
912 for (tp = rtnl_dereference(chain->filter_chain); tp;
913 tp = rtnl_dereference(tp->next)) {
914 if (tp->ops->reoffload) {
915 err = tp->ops->reoffload(tp, add, cb, cb_priv,
916 extack);
917 if (err && add)
918 goto err_playback_remove;
919 } else if (add && offload_in_use) {
920 err = -EOPNOTSUPP;
921 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
922 goto err_playback_remove;
923 }
924 }
925 }
926
927 return 0;
928
929err_playback_remove:
930 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
931 extack);
932 return err;
933}
934
Jiri Pirkoacb67442017-10-19 15:50:31 +0200935struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
936 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700937 void *cb_priv,
938 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200939{
940 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700941 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200942
John Hurley32636742018-06-25 14:30:10 -0700943 /* Replay any already present rules */
944 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
945 tcf_block_offload_in_use(block),
946 extack);
947 if (err)
948 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100949
Jiri Pirkoacb67442017-10-19 15:50:31 +0200950 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
951 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100952 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200953 block_cb->cb = cb;
954 block_cb->cb_ident = cb_ident;
955 block_cb->cb_priv = cb_priv;
956 list_add(&block_cb->list, &block->cb_list);
957 return block_cb;
958}
959EXPORT_SYMBOL(__tcf_block_cb_register);
960
961int 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, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200964{
965 struct tcf_block_cb *block_cb;
966
John Hurley60513bd2018-06-25 14:30:04 -0700967 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
968 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500969 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200970}
971EXPORT_SYMBOL(tcf_block_cb_register);
972
John Hurley32636742018-06-25 14:30:10 -0700973void __tcf_block_cb_unregister(struct tcf_block *block,
974 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200975{
John Hurley32636742018-06-25 14:30:10 -0700976 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
977 false, tcf_block_offload_in_use(block),
978 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200979 list_del(&block_cb->list);
980 kfree(block_cb);
981}
982EXPORT_SYMBOL(__tcf_block_cb_unregister);
983
984void tcf_block_cb_unregister(struct tcf_block *block,
985 tc_setup_cb_t *cb, void *cb_ident)
986{
987 struct tcf_block_cb *block_cb;
988
989 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
990 if (!block_cb)
991 return;
John Hurley32636742018-06-25 14:30:10 -0700992 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200993}
994EXPORT_SYMBOL(tcf_block_cb_unregister);
995
996static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
997 void *type_data, bool err_stop)
998{
999 struct tcf_block_cb *block_cb;
1000 int ok_count = 0;
1001 int err;
1002
David S. Miller9a99dc12018-06-06 13:55:47 -04001003 /* Make sure all netdevs sharing this block are offload-capable. */
1004 if (block->nooffloaddevcnt && err_stop)
1005 return -EOPNOTSUPP;
1006
Jiri Pirkoacb67442017-10-19 15:50:31 +02001007 list_for_each_entry(block_cb, &block->cb_list, list) {
1008 err = block_cb->cb(type, type_data, block_cb->cb_priv);
1009 if (err) {
1010 if (err_stop)
1011 return err;
1012 } else {
1013 ok_count++;
1014 }
1015 }
1016 return ok_count;
1017}
1018
Jiri Pirko87d83092017-05-17 11:07:54 +02001019/* Main classifier routine: scans classifier chain attached
1020 * to this qdisc, (optionally) tests for protocol and asks
1021 * specific classifiers.
1022 */
1023int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1024 struct tcf_result *res, bool compat_mode)
1025{
1026 __be16 protocol = tc_skb_protocol(skb);
1027#ifdef CONFIG_NET_CLS_ACT
1028 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001029 const struct tcf_proto *orig_tp = tp;
1030 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001031 int limit = 0;
1032
1033reclassify:
1034#endif
1035 for (; tp; tp = rcu_dereference_bh(tp->next)) {
1036 int err;
1037
1038 if (tp->protocol != protocol &&
1039 tp->protocol != htons(ETH_P_ALL))
1040 continue;
1041
1042 err = tp->classify(skb, tp, res);
1043#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +02001044 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001045 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001046 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +02001047 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001048 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +02001049 goto reset;
1050 }
Jiri Pirko87d83092017-05-17 11:07:54 +02001051#endif
1052 if (err >= 0)
1053 return err;
1054 }
1055
1056 return TC_ACT_UNSPEC; /* signal: continue lookup */
1057#ifdef CONFIG_NET_CLS_ACT
1058reset:
1059 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +01001060 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1061 tp->chain->block->index,
1062 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +02001063 ntohs(tp->protocol));
1064 return TC_ACT_SHOT;
1065 }
1066
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001067 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001068 protocol = tc_skb_protocol(skb);
1069 goto reclassify;
1070#endif
1071}
1072EXPORT_SYMBOL(tcf_classify);
1073
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001074struct tcf_chain_info {
1075 struct tcf_proto __rcu **pprev;
1076 struct tcf_proto __rcu *next;
1077};
1078
1079static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1080{
1081 return rtnl_dereference(*chain_info->pprev);
1082}
1083
1084static void tcf_chain_tp_insert(struct tcf_chain *chain,
1085 struct tcf_chain_info *chain_info,
1086 struct tcf_proto *tp)
1087{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001088 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001089 tcf_chain0_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001090 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1091 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -07001092 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001093}
1094
1095static void tcf_chain_tp_remove(struct tcf_chain *chain,
1096 struct tcf_chain_info *chain_info,
1097 struct tcf_proto *tp)
1098{
1099 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1100
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001101 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001102 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001103 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -07001104 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001105}
1106
1107static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1108 struct tcf_chain_info *chain_info,
1109 u32 protocol, u32 prio,
1110 bool prio_allocate)
1111{
1112 struct tcf_proto **pprev;
1113 struct tcf_proto *tp;
1114
1115 /* Check the chain for existence of proto-tcf with this priority */
1116 for (pprev = &chain->filter_chain;
1117 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1118 if (tp->prio >= prio) {
1119 if (tp->prio == prio) {
1120 if (prio_allocate ||
1121 (tp->protocol != protocol && protocol))
1122 return ERR_PTR(-EINVAL);
1123 } else {
1124 tp = NULL;
1125 }
1126 break;
1127 }
1128 }
1129 chain_info->pprev = pprev;
1130 chain_info->next = tp ? tp->next : NULL;
1131 return tp;
1132}
1133
WANG Cong71203712017-08-07 15:26:50 -07001134static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001135 struct tcf_proto *tp, struct tcf_block *block,
1136 struct Qdisc *q, u32 parent, void *fh,
1137 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -07001138{
1139 struct tcmsg *tcm;
1140 struct nlmsghdr *nlh;
1141 unsigned char *b = skb_tail_pointer(skb);
1142
1143 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1144 if (!nlh)
1145 goto out_nlmsg_trim;
1146 tcm = nlmsg_data(nlh);
1147 tcm->tcm_family = AF_UNSPEC;
1148 tcm->tcm__pad1 = 0;
1149 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001150 if (q) {
1151 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1152 tcm->tcm_parent = parent;
1153 } else {
1154 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1155 tcm->tcm_block_index = block->index;
1156 }
WANG Cong71203712017-08-07 15:26:50 -07001157 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1158 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1159 goto nla_put_failure;
1160 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1161 goto nla_put_failure;
1162 if (!fh) {
1163 tcm->tcm_handle = 0;
1164 } else {
1165 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1166 goto nla_put_failure;
1167 }
1168 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1169 return skb->len;
1170
1171out_nlmsg_trim:
1172nla_put_failure:
1173 nlmsg_trim(skb, b);
1174 return -1;
1175}
1176
1177static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1178 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001179 struct tcf_block *block, struct Qdisc *q,
1180 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001181{
1182 struct sk_buff *skb;
1183 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1184
1185 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1186 if (!skb)
1187 return -ENOBUFS;
1188
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001189 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1190 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001191 kfree_skb(skb);
1192 return -EINVAL;
1193 }
1194
1195 if (unicast)
1196 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1197
1198 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1199 n->nlmsg_flags & NLM_F_ECHO);
1200}
1201
1202static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1203 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001204 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001205 u32 parent, void *fh, bool unicast, bool *last,
1206 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001207{
1208 struct sk_buff *skb;
1209 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1210 int err;
1211
1212 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1213 if (!skb)
1214 return -ENOBUFS;
1215
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001216 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1217 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001218 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001219 kfree_skb(skb);
1220 return -EINVAL;
1221 }
1222
Alexander Aring571acf22018-01-18 11:20:53 -05001223 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001224 if (err) {
1225 kfree_skb(skb);
1226 return err;
1227 }
1228
1229 if (unicast)
1230 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1231
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001232 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1233 n->nlmsg_flags & NLM_F_ECHO);
1234 if (err < 0)
1235 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1236 return err;
WANG Cong71203712017-08-07 15:26:50 -07001237}
1238
1239static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001240 struct tcf_block *block, struct Qdisc *q,
1241 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001242 struct tcf_chain *chain, int event)
1243{
1244 struct tcf_proto *tp;
1245
1246 for (tp = rtnl_dereference(chain->filter_chain);
1247 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001248 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001249 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001250}
1251
Vlad Buslovc431f892018-05-31 09:52:53 +03001252static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001253 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001255 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001256 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct tcmsg *t;
1258 u32 protocol;
1259 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001260 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001262 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001263 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001264 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001265 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001266 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001269 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001271 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Vlad Buslovc431f892018-05-31 09:52:53 +03001273 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001274 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001277 tp_created = 0;
1278
David Ahernc21ef3e2017-04-16 09:48:24 -07001279 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001280 if (err < 0)
1281 return err;
1282
David S. Miller942b8162012-06-26 21:48:50 -07001283 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 protocol = TC_H_MIN(t->tcm_info);
1285 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001286 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 parent = t->tcm_parent;
1288 cl = 0;
1289
1290 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001291 /* If no priority is provided by the user,
1292 * we allocate one.
1293 */
1294 if (n->nlmsg_flags & NLM_F_CREATE) {
1295 prio = TC_H_MAKE(0x80000000U, 0U);
1296 prio_allocate = true;
1297 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001298 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
1303 /* Find head of filter chain. */
1304
Vlad Buslovc431f892018-05-31 09:52:53 +03001305 block = tcf_block_find(net, &q, &parent, &cl,
1306 t->tcm_ifindex, t->tcm_block_index, extack);
1307 if (IS_ERR(block)) {
1308 err = PTR_ERR(block);
1309 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001310 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001311
1312 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1313 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001314 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001315 err = -EINVAL;
1316 goto errout;
1317 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001318 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001319 if (!chain) {
Jiri Pirkod5ed72a2018-08-27 20:58:43 +02001320 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001321 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001322 goto errout;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001325 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1326 prio, prio_allocate);
1327 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001328 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001329 err = PTR_ERR(tp);
1330 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
1333 if (tp == NULL) {
1334 /* Proto-tcf does not exist, create new one */
1335
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001336 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001337 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001338 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Vlad Buslovc431f892018-05-31 09:52:53 +03001342 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001343 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 +01001344 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001348 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001349 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Jiri Pirko33a48922017-02-09 14:38:57 +01001351 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001352 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001353 if (IS_ERR(tp)) {
1354 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 goto errout;
1356 }
Minoru Usui12186be2009-06-02 02:17:34 -07001357 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001358 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001359 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001360 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 fh = tp->ops->get(tp, t->tcm_handle);
1365
WANG Cong8113c092017-08-04 21:31:43 -07001366 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001367 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001368 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 +01001369 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001371 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001372 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1373 NL_SET_ERR_MSG(extack, "Filter already exists");
1374 err = -EEXIST;
1375 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
Jiri Pirko9f407f12018-07-23 09:23:07 +02001378 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1379 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1380 err = -EINVAL;
1381 goto errout;
1382 }
1383
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001384 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001385 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1386 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001387 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001388 if (tp_created)
1389 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001390 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001391 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001392 } else {
1393 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001394 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001398 if (chain)
1399 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001400 tcf_block_release(q, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (err == -EAGAIN)
1402 /* Replay the request. */
1403 goto replay;
1404 return err;
1405}
1406
Vlad Buslovc431f892018-05-31 09:52:53 +03001407static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1408 struct netlink_ext_ack *extack)
1409{
1410 struct net *net = sock_net(skb->sk);
1411 struct nlattr *tca[TCA_MAX + 1];
1412 struct tcmsg *t;
1413 u32 protocol;
1414 u32 prio;
1415 u32 parent;
1416 u32 chain_index;
1417 struct Qdisc *q = NULL;
1418 struct tcf_chain_info chain_info;
1419 struct tcf_chain *chain = NULL;
1420 struct tcf_block *block;
1421 struct tcf_proto *tp = NULL;
1422 unsigned long cl = 0;
1423 void *fh = NULL;
1424 int err;
1425
1426 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1427 return -EPERM;
1428
1429 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1430 if (err < 0)
1431 return err;
1432
1433 t = nlmsg_data(n);
1434 protocol = TC_H_MIN(t->tcm_info);
1435 prio = TC_H_MAJ(t->tcm_info);
1436 parent = t->tcm_parent;
1437
1438 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1439 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1440 return -ENOENT;
1441 }
1442
1443 /* Find head of filter chain. */
1444
1445 block = tcf_block_find(net, &q, &parent, &cl,
1446 t->tcm_ifindex, t->tcm_block_index, extack);
1447 if (IS_ERR(block)) {
1448 err = PTR_ERR(block);
1449 goto errout;
1450 }
1451
1452 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1453 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1454 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1455 err = -EINVAL;
1456 goto errout;
1457 }
1458 chain = tcf_chain_get(block, chain_index, false);
1459 if (!chain) {
Jiri Pirko5ca8a252018-08-03 11:08:47 +02001460 /* User requested flush on non-existent chain. Nothing to do,
1461 * so just return success.
1462 */
1463 if (prio == 0) {
1464 err = 0;
1465 goto errout;
1466 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001467 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Jiri Pirkob7b42472018-08-27 20:58:44 +02001468 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001469 goto errout;
1470 }
1471
1472 if (prio == 0) {
1473 tfilter_notify_chain(net, skb, block, q, parent, n,
1474 chain, RTM_DELTFILTER);
1475 tcf_chain_flush(chain);
1476 err = 0;
1477 goto errout;
1478 }
1479
1480 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1481 prio, false);
1482 if (!tp || IS_ERR(tp)) {
1483 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001484 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001485 goto errout;
1486 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1487 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1488 err = -EINVAL;
1489 goto errout;
1490 }
1491
1492 fh = tp->ops->get(tp, t->tcm_handle);
1493
1494 if (!fh) {
1495 if (t->tcm_handle == 0) {
1496 tcf_chain_tp_remove(chain, &chain_info, tp);
1497 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1498 RTM_DELTFILTER, false);
1499 tcf_proto_destroy(tp, extack);
1500 err = 0;
1501 } else {
1502 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1503 err = -ENOENT;
1504 }
1505 } else {
1506 bool last;
1507
1508 err = tfilter_del_notify(net, skb, n, tp, block,
1509 q, parent, fh, false, &last,
1510 extack);
1511 if (err)
1512 goto errout;
1513 if (last) {
1514 tcf_chain_tp_remove(chain, &chain_info, tp);
1515 tcf_proto_destroy(tp, extack);
1516 }
1517 }
1518
1519errout:
1520 if (chain)
1521 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001522 tcf_block_release(q, block);
Vlad Buslovc431f892018-05-31 09:52:53 +03001523 return err;
1524}
1525
1526static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1527 struct netlink_ext_ack *extack)
1528{
1529 struct net *net = sock_net(skb->sk);
1530 struct nlattr *tca[TCA_MAX + 1];
1531 struct tcmsg *t;
1532 u32 protocol;
1533 u32 prio;
1534 u32 parent;
1535 u32 chain_index;
1536 struct Qdisc *q = NULL;
1537 struct tcf_chain_info chain_info;
1538 struct tcf_chain *chain = NULL;
1539 struct tcf_block *block;
1540 struct tcf_proto *tp = NULL;
1541 unsigned long cl = 0;
1542 void *fh = NULL;
1543 int err;
1544
1545 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1546 if (err < 0)
1547 return err;
1548
1549 t = nlmsg_data(n);
1550 protocol = TC_H_MIN(t->tcm_info);
1551 prio = TC_H_MAJ(t->tcm_info);
1552 parent = t->tcm_parent;
1553
1554 if (prio == 0) {
1555 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1556 return -ENOENT;
1557 }
1558
1559 /* Find head of filter chain. */
1560
1561 block = tcf_block_find(net, &q, &parent, &cl,
1562 t->tcm_ifindex, t->tcm_block_index, extack);
1563 if (IS_ERR(block)) {
1564 err = PTR_ERR(block);
1565 goto errout;
1566 }
1567
1568 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1569 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1570 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1571 err = -EINVAL;
1572 goto errout;
1573 }
1574 chain = tcf_chain_get(block, chain_index, false);
1575 if (!chain) {
1576 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1577 err = -EINVAL;
1578 goto errout;
1579 }
1580
1581 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1582 prio, false);
1583 if (!tp || IS_ERR(tp)) {
1584 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001585 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001586 goto errout;
1587 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1588 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1589 err = -EINVAL;
1590 goto errout;
1591 }
1592
1593 fh = tp->ops->get(tp, t->tcm_handle);
1594
1595 if (!fh) {
1596 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1597 err = -ENOENT;
1598 } else {
1599 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1600 fh, RTM_NEWTFILTER, true);
1601 if (err < 0)
1602 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1603 }
1604
1605errout:
1606 if (chain)
1607 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03001608 tcf_block_release(q, block);
Vlad Buslovc431f892018-05-31 09:52:53 +03001609 return err;
1610}
1611
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001612struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 struct tcf_walker w;
1614 struct sk_buff *skb;
1615 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001616 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001617 struct Qdisc *q;
1618 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619};
1620
WANG Cong8113c092017-08-04 21:31:43 -07001621static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001623 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001624 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001626 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001627 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001628 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1629 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001632static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1633 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001634 long index_start, long *p_index)
1635{
1636 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001637 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001638 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1639 struct tcf_dump_args arg;
1640 struct tcf_proto *tp;
1641
1642 for (tp = rtnl_dereference(chain->filter_chain);
1643 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1644 if (*p_index < index_start)
1645 continue;
1646 if (TC_H_MAJ(tcm->tcm_info) &&
1647 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1648 continue;
1649 if (TC_H_MIN(tcm->tcm_info) &&
1650 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1651 continue;
1652 if (*p_index > index_start)
1653 memset(&cb->args[1], 0,
1654 sizeof(cb->args) - sizeof(cb->args[0]));
1655 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001656 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001657 NETLINK_CB(cb->skb).portid,
1658 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1659 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001660 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001661
1662 cb->args[1] = 1;
1663 }
1664 if (!tp->ops->walk)
1665 continue;
1666 arg.w.fn = tcf_node_dump;
1667 arg.skb = skb;
1668 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001669 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001670 arg.q = q;
1671 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001672 arg.w.stop = 0;
1673 arg.w.skip = cb->args[1] - 1;
1674 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001675 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001676 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001677 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001678 cb->args[1] = arg.w.count + 1;
1679 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001680 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001681 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001682 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001683}
1684
Eric Dumazetbd27a872009-11-05 20:57:26 -08001685/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1687{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001688 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001689 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001690 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001691 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001692 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001693 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001694 long index_start;
1695 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001696 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001697 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Hong zhi guo573ce262013-03-27 06:47:04 +00001699 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001701
1702 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1703 if (err)
1704 return err;
1705
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001706 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1707 block = tcf_block_lookup(net, tcm->tcm_block_index);
1708 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001709 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001710 /* If we work with block index, q is NULL and parent value
1711 * will never be used in the following code. The check
1712 * in tcf_fill_node prevents it. However, compiler does not
1713 * see that far, so set parent to zero to silence the warning
1714 * about parent being uninitialized.
1715 */
1716 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001717 } else {
1718 const struct Qdisc_class_ops *cops;
1719 struct net_device *dev;
1720 unsigned long cl = 0;
1721
1722 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1723 if (!dev)
1724 return skb->len;
1725
1726 parent = tcm->tcm_parent;
1727 if (!parent) {
1728 q = dev->qdisc;
1729 parent = q->handle;
1730 } else {
1731 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1732 }
1733 if (!q)
1734 goto out;
1735 cops = q->ops->cl_ops;
1736 if (!cops)
1737 goto out;
1738 if (!cops->tcf_block)
1739 goto out;
1740 if (TC_H_MIN(tcm->tcm_parent)) {
1741 cl = cops->find(q, tcm->tcm_parent);
1742 if (cl == 0)
1743 goto out;
1744 }
1745 block = cops->tcf_block(q, cl, NULL);
1746 if (!block)
1747 goto out;
1748 if (tcf_block_shared(block))
1749 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001752 index_start = cb->args[0];
1753 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001754
1755 list_for_each_entry(chain, &block->chain_list, list) {
1756 if (tca[TCA_CHAIN] &&
1757 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1758 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001759 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001760 index_start, &index)) {
1761 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001762 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001763 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001764 }
1765
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001766 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001769 /* If we did no progress, the error (EMSGSIZE) is real */
1770 if (skb->len == 0 && err)
1771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return skb->len;
1773}
1774
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001775static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1776 struct sk_buff *skb, struct tcf_block *block,
1777 u32 portid, u32 seq, u16 flags, int event)
1778{
1779 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02001780 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001781 struct nlmsghdr *nlh;
1782 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02001783 void *priv;
1784
1785 ops = chain->tmplt_ops;
1786 priv = chain->tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001787
1788 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1789 if (!nlh)
1790 goto out_nlmsg_trim;
1791 tcm = nlmsg_data(nlh);
1792 tcm->tcm_family = AF_UNSPEC;
1793 tcm->tcm__pad1 = 0;
1794 tcm->tcm__pad2 = 0;
1795 tcm->tcm_handle = 0;
1796 if (block->q) {
1797 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1798 tcm->tcm_parent = block->q->handle;
1799 } else {
1800 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1801 tcm->tcm_block_index = block->index;
1802 }
1803
1804 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1805 goto nla_put_failure;
1806
Jiri Pirko9f407f12018-07-23 09:23:07 +02001807 if (ops) {
1808 if (nla_put_string(skb, TCA_KIND, ops->kind))
1809 goto nla_put_failure;
1810 if (ops->tmplt_dump(skb, net, priv) < 0)
1811 goto nla_put_failure;
1812 }
1813
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001814 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1815 return skb->len;
1816
1817out_nlmsg_trim:
1818nla_put_failure:
1819 nlmsg_trim(skb, b);
1820 return -EMSGSIZE;
1821}
1822
1823static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1824 u32 seq, u16 flags, int event, bool unicast)
1825{
1826 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1827 struct tcf_block *block = chain->block;
1828 struct net *net = block->net;
1829 struct sk_buff *skb;
1830
1831 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1832 if (!skb)
1833 return -ENOBUFS;
1834
1835 if (tc_chain_fill_node(chain, net, skb, block, portid,
1836 seq, flags, event) <= 0) {
1837 kfree_skb(skb);
1838 return -EINVAL;
1839 }
1840
1841 if (unicast)
1842 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1843
1844 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1845}
1846
Jiri Pirko9f407f12018-07-23 09:23:07 +02001847static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1848 struct nlattr **tca,
1849 struct netlink_ext_ack *extack)
1850{
1851 const struct tcf_proto_ops *ops;
1852 void *tmplt_priv;
1853
1854 /* If kind is not set, user did not specify template. */
1855 if (!tca[TCA_KIND])
1856 return 0;
1857
1858 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1859 if (IS_ERR(ops))
1860 return PTR_ERR(ops);
1861 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1862 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1863 return -EOPNOTSUPP;
1864 }
1865
1866 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1867 if (IS_ERR(tmplt_priv)) {
1868 module_put(ops->owner);
1869 return PTR_ERR(tmplt_priv);
1870 }
1871 chain->tmplt_ops = ops;
1872 chain->tmplt_priv = tmplt_priv;
1873 return 0;
1874}
1875
1876static void tc_chain_tmplt_del(struct tcf_chain *chain)
1877{
1878 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1879
1880 /* If template ops are set, no work to do for us. */
1881 if (!ops)
1882 return;
1883
1884 ops->tmplt_destroy(chain->tmplt_priv);
1885 module_put(ops->owner);
1886}
1887
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001888/* Add/delete/get a chain */
1889
1890static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1891 struct netlink_ext_ack *extack)
1892{
1893 struct net *net = sock_net(skb->sk);
1894 struct nlattr *tca[TCA_MAX + 1];
1895 struct tcmsg *t;
1896 u32 parent;
1897 u32 chain_index;
1898 struct Qdisc *q = NULL;
1899 struct tcf_chain *chain = NULL;
1900 struct tcf_block *block;
1901 unsigned long cl;
1902 int err;
1903
1904 if (n->nlmsg_type != RTM_GETCHAIN &&
1905 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1906 return -EPERM;
1907
1908replay:
1909 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1910 if (err < 0)
1911 return err;
1912
1913 t = nlmsg_data(n);
1914 parent = t->tcm_parent;
1915 cl = 0;
1916
1917 block = tcf_block_find(net, &q, &parent, &cl,
1918 t->tcm_ifindex, t->tcm_block_index, extack);
1919 if (IS_ERR(block))
1920 return PTR_ERR(block);
1921
1922 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1923 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1924 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001925 err = -EINVAL;
1926 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001927 }
1928 chain = tcf_chain_lookup(block, chain_index);
1929 if (n->nlmsg_type == RTM_NEWCHAIN) {
1930 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001931 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001932 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001933 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001934 */
1935 tcf_chain_hold(chain);
1936 } else {
1937 NL_SET_ERR_MSG(extack, "Filter chain already exists");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001938 err = -EEXIST;
1939 goto errout_block;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001940 }
1941 } else {
1942 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1943 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 +03001944 err = -ENOENT;
1945 goto errout_block;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001946 }
1947 chain = tcf_chain_create(block, chain_index);
1948 if (!chain) {
1949 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001950 err = -ENOMEM;
1951 goto errout_block;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001952 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001953 }
1954 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001955 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001956 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03001957 err = -EINVAL;
1958 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001959 }
1960 tcf_chain_hold(chain);
1961 }
1962
1963 switch (n->nlmsg_type) {
1964 case RTM_NEWCHAIN:
Jiri Pirko9f407f12018-07-23 09:23:07 +02001965 err = tc_chain_tmplt_add(chain, net, tca, extack);
1966 if (err)
1967 goto errout;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001968 /* In case the chain was successfully added, take a reference
1969 * to the chain. This ensures that an empty chain
1970 * does not disappear at the end of this function.
1971 */
1972 tcf_chain_hold(chain);
1973 chain->explicitly_created = true;
1974 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1975 RTM_NEWCHAIN, false);
1976 break;
1977 case RTM_DELCHAIN:
Cong Wangf5b9bac2018-09-11 14:22:23 -07001978 tfilter_notify_chain(net, skb, block, q, parent, n,
1979 chain, RTM_DELTFILTER);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001980 /* Flush the chain first as the user requested chain removal. */
1981 tcf_chain_flush(chain);
1982 /* In case the chain was successfully deleted, put a reference
1983 * to the chain previously taken during addition.
1984 */
1985 tcf_chain_put_explicitly_created(chain);
Jiri Pirkoc921d7d2018-07-26 18:27:58 +02001986 chain->explicitly_created = false;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001987 break;
1988 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001989 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1990 n->nlmsg_seq, n->nlmsg_type, true);
1991 if (err < 0)
1992 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1993 break;
1994 default:
1995 err = -EOPNOTSUPP;
1996 NL_SET_ERR_MSG(extack, "Unsupported message type");
1997 goto errout;
1998 }
1999
2000errout:
2001 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03002002errout_block:
2003 tcf_block_release(q, block);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002004 if (err == -EAGAIN)
2005 /* Replay the request. */
2006 goto replay;
2007 return err;
2008}
2009
2010/* called with RTNL */
2011static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2012{
2013 struct net *net = sock_net(skb->sk);
2014 struct nlattr *tca[TCA_MAX + 1];
2015 struct Qdisc *q = NULL;
2016 struct tcf_block *block;
2017 struct tcf_chain *chain;
2018 struct tcmsg *tcm = nlmsg_data(cb->nlh);
2019 long index_start;
2020 long index;
2021 u32 parent;
2022 int err;
2023
2024 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2025 return skb->len;
2026
2027 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
2028 if (err)
2029 return err;
2030
2031 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
2032 block = tcf_block_lookup(net, tcm->tcm_block_index);
2033 if (!block)
2034 goto out;
2035 /* If we work with block index, q is NULL and parent value
2036 * will never be used in the following code. The check
2037 * in tcf_fill_node prevents it. However, compiler does not
2038 * see that far, so set parent to zero to silence the warning
2039 * about parent being uninitialized.
2040 */
2041 parent = 0;
2042 } else {
2043 const struct Qdisc_class_ops *cops;
2044 struct net_device *dev;
2045 unsigned long cl = 0;
2046
2047 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2048 if (!dev)
2049 return skb->len;
2050
2051 parent = tcm->tcm_parent;
2052 if (!parent) {
2053 q = dev->qdisc;
2054 parent = q->handle;
2055 } else {
2056 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2057 }
2058 if (!q)
2059 goto out;
2060 cops = q->ops->cl_ops;
2061 if (!cops)
2062 goto out;
2063 if (!cops->tcf_block)
2064 goto out;
2065 if (TC_H_MIN(tcm->tcm_parent)) {
2066 cl = cops->find(q, tcm->tcm_parent);
2067 if (cl == 0)
2068 goto out;
2069 }
2070 block = cops->tcf_block(q, cl, NULL);
2071 if (!block)
2072 goto out;
2073 if (tcf_block_shared(block))
2074 q = NULL;
2075 }
2076
2077 index_start = cb->args[0];
2078 index = 0;
2079
2080 list_for_each_entry(chain, &block->chain_list, list) {
2081 if ((tca[TCA_CHAIN] &&
2082 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2083 continue;
2084 if (index < index_start) {
2085 index++;
2086 continue;
2087 }
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002088 if (tcf_chain_held_by_acts_only(chain))
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002089 continue;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002090 err = tc_chain_fill_node(chain, net, skb, block,
2091 NETLINK_CB(cb->skb).portid,
2092 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2093 RTM_NEWCHAIN);
2094 if (err <= 0)
2095 break;
2096 index++;
2097 }
2098
2099 cb->args[0] = index;
2100
2101out:
2102 /* If we did no progress, the error (EMSGSIZE) is real */
2103 if (skb->len == 0 && err)
2104 return err;
2105 return skb->len;
2106}
2107
WANG Cong18d02642014-09-25 10:26:37 -07002108void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110#ifdef CONFIG_NET_CLS_ACT
Vlad Buslov90b73b72018-07-05 17:24:33 +03002111 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
WANG Cong22dc13c2016-08-13 22:35:00 -07002112 kfree(exts->actions);
2113 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114#endif
2115}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002116EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00002118int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05002119 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2120 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122#ifdef CONFIG_NET_CLS_ACT
2123 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05002125 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
WANG Cong5da57f42013-12-15 20:15:07 -08002127 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002128 act = tcf_action_init_1(net, tp, tb[exts->police],
2129 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03002130 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08002131 if (IS_ERR(act))
2132 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
WANG Cong33be6272013-12-15 20:15:05 -08002134 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07002135 exts->actions[0] = act;
2136 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08002137 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03002138 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07002139
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002140 err = tcf_action_init(net, tp, tb[exts->action],
2141 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03002142 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03002143 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03002144 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002145 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03002146 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
Cong Wange4b95c42017-11-06 13:47:19 -08002148 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150#else
WANG Cong5da57f42013-12-15 20:15:07 -08002151 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05002152 (exts->police && tb[exts->police])) {
2153 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05002155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#endif
2157
2158 return 0;
2159}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002160EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002162void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07002165 struct tcf_exts old = *dst;
2166
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002167 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07002168 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169#endif
2170}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002171EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
WANG Cong22dc13c2016-08-13 22:35:00 -07002173#ifdef CONFIG_NET_CLS_ACT
2174static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2175{
2176 if (exts->nr_actions == 0)
2177 return NULL;
2178 else
2179 return exts->actions[0];
2180}
2181#endif
WANG Cong33be6272013-12-15 20:15:05 -08002182
WANG Cong5da57f42013-12-15 20:15:07 -08002183int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07002186 struct nlattr *nest;
2187
Jiri Pirko978dfd82017-08-04 14:29:03 +02002188 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /*
2190 * again for backward compatible mode - we want
2191 * to work with both old and new modes of entering
2192 * tc data even if iproute2 was newer - jhs
2193 */
WANG Cong33be6272013-12-15 20:15:05 -08002194 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08002195 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002196 if (nest == NULL)
2197 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07002198
Vlad Buslov90b73b72018-07-05 17:24:33 +03002199 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002200 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002201 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08002202 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08002203 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08002204 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05002205 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002206 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08002207 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002208 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002209 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07002213
2214nla_put_failure:
2215 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07002217#else
2218 return 0;
2219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002221EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002223
WANG Cong5da57f42013-12-15 20:15:07 -08002224int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08002227 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01002228 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002229 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230#endif
2231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002233EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Jiri Pirko717503b2017-10-11 09:41:09 +02002235static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2236 enum tc_setup_type type,
2237 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002238{
2239 int ok_count = 0;
2240#ifdef CONFIG_NET_CLS_ACT
2241 const struct tc_action *a;
2242 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002243 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002244
2245 if (!tcf_exts_has_actions(exts))
2246 return 0;
2247
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002248 for (i = 0; i < exts->nr_actions; i++) {
2249 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002250 if (!a->ops->get_dev)
2251 continue;
2252 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01002253 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002254 continue;
2255 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
Vlad Buslov84a75b32018-08-10 20:51:52 +03002256 a->ops->put_dev(dev);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002257 if (ret < 0)
2258 return ret;
2259 ok_count += ret;
2260 }
2261#endif
2262 return ok_count;
2263}
Jiri Pirko717503b2017-10-11 09:41:09 +02002264
Jiri Pirko208c0f42017-10-19 15:50:32 +02002265int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2266 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02002267{
David S. Miller9a99dc12018-06-06 13:55:47 -04002268 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002269 int ret;
2270
David S. Miller9a99dc12018-06-06 13:55:47 -04002271 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2272 if (ret < 0)
2273 return ret;
2274 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002275
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03002276 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04002277 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002278 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2279 if (ret < 0)
2280 return ret;
2281 ok_count += ret;
2282
2283 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02002284}
2285EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002286
Jiri Pirko48617382018-01-17 11:46:46 +01002287static __net_init int tcf_net_init(struct net *net)
2288{
2289 struct tcf_net *tn = net_generic(net, tcf_net_id);
2290
Vlad Buslovab281622018-09-24 19:22:56 +03002291 spin_lock_init(&tn->idr_lock);
Jiri Pirko48617382018-01-17 11:46:46 +01002292 idr_init(&tn->idr);
2293 return 0;
2294}
2295
2296static void __net_exit tcf_net_exit(struct net *net)
2297{
2298 struct tcf_net *tn = net_generic(net, tcf_net_id);
2299
2300 idr_destroy(&tn->idr);
2301}
2302
2303static struct pernet_operations tcf_net_ops = {
2304 .init = tcf_net_init,
2305 .exit = tcf_net_exit,
2306 .id = &tcf_net_id,
2307 .size = sizeof(struct tcf_net),
2308};
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310static int __init tc_filter_init(void)
2311{
Jiri Pirko48617382018-01-17 11:46:46 +01002312 int err;
2313
Cong Wang7aa00452017-10-26 18:24:28 -07002314 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2315 if (!tc_filter_wq)
2316 return -ENOMEM;
2317
Jiri Pirko48617382018-01-17 11:46:46 +01002318 err = register_pernet_subsys(&tcf_net_ops);
2319 if (err)
2320 goto err_register_pernet_subsys;
2321
Vlad Buslovc431f892018-05-31 09:52:53 +03002322 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2323 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2324 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02002325 tc_dump_tfilter, 0);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002326 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2327 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2328 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2329 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01002332
2333err_register_pernet_subsys:
2334 destroy_workqueue(tc_filter_wq);
2335 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336}
2337
2338subsys_initcall(tc_filter_init);