blob: eb0bf9037ef956054c78903ec02a915eaaf5a788 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/cls_api.c Packet classifier API.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * Changes:
12 *
13 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support
14 *
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
Jiri Pirko33a48922017-02-09 14:38:57 +010022#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Jiri Pirko48617382018-01-17 11:46:46 +010027#include <linux/idr.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110028#include <net/net_namespace.h>
29#include <net/sock.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070030#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/pkt_sched.h>
32#include <net/pkt_cls.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* The list of all installed classifier types */
WANG Cong36272872013-12-15 20:15:11 -080035static LIST_HEAD(tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Protects list of registered TC modules. It is pure SMP lock. */
38static DEFINE_RWLOCK(cls_mod_lock);
39
40/* Find classifier type by string name */
41
Jiri Pirkof34e8bf2018-07-23 09:23:04 +020042static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Eric Dumazetdcd76082013-12-20 10:04:18 -080044 const struct tcf_proto_ops *t, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 if (kind) {
47 read_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080048 list_for_each_entry(t, &tcf_proto_base, head) {
Jiri Pirko33a48922017-02-09 14:38:57 +010049 if (strcmp(kind, t->kind) == 0) {
Eric Dumazetdcd76082013-12-20 10:04:18 -080050 if (try_module_get(t->owner))
51 res = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53 }
54 }
55 read_unlock(&cls_mod_lock);
56 }
Eric Dumazetdcd76082013-12-20 10:04:18 -080057 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Jiri Pirkof34e8bf2018-07-23 09:23:04 +020060static const struct tcf_proto_ops *
61tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
62{
63 const struct tcf_proto_ops *ops;
64
65 ops = __tcf_proto_lookup_ops(kind);
66 if (ops)
67 return ops;
68#ifdef CONFIG_MODULES
69 rtnl_unlock();
70 request_module("cls_%s", kind);
71 rtnl_lock();
72 ops = __tcf_proto_lookup_ops(kind);
73 /* We dropped the RTNL semaphore in order to perform
74 * the module load. So, even if we succeeded in loading
75 * the module we have to replay the request. We indicate
76 * this using -EAGAIN.
77 */
78 if (ops) {
79 module_put(ops->owner);
80 return ERR_PTR(-EAGAIN);
81 }
82#endif
83 NL_SET_ERR_MSG(extack, "TC classifier not found");
84 return ERR_PTR(-ENOENT);
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Register(unregister) new classifier type */
88
89int register_tcf_proto_ops(struct tcf_proto_ops *ops)
90{
WANG Cong36272872013-12-15 20:15:11 -080091 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 int rc = -EEXIST;
93
94 write_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080095 list_for_each_entry(t, &tcf_proto_base, head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (!strcmp(ops->kind, t->kind))
97 goto out;
98
WANG Cong36272872013-12-15 20:15:11 -080099 list_add_tail(&ops->head, &tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 rc = 0;
101out:
102 write_unlock(&cls_mod_lock);
103 return rc;
104}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800105EXPORT_SYMBOL(register_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Cong Wang7aa00452017-10-26 18:24:28 -0700107static struct workqueue_struct *tc_filter_wq;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
110{
WANG Cong36272872013-12-15 20:15:11 -0800111 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int rc = -ENOENT;
113
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200114 /* Wait for outstanding call_rcu()s, if any, from a
115 * tcf_proto_ops's destroy() handler.
116 */
117 rcu_barrier();
Cong Wang7aa00452017-10-26 18:24:28 -0700118 flush_workqueue(tc_filter_wq);
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 write_lock(&cls_mod_lock);
Eric Dumazetdcd76082013-12-20 10:04:18 -0800121 list_for_each_entry(t, &tcf_proto_base, head) {
122 if (t == ops) {
123 list_del(&t->head);
124 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
Eric Dumazetdcd76082013-12-20 10:04:18 -0800126 }
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 write_unlock(&cls_mod_lock);
129 return rc;
130}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800131EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Cong Wangaaa908f2018-05-23 15:26:53 -0700133bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
Cong Wang7aa00452017-10-26 18:24:28 -0700134{
Cong Wangaaa908f2018-05-23 15:26:53 -0700135 INIT_RCU_WORK(rwork, func);
136 return queue_rcu_work(tc_filter_wq, rwork);
Cong Wang7aa00452017-10-26 18:24:28 -0700137}
138EXPORT_SYMBOL(tcf_queue_work);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* Select new prio value from the range, managed by kernel. */
141
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800142static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800144 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000147 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jiri Pirko79619732017-05-17 11:07:58 +0200149 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Jiri Pirko33a48922017-02-09 14:38:57 +0100152static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500153 u32 prio, struct tcf_chain *chain,
154 struct netlink_ext_ack *extack)
Jiri Pirko33a48922017-02-09 14:38:57 +0100155{
156 struct tcf_proto *tp;
157 int err;
158
159 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
160 if (!tp)
161 return ERR_PTR(-ENOBUFS);
162
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200163 tp->ops = tcf_proto_lookup_ops(kind, extack);
164 if (IS_ERR(tp->ops)) {
165 err = PTR_ERR(tp->ops);
Jiri Pirkod68d75f2018-05-11 17:45:32 +0200166 goto errout;
Jiri Pirko33a48922017-02-09 14:38:57 +0100167 }
168 tp->classify = tp->ops->classify;
169 tp->protocol = protocol;
170 tp->prio = prio;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200171 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100172
173 err = tp->ops->init(tp);
174 if (err) {
175 module_put(tp->ops->owner);
176 goto errout;
177 }
178 return tp;
179
180errout:
181 kfree(tp);
182 return ERR_PTR(err);
183}
184
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800185static void tcf_proto_destroy(struct tcf_proto *tp,
186 struct netlink_ext_ack *extack)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100187{
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800188 tp->ops->destroy(tp, extack);
WANG Cong763dbf62017-04-19 14:21:21 -0700189 module_put(tp->ops->owner);
190 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100191}
192
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100193struct tcf_filter_chain_list_item {
194 struct list_head list;
195 tcf_chain_head_change_t *chain_head_change;
196 void *chain_head_change_priv;
197};
198
Jiri Pirko5bc17012017-05-17 11:08:01 +0200199static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
200 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200201{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200202 struct tcf_chain *chain;
203
204 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
205 if (!chain)
206 return NULL;
207 list_add_tail(&chain->list, &block->chain_list);
208 chain->block = block;
209 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700210 chain->refcnt = 1;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200211 if (!chain->index)
212 block->chain0.chain = chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200213 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200214}
215
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100216static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
217 struct tcf_proto *tp_head)
218{
219 if (item->chain_head_change)
220 item->chain_head_change(tp_head, item->chain_head_change_priv);
221}
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200222
223static void tcf_chain0_head_change(struct tcf_chain *chain,
224 struct tcf_proto *tp_head)
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100225{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100226 struct tcf_filter_chain_list_item *item;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200227 struct tcf_block *block = chain->block;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100228
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200229 if (chain->index)
230 return;
231 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100232 tcf_chain_head_change_item(item, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100233}
234
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200235static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100236{
Roman Kapld7aa04a2017-11-20 22:21:13 +0100237 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100238
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200239 tcf_chain0_head_change(chain, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100240 while (tp) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200241 RCU_INIT_POINTER(chain->filter_chain, tp->next);
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800242 tcf_proto_destroy(tp, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100243 tp = rtnl_dereference(chain->filter_chain);
244 tcf_chain_put(chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100245 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200246}
247
248static void tcf_chain_destroy(struct tcf_chain *chain)
249{
Cong Wangefbf7892017-12-04 10:48:18 -0800250 struct tcf_block *block = chain->block;
251
Cong Wange2ef7542017-09-11 16:33:31 -0700252 list_del(&chain->list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200253 if (!chain->index)
254 block->chain0.chain = NULL;
Cong Wange2ef7542017-09-11 16:33:31 -0700255 kfree(chain);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200256 if (list_empty(&block->chain_list) && block->refcnt == 0)
Cong Wangefbf7892017-12-04 10:48:18 -0800257 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700258}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200259
Cong Wange2ef7542017-09-11 16:33:31 -0700260static void tcf_chain_hold(struct tcf_chain *chain)
261{
262 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200263}
264
WANG Cong367a8ce2017-05-23 09:42:37 -0700265struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
266 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200267{
268 struct tcf_chain *chain;
269
270 list_for_each_entry(chain, &block->chain_list, list) {
Cong Wange2ef7542017-09-11 16:33:31 -0700271 if (chain->index == chain_index) {
272 tcf_chain_hold(chain);
273 return chain;
274 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200275 }
Jiri Pirko80532382017-09-06 13:14:19 +0200276
Cong Wange2ef7542017-09-11 16:33:31 -0700277 return create ? tcf_chain_create(block, chain_index) : NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200278}
279EXPORT_SYMBOL(tcf_chain_get);
280
281void tcf_chain_put(struct tcf_chain *chain)
282{
Cong Wange2ef7542017-09-11 16:33:31 -0700283 if (--chain->refcnt == 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200284 tcf_chain_destroy(chain);
285}
286EXPORT_SYMBOL(tcf_chain_put);
287
Jiri Pirkocaa72602018-01-17 11:46:50 +0100288static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200289{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100290 return block->offloadcnt;
291}
292
293static int tcf_block_offload_cmd(struct tcf_block *block,
294 struct net_device *dev,
295 struct tcf_block_ext_info *ei,
John Hurley60513bd2018-06-25 14:30:04 -0700296 enum tc_block_command command,
297 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100298{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200299 struct tc_block_offload bo = {};
300
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200301 bo.command = command;
302 bo.binder_type = ei->binder_type;
303 bo.block = block;
John Hurley60513bd2018-06-25 14:30:04 -0700304 bo.extack = extack;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100305 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200306}
307
Jiri Pirkocaa72602018-01-17 11:46:50 +0100308static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700309 struct tcf_block_ext_info *ei,
310 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200311{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100312 struct net_device *dev = q->dev_queue->dev;
313 int err;
314
315 if (!dev->netdev_ops->ndo_setup_tc)
316 goto no_offload_dev_inc;
317
318 /* If tc offload feature is disabled and the block we try to bind
319 * to already has some offloaded filters, forbid to bind.
320 */
John Hurley60513bd2018-06-25 14:30:04 -0700321 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
322 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Jiri Pirkocaa72602018-01-17 11:46:50 +0100323 return -EOPNOTSUPP;
John Hurley60513bd2018-06-25 14:30:04 -0700324 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100325
John Hurley60513bd2018-06-25 14:30:04 -0700326 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100327 if (err == -EOPNOTSUPP)
328 goto no_offload_dev_inc;
329 return err;
330
331no_offload_dev_inc:
332 if (tcf_block_offload_in_use(block))
333 return -EOPNOTSUPP;
334 block->nooffloaddevcnt++;
335 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200336}
337
338static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
339 struct tcf_block_ext_info *ei)
340{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100341 struct net_device *dev = q->dev_queue->dev;
342 int err;
343
344 if (!dev->netdev_ops->ndo_setup_tc)
345 goto no_offload_dev_dec;
John Hurley60513bd2018-06-25 14:30:04 -0700346 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100347 if (err == -EOPNOTSUPP)
348 goto no_offload_dev_dec;
349 return;
350
351no_offload_dev_dec:
352 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200353}
354
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100355static int
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200356tcf_chain0_head_change_cb_add(struct tcf_block *block,
357 struct tcf_block_ext_info *ei,
358 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100359{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200360 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100361 struct tcf_filter_chain_list_item *item;
362
363 item = kmalloc(sizeof(*item), GFP_KERNEL);
364 if (!item) {
365 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
366 return -ENOMEM;
367 }
368 item->chain_head_change = ei->chain_head_change;
369 item->chain_head_change_priv = ei->chain_head_change_priv;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200370 if (chain0 && chain0->filter_chain)
371 tcf_chain_head_change_item(item, chain0->filter_chain);
372 list_add(&item->list, &block->chain0.filter_chain_list);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100373 return 0;
374}
375
376static void
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200377tcf_chain0_head_change_cb_del(struct tcf_block *block,
378 struct tcf_block_ext_info *ei)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100379{
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200380 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100381 struct tcf_filter_chain_list_item *item;
382
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200383 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100384 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
385 (item->chain_head_change == ei->chain_head_change &&
386 item->chain_head_change_priv == ei->chain_head_change_priv)) {
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200387 if (chain0)
388 tcf_chain_head_change_item(item, NULL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100389 list_del(&item->list);
390 kfree(item);
391 return;
392 }
393 }
394 WARN_ON(1);
395}
396
Jiri Pirko48617382018-01-17 11:46:46 +0100397struct tcf_net {
398 struct idr idr;
399};
400
401static unsigned int tcf_net_id;
402
403static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100404 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100405{
Jiri Pirko48617382018-01-17 11:46:46 +0100406 struct tcf_net *tn = net_generic(net, tcf_net_id);
Jiri Pirko48617382018-01-17 11:46:46 +0100407
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100408 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
409 GFP_KERNEL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100410}
411
Jiri Pirko48617382018-01-17 11:46:46 +0100412static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200413{
Jiri Pirko48617382018-01-17 11:46:46 +0100414 struct tcf_net *tn = net_generic(net, tcf_net_id);
415
Matthew Wilcox9c160942017-11-28 09:48:43 -0500416 idr_remove(&tn->idr, block->index);
Jiri Pirko48617382018-01-17 11:46:46 +0100417}
418
419static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100420 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100421 struct netlink_ext_ack *extack)
422{
423 struct tcf_block *block;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200424
Jiri Pirko48617382018-01-17 11:46:46 +0100425 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500426 if (!block) {
427 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100428 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500429 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200430 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200431 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100432 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200433 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200434
Jiri Pirko48617382018-01-17 11:46:46 +0100435 block->refcnt = 1;
436 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100437 block->index = block_index;
438
439 /* Don't store q pointer for blocks which are shared */
440 if (!tcf_block_shared(block))
441 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100442 return block;
Jiri Pirko48617382018-01-17 11:46:46 +0100443}
444
445static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
446{
447 struct tcf_net *tn = net_generic(net, tcf_net_id);
448
Matthew Wilcox322d8842017-11-28 10:01:24 -0500449 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100450}
451
Vlad Buslovc431f892018-05-31 09:52:53 +0300452/* Find tcf block.
453 * Set q, parent, cl when appropriate.
454 */
455
456static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
457 u32 *parent, unsigned long *cl,
458 int ifindex, u32 block_index,
459 struct netlink_ext_ack *extack)
460{
461 struct tcf_block *block;
462
463 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
464 block = tcf_block_lookup(net, block_index);
465 if (!block) {
466 NL_SET_ERR_MSG(extack, "Block of given index was not found");
467 return ERR_PTR(-EINVAL);
468 }
469 } else {
470 const struct Qdisc_class_ops *cops;
471 struct net_device *dev;
472
473 /* Find link */
474 dev = __dev_get_by_index(net, ifindex);
475 if (!dev)
476 return ERR_PTR(-ENODEV);
477
478 /* Find qdisc */
479 if (!*parent) {
480 *q = dev->qdisc;
481 *parent = (*q)->handle;
482 } else {
483 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
484 if (!*q) {
485 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
486 return ERR_PTR(-EINVAL);
487 }
488 }
489
490 /* Is it classful? */
491 cops = (*q)->ops->cl_ops;
492 if (!cops) {
493 NL_SET_ERR_MSG(extack, "Qdisc not classful");
494 return ERR_PTR(-EINVAL);
495 }
496
497 if (!cops->tcf_block) {
498 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
499 return ERR_PTR(-EOPNOTSUPP);
500 }
501
502 /* Do we search for filter, attached to class? */
503 if (TC_H_MIN(*parent)) {
504 *cl = cops->find(*q, *parent);
505 if (*cl == 0) {
506 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
507 return ERR_PTR(-ENOENT);
508 }
509 }
510
511 /* And the last stroke */
512 block = cops->tcf_block(*q, *cl, extack);
513 if (!block)
514 return ERR_PTR(-EINVAL);
515 if (tcf_block_shared(block)) {
516 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
517 return ERR_PTR(-EOPNOTSUPP);
518 }
519 }
520
521 return block;
522}
523
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100524struct tcf_block_owner_item {
525 struct list_head list;
526 struct Qdisc *q;
527 enum tcf_block_binder_type binder_type;
528};
529
530static void
531tcf_block_owner_netif_keep_dst(struct tcf_block *block,
532 struct Qdisc *q,
533 enum tcf_block_binder_type binder_type)
534{
535 if (block->keep_dst &&
536 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
537 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
538 netif_keep_dst(qdisc_dev(q));
539}
540
541void tcf_block_netif_keep_dst(struct tcf_block *block)
542{
543 struct tcf_block_owner_item *item;
544
545 block->keep_dst = true;
546 list_for_each_entry(item, &block->owner_list, list)
547 tcf_block_owner_netif_keep_dst(block, item->q,
548 item->binder_type);
549}
550EXPORT_SYMBOL(tcf_block_netif_keep_dst);
551
552static int tcf_block_owner_add(struct tcf_block *block,
553 struct Qdisc *q,
554 enum tcf_block_binder_type binder_type)
555{
556 struct tcf_block_owner_item *item;
557
558 item = kmalloc(sizeof(*item), GFP_KERNEL);
559 if (!item)
560 return -ENOMEM;
561 item->q = q;
562 item->binder_type = binder_type;
563 list_add(&item->list, &block->owner_list);
564 return 0;
565}
566
567static void tcf_block_owner_del(struct tcf_block *block,
568 struct Qdisc *q,
569 enum tcf_block_binder_type binder_type)
570{
571 struct tcf_block_owner_item *item;
572
573 list_for_each_entry(item, &block->owner_list, list) {
574 if (item->q == q && item->binder_type == binder_type) {
575 list_del(&item->list);
576 kfree(item);
577 return;
578 }
579 }
580 WARN_ON(1);
581}
582
Jiri Pirko48617382018-01-17 11:46:46 +0100583int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
584 struct tcf_block_ext_info *ei,
585 struct netlink_ext_ack *extack)
586{
587 struct net *net = qdisc_net(q);
588 struct tcf_block *block = NULL;
589 bool created = false;
590 int err;
591
592 if (ei->block_index) {
593 /* block_index not 0 means the shared block is requested */
594 block = tcf_block_lookup(net, ei->block_index);
595 if (block)
596 block->refcnt++;
597 }
598
599 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100600 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100601 if (IS_ERR(block))
602 return PTR_ERR(block);
603 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100604 if (tcf_block_shared(block)) {
605 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100606 if (err)
607 goto err_block_insert;
608 }
609 }
610
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100611 err = tcf_block_owner_add(block, q, ei->binder_type);
612 if (err)
613 goto err_block_owner_add;
614
615 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
616
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200617 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100618 if (err)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200619 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100620
John Hurley60513bd2018-06-25 14:30:04 -0700621 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100622 if (err)
623 goto err_block_offload_bind;
624
Jiri Pirko6529eab2017-05-17 11:07:55 +0200625 *p_block = block;
626 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200627
Jiri Pirkocaa72602018-01-17 11:46:50 +0100628err_block_offload_bind:
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200629 tcf_chain0_head_change_cb_del(block, ei);
630err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100631 tcf_block_owner_del(block, q, ei->binder_type);
632err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100633 if (created) {
634 if (tcf_block_shared(block))
635 tcf_block_remove(block, net);
636err_block_insert:
Jiri Pirko48617382018-01-17 11:46:46 +0100637 kfree(block);
638 } else {
639 block->refcnt--;
640 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200641 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200642}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200643EXPORT_SYMBOL(tcf_block_get_ext);
644
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100645static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
646{
647 struct tcf_proto __rcu **p_filter_chain = priv;
648
649 rcu_assign_pointer(*p_filter_chain, tp_head);
650}
651
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200652int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500653 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
654 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200655{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100656 struct tcf_block_ext_info ei = {
657 .chain_head_change = tcf_chain_head_change_dflt,
658 .chain_head_change_priv = p_filter_chain,
659 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200660
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100661 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500662 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200663}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200664EXPORT_SYMBOL(tcf_block_get);
665
Cong Wang7aa00452017-10-26 18:24:28 -0700666/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100667 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700668 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100669void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900670 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700671{
Cong Wangefbf7892017-12-04 10:48:18 -0800672 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700673
David S. Millerc30abd52017-12-16 22:11:55 -0500674 if (!block)
675 return;
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200676 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100677 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100678
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200679 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100680 if (tcf_block_shared(block))
681 tcf_block_remove(block, block->net);
682
683 /* Hold a refcnt for all chains, so that they don't disappear
684 * while we are iterating.
685 */
686 list_for_each_entry(chain, &block->chain_list, list)
687 tcf_chain_hold(chain);
688
689 list_for_each_entry(chain, &block->chain_list, list)
690 tcf_chain_flush(chain);
691 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700692
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100693 tcf_block_offload_unbind(block, q, ei);
694
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200695 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100696 /* At this point, all the chains should have refcnt >= 1. */
697 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
698 tcf_chain_put(chain);
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100699
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200700 block->refcnt--;
701 if (list_empty(&block->chain_list))
702 kfree(block);
Jiri Pirko48617382018-01-17 11:46:46 +0100703 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200704}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200705EXPORT_SYMBOL(tcf_block_put_ext);
706
707void tcf_block_put(struct tcf_block *block)
708{
709 struct tcf_block_ext_info ei = {0, };
710
Jiri Pirko4853f122017-12-21 13:13:59 +0100711 if (!block)
712 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100713 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200714}
David S. Millere1ea2f92017-10-30 14:10:01 +0900715
Jiri Pirko6529eab2017-05-17 11:07:55 +0200716EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100717
Jiri Pirkoacb67442017-10-19 15:50:31 +0200718struct tcf_block_cb {
719 struct list_head list;
720 tc_setup_cb_t *cb;
721 void *cb_ident;
722 void *cb_priv;
723 unsigned int refcnt;
724};
725
726void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
727{
728 return block_cb->cb_priv;
729}
730EXPORT_SYMBOL(tcf_block_cb_priv);
731
732struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
733 tc_setup_cb_t *cb, void *cb_ident)
734{ struct tcf_block_cb *block_cb;
735
736 list_for_each_entry(block_cb, &block->cb_list, list)
737 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
738 return block_cb;
739 return NULL;
740}
741EXPORT_SYMBOL(tcf_block_cb_lookup);
742
743void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
744{
745 block_cb->refcnt++;
746}
747EXPORT_SYMBOL(tcf_block_cb_incref);
748
749unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
750{
751 return --block_cb->refcnt;
752}
753EXPORT_SYMBOL(tcf_block_cb_decref);
754
John Hurley32636742018-06-25 14:30:10 -0700755static int
756tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
757 void *cb_priv, bool add, bool offload_in_use,
758 struct netlink_ext_ack *extack)
759{
760 struct tcf_chain *chain;
761 struct tcf_proto *tp;
762 int err;
763
764 list_for_each_entry(chain, &block->chain_list, list) {
765 for (tp = rtnl_dereference(chain->filter_chain); tp;
766 tp = rtnl_dereference(tp->next)) {
767 if (tp->ops->reoffload) {
768 err = tp->ops->reoffload(tp, add, cb, cb_priv,
769 extack);
770 if (err && add)
771 goto err_playback_remove;
772 } else if (add && offload_in_use) {
773 err = -EOPNOTSUPP;
774 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
775 goto err_playback_remove;
776 }
777 }
778 }
779
780 return 0;
781
782err_playback_remove:
783 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
784 extack);
785 return err;
786}
787
Jiri Pirkoacb67442017-10-19 15:50:31 +0200788struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
789 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700790 void *cb_priv,
791 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200792{
793 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700794 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200795
John Hurley32636742018-06-25 14:30:10 -0700796 /* Replay any already present rules */
797 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
798 tcf_block_offload_in_use(block),
799 extack);
800 if (err)
801 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100802
Jiri Pirkoacb67442017-10-19 15:50:31 +0200803 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
804 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100805 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200806 block_cb->cb = cb;
807 block_cb->cb_ident = cb_ident;
808 block_cb->cb_priv = cb_priv;
809 list_add(&block_cb->list, &block->cb_list);
810 return block_cb;
811}
812EXPORT_SYMBOL(__tcf_block_cb_register);
813
814int tcf_block_cb_register(struct tcf_block *block,
815 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700816 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200817{
818 struct tcf_block_cb *block_cb;
819
John Hurley60513bd2018-06-25 14:30:04 -0700820 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
821 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500822 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200823}
824EXPORT_SYMBOL(tcf_block_cb_register);
825
John Hurley32636742018-06-25 14:30:10 -0700826void __tcf_block_cb_unregister(struct tcf_block *block,
827 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200828{
John Hurley32636742018-06-25 14:30:10 -0700829 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
830 false, tcf_block_offload_in_use(block),
831 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200832 list_del(&block_cb->list);
833 kfree(block_cb);
834}
835EXPORT_SYMBOL(__tcf_block_cb_unregister);
836
837void tcf_block_cb_unregister(struct tcf_block *block,
838 tc_setup_cb_t *cb, void *cb_ident)
839{
840 struct tcf_block_cb *block_cb;
841
842 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
843 if (!block_cb)
844 return;
John Hurley32636742018-06-25 14:30:10 -0700845 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200846}
847EXPORT_SYMBOL(tcf_block_cb_unregister);
848
849static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
850 void *type_data, bool err_stop)
851{
852 struct tcf_block_cb *block_cb;
853 int ok_count = 0;
854 int err;
855
David S. Miller9a99dc12018-06-06 13:55:47 -0400856 /* Make sure all netdevs sharing this block are offload-capable. */
857 if (block->nooffloaddevcnt && err_stop)
858 return -EOPNOTSUPP;
859
Jiri Pirkoacb67442017-10-19 15:50:31 +0200860 list_for_each_entry(block_cb, &block->cb_list, list) {
861 err = block_cb->cb(type, type_data, block_cb->cb_priv);
862 if (err) {
863 if (err_stop)
864 return err;
865 } else {
866 ok_count++;
867 }
868 }
869 return ok_count;
870}
871
Jiri Pirko87d83092017-05-17 11:07:54 +0200872/* Main classifier routine: scans classifier chain attached
873 * to this qdisc, (optionally) tests for protocol and asks
874 * specific classifiers.
875 */
876int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
877 struct tcf_result *res, bool compat_mode)
878{
879 __be16 protocol = tc_skb_protocol(skb);
880#ifdef CONFIG_NET_CLS_ACT
881 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200882 const struct tcf_proto *orig_tp = tp;
883 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200884 int limit = 0;
885
886reclassify:
887#endif
888 for (; tp; tp = rcu_dereference_bh(tp->next)) {
889 int err;
890
891 if (tp->protocol != protocol &&
892 tp->protocol != htons(ETH_P_ALL))
893 continue;
894
895 err = tp->classify(skb, tp, res);
896#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200897 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200898 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200899 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200900 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200901 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200902 goto reset;
903 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200904#endif
905 if (err >= 0)
906 return err;
907 }
908
909 return TC_ACT_UNSPEC; /* signal: continue lookup */
910#ifdef CONFIG_NET_CLS_ACT
911reset:
912 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100913 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
914 tp->chain->block->index,
915 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200916 ntohs(tp->protocol));
917 return TC_ACT_SHOT;
918 }
919
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200920 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200921 protocol = tc_skb_protocol(skb);
922 goto reclassify;
923#endif
924}
925EXPORT_SYMBOL(tcf_classify);
926
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200927struct tcf_chain_info {
928 struct tcf_proto __rcu **pprev;
929 struct tcf_proto __rcu *next;
930};
931
932static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
933{
934 return rtnl_dereference(*chain_info->pprev);
935}
936
937static void tcf_chain_tp_insert(struct tcf_chain *chain,
938 struct tcf_chain_info *chain_info,
939 struct tcf_proto *tp)
940{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100941 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200942 tcf_chain0_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200943 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
944 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700945 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200946}
947
948static void tcf_chain_tp_remove(struct tcf_chain *chain,
949 struct tcf_chain_info *chain_info,
950 struct tcf_proto *tp)
951{
952 struct tcf_proto *next = rtnl_dereference(chain_info->next);
953
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100954 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca42018-07-23 09:23:05 +0200955 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200956 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700957 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200958}
959
960static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
961 struct tcf_chain_info *chain_info,
962 u32 protocol, u32 prio,
963 bool prio_allocate)
964{
965 struct tcf_proto **pprev;
966 struct tcf_proto *tp;
967
968 /* Check the chain for existence of proto-tcf with this priority */
969 for (pprev = &chain->filter_chain;
970 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
971 if (tp->prio >= prio) {
972 if (tp->prio == prio) {
973 if (prio_allocate ||
974 (tp->protocol != protocol && protocol))
975 return ERR_PTR(-EINVAL);
976 } else {
977 tp = NULL;
978 }
979 break;
980 }
981 }
982 chain_info->pprev = pprev;
983 chain_info->next = tp ? tp->next : NULL;
984 return tp;
985}
986
WANG Cong71203712017-08-07 15:26:50 -0700987static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100988 struct tcf_proto *tp, struct tcf_block *block,
989 struct Qdisc *q, u32 parent, void *fh,
990 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700991{
992 struct tcmsg *tcm;
993 struct nlmsghdr *nlh;
994 unsigned char *b = skb_tail_pointer(skb);
995
996 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
997 if (!nlh)
998 goto out_nlmsg_trim;
999 tcm = nlmsg_data(nlh);
1000 tcm->tcm_family = AF_UNSPEC;
1001 tcm->tcm__pad1 = 0;
1002 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001003 if (q) {
1004 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1005 tcm->tcm_parent = parent;
1006 } else {
1007 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1008 tcm->tcm_block_index = block->index;
1009 }
WANG Cong71203712017-08-07 15:26:50 -07001010 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1011 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1012 goto nla_put_failure;
1013 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1014 goto nla_put_failure;
1015 if (!fh) {
1016 tcm->tcm_handle = 0;
1017 } else {
1018 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1019 goto nla_put_failure;
1020 }
1021 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1022 return skb->len;
1023
1024out_nlmsg_trim:
1025nla_put_failure:
1026 nlmsg_trim(skb, b);
1027 return -1;
1028}
1029
1030static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1031 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001032 struct tcf_block *block, struct Qdisc *q,
1033 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001034{
1035 struct sk_buff *skb;
1036 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1037
1038 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1039 if (!skb)
1040 return -ENOBUFS;
1041
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001042 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1043 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001044 kfree_skb(skb);
1045 return -EINVAL;
1046 }
1047
1048 if (unicast)
1049 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1050
1051 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1052 n->nlmsg_flags & NLM_F_ECHO);
1053}
1054
1055static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1056 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001057 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001058 u32 parent, void *fh, bool unicast, bool *last,
1059 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001060{
1061 struct sk_buff *skb;
1062 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1063 int err;
1064
1065 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1066 if (!skb)
1067 return -ENOBUFS;
1068
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001069 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1070 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001071 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001072 kfree_skb(skb);
1073 return -EINVAL;
1074 }
1075
Alexander Aring571acf22018-01-18 11:20:53 -05001076 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001077 if (err) {
1078 kfree_skb(skb);
1079 return err;
1080 }
1081
1082 if (unicast)
1083 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1084
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001085 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1086 n->nlmsg_flags & NLM_F_ECHO);
1087 if (err < 0)
1088 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1089 return err;
WANG Cong71203712017-08-07 15:26:50 -07001090}
1091
1092static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001093 struct tcf_block *block, struct Qdisc *q,
1094 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001095 struct tcf_chain *chain, int event)
1096{
1097 struct tcf_proto *tp;
1098
1099 for (tp = rtnl_dereference(chain->filter_chain);
1100 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001101 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001102 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001103}
1104
Vlad Buslovc431f892018-05-31 09:52:53 +03001105static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001106 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001108 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001109 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct tcmsg *t;
1111 u32 protocol;
1112 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001113 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001115 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001116 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001117 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001118 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001119 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001122 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001124 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Vlad Buslovc431f892018-05-31 09:52:53 +03001126 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001127 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001130 tp_created = 0;
1131
David Ahernc21ef3e2017-04-16 09:48:24 -07001132 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001133 if (err < 0)
1134 return err;
1135
David S. Miller942b8162012-06-26 21:48:50 -07001136 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 protocol = TC_H_MIN(t->tcm_info);
1138 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001139 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 parent = t->tcm_parent;
1141 cl = 0;
1142
1143 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001144 /* If no priority is provided by the user,
1145 * we allocate one.
1146 */
1147 if (n->nlmsg_flags & NLM_F_CREATE) {
1148 prio = TC_H_MAKE(0x80000000U, 0U);
1149 prio_allocate = true;
1150 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001151 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155
1156 /* Find head of filter chain. */
1157
Vlad Buslovc431f892018-05-31 09:52:53 +03001158 block = tcf_block_find(net, &q, &parent, &cl,
1159 t->tcm_ifindex, t->tcm_block_index, extack);
1160 if (IS_ERR(block)) {
1161 err = PTR_ERR(block);
1162 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001163 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001164
1165 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1166 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001167 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001168 err = -EINVAL;
1169 goto errout;
1170 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001171 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001172 if (!chain) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001173 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001174 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001175 goto errout;
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001178 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1179 prio, prio_allocate);
1180 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001181 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001182 err = PTR_ERR(tp);
1183 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185
1186 if (tp == NULL) {
1187 /* Proto-tcf does not exist, create new one */
1188
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001189 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001190 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001191 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Vlad Buslovc431f892018-05-31 09:52:53 +03001195 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001196 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 +01001197 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001201 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001202 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Jiri Pirko33a48922017-02-09 14:38:57 +01001204 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001205 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001206 if (IS_ERR(tp)) {
1207 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 goto errout;
1209 }
Minoru Usui12186be2009-06-02 02:17:34 -07001210 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001211 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001212 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001213 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 fh = tp->ops->get(tp, t->tcm_handle);
1218
WANG Cong8113c092017-08-04 21:31:43 -07001219 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001220 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001221 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 +01001222 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001224 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001225 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1226 NL_SET_ERR_MSG(extack, "Filter already exists");
1227 err = -EEXIST;
1228 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001231 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001232 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1233 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001234 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001235 if (tp_created)
1236 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001237 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001238 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001239 } else {
1240 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001241 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001245 if (chain)
1246 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (err == -EAGAIN)
1248 /* Replay the request. */
1249 goto replay;
1250 return err;
1251}
1252
Vlad Buslovc431f892018-05-31 09:52:53 +03001253static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1254 struct netlink_ext_ack *extack)
1255{
1256 struct net *net = sock_net(skb->sk);
1257 struct nlattr *tca[TCA_MAX + 1];
1258 struct tcmsg *t;
1259 u32 protocol;
1260 u32 prio;
1261 u32 parent;
1262 u32 chain_index;
1263 struct Qdisc *q = NULL;
1264 struct tcf_chain_info chain_info;
1265 struct tcf_chain *chain = NULL;
1266 struct tcf_block *block;
1267 struct tcf_proto *tp = NULL;
1268 unsigned long cl = 0;
1269 void *fh = NULL;
1270 int err;
1271
1272 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1273 return -EPERM;
1274
1275 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1276 if (err < 0)
1277 return err;
1278
1279 t = nlmsg_data(n);
1280 protocol = TC_H_MIN(t->tcm_info);
1281 prio = TC_H_MAJ(t->tcm_info);
1282 parent = t->tcm_parent;
1283
1284 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1285 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1286 return -ENOENT;
1287 }
1288
1289 /* Find head of filter chain. */
1290
1291 block = tcf_block_find(net, &q, &parent, &cl,
1292 t->tcm_ifindex, t->tcm_block_index, extack);
1293 if (IS_ERR(block)) {
1294 err = PTR_ERR(block);
1295 goto errout;
1296 }
1297
1298 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1299 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1300 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1301 err = -EINVAL;
1302 goto errout;
1303 }
1304 chain = tcf_chain_get(block, chain_index, false);
1305 if (!chain) {
1306 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1307 err = -EINVAL;
1308 goto errout;
1309 }
1310
1311 if (prio == 0) {
1312 tfilter_notify_chain(net, skb, block, q, parent, n,
1313 chain, RTM_DELTFILTER);
1314 tcf_chain_flush(chain);
1315 err = 0;
1316 goto errout;
1317 }
1318
1319 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1320 prio, false);
1321 if (!tp || IS_ERR(tp)) {
1322 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001323 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001324 goto errout;
1325 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1326 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1327 err = -EINVAL;
1328 goto errout;
1329 }
1330
1331 fh = tp->ops->get(tp, t->tcm_handle);
1332
1333 if (!fh) {
1334 if (t->tcm_handle == 0) {
1335 tcf_chain_tp_remove(chain, &chain_info, tp);
1336 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1337 RTM_DELTFILTER, false);
1338 tcf_proto_destroy(tp, extack);
1339 err = 0;
1340 } else {
1341 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1342 err = -ENOENT;
1343 }
1344 } else {
1345 bool last;
1346
1347 err = tfilter_del_notify(net, skb, n, tp, block,
1348 q, parent, fh, false, &last,
1349 extack);
1350 if (err)
1351 goto errout;
1352 if (last) {
1353 tcf_chain_tp_remove(chain, &chain_info, tp);
1354 tcf_proto_destroy(tp, extack);
1355 }
1356 }
1357
1358errout:
1359 if (chain)
1360 tcf_chain_put(chain);
1361 return err;
1362}
1363
1364static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1365 struct netlink_ext_ack *extack)
1366{
1367 struct net *net = sock_net(skb->sk);
1368 struct nlattr *tca[TCA_MAX + 1];
1369 struct tcmsg *t;
1370 u32 protocol;
1371 u32 prio;
1372 u32 parent;
1373 u32 chain_index;
1374 struct Qdisc *q = NULL;
1375 struct tcf_chain_info chain_info;
1376 struct tcf_chain *chain = NULL;
1377 struct tcf_block *block;
1378 struct tcf_proto *tp = NULL;
1379 unsigned long cl = 0;
1380 void *fh = NULL;
1381 int err;
1382
1383 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1384 if (err < 0)
1385 return err;
1386
1387 t = nlmsg_data(n);
1388 protocol = TC_H_MIN(t->tcm_info);
1389 prio = TC_H_MAJ(t->tcm_info);
1390 parent = t->tcm_parent;
1391
1392 if (prio == 0) {
1393 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1394 return -ENOENT;
1395 }
1396
1397 /* Find head of filter chain. */
1398
1399 block = tcf_block_find(net, &q, &parent, &cl,
1400 t->tcm_ifindex, t->tcm_block_index, extack);
1401 if (IS_ERR(block)) {
1402 err = PTR_ERR(block);
1403 goto errout;
1404 }
1405
1406 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1407 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1408 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1409 err = -EINVAL;
1410 goto errout;
1411 }
1412 chain = tcf_chain_get(block, chain_index, false);
1413 if (!chain) {
1414 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1415 err = -EINVAL;
1416 goto errout;
1417 }
1418
1419 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1420 prio, false);
1421 if (!tp || IS_ERR(tp)) {
1422 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001423 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001424 goto errout;
1425 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1426 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1427 err = -EINVAL;
1428 goto errout;
1429 }
1430
1431 fh = tp->ops->get(tp, t->tcm_handle);
1432
1433 if (!fh) {
1434 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1435 err = -ENOENT;
1436 } else {
1437 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1438 fh, RTM_NEWTFILTER, true);
1439 if (err < 0)
1440 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1441 }
1442
1443errout:
1444 if (chain)
1445 tcf_chain_put(chain);
1446 return err;
1447}
1448
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001449struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 struct tcf_walker w;
1451 struct sk_buff *skb;
1452 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001453 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001454 struct Qdisc *q;
1455 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456};
1457
WANG Cong8113c092017-08-04 21:31:43 -07001458static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001460 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001461 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001463 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001464 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001465 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1466 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001469static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1470 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001471 long index_start, long *p_index)
1472{
1473 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001474 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001475 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1476 struct tcf_dump_args arg;
1477 struct tcf_proto *tp;
1478
1479 for (tp = rtnl_dereference(chain->filter_chain);
1480 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1481 if (*p_index < index_start)
1482 continue;
1483 if (TC_H_MAJ(tcm->tcm_info) &&
1484 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1485 continue;
1486 if (TC_H_MIN(tcm->tcm_info) &&
1487 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1488 continue;
1489 if (*p_index > index_start)
1490 memset(&cb->args[1], 0,
1491 sizeof(cb->args) - sizeof(cb->args[0]));
1492 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001493 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001494 NETLINK_CB(cb->skb).portid,
1495 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1496 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001497 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001498
1499 cb->args[1] = 1;
1500 }
1501 if (!tp->ops->walk)
1502 continue;
1503 arg.w.fn = tcf_node_dump;
1504 arg.skb = skb;
1505 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001506 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001507 arg.q = q;
1508 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001509 arg.w.stop = 0;
1510 arg.w.skip = cb->args[1] - 1;
1511 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001512 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001513 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001514 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001515 cb->args[1] = arg.w.count + 1;
1516 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001517 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001518 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001519 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001520}
1521
Eric Dumazetbd27a872009-11-05 20:57:26 -08001522/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1524{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001525 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001526 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001527 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001528 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001529 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001530 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001531 long index_start;
1532 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001533 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001534 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Hong zhi guo573ce262013-03-27 06:47:04 +00001536 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001538
1539 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1540 if (err)
1541 return err;
1542
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001543 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1544 block = tcf_block_lookup(net, tcm->tcm_block_index);
1545 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001546 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001547 /* If we work with block index, q is NULL and parent value
1548 * will never be used in the following code. The check
1549 * in tcf_fill_node prevents it. However, compiler does not
1550 * see that far, so set parent to zero to silence the warning
1551 * about parent being uninitialized.
1552 */
1553 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001554 } else {
1555 const struct Qdisc_class_ops *cops;
1556 struct net_device *dev;
1557 unsigned long cl = 0;
1558
1559 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1560 if (!dev)
1561 return skb->len;
1562
1563 parent = tcm->tcm_parent;
1564 if (!parent) {
1565 q = dev->qdisc;
1566 parent = q->handle;
1567 } else {
1568 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1569 }
1570 if (!q)
1571 goto out;
1572 cops = q->ops->cl_ops;
1573 if (!cops)
1574 goto out;
1575 if (!cops->tcf_block)
1576 goto out;
1577 if (TC_H_MIN(tcm->tcm_parent)) {
1578 cl = cops->find(q, tcm->tcm_parent);
1579 if (cl == 0)
1580 goto out;
1581 }
1582 block = cops->tcf_block(q, cl, NULL);
1583 if (!block)
1584 goto out;
1585 if (tcf_block_shared(block))
1586 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001589 index_start = cb->args[0];
1590 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001591
1592 list_for_each_entry(chain, &block->chain_list, list) {
1593 if (tca[TCA_CHAIN] &&
1594 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1595 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001596 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001597 index_start, &index)) {
1598 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001599 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001600 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001601 }
1602
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001603 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001606 /* If we did no progress, the error (EMSGSIZE) is real */
1607 if (skb->len == 0 && err)
1608 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return skb->len;
1610}
1611
WANG Cong18d02642014-09-25 10:26:37 -07001612void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614#ifdef CONFIG_NET_CLS_ACT
Vlad Buslov90b73b72018-07-05 17:24:33 +03001615 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
WANG Cong22dc13c2016-08-13 22:35:00 -07001616 kfree(exts->actions);
1617 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618#endif
1619}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001620EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00001622int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05001623 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
1624 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626#ifdef CONFIG_NET_CLS_ACT
1627 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001629 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
WANG Cong5da57f42013-12-15 20:15:07 -08001631 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001632 act = tcf_action_init_1(net, tp, tb[exts->police],
1633 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03001634 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001635 if (IS_ERR(act))
1636 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
WANG Cong33be6272013-12-15 20:15:05 -08001638 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07001639 exts->actions[0] = act;
1640 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08001641 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03001642 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07001643
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001644 err = tcf_action_init(net, tp, tb[exts->action],
1645 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03001646 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03001647 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001648 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001649 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001650 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
Cong Wange4b95c42017-11-06 13:47:19 -08001652 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654#else
WANG Cong5da57f42013-12-15 20:15:07 -08001655 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05001656 (exts->police && tb[exts->police])) {
1657 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05001659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660#endif
1661
1662 return 0;
1663}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001664EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001666void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001669 struct tcf_exts old = *dst;
1670
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001671 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07001672 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673#endif
1674}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001675EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
WANG Cong22dc13c2016-08-13 22:35:00 -07001677#ifdef CONFIG_NET_CLS_ACT
1678static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1679{
1680 if (exts->nr_actions == 0)
1681 return NULL;
1682 else
1683 return exts->actions[0];
1684}
1685#endif
WANG Cong33be6272013-12-15 20:15:05 -08001686
WANG Cong5da57f42013-12-15 20:15:07 -08001687int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07001690 struct nlattr *nest;
1691
Jiri Pirko978dfd82017-08-04 14:29:03 +02001692 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 /*
1694 * again for backward compatible mode - we want
1695 * to work with both old and new modes of entering
1696 * tc data even if iproute2 was newer - jhs
1697 */
WANG Cong33be6272013-12-15 20:15:05 -08001698 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08001699 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001700 if (nest == NULL)
1701 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07001702
Vlad Buslov90b73b72018-07-05 17:24:33 +03001703 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001704 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001705 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08001706 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08001707 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08001708 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001709 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001710 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08001711 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001712 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001713 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001717
1718nla_put_failure:
1719 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001721#else
1722 return 0;
1723#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001725EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001727
WANG Cong5da57f42013-12-15 20:15:07 -08001728int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001731 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001732 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001733 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734#endif
1735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001737EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Jiri Pirko717503b2017-10-11 09:41:09 +02001739static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1740 enum tc_setup_type type,
1741 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001742{
1743 int ok_count = 0;
1744#ifdef CONFIG_NET_CLS_ACT
1745 const struct tc_action *a;
1746 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001747 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001748
1749 if (!tcf_exts_has_actions(exts))
1750 return 0;
1751
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001752 for (i = 0; i < exts->nr_actions; i++) {
1753 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001754 if (!a->ops->get_dev)
1755 continue;
1756 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01001757 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001758 continue;
1759 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1760 if (ret < 0)
1761 return ret;
1762 ok_count += ret;
1763 }
1764#endif
1765 return ok_count;
1766}
Jiri Pirko717503b2017-10-11 09:41:09 +02001767
Jiri Pirko208c0f42017-10-19 15:50:32 +02001768int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1769 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02001770{
David S. Miller9a99dc12018-06-06 13:55:47 -04001771 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001772 int ret;
1773
David S. Miller9a99dc12018-06-06 13:55:47 -04001774 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1775 if (ret < 0)
1776 return ret;
1777 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001778
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03001779 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04001780 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001781 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1782 if (ret < 0)
1783 return ret;
1784 ok_count += ret;
1785
1786 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02001787}
1788EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001789
Jiri Pirko48617382018-01-17 11:46:46 +01001790static __net_init int tcf_net_init(struct net *net)
1791{
1792 struct tcf_net *tn = net_generic(net, tcf_net_id);
1793
1794 idr_init(&tn->idr);
1795 return 0;
1796}
1797
1798static void __net_exit tcf_net_exit(struct net *net)
1799{
1800 struct tcf_net *tn = net_generic(net, tcf_net_id);
1801
1802 idr_destroy(&tn->idr);
1803}
1804
1805static struct pernet_operations tcf_net_ops = {
1806 .init = tcf_net_init,
1807 .exit = tcf_net_exit,
1808 .id = &tcf_net_id,
1809 .size = sizeof(struct tcf_net),
1810};
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812static int __init tc_filter_init(void)
1813{
Jiri Pirko48617382018-01-17 11:46:46 +01001814 int err;
1815
Cong Wang7aa00452017-10-26 18:24:28 -07001816 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1817 if (!tc_filter_wq)
1818 return -ENOMEM;
1819
Jiri Pirko48617382018-01-17 11:46:46 +01001820 err = register_pernet_subsys(&tcf_net_ops);
1821 if (err)
1822 goto err_register_pernet_subsys;
1823
Vlad Buslovc431f892018-05-31 09:52:53 +03001824 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
1825 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
1826 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001827 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01001830
1831err_register_pernet_subsys:
1832 destroy_workqueue(tc_filter_wq);
1833 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
1836subsys_initcall(tc_filter_init);