blob: 1e8d69790d820f1ed56459ae20bf850e2e14ac29 [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;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100207 INIT_LIST_HEAD(&chain->filter_chain_list);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200208 list_add_tail(&chain->list, &block->chain_list);
209 chain->block = block;
210 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700211 chain->refcnt = 1;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200212 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200213}
214
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100215static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
216 struct tcf_proto *tp_head)
217{
218 if (item->chain_head_change)
219 item->chain_head_change(tp_head, item->chain_head_change_priv);
220}
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100221static void tcf_chain_head_change(struct tcf_chain *chain,
222 struct tcf_proto *tp_head)
223{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100224 struct tcf_filter_chain_list_item *item;
225
226 list_for_each_entry(item, &chain->filter_chain_list, list)
227 tcf_chain_head_change_item(item, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100228}
229
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200230static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100231{
Roman Kapld7aa04a2017-11-20 22:21:13 +0100232 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100233
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100234 tcf_chain_head_change(chain, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100235 while (tp) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200236 RCU_INIT_POINTER(chain->filter_chain, tp->next);
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800237 tcf_proto_destroy(tp, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100238 tp = rtnl_dereference(chain->filter_chain);
239 tcf_chain_put(chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100240 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200241}
242
243static void tcf_chain_destroy(struct tcf_chain *chain)
244{
Cong Wangefbf7892017-12-04 10:48:18 -0800245 struct tcf_block *block = chain->block;
246
Cong Wange2ef7542017-09-11 16:33:31 -0700247 list_del(&chain->list);
248 kfree(chain);
Cong Wangefbf7892017-12-04 10:48:18 -0800249 if (list_empty(&block->chain_list))
250 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700251}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200252
Cong Wange2ef7542017-09-11 16:33:31 -0700253static void tcf_chain_hold(struct tcf_chain *chain)
254{
255 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200256}
257
WANG Cong367a8ce2017-05-23 09:42:37 -0700258struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
259 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200260{
261 struct tcf_chain *chain;
262
263 list_for_each_entry(chain, &block->chain_list, list) {
Cong Wange2ef7542017-09-11 16:33:31 -0700264 if (chain->index == chain_index) {
265 tcf_chain_hold(chain);
266 return chain;
267 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200268 }
Jiri Pirko80532382017-09-06 13:14:19 +0200269
Cong Wange2ef7542017-09-11 16:33:31 -0700270 return create ? tcf_chain_create(block, chain_index) : NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200271}
272EXPORT_SYMBOL(tcf_chain_get);
273
274void tcf_chain_put(struct tcf_chain *chain)
275{
Cong Wange2ef7542017-09-11 16:33:31 -0700276 if (--chain->refcnt == 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200277 tcf_chain_destroy(chain);
278}
279EXPORT_SYMBOL(tcf_chain_put);
280
Jiri Pirkocaa72602018-01-17 11:46:50 +0100281static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200282{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100283 return block->offloadcnt;
284}
285
286static int tcf_block_offload_cmd(struct tcf_block *block,
287 struct net_device *dev,
288 struct tcf_block_ext_info *ei,
John Hurley60513bd2018-06-25 14:30:04 -0700289 enum tc_block_command command,
290 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100291{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200292 struct tc_block_offload bo = {};
293
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200294 bo.command = command;
295 bo.binder_type = ei->binder_type;
296 bo.block = block;
John Hurley60513bd2018-06-25 14:30:04 -0700297 bo.extack = extack;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100298 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200299}
300
Jiri Pirkocaa72602018-01-17 11:46:50 +0100301static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700302 struct tcf_block_ext_info *ei,
303 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200304{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100305 struct net_device *dev = q->dev_queue->dev;
306 int err;
307
308 if (!dev->netdev_ops->ndo_setup_tc)
309 goto no_offload_dev_inc;
310
311 /* If tc offload feature is disabled and the block we try to bind
312 * to already has some offloaded filters, forbid to bind.
313 */
John Hurley60513bd2018-06-25 14:30:04 -0700314 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
315 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Jiri Pirkocaa72602018-01-17 11:46:50 +0100316 return -EOPNOTSUPP;
John Hurley60513bd2018-06-25 14:30:04 -0700317 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100318
John Hurley60513bd2018-06-25 14:30:04 -0700319 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100320 if (err == -EOPNOTSUPP)
321 goto no_offload_dev_inc;
322 return err;
323
324no_offload_dev_inc:
325 if (tcf_block_offload_in_use(block))
326 return -EOPNOTSUPP;
327 block->nooffloaddevcnt++;
328 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200329}
330
331static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
332 struct tcf_block_ext_info *ei)
333{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100334 struct net_device *dev = q->dev_queue->dev;
335 int err;
336
337 if (!dev->netdev_ops->ndo_setup_tc)
338 goto no_offload_dev_dec;
John Hurley60513bd2018-06-25 14:30:04 -0700339 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100340 if (err == -EOPNOTSUPP)
341 goto no_offload_dev_dec;
342 return;
343
344no_offload_dev_dec:
345 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200346}
347
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100348static int
349tcf_chain_head_change_cb_add(struct tcf_chain *chain,
350 struct tcf_block_ext_info *ei,
351 struct netlink_ext_ack *extack)
352{
353 struct tcf_filter_chain_list_item *item;
354
355 item = kmalloc(sizeof(*item), GFP_KERNEL);
356 if (!item) {
357 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
358 return -ENOMEM;
359 }
360 item->chain_head_change = ei->chain_head_change;
361 item->chain_head_change_priv = ei->chain_head_change_priv;
362 if (chain->filter_chain)
363 tcf_chain_head_change_item(item, chain->filter_chain);
364 list_add(&item->list, &chain->filter_chain_list);
365 return 0;
366}
367
368static void
369tcf_chain_head_change_cb_del(struct tcf_chain *chain,
370 struct tcf_block_ext_info *ei)
371{
372 struct tcf_filter_chain_list_item *item;
373
374 list_for_each_entry(item, &chain->filter_chain_list, list) {
375 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
376 (item->chain_head_change == ei->chain_head_change &&
377 item->chain_head_change_priv == ei->chain_head_change_priv)) {
378 tcf_chain_head_change_item(item, NULL);
379 list_del(&item->list);
380 kfree(item);
381 return;
382 }
383 }
384 WARN_ON(1);
385}
386
Jiri Pirko48617382018-01-17 11:46:46 +0100387struct tcf_net {
388 struct idr idr;
389};
390
391static unsigned int tcf_net_id;
392
393static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100394 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100395{
Jiri Pirko48617382018-01-17 11:46:46 +0100396 struct tcf_net *tn = net_generic(net, tcf_net_id);
Jiri Pirko48617382018-01-17 11:46:46 +0100397
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100398 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
399 GFP_KERNEL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100400}
401
Jiri Pirko48617382018-01-17 11:46:46 +0100402static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200403{
Jiri Pirko48617382018-01-17 11:46:46 +0100404 struct tcf_net *tn = net_generic(net, tcf_net_id);
405
Matthew Wilcox9c160942017-11-28 09:48:43 -0500406 idr_remove(&tn->idr, block->index);
Jiri Pirko48617382018-01-17 11:46:46 +0100407}
408
409static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100410 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100411 struct netlink_ext_ack *extack)
412{
413 struct tcf_block *block;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200414 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200415 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200416
Jiri Pirko48617382018-01-17 11:46:46 +0100417 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500418 if (!block) {
419 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100420 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500421 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200422 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200423 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100424 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200425
Jiri Pirko5bc17012017-05-17 11:08:01 +0200426 /* Create chain 0 by default, it has to be always present. */
427 chain = tcf_chain_create(block, 0);
428 if (!chain) {
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500429 NL_SET_ERR_MSG(extack, "Failed to create new tcf chain");
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200430 err = -ENOMEM;
431 goto err_chain_create;
432 }
Jiri Pirko48617382018-01-17 11:46:46 +0100433 block->refcnt = 1;
434 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100435 block->index = block_index;
436
437 /* Don't store q pointer for blocks which are shared */
438 if (!tcf_block_shared(block))
439 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100440 return block;
441
442err_chain_create:
443 kfree(block);
444 return ERR_PTR(err);
445}
446
447static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
448{
449 struct tcf_net *tn = net_generic(net, tcf_net_id);
450
Matthew Wilcox322d8842017-11-28 10:01:24 -0500451 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100452}
453
Vlad Buslovc431f892018-05-31 09:52:53 +0300454/* Find tcf block.
455 * Set q, parent, cl when appropriate.
456 */
457
458static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
459 u32 *parent, unsigned long *cl,
460 int ifindex, u32 block_index,
461 struct netlink_ext_ack *extack)
462{
463 struct tcf_block *block;
464
465 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
466 block = tcf_block_lookup(net, block_index);
467 if (!block) {
468 NL_SET_ERR_MSG(extack, "Block of given index was not found");
469 return ERR_PTR(-EINVAL);
470 }
471 } else {
472 const struct Qdisc_class_ops *cops;
473 struct net_device *dev;
474
475 /* Find link */
476 dev = __dev_get_by_index(net, ifindex);
477 if (!dev)
478 return ERR_PTR(-ENODEV);
479
480 /* Find qdisc */
481 if (!*parent) {
482 *q = dev->qdisc;
483 *parent = (*q)->handle;
484 } else {
485 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
486 if (!*q) {
487 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
488 return ERR_PTR(-EINVAL);
489 }
490 }
491
492 /* Is it classful? */
493 cops = (*q)->ops->cl_ops;
494 if (!cops) {
495 NL_SET_ERR_MSG(extack, "Qdisc not classful");
496 return ERR_PTR(-EINVAL);
497 }
498
499 if (!cops->tcf_block) {
500 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
501 return ERR_PTR(-EOPNOTSUPP);
502 }
503
504 /* Do we search for filter, attached to class? */
505 if (TC_H_MIN(*parent)) {
506 *cl = cops->find(*q, *parent);
507 if (*cl == 0) {
508 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
509 return ERR_PTR(-ENOENT);
510 }
511 }
512
513 /* And the last stroke */
514 block = cops->tcf_block(*q, *cl, extack);
515 if (!block)
516 return ERR_PTR(-EINVAL);
517 if (tcf_block_shared(block)) {
518 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
519 return ERR_PTR(-EOPNOTSUPP);
520 }
521 }
522
523 return block;
524}
525
Jiri Pirko48617382018-01-17 11:46:46 +0100526static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
527{
528 return list_first_entry(&block->chain_list, struct tcf_chain, list);
529}
530
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100531struct tcf_block_owner_item {
532 struct list_head list;
533 struct Qdisc *q;
534 enum tcf_block_binder_type binder_type;
535};
536
537static void
538tcf_block_owner_netif_keep_dst(struct tcf_block *block,
539 struct Qdisc *q,
540 enum tcf_block_binder_type binder_type)
541{
542 if (block->keep_dst &&
543 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
544 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
545 netif_keep_dst(qdisc_dev(q));
546}
547
548void tcf_block_netif_keep_dst(struct tcf_block *block)
549{
550 struct tcf_block_owner_item *item;
551
552 block->keep_dst = true;
553 list_for_each_entry(item, &block->owner_list, list)
554 tcf_block_owner_netif_keep_dst(block, item->q,
555 item->binder_type);
556}
557EXPORT_SYMBOL(tcf_block_netif_keep_dst);
558
559static int tcf_block_owner_add(struct tcf_block *block,
560 struct Qdisc *q,
561 enum tcf_block_binder_type binder_type)
562{
563 struct tcf_block_owner_item *item;
564
565 item = kmalloc(sizeof(*item), GFP_KERNEL);
566 if (!item)
567 return -ENOMEM;
568 item->q = q;
569 item->binder_type = binder_type;
570 list_add(&item->list, &block->owner_list);
571 return 0;
572}
573
574static void tcf_block_owner_del(struct tcf_block *block,
575 struct Qdisc *q,
576 enum tcf_block_binder_type binder_type)
577{
578 struct tcf_block_owner_item *item;
579
580 list_for_each_entry(item, &block->owner_list, list) {
581 if (item->q == q && item->binder_type == binder_type) {
582 list_del(&item->list);
583 kfree(item);
584 return;
585 }
586 }
587 WARN_ON(1);
588}
589
Jiri Pirko48617382018-01-17 11:46:46 +0100590int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
591 struct tcf_block_ext_info *ei,
592 struct netlink_ext_ack *extack)
593{
594 struct net *net = qdisc_net(q);
595 struct tcf_block *block = NULL;
596 bool created = false;
597 int err;
598
599 if (ei->block_index) {
600 /* block_index not 0 means the shared block is requested */
601 block = tcf_block_lookup(net, ei->block_index);
602 if (block)
603 block->refcnt++;
604 }
605
606 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100607 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100608 if (IS_ERR(block))
609 return PTR_ERR(block);
610 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100611 if (tcf_block_shared(block)) {
612 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100613 if (err)
614 goto err_block_insert;
615 }
616 }
617
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100618 err = tcf_block_owner_add(block, q, ei->binder_type);
619 if (err)
620 goto err_block_owner_add;
621
622 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
623
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100624 err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block),
625 ei, extack);
626 if (err)
627 goto err_chain_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100628
John Hurley60513bd2018-06-25 14:30:04 -0700629 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100630 if (err)
631 goto err_block_offload_bind;
632
Jiri Pirko6529eab2017-05-17 11:07:55 +0200633 *p_block = block;
634 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200635
Jiri Pirkocaa72602018-01-17 11:46:50 +0100636err_block_offload_bind:
637 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100638err_chain_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100639 tcf_block_owner_del(block, q, ei->binder_type);
640err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100641 if (created) {
642 if (tcf_block_shared(block))
643 tcf_block_remove(block, net);
644err_block_insert:
645 kfree(tcf_block_chain_zero(block));
646 kfree(block);
647 } else {
648 block->refcnt--;
649 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200650 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200651}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200652EXPORT_SYMBOL(tcf_block_get_ext);
653
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100654static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
655{
656 struct tcf_proto __rcu **p_filter_chain = priv;
657
658 rcu_assign_pointer(*p_filter_chain, tp_head);
659}
660
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200661int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500662 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
663 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200664{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100665 struct tcf_block_ext_info ei = {
666 .chain_head_change = tcf_chain_head_change_dflt,
667 .chain_head_change_priv = p_filter_chain,
668 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200669
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100670 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500671 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200672}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200673EXPORT_SYMBOL(tcf_block_get);
674
Cong Wang7aa00452017-10-26 18:24:28 -0700675/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100676 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700677 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100678void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900679 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700680{
Cong Wangefbf7892017-12-04 10:48:18 -0800681 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700682
David S. Millerc30abd52017-12-16 22:11:55 -0500683 if (!block)
684 return;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100685 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100686 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100687
Jiri Pirko48617382018-01-17 11:46:46 +0100688 if (--block->refcnt == 0) {
689 if (tcf_block_shared(block))
690 tcf_block_remove(block, block->net);
691
692 /* Hold a refcnt for all chains, so that they don't disappear
693 * while we are iterating.
694 */
695 list_for_each_entry(chain, &block->chain_list, list)
696 tcf_chain_hold(chain);
697
698 list_for_each_entry(chain, &block->chain_list, list)
699 tcf_chain_flush(chain);
700 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700701
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100702 tcf_block_offload_unbind(block, q, ei);
703
Jiri Pirko48617382018-01-17 11:46:46 +0100704 if (block->refcnt == 0) {
705 /* At this point, all the chains should have refcnt >= 1. */
706 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
707 tcf_chain_put(chain);
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100708
Jiri Pirko48617382018-01-17 11:46:46 +0100709 /* Finally, put chain 0 and allow block to be freed. */
710 tcf_chain_put(tcf_block_chain_zero(block));
711 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200712}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200713EXPORT_SYMBOL(tcf_block_put_ext);
714
715void tcf_block_put(struct tcf_block *block)
716{
717 struct tcf_block_ext_info ei = {0, };
718
Jiri Pirko4853f122017-12-21 13:13:59 +0100719 if (!block)
720 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100721 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200722}
David S. Millere1ea2f92017-10-30 14:10:01 +0900723
Jiri Pirko6529eab2017-05-17 11:07:55 +0200724EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100725
Jiri Pirkoacb67442017-10-19 15:50:31 +0200726struct tcf_block_cb {
727 struct list_head list;
728 tc_setup_cb_t *cb;
729 void *cb_ident;
730 void *cb_priv;
731 unsigned int refcnt;
732};
733
734void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
735{
736 return block_cb->cb_priv;
737}
738EXPORT_SYMBOL(tcf_block_cb_priv);
739
740struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
741 tc_setup_cb_t *cb, void *cb_ident)
742{ struct tcf_block_cb *block_cb;
743
744 list_for_each_entry(block_cb, &block->cb_list, list)
745 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
746 return block_cb;
747 return NULL;
748}
749EXPORT_SYMBOL(tcf_block_cb_lookup);
750
751void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
752{
753 block_cb->refcnt++;
754}
755EXPORT_SYMBOL(tcf_block_cb_incref);
756
757unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
758{
759 return --block_cb->refcnt;
760}
761EXPORT_SYMBOL(tcf_block_cb_decref);
762
John Hurley32636742018-06-25 14:30:10 -0700763static int
764tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
765 void *cb_priv, bool add, bool offload_in_use,
766 struct netlink_ext_ack *extack)
767{
768 struct tcf_chain *chain;
769 struct tcf_proto *tp;
770 int err;
771
772 list_for_each_entry(chain, &block->chain_list, list) {
773 for (tp = rtnl_dereference(chain->filter_chain); tp;
774 tp = rtnl_dereference(tp->next)) {
775 if (tp->ops->reoffload) {
776 err = tp->ops->reoffload(tp, add, cb, cb_priv,
777 extack);
778 if (err && add)
779 goto err_playback_remove;
780 } else if (add && offload_in_use) {
781 err = -EOPNOTSUPP;
782 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
783 goto err_playback_remove;
784 }
785 }
786 }
787
788 return 0;
789
790err_playback_remove:
791 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
792 extack);
793 return err;
794}
795
Jiri Pirkoacb67442017-10-19 15:50:31 +0200796struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
797 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700798 void *cb_priv,
799 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200800{
801 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700802 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200803
John Hurley32636742018-06-25 14:30:10 -0700804 /* Replay any already present rules */
805 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
806 tcf_block_offload_in_use(block),
807 extack);
808 if (err)
809 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100810
Jiri Pirkoacb67442017-10-19 15:50:31 +0200811 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
812 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100813 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200814 block_cb->cb = cb;
815 block_cb->cb_ident = cb_ident;
816 block_cb->cb_priv = cb_priv;
817 list_add(&block_cb->list, &block->cb_list);
818 return block_cb;
819}
820EXPORT_SYMBOL(__tcf_block_cb_register);
821
822int tcf_block_cb_register(struct tcf_block *block,
823 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700824 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200825{
826 struct tcf_block_cb *block_cb;
827
John Hurley60513bd2018-06-25 14:30:04 -0700828 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
829 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500830 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200831}
832EXPORT_SYMBOL(tcf_block_cb_register);
833
John Hurley32636742018-06-25 14:30:10 -0700834void __tcf_block_cb_unregister(struct tcf_block *block,
835 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200836{
John Hurley32636742018-06-25 14:30:10 -0700837 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
838 false, tcf_block_offload_in_use(block),
839 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200840 list_del(&block_cb->list);
841 kfree(block_cb);
842}
843EXPORT_SYMBOL(__tcf_block_cb_unregister);
844
845void tcf_block_cb_unregister(struct tcf_block *block,
846 tc_setup_cb_t *cb, void *cb_ident)
847{
848 struct tcf_block_cb *block_cb;
849
850 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
851 if (!block_cb)
852 return;
John Hurley32636742018-06-25 14:30:10 -0700853 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200854}
855EXPORT_SYMBOL(tcf_block_cb_unregister);
856
857static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
858 void *type_data, bool err_stop)
859{
860 struct tcf_block_cb *block_cb;
861 int ok_count = 0;
862 int err;
863
David S. Miller9a99dc12018-06-06 13:55:47 -0400864 /* Make sure all netdevs sharing this block are offload-capable. */
865 if (block->nooffloaddevcnt && err_stop)
866 return -EOPNOTSUPP;
867
Jiri Pirkoacb67442017-10-19 15:50:31 +0200868 list_for_each_entry(block_cb, &block->cb_list, list) {
869 err = block_cb->cb(type, type_data, block_cb->cb_priv);
870 if (err) {
871 if (err_stop)
872 return err;
873 } else {
874 ok_count++;
875 }
876 }
877 return ok_count;
878}
879
Jiri Pirko87d83092017-05-17 11:07:54 +0200880/* Main classifier routine: scans classifier chain attached
881 * to this qdisc, (optionally) tests for protocol and asks
882 * specific classifiers.
883 */
884int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
885 struct tcf_result *res, bool compat_mode)
886{
887 __be16 protocol = tc_skb_protocol(skb);
888#ifdef CONFIG_NET_CLS_ACT
889 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200890 const struct tcf_proto *orig_tp = tp;
891 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200892 int limit = 0;
893
894reclassify:
895#endif
896 for (; tp; tp = rcu_dereference_bh(tp->next)) {
897 int err;
898
899 if (tp->protocol != protocol &&
900 tp->protocol != htons(ETH_P_ALL))
901 continue;
902
903 err = tp->classify(skb, tp, res);
904#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200905 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200906 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200907 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200908 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200909 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200910 goto reset;
911 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200912#endif
913 if (err >= 0)
914 return err;
915 }
916
917 return TC_ACT_UNSPEC; /* signal: continue lookup */
918#ifdef CONFIG_NET_CLS_ACT
919reset:
920 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100921 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
922 tp->chain->block->index,
923 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200924 ntohs(tp->protocol));
925 return TC_ACT_SHOT;
926 }
927
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200928 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200929 protocol = tc_skb_protocol(skb);
930 goto reclassify;
931#endif
932}
933EXPORT_SYMBOL(tcf_classify);
934
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200935struct tcf_chain_info {
936 struct tcf_proto __rcu **pprev;
937 struct tcf_proto __rcu *next;
938};
939
940static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
941{
942 return rtnl_dereference(*chain_info->pprev);
943}
944
945static void tcf_chain_tp_insert(struct tcf_chain *chain,
946 struct tcf_chain_info *chain_info,
947 struct tcf_proto *tp)
948{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100949 if (*chain_info->pprev == chain->filter_chain)
950 tcf_chain_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200951 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
952 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700953 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200954}
955
956static void tcf_chain_tp_remove(struct tcf_chain *chain,
957 struct tcf_chain_info *chain_info,
958 struct tcf_proto *tp)
959{
960 struct tcf_proto *next = rtnl_dereference(chain_info->next);
961
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100962 if (tp == chain->filter_chain)
963 tcf_chain_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200964 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700965 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200966}
967
968static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
969 struct tcf_chain_info *chain_info,
970 u32 protocol, u32 prio,
971 bool prio_allocate)
972{
973 struct tcf_proto **pprev;
974 struct tcf_proto *tp;
975
976 /* Check the chain for existence of proto-tcf with this priority */
977 for (pprev = &chain->filter_chain;
978 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
979 if (tp->prio >= prio) {
980 if (tp->prio == prio) {
981 if (prio_allocate ||
982 (tp->protocol != protocol && protocol))
983 return ERR_PTR(-EINVAL);
984 } else {
985 tp = NULL;
986 }
987 break;
988 }
989 }
990 chain_info->pprev = pprev;
991 chain_info->next = tp ? tp->next : NULL;
992 return tp;
993}
994
WANG Cong71203712017-08-07 15:26:50 -0700995static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100996 struct tcf_proto *tp, struct tcf_block *block,
997 struct Qdisc *q, u32 parent, void *fh,
998 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700999{
1000 struct tcmsg *tcm;
1001 struct nlmsghdr *nlh;
1002 unsigned char *b = skb_tail_pointer(skb);
1003
1004 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1005 if (!nlh)
1006 goto out_nlmsg_trim;
1007 tcm = nlmsg_data(nlh);
1008 tcm->tcm_family = AF_UNSPEC;
1009 tcm->tcm__pad1 = 0;
1010 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001011 if (q) {
1012 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1013 tcm->tcm_parent = parent;
1014 } else {
1015 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1016 tcm->tcm_block_index = block->index;
1017 }
WANG Cong71203712017-08-07 15:26:50 -07001018 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1019 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1020 goto nla_put_failure;
1021 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1022 goto nla_put_failure;
1023 if (!fh) {
1024 tcm->tcm_handle = 0;
1025 } else {
1026 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1027 goto nla_put_failure;
1028 }
1029 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1030 return skb->len;
1031
1032out_nlmsg_trim:
1033nla_put_failure:
1034 nlmsg_trim(skb, b);
1035 return -1;
1036}
1037
1038static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1039 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001040 struct tcf_block *block, struct Qdisc *q,
1041 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001042{
1043 struct sk_buff *skb;
1044 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1045
1046 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1047 if (!skb)
1048 return -ENOBUFS;
1049
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001050 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1051 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001052 kfree_skb(skb);
1053 return -EINVAL;
1054 }
1055
1056 if (unicast)
1057 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1058
1059 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1060 n->nlmsg_flags & NLM_F_ECHO);
1061}
1062
1063static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1064 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001065 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001066 u32 parent, void *fh, bool unicast, bool *last,
1067 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001068{
1069 struct sk_buff *skb;
1070 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1071 int err;
1072
1073 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1074 if (!skb)
1075 return -ENOBUFS;
1076
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001077 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1078 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001079 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001080 kfree_skb(skb);
1081 return -EINVAL;
1082 }
1083
Alexander Aring571acf22018-01-18 11:20:53 -05001084 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001085 if (err) {
1086 kfree_skb(skb);
1087 return err;
1088 }
1089
1090 if (unicast)
1091 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1092
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001093 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1094 n->nlmsg_flags & NLM_F_ECHO);
1095 if (err < 0)
1096 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1097 return err;
WANG Cong71203712017-08-07 15:26:50 -07001098}
1099
1100static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001101 struct tcf_block *block, struct Qdisc *q,
1102 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001103 struct tcf_chain *chain, int event)
1104{
1105 struct tcf_proto *tp;
1106
1107 for (tp = rtnl_dereference(chain->filter_chain);
1108 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001109 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001110 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001111}
1112
Vlad Buslovc431f892018-05-31 09:52:53 +03001113static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001114 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001116 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001117 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct tcmsg *t;
1119 u32 protocol;
1120 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001121 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001123 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001124 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001125 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001126 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001127 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001130 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001132 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Vlad Buslovc431f892018-05-31 09:52:53 +03001134 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001135 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001138 tp_created = 0;
1139
David Ahernc21ef3e2017-04-16 09:48:24 -07001140 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001141 if (err < 0)
1142 return err;
1143
David S. Miller942b8162012-06-26 21:48:50 -07001144 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 protocol = TC_H_MIN(t->tcm_info);
1146 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001147 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 parent = t->tcm_parent;
1149 cl = 0;
1150
1151 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001152 /* If no priority is provided by the user,
1153 * we allocate one.
1154 */
1155 if (n->nlmsg_flags & NLM_F_CREATE) {
1156 prio = TC_H_MAKE(0x80000000U, 0U);
1157 prio_allocate = true;
1158 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001159 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163
1164 /* Find head of filter chain. */
1165
Vlad Buslovc431f892018-05-31 09:52:53 +03001166 block = tcf_block_find(net, &q, &parent, &cl,
1167 t->tcm_ifindex, t->tcm_block_index, extack);
1168 if (IS_ERR(block)) {
1169 err = PTR_ERR(block);
1170 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001171 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001172
1173 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1174 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001175 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001176 err = -EINVAL;
1177 goto errout;
1178 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001179 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001180 if (!chain) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001181 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001182 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001183 goto errout;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001186 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1187 prio, prio_allocate);
1188 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001189 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001190 err = PTR_ERR(tp);
1191 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
1194 if (tp == NULL) {
1195 /* Proto-tcf does not exist, create new one */
1196
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001197 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001198 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001199 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Vlad Buslovc431f892018-05-31 09:52:53 +03001203 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001204 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 +01001205 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001209 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001210 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Jiri Pirko33a48922017-02-09 14:38:57 +01001212 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001213 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001214 if (IS_ERR(tp)) {
1215 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto errout;
1217 }
Minoru Usui12186be2009-06-02 02:17:34 -07001218 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001219 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001220 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001221 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 fh = tp->ops->get(tp, t->tcm_handle);
1226
WANG Cong8113c092017-08-04 21:31:43 -07001227 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001228 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001229 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 +01001230 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001232 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001233 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1234 NL_SET_ERR_MSG(extack, "Filter already exists");
1235 err = -EEXIST;
1236 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001239 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001240 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1241 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001242 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001243 if (tp_created)
1244 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001245 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001246 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001247 } else {
1248 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001249 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001253 if (chain)
1254 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (err == -EAGAIN)
1256 /* Replay the request. */
1257 goto replay;
1258 return err;
1259}
1260
Vlad Buslovc431f892018-05-31 09:52:53 +03001261static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1262 struct netlink_ext_ack *extack)
1263{
1264 struct net *net = sock_net(skb->sk);
1265 struct nlattr *tca[TCA_MAX + 1];
1266 struct tcmsg *t;
1267 u32 protocol;
1268 u32 prio;
1269 u32 parent;
1270 u32 chain_index;
1271 struct Qdisc *q = NULL;
1272 struct tcf_chain_info chain_info;
1273 struct tcf_chain *chain = NULL;
1274 struct tcf_block *block;
1275 struct tcf_proto *tp = NULL;
1276 unsigned long cl = 0;
1277 void *fh = NULL;
1278 int err;
1279
1280 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1281 return -EPERM;
1282
1283 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1284 if (err < 0)
1285 return err;
1286
1287 t = nlmsg_data(n);
1288 protocol = TC_H_MIN(t->tcm_info);
1289 prio = TC_H_MAJ(t->tcm_info);
1290 parent = t->tcm_parent;
1291
1292 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1293 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1294 return -ENOENT;
1295 }
1296
1297 /* Find head of filter chain. */
1298
1299 block = tcf_block_find(net, &q, &parent, &cl,
1300 t->tcm_ifindex, t->tcm_block_index, extack);
1301 if (IS_ERR(block)) {
1302 err = PTR_ERR(block);
1303 goto errout;
1304 }
1305
1306 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1307 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1308 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1309 err = -EINVAL;
1310 goto errout;
1311 }
1312 chain = tcf_chain_get(block, chain_index, false);
1313 if (!chain) {
1314 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1315 err = -EINVAL;
1316 goto errout;
1317 }
1318
1319 if (prio == 0) {
1320 tfilter_notify_chain(net, skb, block, q, parent, n,
1321 chain, RTM_DELTFILTER);
1322 tcf_chain_flush(chain);
1323 err = 0;
1324 goto errout;
1325 }
1326
1327 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1328 prio, false);
1329 if (!tp || IS_ERR(tp)) {
1330 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001331 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001332 goto errout;
1333 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1334 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1335 err = -EINVAL;
1336 goto errout;
1337 }
1338
1339 fh = tp->ops->get(tp, t->tcm_handle);
1340
1341 if (!fh) {
1342 if (t->tcm_handle == 0) {
1343 tcf_chain_tp_remove(chain, &chain_info, tp);
1344 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1345 RTM_DELTFILTER, false);
1346 tcf_proto_destroy(tp, extack);
1347 err = 0;
1348 } else {
1349 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1350 err = -ENOENT;
1351 }
1352 } else {
1353 bool last;
1354
1355 err = tfilter_del_notify(net, skb, n, tp, block,
1356 q, parent, fh, false, &last,
1357 extack);
1358 if (err)
1359 goto errout;
1360 if (last) {
1361 tcf_chain_tp_remove(chain, &chain_info, tp);
1362 tcf_proto_destroy(tp, extack);
1363 }
1364 }
1365
1366errout:
1367 if (chain)
1368 tcf_chain_put(chain);
1369 return err;
1370}
1371
1372static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1373 struct netlink_ext_ack *extack)
1374{
1375 struct net *net = sock_net(skb->sk);
1376 struct nlattr *tca[TCA_MAX + 1];
1377 struct tcmsg *t;
1378 u32 protocol;
1379 u32 prio;
1380 u32 parent;
1381 u32 chain_index;
1382 struct Qdisc *q = NULL;
1383 struct tcf_chain_info chain_info;
1384 struct tcf_chain *chain = NULL;
1385 struct tcf_block *block;
1386 struct tcf_proto *tp = NULL;
1387 unsigned long cl = 0;
1388 void *fh = NULL;
1389 int err;
1390
1391 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1392 if (err < 0)
1393 return err;
1394
1395 t = nlmsg_data(n);
1396 protocol = TC_H_MIN(t->tcm_info);
1397 prio = TC_H_MAJ(t->tcm_info);
1398 parent = t->tcm_parent;
1399
1400 if (prio == 0) {
1401 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1402 return -ENOENT;
1403 }
1404
1405 /* Find head of filter chain. */
1406
1407 block = tcf_block_find(net, &q, &parent, &cl,
1408 t->tcm_ifindex, t->tcm_block_index, extack);
1409 if (IS_ERR(block)) {
1410 err = PTR_ERR(block);
1411 goto errout;
1412 }
1413
1414 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1415 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1416 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1417 err = -EINVAL;
1418 goto errout;
1419 }
1420 chain = tcf_chain_get(block, chain_index, false);
1421 if (!chain) {
1422 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1423 err = -EINVAL;
1424 goto errout;
1425 }
1426
1427 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1428 prio, false);
1429 if (!tp || IS_ERR(tp)) {
1430 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001431 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001432 goto errout;
1433 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1434 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1435 err = -EINVAL;
1436 goto errout;
1437 }
1438
1439 fh = tp->ops->get(tp, t->tcm_handle);
1440
1441 if (!fh) {
1442 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1443 err = -ENOENT;
1444 } else {
1445 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1446 fh, RTM_NEWTFILTER, true);
1447 if (err < 0)
1448 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1449 }
1450
1451errout:
1452 if (chain)
1453 tcf_chain_put(chain);
1454 return err;
1455}
1456
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001457struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 struct tcf_walker w;
1459 struct sk_buff *skb;
1460 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001461 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001462 struct Qdisc *q;
1463 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464};
1465
WANG Cong8113c092017-08-04 21:31:43 -07001466static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001468 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001469 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001471 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001472 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001473 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1474 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001477static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1478 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001479 long index_start, long *p_index)
1480{
1481 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001482 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001483 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1484 struct tcf_dump_args arg;
1485 struct tcf_proto *tp;
1486
1487 for (tp = rtnl_dereference(chain->filter_chain);
1488 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1489 if (*p_index < index_start)
1490 continue;
1491 if (TC_H_MAJ(tcm->tcm_info) &&
1492 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1493 continue;
1494 if (TC_H_MIN(tcm->tcm_info) &&
1495 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1496 continue;
1497 if (*p_index > index_start)
1498 memset(&cb->args[1], 0,
1499 sizeof(cb->args) - sizeof(cb->args[0]));
1500 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001501 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001502 NETLINK_CB(cb->skb).portid,
1503 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1504 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001505 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001506
1507 cb->args[1] = 1;
1508 }
1509 if (!tp->ops->walk)
1510 continue;
1511 arg.w.fn = tcf_node_dump;
1512 arg.skb = skb;
1513 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001514 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001515 arg.q = q;
1516 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001517 arg.w.stop = 0;
1518 arg.w.skip = cb->args[1] - 1;
1519 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001520 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001521 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001522 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001523 cb->args[1] = arg.w.count + 1;
1524 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001525 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001526 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001527 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001528}
1529
Eric Dumazetbd27a872009-11-05 20:57:26 -08001530/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1532{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001533 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001534 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001535 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001536 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001537 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001538 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001539 long index_start;
1540 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001541 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001542 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Hong zhi guo573ce262013-03-27 06:47:04 +00001544 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001546
1547 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1548 if (err)
1549 return err;
1550
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001551 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1552 block = tcf_block_lookup(net, tcm->tcm_block_index);
1553 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001554 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001555 /* If we work with block index, q is NULL and parent value
1556 * will never be used in the following code. The check
1557 * in tcf_fill_node prevents it. However, compiler does not
1558 * see that far, so set parent to zero to silence the warning
1559 * about parent being uninitialized.
1560 */
1561 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001562 } else {
1563 const struct Qdisc_class_ops *cops;
1564 struct net_device *dev;
1565 unsigned long cl = 0;
1566
1567 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1568 if (!dev)
1569 return skb->len;
1570
1571 parent = tcm->tcm_parent;
1572 if (!parent) {
1573 q = dev->qdisc;
1574 parent = q->handle;
1575 } else {
1576 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1577 }
1578 if (!q)
1579 goto out;
1580 cops = q->ops->cl_ops;
1581 if (!cops)
1582 goto out;
1583 if (!cops->tcf_block)
1584 goto out;
1585 if (TC_H_MIN(tcm->tcm_parent)) {
1586 cl = cops->find(q, tcm->tcm_parent);
1587 if (cl == 0)
1588 goto out;
1589 }
1590 block = cops->tcf_block(q, cl, NULL);
1591 if (!block)
1592 goto out;
1593 if (tcf_block_shared(block))
1594 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001597 index_start = cb->args[0];
1598 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001599
1600 list_for_each_entry(chain, &block->chain_list, list) {
1601 if (tca[TCA_CHAIN] &&
1602 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1603 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001604 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001605 index_start, &index)) {
1606 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001607 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001608 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001609 }
1610
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001611 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001614 /* If we did no progress, the error (EMSGSIZE) is real */
1615 if (skb->len == 0 && err)
1616 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return skb->len;
1618}
1619
WANG Cong18d02642014-09-25 10:26:37 -07001620void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622#ifdef CONFIG_NET_CLS_ACT
Vlad Buslov90b73b72018-07-05 17:24:33 +03001623 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
WANG Cong22dc13c2016-08-13 22:35:00 -07001624 kfree(exts->actions);
1625 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626#endif
1627}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001628EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00001630int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05001631 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
1632 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634#ifdef CONFIG_NET_CLS_ACT
1635 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001637 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
WANG Cong5da57f42013-12-15 20:15:07 -08001639 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001640 act = tcf_action_init_1(net, tp, tb[exts->police],
1641 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03001642 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001643 if (IS_ERR(act))
1644 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
WANG Cong33be6272013-12-15 20:15:05 -08001646 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07001647 exts->actions[0] = act;
1648 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08001649 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03001650 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07001651
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001652 err = tcf_action_init(net, tp, tb[exts->action],
1653 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03001654 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03001655 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03001656 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001657 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03001658 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Cong Wange4b95c42017-11-06 13:47:19 -08001660 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662#else
WANG Cong5da57f42013-12-15 20:15:07 -08001663 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05001664 (exts->police && tb[exts->police])) {
1665 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05001667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668#endif
1669
1670 return 0;
1671}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001672EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001674void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001677 struct tcf_exts old = *dst;
1678
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001679 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07001680 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681#endif
1682}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001683EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
WANG Cong22dc13c2016-08-13 22:35:00 -07001685#ifdef CONFIG_NET_CLS_ACT
1686static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1687{
1688 if (exts->nr_actions == 0)
1689 return NULL;
1690 else
1691 return exts->actions[0];
1692}
1693#endif
WANG Cong33be6272013-12-15 20:15:05 -08001694
WANG Cong5da57f42013-12-15 20:15:07 -08001695int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07001698 struct nlattr *nest;
1699
Jiri Pirko978dfd82017-08-04 14:29:03 +02001700 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /*
1702 * again for backward compatible mode - we want
1703 * to work with both old and new modes of entering
1704 * tc data even if iproute2 was newer - jhs
1705 */
WANG Cong33be6272013-12-15 20:15:05 -08001706 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08001707 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001708 if (nest == NULL)
1709 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07001710
Vlad Buslov90b73b72018-07-05 17:24:33 +03001711 if (tcf_action_dump(skb, exts->actions, 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);
WANG Cong5da57f42013-12-15 20:15:07 -08001714 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08001715 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08001716 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001717 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001718 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08001719 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001720 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001721 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 }
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001725
1726nla_put_failure:
1727 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001729#else
1730 return 0;
1731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001733EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001735
WANG Cong5da57f42013-12-15 20:15:07 -08001736int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001739 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001740 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001741 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742#endif
1743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001745EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Jiri Pirko717503b2017-10-11 09:41:09 +02001747static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1748 enum tc_setup_type type,
1749 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001750{
1751 int ok_count = 0;
1752#ifdef CONFIG_NET_CLS_ACT
1753 const struct tc_action *a;
1754 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001755 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001756
1757 if (!tcf_exts_has_actions(exts))
1758 return 0;
1759
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001760 for (i = 0; i < exts->nr_actions; i++) {
1761 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001762 if (!a->ops->get_dev)
1763 continue;
1764 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01001765 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001766 continue;
1767 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1768 if (ret < 0)
1769 return ret;
1770 ok_count += ret;
1771 }
1772#endif
1773 return ok_count;
1774}
Jiri Pirko717503b2017-10-11 09:41:09 +02001775
Jiri Pirko208c0f42017-10-19 15:50:32 +02001776int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1777 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02001778{
David S. Miller9a99dc12018-06-06 13:55:47 -04001779 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001780 int ret;
1781
David S. Miller9a99dc12018-06-06 13:55:47 -04001782 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1783 if (ret < 0)
1784 return ret;
1785 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001786
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03001787 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04001788 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001789 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1790 if (ret < 0)
1791 return ret;
1792 ok_count += ret;
1793
1794 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02001795}
1796EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001797
Jiri Pirko48617382018-01-17 11:46:46 +01001798static __net_init int tcf_net_init(struct net *net)
1799{
1800 struct tcf_net *tn = net_generic(net, tcf_net_id);
1801
1802 idr_init(&tn->idr);
1803 return 0;
1804}
1805
1806static void __net_exit tcf_net_exit(struct net *net)
1807{
1808 struct tcf_net *tn = net_generic(net, tcf_net_id);
1809
1810 idr_destroy(&tn->idr);
1811}
1812
1813static struct pernet_operations tcf_net_ops = {
1814 .init = tcf_net_init,
1815 .exit = tcf_net_exit,
1816 .id = &tcf_net_id,
1817 .size = sizeof(struct tcf_net),
1818};
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820static int __init tc_filter_init(void)
1821{
Jiri Pirko48617382018-01-17 11:46:46 +01001822 int err;
1823
Cong Wang7aa00452017-10-26 18:24:28 -07001824 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1825 if (!tc_filter_wq)
1826 return -ENOMEM;
1827
Jiri Pirko48617382018-01-17 11:46:46 +01001828 err = register_pernet_subsys(&tcf_net_ops);
1829 if (err)
1830 goto err_register_pernet_subsys;
1831
Vlad Buslovc431f892018-05-31 09:52:53 +03001832 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
1833 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
1834 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001835 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01001838
1839err_register_pernet_subsys:
1840 destroy_workqueue(tc_filter_wq);
1841 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844subsys_initcall(tc_filter_init);