blob: ebc2b9dd783f86e97784182450ff1700373d404a [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 Pirko33a48922017-02-09 14:38:57 +010042static 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
60/* Register(unregister) new classifier type */
61
62int register_tcf_proto_ops(struct tcf_proto_ops *ops)
63{
WANG Cong36272872013-12-15 20:15:11 -080064 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int rc = -EEXIST;
66
67 write_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080068 list_for_each_entry(t, &tcf_proto_base, head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (!strcmp(ops->kind, t->kind))
70 goto out;
71
WANG Cong36272872013-12-15 20:15:11 -080072 list_add_tail(&ops->head, &tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 rc = 0;
74out:
75 write_unlock(&cls_mod_lock);
76 return rc;
77}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -080078EXPORT_SYMBOL(register_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Cong Wang7aa00452017-10-26 18:24:28 -070080static struct workqueue_struct *tc_filter_wq;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
83{
WANG Cong36272872013-12-15 20:15:11 -080084 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int rc = -ENOENT;
86
Daniel Borkmannc78e1742015-05-20 17:13:33 +020087 /* Wait for outstanding call_rcu()s, if any, from a
88 * tcf_proto_ops's destroy() handler.
89 */
90 rcu_barrier();
Cong Wang7aa00452017-10-26 18:24:28 -070091 flush_workqueue(tc_filter_wq);
Daniel Borkmannc78e1742015-05-20 17:13:33 +020092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 write_lock(&cls_mod_lock);
Eric Dumazetdcd76082013-12-20 10:04:18 -080094 list_for_each_entry(t, &tcf_proto_base, head) {
95 if (t == ops) {
96 list_del(&t->head);
97 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 break;
Eric Dumazetdcd76082013-12-20 10:04:18 -080099 }
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 write_unlock(&cls_mod_lock);
102 return rc;
103}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800104EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Cong Wangaaa908f2018-05-23 15:26:53 -0700106bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
Cong Wang7aa00452017-10-26 18:24:28 -0700107{
Cong Wangaaa908f2018-05-23 15:26:53 -0700108 INIT_RCU_WORK(rwork, func);
109 return queue_rcu_work(tc_filter_wq, rwork);
Cong Wang7aa00452017-10-26 18:24:28 -0700110}
111EXPORT_SYMBOL(tcf_queue_work);
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/* Select new prio value from the range, managed by kernel. */
114
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800115static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800117 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000120 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jiri Pirko79619732017-05-17 11:07:58 +0200122 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Jiri Pirko33a48922017-02-09 14:38:57 +0100125static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500126 u32 prio, struct tcf_chain *chain,
127 struct netlink_ext_ack *extack)
Jiri Pirko33a48922017-02-09 14:38:57 +0100128{
129 struct tcf_proto *tp;
130 int err;
131
132 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
133 if (!tp)
134 return ERR_PTR(-ENOBUFS);
135
136 err = -ENOENT;
137 tp->ops = tcf_proto_lookup_ops(kind);
138 if (!tp->ops) {
139#ifdef CONFIG_MODULES
140 rtnl_unlock();
141 request_module("cls_%s", kind);
142 rtnl_lock();
143 tp->ops = tcf_proto_lookup_ops(kind);
144 /* We dropped the RTNL semaphore in order to perform
145 * the module load. So, even if we succeeded in loading
146 * the module we have to replay the request. We indicate
147 * this using -EAGAIN.
148 */
149 if (tp->ops) {
150 module_put(tp->ops->owner);
151 err = -EAGAIN;
152 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500153 NL_SET_ERR_MSG(extack, "TC classifier not found");
Jiri Pirko33a48922017-02-09 14:38:57 +0100154 err = -ENOENT;
155 }
Jiri Pirko33a48922017-02-09 14:38:57 +0100156#endif
Jiri Pirkod68d75f2018-05-11 17:45:32 +0200157 goto errout;
Jiri Pirko33a48922017-02-09 14:38:57 +0100158 }
159 tp->classify = tp->ops->classify;
160 tp->protocol = protocol;
161 tp->prio = prio;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200162 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100163
164 err = tp->ops->init(tp);
165 if (err) {
166 module_put(tp->ops->owner);
167 goto errout;
168 }
169 return tp;
170
171errout:
172 kfree(tp);
173 return ERR_PTR(err);
174}
175
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800176static void tcf_proto_destroy(struct tcf_proto *tp,
177 struct netlink_ext_ack *extack)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100178{
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800179 tp->ops->destroy(tp, extack);
WANG Cong763dbf62017-04-19 14:21:21 -0700180 module_put(tp->ops->owner);
181 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100182}
183
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100184struct tcf_filter_chain_list_item {
185 struct list_head list;
186 tcf_chain_head_change_t *chain_head_change;
187 void *chain_head_change_priv;
188};
189
Jiri Pirko5bc17012017-05-17 11:08:01 +0200190static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
191 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200192{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200193 struct tcf_chain *chain;
194
195 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
196 if (!chain)
197 return NULL;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100198 INIT_LIST_HEAD(&chain->filter_chain_list);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200199 list_add_tail(&chain->list, &block->chain_list);
200 chain->block = block;
201 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700202 chain->refcnt = 1;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200203 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200204}
205
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100206static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
207 struct tcf_proto *tp_head)
208{
209 if (item->chain_head_change)
210 item->chain_head_change(tp_head, item->chain_head_change_priv);
211}
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100212static void tcf_chain_head_change(struct tcf_chain *chain,
213 struct tcf_proto *tp_head)
214{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100215 struct tcf_filter_chain_list_item *item;
216
217 list_for_each_entry(item, &chain->filter_chain_list, list)
218 tcf_chain_head_change_item(item, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100219}
220
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200221static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100222{
Roman Kapld7aa04a2017-11-20 22:21:13 +0100223 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100224
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100225 tcf_chain_head_change(chain, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100226 while (tp) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200227 RCU_INIT_POINTER(chain->filter_chain, tp->next);
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800228 tcf_proto_destroy(tp, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100229 tp = rtnl_dereference(chain->filter_chain);
230 tcf_chain_put(chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100231 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200232}
233
234static void tcf_chain_destroy(struct tcf_chain *chain)
235{
Cong Wangefbf7892017-12-04 10:48:18 -0800236 struct tcf_block *block = chain->block;
237
Cong Wange2ef7542017-09-11 16:33:31 -0700238 list_del(&chain->list);
239 kfree(chain);
Cong Wangefbf7892017-12-04 10:48:18 -0800240 if (list_empty(&block->chain_list))
241 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700242}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200243
Cong Wange2ef7542017-09-11 16:33:31 -0700244static void tcf_chain_hold(struct tcf_chain *chain)
245{
246 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200247}
248
WANG Cong367a8ce2017-05-23 09:42:37 -0700249struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
250 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200251{
252 struct tcf_chain *chain;
253
254 list_for_each_entry(chain, &block->chain_list, list) {
Cong Wange2ef7542017-09-11 16:33:31 -0700255 if (chain->index == chain_index) {
256 tcf_chain_hold(chain);
257 return chain;
258 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200259 }
Jiri Pirko80532382017-09-06 13:14:19 +0200260
Cong Wange2ef7542017-09-11 16:33:31 -0700261 return create ? tcf_chain_create(block, chain_index) : NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200262}
263EXPORT_SYMBOL(tcf_chain_get);
264
265void tcf_chain_put(struct tcf_chain *chain)
266{
Cong Wange2ef7542017-09-11 16:33:31 -0700267 if (--chain->refcnt == 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200268 tcf_chain_destroy(chain);
269}
270EXPORT_SYMBOL(tcf_chain_put);
271
Jiri Pirkocaa72602018-01-17 11:46:50 +0100272static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200273{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100274 return block->offloadcnt;
275}
276
277static int tcf_block_offload_cmd(struct tcf_block *block,
278 struct net_device *dev,
279 struct tcf_block_ext_info *ei,
John Hurley60513bd2018-06-25 14:30:04 -0700280 enum tc_block_command command,
281 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100282{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200283 struct tc_block_offload bo = {};
284
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200285 bo.command = command;
286 bo.binder_type = ei->binder_type;
287 bo.block = block;
John Hurley60513bd2018-06-25 14:30:04 -0700288 bo.extack = extack;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100289 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200290}
291
Jiri Pirkocaa72602018-01-17 11:46:50 +0100292static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700293 struct tcf_block_ext_info *ei,
294 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200295{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100296 struct net_device *dev = q->dev_queue->dev;
297 int err;
298
299 if (!dev->netdev_ops->ndo_setup_tc)
300 goto no_offload_dev_inc;
301
302 /* If tc offload feature is disabled and the block we try to bind
303 * to already has some offloaded filters, forbid to bind.
304 */
John Hurley60513bd2018-06-25 14:30:04 -0700305 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
306 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Jiri Pirkocaa72602018-01-17 11:46:50 +0100307 return -EOPNOTSUPP;
John Hurley60513bd2018-06-25 14:30:04 -0700308 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100309
John Hurley60513bd2018-06-25 14:30:04 -0700310 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100311 if (err == -EOPNOTSUPP)
312 goto no_offload_dev_inc;
313 return err;
314
315no_offload_dev_inc:
316 if (tcf_block_offload_in_use(block))
317 return -EOPNOTSUPP;
318 block->nooffloaddevcnt++;
319 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200320}
321
322static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
323 struct tcf_block_ext_info *ei)
324{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100325 struct net_device *dev = q->dev_queue->dev;
326 int err;
327
328 if (!dev->netdev_ops->ndo_setup_tc)
329 goto no_offload_dev_dec;
John Hurley60513bd2018-06-25 14:30:04 -0700330 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100331 if (err == -EOPNOTSUPP)
332 goto no_offload_dev_dec;
333 return;
334
335no_offload_dev_dec:
336 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200337}
338
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100339static int
340tcf_chain_head_change_cb_add(struct tcf_chain *chain,
341 struct tcf_block_ext_info *ei,
342 struct netlink_ext_ack *extack)
343{
344 struct tcf_filter_chain_list_item *item;
345
346 item = kmalloc(sizeof(*item), GFP_KERNEL);
347 if (!item) {
348 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
349 return -ENOMEM;
350 }
351 item->chain_head_change = ei->chain_head_change;
352 item->chain_head_change_priv = ei->chain_head_change_priv;
353 if (chain->filter_chain)
354 tcf_chain_head_change_item(item, chain->filter_chain);
355 list_add(&item->list, &chain->filter_chain_list);
356 return 0;
357}
358
359static void
360tcf_chain_head_change_cb_del(struct tcf_chain *chain,
361 struct tcf_block_ext_info *ei)
362{
363 struct tcf_filter_chain_list_item *item;
364
365 list_for_each_entry(item, &chain->filter_chain_list, list) {
366 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
367 (item->chain_head_change == ei->chain_head_change &&
368 item->chain_head_change_priv == ei->chain_head_change_priv)) {
369 tcf_chain_head_change_item(item, NULL);
370 list_del(&item->list);
371 kfree(item);
372 return;
373 }
374 }
375 WARN_ON(1);
376}
377
Jiri Pirko48617382018-01-17 11:46:46 +0100378struct tcf_net {
379 struct idr idr;
380};
381
382static unsigned int tcf_net_id;
383
384static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100385 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100386{
Jiri Pirko48617382018-01-17 11:46:46 +0100387 struct tcf_net *tn = net_generic(net, tcf_net_id);
Jiri Pirko48617382018-01-17 11:46:46 +0100388
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100389 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
390 GFP_KERNEL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100391}
392
Jiri Pirko48617382018-01-17 11:46:46 +0100393static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200394{
Jiri Pirko48617382018-01-17 11:46:46 +0100395 struct tcf_net *tn = net_generic(net, tcf_net_id);
396
Matthew Wilcox9c160942017-11-28 09:48:43 -0500397 idr_remove(&tn->idr, block->index);
Jiri Pirko48617382018-01-17 11:46:46 +0100398}
399
400static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100401 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100402 struct netlink_ext_ack *extack)
403{
404 struct tcf_block *block;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200405 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200406 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200407
Jiri Pirko48617382018-01-17 11:46:46 +0100408 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500409 if (!block) {
410 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100411 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500412 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200413 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200414 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100415 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200416
Jiri Pirko5bc17012017-05-17 11:08:01 +0200417 /* Create chain 0 by default, it has to be always present. */
418 chain = tcf_chain_create(block, 0);
419 if (!chain) {
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500420 NL_SET_ERR_MSG(extack, "Failed to create new tcf chain");
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200421 err = -ENOMEM;
422 goto err_chain_create;
423 }
Jiri Pirko48617382018-01-17 11:46:46 +0100424 block->refcnt = 1;
425 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100426 block->index = block_index;
427
428 /* Don't store q pointer for blocks which are shared */
429 if (!tcf_block_shared(block))
430 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100431 return block;
432
433err_chain_create:
434 kfree(block);
435 return ERR_PTR(err);
436}
437
438static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
439{
440 struct tcf_net *tn = net_generic(net, tcf_net_id);
441
Matthew Wilcox322d8842017-11-28 10:01:24 -0500442 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100443}
444
Vlad Buslovc431f892018-05-31 09:52:53 +0300445/* Find tcf block.
446 * Set q, parent, cl when appropriate.
447 */
448
449static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
450 u32 *parent, unsigned long *cl,
451 int ifindex, u32 block_index,
452 struct netlink_ext_ack *extack)
453{
454 struct tcf_block *block;
455
456 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
457 block = tcf_block_lookup(net, block_index);
458 if (!block) {
459 NL_SET_ERR_MSG(extack, "Block of given index was not found");
460 return ERR_PTR(-EINVAL);
461 }
462 } else {
463 const struct Qdisc_class_ops *cops;
464 struct net_device *dev;
465
466 /* Find link */
467 dev = __dev_get_by_index(net, ifindex);
468 if (!dev)
469 return ERR_PTR(-ENODEV);
470
471 /* Find qdisc */
472 if (!*parent) {
473 *q = dev->qdisc;
474 *parent = (*q)->handle;
475 } else {
476 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
477 if (!*q) {
478 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
479 return ERR_PTR(-EINVAL);
480 }
481 }
482
483 /* Is it classful? */
484 cops = (*q)->ops->cl_ops;
485 if (!cops) {
486 NL_SET_ERR_MSG(extack, "Qdisc not classful");
487 return ERR_PTR(-EINVAL);
488 }
489
490 if (!cops->tcf_block) {
491 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
492 return ERR_PTR(-EOPNOTSUPP);
493 }
494
495 /* Do we search for filter, attached to class? */
496 if (TC_H_MIN(*parent)) {
497 *cl = cops->find(*q, *parent);
498 if (*cl == 0) {
499 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
500 return ERR_PTR(-ENOENT);
501 }
502 }
503
504 /* And the last stroke */
505 block = cops->tcf_block(*q, *cl, extack);
506 if (!block)
507 return ERR_PTR(-EINVAL);
508 if (tcf_block_shared(block)) {
509 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
510 return ERR_PTR(-EOPNOTSUPP);
511 }
512 }
513
514 return block;
515}
516
Jiri Pirko48617382018-01-17 11:46:46 +0100517static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
518{
519 return list_first_entry(&block->chain_list, struct tcf_chain, list);
520}
521
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100522struct tcf_block_owner_item {
523 struct list_head list;
524 struct Qdisc *q;
525 enum tcf_block_binder_type binder_type;
526};
527
528static void
529tcf_block_owner_netif_keep_dst(struct tcf_block *block,
530 struct Qdisc *q,
531 enum tcf_block_binder_type binder_type)
532{
533 if (block->keep_dst &&
534 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
535 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
536 netif_keep_dst(qdisc_dev(q));
537}
538
539void tcf_block_netif_keep_dst(struct tcf_block *block)
540{
541 struct tcf_block_owner_item *item;
542
543 block->keep_dst = true;
544 list_for_each_entry(item, &block->owner_list, list)
545 tcf_block_owner_netif_keep_dst(block, item->q,
546 item->binder_type);
547}
548EXPORT_SYMBOL(tcf_block_netif_keep_dst);
549
550static int tcf_block_owner_add(struct tcf_block *block,
551 struct Qdisc *q,
552 enum tcf_block_binder_type binder_type)
553{
554 struct tcf_block_owner_item *item;
555
556 item = kmalloc(sizeof(*item), GFP_KERNEL);
557 if (!item)
558 return -ENOMEM;
559 item->q = q;
560 item->binder_type = binder_type;
561 list_add(&item->list, &block->owner_list);
562 return 0;
563}
564
565static void tcf_block_owner_del(struct tcf_block *block,
566 struct Qdisc *q,
567 enum tcf_block_binder_type binder_type)
568{
569 struct tcf_block_owner_item *item;
570
571 list_for_each_entry(item, &block->owner_list, list) {
572 if (item->q == q && item->binder_type == binder_type) {
573 list_del(&item->list);
574 kfree(item);
575 return;
576 }
577 }
578 WARN_ON(1);
579}
580
Jiri Pirko48617382018-01-17 11:46:46 +0100581int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
582 struct tcf_block_ext_info *ei,
583 struct netlink_ext_ack *extack)
584{
585 struct net *net = qdisc_net(q);
586 struct tcf_block *block = NULL;
587 bool created = false;
588 int err;
589
590 if (ei->block_index) {
591 /* block_index not 0 means the shared block is requested */
592 block = tcf_block_lookup(net, ei->block_index);
593 if (block)
594 block->refcnt++;
595 }
596
597 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100598 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100599 if (IS_ERR(block))
600 return PTR_ERR(block);
601 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100602 if (tcf_block_shared(block)) {
603 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100604 if (err)
605 goto err_block_insert;
606 }
607 }
608
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100609 err = tcf_block_owner_add(block, q, ei->binder_type);
610 if (err)
611 goto err_block_owner_add;
612
613 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
614
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100615 err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block),
616 ei, extack);
617 if (err)
618 goto err_chain_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100619
John Hurley60513bd2018-06-25 14:30:04 -0700620 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100621 if (err)
622 goto err_block_offload_bind;
623
Jiri Pirko6529eab2017-05-17 11:07:55 +0200624 *p_block = block;
625 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200626
Jiri Pirkocaa72602018-01-17 11:46:50 +0100627err_block_offload_bind:
628 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100629err_chain_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100630 tcf_block_owner_del(block, q, ei->binder_type);
631err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100632 if (created) {
633 if (tcf_block_shared(block))
634 tcf_block_remove(block, net);
635err_block_insert:
636 kfree(tcf_block_chain_zero(block));
637 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 Pirkoa9b19442018-01-17 11:46:45 +0100676 tcf_chain_head_change_cb_del(tcf_block_chain_zero(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 Pirko48617382018-01-17 11:46:46 +0100679 if (--block->refcnt == 0) {
680 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 Pirko48617382018-01-17 11:46:46 +0100695 if (block->refcnt == 0) {
696 /* 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 Pirko48617382018-01-17 11:46:46 +0100700 /* Finally, put chain 0 and allow block to be freed. */
701 tcf_chain_put(tcf_block_chain_zero(block));
702 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200703}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200704EXPORT_SYMBOL(tcf_block_put_ext);
705
706void tcf_block_put(struct tcf_block *block)
707{
708 struct tcf_block_ext_info ei = {0, };
709
Jiri Pirko4853f122017-12-21 13:13:59 +0100710 if (!block)
711 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100712 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200713}
David S. Millere1ea2f92017-10-30 14:10:01 +0900714
Jiri Pirko6529eab2017-05-17 11:07:55 +0200715EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100716
Jiri Pirkoacb67442017-10-19 15:50:31 +0200717struct tcf_block_cb {
718 struct list_head list;
719 tc_setup_cb_t *cb;
720 void *cb_ident;
721 void *cb_priv;
722 unsigned int refcnt;
723};
724
725void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
726{
727 return block_cb->cb_priv;
728}
729EXPORT_SYMBOL(tcf_block_cb_priv);
730
731struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
732 tc_setup_cb_t *cb, void *cb_ident)
733{ struct tcf_block_cb *block_cb;
734
735 list_for_each_entry(block_cb, &block->cb_list, list)
736 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
737 return block_cb;
738 return NULL;
739}
740EXPORT_SYMBOL(tcf_block_cb_lookup);
741
742void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
743{
744 block_cb->refcnt++;
745}
746EXPORT_SYMBOL(tcf_block_cb_incref);
747
748unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
749{
750 return --block_cb->refcnt;
751}
752EXPORT_SYMBOL(tcf_block_cb_decref);
753
John Hurley32636742018-06-25 14:30:10 -0700754static int
755tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
756 void *cb_priv, bool add, bool offload_in_use,
757 struct netlink_ext_ack *extack)
758{
759 struct tcf_chain *chain;
760 struct tcf_proto *tp;
761 int err;
762
763 list_for_each_entry(chain, &block->chain_list, list) {
764 for (tp = rtnl_dereference(chain->filter_chain); tp;
765 tp = rtnl_dereference(tp->next)) {
766 if (tp->ops->reoffload) {
767 err = tp->ops->reoffload(tp, add, cb, cb_priv,
768 extack);
769 if (err && add)
770 goto err_playback_remove;
771 } else if (add && offload_in_use) {
772 err = -EOPNOTSUPP;
773 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
774 goto err_playback_remove;
775 }
776 }
777 }
778
779 return 0;
780
781err_playback_remove:
782 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
783 extack);
784 return err;
785}
786
Jiri Pirkoacb67442017-10-19 15:50:31 +0200787struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
788 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700789 void *cb_priv,
790 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200791{
792 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700793 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200794
John Hurley32636742018-06-25 14:30:10 -0700795 /* Replay any already present rules */
796 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
797 tcf_block_offload_in_use(block),
798 extack);
799 if (err)
800 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100801
Jiri Pirkoacb67442017-10-19 15:50:31 +0200802 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
803 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100804 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200805 block_cb->cb = cb;
806 block_cb->cb_ident = cb_ident;
807 block_cb->cb_priv = cb_priv;
808 list_add(&block_cb->list, &block->cb_list);
809 return block_cb;
810}
811EXPORT_SYMBOL(__tcf_block_cb_register);
812
813int tcf_block_cb_register(struct tcf_block *block,
814 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700815 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200816{
817 struct tcf_block_cb *block_cb;
818
John Hurley60513bd2018-06-25 14:30:04 -0700819 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
820 extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100821 return IS_ERR(block_cb) ? PTR_ERR(block_cb) : 0;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200822}
823EXPORT_SYMBOL(tcf_block_cb_register);
824
John Hurley32636742018-06-25 14:30:10 -0700825void __tcf_block_cb_unregister(struct tcf_block *block,
826 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200827{
John Hurley32636742018-06-25 14:30:10 -0700828 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
829 false, tcf_block_offload_in_use(block),
830 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200831 list_del(&block_cb->list);
832 kfree(block_cb);
833}
834EXPORT_SYMBOL(__tcf_block_cb_unregister);
835
836void tcf_block_cb_unregister(struct tcf_block *block,
837 tc_setup_cb_t *cb, void *cb_ident)
838{
839 struct tcf_block_cb *block_cb;
840
841 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
842 if (!block_cb)
843 return;
John Hurley32636742018-06-25 14:30:10 -0700844 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200845}
846EXPORT_SYMBOL(tcf_block_cb_unregister);
847
848static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
849 void *type_data, bool err_stop)
850{
851 struct tcf_block_cb *block_cb;
852 int ok_count = 0;
853 int err;
854
David S. Miller9a99dc12018-06-06 13:55:47 -0400855 /* Make sure all netdevs sharing this block are offload-capable. */
856 if (block->nooffloaddevcnt && err_stop)
857 return -EOPNOTSUPP;
858
Jiri Pirkoacb67442017-10-19 15:50:31 +0200859 list_for_each_entry(block_cb, &block->cb_list, list) {
860 err = block_cb->cb(type, type_data, block_cb->cb_priv);
861 if (err) {
862 if (err_stop)
863 return err;
864 } else {
865 ok_count++;
866 }
867 }
868 return ok_count;
869}
870
Jiri Pirko87d83092017-05-17 11:07:54 +0200871/* Main classifier routine: scans classifier chain attached
872 * to this qdisc, (optionally) tests for protocol and asks
873 * specific classifiers.
874 */
875int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
876 struct tcf_result *res, bool compat_mode)
877{
878 __be16 protocol = tc_skb_protocol(skb);
879#ifdef CONFIG_NET_CLS_ACT
880 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200881 const struct tcf_proto *orig_tp = tp;
882 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200883 int limit = 0;
884
885reclassify:
886#endif
887 for (; tp; tp = rcu_dereference_bh(tp->next)) {
888 int err;
889
890 if (tp->protocol != protocol &&
891 tp->protocol != htons(ETH_P_ALL))
892 continue;
893
894 err = tp->classify(skb, tp, res);
895#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200896 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200897 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200898 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200899 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200900 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200901 goto reset;
902 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200903#endif
904 if (err >= 0)
905 return err;
906 }
907
908 return TC_ACT_UNSPEC; /* signal: continue lookup */
909#ifdef CONFIG_NET_CLS_ACT
910reset:
911 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100912 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
913 tp->chain->block->index,
914 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200915 ntohs(tp->protocol));
916 return TC_ACT_SHOT;
917 }
918
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200919 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200920 protocol = tc_skb_protocol(skb);
921 goto reclassify;
922#endif
923}
924EXPORT_SYMBOL(tcf_classify);
925
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200926struct tcf_chain_info {
927 struct tcf_proto __rcu **pprev;
928 struct tcf_proto __rcu *next;
929};
930
931static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
932{
933 return rtnl_dereference(*chain_info->pprev);
934}
935
936static void tcf_chain_tp_insert(struct tcf_chain *chain,
937 struct tcf_chain_info *chain_info,
938 struct tcf_proto *tp)
939{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100940 if (*chain_info->pprev == chain->filter_chain)
941 tcf_chain_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200942 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
943 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700944 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200945}
946
947static void tcf_chain_tp_remove(struct tcf_chain *chain,
948 struct tcf_chain_info *chain_info,
949 struct tcf_proto *tp)
950{
951 struct tcf_proto *next = rtnl_dereference(chain_info->next);
952
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100953 if (tp == chain->filter_chain)
954 tcf_chain_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200955 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700956 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200957}
958
959static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
960 struct tcf_chain_info *chain_info,
961 u32 protocol, u32 prio,
962 bool prio_allocate)
963{
964 struct tcf_proto **pprev;
965 struct tcf_proto *tp;
966
967 /* Check the chain for existence of proto-tcf with this priority */
968 for (pprev = &chain->filter_chain;
969 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
970 if (tp->prio >= prio) {
971 if (tp->prio == prio) {
972 if (prio_allocate ||
973 (tp->protocol != protocol && protocol))
974 return ERR_PTR(-EINVAL);
975 } else {
976 tp = NULL;
977 }
978 break;
979 }
980 }
981 chain_info->pprev = pprev;
982 chain_info->next = tp ? tp->next : NULL;
983 return tp;
984}
985
WANG Cong71203712017-08-07 15:26:50 -0700986static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100987 struct tcf_proto *tp, struct tcf_block *block,
988 struct Qdisc *q, u32 parent, void *fh,
989 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700990{
991 struct tcmsg *tcm;
992 struct nlmsghdr *nlh;
993 unsigned char *b = skb_tail_pointer(skb);
994
995 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
996 if (!nlh)
997 goto out_nlmsg_trim;
998 tcm = nlmsg_data(nlh);
999 tcm->tcm_family = AF_UNSPEC;
1000 tcm->tcm__pad1 = 0;
1001 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001002 if (q) {
1003 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1004 tcm->tcm_parent = parent;
1005 } else {
1006 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1007 tcm->tcm_block_index = block->index;
1008 }
WANG Cong71203712017-08-07 15:26:50 -07001009 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1010 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1011 goto nla_put_failure;
1012 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1013 goto nla_put_failure;
1014 if (!fh) {
1015 tcm->tcm_handle = 0;
1016 } else {
1017 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1018 goto nla_put_failure;
1019 }
1020 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1021 return skb->len;
1022
1023out_nlmsg_trim:
1024nla_put_failure:
1025 nlmsg_trim(skb, b);
1026 return -1;
1027}
1028
1029static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1030 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001031 struct tcf_block *block, struct Qdisc *q,
1032 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001033{
1034 struct sk_buff *skb;
1035 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1036
1037 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1038 if (!skb)
1039 return -ENOBUFS;
1040
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001041 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1042 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001043 kfree_skb(skb);
1044 return -EINVAL;
1045 }
1046
1047 if (unicast)
1048 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1049
1050 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1051 n->nlmsg_flags & NLM_F_ECHO);
1052}
1053
1054static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1055 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001056 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001057 u32 parent, void *fh, bool unicast, bool *last,
1058 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001059{
1060 struct sk_buff *skb;
1061 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1062 int err;
1063
1064 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1065 if (!skb)
1066 return -ENOBUFS;
1067
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001068 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1069 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001070 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001071 kfree_skb(skb);
1072 return -EINVAL;
1073 }
1074
Alexander Aring571acf22018-01-18 11:20:53 -05001075 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001076 if (err) {
1077 kfree_skb(skb);
1078 return err;
1079 }
1080
1081 if (unicast)
1082 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1083
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001084 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1085 n->nlmsg_flags & NLM_F_ECHO);
1086 if (err < 0)
1087 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1088 return err;
WANG Cong71203712017-08-07 15:26:50 -07001089}
1090
1091static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001092 struct tcf_block *block, struct Qdisc *q,
1093 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001094 struct tcf_chain *chain, int event)
1095{
1096 struct tcf_proto *tp;
1097
1098 for (tp = rtnl_dereference(chain->filter_chain);
1099 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001100 tfilter_notify(net, oskb, n, tp, block,
1101 q, parent, 0, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001102}
1103
Vlad Buslovc431f892018-05-31 09:52:53 +03001104static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001105 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001107 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001108 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct tcmsg *t;
1110 u32 protocol;
1111 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001112 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001114 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001115 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001116 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001117 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001118 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001121 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001123 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Vlad Buslovc431f892018-05-31 09:52:53 +03001125 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001126 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001129 tp_created = 0;
1130
David Ahernc21ef3e2017-04-16 09:48:24 -07001131 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001132 if (err < 0)
1133 return err;
1134
David S. Miller942b8162012-06-26 21:48:50 -07001135 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 protocol = TC_H_MIN(t->tcm_info);
1137 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001138 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 parent = t->tcm_parent;
1140 cl = 0;
1141
1142 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001143 /* If no priority is provided by the user,
1144 * we allocate one.
1145 */
1146 if (n->nlmsg_flags & NLM_F_CREATE) {
1147 prio = TC_H_MAKE(0x80000000U, 0U);
1148 prio_allocate = true;
1149 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001150 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
1155 /* Find head of filter chain. */
1156
Vlad Buslovc431f892018-05-31 09:52:53 +03001157 block = tcf_block_find(net, &q, &parent, &cl,
1158 t->tcm_ifindex, t->tcm_block_index, extack);
1159 if (IS_ERR(block)) {
1160 err = PTR_ERR(block);
1161 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001162 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001163
1164 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1165 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001166 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001167 err = -EINVAL;
1168 goto errout;
1169 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001170 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001171 if (!chain) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001172 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001173 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001174 goto errout;
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001177 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1178 prio, prio_allocate);
1179 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001180 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001181 err = PTR_ERR(tp);
1182 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184
1185 if (tp == NULL) {
1186 /* Proto-tcf does not exist, create new one */
1187
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001188 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001189 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001190 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Vlad Buslovc431f892018-05-31 09:52:53 +03001194 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001195 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 +01001196 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001200 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001201 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jiri Pirko33a48922017-02-09 14:38:57 +01001203 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001204 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001205 if (IS_ERR(tp)) {
1206 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto errout;
1208 }
Minoru Usui12186be2009-06-02 02:17:34 -07001209 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001210 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001211 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001212 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 fh = tp->ops->get(tp, t->tcm_handle);
1217
WANG Cong8113c092017-08-04 21:31:43 -07001218 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001219 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001220 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 +01001221 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001223 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001224 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1225 NL_SET_ERR_MSG(extack, "Filter already exists");
1226 err = -EEXIST;
1227 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001230 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001231 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1232 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001233 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001234 if (tp_created)
1235 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001236 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001237 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001238 } else {
1239 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001240 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001244 if (chain)
1245 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (err == -EAGAIN)
1247 /* Replay the request. */
1248 goto replay;
1249 return err;
1250}
1251
Vlad Buslovc431f892018-05-31 09:52:53 +03001252static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1253 struct netlink_ext_ack *extack)
1254{
1255 struct net *net = sock_net(skb->sk);
1256 struct nlattr *tca[TCA_MAX + 1];
1257 struct tcmsg *t;
1258 u32 protocol;
1259 u32 prio;
1260 u32 parent;
1261 u32 chain_index;
1262 struct Qdisc *q = NULL;
1263 struct tcf_chain_info chain_info;
1264 struct tcf_chain *chain = NULL;
1265 struct tcf_block *block;
1266 struct tcf_proto *tp = NULL;
1267 unsigned long cl = 0;
1268 void *fh = NULL;
1269 int err;
1270
1271 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1272 return -EPERM;
1273
1274 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1275 if (err < 0)
1276 return err;
1277
1278 t = nlmsg_data(n);
1279 protocol = TC_H_MIN(t->tcm_info);
1280 prio = TC_H_MAJ(t->tcm_info);
1281 parent = t->tcm_parent;
1282
1283 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1284 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1285 return -ENOENT;
1286 }
1287
1288 /* Find head of filter chain. */
1289
1290 block = tcf_block_find(net, &q, &parent, &cl,
1291 t->tcm_ifindex, t->tcm_block_index, extack);
1292 if (IS_ERR(block)) {
1293 err = PTR_ERR(block);
1294 goto errout;
1295 }
1296
1297 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1298 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1299 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1300 err = -EINVAL;
1301 goto errout;
1302 }
1303 chain = tcf_chain_get(block, chain_index, false);
1304 if (!chain) {
1305 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1306 err = -EINVAL;
1307 goto errout;
1308 }
1309
1310 if (prio == 0) {
1311 tfilter_notify_chain(net, skb, block, q, parent, n,
1312 chain, RTM_DELTFILTER);
1313 tcf_chain_flush(chain);
1314 err = 0;
1315 goto errout;
1316 }
1317
1318 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1319 prio, false);
1320 if (!tp || IS_ERR(tp)) {
1321 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001322 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001323 goto errout;
1324 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1325 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1326 err = -EINVAL;
1327 goto errout;
1328 }
1329
1330 fh = tp->ops->get(tp, t->tcm_handle);
1331
1332 if (!fh) {
1333 if (t->tcm_handle == 0) {
1334 tcf_chain_tp_remove(chain, &chain_info, tp);
1335 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1336 RTM_DELTFILTER, false);
1337 tcf_proto_destroy(tp, extack);
1338 err = 0;
1339 } else {
1340 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1341 err = -ENOENT;
1342 }
1343 } else {
1344 bool last;
1345
1346 err = tfilter_del_notify(net, skb, n, tp, block,
1347 q, parent, fh, false, &last,
1348 extack);
1349 if (err)
1350 goto errout;
1351 if (last) {
1352 tcf_chain_tp_remove(chain, &chain_info, tp);
1353 tcf_proto_destroy(tp, extack);
1354 }
1355 }
1356
1357errout:
1358 if (chain)
1359 tcf_chain_put(chain);
1360 return err;
1361}
1362
1363static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1364 struct netlink_ext_ack *extack)
1365{
1366 struct net *net = sock_net(skb->sk);
1367 struct nlattr *tca[TCA_MAX + 1];
1368 struct tcmsg *t;
1369 u32 protocol;
1370 u32 prio;
1371 u32 parent;
1372 u32 chain_index;
1373 struct Qdisc *q = NULL;
1374 struct tcf_chain_info chain_info;
1375 struct tcf_chain *chain = NULL;
1376 struct tcf_block *block;
1377 struct tcf_proto *tp = NULL;
1378 unsigned long cl = 0;
1379 void *fh = NULL;
1380 int err;
1381
1382 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1383 if (err < 0)
1384 return err;
1385
1386 t = nlmsg_data(n);
1387 protocol = TC_H_MIN(t->tcm_info);
1388 prio = TC_H_MAJ(t->tcm_info);
1389 parent = t->tcm_parent;
1390
1391 if (prio == 0) {
1392 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1393 return -ENOENT;
1394 }
1395
1396 /* Find head of filter chain. */
1397
1398 block = tcf_block_find(net, &q, &parent, &cl,
1399 t->tcm_ifindex, t->tcm_block_index, extack);
1400 if (IS_ERR(block)) {
1401 err = PTR_ERR(block);
1402 goto errout;
1403 }
1404
1405 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1406 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1407 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1408 err = -EINVAL;
1409 goto errout;
1410 }
1411 chain = tcf_chain_get(block, chain_index, false);
1412 if (!chain) {
1413 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1414 err = -EINVAL;
1415 goto errout;
1416 }
1417
1418 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1419 prio, false);
1420 if (!tp || IS_ERR(tp)) {
1421 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001422 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001423 goto errout;
1424 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1425 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1426 err = -EINVAL;
1427 goto errout;
1428 }
1429
1430 fh = tp->ops->get(tp, t->tcm_handle);
1431
1432 if (!fh) {
1433 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1434 err = -ENOENT;
1435 } else {
1436 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1437 fh, RTM_NEWTFILTER, true);
1438 if (err < 0)
1439 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1440 }
1441
1442errout:
1443 if (chain)
1444 tcf_chain_put(chain);
1445 return err;
1446}
1447
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001448struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 struct tcf_walker w;
1450 struct sk_buff *skb;
1451 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001452 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001453 struct Qdisc *q;
1454 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455};
1456
WANG Cong8113c092017-08-04 21:31:43 -07001457static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001459 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001460 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001462 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001463 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001464 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1465 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001468static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1469 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001470 long index_start, long *p_index)
1471{
1472 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001473 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001474 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1475 struct tcf_dump_args arg;
1476 struct tcf_proto *tp;
1477
1478 for (tp = rtnl_dereference(chain->filter_chain);
1479 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1480 if (*p_index < index_start)
1481 continue;
1482 if (TC_H_MAJ(tcm->tcm_info) &&
1483 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1484 continue;
1485 if (TC_H_MIN(tcm->tcm_info) &&
1486 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1487 continue;
1488 if (*p_index > index_start)
1489 memset(&cb->args[1], 0,
1490 sizeof(cb->args) - sizeof(cb->args[0]));
1491 if (cb->args[1] == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001492 if (tcf_fill_node(net, skb, tp, block, q, parent, 0,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001493 NETLINK_CB(cb->skb).portid,
1494 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1495 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001496 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001497
1498 cb->args[1] = 1;
1499 }
1500 if (!tp->ops->walk)
1501 continue;
1502 arg.w.fn = tcf_node_dump;
1503 arg.skb = skb;
1504 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001505 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001506 arg.q = q;
1507 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001508 arg.w.stop = 0;
1509 arg.w.skip = cb->args[1] - 1;
1510 arg.w.count = 0;
1511 tp->ops->walk(tp, &arg.w);
1512 cb->args[1] = arg.w.count + 1;
1513 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001514 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001515 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001516 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001517}
1518
Eric Dumazetbd27a872009-11-05 20:57:26 -08001519/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1521{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001522 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001523 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001524 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001525 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001526 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001527 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001528 long index_start;
1529 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001530 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001531 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Hong zhi guo573ce262013-03-27 06:47:04 +00001533 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001535
1536 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1537 if (err)
1538 return err;
1539
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001540 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1541 block = tcf_block_lookup(net, tcm->tcm_block_index);
1542 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001543 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001544 /* If we work with block index, q is NULL and parent value
1545 * will never be used in the following code. The check
1546 * in tcf_fill_node prevents it. However, compiler does not
1547 * see that far, so set parent to zero to silence the warning
1548 * about parent being uninitialized.
1549 */
1550 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001551 } else {
1552 const struct Qdisc_class_ops *cops;
1553 struct net_device *dev;
1554 unsigned long cl = 0;
1555
1556 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1557 if (!dev)
1558 return skb->len;
1559
1560 parent = tcm->tcm_parent;
1561 if (!parent) {
1562 q = dev->qdisc;
1563 parent = q->handle;
1564 } else {
1565 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1566 }
1567 if (!q)
1568 goto out;
1569 cops = q->ops->cl_ops;
1570 if (!cops)
1571 goto out;
1572 if (!cops->tcf_block)
1573 goto out;
1574 if (TC_H_MIN(tcm->tcm_parent)) {
1575 cl = cops->find(q, tcm->tcm_parent);
1576 if (cl == 0)
1577 goto out;
1578 }
1579 block = cops->tcf_block(q, cl, NULL);
1580 if (!block)
1581 goto out;
1582 if (tcf_block_shared(block))
1583 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001586 index_start = cb->args[0];
1587 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001588
1589 list_for_each_entry(chain, &block->chain_list, list) {
1590 if (tca[TCA_CHAIN] &&
1591 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1592 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001593 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001594 index_start, &index)) {
1595 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001596 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001597 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001598 }
1599
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001600 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001603 /* If we did no progress, the error (EMSGSIZE) is real */
1604 if (skb->len == 0 && err)
1605 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return skb->len;
1607}
1608
WANG Cong18d02642014-09-25 10:26:37 -07001609void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001612 LIST_HEAD(actions);
1613
Cong Wang2d132eb2017-10-26 18:24:40 -07001614 ASSERT_RTNL();
WANG Cong22dc13c2016-08-13 22:35:00 -07001615 tcf_exts_to_list(exts, &actions);
1616 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1617 kfree(exts->actions);
1618 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619#endif
1620}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001621EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00001623int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05001624 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
1625 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627#ifdef CONFIG_NET_CLS_ACT
1628 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001630 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
WANG Cong5da57f42013-12-15 20:15:07 -08001632 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001633 act = tcf_action_init_1(net, tp, tb[exts->police],
1634 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03001635 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001636 if (IS_ERR(act))
1637 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
WANG Cong33be6272013-12-15 20:15:05 -08001639 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07001640 exts->actions[0] = act;
1641 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08001642 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001643 LIST_HEAD(actions);
1644 int err, i = 0;
1645
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001646 err = tcf_action_init(net, tp, tb[exts->action],
1647 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov789871b2018-07-05 17:24:25 +03001648 &actions, &attr_size, true,
1649 extack);
WANG Cong33be6272013-12-15 20:15:05 -08001650 if (err)
1651 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -07001652 list_for_each_entry(act, &actions, list)
1653 exts->actions[i++] = act;
1654 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Cong Wange4b95c42017-11-06 13:47:19 -08001656 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658#else
WANG Cong5da57f42013-12-15 20:15:07 -08001659 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05001660 (exts->police && tb[exts->police])) {
1661 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05001663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664#endif
1665
1666 return 0;
1667}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001668EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001670void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001673 struct tcf_exts old = *dst;
1674
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001675 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07001676 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677#endif
1678}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001679EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
WANG Cong22dc13c2016-08-13 22:35:00 -07001681#ifdef CONFIG_NET_CLS_ACT
1682static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1683{
1684 if (exts->nr_actions == 0)
1685 return NULL;
1686 else
1687 return exts->actions[0];
1688}
1689#endif
WANG Cong33be6272013-12-15 20:15:05 -08001690
WANG Cong5da57f42013-12-15 20:15:07 -08001691int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07001694 struct nlattr *nest;
1695
Jiri Pirko978dfd82017-08-04 14:29:03 +02001696 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
1698 * again for backward compatible mode - we want
1699 * to work with both old and new modes of entering
1700 * tc data even if iproute2 was newer - jhs
1701 */
WANG Cong33be6272013-12-15 20:15:05 -08001702 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001703 LIST_HEAD(actions);
1704
WANG Cong5da57f42013-12-15 20:15:07 -08001705 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001706 if (nest == NULL)
1707 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07001708
1709 tcf_exts_to_list(exts, &actions);
1710 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001711 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001712 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08001713 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08001714 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08001715 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001716 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001717 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08001718 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001719 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001720 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001724
1725nla_put_failure:
1726 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001728#else
1729 return 0;
1730#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001732EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001734
WANG Cong5da57f42013-12-15 20:15:07 -08001735int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001738 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001739 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001740 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741#endif
1742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001744EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Jiri Pirko717503b2017-10-11 09:41:09 +02001746static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1747 enum tc_setup_type type,
1748 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001749{
1750 int ok_count = 0;
1751#ifdef CONFIG_NET_CLS_ACT
1752 const struct tc_action *a;
1753 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001754 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001755
1756 if (!tcf_exts_has_actions(exts))
1757 return 0;
1758
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001759 for (i = 0; i < exts->nr_actions; i++) {
1760 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001761 if (!a->ops->get_dev)
1762 continue;
1763 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01001764 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001765 continue;
1766 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1767 if (ret < 0)
1768 return ret;
1769 ok_count += ret;
1770 }
1771#endif
1772 return ok_count;
1773}
Jiri Pirko717503b2017-10-11 09:41:09 +02001774
Jiri Pirko208c0f42017-10-19 15:50:32 +02001775int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1776 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02001777{
David S. Miller9a99dc12018-06-06 13:55:47 -04001778 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001779 int ret;
1780
David S. Miller9a99dc12018-06-06 13:55:47 -04001781 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1782 if (ret < 0)
1783 return ret;
1784 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001785
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03001786 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04001787 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001788 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1789 if (ret < 0)
1790 return ret;
1791 ok_count += ret;
1792
1793 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02001794}
1795EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001796
Jiri Pirko48617382018-01-17 11:46:46 +01001797static __net_init int tcf_net_init(struct net *net)
1798{
1799 struct tcf_net *tn = net_generic(net, tcf_net_id);
1800
1801 idr_init(&tn->idr);
1802 return 0;
1803}
1804
1805static void __net_exit tcf_net_exit(struct net *net)
1806{
1807 struct tcf_net *tn = net_generic(net, tcf_net_id);
1808
1809 idr_destroy(&tn->idr);
1810}
1811
1812static struct pernet_operations tcf_net_ops = {
1813 .init = tcf_net_init,
1814 .exit = tcf_net_exit,
1815 .id = &tcf_net_id,
1816 .size = sizeof(struct tcf_net),
1817};
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819static int __init tc_filter_init(void)
1820{
Jiri Pirko48617382018-01-17 11:46:46 +01001821 int err;
1822
Cong Wang7aa00452017-10-26 18:24:28 -07001823 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1824 if (!tc_filter_wq)
1825 return -ENOMEM;
1826
Jiri Pirko48617382018-01-17 11:46:46 +01001827 err = register_pernet_subsys(&tcf_net_ops);
1828 if (err)
1829 goto err_register_pernet_subsys;
1830
Vlad Buslovc431f892018-05-31 09:52:53 +03001831 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
1832 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
1833 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001834 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01001837
1838err_register_pernet_subsys:
1839 destroy_workqueue(tc_filter_wq);
1840 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843subsys_initcall(tc_filter_init);