blob: a00a203b2ef52d5b66e75dff53151e150bc55725 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sched/cls_api.c Packet classifier API.
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6 *
7 * Changes:
8 *
9 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
Jiri Pirko33a48922017-02-09 14:38:57 +010017#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
20#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Jiri Pirko48617382018-01-17 11:46:46 +010022#include <linux/idr.h>
John Hurley7f76fa32018-11-09 21:21:26 -080023#include <linux/rhashtable.h>
John Hurley59eb87c2019-11-02 14:17:47 +000024#include <linux/jhash.h>
Paul Blakey43719292020-02-16 12:01:23 +020025#include <linux/rculist.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110026#include <net/net_namespace.h>
27#include <net/sock.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070028#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/pkt_sched.h>
30#include <net/pkt_cls.h>
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +010031#include <net/tc_act/tc_pedit.h>
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +010032#include <net/tc_act/tc_mirred.h>
33#include <net/tc_act/tc_vlan.h>
34#include <net/tc_act/tc_tunnel_key.h>
35#include <net/tc_act/tc_csum.h>
36#include <net/tc_act/tc_gact.h>
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -070037#include <net/tc_act/tc_police.h>
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -070038#include <net/tc_act/tc_sample.h>
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +010039#include <net/tc_act/tc_skbedit.h>
Paul Blakeyb57dc7c2019-07-09 10:30:48 +030040#include <net/tc_act/tc_ct.h>
John Hurley6749d5902019-07-23 15:33:59 +010041#include <net/tc_act/tc_mpls.h>
Po Liud29bdd62020-05-01 08:53:16 +080042#include <net/tc_act/tc_gate.h>
wenxu4e481902019-08-07 09:13:52 +080043#include <net/flow_offload.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Davide Carattie3314732018-10-10 22:00:58 +020045extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* The list of all installed classifier types */
WANG Cong36272872013-12-15 20:15:11 -080048static LIST_HEAD(tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Protects list of registered TC modules. It is pure SMP lock. */
51static DEFINE_RWLOCK(cls_mod_lock);
52
John Hurley59eb87c2019-11-02 14:17:47 +000053static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
54{
55 return jhash_3words(tp->chain->index, tp->prio,
56 (__force __u32)tp->protocol, 0);
57}
58
59static void tcf_proto_signal_destroying(struct tcf_chain *chain,
60 struct tcf_proto *tp)
61{
62 struct tcf_block *block = chain->block;
63
64 mutex_lock(&block->proto_destroy_lock);
65 hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
66 destroy_obj_hashfn(tp));
67 mutex_unlock(&block->proto_destroy_lock);
68}
69
70static bool tcf_proto_cmp(const struct tcf_proto *tp1,
71 const struct tcf_proto *tp2)
72{
73 return tp1->chain->index == tp2->chain->index &&
74 tp1->prio == tp2->prio &&
75 tp1->protocol == tp2->protocol;
76}
77
78static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
79 struct tcf_proto *tp)
80{
81 u32 hash = destroy_obj_hashfn(tp);
82 struct tcf_proto *iter;
83 bool found = false;
84
85 rcu_read_lock();
86 hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
87 destroy_ht_node, hash) {
88 if (tcf_proto_cmp(tp, iter)) {
89 found = true;
90 break;
91 }
92 }
93 rcu_read_unlock();
94
95 return found;
96}
97
98static void
99tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
100{
101 struct tcf_block *block = chain->block;
102
103 mutex_lock(&block->proto_destroy_lock);
104 if (hash_hashed(&tp->destroy_ht_node))
105 hash_del_rcu(&tp->destroy_ht_node);
106 mutex_unlock(&block->proto_destroy_lock);
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* Find classifier type by string name */
110
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200111static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Eric Dumazetdcd76082013-12-20 10:04:18 -0800113 const struct tcf_proto_ops *t, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (kind) {
116 read_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -0800117 list_for_each_entry(t, &tcf_proto_base, head) {
Jiri Pirko33a48922017-02-09 14:38:57 +0100118 if (strcmp(kind, t->kind) == 0) {
Eric Dumazetdcd76082013-12-20 10:04:18 -0800119 if (try_module_get(t->owner))
120 res = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122 }
123 }
124 read_unlock(&cls_mod_lock);
125 }
Eric Dumazetdcd76082013-12-20 10:04:18 -0800126 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200129static const struct tcf_proto_ops *
Vlad Buslov12db03b2019-02-11 10:55:45 +0200130tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
131 struct netlink_ext_ack *extack)
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200132{
133 const struct tcf_proto_ops *ops;
134
135 ops = __tcf_proto_lookup_ops(kind);
136 if (ops)
137 return ops;
138#ifdef CONFIG_MODULES
Vlad Buslov12db03b2019-02-11 10:55:45 +0200139 if (rtnl_held)
140 rtnl_unlock();
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200141 request_module("cls_%s", kind);
Vlad Buslov12db03b2019-02-11 10:55:45 +0200142 if (rtnl_held)
143 rtnl_lock();
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200144 ops = __tcf_proto_lookup_ops(kind);
145 /* We dropped the RTNL semaphore in order to perform
146 * the module load. So, even if we succeeded in loading
147 * the module we have to replay the request. We indicate
148 * this using -EAGAIN.
149 */
150 if (ops) {
151 module_put(ops->owner);
152 return ERR_PTR(-EAGAIN);
153 }
154#endif
155 NL_SET_ERR_MSG(extack, "TC classifier not found");
156 return ERR_PTR(-ENOENT);
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* Register(unregister) new classifier type */
160
161int register_tcf_proto_ops(struct tcf_proto_ops *ops)
162{
WANG Cong36272872013-12-15 20:15:11 -0800163 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int rc = -EEXIST;
165
166 write_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -0800167 list_for_each_entry(t, &tcf_proto_base, head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (!strcmp(ops->kind, t->kind))
169 goto out;
170
WANG Cong36272872013-12-15 20:15:11 -0800171 list_add_tail(&ops->head, &tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 rc = 0;
173out:
174 write_unlock(&cls_mod_lock);
175 return rc;
176}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800177EXPORT_SYMBOL(register_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Cong Wang7aa00452017-10-26 18:24:28 -0700179static struct workqueue_struct *tc_filter_wq;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
182{
WANG Cong36272872013-12-15 20:15:11 -0800183 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int rc = -ENOENT;
185
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200186 /* Wait for outstanding call_rcu()s, if any, from a
187 * tcf_proto_ops's destroy() handler.
188 */
189 rcu_barrier();
Cong Wang7aa00452017-10-26 18:24:28 -0700190 flush_workqueue(tc_filter_wq);
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 write_lock(&cls_mod_lock);
Eric Dumazetdcd76082013-12-20 10:04:18 -0800193 list_for_each_entry(t, &tcf_proto_base, head) {
194 if (t == ops) {
195 list_del(&t->head);
196 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
Eric Dumazetdcd76082013-12-20 10:04:18 -0800198 }
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 write_unlock(&cls_mod_lock);
201 return rc;
202}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800203EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Cong Wangaaa908f2018-05-23 15:26:53 -0700205bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
Cong Wang7aa00452017-10-26 18:24:28 -0700206{
Cong Wangaaa908f2018-05-23 15:26:53 -0700207 INIT_RCU_WORK(rwork, func);
208 return queue_rcu_work(tc_filter_wq, rwork);
Cong Wang7aa00452017-10-26 18:24:28 -0700209}
210EXPORT_SYMBOL(tcf_queue_work);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/* Select new prio value from the range, managed by kernel. */
213
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800214static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800216 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000219 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jiri Pirko79619732017-05-17 11:07:58 +0200221 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Cong Wang6f96c3c2019-10-07 13:26:28 -0700224static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
225{
226 if (kind)
227 return nla_strlcpy(name, kind, IFNAMSIZ) >= IFNAMSIZ;
228 memset(name, 0, IFNAMSIZ);
229 return false;
230}
231
Vlad Buslov470502d2019-02-11 10:55:48 +0200232static bool tcf_proto_is_unlocked(const char *kind)
233{
234 const struct tcf_proto_ops *ops;
235 bool ret;
236
Cong Wang6f96c3c2019-10-07 13:26:28 -0700237 if (strlen(kind) == 0)
238 return false;
239
Vlad Buslov470502d2019-02-11 10:55:48 +0200240 ops = tcf_proto_lookup_ops(kind, false, NULL);
241 /* On error return false to take rtnl lock. Proto lookup/create
242 * functions will perform lookup again and properly handle errors.
243 */
244 if (IS_ERR(ops))
245 return false;
246
247 ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
248 module_put(ops->owner);
249 return ret;
250}
251
Jiri Pirko33a48922017-02-09 14:38:57 +0100252static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500253 u32 prio, struct tcf_chain *chain,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200254 bool rtnl_held,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500255 struct netlink_ext_ack *extack)
Jiri Pirko33a48922017-02-09 14:38:57 +0100256{
257 struct tcf_proto *tp;
258 int err;
259
260 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
261 if (!tp)
262 return ERR_PTR(-ENOBUFS);
263
Vlad Buslov12db03b2019-02-11 10:55:45 +0200264 tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200265 if (IS_ERR(tp->ops)) {
266 err = PTR_ERR(tp->ops);
Jiri Pirkod68d75f2018-05-11 17:45:32 +0200267 goto errout;
Jiri Pirko33a48922017-02-09 14:38:57 +0100268 }
269 tp->classify = tp->ops->classify;
270 tp->protocol = protocol;
271 tp->prio = prio;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200272 tp->chain = chain;
Vlad Buslov8b646782019-02-11 10:55:41 +0200273 spin_lock_init(&tp->lock);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200274 refcount_set(&tp->refcnt, 1);
Jiri Pirko33a48922017-02-09 14:38:57 +0100275
276 err = tp->ops->init(tp);
277 if (err) {
278 module_put(tp->ops->owner);
279 goto errout;
280 }
281 return tp;
282
283errout:
284 kfree(tp);
285 return ERR_PTR(err);
286}
287
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200288static void tcf_proto_get(struct tcf_proto *tp)
289{
290 refcount_inc(&tp->refcnt);
291}
292
293static void tcf_chain_put(struct tcf_chain *chain);
294
Vlad Buslov12db03b2019-02-11 10:55:45 +0200295static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
John Hurley59eb87c2019-11-02 14:17:47 +0000296 bool sig_destroy, struct netlink_ext_ack *extack)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100297{
Vlad Buslov12db03b2019-02-11 10:55:45 +0200298 tp->ops->destroy(tp, rtnl_held, extack);
John Hurley59eb87c2019-11-02 14:17:47 +0000299 if (sig_destroy)
300 tcf_proto_signal_destroyed(tp->chain, tp);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200301 tcf_chain_put(tp->chain);
WANG Cong763dbf62017-04-19 14:21:21 -0700302 module_put(tp->ops->owner);
303 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100304}
305
Vlad Buslov12db03b2019-02-11 10:55:45 +0200306static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200307 struct netlink_ext_ack *extack)
308{
309 if (refcount_dec_and_test(&tp->refcnt))
John Hurley59eb87c2019-11-02 14:17:47 +0000310 tcf_proto_destroy(tp, rtnl_held, true, extack);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200311}
312
Davide Carattia5b72a02019-12-28 16:36:58 +0100313static bool tcf_proto_check_delete(struct tcf_proto *tp)
Vlad Buslov8b646782019-02-11 10:55:41 +0200314{
Davide Carattia5b72a02019-12-28 16:36:58 +0100315 if (tp->ops->delete_empty)
316 return tp->ops->delete_empty(tp);
Vlad Buslov8b646782019-02-11 10:55:41 +0200317
Davide Carattia5b72a02019-12-28 16:36:58 +0100318 tp->deleting = true;
Vlad Buslov8b646782019-02-11 10:55:41 +0200319 return tp->deleting;
320}
321
322static void tcf_proto_mark_delete(struct tcf_proto *tp)
323{
324 spin_lock(&tp->lock);
325 tp->deleting = true;
326 spin_unlock(&tp->lock);
327}
328
329static bool tcf_proto_is_deleting(struct tcf_proto *tp)
330{
331 bool deleting;
332
333 spin_lock(&tp->lock);
334 deleting = tp->deleting;
335 spin_unlock(&tp->lock);
336
337 return deleting;
338}
339
Vlad Buslovc266f642019-02-11 10:55:32 +0200340#define ASSERT_BLOCK_LOCKED(block) \
341 lockdep_assert_held(&(block)->lock)
342
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100343struct tcf_filter_chain_list_item {
344 struct list_head list;
345 tcf_chain_head_change_t *chain_head_change;
346 void *chain_head_change_priv;
347};
348
Jiri Pirko5bc17012017-05-17 11:08:01 +0200349static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
350 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200351{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200352 struct tcf_chain *chain;
353
Vlad Buslovc266f642019-02-11 10:55:32 +0200354 ASSERT_BLOCK_LOCKED(block);
355
Jiri Pirko5bc17012017-05-17 11:08:01 +0200356 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
357 if (!chain)
358 return NULL;
Paul Blakey43719292020-02-16 12:01:23 +0200359 list_add_tail_rcu(&chain->list, &block->chain_list);
Vlad Busloved76f5e2019-02-11 10:55:38 +0200360 mutex_init(&chain->filter_chain_lock);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200361 chain->block = block;
362 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700363 chain->refcnt = 1;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200364 if (!chain->index)
365 block->chain0.chain = chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200366 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200367}
368
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100369static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
370 struct tcf_proto *tp_head)
371{
372 if (item->chain_head_change)
373 item->chain_head_change(tp_head, item->chain_head_change_priv);
374}
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200375
376static void tcf_chain0_head_change(struct tcf_chain *chain,
377 struct tcf_proto *tp_head)
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100378{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100379 struct tcf_filter_chain_list_item *item;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200380 struct tcf_block *block = chain->block;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100381
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200382 if (chain->index)
383 return;
Vlad Buslov165f0132019-02-11 10:55:35 +0200384
385 mutex_lock(&block->lock);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200386 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100387 tcf_chain_head_change_item(item, tp_head);
Vlad Buslov165f0132019-02-11 10:55:35 +0200388 mutex_unlock(&block->lock);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100389}
390
Vlad Buslovc266f642019-02-11 10:55:32 +0200391/* Returns true if block can be safely freed. */
392
393static bool tcf_chain_detach(struct tcf_chain *chain)
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200394{
Cong Wangefbf7892017-12-04 10:48:18 -0800395 struct tcf_block *block = chain->block;
396
Vlad Buslovc266f642019-02-11 10:55:32 +0200397 ASSERT_BLOCK_LOCKED(block);
398
Paul Blakey43719292020-02-16 12:01:23 +0200399 list_del_rcu(&chain->list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200400 if (!chain->index)
401 block->chain0.chain = NULL;
Vlad Buslovc266f642019-02-11 10:55:32 +0200402
403 if (list_empty(&block->chain_list) &&
404 refcount_read(&block->refcnt) == 0)
405 return true;
406
407 return false;
408}
409
410static void tcf_block_destroy(struct tcf_block *block)
411{
412 mutex_destroy(&block->lock);
John Hurley59eb87c2019-11-02 14:17:47 +0000413 mutex_destroy(&block->proto_destroy_lock);
Vlad Buslovc266f642019-02-11 10:55:32 +0200414 kfree_rcu(block, rcu);
415}
416
417static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
418{
419 struct tcf_block *block = chain->block;
420
Vlad Busloved76f5e2019-02-11 10:55:38 +0200421 mutex_destroy(&chain->filter_chain_lock);
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100422 kfree_rcu(chain, rcu);
Vlad Buslovc266f642019-02-11 10:55:32 +0200423 if (free_block)
424 tcf_block_destroy(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700425}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200426
Cong Wange2ef7542017-09-11 16:33:31 -0700427static void tcf_chain_hold(struct tcf_chain *chain)
428{
Vlad Buslovc266f642019-02-11 10:55:32 +0200429 ASSERT_BLOCK_LOCKED(chain->block);
430
Cong Wange2ef7542017-09-11 16:33:31 -0700431 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200432}
433
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200434static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200435{
Vlad Buslovc266f642019-02-11 10:55:32 +0200436 ASSERT_BLOCK_LOCKED(chain->block);
437
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200438 /* In case all the references are action references, this
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200439 * chain should not be shown to the user.
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200440 */
441 return chain->refcnt == chain->action_refcnt;
442}
443
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200444static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
445 u32 chain_index)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200446{
447 struct tcf_chain *chain;
448
Vlad Buslovc266f642019-02-11 10:55:32 +0200449 ASSERT_BLOCK_LOCKED(block);
450
Jiri Pirko5bc17012017-05-17 11:08:01 +0200451 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200452 if (chain->index == chain_index)
Cong Wange2ef7542017-09-11 16:33:31 -0700453 return chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200454 }
455 return NULL;
456}
457
Paul Blakeyaf699622020-02-16 12:01:24 +0200458#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
459static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
460 u32 chain_index)
461{
462 struct tcf_chain *chain;
463
464 list_for_each_entry_rcu(chain, &block->chain_list, list) {
465 if (chain->index == chain_index)
466 return chain;
467 }
468 return NULL;
469}
470#endif
471
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200472static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
473 u32 seq, u16 flags, int event, bool unicast);
474
Jiri Pirko53681402018-08-01 12:36:56 +0200475static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
476 u32 chain_index, bool create,
477 bool by_act)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200478{
Vlad Buslovc266f642019-02-11 10:55:32 +0200479 struct tcf_chain *chain = NULL;
480 bool is_first_reference;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200481
Vlad Buslovc266f642019-02-11 10:55:32 +0200482 mutex_lock(&block->lock);
483 chain = tcf_chain_lookup(block, chain_index);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200484 if (chain) {
485 tcf_chain_hold(chain);
Jiri Pirko53681402018-08-01 12:36:56 +0200486 } else {
487 if (!create)
Vlad Buslovc266f642019-02-11 10:55:32 +0200488 goto errout;
Jiri Pirko53681402018-08-01 12:36:56 +0200489 chain = tcf_chain_create(block, chain_index);
490 if (!chain)
Vlad Buslovc266f642019-02-11 10:55:32 +0200491 goto errout;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200492 }
Jiri Pirko80532382017-09-06 13:14:19 +0200493
Jiri Pirko53681402018-08-01 12:36:56 +0200494 if (by_act)
495 ++chain->action_refcnt;
Vlad Buslovc266f642019-02-11 10:55:32 +0200496 is_first_reference = chain->refcnt - chain->action_refcnt == 1;
497 mutex_unlock(&block->lock);
Jiri Pirko53681402018-08-01 12:36:56 +0200498
499 /* Send notification only in case we got the first
500 * non-action reference. Until then, the chain acts only as
501 * a placeholder for actions pointing to it and user ought
502 * not know about them.
503 */
Vlad Buslovc266f642019-02-11 10:55:32 +0200504 if (is_first_reference && !by_act)
Jiri Pirko53681402018-08-01 12:36:56 +0200505 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
506 RTM_NEWCHAIN, false);
507
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200508 return chain;
Vlad Buslovc266f642019-02-11 10:55:32 +0200509
510errout:
511 mutex_unlock(&block->lock);
512 return chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200513}
Jiri Pirko53681402018-08-01 12:36:56 +0200514
Jiri Pirko290b1c82018-08-01 12:36:57 +0200515static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
516 bool create)
Jiri Pirko53681402018-08-01 12:36:56 +0200517{
518 return __tcf_chain_get(block, chain_index, create, false);
519}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200520
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200521struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
522{
Jiri Pirko53681402018-08-01 12:36:56 +0200523 return __tcf_chain_get(block, chain_index, true, true);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200524}
525EXPORT_SYMBOL(tcf_chain_get_by_act);
526
Vlad Buslova5654822019-02-11 10:55:37 +0200527static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
528 void *tmplt_priv);
529static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
530 void *tmplt_priv, u32 chain_index,
531 struct tcf_block *block, struct sk_buff *oskb,
532 u32 seq, u16 flags, bool unicast);
Jiri Pirko9f407f12018-07-23 09:23:07 +0200533
Vlad Buslov91052fa2019-02-11 10:55:33 +0200534static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
535 bool explicitly_created)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200536{
Vlad Buslovc266f642019-02-11 10:55:32 +0200537 struct tcf_block *block = chain->block;
Vlad Buslova5654822019-02-11 10:55:37 +0200538 const struct tcf_proto_ops *tmplt_ops;
Vlad Buslovb62989f2019-03-06 17:50:43 +0200539 bool free_block = false;
Vlad Buslovc266f642019-02-11 10:55:32 +0200540 unsigned int refcnt;
Vlad Buslova5654822019-02-11 10:55:37 +0200541 void *tmplt_priv;
Vlad Buslovc266f642019-02-11 10:55:32 +0200542
543 mutex_lock(&block->lock);
Vlad Buslov91052fa2019-02-11 10:55:33 +0200544 if (explicitly_created) {
545 if (!chain->explicitly_created) {
546 mutex_unlock(&block->lock);
547 return;
548 }
549 chain->explicitly_created = false;
550 }
551
Jiri Pirko53681402018-08-01 12:36:56 +0200552 if (by_act)
553 chain->action_refcnt--;
Vlad Buslovc266f642019-02-11 10:55:32 +0200554
555 /* tc_chain_notify_delete can't be called while holding block lock.
556 * However, when block is unlocked chain can be changed concurrently, so
557 * save these to temporary variables.
558 */
559 refcnt = --chain->refcnt;
Vlad Buslova5654822019-02-11 10:55:37 +0200560 tmplt_ops = chain->tmplt_ops;
561 tmplt_priv = chain->tmplt_priv;
Jiri Pirko53681402018-08-01 12:36:56 +0200562
563 /* The last dropped non-action reference will trigger notification. */
Vlad Buslovb62989f2019-03-06 17:50:43 +0200564 if (refcnt - chain->action_refcnt == 0 && !by_act) {
565 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index,
Vlad Buslova5654822019-02-11 10:55:37 +0200566 block, NULL, 0, 0, false);
Vlad Buslov726d06122019-02-11 10:55:42 +0200567 /* Last reference to chain, no need to lock. */
568 chain->flushing = false;
569 }
Jiri Pirko53681402018-08-01 12:36:56 +0200570
Vlad Buslovb62989f2019-03-06 17:50:43 +0200571 if (refcnt == 0)
572 free_block = tcf_chain_detach(chain);
573 mutex_unlock(&block->lock);
574
Vlad Buslovc266f642019-02-11 10:55:32 +0200575 if (refcnt == 0) {
Vlad Buslova5654822019-02-11 10:55:37 +0200576 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
Vlad Buslovc266f642019-02-11 10:55:32 +0200577 tcf_chain_destroy(chain, free_block);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200578 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200579}
Jiri Pirko53681402018-08-01 12:36:56 +0200580
Jiri Pirko290b1c82018-08-01 12:36:57 +0200581static void tcf_chain_put(struct tcf_chain *chain)
Jiri Pirko53681402018-08-01 12:36:56 +0200582{
Vlad Buslov91052fa2019-02-11 10:55:33 +0200583 __tcf_chain_put(chain, false, false);
Jiri Pirko53681402018-08-01 12:36:56 +0200584}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200585
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200586void tcf_chain_put_by_act(struct tcf_chain *chain)
587{
Vlad Buslov91052fa2019-02-11 10:55:33 +0200588 __tcf_chain_put(chain, true, false);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200589}
590EXPORT_SYMBOL(tcf_chain_put_by_act);
591
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200592static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
593{
Vlad Buslov91052fa2019-02-11 10:55:33 +0200594 __tcf_chain_put(chain, false, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200595}
596
Vlad Buslov12db03b2019-02-11 10:55:45 +0200597static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
Jiri Pirko290b1c82018-08-01 12:36:57 +0200598{
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200599 struct tcf_proto *tp, *tp_next;
Jiri Pirko290b1c82018-08-01 12:36:57 +0200600
Vlad Busloved76f5e2019-02-11 10:55:38 +0200601 mutex_lock(&chain->filter_chain_lock);
602 tp = tcf_chain_dereference(chain->filter_chain, chain);
John Hurley59eb87c2019-11-02 14:17:47 +0000603 while (tp) {
604 tp_next = rcu_dereference_protected(tp->next, 1);
605 tcf_proto_signal_destroying(chain, tp);
606 tp = tp_next;
607 }
608 tp = tcf_chain_dereference(chain->filter_chain, chain);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200609 RCU_INIT_POINTER(chain->filter_chain, NULL);
Jiri Pirko290b1c82018-08-01 12:36:57 +0200610 tcf_chain0_head_change(chain, NULL);
Vlad Buslov726d06122019-02-11 10:55:42 +0200611 chain->flushing = true;
Vlad Busloved76f5e2019-02-11 10:55:38 +0200612 mutex_unlock(&chain->filter_chain_lock);
613
Jiri Pirko290b1c82018-08-01 12:36:57 +0200614 while (tp) {
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200615 tp_next = rcu_dereference_protected(tp->next, 1);
Vlad Buslov12db03b2019-02-11 10:55:45 +0200616 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200617 tp = tp_next;
Jiri Pirko290b1c82018-08-01 12:36:57 +0200618 }
619}
620
wenxu4e481902019-08-07 09:13:52 +0800621static int tcf_block_setup(struct tcf_block *block,
622 struct flow_block_offload *bo);
623
Pablo Neira Ayuso324a8232020-05-29 02:25:36 +0200624static void tcf_block_offload_init(struct flow_block_offload *bo,
625 struct net_device *dev,
626 enum flow_block_command command,
627 enum flow_block_binder_type binder_type,
628 struct flow_block *flow_block,
629 bool shared, struct netlink_ext_ack *extack)
630{
631 bo->net = dev_net(dev);
632 bo->command = command;
633 bo->binder_type = binder_type;
634 bo->block = flow_block;
635 bo->block_shared = shared;
636 bo->extack = extack;
637 INIT_LIST_HEAD(&bo->cb_list);
638}
639
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200640static void tcf_block_unbind(struct tcf_block *block,
641 struct flow_block_offload *bo);
John Hurley7f76fa32018-11-09 21:21:26 -0800642
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200643static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
644{
645 struct tcf_block *block = block_cb->indr.data;
646 struct net_device *dev = block_cb->indr.dev;
647 struct netlink_ext_ack extack = {};
648 struct flow_block_offload bo;
649
650 tcf_block_offload_init(&bo, dev, FLOW_BLOCK_UNBIND,
651 block_cb->indr.binder_type,
652 &block->flow_block, tcf_block_shared(block),
653 &extack);
654 down_write(&block->cb_lock);
655 list_move(&block_cb->list, &bo.cb_list);
656 up_write(&block->cb_lock);
657 rtnl_lock();
658 tcf_block_unbind(block, &bo);
659 rtnl_unlock();
John Hurley7f76fa32018-11-09 21:21:26 -0800660}
661
Jiri Pirkocaa72602018-01-17 11:46:50 +0100662static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200663{
Vlad Buslov97394be2019-08-26 16:44:58 +0300664 return atomic_read(&block->offloadcnt);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100665}
666
667static int tcf_block_offload_cmd(struct tcf_block *block,
668 struct net_device *dev,
669 struct tcf_block_ext_info *ei,
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +0200670 enum flow_block_command command,
John Hurley60513bd2018-06-25 14:30:04 -0700671 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100672{
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +0200673 struct flow_block_offload bo = {};
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +0200674 int err;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200675
Pablo Neira Ayuso324a8232020-05-29 02:25:36 +0200676 tcf_block_offload_init(&bo, dev, command, ei->binder_type,
677 &block->flow_block, tcf_block_shared(block),
678 extack);
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +0200679
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200680 if (dev->netdev_ops->ndo_setup_tc)
681 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
682 else
683 err = flow_indr_dev_setup_offload(dev, TC_SETUP_BLOCK, block,
684 &bo, tc_block_indr_cleanup);
685
Jesper Dangaard Brouerb70ba692020-04-23 16:57:45 +0200686 if (err < 0) {
687 if (err != -EOPNOTSUPP)
688 NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +0200689 return err;
Jesper Dangaard Brouerb70ba692020-04-23 16:57:45 +0200690 }
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +0200691
692 return tcf_block_setup(block, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200693}
694
Jiri Pirkocaa72602018-01-17 11:46:50 +0100695static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700696 struct tcf_block_ext_info *ei,
697 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200698{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100699 struct net_device *dev = q->dev_queue->dev;
700 int err;
701
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300702 down_write(&block->cb_lock);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100703
704 /* If tc offload feature is disabled and the block we try to bind
705 * to already has some offloaded filters, forbid to bind.
706 */
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200707 if (dev->netdev_ops->ndo_setup_tc &&
708 !tc_can_offload(dev) &&
709 tcf_block_offload_in_use(block)) {
John Hurley60513bd2018-06-25 14:30:04 -0700710 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300711 err = -EOPNOTSUPP;
712 goto err_unlock;
John Hurley60513bd2018-06-25 14:30:04 -0700713 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100714
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +0200715 err = tcf_block_offload_cmd(block, dev, ei, FLOW_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100716 if (err == -EOPNOTSUPP)
717 goto no_offload_dev_inc;
John Hurley7f76fa32018-11-09 21:21:26 -0800718 if (err)
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300719 goto err_unlock;
John Hurley7f76fa32018-11-09 21:21:26 -0800720
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300721 up_write(&block->cb_lock);
John Hurley7f76fa32018-11-09 21:21:26 -0800722 return 0;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100723
724no_offload_dev_inc:
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200725 if (tcf_block_offload_in_use(block))
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300726 goto err_unlock;
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200727
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300728 err = 0;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100729 block->nooffloaddevcnt++;
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300730err_unlock:
731 up_write(&block->cb_lock);
732 return err;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200733}
734
735static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
736 struct tcf_block_ext_info *ei)
737{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100738 struct net_device *dev = q->dev_queue->dev;
739 int err;
740
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300741 down_write(&block->cb_lock);
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +0200742 err = tcf_block_offload_cmd(block, dev, ei, FLOW_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100743 if (err == -EOPNOTSUPP)
744 goto no_offload_dev_dec;
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300745 up_write(&block->cb_lock);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100746 return;
747
748no_offload_dev_dec:
749 WARN_ON(block->nooffloaddevcnt-- == 0);
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300750 up_write(&block->cb_lock);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200751}
752
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100753static int
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200754tcf_chain0_head_change_cb_add(struct tcf_block *block,
755 struct tcf_block_ext_info *ei,
756 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100757{
758 struct tcf_filter_chain_list_item *item;
Vlad Buslov165f0132019-02-11 10:55:35 +0200759 struct tcf_chain *chain0;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100760
761 item = kmalloc(sizeof(*item), GFP_KERNEL);
762 if (!item) {
763 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
764 return -ENOMEM;
765 }
766 item->chain_head_change = ei->chain_head_change;
767 item->chain_head_change_priv = ei->chain_head_change_priv;
Vlad Buslov165f0132019-02-11 10:55:35 +0200768
769 mutex_lock(&block->lock);
770 chain0 = block->chain0.chain;
Vlad Busloved76f5e2019-02-11 10:55:38 +0200771 if (chain0)
772 tcf_chain_hold(chain0);
773 else
774 list_add(&item->list, &block->chain0.filter_chain_list);
Vlad Buslov165f0132019-02-11 10:55:35 +0200775 mutex_unlock(&block->lock);
776
Vlad Busloved76f5e2019-02-11 10:55:38 +0200777 if (chain0) {
778 struct tcf_proto *tp_head;
779
780 mutex_lock(&chain0->filter_chain_lock);
781
782 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
783 if (tp_head)
784 tcf_chain_head_change_item(item, tp_head);
785
786 mutex_lock(&block->lock);
787 list_add(&item->list, &block->chain0.filter_chain_list);
788 mutex_unlock(&block->lock);
789
790 mutex_unlock(&chain0->filter_chain_lock);
791 tcf_chain_put(chain0);
792 }
793
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100794 return 0;
795}
796
797static void
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200798tcf_chain0_head_change_cb_del(struct tcf_block *block,
799 struct tcf_block_ext_info *ei)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100800{
801 struct tcf_filter_chain_list_item *item;
802
Vlad Buslov165f0132019-02-11 10:55:35 +0200803 mutex_lock(&block->lock);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200804 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100805 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
806 (item->chain_head_change == ei->chain_head_change &&
807 item->chain_head_change_priv == ei->chain_head_change_priv)) {
Vlad Buslov165f0132019-02-11 10:55:35 +0200808 if (block->chain0.chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200809 tcf_chain_head_change_item(item, NULL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100810 list_del(&item->list);
Vlad Buslov165f0132019-02-11 10:55:35 +0200811 mutex_unlock(&block->lock);
812
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100813 kfree(item);
814 return;
815 }
816 }
Vlad Buslov165f0132019-02-11 10:55:35 +0200817 mutex_unlock(&block->lock);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100818 WARN_ON(1);
819}
820
Jiri Pirko48617382018-01-17 11:46:46 +0100821struct tcf_net {
Vlad Buslovab281622018-09-24 19:22:56 +0300822 spinlock_t idr_lock; /* Protects idr */
Jiri Pirko48617382018-01-17 11:46:46 +0100823 struct idr idr;
824};
825
826static unsigned int tcf_net_id;
827
828static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100829 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100830{
Jiri Pirko48617382018-01-17 11:46:46 +0100831 struct tcf_net *tn = net_generic(net, tcf_net_id);
Vlad Buslovab281622018-09-24 19:22:56 +0300832 int err;
Jiri Pirko48617382018-01-17 11:46:46 +0100833
Vlad Buslovab281622018-09-24 19:22:56 +0300834 idr_preload(GFP_KERNEL);
835 spin_lock(&tn->idr_lock);
836 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
837 GFP_NOWAIT);
838 spin_unlock(&tn->idr_lock);
839 idr_preload_end();
840
841 return err;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100842}
843
Jiri Pirko48617382018-01-17 11:46:46 +0100844static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200845{
Jiri Pirko48617382018-01-17 11:46:46 +0100846 struct tcf_net *tn = net_generic(net, tcf_net_id);
847
Vlad Buslovab281622018-09-24 19:22:56 +0300848 spin_lock(&tn->idr_lock);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500849 idr_remove(&tn->idr, block->index);
Vlad Buslovab281622018-09-24 19:22:56 +0300850 spin_unlock(&tn->idr_lock);
Jiri Pirko48617382018-01-17 11:46:46 +0100851}
852
853static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100854 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100855 struct netlink_ext_ack *extack)
856{
857 struct tcf_block *block;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200858
Jiri Pirko48617382018-01-17 11:46:46 +0100859 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500860 if (!block) {
861 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100862 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500863 }
Vlad Buslovc266f642019-02-11 10:55:32 +0200864 mutex_init(&block->lock);
John Hurley59eb87c2019-11-02 14:17:47 +0000865 mutex_init(&block->proto_destroy_lock);
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300866 init_rwsem(&block->cb_lock);
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200867 flow_block_init(&block->flow_block);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200868 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100869 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200870 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200871
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300872 refcount_set(&block->refcnt, 1);
Jiri Pirko48617382018-01-17 11:46:46 +0100873 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100874 block->index = block_index;
875
876 /* Don't store q pointer for blocks which are shared */
877 if (!tcf_block_shared(block))
878 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100879 return block;
Jiri Pirko48617382018-01-17 11:46:46 +0100880}
881
882static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
883{
884 struct tcf_net *tn = net_generic(net, tcf_net_id);
885
Matthew Wilcox322d8842017-11-28 10:01:24 -0500886 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100887}
888
Vlad Buslov0607e432018-09-24 19:22:57 +0300889static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
890{
891 struct tcf_block *block;
892
893 rcu_read_lock();
894 block = tcf_block_lookup(net, block_index);
895 if (block && !refcount_inc_not_zero(&block->refcnt))
896 block = NULL;
897 rcu_read_unlock();
898
899 return block;
900}
901
Vlad Buslovbbf73832019-02-11 10:55:36 +0200902static struct tcf_chain *
903__tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
904{
905 mutex_lock(&block->lock);
906 if (chain)
907 chain = list_is_last(&chain->list, &block->chain_list) ?
908 NULL : list_next_entry(chain, list);
909 else
910 chain = list_first_entry_or_null(&block->chain_list,
911 struct tcf_chain, list);
912
913 /* skip all action-only chains */
914 while (chain && tcf_chain_held_by_acts_only(chain))
915 chain = list_is_last(&chain->list, &block->chain_list) ?
916 NULL : list_next_entry(chain, list);
917
918 if (chain)
919 tcf_chain_hold(chain);
920 mutex_unlock(&block->lock);
921
922 return chain;
923}
924
925/* Function to be used by all clients that want to iterate over all chains on
926 * block. It properly obtains block->lock and takes reference to chain before
927 * returning it. Users of this function must be tolerant to concurrent chain
928 * insertion/deletion or ensure that no concurrent chain modification is
929 * possible. Note that all netlink dump callbacks cannot guarantee to provide
930 * consistent dump because rtnl lock is released each time skb is filled with
931 * data and sent to user-space.
932 */
933
934struct tcf_chain *
935tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
936{
937 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
938
939 if (chain)
940 tcf_chain_put(chain);
941
942 return chain_next;
943}
944EXPORT_SYMBOL(tcf_get_next_chain);
945
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200946static struct tcf_proto *
947__tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
948{
Vlad Buslov8b646782019-02-11 10:55:41 +0200949 u32 prio = 0;
950
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200951 ASSERT_RTNL();
952 mutex_lock(&chain->filter_chain_lock);
953
Vlad Buslov8b646782019-02-11 10:55:41 +0200954 if (!tp) {
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200955 tp = tcf_chain_dereference(chain->filter_chain, chain);
Vlad Buslov8b646782019-02-11 10:55:41 +0200956 } else if (tcf_proto_is_deleting(tp)) {
957 /* 'deleting' flag is set and chain->filter_chain_lock was
958 * unlocked, which means next pointer could be invalid. Restart
959 * search.
960 */
961 prio = tp->prio + 1;
962 tp = tcf_chain_dereference(chain->filter_chain, chain);
963
964 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
965 if (!tp->deleting && tp->prio >= prio)
966 break;
967 } else {
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200968 tp = tcf_chain_dereference(tp->next, chain);
Vlad Buslov8b646782019-02-11 10:55:41 +0200969 }
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200970
971 if (tp)
972 tcf_proto_get(tp);
973
974 mutex_unlock(&chain->filter_chain_lock);
975
976 return tp;
977}
978
979/* Function to be used by all clients that want to iterate over all tp's on
980 * chain. Users of this function must be tolerant to concurrent tp
981 * insertion/deletion or ensure that no concurrent chain modification is
982 * possible. Note that all netlink dump callbacks cannot guarantee to provide
983 * consistent dump because rtnl lock is released each time skb is filled with
984 * data and sent to user-space.
985 */
986
987struct tcf_proto *
Vlad Buslov12db03b2019-02-11 10:55:45 +0200988tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp,
989 bool rtnl_held)
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200990{
991 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
992
993 if (tp)
Vlad Buslov12db03b2019-02-11 10:55:45 +0200994 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200995
996 return tp_next;
997}
998EXPORT_SYMBOL(tcf_get_next_proto);
999
Vlad Buslov12db03b2019-02-11 10:55:45 +02001000static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
Vlad Buslovf0023432018-09-24 19:22:55 +03001001{
1002 struct tcf_chain *chain;
1003
Vlad Buslovbbf73832019-02-11 10:55:36 +02001004 /* Last reference to block. At this point chains cannot be added or
1005 * removed concurrently.
Vlad Buslovf0023432018-09-24 19:22:55 +03001006 */
Vlad Buslovbbf73832019-02-11 10:55:36 +02001007 for (chain = tcf_get_next_chain(block, NULL);
1008 chain;
1009 chain = tcf_get_next_chain(block, chain)) {
Vlad Buslovf0023432018-09-24 19:22:55 +03001010 tcf_chain_put_explicitly_created(chain);
Vlad Buslov12db03b2019-02-11 10:55:45 +02001011 tcf_chain_flush(chain, rtnl_held);
Vlad Buslovf0023432018-09-24 19:22:55 +03001012 }
1013}
1014
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001015/* Lookup Qdisc and increments its reference counter.
1016 * Set parent, if necessary.
1017 */
1018
1019static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1020 u32 *parent, int ifindex, bool rtnl_held,
1021 struct netlink_ext_ack *extack)
1022{
1023 const struct Qdisc_class_ops *cops;
1024 struct net_device *dev;
1025 int err = 0;
1026
1027 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1028 return 0;
1029
1030 rcu_read_lock();
1031
1032 /* Find link */
1033 dev = dev_get_by_index_rcu(net, ifindex);
1034 if (!dev) {
1035 rcu_read_unlock();
1036 return -ENODEV;
1037 }
1038
1039 /* Find qdisc */
1040 if (!*parent) {
1041 *q = dev->qdisc;
1042 *parent = (*q)->handle;
1043 } else {
1044 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1045 if (!*q) {
1046 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1047 err = -EINVAL;
1048 goto errout_rcu;
1049 }
1050 }
1051
1052 *q = qdisc_refcount_inc_nz(*q);
1053 if (!*q) {
1054 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1055 err = -EINVAL;
1056 goto errout_rcu;
1057 }
1058
1059 /* Is it classful? */
1060 cops = (*q)->ops->cl_ops;
1061 if (!cops) {
1062 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1063 err = -EINVAL;
1064 goto errout_qdisc;
1065 }
1066
1067 if (!cops->tcf_block) {
1068 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1069 err = -EOPNOTSUPP;
1070 goto errout_qdisc;
1071 }
1072
1073errout_rcu:
1074 /* At this point we know that qdisc is not noop_qdisc,
1075 * which means that qdisc holds a reference to net_device
1076 * and we hold a reference to qdisc, so it is safe to release
1077 * rcu read lock.
1078 */
1079 rcu_read_unlock();
1080 return err;
1081
1082errout_qdisc:
1083 rcu_read_unlock();
1084
1085 if (rtnl_held)
1086 qdisc_put(*q);
1087 else
1088 qdisc_put_unlocked(*q);
1089 *q = NULL;
1090
1091 return err;
1092}
1093
1094static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1095 int ifindex, struct netlink_ext_ack *extack)
1096{
1097 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1098 return 0;
1099
1100 /* Do we search for filter, attached to class? */
1101 if (TC_H_MIN(parent)) {
1102 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1103
1104 *cl = cops->find(q, parent);
1105 if (*cl == 0) {
1106 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1107 return -ENOENT;
1108 }
1109 }
1110
1111 return 0;
1112}
1113
1114static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1115 unsigned long cl, int ifindex,
1116 u32 block_index,
1117 struct netlink_ext_ack *extack)
1118{
1119 struct tcf_block *block;
1120
1121 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1122 block = tcf_block_refcnt_get(net, block_index);
1123 if (!block) {
1124 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1125 return ERR_PTR(-EINVAL);
1126 }
1127 } else {
1128 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1129
1130 block = cops->tcf_block(q, cl, extack);
1131 if (!block)
1132 return ERR_PTR(-EINVAL);
1133
1134 if (tcf_block_shared(block)) {
1135 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1136 return ERR_PTR(-EOPNOTSUPP);
1137 }
1138
1139 /* Always take reference to block in order to support execution
1140 * of rules update path of cls API without rtnl lock. Caller
1141 * must release block when it is finished using it. 'if' block
1142 * of this conditional obtain reference to block by calling
1143 * tcf_block_refcnt_get().
1144 */
1145 refcount_inc(&block->refcnt);
1146 }
1147
1148 return block;
1149}
1150
Vlad Buslov0607e432018-09-24 19:22:57 +03001151static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001152 struct tcf_block_ext_info *ei, bool rtnl_held)
Vlad Buslov0607e432018-09-24 19:22:57 +03001153{
Vlad Buslovc266f642019-02-11 10:55:32 +02001154 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
Vlad Buslov0607e432018-09-24 19:22:57 +03001155 /* Flushing/putting all chains will cause the block to be
1156 * deallocated when last chain is freed. However, if chain_list
1157 * is empty, block has to be manually deallocated. After block
1158 * reference counter reached 0, it is no longer possible to
1159 * increment it or add new chains to block.
1160 */
1161 bool free_block = list_empty(&block->chain_list);
1162
Vlad Buslovc266f642019-02-11 10:55:32 +02001163 mutex_unlock(&block->lock);
Vlad Buslov0607e432018-09-24 19:22:57 +03001164 if (tcf_block_shared(block))
1165 tcf_block_remove(block, block->net);
Vlad Buslov0607e432018-09-24 19:22:57 +03001166
1167 if (q)
1168 tcf_block_offload_unbind(block, q, ei);
1169
1170 if (free_block)
Vlad Buslovc266f642019-02-11 10:55:32 +02001171 tcf_block_destroy(block);
Vlad Buslov0607e432018-09-24 19:22:57 +03001172 else
Vlad Buslov12db03b2019-02-11 10:55:45 +02001173 tcf_block_flush_all_chains(block, rtnl_held);
Vlad Buslov0607e432018-09-24 19:22:57 +03001174 } else if (q) {
1175 tcf_block_offload_unbind(block, q, ei);
1176 }
1177}
1178
Vlad Buslov12db03b2019-02-11 10:55:45 +02001179static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
Vlad Buslov0607e432018-09-24 19:22:57 +03001180{
Vlad Buslov12db03b2019-02-11 10:55:45 +02001181 __tcf_block_put(block, NULL, NULL, rtnl_held);
Vlad Buslov0607e432018-09-24 19:22:57 +03001182}
1183
Vlad Buslovc431f892018-05-31 09:52:53 +03001184/* Find tcf block.
1185 * Set q, parent, cl when appropriate.
1186 */
1187
1188static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1189 u32 *parent, unsigned long *cl,
1190 int ifindex, u32 block_index,
1191 struct netlink_ext_ack *extack)
1192{
1193 struct tcf_block *block;
Vlad Buslove368fdb2018-09-24 19:22:53 +03001194 int err = 0;
Vlad Buslovc431f892018-05-31 09:52:53 +03001195
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001196 ASSERT_RTNL();
Vlad Buslovc431f892018-05-31 09:52:53 +03001197
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001198 err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1199 if (err)
1200 goto errout;
Vlad Buslove368fdb2018-09-24 19:22:53 +03001201
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001202 err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1203 if (err)
1204 goto errout_qdisc;
Vlad Buslovc431f892018-05-31 09:52:53 +03001205
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001206 block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
Dan Carpenteraf736bf2019-02-18 12:26:32 +03001207 if (IS_ERR(block)) {
1208 err = PTR_ERR(block);
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001209 goto errout_qdisc;
Dan Carpenteraf736bf2019-02-18 12:26:32 +03001210 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001211
1212 return block;
Vlad Buslove368fdb2018-09-24 19:22:53 +03001213
Vlad Buslove368fdb2018-09-24 19:22:53 +03001214errout_qdisc:
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001215 if (*q)
Vlad Buslove368fdb2018-09-24 19:22:53 +03001216 qdisc_put(*q);
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001217errout:
1218 *q = NULL;
Vlad Buslove368fdb2018-09-24 19:22:53 +03001219 return ERR_PTR(err);
1220}
1221
Vlad Buslov12db03b2019-02-11 10:55:45 +02001222static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1223 bool rtnl_held)
Vlad Buslove368fdb2018-09-24 19:22:53 +03001224{
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001225 if (!IS_ERR_OR_NULL(block))
Vlad Buslov12db03b2019-02-11 10:55:45 +02001226 tcf_block_refcnt_put(block, rtnl_held);
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001227
Vlad Buslov470502d2019-02-11 10:55:48 +02001228 if (q) {
1229 if (rtnl_held)
1230 qdisc_put(q);
1231 else
1232 qdisc_put_unlocked(q);
1233 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001234}
1235
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001236struct tcf_block_owner_item {
1237 struct list_head list;
1238 struct Qdisc *q;
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001239 enum flow_block_binder_type binder_type;
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001240};
1241
1242static void
1243tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1244 struct Qdisc *q,
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001245 enum flow_block_binder_type binder_type)
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001246{
1247 if (block->keep_dst &&
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001248 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1249 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001250 netif_keep_dst(qdisc_dev(q));
1251}
1252
1253void tcf_block_netif_keep_dst(struct tcf_block *block)
1254{
1255 struct tcf_block_owner_item *item;
1256
1257 block->keep_dst = true;
1258 list_for_each_entry(item, &block->owner_list, list)
1259 tcf_block_owner_netif_keep_dst(block, item->q,
1260 item->binder_type);
1261}
1262EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1263
1264static int tcf_block_owner_add(struct tcf_block *block,
1265 struct Qdisc *q,
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001266 enum flow_block_binder_type binder_type)
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001267{
1268 struct tcf_block_owner_item *item;
1269
1270 item = kmalloc(sizeof(*item), GFP_KERNEL);
1271 if (!item)
1272 return -ENOMEM;
1273 item->q = q;
1274 item->binder_type = binder_type;
1275 list_add(&item->list, &block->owner_list);
1276 return 0;
1277}
1278
1279static void tcf_block_owner_del(struct tcf_block *block,
1280 struct Qdisc *q,
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001281 enum flow_block_binder_type binder_type)
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001282{
1283 struct tcf_block_owner_item *item;
1284
1285 list_for_each_entry(item, &block->owner_list, list) {
1286 if (item->q == q && item->binder_type == binder_type) {
1287 list_del(&item->list);
1288 kfree(item);
1289 return;
1290 }
1291 }
1292 WARN_ON(1);
1293}
1294
Jiri Pirko48617382018-01-17 11:46:46 +01001295int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1296 struct tcf_block_ext_info *ei,
1297 struct netlink_ext_ack *extack)
1298{
1299 struct net *net = qdisc_net(q);
1300 struct tcf_block *block = NULL;
Jiri Pirko48617382018-01-17 11:46:46 +01001301 int err;
1302
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001303 if (ei->block_index)
Jiri Pirko48617382018-01-17 11:46:46 +01001304 /* block_index not 0 means the shared block is requested */
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001305 block = tcf_block_refcnt_get(net, ei->block_index);
Jiri Pirko48617382018-01-17 11:46:46 +01001306
1307 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +01001308 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +01001309 if (IS_ERR(block))
1310 return PTR_ERR(block);
Jiri Pirkobb047dd2018-02-13 12:00:16 +01001311 if (tcf_block_shared(block)) {
1312 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +01001313 if (err)
1314 goto err_block_insert;
1315 }
1316 }
1317
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001318 err = tcf_block_owner_add(block, q, ei->binder_type);
1319 if (err)
1320 goto err_block_owner_add;
1321
1322 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1323
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001324 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +01001325 if (err)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001326 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +01001327
John Hurley60513bd2018-06-25 14:30:04 -07001328 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +01001329 if (err)
1330 goto err_block_offload_bind;
1331
Jiri Pirko6529eab2017-05-17 11:07:55 +02001332 *p_block = block;
1333 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001334
Jiri Pirkocaa72602018-01-17 11:46:50 +01001335err_block_offload_bind:
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001336 tcf_chain0_head_change_cb_del(block, ei);
1337err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001338 tcf_block_owner_del(block, q, ei->binder_type);
1339err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +01001340err_block_insert:
Vlad Buslov12db03b2019-02-11 10:55:45 +02001341 tcf_block_refcnt_put(block, true);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001342 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001343}
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001344EXPORT_SYMBOL(tcf_block_get_ext);
1345
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001346static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1347{
1348 struct tcf_proto __rcu **p_filter_chain = priv;
1349
1350 rcu_assign_pointer(*p_filter_chain, tp_head);
1351}
1352
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001353int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -05001354 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1355 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001356{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001357 struct tcf_block_ext_info ei = {
1358 .chain_head_change = tcf_chain_head_change_dflt,
1359 .chain_head_change_priv = p_filter_chain,
1360 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001361
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001362 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -05001363 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001364}
Jiri Pirko6529eab2017-05-17 11:07:55 +02001365EXPORT_SYMBOL(tcf_block_get);
1366
Cong Wang7aa00452017-10-26 18:24:28 -07001367/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +01001368 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -07001369 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001370void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +09001371 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -07001372{
David S. Millerc30abd52017-12-16 22:11:55 -05001373 if (!block)
1374 return;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001375 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001376 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +01001377
Vlad Buslov12db03b2019-02-11 10:55:45 +02001378 __tcf_block_put(block, q, ei, true);
Jiri Pirko6529eab2017-05-17 11:07:55 +02001379}
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001380EXPORT_SYMBOL(tcf_block_put_ext);
1381
1382void tcf_block_put(struct tcf_block *block)
1383{
1384 struct tcf_block_ext_info ei = {0, };
1385
Jiri Pirko4853f122017-12-21 13:13:59 +01001386 if (!block)
1387 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001388 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001389}
David S. Millere1ea2f92017-10-30 14:10:01 +09001390
Jiri Pirko6529eab2017-05-17 11:07:55 +02001391EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +01001392
John Hurley32636742018-06-25 14:30:10 -07001393static int
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +02001394tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
John Hurley32636742018-06-25 14:30:10 -07001395 void *cb_priv, bool add, bool offload_in_use,
1396 struct netlink_ext_ack *extack)
1397{
Vlad Buslovbbf73832019-02-11 10:55:36 +02001398 struct tcf_chain *chain, *chain_prev;
Vlad Buslovfe2923a2019-02-11 10:55:40 +02001399 struct tcf_proto *tp, *tp_prev;
John Hurley32636742018-06-25 14:30:10 -07001400 int err;
1401
Vlad Buslov4f8116c2019-08-26 16:44:57 +03001402 lockdep_assert_held(&block->cb_lock);
1403
Vlad Buslovbbf73832019-02-11 10:55:36 +02001404 for (chain = __tcf_get_next_chain(block, NULL);
1405 chain;
1406 chain_prev = chain,
1407 chain = __tcf_get_next_chain(block, chain),
1408 tcf_chain_put(chain_prev)) {
Vlad Buslovfe2923a2019-02-11 10:55:40 +02001409 for (tp = __tcf_get_next_proto(chain, NULL); tp;
1410 tp_prev = tp,
1411 tp = __tcf_get_next_proto(chain, tp),
Vlad Buslov12db03b2019-02-11 10:55:45 +02001412 tcf_proto_put(tp_prev, true, NULL)) {
John Hurley32636742018-06-25 14:30:10 -07001413 if (tp->ops->reoffload) {
1414 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1415 extack);
1416 if (err && add)
1417 goto err_playback_remove;
1418 } else if (add && offload_in_use) {
1419 err = -EOPNOTSUPP;
1420 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1421 goto err_playback_remove;
1422 }
1423 }
1424 }
1425
1426 return 0;
1427
1428err_playback_remove:
Vlad Buslov12db03b2019-02-11 10:55:45 +02001429 tcf_proto_put(tp, true, NULL);
Vlad Buslovbbf73832019-02-11 10:55:36 +02001430 tcf_chain_put(chain);
John Hurley32636742018-06-25 14:30:10 -07001431 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1432 extack);
1433 return err;
1434}
1435
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001436static int tcf_block_bind(struct tcf_block *block,
1437 struct flow_block_offload *bo)
1438{
1439 struct flow_block_cb *block_cb, *next;
1440 int err, i = 0;
1441
Vlad Buslov4f8116c2019-08-26 16:44:57 +03001442 lockdep_assert_held(&block->cb_lock);
1443
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001444 list_for_each_entry(block_cb, &bo->cb_list, list) {
1445 err = tcf_block_playback_offloads(block, block_cb->cb,
1446 block_cb->cb_priv, true,
1447 tcf_block_offload_in_use(block),
1448 bo->extack);
1449 if (err)
1450 goto err_unroll;
Vlad Buslovc9f14472019-08-26 16:45:01 +03001451 if (!bo->unlocked_driver_cb)
1452 block->lockeddevcnt++;
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001453
1454 i++;
1455 }
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +02001456 list_splice(&bo->cb_list, &block->flow_block.cb_list);
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001457
1458 return 0;
1459
1460err_unroll:
1461 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1462 if (i-- > 0) {
1463 list_del(&block_cb->list);
1464 tcf_block_playback_offloads(block, block_cb->cb,
1465 block_cb->cb_priv, false,
1466 tcf_block_offload_in_use(block),
1467 NULL);
Vlad Buslovc9f14472019-08-26 16:45:01 +03001468 if (!bo->unlocked_driver_cb)
1469 block->lockeddevcnt--;
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001470 }
1471 flow_block_cb_free(block_cb);
1472 }
1473
1474 return err;
1475}
1476
1477static void tcf_block_unbind(struct tcf_block *block,
1478 struct flow_block_offload *bo)
1479{
1480 struct flow_block_cb *block_cb, *next;
1481
Vlad Buslov4f8116c2019-08-26 16:44:57 +03001482 lockdep_assert_held(&block->cb_lock);
1483
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001484 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1485 tcf_block_playback_offloads(block, block_cb->cb,
1486 block_cb->cb_priv, false,
1487 tcf_block_offload_in_use(block),
1488 NULL);
1489 list_del(&block_cb->list);
1490 flow_block_cb_free(block_cb);
Vlad Buslovc9f14472019-08-26 16:45:01 +03001491 if (!bo->unlocked_driver_cb)
1492 block->lockeddevcnt--;
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001493 }
1494}
1495
1496static int tcf_block_setup(struct tcf_block *block,
1497 struct flow_block_offload *bo)
1498{
1499 int err;
1500
1501 switch (bo->command) {
1502 case FLOW_BLOCK_BIND:
1503 err = tcf_block_bind(block, bo);
1504 break;
1505 case FLOW_BLOCK_UNBIND:
1506 err = 0;
1507 tcf_block_unbind(block, bo);
1508 break;
1509 default:
1510 WARN_ON_ONCE(1);
1511 err = -EOPNOTSUPP;
1512 }
1513
1514 return err;
1515}
1516
Jiri Pirko87d83092017-05-17 11:07:54 +02001517/* Main classifier routine: scans classifier chain attached
1518 * to this qdisc, (optionally) tests for protocol and asks
1519 * specific classifiers.
1520 */
Paul Blakey9410c942020-02-16 12:01:21 +02001521static inline int __tcf_classify(struct sk_buff *skb,
1522 const struct tcf_proto *tp,
Paul Blakeyaf699622020-02-16 12:01:24 +02001523 const struct tcf_proto *orig_tp,
Paul Blakey9410c942020-02-16 12:01:21 +02001524 struct tcf_result *res,
1525 bool compat_mode,
1526 u32 *last_executed_chain)
Jiri Pirko87d83092017-05-17 11:07:54 +02001527{
Jiri Pirko87d83092017-05-17 11:07:54 +02001528#ifdef CONFIG_NET_CLS_ACT
1529 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001530 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001531 int limit = 0;
1532
1533reclassify:
1534#endif
1535 for (; tp; tp = rcu_dereference_bh(tp->next)) {
Cong Wangcd0c4e72019-01-11 18:55:42 -08001536 __be16 protocol = tc_skb_protocol(skb);
Jiri Pirko87d83092017-05-17 11:07:54 +02001537 int err;
1538
1539 if (tp->protocol != protocol &&
1540 tp->protocol != htons(ETH_P_ALL))
1541 continue;
1542
1543 err = tp->classify(skb, tp, res);
1544#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +02001545 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001546 first_tp = orig_tp;
Paul Blakey9410c942020-02-16 12:01:21 +02001547 *last_executed_chain = first_tp->chain->index;
Jiri Pirko87d83092017-05-17 11:07:54 +02001548 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +02001549 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001550 first_tp = res->goto_tp;
Paul Blakey9410c942020-02-16 12:01:21 +02001551 *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
Jiri Pirkodb505142017-05-17 11:08:03 +02001552 goto reset;
1553 }
Jiri Pirko87d83092017-05-17 11:07:54 +02001554#endif
1555 if (err >= 0)
1556 return err;
1557 }
1558
1559 return TC_ACT_UNSPEC; /* signal: continue lookup */
1560#ifdef CONFIG_NET_CLS_ACT
1561reset:
1562 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +01001563 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1564 tp->chain->block->index,
1565 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +02001566 ntohs(tp->protocol));
1567 return TC_ACT_SHOT;
1568 }
1569
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001570 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001571 goto reclassify;
1572#endif
1573}
Paul Blakey9410c942020-02-16 12:01:21 +02001574
1575int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1576 struct tcf_result *res, bool compat_mode)
1577{
1578 u32 last_executed_chain = 0;
1579
Paul Blakeyaf699622020-02-16 12:01:24 +02001580 return __tcf_classify(skb, tp, tp, res, compat_mode,
Paul Blakey9410c942020-02-16 12:01:21 +02001581 &last_executed_chain);
1582}
Jiri Pirko87d83092017-05-17 11:07:54 +02001583EXPORT_SYMBOL(tcf_classify);
1584
Paul Blakey7d17c542020-02-16 12:01:22 +02001585int tcf_classify_ingress(struct sk_buff *skb,
1586 const struct tcf_block *ingress_block,
1587 const struct tcf_proto *tp,
Paul Blakey9410c942020-02-16 12:01:21 +02001588 struct tcf_result *res, bool compat_mode)
1589{
1590#if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1591 u32 last_executed_chain = 0;
1592
Paul Blakeyaf699622020-02-16 12:01:24 +02001593 return __tcf_classify(skb, tp, tp, res, compat_mode,
Paul Blakey9410c942020-02-16 12:01:21 +02001594 &last_executed_chain);
1595#else
1596 u32 last_executed_chain = tp ? tp->chain->index : 0;
Paul Blakeyaf699622020-02-16 12:01:24 +02001597 const struct tcf_proto *orig_tp = tp;
Paul Blakey9410c942020-02-16 12:01:21 +02001598 struct tc_skb_ext *ext;
1599 int ret;
1600
Paul Blakeyaf699622020-02-16 12:01:24 +02001601 ext = skb_ext_find(skb, TC_SKB_EXT);
1602
1603 if (ext && ext->chain) {
1604 struct tcf_chain *fchain;
1605
1606 fchain = tcf_chain_lookup_rcu(ingress_block, ext->chain);
1607 if (!fchain)
1608 return TC_ACT_SHOT;
1609
1610 /* Consume, so cloned/redirect skbs won't inherit ext */
1611 skb_ext_del(skb, TC_SKB_EXT);
1612
1613 tp = rcu_dereference_bh(fchain->filter_chain);
Paul Blakeya080da62020-04-06 18:36:56 +03001614 last_executed_chain = fchain->index;
Paul Blakeyaf699622020-02-16 12:01:24 +02001615 }
1616
1617 ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode,
1618 &last_executed_chain);
Paul Blakey9410c942020-02-16 12:01:21 +02001619
1620 /* If we missed on some chain */
1621 if (ret == TC_ACT_UNSPEC && last_executed_chain) {
1622 ext = skb_ext_add(skb, TC_SKB_EXT);
1623 if (WARN_ON_ONCE(!ext))
1624 return TC_ACT_SHOT;
1625 ext->chain = last_executed_chain;
1626 }
1627
1628 return ret;
1629#endif
1630}
1631EXPORT_SYMBOL(tcf_classify_ingress);
1632
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001633struct tcf_chain_info {
1634 struct tcf_proto __rcu **pprev;
1635 struct tcf_proto __rcu *next;
1636};
1637
Vlad Busloved76f5e2019-02-11 10:55:38 +02001638static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1639 struct tcf_chain_info *chain_info)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001640{
Vlad Busloved76f5e2019-02-11 10:55:38 +02001641 return tcf_chain_dereference(*chain_info->pprev, chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001642}
1643
Vlad Buslov726d06122019-02-11 10:55:42 +02001644static int tcf_chain_tp_insert(struct tcf_chain *chain,
1645 struct tcf_chain_info *chain_info,
1646 struct tcf_proto *tp)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001647{
Vlad Buslov726d06122019-02-11 10:55:42 +02001648 if (chain->flushing)
1649 return -EAGAIN;
1650
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001651 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001652 tcf_chain0_head_change(chain, tp);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02001653 tcf_proto_get(tp);
Vlad Busloved76f5e2019-02-11 10:55:38 +02001654 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001655 rcu_assign_pointer(*chain_info->pprev, tp);
Vlad Buslov726d06122019-02-11 10:55:42 +02001656
1657 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001658}
1659
1660static void tcf_chain_tp_remove(struct tcf_chain *chain,
1661 struct tcf_chain_info *chain_info,
1662 struct tcf_proto *tp)
1663{
Vlad Busloved76f5e2019-02-11 10:55:38 +02001664 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001665
Vlad Buslov8b646782019-02-11 10:55:41 +02001666 tcf_proto_mark_delete(tp);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001667 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001668 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001669 RCU_INIT_POINTER(*chain_info->pprev, next);
1670}
1671
1672static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1673 struct tcf_chain_info *chain_info,
1674 u32 protocol, u32 prio,
Vlad Buslov8b646782019-02-11 10:55:41 +02001675 bool prio_allocate);
1676
1677/* Try to insert new proto.
1678 * If proto with specified priority already exists, free new proto
1679 * and return existing one.
1680 */
1681
1682static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1683 struct tcf_proto *tp_new,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001684 u32 protocol, u32 prio,
1685 bool rtnl_held)
Vlad Buslov8b646782019-02-11 10:55:41 +02001686{
1687 struct tcf_chain_info chain_info;
1688 struct tcf_proto *tp;
Vlad Buslov726d06122019-02-11 10:55:42 +02001689 int err = 0;
Vlad Buslov8b646782019-02-11 10:55:41 +02001690
1691 mutex_lock(&chain->filter_chain_lock);
1692
John Hurley59eb87c2019-11-02 14:17:47 +00001693 if (tcf_proto_exists_destroying(chain, tp_new)) {
1694 mutex_unlock(&chain->filter_chain_lock);
1695 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1696 return ERR_PTR(-EAGAIN);
1697 }
1698
Vlad Buslov8b646782019-02-11 10:55:41 +02001699 tp = tcf_chain_tp_find(chain, &chain_info,
1700 protocol, prio, false);
1701 if (!tp)
Vlad Buslov726d06122019-02-11 10:55:42 +02001702 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
Vlad Buslov8b646782019-02-11 10:55:41 +02001703 mutex_unlock(&chain->filter_chain_lock);
1704
1705 if (tp) {
John Hurley59eb87c2019-11-02 14:17:47 +00001706 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
Vlad Buslov8b646782019-02-11 10:55:41 +02001707 tp_new = tp;
Vlad Buslov726d06122019-02-11 10:55:42 +02001708 } else if (err) {
John Hurley59eb87c2019-11-02 14:17:47 +00001709 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
Vlad Buslov726d06122019-02-11 10:55:42 +02001710 tp_new = ERR_PTR(err);
Vlad Buslov8b646782019-02-11 10:55:41 +02001711 }
1712
1713 return tp_new;
1714}
1715
1716static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001717 struct tcf_proto *tp, bool rtnl_held,
Vlad Buslov8b646782019-02-11 10:55:41 +02001718 struct netlink_ext_ack *extack)
1719{
1720 struct tcf_chain_info chain_info;
1721 struct tcf_proto *tp_iter;
1722 struct tcf_proto **pprev;
1723 struct tcf_proto *next;
1724
1725 mutex_lock(&chain->filter_chain_lock);
1726
1727 /* Atomically find and remove tp from chain. */
1728 for (pprev = &chain->filter_chain;
1729 (tp_iter = tcf_chain_dereference(*pprev, chain));
1730 pprev = &tp_iter->next) {
1731 if (tp_iter == tp) {
1732 chain_info.pprev = pprev;
1733 chain_info.next = tp_iter->next;
1734 WARN_ON(tp_iter->deleting);
1735 break;
1736 }
1737 }
1738 /* Verify that tp still exists and no new filters were inserted
1739 * concurrently.
1740 * Mark tp for deletion if it is empty.
1741 */
Davide Carattia5b72a02019-12-28 16:36:58 +01001742 if (!tp_iter || !tcf_proto_check_delete(tp)) {
Vlad Buslov8b646782019-02-11 10:55:41 +02001743 mutex_unlock(&chain->filter_chain_lock);
1744 return;
1745 }
1746
John Hurley59eb87c2019-11-02 14:17:47 +00001747 tcf_proto_signal_destroying(chain, tp);
Vlad Buslov8b646782019-02-11 10:55:41 +02001748 next = tcf_chain_dereference(chain_info.next, chain);
1749 if (tp == chain->filter_chain)
1750 tcf_chain0_head_change(chain, next);
1751 RCU_INIT_POINTER(*chain_info.pprev, next);
1752 mutex_unlock(&chain->filter_chain_lock);
1753
Vlad Buslov12db03b2019-02-11 10:55:45 +02001754 tcf_proto_put(tp, rtnl_held, extack);
Vlad Buslov8b646782019-02-11 10:55:41 +02001755}
1756
1757static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1758 struct tcf_chain_info *chain_info,
1759 u32 protocol, u32 prio,
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001760 bool prio_allocate)
1761{
1762 struct tcf_proto **pprev;
1763 struct tcf_proto *tp;
1764
1765 /* Check the chain for existence of proto-tcf with this priority */
1766 for (pprev = &chain->filter_chain;
Vlad Busloved76f5e2019-02-11 10:55:38 +02001767 (tp = tcf_chain_dereference(*pprev, chain));
1768 pprev = &tp->next) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001769 if (tp->prio >= prio) {
1770 if (tp->prio == prio) {
1771 if (prio_allocate ||
1772 (tp->protocol != protocol && protocol))
1773 return ERR_PTR(-EINVAL);
1774 } else {
1775 tp = NULL;
1776 }
1777 break;
1778 }
1779 }
1780 chain_info->pprev = pprev;
Vlad Buslov4dbfa762019-02-11 10:55:39 +02001781 if (tp) {
1782 chain_info->next = tp->next;
1783 tcf_proto_get(tp);
1784 } else {
1785 chain_info->next = NULL;
1786 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001787 return tp;
1788}
1789
WANG Cong71203712017-08-07 15:26:50 -07001790static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001791 struct tcf_proto *tp, struct tcf_block *block,
1792 struct Qdisc *q, u32 parent, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001793 u32 portid, u32 seq, u16 flags, int event,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001794 bool terse_dump, bool rtnl_held)
WANG Cong71203712017-08-07 15:26:50 -07001795{
1796 struct tcmsg *tcm;
1797 struct nlmsghdr *nlh;
1798 unsigned char *b = skb_tail_pointer(skb);
1799
1800 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1801 if (!nlh)
1802 goto out_nlmsg_trim;
1803 tcm = nlmsg_data(nlh);
1804 tcm->tcm_family = AF_UNSPEC;
1805 tcm->tcm__pad1 = 0;
1806 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001807 if (q) {
1808 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1809 tcm->tcm_parent = parent;
1810 } else {
1811 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1812 tcm->tcm_block_index = block->index;
1813 }
WANG Cong71203712017-08-07 15:26:50 -07001814 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1815 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1816 goto nla_put_failure;
1817 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1818 goto nla_put_failure;
1819 if (!fh) {
1820 tcm->tcm_handle = 0;
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001821 } else if (terse_dump) {
1822 if (tp->ops->terse_dump) {
1823 if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
1824 rtnl_held) < 0)
1825 goto nla_put_failure;
1826 } else {
1827 goto cls_op_not_supp;
1828 }
WANG Cong71203712017-08-07 15:26:50 -07001829 } else {
Vlad Buslov12db03b2019-02-11 10:55:45 +02001830 if (tp->ops->dump &&
1831 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
WANG Cong71203712017-08-07 15:26:50 -07001832 goto nla_put_failure;
1833 }
1834 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1835 return skb->len;
1836
1837out_nlmsg_trim:
1838nla_put_failure:
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001839cls_op_not_supp:
WANG Cong71203712017-08-07 15:26:50 -07001840 nlmsg_trim(skb, b);
1841 return -1;
1842}
1843
1844static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1845 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001846 struct tcf_block *block, struct Qdisc *q,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001847 u32 parent, void *fh, int event, bool unicast,
1848 bool rtnl_held)
WANG Cong71203712017-08-07 15:26:50 -07001849{
1850 struct sk_buff *skb;
1851 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001852 int err = 0;
WANG Cong71203712017-08-07 15:26:50 -07001853
1854 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1855 if (!skb)
1856 return -ENOBUFS;
1857
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001858 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001859 n->nlmsg_seq, n->nlmsg_flags, event,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001860 false, rtnl_held) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001861 kfree_skb(skb);
1862 return -EINVAL;
1863 }
1864
1865 if (unicast)
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001866 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1867 else
1868 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1869 n->nlmsg_flags & NLM_F_ECHO);
WANG Cong71203712017-08-07 15:26:50 -07001870
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001871 if (err > 0)
1872 err = 0;
1873 return err;
WANG Cong71203712017-08-07 15:26:50 -07001874}
1875
1876static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1877 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001878 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001879 u32 parent, void *fh, bool unicast, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001880 bool rtnl_held, struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001881{
1882 struct sk_buff *skb;
1883 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1884 int err;
1885
1886 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1887 if (!skb)
1888 return -ENOBUFS;
1889
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001890 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001891 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001892 false, rtnl_held) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001893 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001894 kfree_skb(skb);
1895 return -EINVAL;
1896 }
1897
Vlad Buslov12db03b2019-02-11 10:55:45 +02001898 err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
WANG Cong71203712017-08-07 15:26:50 -07001899 if (err) {
1900 kfree_skb(skb);
1901 return err;
1902 }
1903
1904 if (unicast)
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001905 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1906 else
1907 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1908 n->nlmsg_flags & NLM_F_ECHO);
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001909 if (err < 0)
1910 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001911
1912 if (err > 0)
1913 err = 0;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001914 return err;
WANG Cong71203712017-08-07 15:26:50 -07001915}
1916
1917static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001918 struct tcf_block *block, struct Qdisc *q,
1919 u32 parent, struct nlmsghdr *n,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001920 struct tcf_chain *chain, int event,
1921 bool rtnl_held)
WANG Cong71203712017-08-07 15:26:50 -07001922{
1923 struct tcf_proto *tp;
1924
Vlad Buslov12db03b2019-02-11 10:55:45 +02001925 for (tp = tcf_get_next_proto(chain, NULL, rtnl_held);
1926 tp; tp = tcf_get_next_proto(chain, tp, rtnl_held))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001927 tfilter_notify(net, oskb, n, tp, block,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001928 q, parent, NULL, event, false, rtnl_held);
WANG Cong71203712017-08-07 15:26:50 -07001929}
1930
Vlad Buslov7d5509f2019-02-11 10:55:44 +02001931static void tfilter_put(struct tcf_proto *tp, void *fh)
1932{
1933 if (tp->ops->put && fh)
1934 tp->ops->put(tp, fh);
1935}
1936
Vlad Buslovc431f892018-05-31 09:52:53 +03001937static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001938 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001940 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001941 struct nlattr *tca[TCA_MAX + 1];
Cong Wang6f96c3c2019-10-07 13:26:28 -07001942 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 struct tcmsg *t;
1944 u32 protocol;
1945 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001946 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001948 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001949 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001950 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001951 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001952 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001955 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001957 int tp_created;
Vlad Buslov470502d2019-02-11 10:55:48 +02001958 bool rtnl_held = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Vlad Buslovc431f892018-05-31 09:52:53 +03001960 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001961 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001964 tp_created = 0;
1965
Johannes Berg8cb08172019-04-26 14:07:28 +02001966 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
1967 rtm_tca_policy, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001968 if (err < 0)
1969 return err;
1970
David S. Miller942b8162012-06-26 21:48:50 -07001971 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 protocol = TC_H_MIN(t->tcm_info);
1973 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001974 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 parent = t->tcm_parent;
Vlad Buslov4dbfa762019-02-11 10:55:39 +02001976 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 cl = 0;
Vlad Buslov470502d2019-02-11 10:55:48 +02001978 block = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001981 /* If no priority is provided by the user,
1982 * we allocate one.
1983 */
1984 if (n->nlmsg_flags & NLM_F_CREATE) {
1985 prio = TC_H_MAKE(0x80000000U, 0U);
1986 prio_allocate = true;
1987 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001988 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
1992
1993 /* Find head of filter chain. */
1994
Vlad Buslov470502d2019-02-11 10:55:48 +02001995 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
1996 if (err)
1997 return err;
1998
Cong Wang6f96c3c2019-10-07 13:26:28 -07001999 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2000 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2001 err = -EINVAL;
2002 goto errout;
2003 }
2004
Vlad Buslov470502d2019-02-11 10:55:48 +02002005 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2006 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2007 * type is not specified, classifier is not unlocked.
2008 */
2009 if (rtnl_held ||
2010 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
Cong Wang6f96c3c2019-10-07 13:26:28 -07002011 !tcf_proto_is_unlocked(name)) {
Vlad Buslov470502d2019-02-11 10:55:48 +02002012 rtnl_held = true;
2013 rtnl_lock();
2014 }
2015
2016 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2017 if (err)
2018 goto errout;
2019
2020 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2021 extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002022 if (IS_ERR(block)) {
2023 err = PTR_ERR(block);
2024 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002025 }
Cong Wanga7df4872020-04-30 20:53:49 -07002026 block->classid = parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002027
2028 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2029 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002030 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02002031 err = -EINVAL;
2032 goto errout;
2033 }
Vlad Buslovc431f892018-05-31 09:52:53 +03002034 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02002035 if (!chain) {
Jiri Pirkod5ed72a2018-08-27 20:58:43 +02002036 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03002037 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02002038 goto errout;
2039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Vlad Busloved76f5e2019-02-11 10:55:38 +02002041 mutex_lock(&chain->filter_chain_lock);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02002042 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2043 prio, prio_allocate);
2044 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002045 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02002046 err = PTR_ERR(tp);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002047 goto errout_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049
2050 if (tp == NULL) {
Vlad Buslov8b646782019-02-11 10:55:41 +02002051 struct tcf_proto *tp_new = NULL;
2052
Vlad Buslov726d06122019-02-11 10:55:42 +02002053 if (chain->flushing) {
2054 err = -EAGAIN;
2055 goto errout_locked;
2056 }
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /* Proto-tcf does not exist, create new one */
2059
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002060 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002061 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002062 err = -EINVAL;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002063 goto errout_locked;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Vlad Buslovc431f892018-05-31 09:52:53 +03002066 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002067 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 +01002068 err = -ENOENT;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002069 goto errout_locked;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02002072 if (prio_allocate)
Vlad Busloved76f5e2019-02-11 10:55:38 +02002073 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2074 &chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Vlad Busloved76f5e2019-02-11 10:55:38 +02002076 mutex_unlock(&chain->filter_chain_lock);
Eric Dumazet36d79af2020-01-21 11:02:20 -08002077 tp_new = tcf_proto_create(name, protocol, prio, chain,
2078 rtnl_held, extack);
Vlad Buslov8b646782019-02-11 10:55:41 +02002079 if (IS_ERR(tp_new)) {
2080 err = PTR_ERR(tp_new);
Vlad Buslov726d06122019-02-11 10:55:42 +02002081 goto errout_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Vlad Busloved76f5e2019-02-11 10:55:38 +02002083
Minoru Usui12186be2009-06-02 02:17:34 -07002084 tp_created = 1;
Vlad Buslov12db03b2019-02-11 10:55:45 +02002085 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2086 rtnl_held);
Vlad Buslov726d06122019-02-11 10:55:42 +02002087 if (IS_ERR(tp)) {
2088 err = PTR_ERR(tp);
2089 goto errout_tp;
2090 }
Vlad Busloved76f5e2019-02-11 10:55:38 +02002091 } else {
2092 mutex_unlock(&chain->filter_chain_lock);
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Vlad Buslov8b646782019-02-11 10:55:41 +02002095 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2096 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2097 err = -EINVAL;
2098 goto errout;
2099 }
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 fh = tp->ops->get(tp, t->tcm_handle);
2102
WANG Cong8113c092017-08-04 21:31:43 -07002103 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03002104 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002105 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 +01002106 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002108 }
Vlad Buslovc431f892018-05-31 09:52:53 +03002109 } else if (n->nlmsg_flags & NLM_F_EXCL) {
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002110 tfilter_put(tp, fh);
Vlad Buslovc431f892018-05-31 09:52:53 +03002111 NL_SET_ERR_MSG(extack, "Filter already exists");
2112 err = -EEXIST;
2113 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115
Jiri Pirko9f407f12018-07-23 09:23:07 +02002116 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2117 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2118 err = -EINVAL;
2119 goto errout;
2120 }
2121
Cong Wang2f7ef2f2014-04-25 13:54:06 -07002122 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05002123 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002124 rtnl_held, extack);
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002125 if (err == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002126 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002127 RTM_NEWTFILTER, false, rtnl_held);
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002128 tfilter_put(tp, fh);
Vlad Buslov503d81d2019-07-21 17:44:12 +03002129 /* q pointer is NULL for shared blocks */
2130 if (q)
2131 q->flags &= ~TCQ_F_CAN_BYPASS;
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134errout:
Vlad Buslov8b646782019-02-11 10:55:41 +02002135 if (err && tp_created)
Vlad Buslov12db03b2019-02-11 10:55:45 +02002136 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
Vlad Buslov726d06122019-02-11 10:55:42 +02002137errout_tp:
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002138 if (chain) {
2139 if (tp && !IS_ERR(tp))
Vlad Buslov12db03b2019-02-11 10:55:45 +02002140 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002141 if (!tp_created)
2142 tcf_chain_put(chain);
2143 }
Vlad Buslov12db03b2019-02-11 10:55:45 +02002144 tcf_block_release(q, block, rtnl_held);
Vlad Buslov470502d2019-02-11 10:55:48 +02002145
2146 if (rtnl_held)
2147 rtnl_unlock();
2148
2149 if (err == -EAGAIN) {
2150 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2151 * of target chain.
2152 */
2153 rtnl_held = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /* Replay the request. */
2155 goto replay;
Vlad Buslov470502d2019-02-11 10:55:48 +02002156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return err;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002158
2159errout_locked:
2160 mutex_unlock(&chain->filter_chain_lock);
2161 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Vlad Buslovc431f892018-05-31 09:52:53 +03002164static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2165 struct netlink_ext_ack *extack)
2166{
2167 struct net *net = sock_net(skb->sk);
2168 struct nlattr *tca[TCA_MAX + 1];
Cong Wang6f96c3c2019-10-07 13:26:28 -07002169 char name[IFNAMSIZ];
Vlad Buslovc431f892018-05-31 09:52:53 +03002170 struct tcmsg *t;
2171 u32 protocol;
2172 u32 prio;
2173 u32 parent;
2174 u32 chain_index;
2175 struct Qdisc *q = NULL;
2176 struct tcf_chain_info chain_info;
2177 struct tcf_chain *chain = NULL;
Vlad Buslov470502d2019-02-11 10:55:48 +02002178 struct tcf_block *block = NULL;
Vlad Buslovc431f892018-05-31 09:52:53 +03002179 struct tcf_proto *tp = NULL;
2180 unsigned long cl = 0;
2181 void *fh = NULL;
2182 int err;
Vlad Buslov470502d2019-02-11 10:55:48 +02002183 bool rtnl_held = false;
Vlad Buslovc431f892018-05-31 09:52:53 +03002184
2185 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2186 return -EPERM;
2187
Johannes Berg8cb08172019-04-26 14:07:28 +02002188 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2189 rtm_tca_policy, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002190 if (err < 0)
2191 return err;
2192
2193 t = nlmsg_data(n);
2194 protocol = TC_H_MIN(t->tcm_info);
2195 prio = TC_H_MAJ(t->tcm_info);
2196 parent = t->tcm_parent;
2197
2198 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2199 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2200 return -ENOENT;
2201 }
2202
2203 /* Find head of filter chain. */
2204
Vlad Buslov470502d2019-02-11 10:55:48 +02002205 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2206 if (err)
2207 return err;
2208
Cong Wang6f96c3c2019-10-07 13:26:28 -07002209 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2210 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2211 err = -EINVAL;
2212 goto errout;
2213 }
Vlad Buslov470502d2019-02-11 10:55:48 +02002214 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2215 * found), qdisc is not unlocked, classifier type is not specified,
2216 * classifier is not unlocked.
2217 */
2218 if (!prio ||
2219 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
Cong Wang6f96c3c2019-10-07 13:26:28 -07002220 !tcf_proto_is_unlocked(name)) {
Vlad Buslov470502d2019-02-11 10:55:48 +02002221 rtnl_held = true;
2222 rtnl_lock();
2223 }
2224
2225 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2226 if (err)
2227 goto errout;
2228
2229 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2230 extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002231 if (IS_ERR(block)) {
2232 err = PTR_ERR(block);
2233 goto errout;
2234 }
2235
2236 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2237 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2238 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2239 err = -EINVAL;
2240 goto errout;
2241 }
2242 chain = tcf_chain_get(block, chain_index, false);
2243 if (!chain) {
Jiri Pirko5ca8a252018-08-03 11:08:47 +02002244 /* User requested flush on non-existent chain. Nothing to do,
2245 * so just return success.
2246 */
2247 if (prio == 0) {
2248 err = 0;
2249 goto errout;
2250 }
Vlad Buslovc431f892018-05-31 09:52:53 +03002251 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Jiri Pirkob7b42472018-08-27 20:58:44 +02002252 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03002253 goto errout;
2254 }
2255
2256 if (prio == 0) {
2257 tfilter_notify_chain(net, skb, block, q, parent, n,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002258 chain, RTM_DELTFILTER, rtnl_held);
2259 tcf_chain_flush(chain, rtnl_held);
Vlad Buslovc431f892018-05-31 09:52:53 +03002260 err = 0;
2261 goto errout;
2262 }
2263
Vlad Busloved76f5e2019-02-11 10:55:38 +02002264 mutex_lock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002265 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2266 prio, false);
2267 if (!tp || IS_ERR(tp)) {
2268 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03002269 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002270 goto errout_locked;
Vlad Buslovc431f892018-05-31 09:52:53 +03002271 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2272 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2273 err = -EINVAL;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002274 goto errout_locked;
2275 } else if (t->tcm_handle == 0) {
John Hurley59eb87c2019-11-02 14:17:47 +00002276 tcf_proto_signal_destroying(chain, tp);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002277 tcf_chain_tp_remove(chain, &chain_info, tp);
2278 mutex_unlock(&chain->filter_chain_lock);
2279
Vlad Buslov12db03b2019-02-11 10:55:45 +02002280 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002281 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002282 RTM_DELTFILTER, false, rtnl_held);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002283 err = 0;
Vlad Buslovc431f892018-05-31 09:52:53 +03002284 goto errout;
2285 }
Vlad Busloved76f5e2019-02-11 10:55:38 +02002286 mutex_unlock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002287
2288 fh = tp->ops->get(tp, t->tcm_handle);
2289
2290 if (!fh) {
Vlad Busloved76f5e2019-02-11 10:55:38 +02002291 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2292 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03002293 } else {
2294 bool last;
2295
2296 err = tfilter_del_notify(net, skb, n, tp, block,
2297 q, parent, fh, false, &last,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002298 rtnl_held, extack);
2299
Vlad Buslovc431f892018-05-31 09:52:53 +03002300 if (err)
2301 goto errout;
Vlad Buslov8b646782019-02-11 10:55:41 +02002302 if (last)
Vlad Buslov12db03b2019-02-11 10:55:45 +02002303 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002304 }
2305
2306errout:
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002307 if (chain) {
2308 if (tp && !IS_ERR(tp))
Vlad Buslov12db03b2019-02-11 10:55:45 +02002309 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslovc431f892018-05-31 09:52:53 +03002310 tcf_chain_put(chain);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002311 }
Vlad Buslov12db03b2019-02-11 10:55:45 +02002312 tcf_block_release(q, block, rtnl_held);
Vlad Buslov470502d2019-02-11 10:55:48 +02002313
2314 if (rtnl_held)
2315 rtnl_unlock();
2316
Vlad Buslovc431f892018-05-31 09:52:53 +03002317 return err;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002318
2319errout_locked:
2320 mutex_unlock(&chain->filter_chain_lock);
2321 goto errout;
Vlad Buslovc431f892018-05-31 09:52:53 +03002322}
2323
2324static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2325 struct netlink_ext_ack *extack)
2326{
2327 struct net *net = sock_net(skb->sk);
2328 struct nlattr *tca[TCA_MAX + 1];
Cong Wang6f96c3c2019-10-07 13:26:28 -07002329 char name[IFNAMSIZ];
Vlad Buslovc431f892018-05-31 09:52:53 +03002330 struct tcmsg *t;
2331 u32 protocol;
2332 u32 prio;
2333 u32 parent;
2334 u32 chain_index;
2335 struct Qdisc *q = NULL;
2336 struct tcf_chain_info chain_info;
2337 struct tcf_chain *chain = NULL;
Vlad Buslov470502d2019-02-11 10:55:48 +02002338 struct tcf_block *block = NULL;
Vlad Buslovc431f892018-05-31 09:52:53 +03002339 struct tcf_proto *tp = NULL;
2340 unsigned long cl = 0;
2341 void *fh = NULL;
2342 int err;
Vlad Buslov470502d2019-02-11 10:55:48 +02002343 bool rtnl_held = false;
Vlad Buslovc431f892018-05-31 09:52:53 +03002344
Johannes Berg8cb08172019-04-26 14:07:28 +02002345 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2346 rtm_tca_policy, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002347 if (err < 0)
2348 return err;
2349
2350 t = nlmsg_data(n);
2351 protocol = TC_H_MIN(t->tcm_info);
2352 prio = TC_H_MAJ(t->tcm_info);
2353 parent = t->tcm_parent;
2354
2355 if (prio == 0) {
2356 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2357 return -ENOENT;
2358 }
2359
2360 /* Find head of filter chain. */
2361
Vlad Buslov470502d2019-02-11 10:55:48 +02002362 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2363 if (err)
2364 return err;
2365
Cong Wang6f96c3c2019-10-07 13:26:28 -07002366 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2367 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2368 err = -EINVAL;
2369 goto errout;
2370 }
Vlad Buslov470502d2019-02-11 10:55:48 +02002371 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2372 * unlocked, classifier type is not specified, classifier is not
2373 * unlocked.
2374 */
2375 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
Cong Wang6f96c3c2019-10-07 13:26:28 -07002376 !tcf_proto_is_unlocked(name)) {
Vlad Buslov470502d2019-02-11 10:55:48 +02002377 rtnl_held = true;
2378 rtnl_lock();
2379 }
2380
2381 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2382 if (err)
2383 goto errout;
2384
2385 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2386 extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002387 if (IS_ERR(block)) {
2388 err = PTR_ERR(block);
2389 goto errout;
2390 }
2391
2392 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2393 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2394 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2395 err = -EINVAL;
2396 goto errout;
2397 }
2398 chain = tcf_chain_get(block, chain_index, false);
2399 if (!chain) {
2400 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2401 err = -EINVAL;
2402 goto errout;
2403 }
2404
Vlad Busloved76f5e2019-02-11 10:55:38 +02002405 mutex_lock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002406 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2407 prio, false);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002408 mutex_unlock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002409 if (!tp || IS_ERR(tp)) {
2410 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03002411 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03002412 goto errout;
2413 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2414 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2415 err = -EINVAL;
2416 goto errout;
2417 }
2418
2419 fh = tp->ops->get(tp, t->tcm_handle);
2420
2421 if (!fh) {
2422 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2423 err = -ENOENT;
2424 } else {
2425 err = tfilter_notify(net, skb, n, tp, block, q, parent,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002426 fh, RTM_NEWTFILTER, true, rtnl_held);
Vlad Buslovc431f892018-05-31 09:52:53 +03002427 if (err < 0)
2428 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2429 }
2430
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002431 tfilter_put(tp, fh);
Vlad Buslovc431f892018-05-31 09:52:53 +03002432errout:
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002433 if (chain) {
2434 if (tp && !IS_ERR(tp))
Vlad Buslov12db03b2019-02-11 10:55:45 +02002435 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslovc431f892018-05-31 09:52:53 +03002436 tcf_chain_put(chain);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002437 }
Vlad Buslov12db03b2019-02-11 10:55:45 +02002438 tcf_block_release(q, block, rtnl_held);
Vlad Buslov470502d2019-02-11 10:55:48 +02002439
2440 if (rtnl_held)
2441 rtnl_unlock();
2442
Vlad Buslovc431f892018-05-31 09:52:53 +03002443 return err;
2444}
2445
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002446struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 struct tcf_walker w;
2448 struct sk_buff *skb;
2449 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002450 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002451 struct Qdisc *q;
2452 u32 parent;
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002453 bool terse_dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454};
2455
WANG Cong8113c092017-08-04 21:31:43 -07002456static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002458 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08002459 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002461 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002462 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04002463 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002464 RTM_NEWTFILTER, a->terse_dump, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002467static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2468 struct sk_buff *skb, struct netlink_callback *cb,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002469 long index_start, long *p_index, bool terse)
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002470{
2471 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002472 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002473 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002474 struct tcf_proto *tp, *tp_prev;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002475 struct tcf_dump_args arg;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002476
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002477 for (tp = __tcf_get_next_proto(chain, NULL);
2478 tp;
2479 tp_prev = tp,
2480 tp = __tcf_get_next_proto(chain, tp),
Vlad Buslov12db03b2019-02-11 10:55:45 +02002481 tcf_proto_put(tp_prev, true, NULL),
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002482 (*p_index)++) {
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002483 if (*p_index < index_start)
2484 continue;
2485 if (TC_H_MAJ(tcm->tcm_info) &&
2486 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2487 continue;
2488 if (TC_H_MIN(tcm->tcm_info) &&
2489 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2490 continue;
2491 if (*p_index > index_start)
2492 memset(&cb->args[1], 0,
2493 sizeof(cb->args) - sizeof(cb->args[0]));
2494 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08002495 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002496 NETLINK_CB(cb->skb).portid,
2497 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002498 RTM_NEWTFILTER, false, true) <= 0)
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002499 goto errout;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002500 cb->args[1] = 1;
2501 }
2502 if (!tp->ops->walk)
2503 continue;
2504 arg.w.fn = tcf_node_dump;
2505 arg.skb = skb;
2506 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002507 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002508 arg.q = q;
2509 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002510 arg.w.stop = 0;
2511 arg.w.skip = cb->args[1] - 1;
2512 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03002513 arg.w.cookie = cb->args[2];
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002514 arg.terse_dump = terse;
Vlad Buslov12db03b2019-02-11 10:55:45 +02002515 tp->ops->walk(tp, &arg.w, true);
Vlad Buslov01683a12018-07-09 13:29:11 +03002516 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002517 cb->args[1] = arg.w.count + 1;
2518 if (arg.w.stop)
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002519 goto errout;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002520 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02002521 return true;
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002522
2523errout:
Vlad Buslov12db03b2019-02-11 10:55:45 +02002524 tcf_proto_put(tp, true, NULL);
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002525 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002526}
2527
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002528static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2529 [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2530};
2531
Eric Dumazetbd27a872009-11-05 20:57:26 -08002532/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2534{
Vlad Buslovbbf73832019-02-11 10:55:36 +02002535 struct tcf_chain *chain, *chain_prev;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002536 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02002537 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002538 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02002539 struct tcf_block *block;
David S. Miller942b8162012-06-26 21:48:50 -07002540 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002541 bool terse_dump = false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002542 long index_start;
2543 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002544 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002545 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Hong zhi guo573ce262013-03-27 06:47:04 +00002547 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002549
Johannes Berg8cb08172019-04-26 14:07:28 +02002550 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002551 tcf_tfilter_dump_policy, cb->extack);
Jiri Pirko5bc17012017-05-17 11:08:01 +02002552 if (err)
2553 return err;
2554
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002555 if (tca[TCA_DUMP_FLAGS]) {
2556 struct nla_bitfield32 flags =
2557 nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2558
2559 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2560 }
2561
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002562 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002563 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002564 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07002565 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01002566 /* If we work with block index, q is NULL and parent value
2567 * will never be used in the following code. The check
2568 * in tcf_fill_node prevents it. However, compiler does not
2569 * see that far, so set parent to zero to silence the warning
2570 * about parent being uninitialized.
2571 */
2572 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002573 } else {
2574 const struct Qdisc_class_ops *cops;
2575 struct net_device *dev;
2576 unsigned long cl = 0;
2577
2578 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2579 if (!dev)
2580 return skb->len;
2581
2582 parent = tcm->tcm_parent;
Cong Wanga7df4872020-04-30 20:53:49 -07002583 if (!parent)
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002584 q = dev->qdisc;
Cong Wanga7df4872020-04-30 20:53:49 -07002585 else
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002586 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002587 if (!q)
2588 goto out;
2589 cops = q->ops->cl_ops;
2590 if (!cops)
2591 goto out;
2592 if (!cops->tcf_block)
2593 goto out;
2594 if (TC_H_MIN(tcm->tcm_parent)) {
2595 cl = cops->find(q, tcm->tcm_parent);
2596 if (cl == 0)
2597 goto out;
2598 }
2599 block = cops->tcf_block(q, cl, NULL);
2600 if (!block)
2601 goto out;
Cong Wanga7df4872020-04-30 20:53:49 -07002602 parent = block->classid;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002603 if (tcf_block_shared(block))
2604 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002607 index_start = cb->args[0];
2608 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002609
Vlad Buslovbbf73832019-02-11 10:55:36 +02002610 for (chain = __tcf_get_next_chain(block, NULL);
2611 chain;
2612 chain_prev = chain,
2613 chain = __tcf_get_next_chain(block, chain),
2614 tcf_chain_put(chain_prev)) {
Jiri Pirko5bc17012017-05-17 11:08:01 +02002615 if (tca[TCA_CHAIN] &&
2616 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2617 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002618 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002619 index_start, &index, terse_dump)) {
Vlad Buslovbbf73832019-02-11 10:55:36 +02002620 tcf_chain_put(chain);
Roman Kapl5ae437a2018-02-19 21:32:51 +01002621 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002622 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01002623 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02002624 }
2625
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002626 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
Vlad Buslov12db03b2019-02-11 10:55:45 +02002627 tcf_block_refcnt_put(block, true);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002628 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01002631 /* If we did no progress, the error (EMSGSIZE) is real */
2632 if (skb->len == 0 && err)
2633 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return skb->len;
2635}
2636
Vlad Buslova5654822019-02-11 10:55:37 +02002637static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2638 void *tmplt_priv, u32 chain_index,
2639 struct net *net, struct sk_buff *skb,
2640 struct tcf_block *block,
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002641 u32 portid, u32 seq, u16 flags, int event)
2642{
2643 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02002644 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002645 struct nlmsghdr *nlh;
2646 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02002647 void *priv;
2648
Vlad Buslova5654822019-02-11 10:55:37 +02002649 ops = tmplt_ops;
2650 priv = tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002651
2652 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2653 if (!nlh)
2654 goto out_nlmsg_trim;
2655 tcm = nlmsg_data(nlh);
2656 tcm->tcm_family = AF_UNSPEC;
2657 tcm->tcm__pad1 = 0;
2658 tcm->tcm__pad2 = 0;
2659 tcm->tcm_handle = 0;
2660 if (block->q) {
2661 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2662 tcm->tcm_parent = block->q->handle;
2663 } else {
2664 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2665 tcm->tcm_block_index = block->index;
2666 }
2667
Vlad Buslova5654822019-02-11 10:55:37 +02002668 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002669 goto nla_put_failure;
2670
Jiri Pirko9f407f12018-07-23 09:23:07 +02002671 if (ops) {
2672 if (nla_put_string(skb, TCA_KIND, ops->kind))
2673 goto nla_put_failure;
2674 if (ops->tmplt_dump(skb, net, priv) < 0)
2675 goto nla_put_failure;
2676 }
2677
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002678 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2679 return skb->len;
2680
2681out_nlmsg_trim:
2682nla_put_failure:
2683 nlmsg_trim(skb, b);
2684 return -EMSGSIZE;
2685}
2686
2687static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2688 u32 seq, u16 flags, int event, bool unicast)
2689{
2690 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2691 struct tcf_block *block = chain->block;
2692 struct net *net = block->net;
2693 struct sk_buff *skb;
Zhike Wang5b5f99b2019-03-11 03:15:54 -07002694 int err = 0;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002695
2696 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2697 if (!skb)
2698 return -ENOBUFS;
2699
Vlad Buslova5654822019-02-11 10:55:37 +02002700 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2701 chain->index, net, skb, block, portid,
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002702 seq, flags, event) <= 0) {
2703 kfree_skb(skb);
2704 return -EINVAL;
2705 }
2706
2707 if (unicast)
Zhike Wang5b5f99b2019-03-11 03:15:54 -07002708 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2709 else
2710 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2711 flags & NLM_F_ECHO);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002712
Zhike Wang5b5f99b2019-03-11 03:15:54 -07002713 if (err > 0)
2714 err = 0;
2715 return err;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002716}
2717
Vlad Buslova5654822019-02-11 10:55:37 +02002718static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2719 void *tmplt_priv, u32 chain_index,
2720 struct tcf_block *block, struct sk_buff *oskb,
2721 u32 seq, u16 flags, bool unicast)
2722{
2723 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2724 struct net *net = block->net;
2725 struct sk_buff *skb;
2726
2727 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2728 if (!skb)
2729 return -ENOBUFS;
2730
2731 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2732 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2733 kfree_skb(skb);
2734 return -EINVAL;
2735 }
2736
2737 if (unicast)
2738 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2739
2740 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2741}
2742
Jiri Pirko9f407f12018-07-23 09:23:07 +02002743static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2744 struct nlattr **tca,
2745 struct netlink_ext_ack *extack)
2746{
2747 const struct tcf_proto_ops *ops;
Eric Dumazet2dd56162019-12-07 11:34:45 -08002748 char name[IFNAMSIZ];
Jiri Pirko9f407f12018-07-23 09:23:07 +02002749 void *tmplt_priv;
2750
2751 /* If kind is not set, user did not specify template. */
2752 if (!tca[TCA_KIND])
2753 return 0;
2754
Eric Dumazet2dd56162019-12-07 11:34:45 -08002755 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2756 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2757 return -EINVAL;
2758 }
2759
2760 ops = tcf_proto_lookup_ops(name, true, extack);
Jiri Pirko9f407f12018-07-23 09:23:07 +02002761 if (IS_ERR(ops))
2762 return PTR_ERR(ops);
2763 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2764 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2765 return -EOPNOTSUPP;
2766 }
2767
2768 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2769 if (IS_ERR(tmplt_priv)) {
2770 module_put(ops->owner);
2771 return PTR_ERR(tmplt_priv);
2772 }
2773 chain->tmplt_ops = ops;
2774 chain->tmplt_priv = tmplt_priv;
2775 return 0;
2776}
2777
Vlad Buslova5654822019-02-11 10:55:37 +02002778static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2779 void *tmplt_priv)
Jiri Pirko9f407f12018-07-23 09:23:07 +02002780{
Jiri Pirko9f407f12018-07-23 09:23:07 +02002781 /* If template ops are set, no work to do for us. */
Vlad Buslova5654822019-02-11 10:55:37 +02002782 if (!tmplt_ops)
Jiri Pirko9f407f12018-07-23 09:23:07 +02002783 return;
2784
Vlad Buslova5654822019-02-11 10:55:37 +02002785 tmplt_ops->tmplt_destroy(tmplt_priv);
2786 module_put(tmplt_ops->owner);
Jiri Pirko9f407f12018-07-23 09:23:07 +02002787}
2788
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002789/* Add/delete/get a chain */
2790
2791static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2792 struct netlink_ext_ack *extack)
2793{
2794 struct net *net = sock_net(skb->sk);
2795 struct nlattr *tca[TCA_MAX + 1];
2796 struct tcmsg *t;
2797 u32 parent;
2798 u32 chain_index;
2799 struct Qdisc *q = NULL;
2800 struct tcf_chain *chain = NULL;
2801 struct tcf_block *block;
2802 unsigned long cl;
2803 int err;
2804
2805 if (n->nlmsg_type != RTM_GETCHAIN &&
2806 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2807 return -EPERM;
2808
2809replay:
Johannes Berg8cb08172019-04-26 14:07:28 +02002810 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2811 rtm_tca_policy, extack);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002812 if (err < 0)
2813 return err;
2814
2815 t = nlmsg_data(n);
2816 parent = t->tcm_parent;
2817 cl = 0;
2818
2819 block = tcf_block_find(net, &q, &parent, &cl,
2820 t->tcm_ifindex, t->tcm_block_index, extack);
2821 if (IS_ERR(block))
2822 return PTR_ERR(block);
2823
2824 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2825 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2826 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002827 err = -EINVAL;
2828 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002829 }
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002830
2831 mutex_lock(&block->lock);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002832 chain = tcf_chain_lookup(block, chain_index);
2833 if (n->nlmsg_type == RTM_NEWCHAIN) {
2834 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002835 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002836 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002837 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002838 */
2839 tcf_chain_hold(chain);
2840 } else {
2841 NL_SET_ERR_MSG(extack, "Filter chain already exists");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002842 err = -EEXIST;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002843 goto errout_block_locked;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002844 }
2845 } else {
2846 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2847 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 +03002848 err = -ENOENT;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002849 goto errout_block_locked;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002850 }
2851 chain = tcf_chain_create(block, chain_index);
2852 if (!chain) {
2853 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002854 err = -ENOMEM;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002855 goto errout_block_locked;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002856 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002857 }
2858 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002859 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002860 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002861 err = -EINVAL;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002862 goto errout_block_locked;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002863 }
2864 tcf_chain_hold(chain);
2865 }
2866
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002867 if (n->nlmsg_type == RTM_NEWCHAIN) {
2868 /* Modifying chain requires holding parent block lock. In case
2869 * the chain was successfully added, take a reference to the
2870 * chain. This ensures that an empty chain does not disappear at
2871 * the end of this function.
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002872 */
2873 tcf_chain_hold(chain);
2874 chain->explicitly_created = true;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002875 }
2876 mutex_unlock(&block->lock);
2877
2878 switch (n->nlmsg_type) {
2879 case RTM_NEWCHAIN:
2880 err = tc_chain_tmplt_add(chain, net, tca, extack);
2881 if (err) {
2882 tcf_chain_put_explicitly_created(chain);
2883 goto errout;
2884 }
2885
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002886 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2887 RTM_NEWCHAIN, false);
2888 break;
2889 case RTM_DELCHAIN:
Cong Wangf5b9bac2018-09-11 14:22:23 -07002890 tfilter_notify_chain(net, skb, block, q, parent, n,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002891 chain, RTM_DELTFILTER, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002892 /* Flush the chain first as the user requested chain removal. */
Vlad Buslov12db03b2019-02-11 10:55:45 +02002893 tcf_chain_flush(chain, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002894 /* In case the chain was successfully deleted, put a reference
2895 * to the chain previously taken during addition.
2896 */
2897 tcf_chain_put_explicitly_created(chain);
2898 break;
2899 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002900 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
2901 n->nlmsg_seq, n->nlmsg_type, true);
2902 if (err < 0)
2903 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2904 break;
2905 default:
2906 err = -EOPNOTSUPP;
2907 NL_SET_ERR_MSG(extack, "Unsupported message type");
2908 goto errout;
2909 }
2910
2911errout:
2912 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03002913errout_block:
Vlad Buslov12db03b2019-02-11 10:55:45 +02002914 tcf_block_release(q, block, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002915 if (err == -EAGAIN)
2916 /* Replay the request. */
2917 goto replay;
2918 return err;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002919
2920errout_block_locked:
2921 mutex_unlock(&block->lock);
2922 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002923}
2924
2925/* called with RTNL */
2926static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2927{
2928 struct net *net = sock_net(skb->sk);
2929 struct nlattr *tca[TCA_MAX + 1];
2930 struct Qdisc *q = NULL;
2931 struct tcf_block *block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002932 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Vlad Buslovace4a262019-02-25 17:45:44 +02002933 struct tcf_chain *chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002934 long index_start;
2935 long index;
2936 u32 parent;
2937 int err;
2938
2939 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2940 return skb->len;
2941
Johannes Berg8cb08172019-04-26 14:07:28 +02002942 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2943 rtm_tca_policy, cb->extack);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002944 if (err)
2945 return err;
2946
2947 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002948 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002949 if (!block)
2950 goto out;
2951 /* If we work with block index, q is NULL and parent value
2952 * will never be used in the following code. The check
2953 * in tcf_fill_node prevents it. However, compiler does not
2954 * see that far, so set parent to zero to silence the warning
2955 * about parent being uninitialized.
2956 */
2957 parent = 0;
2958 } else {
2959 const struct Qdisc_class_ops *cops;
2960 struct net_device *dev;
2961 unsigned long cl = 0;
2962
2963 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2964 if (!dev)
2965 return skb->len;
2966
2967 parent = tcm->tcm_parent;
2968 if (!parent) {
2969 q = dev->qdisc;
2970 parent = q->handle;
2971 } else {
2972 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2973 }
2974 if (!q)
2975 goto out;
2976 cops = q->ops->cl_ops;
2977 if (!cops)
2978 goto out;
2979 if (!cops->tcf_block)
2980 goto out;
2981 if (TC_H_MIN(tcm->tcm_parent)) {
2982 cl = cops->find(q, tcm->tcm_parent);
2983 if (cl == 0)
2984 goto out;
2985 }
2986 block = cops->tcf_block(q, cl, NULL);
2987 if (!block)
2988 goto out;
2989 if (tcf_block_shared(block))
2990 q = NULL;
2991 }
2992
2993 index_start = cb->args[0];
2994 index = 0;
2995
Vlad Buslovace4a262019-02-25 17:45:44 +02002996 mutex_lock(&block->lock);
2997 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002998 if ((tca[TCA_CHAIN] &&
2999 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3000 continue;
3001 if (index < index_start) {
3002 index++;
3003 continue;
3004 }
Vlad Buslovace4a262019-02-25 17:45:44 +02003005 if (tcf_chain_held_by_acts_only(chain))
3006 continue;
Vlad Buslova5654822019-02-11 10:55:37 +02003007 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3008 chain->index, net, skb, block,
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003009 NETLINK_CB(cb->skb).portid,
3010 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3011 RTM_NEWCHAIN);
Vlad Buslovace4a262019-02-25 17:45:44 +02003012 if (err <= 0)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003013 break;
3014 index++;
3015 }
Vlad Buslovace4a262019-02-25 17:45:44 +02003016 mutex_unlock(&block->lock);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003017
Vlad Buslov787ce6d2018-09-24 19:22:58 +03003018 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
Vlad Buslov12db03b2019-02-11 10:55:45 +02003019 tcf_block_refcnt_put(block, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003020 cb->args[0] = index;
3021
3022out:
3023 /* If we did no progress, the error (EMSGSIZE) is real */
3024 if (skb->len == 0 && err)
3025 return err;
3026 return skb->len;
3027}
3028
WANG Cong18d02642014-09-25 10:26:37 -07003029void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
3031#ifdef CONFIG_NET_CLS_ACT
Eric Dumazet3d66b892019-09-18 12:57:04 -07003032 if (exts->actions) {
3033 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3034 kfree(exts->actions);
3035 }
WANG Cong22dc13c2016-08-13 22:35:00 -07003036 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037#endif
3038}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003039EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00003041int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05003042 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
Vlad Buslovec6743a2019-02-11 10:55:43 +02003043 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045#ifdef CONFIG_NET_CLS_ACT
3046 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05003048 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
WANG Cong5da57f42013-12-15 20:15:07 -08003050 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02003051 act = tcf_action_init_1(net, tp, tb[exts->police],
3052 rate_tlv, "police", ovr,
Vlad Buslovec6743a2019-02-11 10:55:43 +02003053 TCA_ACT_BIND, rtnl_held,
3054 extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08003055 if (IS_ERR(act))
3056 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
WANG Cong33be6272013-12-15 20:15:05 -08003058 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07003059 exts->actions[0] = act;
3060 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08003061 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03003062 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07003063
Jiri Pirko9fb9f252017-05-17 11:08:02 +02003064 err = tcf_action_init(net, tp, tb[exts->action],
3065 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslovec6743a2019-02-11 10:55:43 +02003066 exts->actions, &attr_size,
3067 rtnl_held, extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03003068 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08003069 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03003070 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
3072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073#else
WANG Cong5da57f42013-12-15 20:15:07 -08003074 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05003075 (exts->police && tb[exts->police])) {
3076 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05003078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079#endif
3080
3081 return 0;
3082}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003083EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Jiri Pirko9b0d4442017-08-04 14:29:15 +02003085void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
3087#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07003088 struct tcf_exts old = *dst;
3089
Jiri Pirko9b0d4442017-08-04 14:29:15 +02003090 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07003091 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092#endif
3093}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003094EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
WANG Cong22dc13c2016-08-13 22:35:00 -07003096#ifdef CONFIG_NET_CLS_ACT
3097static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3098{
3099 if (exts->nr_actions == 0)
3100 return NULL;
3101 else
3102 return exts->actions[0];
3103}
3104#endif
WANG Cong33be6272013-12-15 20:15:05 -08003105
WANG Cong5da57f42013-12-15 20:15:07 -08003106int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07003109 struct nlattr *nest;
3110
Jiri Pirko978dfd82017-08-04 14:29:03 +02003111 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 /*
3113 * again for backward compatible mode - we want
3114 * to work with both old and new modes of entering
3115 * tc data even if iproute2 was newer - jhs
3116 */
WANG Cong33be6272013-12-15 20:15:05 -08003117 if (exts->type != TCA_OLD_COMPAT) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003118 nest = nla_nest_start_noflag(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003119 if (nest == NULL)
3120 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07003121
Vlad Buslovca44b732020-05-15 14:40:12 +03003122 if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3123 < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08003124 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003125 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08003126 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08003127 struct tc_action *act = tcf_exts_first_act(exts);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003128 nest = nla_nest_start_noflag(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05003129 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003130 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08003131 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08003132 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003133 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
3135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07003137
3138nla_put_failure:
3139 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07003141#else
3142 return 0;
3143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003145EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Vlad Buslovca44b732020-05-15 14:40:12 +03003147int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3148{
3149#ifdef CONFIG_NET_CLS_ACT
3150 struct nlattr *nest;
3151
3152 if (!exts->action || !tcf_exts_has_actions(exts))
3153 return 0;
3154
3155 nest = nla_nest_start_noflag(skb, exts->action);
3156 if (!nest)
3157 goto nla_put_failure;
3158
3159 if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3160 goto nla_put_failure;
3161 nla_nest_end(skb, nest);
3162 return 0;
3163
3164nla_put_failure:
3165 nla_nest_cancel(skb, nest);
3166 return -1;
3167#else
3168 return 0;
3169#endif
3170}
3171EXPORT_SYMBOL(tcf_exts_terse_dump);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003172
WANG Cong5da57f42013-12-15 20:15:07 -08003173int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
3175#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08003176 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01003177 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08003178 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179#endif
3180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003182EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Vlad Buslov40119212019-08-26 16:44:59 +03003184static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3185{
3186 if (*flags & TCA_CLS_FLAGS_IN_HW)
3187 return;
3188 *flags |= TCA_CLS_FLAGS_IN_HW;
3189 atomic_inc(&block->offloadcnt);
3190}
3191
3192static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3193{
3194 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3195 return;
3196 *flags &= ~TCA_CLS_FLAGS_IN_HW;
3197 atomic_dec(&block->offloadcnt);
3198}
3199
3200static void tc_cls_offload_cnt_update(struct tcf_block *block,
3201 struct tcf_proto *tp, u32 *cnt,
3202 u32 *flags, u32 diff, bool add)
3203{
3204 lockdep_assert_held(&block->cb_lock);
3205
3206 spin_lock(&tp->lock);
3207 if (add) {
3208 if (!*cnt)
3209 tcf_block_offload_inc(block, flags);
3210 *cnt += diff;
3211 } else {
3212 *cnt -= diff;
3213 if (!*cnt)
3214 tcf_block_offload_dec(block, flags);
3215 }
3216 spin_unlock(&tp->lock);
3217}
3218
3219static void
3220tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3221 u32 *cnt, u32 *flags)
3222{
3223 lockdep_assert_held(&block->cb_lock);
3224
3225 spin_lock(&tp->lock);
3226 tcf_block_offload_dec(block, flags);
3227 *cnt = 0;
3228 spin_unlock(&tp->lock);
3229}
3230
3231static int
3232__tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3233 void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02003234{
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02003235 struct flow_block_cb *block_cb;
Cong Wangaeb3fec2018-12-11 11:15:46 -08003236 int ok_count = 0;
3237 int err;
3238
Vlad Buslov40119212019-08-26 16:44:59 +03003239 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3240 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3241 if (err) {
3242 if (err_stop)
3243 return err;
3244 } else {
3245 ok_count++;
3246 }
3247 }
3248 return ok_count;
3249}
3250
3251int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3252 void *type_data, bool err_stop, bool rtnl_held)
3253{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003254 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003255 int ok_count;
3256
Vlad Buslov11bd6342019-08-26 16:45:02 +03003257retry:
3258 if (take_rtnl)
3259 rtnl_lock();
Vlad Buslov40119212019-08-26 16:44:59 +03003260 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003261 /* Need to obtain rtnl lock if block is bound to devs that require it.
3262 * In block bind code cb_lock is obtained while holding rtnl, so we must
3263 * obtain the locks in same order here.
3264 */
3265 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3266 up_read(&block->cb_lock);
3267 take_rtnl = true;
3268 goto retry;
3269 }
3270
Vlad Buslov40119212019-08-26 16:44:59 +03003271 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003272
Vlad Buslov40119212019-08-26 16:44:59 +03003273 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003274 if (take_rtnl)
3275 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003276 return ok_count;
3277}
3278EXPORT_SYMBOL(tc_setup_cb_call);
3279
3280/* Non-destructive filter add. If filter that wasn't already in hardware is
3281 * successfully offloaded, increment block offloads counter. On failure,
3282 * previously offloaded filter is considered to be intact and offloads counter
3283 * is not decremented.
3284 */
3285
3286int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3287 enum tc_setup_type type, void *type_data, bool err_stop,
3288 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3289{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003290 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003291 int ok_count;
3292
Vlad Buslov11bd6342019-08-26 16:45:02 +03003293retry:
3294 if (take_rtnl)
3295 rtnl_lock();
Vlad Buslov4f8116c2019-08-26 16:44:57 +03003296 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003297 /* Need to obtain rtnl lock if block is bound to devs that require it.
3298 * In block bind code cb_lock is obtained while holding rtnl, so we must
3299 * obtain the locks in same order here.
3300 */
3301 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3302 up_read(&block->cb_lock);
3303 take_rtnl = true;
3304 goto retry;
3305 }
3306
Cong Wangaeb3fec2018-12-11 11:15:46 -08003307 /* Make sure all netdevs sharing this block are offload-capable. */
Vlad Buslov4f8116c2019-08-26 16:44:57 +03003308 if (block->nooffloaddevcnt && err_stop) {
3309 ok_count = -EOPNOTSUPP;
3310 goto err_unlock;
3311 }
Cong Wangaeb3fec2018-12-11 11:15:46 -08003312
Vlad Buslov40119212019-08-26 16:44:59 +03003313 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003314 if (ok_count < 0)
3315 goto err_unlock;
3316
3317 if (tp->ops->hw_add)
3318 tp->ops->hw_add(tp, type_data);
Vlad Buslov40119212019-08-26 16:44:59 +03003319 if (ok_count > 0)
3320 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3321 ok_count, true);
Vlad Buslov4f8116c2019-08-26 16:44:57 +03003322err_unlock:
3323 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003324 if (take_rtnl)
3325 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003326 return ok_count < 0 ? ok_count : 0;
Jiri Pirko717503b2017-10-11 09:41:09 +02003327}
Vlad Buslov40119212019-08-26 16:44:59 +03003328EXPORT_SYMBOL(tc_setup_cb_add);
3329
3330/* Destructive filter replace. If filter that wasn't already in hardware is
3331 * successfully offloaded, increment block offload counter. On failure,
3332 * previously offloaded filter is considered to be destroyed and offload counter
3333 * is decremented.
3334 */
3335
3336int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3337 enum tc_setup_type type, void *type_data, bool err_stop,
3338 u32 *old_flags, unsigned int *old_in_hw_count,
3339 u32 *new_flags, unsigned int *new_in_hw_count,
3340 bool rtnl_held)
3341{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003342 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003343 int ok_count;
3344
Vlad Buslov11bd6342019-08-26 16:45:02 +03003345retry:
3346 if (take_rtnl)
3347 rtnl_lock();
Vlad Buslov40119212019-08-26 16:44:59 +03003348 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003349 /* Need to obtain rtnl lock if block is bound to devs that require it.
3350 * In block bind code cb_lock is obtained while holding rtnl, so we must
3351 * obtain the locks in same order here.
3352 */
3353 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3354 up_read(&block->cb_lock);
3355 take_rtnl = true;
3356 goto retry;
3357 }
3358
Vlad Buslov40119212019-08-26 16:44:59 +03003359 /* Make sure all netdevs sharing this block are offload-capable. */
3360 if (block->nooffloaddevcnt && err_stop) {
3361 ok_count = -EOPNOTSUPP;
3362 goto err_unlock;
3363 }
3364
3365 tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003366 if (tp->ops->hw_del)
3367 tp->ops->hw_del(tp, type_data);
Vlad Buslov40119212019-08-26 16:44:59 +03003368
3369 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003370 if (ok_count < 0)
3371 goto err_unlock;
3372
3373 if (tp->ops->hw_add)
3374 tp->ops->hw_add(tp, type_data);
Vlad Buslov40119212019-08-26 16:44:59 +03003375 if (ok_count > 0)
Vlad Buslova449a3e2019-08-26 16:45:00 +03003376 tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3377 new_flags, ok_count, true);
Vlad Buslov40119212019-08-26 16:44:59 +03003378err_unlock:
3379 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003380 if (take_rtnl)
3381 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003382 return ok_count < 0 ? ok_count : 0;
3383}
3384EXPORT_SYMBOL(tc_setup_cb_replace);
3385
3386/* Destroy filter and decrement block offload counter, if filter was previously
3387 * offloaded.
3388 */
3389
3390int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3391 enum tc_setup_type type, void *type_data, bool err_stop,
3392 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3393{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003394 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003395 int ok_count;
3396
Vlad Buslov11bd6342019-08-26 16:45:02 +03003397retry:
3398 if (take_rtnl)
3399 rtnl_lock();
Vlad Buslov40119212019-08-26 16:44:59 +03003400 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003401 /* Need to obtain rtnl lock if block is bound to devs that require it.
3402 * In block bind code cb_lock is obtained while holding rtnl, so we must
3403 * obtain the locks in same order here.
3404 */
3405 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3406 up_read(&block->cb_lock);
3407 take_rtnl = true;
3408 goto retry;
3409 }
3410
Vlad Buslov40119212019-08-26 16:44:59 +03003411 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3412
3413 tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003414 if (tp->ops->hw_del)
3415 tp->ops->hw_del(tp, type_data);
3416
Vlad Buslov40119212019-08-26 16:44:59 +03003417 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003418 if (take_rtnl)
3419 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003420 return ok_count < 0 ? ok_count : 0;
3421}
3422EXPORT_SYMBOL(tc_setup_cb_destroy);
3423
3424int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3425 bool add, flow_setup_cb_t *cb,
3426 enum tc_setup_type type, void *type_data,
3427 void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3428{
3429 int err = cb(type, type_data, cb_priv);
3430
3431 if (err) {
3432 if (add && tc_skip_sw(*flags))
3433 return err;
3434 } else {
3435 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3436 add);
3437 }
3438
3439 return 0;
3440}
3441EXPORT_SYMBOL(tc_setup_cb_reoffload);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02003442
Jiri Pirko20084952020-02-25 11:45:18 +01003443static int tcf_act_get_cookie(struct flow_action_entry *entry,
3444 const struct tc_action *act)
3445{
3446 struct tc_cookie *cookie;
3447 int err = 0;
3448
3449 rcu_read_lock();
3450 cookie = rcu_dereference(act->act_cookie);
3451 if (cookie) {
3452 entry->cookie = flow_action_cookie_create(cookie->data,
3453 cookie->len,
3454 GFP_ATOMIC);
3455 if (!entry->cookie)
3456 err = -ENOMEM;
3457 }
3458 rcu_read_unlock();
3459 return err;
3460}
3461
3462static void tcf_act_put_cookie(struct flow_action_entry *entry)
3463{
3464 flow_action_cookie_destroy(entry->cookie);
3465}
3466
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03003467void tc_cleanup_flow_action(struct flow_action *flow_action)
3468{
3469 struct flow_action_entry *entry;
3470 int i;
3471
Jiri Pirko20084952020-02-25 11:45:18 +01003472 flow_action_for_each(i, entry, flow_action) {
3473 tcf_act_put_cookie(entry);
Vlad Buslov11589582019-09-13 18:28:39 +03003474 if (entry->destructor)
3475 entry->destructor(entry->destructor_priv);
Jiri Pirko20084952020-02-25 11:45:18 +01003476 }
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03003477}
3478EXPORT_SYMBOL(tc_cleanup_flow_action);
3479
Vlad Buslov11589582019-09-13 18:28:39 +03003480static void tcf_mirred_get_dev(struct flow_action_entry *entry,
3481 const struct tc_action *act)
3482{
Vlad Buslov470d5062019-09-13 18:28:41 +03003483#ifdef CONFIG_NET_CLS_ACT
3484 entry->dev = act->ops->get_dev(act, &entry->destructor);
Vlad Buslov11589582019-09-13 18:28:39 +03003485 if (!entry->dev)
3486 return;
Vlad Buslov11589582019-09-13 18:28:39 +03003487 entry->destructor_priv = entry->dev;
Vlad Buslov470d5062019-09-13 18:28:41 +03003488#endif
Vlad Buslov11589582019-09-13 18:28:39 +03003489}
3490
3491static void tcf_tunnel_encap_put_tunnel(void *priv)
3492{
3493 struct ip_tunnel_info *tunnel = priv;
3494
3495 kfree(tunnel);
3496}
3497
3498static int tcf_tunnel_encap_get_tunnel(struct flow_action_entry *entry,
3499 const struct tc_action *act)
3500{
3501 entry->tunnel = tcf_tunnel_info_copy(act);
3502 if (!entry->tunnel)
3503 return -ENOMEM;
3504 entry->destructor = tcf_tunnel_encap_put_tunnel;
3505 entry->destructor_priv = entry->tunnel;
3506 return 0;
3507}
3508
Vlad Buslov4a5da472019-09-13 18:28:40 +03003509static void tcf_sample_get_group(struct flow_action_entry *entry,
3510 const struct tc_action *act)
3511{
3512#ifdef CONFIG_NET_CLS_ACT
3513 entry->sample.psample_group =
3514 act->ops->get_psample_group(act, &entry->destructor);
3515 entry->destructor_priv = entry->sample.psample_group;
3516#endif
3517}
3518
Po Liud29bdd62020-05-01 08:53:16 +08003519static void tcf_gate_entry_destructor(void *priv)
3520{
3521 struct action_gate_entry *oe = priv;
3522
3523 kfree(oe);
3524}
3525
3526static int tcf_gate_get_entries(struct flow_action_entry *entry,
3527 const struct tc_action *act)
3528{
3529 entry->gate.entries = tcf_gate_get_list(act);
3530
3531 if (!entry->gate.entries)
3532 return -EINVAL;
3533
3534 entry->destructor = tcf_gate_entry_destructor;
3535 entry->destructor_priv = entry->gate.entries;
3536
3537 return 0;
3538}
3539
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +02003540static enum flow_action_hw_stats tc_act_hw_stats(u8 hw_stats)
3541{
3542 if (WARN_ON_ONCE(hw_stats > TCA_ACT_HW_STATS_ANY))
3543 return FLOW_ACTION_HW_STATS_DONT_CARE;
3544 else if (!hw_stats)
3545 return FLOW_ACTION_HW_STATS_DISABLED;
3546
3547 return hw_stats;
3548}
3549
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003550int tc_setup_flow_action(struct flow_action *flow_action,
Vlad Buslovb15e7a62020-02-17 12:12:12 +02003551 const struct tcf_exts *exts)
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003552{
Vlad Buslov7a472812020-02-17 12:12:09 +02003553 struct tc_action *act;
Vlad Buslov9838b202019-08-26 16:45:03 +03003554 int i, j, k, err = 0;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003555
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07003556 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3557 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3558 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
Jiri Pirko44f86582020-03-07 12:40:20 +01003559
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003560 if (!exts)
3561 return 0;
3562
3563 j = 0;
3564 tcf_exts_for_each_action(i, act, exts) {
3565 struct flow_action_entry *entry;
3566
3567 entry = &flow_action->entries[j];
Vlad Buslov7a472812020-02-17 12:12:09 +02003568 spin_lock_bh(&act->tcfa_lock);
Jiri Pirko20084952020-02-25 11:45:18 +01003569 err = tcf_act_get_cookie(entry, act);
3570 if (err)
3571 goto err_out_locked;
Jiri Pirko44f86582020-03-07 12:40:20 +01003572
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +02003573 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
Jiri Pirko44f86582020-03-07 12:40:20 +01003574
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003575 if (is_tcf_gact_ok(act)) {
3576 entry->id = FLOW_ACTION_ACCEPT;
3577 } else if (is_tcf_gact_shot(act)) {
3578 entry->id = FLOW_ACTION_DROP;
3579 } else if (is_tcf_gact_trap(act)) {
3580 entry->id = FLOW_ACTION_TRAP;
3581 } else if (is_tcf_gact_goto_chain(act)) {
3582 entry->id = FLOW_ACTION_GOTO;
3583 entry->chain_index = tcf_gact_goto_chain_index(act);
3584 } else if (is_tcf_mirred_egress_redirect(act)) {
3585 entry->id = FLOW_ACTION_REDIRECT;
Vlad Buslov11589582019-09-13 18:28:39 +03003586 tcf_mirred_get_dev(entry, act);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003587 } else if (is_tcf_mirred_egress_mirror(act)) {
3588 entry->id = FLOW_ACTION_MIRRED;
Vlad Buslov11589582019-09-13 18:28:39 +03003589 tcf_mirred_get_dev(entry, act);
John Hurley48e584a2019-08-04 16:09:06 +01003590 } else if (is_tcf_mirred_ingress_redirect(act)) {
3591 entry->id = FLOW_ACTION_REDIRECT_INGRESS;
Vlad Buslov11589582019-09-13 18:28:39 +03003592 tcf_mirred_get_dev(entry, act);
John Hurley48e584a2019-08-04 16:09:06 +01003593 } else if (is_tcf_mirred_ingress_mirror(act)) {
3594 entry->id = FLOW_ACTION_MIRRED_INGRESS;
Vlad Buslov11589582019-09-13 18:28:39 +03003595 tcf_mirred_get_dev(entry, act);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003596 } else if (is_tcf_vlan(act)) {
3597 switch (tcf_vlan_action(act)) {
3598 case TCA_VLAN_ACT_PUSH:
3599 entry->id = FLOW_ACTION_VLAN_PUSH;
3600 entry->vlan.vid = tcf_vlan_push_vid(act);
3601 entry->vlan.proto = tcf_vlan_push_proto(act);
3602 entry->vlan.prio = tcf_vlan_push_prio(act);
3603 break;
3604 case TCA_VLAN_ACT_POP:
3605 entry->id = FLOW_ACTION_VLAN_POP;
3606 break;
3607 case TCA_VLAN_ACT_MODIFY:
3608 entry->id = FLOW_ACTION_VLAN_MANGLE;
3609 entry->vlan.vid = tcf_vlan_push_vid(act);
3610 entry->vlan.proto = tcf_vlan_push_proto(act);
3611 entry->vlan.prio = tcf_vlan_push_prio(act);
3612 break;
3613 default:
Vlad Buslov9838b202019-08-26 16:45:03 +03003614 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003615 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003616 }
3617 } else if (is_tcf_tunnel_set(act)) {
3618 entry->id = FLOW_ACTION_TUNNEL_ENCAP;
Vlad Buslov11589582019-09-13 18:28:39 +03003619 err = tcf_tunnel_encap_get_tunnel(entry, act);
3620 if (err)
Vlad Buslov7a472812020-02-17 12:12:09 +02003621 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003622 } else if (is_tcf_tunnel_release(act)) {
3623 entry->id = FLOW_ACTION_TUNNEL_DECAP;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003624 } else if (is_tcf_pedit(act)) {
3625 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
3626 switch (tcf_pedit_cmd(act, k)) {
3627 case TCA_PEDIT_KEY_EX_CMD_SET:
3628 entry->id = FLOW_ACTION_MANGLE;
3629 break;
3630 case TCA_PEDIT_KEY_EX_CMD_ADD:
3631 entry->id = FLOW_ACTION_ADD;
3632 break;
3633 default:
Vlad Buslov9838b202019-08-26 16:45:03 +03003634 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003635 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003636 }
3637 entry->mangle.htype = tcf_pedit_htype(act, k);
3638 entry->mangle.mask = tcf_pedit_mask(act, k);
3639 entry->mangle.val = tcf_pedit_val(act, k);
3640 entry->mangle.offset = tcf_pedit_offset(act, k);
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +02003641 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
Petr Machata2c4b58d2020-03-18 19:42:29 +02003642 entry = &flow_action->entries[++j];
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003643 }
3644 } else if (is_tcf_csum(act)) {
3645 entry->id = FLOW_ACTION_CSUM;
3646 entry->csum_flags = tcf_csum_update_flags(act);
3647 } else if (is_tcf_skbedit_mark(act)) {
3648 entry->id = FLOW_ACTION_MARK;
3649 entry->mark = tcf_skbedit_mark(act);
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -07003650 } else if (is_tcf_sample(act)) {
3651 entry->id = FLOW_ACTION_SAMPLE;
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -07003652 entry->sample.trunc_size = tcf_sample_trunc_size(act);
3653 entry->sample.truncate = tcf_sample_truncate(act);
3654 entry->sample.rate = tcf_sample_rate(act);
Vlad Buslov4a5da472019-09-13 18:28:40 +03003655 tcf_sample_get_group(entry, act);
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -07003656 } else if (is_tcf_police(act)) {
3657 entry->id = FLOW_ACTION_POLICE;
3658 entry->police.burst = tcf_police_tcfp_burst(act);
3659 entry->police.rate_bytes_ps =
3660 tcf_police_rate_bytes_ps(act);
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03003661 } else if (is_tcf_ct(act)) {
3662 entry->id = FLOW_ACTION_CT;
3663 entry->ct.action = tcf_ct_action(act);
3664 entry->ct.zone = tcf_ct_zone(act);
Paul Blakeyedd58612020-03-12 12:23:09 +02003665 entry->ct.flow_table = tcf_ct_ft(act);
John Hurley6749d5902019-07-23 15:33:59 +01003666 } else if (is_tcf_mpls(act)) {
3667 switch (tcf_mpls_action(act)) {
3668 case TCA_MPLS_ACT_PUSH:
3669 entry->id = FLOW_ACTION_MPLS_PUSH;
3670 entry->mpls_push.proto = tcf_mpls_proto(act);
3671 entry->mpls_push.label = tcf_mpls_label(act);
3672 entry->mpls_push.tc = tcf_mpls_tc(act);
3673 entry->mpls_push.bos = tcf_mpls_bos(act);
3674 entry->mpls_push.ttl = tcf_mpls_ttl(act);
3675 break;
3676 case TCA_MPLS_ACT_POP:
3677 entry->id = FLOW_ACTION_MPLS_POP;
3678 entry->mpls_pop.proto = tcf_mpls_proto(act);
3679 break;
3680 case TCA_MPLS_ACT_MODIFY:
3681 entry->id = FLOW_ACTION_MPLS_MANGLE;
3682 entry->mpls_mangle.label = tcf_mpls_label(act);
3683 entry->mpls_mangle.tc = tcf_mpls_tc(act);
3684 entry->mpls_mangle.bos = tcf_mpls_bos(act);
3685 entry->mpls_mangle.ttl = tcf_mpls_ttl(act);
3686 break;
3687 default:
Vlad Buslov7a472812020-02-17 12:12:09 +02003688 goto err_out_locked;
John Hurley6749d5902019-07-23 15:33:59 +01003689 }
John Hurleyfb1b7752019-08-04 16:09:04 +01003690 } else if (is_tcf_skbedit_ptype(act)) {
3691 entry->id = FLOW_ACTION_PTYPE;
3692 entry->ptype = tcf_skbedit_ptype(act);
Petr Machata2ce12412020-03-19 15:47:21 +02003693 } else if (is_tcf_skbedit_priority(act)) {
3694 entry->id = FLOW_ACTION_PRIORITY;
3695 entry->priority = tcf_skbedit_priority(act);
Po Liud29bdd62020-05-01 08:53:16 +08003696 } else if (is_tcf_gate(act)) {
3697 entry->id = FLOW_ACTION_GATE;
3698 entry->gate.index = tcf_gate_index(act);
3699 entry->gate.prio = tcf_gate_prio(act);
3700 entry->gate.basetime = tcf_gate_basetime(act);
3701 entry->gate.cycletime = tcf_gate_cycletime(act);
3702 entry->gate.cycletimeext = tcf_gate_cycletimeext(act);
3703 entry->gate.num_entries = tcf_gate_num_entries(act);
3704 err = tcf_gate_get_entries(entry, act);
3705 if (err)
3706 goto err_out;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003707 } else {
Vlad Buslov9838b202019-08-26 16:45:03 +03003708 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003709 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003710 }
Vlad Buslov7a472812020-02-17 12:12:09 +02003711 spin_unlock_bh(&act->tcfa_lock);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003712
3713 if (!is_tcf_pedit(act))
3714 j++;
3715 }
Vlad Buslov9838b202019-08-26 16:45:03 +03003716
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003717err_out:
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03003718 if (err)
3719 tc_cleanup_flow_action(flow_action);
3720
Vlad Buslov9838b202019-08-26 16:45:03 +03003721 return err;
Vlad Buslov7a472812020-02-17 12:12:09 +02003722err_out_locked:
3723 spin_unlock_bh(&act->tcfa_lock);
3724 goto err_out;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003725}
3726EXPORT_SYMBOL(tc_setup_flow_action);
3727
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +01003728unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3729{
3730 unsigned int num_acts = 0;
3731 struct tc_action *act;
3732 int i;
3733
3734 tcf_exts_for_each_action(i, act, exts) {
3735 if (is_tcf_pedit(act))
3736 num_acts += tcf_pedit_nkeys(act);
3737 else
3738 num_acts++;
3739 }
3740 return num_acts;
3741}
3742EXPORT_SYMBOL(tcf_exts_num_actions);
3743
Jiri Pirko48617382018-01-17 11:46:46 +01003744static __net_init int tcf_net_init(struct net *net)
3745{
3746 struct tcf_net *tn = net_generic(net, tcf_net_id);
3747
Vlad Buslovab281622018-09-24 19:22:56 +03003748 spin_lock_init(&tn->idr_lock);
Jiri Pirko48617382018-01-17 11:46:46 +01003749 idr_init(&tn->idr);
3750 return 0;
3751}
3752
3753static void __net_exit tcf_net_exit(struct net *net)
3754{
3755 struct tcf_net *tn = net_generic(net, tcf_net_id);
3756
3757 idr_destroy(&tn->idr);
3758}
3759
3760static struct pernet_operations tcf_net_ops = {
3761 .init = tcf_net_init,
3762 .exit = tcf_net_exit,
3763 .id = &tcf_net_id,
3764 .size = sizeof(struct tcf_net),
3765};
3766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767static int __init tc_filter_init(void)
3768{
Jiri Pirko48617382018-01-17 11:46:46 +01003769 int err;
3770
Cong Wang7aa00452017-10-26 18:24:28 -07003771 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3772 if (!tc_filter_wq)
3773 return -ENOMEM;
3774
Jiri Pirko48617382018-01-17 11:46:46 +01003775 err = register_pernet_subsys(&tcf_net_ops);
3776 if (err)
3777 goto err_register_pernet_subsys;
3778
Vlad Buslov470502d2019-02-11 10:55:48 +02003779 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3780 RTNL_FLAG_DOIT_UNLOCKED);
3781 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3782 RTNL_FLAG_DOIT_UNLOCKED);
Vlad Buslovc431f892018-05-31 09:52:53 +03003783 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Vlad Buslov470502d2019-02-11 10:55:48 +02003784 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003785 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3786 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3787 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3788 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01003791
3792err_register_pernet_subsys:
3793 destroy_workqueue(tc_filter_wq);
3794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
3797subsys_initcall(tc_filter_init);