blob: bcb4ccb5f894b8e50e6c7373a091aacf5b66a107 [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,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500125 u32 prio, struct tcf_chain *chain,
126 struct netlink_ext_ack *extack)
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 {
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500152 NL_SET_ERR_MSG(extack, "TC classifier not found");
Jiri Pirko33a48922017-02-09 14:38:57 +0100153 err = -ENOENT;
154 }
155 goto errout;
156#endif
157 }
158 tp->classify = tp->ops->classify;
159 tp->protocol = protocol;
160 tp->prio = prio;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200161 tp->chain = chain;
Jiri Pirko33a48922017-02-09 14:38:57 +0100162
163 err = tp->ops->init(tp);
164 if (err) {
165 module_put(tp->ops->owner);
166 goto errout;
167 }
168 return tp;
169
170errout:
171 kfree(tp);
172 return ERR_PTR(err);
173}
174
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800175static void tcf_proto_destroy(struct tcf_proto *tp,
176 struct netlink_ext_ack *extack)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100177{
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800178 tp->ops->destroy(tp, extack);
WANG Cong763dbf62017-04-19 14:21:21 -0700179 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);
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800227 tcf_proto_destroy(tp, NULL);
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 Pirkocaa72602018-01-17 11:46:50 +0100271static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200272{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100273 return block->offloadcnt;
274}
275
276static int tcf_block_offload_cmd(struct tcf_block *block,
277 struct net_device *dev,
278 struct tcf_block_ext_info *ei,
279 enum tc_block_command command)
280{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200281 struct tc_block_offload bo = {};
282
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200283 bo.command = command;
284 bo.binder_type = ei->binder_type;
285 bo.block = block;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100286 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200287}
288
Jiri Pirkocaa72602018-01-17 11:46:50 +0100289static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
290 struct tcf_block_ext_info *ei)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200291{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100292 struct net_device *dev = q->dev_queue->dev;
293 int err;
294
295 if (!dev->netdev_ops->ndo_setup_tc)
296 goto no_offload_dev_inc;
297
298 /* If tc offload feature is disabled and the block we try to bind
299 * to already has some offloaded filters, forbid to bind.
300 */
301 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block))
302 return -EOPNOTSUPP;
303
304 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND);
305 if (err == -EOPNOTSUPP)
306 goto no_offload_dev_inc;
307 return err;
308
309no_offload_dev_inc:
310 if (tcf_block_offload_in_use(block))
311 return -EOPNOTSUPP;
312 block->nooffloaddevcnt++;
313 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200314}
315
316static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
317 struct tcf_block_ext_info *ei)
318{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100319 struct net_device *dev = q->dev_queue->dev;
320 int err;
321
322 if (!dev->netdev_ops->ndo_setup_tc)
323 goto no_offload_dev_dec;
324 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND);
325 if (err == -EOPNOTSUPP)
326 goto no_offload_dev_dec;
327 return;
328
329no_offload_dev_dec:
330 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200331}
332
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100333static int
334tcf_chain_head_change_cb_add(struct tcf_chain *chain,
335 struct tcf_block_ext_info *ei,
336 struct netlink_ext_ack *extack)
337{
338 struct tcf_filter_chain_list_item *item;
339
340 item = kmalloc(sizeof(*item), GFP_KERNEL);
341 if (!item) {
342 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
343 return -ENOMEM;
344 }
345 item->chain_head_change = ei->chain_head_change;
346 item->chain_head_change_priv = ei->chain_head_change_priv;
347 if (chain->filter_chain)
348 tcf_chain_head_change_item(item, chain->filter_chain);
349 list_add(&item->list, &chain->filter_chain_list);
350 return 0;
351}
352
353static void
354tcf_chain_head_change_cb_del(struct tcf_chain *chain,
355 struct tcf_block_ext_info *ei)
356{
357 struct tcf_filter_chain_list_item *item;
358
359 list_for_each_entry(item, &chain->filter_chain_list, list) {
360 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
361 (item->chain_head_change == ei->chain_head_change &&
362 item->chain_head_change_priv == ei->chain_head_change_priv)) {
363 tcf_chain_head_change_item(item, NULL);
364 list_del(&item->list);
365 kfree(item);
366 return;
367 }
368 }
369 WARN_ON(1);
370}
371
Jiri Pirko48617382018-01-17 11:46:46 +0100372struct tcf_net {
373 struct idr idr;
374};
375
376static unsigned int tcf_net_id;
377
378static int tcf_block_insert(struct tcf_block *block, struct net *net,
379 u32 block_index, struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100380{
Jiri Pirko48617382018-01-17 11:46:46 +0100381 struct tcf_net *tn = net_generic(net, tcf_net_id);
382 int err;
383
384 err = idr_alloc_ext(&tn->idr, block, NULL, block_index,
385 block_index + 1, GFP_KERNEL);
386 if (err)
387 return err;
388 block->index = block_index;
389 return 0;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100390}
391
Jiri Pirko48617382018-01-17 11:46:46 +0100392static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200393{
Jiri Pirko48617382018-01-17 11:46:46 +0100394 struct tcf_net *tn = net_generic(net, tcf_net_id);
395
396 idr_remove_ext(&tn->idr, block->index);
397}
398
399static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
400 struct netlink_ext_ack *extack)
401{
402 struct tcf_block *block;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200403 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200404 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200405
Jiri Pirko48617382018-01-17 11:46:46 +0100406 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500407 if (!block) {
408 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100409 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500410 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200411 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200412 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100413 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200414
Jiri Pirko5bc17012017-05-17 11:08:01 +0200415 /* Create chain 0 by default, it has to be always present. */
416 chain = tcf_chain_create(block, 0);
417 if (!chain) {
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500418 NL_SET_ERR_MSG(extack, "Failed to create new tcf chain");
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200419 err = -ENOMEM;
420 goto err_chain_create;
421 }
Jiri Pirko48617382018-01-17 11:46:46 +0100422 block->net = qdisc_net(q);
423 block->refcnt = 1;
424 block->net = net;
425 block->q = q;
426 return block;
427
428err_chain_create:
429 kfree(block);
430 return ERR_PTR(err);
431}
432
433static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
434{
435 struct tcf_net *tn = net_generic(net, tcf_net_id);
436
437 return idr_find_ext(&tn->idr, block_index);
438}
439
440static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
441{
442 return list_first_entry(&block->chain_list, struct tcf_chain, list);
443}
444
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100445struct tcf_block_owner_item {
446 struct list_head list;
447 struct Qdisc *q;
448 enum tcf_block_binder_type binder_type;
449};
450
451static void
452tcf_block_owner_netif_keep_dst(struct tcf_block *block,
453 struct Qdisc *q,
454 enum tcf_block_binder_type binder_type)
455{
456 if (block->keep_dst &&
457 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
458 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
459 netif_keep_dst(qdisc_dev(q));
460}
461
462void tcf_block_netif_keep_dst(struct tcf_block *block)
463{
464 struct tcf_block_owner_item *item;
465
466 block->keep_dst = true;
467 list_for_each_entry(item, &block->owner_list, list)
468 tcf_block_owner_netif_keep_dst(block, item->q,
469 item->binder_type);
470}
471EXPORT_SYMBOL(tcf_block_netif_keep_dst);
472
473static int tcf_block_owner_add(struct tcf_block *block,
474 struct Qdisc *q,
475 enum tcf_block_binder_type binder_type)
476{
477 struct tcf_block_owner_item *item;
478
479 item = kmalloc(sizeof(*item), GFP_KERNEL);
480 if (!item)
481 return -ENOMEM;
482 item->q = q;
483 item->binder_type = binder_type;
484 list_add(&item->list, &block->owner_list);
485 return 0;
486}
487
488static void tcf_block_owner_del(struct tcf_block *block,
489 struct Qdisc *q,
490 enum tcf_block_binder_type binder_type)
491{
492 struct tcf_block_owner_item *item;
493
494 list_for_each_entry(item, &block->owner_list, list) {
495 if (item->q == q && item->binder_type == binder_type) {
496 list_del(&item->list);
497 kfree(item);
498 return;
499 }
500 }
501 WARN_ON(1);
502}
503
Jiri Pirko48617382018-01-17 11:46:46 +0100504int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
505 struct tcf_block_ext_info *ei,
506 struct netlink_ext_ack *extack)
507{
508 struct net *net = qdisc_net(q);
509 struct tcf_block *block = NULL;
510 bool created = false;
511 int err;
512
513 if (ei->block_index) {
514 /* block_index not 0 means the shared block is requested */
515 block = tcf_block_lookup(net, ei->block_index);
516 if (block)
517 block->refcnt++;
518 }
519
520 if (!block) {
521 block = tcf_block_create(net, q, extack);
522 if (IS_ERR(block))
523 return PTR_ERR(block);
524 created = true;
525 if (ei->block_index) {
526 err = tcf_block_insert(block, net,
527 ei->block_index, extack);
528 if (err)
529 goto err_block_insert;
530 }
531 }
532
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100533 err = tcf_block_owner_add(block, q, ei->binder_type);
534 if (err)
535 goto err_block_owner_add;
536
537 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
538
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100539 err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block),
540 ei, extack);
541 if (err)
542 goto err_chain_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100543
544 err = tcf_block_offload_bind(block, q, ei);
545 if (err)
546 goto err_block_offload_bind;
547
Jiri Pirko6529eab2017-05-17 11:07:55 +0200548 *p_block = block;
549 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200550
Jiri Pirkocaa72602018-01-17 11:46:50 +0100551err_block_offload_bind:
552 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100553err_chain_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100554 tcf_block_owner_del(block, q, ei->binder_type);
555err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100556 if (created) {
557 if (tcf_block_shared(block))
558 tcf_block_remove(block, net);
559err_block_insert:
560 kfree(tcf_block_chain_zero(block));
561 kfree(block);
562 } else {
563 block->refcnt--;
564 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200565 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200566}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200567EXPORT_SYMBOL(tcf_block_get_ext);
568
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100569static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
570{
571 struct tcf_proto __rcu **p_filter_chain = priv;
572
573 rcu_assign_pointer(*p_filter_chain, tp_head);
574}
575
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200576int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500577 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
578 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200579{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100580 struct tcf_block_ext_info ei = {
581 .chain_head_change = tcf_chain_head_change_dflt,
582 .chain_head_change_priv = p_filter_chain,
583 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200584
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100585 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500586 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200587}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200588EXPORT_SYMBOL(tcf_block_get);
589
Cong Wang7aa00452017-10-26 18:24:28 -0700590/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100591 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700592 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100593void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900594 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700595{
Cong Wangefbf7892017-12-04 10:48:18 -0800596 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700597
David S. Millerc30abd52017-12-16 22:11:55 -0500598 if (!block)
599 return;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100600 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100601 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100602
Jiri Pirko48617382018-01-17 11:46:46 +0100603 if (--block->refcnt == 0) {
604 if (tcf_block_shared(block))
605 tcf_block_remove(block, block->net);
606
607 /* Hold a refcnt for all chains, so that they don't disappear
608 * while we are iterating.
609 */
610 list_for_each_entry(chain, &block->chain_list, list)
611 tcf_chain_hold(chain);
612
613 list_for_each_entry(chain, &block->chain_list, list)
614 tcf_chain_flush(chain);
615 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700616
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100617 tcf_block_offload_unbind(block, q, ei);
618
Jiri Pirko48617382018-01-17 11:46:46 +0100619 if (block->refcnt == 0) {
620 /* At this point, all the chains should have refcnt >= 1. */
621 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
622 tcf_chain_put(chain);
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100623
Jiri Pirko48617382018-01-17 11:46:46 +0100624 /* Finally, put chain 0 and allow block to be freed. */
625 tcf_chain_put(tcf_block_chain_zero(block));
626 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200627}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200628EXPORT_SYMBOL(tcf_block_put_ext);
629
630void tcf_block_put(struct tcf_block *block)
631{
632 struct tcf_block_ext_info ei = {0, };
633
Jiri Pirko4853f122017-12-21 13:13:59 +0100634 if (!block)
635 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100636 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200637}
David S. Millere1ea2f92017-10-30 14:10:01 +0900638
Jiri Pirko6529eab2017-05-17 11:07:55 +0200639EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100640
Jiri Pirkoacb67442017-10-19 15:50:31 +0200641struct tcf_block_cb {
642 struct list_head list;
643 tc_setup_cb_t *cb;
644 void *cb_ident;
645 void *cb_priv;
646 unsigned int refcnt;
647};
648
649void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
650{
651 return block_cb->cb_priv;
652}
653EXPORT_SYMBOL(tcf_block_cb_priv);
654
655struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
656 tc_setup_cb_t *cb, void *cb_ident)
657{ struct tcf_block_cb *block_cb;
658
659 list_for_each_entry(block_cb, &block->cb_list, list)
660 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
661 return block_cb;
662 return NULL;
663}
664EXPORT_SYMBOL(tcf_block_cb_lookup);
665
666void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
667{
668 block_cb->refcnt++;
669}
670EXPORT_SYMBOL(tcf_block_cb_incref);
671
672unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
673{
674 return --block_cb->refcnt;
675}
676EXPORT_SYMBOL(tcf_block_cb_decref);
677
678struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
679 tc_setup_cb_t *cb, void *cb_ident,
680 void *cb_priv)
681{
682 struct tcf_block_cb *block_cb;
683
Jiri Pirkocaa72602018-01-17 11:46:50 +0100684 /* At this point, playback of previous block cb calls is not supported,
685 * so forbid to register to block which already has some offloaded
686 * filters present.
687 */
688 if (tcf_block_offload_in_use(block))
689 return ERR_PTR(-EOPNOTSUPP);
690
Jiri Pirkoacb67442017-10-19 15:50:31 +0200691 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
692 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100693 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200694 block_cb->cb = cb;
695 block_cb->cb_ident = cb_ident;
696 block_cb->cb_priv = cb_priv;
697 list_add(&block_cb->list, &block->cb_list);
698 return block_cb;
699}
700EXPORT_SYMBOL(__tcf_block_cb_register);
701
702int tcf_block_cb_register(struct tcf_block *block,
703 tc_setup_cb_t *cb, void *cb_ident,
704 void *cb_priv)
705{
706 struct tcf_block_cb *block_cb;
707
708 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100709 return IS_ERR(block_cb) ? PTR_ERR(block_cb) : 0;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200710}
711EXPORT_SYMBOL(tcf_block_cb_register);
712
713void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
714{
715 list_del(&block_cb->list);
716 kfree(block_cb);
717}
718EXPORT_SYMBOL(__tcf_block_cb_unregister);
719
720void tcf_block_cb_unregister(struct tcf_block *block,
721 tc_setup_cb_t *cb, void *cb_ident)
722{
723 struct tcf_block_cb *block_cb;
724
725 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
726 if (!block_cb)
727 return;
728 __tcf_block_cb_unregister(block_cb);
729}
730EXPORT_SYMBOL(tcf_block_cb_unregister);
731
732static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
733 void *type_data, bool err_stop)
734{
735 struct tcf_block_cb *block_cb;
736 int ok_count = 0;
737 int err;
738
Jiri Pirkocaa72602018-01-17 11:46:50 +0100739 /* Make sure all netdevs sharing this block are offload-capable. */
740 if (block->nooffloaddevcnt && err_stop)
741 return -EOPNOTSUPP;
742
Jiri Pirkoacb67442017-10-19 15:50:31 +0200743 list_for_each_entry(block_cb, &block->cb_list, list) {
744 err = block_cb->cb(type, type_data, block_cb->cb_priv);
745 if (err) {
746 if (err_stop)
747 return err;
748 } else {
749 ok_count++;
750 }
751 }
752 return ok_count;
753}
754
Jiri Pirko87d83092017-05-17 11:07:54 +0200755/* Main classifier routine: scans classifier chain attached
756 * to this qdisc, (optionally) tests for protocol and asks
757 * specific classifiers.
758 */
759int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
760 struct tcf_result *res, bool compat_mode)
761{
762 __be16 protocol = tc_skb_protocol(skb);
763#ifdef CONFIG_NET_CLS_ACT
764 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200765 const struct tcf_proto *orig_tp = tp;
766 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200767 int limit = 0;
768
769reclassify:
770#endif
771 for (; tp; tp = rcu_dereference_bh(tp->next)) {
772 int err;
773
774 if (tp->protocol != protocol &&
775 tp->protocol != htons(ETH_P_ALL))
776 continue;
777
778 err = tp->classify(skb, tp, res);
779#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200780 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200781 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200782 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200783 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200784 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200785 goto reset;
786 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200787#endif
788 if (err >= 0)
789 return err;
790 }
791
792 return TC_ACT_UNSPEC; /* signal: continue lookup */
793#ifdef CONFIG_NET_CLS_ACT
794reset:
795 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100796 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
797 tp->chain->block->index,
798 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200799 ntohs(tp->protocol));
800 return TC_ACT_SHOT;
801 }
802
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200803 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200804 protocol = tc_skb_protocol(skb);
805 goto reclassify;
806#endif
807}
808EXPORT_SYMBOL(tcf_classify);
809
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200810struct tcf_chain_info {
811 struct tcf_proto __rcu **pprev;
812 struct tcf_proto __rcu *next;
813};
814
815static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
816{
817 return rtnl_dereference(*chain_info->pprev);
818}
819
820static void tcf_chain_tp_insert(struct tcf_chain *chain,
821 struct tcf_chain_info *chain_info,
822 struct tcf_proto *tp)
823{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100824 if (*chain_info->pprev == chain->filter_chain)
825 tcf_chain_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200826 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
827 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700828 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200829}
830
831static void tcf_chain_tp_remove(struct tcf_chain *chain,
832 struct tcf_chain_info *chain_info,
833 struct tcf_proto *tp)
834{
835 struct tcf_proto *next = rtnl_dereference(chain_info->next);
836
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100837 if (tp == chain->filter_chain)
838 tcf_chain_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200839 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700840 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200841}
842
843static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
844 struct tcf_chain_info *chain_info,
845 u32 protocol, u32 prio,
846 bool prio_allocate)
847{
848 struct tcf_proto **pprev;
849 struct tcf_proto *tp;
850
851 /* Check the chain for existence of proto-tcf with this priority */
852 for (pprev = &chain->filter_chain;
853 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
854 if (tp->prio >= prio) {
855 if (tp->prio == prio) {
856 if (prio_allocate ||
857 (tp->protocol != protocol && protocol))
858 return ERR_PTR(-EINVAL);
859 } else {
860 tp = NULL;
861 }
862 break;
863 }
864 }
865 chain_info->pprev = pprev;
866 chain_info->next = tp ? tp->next : NULL;
867 return tp;
868}
869
WANG Cong71203712017-08-07 15:26:50 -0700870static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100871 struct tcf_proto *tp, struct tcf_block *block,
872 struct Qdisc *q, u32 parent, void *fh,
873 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700874{
875 struct tcmsg *tcm;
876 struct nlmsghdr *nlh;
877 unsigned char *b = skb_tail_pointer(skb);
878
879 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
880 if (!nlh)
881 goto out_nlmsg_trim;
882 tcm = nlmsg_data(nlh);
883 tcm->tcm_family = AF_UNSPEC;
884 tcm->tcm__pad1 = 0;
885 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100886 if (q) {
887 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
888 tcm->tcm_parent = parent;
889 } else {
890 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
891 tcm->tcm_block_index = block->index;
892 }
WANG Cong71203712017-08-07 15:26:50 -0700893 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
894 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
895 goto nla_put_failure;
896 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
897 goto nla_put_failure;
898 if (!fh) {
899 tcm->tcm_handle = 0;
900 } else {
901 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
902 goto nla_put_failure;
903 }
904 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
905 return skb->len;
906
907out_nlmsg_trim:
908nla_put_failure:
909 nlmsg_trim(skb, b);
910 return -1;
911}
912
913static int tfilter_notify(struct net *net, struct sk_buff *oskb,
914 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100915 struct tcf_block *block, struct Qdisc *q,
916 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -0700917{
918 struct sk_buff *skb;
919 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
920
921 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
922 if (!skb)
923 return -ENOBUFS;
924
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100925 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
926 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -0700927 kfree_skb(skb);
928 return -EINVAL;
929 }
930
931 if (unicast)
932 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
933
934 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
935 n->nlmsg_flags & NLM_F_ECHO);
936}
937
938static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
939 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100940 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500941 u32 parent, void *fh, bool unicast, bool *last,
942 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -0700943{
944 struct sk_buff *skb;
945 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
946 int err;
947
948 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
949 if (!skb)
950 return -ENOBUFS;
951
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100952 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
953 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500954 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -0700955 kfree_skb(skb);
956 return -EINVAL;
957 }
958
Alexander Aring571acf22018-01-18 11:20:53 -0500959 err = tp->ops->delete(tp, fh, last, extack);
WANG Cong71203712017-08-07 15:26:50 -0700960 if (err) {
961 kfree_skb(skb);
962 return err;
963 }
964
965 if (unicast)
966 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
967
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500968 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
969 n->nlmsg_flags & NLM_F_ECHO);
970 if (err < 0)
971 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
972 return err;
WANG Cong71203712017-08-07 15:26:50 -0700973}
974
975static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100976 struct tcf_block *block, struct Qdisc *q,
977 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -0700978 struct tcf_chain *chain, int event)
979{
980 struct tcf_proto *tp;
981
982 for (tp = rtnl_dereference(chain->filter_chain);
983 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100984 tfilter_notify(net, oskb, n, tp, block,
985 q, parent, 0, event, false);
WANG Cong71203712017-08-07 15:26:50 -0700986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/* Add/change/delete/get a filter node */
989
David Ahernc21ef3e2017-04-16 09:48:24 -0700990static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
991 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900993 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800994 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct tcmsg *t;
996 u32 protocol;
997 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200998 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001000 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001001 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001002 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001003 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001004 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001007 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001009 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Stéphane Graber4e8bbb82014-04-30 11:25:43 -04001011 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -04001012 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001013 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001016 tp_created = 0;
1017
David Ahernc21ef3e2017-04-16 09:48:24 -07001018 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001019 if (err < 0)
1020 return err;
1021
David S. Miller942b8162012-06-26 21:48:50 -07001022 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 protocol = TC_H_MIN(t->tcm_info);
1024 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001025 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 parent = t->tcm_parent;
1027 cl = 0;
1028
1029 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001030 switch (n->nlmsg_type) {
1031 case RTM_DELTFILTER:
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001032 if (protocol || t->tcm_handle || tca[TCA_KIND]) {
1033 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001034 return -ENOENT;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001035 }
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001036 break;
1037 case RTM_NEWTFILTER:
1038 /* If no priority is provided by the user,
1039 * we allocate one.
1040 */
1041 if (n->nlmsg_flags & NLM_F_CREATE) {
1042 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001043 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001044 break;
1045 }
1046 /* fall-through */
1047 default:
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001048 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
1053 /* Find head of filter chain. */
1054
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001055 if (t->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1056 block = tcf_block_lookup(net, t->tcm_block_index);
1057 if (!block) {
1058 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1059 err = -EINVAL;
1060 goto errout;
1061 }
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001062 } else {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001063 const struct Qdisc_class_ops *cops;
1064 struct net_device *dev;
1065
1066 /* Find link */
1067 dev = __dev_get_by_index(net, t->tcm_ifindex);
1068 if (!dev)
1069 return -ENODEV;
1070
1071 /* Find qdisc */
1072 if (!parent) {
1073 q = dev->qdisc;
1074 parent = q->handle;
1075 } else {
1076 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001077 if (!q) {
1078 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001079 return -EINVAL;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001080 }
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001081 }
1082
1083 /* Is it classful? */
1084 cops = q->ops->cl_ops;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001085 if (!cops) {
1086 NL_SET_ERR_MSG(extack, "Qdisc not classful");
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001087 return -EINVAL;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001090 if (!cops->tcf_block) {
1091 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001092 return -EOPNOTSUPP;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001095 /* Do we search for filter, attached to class? */
1096 if (TC_H_MIN(parent)) {
1097 cl = cops->find(q, parent);
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001098 if (cl == 0) {
1099 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001100 return -ENOENT;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001101 }
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001102 }
Patrick McHardy71ebe5e2009-09-04 06:41:15 +00001103
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001104 /* And the last stroke */
1105 block = cops->tcf_block(q, cl, extack);
1106 if (!block) {
1107 err = -EINVAL;
1108 goto errout;
1109 }
1110 if (tcf_block_shared(block)) {
1111 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1112 err = -EOPNOTSUPP;
1113 goto errout;
1114 }
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001115 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001116
1117 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1118 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001119 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001120 err = -EINVAL;
1121 goto errout;
1122 }
WANG Cong367a8ce2017-05-23 09:42:37 -07001123 chain = tcf_chain_get(block, chain_index,
1124 n->nlmsg_type == RTM_NEWTFILTER);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001125 if (!chain) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001126 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
WANG Cong367a8ce2017-05-23 09:42:37 -07001127 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001128 goto errout;
1129 }
Jiri Pirko6529eab2017-05-17 11:07:55 +02001130
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001131 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001132 tfilter_notify_chain(net, skb, block, q, parent, n,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001133 chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +02001134 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001135 err = 0;
1136 goto errout;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001139 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1140 prio, prio_allocate);
1141 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001142 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001143 err = PTR_ERR(tp);
1144 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 if (tp == NULL) {
1148 /* Proto-tcf does not exist, create new one */
1149
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001150 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001151 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001152 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001156 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001157 !(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001158 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001159 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001163 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001164 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jiri Pirko33a48922017-02-09 14:38:57 +01001166 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001167 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001168 if (IS_ERR(tp)) {
1169 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 goto errout;
1171 }
Minoru Usui12186be2009-06-02 02:17:34 -07001172 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001173 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001174 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001175 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 fh = tp->ops->get(tp, t->tcm_handle);
1180
WANG Cong8113c092017-08-04 21:31:43 -07001181 if (!fh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001183 tcf_chain_tp_remove(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001184 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -07001185 RTM_DELTFILTER, false);
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001186 tcf_proto_destroy(tp, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 err = 0;
1188 goto errout;
1189 }
1190
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001191 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001192 !(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001193 NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001194 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 } else {
WANG Cong763dbf62017-04-19 14:21:21 -07001198 bool last;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001201 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -07001202 if (n->nlmsg_flags & NLM_F_EXCL) {
1203 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001204 tcf_proto_destroy(tp, NULL);
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001205 NL_SET_ERR_MSG(extack, "Filter already exists");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001206 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -07001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 break;
1210 case RTM_DELTFILTER:
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001211 err = tfilter_del_notify(net, skb, n, tp, block,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001212 q, parent, fh, false, &last,
1213 extack);
Jiri Pirko40c81b22017-02-09 14:39:00 +01001214 if (err)
1215 goto errout;
WANG Cong763dbf62017-04-19 14:21:21 -07001216 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001217 tcf_chain_tp_remove(chain, &chain_info, tp);
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001218 tcf_proto_destroy(tp, extack);
WANG Cong763dbf62017-04-19 14:21:21 -07001219 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +01001220 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 case RTM_GETTFILTER:
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001222 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1223 fh, RTM_NEWTFILTER, true);
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001224 if (err < 0)
1225 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 goto errout;
1227 default:
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001228 NL_SET_ERR_MSG(extack, "Invalid netlink message type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 err = -EINVAL;
1230 goto errout;
1231 }
1232 }
1233
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001234 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
Alexander Aring7306db32018-01-18 11:20:51 -05001235 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE,
1236 extack);
Minoru Usui12186be2009-06-02 02:17:34 -07001237 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001238 if (tp_created)
1239 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001240 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001241 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001242 } else {
1243 if (tp_created)
Jakub Kicinski715df5e2018-01-24 12:54:13 -08001244 tcf_proto_destroy(tp, NULL);
Minoru Usui12186be2009-06-02 02:17:34 -07001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001248 if (chain)
1249 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (err == -EAGAIN)
1251 /* Replay the request. */
1252 goto replay;
1253 return err;
1254}
1255
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001256struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct tcf_walker w;
1258 struct sk_buff *skb;
1259 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001260 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001261 struct Qdisc *q;
1262 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263};
1264
WANG Cong8113c092017-08-04 21:31:43 -07001265static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001267 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001268 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001270 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001271 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001272 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1273 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001276static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1277 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001278 long index_start, long *p_index)
1279{
1280 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001281 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001282 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1283 struct tcf_dump_args arg;
1284 struct tcf_proto *tp;
1285
1286 for (tp = rtnl_dereference(chain->filter_chain);
1287 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1288 if (*p_index < index_start)
1289 continue;
1290 if (TC_H_MAJ(tcm->tcm_info) &&
1291 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1292 continue;
1293 if (TC_H_MIN(tcm->tcm_info) &&
1294 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1295 continue;
1296 if (*p_index > index_start)
1297 memset(&cb->args[1], 0,
1298 sizeof(cb->args) - sizeof(cb->args[0]));
1299 if (cb->args[1] == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001300 if (tcf_fill_node(net, skb, tp, block, q, parent, 0,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001301 NETLINK_CB(cb->skb).portid,
1302 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1303 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001304 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001305
1306 cb->args[1] = 1;
1307 }
1308 if (!tp->ops->walk)
1309 continue;
1310 arg.w.fn = tcf_node_dump;
1311 arg.skb = skb;
1312 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001313 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001314 arg.q = q;
1315 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001316 arg.w.stop = 0;
1317 arg.w.skip = cb->args[1] - 1;
1318 arg.w.count = 0;
1319 tp->ops->walk(tp, &arg.w);
1320 cb->args[1] = arg.w.count + 1;
1321 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001322 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001323 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001324 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001325}
1326
Eric Dumazetbd27a872009-11-05 20:57:26 -08001327/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1329{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001330 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001331 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001332 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001333 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001334 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001335 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001336 long index_start;
1337 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001338 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001339 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Hong zhi guo573ce262013-03-27 06:47:04 +00001341 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001343
1344 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1345 if (err)
1346 return err;
1347
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001348 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1349 block = tcf_block_lookup(net, tcm->tcm_block_index);
1350 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001351 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001352 /* If we work with block index, q is NULL and parent value
1353 * will never be used in the following code. The check
1354 * in tcf_fill_node prevents it. However, compiler does not
1355 * see that far, so set parent to zero to silence the warning
1356 * about parent being uninitialized.
1357 */
1358 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001359 } else {
1360 const struct Qdisc_class_ops *cops;
1361 struct net_device *dev;
1362 unsigned long cl = 0;
1363
1364 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1365 if (!dev)
1366 return skb->len;
1367
1368 parent = tcm->tcm_parent;
1369 if (!parent) {
1370 q = dev->qdisc;
1371 parent = q->handle;
1372 } else {
1373 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1374 }
1375 if (!q)
1376 goto out;
1377 cops = q->ops->cl_ops;
1378 if (!cops)
1379 goto out;
1380 if (!cops->tcf_block)
1381 goto out;
1382 if (TC_H_MIN(tcm->tcm_parent)) {
1383 cl = cops->find(q, tcm->tcm_parent);
1384 if (cl == 0)
1385 goto out;
1386 }
1387 block = cops->tcf_block(q, cl, NULL);
1388 if (!block)
1389 goto out;
1390 if (tcf_block_shared(block))
1391 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001394 index_start = cb->args[0];
1395 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001396
1397 list_for_each_entry(chain, &block->chain_list, list) {
1398 if (tca[TCA_CHAIN] &&
1399 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1400 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001401 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1402 index_start, &index))
Jiri Pirko5bc17012017-05-17 11:08:01 +02001403 break;
1404 }
1405
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001406 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return skb->len;
1410}
1411
WANG Cong18d02642014-09-25 10:26:37 -07001412void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001415 LIST_HEAD(actions);
1416
Cong Wang2d132eb2017-10-26 18:24:40 -07001417 ASSERT_RTNL();
WANG Cong22dc13c2016-08-13 22:35:00 -07001418 tcf_exts_to_list(exts, &actions);
1419 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1420 kfree(exts->actions);
1421 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422#endif
1423}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001424EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00001426int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05001427 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr,
1428 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430#ifdef CONFIG_NET_CLS_ACT
1431 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 struct tc_action *act;
1433
WANG Cong5da57f42013-12-15 20:15:07 -08001434 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001435 act = tcf_action_init_1(net, tp, tb[exts->police],
1436 rate_tlv, "police", ovr,
1437 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001438 if (IS_ERR(act))
1439 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
WANG Cong33be6272013-12-15 20:15:05 -08001441 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07001442 exts->actions[0] = act;
1443 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08001444 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001445 LIST_HEAD(actions);
1446 int err, i = 0;
1447
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001448 err = tcf_action_init(net, tp, tb[exts->action],
1449 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001450 &actions);
WANG Cong33be6272013-12-15 20:15:05 -08001451 if (err)
1452 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -07001453 list_for_each_entry(act, &actions, list)
1454 exts->actions[i++] = act;
1455 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Cong Wange4b95c42017-11-06 13:47:19 -08001457 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459#else
WANG Cong5da57f42013-12-15 20:15:07 -08001460 if ((exts->action && tb[exts->action]) ||
Alexander Aring50a56192018-01-18 11:20:52 -05001461 (exts->police && tb[exts->police])) {
1462 NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return -EOPNOTSUPP;
Alexander Aring50a56192018-01-18 11:20:52 -05001464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465#endif
1466
1467 return 0;
1468}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001469EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001471void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
1473#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001474 struct tcf_exts old = *dst;
1475
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001476 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07001477 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478#endif
1479}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001480EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
WANG Cong22dc13c2016-08-13 22:35:00 -07001482#ifdef CONFIG_NET_CLS_ACT
1483static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1484{
1485 if (exts->nr_actions == 0)
1486 return NULL;
1487 else
1488 return exts->actions[0];
1489}
1490#endif
WANG Cong33be6272013-12-15 20:15:05 -08001491
WANG Cong5da57f42013-12-15 20:15:07 -08001492int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07001495 struct nlattr *nest;
1496
Jiri Pirko978dfd82017-08-04 14:29:03 +02001497 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 /*
1499 * again for backward compatible mode - we want
1500 * to work with both old and new modes of entering
1501 * tc data even if iproute2 was newer - jhs
1502 */
WANG Cong33be6272013-12-15 20:15:05 -08001503 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001504 LIST_HEAD(actions);
1505
WANG Cong5da57f42013-12-15 20:15:07 -08001506 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001507 if (nest == NULL)
1508 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07001509
1510 tcf_exts_to_list(exts, &actions);
1511 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001512 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001513 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08001514 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08001515 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08001516 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001517 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001518 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08001519 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001520 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001521 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001525
1526nla_put_failure:
1527 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001529#else
1530 return 0;
1531#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001533EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001535
WANG Cong5da57f42013-12-15 20:15:07 -08001536int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001539 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001540 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001541 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542#endif
1543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001545EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Jiri Pirko717503b2017-10-11 09:41:09 +02001547static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1548 enum tc_setup_type type,
1549 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001550{
1551 int ok_count = 0;
1552#ifdef CONFIG_NET_CLS_ACT
1553 const struct tc_action *a;
1554 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001555 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001556
1557 if (!tcf_exts_has_actions(exts))
1558 return 0;
1559
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001560 for (i = 0; i < exts->nr_actions; i++) {
1561 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001562 if (!a->ops->get_dev)
1563 continue;
1564 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01001565 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001566 continue;
1567 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1568 if (ret < 0)
1569 return ret;
1570 ok_count += ret;
1571 }
1572#endif
1573 return ok_count;
1574}
Jiri Pirko717503b2017-10-11 09:41:09 +02001575
Jiri Pirko208c0f42017-10-19 15:50:32 +02001576int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1577 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02001578{
Jiri Pirko208c0f42017-10-19 15:50:32 +02001579 int ok_count;
1580 int ret;
1581
1582 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1583 if (ret < 0)
1584 return ret;
1585 ok_count = ret;
1586
1587 if (!exts)
1588 return ok_count;
1589 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1590 if (ret < 0)
1591 return ret;
1592 ok_count += ret;
1593
1594 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02001595}
1596EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001597
Jiri Pirko48617382018-01-17 11:46:46 +01001598static __net_init int tcf_net_init(struct net *net)
1599{
1600 struct tcf_net *tn = net_generic(net, tcf_net_id);
1601
1602 idr_init(&tn->idr);
1603 return 0;
1604}
1605
1606static void __net_exit tcf_net_exit(struct net *net)
1607{
1608 struct tcf_net *tn = net_generic(net, tcf_net_id);
1609
1610 idr_destroy(&tn->idr);
1611}
1612
1613static struct pernet_operations tcf_net_ops = {
1614 .init = tcf_net_init,
1615 .exit = tcf_net_exit,
1616 .id = &tcf_net_id,
1617 .size = sizeof(struct tcf_net),
1618};
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620static int __init tc_filter_init(void)
1621{
Jiri Pirko48617382018-01-17 11:46:46 +01001622 int err;
1623
Cong Wang7aa00452017-10-26 18:24:28 -07001624 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1625 if (!tc_filter_wq)
1626 return -ENOMEM;
1627
Jiri Pirko48617382018-01-17 11:46:46 +01001628 err = register_pernet_subsys(&tcf_net_ops);
1629 if (err)
1630 goto err_register_pernet_subsys;
1631
Florian Westphalb97bac62017-08-09 20:41:48 +02001632 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1633 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
Thomas Graf82623c02007-03-22 11:56:22 -07001634 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001635 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01001638
1639err_register_pernet_subsys:
1640 destroy_workqueue(tc_filter_wq);
1641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
1644subsys_initcall(tc_filter_init);