blob: ee319b1598b5eca6aab15b234967185a8d18a9fb [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 Pirko6529eab2017-05-17 11:07:55 +0200125 u32 prio, u32 parent, struct Qdisc *q,
Jiri Pirko5bc17012017-05-17 11:08:01 +0200126 struct tcf_chain *chain)
Jiri Pirko33a48922017-02-09 14:38:57 +0100127{
128 struct tcf_proto *tp;
129 int err;
130
131 tp = kzalloc(sizeof(*tp), GFP_KERNEL);
132 if (!tp)
133 return ERR_PTR(-ENOBUFS);
134
135 err = -ENOENT;
136 tp->ops = tcf_proto_lookup_ops(kind);
137 if (!tp->ops) {
138#ifdef CONFIG_MODULES
139 rtnl_unlock();
140 request_module("cls_%s", kind);
141 rtnl_lock();
142 tp->ops = tcf_proto_lookup_ops(kind);
143 /* We dropped the RTNL semaphore in order to perform
144 * the module load. So, even if we succeeded in loading
145 * the module we have to replay the request. We indicate
146 * this using -EAGAIN.
147 */
148 if (tp->ops) {
149 module_put(tp->ops->owner);
150 err = -EAGAIN;
151 } else {
152 err = -ENOENT;
153 }
154 goto errout;
155#endif
156 }
157 tp->classify = tp->ops->classify;
158 tp->protocol = protocol;
159 tp->prio = prio;
160 tp->classid = parent;
161 tp->q = q;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200162 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100163
164 err = tp->ops->init(tp);
165 if (err) {
166 module_put(tp->ops->owner);
167 goto errout;
168 }
169 return tp;
170
171errout:
172 kfree(tp);
173 return ERR_PTR(err);
174}
175
WANG Cong763dbf62017-04-19 14:21:21 -0700176static void tcf_proto_destroy(struct tcf_proto *tp)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100177{
WANG Cong763dbf62017-04-19 14:21:21 -0700178 tp->ops->destroy(tp);
179 module_put(tp->ops->owner);
180 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100181}
182
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100183struct tcf_filter_chain_list_item {
184 struct list_head list;
185 tcf_chain_head_change_t *chain_head_change;
186 void *chain_head_change_priv;
187};
188
Jiri Pirko5bc17012017-05-17 11:08:01 +0200189static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
190 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200191{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200192 struct tcf_chain *chain;
193
194 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
195 if (!chain)
196 return NULL;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100197 INIT_LIST_HEAD(&chain->filter_chain_list);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200198 list_add_tail(&chain->list, &block->chain_list);
199 chain->block = block;
200 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700201 chain->refcnt = 1;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200202 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200203}
204
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100205static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
206 struct tcf_proto *tp_head)
207{
208 if (item->chain_head_change)
209 item->chain_head_change(tp_head, item->chain_head_change_priv);
210}
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100211static void tcf_chain_head_change(struct tcf_chain *chain,
212 struct tcf_proto *tp_head)
213{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100214 struct tcf_filter_chain_list_item *item;
215
216 list_for_each_entry(item, &chain->filter_chain_list, list)
217 tcf_chain_head_change_item(item, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100218}
219
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200220static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100221{
Roman Kapld7aa04a2017-11-20 22:21:13 +0100222 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100223
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100224 tcf_chain_head_change(chain, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100225 while (tp) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200226 RCU_INIT_POINTER(chain->filter_chain, tp->next);
WANG Cong763dbf62017-04-19 14:21:21 -0700227 tcf_proto_destroy(tp);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100228 tp = rtnl_dereference(chain->filter_chain);
229 tcf_chain_put(chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100230 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200231}
232
233static void tcf_chain_destroy(struct tcf_chain *chain)
234{
Cong Wangefbf7892017-12-04 10:48:18 -0800235 struct tcf_block *block = chain->block;
236
Cong Wange2ef7542017-09-11 16:33:31 -0700237 list_del(&chain->list);
238 kfree(chain);
Cong Wangefbf7892017-12-04 10:48:18 -0800239 if (list_empty(&block->chain_list))
240 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700241}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200242
Cong Wange2ef7542017-09-11 16:33:31 -0700243static void tcf_chain_hold(struct tcf_chain *chain)
244{
245 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200246}
247
WANG Cong367a8ce2017-05-23 09:42:37 -0700248struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
249 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200250{
251 struct tcf_chain *chain;
252
253 list_for_each_entry(chain, &block->chain_list, list) {
Cong Wange2ef7542017-09-11 16:33:31 -0700254 if (chain->index == chain_index) {
255 tcf_chain_hold(chain);
256 return chain;
257 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200258 }
Jiri Pirko80532382017-09-06 13:14:19 +0200259
Cong Wange2ef7542017-09-11 16:33:31 -0700260 return create ? tcf_chain_create(block, chain_index) : NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200261}
262EXPORT_SYMBOL(tcf_chain_get);
263
264void tcf_chain_put(struct tcf_chain *chain)
265{
Cong Wange2ef7542017-09-11 16:33:31 -0700266 if (--chain->refcnt == 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200267 tcf_chain_destroy(chain);
268}
269EXPORT_SYMBOL(tcf_chain_put);
270
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200271static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q,
272 struct tcf_block_ext_info *ei,
273 enum tc_block_command command)
274{
275 struct net_device *dev = q->dev_queue->dev;
276 struct tc_block_offload bo = {};
277
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100278 if (!dev->netdev_ops->ndo_setup_tc)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200279 return;
280 bo.command = command;
281 bo.binder_type = ei->binder_type;
282 bo.block = block;
283 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
284}
285
286static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
287 struct tcf_block_ext_info *ei)
288{
289 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND);
290}
291
292static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
293 struct tcf_block_ext_info *ei)
294{
295 tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND);
296}
297
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100298static int
299tcf_chain_head_change_cb_add(struct tcf_chain *chain,
300 struct tcf_block_ext_info *ei,
301 struct netlink_ext_ack *extack)
302{
303 struct tcf_filter_chain_list_item *item;
304
305 item = kmalloc(sizeof(*item), GFP_KERNEL);
306 if (!item) {
307 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
308 return -ENOMEM;
309 }
310 item->chain_head_change = ei->chain_head_change;
311 item->chain_head_change_priv = ei->chain_head_change_priv;
312 if (chain->filter_chain)
313 tcf_chain_head_change_item(item, chain->filter_chain);
314 list_add(&item->list, &chain->filter_chain_list);
315 return 0;
316}
317
318static void
319tcf_chain_head_change_cb_del(struct tcf_chain *chain,
320 struct tcf_block_ext_info *ei)
321{
322 struct tcf_filter_chain_list_item *item;
323
324 list_for_each_entry(item, &chain->filter_chain_list, list) {
325 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
326 (item->chain_head_change == ei->chain_head_change &&
327 item->chain_head_change_priv == ei->chain_head_change_priv)) {
328 tcf_chain_head_change_item(item, NULL);
329 list_del(&item->list);
330 kfree(item);
331 return;
332 }
333 }
334 WARN_ON(1);
335}
336
Jiri Pirko48617382018-01-17 11:46:46 +0100337struct tcf_net {
338 struct idr idr;
339};
340
341static unsigned int tcf_net_id;
342
343static int tcf_block_insert(struct tcf_block *block, struct net *net,
344 u32 block_index, struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100345{
Jiri Pirko48617382018-01-17 11:46:46 +0100346 struct tcf_net *tn = net_generic(net, tcf_net_id);
347 int err;
348
349 err = idr_alloc_ext(&tn->idr, block, NULL, block_index,
350 block_index + 1, GFP_KERNEL);
351 if (err)
352 return err;
353 block->index = block_index;
354 return 0;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100355}
356
Jiri Pirko48617382018-01-17 11:46:46 +0100357static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200358{
Jiri Pirko48617382018-01-17 11:46:46 +0100359 struct tcf_net *tn = net_generic(net, tcf_net_id);
360
361 idr_remove_ext(&tn->idr, block->index);
362}
363
364static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
365 struct netlink_ext_ack *extack)
366{
367 struct tcf_block *block;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200368 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200369 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200370
Jiri Pirko48617382018-01-17 11:46:46 +0100371 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500372 if (!block) {
373 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100374 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500375 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200376 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200377 INIT_LIST_HEAD(&block->cb_list);
378
Jiri Pirko5bc17012017-05-17 11:08:01 +0200379 /* Create chain 0 by default, it has to be always present. */
380 chain = tcf_chain_create(block, 0);
381 if (!chain) {
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500382 NL_SET_ERR_MSG(extack, "Failed to create new tcf chain");
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200383 err = -ENOMEM;
384 goto err_chain_create;
385 }
Jiri Pirko48617382018-01-17 11:46:46 +0100386 block->net = qdisc_net(q);
387 block->refcnt = 1;
388 block->net = net;
389 block->q = q;
390 return block;
391
392err_chain_create:
393 kfree(block);
394 return ERR_PTR(err);
395}
396
397static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
398{
399 struct tcf_net *tn = net_generic(net, tcf_net_id);
400
401 return idr_find_ext(&tn->idr, block_index);
402}
403
404static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
405{
406 return list_first_entry(&block->chain_list, struct tcf_chain, list);
407}
408
409int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
410 struct tcf_block_ext_info *ei,
411 struct netlink_ext_ack *extack)
412{
413 struct net *net = qdisc_net(q);
414 struct tcf_block *block = NULL;
415 bool created = false;
416 int err;
417
418 if (ei->block_index) {
419 /* block_index not 0 means the shared block is requested */
420 block = tcf_block_lookup(net, ei->block_index);
421 if (block)
422 block->refcnt++;
423 }
424
425 if (!block) {
426 block = tcf_block_create(net, q, extack);
427 if (IS_ERR(block))
428 return PTR_ERR(block);
429 created = true;
430 if (ei->block_index) {
431 err = tcf_block_insert(block, net,
432 ei->block_index, extack);
433 if (err)
434 goto err_block_insert;
435 }
436 }
437
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100438 err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block),
439 ei, extack);
440 if (err)
441 goto err_chain_head_change_cb_add;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200442 tcf_block_offload_bind(block, q, ei);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200443 *p_block = block;
444 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200445
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100446err_chain_head_change_cb_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100447 if (created) {
448 if (tcf_block_shared(block))
449 tcf_block_remove(block, net);
450err_block_insert:
451 kfree(tcf_block_chain_zero(block));
452 kfree(block);
453 } else {
454 block->refcnt--;
455 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200456 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200457}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200458EXPORT_SYMBOL(tcf_block_get_ext);
459
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100460static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
461{
462 struct tcf_proto __rcu **p_filter_chain = priv;
463
464 rcu_assign_pointer(*p_filter_chain, tp_head);
465}
466
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200467int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500468 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
469 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200470{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100471 struct tcf_block_ext_info ei = {
472 .chain_head_change = tcf_chain_head_change_dflt,
473 .chain_head_change_priv = p_filter_chain,
474 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200475
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100476 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500477 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200478}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200479EXPORT_SYMBOL(tcf_block_get);
480
Cong Wang7aa00452017-10-26 18:24:28 -0700481/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100482 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700483 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100484void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900485 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700486{
Cong Wangefbf7892017-12-04 10:48:18 -0800487 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700488
David S. Millerc30abd52017-12-16 22:11:55 -0500489 if (!block)
490 return;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100491 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Roman Kapla60b3f52017-11-24 12:27:58 +0100492
Jiri Pirko48617382018-01-17 11:46:46 +0100493 if (--block->refcnt == 0) {
494 if (tcf_block_shared(block))
495 tcf_block_remove(block, block->net);
496
497 /* Hold a refcnt for all chains, so that they don't disappear
498 * while we are iterating.
499 */
500 list_for_each_entry(chain, &block->chain_list, list)
501 tcf_chain_hold(chain);
502
503 list_for_each_entry(chain, &block->chain_list, list)
504 tcf_chain_flush(chain);
505 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700506
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100507 tcf_block_offload_unbind(block, q, ei);
508
Jiri Pirko48617382018-01-17 11:46:46 +0100509 if (block->refcnt == 0) {
510 /* At this point, all the chains should have refcnt >= 1. */
511 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
512 tcf_chain_put(chain);
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100513
Jiri Pirko48617382018-01-17 11:46:46 +0100514 /* Finally, put chain 0 and allow block to be freed. */
515 tcf_chain_put(tcf_block_chain_zero(block));
516 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200517}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200518EXPORT_SYMBOL(tcf_block_put_ext);
519
520void tcf_block_put(struct tcf_block *block)
521{
522 struct tcf_block_ext_info ei = {0, };
523
Jiri Pirko4853f122017-12-21 13:13:59 +0100524 if (!block)
525 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100526 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200527}
David S. Millere1ea2f92017-10-30 14:10:01 +0900528
Jiri Pirko6529eab2017-05-17 11:07:55 +0200529EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100530
Jiri Pirkoacb67442017-10-19 15:50:31 +0200531struct tcf_block_cb {
532 struct list_head list;
533 tc_setup_cb_t *cb;
534 void *cb_ident;
535 void *cb_priv;
536 unsigned int refcnt;
537};
538
539void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
540{
541 return block_cb->cb_priv;
542}
543EXPORT_SYMBOL(tcf_block_cb_priv);
544
545struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
546 tc_setup_cb_t *cb, void *cb_ident)
547{ struct tcf_block_cb *block_cb;
548
549 list_for_each_entry(block_cb, &block->cb_list, list)
550 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
551 return block_cb;
552 return NULL;
553}
554EXPORT_SYMBOL(tcf_block_cb_lookup);
555
556void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
557{
558 block_cb->refcnt++;
559}
560EXPORT_SYMBOL(tcf_block_cb_incref);
561
562unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
563{
564 return --block_cb->refcnt;
565}
566EXPORT_SYMBOL(tcf_block_cb_decref);
567
568struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
569 tc_setup_cb_t *cb, void *cb_ident,
570 void *cb_priv)
571{
572 struct tcf_block_cb *block_cb;
573
574 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
575 if (!block_cb)
576 return NULL;
577 block_cb->cb = cb;
578 block_cb->cb_ident = cb_ident;
579 block_cb->cb_priv = cb_priv;
580 list_add(&block_cb->list, &block->cb_list);
581 return block_cb;
582}
583EXPORT_SYMBOL(__tcf_block_cb_register);
584
585int tcf_block_cb_register(struct tcf_block *block,
586 tc_setup_cb_t *cb, void *cb_ident,
587 void *cb_priv)
588{
589 struct tcf_block_cb *block_cb;
590
591 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
592 return block_cb ? 0 : -ENOMEM;
593}
594EXPORT_SYMBOL(tcf_block_cb_register);
595
596void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
597{
598 list_del(&block_cb->list);
599 kfree(block_cb);
600}
601EXPORT_SYMBOL(__tcf_block_cb_unregister);
602
603void tcf_block_cb_unregister(struct tcf_block *block,
604 tc_setup_cb_t *cb, void *cb_ident)
605{
606 struct tcf_block_cb *block_cb;
607
608 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
609 if (!block_cb)
610 return;
611 __tcf_block_cb_unregister(block_cb);
612}
613EXPORT_SYMBOL(tcf_block_cb_unregister);
614
615static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
616 void *type_data, bool err_stop)
617{
618 struct tcf_block_cb *block_cb;
619 int ok_count = 0;
620 int err;
621
622 list_for_each_entry(block_cb, &block->cb_list, list) {
623 err = block_cb->cb(type, type_data, block_cb->cb_priv);
624 if (err) {
625 if (err_stop)
626 return err;
627 } else {
628 ok_count++;
629 }
630 }
631 return ok_count;
632}
633
Jiri Pirko87d83092017-05-17 11:07:54 +0200634/* Main classifier routine: scans classifier chain attached
635 * to this qdisc, (optionally) tests for protocol and asks
636 * specific classifiers.
637 */
638int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
639 struct tcf_result *res, bool compat_mode)
640{
641 __be16 protocol = tc_skb_protocol(skb);
642#ifdef CONFIG_NET_CLS_ACT
643 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200644 const struct tcf_proto *orig_tp = tp;
645 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200646 int limit = 0;
647
648reclassify:
649#endif
650 for (; tp; tp = rcu_dereference_bh(tp->next)) {
651 int err;
652
653 if (tp->protocol != protocol &&
654 tp->protocol != htons(ETH_P_ALL))
655 continue;
656
657 err = tp->classify(skb, tp, res);
658#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200659 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200660 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200661 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200662 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200663 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200664 goto reset;
665 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200666#endif
667 if (err >= 0)
668 return err;
669 }
670
671 return TC_ACT_UNSPEC; /* signal: continue lookup */
672#ifdef CONFIG_NET_CLS_ACT
673reset:
674 if (unlikely(limit++ >= max_reclassify_loop)) {
675 net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
676 tp->q->ops->id, tp->prio & 0xffff,
677 ntohs(tp->protocol));
678 return TC_ACT_SHOT;
679 }
680
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200681 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200682 protocol = tc_skb_protocol(skb);
683 goto reclassify;
684#endif
685}
686EXPORT_SYMBOL(tcf_classify);
687
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200688struct tcf_chain_info {
689 struct tcf_proto __rcu **pprev;
690 struct tcf_proto __rcu *next;
691};
692
693static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
694{
695 return rtnl_dereference(*chain_info->pprev);
696}
697
698static void tcf_chain_tp_insert(struct tcf_chain *chain,
699 struct tcf_chain_info *chain_info,
700 struct tcf_proto *tp)
701{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100702 if (*chain_info->pprev == chain->filter_chain)
703 tcf_chain_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200704 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
705 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700706 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200707}
708
709static void tcf_chain_tp_remove(struct tcf_chain *chain,
710 struct tcf_chain_info *chain_info,
711 struct tcf_proto *tp)
712{
713 struct tcf_proto *next = rtnl_dereference(chain_info->next);
714
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100715 if (tp == chain->filter_chain)
716 tcf_chain_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200717 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700718 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200719}
720
721static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
722 struct tcf_chain_info *chain_info,
723 u32 protocol, u32 prio,
724 bool prio_allocate)
725{
726 struct tcf_proto **pprev;
727 struct tcf_proto *tp;
728
729 /* Check the chain for existence of proto-tcf with this priority */
730 for (pprev = &chain->filter_chain;
731 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
732 if (tp->prio >= prio) {
733 if (tp->prio == prio) {
734 if (prio_allocate ||
735 (tp->protocol != protocol && protocol))
736 return ERR_PTR(-EINVAL);
737 } else {
738 tp = NULL;
739 }
740 break;
741 }
742 }
743 chain_info->pprev = pprev;
744 chain_info->next = tp ? tp->next : NULL;
745 return tp;
746}
747
WANG Cong71203712017-08-07 15:26:50 -0700748static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200749 struct tcf_proto *tp, struct Qdisc *q, u32 parent,
750 void *fh, u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700751{
752 struct tcmsg *tcm;
753 struct nlmsghdr *nlh;
754 unsigned char *b = skb_tail_pointer(skb);
755
756 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
757 if (!nlh)
758 goto out_nlmsg_trim;
759 tcm = nlmsg_data(nlh);
760 tcm->tcm_family = AF_UNSPEC;
761 tcm->tcm__pad1 = 0;
762 tcm->tcm__pad2 = 0;
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200763 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
764 tcm->tcm_parent = parent;
WANG Cong71203712017-08-07 15:26:50 -0700765 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
766 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
767 goto nla_put_failure;
768 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
769 goto nla_put_failure;
770 if (!fh) {
771 tcm->tcm_handle = 0;
772 } else {
773 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
774 goto nla_put_failure;
775 }
776 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
777 return skb->len;
778
779out_nlmsg_trim:
780nla_put_failure:
781 nlmsg_trim(skb, b);
782 return -1;
783}
784
785static int tfilter_notify(struct net *net, struct sk_buff *oskb,
786 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200787 struct Qdisc *q, u32 parent,
WANG Cong71203712017-08-07 15:26:50 -0700788 void *fh, int event, bool unicast)
789{
790 struct sk_buff *skb;
791 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
792
793 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
794 if (!skb)
795 return -ENOBUFS;
796
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200797 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
WANG Cong71203712017-08-07 15:26:50 -0700798 n->nlmsg_flags, event) <= 0) {
799 kfree_skb(skb);
800 return -EINVAL;
801 }
802
803 if (unicast)
804 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
805
806 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
807 n->nlmsg_flags & NLM_F_ECHO);
808}
809
810static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
811 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200812 struct Qdisc *q, u32 parent,
WANG Cong71203712017-08-07 15:26:50 -0700813 void *fh, bool unicast, bool *last)
814{
815 struct sk_buff *skb;
816 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
817 int err;
818
819 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
820 if (!skb)
821 return -ENOBUFS;
822
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200823 if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq,
WANG Cong71203712017-08-07 15:26:50 -0700824 n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
825 kfree_skb(skb);
826 return -EINVAL;
827 }
828
829 err = tp->ops->delete(tp, fh, last);
830 if (err) {
831 kfree_skb(skb);
832 return err;
833 }
834
835 if (unicast)
836 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
837
838 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
839 n->nlmsg_flags & NLM_F_ECHO);
840}
841
842static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200843 struct Qdisc *q, u32 parent,
WANG Cong71203712017-08-07 15:26:50 -0700844 struct nlmsghdr *n,
845 struct tcf_chain *chain, int event)
846{
847 struct tcf_proto *tp;
848
849 for (tp = rtnl_dereference(chain->filter_chain);
850 tp; tp = rtnl_dereference(tp->next))
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200851 tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false);
WANG Cong71203712017-08-07 15:26:50 -0700852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854/* Add/change/delete/get a filter node */
855
David Ahernc21ef3e2017-04-16 09:48:24 -0700856static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
857 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900859 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800860 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct tcmsg *t;
862 u32 protocol;
863 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200864 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200866 u32 chain_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct net_device *dev;
868 struct Qdisc *q;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200869 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200870 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200871 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct tcf_proto *tp;
Eric Dumazet20fea082007-11-14 01:44:41 -0800873 const struct Qdisc_class_ops *cops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -0700875 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +0100877 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Stéphane Graber4e8bbb82014-04-30 11:25:43 -0400879 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -0400880 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +0000881 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +0000882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +0100884 tp_created = 0;
885
David Ahernc21ef3e2017-04-16 09:48:24 -0700886 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +0000887 if (err < 0)
888 return err;
889
David S. Miller942b8162012-06-26 21:48:50 -0700890 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 protocol = TC_H_MIN(t->tcm_info);
892 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200893 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 parent = t->tcm_parent;
895 cl = 0;
896
897 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200898 switch (n->nlmsg_type) {
899 case RTM_DELTFILTER:
Daniel Borkmann9f6ed032016-06-16 23:19:29 +0200900 if (protocol || t->tcm_handle || tca[TCA_KIND])
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200901 return -ENOENT;
902 break;
903 case RTM_NEWTFILTER:
904 /* If no priority is provided by the user,
905 * we allocate one.
906 */
907 if (n->nlmsg_flags & NLM_F_CREATE) {
908 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200909 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200910 break;
911 }
912 /* fall-through */
913 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
918 /* Find head of filter chain. */
919
920 /* Find link */
Tom Goff7316ae82010-03-19 15:40:13 +0000921 dev = __dev_get_by_index(net, t->tcm_ifindex);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800922 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -ENODEV;
924
925 /* Find qdisc */
926 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +0000927 q = dev->qdisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 parent = q->handle;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800929 } else {
930 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
931 if (q == NULL)
932 return -EINVAL;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* Is it classful? */
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000936 cops = q->ops->cl_ops;
937 if (!cops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return -EINVAL;
939
Jiri Pirko6529eab2017-05-17 11:07:55 +0200940 if (!cops->tcf_block)
Patrick McHardy71ebe5e2009-09-04 06:41:15 +0000941 return -EOPNOTSUPP;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* Do we search for filter, attached to class? */
944 if (TC_H_MIN(parent)) {
WANG Cong143976c2017-08-24 16:51:29 -0700945 cl = cops->find(q, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (cl == 0)
947 return -ENOENT;
948 }
949
950 /* And the last stroke */
Alexander Aringcbaacc42017-12-20 12:35:16 -0500951 block = cops->tcf_block(q, cl, extack);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200952 if (!block) {
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100953 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100955 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200956
957 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
958 if (chain_index > TC_ACT_EXT_VAL_MASK) {
959 err = -EINVAL;
960 goto errout;
961 }
WANG Cong367a8ce2017-05-23 09:42:37 -0700962 chain = tcf_chain_get(block, chain_index,
963 n->nlmsg_type == RTM_NEWTFILTER);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200964 if (!chain) {
WANG Cong367a8ce2017-05-23 09:42:37 -0700965 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200966 goto errout;
967 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200968
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200969 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
Jiri Pirkoa10fa202017-10-13 14:01:05 +0200970 tfilter_notify_chain(net, skb, q, parent, n,
971 chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200972 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +0200973 err = 0;
974 goto errout;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200977 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
978 prio, prio_allocate);
979 if (IS_ERR(tp)) {
980 err = PTR_ERR(tp);
981 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983
984 if (tp == NULL) {
985 /* Proto-tcf does not exist, create new one */
986
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100987 if (tca[TCA_KIND] == NULL || !protocol) {
988 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000992 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100993 !(n->nlmsg_flags & NLM_F_CREATE)) {
994 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +0100996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200998 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200999 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Jiri Pirko33a48922017-02-09 14:38:57 +01001001 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Jiri Pirko5bc17012017-05-17 11:08:01 +02001002 protocol, prio, parent, q, chain);
Jiri Pirko33a48922017-02-09 14:38:57 +01001003 if (IS_ERR(tp)) {
1004 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto errout;
1006 }
Minoru Usui12186be2009-06-02 02:17:34 -07001007 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001008 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
1009 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 fh = tp->ops->get(tp, t->tcm_handle);
1014
WANG Cong8113c092017-08-04 21:31:43 -07001015 if (!fh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001017 tcf_chain_tp_remove(chain, &chain_info, tp);
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001018 tfilter_notify(net, skb, n, tp, q, parent, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -07001019 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -07001020 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 err = 0;
1022 goto errout;
1023 }
1024
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001025 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001026 !(n->nlmsg_flags & NLM_F_CREATE)) {
1027 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 } else {
WANG Cong763dbf62017-04-19 14:21:21 -07001031 bool last;
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001034 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -07001035 if (n->nlmsg_flags & NLM_F_EXCL) {
1036 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -07001037 tcf_proto_destroy(tp);
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001038 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -07001040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
1042 case RTM_DELTFILTER:
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001043 err = tfilter_del_notify(net, skb, n, tp, q, parent,
1044 fh, false, &last);
Jiri Pirko40c81b22017-02-09 14:39:00 +01001045 if (err)
1046 goto errout;
WANG Cong763dbf62017-04-19 14:21:21 -07001047 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001048 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -07001049 tcf_proto_destroy(tp);
1050 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +01001051 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 case RTM_GETTFILTER:
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001053 err = tfilter_notify(net, skb, n, tp, q, parent, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -07001054 RTM_NEWTFILTER, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 goto errout;
1056 default:
1057 err = -EINVAL;
1058 goto errout;
1059 }
1060 }
1061
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001062 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
1063 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -07001064 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001065 if (tp_created)
1066 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001067 tfilter_notify(net, skb, n, tp, q, parent, fh,
1068 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001069 } else {
1070 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -07001071 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -07001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001075 if (chain)
1076 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (err == -EAGAIN)
1078 /* Replay the request. */
1079 goto replay;
1080 return err;
1081}
1082
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001083struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct tcf_walker w;
1085 struct sk_buff *skb;
1086 struct netlink_callback *cb;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001087 struct Qdisc *q;
1088 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089};
1090
WANG Cong8113c092017-08-04 21:31:43 -07001091static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001093 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001094 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001096 return tcf_fill_node(net, a->skb, tp, a->q, a->parent,
1097 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001098 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1099 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001102static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1103 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001104 long index_start, long *p_index)
1105{
1106 struct net *net = sock_net(skb->sk);
1107 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1108 struct tcf_dump_args arg;
1109 struct tcf_proto *tp;
1110
1111 for (tp = rtnl_dereference(chain->filter_chain);
1112 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1113 if (*p_index < index_start)
1114 continue;
1115 if (TC_H_MAJ(tcm->tcm_info) &&
1116 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1117 continue;
1118 if (TC_H_MIN(tcm->tcm_info) &&
1119 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1120 continue;
1121 if (*p_index > index_start)
1122 memset(&cb->args[1], 0,
1123 sizeof(cb->args) - sizeof(cb->args[0]));
1124 if (cb->args[1] == 0) {
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001125 if (tcf_fill_node(net, skb, tp, q, parent, 0,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001126 NETLINK_CB(cb->skb).portid,
1127 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1128 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001129 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001130
1131 cb->args[1] = 1;
1132 }
1133 if (!tp->ops->walk)
1134 continue;
1135 arg.w.fn = tcf_node_dump;
1136 arg.skb = skb;
1137 arg.cb = cb;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001138 arg.q = q;
1139 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001140 arg.w.stop = 0;
1141 arg.w.skip = cb->args[1] - 1;
1142 arg.w.count = 0;
1143 tp->ops->walk(tp, &arg.w);
1144 cb->args[1] = arg.w.count + 1;
1145 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001146 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001147 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001148 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001149}
1150
Eric Dumazetbd27a872009-11-05 20:57:26 -08001151/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1153{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001154 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001155 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 struct net_device *dev;
1157 struct Qdisc *q;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001158 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001159 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001160 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unsigned long cl = 0;
Eric Dumazet20fea082007-11-14 01:44:41 -08001162 const struct Qdisc_class_ops *cops;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001163 long index_start;
1164 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001165 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001166 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Hong zhi guo573ce262013-03-27 06:47:04 +00001168 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001170
1171 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1172 if (err)
1173 return err;
1174
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001175 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1176 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return skb->len;
1178
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001179 parent = tcm->tcm_parent;
1180 if (!parent) {
Patrick McHardyaf356af2009-09-04 06:41:18 +00001181 q = dev->qdisc;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001182 parent = q->handle;
1183 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (!q)
1187 goto out;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001188 cops = q->ops->cl_ops;
1189 if (!cops)
WANG Cong143976c2017-08-24 16:51:29 -07001190 goto out;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001191 if (!cops->tcf_block)
WANG Cong143976c2017-08-24 16:51:29 -07001192 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (TC_H_MIN(tcm->tcm_parent)) {
WANG Cong143976c2017-08-24 16:51:29 -07001194 cl = cops->find(q, tcm->tcm_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (cl == 0)
WANG Cong143976c2017-08-24 16:51:29 -07001196 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
Alexander Aringcbaacc42017-12-20 12:35:16 -05001198 block = cops->tcf_block(q, cl, NULL);
Jiri Pirko6529eab2017-05-17 11:07:55 +02001199 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001200 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001202 index_start = cb->args[0];
1203 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001204
1205 list_for_each_entry(chain, &block->chain_list, list) {
1206 if (tca[TCA_CHAIN] &&
1207 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1208 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001209 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1210 index_start, &index))
Jiri Pirko5bc17012017-05-17 11:08:01 +02001211 break;
1212 }
1213
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001214 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return skb->len;
1218}
1219
WANG Cong18d02642014-09-25 10:26:37 -07001220void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001223 LIST_HEAD(actions);
1224
Cong Wang2d132eb2017-10-26 18:24:40 -07001225 ASSERT_RTNL();
WANG Cong22dc13c2016-08-13 22:35:00 -07001226 tcf_exts_to_list(exts, &actions);
1227 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1228 kfree(exts->actions);
1229 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230#endif
1231}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001232EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00001234int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001235 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237#ifdef CONFIG_NET_CLS_ACT
1238 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 struct tc_action *act;
1240
WANG Cong5da57f42013-12-15 20:15:07 -08001241 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001242 act = tcf_action_init_1(net, tp, tb[exts->police],
1243 rate_tlv, "police", ovr,
1244 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001245 if (IS_ERR(act))
1246 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
WANG Cong33be6272013-12-15 20:15:05 -08001248 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07001249 exts->actions[0] = act;
1250 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08001251 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001252 LIST_HEAD(actions);
1253 int err, i = 0;
1254
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001255 err = tcf_action_init(net, tp, tb[exts->action],
1256 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001257 &actions);
WANG Cong33be6272013-12-15 20:15:05 -08001258 if (err)
1259 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -07001260 list_for_each_entry(act, &actions, list)
1261 exts->actions[i++] = act;
1262 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Cong Wange4b95c42017-11-06 13:47:19 -08001264 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266#else
WANG Cong5da57f42013-12-15 20:15:07 -08001267 if ((exts->action && tb[exts->action]) ||
1268 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return -EOPNOTSUPP;
1270#endif
1271
1272 return 0;
1273}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001274EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001276void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001279 struct tcf_exts old = *dst;
1280
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001281 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07001282 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283#endif
1284}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001285EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
WANG Cong22dc13c2016-08-13 22:35:00 -07001287#ifdef CONFIG_NET_CLS_ACT
1288static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1289{
1290 if (exts->nr_actions == 0)
1291 return NULL;
1292 else
1293 return exts->actions[0];
1294}
1295#endif
WANG Cong33be6272013-12-15 20:15:05 -08001296
WANG Cong5da57f42013-12-15 20:15:07 -08001297int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07001300 struct nlattr *nest;
1301
Jiri Pirko978dfd82017-08-04 14:29:03 +02001302 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /*
1304 * again for backward compatible mode - we want
1305 * to work with both old and new modes of entering
1306 * tc data even if iproute2 was newer - jhs
1307 */
WANG Cong33be6272013-12-15 20:15:05 -08001308 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001309 LIST_HEAD(actions);
1310
WANG Cong5da57f42013-12-15 20:15:07 -08001311 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001312 if (nest == NULL)
1313 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07001314
1315 tcf_exts_to_list(exts, &actions);
1316 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001317 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001318 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08001319 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08001320 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08001321 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001322 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001323 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08001324 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001325 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001326 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001330
1331nla_put_failure:
1332 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001334#else
1335 return 0;
1336#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001338EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001340
WANG Cong5da57f42013-12-15 20:15:07 -08001341int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001344 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001345 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001346 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347#endif
1348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001350EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Jiri Pirko717503b2017-10-11 09:41:09 +02001352static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1353 enum tc_setup_type type,
1354 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001355{
1356 int ok_count = 0;
1357#ifdef CONFIG_NET_CLS_ACT
1358 const struct tc_action *a;
1359 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001360 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001361
1362 if (!tcf_exts_has_actions(exts))
1363 return 0;
1364
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001365 for (i = 0; i < exts->nr_actions; i++) {
1366 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001367 if (!a->ops->get_dev)
1368 continue;
1369 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01001370 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001371 continue;
1372 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1373 if (ret < 0)
1374 return ret;
1375 ok_count += ret;
1376 }
1377#endif
1378 return ok_count;
1379}
Jiri Pirko717503b2017-10-11 09:41:09 +02001380
Jiri Pirko208c0f42017-10-19 15:50:32 +02001381int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1382 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02001383{
Jiri Pirko208c0f42017-10-19 15:50:32 +02001384 int ok_count;
1385 int ret;
1386
1387 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1388 if (ret < 0)
1389 return ret;
1390 ok_count = ret;
1391
1392 if (!exts)
1393 return ok_count;
1394 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1395 if (ret < 0)
1396 return ret;
1397 ok_count += ret;
1398
1399 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02001400}
1401EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001402
Jiri Pirko48617382018-01-17 11:46:46 +01001403static __net_init int tcf_net_init(struct net *net)
1404{
1405 struct tcf_net *tn = net_generic(net, tcf_net_id);
1406
1407 idr_init(&tn->idr);
1408 return 0;
1409}
1410
1411static void __net_exit tcf_net_exit(struct net *net)
1412{
1413 struct tcf_net *tn = net_generic(net, tcf_net_id);
1414
1415 idr_destroy(&tn->idr);
1416}
1417
1418static struct pernet_operations tcf_net_ops = {
1419 .init = tcf_net_init,
1420 .exit = tcf_net_exit,
1421 .id = &tcf_net_id,
1422 .size = sizeof(struct tcf_net),
1423};
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425static int __init tc_filter_init(void)
1426{
Jiri Pirko48617382018-01-17 11:46:46 +01001427 int err;
1428
Cong Wang7aa00452017-10-26 18:24:28 -07001429 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1430 if (!tc_filter_wq)
1431 return -ENOMEM;
1432
Jiri Pirko48617382018-01-17 11:46:46 +01001433 err = register_pernet_subsys(&tcf_net_ops);
1434 if (err)
1435 goto err_register_pernet_subsys;
1436
Florian Westphalb97bac62017-08-09 20:41:48 +02001437 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1438 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
Thomas Graf82623c02007-03-22 11:56:22 -07001439 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001440 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01001443
1444err_register_pernet_subsys:
1445 destroy_workqueue(tc_filter_wq);
1446 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449subsys_initcall(tc_filter_init);