blob: e500d11da9cd5a84d89ab8f6c2b1d0d593651448 [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 Wang7aa00452017-10-26 18:24:28 -0700106bool tcf_queue_work(struct work_struct *work)
107{
108 return queue_work(tc_filter_wq, work);
109}
110EXPORT_SYMBOL(tcf_queue_work);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* Select new prio value from the range, managed by kernel. */
113
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800114static inline u32 tcf_auto_prio(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800116 u32 first = TC_H_MAKE(0xC0000000U, 0U);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (tp)
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000119 first = tp->prio - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Jiri Pirko79619732017-05-17 11:07:58 +0200121 return TC_H_MAJ(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Jiri Pirko33a48922017-02-09 14:38:57 +0100124static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
Jiri Pirkoedf67112018-01-17 11:46:49 +0100125 u32 prio, struct tcf_chain *chain)
Jiri Pirko33a48922017-02-09 14:38:57 +0100126{
127 struct tcf_proto *tp;
128 int err;
129
130 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
131 if (!tp)
132 return ERR_PTR(-ENOBUFS);
133
134 err = -ENOENT;
135 tp->ops = tcf_proto_lookup_ops(kind);
136 if (!tp->ops) {
137#ifdef CONFIG_MODULES
138 rtnl_unlock();
139 request_module("cls_%s", kind);
140 rtnl_lock();
141 tp->ops = tcf_proto_lookup_ops(kind);
142 /* We dropped the RTNL semaphore in order to perform
143 * the module load. So, even if we succeeded in loading
144 * the module we have to replay the request. We indicate
145 * this using -EAGAIN.
146 */
147 if (tp->ops) {
148 module_put(tp->ops->owner);
149 err = -EAGAIN;
150 } else {
151 err = -ENOENT;
152 }
153 goto errout;
154#endif
155 }
156 tp->classify = tp->ops->classify;
157 tp->protocol = protocol;
158 tp->prio = prio;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200159 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100160
161 err = tp->ops->init(tp);
162 if (err) {
163 module_put(tp->ops->owner);
164 goto errout;
165 }
166 return tp;
167
168errout:
169 kfree(tp);
170 return ERR_PTR(err);
171}
172
WANG Cong763dbf62017-04-19 14:21:21 -0700173static void tcf_proto_destroy(struct tcf_proto *tp)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100174{
WANG Cong763dbf62017-04-19 14:21:21 -0700175 tp->ops->destroy(tp);
176 module_put(tp->ops->owner);
177 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100178}
179
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100180struct tcf_filter_chain_list_item {
181 struct list_head list;
182 tcf_chain_head_change_t *chain_head_change;
183 void *chain_head_change_priv;
184};
185
Jiri Pirko5bc17012017-05-17 11:08:01 +0200186static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
187 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200188{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200189 struct tcf_chain *chain;
190
191 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
192 if (!chain)
193 return NULL;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100194 INIT_LIST_HEAD(&chain->filter_chain_list);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200195 list_add_tail(&chain->list, &block->chain_list);
196 chain->block = block;
197 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700198 chain->refcnt = 1;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200199 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200200}
201
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100202static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
203 struct tcf_proto *tp_head)
204{
205 if (item->chain_head_change)
206 item->chain_head_change(tp_head, item->chain_head_change_priv);
207}
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100208static void tcf_chain_head_change(struct tcf_chain *chain,
209 struct tcf_proto *tp_head)
210{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100211 struct tcf_filter_chain_list_item *item;
212
213 list_for_each_entry(item, &chain->filter_chain_list, list)
214 tcf_chain_head_change_item(item, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100215}
216
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200217static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100218{
Roman Kapld7aa04a2017-11-20 22:21:13 +0100219 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100220
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100221 tcf_chain_head_change(chain, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100222 while (tp) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200223 RCU_INIT_POINTER(chain->filter_chain, tp->next);
WANG Cong763dbf62017-04-19 14:21:21 -0700224 tcf_proto_destroy(tp);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100225 tp = rtnl_dereference(chain->filter_chain);
226 tcf_chain_put(chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100227 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200228}
229
230static void tcf_chain_destroy(struct tcf_chain *chain)
231{
Cong Wangefbf7892017-12-04 10:48:18 -0800232 struct tcf_block *block = chain->block;
233
Cong Wange2ef7542017-09-11 16:33:31 -0700234 list_del(&chain->list);
235 kfree(chain);
Cong Wangefbf7892017-12-04 10:48:18 -0800236 if (list_empty(&block->chain_list))
237 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700238}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200239
Cong Wange2ef7542017-09-11 16:33:31 -0700240static void tcf_chain_hold(struct tcf_chain *chain)
241{
242 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200243}
244
WANG Cong367a8ce2017-05-23 09:42:37 -0700245struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
246 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200247{
248 struct tcf_chain *chain;
249
250 list_for_each_entry(chain, &block->chain_list, list) {
Cong Wange2ef7542017-09-11 16:33:31 -0700251 if (chain->index == chain_index) {
252 tcf_chain_hold(chain);
253 return chain;
254 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200255 }
Jiri Pirko80532382017-09-06 13:14:19 +0200256
Cong Wange2ef7542017-09-11 16:33:31 -0700257 return create ? tcf_chain_create(block, chain_index) : NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200258}
259EXPORT_SYMBOL(tcf_chain_get);
260
261void tcf_chain_put(struct tcf_chain *chain)
262{
Cong Wange2ef7542017-09-11 16:33:31 -0700263 if (--chain->refcnt == 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200264 tcf_chain_destroy(chain);
265}
266EXPORT_SYMBOL(tcf_chain_put);
267
Jiri Pirkocaa72602018-01-17 11:46:50 +0100268static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200269{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100270 return block->offloadcnt;
271}
272
273static int tcf_block_offload_cmd(struct tcf_block *block,
274 struct net_device *dev,
275 struct tcf_block_ext_info *ei,
276 enum tc_block_command command)
277{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200278 struct tc_block_offload bo = {};
279
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200280 bo.command = command;
281 bo.binder_type = ei->binder_type;
282 bo.block = block;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100283 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200284}
285
Jiri Pirkocaa72602018-01-17 11:46:50 +0100286static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
287 struct tcf_block_ext_info *ei)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200288{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100289 struct net_device *dev = q->dev_queue->dev;
290 int err;
291
292 if (!dev->netdev_ops->ndo_setup_tc)
293 goto no_offload_dev_inc;
294
295 /* If tc offload feature is disabled and the block we try to bind
296 * to already has some offloaded filters, forbid to bind.
297 */
298 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block))
299 return -EOPNOTSUPP;
300
301 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND);
302 if (err == -EOPNOTSUPP)
303 goto no_offload_dev_inc;
304 return err;
305
306no_offload_dev_inc:
307 if (tcf_block_offload_in_use(block))
308 return -EOPNOTSUPP;
309 block->nooffloaddevcnt++;
310 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200311}
312
313static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
314 struct tcf_block_ext_info *ei)
315{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100316 struct net_device *dev = q->dev_queue->dev;
317 int err;
318
319 if (!dev->netdev_ops->ndo_setup_tc)
320 goto no_offload_dev_dec;
321 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND);
322 if (err == -EOPNOTSUPP)
323 goto no_offload_dev_dec;
324 return;
325
326no_offload_dev_dec:
327 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200328}
329
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100330static int
331tcf_chain_head_change_cb_add(struct tcf_chain *chain,
332 struct tcf_block_ext_info *ei,
333 struct netlink_ext_ack *extack)
334{
335 struct tcf_filter_chain_list_item *item;
336
337 item = kmalloc(sizeof(*item), GFP_KERNEL);
338 if (!item) {
339 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
340 return -ENOMEM;
341 }
342 item->chain_head_change = ei->chain_head_change;
343 item->chain_head_change_priv = ei->chain_head_change_priv;
344 if (chain->filter_chain)
345 tcf_chain_head_change_item(item, chain->filter_chain);
346 list_add(&item->list, &chain->filter_chain_list);
347 return 0;
348}
349
350static void
351tcf_chain_head_change_cb_del(struct tcf_chain *chain,
352 struct tcf_block_ext_info *ei)
353{
354 struct tcf_filter_chain_list_item *item;
355
356 list_for_each_entry(item, &chain->filter_chain_list, list) {
357 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
358 (item->chain_head_change == ei->chain_head_change &&
359 item->chain_head_change_priv == ei->chain_head_change_priv)) {
360 tcf_chain_head_change_item(item, NULL);
361 list_del(&item->list);
362 kfree(item);
363 return;
364 }
365 }
366 WARN_ON(1);
367}
368
Jiri Pirko48617382018-01-17 11:46:46 +0100369struct tcf_net {
370 struct idr idr;
371};
372
373static unsigned int tcf_net_id;
374
375static int tcf_block_insert(struct tcf_block *block, struct net *net,
376 u32 block_index, struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100377{
Jiri Pirko48617382018-01-17 11:46:46 +0100378 struct tcf_net *tn = net_generic(net, tcf_net_id);
379 int err;
380
381 err = idr_alloc_ext(&tn->idr, block, NULL, block_index,
382 block_index + 1, GFP_KERNEL);
383 if (err)
384 return err;
385 block->index = block_index;
386 return 0;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100387}
388
Jiri Pirko48617382018-01-17 11:46:46 +0100389static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200390{
Jiri Pirko48617382018-01-17 11:46:46 +0100391 struct tcf_net *tn = net_generic(net, tcf_net_id);
392
393 idr_remove_ext(&tn->idr, block->index);
394}
395
396static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
397 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->net = qdisc_net(q);
420 block->refcnt = 1;
421 block->net = net;
422 block->q = q;
423 return block;
424
425err_chain_create:
426 kfree(block);
427 return ERR_PTR(err);
428}
429
430static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
431{
432 struct tcf_net *tn = net_generic(net, tcf_net_id);
433
434 return idr_find_ext(&tn->idr, block_index);
435}
436
437static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
438{
439 return list_first_entry(&block->chain_list, struct tcf_chain, list);
440}
441
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100442struct tcf_block_owner_item {
443 struct list_head list;
444 struct Qdisc *q;
445 enum tcf_block_binder_type binder_type;
446};
447
448static void
449tcf_block_owner_netif_keep_dst(struct tcf_block *block,
450 struct Qdisc *q,
451 enum tcf_block_binder_type binder_type)
452{
453 if (block->keep_dst &&
454 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
455 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
456 netif_keep_dst(qdisc_dev(q));
457}
458
459void tcf_block_netif_keep_dst(struct tcf_block *block)
460{
461 struct tcf_block_owner_item *item;
462
463 block->keep_dst = true;
464 list_for_each_entry(item, &block->owner_list, list)
465 tcf_block_owner_netif_keep_dst(block, item->q,
466 item->binder_type);
467}
468EXPORT_SYMBOL(tcf_block_netif_keep_dst);
469
470static int tcf_block_owner_add(struct tcf_block *block,
471 struct Qdisc *q,
472 enum tcf_block_binder_type binder_type)
473{
474 struct tcf_block_owner_item *item;
475
476 item = kmalloc(sizeof(*item), GFP_KERNEL);
477 if (!item)
478 return -ENOMEM;
479 item->q = q;
480 item->binder_type = binder_type;
481 list_add(&item->list, &block->owner_list);
482 return 0;
483}
484
485static void tcf_block_owner_del(struct tcf_block *block,
486 struct Qdisc *q,
487 enum tcf_block_binder_type binder_type)
488{
489 struct tcf_block_owner_item *item;
490
491 list_for_each_entry(item, &block->owner_list, list) {
492 if (item->q == q && item->binder_type == binder_type) {
493 list_del(&item->list);
494 kfree(item);
495 return;
496 }
497 }
498 WARN_ON(1);
499}
500
Jiri Pirko48617382018-01-17 11:46:46 +0100501int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
502 struct tcf_block_ext_info *ei,
503 struct netlink_ext_ack *extack)
504{
505 struct net *net = qdisc_net(q);
506 struct tcf_block *block = NULL;
507 bool created = false;
508 int err;
509
510 if (ei->block_index) {
511 /* block_index not 0 means the shared block is requested */
512 block = tcf_block_lookup(net, ei->block_index);
513 if (block)
514 block->refcnt++;
515 }
516
517 if (!block) {
518 block = tcf_block_create(net, q, extack);
519 if (IS_ERR(block))
520 return PTR_ERR(block);
521 created = true;
522 if (ei->block_index) {
523 err = tcf_block_insert(block, net,
524 ei->block_index, extack);
525 if (err)
526 goto err_block_insert;
527 }
528 }
529
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100530 err = tcf_block_owner_add(block, q, ei->binder_type);
531 if (err)
532 goto err_block_owner_add;
533
534 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
535
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100536 err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block),
537 ei, extack);
538 if (err)
539 goto err_chain_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100540
541 err = tcf_block_offload_bind(block, q, ei);
542 if (err)
543 goto err_block_offload_bind;
544
Jiri Pirko6529eab2017-05-17 11:07:55 +0200545 *p_block = block;
546 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200547
Jiri Pirkocaa72602018-01-17 11:46:50 +0100548err_block_offload_bind:
549 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100550err_chain_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100551 tcf_block_owner_del(block, q, ei->binder_type);
552err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100553 if (created) {
554 if (tcf_block_shared(block))
555 tcf_block_remove(block, net);
556err_block_insert:
557 kfree(tcf_block_chain_zero(block));
558 kfree(block);
559 } else {
560 block->refcnt--;
561 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200562 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200563}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200564EXPORT_SYMBOL(tcf_block_get_ext);
565
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100566static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
567{
568 struct tcf_proto __rcu **p_filter_chain = priv;
569
570 rcu_assign_pointer(*p_filter_chain, tp_head);
571}
572
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200573int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500574 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
575 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200576{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100577 struct tcf_block_ext_info ei = {
578 .chain_head_change = tcf_chain_head_change_dflt,
579 .chain_head_change_priv = p_filter_chain,
580 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200581
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100582 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500583 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200584}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200585EXPORT_SYMBOL(tcf_block_get);
586
Cong Wang7aa00452017-10-26 18:24:28 -0700587/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100588 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700589 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100590void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900591 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700592{
Cong Wangefbf7892017-12-04 10:48:18 -0800593 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700594
David S. Millerc30abd52017-12-16 22:11:55 -0500595 if (!block)
596 return;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100597 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100598 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100599
Jiri Pirko48617382018-01-17 11:46:46 +0100600 if (--block->refcnt == 0) {
601 if (tcf_block_shared(block))
602 tcf_block_remove(block, block->net);
603
604 /* Hold a refcnt for all chains, so that they don't disappear
605 * while we are iterating.
606 */
607 list_for_each_entry(chain, &block->chain_list, list)
608 tcf_chain_hold(chain);
609
610 list_for_each_entry(chain, &block->chain_list, list)
611 tcf_chain_flush(chain);
612 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700613
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100614 tcf_block_offload_unbind(block, q, ei);
615
Jiri Pirko48617382018-01-17 11:46:46 +0100616 if (block->refcnt == 0) {
617 /* At this point, all the chains should have refcnt >= 1. */
618 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
619 tcf_chain_put(chain);
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100620
Jiri Pirko48617382018-01-17 11:46:46 +0100621 /* Finally, put chain 0 and allow block to be freed. */
622 tcf_chain_put(tcf_block_chain_zero(block));
623 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200624}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200625EXPORT_SYMBOL(tcf_block_put_ext);
626
627void tcf_block_put(struct tcf_block *block)
628{
629 struct tcf_block_ext_info ei = {0, };
630
Jiri Pirko4853f122017-12-21 13:13:59 +0100631 if (!block)
632 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100633 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200634}
David S. Millere1ea2f92017-10-30 14:10:01 +0900635
Jiri Pirko6529eab2017-05-17 11:07:55 +0200636EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100637
Jiri Pirkoacb67442017-10-19 15:50:31 +0200638struct tcf_block_cb {
639 struct list_head list;
640 tc_setup_cb_t *cb;
641 void *cb_ident;
642 void *cb_priv;
643 unsigned int refcnt;
644};
645
646void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
647{
648 return block_cb->cb_priv;
649}
650EXPORT_SYMBOL(tcf_block_cb_priv);
651
652struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
653 tc_setup_cb_t *cb, void *cb_ident)
654{ struct tcf_block_cb *block_cb;
655
656 list_for_each_entry(block_cb, &block->cb_list, list)
657 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
658 return block_cb;
659 return NULL;
660}
661EXPORT_SYMBOL(tcf_block_cb_lookup);
662
663void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
664{
665 block_cb->refcnt++;
666}
667EXPORT_SYMBOL(tcf_block_cb_incref);
668
669unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
670{
671 return --block_cb->refcnt;
672}
673EXPORT_SYMBOL(tcf_block_cb_decref);
674
675struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
676 tc_setup_cb_t *cb, void *cb_ident,
677 void *cb_priv)
678{
679 struct tcf_block_cb *block_cb;
680
Jiri Pirkocaa72602018-01-17 11:46:50 +0100681 /* At this point, playback of previous block cb calls is not supported,
682 * so forbid to register to block which already has some offloaded
683 * filters present.
684 */
685 if (tcf_block_offload_in_use(block))
686 return ERR_PTR(-EOPNOTSUPP);
687
Jiri Pirkoacb67442017-10-19 15:50:31 +0200688 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
689 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100690 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200691 block_cb->cb = cb;
692 block_cb->cb_ident = cb_ident;
693 block_cb->cb_priv = cb_priv;
694 list_add(&block_cb->list, &block->cb_list);
695 return block_cb;
696}
697EXPORT_SYMBOL(__tcf_block_cb_register);
698
699int tcf_block_cb_register(struct tcf_block *block,
700 tc_setup_cb_t *cb, void *cb_ident,
701 void *cb_priv)
702{
703 struct tcf_block_cb *block_cb;
704
705 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100706 return IS_ERR(block_cb) ? PTR_ERR(block_cb) : 0;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200707}
708EXPORT_SYMBOL(tcf_block_cb_register);
709
710void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
711{
712 list_del(&block_cb->list);
713 kfree(block_cb);
714}
715EXPORT_SYMBOL(__tcf_block_cb_unregister);
716
717void tcf_block_cb_unregister(struct tcf_block *block,
718 tc_setup_cb_t *cb, void *cb_ident)
719{
720 struct tcf_block_cb *block_cb;
721
722 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
723 if (!block_cb)
724 return;
725 __tcf_block_cb_unregister(block_cb);
726}
727EXPORT_SYMBOL(tcf_block_cb_unregister);
728
729static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
730 void *type_data, bool err_stop)
731{
732 struct tcf_block_cb *block_cb;
733 int ok_count = 0;
734 int err;
735
Jiri Pirkocaa72602018-01-17 11:46:50 +0100736 /* Make sure all netdevs sharing this block are offload-capable. */
737 if (block->nooffloaddevcnt && err_stop)
738 return -EOPNOTSUPP;
739
Jiri Pirkoacb67442017-10-19 15:50:31 +0200740 list_for_each_entry(block_cb, &block->cb_list, list) {
741 err = block_cb->cb(type, type_data, block_cb->cb_priv);
742 if (err) {
743 if (err_stop)
744 return err;
745 } else {
746 ok_count++;
747 }
748 }
749 return ok_count;
750}
751
Jiri Pirko87d83092017-05-17 11:07:54 +0200752/* Main classifier routine: scans classifier chain attached
753 * to this qdisc, (optionally) tests for protocol and asks
754 * specific classifiers.
755 */
756int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
757 struct tcf_result *res, bool compat_mode)
758{
759 __be16 protocol = tc_skb_protocol(skb);
760#ifdef CONFIG_NET_CLS_ACT
761 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200762 const struct tcf_proto *orig_tp = tp;
763 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200764 int limit = 0;
765
766reclassify:
767#endif
768 for (; tp; tp = rcu_dereference_bh(tp->next)) {
769 int err;
770
771 if (tp->protocol != protocol &&
772 tp->protocol != htons(ETH_P_ALL))
773 continue;
774
775 err = tp->classify(skb, tp, res);
776#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200777 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200778 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200779 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200780 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200781 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200782 goto reset;
783 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200784#endif
785 if (err >= 0)
786 return err;
787 }
788
789 return TC_ACT_UNSPEC; /* signal: continue lookup */
790#ifdef CONFIG_NET_CLS_ACT
791reset:
792 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100793 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
794 tp->chain->block->index,
795 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200796 ntohs(tp->protocol));
797 return TC_ACT_SHOT;
798 }
799
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200800 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200801 protocol = tc_skb_protocol(skb);
802 goto reclassify;
803#endif
804}
805EXPORT_SYMBOL(tcf_classify);
806
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200807struct tcf_chain_info {
808 struct tcf_proto __rcu **pprev;
809 struct tcf_proto __rcu *next;
810};
811
812static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
813{
814 return rtnl_dereference(*chain_info->pprev);
815}
816
817static void tcf_chain_tp_insert(struct tcf_chain *chain,
818 struct tcf_chain_info *chain_info,
819 struct tcf_proto *tp)
820{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100821 if (*chain_info->pprev == chain->filter_chain)
822 tcf_chain_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200823 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
824 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700825 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200826}
827
828static void tcf_chain_tp_remove(struct tcf_chain *chain,
829 struct tcf_chain_info *chain_info,
830 struct tcf_proto *tp)
831{
832 struct tcf_proto *next = rtnl_dereference(chain_info->next);
833
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100834 if (tp == chain->filter_chain)
835 tcf_chain_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200836 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700837 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200838}
839
840static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
841 struct tcf_chain_info *chain_info,
842 u32 protocol, u32 prio,
843 bool prio_allocate)
844{
845 struct tcf_proto **pprev;
846 struct tcf_proto *tp;
847
848 /* Check the chain for existence of proto-tcf with this priority */
849 for (pprev = &chain->filter_chain;
850 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
851 if (tp->prio >= prio) {
852 if (tp->prio == prio) {
853 if (prio_allocate ||
854 (tp->protocol != protocol && protocol))
855 return ERR_PTR(-EINVAL);
856 } else {
857 tp = NULL;
858 }
859 break;
860 }
861 }
862 chain_info->pprev = pprev;
863 chain_info->next = tp ? tp->next : NULL;
864 return tp;
865}
866
WANG Cong71203712017-08-07 15:26:50 -0700867static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100868 struct tcf_proto *tp, struct tcf_block *block,
869 struct Qdisc *q, u32 parent, void *fh,
870 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700871{
872 struct tcmsg *tcm;
873 struct nlmsghdr *nlh;
874 unsigned char *b = skb_tail_pointer(skb);
875
876 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
877 if (!nlh)
878 goto out_nlmsg_trim;
879 tcm = nlmsg_data(nlh);
880 tcm->tcm_family = AF_UNSPEC;
881 tcm->tcm__pad1 = 0;
882 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100883 if (q) {
884 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
885 tcm->tcm_parent = parent;
886 } else {
887 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
888 tcm->tcm_block_index = block->index;
889 }
WANG Cong71203712017-08-07 15:26:50 -0700890 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
891 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
892 goto nla_put_failure;
893 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
894 goto nla_put_failure;
895 if (!fh) {
896 tcm->tcm_handle = 0;
897 } else {
898 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
899 goto nla_put_failure;
900 }
901 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
902 return skb->len;
903
904out_nlmsg_trim:
905nla_put_failure:
906 nlmsg_trim(skb, b);
907 return -1;
908}
909
910static int tfilter_notify(struct net *net, struct sk_buff *oskb,
911 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100912 struct tcf_block *block, struct Qdisc *q,
913 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -0700914{
915 struct sk_buff *skb;
916 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
917
918 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
919 if (!skb)
920 return -ENOBUFS;
921
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100922 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
923 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -0700924 kfree_skb(skb);
925 return -EINVAL;
926 }
927
928 if (unicast)
929 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
930
931 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
932 n->nlmsg_flags & NLM_F_ECHO);
933}
934
935static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
936 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100937 struct tcf_block *block, struct Qdisc *q,
938 u32 parent, void *fh, bool unicast, bool *last)
WANG Cong71203712017-08-07 15:26:50 -0700939{
940 struct sk_buff *skb;
941 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
942 int err;
943
944 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
945 if (!skb)
946 return -ENOBUFS;
947
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100948 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
949 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -0700950 kfree_skb(skb);
951 return -EINVAL;
952 }
953
954 err = tp->ops->delete(tp, fh, last);
955 if (err) {
956 kfree_skb(skb);
957 return err;
958 }
959
960 if (unicast)
961 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
962
963 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
964 n->nlmsg_flags & NLM_F_ECHO);
965}
966
967static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100968 struct tcf_block *block, struct Qdisc *q,
969 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -0700970 struct tcf_chain *chain, int event)
971{
972 struct tcf_proto *tp;
973
974 for (tp = rtnl_dereference(chain->filter_chain);
975 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100976 tfilter_notify(net, oskb, n, tp, block,
977 q, parent, 0, event, false);
WANG Cong71203712017-08-07 15:26:50 -0700978}
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/* Add/change/delete/get a filter node */
981
David Ahernc21ef3e2017-04-16 09:48:24 -0700982static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
983 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900985 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800986 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 struct tcmsg *t;
988 u32 protocol;
989 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200990 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200992 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100993 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200994 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200995 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200996 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -0700999 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001001 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Stéphane Graber4e8bbb82014-04-30 11:25:43 -04001003 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -04001004 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001005 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001008 tp_created = 0;
1009
David Ahernc21ef3e2017-04-16 09:48:24 -07001010 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001011 if (err < 0)
1012 return err;
1013
David S. Miller942b8162012-06-26 21:48:50 -07001014 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 protocol = TC_H_MIN(t->tcm_info);
1016 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001017 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 parent = t->tcm_parent;
1019 cl = 0;
1020
1021 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001022 switch (n->nlmsg_type) {
1023 case RTM_DELTFILTER:
Daniel Borkmann9f6ed032016-06-16 23:19:29 +02001024 if (protocol || t->tcm_handle || tca[TCA_KIND])
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001025 return -ENOENT;
1026 break;
1027 case RTM_NEWTFILTER:
1028 /* If no priority is provided by the user,
1029 * we allocate one.
1030 */
1031 if (n->nlmsg_flags & NLM_F_CREATE) {
1032 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001033 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001034 break;
1035 }
1036 /* fall-through */
1037 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041
1042 /* Find head of filter chain. */
1043
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001044 if (t->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1045 block = tcf_block_lookup(net, t->tcm_block_index);
1046 if (!block) {
1047 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1048 err = -EINVAL;
1049 goto errout;
1050 }
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001051 } else {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001052 const struct Qdisc_class_ops *cops;
1053 struct net_device *dev;
1054
1055 /* Find link */
1056 dev = __dev_get_by_index(net, t->tcm_ifindex);
1057 if (!dev)
1058 return -ENODEV;
1059
1060 /* Find qdisc */
1061 if (!parent) {
1062 q = dev->qdisc;
1063 parent = q->handle;
1064 } else {
1065 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
1066 if (!q)
1067 return -EINVAL;
1068 }
1069
1070 /* Is it classful? */
1071 cops = q->ops->cl_ops;
1072 if (!cops)
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001073 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001075 if (!cops->tcf_block)
1076 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001078 /* Do we search for filter, attached to class? */
1079 if (TC_H_MIN(parent)) {
1080 cl = cops->find(q, parent);
1081 if (cl == 0)
1082 return -ENOENT;
1083 }
Patrick McHardy71ebe5e2009-09-04 06:41:15 +00001084
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001085 /* And the last stroke */
1086 block = cops->tcf_block(q, cl, extack);
1087 if (!block) {
1088 err = -EINVAL;
1089 goto errout;
1090 }
1091 if (tcf_block_shared(block)) {
1092 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1093 err = -EOPNOTSUPP;
1094 goto errout;
1095 }
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001096 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001097
1098 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1099 if (chain_index > TC_ACT_EXT_VAL_MASK) {
1100 err = -EINVAL;
1101 goto errout;
1102 }
WANG Cong367a8ce2017-05-23 09:42:37 -07001103 chain = tcf_chain_get(block, chain_index,
1104 n->nlmsg_type == RTM_NEWTFILTER);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001105 if (!chain) {
WANG Cong367a8ce2017-05-23 09:42:37 -07001106 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001107 goto errout;
1108 }
Jiri Pirko6529eab2017-05-17 11:07:55 +02001109
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001110 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001111 tfilter_notify_chain(net, skb, block, q, parent, n,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001112 chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +02001113 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001114 err = 0;
1115 goto errout;
1116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001118 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1119 prio, prio_allocate);
1120 if (IS_ERR(tp)) {
1121 err = PTR_ERR(tp);
1122 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
1125 if (tp == NULL) {
1126 /* Proto-tcf does not exist, create new one */
1127
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001128 if (tca[TCA_KIND] == NULL || !protocol) {
1129 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001133 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001134 !(n->nlmsg_flags & NLM_F_CREATE)) {
1135 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001139 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001140 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Jiri Pirko33a48922017-02-09 14:38:57 +01001142 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Jiri Pirkoedf67112018-01-17 11:46:49 +01001143 protocol, prio, chain);
Jiri Pirko33a48922017-02-09 14:38:57 +01001144 if (IS_ERR(tp)) {
1145 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 goto errout;
1147 }
Minoru Usui12186be2009-06-02 02:17:34 -07001148 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001149 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1150 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 fh = tp->ops->get(tp, t->tcm_handle);
1155
WANG Cong8113c092017-08-04 21:31:43 -07001156 if (!fh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001158 tcf_chain_tp_remove(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001159 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -07001160 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -07001161 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 err = 0;
1163 goto errout;
1164 }
1165
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001166 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001167 !(n->nlmsg_flags & NLM_F_CREATE)) {
1168 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 } else {
WANG Cong763dbf62017-04-19 14:21:21 -07001172 bool last;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001175 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -07001176 if (n->nlmsg_flags & NLM_F_EXCL) {
1177 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -07001178 tcf_proto_destroy(tp);
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001179 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -07001181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
1183 case RTM_DELTFILTER:
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001184 err = tfilter_del_notify(net, skb, n, tp, block,
1185 q, parent, fh, false, &last);
Jiri Pirko40c81b22017-02-09 14:39:00 +01001186 if (err)
1187 goto errout;
WANG Cong763dbf62017-04-19 14:21:21 -07001188 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001189 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -07001190 tcf_proto_destroy(tp);
1191 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +01001192 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 case RTM_GETTFILTER:
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001194 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1195 fh, RTM_NEWTFILTER, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 goto errout;
1197 default:
1198 err = -EINVAL;
1199 goto errout;
1200 }
1201 }
1202
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001203 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
1204 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -07001205 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001206 if (tp_created)
1207 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001208 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001209 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001210 } else {
1211 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -07001212 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -07001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001216 if (chain)
1217 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (err == -EAGAIN)
1219 /* Replay the request. */
1220 goto replay;
1221 return err;
1222}
1223
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001224struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct tcf_walker w;
1226 struct sk_buff *skb;
1227 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001228 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001229 struct Qdisc *q;
1230 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231};
1232
WANG Cong8113c092017-08-04 21:31:43 -07001233static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001235 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001236 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001238 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001239 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001240 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1241 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001244static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1245 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001246 long index_start, long *p_index)
1247{
1248 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001249 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001250 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1251 struct tcf_dump_args arg;
1252 struct tcf_proto *tp;
1253
1254 for (tp = rtnl_dereference(chain->filter_chain);
1255 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1256 if (*p_index < index_start)
1257 continue;
1258 if (TC_H_MAJ(tcm->tcm_info) &&
1259 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1260 continue;
1261 if (TC_H_MIN(tcm->tcm_info) &&
1262 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1263 continue;
1264 if (*p_index > index_start)
1265 memset(&cb->args[1], 0,
1266 sizeof(cb->args) - sizeof(cb->args[0]));
1267 if (cb->args[1] == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001268 if (tcf_fill_node(net, skb, tp, block, q, parent, 0,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001269 NETLINK_CB(cb->skb).portid,
1270 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1271 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001272 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001273
1274 cb->args[1] = 1;
1275 }
1276 if (!tp->ops->walk)
1277 continue;
1278 arg.w.fn = tcf_node_dump;
1279 arg.skb = skb;
1280 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001281 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001282 arg.q = q;
1283 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001284 arg.w.stop = 0;
1285 arg.w.skip = cb->args[1] - 1;
1286 arg.w.count = 0;
1287 tp->ops->walk(tp, &arg.w);
1288 cb->args[1] = arg.w.count + 1;
1289 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001290 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001291 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001292 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001293}
1294
Eric Dumazetbd27a872009-11-05 20:57:26 -08001295/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1297{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001298 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001299 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001300 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001301 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001302 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001303 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001304 long index_start;
1305 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001306 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001307 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Hong zhi guo573ce262013-03-27 06:47:04 +00001309 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001311
1312 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1313 if (err)
1314 return err;
1315
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001316 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1317 block = tcf_block_lookup(net, tcm->tcm_block_index);
1318 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001319 goto out;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001320 } else {
1321 const struct Qdisc_class_ops *cops;
1322 struct net_device *dev;
1323 unsigned long cl = 0;
1324
1325 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1326 if (!dev)
1327 return skb->len;
1328
1329 parent = tcm->tcm_parent;
1330 if (!parent) {
1331 q = dev->qdisc;
1332 parent = q->handle;
1333 } else {
1334 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1335 }
1336 if (!q)
1337 goto out;
1338 cops = q->ops->cl_ops;
1339 if (!cops)
1340 goto out;
1341 if (!cops->tcf_block)
1342 goto out;
1343 if (TC_H_MIN(tcm->tcm_parent)) {
1344 cl = cops->find(q, tcm->tcm_parent);
1345 if (cl == 0)
1346 goto out;
1347 }
1348 block = cops->tcf_block(q, cl, NULL);
1349 if (!block)
1350 goto out;
1351 if (tcf_block_shared(block))
1352 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001355 index_start = cb->args[0];
1356 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001357
1358 list_for_each_entry(chain, &block->chain_list, list) {
1359 if (tca[TCA_CHAIN] &&
1360 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1361 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001362 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1363 index_start, &index))
Jiri Pirko5bc17012017-05-17 11:08:01 +02001364 break;
1365 }
1366
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001367 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return skb->len;
1371}
1372
WANG Cong18d02642014-09-25 10:26:37 -07001373void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001376 LIST_HEAD(actions);
1377
Cong Wang2d132eb2017-10-26 18:24:40 -07001378 ASSERT_RTNL();
WANG Cong22dc13c2016-08-13 22:35:00 -07001379 tcf_exts_to_list(exts, &actions);
1380 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1381 kfree(exts->actions);
1382 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383#endif
1384}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001385EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00001387int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001388 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390#ifdef CONFIG_NET_CLS_ACT
1391 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 struct tc_action *act;
1393
WANG Cong5da57f42013-12-15 20:15:07 -08001394 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001395 act = tcf_action_init_1(net, tp, tb[exts->police],
1396 rate_tlv, "police", ovr,
1397 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001398 if (IS_ERR(act))
1399 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
WANG Cong33be6272013-12-15 20:15:05 -08001401 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07001402 exts->actions[0] = act;
1403 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08001404 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001405 LIST_HEAD(actions);
1406 int err, i = 0;
1407
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001408 err = tcf_action_init(net, tp, tb[exts->action],
1409 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001410 &actions);
WANG Cong33be6272013-12-15 20:15:05 -08001411 if (err)
1412 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -07001413 list_for_each_entry(act, &actions, list)
1414 exts->actions[i++] = act;
1415 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Cong Wange4b95c42017-11-06 13:47:19 -08001417 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419#else
WANG Cong5da57f42013-12-15 20:15:07 -08001420 if ((exts->action && tb[exts->action]) ||
1421 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return -EOPNOTSUPP;
1423#endif
1424
1425 return 0;
1426}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001427EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001429void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001432 struct tcf_exts old = *dst;
1433
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001434 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07001435 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436#endif
1437}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001438EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
WANG Cong22dc13c2016-08-13 22:35:00 -07001440#ifdef CONFIG_NET_CLS_ACT
1441static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1442{
1443 if (exts->nr_actions == 0)
1444 return NULL;
1445 else
1446 return exts->actions[0];
1447}
1448#endif
WANG Cong33be6272013-12-15 20:15:05 -08001449
WANG Cong5da57f42013-12-15 20:15:07 -08001450int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07001453 struct nlattr *nest;
1454
Jiri Pirko978dfd82017-08-04 14:29:03 +02001455 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /*
1457 * again for backward compatible mode - we want
1458 * to work with both old and new modes of entering
1459 * tc data even if iproute2 was newer - jhs
1460 */
WANG Cong33be6272013-12-15 20:15:05 -08001461 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001462 LIST_HEAD(actions);
1463
WANG Cong5da57f42013-12-15 20:15:07 -08001464 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001465 if (nest == NULL)
1466 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07001467
1468 tcf_exts_to_list(exts, &actions);
1469 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001470 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001471 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08001472 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08001473 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08001474 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001475 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001476 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08001477 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001478 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001479 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001483
1484nla_put_failure:
1485 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001487#else
1488 return 0;
1489#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001491EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001493
WANG Cong5da57f42013-12-15 20:15:07 -08001494int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001497 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001498 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001499 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500#endif
1501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001503EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Jiri Pirko717503b2017-10-11 09:41:09 +02001505static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1506 enum tc_setup_type type,
1507 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001508{
1509 int ok_count = 0;
1510#ifdef CONFIG_NET_CLS_ACT
1511 const struct tc_action *a;
1512 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001513 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001514
1515 if (!tcf_exts_has_actions(exts))
1516 return 0;
1517
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001518 for (i = 0; i < exts->nr_actions; i++) {
1519 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001520 if (!a->ops->get_dev)
1521 continue;
1522 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01001523 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001524 continue;
1525 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1526 if (ret < 0)
1527 return ret;
1528 ok_count += ret;
1529 }
1530#endif
1531 return ok_count;
1532}
Jiri Pirko717503b2017-10-11 09:41:09 +02001533
Jiri Pirko208c0f42017-10-19 15:50:32 +02001534int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1535 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02001536{
Jiri Pirko208c0f42017-10-19 15:50:32 +02001537 int ok_count;
1538 int ret;
1539
1540 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1541 if (ret < 0)
1542 return ret;
1543 ok_count = ret;
1544
1545 if (!exts)
1546 return ok_count;
1547 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1548 if (ret < 0)
1549 return ret;
1550 ok_count += ret;
1551
1552 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02001553}
1554EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001555
Jiri Pirko48617382018-01-17 11:46:46 +01001556static __net_init int tcf_net_init(struct net *net)
1557{
1558 struct tcf_net *tn = net_generic(net, tcf_net_id);
1559
1560 idr_init(&tn->idr);
1561 return 0;
1562}
1563
1564static void __net_exit tcf_net_exit(struct net *net)
1565{
1566 struct tcf_net *tn = net_generic(net, tcf_net_id);
1567
1568 idr_destroy(&tn->idr);
1569}
1570
1571static struct pernet_operations tcf_net_ops = {
1572 .init = tcf_net_init,
1573 .exit = tcf_net_exit,
1574 .id = &tcf_net_id,
1575 .size = sizeof(struct tcf_net),
1576};
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578static int __init tc_filter_init(void)
1579{
Jiri Pirko48617382018-01-17 11:46:46 +01001580 int err;
1581
Cong Wang7aa00452017-10-26 18:24:28 -07001582 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1583 if (!tc_filter_wq)
1584 return -ENOMEM;
1585
Jiri Pirko48617382018-01-17 11:46:46 +01001586 err = register_pernet_subsys(&tcf_net_ops);
1587 if (err)
1588 goto err_register_pernet_subsys;
1589
Florian Westphalb97bac62017-08-09 20:41:48 +02001590 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1591 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
Thomas Graf82623c02007-03-22 11:56:22 -07001592 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001593 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01001596
1597err_register_pernet_subsys:
1598 destroy_workqueue(tc_filter_wq);
1599 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
1602subsys_initcall(tc_filter_init);