blob: 3e3841adbd5c57ad650937af6ec58145e9a4689a [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
WANG Cong763dbf62017-04-19 14:21:21 -0700175static void tcf_proto_destroy(struct tcf_proto *tp)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100176{
WANG Cong763dbf62017-04-19 14:21:21 -0700177 tp->ops->destroy(tp);
178 module_put(tp->ops->owner);
179 kfree_rcu(tp, rcu);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100180}
181
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100182struct tcf_filter_chain_list_item {
183 struct list_head list;
184 tcf_chain_head_change_t *chain_head_change;
185 void *chain_head_change_priv;
186};
187
Jiri Pirko5bc17012017-05-17 11:08:01 +0200188static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
189 u32 chain_index)
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200190{
Jiri Pirko5bc17012017-05-17 11:08:01 +0200191 struct tcf_chain *chain;
192
193 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
194 if (!chain)
195 return NULL;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100196 INIT_LIST_HEAD(&chain->filter_chain_list);
Jiri Pirko5bc17012017-05-17 11:08:01 +0200197 list_add_tail(&chain->list, &block->chain_list);
198 chain->block = block;
199 chain->index = chain_index;
Cong Wange2ef7542017-09-11 16:33:31 -0700200 chain->refcnt = 1;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200201 return chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200202}
203
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100204static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item,
205 struct tcf_proto *tp_head)
206{
207 if (item->chain_head_change)
208 item->chain_head_change(tp_head, item->chain_head_change_priv);
209}
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100210static void tcf_chain_head_change(struct tcf_chain *chain,
211 struct tcf_proto *tp_head)
212{
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100213 struct tcf_filter_chain_list_item *item;
214
215 list_for_each_entry(item, &chain->filter_chain_list, list)
216 tcf_chain_head_change_item(item, tp_head);
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100217}
218
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200219static void tcf_chain_flush(struct tcf_chain *chain)
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100220{
Roman Kapld7aa04a2017-11-20 22:21:13 +0100221 struct tcf_proto *tp = rtnl_dereference(chain->filter_chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100222
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100223 tcf_chain_head_change(chain, NULL);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100224 while (tp) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200225 RCU_INIT_POINTER(chain->filter_chain, tp->next);
WANG Cong763dbf62017-04-19 14:21:21 -0700226 tcf_proto_destroy(tp);
Roman Kapld7aa04a2017-11-20 22:21:13 +0100227 tp = rtnl_dereference(chain->filter_chain);
228 tcf_chain_put(chain);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100229 }
Jiri Pirkof93e1cd2017-05-20 15:01:32 +0200230}
231
232static void tcf_chain_destroy(struct tcf_chain *chain)
233{
Cong Wangefbf7892017-12-04 10:48:18 -0800234 struct tcf_block *block = chain->block;
235
Cong Wange2ef7542017-09-11 16:33:31 -0700236 list_del(&chain->list);
237 kfree(chain);
Cong Wangefbf7892017-12-04 10:48:18 -0800238 if (list_empty(&block->chain_list))
239 kfree(block);
Cong Wange2ef7542017-09-11 16:33:31 -0700240}
Jiri Pirko744a4cf2017-08-22 22:46:49 +0200241
Cong Wange2ef7542017-09-11 16:33:31 -0700242static void tcf_chain_hold(struct tcf_chain *chain)
243{
244 ++chain->refcnt;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200245}
246
WANG Cong367a8ce2017-05-23 09:42:37 -0700247struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
248 bool create)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200249{
250 struct tcf_chain *chain;
251
252 list_for_each_entry(chain, &block->chain_list, list) {
Cong Wange2ef7542017-09-11 16:33:31 -0700253 if (chain->index == chain_index) {
254 tcf_chain_hold(chain);
255 return chain;
256 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200257 }
Jiri Pirko80532382017-09-06 13:14:19 +0200258
Cong Wange2ef7542017-09-11 16:33:31 -0700259 return create ? tcf_chain_create(block, chain_index) : NULL;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200260}
261EXPORT_SYMBOL(tcf_chain_get);
262
263void tcf_chain_put(struct tcf_chain *chain)
264{
Cong Wange2ef7542017-09-11 16:33:31 -0700265 if (--chain->refcnt == 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +0200266 tcf_chain_destroy(chain);
267}
268EXPORT_SYMBOL(tcf_chain_put);
269
Jiri Pirkocaa72602018-01-17 11:46:50 +0100270static bool tcf_block_offload_in_use(struct tcf_block *block)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200271{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100272 return block->offloadcnt;
273}
274
275static int tcf_block_offload_cmd(struct tcf_block *block,
276 struct net_device *dev,
277 struct tcf_block_ext_info *ei,
278 enum tc_block_command command)
279{
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200280 struct tc_block_offload bo = {};
281
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200282 bo.command = command;
283 bo.binder_type = ei->binder_type;
284 bo.block = block;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100285 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200286}
287
Jiri Pirkocaa72602018-01-17 11:46:50 +0100288static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q,
289 struct tcf_block_ext_info *ei)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200290{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100291 struct net_device *dev = q->dev_queue->dev;
292 int err;
293
294 if (!dev->netdev_ops->ndo_setup_tc)
295 goto no_offload_dev_inc;
296
297 /* If tc offload feature is disabled and the block we try to bind
298 * to already has some offloaded filters, forbid to bind.
299 */
300 if (!tc_can_offload(dev) && tcf_block_offload_in_use(block))
301 return -EOPNOTSUPP;
302
303 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND);
304 if (err == -EOPNOTSUPP)
305 goto no_offload_dev_inc;
306 return err;
307
308no_offload_dev_inc:
309 if (tcf_block_offload_in_use(block))
310 return -EOPNOTSUPP;
311 block->nooffloaddevcnt++;
312 return 0;
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200313}
314
315static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q,
316 struct tcf_block_ext_info *ei)
317{
Jiri Pirkocaa72602018-01-17 11:46:50 +0100318 struct net_device *dev = q->dev_queue->dev;
319 int err;
320
321 if (!dev->netdev_ops->ndo_setup_tc)
322 goto no_offload_dev_dec;
323 err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND);
324 if (err == -EOPNOTSUPP)
325 goto no_offload_dev_dec;
326 return;
327
328no_offload_dev_dec:
329 WARN_ON(block->nooffloaddevcnt-- == 0);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200330}
331
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100332static int
333tcf_chain_head_change_cb_add(struct tcf_chain *chain,
334 struct tcf_block_ext_info *ei,
335 struct netlink_ext_ack *extack)
336{
337 struct tcf_filter_chain_list_item *item;
338
339 item = kmalloc(sizeof(*item), GFP_KERNEL);
340 if (!item) {
341 NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed");
342 return -ENOMEM;
343 }
344 item->chain_head_change = ei->chain_head_change;
345 item->chain_head_change_priv = ei->chain_head_change_priv;
346 if (chain->filter_chain)
347 tcf_chain_head_change_item(item, chain->filter_chain);
348 list_add(&item->list, &chain->filter_chain_list);
349 return 0;
350}
351
352static void
353tcf_chain_head_change_cb_del(struct tcf_chain *chain,
354 struct tcf_block_ext_info *ei)
355{
356 struct tcf_filter_chain_list_item *item;
357
358 list_for_each_entry(item, &chain->filter_chain_list, list) {
359 if ((!ei->chain_head_change && !ei->chain_head_change_priv) ||
360 (item->chain_head_change == ei->chain_head_change &&
361 item->chain_head_change_priv == ei->chain_head_change_priv)) {
362 tcf_chain_head_change_item(item, NULL);
363 list_del(&item->list);
364 kfree(item);
365 return;
366 }
367 }
368 WARN_ON(1);
369}
370
Jiri Pirko48617382018-01-17 11:46:46 +0100371struct tcf_net {
372 struct idr idr;
373};
374
375static unsigned int tcf_net_id;
376
377static int tcf_block_insert(struct tcf_block *block, struct net *net,
378 u32 block_index, struct netlink_ext_ack *extack)
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100379{
Jiri Pirko48617382018-01-17 11:46:46 +0100380 struct tcf_net *tn = net_generic(net, tcf_net_id);
381 int err;
382
383 err = idr_alloc_ext(&tn->idr, block, NULL, block_index,
384 block_index + 1, GFP_KERNEL);
385 if (err)
386 return err;
387 block->index = block_index;
388 return 0;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100389}
390
Jiri Pirko48617382018-01-17 11:46:46 +0100391static void tcf_block_remove(struct tcf_block *block, struct net *net)
Jiri Pirko6529eab2017-05-17 11:07:55 +0200392{
Jiri Pirko48617382018-01-17 11:46:46 +0100393 struct tcf_net *tn = net_generic(net, tcf_net_id);
394
395 idr_remove_ext(&tn->idr, block->index);
396}
397
398static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q,
399 struct netlink_ext_ack *extack)
400{
401 struct tcf_block *block;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200402 struct tcf_chain *chain;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200403 int err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200404
Jiri Pirko48617382018-01-17 11:46:46 +0100405 block = kzalloc(sizeof(*block), GFP_KERNEL);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500406 if (!block) {
407 NL_SET_ERR_MSG(extack, "Memory allocation for block failed");
Jiri Pirko48617382018-01-17 11:46:46 +0100408 return ERR_PTR(-ENOMEM);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500409 }
Jiri Pirko5bc17012017-05-17 11:08:01 +0200410 INIT_LIST_HEAD(&block->chain_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200411 INIT_LIST_HEAD(&block->cb_list);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100412 INIT_LIST_HEAD(&block->owner_list);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200413
Jiri Pirko5bc17012017-05-17 11:08:01 +0200414 /* Create chain 0 by default, it has to be always present. */
415 chain = tcf_chain_create(block, 0);
416 if (!chain) {
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500417 NL_SET_ERR_MSG(extack, "Failed to create new tcf chain");
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200418 err = -ENOMEM;
419 goto err_chain_create;
420 }
Jiri Pirko48617382018-01-17 11:46:46 +0100421 block->net = qdisc_net(q);
422 block->refcnt = 1;
423 block->net = net;
424 block->q = q;
425 return block;
426
427err_chain_create:
428 kfree(block);
429 return ERR_PTR(err);
430}
431
432static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index)
433{
434 struct tcf_net *tn = net_generic(net, tcf_net_id);
435
436 return idr_find_ext(&tn->idr, block_index);
437}
438
439static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block)
440{
441 return list_first_entry(&block->chain_list, struct tcf_chain, list);
442}
443
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100444struct tcf_block_owner_item {
445 struct list_head list;
446 struct Qdisc *q;
447 enum tcf_block_binder_type binder_type;
448};
449
450static void
451tcf_block_owner_netif_keep_dst(struct tcf_block *block,
452 struct Qdisc *q,
453 enum tcf_block_binder_type binder_type)
454{
455 if (block->keep_dst &&
456 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
457 binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
458 netif_keep_dst(qdisc_dev(q));
459}
460
461void tcf_block_netif_keep_dst(struct tcf_block *block)
462{
463 struct tcf_block_owner_item *item;
464
465 block->keep_dst = true;
466 list_for_each_entry(item, &block->owner_list, list)
467 tcf_block_owner_netif_keep_dst(block, item->q,
468 item->binder_type);
469}
470EXPORT_SYMBOL(tcf_block_netif_keep_dst);
471
472static int tcf_block_owner_add(struct tcf_block *block,
473 struct Qdisc *q,
474 enum tcf_block_binder_type binder_type)
475{
476 struct tcf_block_owner_item *item;
477
478 item = kmalloc(sizeof(*item), GFP_KERNEL);
479 if (!item)
480 return -ENOMEM;
481 item->q = q;
482 item->binder_type = binder_type;
483 list_add(&item->list, &block->owner_list);
484 return 0;
485}
486
487static void tcf_block_owner_del(struct tcf_block *block,
488 struct Qdisc *q,
489 enum tcf_block_binder_type binder_type)
490{
491 struct tcf_block_owner_item *item;
492
493 list_for_each_entry(item, &block->owner_list, list) {
494 if (item->q == q && item->binder_type == binder_type) {
495 list_del(&item->list);
496 kfree(item);
497 return;
498 }
499 }
500 WARN_ON(1);
501}
502
Jiri Pirko48617382018-01-17 11:46:46 +0100503int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
504 struct tcf_block_ext_info *ei,
505 struct netlink_ext_ack *extack)
506{
507 struct net *net = qdisc_net(q);
508 struct tcf_block *block = NULL;
509 bool created = false;
510 int err;
511
512 if (ei->block_index) {
513 /* block_index not 0 means the shared block is requested */
514 block = tcf_block_lookup(net, ei->block_index);
515 if (block)
516 block->refcnt++;
517 }
518
519 if (!block) {
520 block = tcf_block_create(net, q, extack);
521 if (IS_ERR(block))
522 return PTR_ERR(block);
523 created = true;
524 if (ei->block_index) {
525 err = tcf_block_insert(block, net,
526 ei->block_index, extack);
527 if (err)
528 goto err_block_insert;
529 }
530 }
531
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100532 err = tcf_block_owner_add(block, q, ei->binder_type);
533 if (err)
534 goto err_block_owner_add;
535
536 tcf_block_owner_netif_keep_dst(block, q, ei->binder_type);
537
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100538 err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block),
539 ei, extack);
540 if (err)
541 goto err_chain_head_change_cb_add;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100542
543 err = tcf_block_offload_bind(block, q, ei);
544 if (err)
545 goto err_block_offload_bind;
546
Jiri Pirko6529eab2017-05-17 11:07:55 +0200547 *p_block = block;
548 return 0;
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200549
Jiri Pirkocaa72602018-01-17 11:46:50 +0100550err_block_offload_bind:
551 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100552err_chain_head_change_cb_add:
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100553 tcf_block_owner_del(block, q, ei->binder_type);
554err_block_owner_add:
Jiri Pirko48617382018-01-17 11:46:46 +0100555 if (created) {
556 if (tcf_block_shared(block))
557 tcf_block_remove(block, net);
558err_block_insert:
559 kfree(tcf_block_chain_zero(block));
560 kfree(block);
561 } else {
562 block->refcnt--;
563 }
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200564 return err;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200565}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200566EXPORT_SYMBOL(tcf_block_get_ext);
567
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100568static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv)
569{
570 struct tcf_proto __rcu **p_filter_chain = priv;
571
572 rcu_assign_pointer(*p_filter_chain, tp_head);
573}
574
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200575int tcf_block_get(struct tcf_block **p_block,
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500576 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
577 struct netlink_ext_ack *extack)
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200578{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100579 struct tcf_block_ext_info ei = {
580 .chain_head_change = tcf_chain_head_change_dflt,
581 .chain_head_change_priv = p_filter_chain,
582 };
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200583
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100584 WARN_ON(!p_filter_chain);
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500585 return tcf_block_get_ext(p_block, q, &ei, extack);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200586}
Jiri Pirko6529eab2017-05-17 11:07:55 +0200587EXPORT_SYMBOL(tcf_block_get);
588
Cong Wang7aa00452017-10-26 18:24:28 -0700589/* XXX: Standalone actions are not allowed to jump to any chain, and bound
Roman Kapla60b3f52017-11-24 12:27:58 +0100590 * actions should be all removed after flushing.
Cong Wang7aa00452017-10-26 18:24:28 -0700591 */
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100592void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
David S. Millere1ea2f92017-10-30 14:10:01 +0900593 struct tcf_block_ext_info *ei)
Cong Wang7aa00452017-10-26 18:24:28 -0700594{
Cong Wangefbf7892017-12-04 10:48:18 -0800595 struct tcf_chain *chain, *tmp;
Cong Wang1697c4b2017-09-11 16:33:32 -0700596
David S. Millerc30abd52017-12-16 22:11:55 -0500597 if (!block)
598 return;
Jiri Pirkoa9b19442018-01-17 11:46:45 +0100599 tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei);
Jiri Pirkof36fe1c2018-01-17 11:46:48 +0100600 tcf_block_owner_del(block, q, ei->binder_type);
Roman Kapla60b3f52017-11-24 12:27:58 +0100601
Jiri Pirko48617382018-01-17 11:46:46 +0100602 if (--block->refcnt == 0) {
603 if (tcf_block_shared(block))
604 tcf_block_remove(block, block->net);
605
606 /* Hold a refcnt for all chains, so that they don't disappear
607 * while we are iterating.
608 */
609 list_for_each_entry(chain, &block->chain_list, list)
610 tcf_chain_hold(chain);
611
612 list_for_each_entry(chain, &block->chain_list, list)
613 tcf_chain_flush(chain);
614 }
Cong Wang1697c4b2017-09-11 16:33:32 -0700615
Jiri Pirko4bb1b112017-11-02 15:07:01 +0100616 tcf_block_offload_unbind(block, q, ei);
617
Jiri Pirko48617382018-01-17 11:46:46 +0100618 if (block->refcnt == 0) {
619 /* At this point, all the chains should have refcnt >= 1. */
620 list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
621 tcf_chain_put(chain);
Jiri Pirkodf45bf82017-12-08 19:27:27 +0100622
Jiri Pirko48617382018-01-17 11:46:46 +0100623 /* Finally, put chain 0 and allow block to be freed. */
624 tcf_chain_put(tcf_block_chain_zero(block));
625 }
Jiri Pirko6529eab2017-05-17 11:07:55 +0200626}
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200627EXPORT_SYMBOL(tcf_block_put_ext);
628
629void tcf_block_put(struct tcf_block *block)
630{
631 struct tcf_block_ext_info ei = {0, };
632
Jiri Pirko4853f122017-12-21 13:13:59 +0100633 if (!block)
634 return;
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100635 tcf_block_put_ext(block, block->q, &ei);
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200636}
David S. Millere1ea2f92017-10-30 14:10:01 +0900637
Jiri Pirko6529eab2017-05-17 11:07:55 +0200638EXPORT_SYMBOL(tcf_block_put);
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100639
Jiri Pirkoacb67442017-10-19 15:50:31 +0200640struct tcf_block_cb {
641 struct list_head list;
642 tc_setup_cb_t *cb;
643 void *cb_ident;
644 void *cb_priv;
645 unsigned int refcnt;
646};
647
648void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
649{
650 return block_cb->cb_priv;
651}
652EXPORT_SYMBOL(tcf_block_cb_priv);
653
654struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
655 tc_setup_cb_t *cb, void *cb_ident)
656{ struct tcf_block_cb *block_cb;
657
658 list_for_each_entry(block_cb, &block->cb_list, list)
659 if (block_cb->cb == cb && block_cb->cb_ident == cb_ident)
660 return block_cb;
661 return NULL;
662}
663EXPORT_SYMBOL(tcf_block_cb_lookup);
664
665void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
666{
667 block_cb->refcnt++;
668}
669EXPORT_SYMBOL(tcf_block_cb_incref);
670
671unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
672{
673 return --block_cb->refcnt;
674}
675EXPORT_SYMBOL(tcf_block_cb_decref);
676
677struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
678 tc_setup_cb_t *cb, void *cb_ident,
679 void *cb_priv)
680{
681 struct tcf_block_cb *block_cb;
682
Jiri Pirkocaa72602018-01-17 11:46:50 +0100683 /* At this point, playback of previous block cb calls is not supported,
684 * so forbid to register to block which already has some offloaded
685 * filters present.
686 */
687 if (tcf_block_offload_in_use(block))
688 return ERR_PTR(-EOPNOTSUPP);
689
Jiri Pirkoacb67442017-10-19 15:50:31 +0200690 block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL);
691 if (!block_cb)
Jiri Pirkocaa72602018-01-17 11:46:50 +0100692 return ERR_PTR(-ENOMEM);
Jiri Pirkoacb67442017-10-19 15:50:31 +0200693 block_cb->cb = cb;
694 block_cb->cb_ident = cb_ident;
695 block_cb->cb_priv = cb_priv;
696 list_add(&block_cb->list, &block->cb_list);
697 return block_cb;
698}
699EXPORT_SYMBOL(__tcf_block_cb_register);
700
701int tcf_block_cb_register(struct tcf_block *block,
702 tc_setup_cb_t *cb, void *cb_ident,
703 void *cb_priv)
704{
705 struct tcf_block_cb *block_cb;
706
707 block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100708 return IS_ERR(block_cb) ? PTR_ERR(block_cb) : 0;
Jiri Pirkoacb67442017-10-19 15:50:31 +0200709}
710EXPORT_SYMBOL(tcf_block_cb_register);
711
712void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
713{
714 list_del(&block_cb->list);
715 kfree(block_cb);
716}
717EXPORT_SYMBOL(__tcf_block_cb_unregister);
718
719void tcf_block_cb_unregister(struct tcf_block *block,
720 tc_setup_cb_t *cb, void *cb_ident)
721{
722 struct tcf_block_cb *block_cb;
723
724 block_cb = tcf_block_cb_lookup(block, cb, cb_ident);
725 if (!block_cb)
726 return;
727 __tcf_block_cb_unregister(block_cb);
728}
729EXPORT_SYMBOL(tcf_block_cb_unregister);
730
731static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type,
732 void *type_data, bool err_stop)
733{
734 struct tcf_block_cb *block_cb;
735 int ok_count = 0;
736 int err;
737
Jiri Pirkocaa72602018-01-17 11:46:50 +0100738 /* Make sure all netdevs sharing this block are offload-capable. */
739 if (block->nooffloaddevcnt && err_stop)
740 return -EOPNOTSUPP;
741
Jiri Pirkoacb67442017-10-19 15:50:31 +0200742 list_for_each_entry(block_cb, &block->cb_list, list) {
743 err = block_cb->cb(type, type_data, block_cb->cb_priv);
744 if (err) {
745 if (err_stop)
746 return err;
747 } else {
748 ok_count++;
749 }
750 }
751 return ok_count;
752}
753
Jiri Pirko87d83092017-05-17 11:07:54 +0200754/* Main classifier routine: scans classifier chain attached
755 * to this qdisc, (optionally) tests for protocol and asks
756 * specific classifiers.
757 */
758int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
759 struct tcf_result *res, bool compat_mode)
760{
761 __be16 protocol = tc_skb_protocol(skb);
762#ifdef CONFIG_NET_CLS_ACT
763 const int max_reclassify_loop = 4;
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200764 const struct tcf_proto *orig_tp = tp;
765 const struct tcf_proto *first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200766 int limit = 0;
767
768reclassify:
769#endif
770 for (; tp; tp = rcu_dereference_bh(tp->next)) {
771 int err;
772
773 if (tp->protocol != protocol &&
774 tp->protocol != htons(ETH_P_ALL))
775 continue;
776
777 err = tp->classify(skb, tp, res);
778#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkodb505142017-05-17 11:08:03 +0200779 if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200780 first_tp = orig_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200781 goto reset;
Jiri Pirkodb505142017-05-17 11:08:03 +0200782 } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200783 first_tp = res->goto_tp;
Jiri Pirkodb505142017-05-17 11:08:03 +0200784 goto reset;
785 }
Jiri Pirko87d83092017-05-17 11:07:54 +0200786#endif
787 if (err >= 0)
788 return err;
789 }
790
791 return TC_ACT_UNSPEC; /* signal: continue lookup */
792#ifdef CONFIG_NET_CLS_ACT
793reset:
794 if (unlikely(limit++ >= max_reclassify_loop)) {
Jiri Pirko9d3aaff2018-01-17 11:46:47 +0100795 net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n",
796 tp->chain->block->index,
797 tp->prio & 0xffff,
Jiri Pirko87d83092017-05-17 11:07:54 +0200798 ntohs(tp->protocol));
799 return TC_ACT_SHOT;
800 }
801
Jiri Pirkoee538dc2017-05-23 09:11:59 +0200802 tp = first_tp;
Jiri Pirko87d83092017-05-17 11:07:54 +0200803 protocol = tc_skb_protocol(skb);
804 goto reclassify;
805#endif
806}
807EXPORT_SYMBOL(tcf_classify);
808
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200809struct tcf_chain_info {
810 struct tcf_proto __rcu **pprev;
811 struct tcf_proto __rcu *next;
812};
813
814static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
815{
816 return rtnl_dereference(*chain_info->pprev);
817}
818
819static void tcf_chain_tp_insert(struct tcf_chain *chain,
820 struct tcf_chain_info *chain_info,
821 struct tcf_proto *tp)
822{
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100823 if (*chain_info->pprev == chain->filter_chain)
824 tcf_chain_head_change(chain, tp);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200825 RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
826 rcu_assign_pointer(*chain_info->pprev, tp);
Cong Wange2ef7542017-09-11 16:33:31 -0700827 tcf_chain_hold(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200828}
829
830static void tcf_chain_tp_remove(struct tcf_chain *chain,
831 struct tcf_chain_info *chain_info,
832 struct tcf_proto *tp)
833{
834 struct tcf_proto *next = rtnl_dereference(chain_info->next);
835
Jiri Pirkoc7eb7d72017-11-03 11:46:24 +0100836 if (tp == chain->filter_chain)
837 tcf_chain_head_change(chain, next);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200838 RCU_INIT_POINTER(*chain_info->pprev, next);
Cong Wange2ef7542017-09-11 16:33:31 -0700839 tcf_chain_put(chain);
Jiri Pirko2190d1d2017-05-17 11:07:59 +0200840}
841
842static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
843 struct tcf_chain_info *chain_info,
844 u32 protocol, u32 prio,
845 bool prio_allocate)
846{
847 struct tcf_proto **pprev;
848 struct tcf_proto *tp;
849
850 /* Check the chain for existence of proto-tcf with this priority */
851 for (pprev = &chain->filter_chain;
852 (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
853 if (tp->prio >= prio) {
854 if (tp->prio == prio) {
855 if (prio_allocate ||
856 (tp->protocol != protocol && protocol))
857 return ERR_PTR(-EINVAL);
858 } else {
859 tp = NULL;
860 }
861 break;
862 }
863 }
864 chain_info->pprev = pprev;
865 chain_info->next = tp ? tp->next : NULL;
866 return tp;
867}
868
WANG Cong71203712017-08-07 15:26:50 -0700869static int tcf_fill_node(struct net *net, struct sk_buff *skb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100870 struct tcf_proto *tp, struct tcf_block *block,
871 struct Qdisc *q, u32 parent, void *fh,
872 u32 portid, u32 seq, u16 flags, int event)
WANG Cong71203712017-08-07 15:26:50 -0700873{
874 struct tcmsg *tcm;
875 struct nlmsghdr *nlh;
876 unsigned char *b = skb_tail_pointer(skb);
877
878 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
879 if (!nlh)
880 goto out_nlmsg_trim;
881 tcm = nlmsg_data(nlh);
882 tcm->tcm_family = AF_UNSPEC;
883 tcm->tcm__pad1 = 0;
884 tcm->tcm__pad2 = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100885 if (q) {
886 tcm->tcm_ifindex = qdisc_dev(q)->ifindex;
887 tcm->tcm_parent = parent;
888 } else {
889 tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK;
890 tcm->tcm_block_index = block->index;
891 }
WANG Cong71203712017-08-07 15:26:50 -0700892 tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
893 if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
894 goto nla_put_failure;
895 if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
896 goto nla_put_failure;
897 if (!fh) {
898 tcm->tcm_handle = 0;
899 } else {
900 if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
901 goto nla_put_failure;
902 }
903 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
904 return skb->len;
905
906out_nlmsg_trim:
907nla_put_failure:
908 nlmsg_trim(skb, b);
909 return -1;
910}
911
912static int tfilter_notify(struct net *net, struct sk_buff *oskb,
913 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100914 struct tcf_block *block, struct Qdisc *q,
915 u32 parent, void *fh, int event, bool unicast)
WANG Cong71203712017-08-07 15:26:50 -0700916{
917 struct sk_buff *skb;
918 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
919
920 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
921 if (!skb)
922 return -ENOBUFS;
923
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100924 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
925 n->nlmsg_seq, n->nlmsg_flags, event) <= 0) {
WANG Cong71203712017-08-07 15:26:50 -0700926 kfree_skb(skb);
927 return -EINVAL;
928 }
929
930 if (unicast)
931 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
932
933 return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
934 n->nlmsg_flags & NLM_F_ECHO);
935}
936
937static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
938 struct nlmsghdr *n, struct tcf_proto *tp,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100939 struct tcf_block *block, struct Qdisc *q,
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500940 u32 parent, void *fh, bool unicast, bool *last,
941 struct netlink_ext_ack *extack)
WANG Cong71203712017-08-07 15:26:50 -0700942{
943 struct sk_buff *skb;
944 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
945 int err;
946
947 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
948 if (!skb)
949 return -ENOBUFS;
950
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100951 if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid,
952 n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500953 NL_SET_ERR_MSG(extack, "Failed to build del event notification");
WANG Cong71203712017-08-07 15:26:50 -0700954 kfree_skb(skb);
955 return -EINVAL;
956 }
957
958 err = tp->ops->delete(tp, fh, last);
959 if (err) {
960 kfree_skb(skb);
961 return err;
962 }
963
964 if (unicast)
965 return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
966
Alexander Aringc35a4ac2018-01-18 11:20:50 -0500967 err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
968 n->nlmsg_flags & NLM_F_ECHO);
969 if (err < 0)
970 NL_SET_ERR_MSG(extack, "Failed to send filter delete notification");
971 return err;
WANG Cong71203712017-08-07 15:26:50 -0700972}
973
974static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100975 struct tcf_block *block, struct Qdisc *q,
976 u32 parent, struct nlmsghdr *n,
WANG Cong71203712017-08-07 15:26:50 -0700977 struct tcf_chain *chain, int event)
978{
979 struct tcf_proto *tp;
980
981 for (tp = rtnl_dereference(chain->filter_chain);
982 tp; tp = rtnl_dereference(tp->next))
Jiri Pirko7960d1d2018-01-17 11:46:51 +0100983 tfilter_notify(net, oskb, n, tp, block,
984 q, parent, 0, event, false);
WANG Cong71203712017-08-07 15:26:50 -0700985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/* Add/change/delete/get a filter node */
988
David Ahernc21ef3e2017-04-16 09:48:24 -0700989static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
990 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900992 struct net *net = sock_net(skb->sk);
Patrick McHardyadd93b62008-01-22 22:11:33 -0800993 struct nlattr *tca[TCA_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct tcmsg *t;
995 u32 protocol;
996 u32 prio;
Jiri Pirko9d36d9e2017-05-17 11:07:57 +0200997 bool prio_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +0200999 u32 chain_index;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001000 struct Qdisc *q = NULL;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001001 struct tcf_chain_info chain_info;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001002 struct tcf_chain *chain = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001003 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 unsigned long cl;
WANG Cong8113c092017-08-04 21:31:43 -07001006 void *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int err;
Daniel Borkmann628185c2016-12-21 18:04:11 +01001008 int tp_created;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Stéphane Graber4e8bbb82014-04-30 11:25:43 -04001010 if ((n->nlmsg_type != RTM_GETTFILTER) &&
David S. Miller5f013c9b2014-05-12 13:19:14 -04001011 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
Eric W. Biedermandfc47ef2012-11-16 03:03:00 +00001012 return -EPERM;
Hong zhi guode179c82013-03-25 17:36:33 +00001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014replay:
Daniel Borkmann628185c2016-12-21 18:04:11 +01001015 tp_created = 0;
1016
David Ahernc21ef3e2017-04-16 09:48:24 -07001017 err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
Hong zhi guode179c82013-03-25 17:36:33 +00001018 if (err < 0)
1019 return err;
1020
David S. Miller942b8162012-06-26 21:48:50 -07001021 t = nlmsg_data(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 protocol = TC_H_MIN(t->tcm_info);
1023 prio = TC_H_MAJ(t->tcm_info);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001024 prio_allocate = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 parent = t->tcm_parent;
1026 cl = 0;
1027
1028 if (prio == 0) {
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001029 switch (n->nlmsg_type) {
1030 case RTM_DELTFILTER:
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001031 if (protocol || t->tcm_handle || tca[TCA_KIND]) {
1032 NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set");
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001033 return -ENOENT;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001034 }
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001035 break;
1036 case RTM_NEWTFILTER:
1037 /* If no priority is provided by the user,
1038 * we allocate one.
1039 */
1040 if (n->nlmsg_flags & NLM_F_CREATE) {
1041 prio = TC_H_MAKE(0x80000000U, 0U);
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001042 prio_allocate = true;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001043 break;
1044 }
1045 /* fall-through */
1046 default:
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001047 NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return -ENOENT;
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051
1052 /* Find head of filter chain. */
1053
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001054 if (t->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1055 block = tcf_block_lookup(net, t->tcm_block_index);
1056 if (!block) {
1057 NL_SET_ERR_MSG(extack, "Block of given index was not found");
1058 err = -EINVAL;
1059 goto errout;
1060 }
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001061 } else {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001062 const struct Qdisc_class_ops *cops;
1063 struct net_device *dev;
1064
1065 /* Find link */
1066 dev = __dev_get_by_index(net, t->tcm_ifindex);
1067 if (!dev)
1068 return -ENODEV;
1069
1070 /* Find qdisc */
1071 if (!parent) {
1072 q = dev->qdisc;
1073 parent = q->handle;
1074 } else {
1075 q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001076 if (!q) {
1077 NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists");
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001078 return -EINVAL;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001079 }
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001080 }
1081
1082 /* Is it classful? */
1083 cops = q->ops->cl_ops;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001084 if (!cops) {
1085 NL_SET_ERR_MSG(extack, "Qdisc not classful");
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001086 return -EINVAL;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001089 if (!cops->tcf_block) {
1090 NL_SET_ERR_MSG(extack, "Class doesn't support blocks");
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001091 return -EOPNOTSUPP;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001094 /* Do we search for filter, attached to class? */
1095 if (TC_H_MIN(parent)) {
1096 cl = cops->find(q, parent);
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001097 if (cl == 0) {
1098 NL_SET_ERR_MSG(extack, "Specified class doesn't exist");
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001099 return -ENOENT;
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001100 }
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001101 }
Patrick McHardy71ebe5e2009-09-04 06:41:15 +00001102
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001103 /* And the last stroke */
1104 block = cops->tcf_block(q, cl, extack);
1105 if (!block) {
1106 err = -EINVAL;
1107 goto errout;
1108 }
1109 if (tcf_block_shared(block)) {
1110 NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters");
1111 err = -EOPNOTSUPP;
1112 goto errout;
1113 }
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001114 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001115
1116 chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
1117 if (chain_index > TC_ACT_EXT_VAL_MASK) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001118 NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit");
Jiri Pirko5bc17012017-05-17 11:08:01 +02001119 err = -EINVAL;
1120 goto errout;
1121 }
WANG Cong367a8ce2017-05-23 09:42:37 -07001122 chain = tcf_chain_get(block, chain_index,
1123 n->nlmsg_type == RTM_NEWTFILTER);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001124 if (!chain) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001125 NL_SET_ERR_MSG(extack, "Cannot find specified filter chain");
WANG Cong367a8ce2017-05-23 09:42:37 -07001126 err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001127 goto errout;
1128 }
Jiri Pirko6529eab2017-05-17 11:07:55 +02001129
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001130 if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001131 tfilter_notify_chain(net, skb, block, q, parent, n,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001132 chain, RTM_DELTFILTER);
Jiri Pirkof93e1cd2017-05-20 15:01:32 +02001133 tcf_chain_flush(chain);
Daniel Borkmannea7f8272016-06-10 23:10:22 +02001134 err = 0;
1135 goto errout;
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001138 tp = tcf_chain_tp_find(chain, &chain_info, protocol,
1139 prio, prio_allocate);
1140 if (IS_ERR(tp)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001141 NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001142 err = PTR_ERR(tp);
1143 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145
1146 if (tp == NULL) {
1147 /* Proto-tcf does not exist, create new one */
1148
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001149 if (tca[TCA_KIND] == NULL || !protocol) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001150 NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001151 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001155 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001156 !(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001157 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 +01001158 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jiri Pirko9d36d9e2017-05-17 11:07:57 +02001162 if (prio_allocate)
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001163 prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Jiri Pirko33a48922017-02-09 14:38:57 +01001165 tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001166 protocol, prio, chain, extack);
Jiri Pirko33a48922017-02-09 14:38:57 +01001167 if (IS_ERR(tp)) {
1168 err = PTR_ERR(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto errout;
1170 }
Minoru Usui12186be2009-06-02 02:17:34 -07001171 tp_created = 1;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001172 } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001173 NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001174 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 fh = tp->ops->get(tp, t->tcm_handle);
1179
WANG Cong8113c092017-08-04 21:31:43 -07001180 if (!fh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001182 tcf_chain_tp_remove(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001183 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Eric Dumazetfa59b272016-10-09 20:25:55 -07001184 RTM_DELTFILTER, false);
WANG Cong763dbf62017-04-19 14:21:21 -07001185 tcf_proto_destroy(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 err = 0;
1187 goto errout;
1188 }
1189
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001190 if (n->nlmsg_type != RTM_NEWTFILTER ||
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001191 !(n->nlmsg_flags & NLM_F_CREATE)) {
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001192 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 +01001193 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto errout;
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 } else {
WANG Cong763dbf62017-04-19 14:21:21 -07001197 bool last;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 switch (n->nlmsg_type) {
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001200 case RTM_NEWTFILTER:
Minoru Usui12186be2009-06-02 02:17:34 -07001201 if (n->nlmsg_flags & NLM_F_EXCL) {
1202 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -07001203 tcf_proto_destroy(tp);
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001204 NL_SET_ERR_MSG(extack, "Filter already exists");
Jiri Pirko6bb16e72017-02-09 14:38:58 +01001205 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 goto errout;
Minoru Usui12186be2009-06-02 02:17:34 -07001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 break;
1209 case RTM_DELTFILTER:
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001210 err = tfilter_del_notify(net, skb, n, tp, block,
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001211 q, parent, fh, false, &last,
1212 extack);
Jiri Pirko40c81b22017-02-09 14:39:00 +01001213 if (err)
1214 goto errout;
WANG Cong763dbf62017-04-19 14:21:21 -07001215 if (last) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001216 tcf_chain_tp_remove(chain, &chain_info, tp);
WANG Cong763dbf62017-04-19 14:21:21 -07001217 tcf_proto_destroy(tp);
1218 }
Jiri Pirkod7cf52c2017-02-14 16:27:13 +01001219 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case RTM_GETTFILTER:
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001221 err = tfilter_notify(net, skb, n, tp, block, q, parent,
1222 fh, RTM_NEWTFILTER, true);
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001223 if (err < 0)
1224 NL_SET_ERR_MSG(extack, "Failed to send filter notify message");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 goto errout;
1226 default:
Alexander Aringc35a4ac2018-01-18 11:20:50 -05001227 NL_SET_ERR_MSG(extack, "Invalid netlink message type");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 err = -EINVAL;
1229 goto errout;
1230 }
1231 }
1232
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001233 err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
1234 n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
Minoru Usui12186be2009-06-02 02:17:34 -07001235 if (err == 0) {
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001236 if (tp_created)
1237 tcf_chain_tp_insert(chain, &chain_info, tp);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001238 tfilter_notify(net, skb, n, tp, block, q, parent, fh,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001239 RTM_NEWTFILTER, false);
Minoru Usui12186be2009-06-02 02:17:34 -07001240 } else {
1241 if (tp_created)
WANG Cong763dbf62017-04-19 14:21:21 -07001242 tcf_proto_destroy(tp);
Minoru Usui12186be2009-06-02 02:17:34 -07001243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245errout:
Jiri Pirko5bc17012017-05-17 11:08:01 +02001246 if (chain)
1247 tcf_chain_put(chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (err == -EAGAIN)
1249 /* Replay the request. */
1250 goto replay;
1251 return err;
1252}
1253
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001254struct tcf_dump_args {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct tcf_walker w;
1256 struct sk_buff *skb;
1257 struct netlink_callback *cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001258 struct tcf_block *block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001259 struct Qdisc *q;
1260 u32 parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261};
1262
WANG Cong8113c092017-08-04 21:31:43 -07001263static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001265 struct tcf_dump_args *a = (void *)arg;
WANG Cong832d1d52014-01-09 16:14:01 -08001266 struct net *net = sock_net(a->skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001268 return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent,
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001269 n, NETLINK_CB(a->cb->skb).portid,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001270 a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
1271 RTM_NEWTFILTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001274static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent,
1275 struct sk_buff *skb, struct netlink_callback *cb,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001276 long index_start, long *p_index)
1277{
1278 struct net *net = sock_net(skb->sk);
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001279 struct tcf_block *block = chain->block;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001280 struct tcmsg *tcm = nlmsg_data(cb->nlh);
1281 struct tcf_dump_args arg;
1282 struct tcf_proto *tp;
1283
1284 for (tp = rtnl_dereference(chain->filter_chain);
1285 tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
1286 if (*p_index < index_start)
1287 continue;
1288 if (TC_H_MAJ(tcm->tcm_info) &&
1289 TC_H_MAJ(tcm->tcm_info) != tp->prio)
1290 continue;
1291 if (TC_H_MIN(tcm->tcm_info) &&
1292 TC_H_MIN(tcm->tcm_info) != tp->protocol)
1293 continue;
1294 if (*p_index > index_start)
1295 memset(&cb->args[1], 0,
1296 sizeof(cb->args) - sizeof(cb->args[0]));
1297 if (cb->args[1] == 0) {
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001298 if (tcf_fill_node(net, skb, tp, block, q, parent, 0,
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001299 NETLINK_CB(cb->skb).portid,
1300 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1301 RTM_NEWTFILTER) <= 0)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001302 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001303
1304 cb->args[1] = 1;
1305 }
1306 if (!tp->ops->walk)
1307 continue;
1308 arg.w.fn = tcf_node_dump;
1309 arg.skb = skb;
1310 arg.cb = cb;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001311 arg.block = block;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001312 arg.q = q;
1313 arg.parent = parent;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001314 arg.w.stop = 0;
1315 arg.w.skip = cb->args[1] - 1;
1316 arg.w.count = 0;
1317 tp->ops->walk(tp, &arg.w);
1318 cb->args[1] = arg.w.count + 1;
1319 if (arg.w.stop)
Jiri Pirko5bc17012017-05-17 11:08:01 +02001320 return false;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001321 }
Jiri Pirko5bc17012017-05-17 11:08:01 +02001322 return true;
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001323}
1324
Eric Dumazetbd27a872009-11-05 20:57:26 -08001325/* called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
1327{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001328 struct net *net = sock_net(skb->sk);
Jiri Pirko5bc17012017-05-17 11:08:01 +02001329 struct nlattr *tca[TCA_MAX + 1];
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001330 struct Qdisc *q = NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +02001331 struct tcf_block *block;
Jiri Pirko2190d1d2017-05-17 11:07:59 +02001332 struct tcf_chain *chain;
David S. Miller942b8162012-06-26 21:48:50 -07001333 struct tcmsg *tcm = nlmsg_data(cb->nlh);
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001334 long index_start;
1335 long index;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001336 u32 parent;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001337 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Hong zhi guo573ce262013-03-27 06:47:04 +00001339 if (nlmsg_len(cb->nlh) < sizeof(*tcm))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return skb->len;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001341
1342 err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
1343 if (err)
1344 return err;
1345
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001346 if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) {
1347 block = tcf_block_lookup(net, tcm->tcm_block_index);
1348 if (!block)
WANG Cong143976c2017-08-24 16:51:29 -07001349 goto out;
Jiri Pirkod680b352018-01-18 16:14:49 +01001350 /* If we work with block index, q is NULL and parent value
1351 * will never be used in the following code. The check
1352 * in tcf_fill_node prevents it. However, compiler does not
1353 * see that far, so set parent to zero to silence the warning
1354 * about parent being uninitialized.
1355 */
1356 parent = 0;
Jiri Pirko7960d1d2018-01-17 11:46:51 +01001357 } else {
1358 const struct Qdisc_class_ops *cops;
1359 struct net_device *dev;
1360 unsigned long cl = 0;
1361
1362 dev = __dev_get_by_index(net, tcm->tcm_ifindex);
1363 if (!dev)
1364 return skb->len;
1365
1366 parent = tcm->tcm_parent;
1367 if (!parent) {
1368 q = dev->qdisc;
1369 parent = q->handle;
1370 } else {
1371 q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
1372 }
1373 if (!q)
1374 goto out;
1375 cops = q->ops->cl_ops;
1376 if (!cops)
1377 goto out;
1378 if (!cops->tcf_block)
1379 goto out;
1380 if (TC_H_MIN(tcm->tcm_parent)) {
1381 cl = cops->find(q, tcm->tcm_parent);
1382 if (cl == 0)
1383 goto out;
1384 }
1385 block = cops->tcf_block(q, cl, NULL);
1386 if (!block)
1387 goto out;
1388 if (tcf_block_shared(block))
1389 q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001392 index_start = cb->args[0];
1393 index = 0;
Jiri Pirko5bc17012017-05-17 11:08:01 +02001394
1395 list_for_each_entry(chain, &block->chain_list, list) {
1396 if (tca[TCA_CHAIN] &&
1397 nla_get_u32(tca[TCA_CHAIN]) != chain->index)
1398 continue;
Jiri Pirkoa10fa202017-10-13 14:01:05 +02001399 if (!tcf_chain_dump(chain, q, parent, skb, cb,
1400 index_start, &index))
Jiri Pirko5bc17012017-05-17 11:08:01 +02001401 break;
1402 }
1403
Jiri Pirkoacb31fa2017-05-17 11:08:00 +02001404 cb->args[0] = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return skb->len;
1408}
1409
WANG Cong18d02642014-09-25 10:26:37 -07001410void tcf_exts_destroy(struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
1412#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001413 LIST_HEAD(actions);
1414
Cong Wang2d132eb2017-10-26 18:24:40 -07001415 ASSERT_RTNL();
WANG Cong22dc13c2016-08-13 22:35:00 -07001416 tcf_exts_to_list(exts, &actions);
1417 tcf_action_destroy(&actions, TCA_ACT_UNBIND);
1418 kfree(exts->actions);
1419 exts->nr_actions = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420#endif
1421}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001422EXPORT_SYMBOL(tcf_exts_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Benjamin LaHaisec1b52732013-01-14 05:15:39 +00001424int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001425 struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427#ifdef CONFIG_NET_CLS_ACT
1428 {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct tc_action *act;
1430
WANG Cong5da57f42013-12-15 20:15:07 -08001431 if (exts->police && tb[exts->police]) {
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001432 act = tcf_action_init_1(net, tp, tb[exts->police],
1433 rate_tlv, "police", ovr,
1434 TCA_ACT_BIND);
Patrick McHardyab27cfb2008-01-23 20:33:13 -08001435 if (IS_ERR(act))
1436 return PTR_ERR(act);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
WANG Cong33be6272013-12-15 20:15:05 -08001438 act->type = exts->type = TCA_OLD_COMPAT;
WANG Cong22dc13c2016-08-13 22:35:00 -07001439 exts->actions[0] = act;
1440 exts->nr_actions = 1;
WANG Cong5da57f42013-12-15 20:15:07 -08001441 } else if (exts->action && tb[exts->action]) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001442 LIST_HEAD(actions);
1443 int err, i = 0;
1444
Jiri Pirko9fb9f252017-05-17 11:08:02 +02001445 err = tcf_action_init(net, tp, tb[exts->action],
1446 rate_tlv, NULL, ovr, TCA_ACT_BIND,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001447 &actions);
WANG Cong33be6272013-12-15 20:15:05 -08001448 if (err)
1449 return err;
WANG Cong22dc13c2016-08-13 22:35:00 -07001450 list_for_each_entry(act, &actions, list)
1451 exts->actions[i++] = act;
1452 exts->nr_actions = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
Cong Wange4b95c42017-11-06 13:47:19 -08001454 exts->net = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456#else
WANG Cong5da57f42013-12-15 20:15:07 -08001457 if ((exts->action && tb[exts->action]) ||
1458 (exts->police && tb[exts->police]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -EOPNOTSUPP;
1460#endif
1461
1462 return 0;
1463}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001464EXPORT_SYMBOL(tcf_exts_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001466void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468#ifdef CONFIG_NET_CLS_ACT
WANG Cong22dc13c2016-08-13 22:35:00 -07001469 struct tcf_exts old = *dst;
1470
Jiri Pirko9b0d4442017-08-04 14:29:15 +02001471 *dst = *src;
WANG Cong22dc13c2016-08-13 22:35:00 -07001472 tcf_exts_destroy(&old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473#endif
1474}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001475EXPORT_SYMBOL(tcf_exts_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
WANG Cong22dc13c2016-08-13 22:35:00 -07001477#ifdef CONFIG_NET_CLS_ACT
1478static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
1479{
1480 if (exts->nr_actions == 0)
1481 return NULL;
1482 else
1483 return exts->actions[0];
1484}
1485#endif
WANG Cong33be6272013-12-15 20:15:05 -08001486
WANG Cong5da57f42013-12-15 20:15:07 -08001487int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489#ifdef CONFIG_NET_CLS_ACT
Cong Wang9cc63db2014-07-16 14:25:30 -07001490 struct nlattr *nest;
1491
Jiri Pirko978dfd82017-08-04 14:29:03 +02001492 if (exts->action && tcf_exts_has_actions(exts)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /*
1494 * again for backward compatible mode - we want
1495 * to work with both old and new modes of entering
1496 * tc data even if iproute2 was newer - jhs
1497 */
WANG Cong33be6272013-12-15 20:15:05 -08001498 if (exts->type != TCA_OLD_COMPAT) {
WANG Cong22dc13c2016-08-13 22:35:00 -07001499 LIST_HEAD(actions);
1500
WANG Cong5da57f42013-12-15 20:15:07 -08001501 nest = nla_nest_start(skb, exts->action);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001502 if (nest == NULL)
1503 goto nla_put_failure;
WANG Cong22dc13c2016-08-13 22:35:00 -07001504
1505 tcf_exts_to_list(exts, &actions);
1506 if (tcf_action_dump(skb, &actions, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001507 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001508 nla_nest_end(skb, nest);
WANG Cong5da57f42013-12-15 20:15:07 -08001509 } else if (exts->police) {
WANG Cong33be6272013-12-15 20:15:05 -08001510 struct tc_action *act = tcf_exts_first_act(exts);
WANG Cong5da57f42013-12-15 20:15:07 -08001511 nest = nla_nest_start(skb, exts->police);
Jamal Hadi Salim63acd682013-12-23 08:02:12 -05001512 if (nest == NULL || !act)
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001513 goto nla_put_failure;
WANG Cong33be6272013-12-15 20:15:05 -08001514 if (tcf_action_dump_old(skb, act, 0, 0) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001515 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001516 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
Cong Wang9cc63db2014-07-16 14:25:30 -07001520
1521nla_put_failure:
1522 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -1;
Cong Wang9cc63db2014-07-16 14:25:30 -07001524#else
1525 return 0;
1526#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001528EXPORT_SYMBOL(tcf_exts_dump);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001530
WANG Cong5da57f42013-12-15 20:15:07 -08001531int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -08001534 struct tc_action *a = tcf_exts_first_act(exts);
Ignacy Gawędzkib057df22015-02-03 19:05:18 +01001535 if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
WANG Cong33be6272013-12-15 20:15:05 -08001536 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537#endif
1538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -08001540EXPORT_SYMBOL(tcf_exts_dump_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Jiri Pirko717503b2017-10-11 09:41:09 +02001542static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts,
1543 enum tc_setup_type type,
1544 void *type_data, bool err_stop)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001545{
1546 int ok_count = 0;
1547#ifdef CONFIG_NET_CLS_ACT
1548 const struct tc_action *a;
1549 struct net_device *dev;
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001550 int i, ret;
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001551
1552 if (!tcf_exts_has_actions(exts))
1553 return 0;
1554
Or Gerlitz9d452ce2017-10-24 08:58:02 +03001555 for (i = 0; i < exts->nr_actions; i++) {
1556 a = exts->actions[i];
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001557 if (!a->ops->get_dev)
1558 continue;
1559 dev = a->ops->get_dev(a);
Jiri Pirko7612fb02017-11-01 11:47:40 +01001560 if (!dev)
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001561 continue;
1562 ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop);
1563 if (ret < 0)
1564 return ret;
1565 ok_count += ret;
1566 }
1567#endif
1568 return ok_count;
1569}
Jiri Pirko717503b2017-10-11 09:41:09 +02001570
Jiri Pirko208c0f42017-10-19 15:50:32 +02001571int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
1572 enum tc_setup_type type, void *type_data, bool err_stop)
Jiri Pirko717503b2017-10-11 09:41:09 +02001573{
Jiri Pirko208c0f42017-10-19 15:50:32 +02001574 int ok_count;
1575 int ret;
1576
1577 ret = tcf_block_cb_call(block, type, type_data, err_stop);
1578 if (ret < 0)
1579 return ret;
1580 ok_count = ret;
1581
1582 if (!exts)
1583 return ok_count;
1584 ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop);
1585 if (ret < 0)
1586 return ret;
1587 ok_count += ret;
1588
1589 return ok_count;
Jiri Pirko717503b2017-10-11 09:41:09 +02001590}
1591EXPORT_SYMBOL(tc_setup_cb_call);
Jiri Pirkob3f55bd2017-10-11 09:41:08 +02001592
Jiri Pirko48617382018-01-17 11:46:46 +01001593static __net_init int tcf_net_init(struct net *net)
1594{
1595 struct tcf_net *tn = net_generic(net, tcf_net_id);
1596
1597 idr_init(&tn->idr);
1598 return 0;
1599}
1600
1601static void __net_exit tcf_net_exit(struct net *net)
1602{
1603 struct tcf_net *tn = net_generic(net, tcf_net_id);
1604
1605 idr_destroy(&tn->idr);
1606}
1607
1608static struct pernet_operations tcf_net_ops = {
1609 .init = tcf_net_init,
1610 .exit = tcf_net_exit,
1611 .id = &tcf_net_id,
1612 .size = sizeof(struct tcf_net),
1613};
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615static int __init tc_filter_init(void)
1616{
Jiri Pirko48617382018-01-17 11:46:46 +01001617 int err;
1618
Cong Wang7aa00452017-10-26 18:24:28 -07001619 tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0);
1620 if (!tc_filter_wq)
1621 return -ENOMEM;
1622
Jiri Pirko48617382018-01-17 11:46:46 +01001623 err = register_pernet_subsys(&tcf_net_ops);
1624 if (err)
1625 goto err_register_pernet_subsys;
1626
Florian Westphalb97bac62017-08-09 20:41:48 +02001627 rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
1628 rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
Thomas Graf82623c02007-03-22 11:56:22 -07001629 rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
Florian Westphalb97bac62017-08-09 20:41:48 +02001630 tc_dump_tfilter, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return 0;
Jiri Pirko48617382018-01-17 11:46:46 +01001633
1634err_register_pernet_subsys:
1635 destroy_workqueue(tc_filter_wq);
1636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639subsys_initcall(tc_filter_init);