blob: cb1331b3574512932890bef65df1e1deacf5f88d [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;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001957 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001958 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001959 struct tcf_chain *chain = NULL;
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001989 /* If no priority is provided by the user,
1990 * we allocate one.
1991 */
1992 if (n->nlmsg_flags & NLM_F_CREATE) {
1993 prio = TC_H_MAKE(0x80000000U, 0U);
1994 prio_allocate = true;
1995 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001996 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000
2001 /* Find head of filter chain. */
2002
Vlad Buslov470502d2019-02-11 10:55:48 +02002003 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2004 if (err)
2005 return err;
2006
Cong Wang6f96c3c2019-10-07 13:26:28 -07002007 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2008 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2009 err = -EINVAL;
2010 goto errout;
2011 }
2012
Vlad Buslov470502d2019-02-11 10:55:48 +02002013 /* Take rtnl mutex if rtnl_held was set to true on previous iteration,
2014 * block is shared (no qdisc found), qdisc is not unlocked, classifier
2015 * type is not specified, classifier is not unlocked.
2016 */
2017 if (rtnl_held ||
2018 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
Cong Wang6f96c3c2019-10-07 13:26:28 -07002019 !tcf_proto_is_unlocked(name)) {
Vlad Buslov470502d2019-02-11 10:55:48 +02002020 rtnl_held = true;
2021 rtnl_lock();
2022 }
2023
2024 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2025 if (err)
2026 goto errout;
2027
2028 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2029 extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002030 if (IS_ERR(block)) {
2031 err = PTR_ERR(block);
2032 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002033 }
Cong Wanga7df4872020-04-30 20:53:49 -07002034 block->classid = parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002035
2036 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2037 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002038 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02002039 err = -EINVAL;
2040 goto errout;
2041 }
Vlad Buslovc431f892018-05-31 09:52:53 +03002042 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02002043 if (!chain) {
Jiri Pirkod5ed72a2018-08-27 20:58:43 +02002044 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03002045 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02002046 goto errout;
2047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Vlad Busloved76f5e2019-02-11 10:55:38 +02002049 mutex_lock(&chain->filter_chain_lock);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02002050 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2051 prio, prio_allocate);
2052 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002053 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02002054 err = PTR_ERR(tp);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002055 goto errout_locked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057
2058 if (tp == NULL) {
Vlad Buslov8b646782019-02-11 10:55:41 +02002059 struct tcf_proto *tp_new = NULL;
2060
Vlad Buslov726d06122019-02-11 10:55:42 +02002061 if (chain->flushing) {
2062 err = -EAGAIN;
2063 goto errout_locked;
2064 }
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 /* Proto-tcf does not exist, create new one */
2067
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002068 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002069 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002070 err = -EINVAL;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002071 goto errout_locked;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Vlad Buslovc431f892018-05-31 09:52:53 +03002074 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002075 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 +01002076 err = -ENOENT;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002077 goto errout_locked;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02002080 if (prio_allocate)
Vlad Busloved76f5e2019-02-11 10:55:38 +02002081 prio = tcf_auto_prio(tcf_chain_tp_prev(chain,
2082 &chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Vlad Busloved76f5e2019-02-11 10:55:38 +02002084 mutex_unlock(&chain->filter_chain_lock);
Eric Dumazet36d79af2020-01-21 11:02:20 -08002085 tp_new = tcf_proto_create(name, protocol, prio, chain,
2086 rtnl_held, extack);
Vlad Buslov8b646782019-02-11 10:55:41 +02002087 if (IS_ERR(tp_new)) {
2088 err = PTR_ERR(tp_new);
Vlad Buslov726d06122019-02-11 10:55:42 +02002089 goto errout_tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
Vlad Busloved76f5e2019-02-11 10:55:38 +02002091
Minoru Usui12186be2009-06-02 02:17:34 -07002092 tp_created = 1;
Vlad Buslov12db03b2019-02-11 10:55:45 +02002093 tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio,
2094 rtnl_held);
Vlad Buslov726d06122019-02-11 10:55:42 +02002095 if (IS_ERR(tp)) {
2096 err = PTR_ERR(tp);
2097 goto errout_tp;
2098 }
Vlad Busloved76f5e2019-02-11 10:55:38 +02002099 } else {
2100 mutex_unlock(&chain->filter_chain_lock);
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Vlad Buslov8b646782019-02-11 10:55:41 +02002103 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2104 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2105 err = -EINVAL;
2106 goto errout;
2107 }
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 fh = tp->ops->get(tp, t->tcm_handle);
2110
WANG Cong8113c092017-08-04 21:31:43 -07002111 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03002112 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05002113 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 +01002114 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01002116 }
Vlad Buslovc431f892018-05-31 09:52:53 +03002117 } else if (n->nlmsg_flags & NLM_F_EXCL) {
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002118 tfilter_put(tp, fh);
Vlad Buslovc431f892018-05-31 09:52:53 +03002119 NL_SET_ERR_MSG(extack, "Filter already exists");
2120 err = -EEXIST;
2121 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
2123
Jiri Pirko9f407f12018-07-23 09:23:07 +02002124 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
2125 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
2126 err = -EINVAL;
2127 goto errout;
2128 }
2129
Cong Wang2f7ef2f2014-04-25 13:54:06 -07002130 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05002131 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002132 rtnl_held, extack);
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002133 if (err == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002134 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002135 RTM_NEWTFILTER, false, rtnl_held);
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002136 tfilter_put(tp, fh);
Vlad Buslov503d81d2019-07-21 17:44:12 +03002137 /* q pointer is NULL for shared blocks */
2138 if (q)
2139 q->flags &= ~TCQ_F_CAN_BYPASS;
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142errout:
Vlad Buslov8b646782019-02-11 10:55:41 +02002143 if (err && tp_created)
Vlad Buslov12db03b2019-02-11 10:55:45 +02002144 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL);
Vlad Buslov726d06122019-02-11 10:55:42 +02002145errout_tp:
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002146 if (chain) {
2147 if (tp && !IS_ERR(tp))
Vlad Buslov12db03b2019-02-11 10:55:45 +02002148 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002149 if (!tp_created)
2150 tcf_chain_put(chain);
2151 }
Vlad Buslov12db03b2019-02-11 10:55:45 +02002152 tcf_block_release(q, block, rtnl_held);
Vlad Buslov470502d2019-02-11 10:55:48 +02002153
2154 if (rtnl_held)
2155 rtnl_unlock();
2156
2157 if (err == -EAGAIN) {
2158 /* Take rtnl lock in case EAGAIN is caused by concurrent flush
2159 * of target chain.
2160 */
2161 rtnl_held = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /* Replay the request. */
2163 goto replay;
Vlad Buslov470502d2019-02-11 10:55:48 +02002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return err;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002166
2167errout_locked:
2168 mutex_unlock(&chain->filter_chain_lock);
2169 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Vlad Buslovc431f892018-05-31 09:52:53 +03002172static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2173 struct netlink_ext_ack *extack)
2174{
2175 struct net *net = sock_net(skb->sk);
2176 struct nlattr *tca[TCA_MAX + 1];
Cong Wang6f96c3c2019-10-07 13:26:28 -07002177 char name[IFNAMSIZ];
Vlad Buslovc431f892018-05-31 09:52:53 +03002178 struct tcmsg *t;
2179 u32 protocol;
2180 u32 prio;
2181 u32 parent;
2182 u32 chain_index;
2183 struct Qdisc *q = NULL;
2184 struct tcf_chain_info chain_info;
2185 struct tcf_chain *chain = NULL;
Vlad Buslov470502d2019-02-11 10:55:48 +02002186 struct tcf_block *block = NULL;
Vlad Buslovc431f892018-05-31 09:52:53 +03002187 struct tcf_proto *tp = NULL;
2188 unsigned long cl = 0;
2189 void *fh = NULL;
2190 int err;
Vlad Buslov470502d2019-02-11 10:55:48 +02002191 bool rtnl_held = false;
Vlad Buslovc431f892018-05-31 09:52:53 +03002192
2193 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2194 return -EPERM;
2195
Johannes Berg8cb08172019-04-26 14:07:28 +02002196 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2197 rtm_tca_policy, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002198 if (err < 0)
2199 return err;
2200
2201 t = nlmsg_data(n);
2202 protocol = TC_H_MIN(t->tcm_info);
2203 prio = TC_H_MAJ(t->tcm_info);
2204 parent = t->tcm_parent;
2205
2206 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
2207 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
2208 return -ENOENT;
2209 }
2210
2211 /* Find head of filter chain. */
2212
Vlad Buslov470502d2019-02-11 10:55:48 +02002213 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2214 if (err)
2215 return err;
2216
Cong Wang6f96c3c2019-10-07 13:26:28 -07002217 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2218 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2219 err = -EINVAL;
2220 goto errout;
2221 }
Vlad Buslov470502d2019-02-11 10:55:48 +02002222 /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc
2223 * found), qdisc is not unlocked, classifier type is not specified,
2224 * classifier is not unlocked.
2225 */
2226 if (!prio ||
2227 (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
Cong Wang6f96c3c2019-10-07 13:26:28 -07002228 !tcf_proto_is_unlocked(name)) {
Vlad Buslov470502d2019-02-11 10:55:48 +02002229 rtnl_held = true;
2230 rtnl_lock();
2231 }
2232
2233 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2234 if (err)
2235 goto errout;
2236
2237 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2238 extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002239 if (IS_ERR(block)) {
2240 err = PTR_ERR(block);
2241 goto errout;
2242 }
2243
2244 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2245 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2246 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2247 err = -EINVAL;
2248 goto errout;
2249 }
2250 chain = tcf_chain_get(block, chain_index, false);
2251 if (!chain) {
Jiri Pirko5ca8a252018-08-03 11:08:47 +02002252 /* User requested flush on non-existent chain. Nothing to do,
2253 * so just return success.
2254 */
2255 if (prio == 0) {
2256 err = 0;
2257 goto errout;
2258 }
Vlad Buslovc431f892018-05-31 09:52:53 +03002259 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Jiri Pirkob7b42472018-08-27 20:58:44 +02002260 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03002261 goto errout;
2262 }
2263
2264 if (prio == 0) {
2265 tfilter_notify_chain(net, skb, block, q, parent, n,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002266 chain, RTM_DELTFILTER, rtnl_held);
2267 tcf_chain_flush(chain, rtnl_held);
Vlad Buslovc431f892018-05-31 09:52:53 +03002268 err = 0;
2269 goto errout;
2270 }
2271
Vlad Busloved76f5e2019-02-11 10:55:38 +02002272 mutex_lock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002273 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2274 prio, false);
2275 if (!tp || IS_ERR(tp)) {
2276 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03002277 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002278 goto errout_locked;
Vlad Buslovc431f892018-05-31 09:52:53 +03002279 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2280 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2281 err = -EINVAL;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002282 goto errout_locked;
2283 } else if (t->tcm_handle == 0) {
John Hurley59eb87c2019-11-02 14:17:47 +00002284 tcf_proto_signal_destroying(chain, tp);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002285 tcf_chain_tp_remove(chain, &chain_info, tp);
2286 mutex_unlock(&chain->filter_chain_lock);
2287
Vlad Buslov12db03b2019-02-11 10:55:45 +02002288 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002289 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002290 RTM_DELTFILTER, false, rtnl_held);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002291 err = 0;
Vlad Buslovc431f892018-05-31 09:52:53 +03002292 goto errout;
2293 }
Vlad Busloved76f5e2019-02-11 10:55:38 +02002294 mutex_unlock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002295
2296 fh = tp->ops->get(tp, t->tcm_handle);
2297
2298 if (!fh) {
Vlad Busloved76f5e2019-02-11 10:55:38 +02002299 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2300 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03002301 } else {
2302 bool last;
2303
2304 err = tfilter_del_notify(net, skb, n, tp, block,
2305 q, parent, fh, false, &last,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002306 rtnl_held, extack);
2307
Vlad Buslovc431f892018-05-31 09:52:53 +03002308 if (err)
2309 goto errout;
Vlad Buslov8b646782019-02-11 10:55:41 +02002310 if (last)
Vlad Buslov12db03b2019-02-11 10:55:45 +02002311 tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002312 }
2313
2314errout:
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002315 if (chain) {
2316 if (tp && !IS_ERR(tp))
Vlad Buslov12db03b2019-02-11 10:55:45 +02002317 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslovc431f892018-05-31 09:52:53 +03002318 tcf_chain_put(chain);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002319 }
Vlad Buslov12db03b2019-02-11 10:55:45 +02002320 tcf_block_release(q, block, rtnl_held);
Vlad Buslov470502d2019-02-11 10:55:48 +02002321
2322 if (rtnl_held)
2323 rtnl_unlock();
2324
Vlad Buslovc431f892018-05-31 09:52:53 +03002325 return err;
Vlad Busloved76f5e2019-02-11 10:55:38 +02002326
2327errout_locked:
2328 mutex_unlock(&chain->filter_chain_lock);
2329 goto errout;
Vlad Buslovc431f892018-05-31 09:52:53 +03002330}
2331
2332static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
2333 struct netlink_ext_ack *extack)
2334{
2335 struct net *net = sock_net(skb->sk);
2336 struct nlattr *tca[TCA_MAX + 1];
Cong Wang6f96c3c2019-10-07 13:26:28 -07002337 char name[IFNAMSIZ];
Vlad Buslovc431f892018-05-31 09:52:53 +03002338 struct tcmsg *t;
2339 u32 protocol;
2340 u32 prio;
2341 u32 parent;
2342 u32 chain_index;
2343 struct Qdisc *q = NULL;
2344 struct tcf_chain_info chain_info;
2345 struct tcf_chain *chain = NULL;
Vlad Buslov470502d2019-02-11 10:55:48 +02002346 struct tcf_block *block = NULL;
Vlad Buslovc431f892018-05-31 09:52:53 +03002347 struct tcf_proto *tp = NULL;
2348 unsigned long cl = 0;
2349 void *fh = NULL;
2350 int err;
Vlad Buslov470502d2019-02-11 10:55:48 +02002351 bool rtnl_held = false;
Vlad Buslovc431f892018-05-31 09:52:53 +03002352
Johannes Berg8cb08172019-04-26 14:07:28 +02002353 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2354 rtm_tca_policy, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002355 if (err < 0)
2356 return err;
2357
2358 t = nlmsg_data(n);
2359 protocol = TC_H_MIN(t->tcm_info);
2360 prio = TC_H_MAJ(t->tcm_info);
2361 parent = t->tcm_parent;
2362
2363 if (prio == 0) {
2364 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
2365 return -ENOENT;
2366 }
2367
2368 /* Find head of filter chain. */
2369
Vlad Buslov470502d2019-02-11 10:55:48 +02002370 err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack);
2371 if (err)
2372 return err;
2373
Cong Wang6f96c3c2019-10-07 13:26:28 -07002374 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2375 NL_SET_ERR_MSG(extack, "Specified TC filter name too long");
2376 err = -EINVAL;
2377 goto errout;
2378 }
Vlad Buslov470502d2019-02-11 10:55:48 +02002379 /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not
2380 * unlocked, classifier type is not specified, classifier is not
2381 * unlocked.
2382 */
2383 if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) ||
Cong Wang6f96c3c2019-10-07 13:26:28 -07002384 !tcf_proto_is_unlocked(name)) {
Vlad Buslov470502d2019-02-11 10:55:48 +02002385 rtnl_held = true;
2386 rtnl_lock();
2387 }
2388
2389 err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack);
2390 if (err)
2391 goto errout;
2392
2393 block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index,
2394 extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03002395 if (IS_ERR(block)) {
2396 err = PTR_ERR(block);
2397 goto errout;
2398 }
2399
2400 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2401 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2402 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
2403 err = -EINVAL;
2404 goto errout;
2405 }
2406 chain = tcf_chain_get(block, chain_index, false);
2407 if (!chain) {
2408 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
2409 err = -EINVAL;
2410 goto errout;
2411 }
2412
Vlad Busloved76f5e2019-02-11 10:55:38 +02002413 mutex_lock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002414 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
2415 prio, false);
Vlad Busloved76f5e2019-02-11 10:55:38 +02002416 mutex_unlock(&chain->filter_chain_lock);
Vlad Buslovc431f892018-05-31 09:52:53 +03002417 if (!tp || IS_ERR(tp)) {
2418 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03002419 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03002420 goto errout;
2421 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
2422 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
2423 err = -EINVAL;
2424 goto errout;
2425 }
2426
2427 fh = tp->ops->get(tp, t->tcm_handle);
2428
2429 if (!fh) {
2430 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
2431 err = -ENOENT;
2432 } else {
2433 err = tfilter_notify(net, skb, n, tp, block, q, parent,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002434 fh, RTM_NEWTFILTER, true, rtnl_held);
Vlad Buslovc431f892018-05-31 09:52:53 +03002435 if (err < 0)
2436 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
2437 }
2438
Vlad Buslov7d5509f2019-02-11 10:55:44 +02002439 tfilter_put(tp, fh);
Vlad Buslovc431f892018-05-31 09:52:53 +03002440errout:
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002441 if (chain) {
2442 if (tp && !IS_ERR(tp))
Vlad Buslov12db03b2019-02-11 10:55:45 +02002443 tcf_proto_put(tp, rtnl_held, NULL);
Vlad Buslovc431f892018-05-31 09:52:53 +03002444 tcf_chain_put(chain);
Vlad Buslov4dbfa762019-02-11 10:55:39 +02002445 }
Vlad Buslov12db03b2019-02-11 10:55:45 +02002446 tcf_block_release(q, block, rtnl_held);
Vlad Buslov470502d2019-02-11 10:55:48 +02002447
2448 if (rtnl_held)
2449 rtnl_unlock();
2450
Vlad Buslovc431f892018-05-31 09:52:53 +03002451 return err;
2452}
2453
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002454struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 struct tcf_walker w;
2456 struct sk_buff *skb;
2457 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002458 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002459 struct Qdisc *q;
2460 u32 parent;
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002461 bool terse_dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462};
2463
WANG Cong8113c092017-08-04 21:31:43 -07002464static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002466 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08002467 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002469 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002470 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04002471 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002472 RTM_NEWTFILTER, a->terse_dump, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002475static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
2476 struct sk_buff *skb, struct netlink_callback *cb,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002477 long index_start, long *p_index, bool terse)
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002478{
2479 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002480 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002481 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002482 struct tcf_proto *tp, *tp_prev;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002483 struct tcf_dump_args arg;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002484
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002485 for (tp = __tcf_get_next_proto(chain, NULL);
2486 tp;
2487 tp_prev = tp,
2488 tp = __tcf_get_next_proto(chain, tp),
Vlad Buslov12db03b2019-02-11 10:55:45 +02002489 tcf_proto_put(tp_prev, true, NULL),
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002490 (*p_index)++) {
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002491 if (*p_index < index_start)
2492 continue;
2493 if (TC_H_MAJ(tcm->tcm_info) &&
2494 TC_H_MAJ(tcm->tcm_info) != tp->prio)
2495 continue;
2496 if (TC_H_MIN(tcm->tcm_info) &&
2497 TC_H_MIN(tcm->tcm_info) != tp->protocol)
2498 continue;
2499 if (*p_index > index_start)
2500 memset(&cb->args[1], 0,
2501 sizeof(cb->args) - sizeof(cb->args[0]));
2502 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08002503 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002504 NETLINK_CB(cb->skb).portid,
2505 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002506 RTM_NEWTFILTER, false, true) <= 0)
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002507 goto errout;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002508 cb->args[1] = 1;
2509 }
2510 if (!tp->ops->walk)
2511 continue;
2512 arg.w.fn = tcf_node_dump;
2513 arg.skb = skb;
2514 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002515 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002516 arg.q = q;
2517 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002518 arg.w.stop = 0;
2519 arg.w.skip = cb->args[1] - 1;
2520 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03002521 arg.w.cookie = cb->args[2];
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002522 arg.terse_dump = terse;
Vlad Buslov12db03b2019-02-11 10:55:45 +02002523 tp->ops->walk(tp, &arg.w, true);
Vlad Buslov01683a12018-07-09 13:29:11 +03002524 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002525 cb->args[1] = arg.w.count + 1;
2526 if (arg.w.stop)
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002527 goto errout;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002528 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02002529 return true;
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002530
2531errout:
Vlad Buslov12db03b2019-02-11 10:55:45 +02002532 tcf_proto_put(tp, true, NULL);
Vlad Buslovfe2923a2019-02-11 10:55:40 +02002533 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002534}
2535
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002536static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = {
2537 [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE),
2538};
2539
Eric Dumazetbd27a872009-11-05 20:57:26 -08002540/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
2542{
Vlad Buslovbbf73832019-02-11 10:55:36 +02002543 struct tcf_chain *chain, *chain_prev;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002544 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02002545 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002546 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02002547 struct tcf_block *block;
David S. Miller942b8162012-06-26 21:48:50 -07002548 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002549 bool terse_dump = false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002550 long index_start;
2551 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002552 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002553 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Hong zhi guo573ce262013-03-27 06:47:04 +00002555 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002557
Johannes Berg8cb08172019-04-26 14:07:28 +02002558 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002559 tcf_tfilter_dump_policy, cb->extack);
Jiri Pirko5bc17012017-05-17 11:08:01 +02002560 if (err)
2561 return err;
2562
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002563 if (tca[TCA_DUMP_FLAGS]) {
2564 struct nla_bitfield32 flags =
2565 nla_get_bitfield32(tca[TCA_DUMP_FLAGS]);
2566
2567 terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE;
2568 }
2569
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002570 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002571 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002572 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07002573 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01002574 /* If we work with block index, q is NULL and parent value
2575 * will never be used in the following code. The check
2576 * in tcf_fill_node prevents it. However, compiler does not
2577 * see that far, so set parent to zero to silence the warning
2578 * about parent being uninitialized.
2579 */
2580 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002581 } else {
2582 const struct Qdisc_class_ops *cops;
2583 struct net_device *dev;
2584 unsigned long cl = 0;
2585
2586 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2587 if (!dev)
2588 return skb->len;
2589
2590 parent = tcm->tcm_parent;
Cong Wanga7df4872020-04-30 20:53:49 -07002591 if (!parent)
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002592 q = dev->qdisc;
Cong Wanga7df4872020-04-30 20:53:49 -07002593 else
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002594 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002595 if (!q)
2596 goto out;
2597 cops = q->ops->cl_ops;
2598 if (!cops)
2599 goto out;
2600 if (!cops->tcf_block)
2601 goto out;
2602 if (TC_H_MIN(tcm->tcm_parent)) {
2603 cl = cops->find(q, tcm->tcm_parent);
2604 if (cl == 0)
2605 goto out;
2606 }
2607 block = cops->tcf_block(q, cl, NULL);
2608 if (!block)
2609 goto out;
Cong Wanga7df4872020-04-30 20:53:49 -07002610 parent = block->classid;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01002611 if (tcf_block_shared(block))
2612 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002615 index_start = cb->args[0];
2616 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002617
Vlad Buslovbbf73832019-02-11 10:55:36 +02002618 for (chain = __tcf_get_next_chain(block, NULL);
2619 chain;
2620 chain_prev = chain,
2621 chain = __tcf_get_next_chain(block, chain),
2622 tcf_chain_put(chain_prev)) {
Jiri Pirko5bc17012017-05-17 11:08:01 +02002623 if (tca[TCA_CHAIN] &&
2624 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
2625 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02002626 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Vlad Buslovf8ab1802020-05-15 14:40:11 +03002627 index_start, &index, terse_dump)) {
Vlad Buslovbbf73832019-02-11 10:55:36 +02002628 tcf_chain_put(chain);
Roman Kapl5ae437a2018-02-19 21:32:51 +01002629 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02002630 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01002631 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02002632 }
2633
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002634 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
Vlad Buslov12db03b2019-02-11 10:55:45 +02002635 tcf_block_refcnt_put(block, true);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02002636 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01002639 /* If we did no progress, the error (EMSGSIZE) is real */
2640 if (skb->len == 0 && err)
2641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return skb->len;
2643}
2644
Vlad Buslova5654822019-02-11 10:55:37 +02002645static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops,
2646 void *tmplt_priv, u32 chain_index,
2647 struct net *net, struct sk_buff *skb,
2648 struct tcf_block *block,
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002649 u32 portid, u32 seq, u16 flags, int event)
2650{
2651 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02002652 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002653 struct nlmsghdr *nlh;
2654 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02002655 void *priv;
2656
Vlad Buslova5654822019-02-11 10:55:37 +02002657 ops = tmplt_ops;
2658 priv = tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002659
2660 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
2661 if (!nlh)
2662 goto out_nlmsg_trim;
2663 tcm = nlmsg_data(nlh);
2664 tcm->tcm_family = AF_UNSPEC;
2665 tcm->tcm__pad1 = 0;
2666 tcm->tcm__pad2 = 0;
2667 tcm->tcm_handle = 0;
2668 if (block->q) {
2669 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
2670 tcm->tcm_parent = block->q->handle;
2671 } else {
2672 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
2673 tcm->tcm_block_index = block->index;
2674 }
2675
Vlad Buslova5654822019-02-11 10:55:37 +02002676 if (nla_put_u32(skb, TCA_CHAIN, chain_index))
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002677 goto nla_put_failure;
2678
Jiri Pirko9f407f12018-07-23 09:23:07 +02002679 if (ops) {
2680 if (nla_put_string(skb, TCA_KIND, ops->kind))
2681 goto nla_put_failure;
2682 if (ops->tmplt_dump(skb, net, priv) < 0)
2683 goto nla_put_failure;
2684 }
2685
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002686 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
2687 return skb->len;
2688
2689out_nlmsg_trim:
2690nla_put_failure:
2691 nlmsg_trim(skb, b);
2692 return -EMSGSIZE;
2693}
2694
2695static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
2696 u32 seq, u16 flags, int event, bool unicast)
2697{
2698 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2699 struct tcf_block *block = chain->block;
2700 struct net *net = block->net;
2701 struct sk_buff *skb;
Zhike Wang5b5f99b2019-03-11 03:15:54 -07002702 int err = 0;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002703
2704 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2705 if (!skb)
2706 return -ENOBUFS;
2707
Vlad Buslova5654822019-02-11 10:55:37 +02002708 if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
2709 chain->index, net, skb, block, portid,
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002710 seq, flags, event) <= 0) {
2711 kfree_skb(skb);
2712 return -EINVAL;
2713 }
2714
2715 if (unicast)
Zhike Wang5b5f99b2019-03-11 03:15:54 -07002716 err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2717 else
2718 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
2719 flags & NLM_F_ECHO);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002720
Zhike Wang5b5f99b2019-03-11 03:15:54 -07002721 if (err > 0)
2722 err = 0;
2723 return err;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002724}
2725
Vlad Buslova5654822019-02-11 10:55:37 +02002726static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops,
2727 void *tmplt_priv, u32 chain_index,
2728 struct tcf_block *block, struct sk_buff *oskb,
2729 u32 seq, u16 flags, bool unicast)
2730{
2731 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
2732 struct net *net = block->net;
2733 struct sk_buff *skb;
2734
2735 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2736 if (!skb)
2737 return -ENOBUFS;
2738
2739 if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb,
2740 block, portid, seq, flags, RTM_DELCHAIN) <= 0) {
2741 kfree_skb(skb);
2742 return -EINVAL;
2743 }
2744
2745 if (unicast)
2746 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
2747
2748 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
2749}
2750
Jiri Pirko9f407f12018-07-23 09:23:07 +02002751static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
2752 struct nlattr **tca,
2753 struct netlink_ext_ack *extack)
2754{
2755 const struct tcf_proto_ops *ops;
Eric Dumazet2dd56162019-12-07 11:34:45 -08002756 char name[IFNAMSIZ];
Jiri Pirko9f407f12018-07-23 09:23:07 +02002757 void *tmplt_priv;
2758
2759 /* If kind is not set, user did not specify template. */
2760 if (!tca[TCA_KIND])
2761 return 0;
2762
Eric Dumazet2dd56162019-12-07 11:34:45 -08002763 if (tcf_proto_check_kind(tca[TCA_KIND], name)) {
2764 NL_SET_ERR_MSG(extack, "Specified TC chain template name too long");
2765 return -EINVAL;
2766 }
2767
2768 ops = tcf_proto_lookup_ops(name, true, extack);
Jiri Pirko9f407f12018-07-23 09:23:07 +02002769 if (IS_ERR(ops))
2770 return PTR_ERR(ops);
2771 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
2772 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
2773 return -EOPNOTSUPP;
2774 }
2775
2776 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
2777 if (IS_ERR(tmplt_priv)) {
2778 module_put(ops->owner);
2779 return PTR_ERR(tmplt_priv);
2780 }
2781 chain->tmplt_ops = ops;
2782 chain->tmplt_priv = tmplt_priv;
2783 return 0;
2784}
2785
Vlad Buslova5654822019-02-11 10:55:37 +02002786static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops,
2787 void *tmplt_priv)
Jiri Pirko9f407f12018-07-23 09:23:07 +02002788{
Jiri Pirko9f407f12018-07-23 09:23:07 +02002789 /* If template ops are set, no work to do for us. */
Vlad Buslova5654822019-02-11 10:55:37 +02002790 if (!tmplt_ops)
Jiri Pirko9f407f12018-07-23 09:23:07 +02002791 return;
2792
Vlad Buslova5654822019-02-11 10:55:37 +02002793 tmplt_ops->tmplt_destroy(tmplt_priv);
2794 module_put(tmplt_ops->owner);
Jiri Pirko9f407f12018-07-23 09:23:07 +02002795}
2796
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002797/* Add/delete/get a chain */
2798
2799static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
2800 struct netlink_ext_ack *extack)
2801{
2802 struct net *net = sock_net(skb->sk);
2803 struct nlattr *tca[TCA_MAX + 1];
2804 struct tcmsg *t;
2805 u32 parent;
2806 u32 chain_index;
2807 struct Qdisc *q = NULL;
2808 struct tcf_chain *chain = NULL;
2809 struct tcf_block *block;
2810 unsigned long cl;
2811 int err;
2812
2813 if (n->nlmsg_type != RTM_GETCHAIN &&
2814 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
2815 return -EPERM;
2816
2817replay:
Johannes Berg8cb08172019-04-26 14:07:28 +02002818 err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX,
2819 rtm_tca_policy, extack);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002820 if (err < 0)
2821 return err;
2822
2823 t = nlmsg_data(n);
2824 parent = t->tcm_parent;
2825 cl = 0;
2826
2827 block = tcf_block_find(net, &q, &parent, &cl,
2828 t->tcm_ifindex, t->tcm_block_index, extack);
2829 if (IS_ERR(block))
2830 return PTR_ERR(block);
2831
2832 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
2833 if (chain_index > TC_ACT_EXT_VAL_MASK) {
2834 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002835 err = -EINVAL;
2836 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002837 }
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002838
2839 mutex_lock(&block->lock);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002840 chain = tcf_chain_lookup(block, chain_index);
2841 if (n->nlmsg_type == RTM_NEWCHAIN) {
2842 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002843 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002844 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002845 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002846 */
2847 tcf_chain_hold(chain);
2848 } else {
2849 NL_SET_ERR_MSG(extack, "Filter chain already exists");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002850 err = -EEXIST;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002851 goto errout_block_locked;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002852 }
2853 } else {
2854 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
2855 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 +03002856 err = -ENOENT;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002857 goto errout_block_locked;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002858 }
2859 chain = tcf_chain_create(block, chain_index);
2860 if (!chain) {
2861 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002862 err = -ENOMEM;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002863 goto errout_block_locked;
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002864 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002865 }
2866 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002867 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002868 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslove368fdb2018-09-24 19:22:53 +03002869 err = -EINVAL;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002870 goto errout_block_locked;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002871 }
2872 tcf_chain_hold(chain);
2873 }
2874
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002875 if (n->nlmsg_type == RTM_NEWCHAIN) {
2876 /* Modifying chain requires holding parent block lock. In case
2877 * the chain was successfully added, take a reference to the
2878 * chain. This ensures that an empty chain does not disappear at
2879 * the end of this function.
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002880 */
2881 tcf_chain_hold(chain);
2882 chain->explicitly_created = true;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002883 }
2884 mutex_unlock(&block->lock);
2885
2886 switch (n->nlmsg_type) {
2887 case RTM_NEWCHAIN:
2888 err = tc_chain_tmplt_add(chain, net, tca, extack);
2889 if (err) {
2890 tcf_chain_put_explicitly_created(chain);
2891 goto errout;
2892 }
2893
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002894 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
2895 RTM_NEWCHAIN, false);
2896 break;
2897 case RTM_DELCHAIN:
Cong Wangf5b9bac2018-09-11 14:22:23 -07002898 tfilter_notify_chain(net, skb, block, q, parent, n,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002899 chain, RTM_DELTFILTER, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002900 /* Flush the chain first as the user requested chain removal. */
Vlad Buslov12db03b2019-02-11 10:55:45 +02002901 tcf_chain_flush(chain, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002902 /* In case the chain was successfully deleted, put a reference
2903 * to the chain previously taken during addition.
2904 */
2905 tcf_chain_put_explicitly_created(chain);
2906 break;
2907 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002908 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
Yajun Deng9e037392021-07-22 11:23:43 +08002909 n->nlmsg_flags, n->nlmsg_type, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002910 if (err < 0)
2911 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
2912 break;
2913 default:
2914 err = -EOPNOTSUPP;
2915 NL_SET_ERR_MSG(extack, "Unsupported message type");
2916 goto errout;
2917 }
2918
2919errout:
2920 tcf_chain_put(chain);
Vlad Buslove368fdb2018-09-24 19:22:53 +03002921errout_block:
Vlad Buslov12db03b2019-02-11 10:55:45 +02002922 tcf_block_release(q, block, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002923 if (err == -EAGAIN)
2924 /* Replay the request. */
2925 goto replay;
2926 return err;
Vlad Buslov2cbfab02019-02-11 10:55:34 +02002927
2928errout_block_locked:
2929 mutex_unlock(&block->lock);
2930 goto errout_block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002931}
2932
2933/* called with RTNL */
2934static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
2935{
2936 struct net *net = sock_net(skb->sk);
2937 struct nlattr *tca[TCA_MAX + 1];
2938 struct Qdisc *q = NULL;
2939 struct tcf_block *block;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002940 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Vlad Buslovace4a262019-02-25 17:45:44 +02002941 struct tcf_chain *chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002942 long index_start;
2943 long index;
2944 u32 parent;
2945 int err;
2946
2947 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
2948 return skb->len;
2949
Johannes Berg8cb08172019-04-26 14:07:28 +02002950 err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX,
2951 rtm_tca_policy, cb->extack);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002952 if (err)
2953 return err;
2954
2955 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
Vlad Buslov787ce6d2018-09-24 19:22:58 +03002956 block = tcf_block_refcnt_get(net, tcm->tcm_block_index);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002957 if (!block)
2958 goto out;
2959 /* If we work with block index, q is NULL and parent value
2960 * will never be used in the following code. The check
2961 * in tcf_fill_node prevents it. However, compiler does not
2962 * see that far, so set parent to zero to silence the warning
2963 * about parent being uninitialized.
2964 */
2965 parent = 0;
2966 } else {
2967 const struct Qdisc_class_ops *cops;
2968 struct net_device *dev;
2969 unsigned long cl = 0;
2970
2971 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
2972 if (!dev)
2973 return skb->len;
2974
2975 parent = tcm->tcm_parent;
2976 if (!parent) {
2977 q = dev->qdisc;
2978 parent = q->handle;
2979 } else {
2980 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
2981 }
2982 if (!q)
2983 goto out;
2984 cops = q->ops->cl_ops;
2985 if (!cops)
2986 goto out;
2987 if (!cops->tcf_block)
2988 goto out;
2989 if (TC_H_MIN(tcm->tcm_parent)) {
2990 cl = cops->find(q, tcm->tcm_parent);
2991 if (cl == 0)
2992 goto out;
2993 }
2994 block = cops->tcf_block(q, cl, NULL);
2995 if (!block)
2996 goto out;
2997 if (tcf_block_shared(block))
2998 q = NULL;
2999 }
3000
3001 index_start = cb->args[0];
3002 index = 0;
3003
Vlad Buslovace4a262019-02-25 17:45:44 +02003004 mutex_lock(&block->lock);
3005 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003006 if ((tca[TCA_CHAIN] &&
3007 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
3008 continue;
3009 if (index < index_start) {
3010 index++;
3011 continue;
3012 }
Vlad Buslovace4a262019-02-25 17:45:44 +02003013 if (tcf_chain_held_by_acts_only(chain))
3014 continue;
Vlad Buslova5654822019-02-11 10:55:37 +02003015 err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv,
3016 chain->index, net, skb, block,
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003017 NETLINK_CB(cb->skb).portid,
3018 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3019 RTM_NEWCHAIN);
Vlad Buslovace4a262019-02-25 17:45:44 +02003020 if (err <= 0)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003021 break;
3022 index++;
3023 }
Vlad Buslovace4a262019-02-25 17:45:44 +02003024 mutex_unlock(&block->lock);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003025
Vlad Buslov787ce6d2018-09-24 19:22:58 +03003026 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK)
Vlad Buslov12db03b2019-02-11 10:55:45 +02003027 tcf_block_refcnt_put(block, true);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003028 cb->args[0] = index;
3029
3030out:
3031 /* If we did no progress, the error (EMSGSIZE) is real */
3032 if (skb->len == 0 && err)
3033 return err;
3034 return skb->len;
3035}
3036
WANG Cong18d02642014-09-25 10:26:37 -07003037void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
3039#ifdef CONFIG_NET_CLS_ACT
Eric Dumazet3d66b892019-09-18 12:57:04 -07003040 if (exts->actions) {
3041 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
3042 kfree(exts->actions);
3043 }
WANG Cong22dc13c2016-08-13 22:35:00 -07003044 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045#endif
3046}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003047EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00003049int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05003050 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
Vlad Buslovec6743a2019-02-11 10:55:43 +02003051 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053#ifdef CONFIG_NET_CLS_ACT
3054 {
Vlad Buslov81692c62021-04-07 18:36:03 +03003055 int init_res[TCA_ACT_MAX_PRIO] = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05003057 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
WANG Cong5da57f42013-12-15 20:15:07 -08003059 if (exts->police && tb[exts->police]) {
Cong Wanga3b6f3a2021-01-16 16:56:57 -08003060 struct tc_action_ops *a_o;
3061
3062 a_o = tc_action_load_ops("police", tb[exts->police], rtnl_held, extack);
3063 if (IS_ERR(a_o))
3064 return PTR_ERR(a_o);
Jiri Pirko9fb9f252017-05-17 11:08:02 +02003065 act = tcf_action_init_1(net, tp, tb[exts->police],
3066 rate_tlv, "police", ovr,
Vlad Buslov81692c62021-04-07 18:36:03 +03003067 TCA_ACT_BIND, a_o, init_res,
3068 rtnl_held, extack);
Vlad Buslov4a78ae12021-04-07 18:36:04 +03003069 module_put(a_o->owner);
3070 if (IS_ERR(act))
Patrick McHardyab27cfb2008-01-23 20:33:13 -08003071 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
WANG Cong33be6272013-12-15 20:15:05 -08003073 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07003074 exts->actions[0] = act;
3075 exts->nr_actions = 1;
Vlad Buslovbba8ef22021-02-16 18:22:00 +02003076 tcf_idr_insert_many(exts->actions);
WANG Cong5da57f42013-12-15 20:15:07 -08003077 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03003078 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07003079
Jiri Pirko9fb9f252017-05-17 11:08:02 +02003080 err = tcf_action_init(net, tp, tb[exts->action],
3081 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov81692c62021-04-07 18:36:03 +03003082 exts->actions, init_res,
3083 &attr_size, rtnl_held, extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03003084 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08003085 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03003086 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
3088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089#else
WANG Cong5da57f42013-12-15 20:15:07 -08003090 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05003091 (exts->police && tb[exts->police])) {
3092 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05003094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095#endif
3096
3097 return 0;
3098}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003099EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Jiri Pirko9b0d4442017-08-04 14:29:15 +02003101void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
3103#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07003104 struct tcf_exts old = *dst;
3105
Jiri Pirko9b0d4442017-08-04 14:29:15 +02003106 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07003107 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108#endif
3109}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003110EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
WANG Cong22dc13c2016-08-13 22:35:00 -07003112#ifdef CONFIG_NET_CLS_ACT
3113static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
3114{
3115 if (exts->nr_actions == 0)
3116 return NULL;
3117 else
3118 return exts->actions[0];
3119}
3120#endif
WANG Cong33be6272013-12-15 20:15:05 -08003121
WANG Cong5da57f42013-12-15 20:15:07 -08003122int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
3124#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07003125 struct nlattr *nest;
3126
Jiri Pirko978dfd82017-08-04 14:29:03 +02003127 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 /*
3129 * again for backward compatible mode - we want
3130 * to work with both old and new modes of entering
3131 * tc data even if iproute2 was newer - jhs
3132 */
WANG Cong33be6272013-12-15 20:15:05 -08003133 if (exts->type != TCA_OLD_COMPAT) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003134 nest = nla_nest_start_noflag(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003135 if (nest == NULL)
3136 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07003137
Vlad Buslovca44b732020-05-15 14:40:12 +03003138 if (tcf_action_dump(skb, exts->actions, 0, 0, false)
3139 < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08003140 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003141 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08003142 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08003143 struct tc_action *act = tcf_exts_first_act(exts);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003144 nest = nla_nest_start_noflag(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05003145 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003146 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08003147 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08003148 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08003149 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 }
3151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07003153
3154nla_put_failure:
3155 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07003157#else
3158 return 0;
3159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003161EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Vlad Buslovca44b732020-05-15 14:40:12 +03003163int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts)
3164{
3165#ifdef CONFIG_NET_CLS_ACT
3166 struct nlattr *nest;
3167
3168 if (!exts->action || !tcf_exts_has_actions(exts))
3169 return 0;
3170
3171 nest = nla_nest_start_noflag(skb, exts->action);
3172 if (!nest)
3173 goto nla_put_failure;
3174
3175 if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0)
3176 goto nla_put_failure;
3177 nla_nest_end(skb, nest);
3178 return 0;
3179
3180nla_put_failure:
3181 nla_nest_cancel(skb, nest);
3182 return -1;
3183#else
3184 return 0;
3185#endif
3186}
3187EXPORT_SYMBOL(tcf_exts_terse_dump);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003188
WANG Cong5da57f42013-12-15 20:15:07 -08003189int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08003192 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01003193 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08003194 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195#endif
3196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08003198EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Vlad Buslov40119212019-08-26 16:44:59 +03003200static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags)
3201{
3202 if (*flags & TCA_CLS_FLAGS_IN_HW)
3203 return;
3204 *flags |= TCA_CLS_FLAGS_IN_HW;
3205 atomic_inc(&block->offloadcnt);
3206}
3207
3208static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags)
3209{
3210 if (!(*flags & TCA_CLS_FLAGS_IN_HW))
3211 return;
3212 *flags &= ~TCA_CLS_FLAGS_IN_HW;
3213 atomic_dec(&block->offloadcnt);
3214}
3215
3216static void tc_cls_offload_cnt_update(struct tcf_block *block,
3217 struct tcf_proto *tp, u32 *cnt,
3218 u32 *flags, u32 diff, bool add)
3219{
3220 lockdep_assert_held(&block->cb_lock);
3221
3222 spin_lock(&tp->lock);
3223 if (add) {
3224 if (!*cnt)
3225 tcf_block_offload_inc(block, flags);
3226 *cnt += diff;
3227 } else {
3228 *cnt -= diff;
3229 if (!*cnt)
3230 tcf_block_offload_dec(block, flags);
3231 }
3232 spin_unlock(&tp->lock);
3233}
3234
3235static void
3236tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp,
3237 u32 *cnt, u32 *flags)
3238{
3239 lockdep_assert_held(&block->cb_lock);
3240
3241 spin_lock(&tp->lock);
3242 tcf_block_offload_dec(block, flags);
3243 *cnt = 0;
3244 spin_unlock(&tp->lock);
3245}
3246
3247static int
3248__tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3249 void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02003250{
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02003251 struct flow_block_cb *block_cb;
Cong Wangaeb3fec2018-12-11 11:15:46 -08003252 int ok_count = 0;
3253 int err;
3254
Vlad Buslov40119212019-08-26 16:44:59 +03003255 list_for_each_entry(block_cb, &block->flow_block.cb_list, list) {
3256 err = block_cb->cb(type, type_data, block_cb->cb_priv);
3257 if (err) {
3258 if (err_stop)
3259 return err;
3260 } else {
3261 ok_count++;
3262 }
3263 }
3264 return ok_count;
3265}
3266
3267int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
3268 void *type_data, bool err_stop, bool rtnl_held)
3269{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003270 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003271 int ok_count;
3272
Vlad Buslov11bd6342019-08-26 16:45:02 +03003273retry:
3274 if (take_rtnl)
3275 rtnl_lock();
Vlad Buslov40119212019-08-26 16:44:59 +03003276 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003277 /* Need to obtain rtnl lock if block is bound to devs that require it.
3278 * In block bind code cb_lock is obtained while holding rtnl, so we must
3279 * obtain the locks in same order here.
3280 */
3281 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3282 up_read(&block->cb_lock);
3283 take_rtnl = true;
3284 goto retry;
3285 }
3286
Vlad Buslov40119212019-08-26 16:44:59 +03003287 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003288
Vlad Buslov40119212019-08-26 16:44:59 +03003289 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003290 if (take_rtnl)
3291 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003292 return ok_count;
3293}
3294EXPORT_SYMBOL(tc_setup_cb_call);
3295
3296/* Non-destructive filter add. If filter that wasn't already in hardware is
3297 * successfully offloaded, increment block offloads counter. On failure,
3298 * previously offloaded filter is considered to be intact and offloads counter
3299 * is not decremented.
3300 */
3301
3302int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
3303 enum tc_setup_type type, void *type_data, bool err_stop,
3304 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3305{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003306 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003307 int ok_count;
3308
Vlad Buslov11bd6342019-08-26 16:45:02 +03003309retry:
3310 if (take_rtnl)
3311 rtnl_lock();
Vlad Buslov4f8116c2019-08-26 16:44:57 +03003312 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003313 /* Need to obtain rtnl lock if block is bound to devs that require it.
3314 * In block bind code cb_lock is obtained while holding rtnl, so we must
3315 * obtain the locks in same order here.
3316 */
3317 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3318 up_read(&block->cb_lock);
3319 take_rtnl = true;
3320 goto retry;
3321 }
3322
Cong Wangaeb3fec2018-12-11 11:15:46 -08003323 /* Make sure all netdevs sharing this block are offload-capable. */
Vlad Buslov4f8116c2019-08-26 16:44:57 +03003324 if (block->nooffloaddevcnt && err_stop) {
3325 ok_count = -EOPNOTSUPP;
3326 goto err_unlock;
3327 }
Cong Wangaeb3fec2018-12-11 11:15:46 -08003328
Vlad Buslov40119212019-08-26 16:44:59 +03003329 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003330 if (ok_count < 0)
3331 goto err_unlock;
3332
3333 if (tp->ops->hw_add)
3334 tp->ops->hw_add(tp, type_data);
Vlad Buslov40119212019-08-26 16:44:59 +03003335 if (ok_count > 0)
3336 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags,
3337 ok_count, true);
Vlad Buslov4f8116c2019-08-26 16:44:57 +03003338err_unlock:
3339 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003340 if (take_rtnl)
3341 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003342 return ok_count < 0 ? ok_count : 0;
Jiri Pirko717503b2017-10-11 09:41:09 +02003343}
Vlad Buslov40119212019-08-26 16:44:59 +03003344EXPORT_SYMBOL(tc_setup_cb_add);
3345
3346/* Destructive filter replace. If filter that wasn't already in hardware is
3347 * successfully offloaded, increment block offload counter. On failure,
3348 * previously offloaded filter is considered to be destroyed and offload counter
3349 * is decremented.
3350 */
3351
3352int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
3353 enum tc_setup_type type, void *type_data, bool err_stop,
3354 u32 *old_flags, unsigned int *old_in_hw_count,
3355 u32 *new_flags, unsigned int *new_in_hw_count,
3356 bool rtnl_held)
3357{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003358 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003359 int ok_count;
3360
Vlad Buslov11bd6342019-08-26 16:45:02 +03003361retry:
3362 if (take_rtnl)
3363 rtnl_lock();
Vlad Buslov40119212019-08-26 16:44:59 +03003364 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003365 /* Need to obtain rtnl lock if block is bound to devs that require it.
3366 * In block bind code cb_lock is obtained while holding rtnl, so we must
3367 * obtain the locks in same order here.
3368 */
3369 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3370 up_read(&block->cb_lock);
3371 take_rtnl = true;
3372 goto retry;
3373 }
3374
Vlad Buslov40119212019-08-26 16:44:59 +03003375 /* Make sure all netdevs sharing this block are offload-capable. */
3376 if (block->nooffloaddevcnt && err_stop) {
3377 ok_count = -EOPNOTSUPP;
3378 goto err_unlock;
3379 }
3380
3381 tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003382 if (tp->ops->hw_del)
3383 tp->ops->hw_del(tp, type_data);
Vlad Buslov40119212019-08-26 16:44:59 +03003384
3385 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003386 if (ok_count < 0)
3387 goto err_unlock;
3388
3389 if (tp->ops->hw_add)
3390 tp->ops->hw_add(tp, type_data);
Vlad Buslov40119212019-08-26 16:44:59 +03003391 if (ok_count > 0)
Vlad Buslova449a3e2019-08-26 16:45:00 +03003392 tc_cls_offload_cnt_update(block, tp, new_in_hw_count,
3393 new_flags, ok_count, true);
Vlad Buslov40119212019-08-26 16:44:59 +03003394err_unlock:
3395 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003396 if (take_rtnl)
3397 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003398 return ok_count < 0 ? ok_count : 0;
3399}
3400EXPORT_SYMBOL(tc_setup_cb_replace);
3401
3402/* Destroy filter and decrement block offload counter, if filter was previously
3403 * offloaded.
3404 */
3405
3406int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
3407 enum tc_setup_type type, void *type_data, bool err_stop,
3408 u32 *flags, unsigned int *in_hw_count, bool rtnl_held)
3409{
Vlad Buslov11bd6342019-08-26 16:45:02 +03003410 bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held;
Vlad Buslov40119212019-08-26 16:44:59 +03003411 int ok_count;
3412
Vlad Buslov11bd6342019-08-26 16:45:02 +03003413retry:
3414 if (take_rtnl)
3415 rtnl_lock();
Vlad Buslov40119212019-08-26 16:44:59 +03003416 down_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003417 /* Need to obtain rtnl lock if block is bound to devs that require it.
3418 * In block bind code cb_lock is obtained while holding rtnl, so we must
3419 * obtain the locks in same order here.
3420 */
3421 if (!rtnl_held && !take_rtnl && block->lockeddevcnt) {
3422 up_read(&block->cb_lock);
3423 take_rtnl = true;
3424 goto retry;
3425 }
3426
Vlad Buslov40119212019-08-26 16:44:59 +03003427 ok_count = __tc_setup_cb_call(block, type, type_data, err_stop);
3428
3429 tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags);
Vlad Buslova449a3e2019-08-26 16:45:00 +03003430 if (tp->ops->hw_del)
3431 tp->ops->hw_del(tp, type_data);
3432
Vlad Buslov40119212019-08-26 16:44:59 +03003433 up_read(&block->cb_lock);
Vlad Buslov11bd6342019-08-26 16:45:02 +03003434 if (take_rtnl)
3435 rtnl_unlock();
Vlad Buslov40119212019-08-26 16:44:59 +03003436 return ok_count < 0 ? ok_count : 0;
3437}
3438EXPORT_SYMBOL(tc_setup_cb_destroy);
3439
3440int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
3441 bool add, flow_setup_cb_t *cb,
3442 enum tc_setup_type type, void *type_data,
3443 void *cb_priv, u32 *flags, unsigned int *in_hw_count)
3444{
3445 int err = cb(type, type_data, cb_priv);
3446
3447 if (err) {
3448 if (add && tc_skip_sw(*flags))
3449 return err;
3450 } else {
3451 tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1,
3452 add);
3453 }
3454
3455 return 0;
3456}
3457EXPORT_SYMBOL(tc_setup_cb_reoffload);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02003458
Jiri Pirko20084952020-02-25 11:45:18 +01003459static int tcf_act_get_cookie(struct flow_action_entry *entry,
3460 const struct tc_action *act)
3461{
3462 struct tc_cookie *cookie;
3463 int err = 0;
3464
3465 rcu_read_lock();
3466 cookie = rcu_dereference(act->act_cookie);
3467 if (cookie) {
3468 entry->cookie = flow_action_cookie_create(cookie->data,
3469 cookie->len,
3470 GFP_ATOMIC);
3471 if (!entry->cookie)
3472 err = -ENOMEM;
3473 }
3474 rcu_read_unlock();
3475 return err;
3476}
3477
3478static void tcf_act_put_cookie(struct flow_action_entry *entry)
3479{
3480 flow_action_cookie_destroy(entry->cookie);
3481}
3482
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03003483void tc_cleanup_flow_action(struct flow_action *flow_action)
3484{
3485 struct flow_action_entry *entry;
3486 int i;
3487
Jiri Pirko20084952020-02-25 11:45:18 +01003488 flow_action_for_each(i, entry, flow_action) {
3489 tcf_act_put_cookie(entry);
Vlad Buslov11589582019-09-13 18:28:39 +03003490 if (entry->destructor)
3491 entry->destructor(entry->destructor_priv);
Jiri Pirko20084952020-02-25 11:45:18 +01003492 }
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03003493}
3494EXPORT_SYMBOL(tc_cleanup_flow_action);
3495
Vlad Buslov11589582019-09-13 18:28:39 +03003496static void tcf_mirred_get_dev(struct flow_action_entry *entry,
3497 const struct tc_action *act)
3498{
Vlad Buslov470d5062019-09-13 18:28:41 +03003499#ifdef CONFIG_NET_CLS_ACT
3500 entry->dev = act->ops->get_dev(act, &entry->destructor);
Vlad Buslov11589582019-09-13 18:28:39 +03003501 if (!entry->dev)
3502 return;
Vlad Buslov11589582019-09-13 18:28:39 +03003503 entry->destructor_priv = entry->dev;
Vlad Buslov470d5062019-09-13 18:28:41 +03003504#endif
Vlad Buslov11589582019-09-13 18:28:39 +03003505}
3506
3507static void tcf_tunnel_encap_put_tunnel(void *priv)
3508{
3509 struct ip_tunnel_info *tunnel = priv;
3510
3511 kfree(tunnel);
3512}
3513
3514static int tcf_tunnel_encap_get_tunnel(struct flow_action_entry *entry,
3515 const struct tc_action *act)
3516{
3517 entry->tunnel = tcf_tunnel_info_copy(act);
3518 if (!entry->tunnel)
3519 return -ENOMEM;
3520 entry->destructor = tcf_tunnel_encap_put_tunnel;
3521 entry->destructor_priv = entry->tunnel;
3522 return 0;
3523}
3524
Vlad Buslov4a5da472019-09-13 18:28:40 +03003525static void tcf_sample_get_group(struct flow_action_entry *entry,
3526 const struct tc_action *act)
3527{
3528#ifdef CONFIG_NET_CLS_ACT
3529 entry->sample.psample_group =
3530 act->ops->get_psample_group(act, &entry->destructor);
3531 entry->destructor_priv = entry->sample.psample_group;
3532#endif
3533}
3534
Po Liud29bdd62020-05-01 08:53:16 +08003535static void tcf_gate_entry_destructor(void *priv)
3536{
3537 struct action_gate_entry *oe = priv;
3538
3539 kfree(oe);
3540}
3541
3542static int tcf_gate_get_entries(struct flow_action_entry *entry,
3543 const struct tc_action *act)
3544{
3545 entry->gate.entries = tcf_gate_get_list(act);
3546
3547 if (!entry->gate.entries)
3548 return -EINVAL;
3549
3550 entry->destructor = tcf_gate_entry_destructor;
3551 entry->destructor_priv = entry->gate.entries;
3552
3553 return 0;
3554}
3555
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +02003556static enum flow_action_hw_stats tc_act_hw_stats(u8 hw_stats)
3557{
3558 if (WARN_ON_ONCE(hw_stats > TCA_ACT_HW_STATS_ANY))
3559 return FLOW_ACTION_HW_STATS_DONT_CARE;
3560 else if (!hw_stats)
3561 return FLOW_ACTION_HW_STATS_DISABLED;
3562
3563 return hw_stats;
3564}
3565
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003566int tc_setup_flow_action(struct flow_action *flow_action,
Vlad Buslovb15e7a62020-02-17 12:12:12 +02003567 const struct tcf_exts *exts)
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003568{
Vlad Buslov7a472812020-02-17 12:12:09 +02003569 struct tc_action *act;
Vlad Buslov9838b202019-08-26 16:45:03 +03003570 int i, j, k, err = 0;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003571
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -07003572 BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY);
3573 BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE);
3574 BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED);
Jiri Pirko44f86582020-03-07 12:40:20 +01003575
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003576 if (!exts)
3577 return 0;
3578
3579 j = 0;
3580 tcf_exts_for_each_action(i, act, exts) {
3581 struct flow_action_entry *entry;
3582
3583 entry = &flow_action->entries[j];
Vlad Buslov7a472812020-02-17 12:12:09 +02003584 spin_lock_bh(&act->tcfa_lock);
Jiri Pirko20084952020-02-25 11:45:18 +01003585 err = tcf_act_get_cookie(entry, act);
3586 if (err)
3587 goto err_out_locked;
Jiri Pirko44f86582020-03-07 12:40:20 +01003588
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +02003589 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
Jiri Pirko44f86582020-03-07 12:40:20 +01003590
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003591 if (is_tcf_gact_ok(act)) {
3592 entry->id = FLOW_ACTION_ACCEPT;
3593 } else if (is_tcf_gact_shot(act)) {
3594 entry->id = FLOW_ACTION_DROP;
3595 } else if (is_tcf_gact_trap(act)) {
3596 entry->id = FLOW_ACTION_TRAP;
3597 } else if (is_tcf_gact_goto_chain(act)) {
3598 entry->id = FLOW_ACTION_GOTO;
3599 entry->chain_index = tcf_gact_goto_chain_index(act);
3600 } else if (is_tcf_mirred_egress_redirect(act)) {
3601 entry->id = FLOW_ACTION_REDIRECT;
Vlad Buslov11589582019-09-13 18:28:39 +03003602 tcf_mirred_get_dev(entry, act);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003603 } else if (is_tcf_mirred_egress_mirror(act)) {
3604 entry->id = FLOW_ACTION_MIRRED;
Vlad Buslov11589582019-09-13 18:28:39 +03003605 tcf_mirred_get_dev(entry, act);
John Hurley48e584a2019-08-04 16:09:06 +01003606 } else if (is_tcf_mirred_ingress_redirect(act)) {
3607 entry->id = FLOW_ACTION_REDIRECT_INGRESS;
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_mirror(act)) {
3610 entry->id = FLOW_ACTION_MIRRED_INGRESS;
Vlad Buslov11589582019-09-13 18:28:39 +03003611 tcf_mirred_get_dev(entry, act);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003612 } else if (is_tcf_vlan(act)) {
3613 switch (tcf_vlan_action(act)) {
3614 case TCA_VLAN_ACT_PUSH:
3615 entry->id = FLOW_ACTION_VLAN_PUSH;
3616 entry->vlan.vid = tcf_vlan_push_vid(act);
3617 entry->vlan.proto = tcf_vlan_push_proto(act);
3618 entry->vlan.prio = tcf_vlan_push_prio(act);
3619 break;
3620 case TCA_VLAN_ACT_POP:
3621 entry->id = FLOW_ACTION_VLAN_POP;
3622 break;
3623 case TCA_VLAN_ACT_MODIFY:
3624 entry->id = FLOW_ACTION_VLAN_MANGLE;
3625 entry->vlan.vid = tcf_vlan_push_vid(act);
3626 entry->vlan.proto = tcf_vlan_push_proto(act);
3627 entry->vlan.prio = tcf_vlan_push_prio(act);
3628 break;
3629 default:
Vlad Buslov9838b202019-08-26 16:45:03 +03003630 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003631 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003632 }
3633 } else if (is_tcf_tunnel_set(act)) {
3634 entry->id = FLOW_ACTION_TUNNEL_ENCAP;
Vlad Buslov11589582019-09-13 18:28:39 +03003635 err = tcf_tunnel_encap_get_tunnel(entry, act);
3636 if (err)
Vlad Buslov7a472812020-02-17 12:12:09 +02003637 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003638 } else if (is_tcf_tunnel_release(act)) {
3639 entry->id = FLOW_ACTION_TUNNEL_DECAP;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003640 } else if (is_tcf_pedit(act)) {
3641 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
3642 switch (tcf_pedit_cmd(act, k)) {
3643 case TCA_PEDIT_KEY_EX_CMD_SET:
3644 entry->id = FLOW_ACTION_MANGLE;
3645 break;
3646 case TCA_PEDIT_KEY_EX_CMD_ADD:
3647 entry->id = FLOW_ACTION_ADD;
3648 break;
3649 default:
Vlad Buslov9838b202019-08-26 16:45:03 +03003650 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003651 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003652 }
3653 entry->mangle.htype = tcf_pedit_htype(act, k);
3654 entry->mangle.mask = tcf_pedit_mask(act, k);
3655 entry->mangle.val = tcf_pedit_val(act, k);
3656 entry->mangle.offset = tcf_pedit_offset(act, k);
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +02003657 entry->hw_stats = tc_act_hw_stats(act->hw_stats);
Petr Machata2c4b58d2020-03-18 19:42:29 +02003658 entry = &flow_action->entries[++j];
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003659 }
3660 } else if (is_tcf_csum(act)) {
3661 entry->id = FLOW_ACTION_CSUM;
3662 entry->csum_flags = tcf_csum_update_flags(act);
3663 } else if (is_tcf_skbedit_mark(act)) {
3664 entry->id = FLOW_ACTION_MARK;
3665 entry->mark = tcf_skbedit_mark(act);
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -07003666 } else if (is_tcf_sample(act)) {
3667 entry->id = FLOW_ACTION_SAMPLE;
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -07003668 entry->sample.trunc_size = tcf_sample_trunc_size(act);
3669 entry->sample.truncate = tcf_sample_truncate(act);
3670 entry->sample.rate = tcf_sample_rate(act);
Vlad Buslov4a5da472019-09-13 18:28:40 +03003671 tcf_sample_get_group(entry, act);
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -07003672 } else if (is_tcf_police(act)) {
3673 entry->id = FLOW_ACTION_POLICE;
Po Liu5f035af2020-06-29 14:54:16 +08003674 entry->police.burst = tcf_police_burst(act);
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -07003675 entry->police.rate_bytes_ps =
3676 tcf_police_rate_bytes_ps(act);
Po Liu19e528d2020-06-24 17:36:28 +08003677 entry->police.mtu = tcf_police_tcfp_mtu(act);
Po Liu627e39b2020-06-24 17:36:30 +08003678 entry->police.index = act->tcfa_index;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +03003679 } else if (is_tcf_ct(act)) {
3680 entry->id = FLOW_ACTION_CT;
3681 entry->ct.action = tcf_ct_action(act);
3682 entry->ct.zone = tcf_ct_zone(act);
Paul Blakeyedd58612020-03-12 12:23:09 +02003683 entry->ct.flow_table = tcf_ct_ft(act);
John Hurley6749d5902019-07-23 15:33:59 +01003684 } else if (is_tcf_mpls(act)) {
3685 switch (tcf_mpls_action(act)) {
3686 case TCA_MPLS_ACT_PUSH:
3687 entry->id = FLOW_ACTION_MPLS_PUSH;
3688 entry->mpls_push.proto = tcf_mpls_proto(act);
3689 entry->mpls_push.label = tcf_mpls_label(act);
3690 entry->mpls_push.tc = tcf_mpls_tc(act);
3691 entry->mpls_push.bos = tcf_mpls_bos(act);
3692 entry->mpls_push.ttl = tcf_mpls_ttl(act);
3693 break;
3694 case TCA_MPLS_ACT_POP:
3695 entry->id = FLOW_ACTION_MPLS_POP;
3696 entry->mpls_pop.proto = tcf_mpls_proto(act);
3697 break;
3698 case TCA_MPLS_ACT_MODIFY:
3699 entry->id = FLOW_ACTION_MPLS_MANGLE;
3700 entry->mpls_mangle.label = tcf_mpls_label(act);
3701 entry->mpls_mangle.tc = tcf_mpls_tc(act);
3702 entry->mpls_mangle.bos = tcf_mpls_bos(act);
3703 entry->mpls_mangle.ttl = tcf_mpls_ttl(act);
3704 break;
3705 default:
Baowen Zheng67f43622021-12-13 15:46:04 +01003706 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003707 goto err_out_locked;
John Hurley6749d5902019-07-23 15:33:59 +01003708 }
John Hurleyfb1b7752019-08-04 16:09:04 +01003709 } else if (is_tcf_skbedit_ptype(act)) {
3710 entry->id = FLOW_ACTION_PTYPE;
3711 entry->ptype = tcf_skbedit_ptype(act);
Petr Machata2ce12412020-03-19 15:47:21 +02003712 } else if (is_tcf_skbedit_priority(act)) {
3713 entry->id = FLOW_ACTION_PRIORITY;
3714 entry->priority = tcf_skbedit_priority(act);
Po Liud29bdd62020-05-01 08:53:16 +08003715 } else if (is_tcf_gate(act)) {
3716 entry->id = FLOW_ACTION_GATE;
3717 entry->gate.index = tcf_gate_index(act);
3718 entry->gate.prio = tcf_gate_prio(act);
3719 entry->gate.basetime = tcf_gate_basetime(act);
3720 entry->gate.cycletime = tcf_gate_cycletime(act);
3721 entry->gate.cycletimeext = tcf_gate_cycletimeext(act);
3722 entry->gate.num_entries = tcf_gate_num_entries(act);
3723 err = tcf_gate_get_entries(entry, act);
3724 if (err)
Guillaume Naultb1307622020-10-20 17:34:31 +02003725 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003726 } else {
Vlad Buslov9838b202019-08-26 16:45:03 +03003727 err = -EOPNOTSUPP;
Vlad Buslov7a472812020-02-17 12:12:09 +02003728 goto err_out_locked;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003729 }
Vlad Buslov7a472812020-02-17 12:12:09 +02003730 spin_unlock_bh(&act->tcfa_lock);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003731
3732 if (!is_tcf_pedit(act))
3733 j++;
3734 }
Vlad Buslov9838b202019-08-26 16:45:03 +03003735
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003736err_out:
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03003737 if (err)
3738 tc_cleanup_flow_action(flow_action);
3739
Vlad Buslov9838b202019-08-26 16:45:03 +03003740 return err;
Vlad Buslov7a472812020-02-17 12:12:09 +02003741err_out_locked:
3742 spin_unlock_bh(&act->tcfa_lock);
3743 goto err_out;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +01003744}
3745EXPORT_SYMBOL(tc_setup_flow_action);
3746
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +01003747unsigned int tcf_exts_num_actions(struct tcf_exts *exts)
3748{
3749 unsigned int num_acts = 0;
3750 struct tc_action *act;
3751 int i;
3752
3753 tcf_exts_for_each_action(i, act, exts) {
3754 if (is_tcf_pedit(act))
3755 num_acts += tcf_pedit_nkeys(act);
3756 else
3757 num_acts++;
3758 }
3759 return num_acts;
3760}
3761EXPORT_SYMBOL(tcf_exts_num_actions);
3762
Petr Machata36257502020-06-27 01:45:26 +03003763#ifdef CONFIG_NET_CLS_ACT
3764static int tcf_qevent_parse_block_index(struct nlattr *block_index_attr,
3765 u32 *p_block_index,
3766 struct netlink_ext_ack *extack)
3767{
3768 *p_block_index = nla_get_u32(block_index_attr);
3769 if (!*p_block_index) {
3770 NL_SET_ERR_MSG(extack, "Block number may not be zero");
3771 return -EINVAL;
3772 }
3773
3774 return 0;
3775}
3776
3777int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
3778 enum flow_block_binder_type binder_type,
3779 struct nlattr *block_index_attr,
3780 struct netlink_ext_ack *extack)
3781{
3782 u32 block_index;
3783 int err;
3784
3785 if (!block_index_attr)
3786 return 0;
3787
3788 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3789 if (err)
3790 return err;
3791
3792 if (!block_index)
3793 return 0;
3794
3795 qe->info.binder_type = binder_type;
3796 qe->info.chain_head_change = tcf_chain_head_change_dflt;
3797 qe->info.chain_head_change_priv = &qe->filter_chain;
3798 qe->info.block_index = block_index;
3799
3800 return tcf_block_get_ext(&qe->block, sch, &qe->info, extack);
3801}
3802EXPORT_SYMBOL(tcf_qevent_init);
3803
3804void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
3805{
3806 if (qe->info.block_index)
3807 tcf_block_put_ext(qe->block, sch, &qe->info);
3808}
3809EXPORT_SYMBOL(tcf_qevent_destroy);
3810
3811int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
3812 struct netlink_ext_ack *extack)
3813{
3814 u32 block_index;
3815 int err;
3816
3817 if (!block_index_attr)
3818 return 0;
3819
3820 err = tcf_qevent_parse_block_index(block_index_attr, &block_index, extack);
3821 if (err)
3822 return err;
3823
3824 /* Bounce newly-configured block or change in block. */
3825 if (block_index != qe->info.block_index) {
3826 NL_SET_ERR_MSG(extack, "Change of blocks is not supported");
3827 return -EINVAL;
3828 }
3829
3830 return 0;
3831}
3832EXPORT_SYMBOL(tcf_qevent_validate_change);
3833
3834struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
Petr Machata55f656c2020-07-14 20:03:07 +03003835 struct sk_buff **to_free, int *ret)
Petr Machata36257502020-06-27 01:45:26 +03003836{
3837 struct tcf_result cl_res;
3838 struct tcf_proto *fl;
3839
3840 if (!qe->info.block_index)
3841 return skb;
3842
3843 fl = rcu_dereference_bh(qe->filter_chain);
3844
Petr Machata36257502020-06-27 01:45:26 +03003845 switch (tcf_classify(skb, fl, &cl_res, false)) {
3846 case TC_ACT_SHOT:
3847 qdisc_qstats_drop(sch);
3848 __qdisc_drop(skb, to_free);
3849 *ret = __NET_XMIT_BYPASS;
3850 return NULL;
3851 case TC_ACT_STOLEN:
3852 case TC_ACT_QUEUED:
3853 case TC_ACT_TRAP:
3854 __qdisc_drop(skb, to_free);
3855 *ret = __NET_XMIT_STOLEN;
3856 return NULL;
3857 case TC_ACT_REDIRECT:
3858 skb_do_redirect(skb);
3859 *ret = __NET_XMIT_STOLEN;
3860 return NULL;
3861 }
3862
Petr Machata36257502020-06-27 01:45:26 +03003863 return skb;
3864}
3865EXPORT_SYMBOL(tcf_qevent_handle);
3866
3867int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
3868{
3869 if (!qe->info.block_index)
3870 return 0;
3871 return nla_put_u32(skb, attr_name, qe->info.block_index);
3872}
3873EXPORT_SYMBOL(tcf_qevent_dump);
3874#endif
3875
Jiri Pirko48617382018-01-17 11:46:46 +01003876static __net_init int tcf_net_init(struct net *net)
3877{
3878 struct tcf_net *tn = net_generic(net, tcf_net_id);
3879
Vlad Buslovab281622018-09-24 19:22:56 +03003880 spin_lock_init(&tn->idr_lock);
Jiri Pirko48617382018-01-17 11:46:46 +01003881 idr_init(&tn->idr);
3882 return 0;
3883}
3884
3885static void __net_exit tcf_net_exit(struct net *net)
3886{
3887 struct tcf_net *tn = net_generic(net, tcf_net_id);
3888
3889 idr_destroy(&tn->idr);
3890}
3891
3892static struct pernet_operations tcf_net_ops = {
3893 .init = tcf_net_init,
3894 .exit = tcf_net_exit,
3895 .id = &tcf_net_id,
3896 .size = sizeof(struct tcf_net),
3897};
3898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899static int __init tc_filter_init(void)
3900{
Jiri Pirko48617382018-01-17 11:46:46 +01003901 int err;
3902
Cong Wang7aa00452017-10-26 18:24:28 -07003903 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
3904 if (!tc_filter_wq)
3905 return -ENOMEM;
3906
Jiri Pirko48617382018-01-17 11:46:46 +01003907 err = register_pernet_subsys(&tcf_net_ops);
3908 if (err)
3909 goto err_register_pernet_subsys;
3910
Vlad Buslov470502d2019-02-11 10:55:48 +02003911 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL,
3912 RTNL_FLAG_DOIT_UNLOCKED);
3913 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL,
3914 RTNL_FLAG_DOIT_UNLOCKED);
Vlad Buslovc431f892018-05-31 09:52:53 +03003915 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Vlad Buslov470502d2019-02-11 10:55:48 +02003916 tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02003917 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
3918 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
3919 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
3920 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01003923
3924err_register_pernet_subsys:
3925 destroy_workqueue(tc_filter_wq);
3926 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927}
3928
3929subsys_initcall(tc_filter_init);