blob: f74513a7c7a8ed179bfbeabb17fe60dd2f9b6eb2 [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,
280 enum tc_block_command command)
281{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200282 struct tc_block_offload bo = {};
283
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200284 bo.command = command;
285 bo.binder_type = ei->binder_type;
286 bo.block = block;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100287 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200288}
289
Jiri Pirkocaa72602018-01-17 11:46:50 +0100290static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
291 struct tcf_block_ext_info *ei)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200292{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100293 struct net_device *dev = q->dev_queue->dev;
294 int err;
295
296 if (!dev->netdev_ops->ndo_setup_tc)
297 goto no_offload_dev_inc;
298
299 /* If tc offload feature is disabled and the block we try to bind
300 * to already has some offloaded filters, forbid to bind.
301 */
302 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block))
303 return -EOPNOTSUPP;
304
305 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND);
306 if (err == -EOPNOTSUPP)
307 goto no_offload_dev_inc;
308 return err;
309
310no_offload_dev_inc:
311 if (tcf_block_offload_in_use(block))
312 return -EOPNOTSUPP;
313 block->nooffloaddevcnt++;
314 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200315}
316
317static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
318 struct tcf_block_ext_info *ei)
319{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100320 struct net_device *dev = q->dev_queue->dev;
321 int err;
322
323 if (!dev->netdev_ops->ndo_setup_tc)
324 goto no_offload_dev_dec;
325 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND);
326 if (err == -EOPNOTSUPP)
327 goto no_offload_dev_dec;
328 return;
329
330no_offload_dev_dec:
331 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200332}
333
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100334static int
335tcf_chain_head_change_cb_add(struct tcf_chain *chain,
336 struct tcf_block_ext_info *ei,
337 struct netlink_ext_ack *extack)
338{
339 struct tcf_filter_chain_list_item *item;
340
341 item = kmalloc(sizeof(*item), GFP_KERNEL);
342 if (!item) {
343 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
344 return -ENOMEM;
345 }
346 item->chain_head_change = ei->chain_head_change;
347 item->chain_head_change_priv = ei->chain_head_change_priv;
348 if (chain->filter_chain)
349 tcf_chain_head_change_item(item, chain->filter_chain);
350 list_add(&item->list, &chain->filter_chain_list);
351 return 0;
352}
353
354static void
355tcf_chain_head_change_cb_del(struct tcf_chain *chain,
356 struct tcf_block_ext_info *ei)
357{
358 struct tcf_filter_chain_list_item *item;
359
360 list_for_each_entry(item, &chain->filter_chain_list, list) {
361 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
362 (item->chain_head_change == ei->chain_head_change &&
363 item->chain_head_change_priv == ei->chain_head_change_priv)) {
364 tcf_chain_head_change_item(item, NULL);
365 list_del(&item->list);
366 kfree(item);
367 return;
368 }
369 }
370 WARN_ON(1);
371}
372
Jiri Pirko48617382018-01-17 11:46:46 +0100373struct tcf_net {
374 struct idr idr;
375};
376
377static unsigned int tcf_net_id;
378
379static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100380 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100381{
Jiri Pirko48617382018-01-17 11:46:46 +0100382 struct tcf_net *tn = net_generic(net, tcf_net_id);
Jiri Pirko48617382018-01-17 11:46:46 +0100383
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100384 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
385 GFP_KERNEL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100386}
387
Jiri Pirko48617382018-01-17 11:46:46 +0100388static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200389{
Jiri Pirko48617382018-01-17 11:46:46 +0100390 struct tcf_net *tn = net_generic(net, tcf_net_id);
391
Matthew Wilcox9c160942017-11-28 09:48:43 -0500392 idr_remove(&tn->idr, block->index);
Jiri Pirko48617382018-01-17 11:46:46 +0100393}
394
395static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100396 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100397 struct netlink_ext_ack *extack)
398{
399 struct tcf_block *block;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200400 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200401 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200402
Jiri Pirko48617382018-01-17 11:46:46 +0100403 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500404 if (!block) {
405 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100406 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500407 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200408 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200409 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100410 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200411
Jiri Pirko5bc17012017-05-17 11:08:01 +0200412 /* Create chain 0 by default, it has to be always present. */
413 chain = tcf_chain_create(block, 0);
414 if (!chain) {
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500415 NL_SET_ERR_MSG(extack, "Failed to create new tcf chain");
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200416 err = -ENOMEM;
417 goto err_chain_create;
418 }
Jiri Pirko48617382018-01-17 11:46:46 +0100419 block->refcnt = 1;
420 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100421 block->index = block_index;
422
423 /* Don't store q pointer for blocks which are shared */
424 if (!tcf_block_shared(block))
425 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100426 return block;
427
428err_chain_create:
429 kfree(block);
430 return ERR_PTR(err);
431}
432
433static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
434{
435 struct tcf_net *tn = net_generic(net, tcf_net_id);
436
Matthew Wilcox322d8842017-11-28 10:01:24 -0500437 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100438}
439
Vlad Buslovc431f892018-05-31 09:52:53 +0300440/* Find tcf block.
441 * Set q, parent, cl when appropriate.
442 */
443
444static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
445 u32 *parent, unsigned long *cl,
446 int ifindex, u32 block_index,
447 struct netlink_ext_ack *extack)
448{
449 struct tcf_block *block;
450
451 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
452 block = tcf_block_lookup(net, block_index);
453 if (!block) {
454 NL_SET_ERR_MSG(extack, "Block of given index was not found");
455 return ERR_PTR(-EINVAL);
456 }
457 } else {
458 const struct Qdisc_class_ops *cops;
459 struct net_device *dev;
460
461 /* Find link */
462 dev = __dev_get_by_index(net, ifindex);
463 if (!dev)
464 return ERR_PTR(-ENODEV);
465
466 /* Find qdisc */
467 if (!*parent) {
468 *q = dev->qdisc;
469 *parent = (*q)->handle;
470 } else {
471 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
472 if (!*q) {
473 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
474 return ERR_PTR(-EINVAL);
475 }
476 }
477
478 /* Is it classful? */
479 cops = (*q)->ops->cl_ops;
480 if (!cops) {
481 NL_SET_ERR_MSG(extack, "Qdisc not classful");
482 return ERR_PTR(-EINVAL);
483 }
484
485 if (!cops->tcf_block) {
486 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
487 return ERR_PTR(-EOPNOTSUPP);
488 }
489
490 /* Do we search for filter, attached to class? */
491 if (TC_H_MIN(*parent)) {
492 *cl = cops->find(*q, *parent);
493 if (*cl == 0) {
494 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
495 return ERR_PTR(-ENOENT);
496 }
497 }
498
499 /* And the last stroke */
500 block = cops->tcf_block(*q, *cl, extack);
501 if (!block)
502 return ERR_PTR(-EINVAL);
503 if (tcf_block_shared(block)) {
504 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
505 return ERR_PTR(-EOPNOTSUPP);
506 }
507 }
508
509 return block;
510}
511
Jiri Pirko48617382018-01-17 11:46:46 +0100512static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
513{
514 return list_first_entry(&block->chain_list, struct tcf_chain, list);
515}
516
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100517struct tcf_block_owner_item {
518 struct list_head list;
519 struct Qdisc *q;
520 enum tcf_block_binder_type binder_type;
521};
522
523static void
524tcf_block_owner_netif_keep_dst(struct tcf_block *block,
525 struct Qdisc *q,
526 enum tcf_block_binder_type binder_type)
527{
528 if (block->keep_dst &&
529 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
530 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
531 netif_keep_dst(qdisc_dev(q));
532}
533
534void tcf_block_netif_keep_dst(struct tcf_block *block)
535{
536 struct tcf_block_owner_item *item;
537
538 block->keep_dst = true;
539 list_for_each_entry(item, &block->owner_list, list)
540 tcf_block_owner_netif_keep_dst(block, item->q,
541 item->binder_type);
542}
543EXPORT_SYMBOL(tcf_block_netif_keep_dst);
544
545static int tcf_block_owner_add(struct tcf_block *block,
546 struct Qdisc *q,
547 enum tcf_block_binder_type binder_type)
548{
549 struct tcf_block_owner_item *item;
550
551 item = kmalloc(sizeof(*item), GFP_KERNEL);
552 if (!item)
553 return -ENOMEM;
554 item->q = q;
555 item->binder_type = binder_type;
556 list_add(&item->list, &block->owner_list);
557 return 0;
558}
559
560static void tcf_block_owner_del(struct tcf_block *block,
561 struct Qdisc *q,
562 enum tcf_block_binder_type binder_type)
563{
564 struct tcf_block_owner_item *item;
565
566 list_for_each_entry(item, &block->owner_list, list) {
567 if (item->q == q && item->binder_type == binder_type) {
568 list_del(&item->list);
569 kfree(item);
570 return;
571 }
572 }
573 WARN_ON(1);
574}
575
Jiri Pirko48617382018-01-17 11:46:46 +0100576int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
577 struct tcf_block_ext_info *ei,
578 struct netlink_ext_ack *extack)
579{
580 struct net *net = qdisc_net(q);
581 struct tcf_block *block = NULL;
582 bool created = false;
583 int err;
584
585 if (ei->block_index) {
586 /* block_index not 0 means the shared block is requested */
587 block = tcf_block_lookup(net, ei->block_index);
588 if (block)
589 block->refcnt++;
590 }
591
592 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100593 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100594 if (IS_ERR(block))
595 return PTR_ERR(block);
596 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100597 if (tcf_block_shared(block)) {
598 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100599 if (err)
600 goto err_block_insert;
601 }
602 }
603
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100604 err = tcf_block_owner_add(block, q, ei->binder_type);
605 if (err)
606 goto err_block_owner_add;
607
608 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
609
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100610 err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block),
611 ei, extack);
612 if (err)
613 goto err_chain_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100614
615 err = tcf_block_offload_bind(block, q, ei);
616 if (err)
617 goto err_block_offload_bind;
618
Jiri Pirko6529eab2017-05-17 11:07:55 +0200619 *p_block = block;
620 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200621
Jiri Pirkocaa72602018-01-17 11:46:50 +0100622err_block_offload_bind:
623 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100624err_chain_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100625 tcf_block_owner_del(block, q, ei->binder_type);
626err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100627 if (created) {
628 if (tcf_block_shared(block))
629 tcf_block_remove(block, net);
630err_block_insert:
631 kfree(tcf_block_chain_zero(block));
632 kfree(block);
633 } else {
634 block->refcnt--;
635 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200636 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200637}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200638EXPORT_SYMBOL(tcf_block_get_ext);
639
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100640static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
641{
642 struct tcf_proto __rcu **p_filter_chain = priv;
643
644 rcu_assign_pointer(*p_filter_chain, tp_head);
645}
646
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200647int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500648 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
649 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200650{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100651 struct tcf_block_ext_info ei = {
652 .chain_head_change = tcf_chain_head_change_dflt,
653 .chain_head_change_priv = p_filter_chain,
654 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200655
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100656 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500657 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200658}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200659EXPORT_SYMBOL(tcf_block_get);
660
Cong Wang7aa00452017-10-26 18:24:28 -0700661/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100662 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700663 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100664void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900665 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700666{
Cong Wangefbf7892017-12-04 10:48:18 -0800667 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700668
David S. Millerc30abd52017-12-16 22:11:55 -0500669 if (!block)
670 return;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100671 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100672 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100673
Jiri Pirko48617382018-01-17 11:46:46 +0100674 if (--block->refcnt == 0) {
675 if (tcf_block_shared(block))
676 tcf_block_remove(block, block->net);
677
678 /* Hold a refcnt for all chains, so that they don't disappear
679 * while we are iterating.
680 */
681 list_for_each_entry(chain, &block->chain_list, list)
682 tcf_chain_hold(chain);
683
684 list_for_each_entry(chain, &block->chain_list, list)
685 tcf_chain_flush(chain);
686 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700687
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100688 tcf_block_offload_unbind(block, q, ei);
689
Jiri Pirko48617382018-01-17 11:46:46 +0100690 if (block->refcnt == 0) {
691 /* At this point, all the chains should have refcnt >= 1. */
692 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
693 tcf_chain_put(chain);
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100694
Jiri Pirko48617382018-01-17 11:46:46 +0100695 /* Finally, put chain 0 and allow block to be freed. */
696 tcf_chain_put(tcf_block_chain_zero(block));
697 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200698}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200699EXPORT_SYMBOL(tcf_block_put_ext);
700
701void tcf_block_put(struct tcf_block *block)
702{
703 struct tcf_block_ext_info ei = {0, };
704
Jiri Pirko4853f122017-12-21 13:13:59 +0100705 if (!block)
706 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100707 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200708}
David S. Millere1ea2f92017-10-30 14:10:01 +0900709
Jiri Pirko6529eab2017-05-17 11:07:55 +0200710EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100711
Jiri Pirkoacb67442017-10-19 15:50:31 +0200712struct tcf_block_cb {
713 struct list_head list;
714 tc_setup_cb_t *cb;
715 void *cb_ident;
716 void *cb_priv;
717 unsigned int refcnt;
718};
719
720void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
721{
722 return block_cb->cb_priv;
723}
724EXPORT_SYMBOL(tcf_block_cb_priv);
725
726struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
727 tc_setup_cb_t *cb, void *cb_ident)
728{ struct tcf_block_cb *block_cb;
729
730 list_for_each_entry(block_cb, &block->cb_list, list)
731 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
732 return block_cb;
733 return NULL;
734}
735EXPORT_SYMBOL(tcf_block_cb_lookup);
736
737void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
738{
739 block_cb->refcnt++;
740}
741EXPORT_SYMBOL(tcf_block_cb_incref);
742
743unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
744{
745 return --block_cb->refcnt;
746}
747EXPORT_SYMBOL(tcf_block_cb_decref);
748
749struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
750 tc_setup_cb_t *cb, void *cb_ident,
751 void *cb_priv)
752{
753 struct tcf_block_cb *block_cb;
754
Jiri Pirkocaa72602018-01-17 11:46:50 +0100755 /* At this point, playback of previous block cb calls is not supported,
756 * so forbid to register to block which already has some offloaded
757 * filters present.
758 */
759 if (tcf_block_offload_in_use(block))
760 return ERR_PTR(-EOPNOTSUPP);
761
Jiri Pirkoacb67442017-10-19 15:50:31 +0200762 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
763 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100764 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200765 block_cb->cb = cb;
766 block_cb->cb_ident = cb_ident;
767 block_cb->cb_priv = cb_priv;
768 list_add(&block_cb->list, &block->cb_list);
769 return block_cb;
770}
771EXPORT_SYMBOL(__tcf_block_cb_register);
772
773int tcf_block_cb_register(struct tcf_block *block,
774 tc_setup_cb_t *cb, void *cb_ident,
775 void *cb_priv)
776{
777 struct tcf_block_cb *block_cb;
778
779 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100780 return IS_ERR(block_cb) ? PTR_ERR(block_cb) : 0;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200781}
782EXPORT_SYMBOL(tcf_block_cb_register);
783
784void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
785{
786 list_del(&block_cb->list);
787 kfree(block_cb);
788}
789EXPORT_SYMBOL(__tcf_block_cb_unregister);
790
791void tcf_block_cb_unregister(struct tcf_block *block,
792 tc_setup_cb_t *cb, void *cb_ident)
793{
794 struct tcf_block_cb *block_cb;
795
796 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
797 if (!block_cb)
798 return;
799 __tcf_block_cb_unregister(block_cb);
800}
801EXPORT_SYMBOL(tcf_block_cb_unregister);
802
803static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
804 void *type_data, bool err_stop)
805{
806 struct tcf_block_cb *block_cb;
807 int ok_count = 0;
808 int err;
809
David S. Miller9a99dc12018-06-06 13:55:47 -0400810 /* Make sure all netdevs sharing this block are offload-capable. */
811 if (block->nooffloaddevcnt && err_stop)
812 return -EOPNOTSUPP;
813
Jiri Pirkoacb67442017-10-19 15:50:31 +0200814 list_for_each_entry(block_cb, &block->cb_list, list) {
815 err = block_cb->cb(type, type_data, block_cb->cb_priv);
816 if (err) {
817 if (err_stop)
818 return err;
819 } else {
820 ok_count++;
821 }
822 }
823 return ok_count;
824}
825
Jiri Pirko87d83092017-05-17 11:07:54 +0200826/* Main classifier routine: scans classifier chain attached
827 * to this qdisc, (optionally) tests for protocol and asks
828 * specific classifiers.
829 */
830int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
831 struct tcf_result *res, bool compat_mode)
832{
833 __be16 protocol = tc_skb_protocol(skb);
834#ifdef CONFIG_NET_CLS_ACT
835 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200836 const struct tcf_proto *orig_tp = tp;
837 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200838 int limit = 0;
839
840reclassify:
841#endif
842 for (; tp; tp = rcu_dereference_bh(tp->next)) {
843 int err;
844
845 if (tp->protocol != protocol &&
846 tp->protocol != htons(ETH_P_ALL))
847 continue;
848
849 err = tp->classify(skb, tp, res);
850#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200851 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200852 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200853 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200854 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200855 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200856 goto reset;
857 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200858#endif
859 if (err >= 0)
860 return err;
861 }
862
863 return TC_ACT_UNSPEC; /* signal: continue lookup */
864#ifdef CONFIG_NET_CLS_ACT
865reset:
866 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100867 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
868 tp->chain->block->index,
869 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200870 ntohs(tp->protocol));
871 return TC_ACT_SHOT;
872 }
873
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200874 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200875 protocol = tc_skb_protocol(skb);
876 goto reclassify;
877#endif
878}
879EXPORT_SYMBOL(tcf_classify);
880
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200881struct tcf_chain_info {
882 struct tcf_proto __rcu **pprev;
883 struct tcf_proto __rcu *next;
884};
885
886static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
887{
888 return rtnl_dereference(*chain_info->pprev);
889}
890
891static void tcf_chain_tp_insert(struct tcf_chain *chain,
892 struct tcf_chain_info *chain_info,
893 struct tcf_proto *tp)
894{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100895 if (*chain_info->pprev == chain->filter_chain)
896 tcf_chain_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200897 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
898 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700899 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200900}
901
902static void tcf_chain_tp_remove(struct tcf_chain *chain,
903 struct tcf_chain_info *chain_info,
904 struct tcf_proto *tp)
905{
906 struct tcf_proto *next = rtnl_dereference(chain_info->next);
907
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100908 if (tp == chain->filter_chain)
909 tcf_chain_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200910 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700911 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200912}
913
914static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
915 struct tcf_chain_info *chain_info,
916 u32 protocol, u32 prio,
917 bool prio_allocate)
918{
919 struct tcf_proto **pprev;
920 struct tcf_proto *tp;
921
922 /* Check the chain for existence of proto-tcf with this priority */
923 for (pprev = &chain->filter_chain;
924 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
925 if (tp->prio >= prio) {
926 if (tp->prio == prio) {
927 if (prio_allocate ||
928 (tp->protocol != protocol && protocol))
929 return ERR_PTR(-EINVAL);
930 } else {
931 tp = NULL;
932 }
933 break;
934 }
935 }
936 chain_info->pprev = pprev;
937 chain_info->next = tp ? tp->next : NULL;
938 return tp;
939}
940
WANG Cong71203712017-08-07 15:26:50 -0700941static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100942 struct tcf_proto *tp, struct tcf_block *block,
943 struct Qdisc *q, u32 parent, void *fh,
944 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700945{
946 struct tcmsg *tcm;
947 struct nlmsghdr *nlh;
948 unsigned char *b = skb_tail_pointer(skb);
949
950 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
951 if (!nlh)
952 goto out_nlmsg_trim;
953 tcm = nlmsg_data(nlh);
954 tcm->tcm_family = AF_UNSPEC;
955 tcm->tcm__pad1 = 0;
956 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100957 if (q) {
958 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
959 tcm->tcm_parent = parent;
960 } else {
961 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
962 tcm->tcm_block_index = block->index;
963 }
WANG Cong71203712017-08-07 15:26:50 -0700964 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
965 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
966 goto nla_put_failure;
967 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
968 goto nla_put_failure;
969 if (!fh) {
970 tcm->tcm_handle = 0;
971 } else {
972 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
973 goto nla_put_failure;
974 }
975 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
976 return skb->len;
977
978out_nlmsg_trim:
979nla_put_failure:
980 nlmsg_trim(skb, b);
981 return -1;
982}
983
984static int tfilter_notify(struct net *net, struct sk_buff *oskb,
985 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100986 struct tcf_block *block, struct Qdisc *q,
987 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -0700988{
989 struct sk_buff *skb;
990 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
991
992 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
993 if (!skb)
994 return -ENOBUFS;
995
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100996 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
997 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -0700998 kfree_skb(skb);
999 return -EINVAL;
1000 }
1001
1002 if (unicast)
1003 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1004
1005 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1006 n->nlmsg_flags & NLM_F_ECHO);
1007}
1008
1009static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1010 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001011 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001012 u32 parent, void *fh, bool unicast, bool *last,
1013 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001014{
1015 struct sk_buff *skb;
1016 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1017 int err;
1018
1019 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1020 if (!skb)
1021 return -ENOBUFS;
1022
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001023 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1024 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001025 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001026 kfree_skb(skb);
1027 return -EINVAL;
1028 }
1029
Alexander Aring571acf22018-01-18 11:20:53 -05001030 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001031 if (err) {
1032 kfree_skb(skb);
1033 return err;
1034 }
1035
1036 if (unicast)
1037 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1038
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001039 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1040 n->nlmsg_flags & NLM_F_ECHO);
1041 if (err < 0)
1042 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1043 return err;
WANG Cong71203712017-08-07 15:26:50 -07001044}
1045
1046static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001047 struct tcf_block *block, struct Qdisc *q,
1048 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001049 struct tcf_chain *chain, int event)
1050{
1051 struct tcf_proto *tp;
1052
1053 for (tp = rtnl_dereference(chain->filter_chain);
1054 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001055 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001056 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001057}
1058
Vlad Buslovc431f892018-05-31 09:52:53 +03001059static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001060 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001062 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001063 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct tcmsg *t;
1065 u32 protocol;
1066 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001067 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001069 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001070 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001071 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001072 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001073 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001076 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001078 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Vlad Buslovc431f892018-05-31 09:52:53 +03001080 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001081 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001084 tp_created = 0;
1085
David Ahernc21ef3e2017-04-16 09:48:24 -07001086 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001087 if (err < 0)
1088 return err;
1089
David S. Miller942b8162012-06-26 21:48:50 -07001090 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 protocol = TC_H_MIN(t->tcm_info);
1092 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001093 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 parent = t->tcm_parent;
1095 cl = 0;
1096
1097 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001098 /* If no priority is provided by the user,
1099 * we allocate one.
1100 */
1101 if (n->nlmsg_flags & NLM_F_CREATE) {
1102 prio = TC_H_MAKE(0x80000000U, 0U);
1103 prio_allocate = true;
1104 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001105 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109
1110 /* Find head of filter chain. */
1111
Vlad Buslovc431f892018-05-31 09:52:53 +03001112 block = tcf_block_find(net, &q, &parent, &cl,
1113 t->tcm_ifindex, t->tcm_block_index, extack);
1114 if (IS_ERR(block)) {
1115 err = PTR_ERR(block);
1116 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001117 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001118
1119 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1120 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001121 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001122 err = -EINVAL;
1123 goto errout;
1124 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001125 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001126 if (!chain) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001127 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001128 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001129 goto errout;
1130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001132 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1133 prio, prio_allocate);
1134 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001135 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001136 err = PTR_ERR(tp);
1137 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
1140 if (tp == NULL) {
1141 /* Proto-tcf does not exist, create new one */
1142
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001143 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001144 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001145 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Vlad Buslovc431f892018-05-31 09:52:53 +03001149 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001150 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 +01001151 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001155 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001156 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Jiri Pirko33a48922017-02-09 14:38:57 +01001158 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001159 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001160 if (IS_ERR(tp)) {
1161 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 goto errout;
1163 }
Minoru Usui12186be2009-06-02 02:17:34 -07001164 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001165 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001166 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001167 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 fh = tp->ops->get(tp, t->tcm_handle);
1172
WANG Cong8113c092017-08-04 21:31:43 -07001173 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001174 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001175 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 +01001176 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001178 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001179 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1180 NL_SET_ERR_MSG(extack, "Filter already exists");
1181 err = -EEXIST;
1182 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001185 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001186 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1187 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001188 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001189 if (tp_created)
1190 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001191 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001192 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001193 } else {
1194 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001195 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001199 if (chain)
1200 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (err == -EAGAIN)
1202 /* Replay the request. */
1203 goto replay;
1204 return err;
1205}
1206
Vlad Buslovc431f892018-05-31 09:52:53 +03001207static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1208 struct netlink_ext_ack *extack)
1209{
1210 struct net *net = sock_net(skb->sk);
1211 struct nlattr *tca[TCA_MAX + 1];
1212 struct tcmsg *t;
1213 u32 protocol;
1214 u32 prio;
1215 u32 parent;
1216 u32 chain_index;
1217 struct Qdisc *q = NULL;
1218 struct tcf_chain_info chain_info;
1219 struct tcf_chain *chain = NULL;
1220 struct tcf_block *block;
1221 struct tcf_proto *tp = NULL;
1222 unsigned long cl = 0;
1223 void *fh = NULL;
1224 int err;
1225
1226 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1227 return -EPERM;
1228
1229 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1230 if (err < 0)
1231 return err;
1232
1233 t = nlmsg_data(n);
1234 protocol = TC_H_MIN(t->tcm_info);
1235 prio = TC_H_MAJ(t->tcm_info);
1236 parent = t->tcm_parent;
1237
1238 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1239 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1240 return -ENOENT;
1241 }
1242
1243 /* Find head of filter chain. */
1244
1245 block = tcf_block_find(net, &q, &parent, &cl,
1246 t->tcm_ifindex, t->tcm_block_index, extack);
1247 if (IS_ERR(block)) {
1248 err = PTR_ERR(block);
1249 goto errout;
1250 }
1251
1252 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1253 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1254 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1255 err = -EINVAL;
1256 goto errout;
1257 }
1258 chain = tcf_chain_get(block, chain_index, false);
1259 if (!chain) {
1260 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1261 err = -EINVAL;
1262 goto errout;
1263 }
1264
1265 if (prio == 0) {
1266 tfilter_notify_chain(net, skb, block, q, parent, n,
1267 chain, RTM_DELTFILTER);
1268 tcf_chain_flush(chain);
1269 err = 0;
1270 goto errout;
1271 }
1272
1273 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1274 prio, false);
1275 if (!tp || IS_ERR(tp)) {
1276 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001277 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001278 goto errout;
1279 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1280 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1281 err = -EINVAL;
1282 goto errout;
1283 }
1284
1285 fh = tp->ops->get(tp, t->tcm_handle);
1286
1287 if (!fh) {
1288 if (t->tcm_handle == 0) {
1289 tcf_chain_tp_remove(chain, &chain_info, tp);
1290 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1291 RTM_DELTFILTER, false);
1292 tcf_proto_destroy(tp, extack);
1293 err = 0;
1294 } else {
1295 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1296 err = -ENOENT;
1297 }
1298 } else {
1299 bool last;
1300
1301 err = tfilter_del_notify(net, skb, n, tp, block,
1302 q, parent, fh, false, &last,
1303 extack);
1304 if (err)
1305 goto errout;
1306 if (last) {
1307 tcf_chain_tp_remove(chain, &chain_info, tp);
1308 tcf_proto_destroy(tp, extack);
1309 }
1310 }
1311
1312errout:
1313 if (chain)
1314 tcf_chain_put(chain);
1315 return err;
1316}
1317
1318static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1319 struct netlink_ext_ack *extack)
1320{
1321 struct net *net = sock_net(skb->sk);
1322 struct nlattr *tca[TCA_MAX + 1];
1323 struct tcmsg *t;
1324 u32 protocol;
1325 u32 prio;
1326 u32 parent;
1327 u32 chain_index;
1328 struct Qdisc *q = NULL;
1329 struct tcf_chain_info chain_info;
1330 struct tcf_chain *chain = NULL;
1331 struct tcf_block *block;
1332 struct tcf_proto *tp = NULL;
1333 unsigned long cl = 0;
1334 void *fh = NULL;
1335 int err;
1336
1337 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
1338 if (err < 0)
1339 return err;
1340
1341 t = nlmsg_data(n);
1342 protocol = TC_H_MIN(t->tcm_info);
1343 prio = TC_H_MAJ(t->tcm_info);
1344 parent = t->tcm_parent;
1345
1346 if (prio == 0) {
1347 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1348 return -ENOENT;
1349 }
1350
1351 /* Find head of filter chain. */
1352
1353 block = tcf_block_find(net, &q, &parent, &cl,
1354 t->tcm_ifindex, t->tcm_block_index, extack);
1355 if (IS_ERR(block)) {
1356 err = PTR_ERR(block);
1357 goto errout;
1358 }
1359
1360 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1361 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1362 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1363 err = -EINVAL;
1364 goto errout;
1365 }
1366 chain = tcf_chain_get(block, chain_index, false);
1367 if (!chain) {
1368 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1369 err = -EINVAL;
1370 goto errout;
1371 }
1372
1373 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1374 prio, false);
1375 if (!tp || IS_ERR(tp)) {
1376 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001377 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001378 goto errout;
1379 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1380 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1381 err = -EINVAL;
1382 goto errout;
1383 }
1384
1385 fh = tp->ops->get(tp, t->tcm_handle);
1386
1387 if (!fh) {
1388 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1389 err = -ENOENT;
1390 } else {
1391 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1392 fh, RTM_NEWTFILTER, true);
1393 if (err < 0)
1394 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1395 }
1396
1397errout:
1398 if (chain)
1399 tcf_chain_put(chain);
1400 return err;
1401}
1402
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001403struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct tcf_walker w;
1405 struct sk_buff *skb;
1406 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001407 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001408 struct Qdisc *q;
1409 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410};
1411
WANG Cong8113c092017-08-04 21:31:43 -07001412static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001414 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001415 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001417 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001418 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001419 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1420 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001423static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1424 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001425 long index_start, long *p_index)
1426{
1427 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001428 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001429 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1430 struct tcf_dump_args arg;
1431 struct tcf_proto *tp;
1432
1433 for (tp = rtnl_dereference(chain->filter_chain);
1434 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1435 if (*p_index < index_start)
1436 continue;
1437 if (TC_H_MAJ(tcm->tcm_info) &&
1438 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1439 continue;
1440 if (TC_H_MIN(tcm->tcm_info) &&
1441 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1442 continue;
1443 if (*p_index > index_start)
1444 memset(&cb->args[1], 0,
1445 sizeof(cb->args) - sizeof(cb->args[0]));
1446 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001447 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001448 NETLINK_CB(cb->skb).portid,
1449 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1450 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001451 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001452
1453 cb->args[1] = 1;
1454 }
1455 if (!tp->ops->walk)
1456 continue;
1457 arg.w.fn = tcf_node_dump;
1458 arg.skb = skb;
1459 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001460 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001461 arg.q = q;
1462 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001463 arg.w.stop = 0;
1464 arg.w.skip = cb->args[1] - 1;
1465 arg.w.count = 0;
1466 tp->ops->walk(tp, &arg.w);
1467 cb->args[1] = arg.w.count + 1;
1468 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001469 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001470 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001471 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001472}
1473
Eric Dumazetbd27a872009-11-05 20:57:26 -08001474/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1476{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001477 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001478 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001479 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001480 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001481 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001482 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001483 long index_start;
1484 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001485 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001486 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Hong zhi guo573ce262013-03-27 06:47:04 +00001488 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001490
1491 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1492 if (err)
1493 return err;
1494
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001495 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1496 block = tcf_block_lookup(net, tcm->tcm_block_index);
1497 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001498 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001499 /* If we work with block index, q is NULL and parent value
1500 * will never be used in the following code. The check
1501 * in tcf_fill_node prevents it. However, compiler does not
1502 * see that far, so set parent to zero to silence the warning
1503 * about parent being uninitialized.
1504 */
1505 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001506 } else {
1507 const struct Qdisc_class_ops *cops;
1508 struct net_device *dev;
1509 unsigned long cl = 0;
1510
1511 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1512 if (!dev)
1513 return skb->len;
1514
1515 parent = tcm->tcm_parent;
1516 if (!parent) {
1517 q = dev->qdisc;
1518 parent = q->handle;
1519 } else {
1520 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1521 }
1522 if (!q)
1523 goto out;
1524 cops = q->ops->cl_ops;
1525 if (!cops)
1526 goto out;
1527 if (!cops->tcf_block)
1528 goto out;
1529 if (TC_H_MIN(tcm->tcm_parent)) {
1530 cl = cops->find(q, tcm->tcm_parent);
1531 if (cl == 0)
1532 goto out;
1533 }
1534 block = cops->tcf_block(q, cl, NULL);
1535 if (!block)
1536 goto out;
1537 if (tcf_block_shared(block))
1538 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001541 index_start = cb->args[0];
1542 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001543
1544 list_for_each_entry(chain, &block->chain_list, list) {
1545 if (tca[TCA_CHAIN] &&
1546 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1547 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001548 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001549 index_start, &index)) {
1550 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001551 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001552 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001553 }
1554
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001555 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001558 /* If we did no progress, the error (EMSGSIZE) is real */
1559 if (skb->len == 0 && err)
1560 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return skb->len;
1562}
1563
WANG Cong18d02642014-09-25 10:26:37 -07001564void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001567 LIST_HEAD(actions);
1568
Cong Wang2d132eb2017-10-26 18:24:40 -07001569 ASSERT_RTNL();
WANG Cong22dc13c2016-08-13 22:35:00 -07001570 tcf_exts_to_list(exts, &actions);
1571 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1572 kfree(exts->actions);
1573 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574#endif
1575}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001576EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00001578int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05001579 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
1580 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582#ifdef CONFIG_NET_CLS_ACT
1583 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05001585 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
WANG Cong5da57f42013-12-15 20:15:07 -08001587 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001588 act = tcf_action_init_1(net, tp, tb[exts->police],
1589 rate_tlv, "police", ovr,
Alexander Aringaea0d722018-02-15 10:54:54 -05001590 TCA_ACT_BIND, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001591 if (IS_ERR(act))
1592 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
WANG Cong33be6272013-12-15 20:15:05 -08001594 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07001595 exts->actions[0] = act;
1596 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08001597 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001598 LIST_HEAD(actions);
1599 int err, i = 0;
1600
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001601 err = tcf_action_init(net, tp, tb[exts->action],
1602 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Roman Mashakd04e6992018-03-08 16:59:17 -05001603 &actions, &attr_size, extack);
WANG Cong33be6272013-12-15 20:15:05 -08001604 if (err)
1605 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -07001606 list_for_each_entry(act, &actions, list)
1607 exts->actions[i++] = act;
1608 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
Cong Wange4b95c42017-11-06 13:47:19 -08001610 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#else
WANG Cong5da57f42013-12-15 20:15:07 -08001613 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05001614 (exts->police && tb[exts->police])) {
1615 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05001617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618#endif
1619
1620 return 0;
1621}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001622EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001624void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001627 struct tcf_exts old = *dst;
1628
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001629 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07001630 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631#endif
1632}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001633EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
WANG Cong22dc13c2016-08-13 22:35:00 -07001635#ifdef CONFIG_NET_CLS_ACT
1636static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1637{
1638 if (exts->nr_actions == 0)
1639 return NULL;
1640 else
1641 return exts->actions[0];
1642}
1643#endif
WANG Cong33be6272013-12-15 20:15:05 -08001644
WANG Cong5da57f42013-12-15 20:15:07 -08001645int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07001648 struct nlattr *nest;
1649
Jiri Pirko978dfd82017-08-04 14:29:03 +02001650 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /*
1652 * again for backward compatible mode - we want
1653 * to work with both old and new modes of entering
1654 * tc data even if iproute2 was newer - jhs
1655 */
WANG Cong33be6272013-12-15 20:15:05 -08001656 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001657 LIST_HEAD(actions);
1658
WANG Cong5da57f42013-12-15 20:15:07 -08001659 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001660 if (nest == NULL)
1661 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07001662
1663 tcf_exts_to_list(exts, &actions);
1664 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001665 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001666 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08001667 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08001668 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08001669 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001670 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001671 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08001672 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001673 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001674 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001678
1679nla_put_failure:
1680 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001682#else
1683 return 0;
1684#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001686EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001688
WANG Cong5da57f42013-12-15 20:15:07 -08001689int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001692 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001693 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001694 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695#endif
1696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001698EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Jiri Pirko717503b2017-10-11 09:41:09 +02001700static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1701 enum tc_setup_type type,
1702 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001703{
1704 int ok_count = 0;
1705#ifdef CONFIG_NET_CLS_ACT
1706 const struct tc_action *a;
1707 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001708 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001709
1710 if (!tcf_exts_has_actions(exts))
1711 return 0;
1712
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001713 for (i = 0; i < exts->nr_actions; i++) {
1714 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001715 if (!a->ops->get_dev)
1716 continue;
1717 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01001718 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001719 continue;
1720 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1721 if (ret < 0)
1722 return ret;
1723 ok_count += ret;
1724 }
1725#endif
1726 return ok_count;
1727}
Jiri Pirko717503b2017-10-11 09:41:09 +02001728
Jiri Pirko208c0f42017-10-19 15:50:32 +02001729int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1730 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02001731{
David S. Miller9a99dc12018-06-06 13:55:47 -04001732 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001733 int ret;
1734
David S. Miller9a99dc12018-06-06 13:55:47 -04001735 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1736 if (ret < 0)
1737 return ret;
1738 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001739
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03001740 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04001741 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02001742 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1743 if (ret < 0)
1744 return ret;
1745 ok_count += ret;
1746
1747 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02001748}
1749EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001750
Jiri Pirko48617382018-01-17 11:46:46 +01001751static __net_init int tcf_net_init(struct net *net)
1752{
1753 struct tcf_net *tn = net_generic(net, tcf_net_id);
1754
1755 idr_init(&tn->idr);
1756 return 0;
1757}
1758
1759static void __net_exit tcf_net_exit(struct net *net)
1760{
1761 struct tcf_net *tn = net_generic(net, tcf_net_id);
1762
1763 idr_destroy(&tn->idr);
1764}
1765
1766static struct pernet_operations tcf_net_ops = {
1767 .init = tcf_net_init,
1768 .exit = tcf_net_exit,
1769 .id = &tcf_net_id,
1770 .size = sizeof(struct tcf_net),
1771};
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773static int __init tc_filter_init(void)
1774{
Jiri Pirko48617382018-01-17 11:46:46 +01001775 int err;
1776
Cong Wang7aa00452017-10-26 18:24:28 -07001777 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1778 if (!tc_filter_wq)
1779 return -ENOMEM;
1780
Jiri Pirko48617382018-01-17 11:46:46 +01001781 err = register_pernet_subsys(&tcf_net_ops);
1782 if (err)
1783 goto err_register_pernet_subsys;
1784
Vlad Buslovc431f892018-05-31 09:52:53 +03001785 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
1786 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
1787 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001788 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01001791
1792err_register_pernet_subsys:
1793 destroy_workqueue(tc_filter_wq);
1794 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
1797subsys_initcall(tc_filter_init);