blob: 7993a692c7fdacef63a5e97a3fe9a086d822f086 [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 Hurley59eb87c2019-11-02 14:17:47 +000023#include <linux/jhash.h>
Paul Blakey43719292020-02-16 12:01:23 +020024#include <linux/rculist.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110025#include <net/net_namespace.h>
26#include <net/sock.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070027#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <net/pkt_sched.h>
29#include <net/pkt_cls.h>
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +010030#include <net/tc_act/tc_pedit.h>
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +010031#include <net/tc_act/tc_mirred.h>
32#include <net/tc_act/tc_vlan.h>
33#include <net/tc_act/tc_tunnel_key.h>
34#include <net/tc_act/tc_csum.h>
35#include <net/tc_act/tc_gact.h>
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -070036#include <net/tc_act/tc_police.h>
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -070037#include <net/tc_act/tc_sample.h>
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +010038#include <net/tc_act/tc_skbedit.h>
Paul Blakeyb57dc7c2019-07-09 10:30:48 +030039#include <net/tc_act/tc_ct.h>
John Hurley6749d5902019-07-23 15:33:59 +010040#include <net/tc_act/tc_mpls.h>
Po Liud29bdd62020-05-01 08:53:16 +080041#include <net/tc_act/tc_gate.h>
wenxu4e481902019-08-07 09:13:52 +080042#include <net/flow_offload.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Davide Carattie3314732018-10-10 22:00:58 +020044extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* The list of all installed classifier types */
WANG Cong36272872013-12-15 20:15:11 -080047static LIST_HEAD(tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Protects list of registered TC modules. It is pure SMP lock. */
50static DEFINE_RWLOCK(cls_mod_lock);
51
John Hurley59eb87c2019-11-02 14:17:47 +000052static u32 destroy_obj_hashfn(const struct tcf_proto *tp)
53{
54 return jhash_3words(tp->chain->index, tp->prio,
55 (__force __u32)tp->protocol, 0);
56}
57
58static void tcf_proto_signal_destroying(struct tcf_chain *chain,
59 struct tcf_proto *tp)
60{
61 struct tcf_block *block = chain->block;
62
63 mutex_lock(&block->proto_destroy_lock);
64 hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node,
65 destroy_obj_hashfn(tp));
66 mutex_unlock(&block->proto_destroy_lock);
67}
68
69static bool tcf_proto_cmp(const struct tcf_proto *tp1,
70 const struct tcf_proto *tp2)
71{
72 return tp1->chain->index == tp2->chain->index &&
73 tp1->prio == tp2->prio &&
74 tp1->protocol == tp2->protocol;
75}
76
77static bool tcf_proto_exists_destroying(struct tcf_chain *chain,
78 struct tcf_proto *tp)
79{
80 u32 hash = destroy_obj_hashfn(tp);
81 struct tcf_proto *iter;
82 bool found = false;
83
84 rcu_read_lock();
85 hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter,
86 destroy_ht_node, hash) {
87 if (tcf_proto_cmp(tp, iter)) {
88 found = true;
89 break;
90 }
91 }
92 rcu_read_unlock();
93
94 return found;
95}
96
97static void
98tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp)
99{
100 struct tcf_block *block = chain->block;
101
102 mutex_lock(&block->proto_destroy_lock);
103 if (hash_hashed(&tp->destroy_ht_node))
104 hash_del_rcu(&tp->destroy_ht_node);
105 mutex_unlock(&block->proto_destroy_lock);
106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* Find classifier type by string name */
109
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200110static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Eric Dumazetdcd76082013-12-20 10:04:18 -0800112 const struct tcf_proto_ops *t, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if (kind) {
115 read_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -0800116 list_for_each_entry(t, &tcf_proto_base, head) {
Jiri Pirko33a48922017-02-09 14:38:57 +0100117 if (strcmp(kind, t->kind) == 0) {
Eric Dumazetdcd76082013-12-20 10:04:18 -0800118 if (try_module_get(t->owner))
119 res = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 break;
121 }
122 }
123 read_unlock(&cls_mod_lock);
124 }
Eric Dumazetdcd76082013-12-20 10:04:18 -0800125 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200128static const struct tcf_proto_ops *
Vlad Buslov12db03b2019-02-11 10:55:45 +0200129tcf_proto_lookup_ops(const char *kind, bool rtnl_held,
130 struct netlink_ext_ack *extack)
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200131{
132 const struct tcf_proto_ops *ops;
133
134 ops = __tcf_proto_lookup_ops(kind);
135 if (ops)
136 return ops;
137#ifdef CONFIG_MODULES
Vlad Buslov12db03b2019-02-11 10:55:45 +0200138 if (rtnl_held)
139 rtnl_unlock();
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200140 request_module("cls_%s", kind);
Vlad Buslov12db03b2019-02-11 10:55:45 +0200141 if (rtnl_held)
142 rtnl_lock();
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200143 ops = __tcf_proto_lookup_ops(kind);
144 /* We dropped the RTNL semaphore in order to perform
145 * the module load. So, even if we succeeded in loading
146 * the module we have to replay the request. We indicate
147 * this using -EAGAIN.
148 */
149 if (ops) {
150 module_put(ops->owner);
151 return ERR_PTR(-EAGAIN);
152 }
153#endif
154 NL_SET_ERR_MSG(extack, "TC classifier not found");
155 return ERR_PTR(-ENOENT);
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Register(unregister) new classifier type */
159
160int register_tcf_proto_ops(struct tcf_proto_ops *ops)
161{
WANG Cong36272872013-12-15 20:15:11 -0800162 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int rc = -EEXIST;
164
165 write_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -0800166 list_for_each_entry(t, &tcf_proto_base, head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!strcmp(ops->kind, t->kind))
168 goto out;
169
WANG Cong36272872013-12-15 20:15:11 -0800170 list_add_tail(&ops->head, &tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 rc = 0;
172out:
173 write_unlock(&cls_mod_lock);
174 return rc;
175}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800176EXPORT_SYMBOL(register_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Cong Wang7aa00452017-10-26 18:24:28 -0700178static struct workqueue_struct *tc_filter_wq;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
181{
WANG Cong36272872013-12-15 20:15:11 -0800182 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int rc = -ENOENT;
184
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200185 /* Wait for outstanding call_rcu()s, if any, from a
186 * tcf_proto_ops's destroy() handler.
187 */
188 rcu_barrier();
Cong Wang7aa00452017-10-26 18:24:28 -0700189 flush_workqueue(tc_filter_wq);
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 write_lock(&cls_mod_lock);
Eric Dumazetdcd76082013-12-20 10:04:18 -0800192 list_for_each_entry(t, &tcf_proto_base, head) {
193 if (t == ops) {
194 list_del(&t->head);
195 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
Eric Dumazetdcd76082013-12-20 10:04:18 -0800197 }
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 write_unlock(&cls_mod_lock);
200 return rc;
201}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800202EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Cong Wangaaa908f2018-05-23 15:26:53 -0700204bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
Cong Wang7aa00452017-10-26 18:24:28 -0700205{
Cong Wangaaa908f2018-05-23 15:26:53 -0700206 INIT_RCU_WORK(rwork, func);
207 return queue_rcu_work(tc_filter_wq, rwork);
Cong Wang7aa00452017-10-26 18:24:28 -0700208}
209EXPORT_SYMBOL(tcf_queue_work);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/* Select new prio value from the range, managed by kernel. */
212
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800213static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800215 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000218 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jiri Pirko79619732017-05-17 11:07:58 +0200220 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Cong Wang6f96c3c2019-10-07 13:26:28 -0700223static bool tcf_proto_check_kind(struct nlattr *kind, char *name)
224{
225 if (kind)
226 return nla_strlcpy(name, kind, IFNAMSIZ) >= IFNAMSIZ;
227 memset(name, 0, IFNAMSIZ);
228 return false;
229}
230
Vlad Buslov470502d2019-02-11 10:55:48 +0200231static bool tcf_proto_is_unlocked(const char *kind)
232{
233 const struct tcf_proto_ops *ops;
234 bool ret;
235
Cong Wang6f96c3c2019-10-07 13:26:28 -0700236 if (strlen(kind) == 0)
237 return false;
238
Vlad Buslov470502d2019-02-11 10:55:48 +0200239 ops = tcf_proto_lookup_ops(kind, false, NULL);
240 /* On error return false to take rtnl lock. Proto lookup/create
241 * functions will perform lookup again and properly handle errors.
242 */
243 if (IS_ERR(ops))
244 return false;
245
246 ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED);
247 module_put(ops->owner);
248 return ret;
249}
250
Jiri Pirko33a48922017-02-09 14:38:57 +0100251static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500252 u32 prio, struct tcf_chain *chain,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200253 bool rtnl_held,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500254 struct netlink_ext_ack *extack)
Jiri Pirko33a48922017-02-09 14:38:57 +0100255{
256 struct tcf_proto *tp;
257 int err;
258
259 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
260 if (!tp)
261 return ERR_PTR(-ENOBUFS);
262
Vlad Buslov12db03b2019-02-11 10:55:45 +0200263 tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack);
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200264 if (IS_ERR(tp->ops)) {
265 err = PTR_ERR(tp->ops);
Jiri Pirkod68d75f2018-05-11 17:45:32 +0200266 goto errout;
Jiri Pirko33a48922017-02-09 14:38:57 +0100267 }
268 tp->classify = tp->ops->classify;
269 tp->protocol = protocol;
270 tp->prio = prio;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200271 tp->chain = chain;
Vlad Buslov8b646782019-02-11 10:55:41 +0200272 spin_lock_init(&tp->lock);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200273 refcount_set(&tp->refcnt, 1);
Jiri Pirko33a48922017-02-09 14:38:57 +0100274
275 err = tp->ops->init(tp);
276 if (err) {
277 module_put(tp->ops->owner);
278 goto errout;
279 }
280 return tp;
281
282errout:
283 kfree(tp);
284 return ERR_PTR(err);
285}
286
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200287static void tcf_proto_get(struct tcf_proto *tp)
288{
289 refcount_inc(&tp->refcnt);
290}
291
292static void tcf_chain_put(struct tcf_chain *chain);
293
Vlad Buslov12db03b2019-02-11 10:55:45 +0200294static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held,
John Hurley59eb87c2019-11-02 14:17:47 +0000295 bool sig_destroy, struct netlink_ext_ack *extack)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100296{
Vlad Buslov12db03b2019-02-11 10:55:45 +0200297 tp->ops->destroy(tp, rtnl_held, extack);
John Hurley59eb87c2019-11-02 14:17:47 +0000298 if (sig_destroy)
299 tcf_proto_signal_destroyed(tp->chain, tp);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200300 tcf_chain_put(tp->chain);
WANG Cong763dbf62017-04-19 14:21:21 -0700301 module_put(tp->ops->owner);
302 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100303}
304
Vlad Buslov12db03b2019-02-11 10:55:45 +0200305static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held,
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200306 struct netlink_ext_ack *extack)
307{
308 if (refcount_dec_and_test(&tp->refcnt))
John Hurley59eb87c2019-11-02 14:17:47 +0000309 tcf_proto_destroy(tp, rtnl_held, true, extack);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200310}
311
Davide Carattia5b72a02019-12-28 16:36:58 +0100312static bool tcf_proto_check_delete(struct tcf_proto *tp)
Vlad Buslov8b646782019-02-11 10:55:41 +0200313{
Davide Carattia5b72a02019-12-28 16:36:58 +0100314 if (tp->ops->delete_empty)
315 return tp->ops->delete_empty(tp);
Vlad Buslov8b646782019-02-11 10:55:41 +0200316
Davide Carattia5b72a02019-12-28 16:36:58 +0100317 tp->deleting = true;
Vlad Buslov8b646782019-02-11 10:55:41 +0200318 return tp->deleting;
319}
320
321static void tcf_proto_mark_delete(struct tcf_proto *tp)
322{
323 spin_lock(&tp->lock);
324 tp->deleting = true;
325 spin_unlock(&tp->lock);
326}
327
328static bool tcf_proto_is_deleting(struct tcf_proto *tp)
329{
330 bool deleting;
331
332 spin_lock(&tp->lock);
333 deleting = tp->deleting;
334 spin_unlock(&tp->lock);
335
336 return deleting;
337}
338
Vlad Buslovc266f642019-02-11 10:55:32 +0200339#define ASSERT_BLOCK_LOCKED(block) \
340 lockdep_assert_held(&(block)->lock)
341
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100342struct tcf_filter_chain_list_item {
343 struct list_head list;
344 tcf_chain_head_change_t *chain_head_change;
345 void *chain_head_change_priv;
346};
347
Jiri Pirko5bc17012017-05-17 11:08:01 +0200348static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
349 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200350{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200351 struct tcf_chain *chain;
352
Vlad Buslovc266f642019-02-11 10:55:32 +0200353 ASSERT_BLOCK_LOCKED(block);
354
Jiri Pirko5bc17012017-05-17 11:08:01 +0200355 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
356 if (!chain)
357 return NULL;
Paul Blakey43719292020-02-16 12:01:23 +0200358 list_add_tail_rcu(&chain->list, &block->chain_list);
Vlad Busloved76f5e2019-02-11 10:55:38 +0200359 mutex_init(&chain->filter_chain_lock);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200360 chain->block = block;
361 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700362 chain->refcnt = 1;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200363 if (!chain->index)
364 block->chain0.chain = chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200365 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200366}
367
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100368static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
369 struct tcf_proto *tp_head)
370{
371 if (item->chain_head_change)
372 item->chain_head_change(tp_head, item->chain_head_change_priv);
373}
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200374
375static void tcf_chain0_head_change(struct tcf_chain *chain,
376 struct tcf_proto *tp_head)
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100377{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100378 struct tcf_filter_chain_list_item *item;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200379 struct tcf_block *block = chain->block;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100380
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200381 if (chain->index)
382 return;
Vlad Buslov165f0132019-02-11 10:55:35 +0200383
384 mutex_lock(&block->lock);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200385 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100386 tcf_chain_head_change_item(item, tp_head);
Vlad Buslov165f0132019-02-11 10:55:35 +0200387 mutex_unlock(&block->lock);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100388}
389
Vlad Buslovc266f642019-02-11 10:55:32 +0200390/* Returns true if block can be safely freed. */
391
392static bool tcf_chain_detach(struct tcf_chain *chain)
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200393{
Cong Wangefbf7892017-12-04 10:48:18 -0800394 struct tcf_block *block = chain->block;
395
Vlad Buslovc266f642019-02-11 10:55:32 +0200396 ASSERT_BLOCK_LOCKED(block);
397
Paul Blakey43719292020-02-16 12:01:23 +0200398 list_del_rcu(&chain->list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200399 if (!chain->index)
400 block->chain0.chain = NULL;
Vlad Buslovc266f642019-02-11 10:55:32 +0200401
402 if (list_empty(&block->chain_list) &&
403 refcount_read(&block->refcnt) == 0)
404 return true;
405
406 return false;
407}
408
409static void tcf_block_destroy(struct tcf_block *block)
410{
411 mutex_destroy(&block->lock);
John Hurley59eb87c2019-11-02 14:17:47 +0000412 mutex_destroy(&block->proto_destroy_lock);
Vlad Buslovc266f642019-02-11 10:55:32 +0200413 kfree_rcu(block, rcu);
414}
415
416static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block)
417{
418 struct tcf_block *block = chain->block;
419
Vlad Busloved76f5e2019-02-11 10:55:38 +0200420 mutex_destroy(&chain->filter_chain_lock);
Davide Carattiee3bbfe2019-03-20 15:00:16 +0100421 kfree_rcu(chain, rcu);
Vlad Buslovc266f642019-02-11 10:55:32 +0200422 if (free_block)
423 tcf_block_destroy(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700424}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200425
Cong Wange2ef7542017-09-11 16:33:31 -0700426static void tcf_chain_hold(struct tcf_chain *chain)
427{
Vlad Buslovc266f642019-02-11 10:55:32 +0200428 ASSERT_BLOCK_LOCKED(chain->block);
429
Cong Wange2ef7542017-09-11 16:33:31 -0700430 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200431}
432
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200433static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200434{
Vlad Buslovc266f642019-02-11 10:55:32 +0200435 ASSERT_BLOCK_LOCKED(chain->block);
436
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200437 /* In case all the references are action references, this
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200438 * chain should not be shown to the user.
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200439 */
440 return chain->refcnt == chain->action_refcnt;
441}
442
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200443static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
444 u32 chain_index)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200445{
446 struct tcf_chain *chain;
447
Vlad Buslovc266f642019-02-11 10:55:32 +0200448 ASSERT_BLOCK_LOCKED(block);
449
Jiri Pirko5bc17012017-05-17 11:08:01 +0200450 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200451 if (chain->index == chain_index)
Cong Wange2ef7542017-09-11 16:33:31 -0700452 return chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200453 }
454 return NULL;
455}
456
Paul Blakeyaf699622020-02-16 12:01:24 +0200457#if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
458static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block,
459 u32 chain_index)
460{
461 struct tcf_chain *chain;
462
463 list_for_each_entry_rcu(chain, &block->chain_list, list) {
464 if (chain->index == chain_index)
465 return chain;
466 }
467 return NULL;
468}
469#endif
470
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200471static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
472 u32 seq, u16 flags, int event, bool unicast);
473
Jiri Pirko53681402018-08-01 12:36:56 +0200474static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
475 u32 chain_index, bool create,
476 bool by_act)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200477{
Vlad Buslovc266f642019-02-11 10:55:32 +0200478 struct tcf_chain *chain = NULL;
479 bool is_first_reference;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200480
Vlad Buslovc266f642019-02-11 10:55:32 +0200481 mutex_lock(&block->lock);
482 chain = tcf_chain_lookup(block, chain_index);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200483 if (chain) {
484 tcf_chain_hold(chain);
Jiri Pirko53681402018-08-01 12:36:56 +0200485 } else {
486 if (!create)
Vlad Buslovc266f642019-02-11 10:55:32 +0200487 goto errout;
Jiri Pirko53681402018-08-01 12:36:56 +0200488 chain = tcf_chain_create(block, chain_index);
489 if (!chain)
Vlad Buslovc266f642019-02-11 10:55:32 +0200490 goto errout;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200491 }
Jiri Pirko80532382017-09-06 13:14:19 +0200492
Jiri Pirko53681402018-08-01 12:36:56 +0200493 if (by_act)
494 ++chain->action_refcnt;
Vlad Buslovc266f642019-02-11 10:55:32 +0200495 is_first_reference = chain->refcnt - chain->action_refcnt == 1;
496 mutex_unlock(&block->lock);
Jiri Pirko53681402018-08-01 12:36:56 +0200497
498 /* Send notification only in case we got the first
499 * non-action reference. Until then, the chain acts only as
500 * a placeholder for actions pointing to it and user ought
501 * not know about them.
502 */
Vlad Buslovc266f642019-02-11 10:55:32 +0200503 if (is_first_reference && !by_act)
Jiri Pirko53681402018-08-01 12:36:56 +0200504 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
505 RTM_NEWCHAIN, false);
506
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200507 return chain;
Vlad Buslovc266f642019-02-11 10:55:32 +0200508
509errout:
510 mutex_unlock(&block->lock);
511 return chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200512}
Jiri Pirko53681402018-08-01 12:36:56 +0200513
Jiri Pirko290b1c82018-08-01 12:36:57 +0200514static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
515 bool create)
Jiri Pirko53681402018-08-01 12:36:56 +0200516{
517 return __tcf_chain_get(block, chain_index, create, false);
518}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200519
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200520struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
521{
Jiri Pirko53681402018-08-01 12:36:56 +0200522 return __tcf_chain_get(block, chain_index, true, true);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200523}
524EXPORT_SYMBOL(tcf_chain_get_by_act);
525
Vlad Buslova5654822019-02-11 10:55:37 +0200526static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
527 void *tmplt_priv);
528static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
529 void *tmplt_priv, u32 chain_index,
530 struct tcf_block *block, struct sk_buff *oskb,
531 u32 seq, u16 flags, bool unicast);
Jiri Pirko9f407f12018-07-23 09:23:07 +0200532
Vlad Buslov91052fa2019-02-11 10:55:33 +0200533static void __tcf_chain_put(struct tcf_chain *chain, bool by_act,
534 bool explicitly_created)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200535{
Vlad Buslovc266f642019-02-11 10:55:32 +0200536 struct tcf_block *block = chain->block;
Vlad Buslova5654822019-02-11 10:55:37 +0200537 const struct tcf_proto_ops *tmplt_ops;
Vlad Buslovb62989f2019-03-06 17:50:43 +0200538 bool free_block = false;
Vlad Buslovc266f642019-02-11 10:55:32 +0200539 unsigned int refcnt;
Vlad Buslova5654822019-02-11 10:55:37 +0200540 void *tmplt_priv;
Vlad Buslovc266f642019-02-11 10:55:32 +0200541
542 mutex_lock(&block->lock);
Vlad Buslov91052fa2019-02-11 10:55:33 +0200543 if (explicitly_created) {
544 if (!chain->explicitly_created) {
545 mutex_unlock(&block->lock);
546 return;
547 }
548 chain->explicitly_created = false;
549 }
550
Jiri Pirko53681402018-08-01 12:36:56 +0200551 if (by_act)
552 chain->action_refcnt--;
Vlad Buslovc266f642019-02-11 10:55:32 +0200553
554 /* tc_chain_notify_delete can't be called while holding block lock.
555 * However, when block is unlocked chain can be changed concurrently, so
556 * save these to temporary variables.
557 */
558 refcnt = --chain->refcnt;
Vlad Buslova5654822019-02-11 10:55:37 +0200559 tmplt_ops = chain->tmplt_ops;
560 tmplt_priv = chain->tmplt_priv;
Jiri Pirko53681402018-08-01 12:36:56 +0200561
562 /* The last dropped non-action reference will trigger notification. */
Vlad Buslovb62989f2019-03-06 17:50:43 +0200563 if (refcnt - chain->action_refcnt == 0 && !by_act) {
564 tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index,
Vlad Buslova5654822019-02-11 10:55:37 +0200565 block, NULL, 0, 0, false);
Vlad Buslov726d06122019-02-11 10:55:42 +0200566 /* Last reference to chain, no need to lock. */
567 chain->flushing = false;
568 }
Jiri Pirko53681402018-08-01 12:36:56 +0200569
Vlad Buslovb62989f2019-03-06 17:50:43 +0200570 if (refcnt == 0)
571 free_block = tcf_chain_detach(chain);
572 mutex_unlock(&block->lock);
573
Vlad Buslovc266f642019-02-11 10:55:32 +0200574 if (refcnt == 0) {
Vlad Buslova5654822019-02-11 10:55:37 +0200575 tc_chain_tmplt_del(tmplt_ops, tmplt_priv);
Vlad Buslovc266f642019-02-11 10:55:32 +0200576 tcf_chain_destroy(chain, free_block);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200577 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200578}
Jiri Pirko53681402018-08-01 12:36:56 +0200579
Jiri Pirko290b1c82018-08-01 12:36:57 +0200580static void tcf_chain_put(struct tcf_chain *chain)
Jiri Pirko53681402018-08-01 12:36:56 +0200581{
Vlad Buslov91052fa2019-02-11 10:55:33 +0200582 __tcf_chain_put(chain, false, false);
Jiri Pirko53681402018-08-01 12:36:56 +0200583}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200584
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200585void tcf_chain_put_by_act(struct tcf_chain *chain)
586{
Vlad Buslov91052fa2019-02-11 10:55:33 +0200587 __tcf_chain_put(chain, true, false);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200588}
589EXPORT_SYMBOL(tcf_chain_put_by_act);
590
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200591static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
592{
Vlad Buslov91052fa2019-02-11 10:55:33 +0200593 __tcf_chain_put(chain, false, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200594}
595
Vlad Buslov12db03b2019-02-11 10:55:45 +0200596static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held)
Jiri Pirko290b1c82018-08-01 12:36:57 +0200597{
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200598 struct tcf_proto *tp, *tp_next;
Jiri Pirko290b1c82018-08-01 12:36:57 +0200599
Vlad Busloved76f5e2019-02-11 10:55:38 +0200600 mutex_lock(&chain->filter_chain_lock);
601 tp = tcf_chain_dereference(chain->filter_chain, chain);
John Hurley59eb87c2019-11-02 14:17:47 +0000602 while (tp) {
603 tp_next = rcu_dereference_protected(tp->next, 1);
604 tcf_proto_signal_destroying(chain, tp);
605 tp = tp_next;
606 }
607 tp = tcf_chain_dereference(chain->filter_chain, chain);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200608 RCU_INIT_POINTER(chain->filter_chain, NULL);
Jiri Pirko290b1c82018-08-01 12:36:57 +0200609 tcf_chain0_head_change(chain, NULL);
Vlad Buslov726d06122019-02-11 10:55:42 +0200610 chain->flushing = true;
Vlad Busloved76f5e2019-02-11 10:55:38 +0200611 mutex_unlock(&chain->filter_chain_lock);
612
Jiri Pirko290b1c82018-08-01 12:36:57 +0200613 while (tp) {
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200614 tp_next = rcu_dereference_protected(tp->next, 1);
Vlad Buslov12db03b2019-02-11 10:55:45 +0200615 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslov4dbfa762019-02-11 10:55:39 +0200616 tp = tp_next;
Jiri Pirko290b1c82018-08-01 12:36:57 +0200617 }
618}
619
wenxu4e481902019-08-07 09:13:52 +0800620static int tcf_block_setup(struct tcf_block *block,
621 struct flow_block_offload *bo);
622
Pablo Neira Ayuso324a8232020-05-29 02:25:36 +0200623static void tcf_block_offload_init(struct flow_block_offload *bo,
Petr Machatac40f4e52020-07-11 00:55:03 +0300624 struct net_device *dev, struct Qdisc *sch,
Pablo Neira Ayuso324a8232020-05-29 02:25:36 +0200625 enum flow_block_command command,
626 enum flow_block_binder_type binder_type,
627 struct flow_block *flow_block,
628 bool shared, struct netlink_ext_ack *extack)
629{
630 bo->net = dev_net(dev);
631 bo->command = command;
632 bo->binder_type = binder_type;
633 bo->block = flow_block;
634 bo->block_shared = shared;
635 bo->extack = extack;
Petr Machatac40f4e52020-07-11 00:55:03 +0300636 bo->sch = sch;
Eli Cohen1f5db5b2021-08-17 20:05:18 +0300637 bo->cb_list_head = &flow_block->cb_list;
Pablo Neira Ayuso324a8232020-05-29 02:25:36 +0200638 INIT_LIST_HEAD(&bo->cb_list);
639}
640
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200641static void tcf_block_unbind(struct tcf_block *block,
642 struct flow_block_offload *bo);
John Hurley7f76fa32018-11-09 21:21:26 -0800643
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200644static void tc_block_indr_cleanup(struct flow_block_cb *block_cb)
645{
646 struct tcf_block *block = block_cb->indr.data;
647 struct net_device *dev = block_cb->indr.dev;
Petr Machatac40f4e52020-07-11 00:55:03 +0300648 struct Qdisc *sch = block_cb->indr.sch;
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200649 struct netlink_ext_ack extack = {};
Yunjian Wangaf36da52021-04-01 12:52:48 +0800650 struct flow_block_offload bo = {};
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200651
Petr Machatac40f4e52020-07-11 00:55:03 +0300652 tcf_block_offload_init(&bo, dev, sch, FLOW_BLOCK_UNBIND,
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200653 block_cb->indr.binder_type,
654 &block->flow_block, tcf_block_shared(block),
655 &extack);
Leon Romanovskyd6535dc2020-10-26 14:33:27 +0200656 rtnl_lock();
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200657 down_write(&block->cb_lock);
wenxua1db2172020-06-18 20:49:10 +0800658 list_del(&block_cb->driver_list);
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200659 list_move(&block_cb->list, &bo.cb_list);
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200660 tcf_block_unbind(block, &bo);
Leon Romanovskyd6535dc2020-10-26 14:33:27 +0200661 up_write(&block->cb_lock);
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200662 rtnl_unlock();
John Hurley7f76fa32018-11-09 21:21:26 -0800663}
664
Jiri Pirkocaa72602018-01-17 11:46:50 +0100665static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200666{
Vlad Buslov97394be2019-08-26 16:44:58 +0300667 return atomic_read(&block->offloadcnt);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100668}
669
670static int tcf_block_offload_cmd(struct tcf_block *block,
Petr Machatac40f4e52020-07-11 00:55:03 +0300671 struct net_device *dev, struct Qdisc *sch,
Jiri Pirkocaa72602018-01-17 11:46:50 +0100672 struct tcf_block_ext_info *ei,
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +0200673 enum flow_block_command command,
John Hurley60513bd2018-06-25 14:30:04 -0700674 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100675{
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +0200676 struct flow_block_offload bo = {};
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200677
Petr Machatac40f4e52020-07-11 00:55:03 +0300678 tcf_block_offload_init(&bo, dev, sch, command, ei->binder_type,
Pablo Neira Ayuso324a8232020-05-29 02:25:36 +0200679 &block->flow_block, tcf_block_shared(block),
680 extack);
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +0200681
wenxu3c005112020-06-18 20:49:11 +0800682 if (dev->netdev_ops->ndo_setup_tc) {
683 int err;
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200684
wenxu3c005112020-06-18 20:49:11 +0800685 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
686 if (err < 0) {
687 if (err != -EOPNOTSUPP)
688 NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed");
689 return err;
690 }
691
692 return tcf_block_setup(block, &bo);
Jesper Dangaard Brouerb70ba692020-04-23 16:57:45 +0200693 }
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +0200694
Petr Machatac40f4e52020-07-11 00:55:03 +0300695 flow_indr_dev_setup_offload(dev, sch, TC_SETUP_BLOCK, block, &bo,
wenxu3c005112020-06-18 20:49:11 +0800696 tc_block_indr_cleanup);
697 tcf_block_setup(block, &bo);
698
699 return -EOPNOTSUPP;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200700}
701
Jiri Pirkocaa72602018-01-17 11:46:50 +0100702static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700703 struct tcf_block_ext_info *ei,
704 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200705{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100706 struct net_device *dev = q->dev_queue->dev;
707 int err;
708
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300709 down_write(&block->cb_lock);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100710
711 /* If tc offload feature is disabled and the block we try to bind
712 * to already has some offloaded filters, forbid to bind.
713 */
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200714 if (dev->netdev_ops->ndo_setup_tc &&
715 !tc_can_offload(dev) &&
716 tcf_block_offload_in_use(block)) {
John Hurley60513bd2018-06-25 14:30:04 -0700717 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300718 err = -EOPNOTSUPP;
719 goto err_unlock;
John Hurley60513bd2018-06-25 14:30:04 -0700720 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100721
Petr Machatac40f4e52020-07-11 00:55:03 +0300722 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100723 if (err == -EOPNOTSUPP)
724 goto no_offload_dev_inc;
John Hurley7f76fa32018-11-09 21:21:26 -0800725 if (err)
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300726 goto err_unlock;
John Hurley7f76fa32018-11-09 21:21:26 -0800727
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300728 up_write(&block->cb_lock);
John Hurley7f76fa32018-11-09 21:21:26 -0800729 return 0;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100730
731no_offload_dev_inc:
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200732 if (tcf_block_offload_in_use(block))
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300733 goto err_unlock;
Pablo Neira Ayuso0fdcf782020-05-29 02:25:37 +0200734
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300735 err = 0;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100736 block->nooffloaddevcnt++;
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300737err_unlock:
738 up_write(&block->cb_lock);
739 return err;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200740}
741
742static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
743 struct tcf_block_ext_info *ei)
744{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100745 struct net_device *dev = q->dev_queue->dev;
746 int err;
747
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300748 down_write(&block->cb_lock);
Petr Machatac40f4e52020-07-11 00:55:03 +0300749 err = tcf_block_offload_cmd(block, dev, q, ei, FLOW_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100750 if (err == -EOPNOTSUPP)
751 goto no_offload_dev_dec;
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300752 up_write(&block->cb_lock);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100753 return;
754
755no_offload_dev_dec:
756 WARN_ON(block->nooffloaddevcnt-- == 0);
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300757 up_write(&block->cb_lock);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200758}
759
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100760static int
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200761tcf_chain0_head_change_cb_add(struct tcf_block *block,
762 struct tcf_block_ext_info *ei,
763 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100764{
765 struct tcf_filter_chain_list_item *item;
Vlad Buslov165f0132019-02-11 10:55:35 +0200766 struct tcf_chain *chain0;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100767
768 item = kmalloc(sizeof(*item), GFP_KERNEL);
769 if (!item) {
770 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
771 return -ENOMEM;
772 }
773 item->chain_head_change = ei->chain_head_change;
774 item->chain_head_change_priv = ei->chain_head_change_priv;
Vlad Buslov165f0132019-02-11 10:55:35 +0200775
776 mutex_lock(&block->lock);
777 chain0 = block->chain0.chain;
Vlad Busloved76f5e2019-02-11 10:55:38 +0200778 if (chain0)
779 tcf_chain_hold(chain0);
780 else
781 list_add(&item->list, &block->chain0.filter_chain_list);
Vlad Buslov165f0132019-02-11 10:55:35 +0200782 mutex_unlock(&block->lock);
783
Vlad Busloved76f5e2019-02-11 10:55:38 +0200784 if (chain0) {
785 struct tcf_proto *tp_head;
786
787 mutex_lock(&chain0->filter_chain_lock);
788
789 tp_head = tcf_chain_dereference(chain0->filter_chain, chain0);
790 if (tp_head)
791 tcf_chain_head_change_item(item, tp_head);
792
793 mutex_lock(&block->lock);
794 list_add(&item->list, &block->chain0.filter_chain_list);
795 mutex_unlock(&block->lock);
796
797 mutex_unlock(&chain0->filter_chain_lock);
798 tcf_chain_put(chain0);
799 }
800
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100801 return 0;
802}
803
804static void
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200805tcf_chain0_head_change_cb_del(struct tcf_block *block,
806 struct tcf_block_ext_info *ei)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100807{
808 struct tcf_filter_chain_list_item *item;
809
Vlad Buslov165f0132019-02-11 10:55:35 +0200810 mutex_lock(&block->lock);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200811 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100812 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
813 (item->chain_head_change == ei->chain_head_change &&
814 item->chain_head_change_priv == ei->chain_head_change_priv)) {
Vlad Buslov165f0132019-02-11 10:55:35 +0200815 if (block->chain0.chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200816 tcf_chain_head_change_item(item, NULL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100817 list_del(&item->list);
Vlad Buslov165f0132019-02-11 10:55:35 +0200818 mutex_unlock(&block->lock);
819
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100820 kfree(item);
821 return;
822 }
823 }
Vlad Buslov165f0132019-02-11 10:55:35 +0200824 mutex_unlock(&block->lock);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100825 WARN_ON(1);
826}
827
Jiri Pirko48617382018-01-17 11:46:46 +0100828struct tcf_net {
Vlad Buslovab281622018-09-24 19:22:56 +0300829 spinlock_t idr_lock; /* Protects idr */
Jiri Pirko48617382018-01-17 11:46:46 +0100830 struct idr idr;
831};
832
833static unsigned int tcf_net_id;
834
835static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100836 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100837{
Jiri Pirko48617382018-01-17 11:46:46 +0100838 struct tcf_net *tn = net_generic(net, tcf_net_id);
Vlad Buslovab281622018-09-24 19:22:56 +0300839 int err;
Jiri Pirko48617382018-01-17 11:46:46 +0100840
Vlad Buslovab281622018-09-24 19:22:56 +0300841 idr_preload(GFP_KERNEL);
842 spin_lock(&tn->idr_lock);
843 err = idr_alloc_u32(&tn->idr, block, &block->index, block->index,
844 GFP_NOWAIT);
845 spin_unlock(&tn->idr_lock);
846 idr_preload_end();
847
848 return err;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100849}
850
Jiri Pirko48617382018-01-17 11:46:46 +0100851static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200852{
Jiri Pirko48617382018-01-17 11:46:46 +0100853 struct tcf_net *tn = net_generic(net, tcf_net_id);
854
Vlad Buslovab281622018-09-24 19:22:56 +0300855 spin_lock(&tn->idr_lock);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500856 idr_remove(&tn->idr, block->index);
Vlad Buslovab281622018-09-24 19:22:56 +0300857 spin_unlock(&tn->idr_lock);
Jiri Pirko48617382018-01-17 11:46:46 +0100858}
859
860static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100861 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100862 struct netlink_ext_ack *extack)
863{
864 struct tcf_block *block;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200865
Jiri Pirko48617382018-01-17 11:46:46 +0100866 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500867 if (!block) {
868 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100869 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500870 }
Vlad Buslovc266f642019-02-11 10:55:32 +0200871 mutex_init(&block->lock);
John Hurley59eb87c2019-11-02 14:17:47 +0000872 mutex_init(&block->proto_destroy_lock);
Vlad Buslov4f8116c2019-08-26 16:44:57 +0300873 init_rwsem(&block->cb_lock);
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200874 flow_block_init(&block->flow_block);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200875 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100876 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200877 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200878
Vlad Buslovcfebd7e2018-09-24 19:22:54 +0300879 refcount_set(&block->refcnt, 1);
Jiri Pirko48617382018-01-17 11:46:46 +0100880 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100881 block->index = block_index;
882
883 /* Don't store q pointer for blocks which are shared */
884 if (!tcf_block_shared(block))
885 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100886 return block;
Jiri Pirko48617382018-01-17 11:46:46 +0100887}
888
889static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
890{
891 struct tcf_net *tn = net_generic(net, tcf_net_id);
892
Matthew Wilcox322d8842017-11-28 10:01:24 -0500893 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100894}
895
Vlad Buslov0607e432018-09-24 19:22:57 +0300896static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index)
897{
898 struct tcf_block *block;
899
900 rcu_read_lock();
901 block = tcf_block_lookup(net, block_index);
902 if (block && !refcount_inc_not_zero(&block->refcnt))
903 block = NULL;
904 rcu_read_unlock();
905
906 return block;
907}
908
Vlad Buslovbbf73832019-02-11 10:55:36 +0200909static struct tcf_chain *
910__tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
911{
912 mutex_lock(&block->lock);
913 if (chain)
914 chain = list_is_last(&chain->list, &block->chain_list) ?
915 NULL : list_next_entry(chain, list);
916 else
917 chain = list_first_entry_or_null(&block->chain_list,
918 struct tcf_chain, list);
919
920 /* skip all action-only chains */
921 while (chain && tcf_chain_held_by_acts_only(chain))
922 chain = list_is_last(&chain->list, &block->chain_list) ?
923 NULL : list_next_entry(chain, list);
924
925 if (chain)
926 tcf_chain_hold(chain);
927 mutex_unlock(&block->lock);
928
929 return chain;
930}
931
932/* Function to be used by all clients that want to iterate over all chains on
933 * block. It properly obtains block->lock and takes reference to chain before
934 * returning it. Users of this function must be tolerant to concurrent chain
935 * insertion/deletion or ensure that no concurrent chain modification is
936 * possible. Note that all netlink dump callbacks cannot guarantee to provide
937 * consistent dump because rtnl lock is released each time skb is filled with
938 * data and sent to user-space.
939 */
940
941struct tcf_chain *
942tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain)
943{
944 struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain);
945
946 if (chain)
947 tcf_chain_put(chain);
948
949 return chain_next;
950}
951EXPORT_SYMBOL(tcf_get_next_chain);
952
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200953static struct tcf_proto *
954__tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp)
955{
Vlad Buslov8b646782019-02-11 10:55:41 +0200956 u32 prio = 0;
957
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200958 ASSERT_RTNL();
959 mutex_lock(&chain->filter_chain_lock);
960
Vlad Buslov8b646782019-02-11 10:55:41 +0200961 if (!tp) {
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200962 tp = tcf_chain_dereference(chain->filter_chain, chain);
Vlad Buslov8b646782019-02-11 10:55:41 +0200963 } else if (tcf_proto_is_deleting(tp)) {
964 /* 'deleting' flag is set and chain->filter_chain_lock was
965 * unlocked, which means next pointer could be invalid. Restart
966 * search.
967 */
968 prio = tp->prio + 1;
969 tp = tcf_chain_dereference(chain->filter_chain, chain);
970
971 for (; tp; tp = tcf_chain_dereference(tp->next, chain))
972 if (!tp->deleting && tp->prio >= prio)
973 break;
974 } else {
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200975 tp = tcf_chain_dereference(tp->next, chain);
Vlad Buslov8b646782019-02-11 10:55:41 +0200976 }
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200977
978 if (tp)
979 tcf_proto_get(tp);
980
981 mutex_unlock(&chain->filter_chain_lock);
982
983 return tp;
984}
985
986/* Function to be used by all clients that want to iterate over all tp's on
987 * chain. Users of this function must be tolerant to concurrent tp
988 * insertion/deletion or ensure that no concurrent chain modification is
989 * possible. Note that all netlink dump callbacks cannot guarantee to provide
990 * consistent dump because rtnl lock is released each time skb is filled with
991 * data and sent to user-space.
992 */
993
994struct tcf_proto *
Vlad Buslov12db03b2019-02-11 10:55:45 +0200995tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp,
996 bool rtnl_held)
Vlad Buslovfe2923a2019-02-11 10:55:40 +0200997{
998 struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp);
999
1000 if (tp)
Vlad Buslov12db03b2019-02-11 10:55:45 +02001001 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslovfe2923a2019-02-11 10:55:40 +02001002
1003 return tp_next;
1004}
1005EXPORT_SYMBOL(tcf_get_next_proto);
1006
Vlad Buslov12db03b2019-02-11 10:55:45 +02001007static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held)
Vlad Buslovf0023432018-09-24 19:22:55 +03001008{
1009 struct tcf_chain *chain;
1010
Vlad Buslovbbf73832019-02-11 10:55:36 +02001011 /* Last reference to block. At this point chains cannot be added or
1012 * removed concurrently.
Vlad Buslovf0023432018-09-24 19:22:55 +03001013 */
Vlad Buslovbbf73832019-02-11 10:55:36 +02001014 for (chain = tcf_get_next_chain(block, NULL);
1015 chain;
1016 chain = tcf_get_next_chain(block, chain)) {
Vlad Buslovf0023432018-09-24 19:22:55 +03001017 tcf_chain_put_explicitly_created(chain);
Vlad Buslov12db03b2019-02-11 10:55:45 +02001018 tcf_chain_flush(chain, rtnl_held);
Vlad Buslovf0023432018-09-24 19:22:55 +03001019 }
1020}
1021
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001022/* Lookup Qdisc and increments its reference counter.
1023 * Set parent, if necessary.
1024 */
1025
1026static int __tcf_qdisc_find(struct net *net, struct Qdisc **q,
1027 u32 *parent, int ifindex, bool rtnl_held,
1028 struct netlink_ext_ack *extack)
1029{
1030 const struct Qdisc_class_ops *cops;
1031 struct net_device *dev;
1032 int err = 0;
1033
1034 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1035 return 0;
1036
1037 rcu_read_lock();
1038
1039 /* Find link */
1040 dev = dev_get_by_index_rcu(net, ifindex);
1041 if (!dev) {
1042 rcu_read_unlock();
1043 return -ENODEV;
1044 }
1045
1046 /* Find qdisc */
1047 if (!*parent) {
1048 *q = dev->qdisc;
1049 *parent = (*q)->handle;
1050 } else {
1051 *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent));
1052 if (!*q) {
1053 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1054 err = -EINVAL;
1055 goto errout_rcu;
1056 }
1057 }
1058
1059 *q = qdisc_refcount_inc_nz(*q);
1060 if (!*q) {
1061 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
1062 err = -EINVAL;
1063 goto errout_rcu;
1064 }
1065
1066 /* Is it classful? */
1067 cops = (*q)->ops->cl_ops;
1068 if (!cops) {
1069 NL_SET_ERR_MSG(extack, "Qdisc not classful");
1070 err = -EINVAL;
1071 goto errout_qdisc;
1072 }
1073
1074 if (!cops->tcf_block) {
1075 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
1076 err = -EOPNOTSUPP;
1077 goto errout_qdisc;
1078 }
1079
1080errout_rcu:
1081 /* At this point we know that qdisc is not noop_qdisc,
1082 * which means that qdisc holds a reference to net_device
1083 * and we hold a reference to qdisc, so it is safe to release
1084 * rcu read lock.
1085 */
1086 rcu_read_unlock();
1087 return err;
1088
1089errout_qdisc:
1090 rcu_read_unlock();
1091
1092 if (rtnl_held)
1093 qdisc_put(*q);
1094 else
1095 qdisc_put_unlocked(*q);
1096 *q = NULL;
1097
1098 return err;
1099}
1100
1101static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl,
1102 int ifindex, struct netlink_ext_ack *extack)
1103{
1104 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK)
1105 return 0;
1106
1107 /* Do we search for filter, attached to class? */
1108 if (TC_H_MIN(parent)) {
1109 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1110
1111 *cl = cops->find(q, parent);
1112 if (*cl == 0) {
1113 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
1114 return -ENOENT;
1115 }
1116 }
1117
1118 return 0;
1119}
1120
1121static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q,
1122 unsigned long cl, int ifindex,
1123 u32 block_index,
1124 struct netlink_ext_ack *extack)
1125{
1126 struct tcf_block *block;
1127
1128 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1129 block = tcf_block_refcnt_get(net, block_index);
1130 if (!block) {
1131 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1132 return ERR_PTR(-EINVAL);
1133 }
1134 } else {
1135 const struct Qdisc_class_ops *cops = q->ops->cl_ops;
1136
1137 block = cops->tcf_block(q, cl, extack);
1138 if (!block)
1139 return ERR_PTR(-EINVAL);
1140
1141 if (tcf_block_shared(block)) {
1142 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1143 return ERR_PTR(-EOPNOTSUPP);
1144 }
1145
1146 /* Always take reference to block in order to support execution
1147 * of rules update path of cls API without rtnl lock. Caller
1148 * must release block when it is finished using it. 'if' block
1149 * of this conditional obtain reference to block by calling
1150 * tcf_block_refcnt_get().
1151 */
1152 refcount_inc(&block->refcnt);
1153 }
1154
1155 return block;
1156}
1157
Vlad Buslov0607e432018-09-24 19:22:57 +03001158static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001159 struct tcf_block_ext_info *ei, bool rtnl_held)
Vlad Buslov0607e432018-09-24 19:22:57 +03001160{
Vlad Buslovc266f642019-02-11 10:55:32 +02001161 if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) {
Vlad Buslov0607e432018-09-24 19:22:57 +03001162 /* Flushing/putting all chains will cause the block to be
1163 * deallocated when last chain is freed. However, if chain_list
1164 * is empty, block has to be manually deallocated. After block
1165 * reference counter reached 0, it is no longer possible to
1166 * increment it or add new chains to block.
1167 */
1168 bool free_block = list_empty(&block->chain_list);
1169
Vlad Buslovc266f642019-02-11 10:55:32 +02001170 mutex_unlock(&block->lock);
Vlad Buslov0607e432018-09-24 19:22:57 +03001171 if (tcf_block_shared(block))
1172 tcf_block_remove(block, block->net);
Vlad Buslov0607e432018-09-24 19:22:57 +03001173
1174 if (q)
1175 tcf_block_offload_unbind(block, q, ei);
1176
1177 if (free_block)
Vlad Buslovc266f642019-02-11 10:55:32 +02001178 tcf_block_destroy(block);
Vlad Buslov0607e432018-09-24 19:22:57 +03001179 else
Vlad Buslov12db03b2019-02-11 10:55:45 +02001180 tcf_block_flush_all_chains(block, rtnl_held);
Vlad Buslov0607e432018-09-24 19:22:57 +03001181 } else if (q) {
1182 tcf_block_offload_unbind(block, q, ei);
1183 }
1184}
1185
Vlad Buslov12db03b2019-02-11 10:55:45 +02001186static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held)
Vlad Buslov0607e432018-09-24 19:22:57 +03001187{
Vlad Buslov12db03b2019-02-11 10:55:45 +02001188 __tcf_block_put(block, NULL, NULL, rtnl_held);
Vlad Buslov0607e432018-09-24 19:22:57 +03001189}
1190
Vlad Buslovc431f892018-05-31 09:52:53 +03001191/* Find tcf block.
1192 * Set q, parent, cl when appropriate.
1193 */
1194
1195static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
1196 u32 *parent, unsigned long *cl,
1197 int ifindex, u32 block_index,
1198 struct netlink_ext_ack *extack)
1199{
1200 struct tcf_block *block;
Vlad Buslove368fdb2018-09-24 19:22:53 +03001201 int err = 0;
Vlad Buslovc431f892018-05-31 09:52:53 +03001202
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001203 ASSERT_RTNL();
Vlad Buslovc431f892018-05-31 09:52:53 +03001204
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001205 err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack);
1206 if (err)
1207 goto errout;
Vlad Buslove368fdb2018-09-24 19:22:53 +03001208
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001209 err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack);
1210 if (err)
1211 goto errout_qdisc;
Vlad Buslovc431f892018-05-31 09:52:53 +03001212
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001213 block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack);
Dan Carpenteraf736bf2019-02-18 12:26:32 +03001214 if (IS_ERR(block)) {
1215 err = PTR_ERR(block);
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001216 goto errout_qdisc;
Dan Carpenteraf736bf2019-02-18 12:26:32 +03001217 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001218
1219 return block;
Vlad Buslove368fdb2018-09-24 19:22:53 +03001220
Vlad Buslove368fdb2018-09-24 19:22:53 +03001221errout_qdisc:
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001222 if (*q)
Vlad Buslove368fdb2018-09-24 19:22:53 +03001223 qdisc_put(*q);
Vlad Buslov18d3eef2019-02-11 10:55:47 +02001224errout:
1225 *q = NULL;
Vlad Buslove368fdb2018-09-24 19:22:53 +03001226 return ERR_PTR(err);
1227}
1228
Vlad Buslov12db03b2019-02-11 10:55:45 +02001229static void tcf_block_release(struct Qdisc *q, struct tcf_block *block,
1230 bool rtnl_held)
Vlad Buslove368fdb2018-09-24 19:22:53 +03001231{
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001232 if (!IS_ERR_OR_NULL(block))
Vlad Buslov12db03b2019-02-11 10:55:45 +02001233 tcf_block_refcnt_put(block, rtnl_held);
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001234
Vlad Buslov470502d2019-02-11 10:55:48 +02001235 if (q) {
1236 if (rtnl_held)
1237 qdisc_put(q);
1238 else
1239 qdisc_put_unlocked(q);
1240 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001241}
1242
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001243struct tcf_block_owner_item {
1244 struct list_head list;
1245 struct Qdisc *q;
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001246 enum flow_block_binder_type binder_type;
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001247};
1248
1249static void
1250tcf_block_owner_netif_keep_dst(struct tcf_block *block,
1251 struct Qdisc *q,
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001252 enum flow_block_binder_type binder_type)
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001253{
1254 if (block->keep_dst &&
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001255 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
1256 binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001257 netif_keep_dst(qdisc_dev(q));
1258}
1259
1260void tcf_block_netif_keep_dst(struct tcf_block *block)
1261{
1262 struct tcf_block_owner_item *item;
1263
1264 block->keep_dst = true;
1265 list_for_each_entry(item, &block->owner_list, list)
1266 tcf_block_owner_netif_keep_dst(block, item->q,
1267 item->binder_type);
1268}
1269EXPORT_SYMBOL(tcf_block_netif_keep_dst);
1270
1271static int tcf_block_owner_add(struct tcf_block *block,
1272 struct Qdisc *q,
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001273 enum flow_block_binder_type binder_type)
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001274{
1275 struct tcf_block_owner_item *item;
1276
1277 item = kmalloc(sizeof(*item), GFP_KERNEL);
1278 if (!item)
1279 return -ENOMEM;
1280 item->q = q;
1281 item->binder_type = binder_type;
1282 list_add(&item->list, &block->owner_list);
1283 return 0;
1284}
1285
1286static void tcf_block_owner_del(struct tcf_block *block,
1287 struct Qdisc *q,
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001288 enum flow_block_binder_type binder_type)
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001289{
1290 struct tcf_block_owner_item *item;
1291
1292 list_for_each_entry(item, &block->owner_list, list) {
1293 if (item->q == q && item->binder_type == binder_type) {
1294 list_del(&item->list);
1295 kfree(item);
1296 return;
1297 }
1298 }
1299 WARN_ON(1);
1300}
1301
Jiri Pirko48617382018-01-17 11:46:46 +01001302int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
1303 struct tcf_block_ext_info *ei,
1304 struct netlink_ext_ack *extack)
1305{
1306 struct net *net = qdisc_net(q);
1307 struct tcf_block *block = NULL;
Jiri Pirko48617382018-01-17 11:46:46 +01001308 int err;
1309
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001310 if (ei->block_index)
Jiri Pirko48617382018-01-17 11:46:46 +01001311 /* block_index not 0 means the shared block is requested */
Vlad Buslov787ce6d2018-09-24 19:22:58 +03001312 block = tcf_block_refcnt_get(net, ei->block_index);
Jiri Pirko48617382018-01-17 11:46:46 +01001313
1314 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +01001315 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +01001316 if (IS_ERR(block))
1317 return PTR_ERR(block);
Jiri Pirkobb047dd2018-02-13 12:00:16 +01001318 if (tcf_block_shared(block)) {
1319 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +01001320 if (err)
1321 goto err_block_insert;
1322 }
1323 }
1324
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001325 err = tcf_block_owner_add(block, q, ei->binder_type);
1326 if (err)
1327 goto err_block_owner_add;
1328
1329 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
1330
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001331 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +01001332 if (err)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001333 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +01001334
John Hurley60513bd2018-06-25 14:30:04 -07001335 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +01001336 if (err)
1337 goto err_block_offload_bind;
1338
Jiri Pirko6529eab2017-05-17 11:07:55 +02001339 *p_block = block;
1340 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001341
Jiri Pirkocaa72602018-01-17 11:46:50 +01001342err_block_offload_bind:
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001343 tcf_chain0_head_change_cb_del(block, ei);
1344err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001345 tcf_block_owner_del(block, q, ei->binder_type);
1346err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +01001347err_block_insert:
Vlad Buslov12db03b2019-02-11 10:55:45 +02001348 tcf_block_refcnt_put(block, true);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001349 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001350}
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001351EXPORT_SYMBOL(tcf_block_get_ext);
1352
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001353static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
1354{
1355 struct tcf_proto __rcu **p_filter_chain = priv;
1356
1357 rcu_assign_pointer(*p_filter_chain, tp_head);
1358}
1359
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001360int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -05001361 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
1362 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001363{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001364 struct tcf_block_ext_info ei = {
1365 .chain_head_change = tcf_chain_head_change_dflt,
1366 .chain_head_change_priv = p_filter_chain,
1367 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001368
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001369 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -05001370 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001371}
Jiri Pirko6529eab2017-05-17 11:07:55 +02001372EXPORT_SYMBOL(tcf_block_get);
1373
Cong Wang7aa00452017-10-26 18:24:28 -07001374/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +01001375 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -07001376 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001377void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +09001378 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -07001379{
David S. Millerc30abd52017-12-16 22:11:55 -05001380 if (!block)
1381 return;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001382 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +01001383 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +01001384
Vlad Buslov12db03b2019-02-11 10:55:45 +02001385 __tcf_block_put(block, q, ei, true);
Jiri Pirko6529eab2017-05-17 11:07:55 +02001386}
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001387EXPORT_SYMBOL(tcf_block_put_ext);
1388
1389void tcf_block_put(struct tcf_block *block)
1390{
1391 struct tcf_block_ext_info ei = {0, };
1392
Jiri Pirko4853f122017-12-21 13:13:59 +01001393 if (!block)
1394 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001395 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +02001396}
David S. Millere1ea2f92017-10-30 14:10:01 +09001397
Jiri Pirko6529eab2017-05-17 11:07:55 +02001398EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +01001399
John Hurley32636742018-06-25 14:30:10 -07001400static int
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +02001401tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb,
John Hurley32636742018-06-25 14:30:10 -07001402 void *cb_priv, bool add, bool offload_in_use,
1403 struct netlink_ext_ack *extack)
1404{
Vlad Buslovbbf73832019-02-11 10:55:36 +02001405 struct tcf_chain *chain, *chain_prev;
Vlad Buslovfe2923a2019-02-11 10:55:40 +02001406 struct tcf_proto *tp, *tp_prev;
John Hurley32636742018-06-25 14:30:10 -07001407 int err;
1408
Vlad Buslov4f8116c2019-08-26 16:44:57 +03001409 lockdep_assert_held(&block->cb_lock);
1410
Vlad Buslovbbf73832019-02-11 10:55:36 +02001411 for (chain = __tcf_get_next_chain(block, NULL);
1412 chain;
1413 chain_prev = chain,
1414 chain = __tcf_get_next_chain(block, chain),
1415 tcf_chain_put(chain_prev)) {
Vlad Buslovfe2923a2019-02-11 10:55:40 +02001416 for (tp = __tcf_get_next_proto(chain, NULL); tp;
1417 tp_prev = tp,
1418 tp = __tcf_get_next_proto(chain, tp),
Vlad Buslov12db03b2019-02-11 10:55:45 +02001419 tcf_proto_put(tp_prev, true, NULL)) {
John Hurley32636742018-06-25 14:30:10 -07001420 if (tp->ops->reoffload) {
1421 err = tp->ops->reoffload(tp, add, cb, cb_priv,
1422 extack);
1423 if (err && add)
1424 goto err_playback_remove;
1425 } else if (add && offload_in_use) {
1426 err = -EOPNOTSUPP;
1427 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
1428 goto err_playback_remove;
1429 }
1430 }
1431 }
1432
1433 return 0;
1434
1435err_playback_remove:
Vlad Buslov12db03b2019-02-11 10:55:45 +02001436 tcf_proto_put(tp, true, NULL);
Vlad Buslovbbf73832019-02-11 10:55:36 +02001437 tcf_chain_put(chain);
John Hurley32636742018-06-25 14:30:10 -07001438 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
1439 extack);
1440 return err;
1441}
1442
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001443static int tcf_block_bind(struct tcf_block *block,
1444 struct flow_block_offload *bo)
1445{
1446 struct flow_block_cb *block_cb, *next;
1447 int err, i = 0;
1448
Vlad Buslov4f8116c2019-08-26 16:44:57 +03001449 lockdep_assert_held(&block->cb_lock);
1450
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001451 list_for_each_entry(block_cb, &bo->cb_list, list) {
1452 err = tcf_block_playback_offloads(block, block_cb->cb,
1453 block_cb->cb_priv, true,
1454 tcf_block_offload_in_use(block),
1455 bo->extack);
1456 if (err)
1457 goto err_unroll;
Vlad Buslovc9f14472019-08-26 16:45:01 +03001458 if (!bo->unlocked_driver_cb)
1459 block->lockeddevcnt++;
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001460
1461 i++;
1462 }
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +02001463 list_splice(&bo->cb_list, &block->flow_block.cb_list);
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001464
1465 return 0;
1466
1467err_unroll:
1468 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1469 if (i-- > 0) {
1470 list_del(&block_cb->list);
1471 tcf_block_playback_offloads(block, block_cb->cb,
1472 block_cb->cb_priv, false,
1473 tcf_block_offload_in_use(block),
1474 NULL);
Vlad Buslovc9f14472019-08-26 16:45:01 +03001475 if (!bo->unlocked_driver_cb)
1476 block->lockeddevcnt--;
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001477 }
1478 flow_block_cb_free(block_cb);
1479 }
1480
1481 return err;
1482}
1483
1484static void tcf_block_unbind(struct tcf_block *block,
1485 struct flow_block_offload *bo)
1486{
1487 struct flow_block_cb *block_cb, *next;
1488
Vlad Buslov4f8116c2019-08-26 16:44:57 +03001489 lockdep_assert_held(&block->cb_lock);
1490
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001491 list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
1492 tcf_block_playback_offloads(block, block_cb->cb,
1493 block_cb->cb_priv, false,
1494 tcf_block_offload_in_use(block),
1495 NULL);
1496 list_del(&block_cb->list);
1497 flow_block_cb_free(block_cb);
Vlad Buslovc9f14472019-08-26 16:45:01 +03001498 if (!bo->unlocked_driver_cb)
1499 block->lockeddevcnt--;
Pablo Neira Ayuso59094b12019-07-09 22:55:45 +02001500 }
1501}
1502
1503static int tcf_block_setup(struct tcf_block *block,
1504 struct flow_block_offload *bo)
1505{
1506 int err;
1507
1508 switch (bo->command) {
1509 case FLOW_BLOCK_BIND:
1510 err = tcf_block_bind(block, bo);
1511 break;
1512 case FLOW_BLOCK_UNBIND:
1513 err = 0;
1514 tcf_block_unbind(block, bo);
1515 break;
1516 default:
1517 WARN_ON_ONCE(1);
1518 err = -EOPNOTSUPP;
1519 }
1520
1521 return err;
1522}
1523
Jiri Pirko87d83092017-05-17 11:07:54 +02001524/* Main classifier routine: scans classifier chain attached
1525 * to this qdisc, (optionally) tests for protocol and asks
1526 * specific classifiers.
1527 */
Paul Blakey9410c942020-02-16 12:01:21 +02001528static inline int __tcf_classify(struct sk_buff *skb,
1529 const struct tcf_proto *tp,
Paul Blakeyaf699622020-02-16 12:01:24 +02001530 const struct tcf_proto *orig_tp,
Paul Blakey9410c942020-02-16 12:01:21 +02001531 struct tcf_result *res,
1532 bool compat_mode,
1533 u32 *last_executed_chain)
Jiri Pirko87d83092017-05-17 11:07:54 +02001534{
Jiri Pirko87d83092017-05-17 11:07:54 +02001535#ifdef CONFIG_NET_CLS_ACT
Davide Caratti1b832bd2021-05-19 15:17:21 +02001536 const int max_reclassify_loop = 16;
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001537 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001538 int limit = 0;
1539
1540reclassify:
1541#endif
1542 for (; tp; tp = rcu_dereference_bh(tp->next)) {
Toke Høiland-Jørgensend7bf2eb2020-07-03 22:26:43 +02001543 __be16 protocol = skb_protocol(skb, false);
Jiri Pirko87d83092017-05-17 11:07:54 +02001544 int err;
1545
1546 if (tp->protocol != protocol &&
1547 tp->protocol != htons(ETH_P_ALL))
1548 continue;
1549
1550 err = tp->classify(skb, tp, res);
1551#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +02001552 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001553 first_tp = orig_tp;
Paul Blakey9410c942020-02-16 12:01:21 +02001554 *last_executed_chain = first_tp->chain->index;
Jiri Pirko87d83092017-05-17 11:07:54 +02001555 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +02001556 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001557 first_tp = res->goto_tp;
Paul Blakey9410c942020-02-16 12:01:21 +02001558 *last_executed_chain = err & TC_ACT_EXT_VAL_MASK;
Jiri Pirkodb505142017-05-17 11:08:03 +02001559 goto reset;
1560 }
Jiri Pirko87d83092017-05-17 11:07:54 +02001561#endif
1562 if (err >= 0)
1563 return err;
1564 }
1565
1566 return TC_ACT_UNSPEC; /* signal: continue lookup */
1567#ifdef CONFIG_NET_CLS_ACT
1568reset:
1569 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +01001570 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
1571 tp->chain->block->index,
1572 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +02001573 ntohs(tp->protocol));
1574 return TC_ACT_SHOT;
1575 }
1576
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001577 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001578 goto reclassify;
1579#endif
1580}
Paul Blakey9410c942020-02-16 12:01:21 +02001581
1582int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
1583 struct tcf_result *res, bool compat_mode)
1584{
1585 u32 last_executed_chain = 0;
1586
Paul Blakeyaf699622020-02-16 12:01:24 +02001587 return __tcf_classify(skb, tp, tp, res, compat_mode,
Paul Blakey9410c942020-02-16 12:01:21 +02001588 &last_executed_chain);
1589}
Jiri Pirko87d83092017-05-17 11:07:54 +02001590EXPORT_SYMBOL(tcf_classify);
1591
Paul Blakey7d17c542020-02-16 12:01:22 +02001592int tcf_classify_ingress(struct sk_buff *skb,
1593 const struct tcf_block *ingress_block,
1594 const struct tcf_proto *tp,
Paul Blakey9410c942020-02-16 12:01:21 +02001595 struct tcf_result *res, bool compat_mode)
1596{
1597#if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1598 u32 last_executed_chain = 0;
1599
Paul Blakeyaf699622020-02-16 12:01:24 +02001600 return __tcf_classify(skb, tp, tp, res, compat_mode,
Paul Blakey9410c942020-02-16 12:01:21 +02001601 &last_executed_chain);
1602#else
1603 u32 last_executed_chain = tp ? tp->chain->index : 0;
Paul Blakeyaf699622020-02-16 12:01:24 +02001604 const struct tcf_proto *orig_tp = tp;
Paul Blakey9410c942020-02-16 12:01:21 +02001605 struct tc_skb_ext *ext;
1606 int ret;
1607
Paul Blakeyaf699622020-02-16 12:01:24 +02001608 ext = skb_ext_find(skb, TC_SKB_EXT);
1609
1610 if (ext && ext->chain) {
1611 struct tcf_chain *fchain;
1612
1613 fchain = tcf_chain_lookup_rcu(ingress_block, ext->chain);
1614 if (!fchain)
1615 return TC_ACT_SHOT;
1616
1617 /* Consume, so cloned/redirect skbs won't inherit ext */
1618 skb_ext_del(skb, TC_SKB_EXT);
1619
1620 tp = rcu_dereference_bh(fchain->filter_chain);
Paul Blakeya080da62020-04-06 18:36:56 +03001621 last_executed_chain = fchain->index;
Paul Blakeyaf699622020-02-16 12:01:24 +02001622 }
1623
1624 ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode,
1625 &last_executed_chain);
Paul Blakey9410c942020-02-16 12:01:21 +02001626
1627 /* If we missed on some chain */
1628 if (ret == TC_ACT_UNSPEC && last_executed_chain) {
Vlad Buslovac493452021-05-25 16:21:52 +03001629 ext = tc_skb_ext_alloc(skb);
Paul Blakey9410c942020-02-16 12:01:21 +02001630 if (WARN_ON_ONCE(!ext))
1631 return TC_ACT_SHOT;
1632 ext->chain = last_executed_chain;
wenxu038ebb12020-07-31 10:45:01 +08001633 ext->mru = qdisc_skb_cb(skb)->mru;
Paul Blakey9410c942020-02-16 12:01:21 +02001634 }
1635
1636 return ret;
1637#endif
1638}
1639EXPORT_SYMBOL(tcf_classify_ingress);
1640
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001641struct tcf_chain_info {
1642 struct tcf_proto __rcu **pprev;
1643 struct tcf_proto __rcu *next;
1644};
1645
Vlad Busloved76f5e2019-02-11 10:55:38 +02001646static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain,
1647 struct tcf_chain_info *chain_info)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001648{
Vlad Busloved76f5e2019-02-11 10:55:38 +02001649 return tcf_chain_dereference(*chain_info->pprev, chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001650}
1651
Vlad Buslov726d06122019-02-11 10:55:42 +02001652static int tcf_chain_tp_insert(struct tcf_chain *chain,
1653 struct tcf_chain_info *chain_info,
1654 struct tcf_proto *tp)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001655{
Vlad Buslov726d06122019-02-11 10:55:42 +02001656 if (chain->flushing)
1657 return -EAGAIN;
1658
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001659 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001660 tcf_chain0_head_change(chain, tp);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02001661 tcf_proto_get(tp);
Vlad Busloved76f5e2019-02-11 10:55:38 +02001662 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info));
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001663 rcu_assign_pointer(*chain_info->pprev, tp);
Vlad Buslov726d06122019-02-11 10:55:42 +02001664
1665 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001666}
1667
1668static void tcf_chain_tp_remove(struct tcf_chain *chain,
1669 struct tcf_chain_info *chain_info,
1670 struct tcf_proto *tp)
1671{
Vlad Busloved76f5e2019-02-11 10:55:38 +02001672 struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001673
Vlad Buslov8b646782019-02-11 10:55:41 +02001674 tcf_proto_mark_delete(tp);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001675 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +02001676 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001677 RCU_INIT_POINTER(*chain_info->pprev, next);
1678}
1679
1680static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1681 struct tcf_chain_info *chain_info,
1682 u32 protocol, u32 prio,
Vlad Buslov8b646782019-02-11 10:55:41 +02001683 bool prio_allocate);
1684
1685/* Try to insert new proto.
1686 * If proto with specified priority already exists, free new proto
1687 * and return existing one.
1688 */
1689
1690static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain,
1691 struct tcf_proto *tp_new,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001692 u32 protocol, u32 prio,
1693 bool rtnl_held)
Vlad Buslov8b646782019-02-11 10:55:41 +02001694{
1695 struct tcf_chain_info chain_info;
1696 struct tcf_proto *tp;
Vlad Buslov726d06122019-02-11 10:55:42 +02001697 int err = 0;
Vlad Buslov8b646782019-02-11 10:55:41 +02001698
1699 mutex_lock(&chain->filter_chain_lock);
1700
John Hurley59eb87c2019-11-02 14:17:47 +00001701 if (tcf_proto_exists_destroying(chain, tp_new)) {
1702 mutex_unlock(&chain->filter_chain_lock);
1703 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
1704 return ERR_PTR(-EAGAIN);
1705 }
1706
Vlad Buslov8b646782019-02-11 10:55:41 +02001707 tp = tcf_chain_tp_find(chain, &chain_info,
1708 protocol, prio, false);
1709 if (!tp)
Vlad Buslov726d06122019-02-11 10:55:42 +02001710 err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
Vlad Buslov8b646782019-02-11 10:55:41 +02001711 mutex_unlock(&chain->filter_chain_lock);
1712
1713 if (tp) {
John Hurley59eb87c2019-11-02 14:17:47 +00001714 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
Vlad Buslov8b646782019-02-11 10:55:41 +02001715 tp_new = tp;
Vlad Buslov726d06122019-02-11 10:55:42 +02001716 } else if (err) {
John Hurley59eb87c2019-11-02 14:17:47 +00001717 tcf_proto_destroy(tp_new, rtnl_held, false, NULL);
Vlad Buslov726d06122019-02-11 10:55:42 +02001718 tp_new = ERR_PTR(err);
Vlad Buslov8b646782019-02-11 10:55:41 +02001719 }
1720
1721 return tp_new;
1722}
1723
1724static void tcf_chain_tp_delete_empty(struct tcf_chain *chain,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001725 struct tcf_proto *tp, bool rtnl_held,
Vlad Buslov8b646782019-02-11 10:55:41 +02001726 struct netlink_ext_ack *extack)
1727{
1728 struct tcf_chain_info chain_info;
1729 struct tcf_proto *tp_iter;
1730 struct tcf_proto **pprev;
1731 struct tcf_proto *next;
1732
1733 mutex_lock(&chain->filter_chain_lock);
1734
1735 /* Atomically find and remove tp from chain. */
1736 for (pprev = &chain->filter_chain;
1737 (tp_iter = tcf_chain_dereference(*pprev, chain));
1738 pprev = &tp_iter->next) {
1739 if (tp_iter == tp) {
1740 chain_info.pprev = pprev;
1741 chain_info.next = tp_iter->next;
1742 WARN_ON(tp_iter->deleting);
1743 break;
1744 }
1745 }
1746 /* Verify that tp still exists and no new filters were inserted
1747 * concurrently.
1748 * Mark tp for deletion if it is empty.
1749 */
Davide Carattia5b72a02019-12-28 16:36:58 +01001750 if (!tp_iter || !tcf_proto_check_delete(tp)) {
Vlad Buslov8b646782019-02-11 10:55:41 +02001751 mutex_unlock(&chain->filter_chain_lock);
1752 return;
1753 }
1754
John Hurley59eb87c2019-11-02 14:17:47 +00001755 tcf_proto_signal_destroying(chain, tp);
Vlad Buslov8b646782019-02-11 10:55:41 +02001756 next = tcf_chain_dereference(chain_info.next, chain);
1757 if (tp == chain->filter_chain)
1758 tcf_chain0_head_change(chain, next);
1759 RCU_INIT_POINTER(*chain_info.pprev, next);
1760 mutex_unlock(&chain->filter_chain_lock);
1761
Vlad Buslov12db03b2019-02-11 10:55:45 +02001762 tcf_proto_put(tp, rtnl_held, extack);
Vlad Buslov8b646782019-02-11 10:55:41 +02001763}
1764
1765static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1766 struct tcf_chain_info *chain_info,
1767 u32 protocol, u32 prio,
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001768 bool prio_allocate)
1769{
1770 struct tcf_proto **pprev;
1771 struct tcf_proto *tp;
1772
1773 /* Check the chain for existence of proto-tcf with this priority */
1774 for (pprev = &chain->filter_chain;
Vlad Busloved76f5e2019-02-11 10:55:38 +02001775 (tp = tcf_chain_dereference(*pprev, chain));
1776 pprev = &tp->next) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001777 if (tp->prio >= prio) {
1778 if (tp->prio == prio) {
1779 if (prio_allocate ||
1780 (tp->protocol != protocol && protocol))
1781 return ERR_PTR(-EINVAL);
1782 } else {
1783 tp = NULL;
1784 }
1785 break;
1786 }
1787 }
1788 chain_info->pprev = pprev;
Vlad Buslov4dbfa762019-02-11 10:55:39 +02001789 if (tp) {
1790 chain_info->next = tp->next;
1791 tcf_proto_get(tp);
1792 } else {
1793 chain_info->next = NULL;
1794 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001795 return tp;
1796}
1797
WANG Cong71203712017-08-07 15:26:50 -07001798static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001799 struct tcf_proto *tp, struct tcf_block *block,
1800 struct Qdisc *q, u32 parent, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001801 u32 portid, u32 seq, u16 flags, int event,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001802 bool terse_dump, bool rtnl_held)
WANG Cong71203712017-08-07 15:26:50 -07001803{
1804 struct tcmsg *tcm;
1805 struct nlmsghdr *nlh;
1806 unsigned char *b = skb_tail_pointer(skb);
1807
1808 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1809 if (!nlh)
1810 goto out_nlmsg_trim;
1811 tcm = nlmsg_data(nlh);
1812 tcm->tcm_family = AF_UNSPEC;
1813 tcm->tcm__pad1 = 0;
1814 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001815 if (q) {
1816 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1817 tcm->tcm_parent = parent;
1818 } else {
1819 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1820 tcm->tcm_block_index = block->index;
1821 }
WANG Cong71203712017-08-07 15:26:50 -07001822 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1823 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1824 goto nla_put_failure;
1825 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1826 goto nla_put_failure;
1827 if (!fh) {
1828 tcm->tcm_handle = 0;
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001829 } else if (terse_dump) {
1830 if (tp->ops->terse_dump) {
1831 if (tp->ops->terse_dump(net, tp, fh, skb, tcm,
1832 rtnl_held) < 0)
1833 goto nla_put_failure;
1834 } else {
1835 goto cls_op_not_supp;
1836 }
WANG Cong71203712017-08-07 15:26:50 -07001837 } else {
Vlad Buslov12db03b2019-02-11 10:55:45 +02001838 if (tp->ops->dump &&
1839 tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0)
WANG Cong71203712017-08-07 15:26:50 -07001840 goto nla_put_failure;
1841 }
1842 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1843 return skb->len;
1844
1845out_nlmsg_trim:
1846nla_put_failure:
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001847cls_op_not_supp:
WANG Cong71203712017-08-07 15:26:50 -07001848 nlmsg_trim(skb, b);
1849 return -1;
1850}
1851
1852static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1853 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001854 struct tcf_block *block, struct Qdisc *q,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001855 u32 parent, void *fh, int event, bool unicast,
1856 bool rtnl_held)
WANG Cong71203712017-08-07 15:26:50 -07001857{
1858 struct sk_buff *skb;
1859 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001860 int err = 0;
WANG Cong71203712017-08-07 15:26:50 -07001861
1862 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1863 if (!skb)
1864 return -ENOBUFS;
1865
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001866 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001867 n->nlmsg_seq, n->nlmsg_flags, event,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001868 false, rtnl_held) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001869 kfree_skb(skb);
1870 return -EINVAL;
1871 }
1872
1873 if (unicast)
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001874 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1875 else
1876 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1877 n->nlmsg_flags & NLM_F_ECHO);
WANG Cong71203712017-08-07 15:26:50 -07001878
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001879 if (err > 0)
1880 err = 0;
1881 return err;
WANG Cong71203712017-08-07 15:26:50 -07001882}
1883
1884static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1885 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001886 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001887 u32 parent, void *fh, bool unicast, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001888 bool rtnl_held, struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001889{
1890 struct sk_buff *skb;
1891 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1892 int err;
1893
1894 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1895 if (!skb)
1896 return -ENOBUFS;
1897
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001898 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001899 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03001900 false, rtnl_held) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001901 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001902 kfree_skb(skb);
1903 return -EINVAL;
1904 }
1905
Vlad Buslov12db03b2019-02-11 10:55:45 +02001906 err = tp->ops->delete(tp, fh, last, rtnl_held, extack);
WANG Cong71203712017-08-07 15:26:50 -07001907 if (err) {
1908 kfree_skb(skb);
1909 return err;
1910 }
1911
1912 if (unicast)
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001913 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1914 else
1915 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1916 n->nlmsg_flags & NLM_F_ECHO);
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001917 if (err < 0)
1918 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
Zhike Wang5b5f99b2019-03-11 03:15:54 -07001919
1920 if (err > 0)
1921 err = 0;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001922 return err;
WANG Cong71203712017-08-07 15:26:50 -07001923}
1924
1925static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001926 struct tcf_block *block, struct Qdisc *q,
1927 u32 parent, struct nlmsghdr *n,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001928 struct tcf_chain *chain, int event,
1929 bool rtnl_held)
WANG Cong71203712017-08-07 15:26:50 -07001930{
1931 struct tcf_proto *tp;
1932
Vlad Buslov12db03b2019-02-11 10:55:45 +02001933 for (tp = tcf_get_next_proto(chain, NULL, rtnl_held);
1934 tp; tp = tcf_get_next_proto(chain, tp, rtnl_held))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001935 tfilter_notify(net, oskb, n, tp, block,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001936 q, parent, NULL, event, false, rtnl_held);
WANG Cong71203712017-08-07 15:26:50 -07001937}
1938
Vlad Buslov7d5509f2019-02-11 10:55:44 +02001939static void tfilter_put(struct tcf_proto *tp, void *fh)
1940{
1941 if (tp->ops->put && fh)
1942 tp->ops->put(tp, fh);
1943}
1944
Vlad Buslovc431f892018-05-31 09:52:53 +03001945static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001946 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001948 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001949 struct nlattr *tca[TCA_MAX + 1];
Cong Wang6f96c3c2019-10-07 13:26:28 -07001950 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 struct tcmsg *t;
1952 u32 protocol;
1953 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001954 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001956 u32 chain_index;
Eric Dumazete7be5692022-01-31 09:20:18 -08001957 struct Qdisc *q;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001958 struct tcf_chain_info chain_info;
Eric Dumazete7be5692022-01-31 09:20:18 -08001959 struct tcf_chain *chain;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001960 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001963 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001965 int tp_created;
Vlad Buslov470502d2019-02-11 10:55:48 +02001966 bool rtnl_held = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Vlad Buslovc431f892018-05-31 09:52:53 +03001968 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001969 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001972 tp_created = 0;
1973
Johannes Berg8cb08172019-04-26 14:07:28 +02001974 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
1975 rtm_tca_policy, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001976 if (err < 0)
1977 return err;
1978
David S. Miller942b8162012-06-26 21:48:50 -07001979 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 protocol = TC_H_MIN(t->tcm_info);
1981 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001982 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 parent = t->tcm_parent;
Vlad Buslov4dbfa762019-02-11 10:55:39 +02001984 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 cl = 0;
Vlad Buslov470502d2019-02-11 10:55:48 +02001986 block = NULL;
Eric Dumazete7be5692022-01-31 09:20:18 -08001987 q = NULL;
1988 chain = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001991 /* If no priority is provided by the user,
1992 * we allocate one.
1993 */
1994 if (n->nlmsg_flags & NLM_F_CREATE) {
1995 prio = TC_H_MAKE(0x80000000U, 0U);
1996 prio_allocate = true;
1997 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001998 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02002000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002
2003 /* Find head of filter chain. */
2004
Vlad Buslov470502d2019-02-11 10:55:48 +02002005 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2006 if (err)
2007 return err;
2008
Cong Wang6f96c3c2019-10-07 13:26:28 -07002009 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2010 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2011 err = -EINVAL;
2012 goto errout;
2013 }
2014
Vlad Buslov470502d2019-02-11 10:55:48 +02002015 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2016 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2017 * type is not specified, classifier is not unlocked.
2018 */
2019 if (rtnl_held ||
2020 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
Cong Wang6f96c3c2019-10-07 13:26:28 -07002021 !tcf_proto_is_unlocked(name)) {
Vlad Buslov470502d2019-02-11 10:55:48 +02002022 rtnl_held = true;
2023 rtnl_lock();
2024 }
2025
2026 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2027 if (err)
2028 goto errout;
2029
2030 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2031 extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002032 if (IS_ERR(block)) {
2033 err = PTR_ERR(block);
2034 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002035 }
Cong Wanga7df4872020-04-30 20:53:49 -07002036 block->classid = parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002037
2038 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2039 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002040 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02002041 err = -EINVAL;
2042 goto errout;
2043 }
Vlad Buslovc431f892018-05-31 09:52:53 +03002044 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02002045 if (!chain) {
Jiri Pirkod5ed72a2018-08-27 20:58:43 +02002046 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03002047 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02002048 goto errout;
2049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Vlad Busloved76f5e2019-02-11 10:55:38 +02002051 mutex_lock(&chain->filter_chain_lock);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02002052 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2053 prio, prio_allocate);
2054 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002055 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02002056 err = PTR_ERR(tp);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002057 goto errout_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059
2060 if (tp == NULL) {
Vlad Buslov8b646782019-02-11 10:55:41 +02002061 struct tcf_proto *tp_new = NULL;
2062
Vlad Buslov726d06122019-02-11 10:55:42 +02002063 if (chain->flushing) {
2064 err = -EAGAIN;
2065 goto errout_locked;
2066 }
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /* Proto-tcf does not exist, create new one */
2069
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002070 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002071 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002072 err = -EINVAL;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002073 goto errout_locked;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Vlad Buslovc431f892018-05-31 09:52:53 +03002076 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002077 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 +01002078 err = -ENOENT;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002079 goto errout_locked;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02002082 if (prio_allocate)
Vlad Busloved76f5e2019-02-11 10:55:38 +02002083 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2084 &chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Vlad Busloved76f5e2019-02-11 10:55:38 +02002086 mutex_unlock(&chain->filter_chain_lock);
Eric Dumazet36d79af2020-01-21 11:02:20 -08002087 tp_new = tcf_proto_create(name, protocol, prio, chain,
2088 rtnl_held, extack);
Vlad Buslov8b646782019-02-11 10:55:41 +02002089 if (IS_ERR(tp_new)) {
2090 err = PTR_ERR(tp_new);
Vlad Buslov726d06122019-02-11 10:55:42 +02002091 goto errout_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Vlad Busloved76f5e2019-02-11 10:55:38 +02002093
Minoru Usui12186be2009-06-02 02:17:34 -07002094 tp_created = 1;
Vlad Buslov12db03b2019-02-11 10:55:45 +02002095 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2096 rtnl_held);
Vlad Buslov726d06122019-02-11 10:55:42 +02002097 if (IS_ERR(tp)) {
2098 err = PTR_ERR(tp);
2099 goto errout_tp;
2100 }
Vlad Busloved76f5e2019-02-11 10:55:38 +02002101 } else {
2102 mutex_unlock(&chain->filter_chain_lock);
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Vlad Buslov8b646782019-02-11 10:55:41 +02002105 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2106 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2107 err = -EINVAL;
2108 goto errout;
2109 }
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 fh = tp->ops->get(tp, t->tcm_handle);
2112
WANG Cong8113c092017-08-04 21:31:43 -07002113 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03002114 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002115 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 +01002116 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002118 }
Vlad Buslovc431f892018-05-31 09:52:53 +03002119 } else if (n->nlmsg_flags & NLM_F_EXCL) {
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002120 tfilter_put(tp, fh);
Vlad Buslovc431f892018-05-31 09:52:53 +03002121 NL_SET_ERR_MSG(extack, "Filter already exists");
2122 err = -EEXIST;
2123 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
Jiri Pirko9f407f12018-07-23 09:23:07 +02002126 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2127 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2128 err = -EINVAL;
2129 goto errout;
2130 }
2131
Cong Wang2f7ef2f2014-04-25 13:54:06 -07002132 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05002133 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002134 rtnl_held, extack);
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002135 if (err == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002136 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002137 RTM_NEWTFILTER, false, rtnl_held);
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002138 tfilter_put(tp, fh);
Vlad Buslov503d81d2019-07-21 17:44:12 +03002139 /* q pointer is NULL for shared blocks */
2140 if (q)
2141 q->flags &= ~TCQ_F_CAN_BYPASS;
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144errout:
Vlad Buslov8b646782019-02-11 10:55:41 +02002145 if (err && tp_created)
Vlad Buslov12db03b2019-02-11 10:55:45 +02002146 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
Vlad Buslov726d06122019-02-11 10:55:42 +02002147errout_tp:
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002148 if (chain) {
2149 if (tp && !IS_ERR(tp))
Vlad Buslov12db03b2019-02-11 10:55:45 +02002150 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002151 if (!tp_created)
2152 tcf_chain_put(chain);
2153 }
Vlad Buslov12db03b2019-02-11 10:55:45 +02002154 tcf_block_release(q, block, rtnl_held);
Vlad Buslov470502d2019-02-11 10:55:48 +02002155
2156 if (rtnl_held)
2157 rtnl_unlock();
2158
2159 if (err == -EAGAIN) {
2160 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2161 * of target chain.
2162 */
2163 rtnl_held = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 /* Replay the request. */
2165 goto replay;
Vlad Buslov470502d2019-02-11 10:55:48 +02002166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return err;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002168
2169errout_locked:
2170 mutex_unlock(&chain->filter_chain_lock);
2171 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Vlad Buslovc431f892018-05-31 09:52:53 +03002174static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2175 struct netlink_ext_ack *extack)
2176{
2177 struct net *net = sock_net(skb->sk);
2178 struct nlattr *tca[TCA_MAX + 1];
Cong Wang6f96c3c2019-10-07 13:26:28 -07002179 char name[IFNAMSIZ];
Vlad Buslovc431f892018-05-31 09:52:53 +03002180 struct tcmsg *t;
2181 u32 protocol;
2182 u32 prio;
2183 u32 parent;
2184 u32 chain_index;
2185 struct Qdisc *q = NULL;
2186 struct tcf_chain_info chain_info;
2187 struct tcf_chain *chain = NULL;
Vlad Buslov470502d2019-02-11 10:55:48 +02002188 struct tcf_block *block = NULL;
Vlad Buslovc431f892018-05-31 09:52:53 +03002189 struct tcf_proto *tp = NULL;
2190 unsigned long cl = 0;
2191 void *fh = NULL;
2192 int err;
Vlad Buslov470502d2019-02-11 10:55:48 +02002193 bool rtnl_held = false;
Vlad Buslovc431f892018-05-31 09:52:53 +03002194
2195 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2196 return -EPERM;
2197
Johannes Berg8cb08172019-04-26 14:07:28 +02002198 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2199 rtm_tca_policy, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002200 if (err < 0)
2201 return err;
2202
2203 t = nlmsg_data(n);
2204 protocol = TC_H_MIN(t->tcm_info);
2205 prio = TC_H_MAJ(t->tcm_info);
2206 parent = t->tcm_parent;
2207
2208 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2209 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2210 return -ENOENT;
2211 }
2212
2213 /* Find head of filter chain. */
2214
Vlad Buslov470502d2019-02-11 10:55:48 +02002215 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2216 if (err)
2217 return err;
2218
Cong Wang6f96c3c2019-10-07 13:26:28 -07002219 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2220 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2221 err = -EINVAL;
2222 goto errout;
2223 }
Vlad Buslov470502d2019-02-11 10:55:48 +02002224 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2225 * found), qdisc is not unlocked, classifier type is not specified,
2226 * classifier is not unlocked.
2227 */
2228 if (!prio ||
2229 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
Cong Wang6f96c3c2019-10-07 13:26:28 -07002230 !tcf_proto_is_unlocked(name)) {
Vlad Buslov470502d2019-02-11 10:55:48 +02002231 rtnl_held = true;
2232 rtnl_lock();
2233 }
2234
2235 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2236 if (err)
2237 goto errout;
2238
2239 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2240 extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002241 if (IS_ERR(block)) {
2242 err = PTR_ERR(block);
2243 goto errout;
2244 }
2245
2246 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2247 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2248 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2249 err = -EINVAL;
2250 goto errout;
2251 }
2252 chain = tcf_chain_get(block, chain_index, false);
2253 if (!chain) {
Jiri Pirko5ca8a252018-08-03 11:08:47 +02002254 /* User requested flush on non-existent chain. Nothing to do,
2255 * so just return success.
2256 */
2257 if (prio == 0) {
2258 err = 0;
2259 goto errout;
2260 }
Vlad Buslovc431f892018-05-31 09:52:53 +03002261 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Jiri Pirkob7b42472018-08-27 20:58:44 +02002262 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03002263 goto errout;
2264 }
2265
2266 if (prio == 0) {
2267 tfilter_notify_chain(net, skb, block, q, parent, n,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002268 chain, RTM_DELTFILTER, rtnl_held);
2269 tcf_chain_flush(chain, rtnl_held);
Vlad Buslovc431f892018-05-31 09:52:53 +03002270 err = 0;
2271 goto errout;
2272 }
2273
Vlad Busloved76f5e2019-02-11 10:55:38 +02002274 mutex_lock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002275 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2276 prio, false);
2277 if (!tp || IS_ERR(tp)) {
2278 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03002279 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002280 goto errout_locked;
Vlad Buslovc431f892018-05-31 09:52:53 +03002281 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2282 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2283 err = -EINVAL;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002284 goto errout_locked;
2285 } else if (t->tcm_handle == 0) {
John Hurley59eb87c2019-11-02 14:17:47 +00002286 tcf_proto_signal_destroying(chain, tp);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002287 tcf_chain_tp_remove(chain, &chain_info, tp);
2288 mutex_unlock(&chain->filter_chain_lock);
2289
Vlad Buslov12db03b2019-02-11 10:55:45 +02002290 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002291 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002292 RTM_DELTFILTER, false, rtnl_held);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002293 err = 0;
Vlad Buslovc431f892018-05-31 09:52:53 +03002294 goto errout;
2295 }
Vlad Busloved76f5e2019-02-11 10:55:38 +02002296 mutex_unlock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002297
2298 fh = tp->ops->get(tp, t->tcm_handle);
2299
2300 if (!fh) {
Vlad Busloved76f5e2019-02-11 10:55:38 +02002301 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2302 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03002303 } else {
2304 bool last;
2305
2306 err = tfilter_del_notify(net, skb, n, tp, block,
2307 q, parent, fh, false, &last,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002308 rtnl_held, extack);
2309
Vlad Buslovc431f892018-05-31 09:52:53 +03002310 if (err)
2311 goto errout;
Vlad Buslov8b646782019-02-11 10:55:41 +02002312 if (last)
Vlad Buslov12db03b2019-02-11 10:55:45 +02002313 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002314 }
2315
2316errout:
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002317 if (chain) {
2318 if (tp && !IS_ERR(tp))
Vlad Buslov12db03b2019-02-11 10:55:45 +02002319 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslovc431f892018-05-31 09:52:53 +03002320 tcf_chain_put(chain);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002321 }
Vlad Buslov12db03b2019-02-11 10:55:45 +02002322 tcf_block_release(q, block, rtnl_held);
Vlad Buslov470502d2019-02-11 10:55:48 +02002323
2324 if (rtnl_held)
2325 rtnl_unlock();
2326
Vlad Buslovc431f892018-05-31 09:52:53 +03002327 return err;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002328
2329errout_locked:
2330 mutex_unlock(&chain->filter_chain_lock);
2331 goto errout;
Vlad Buslovc431f892018-05-31 09:52:53 +03002332}
2333
2334static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2335 struct netlink_ext_ack *extack)
2336{
2337 struct net *net = sock_net(skb->sk);
2338 struct nlattr *tca[TCA_MAX + 1];
Cong Wang6f96c3c2019-10-07 13:26:28 -07002339 char name[IFNAMSIZ];
Vlad Buslovc431f892018-05-31 09:52:53 +03002340 struct tcmsg *t;
2341 u32 protocol;
2342 u32 prio;
2343 u32 parent;
2344 u32 chain_index;
2345 struct Qdisc *q = NULL;
2346 struct tcf_chain_info chain_info;
2347 struct tcf_chain *chain = NULL;
Vlad Buslov470502d2019-02-11 10:55:48 +02002348 struct tcf_block *block = NULL;
Vlad Buslovc431f892018-05-31 09:52:53 +03002349 struct tcf_proto *tp = NULL;
2350 unsigned long cl = 0;
2351 void *fh = NULL;
2352 int err;
Vlad Buslov470502d2019-02-11 10:55:48 +02002353 bool rtnl_held = false;
Vlad Buslovc431f892018-05-31 09:52:53 +03002354
Johannes Berg8cb08172019-04-26 14:07:28 +02002355 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2356 rtm_tca_policy, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002357 if (err < 0)
2358 return err;
2359
2360 t = nlmsg_data(n);
2361 protocol = TC_H_MIN(t->tcm_info);
2362 prio = TC_H_MAJ(t->tcm_info);
2363 parent = t->tcm_parent;
2364
2365 if (prio == 0) {
2366 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2367 return -ENOENT;
2368 }
2369
2370 /* Find head of filter chain. */
2371
Vlad Buslov470502d2019-02-11 10:55:48 +02002372 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2373 if (err)
2374 return err;
2375
Cong Wang6f96c3c2019-10-07 13:26:28 -07002376 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2377 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2378 err = -EINVAL;
2379 goto errout;
2380 }
Vlad Buslov470502d2019-02-11 10:55:48 +02002381 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2382 * unlocked, classifier type is not specified, classifier is not
2383 * unlocked.
2384 */
2385 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
Cong Wang6f96c3c2019-10-07 13:26:28 -07002386 !tcf_proto_is_unlocked(name)) {
Vlad Buslov470502d2019-02-11 10:55:48 +02002387 rtnl_held = true;
2388 rtnl_lock();
2389 }
2390
2391 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2392 if (err)
2393 goto errout;
2394
2395 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2396 extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002397 if (IS_ERR(block)) {
2398 err = PTR_ERR(block);
2399 goto errout;
2400 }
2401
2402 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2403 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2404 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2405 err = -EINVAL;
2406 goto errout;
2407 }
2408 chain = tcf_chain_get(block, chain_index, false);
2409 if (!chain) {
2410 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2411 err = -EINVAL;
2412 goto errout;
2413 }
2414
Vlad Busloved76f5e2019-02-11 10:55:38 +02002415 mutex_lock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002416 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2417 prio, false);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002418 mutex_unlock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002419 if (!tp || IS_ERR(tp)) {
2420 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03002421 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03002422 goto errout;
2423 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2424 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2425 err = -EINVAL;
2426 goto errout;
2427 }
2428
2429 fh = tp->ops->get(tp, t->tcm_handle);
2430
2431 if (!fh) {
2432 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2433 err = -ENOENT;
2434 } else {
2435 err = tfilter_notify(net, skb, n, tp, block, q, parent,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002436 fh, RTM_NEWTFILTER, true, rtnl_held);
Vlad Buslovc431f892018-05-31 09:52:53 +03002437 if (err < 0)
2438 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2439 }
2440
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002441 tfilter_put(tp, fh);
Vlad Buslovc431f892018-05-31 09:52:53 +03002442errout:
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002443 if (chain) {
2444 if (tp && !IS_ERR(tp))
Vlad Buslov12db03b2019-02-11 10:55:45 +02002445 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslovc431f892018-05-31 09:52:53 +03002446 tcf_chain_put(chain);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002447 }
Vlad Buslov12db03b2019-02-11 10:55:45 +02002448 tcf_block_release(q, block, rtnl_held);
Vlad Buslov470502d2019-02-11 10:55:48 +02002449
2450 if (rtnl_held)
2451 rtnl_unlock();
2452
Vlad Buslovc431f892018-05-31 09:52:53 +03002453 return err;
2454}
2455
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002456struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct tcf_walker w;
2458 struct sk_buff *skb;
2459 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002460 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002461 struct Qdisc *q;
2462 u32 parent;
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002463 bool terse_dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464};
2465
WANG Cong8113c092017-08-04 21:31:43 -07002466static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002468 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08002469 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002471 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002472 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04002473 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002474 RTM_NEWTFILTER, a->terse_dump, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002477static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2478 struct sk_buff *skb, struct netlink_callback *cb,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002479 long index_start, long *p_index, bool terse)
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002480{
2481 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002482 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002483 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002484 struct tcf_proto *tp, *tp_prev;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002485 struct tcf_dump_args arg;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002486
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002487 for (tp = __tcf_get_next_proto(chain, NULL);
2488 tp;
2489 tp_prev = tp,
2490 tp = __tcf_get_next_proto(chain, tp),
Vlad Buslov12db03b2019-02-11 10:55:45 +02002491 tcf_proto_put(tp_prev, true, NULL),
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002492 (*p_index)++) {
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002493 if (*p_index < index_start)
2494 continue;
2495 if (TC_H_MAJ(tcm->tcm_info) &&
2496 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2497 continue;
2498 if (TC_H_MIN(tcm->tcm_info) &&
2499 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2500 continue;
2501 if (*p_index > index_start)
2502 memset(&cb->args[1], 0,
2503 sizeof(cb->args) - sizeof(cb->args[0]));
2504 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08002505 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002506 NETLINK_CB(cb->skb).portid,
2507 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002508 RTM_NEWTFILTER, false, true) <= 0)
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002509 goto errout;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002510 cb->args[1] = 1;
2511 }
2512 if (!tp->ops->walk)
2513 continue;
2514 arg.w.fn = tcf_node_dump;
2515 arg.skb = skb;
2516 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002517 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002518 arg.q = q;
2519 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002520 arg.w.stop = 0;
2521 arg.w.skip = cb->args[1] - 1;
2522 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03002523 arg.w.cookie = cb->args[2];
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002524 arg.terse_dump = terse;
Vlad Buslov12db03b2019-02-11 10:55:45 +02002525 tp->ops->walk(tp, &arg.w, true);
Vlad Buslov01683a12018-07-09 13:29:11 +03002526 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002527 cb->args[1] = arg.w.count + 1;
2528 if (arg.w.stop)
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002529 goto errout;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002530 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02002531 return true;
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002532
2533errout:
Vlad Buslov12db03b2019-02-11 10:55:45 +02002534 tcf_proto_put(tp, true, NULL);
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002535 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002536}
2537
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002538static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2539 [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2540};
2541
Eric Dumazetbd27a872009-11-05 20:57:26 -08002542/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2544{
Vlad Buslovbbf73832019-02-11 10:55:36 +02002545 struct tcf_chain *chain, *chain_prev;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002546 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02002547 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002548 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02002549 struct tcf_block *block;
David S. Miller942b8162012-06-26 21:48:50 -07002550 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002551 bool terse_dump = false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002552 long index_start;
2553 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002554 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002555 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Hong zhi guo573ce262013-03-27 06:47:04 +00002557 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002559
Johannes Berg8cb08172019-04-26 14:07:28 +02002560 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002561 tcf_tfilter_dump_policy, cb->extack);
Jiri Pirko5bc17012017-05-17 11:08:01 +02002562 if (err)
2563 return err;
2564
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002565 if (tca[TCA_DUMP_FLAGS]) {
2566 struct nla_bitfield32 flags =
2567 nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2568
2569 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2570 }
2571
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002572 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002573 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002574 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07002575 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01002576 /* If we work with block index, q is NULL and parent value
2577 * will never be used in the following code. The check
2578 * in tcf_fill_node prevents it. However, compiler does not
2579 * see that far, so set parent to zero to silence the warning
2580 * about parent being uninitialized.
2581 */
2582 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002583 } else {
2584 const struct Qdisc_class_ops *cops;
2585 struct net_device *dev;
2586 unsigned long cl = 0;
2587
2588 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2589 if (!dev)
2590 return skb->len;
2591
2592 parent = tcm->tcm_parent;
Cong Wanga7df4872020-04-30 20:53:49 -07002593 if (!parent)
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002594 q = dev->qdisc;
Cong Wanga7df4872020-04-30 20:53:49 -07002595 else
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002596 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002597 if (!q)
2598 goto out;
2599 cops = q->ops->cl_ops;
2600 if (!cops)
2601 goto out;
2602 if (!cops->tcf_block)
2603 goto out;
2604 if (TC_H_MIN(tcm->tcm_parent)) {
2605 cl = cops->find(q, tcm->tcm_parent);
2606 if (cl == 0)
2607 goto out;
2608 }
2609 block = cops->tcf_block(q, cl, NULL);
2610 if (!block)
2611 goto out;
Cong Wanga7df4872020-04-30 20:53:49 -07002612 parent = block->classid;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002613 if (tcf_block_shared(block))
2614 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002617 index_start = cb->args[0];
2618 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002619
Vlad Buslovbbf73832019-02-11 10:55:36 +02002620 for (chain = __tcf_get_next_chain(block, NULL);
2621 chain;
2622 chain_prev = chain,
2623 chain = __tcf_get_next_chain(block, chain),
2624 tcf_chain_put(chain_prev)) {
Jiri Pirko5bc17012017-05-17 11:08:01 +02002625 if (tca[TCA_CHAIN] &&
2626 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2627 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002628 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002629 index_start, &index, terse_dump)) {
Vlad Buslovbbf73832019-02-11 10:55:36 +02002630 tcf_chain_put(chain);
Roman Kapl5ae437a2018-02-19 21:32:51 +01002631 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002632 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01002633 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02002634 }
2635
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002636 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
Vlad Buslov12db03b2019-02-11 10:55:45 +02002637 tcf_block_refcnt_put(block, true);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002638 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01002641 /* If we did no progress, the error (EMSGSIZE) is real */
2642 if (skb->len == 0 && err)
2643 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return skb->len;
2645}
2646
Vlad Buslova5654822019-02-11 10:55:37 +02002647static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2648 void *tmplt_priv, u32 chain_index,
2649 struct net *net, struct sk_buff *skb,
2650 struct tcf_block *block,
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002651 u32 portid, u32 seq, u16 flags, int event)
2652{
2653 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02002654 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002655 struct nlmsghdr *nlh;
2656 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02002657 void *priv;
2658
Vlad Buslova5654822019-02-11 10:55:37 +02002659 ops = tmplt_ops;
2660 priv = tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002661
2662 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2663 if (!nlh)
2664 goto out_nlmsg_trim;
2665 tcm = nlmsg_data(nlh);
2666 tcm->tcm_family = AF_UNSPEC;
2667 tcm->tcm__pad1 = 0;
2668 tcm->tcm__pad2 = 0;
2669 tcm->tcm_handle = 0;
2670 if (block->q) {
2671 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2672 tcm->tcm_parent = block->q->handle;
2673 } else {
2674 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2675 tcm->tcm_block_index = block->index;
2676 }
2677
Vlad Buslova5654822019-02-11 10:55:37 +02002678 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002679 goto nla_put_failure;
2680
Jiri Pirko9f407f12018-07-23 09:23:07 +02002681 if (ops) {
2682 if (nla_put_string(skb, TCA_KIND, ops->kind))
2683 goto nla_put_failure;
2684 if (ops->tmplt_dump(skb, net, priv) < 0)
2685 goto nla_put_failure;
2686 }
2687
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002688 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2689 return skb->len;
2690
2691out_nlmsg_trim:
2692nla_put_failure:
2693 nlmsg_trim(skb, b);
2694 return -EMSGSIZE;
2695}
2696
2697static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2698 u32 seq, u16 flags, int event, bool unicast)
2699{
2700 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2701 struct tcf_block *block = chain->block;
2702 struct net *net = block->net;
2703 struct sk_buff *skb;
Zhike Wang5b5f99b2019-03-11 03:15:54 -07002704 int err = 0;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002705
2706 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2707 if (!skb)
2708 return -ENOBUFS;
2709
Vlad Buslova5654822019-02-11 10:55:37 +02002710 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2711 chain->index, net, skb, block, portid,
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002712 seq, flags, event) <= 0) {
2713 kfree_skb(skb);
2714 return -EINVAL;
2715 }
2716
2717 if (unicast)
Zhike Wang5b5f99b2019-03-11 03:15:54 -07002718 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2719 else
2720 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2721 flags & NLM_F_ECHO);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002722
Zhike Wang5b5f99b2019-03-11 03:15:54 -07002723 if (err > 0)
2724 err = 0;
2725 return err;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002726}
2727
Vlad Buslova5654822019-02-11 10:55:37 +02002728static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2729 void *tmplt_priv, u32 chain_index,
2730 struct tcf_block *block, struct sk_buff *oskb,
2731 u32 seq, u16 flags, bool unicast)
2732{
2733 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2734 struct net *net = block->net;
2735 struct sk_buff *skb;
2736
2737 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2738 if (!skb)
2739 return -ENOBUFS;
2740
2741 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2742 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2743 kfree_skb(skb);
2744 return -EINVAL;
2745 }
2746
2747 if (unicast)
2748 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2749
2750 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2751}
2752
Jiri Pirko9f407f12018-07-23 09:23:07 +02002753static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2754 struct nlattr **tca,
2755 struct netlink_ext_ack *extack)
2756{
2757 const struct tcf_proto_ops *ops;
Eric Dumazet2dd56162019-12-07 11:34:45 -08002758 char name[IFNAMSIZ];
Jiri Pirko9f407f12018-07-23 09:23:07 +02002759 void *tmplt_priv;
2760
2761 /* If kind is not set, user did not specify template. */
2762 if (!tca[TCA_KIND])
2763 return 0;
2764
Eric Dumazet2dd56162019-12-07 11:34:45 -08002765 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2766 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2767 return -EINVAL;
2768 }
2769
2770 ops = tcf_proto_lookup_ops(name, true, extack);
Jiri Pirko9f407f12018-07-23 09:23:07 +02002771 if (IS_ERR(ops))
2772 return PTR_ERR(ops);
2773 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2774 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2775 return -EOPNOTSUPP;
2776 }
2777
2778 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2779 if (IS_ERR(tmplt_priv)) {
2780 module_put(ops->owner);
2781 return PTR_ERR(tmplt_priv);
2782 }
2783 chain->tmplt_ops = ops;
2784 chain->tmplt_priv = tmplt_priv;
2785 return 0;
2786}
2787
Vlad Buslova5654822019-02-11 10:55:37 +02002788static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2789 void *tmplt_priv)
Jiri Pirko9f407f12018-07-23 09:23:07 +02002790{
Jiri Pirko9f407f12018-07-23 09:23:07 +02002791 /* If template ops are set, no work to do for us. */
Vlad Buslova5654822019-02-11 10:55:37 +02002792 if (!tmplt_ops)
Jiri Pirko9f407f12018-07-23 09:23:07 +02002793 return;
2794
Vlad Buslova5654822019-02-11 10:55:37 +02002795 tmplt_ops->tmplt_destroy(tmplt_priv);
2796 module_put(tmplt_ops->owner);
Jiri Pirko9f407f12018-07-23 09:23:07 +02002797}
2798
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002799/* Add/delete/get a chain */
2800
2801static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2802 struct netlink_ext_ack *extack)
2803{
2804 struct net *net = sock_net(skb->sk);
2805 struct nlattr *tca[TCA_MAX + 1];
2806 struct tcmsg *t;
2807 u32 parent;
2808 u32 chain_index;
Eric Dumazete7be5692022-01-31 09:20:18 -08002809 struct Qdisc *q;
2810 struct tcf_chain *chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002811 struct tcf_block *block;
2812 unsigned long cl;
2813 int err;
2814
2815 if (n->nlmsg_type != RTM_GETCHAIN &&
2816 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2817 return -EPERM;
2818
2819replay:
Eric Dumazete7be5692022-01-31 09:20:18 -08002820 q = NULL;
Johannes Berg8cb08172019-04-26 14:07:28 +02002821 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2822 rtm_tca_policy, extack);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002823 if (err < 0)
2824 return err;
2825
2826 t = nlmsg_data(n);
2827 parent = t->tcm_parent;
2828 cl = 0;
2829
2830 block = tcf_block_find(net, &q, &parent, &cl,
2831 t->tcm_ifindex, t->tcm_block_index, extack);
2832 if (IS_ERR(block))
2833 return PTR_ERR(block);
2834
2835 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2836 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2837 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002838 err = -EINVAL;
2839 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002840 }
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002841
2842 mutex_lock(&block->lock);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002843 chain = tcf_chain_lookup(block, chain_index);
2844 if (n->nlmsg_type == RTM_NEWCHAIN) {
2845 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002846 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002847 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002848 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002849 */
2850 tcf_chain_hold(chain);
2851 } else {
2852 NL_SET_ERR_MSG(extack, "Filter chain already exists");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002853 err = -EEXIST;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002854 goto errout_block_locked;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002855 }
2856 } else {
2857 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2858 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 +03002859 err = -ENOENT;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002860 goto errout_block_locked;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002861 }
2862 chain = tcf_chain_create(block, chain_index);
2863 if (!chain) {
2864 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002865 err = -ENOMEM;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002866 goto errout_block_locked;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002867 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002868 }
2869 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002870 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002871 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002872 err = -EINVAL;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002873 goto errout_block_locked;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002874 }
2875 tcf_chain_hold(chain);
2876 }
2877
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002878 if (n->nlmsg_type == RTM_NEWCHAIN) {
2879 /* Modifying chain requires holding parent block lock. In case
2880 * the chain was successfully added, take a reference to the
2881 * chain. This ensures that an empty chain does not disappear at
2882 * the end of this function.
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002883 */
2884 tcf_chain_hold(chain);
2885 chain->explicitly_created = true;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002886 }
2887 mutex_unlock(&block->lock);
2888
2889 switch (n->nlmsg_type) {
2890 case RTM_NEWCHAIN:
2891 err = tc_chain_tmplt_add(chain, net, tca, extack);
2892 if (err) {
2893 tcf_chain_put_explicitly_created(chain);
2894 goto errout;
2895 }
2896
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002897 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2898 RTM_NEWCHAIN, false);
2899 break;
2900 case RTM_DELCHAIN:
Cong Wangf5b9bac2018-09-11 14:22:23 -07002901 tfilter_notify_chain(net, skb, block, q, parent, n,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002902 chain, RTM_DELTFILTER, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002903 /* Flush the chain first as the user requested chain removal. */
Vlad Buslov12db03b2019-02-11 10:55:45 +02002904 tcf_chain_flush(chain, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002905 /* In case the chain was successfully deleted, put a reference
2906 * to the chain previously taken during addition.
2907 */
2908 tcf_chain_put_explicitly_created(chain);
2909 break;
2910 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002911 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
Yajun Deng9e037392021-07-22 11:23:43 +08002912 n->nlmsg_flags, n->nlmsg_type, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002913 if (err < 0)
2914 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2915 break;
2916 default:
2917 err = -EOPNOTSUPP;
2918 NL_SET_ERR_MSG(extack, "Unsupported message type");
2919 goto errout;
2920 }
2921
2922errout:
2923 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03002924errout_block:
Vlad Buslov12db03b2019-02-11 10:55:45 +02002925 tcf_block_release(q, block, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002926 if (err == -EAGAIN)
2927 /* Replay the request. */
2928 goto replay;
2929 return err;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002930
2931errout_block_locked:
2932 mutex_unlock(&block->lock);
2933 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002934}
2935
2936/* called with RTNL */
2937static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2938{
2939 struct net *net = sock_net(skb->sk);
2940 struct nlattr *tca[TCA_MAX + 1];
2941 struct Qdisc *q = NULL;
2942 struct tcf_block *block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002943 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Vlad Buslovace4a262019-02-25 17:45:44 +02002944 struct tcf_chain *chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002945 long index_start;
2946 long index;
2947 u32 parent;
2948 int err;
2949
2950 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2951 return skb->len;
2952
Johannes Berg8cb08172019-04-26 14:07:28 +02002953 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2954 rtm_tca_policy, cb->extack);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002955 if (err)
2956 return err;
2957
2958 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002959 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002960 if (!block)
2961 goto out;
2962 /* If we work with block index, q is NULL and parent value
2963 * will never be used in the following code. The check
2964 * in tcf_fill_node prevents it. However, compiler does not
2965 * see that far, so set parent to zero to silence the warning
2966 * about parent being uninitialized.
2967 */
2968 parent = 0;
2969 } else {
2970 const struct Qdisc_class_ops *cops;
2971 struct net_device *dev;
2972 unsigned long cl = 0;
2973
2974 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2975 if (!dev)
2976 return skb->len;
2977
2978 parent = tcm->tcm_parent;
2979 if (!parent) {
2980 q = dev->qdisc;
2981 parent = q->handle;
2982 } else {
2983 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2984 }
2985 if (!q)
2986 goto out;
2987 cops = q->ops->cl_ops;
2988 if (!cops)
2989 goto out;
2990 if (!cops->tcf_block)
2991 goto out;
2992 if (TC_H_MIN(tcm->tcm_parent)) {
2993 cl = cops->find(q, tcm->tcm_parent);
2994 if (cl == 0)
2995 goto out;
2996 }
2997 block = cops->tcf_block(q, cl, NULL);
2998 if (!block)
2999 goto out;
3000 if (tcf_block_shared(block))
3001 q = NULL;
3002 }
3003
3004 index_start = cb->args[0];
3005 index = 0;
3006
Vlad Buslovace4a262019-02-25 17:45:44 +02003007 mutex_lock(&block->lock);
3008 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003009 if ((tca[TCA_CHAIN] &&
3010 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3011 continue;
3012 if (index < index_start) {
3013 index++;
3014 continue;
3015 }
Vlad Buslovace4a262019-02-25 17:45:44 +02003016 if (tcf_chain_held_by_acts_only(chain))
3017 continue;
Vlad Buslova5654822019-02-11 10:55:37 +02003018 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3019 chain->index, net, skb, block,
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003020 NETLINK_CB(cb->skb).portid,
3021 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3022 RTM_NEWCHAIN);
Vlad Buslovace4a262019-02-25 17:45:44 +02003023 if (err <= 0)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003024 break;
3025 index++;
3026 }
Vlad Buslovace4a262019-02-25 17:45:44 +02003027 mutex_unlock(&block->lock);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003028
Vlad Buslov787ce6d2018-09-24 19:22:58 +03003029 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
Vlad Buslov12db03b2019-02-11 10:55:45 +02003030 tcf_block_refcnt_put(block, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003031 cb->args[0] = index;
3032
3033out:
3034 /* If we did no progress, the error (EMSGSIZE) is real */
3035 if (skb->len == 0 && err)
3036 return err;
3037 return skb->len;
3038}
3039
WANG Cong18d02642014-09-25 10:26:37 -07003040void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
3042#ifdef CONFIG_NET_CLS_ACT
Eric Dumazet3d66b892019-09-18 12:57:04 -07003043 if (exts->actions) {
3044 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3045 kfree(exts->actions);
3046 }
WANG Cong22dc13c2016-08-13 22:35:00 -07003047 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048#endif
3049}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003050EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00003052int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05003053 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
Vlad Buslovec6743a2019-02-11 10:55:43 +02003054 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056#ifdef CONFIG_NET_CLS_ACT
3057 {
Vlad Buslov81692c62021-04-07 18:36:03 +03003058 int init_res[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05003060 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
WANG Cong5da57f42013-12-15 20:15:07 -08003062 if (exts->police && tb[exts->police]) {
Cong Wanga3b6f3a2021-01-16 16:56:57 -08003063 struct tc_action_ops *a_o;
3064
3065 a_o = tc_action_load_ops("police", tb[exts->police], rtnl_held, extack);
3066 if (IS_ERR(a_o))
3067 return PTR_ERR(a_o);
Jiri Pirko9fb9f252017-05-17 11:08:02 +02003068 act = tcf_action_init_1(net, tp, tb[exts->police],
3069 rate_tlv, "police", ovr,
Vlad Buslov81692c62021-04-07 18:36:03 +03003070 TCA_ACT_BIND, a_o, init_res,
3071 rtnl_held, extack);
Vlad Buslov4a78ae12021-04-07 18:36:04 +03003072 module_put(a_o->owner);
3073 if (IS_ERR(act))
Patrick McHardyab27cfb2008-01-23 20:33:13 -08003074 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
WANG Cong33be6272013-12-15 20:15:05 -08003076 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07003077 exts->actions[0] = act;
3078 exts->nr_actions = 1;
Vlad Buslovbba8ef22021-02-16 18:22:00 +02003079 tcf_idr_insert_many(exts->actions);
WANG Cong5da57f42013-12-15 20:15:07 -08003080 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03003081 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07003082
Jiri Pirko9fb9f252017-05-17 11:08:02 +02003083 err = tcf_action_init(net, tp, tb[exts->action],
3084 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov81692c62021-04-07 18:36:03 +03003085 exts->actions, init_res,
3086 &attr_size, rtnl_held, extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03003087 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08003088 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03003089 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
3091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092#else
WANG Cong5da57f42013-12-15 20:15:07 -08003093 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05003094 (exts->police && tb[exts->police])) {
3095 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05003097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098#endif
3099
3100 return 0;
3101}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003102EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Jiri Pirko9b0d4442017-08-04 14:29:15 +02003104void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07003107 struct tcf_exts old = *dst;
3108
Jiri Pirko9b0d4442017-08-04 14:29:15 +02003109 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07003110 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111#endif
3112}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003113EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
WANG Cong22dc13c2016-08-13 22:35:00 -07003115#ifdef CONFIG_NET_CLS_ACT
3116static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3117{
3118 if (exts->nr_actions == 0)
3119 return NULL;
3120 else
3121 return exts->actions[0];
3122}
3123#endif
WANG Cong33be6272013-12-15 20:15:05 -08003124
WANG Cong5da57f42013-12-15 20:15:07 -08003125int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
3127#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07003128 struct nlattr *nest;
3129
Jiri Pirko978dfd82017-08-04 14:29:03 +02003130 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 /*
3132 * again for backward compatible mode - we want
3133 * to work with both old and new modes of entering
3134 * tc data even if iproute2 was newer - jhs
3135 */
WANG Cong33be6272013-12-15 20:15:05 -08003136 if (exts->type != TCA_OLD_COMPAT) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003137 nest = nla_nest_start_noflag(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003138 if (nest == NULL)
3139 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07003140
Vlad Buslovca44b732020-05-15 14:40:12 +03003141 if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3142 < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08003143 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003144 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08003145 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08003146 struct tc_action *act = tcf_exts_first_act(exts);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003147 nest = nla_nest_start_noflag(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05003148 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003149 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08003150 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08003151 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003152 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
3154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07003156
3157nla_put_failure:
3158 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07003160#else
3161 return 0;
3162#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003164EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Vlad Buslovca44b732020-05-15 14:40:12 +03003166int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3167{
3168#ifdef CONFIG_NET_CLS_ACT
3169 struct nlattr *nest;
3170
3171 if (!exts->action || !tcf_exts_has_actions(exts))
3172 return 0;
3173
3174 nest = nla_nest_start_noflag(skb, exts->action);
3175 if (!nest)
3176 goto nla_put_failure;
3177
3178 if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3179 goto nla_put_failure;
3180 nla_nest_end(skb, nest);
3181 return 0;
3182
3183nla_put_failure:
3184 nla_nest_cancel(skb, nest);
3185 return -1;
3186#else
3187 return 0;
3188#endif
3189}
3190EXPORT_SYMBOL(tcf_exts_terse_dump);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003191
WANG Cong5da57f42013-12-15 20:15:07 -08003192int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08003195 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01003196 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08003197 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198#endif
3199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003201EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Vlad Buslov40119212019-08-26 16:44:59 +03003203static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3204{
3205 if (*flags & TCA_CLS_FLAGS_IN_HW)
3206 return;
3207 *flags |= TCA_CLS_FLAGS_IN_HW;
3208 atomic_inc(&block->offloadcnt);
3209}
3210
3211static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3212{
3213 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3214 return;
3215 *flags &= ~TCA_CLS_FLAGS_IN_HW;
3216 atomic_dec(&block->offloadcnt);
3217}
3218
3219static void tc_cls_offload_cnt_update(struct tcf_block *block,
3220 struct tcf_proto *tp, u32 *cnt,
3221 u32 *flags, u32 diff, bool add)
3222{
3223 lockdep_assert_held(&block->cb_lock);
3224
3225 spin_lock(&tp->lock);
3226 if (add) {
3227 if (!*cnt)
3228 tcf_block_offload_inc(block, flags);
3229 *cnt += diff;
3230 } else {
3231 *cnt -= diff;
3232 if (!*cnt)
3233 tcf_block_offload_dec(block, flags);
3234 }
3235 spin_unlock(&tp->lock);
3236}
3237
3238static void
3239tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3240 u32 *cnt, u32 *flags)
3241{
3242 lockdep_assert_held(&block->cb_lock);
3243
3244 spin_lock(&tp->lock);
3245 tcf_block_offload_dec(block, flags);
3246 *cnt = 0;
3247 spin_unlock(&tp->lock);
3248}
3249
3250static int
3251__tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3252 void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02003253{
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02003254 struct flow_block_cb *block_cb;
Cong Wangaeb3fec2018-12-11 11:15:46 -08003255 int ok_count = 0;
3256 int err;
3257
Vlad Buslov40119212019-08-26 16:44:59 +03003258 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3259 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3260 if (err) {
3261 if (err_stop)
3262 return err;
3263 } else {
3264 ok_count++;
3265 }
3266 }
3267 return ok_count;
3268}
3269
3270int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3271 void *type_data, bool err_stop, bool rtnl_held)
3272{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003273 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003274 int ok_count;
3275
Vlad Buslov11bd6342019-08-26 16:45:02 +03003276retry:
3277 if (take_rtnl)
3278 rtnl_lock();
Vlad Buslov40119212019-08-26 16:44:59 +03003279 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003280 /* Need to obtain rtnl lock if block is bound to devs that require it.
3281 * In block bind code cb_lock is obtained while holding rtnl, so we must
3282 * obtain the locks in same order here.
3283 */
3284 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3285 up_read(&block->cb_lock);
3286 take_rtnl = true;
3287 goto retry;
3288 }
3289
Vlad Buslov40119212019-08-26 16:44:59 +03003290 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003291
Vlad Buslov40119212019-08-26 16:44:59 +03003292 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003293 if (take_rtnl)
3294 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003295 return ok_count;
3296}
3297EXPORT_SYMBOL(tc_setup_cb_call);
3298
3299/* Non-destructive filter add. If filter that wasn't already in hardware is
3300 * successfully offloaded, increment block offloads counter. On failure,
3301 * previously offloaded filter is considered to be intact and offloads counter
3302 * is not decremented.
3303 */
3304
3305int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3306 enum tc_setup_type type, void *type_data, bool err_stop,
3307 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3308{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003309 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003310 int ok_count;
3311
Vlad Buslov11bd6342019-08-26 16:45:02 +03003312retry:
3313 if (take_rtnl)
3314 rtnl_lock();
Vlad Buslov4f8116c2019-08-26 16:44:57 +03003315 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003316 /* Need to obtain rtnl lock if block is bound to devs that require it.
3317 * In block bind code cb_lock is obtained while holding rtnl, so we must
3318 * obtain the locks in same order here.
3319 */
3320 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3321 up_read(&block->cb_lock);
3322 take_rtnl = true;
3323 goto retry;
3324 }
3325
Cong Wangaeb3fec2018-12-11 11:15:46 -08003326 /* Make sure all netdevs sharing this block are offload-capable. */
Vlad Buslov4f8116c2019-08-26 16:44:57 +03003327 if (block->nooffloaddevcnt && err_stop) {
3328 ok_count = -EOPNOTSUPP;
3329 goto err_unlock;
3330 }
Cong Wangaeb3fec2018-12-11 11:15:46 -08003331
Vlad Buslov40119212019-08-26 16:44:59 +03003332 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003333 if (ok_count < 0)
3334 goto err_unlock;
3335
3336 if (tp->ops->hw_add)
3337 tp->ops->hw_add(tp, type_data);
Vlad Buslov40119212019-08-26 16:44:59 +03003338 if (ok_count > 0)
3339 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3340 ok_count, true);
Vlad Buslov4f8116c2019-08-26 16:44:57 +03003341err_unlock:
3342 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003343 if (take_rtnl)
3344 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003345 return ok_count < 0 ? ok_count : 0;
Jiri Pirko717503b2017-10-11 09:41:09 +02003346}
Vlad Buslov40119212019-08-26 16:44:59 +03003347EXPORT_SYMBOL(tc_setup_cb_add);
3348
3349/* Destructive filter replace. If filter that wasn't already in hardware is
3350 * successfully offloaded, increment block offload counter. On failure,
3351 * previously offloaded filter is considered to be destroyed and offload counter
3352 * is decremented.
3353 */
3354
3355int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3356 enum tc_setup_type type, void *type_data, bool err_stop,
3357 u32 *old_flags, unsigned int *old_in_hw_count,
3358 u32 *new_flags, unsigned int *new_in_hw_count,
3359 bool rtnl_held)
3360{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003361 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003362 int ok_count;
3363
Vlad Buslov11bd6342019-08-26 16:45:02 +03003364retry:
3365 if (take_rtnl)
3366 rtnl_lock();
Vlad Buslov40119212019-08-26 16:44:59 +03003367 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003368 /* Need to obtain rtnl lock if block is bound to devs that require it.
3369 * In block bind code cb_lock is obtained while holding rtnl, so we must
3370 * obtain the locks in same order here.
3371 */
3372 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3373 up_read(&block->cb_lock);
3374 take_rtnl = true;
3375 goto retry;
3376 }
3377
Vlad Buslov40119212019-08-26 16:44:59 +03003378 /* Make sure all netdevs sharing this block are offload-capable. */
3379 if (block->nooffloaddevcnt && err_stop) {
3380 ok_count = -EOPNOTSUPP;
3381 goto err_unlock;
3382 }
3383
3384 tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003385 if (tp->ops->hw_del)
3386 tp->ops->hw_del(tp, type_data);
Vlad Buslov40119212019-08-26 16:44:59 +03003387
3388 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003389 if (ok_count < 0)
3390 goto err_unlock;
3391
3392 if (tp->ops->hw_add)
3393 tp->ops->hw_add(tp, type_data);
Vlad Buslov40119212019-08-26 16:44:59 +03003394 if (ok_count > 0)
Vlad Buslova449a3e2019-08-26 16:45:00 +03003395 tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3396 new_flags, ok_count, true);
Vlad Buslov40119212019-08-26 16:44:59 +03003397err_unlock:
3398 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003399 if (take_rtnl)
3400 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003401 return ok_count < 0 ? ok_count : 0;
3402}
3403EXPORT_SYMBOL(tc_setup_cb_replace);
3404
3405/* Destroy filter and decrement block offload counter, if filter was previously
3406 * offloaded.
3407 */
3408
3409int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3410 enum tc_setup_type type, void *type_data, bool err_stop,
3411 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3412{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003413 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003414 int ok_count;
3415
Vlad Buslov11bd6342019-08-26 16:45:02 +03003416retry:
3417 if (take_rtnl)
3418 rtnl_lock();
Vlad Buslov40119212019-08-26 16:44:59 +03003419 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003420 /* Need to obtain rtnl lock if block is bound to devs that require it.
3421 * In block bind code cb_lock is obtained while holding rtnl, so we must
3422 * obtain the locks in same order here.
3423 */
3424 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3425 up_read(&block->cb_lock);
3426 take_rtnl = true;
3427 goto retry;
3428 }
3429
Vlad Buslov40119212019-08-26 16:44:59 +03003430 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3431
3432 tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003433 if (tp->ops->hw_del)
3434 tp->ops->hw_del(tp, type_data);
3435
Vlad Buslov40119212019-08-26 16:44:59 +03003436 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003437 if (take_rtnl)
3438 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003439 return ok_count < 0 ? ok_count : 0;
3440}
3441EXPORT_SYMBOL(tc_setup_cb_destroy);
3442
3443int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3444 bool add, flow_setup_cb_t *cb,
3445 enum tc_setup_type type, void *type_data,
3446 void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3447{
3448 int err = cb(type, type_data, cb_priv);
3449
3450 if (err) {
3451 if (add && tc_skip_sw(*flags))
3452 return err;
3453 } else {
3454 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3455 add);
3456 }
3457
3458 return 0;
3459}
3460EXPORT_SYMBOL(tc_setup_cb_reoffload);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02003461
Jiri Pirko20084952020-02-25 11:45:18 +01003462static int tcf_act_get_cookie(struct flow_action_entry *entry,
3463 const struct tc_action *act)
3464{
3465 struct tc_cookie *cookie;
3466 int err = 0;
3467
3468 rcu_read_lock();
3469 cookie = rcu_dereference(act->act_cookie);
3470 if (cookie) {
3471 entry->cookie = flow_action_cookie_create(cookie->data,
3472 cookie->len,
3473 GFP_ATOMIC);
3474 if (!entry->cookie)
3475 err = -ENOMEM;
3476 }
3477 rcu_read_unlock();
3478 return err;
3479}
3480
3481static void tcf_act_put_cookie(struct flow_action_entry *entry)
3482{
3483 flow_action_cookie_destroy(entry->cookie);
3484}
3485
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03003486void tc_cleanup_flow_action(struct flow_action *flow_action)
3487{
3488 struct flow_action_entry *entry;
3489 int i;
3490
Jiri Pirko20084952020-02-25 11:45:18 +01003491 flow_action_for_each(i, entry, flow_action) {
3492 tcf_act_put_cookie(entry);
Vlad Buslov11589582019-09-13 18:28:39 +03003493 if (entry->destructor)
3494 entry->destructor(entry->destructor_priv);
Jiri Pirko20084952020-02-25 11:45:18 +01003495 }
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03003496}
3497EXPORT_SYMBOL(tc_cleanup_flow_action);
3498
Vlad Buslov11589582019-09-13 18:28:39 +03003499static void tcf_mirred_get_dev(struct flow_action_entry *entry,
3500 const struct tc_action *act)
3501{
Vlad Buslov470d5062019-09-13 18:28:41 +03003502#ifdef CONFIG_NET_CLS_ACT
3503 entry->dev = act->ops->get_dev(act, &entry->destructor);
Vlad Buslov11589582019-09-13 18:28:39 +03003504 if (!entry->dev)
3505 return;
Vlad Buslov11589582019-09-13 18:28:39 +03003506 entry->destructor_priv = entry->dev;
Vlad Buslov470d5062019-09-13 18:28:41 +03003507#endif
Vlad Buslov11589582019-09-13 18:28:39 +03003508}
3509
3510static void tcf_tunnel_encap_put_tunnel(void *priv)
3511{
3512 struct ip_tunnel_info *tunnel = priv;
3513
3514 kfree(tunnel);
3515}
3516
3517static int tcf_tunnel_encap_get_tunnel(struct flow_action_entry *entry,
3518 const struct tc_action *act)
3519{
3520 entry->tunnel = tcf_tunnel_info_copy(act);
3521 if (!entry->tunnel)
3522 return -ENOMEM;
3523 entry->destructor = tcf_tunnel_encap_put_tunnel;
3524 entry->destructor_priv = entry->tunnel;
3525 return 0;
3526}
3527
Vlad Buslov4a5da472019-09-13 18:28:40 +03003528static void tcf_sample_get_group(struct flow_action_entry *entry,
3529 const struct tc_action *act)
3530{
3531#ifdef CONFIG_NET_CLS_ACT
3532 entry->sample.psample_group =
3533 act->ops->get_psample_group(act, &entry->destructor);
3534 entry->destructor_priv = entry->sample.psample_group;
3535#endif
3536}
3537
Po Liud29bdd62020-05-01 08:53:16 +08003538static void tcf_gate_entry_destructor(void *priv)
3539{
3540 struct action_gate_entry *oe = priv;
3541
3542 kfree(oe);
3543}
3544
3545static int tcf_gate_get_entries(struct flow_action_entry *entry,
3546 const struct tc_action *act)
3547{
3548 entry->gate.entries = tcf_gate_get_list(act);
3549
3550 if (!entry->gate.entries)
3551 return -EINVAL;
3552
3553 entry->destructor = tcf_gate_entry_destructor;
3554 entry->destructor_priv = entry->gate.entries;
3555
3556 return 0;
3557}
3558
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +02003559static enum flow_action_hw_stats tc_act_hw_stats(u8 hw_stats)
3560{
3561 if (WARN_ON_ONCE(hw_stats > TCA_ACT_HW_STATS_ANY))
3562 return FLOW_ACTION_HW_STATS_DONT_CARE;
3563 else if (!hw_stats)
3564 return FLOW_ACTION_HW_STATS_DISABLED;
3565
3566 return hw_stats;
3567}
3568
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003569int tc_setup_flow_action(struct flow_action *flow_action,
Vlad Buslovb15e7a62020-02-17 12:12:12 +02003570 const struct tcf_exts *exts)
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003571{
Vlad Buslov7a472812020-02-17 12:12:09 +02003572 struct tc_action *act;
Vlad Buslov9838b202019-08-26 16:45:03 +03003573 int i, j, k, err = 0;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003574
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07003575 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3576 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3577 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
Jiri Pirko44f86582020-03-07 12:40:20 +01003578
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003579 if (!exts)
3580 return 0;
3581
3582 j = 0;
3583 tcf_exts_for_each_action(i, act, exts) {
3584 struct flow_action_entry *entry;
3585
3586 entry = &flow_action->entries[j];
Vlad Buslov7a472812020-02-17 12:12:09 +02003587 spin_lock_bh(&act->tcfa_lock);
Jiri Pirko20084952020-02-25 11:45:18 +01003588 err = tcf_act_get_cookie(entry, act);
3589 if (err)
3590 goto err_out_locked;
Jiri Pirko44f86582020-03-07 12:40:20 +01003591
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +02003592 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
Jiri Pirko44f86582020-03-07 12:40:20 +01003593
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003594 if (is_tcf_gact_ok(act)) {
3595 entry->id = FLOW_ACTION_ACCEPT;
3596 } else if (is_tcf_gact_shot(act)) {
3597 entry->id = FLOW_ACTION_DROP;
3598 } else if (is_tcf_gact_trap(act)) {
3599 entry->id = FLOW_ACTION_TRAP;
3600 } else if (is_tcf_gact_goto_chain(act)) {
3601 entry->id = FLOW_ACTION_GOTO;
3602 entry->chain_index = tcf_gact_goto_chain_index(act);
3603 } else if (is_tcf_mirred_egress_redirect(act)) {
3604 entry->id = FLOW_ACTION_REDIRECT;
Vlad Buslov11589582019-09-13 18:28:39 +03003605 tcf_mirred_get_dev(entry, act);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003606 } else if (is_tcf_mirred_egress_mirror(act)) {
3607 entry->id = FLOW_ACTION_MIRRED;
Vlad Buslov11589582019-09-13 18:28:39 +03003608 tcf_mirred_get_dev(entry, act);
John Hurley48e584a2019-08-04 16:09:06 +01003609 } else if (is_tcf_mirred_ingress_redirect(act)) {
3610 entry->id = FLOW_ACTION_REDIRECT_INGRESS;
Vlad Buslov11589582019-09-13 18:28:39 +03003611 tcf_mirred_get_dev(entry, act);
John Hurley48e584a2019-08-04 16:09:06 +01003612 } else if (is_tcf_mirred_ingress_mirror(act)) {
3613 entry->id = FLOW_ACTION_MIRRED_INGRESS;
Vlad Buslov11589582019-09-13 18:28:39 +03003614 tcf_mirred_get_dev(entry, act);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003615 } else if (is_tcf_vlan(act)) {
3616 switch (tcf_vlan_action(act)) {
3617 case TCA_VLAN_ACT_PUSH:
3618 entry->id = FLOW_ACTION_VLAN_PUSH;
3619 entry->vlan.vid = tcf_vlan_push_vid(act);
3620 entry->vlan.proto = tcf_vlan_push_proto(act);
3621 entry->vlan.prio = tcf_vlan_push_prio(act);
3622 break;
3623 case TCA_VLAN_ACT_POP:
3624 entry->id = FLOW_ACTION_VLAN_POP;
3625 break;
3626 case TCA_VLAN_ACT_MODIFY:
3627 entry->id = FLOW_ACTION_VLAN_MANGLE;
3628 entry->vlan.vid = tcf_vlan_push_vid(act);
3629 entry->vlan.proto = tcf_vlan_push_proto(act);
3630 entry->vlan.prio = tcf_vlan_push_prio(act);
3631 break;
3632 default:
Vlad Buslov9838b202019-08-26 16:45:03 +03003633 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003634 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003635 }
3636 } else if (is_tcf_tunnel_set(act)) {
3637 entry->id = FLOW_ACTION_TUNNEL_ENCAP;
Vlad Buslov11589582019-09-13 18:28:39 +03003638 err = tcf_tunnel_encap_get_tunnel(entry, act);
3639 if (err)
Vlad Buslov7a472812020-02-17 12:12:09 +02003640 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003641 } else if (is_tcf_tunnel_release(act)) {
3642 entry->id = FLOW_ACTION_TUNNEL_DECAP;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003643 } else if (is_tcf_pedit(act)) {
3644 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
3645 switch (tcf_pedit_cmd(act, k)) {
3646 case TCA_PEDIT_KEY_EX_CMD_SET:
3647 entry->id = FLOW_ACTION_MANGLE;
3648 break;
3649 case TCA_PEDIT_KEY_EX_CMD_ADD:
3650 entry->id = FLOW_ACTION_ADD;
3651 break;
3652 default:
Vlad Buslov9838b202019-08-26 16:45:03 +03003653 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003654 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003655 }
3656 entry->mangle.htype = tcf_pedit_htype(act, k);
3657 entry->mangle.mask = tcf_pedit_mask(act, k);
3658 entry->mangle.val = tcf_pedit_val(act, k);
3659 entry->mangle.offset = tcf_pedit_offset(act, k);
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +02003660 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
Petr Machata2c4b58d2020-03-18 19:42:29 +02003661 entry = &flow_action->entries[++j];
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003662 }
3663 } else if (is_tcf_csum(act)) {
3664 entry->id = FLOW_ACTION_CSUM;
3665 entry->csum_flags = tcf_csum_update_flags(act);
3666 } else if (is_tcf_skbedit_mark(act)) {
3667 entry->id = FLOW_ACTION_MARK;
3668 entry->mark = tcf_skbedit_mark(act);
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -07003669 } else if (is_tcf_sample(act)) {
3670 entry->id = FLOW_ACTION_SAMPLE;
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -07003671 entry->sample.trunc_size = tcf_sample_trunc_size(act);
3672 entry->sample.truncate = tcf_sample_truncate(act);
3673 entry->sample.rate = tcf_sample_rate(act);
Vlad Buslov4a5da472019-09-13 18:28:40 +03003674 tcf_sample_get_group(entry, act);
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -07003675 } else if (is_tcf_police(act)) {
3676 entry->id = FLOW_ACTION_POLICE;
Po Liu5f035af2020-06-29 14:54:16 +08003677 entry->police.burst = tcf_police_burst(act);
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -07003678 entry->police.rate_bytes_ps =
3679 tcf_police_rate_bytes_ps(act);
Po Liu19e528d2020-06-24 17:36:28 +08003680 entry->police.mtu = tcf_police_tcfp_mtu(act);
Po Liu627e39b2020-06-24 17:36:30 +08003681 entry->police.index = act->tcfa_index;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03003682 } else if (is_tcf_ct(act)) {
3683 entry->id = FLOW_ACTION_CT;
3684 entry->ct.action = tcf_ct_action(act);
3685 entry->ct.zone = tcf_ct_zone(act);
Paul Blakeyedd58612020-03-12 12:23:09 +02003686 entry->ct.flow_table = tcf_ct_ft(act);
John Hurley6749d5902019-07-23 15:33:59 +01003687 } else if (is_tcf_mpls(act)) {
3688 switch (tcf_mpls_action(act)) {
3689 case TCA_MPLS_ACT_PUSH:
3690 entry->id = FLOW_ACTION_MPLS_PUSH;
3691 entry->mpls_push.proto = tcf_mpls_proto(act);
3692 entry->mpls_push.label = tcf_mpls_label(act);
3693 entry->mpls_push.tc = tcf_mpls_tc(act);
3694 entry->mpls_push.bos = tcf_mpls_bos(act);
3695 entry->mpls_push.ttl = tcf_mpls_ttl(act);
3696 break;
3697 case TCA_MPLS_ACT_POP:
3698 entry->id = FLOW_ACTION_MPLS_POP;
3699 entry->mpls_pop.proto = tcf_mpls_proto(act);
3700 break;
3701 case TCA_MPLS_ACT_MODIFY:
3702 entry->id = FLOW_ACTION_MPLS_MANGLE;
3703 entry->mpls_mangle.label = tcf_mpls_label(act);
3704 entry->mpls_mangle.tc = tcf_mpls_tc(act);
3705 entry->mpls_mangle.bos = tcf_mpls_bos(act);
3706 entry->mpls_mangle.ttl = tcf_mpls_ttl(act);
3707 break;
3708 default:
Baowen Zheng67f43622021-12-13 15:46:04 +01003709 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003710 goto err_out_locked;
John Hurley6749d5902019-07-23 15:33:59 +01003711 }
John Hurleyfb1b7752019-08-04 16:09:04 +01003712 } else if (is_tcf_skbedit_ptype(act)) {
3713 entry->id = FLOW_ACTION_PTYPE;
3714 entry->ptype = tcf_skbedit_ptype(act);
Petr Machata2ce12412020-03-19 15:47:21 +02003715 } else if (is_tcf_skbedit_priority(act)) {
3716 entry->id = FLOW_ACTION_PRIORITY;
3717 entry->priority = tcf_skbedit_priority(act);
Po Liud29bdd62020-05-01 08:53:16 +08003718 } else if (is_tcf_gate(act)) {
3719 entry->id = FLOW_ACTION_GATE;
3720 entry->gate.index = tcf_gate_index(act);
3721 entry->gate.prio = tcf_gate_prio(act);
3722 entry->gate.basetime = tcf_gate_basetime(act);
3723 entry->gate.cycletime = tcf_gate_cycletime(act);
3724 entry->gate.cycletimeext = tcf_gate_cycletimeext(act);
3725 entry->gate.num_entries = tcf_gate_num_entries(act);
3726 err = tcf_gate_get_entries(entry, act);
3727 if (err)
Guillaume Naultb1307622020-10-20 17:34:31 +02003728 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003729 } else {
Vlad Buslov9838b202019-08-26 16:45:03 +03003730 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003731 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003732 }
Vlad Buslov7a472812020-02-17 12:12:09 +02003733 spin_unlock_bh(&act->tcfa_lock);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003734
3735 if (!is_tcf_pedit(act))
3736 j++;
3737 }
Vlad Buslov9838b202019-08-26 16:45:03 +03003738
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003739err_out:
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03003740 if (err)
3741 tc_cleanup_flow_action(flow_action);
3742
Vlad Buslov9838b202019-08-26 16:45:03 +03003743 return err;
Vlad Buslov7a472812020-02-17 12:12:09 +02003744err_out_locked:
3745 spin_unlock_bh(&act->tcfa_lock);
3746 goto err_out;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003747}
3748EXPORT_SYMBOL(tc_setup_flow_action);
3749
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +01003750unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3751{
3752 unsigned int num_acts = 0;
3753 struct tc_action *act;
3754 int i;
3755
3756 tcf_exts_for_each_action(i, act, exts) {
3757 if (is_tcf_pedit(act))
3758 num_acts += tcf_pedit_nkeys(act);
3759 else
3760 num_acts++;
3761 }
3762 return num_acts;
3763}
3764EXPORT_SYMBOL(tcf_exts_num_actions);
3765
Petr Machata36257502020-06-27 01:45:26 +03003766#ifdef CONFIG_NET_CLS_ACT
3767static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
3768 u32 *p_block_index,
3769 struct netlink_ext_ack *extack)
3770{
3771 *p_block_index = nla_get_u32(block_index_attr);
3772 if (!*p_block_index) {
3773 NL_SET_ERR_MSG(extack, "Block number may not be zero");
3774 return -EINVAL;
3775 }
3776
3777 return 0;
3778}
3779
3780int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
3781 enum flow_block_binder_type binder_type,
3782 struct nlattr *block_index_attr,
3783 struct netlink_ext_ack *extack)
3784{
3785 u32 block_index;
3786 int err;
3787
3788 if (!block_index_attr)
3789 return 0;
3790
3791 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3792 if (err)
3793 return err;
3794
3795 if (!block_index)
3796 return 0;
3797
3798 qe->info.binder_type = binder_type;
3799 qe->info.chain_head_change = tcf_chain_head_change_dflt;
3800 qe->info.chain_head_change_priv = &qe->filter_chain;
3801 qe->info.block_index = block_index;
3802
3803 return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
3804}
3805EXPORT_SYMBOL(tcf_qevent_init);
3806
3807void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
3808{
3809 if (qe->info.block_index)
3810 tcf_block_put_ext(qe->block, sch, &qe->info);
3811}
3812EXPORT_SYMBOL(tcf_qevent_destroy);
3813
3814int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
3815 struct netlink_ext_ack *extack)
3816{
3817 u32 block_index;
3818 int err;
3819
3820 if (!block_index_attr)
3821 return 0;
3822
3823 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3824 if (err)
3825 return err;
3826
3827 /* Bounce newly-configured block or change in block. */
3828 if (block_index != qe->info.block_index) {
3829 NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
3830 return -EINVAL;
3831 }
3832
3833 return 0;
3834}
3835EXPORT_SYMBOL(tcf_qevent_validate_change);
3836
3837struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
Petr Machata55f656c2020-07-14 20:03:07 +03003838 struct sk_buff **to_free, int *ret)
Petr Machata36257502020-06-27 01:45:26 +03003839{
3840 struct tcf_result cl_res;
3841 struct tcf_proto *fl;
3842
3843 if (!qe->info.block_index)
3844 return skb;
3845
3846 fl = rcu_dereference_bh(qe->filter_chain);
3847
Petr Machata36257502020-06-27 01:45:26 +03003848 switch (tcf_classify(skb, fl, &cl_res, false)) {
3849 case TC_ACT_SHOT:
3850 qdisc_qstats_drop(sch);
3851 __qdisc_drop(skb, to_free);
3852 *ret = __NET_XMIT_BYPASS;
3853 return NULL;
3854 case TC_ACT_STOLEN:
3855 case TC_ACT_QUEUED:
3856 case TC_ACT_TRAP:
3857 __qdisc_drop(skb, to_free);
3858 *ret = __NET_XMIT_STOLEN;
3859 return NULL;
3860 case TC_ACT_REDIRECT:
3861 skb_do_redirect(skb);
3862 *ret = __NET_XMIT_STOLEN;
3863 return NULL;
3864 }
3865
Petr Machata36257502020-06-27 01:45:26 +03003866 return skb;
3867}
3868EXPORT_SYMBOL(tcf_qevent_handle);
3869
3870int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
3871{
3872 if (!qe->info.block_index)
3873 return 0;
3874 return nla_put_u32(skb, attr_name, qe->info.block_index);
3875}
3876EXPORT_SYMBOL(tcf_qevent_dump);
3877#endif
3878
Jiri Pirko48617382018-01-17 11:46:46 +01003879static __net_init int tcf_net_init(struct net *net)
3880{
3881 struct tcf_net *tn = net_generic(net, tcf_net_id);
3882
Vlad Buslovab281622018-09-24 19:22:56 +03003883 spin_lock_init(&tn->idr_lock);
Jiri Pirko48617382018-01-17 11:46:46 +01003884 idr_init(&tn->idr);
3885 return 0;
3886}
3887
3888static void __net_exit tcf_net_exit(struct net *net)
3889{
3890 struct tcf_net *tn = net_generic(net, tcf_net_id);
3891
3892 idr_destroy(&tn->idr);
3893}
3894
3895static struct pernet_operations tcf_net_ops = {
3896 .init = tcf_net_init,
3897 .exit = tcf_net_exit,
3898 .id = &tcf_net_id,
3899 .size = sizeof(struct tcf_net),
3900};
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902static int __init tc_filter_init(void)
3903{
Jiri Pirko48617382018-01-17 11:46:46 +01003904 int err;
3905
Cong Wang7aa00452017-10-26 18:24:28 -07003906 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3907 if (!tc_filter_wq)
3908 return -ENOMEM;
3909
Jiri Pirko48617382018-01-17 11:46:46 +01003910 err = register_pernet_subsys(&tcf_net_ops);
3911 if (err)
3912 goto err_register_pernet_subsys;
3913
Vlad Buslov470502d2019-02-11 10:55:48 +02003914 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3915 RTNL_FLAG_DOIT_UNLOCKED);
3916 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3917 RTNL_FLAG_DOIT_UNLOCKED);
Vlad Buslovc431f892018-05-31 09:52:53 +03003918 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Vlad Buslov470502d2019-02-11 10:55:48 +02003919 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003920 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3921 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3922 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3923 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01003926
3927err_register_pernet_subsys:
3928 destroy_workqueue(tc_filter_wq);
3929 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930}
3931
3932subsys_initcall(tc_filter_init);