blob: e217ebc693f8c832362a99c51b6543ee6a72b0c4 [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
Davide Carattie3314732018-10-10 22:00:58 +020034extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1];
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* The list of all installed classifier types */
WANG Cong36272872013-12-15 20:15:11 -080037static LIST_HEAD(tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* Protects list of registered TC modules. It is pure SMP lock. */
40static DEFINE_RWLOCK(cls_mod_lock);
41
42/* Find classifier type by string name */
43
Jiri Pirkof34e8bf2018-07-23 09:23:04 +020044static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Eric Dumazetdcd76082013-12-20 10:04:18 -080046 const struct tcf_proto_ops *t, *res = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 if (kind) {
49 read_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080050 list_for_each_entry(t, &tcf_proto_base, head) {
Jiri Pirko33a48922017-02-09 14:38:57 +010051 if (strcmp(kind, t->kind) == 0) {
Eric Dumazetdcd76082013-12-20 10:04:18 -080052 if (try_module_get(t->owner))
53 res = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 break;
55 }
56 }
57 read_unlock(&cls_mod_lock);
58 }
Eric Dumazetdcd76082013-12-20 10:04:18 -080059 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Jiri Pirkof34e8bf2018-07-23 09:23:04 +020062static const struct tcf_proto_ops *
63tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack)
64{
65 const struct tcf_proto_ops *ops;
66
67 ops = __tcf_proto_lookup_ops(kind);
68 if (ops)
69 return ops;
70#ifdef CONFIG_MODULES
71 rtnl_unlock();
72 request_module("cls_%s", kind);
73 rtnl_lock();
74 ops = __tcf_proto_lookup_ops(kind);
75 /* We dropped the RTNL semaphore in order to perform
76 * the module load. So, even if we succeeded in loading
77 * the module we have to replay the request. We indicate
78 * this using -EAGAIN.
79 */
80 if (ops) {
81 module_put(ops->owner);
82 return ERR_PTR(-EAGAIN);
83 }
84#endif
85 NL_SET_ERR_MSG(extack, "TC classifier not found");
86 return ERR_PTR(-ENOENT);
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Register(unregister) new classifier type */
90
91int register_tcf_proto_ops(struct tcf_proto_ops *ops)
92{
WANG Cong36272872013-12-15 20:15:11 -080093 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int rc = -EEXIST;
95
96 write_lock(&cls_mod_lock);
WANG Cong36272872013-12-15 20:15:11 -080097 list_for_each_entry(t, &tcf_proto_base, head)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (!strcmp(ops->kind, t->kind))
99 goto out;
100
WANG Cong36272872013-12-15 20:15:11 -0800101 list_add_tail(&ops->head, &tcf_proto_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 rc = 0;
103out:
104 write_unlock(&cls_mod_lock);
105 return rc;
106}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800107EXPORT_SYMBOL(register_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Cong Wang7aa00452017-10-26 18:24:28 -0700109static struct workqueue_struct *tc_filter_wq;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111int unregister_tcf_proto_ops(struct tcf_proto_ops *ops)
112{
WANG Cong36272872013-12-15 20:15:11 -0800113 struct tcf_proto_ops *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int rc = -ENOENT;
115
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200116 /* Wait for outstanding call_rcu()s, if any, from a
117 * tcf_proto_ops's destroy() handler.
118 */
119 rcu_barrier();
Cong Wang7aa00452017-10-26 18:24:28 -0700120 flush_workqueue(tc_filter_wq);
Daniel Borkmannc78e1742015-05-20 17:13:33 +0200121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 write_lock(&cls_mod_lock);
Eric Dumazetdcd76082013-12-20 10:04:18 -0800123 list_for_each_entry(t, &tcf_proto_base, head) {
124 if (t == ops) {
125 list_del(&t->head);
126 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 break;
Eric Dumazetdcd76082013-12-20 10:04:18 -0800128 }
129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 write_unlock(&cls_mod_lock);
131 return rc;
132}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800133EXPORT_SYMBOL(unregister_tcf_proto_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Cong Wangaaa908f2018-05-23 15:26:53 -0700135bool tcf_queue_work(struct rcu_work *rwork, work_func_t func)
Cong Wang7aa00452017-10-26 18:24:28 -0700136{
Cong Wangaaa908f2018-05-23 15:26:53 -0700137 INIT_RCU_WORK(rwork, func);
138 return queue_rcu_work(tc_filter_wq, rwork);
Cong Wang7aa00452017-10-26 18:24:28 -0700139}
140EXPORT_SYMBOL(tcf_queue_work);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Select new prio value from the range, managed by kernel. */
143
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800144static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800146 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000149 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jiri Pirko79619732017-05-17 11:07:58 +0200151 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Jiri Pirko33a48922017-02-09 14:38:57 +0100154static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500155 u32 prio, struct tcf_chain *chain,
156 struct netlink_ext_ack *extack)
Jiri Pirko33a48922017-02-09 14:38:57 +0100157{
158 struct tcf_proto *tp;
159 int err;
160
161 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
162 if (!tp)
163 return ERR_PTR(-ENOBUFS);
164
Jiri Pirkof34e8bf2018-07-23 09:23:04 +0200165 tp->ops = tcf_proto_lookup_ops(kind, extack);
166 if (IS_ERR(tp->ops)) {
167 err = PTR_ERR(tp->ops);
Jiri Pirkod68d75f2018-05-11 17:45:32 +0200168 goto errout;
Jiri Pirko33a48922017-02-09 14:38:57 +0100169 }
170 tp->classify = tp->ops->classify;
171 tp->protocol = protocol;
172 tp->prio = prio;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200173 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100174
175 err = tp->ops->init(tp);
176 if (err) {
177 module_put(tp->ops->owner);
178 goto errout;
179 }
180 return tp;
181
182errout:
183 kfree(tp);
184 return ERR_PTR(err);
185}
186
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800187static void tcf_proto_destroy(struct tcf_proto *tp,
188 struct netlink_ext_ack *extack)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100189{
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800190 tp->ops->destroy(tp, extack);
WANG Cong763dbf62017-04-19 14:21:21 -0700191 module_put(tp->ops->owner);
192 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100193}
194
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100195struct tcf_filter_chain_list_item {
196 struct list_head list;
197 tcf_chain_head_change_t *chain_head_change;
198 void *chain_head_change_priv;
199};
200
Jiri Pirko5bc17012017-05-17 11:08:01 +0200201static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
202 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200203{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200204 struct tcf_chain *chain;
205
206 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
207 if (!chain)
208 return NULL;
209 list_add_tail(&chain->list, &block->chain_list);
210 chain->block = block;
211 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700212 chain->refcnt = 1;
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200213 if (!chain->index)
214 block->chain0.chain = chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200215 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200216}
217
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100218static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
219 struct tcf_proto *tp_head)
220{
221 if (item->chain_head_change)
222 item->chain_head_change(tp_head, item->chain_head_change_priv);
223}
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200224
225static void tcf_chain0_head_change(struct tcf_chain *chain,
226 struct tcf_proto *tp_head)
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100227{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100228 struct tcf_filter_chain_list_item *item;
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200229 struct tcf_block *block = chain->block;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100230
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200231 if (chain->index)
232 return;
233 list_for_each_entry(item, &block->chain0.filter_chain_list, list)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100234 tcf_chain_head_change_item(item, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100235}
236
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200237static void tcf_chain_destroy(struct tcf_chain *chain)
238{
Cong Wangefbf7892017-12-04 10:48:18 -0800239 struct tcf_block *block = chain->block;
240
Cong Wange2ef7542017-09-11 16:33:31 -0700241 list_del(&chain->list);
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200242 if (!chain->index)
243 block->chain0.chain = NULL;
Cong Wange2ef7542017-09-11 16:33:31 -0700244 kfree(chain);
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200245 if (list_empty(&block->chain_list) && block->refcnt == 0)
Cong Wangefbf7892017-12-04 10:48:18 -0800246 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700247}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200248
Cong Wange2ef7542017-09-11 16:33:31 -0700249static void tcf_chain_hold(struct tcf_chain *chain)
250{
251 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200252}
253
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200254static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain)
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200255{
256 /* In case all the references are action references, this
Jiri Pirko3d32f4c2018-08-01 12:36:55 +0200257 * chain should not be shown to the user.
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200258 */
259 return chain->refcnt == chain->action_refcnt;
260}
261
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200262static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block,
263 u32 chain_index)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200264{
265 struct tcf_chain *chain;
266
267 list_for_each_entry(chain, &block->chain_list, list) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200268 if (chain->index == chain_index)
Cong Wange2ef7542017-09-11 16:33:31 -0700269 return chain;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200270 }
271 return NULL;
272}
273
274static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
275 u32 seq, u16 flags, int event, bool unicast);
276
Jiri Pirko53681402018-08-01 12:36:56 +0200277static struct tcf_chain *__tcf_chain_get(struct tcf_block *block,
278 u32 chain_index, bool create,
279 bool by_act)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200280{
281 struct tcf_chain *chain = tcf_chain_lookup(block, chain_index);
282
283 if (chain) {
284 tcf_chain_hold(chain);
Jiri Pirko53681402018-08-01 12:36:56 +0200285 } else {
286 if (!create)
287 return NULL;
288 chain = tcf_chain_create(block, chain_index);
289 if (!chain)
290 return NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200291 }
Jiri Pirko80532382017-09-06 13:14:19 +0200292
Jiri Pirko53681402018-08-01 12:36:56 +0200293 if (by_act)
294 ++chain->action_refcnt;
295
296 /* Send notification only in case we got the first
297 * non-action reference. Until then, the chain acts only as
298 * a placeholder for actions pointing to it and user ought
299 * not know about them.
300 */
301 if (chain->refcnt - chain->action_refcnt == 1 && !by_act)
302 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
303 RTM_NEWCHAIN, false);
304
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200305 return chain;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200306}
Jiri Pirko53681402018-08-01 12:36:56 +0200307
Jiri Pirko290b1c82018-08-01 12:36:57 +0200308static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
309 bool create)
Jiri Pirko53681402018-08-01 12:36:56 +0200310{
311 return __tcf_chain_get(block, chain_index, create, false);
312}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200313
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200314struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index)
315{
Jiri Pirko53681402018-08-01 12:36:56 +0200316 return __tcf_chain_get(block, chain_index, true, true);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200317}
318EXPORT_SYMBOL(tcf_chain_get_by_act);
319
Jiri Pirko9f407f12018-07-23 09:23:07 +0200320static void tc_chain_tmplt_del(struct tcf_chain *chain);
321
Jiri Pirko53681402018-08-01 12:36:56 +0200322static void __tcf_chain_put(struct tcf_chain *chain, bool by_act)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200323{
Jiri Pirko53681402018-08-01 12:36:56 +0200324 if (by_act)
325 chain->action_refcnt--;
326 chain->refcnt--;
327
328 /* The last dropped non-action reference will trigger notification. */
329 if (chain->refcnt - chain->action_refcnt == 0 && !by_act)
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200330 tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false);
Jiri Pirko53681402018-08-01 12:36:56 +0200331
332 if (chain->refcnt == 0) {
Jiri Pirko9f407f12018-07-23 09:23:07 +0200333 tc_chain_tmplt_del(chain);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200334 tcf_chain_destroy(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200335 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200336}
Jiri Pirko53681402018-08-01 12:36:56 +0200337
Jiri Pirko290b1c82018-08-01 12:36:57 +0200338static void tcf_chain_put(struct tcf_chain *chain)
Jiri Pirko53681402018-08-01 12:36:56 +0200339{
340 __tcf_chain_put(chain, false);
341}
Jiri Pirko5bc17012017-05-17 11:08:01 +0200342
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200343void tcf_chain_put_by_act(struct tcf_chain *chain)
344{
Jiri Pirko53681402018-08-01 12:36:56 +0200345 __tcf_chain_put(chain, true);
Jiri Pirko1f3ed382018-07-27 09:45:05 +0200346}
347EXPORT_SYMBOL(tcf_chain_put_by_act);
348
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200349static void tcf_chain_put_explicitly_created(struct tcf_chain *chain)
350{
351 if (chain->explicitly_created)
352 tcf_chain_put(chain);
353}
354
Jiri Pirko290b1c82018-08-01 12:36:57 +0200355static void tcf_chain_flush(struct tcf_chain *chain)
356{
357 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
358
359 tcf_chain0_head_change(chain, NULL);
360 while (tp) {
361 RCU_INIT_POINTER(chain->filter_chain, tp->next);
362 tcf_proto_destroy(tp, NULL);
363 tp = rtnl_dereference(chain->filter_chain);
364 tcf_chain_put(chain);
365 }
366}
367
Jiri Pirkocaa72602018-01-17 11:46:50 +0100368static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200369{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100370 return block->offloadcnt;
371}
372
373static int tcf_block_offload_cmd(struct tcf_block *block,
374 struct net_device *dev,
375 struct tcf_block_ext_info *ei,
John Hurley60513bd2018-06-25 14:30:04 -0700376 enum tc_block_command command,
377 struct netlink_ext_ack *extack)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100378{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200379 struct tc_block_offload bo = {};
380
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200381 bo.command = command;
382 bo.binder_type = ei->binder_type;
383 bo.block = block;
John Hurley60513bd2018-06-25 14:30:04 -0700384 bo.extack = extack;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100385 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200386}
387
Jiri Pirkocaa72602018-01-17 11:46:50 +0100388static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
John Hurley60513bd2018-06-25 14:30:04 -0700389 struct tcf_block_ext_info *ei,
390 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200391{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100392 struct net_device *dev = q->dev_queue->dev;
393 int err;
394
395 if (!dev->netdev_ops->ndo_setup_tc)
396 goto no_offload_dev_inc;
397
398 /* If tc offload feature is disabled and the block we try to bind
399 * to already has some offloaded filters, forbid to bind.
400 */
John Hurley60513bd2018-06-25 14:30:04 -0700401 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) {
402 NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled");
Jiri Pirkocaa72602018-01-17 11:46:50 +0100403 return -EOPNOTSUPP;
John Hurley60513bd2018-06-25 14:30:04 -0700404 }
Jiri Pirkocaa72602018-01-17 11:46:50 +0100405
John Hurley60513bd2018-06-25 14:30:04 -0700406 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100407 if (err == -EOPNOTSUPP)
408 goto no_offload_dev_inc;
409 return err;
410
411no_offload_dev_inc:
412 if (tcf_block_offload_in_use(block))
413 return -EOPNOTSUPP;
414 block->nooffloaddevcnt++;
415 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200416}
417
418static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
419 struct tcf_block_ext_info *ei)
420{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100421 struct net_device *dev = q->dev_queue->dev;
422 int err;
423
424 if (!dev->netdev_ops->ndo_setup_tc)
425 goto no_offload_dev_dec;
John Hurley60513bd2018-06-25 14:30:04 -0700426 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100427 if (err == -EOPNOTSUPP)
428 goto no_offload_dev_dec;
429 return;
430
431no_offload_dev_dec:
432 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200433}
434
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100435static int
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200436tcf_chain0_head_change_cb_add(struct tcf_block *block,
437 struct tcf_block_ext_info *ei,
438 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100439{
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200440 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100441 struct tcf_filter_chain_list_item *item;
442
443 item = kmalloc(sizeof(*item), GFP_KERNEL);
444 if (!item) {
445 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
446 return -ENOMEM;
447 }
448 item->chain_head_change = ei->chain_head_change;
449 item->chain_head_change_priv = ei->chain_head_change_priv;
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200450 if (chain0 && chain0->filter_chain)
451 tcf_chain_head_change_item(item, chain0->filter_chain);
452 list_add(&item->list, &block->chain0.filter_chain_list);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100453 return 0;
454}
455
456static void
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200457tcf_chain0_head_change_cb_del(struct tcf_block *block,
458 struct tcf_block_ext_info *ei)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100459{
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200460 struct tcf_chain *chain0 = block->chain0.chain;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100461 struct tcf_filter_chain_list_item *item;
462
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200463 list_for_each_entry(item, &block->chain0.filter_chain_list, list) {
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100464 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
465 (item->chain_head_change == ei->chain_head_change &&
466 item->chain_head_change_priv == ei->chain_head_change_priv)) {
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200467 if (chain0)
468 tcf_chain_head_change_item(item, NULL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100469 list_del(&item->list);
470 kfree(item);
471 return;
472 }
473 }
474 WARN_ON(1);
475}
476
Jiri Pirko48617382018-01-17 11:46:46 +0100477struct tcf_net {
478 struct idr idr;
479};
480
481static unsigned int tcf_net_id;
482
483static int tcf_block_insert(struct tcf_block *block, struct net *net,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100484 struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100485{
Jiri Pirko48617382018-01-17 11:46:46 +0100486 struct tcf_net *tn = net_generic(net, tcf_net_id);
Jiri Pirko48617382018-01-17 11:46:46 +0100487
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100488 return idr_alloc_u32(&tn->idr, block, &block->index, block->index,
489 GFP_KERNEL);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100490}
491
Jiri Pirko48617382018-01-17 11:46:46 +0100492static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200493{
Jiri Pirko48617382018-01-17 11:46:46 +0100494 struct tcf_net *tn = net_generic(net, tcf_net_id);
495
Matthew Wilcox9c160942017-11-28 09:48:43 -0500496 idr_remove(&tn->idr, block->index);
Jiri Pirko48617382018-01-17 11:46:46 +0100497}
498
499static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100500 u32 block_index,
Jiri Pirko48617382018-01-17 11:46:46 +0100501 struct netlink_ext_ack *extack)
502{
503 struct tcf_block *block;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200504
Jiri Pirko48617382018-01-17 11:46:46 +0100505 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500506 if (!block) {
507 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100508 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500509 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200510 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200511 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100512 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200513 INIT_LIST_HEAD(&block->chain0.filter_chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200514
Jiri Pirko48617382018-01-17 11:46:46 +0100515 block->refcnt = 1;
516 block->net = net;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100517 block->index = block_index;
518
519 /* Don't store q pointer for blocks which are shared */
520 if (!tcf_block_shared(block))
521 block->q = q;
Jiri Pirko48617382018-01-17 11:46:46 +0100522 return block;
Jiri Pirko48617382018-01-17 11:46:46 +0100523}
524
525static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
526{
527 struct tcf_net *tn = net_generic(net, tcf_net_id);
528
Matthew Wilcox322d8842017-11-28 10:01:24 -0500529 return idr_find(&tn->idr, block_index);
Jiri Pirko48617382018-01-17 11:46:46 +0100530}
531
Vlad Buslovc431f892018-05-31 09:52:53 +0300532/* Find tcf block.
533 * Set q, parent, cl when appropriate.
534 */
535
536static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q,
537 u32 *parent, unsigned long *cl,
538 int ifindex, u32 block_index,
539 struct netlink_ext_ack *extack)
540{
541 struct tcf_block *block;
542
543 if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
544 block = tcf_block_lookup(net, block_index);
545 if (!block) {
546 NL_SET_ERR_MSG(extack, "Block of given index was not found");
547 return ERR_PTR(-EINVAL);
548 }
549 } else {
550 const struct Qdisc_class_ops *cops;
551 struct net_device *dev;
552
553 /* Find link */
554 dev = __dev_get_by_index(net, ifindex);
555 if (!dev)
556 return ERR_PTR(-ENODEV);
557
558 /* Find qdisc */
559 if (!*parent) {
560 *q = dev->qdisc;
561 *parent = (*q)->handle;
562 } else {
563 *q = qdisc_lookup(dev, TC_H_MAJ(*parent));
564 if (!*q) {
565 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
566 return ERR_PTR(-EINVAL);
567 }
568 }
569
570 /* Is it classful? */
571 cops = (*q)->ops->cl_ops;
572 if (!cops) {
573 NL_SET_ERR_MSG(extack, "Qdisc not classful");
574 return ERR_PTR(-EINVAL);
575 }
576
577 if (!cops->tcf_block) {
578 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
579 return ERR_PTR(-EOPNOTSUPP);
580 }
581
582 /* Do we search for filter, attached to class? */
583 if (TC_H_MIN(*parent)) {
584 *cl = cops->find(*q, *parent);
585 if (*cl == 0) {
586 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
587 return ERR_PTR(-ENOENT);
588 }
589 }
590
591 /* And the last stroke */
592 block = cops->tcf_block(*q, *cl, extack);
593 if (!block)
594 return ERR_PTR(-EINVAL);
595 if (tcf_block_shared(block)) {
596 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
597 return ERR_PTR(-EOPNOTSUPP);
598 }
599 }
600
601 return block;
602}
603
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100604struct tcf_block_owner_item {
605 struct list_head list;
606 struct Qdisc *q;
607 enum tcf_block_binder_type binder_type;
608};
609
610static void
611tcf_block_owner_netif_keep_dst(struct tcf_block *block,
612 struct Qdisc *q,
613 enum tcf_block_binder_type binder_type)
614{
615 if (block->keep_dst &&
616 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
617 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
618 netif_keep_dst(qdisc_dev(q));
619}
620
621void tcf_block_netif_keep_dst(struct tcf_block *block)
622{
623 struct tcf_block_owner_item *item;
624
625 block->keep_dst = true;
626 list_for_each_entry(item, &block->owner_list, list)
627 tcf_block_owner_netif_keep_dst(block, item->q,
628 item->binder_type);
629}
630EXPORT_SYMBOL(tcf_block_netif_keep_dst);
631
632static int tcf_block_owner_add(struct tcf_block *block,
633 struct Qdisc *q,
634 enum tcf_block_binder_type binder_type)
635{
636 struct tcf_block_owner_item *item;
637
638 item = kmalloc(sizeof(*item), GFP_KERNEL);
639 if (!item)
640 return -ENOMEM;
641 item->q = q;
642 item->binder_type = binder_type;
643 list_add(&item->list, &block->owner_list);
644 return 0;
645}
646
647static void tcf_block_owner_del(struct tcf_block *block,
648 struct Qdisc *q,
649 enum tcf_block_binder_type binder_type)
650{
651 struct tcf_block_owner_item *item;
652
653 list_for_each_entry(item, &block->owner_list, list) {
654 if (item->q == q && item->binder_type == binder_type) {
655 list_del(&item->list);
656 kfree(item);
657 return;
658 }
659 }
660 WARN_ON(1);
661}
662
Jiri Pirko48617382018-01-17 11:46:46 +0100663int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
664 struct tcf_block_ext_info *ei,
665 struct netlink_ext_ack *extack)
666{
667 struct net *net = qdisc_net(q);
668 struct tcf_block *block = NULL;
669 bool created = false;
670 int err;
671
672 if (ei->block_index) {
673 /* block_index not 0 means the shared block is requested */
674 block = tcf_block_lookup(net, ei->block_index);
675 if (block)
676 block->refcnt++;
677 }
678
679 if (!block) {
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100680 block = tcf_block_create(net, q, ei->block_index, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100681 if (IS_ERR(block))
682 return PTR_ERR(block);
683 created = true;
Jiri Pirkobb047dd2018-02-13 12:00:16 +0100684 if (tcf_block_shared(block)) {
685 err = tcf_block_insert(block, net, extack);
Jiri Pirko48617382018-01-17 11:46:46 +0100686 if (err)
687 goto err_block_insert;
688 }
689 }
690
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100691 err = tcf_block_owner_add(block, q, ei->binder_type);
692 if (err)
693 goto err_block_owner_add;
694
695 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
696
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200697 err = tcf_chain0_head_change_cb_add(block, ei, extack);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100698 if (err)
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200699 goto err_chain0_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100700
John Hurley60513bd2018-06-25 14:30:04 -0700701 err = tcf_block_offload_bind(block, q, ei, extack);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100702 if (err)
703 goto err_block_offload_bind;
704
Jiri Pirko6529eab2017-05-17 11:07:55 +0200705 *p_block = block;
706 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200707
Jiri Pirkocaa72602018-01-17 11:46:50 +0100708err_block_offload_bind:
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200709 tcf_chain0_head_change_cb_del(block, ei);
710err_chain0_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100711 tcf_block_owner_del(block, q, ei->binder_type);
712err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100713 if (created) {
714 if (tcf_block_shared(block))
715 tcf_block_remove(block, net);
716err_block_insert:
Jiri Pirko48617382018-01-17 11:46:46 +0100717 kfree(block);
718 } else {
719 block->refcnt--;
720 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200721 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200722}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200723EXPORT_SYMBOL(tcf_block_get_ext);
724
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100725static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
726{
727 struct tcf_proto __rcu **p_filter_chain = priv;
728
729 rcu_assign_pointer(*p_filter_chain, tp_head);
730}
731
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200732int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500733 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
734 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200735{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100736 struct tcf_block_ext_info ei = {
737 .chain_head_change = tcf_chain_head_change_dflt,
738 .chain_head_change_priv = p_filter_chain,
739 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200740
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100741 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500742 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200743}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200744EXPORT_SYMBOL(tcf_block_get);
745
Cong Wang7aa00452017-10-26 18:24:28 -0700746/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100747 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700748 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100749void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900750 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700751{
Cong Wangefbf7892017-12-04 10:48:18 -0800752 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700753
David S. Millerc30abd52017-12-16 22:11:55 -0500754 if (!block)
755 return;
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200756 tcf_chain0_head_change_cb_del(block, ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100757 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100758
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200759 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100760 if (tcf_block_shared(block))
761 tcf_block_remove(block, block->net);
762
763 /* Hold a refcnt for all chains, so that they don't disappear
764 * while we are iterating.
765 */
766 list_for_each_entry(chain, &block->chain_list, list)
767 tcf_chain_hold(chain);
768
769 list_for_each_entry(chain, &block->chain_list, list)
770 tcf_chain_flush(chain);
771 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700772
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100773 tcf_block_offload_unbind(block, q, ei);
774
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200775 if (block->refcnt == 1) {
Jiri Pirko48617382018-01-17 11:46:46 +0100776 /* At this point, all the chains should have refcnt >= 1. */
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200777 list_for_each_entry_safe(chain, tmp, &block->chain_list, list) {
778 tcf_chain_put_explicitly_created(chain);
Jiri Pirko48617382018-01-17 11:46:46 +0100779 tcf_chain_put(chain);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +0200780 }
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100781
Jiri Pirkof71e0ca2018-07-23 09:23:05 +0200782 block->refcnt--;
783 if (list_empty(&block->chain_list))
784 kfree(block);
Jiri Pirko63cc5bc2018-08-08 14:04:13 +0200785 } else {
786 block->refcnt--;
Jiri Pirko48617382018-01-17 11:46:46 +0100787 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200788}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200789EXPORT_SYMBOL(tcf_block_put_ext);
790
791void tcf_block_put(struct tcf_block *block)
792{
793 struct tcf_block_ext_info ei = {0, };
794
Jiri Pirko4853f122017-12-21 13:13:59 +0100795 if (!block)
796 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100797 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200798}
David S. Millere1ea2f92017-10-30 14:10:01 +0900799
Jiri Pirko6529eab2017-05-17 11:07:55 +0200800EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100801
Jiri Pirkoacb67442017-10-19 15:50:31 +0200802struct tcf_block_cb {
803 struct list_head list;
804 tc_setup_cb_t *cb;
805 void *cb_ident;
806 void *cb_priv;
807 unsigned int refcnt;
808};
809
810void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
811{
812 return block_cb->cb_priv;
813}
814EXPORT_SYMBOL(tcf_block_cb_priv);
815
816struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
817 tc_setup_cb_t *cb, void *cb_ident)
818{ struct tcf_block_cb *block_cb;
819
820 list_for_each_entry(block_cb, &block->cb_list, list)
821 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
822 return block_cb;
823 return NULL;
824}
825EXPORT_SYMBOL(tcf_block_cb_lookup);
826
827void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
828{
829 block_cb->refcnt++;
830}
831EXPORT_SYMBOL(tcf_block_cb_incref);
832
833unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
834{
835 return --block_cb->refcnt;
836}
837EXPORT_SYMBOL(tcf_block_cb_decref);
838
John Hurley32636742018-06-25 14:30:10 -0700839static int
840tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb,
841 void *cb_priv, bool add, bool offload_in_use,
842 struct netlink_ext_ack *extack)
843{
844 struct tcf_chain *chain;
845 struct tcf_proto *tp;
846 int err;
847
848 list_for_each_entry(chain, &block->chain_list, list) {
849 for (tp = rtnl_dereference(chain->filter_chain); tp;
850 tp = rtnl_dereference(tp->next)) {
851 if (tp->ops->reoffload) {
852 err = tp->ops->reoffload(tp, add, cb, cb_priv,
853 extack);
854 if (err && add)
855 goto err_playback_remove;
856 } else if (add && offload_in_use) {
857 err = -EOPNOTSUPP;
858 NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support");
859 goto err_playback_remove;
860 }
861 }
862 }
863
864 return 0;
865
866err_playback_remove:
867 tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use,
868 extack);
869 return err;
870}
871
Jiri Pirkoacb67442017-10-19 15:50:31 +0200872struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
873 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700874 void *cb_priv,
875 struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200876{
877 struct tcf_block_cb *block_cb;
John Hurley32636742018-06-25 14:30:10 -0700878 int err;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200879
John Hurley32636742018-06-25 14:30:10 -0700880 /* Replay any already present rules */
881 err = tcf_block_playback_offloads(block, cb, cb_priv, true,
882 tcf_block_offload_in_use(block),
883 extack);
884 if (err)
885 return ERR_PTR(err);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100886
Jiri Pirkoacb67442017-10-19 15:50:31 +0200887 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
888 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100889 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200890 block_cb->cb = cb;
891 block_cb->cb_ident = cb_ident;
892 block_cb->cb_priv = cb_priv;
893 list_add(&block_cb->list, &block->cb_list);
894 return block_cb;
895}
896EXPORT_SYMBOL(__tcf_block_cb_register);
897
898int tcf_block_cb_register(struct tcf_block *block,
899 tc_setup_cb_t *cb, void *cb_ident,
John Hurley60513bd2018-06-25 14:30:04 -0700900 void *cb_priv, struct netlink_ext_ack *extack)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200901{
902 struct tcf_block_cb *block_cb;
903
John Hurley60513bd2018-06-25 14:30:04 -0700904 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv,
905 extack);
Gustavo A. R. Silvabaa2d2b2018-07-18 23:14:17 -0500906 return PTR_ERR_OR_ZERO(block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200907}
908EXPORT_SYMBOL(tcf_block_cb_register);
909
John Hurley32636742018-06-25 14:30:10 -0700910void __tcf_block_cb_unregister(struct tcf_block *block,
911 struct tcf_block_cb *block_cb)
Jiri Pirkoacb67442017-10-19 15:50:31 +0200912{
John Hurley32636742018-06-25 14:30:10 -0700913 tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv,
914 false, tcf_block_offload_in_use(block),
915 NULL);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200916 list_del(&block_cb->list);
917 kfree(block_cb);
918}
919EXPORT_SYMBOL(__tcf_block_cb_unregister);
920
921void tcf_block_cb_unregister(struct tcf_block *block,
922 tc_setup_cb_t *cb, void *cb_ident)
923{
924 struct tcf_block_cb *block_cb;
925
926 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
927 if (!block_cb)
928 return;
John Hurley32636742018-06-25 14:30:10 -0700929 __tcf_block_cb_unregister(block, block_cb);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200930}
931EXPORT_SYMBOL(tcf_block_cb_unregister);
932
933static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
934 void *type_data, bool err_stop)
935{
936 struct tcf_block_cb *block_cb;
937 int ok_count = 0;
938 int err;
939
David S. Miller9a99dc12018-06-06 13:55:47 -0400940 /* Make sure all netdevs sharing this block are offload-capable. */
941 if (block->nooffloaddevcnt && err_stop)
942 return -EOPNOTSUPP;
943
Jiri Pirkoacb67442017-10-19 15:50:31 +0200944 list_for_each_entry(block_cb, &block->cb_list, list) {
945 err = block_cb->cb(type, type_data, block_cb->cb_priv);
946 if (err) {
947 if (err_stop)
948 return err;
949 } else {
950 ok_count++;
951 }
952 }
953 return ok_count;
954}
955
Jiri Pirko87d83092017-05-17 11:07:54 +0200956/* Main classifier routine: scans classifier chain attached
957 * to this qdisc, (optionally) tests for protocol and asks
958 * specific classifiers.
959 */
960int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
961 struct tcf_result *res, bool compat_mode)
962{
Jiri Pirko87d83092017-05-17 11:07:54 +0200963#ifdef CONFIG_NET_CLS_ACT
964 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200965 const struct tcf_proto *orig_tp = tp;
966 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200967 int limit = 0;
968
969reclassify:
970#endif
971 for (; tp; tp = rcu_dereference_bh(tp->next)) {
Cong Wang916c27c2019-01-11 18:55:42 -0800972 __be16 protocol = tc_skb_protocol(skb);
Jiri Pirko87d83092017-05-17 11:07:54 +0200973 int err;
974
975 if (tp->protocol != protocol &&
976 tp->protocol != htons(ETH_P_ALL))
977 continue;
978
979 err = tp->classify(skb, tp, res);
980#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200981 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200982 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200983 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200984 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200985 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200986 goto reset;
987 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200988#endif
989 if (err >= 0)
990 return err;
991 }
992
993 return TC_ACT_UNSPEC; /* signal: continue lookup */
994#ifdef CONFIG_NET_CLS_ACT
995reset:
996 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100997 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
998 tp->chain->block->index,
999 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +02001000 ntohs(tp->protocol));
1001 return TC_ACT_SHOT;
1002 }
1003
Jiri Pirkoee538dc2017-05-23 09:11:59 +02001004 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +02001005 goto reclassify;
1006#endif
1007}
1008EXPORT_SYMBOL(tcf_classify);
1009
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001010struct tcf_chain_info {
1011 struct tcf_proto __rcu **pprev;
1012 struct tcf_proto __rcu *next;
1013};
1014
1015static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
1016{
1017 return rtnl_dereference(*chain_info->pprev);
1018}
1019
1020static void tcf_chain_tp_insert(struct tcf_chain *chain,
1021 struct tcf_chain_info *chain_info,
1022 struct tcf_proto *tp)
1023{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001024 if (*chain_info->pprev == chain->filter_chain)
Jiri Pirkof71e0ca2018-07-23 09:23:05 +02001025 tcf_chain0_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001026 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
1027 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -07001028 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001029}
1030
1031static void tcf_chain_tp_remove(struct tcf_chain *chain,
1032 struct tcf_chain_info *chain_info,
1033 struct tcf_proto *tp)
1034{
1035 struct tcf_proto *next = rtnl_dereference(chain_info->next);
1036
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +01001037 if (tp == chain->filter_chain)
Jiri Pirkof71e0ca2018-07-23 09:23:05 +02001038 tcf_chain0_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001039 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -07001040 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001041}
1042
1043static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
1044 struct tcf_chain_info *chain_info,
1045 u32 protocol, u32 prio,
1046 bool prio_allocate)
1047{
1048 struct tcf_proto **pprev;
1049 struct tcf_proto *tp;
1050
1051 /* Check the chain for existence of proto-tcf with this priority */
1052 for (pprev = &chain->filter_chain;
1053 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
1054 if (tp->prio >= prio) {
1055 if (tp->prio == prio) {
1056 if (prio_allocate ||
1057 (tp->protocol != protocol && protocol))
1058 return ERR_PTR(-EINVAL);
1059 } else {
1060 tp = NULL;
1061 }
1062 break;
1063 }
1064 }
1065 chain_info->pprev = pprev;
1066 chain_info->next = tp ? tp->next : NULL;
1067 return tp;
1068}
1069
WANG Cong71203712017-08-07 15:26:50 -07001070static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001071 struct tcf_proto *tp, struct tcf_block *block,
1072 struct Qdisc *q, u32 parent, void *fh,
1073 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -07001074{
1075 struct tcmsg *tcm;
1076 struct nlmsghdr *nlh;
1077 unsigned char *b = skb_tail_pointer(skb);
1078
1079 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1080 if (!nlh)
1081 goto out_nlmsg_trim;
1082 tcm = nlmsg_data(nlh);
1083 tcm->tcm_family = AF_UNSPEC;
1084 tcm->tcm__pad1 = 0;
1085 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001086 if (q) {
1087 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
1088 tcm->tcm_parent = parent;
1089 } else {
1090 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1091 tcm->tcm_block_index = block->index;
1092 }
WANG Cong71203712017-08-07 15:26:50 -07001093 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
1094 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
1095 goto nla_put_failure;
1096 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
1097 goto nla_put_failure;
1098 if (!fh) {
1099 tcm->tcm_handle = 0;
1100 } else {
1101 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
1102 goto nla_put_failure;
1103 }
1104 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1105 return skb->len;
1106
1107out_nlmsg_trim:
1108nla_put_failure:
1109 nlmsg_trim(skb, b);
1110 return -1;
1111}
1112
1113static int tfilter_notify(struct net *net, struct sk_buff *oskb,
1114 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001115 struct tcf_block *block, struct Qdisc *q,
1116 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -07001117{
1118 struct sk_buff *skb;
1119 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1120
1121 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1122 if (!skb)
1123 return -ENOBUFS;
1124
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001125 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1126 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -07001127 kfree_skb(skb);
1128 return -EINVAL;
1129 }
1130
1131 if (unicast)
1132 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1133
1134 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1135 n->nlmsg_flags & NLM_F_ECHO);
1136}
1137
1138static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
1139 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001140 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001141 u32 parent, void *fh, bool unicast, bool *last,
1142 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -07001143{
1144 struct sk_buff *skb;
1145 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1146 int err;
1147
1148 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1149 if (!skb)
1150 return -ENOBUFS;
1151
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001152 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
1153 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001154 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -07001155 kfree_skb(skb);
1156 return -EINVAL;
1157 }
1158
Alexander Aring571acf22018-01-18 11:20:53 -05001159 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -07001160 if (err) {
1161 kfree_skb(skb);
1162 return err;
1163 }
1164
1165 if (unicast)
1166 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1167
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001168 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
1169 n->nlmsg_flags & NLM_F_ECHO);
1170 if (err < 0)
1171 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
1172 return err;
WANG Cong71203712017-08-07 15:26:50 -07001173}
1174
1175static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001176 struct tcf_block *block, struct Qdisc *q,
1177 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -07001178 struct tcf_chain *chain, int event)
1179{
1180 struct tcf_proto *tp;
1181
1182 for (tp = rtnl_dereference(chain->filter_chain);
1183 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001184 tfilter_notify(net, oskb, n, tp, block,
YueHaibing53189182018-07-17 20:58:14 +08001185 q, parent, NULL, event, false);
WANG Cong71203712017-08-07 15:26:50 -07001186}
1187
Vlad Buslovc431f892018-05-31 09:52:53 +03001188static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
David Ahernc21ef3e2017-04-16 09:48:24 -07001189 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001191 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -08001192 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 struct tcmsg *t;
1194 u32 protocol;
1195 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001196 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001198 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001199 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001200 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001201 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001202 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001205 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001207 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Vlad Buslovc431f892018-05-31 09:52:53 +03001209 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001210 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001213 tp_created = 0;
1214
Davide Carattie3314732018-10-10 22:00:58 +02001215 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001216 if (err < 0)
1217 return err;
1218
David S. Miller942b8162012-06-26 21:48:50 -07001219 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 protocol = TC_H_MIN(t->tcm_info);
1221 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001222 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 parent = t->tcm_parent;
1224 cl = 0;
1225
1226 if (prio == 0) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001227 /* If no priority is provided by the user,
1228 * we allocate one.
1229 */
1230 if (n->nlmsg_flags & NLM_F_CREATE) {
1231 prio = TC_H_MAKE(0x80000000U, 0U);
1232 prio_allocate = true;
1233 } else {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001234 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238
1239 /* Find head of filter chain. */
1240
Vlad Buslovc431f892018-05-31 09:52:53 +03001241 block = tcf_block_find(net, &q, &parent, &cl,
1242 t->tcm_ifindex, t->tcm_block_index, extack);
1243 if (IS_ERR(block)) {
1244 err = PTR_ERR(block);
1245 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001246 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001247
1248 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1249 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001250 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001251 err = -EINVAL;
1252 goto errout;
1253 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001254 chain = tcf_chain_get(block, chain_index, true);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001255 if (!chain) {
Jiri Pirkod5ed72a2018-08-27 20:58:43 +02001256 NL_SET_ERR_MSG(extack, "Cannot create specified filter chain");
Vlad Buslovc431f892018-05-31 09:52:53 +03001257 err = -ENOMEM;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001258 goto errout;
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001261 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1262 prio, prio_allocate);
1263 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001264 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001265 err = PTR_ERR(tp);
1266 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
1269 if (tp == NULL) {
1270 /* Proto-tcf does not exist, create new one */
1271
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001272 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001273 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001274 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Vlad Buslovc431f892018-05-31 09:52:53 +03001278 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001279 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 +01001280 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001284 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001285 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Jiri Pirko33a48922017-02-09 14:38:57 +01001287 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001288 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001289 if (IS_ERR(tp)) {
1290 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto errout;
1292 }
Minoru Usui12186be2009-06-02 02:17:34 -07001293 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001294 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001295 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001296 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 fh = tp->ops->get(tp, t->tcm_handle);
1301
WANG Cong8113c092017-08-04 21:31:43 -07001302 if (!fh) {
Vlad Buslovc431f892018-05-31 09:52:53 +03001303 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001304 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 +01001305 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001307 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001308 } else if (n->nlmsg_flags & NLM_F_EXCL) {
1309 NL_SET_ERR_MSG(extack, "Filter already exists");
1310 err = -EEXIST;
1311 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
Jiri Pirko9f407f12018-07-23 09:23:07 +02001314 if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) {
1315 NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind");
1316 err = -EINVAL;
1317 goto errout;
1318 }
1319
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001320 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001321 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1322 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001323 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001324 if (tp_created)
1325 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001326 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001327 RTM_NEWTFILTER, false);
Vlad Buslov60e9bab2019-07-21 17:44:12 +03001328 /* q pointer is NULL for shared blocks */
1329 if (q)
1330 q->flags &= ~TCQ_F_CAN_BYPASS;
Minoru Usui12186be2009-06-02 02:17:34 -07001331 } else {
1332 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001333 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001337 if (chain)
1338 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (err == -EAGAIN)
1340 /* Replay the request. */
1341 goto replay;
1342 return err;
1343}
1344
Vlad Buslovc431f892018-05-31 09:52:53 +03001345static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1346 struct netlink_ext_ack *extack)
1347{
1348 struct net *net = sock_net(skb->sk);
1349 struct nlattr *tca[TCA_MAX + 1];
1350 struct tcmsg *t;
1351 u32 protocol;
1352 u32 prio;
1353 u32 parent;
1354 u32 chain_index;
1355 struct Qdisc *q = NULL;
1356 struct tcf_chain_info chain_info;
1357 struct tcf_chain *chain = NULL;
1358 struct tcf_block *block;
1359 struct tcf_proto *tp = NULL;
1360 unsigned long cl = 0;
1361 void *fh = NULL;
1362 int err;
1363
1364 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1365 return -EPERM;
1366
Davide Carattie3314732018-10-10 22:00:58 +02001367 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03001368 if (err < 0)
1369 return err;
1370
1371 t = nlmsg_data(n);
1372 protocol = TC_H_MIN(t->tcm_info);
1373 prio = TC_H_MAJ(t->tcm_info);
1374 parent = t->tcm_parent;
1375
1376 if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) {
1377 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
1378 return -ENOENT;
1379 }
1380
1381 /* Find head of filter chain. */
1382
1383 block = tcf_block_find(net, &q, &parent, &cl,
1384 t->tcm_ifindex, t->tcm_block_index, extack);
1385 if (IS_ERR(block)) {
1386 err = PTR_ERR(block);
1387 goto errout;
1388 }
1389
1390 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1391 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1392 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1393 err = -EINVAL;
1394 goto errout;
1395 }
1396 chain = tcf_chain_get(block, chain_index, false);
1397 if (!chain) {
Jiri Pirko5ca8a252018-08-03 11:08:47 +02001398 /* User requested flush on non-existent chain. Nothing to do,
1399 * so just return success.
1400 */
1401 if (prio == 0) {
1402 err = 0;
1403 goto errout;
1404 }
Vlad Buslovc431f892018-05-31 09:52:53 +03001405 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
Jiri Pirkob7b42472018-08-27 20:58:44 +02001406 err = -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001407 goto errout;
1408 }
1409
1410 if (prio == 0) {
1411 tfilter_notify_chain(net, skb, block, q, parent, n,
1412 chain, RTM_DELTFILTER);
1413 tcf_chain_flush(chain);
1414 err = 0;
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 if (t->tcm_handle == 0) {
1434 tcf_chain_tp_remove(chain, &chain_info, tp);
1435 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
1436 RTM_DELTFILTER, false);
1437 tcf_proto_destroy(tp, extack);
1438 err = 0;
1439 } else {
1440 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1441 err = -ENOENT;
1442 }
1443 } else {
1444 bool last;
1445
1446 err = tfilter_del_notify(net, skb, n, tp, block,
1447 q, parent, fh, false, &last,
1448 extack);
1449 if (err)
1450 goto errout;
1451 if (last) {
1452 tcf_chain_tp_remove(chain, &chain_info, tp);
1453 tcf_proto_destroy(tp, extack);
1454 }
1455 }
1456
1457errout:
1458 if (chain)
1459 tcf_chain_put(chain);
1460 return err;
1461}
1462
1463static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
1464 struct netlink_ext_ack *extack)
1465{
1466 struct net *net = sock_net(skb->sk);
1467 struct nlattr *tca[TCA_MAX + 1];
1468 struct tcmsg *t;
1469 u32 protocol;
1470 u32 prio;
1471 u32 parent;
1472 u32 chain_index;
1473 struct Qdisc *q = NULL;
1474 struct tcf_chain_info chain_info;
1475 struct tcf_chain *chain = NULL;
1476 struct tcf_block *block;
1477 struct tcf_proto *tp = NULL;
1478 unsigned long cl = 0;
1479 void *fh = NULL;
1480 int err;
1481
Davide Carattie3314732018-10-10 22:00:58 +02001482 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
Vlad Buslovc431f892018-05-31 09:52:53 +03001483 if (err < 0)
1484 return err;
1485
1486 t = nlmsg_data(n);
1487 protocol = TC_H_MIN(t->tcm_info);
1488 prio = TC_H_MAJ(t->tcm_info);
1489 parent = t->tcm_parent;
1490
1491 if (prio == 0) {
1492 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
1493 return -ENOENT;
1494 }
1495
1496 /* Find head of filter chain. */
1497
1498 block = tcf_block_find(net, &q, &parent, &cl,
1499 t->tcm_ifindex, t->tcm_block_index, extack);
1500 if (IS_ERR(block)) {
1501 err = PTR_ERR(block);
1502 goto errout;
1503 }
1504
1505 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1506 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1507 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1508 err = -EINVAL;
1509 goto errout;
1510 }
1511 chain = tcf_chain_get(block, chain_index, false);
1512 if (!chain) {
1513 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1514 err = -EINVAL;
1515 goto errout;
1516 }
1517
1518 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1519 prio, false);
1520 if (!tp || IS_ERR(tp)) {
1521 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Vlad Buslov0e399032018-06-04 18:32:23 +03001522 err = tp ? PTR_ERR(tp) : -ENOENT;
Vlad Buslovc431f892018-05-31 09:52:53 +03001523 goto errout;
1524 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1525 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
1526 err = -EINVAL;
1527 goto errout;
1528 }
1529
1530 fh = tp->ops->get(tp, t->tcm_handle);
1531
1532 if (!fh) {
1533 NL_SET_ERR_MSG(extack, "Specified filter handle not found");
1534 err = -ENOENT;
1535 } else {
1536 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1537 fh, RTM_NEWTFILTER, true);
1538 if (err < 0)
1539 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
1540 }
1541
1542errout:
1543 if (chain)
1544 tcf_chain_put(chain);
1545 return err;
1546}
1547
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001548struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 struct tcf_walker w;
1550 struct sk_buff *skb;
1551 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001552 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001553 struct Qdisc *q;
1554 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555};
1556
WANG Cong8113c092017-08-04 21:31:43 -07001557static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001559 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001560 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001562 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001563 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001564 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1565 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001568static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1569 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001570 long index_start, long *p_index)
1571{
1572 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001573 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001574 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1575 struct tcf_dump_args arg;
1576 struct tcf_proto *tp;
1577
1578 for (tp = rtnl_dereference(chain->filter_chain);
1579 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1580 if (*p_index < index_start)
1581 continue;
1582 if (TC_H_MAJ(tcm->tcm_info) &&
1583 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1584 continue;
1585 if (TC_H_MIN(tcm->tcm_info) &&
1586 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1587 continue;
1588 if (*p_index > index_start)
1589 memset(&cb->args[1], 0,
1590 sizeof(cb->args) - sizeof(cb->args[0]));
1591 if (cb->args[1] == 0) {
YueHaibing53189182018-07-17 20:58:14 +08001592 if (tcf_fill_node(net, skb, tp, block, q, parent, NULL,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001593 NETLINK_CB(cb->skb).portid,
1594 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1595 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001596 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001597
1598 cb->args[1] = 1;
1599 }
1600 if (!tp->ops->walk)
1601 continue;
1602 arg.w.fn = tcf_node_dump;
1603 arg.skb = skb;
1604 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001605 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001606 arg.q = q;
1607 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001608 arg.w.stop = 0;
1609 arg.w.skip = cb->args[1] - 1;
1610 arg.w.count = 0;
Vlad Buslov01683a12018-07-09 13:29:11 +03001611 arg.w.cookie = cb->args[2];
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001612 tp->ops->walk(tp, &arg.w);
Vlad Buslov01683a12018-07-09 13:29:11 +03001613 cb->args[2] = arg.w.cookie;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001614 cb->args[1] = arg.w.count + 1;
1615 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001616 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001617 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001618 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001619}
1620
Eric Dumazetbd27a872009-11-05 20:57:26 -08001621/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1623{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001624 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001625 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001626 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001627 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001628 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001629 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001630 long index_start;
1631 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001632 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001633 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Hong zhi guo573ce262013-03-27 06:47:04 +00001635 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001637
1638 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1639 if (err)
1640 return err;
1641
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001642 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1643 block = tcf_block_lookup(net, tcm->tcm_block_index);
1644 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001645 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001646 /* If we work with block index, q is NULL and parent value
1647 * will never be used in the following code. The check
1648 * in tcf_fill_node prevents it. However, compiler does not
1649 * see that far, so set parent to zero to silence the warning
1650 * about parent being uninitialized.
1651 */
1652 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001653 } else {
1654 const struct Qdisc_class_ops *cops;
1655 struct net_device *dev;
1656 unsigned long cl = 0;
1657
1658 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1659 if (!dev)
1660 return skb->len;
1661
1662 parent = tcm->tcm_parent;
1663 if (!parent) {
1664 q = dev->qdisc;
1665 parent = q->handle;
1666 } else {
1667 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1668 }
1669 if (!q)
1670 goto out;
1671 cops = q->ops->cl_ops;
1672 if (!cops)
1673 goto out;
1674 if (!cops->tcf_block)
1675 goto out;
1676 if (TC_H_MIN(tcm->tcm_parent)) {
1677 cl = cops->find(q, tcm->tcm_parent);
1678 if (cl == 0)
1679 goto out;
1680 }
1681 block = cops->tcf_block(q, cl, NULL);
1682 if (!block)
1683 goto out;
1684 if (tcf_block_shared(block))
1685 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001688 index_start = cb->args[0];
1689 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001690
1691 list_for_each_entry(chain, &block->chain_list, list) {
1692 if (tca[TCA_CHAIN] &&
1693 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1694 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001695 if (!tcf_chain_dump(chain, q, parent, skb, cb,
Roman Kapl5ae437a2018-02-19 21:32:51 +01001696 index_start, &index)) {
1697 err = -EMSGSIZE;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001698 break;
Roman Kapl5ae437a2018-02-19 21:32:51 +01001699 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001700 }
1701
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001702 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704out:
Roman Kapl5ae437a2018-02-19 21:32:51 +01001705 /* If we did no progress, the error (EMSGSIZE) is real */
1706 if (skb->len == 0 && err)
1707 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return skb->len;
1709}
1710
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001711static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net,
1712 struct sk_buff *skb, struct tcf_block *block,
1713 u32 portid, u32 seq, u16 flags, int event)
1714{
1715 unsigned char *b = skb_tail_pointer(skb);
Jiri Pirko9f407f12018-07-23 09:23:07 +02001716 const struct tcf_proto_ops *ops;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001717 struct nlmsghdr *nlh;
1718 struct tcmsg *tcm;
Jiri Pirko9f407f12018-07-23 09:23:07 +02001719 void *priv;
1720
1721 ops = chain->tmplt_ops;
1722 priv = chain->tmplt_priv;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001723
1724 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
1725 if (!nlh)
1726 goto out_nlmsg_trim;
1727 tcm = nlmsg_data(nlh);
1728 tcm->tcm_family = AF_UNSPEC;
1729 tcm->tcm__pad1 = 0;
1730 tcm->tcm__pad2 = 0;
1731 tcm->tcm_handle = 0;
1732 if (block->q) {
1733 tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex;
1734 tcm->tcm_parent = block->q->handle;
1735 } else {
1736 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
1737 tcm->tcm_block_index = block->index;
1738 }
1739
1740 if (nla_put_u32(skb, TCA_CHAIN, chain->index))
1741 goto nla_put_failure;
1742
Jiri Pirko9f407f12018-07-23 09:23:07 +02001743 if (ops) {
1744 if (nla_put_string(skb, TCA_KIND, ops->kind))
1745 goto nla_put_failure;
1746 if (ops->tmplt_dump(skb, net, priv) < 0)
1747 goto nla_put_failure;
1748 }
1749
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001750 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1751 return skb->len;
1752
1753out_nlmsg_trim:
1754nla_put_failure:
1755 nlmsg_trim(skb, b);
1756 return -EMSGSIZE;
1757}
1758
1759static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb,
1760 u32 seq, u16 flags, int event, bool unicast)
1761{
1762 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
1763 struct tcf_block *block = chain->block;
1764 struct net *net = block->net;
1765 struct sk_buff *skb;
1766
1767 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1768 if (!skb)
1769 return -ENOBUFS;
1770
1771 if (tc_chain_fill_node(chain, net, skb, block, portid,
1772 seq, flags, event) <= 0) {
1773 kfree_skb(skb);
1774 return -EINVAL;
1775 }
1776
1777 if (unicast)
1778 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
1779
1780 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO);
1781}
1782
Jiri Pirko9f407f12018-07-23 09:23:07 +02001783static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net,
1784 struct nlattr **tca,
1785 struct netlink_ext_ack *extack)
1786{
1787 const struct tcf_proto_ops *ops;
1788 void *tmplt_priv;
1789
1790 /* If kind is not set, user did not specify template. */
1791 if (!tca[TCA_KIND])
1792 return 0;
1793
1794 ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack);
1795 if (IS_ERR(ops))
1796 return PTR_ERR(ops);
1797 if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) {
1798 NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier");
1799 return -EOPNOTSUPP;
1800 }
1801
1802 tmplt_priv = ops->tmplt_create(net, chain, tca, extack);
1803 if (IS_ERR(tmplt_priv)) {
1804 module_put(ops->owner);
1805 return PTR_ERR(tmplt_priv);
1806 }
1807 chain->tmplt_ops = ops;
1808 chain->tmplt_priv = tmplt_priv;
1809 return 0;
1810}
1811
1812static void tc_chain_tmplt_del(struct tcf_chain *chain)
1813{
1814 const struct tcf_proto_ops *ops = chain->tmplt_ops;
1815
1816 /* If template ops are set, no work to do for us. */
1817 if (!ops)
1818 return;
1819
1820 ops->tmplt_destroy(chain->tmplt_priv);
1821 module_put(ops->owner);
1822}
1823
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001824/* Add/delete/get a chain */
1825
1826static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n,
1827 struct netlink_ext_ack *extack)
1828{
1829 struct net *net = sock_net(skb->sk);
1830 struct nlattr *tca[TCA_MAX + 1];
1831 struct tcmsg *t;
1832 u32 parent;
1833 u32 chain_index;
1834 struct Qdisc *q = NULL;
1835 struct tcf_chain *chain = NULL;
1836 struct tcf_block *block;
1837 unsigned long cl;
1838 int err;
1839
1840 if (n->nlmsg_type != RTM_GETCHAIN &&
1841 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
1842 return -EPERM;
1843
1844replay:
Davide Carattie3314732018-10-10 22:00:58 +02001845 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001846 if (err < 0)
1847 return err;
1848
1849 t = nlmsg_data(n);
1850 parent = t->tcm_parent;
1851 cl = 0;
1852
1853 block = tcf_block_find(net, &q, &parent, &cl,
1854 t->tcm_ifindex, t->tcm_block_index, extack);
1855 if (IS_ERR(block))
1856 return PTR_ERR(block);
1857
1858 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1859 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1860 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
1861 return -EINVAL;
1862 }
1863 chain = tcf_chain_lookup(block, chain_index);
1864 if (n->nlmsg_type == RTM_NEWCHAIN) {
1865 if (chain) {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001866 if (tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001867 /* The chain exists only because there is
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001868 * some action referencing it.
Jiri Pirko1f3ed382018-07-27 09:45:05 +02001869 */
1870 tcf_chain_hold(chain);
1871 } else {
1872 NL_SET_ERR_MSG(extack, "Filter chain already exists");
1873 return -EEXIST;
1874 }
1875 } else {
1876 if (!(n->nlmsg_flags & NLM_F_CREATE)) {
1877 NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain");
1878 return -ENOENT;
1879 }
1880 chain = tcf_chain_create(block, chain_index);
1881 if (!chain) {
1882 NL_SET_ERR_MSG(extack, "Failed to create filter chain");
1883 return -ENOMEM;
1884 }
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001885 }
1886 } else {
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02001887 if (!chain || tcf_chain_held_by_acts_only(chain)) {
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001888 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
1889 return -EINVAL;
1890 }
1891 tcf_chain_hold(chain);
1892 }
1893
1894 switch (n->nlmsg_type) {
1895 case RTM_NEWCHAIN:
Jiri Pirko9f407f12018-07-23 09:23:07 +02001896 err = tc_chain_tmplt_add(chain, net, tca, extack);
1897 if (err)
1898 goto errout;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001899 /* In case the chain was successfully added, take a reference
1900 * to the chain. This ensures that an empty chain
1901 * does not disappear at the end of this function.
1902 */
1903 tcf_chain_hold(chain);
1904 chain->explicitly_created = true;
1905 tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL,
1906 RTM_NEWCHAIN, false);
1907 break;
1908 case RTM_DELCHAIN:
Cong Wangf5b9bac2018-09-11 14:22:23 -07001909 tfilter_notify_chain(net, skb, block, q, parent, n,
1910 chain, RTM_DELTFILTER);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001911 /* Flush the chain first as the user requested chain removal. */
1912 tcf_chain_flush(chain);
1913 /* In case the chain was successfully deleted, put a reference
1914 * to the chain previously taken during addition.
1915 */
1916 tcf_chain_put_explicitly_created(chain);
Jiri Pirkoc921d7d2018-07-26 18:27:58 +02001917 chain->explicitly_created = false;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001918 break;
1919 case RTM_GETCHAIN:
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001920 err = tc_chain_notify(chain, skb, n->nlmsg_seq,
1921 n->nlmsg_seq, n->nlmsg_type, true);
1922 if (err < 0)
1923 NL_SET_ERR_MSG(extack, "Failed to send chain notify message");
1924 break;
1925 default:
1926 err = -EOPNOTSUPP;
1927 NL_SET_ERR_MSG(extack, "Unsupported message type");
1928 goto errout;
1929 }
1930
1931errout:
1932 tcf_chain_put(chain);
1933 if (err == -EAGAIN)
1934 /* Replay the request. */
1935 goto replay;
1936 return err;
1937}
1938
1939/* called with RTNL */
1940static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb)
1941{
1942 struct net *net = sock_net(skb->sk);
1943 struct nlattr *tca[TCA_MAX + 1];
1944 struct Qdisc *q = NULL;
1945 struct tcf_block *block;
1946 struct tcf_chain *chain;
1947 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1948 long index_start;
1949 long index;
1950 u32 parent;
1951 int err;
1952
1953 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
1954 return skb->len;
1955
Davide Carattie3314732018-10-10 22:00:58 +02001956 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy,
1957 NULL);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02001958 if (err)
1959 return err;
1960
1961 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1962 block = tcf_block_lookup(net, tcm->tcm_block_index);
1963 if (!block)
1964 goto out;
1965 /* If we work with block index, q is NULL and parent value
1966 * will never be used in the following code. The check
1967 * in tcf_fill_node prevents it. However, compiler does not
1968 * see that far, so set parent to zero to silence the warning
1969 * about parent being uninitialized.
1970 */
1971 parent = 0;
1972 } else {
1973 const struct Qdisc_class_ops *cops;
1974 struct net_device *dev;
1975 unsigned long cl = 0;
1976
1977 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1978 if (!dev)
1979 return skb->len;
1980
1981 parent = tcm->tcm_parent;
1982 if (!parent) {
1983 q = dev->qdisc;
1984 parent = q->handle;
1985 } else {
1986 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1987 }
1988 if (!q)
1989 goto out;
1990 cops = q->ops->cl_ops;
1991 if (!cops)
1992 goto out;
1993 if (!cops->tcf_block)
1994 goto out;
1995 if (TC_H_MIN(tcm->tcm_parent)) {
1996 cl = cops->find(q, tcm->tcm_parent);
1997 if (cl == 0)
1998 goto out;
1999 }
2000 block = cops->tcf_block(q, cl, NULL);
2001 if (!block)
2002 goto out;
2003 if (tcf_block_shared(block))
2004 q = NULL;
2005 }
2006
2007 index_start = cb->args[0];
2008 index = 0;
2009
2010 list_for_each_entry(chain, &block->chain_list, list) {
2011 if ((tca[TCA_CHAIN] &&
2012 nla_get_u32(tca[TCA_CHAIN]) != chain->index))
2013 continue;
2014 if (index < index_start) {
2015 index++;
2016 continue;
2017 }
Jiri Pirko3d32f4c2018-08-01 12:36:55 +02002018 if (tcf_chain_held_by_acts_only(chain))
Jiri Pirko1f3ed382018-07-27 09:45:05 +02002019 continue;
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002020 err = tc_chain_fill_node(chain, net, skb, block,
2021 NETLINK_CB(cb->skb).portid,
2022 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2023 RTM_NEWCHAIN);
2024 if (err <= 0)
2025 break;
2026 index++;
2027 }
2028
2029 cb->args[0] = index;
2030
2031out:
2032 /* If we did no progress, the error (EMSGSIZE) is real */
2033 if (skb->len == 0 && err)
2034 return err;
2035 return skb->len;
2036}
2037
WANG Cong18d02642014-09-25 10:26:37 -07002038void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040#ifdef CONFIG_NET_CLS_ACT
Eric Dumazeta749eea2019-09-18 12:57:04 -07002041 if (exts->actions) {
2042 tcf_action_destroy(exts->actions, TCA_ACT_UNBIND);
2043 kfree(exts->actions);
2044 }
WANG Cong22dc13c2016-08-13 22:35:00 -07002045 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046#endif
2047}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002048EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00002050int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05002051 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
2052 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054#ifdef CONFIG_NET_CLS_ACT
2055 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct tc_action *act;
Roman Mashakd04e6992018-03-08 16:59:17 -05002057 size_t attr_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
WANG Cong5da57f42013-12-15 20:15:07 -08002059 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002060 act = tcf_action_init_1(net, tp, tb[exts->police],
2061 rate_tlv, "police", ovr,
Vlad Buslov789871b2018-07-05 17:24:25 +03002062 TCA_ACT_BIND, true, extack);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08002063 if (IS_ERR(act))
2064 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
WANG Cong33be6272013-12-15 20:15:05 -08002066 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07002067 exts->actions[0] = act;
2068 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08002069 } else if (exts->action && tb[exts->action]) {
Vlad Buslov90b73b72018-07-05 17:24:33 +03002070 int err;
WANG Cong22dc13c2016-08-13 22:35:00 -07002071
Jiri Pirko9fb9f252017-05-17 11:08:02 +02002072 err = tcf_action_init(net, tp, tb[exts->action],
2073 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Vlad Buslov90b73b72018-07-05 17:24:33 +03002074 exts->actions, &attr_size, true,
Vlad Buslov789871b2018-07-05 17:24:25 +03002075 extack);
Vlad Buslov90b73b72018-07-05 17:24:33 +03002076 if (err < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002077 return err;
Vlad Buslov90b73b72018-07-05 17:24:33 +03002078 exts->nr_actions = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
Cong Wange4b95c42017-11-06 13:47:19 -08002080 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#else
WANG Cong5da57f42013-12-15 20:15:07 -08002083 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05002084 (exts->police && tb[exts->police])) {
2085 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05002087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088#endif
2089
2090 return 0;
2091}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002092EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002094void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
2096#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07002097 struct tcf_exts old = *dst;
2098
Jiri Pirko9b0d4442017-08-04 14:29:15 +02002099 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07002100 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101#endif
2102}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002103EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
WANG Cong22dc13c2016-08-13 22:35:00 -07002105#ifdef CONFIG_NET_CLS_ACT
2106static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
2107{
2108 if (exts->nr_actions == 0)
2109 return NULL;
2110 else
2111 return exts->actions[0];
2112}
2113#endif
WANG Cong33be6272013-12-15 20:15:05 -08002114
WANG Cong5da57f42013-12-15 20:15:07 -08002115int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
2117#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07002118 struct nlattr *nest;
2119
Jiri Pirko978dfd82017-08-04 14:29:03 +02002120 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /*
2122 * again for backward compatible mode - we want
2123 * to work with both old and new modes of entering
2124 * tc data even if iproute2 was newer - jhs
2125 */
WANG Cong33be6272013-12-15 20:15:05 -08002126 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong5da57f42013-12-15 20:15:07 -08002127 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002128 if (nest == NULL)
2129 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07002130
Vlad Buslov90b73b72018-07-05 17:24:33 +03002131 if (tcf_action_dump(skb, exts->actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002132 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002133 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08002134 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08002135 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08002136 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05002137 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002138 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08002139 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08002140 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08002141 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07002145
2146nla_put_failure:
2147 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07002149#else
2150 return 0;
2151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002153EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002155
WANG Cong5da57f42013-12-15 20:15:07 -08002156int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08002159 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01002160 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08002161 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#endif
2163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08002165EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Jiri Pirko717503b2017-10-11 09:41:09 +02002167static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
2168 enum tc_setup_type type,
2169 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002170{
2171 int ok_count = 0;
2172#ifdef CONFIG_NET_CLS_ACT
2173 const struct tc_action *a;
2174 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002175 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002176
2177 if (!tcf_exts_has_actions(exts))
2178 return 0;
2179
Or Gerlitz9d452ce2017-10-24 08:58:02 +03002180 for (i = 0; i < exts->nr_actions; i++) {
2181 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002182 if (!a->ops->get_dev)
2183 continue;
2184 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01002185 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002186 continue;
2187 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
Vlad Buslov84a75b32018-08-10 20:51:52 +03002188 a->ops->put_dev(dev);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002189 if (ret < 0)
2190 return ret;
2191 ok_count += ret;
2192 }
2193#endif
2194 return ok_count;
2195}
Jiri Pirko717503b2017-10-11 09:41:09 +02002196
Jiri Pirko208c0f42017-10-19 15:50:32 +02002197int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
2198 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02002199{
David S. Miller9a99dc12018-06-06 13:55:47 -04002200 int ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002201 int ret;
2202
David S. Miller9a99dc12018-06-06 13:55:47 -04002203 ret = tcf_block_cb_call(block, type, type_data, err_stop);
2204 if (ret < 0)
2205 return ret;
2206 ok_count = ret;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002207
Or Gerlitzf8f4bef2018-05-23 19:24:48 +03002208 if (!exts || ok_count)
David S. Miller9a99dc12018-06-06 13:55:47 -04002209 return ok_count;
Jiri Pirko208c0f42017-10-19 15:50:32 +02002210 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
2211 if (ret < 0)
2212 return ret;
2213 ok_count += ret;
2214
2215 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02002216}
2217EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02002218
Jiri Pirko48617382018-01-17 11:46:46 +01002219static __net_init int tcf_net_init(struct net *net)
2220{
2221 struct tcf_net *tn = net_generic(net, tcf_net_id);
2222
2223 idr_init(&tn->idr);
2224 return 0;
2225}
2226
2227static void __net_exit tcf_net_exit(struct net *net)
2228{
2229 struct tcf_net *tn = net_generic(net, tcf_net_id);
2230
2231 idr_destroy(&tn->idr);
2232}
2233
2234static struct pernet_operations tcf_net_ops = {
2235 .init = tcf_net_init,
2236 .exit = tcf_net_exit,
2237 .id = &tcf_net_id,
2238 .size = sizeof(struct tcf_net),
2239};
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241static int __init tc_filter_init(void)
2242{
Jiri Pirko48617382018-01-17 11:46:46 +01002243 int err;
2244
Cong Wang7aa00452017-10-26 18:24:28 -07002245 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
2246 if (!tc_filter_wq)
2247 return -ENOMEM;
2248
Jiri Pirko48617382018-01-17 11:46:46 +01002249 err = register_pernet_subsys(&tcf_net_ops);
2250 if (err)
2251 goto err_register_pernet_subsys;
2252
Vlad Buslovc431f892018-05-31 09:52:53 +03002253 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0);
2254 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0);
2255 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02002256 tc_dump_tfilter, 0);
Jiri Pirko32a4f5e2018-07-23 09:23:06 +02002257 rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0);
2258 rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0);
2259 rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain,
2260 tc_dump_chain, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01002263
2264err_register_pernet_subsys:
2265 destroy_workqueue(tc_filter_wq);
2266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
2269subsys_initcall(tc_filter_init);